datapath: Avoid NULL mask check while building mask
[cascardo/ovs.git] / datapath / flow_netlink.c
1 /*
2  * Copyright (c) 2007-2014 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include "flow.h"
22 #include "datapath.h"
23 #include "mpls.h"
24 #include <linux/uaccess.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_vlan.h>
29 #include <net/llc_pdu.h>
30 #include <linux/kernel.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/llc.h>
34 #include <linux/module.h>
35 #include <linux/in.h>
36 #include <linux/rcupdate.h>
37 #include <linux/if_arp.h>
38 #include <linux/ip.h>
39 #include <linux/ipv6.h>
40 #include <linux/sctp.h>
41 #include <linux/tcp.h>
42 #include <linux/udp.h>
43 #include <linux/icmp.h>
44 #include <linux/icmpv6.h>
45 #include <linux/rculist.h>
46 #include <net/geneve.h>
47 #include <net/ip.h>
48 #include <net/ip_tunnels.h>
49 #include <net/ipv6.h>
50 #include <net/ndisc.h>
51
52 #include "flow_netlink.h"
53
54 static void update_range(struct sw_flow_match *match,
55                          size_t offset, size_t size, bool is_mask)
56 {
57         struct sw_flow_key_range *range;
58         size_t start = rounddown(offset, sizeof(long));
59         size_t end = roundup(offset + size, sizeof(long));
60
61         if (!is_mask)
62                 range = &match->range;
63         else
64                 range = &match->mask->range;
65
66         if (range->start == range->end) {
67                 range->start = start;
68                 range->end = end;
69                 return;
70         }
71
72         if (range->start > start)
73                 range->start = start;
74
75         if (range->end < end)
76                 range->end = end;
77 }
78
79 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
80         do { \
81                 update_range(match, offsetof(struct sw_flow_key, field),    \
82                              sizeof((match)->key->field), is_mask);         \
83                 if (is_mask)                                                \
84                         (match)->mask->key.field = value;                   \
85                 else                                                        \
86                         (match)->key->field = value;                        \
87         } while (0)
88
89 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask)     \
90         do {                                                                \
91                 update_range(match, offset, len, is_mask);                  \
92                 if (is_mask)                                                \
93                         memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
94                 else                                                        \
95                         memcpy((u8 *)(match)->key + offset, value_p, len);  \
96         } while (0)
97
98 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask)               \
99         SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
100                                   value_p, len, is_mask)
101
102 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask)              \
103         do {                                                                \
104                 update_range(match, offsetof(struct sw_flow_key, field),    \
105                              sizeof((match)->key->field), is_mask);         \
106                 if (is_mask)                                                \
107                         memset((u8 *)&(match)->mask->key.field, value,      \
108                                sizeof((match)->mask->key.field));           \
109                 else                                                        \
110                         memset((u8 *)&(match)->key->field, value,           \
111                                sizeof((match)->key->field));                \
112         } while (0)
113
114 static bool match_validate(const struct sw_flow_match *match,
115                            u64 key_attrs, u64 mask_attrs)
116 {
117         u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
118         u64 mask_allowed = key_attrs;  /* At most allow all key attributes */
119
120         /* The following mask attributes allowed only if they
121          * pass the validation tests. */
122         mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
123                         | (1ULL << OVS_KEY_ATTR_IPV6)
124                         | (1ULL << OVS_KEY_ATTR_TCP)
125                         | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
126                         | (1ULL << OVS_KEY_ATTR_UDP)
127                         | (1ULL << OVS_KEY_ATTR_SCTP)
128                         | (1ULL << OVS_KEY_ATTR_ICMP)
129                         | (1ULL << OVS_KEY_ATTR_ICMPV6)
130                         | (1ULL << OVS_KEY_ATTR_ARP)
131                         | (1ULL << OVS_KEY_ATTR_ND)
132                         | (1ULL << OVS_KEY_ATTR_MPLS));
133
134         /* Always allowed mask fields. */
135         mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
136                        | (1ULL << OVS_KEY_ATTR_IN_PORT)
137                        | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
138
139         /* Check key attributes. */
140         if (match->key->eth.type == htons(ETH_P_ARP)
141                         || match->key->eth.type == htons(ETH_P_RARP)) {
142                 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
143                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
144                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
145         }
146
147
148         if (eth_p_mpls(match->key->eth.type)) {
149                 key_expected |= 1ULL << OVS_KEY_ATTR_MPLS;
150                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
151                         mask_allowed |= 1ULL << OVS_KEY_ATTR_MPLS;
152         }
153
154         if (match->key->eth.type == htons(ETH_P_IP)) {
155                 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
156                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
157                         mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
158
159                 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
160                         if (match->key->ip.proto == IPPROTO_UDP) {
161                                 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
162                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
163                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
164                         }
165
166                         if (match->key->ip.proto == IPPROTO_SCTP) {
167                                 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
168                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
169                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
170                         }
171
172                         if (match->key->ip.proto == IPPROTO_TCP) {
173                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
174                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
175                                 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
176                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
177                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
178                                 }
179                         }
180
181                         if (match->key->ip.proto == IPPROTO_ICMP) {
182                                 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
183                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
184                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
185                         }
186                 }
187         }
188
189         if (match->key->eth.type == htons(ETH_P_IPV6)) {
190                 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
191                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
192                         mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
193
194                 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
195                         if (match->key->ip.proto == IPPROTO_UDP) {
196                                 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
197                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
198                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
199                         }
200
201                         if (match->key->ip.proto == IPPROTO_SCTP) {
202                                 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
203                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
204                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
205                         }
206
207                         if (match->key->ip.proto == IPPROTO_TCP) {
208                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
209                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
210                                 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
211                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
212                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
213                                 }
214                         }
215
216                         if (match->key->ip.proto == IPPROTO_ICMPV6) {
217                                 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
218                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
219                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
220
221                                 if (match->key->tp.src ==
222                                                 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
223                                     match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
224                                         key_expected |= 1ULL << OVS_KEY_ATTR_ND;
225                                         if (match->mask && (match->mask->key.tp.src == htons(0xff)))
226                                                 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
227                                 }
228                         }
229                 }
230         }
231
232         if ((key_attrs & key_expected) != key_expected) {
233                 /* Key attributes check failed. */
234                 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
235                                 (unsigned long long)key_attrs, (unsigned long long)key_expected);
236                 return false;
237         }
238
239         if ((mask_attrs & mask_allowed) != mask_attrs) {
240                 /* Mask attributes check failed. */
241                 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
242                                 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
243                 return false;
244         }
245
246         return true;
247 }
248
249 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
250 static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
251         [OVS_KEY_ATTR_ENCAP] = -1,
252         [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
253         [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
254         [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
255         [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
256         [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
257         [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
258         [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
259         [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
260         [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
261         [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
262         [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
263         [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
264         [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
265         [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
266         [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
267         [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
268         [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
269         [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
270         [OVS_KEY_ATTR_TUNNEL] = -1,
271         [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
272 };
273
274 static bool is_all_zero(const u8 *fp, size_t size)
275 {
276         int i;
277
278         if (!fp)
279                 return false;
280
281         for (i = 0; i < size; i++)
282                 if (fp[i])
283                         return false;
284
285         return true;
286 }
287
288 static int __parse_flow_nlattrs(const struct nlattr *attr,
289                                 const struct nlattr *a[],
290                                 u64 *attrsp, bool nz)
291 {
292         const struct nlattr *nla;
293         u64 attrs;
294         int rem;
295
296         attrs = *attrsp;
297         nla_for_each_nested(nla, attr, rem) {
298                 u16 type = nla_type(nla);
299                 int expected_len;
300
301                 if (type > OVS_KEY_ATTR_MAX) {
302                         OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
303                                   type, OVS_KEY_ATTR_MAX);
304                         return -EINVAL;
305                 }
306
307                 if (attrs & (1ULL << type)) {
308                         OVS_NLERR("Duplicate key attribute (type %d).\n", type);
309                         return -EINVAL;
310                 }
311
312                 expected_len = ovs_key_lens[type];
313                 if (nla_len(nla) != expected_len && expected_len != -1) {
314                         OVS_NLERR("Key attribute has unexpected length (type=%d"
315                                   ", length=%d, expected=%d).\n", type,
316                                   nla_len(nla), expected_len);
317                         return -EINVAL;
318                 }
319
320                 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
321                         attrs |= 1ULL << type;
322                         a[type] = nla;
323                 }
324         }
325         if (rem) {
326                 OVS_NLERR("Message has %d unknown bytes.\n", rem);
327                 return -EINVAL;
328         }
329
330         *attrsp = attrs;
331         return 0;
332 }
333
334 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
335                                    const struct nlattr *a[], u64 *attrsp)
336 {
337         return __parse_flow_nlattrs(attr, a, attrsp, true);
338 }
339
340 static int parse_flow_nlattrs(const struct nlattr *attr,
341                               const struct nlattr *a[], u64 *attrsp)
342 {
343         return __parse_flow_nlattrs(attr, a, attrsp, false);
344 }
345
346 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
347                                 struct sw_flow_match *match, bool is_mask)
348 {
349         struct nlattr *a;
350         int rem;
351         bool ttl = false;
352         __be16 tun_flags = 0;
353
354         nla_for_each_nested(a, attr, rem) {
355                 int type = nla_type(a);
356                 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
357                         [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
358                         [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
359                         [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
360                         [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
361                         [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
362                         [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
363                         [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
364                         [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
365                         [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
366                 };
367
368                 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
369                         OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
370                         type, OVS_TUNNEL_KEY_ATTR_MAX);
371                         return -EINVAL;
372                 }
373
374                 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
375                     ovs_tunnel_key_lens[type] != -1) {
376                         OVS_NLERR("IPv4 tunnel attribute type has unexpected "
377                                   " length (type=%d, length=%d, expected=%d).\n",
378                                   type, nla_len(a), ovs_tunnel_key_lens[type]);
379                         return -EINVAL;
380                 }
381
382                 switch (type) {
383                 case OVS_TUNNEL_KEY_ATTR_ID:
384                         SW_FLOW_KEY_PUT(match, tun_key.tun_id,
385                                         nla_get_be64(a), is_mask);
386                         tun_flags |= TUNNEL_KEY;
387                         break;
388                 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
389                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
390                                         nla_get_be32(a), is_mask);
391                         break;
392                 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
393                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
394                                         nla_get_be32(a), is_mask);
395                         break;
396                 case OVS_TUNNEL_KEY_ATTR_TOS:
397                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
398                                         nla_get_u8(a), is_mask);
399                         break;
400                 case OVS_TUNNEL_KEY_ATTR_TTL:
401                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
402                                         nla_get_u8(a), is_mask);
403                         ttl = true;
404                         break;
405                 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
406                         tun_flags |= TUNNEL_DONT_FRAGMENT;
407                         break;
408                 case OVS_TUNNEL_KEY_ATTR_CSUM:
409                         tun_flags |= TUNNEL_CSUM;
410                         break;
411                 case OVS_TUNNEL_KEY_ATTR_OAM:
412                         tun_flags |= TUNNEL_OAM;
413                         break;
414                 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
415                         tun_flags |= TUNNEL_OPTIONS_PRESENT;
416                         if (nla_len(a) > sizeof(match->key->tun_opts)) {
417                                 OVS_NLERR("Geneve option length exceeds "
418                                           "maximum size (len %d, max %zu).\n",
419                                           nla_len(a),
420                                           sizeof(match->key->tun_opts));
421                                 return -EINVAL;
422                         }
423
424                         if (nla_len(a) % 4 != 0) {
425                                 OVS_NLERR("Geneve option length is not "
426                                           "a multiple of 4 (len %d).\n",
427                                           nla_len(a));
428                                 return -EINVAL;
429                         }
430
431                         /* We need to record the length of the options passed
432                          * down, otherwise packets with the same format but
433                          * additional options will be silently matched.
434                          */
435                         if (!is_mask) {
436                                 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
437                                                 false);
438                         } else {
439                                 /* This is somewhat unusual because it looks at
440                                  * both the key and mask while parsing the
441                                  * attributes (and by extension assumes the key
442                                  * is parsed first). Normally, we would verify
443                                  * that each is the correct length and that the
444                                  * attributes line up in the validate function.
445                                  * However, that is difficult because this is
446                                  * variable length and we won't have the
447                                  * information later.
448                                  */
449                                 if (match->key->tun_opts_len != nla_len(a)) {
450                                         OVS_NLERR("Geneve option key length (%d)"
451                                            " is different from mask length (%d).",
452                                            match->key->tun_opts_len, nla_len(a));
453                                         return -EINVAL;
454                                 }
455
456                                 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff,
457                                                 true);
458                         }
459
460                         SW_FLOW_KEY_MEMCPY_OFFSET(match,
461                                 (unsigned long)GENEVE_OPTS((struct sw_flow_key *)0,
462                                                            nla_len(a)),
463                                 nla_data(a), nla_len(a), is_mask);
464                         break;
465                 default:
466                         OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n", type);
467                         return -EINVAL;
468                 }
469         }
470
471         SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
472
473         if (rem > 0) {
474                 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
475                 return -EINVAL;
476         }
477
478         if (!is_mask) {
479                 if (!match->key->tun_key.ipv4_dst) {
480                         OVS_NLERR("IPv4 tunnel destination address is zero.\n");
481                         return -EINVAL;
482                 }
483
484                 if (!ttl) {
485                         OVS_NLERR("IPv4 tunnel TTL not specified.\n");
486                         return -EINVAL;
487                 }
488         }
489
490         return 0;
491 }
492
493 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
494                               const struct ovs_key_ipv4_tunnel *output,
495                               const struct geneve_opt *tun_opts,
496                               int swkey_tun_opts_len)
497 {
498         struct nlattr *nla;
499
500         nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
501         if (!nla)
502                 return -EMSGSIZE;
503
504         if (output->tun_flags & TUNNEL_KEY &&
505             nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
506                 return -EMSGSIZE;
507         if (output->ipv4_src &&
508                 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
509                 return -EMSGSIZE;
510         if (output->ipv4_dst &&
511                 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
512                 return -EMSGSIZE;
513         if (output->ipv4_tos &&
514                 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
515                 return -EMSGSIZE;
516         if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
517                 return -EMSGSIZE;
518         if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
519                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
520                 return -EMSGSIZE;
521         if ((output->tun_flags & TUNNEL_CSUM) &&
522                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
523                 return -EMSGSIZE;
524         if ((output->tun_flags & TUNNEL_OAM) &&
525                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
526                 return -EMSGSIZE;
527         if (tun_opts &&
528             nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
529                     swkey_tun_opts_len, tun_opts))
530                 return -EMSGSIZE;
531
532         nla_nest_end(skb, nla);
533         return 0;
534 }
535
536
537 static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
538                                  const struct nlattr **a, bool is_mask)
539 {
540         if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
541                 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
542
543                 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
544                 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
545         }
546
547         if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
548                 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
549
550                 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
551                 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
552         }
553
554         if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
555                 SW_FLOW_KEY_PUT(match, phy.priority,
556                           nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
557                 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
558         }
559
560         if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
561                 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
562
563                 if (is_mask) {
564                         in_port = 0xffffffff; /* Always exact match in_port. */
565                 } else if (in_port >= DP_MAX_PORTS) {
566                         OVS_NLERR("Input port (%d) exceeds maximum allowable (%d).\n",
567                                   in_port, DP_MAX_PORTS);
568                         return -EINVAL;
569                 }
570
571                 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
572                 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
573         } else if (!is_mask) {
574                 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
575         }
576
577         if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
578                 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
579
580                 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
581                 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
582         }
583         if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
584                 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
585                                          is_mask))
586                         return -EINVAL;
587                 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
588         }
589         return 0;
590 }
591
592 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
593                                 const struct nlattr **a, bool is_mask)
594 {
595         int err;
596
597         err = metadata_from_nlattrs(match, &attrs, a, is_mask);
598         if (err)
599                 return err;
600
601         if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
602                 const struct ovs_key_ethernet *eth_key;
603
604                 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
605                 SW_FLOW_KEY_MEMCPY(match, eth.src,
606                                 eth_key->eth_src, ETH_ALEN, is_mask);
607                 SW_FLOW_KEY_MEMCPY(match, eth.dst,
608                                 eth_key->eth_dst, ETH_ALEN, is_mask);
609                 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
610         }
611
612         if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
613                 __be16 tci;
614
615                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
616                 if (!(tci & htons(VLAN_TAG_PRESENT))) {
617                         if (is_mask)
618                                 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
619                         else
620                                 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
621
622                         return -EINVAL;
623                 }
624
625                 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
626                 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
627         }
628
629         if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
630                 __be16 eth_type;
631
632                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
633                 if (is_mask) {
634                         /* Always exact match EtherType. */
635                         eth_type = htons(0xffff);
636                 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
637                         OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
638                                         ntohs(eth_type), ETH_P_802_3_MIN);
639                         return -EINVAL;
640                 }
641
642                 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
643                 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
644         } else if (!is_mask) {
645                 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
646         }
647
648         if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
649                 const struct ovs_key_ipv4 *ipv4_key;
650
651                 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
652                 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
653                         OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
654                                 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
655                         return -EINVAL;
656                 }
657                 SW_FLOW_KEY_PUT(match, ip.proto,
658                                 ipv4_key->ipv4_proto, is_mask);
659                 SW_FLOW_KEY_PUT(match, ip.tos,
660                                 ipv4_key->ipv4_tos, is_mask);
661                 SW_FLOW_KEY_PUT(match, ip.ttl,
662                                 ipv4_key->ipv4_ttl, is_mask);
663                 SW_FLOW_KEY_PUT(match, ip.frag,
664                                 ipv4_key->ipv4_frag, is_mask);
665                 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
666                                 ipv4_key->ipv4_src, is_mask);
667                 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
668                                 ipv4_key->ipv4_dst, is_mask);
669                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
670         }
671
672         if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
673                 const struct ovs_key_ipv6 *ipv6_key;
674
675                 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
676                 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
677                         OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
678                                 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
679                         return -EINVAL;
680                 }
681                 SW_FLOW_KEY_PUT(match, ipv6.label,
682                                 ipv6_key->ipv6_label, is_mask);
683                 SW_FLOW_KEY_PUT(match, ip.proto,
684                                 ipv6_key->ipv6_proto, is_mask);
685                 SW_FLOW_KEY_PUT(match, ip.tos,
686                                 ipv6_key->ipv6_tclass, is_mask);
687                 SW_FLOW_KEY_PUT(match, ip.ttl,
688                                 ipv6_key->ipv6_hlimit, is_mask);
689                 SW_FLOW_KEY_PUT(match, ip.frag,
690                                 ipv6_key->ipv6_frag, is_mask);
691                 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
692                                 ipv6_key->ipv6_src,
693                                 sizeof(match->key->ipv6.addr.src),
694                                 is_mask);
695                 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
696                                 ipv6_key->ipv6_dst,
697                                 sizeof(match->key->ipv6.addr.dst),
698                                 is_mask);
699
700                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
701         }
702
703         if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
704                 const struct ovs_key_arp *arp_key;
705
706                 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
707                 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
708                         OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
709                                   arp_key->arp_op);
710                         return -EINVAL;
711                 }
712
713                 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
714                                 arp_key->arp_sip, is_mask);
715                 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
716                         arp_key->arp_tip, is_mask);
717                 SW_FLOW_KEY_PUT(match, ip.proto,
718                                 ntohs(arp_key->arp_op), is_mask);
719                 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
720                                 arp_key->arp_sha, ETH_ALEN, is_mask);
721                 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
722                                 arp_key->arp_tha, ETH_ALEN, is_mask);
723
724                 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
725         }
726
727         if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) {
728                 const struct ovs_key_mpls *mpls_key;
729
730                 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
731                 SW_FLOW_KEY_PUT(match, mpls.top_lse,
732                                 mpls_key->mpls_lse, is_mask);
733
734                 attrs &= ~(1ULL << OVS_KEY_ATTR_MPLS);
735         }
736
737         if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
738                 const struct ovs_key_tcp *tcp_key;
739
740                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
741                 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
742                 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
743                 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
744         }
745
746         if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
747                 SW_FLOW_KEY_PUT(match, tp.flags,
748                                 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
749                                 is_mask);
750                 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
751         }
752
753         if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
754                 const struct ovs_key_udp *udp_key;
755
756                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
757                 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
758                 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
759                 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
760         }
761
762         if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
763                 const struct ovs_key_sctp *sctp_key;
764
765                 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
766                 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
767                 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
768                 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
769         }
770
771         if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
772                 const struct ovs_key_icmp *icmp_key;
773
774                 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
775                 SW_FLOW_KEY_PUT(match, tp.src,
776                                 htons(icmp_key->icmp_type), is_mask);
777                 SW_FLOW_KEY_PUT(match, tp.dst,
778                                 htons(icmp_key->icmp_code), is_mask);
779                 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
780         }
781
782         if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
783                 const struct ovs_key_icmpv6 *icmpv6_key;
784
785                 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
786                 SW_FLOW_KEY_PUT(match, tp.src,
787                                 htons(icmpv6_key->icmpv6_type), is_mask);
788                 SW_FLOW_KEY_PUT(match, tp.dst,
789                                 htons(icmpv6_key->icmpv6_code), is_mask);
790                 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
791         }
792
793         if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
794                 const struct ovs_key_nd *nd_key;
795
796                 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
797                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
798                         nd_key->nd_target,
799                         sizeof(match->key->ipv6.nd.target),
800                         is_mask);
801                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
802                         nd_key->nd_sll, ETH_ALEN, is_mask);
803                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
804                                 nd_key->nd_tll, ETH_ALEN, is_mask);
805                 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
806         }
807
808         if (attrs != 0) {
809                 OVS_NLERR("Unknown key attributes (%llx).\n",
810                           (unsigned long long)attrs);
811                 return -EINVAL;
812         }
813
814         return 0;
815 }
816
817 static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
818 {
819         struct nlattr *nla;
820         int rem;
821
822         /* The nlattr stream should already have been validated */
823         nla_for_each_nested(nla, attr, rem) {
824                 /* We assume that ovs_key_lens[type] == -1 means that type is a
825                  * nested attribute
826                  */
827                 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
828                         nlattr_set(nla, val, false);
829                 else
830                         memset(nla_data(nla), val, nla_len(nla));
831         }
832 }
833
834 static void mask_set_nlattr(struct nlattr *attr, u8 val)
835 {
836         nlattr_set(attr, val, true);
837 }
838
839 /**
840  * ovs_nla_get_match - parses Netlink attributes into a flow key and
841  * mask. In case the 'mask' is NULL, the flow is treated as exact match
842  * flow. Otherwise, it is treated as a wildcarded flow, except the mask
843  * does not include any don't care bit.
844  * @match: receives the extracted flow match information.
845  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
846  * sequence. The fields should of the packet that triggered the creation
847  * of this flow.
848  * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
849  * attribute specifies the mask field of the wildcarded flow.
850  */
851 int ovs_nla_get_match(struct sw_flow_match *match,
852                       const struct nlattr *nla_key,
853                       const struct nlattr *nla_mask)
854 {
855         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
856         const struct nlattr *encap;
857         struct nlattr *newmask = NULL;
858         u64 key_attrs = 0;
859         u64 mask_attrs = 0;
860         bool encap_valid = false;
861         int err;
862
863         err = parse_flow_nlattrs(nla_key, a, &key_attrs);
864         if (err)
865                 return err;
866
867         if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
868             (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
869             (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
870                 __be16 tci;
871
872                 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
873                       (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
874                         OVS_NLERR("Invalid Vlan frame.\n");
875                         return -EINVAL;
876                 }
877
878                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
879                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
880                 encap = a[OVS_KEY_ATTR_ENCAP];
881                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
882                 encap_valid = true;
883
884                 if (tci & htons(VLAN_TAG_PRESENT)) {
885                         err = parse_flow_nlattrs(encap, a, &key_attrs);
886                         if (err)
887                                 return err;
888                 } else if (!tci) {
889                         /* Corner case for truncated 802.1Q header. */
890                         if (nla_len(encap)) {
891                                 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
892                                 return -EINVAL;
893                         }
894                 } else {
895                         OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
896                         return  -EINVAL;
897                 }
898         }
899
900         err = ovs_key_from_nlattrs(match, key_attrs, a, false);
901         if (err)
902                 return err;
903
904         if (match->mask) {
905
906                 if (!nla_mask) {
907                         /* Create an exact match mask. We need to set to 0xff
908                          * all the 'match->mask' fields that have been touched
909                          * in 'match->key'. We cannot simply memset
910                          * 'match->mask', because padding bytes and fields not
911                          * specified in 'match->key' should be left to 0.
912                          * Instead, we use a stream of netlink attributes,
913                          * copied from 'key' and set to 0xff: ovs_key_from_nlattrs()
914                          * will take care of filling 'match->mask'
915                          * appropriately.
916                          */
917                         newmask = kmemdup(nla_key,
918                                           nla_total_size(nla_len(nla_key)),
919                                           GFP_KERNEL);
920                         if (!newmask)
921                                 return -ENOMEM;
922
923                         mask_set_nlattr(newmask, 0xff);
924
925                         /* The userspace does not send tunnel attributes that
926                          * are 0, but we should not wildcard them nonetheless.
927                          */
928                         if (match->key->tun_key.ipv4_dst)
929                                 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
930                                                          0xff, true);
931
932                         nla_mask = newmask;
933                 }
934
935                 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs);
936                 if (err)
937                         goto free_newmask;
938
939                 /* Always match on tci. */
940                 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
941
942                 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
943                         __be16 eth_type = 0;
944                         __be16 tci = 0;
945
946                         if (!encap_valid) {
947                                 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
948                                 err = -EINVAL;
949                                 goto free_newmask;
950                         }
951
952                         mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
953                         if (a[OVS_KEY_ATTR_ETHERTYPE])
954                                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
955
956                         if (eth_type == htons(0xffff)) {
957                                 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
958                                 encap = a[OVS_KEY_ATTR_ENCAP];
959                                 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
960                                 if (err)
961                                         goto free_newmask;
962                         } else {
963                                 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
964                                                 ntohs(eth_type));
965                                 err = -EINVAL;
966                                 goto free_newmask;
967                         }
968
969                         if (a[OVS_KEY_ATTR_VLAN])
970                                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
971
972                         if (!(tci & htons(VLAN_TAG_PRESENT))) {
973                                 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
974                                 err = -EINVAL;
975                                 goto free_newmask;
976                         }
977                 }
978
979                 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
980                 if (err)
981                         goto free_newmask;
982         }
983
984         if (!match_validate(match, key_attrs, mask_attrs))
985                 err = -EINVAL;
986
987 free_newmask:
988         kfree(newmask);
989         return err;
990 }
991
992 /**
993  * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
994  * @key: Receives extracted in_port, priority, tun_key and skb_mark.
995  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
996  * sequence.
997  *
998  * This parses a series of Netlink attributes that form a flow key, which must
999  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1000  * get the metadata, that is, the parts of the flow key that cannot be
1001  * extracted from the packet itself.
1002  */
1003 int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1004                               struct sw_flow_key *key)
1005 {
1006         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1007         struct sw_flow_match match;
1008         u64 attrs = 0;
1009         int err;
1010
1011         err = parse_flow_nlattrs(attr, a, &attrs);
1012         if (err)
1013                 return -EINVAL;
1014
1015         memset(&match, 0, sizeof(match));
1016         match.key = key;
1017
1018         memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
1019         key->phy.in_port = DP_MAX_PORTS;
1020
1021         return metadata_from_nlattrs(&match, &attrs, a, false);
1022 }
1023
1024 int ovs_nla_put_flow(struct datapath *dp, const struct sw_flow_key *swkey,
1025                      const struct sw_flow_key *output, struct sk_buff *skb)
1026 {
1027         struct ovs_key_ethernet *eth_key;
1028         struct nlattr *nla, *encap;
1029         bool is_mask = (swkey != output);
1030
1031         if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1032                 goto nla_put_failure;
1033
1034         if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1035                 goto nla_put_failure;
1036
1037         if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1038                 goto nla_put_failure;
1039
1040         if ((swkey->tun_key.ipv4_dst || is_mask)) {
1041                 const struct geneve_opt *opts = NULL;
1042
1043                 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1044                         opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1045
1046                 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1047                                         swkey->tun_opts_len))
1048                         goto nla_put_failure;
1049         }
1050
1051         if (swkey->phy.in_port == DP_MAX_PORTS) {
1052                 if (is_mask && (output->phy.in_port == 0xffff))
1053                         if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1054                                 goto nla_put_failure;
1055         } else {
1056                 u16 upper_u16;
1057                 upper_u16 = !is_mask ? 0 : 0xffff;
1058
1059                 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1060                                 (upper_u16 << 16) | output->phy.in_port))
1061                         goto nla_put_failure;
1062         }
1063
1064         if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1065                 goto nla_put_failure;
1066
1067         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1068         if (!nla)
1069                 goto nla_put_failure;
1070
1071         eth_key = nla_data(nla);
1072         ether_addr_copy(eth_key->eth_src, output->eth.src);
1073         ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1074
1075         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1076                 __be16 eth_type;
1077                 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1078                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1079                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1080                         goto nla_put_failure;
1081                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1082                 if (!swkey->eth.tci)
1083                         goto unencap;
1084         } else
1085                 encap = NULL;
1086
1087         if (swkey->eth.type == htons(ETH_P_802_2)) {
1088                 /*
1089                  * Ethertype 802.2 is represented in the netlink with omitted
1090                  * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1091                  * 0xffff in the mask attribute.  Ethertype can also
1092                  * be wildcarded.
1093                  */
1094                 if (is_mask && output->eth.type)
1095                         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1096                                                 output->eth.type))
1097                                 goto nla_put_failure;
1098                 goto unencap;
1099         }
1100
1101         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1102                 goto nla_put_failure;
1103
1104         if (swkey->eth.type == htons(ETH_P_IP)) {
1105                 struct ovs_key_ipv4 *ipv4_key;
1106
1107                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1108                 if (!nla)
1109                         goto nla_put_failure;
1110                 ipv4_key = nla_data(nla);
1111                 ipv4_key->ipv4_src = output->ipv4.addr.src;
1112                 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1113                 ipv4_key->ipv4_proto = output->ip.proto;
1114                 ipv4_key->ipv4_tos = output->ip.tos;
1115                 ipv4_key->ipv4_ttl = output->ip.ttl;
1116                 ipv4_key->ipv4_frag = output->ip.frag;
1117         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1118                 struct ovs_key_ipv6 *ipv6_key;
1119
1120                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1121                 if (!nla)
1122                         goto nla_put_failure;
1123                 ipv6_key = nla_data(nla);
1124                 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1125                                 sizeof(ipv6_key->ipv6_src));
1126                 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1127                                 sizeof(ipv6_key->ipv6_dst));
1128                 ipv6_key->ipv6_label = output->ipv6.label;
1129                 ipv6_key->ipv6_proto = output->ip.proto;
1130                 ipv6_key->ipv6_tclass = output->ip.tos;
1131                 ipv6_key->ipv6_hlimit = output->ip.ttl;
1132                 ipv6_key->ipv6_frag = output->ip.frag;
1133         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1134                    swkey->eth.type == htons(ETH_P_RARP)) {
1135                 struct ovs_key_arp *arp_key;
1136
1137                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1138                 if (!nla)
1139                         goto nla_put_failure;
1140                 arp_key = nla_data(nla);
1141                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1142                 arp_key->arp_sip = output->ipv4.addr.src;
1143                 arp_key->arp_tip = output->ipv4.addr.dst;
1144                 arp_key->arp_op = htons(output->ip.proto);
1145                 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1146                 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1147         } else if (eth_p_mpls(swkey->eth.type)) {
1148                 struct ovs_key_mpls *mpls_key;
1149
1150                 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1151                 if (!nla)
1152                         goto nla_put_failure;
1153                 mpls_key = nla_data(nla);
1154                 mpls_key->mpls_lse = output->mpls.top_lse;
1155         }
1156
1157         if ((swkey->eth.type == htons(ETH_P_IP) ||
1158              swkey->eth.type == htons(ETH_P_IPV6)) &&
1159              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1160
1161                 if (swkey->ip.proto == IPPROTO_TCP) {
1162                         struct ovs_key_tcp *tcp_key;
1163
1164                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1165                         if (!nla)
1166                                 goto nla_put_failure;
1167                         tcp_key = nla_data(nla);
1168                         tcp_key->tcp_src = output->tp.src;
1169                         tcp_key->tcp_dst = output->tp.dst;
1170                         if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1171                                          output->tp.flags))
1172                                 goto nla_put_failure;
1173                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1174                         struct ovs_key_udp *udp_key;
1175
1176                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1177                         if (!nla)
1178                                 goto nla_put_failure;
1179                         udp_key = nla_data(nla);
1180                         udp_key->udp_src = output->tp.src;
1181                         udp_key->udp_dst = output->tp.dst;
1182                 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1183                         struct ovs_key_sctp *sctp_key;
1184
1185                         nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1186                         if (!nla)
1187                                 goto nla_put_failure;
1188                         sctp_key = nla_data(nla);
1189                         sctp_key->sctp_src = output->tp.src;
1190                         sctp_key->sctp_dst = output->tp.dst;
1191                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1192                            swkey->ip.proto == IPPROTO_ICMP) {
1193                         struct ovs_key_icmp *icmp_key;
1194
1195                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1196                         if (!nla)
1197                                 goto nla_put_failure;
1198                         icmp_key = nla_data(nla);
1199                         icmp_key->icmp_type = ntohs(output->tp.src);
1200                         icmp_key->icmp_code = ntohs(output->tp.dst);
1201                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1202                            swkey->ip.proto == IPPROTO_ICMPV6) {
1203                         struct ovs_key_icmpv6 *icmpv6_key;
1204
1205                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1206                                                 sizeof(*icmpv6_key));
1207                         if (!nla)
1208                                 goto nla_put_failure;
1209                         icmpv6_key = nla_data(nla);
1210                         icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1211                         icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1212
1213                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1214                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1215                                 struct ovs_key_nd *nd_key;
1216
1217                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1218                                 if (!nla)
1219                                         goto nla_put_failure;
1220                                 nd_key = nla_data(nla);
1221                                 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1222                                                         sizeof(nd_key->nd_target));
1223                                 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1224                                 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1225                         }
1226                 }
1227         }
1228
1229 unencap:
1230         if (encap)
1231                 nla_nest_end(skb, encap);
1232
1233         return 0;
1234
1235 nla_put_failure:
1236         return -EMSGSIZE;
1237 }
1238
1239 #define MAX_ACTIONS_BUFSIZE     (32 * 1024)
1240
1241 static struct sw_flow_actions *nla_alloc_flow_actions(int size)
1242 {
1243         struct sw_flow_actions *sfa;
1244
1245         if (size > MAX_ACTIONS_BUFSIZE) {
1246                 OVS_NLERR("Flow action size (%u bytes) exceeds maximum "
1247                           "(%u bytes)\n", size, MAX_ACTIONS_BUFSIZE);
1248                 return ERR_PTR(-EINVAL);
1249         }
1250
1251         sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1252         if (!sfa)
1253                 return ERR_PTR(-ENOMEM);
1254
1255         sfa->actions_len = 0;
1256         return sfa;
1257 }
1258
1259 /* RCU callback used by ovs_nla_free_flow_actions. */
1260 static void rcu_free_acts_callback(struct rcu_head *rcu)
1261 {
1262         struct sw_flow_actions *sf_acts = container_of(rcu,
1263                         struct sw_flow_actions, rcu);
1264         kfree(sf_acts);
1265 }
1266
1267 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1268  * The caller must hold rcu_read_lock for this to be sensible. */
1269 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1270 {
1271         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1272 }
1273
1274 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1275                                        int attr_len)
1276 {
1277
1278         struct sw_flow_actions *acts;
1279         int new_acts_size;
1280         int req_size = NLA_ALIGN(attr_len);
1281         int next_offset = offsetof(struct sw_flow_actions, actions) +
1282                                         (*sfa)->actions_len;
1283
1284         if (req_size <= (ksize(*sfa) - next_offset))
1285                 goto out;
1286
1287         new_acts_size = ksize(*sfa) * 2;
1288
1289         if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1290                 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1291                         return ERR_PTR(-EMSGSIZE);
1292                 new_acts_size = MAX_ACTIONS_BUFSIZE;
1293         }
1294
1295         acts = nla_alloc_flow_actions(new_acts_size);
1296         if (IS_ERR(acts))
1297                 return (void *)acts;
1298
1299         memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1300         acts->actions_len = (*sfa)->actions_len;
1301         kfree(*sfa);
1302         *sfa = acts;
1303
1304 out:
1305         (*sfa)->actions_len += req_size;
1306         return  (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1307 }
1308
1309 static struct nlattr *__add_action(struct sw_flow_actions **sfa, int attrtype,
1310                                    void *data, int len)
1311 {
1312         struct nlattr *a;
1313
1314         a = reserve_sfa_size(sfa, nla_attr_size(len));
1315         if (IS_ERR(a))
1316                 return a;
1317
1318         a->nla_type = attrtype;
1319         a->nla_len = nla_attr_size(len);
1320
1321         if (data)
1322                 memcpy(nla_data(a), data, len);
1323         memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1324
1325         return a;
1326 }
1327
1328 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1329                       void *data, int len)
1330 {
1331         struct nlattr *a;
1332
1333         a = __add_action(sfa, attrtype, data, len);
1334         if (IS_ERR(a))
1335                 return PTR_ERR(a);
1336
1337         return 0;
1338 }
1339
1340 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1341                                           int attrtype)
1342 {
1343         int used = (*sfa)->actions_len;
1344         int err;
1345
1346         err = add_action(sfa, attrtype, NULL, 0);
1347         if (err)
1348                 return err;
1349
1350         return used;
1351 }
1352
1353 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1354                                          int st_offset)
1355 {
1356         struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1357                                                                st_offset);
1358
1359         a->nla_len = sfa->actions_len - st_offset;
1360 }
1361
1362 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1363                                   const struct sw_flow_key *key,
1364                                   int depth, struct sw_flow_actions **sfa,
1365                                   __be16 eth_type, __be16 vlan_tci);
1366
1367 static int validate_and_copy_sample(const struct nlattr *attr,
1368                                     const struct sw_flow_key *key, int depth,
1369                                     struct sw_flow_actions **sfa,
1370                                     __be16 eth_type, __be16 vlan_tci)
1371 {
1372         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1373         const struct nlattr *probability, *actions;
1374         const struct nlattr *a;
1375         int rem, start, err, st_acts;
1376
1377         memset(attrs, 0, sizeof(attrs));
1378         nla_for_each_nested(a, attr, rem) {
1379                 int type = nla_type(a);
1380                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1381                         return -EINVAL;
1382                 attrs[type] = a;
1383         }
1384         if (rem)
1385                 return -EINVAL;
1386
1387         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1388         if (!probability || nla_len(probability) != sizeof(u32))
1389                 return -EINVAL;
1390
1391         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1392         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1393                 return -EINVAL;
1394
1395         /* validation done, copy sample action. */
1396         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1397         if (start < 0)
1398                 return start;
1399         err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1400                          nla_data(probability), sizeof(u32));
1401         if (err)
1402                 return err;
1403         st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1404         if (st_acts < 0)
1405                 return st_acts;
1406
1407         err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
1408                                      eth_type, vlan_tci);
1409         if (err)
1410                 return err;
1411
1412         add_nested_action_end(*sfa, st_acts);
1413         add_nested_action_end(*sfa, start);
1414
1415         return 0;
1416 }
1417
1418 static int validate_tp_port(const struct sw_flow_key *flow_key,
1419                             __be16 eth_type)
1420 {
1421         if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1422             (flow_key->tp.src || flow_key->tp.dst))
1423                 return 0;
1424
1425         return -EINVAL;
1426 }
1427
1428 void ovs_match_init(struct sw_flow_match *match,
1429                     struct sw_flow_key *key,
1430                     struct sw_flow_mask *mask)
1431 {
1432         memset(match, 0, sizeof(*match));
1433         match->key = key;
1434         match->mask = mask;
1435
1436         memset(key, 0, sizeof(*key));
1437
1438         if (mask) {
1439                 memset(&mask->key, 0, sizeof(mask->key));
1440                 mask->range.start = mask->range.end = 0;
1441         }
1442 }
1443
1444 static int validate_and_copy_set_tun(const struct nlattr *attr,
1445                                      struct sw_flow_actions **sfa)
1446 {
1447         struct sw_flow_match match;
1448         struct sw_flow_key key;
1449         struct ovs_tunnel_info *tun_info;
1450         struct nlattr *a;
1451         int err, start;
1452
1453         ovs_match_init(&match, &key, NULL);
1454         err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1455         if (err)
1456                 return err;
1457
1458         if (key.tun_opts_len) {
1459                 struct geneve_opt *option = GENEVE_OPTS(&key,
1460                                                         key.tun_opts_len);
1461                 int opts_len = key.tun_opts_len;
1462                 bool crit_opt = false;
1463
1464                 while (opts_len > 0) {
1465                         int len;
1466
1467                         if (opts_len < sizeof(*option))
1468                                 return -EINVAL;
1469
1470                         len = sizeof(*option) + option->length * 4;
1471                         if (len > opts_len)
1472                                 return -EINVAL;
1473
1474                         crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1475
1476                         option = (struct geneve_opt *)((u8 *)option + len);
1477                         opts_len -= len;
1478                 };
1479
1480                 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1481         };
1482
1483         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1484         if (start < 0)
1485                 return start;
1486
1487         a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1488                         sizeof(*tun_info) + key.tun_opts_len);
1489         if (IS_ERR(a))
1490                 return PTR_ERR(a);
1491
1492         tun_info = nla_data(a);
1493         tun_info->tunnel = key.tun_key;
1494         tun_info->options_len = key.tun_opts_len;
1495
1496         if (tun_info->options_len) {
1497                 /* We need to store the options in the action itself since
1498                  * everything else will go away after flow setup. We can append
1499                  * it to tun_info and then point there.
1500                  */
1501                 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1502                 memcpy(tun_info->options, GENEVE_OPTS(&key, key.tun_opts_len),
1503                         key.tun_opts_len);
1504         } else {
1505                 tun_info->options = NULL;
1506         }
1507
1508         add_nested_action_end(*sfa, start);
1509
1510         return err;
1511 }
1512
1513 static int validate_set(const struct nlattr *a,
1514                         const struct sw_flow_key *flow_key,
1515                         struct sw_flow_actions **sfa,
1516                         bool *set_tun, __be16 eth_type)
1517 {
1518         const struct nlattr *ovs_key = nla_data(a);
1519         int key_type = nla_type(ovs_key);
1520
1521         /* There can be only one key in a action */
1522         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1523                 return -EINVAL;
1524
1525         if (key_type > OVS_KEY_ATTR_MAX ||
1526             (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1527              ovs_key_lens[key_type] != -1))
1528                 return -EINVAL;
1529
1530         switch (key_type) {
1531         const struct ovs_key_ipv4 *ipv4_key;
1532         const struct ovs_key_ipv6 *ipv6_key;
1533         int err;
1534
1535         case OVS_KEY_ATTR_PRIORITY:
1536         case OVS_KEY_ATTR_SKB_MARK:
1537         case OVS_KEY_ATTR_ETHERNET:
1538                 break;
1539
1540         case OVS_KEY_ATTR_TUNNEL:
1541                 *set_tun = true;
1542                 err = validate_and_copy_set_tun(a, sfa);
1543                 if (err)
1544                         return err;
1545                 break;
1546
1547         case OVS_KEY_ATTR_IPV4:
1548                 if (eth_type != htons(ETH_P_IP))
1549                         return -EINVAL;
1550
1551                 if (!flow_key->ip.proto)
1552                         return -EINVAL;
1553
1554                 ipv4_key = nla_data(ovs_key);
1555                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1556                         return -EINVAL;
1557
1558                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1559                         return -EINVAL;
1560
1561                 break;
1562
1563         case OVS_KEY_ATTR_IPV6:
1564                 if (eth_type != htons(ETH_P_IPV6))
1565                         return -EINVAL;
1566
1567                 if (!flow_key->ip.proto)
1568                         return -EINVAL;
1569
1570                 ipv6_key = nla_data(ovs_key);
1571                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1572                         return -EINVAL;
1573
1574                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1575                         return -EINVAL;
1576
1577                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1578                         return -EINVAL;
1579
1580                 break;
1581
1582         case OVS_KEY_ATTR_TCP:
1583                 if (flow_key->ip.proto != IPPROTO_TCP)
1584                         return -EINVAL;
1585
1586                 return validate_tp_port(flow_key, eth_type);
1587
1588         case OVS_KEY_ATTR_UDP:
1589                 if (flow_key->ip.proto != IPPROTO_UDP)
1590                         return -EINVAL;
1591
1592                 return validate_tp_port(flow_key, eth_type);
1593
1594         case OVS_KEY_ATTR_MPLS:
1595                 if (!eth_p_mpls(eth_type))
1596                         return -EINVAL;
1597                 break;
1598
1599         case OVS_KEY_ATTR_SCTP:
1600                 if (flow_key->ip.proto != IPPROTO_SCTP)
1601                         return -EINVAL;
1602
1603                 return validate_tp_port(flow_key, eth_type);
1604
1605         default:
1606                 return -EINVAL;
1607         }
1608
1609         return 0;
1610 }
1611
1612 static int validate_userspace(const struct nlattr *attr)
1613 {
1614         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1615                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1616                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1617         };
1618         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1619         int error;
1620
1621         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1622                                  attr, userspace_policy);
1623         if (error)
1624                 return error;
1625
1626         if (!a[OVS_USERSPACE_ATTR_PID] ||
1627             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1628                 return -EINVAL;
1629
1630         return 0;
1631 }
1632
1633 static int copy_action(const struct nlattr *from,
1634                        struct sw_flow_actions **sfa)
1635 {
1636         int totlen = NLA_ALIGN(from->nla_len);
1637         struct nlattr *to;
1638
1639         to = reserve_sfa_size(sfa, from->nla_len);
1640         if (IS_ERR(to))
1641                 return PTR_ERR(to);
1642
1643         memcpy(to, from, totlen);
1644         return 0;
1645 }
1646
1647 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1648                                   const struct sw_flow_key *key,
1649                                   int depth, struct sw_flow_actions **sfa,
1650                                   __be16 eth_type, __be16 vlan_tci)
1651 {
1652         const struct nlattr *a;
1653         int rem, err;
1654
1655         if (depth >= SAMPLE_ACTION_DEPTH)
1656                 return -EOVERFLOW;
1657
1658         nla_for_each_nested(a, attr, rem) {
1659                 /* Expected argument lengths, (u32)-1 for variable length. */
1660                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1661                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1662                         [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1663                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1664                         [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1665                         [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
1666                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1667                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
1668                         [OVS_ACTION_ATTR_SET] = (u32)-1,
1669                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1670                         [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1671                 };
1672                 const struct ovs_action_push_vlan *vlan;
1673                 int type = nla_type(a);
1674                 bool skip_copy;
1675
1676                 if (type > OVS_ACTION_ATTR_MAX ||
1677                     (action_lens[type] != nla_len(a) &&
1678                      action_lens[type] != (u32)-1))
1679                         return -EINVAL;
1680
1681                 skip_copy = false;
1682                 switch (type) {
1683                 case OVS_ACTION_ATTR_UNSPEC:
1684                         return -EINVAL;
1685
1686                 case OVS_ACTION_ATTR_USERSPACE:
1687                         err = validate_userspace(a);
1688                         if (err)
1689                                 return err;
1690                         break;
1691
1692                 case OVS_ACTION_ATTR_OUTPUT:
1693                         if (nla_get_u32(a) >= DP_MAX_PORTS)
1694                                 return -EINVAL;
1695                         break;
1696
1697                 case OVS_ACTION_ATTR_HASH: {
1698                         const struct ovs_action_hash *act_hash = nla_data(a);
1699
1700                         switch (act_hash->hash_alg) {
1701                         case OVS_HASH_ALG_L4:
1702                                 break;
1703                         default:
1704                                 return  -EINVAL;
1705                         }
1706
1707                         break;
1708                 }
1709
1710                 case OVS_ACTION_ATTR_POP_VLAN:
1711                         vlan_tci = htons(0);
1712                         break;
1713
1714                 case OVS_ACTION_ATTR_PUSH_VLAN:
1715                         vlan = nla_data(a);
1716                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1717                                 return -EINVAL;
1718                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1719                                 return -EINVAL;
1720                         vlan_tci = vlan->vlan_tci;
1721                         break;
1722
1723                 case OVS_ACTION_ATTR_RECIRC:
1724                         break;
1725
1726                 case OVS_ACTION_ATTR_PUSH_MPLS: {
1727                         const struct ovs_action_push_mpls *mpls = nla_data(a);
1728
1729                         if (!eth_p_mpls(mpls->mpls_ethertype))
1730                                 return -EINVAL;
1731                         /* Prohibit push MPLS other than to a white list
1732                          * for packets that have a known tag order.
1733                          */
1734                         if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1735                             (eth_type != htons(ETH_P_IP) &&
1736                              eth_type != htons(ETH_P_IPV6) &&
1737                              eth_type != htons(ETH_P_ARP) &&
1738                              eth_type != htons(ETH_P_RARP) &&
1739                              !eth_p_mpls(eth_type)))
1740                                 return -EINVAL;
1741                         eth_type = mpls->mpls_ethertype;
1742                         break;
1743                 }
1744
1745                 case OVS_ACTION_ATTR_POP_MPLS:
1746                         if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1747                             !eth_p_mpls(eth_type))
1748                                 return -EINVAL;
1749
1750                         /* Disallow subsequent L2.5+ set and mpls_pop actions
1751                          * as there is no check here to ensure that the new
1752                          * eth_type is valid and thus set actions could
1753                          * write off the end of the packet or otherwise
1754                          * corrupt it.
1755                          *
1756                          * Support for these actions is planned using packet
1757                          * recirculation.
1758                          */
1759                         eth_type = htons(0);
1760                         break;
1761
1762                 case OVS_ACTION_ATTR_SET:
1763                         err = validate_set(a, key, sfa, &skip_copy, eth_type);
1764                         if (err)
1765                                 return err;
1766                         break;
1767
1768                 case OVS_ACTION_ATTR_SAMPLE:
1769                         err = validate_and_copy_sample(a, key, depth, sfa,
1770                                                        eth_type, vlan_tci);
1771                         if (err)
1772                                 return err;
1773                         skip_copy = true;
1774                         break;
1775
1776                 default:
1777                         return -EINVAL;
1778                 }
1779                 if (!skip_copy) {
1780                         err = copy_action(a, sfa);
1781                         if (err)
1782                                 return err;
1783                 }
1784         }
1785
1786         if (rem > 0)
1787                 return -EINVAL;
1788
1789         return 0;
1790 }
1791
1792 int ovs_nla_copy_actions(const struct nlattr *attr,
1793                          const struct sw_flow_key *key,
1794                          struct sw_flow_actions **sfa)
1795 {
1796         int err;
1797
1798         *sfa = nla_alloc_flow_actions(nla_len(attr));
1799         if (IS_ERR(*sfa))
1800                 return PTR_ERR(*sfa);
1801
1802         err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
1803                                      key->eth.tci);
1804         if (err)
1805                 kfree(*sfa);
1806
1807         return err;
1808 }
1809
1810 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1811 {
1812         const struct nlattr *a;
1813         struct nlattr *start;
1814         int err = 0, rem;
1815
1816         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1817         if (!start)
1818                 return -EMSGSIZE;
1819
1820         nla_for_each_nested(a, attr, rem) {
1821                 int type = nla_type(a);
1822                 struct nlattr *st_sample;
1823
1824                 switch (type) {
1825                 case OVS_SAMPLE_ATTR_PROBABILITY:
1826                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1827                                     sizeof(u32), nla_data(a)))
1828                                 return -EMSGSIZE;
1829                         break;
1830                 case OVS_SAMPLE_ATTR_ACTIONS:
1831                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1832                         if (!st_sample)
1833                                 return -EMSGSIZE;
1834                         err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1835                         if (err)
1836                                 return err;
1837                         nla_nest_end(skb, st_sample);
1838                         break;
1839                 }
1840         }
1841
1842         nla_nest_end(skb, start);
1843         return err;
1844 }
1845
1846 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1847 {
1848         const struct nlattr *ovs_key = nla_data(a);
1849         int key_type = nla_type(ovs_key);
1850         struct nlattr *start;
1851         int err;
1852
1853         switch (key_type) {
1854         case OVS_KEY_ATTR_TUNNEL_INFO: {
1855                 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1856
1857                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1858                 if (!start)
1859                         return -EMSGSIZE;
1860
1861                 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
1862                                          tun_info->options_len ?
1863                                                 tun_info->options : NULL,
1864                                          tun_info->options_len);
1865                 if (err)
1866                         return err;
1867                 nla_nest_end(skb, start);
1868                 break;
1869         }
1870         default:
1871                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1872                         return -EMSGSIZE;
1873                 break;
1874         }
1875
1876         return 0;
1877 }
1878
1879 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1880 {
1881         const struct nlattr *a;
1882         int rem, err;
1883
1884         nla_for_each_attr(a, attr, len, rem) {
1885                 int type = nla_type(a);
1886
1887                 switch (type) {
1888                 case OVS_ACTION_ATTR_SET:
1889                         err = set_action_to_attr(a, skb);
1890                         if (err)
1891                                 return err;
1892                         break;
1893
1894                 case OVS_ACTION_ATTR_SAMPLE:
1895                         err = sample_action_to_attr(a, skb);
1896                         if (err)
1897                                 return err;
1898                         break;
1899                 default:
1900                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1901                                 return -EMSGSIZE;
1902                         break;
1903                 }
1904         }
1905
1906         return 0;
1907 }