datapath/flow_netlink: Fix NDP flow mask validation
[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 = NULL;
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 if (match->mask)
64                 range = &match->mask->range;
65
66         if (!range)
67                 return;
68
69         if (range->start == range->end) {
70                 range->start = start;
71                 range->end = end;
72                 return;
73         }
74
75         if (range->start > start)
76                 range->start = start;
77
78         if (range->end < end)
79                 range->end = end;
80 }
81
82 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
83         do { \
84                 update_range__(match, offsetof(struct sw_flow_key, field),  \
85                                      sizeof((match)->key->field), is_mask); \
86                 if (is_mask) {                                              \
87                         if ((match)->mask)                                  \
88                                 (match)->mask->key.field = value;           \
89                 } else {                                                    \
90                         (match)->key->field = value;                        \
91                 }                                                           \
92         } while (0)
93
94 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
95         do { \
96                 update_range__(match, offset, len, is_mask);                \
97                 if (is_mask) {                                              \
98                         if ((match)->mask)                                  \
99                                 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
100                 } else {                                                    \
101                         memcpy((u8 *)(match)->key + offset, value_p, len);         \
102                 }                                                           \
103         } while (0)
104
105 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
106         SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
107                                   value_p, len, is_mask)
108
109 static u16 range_n_bytes(const struct sw_flow_key_range *range)
110 {
111         return range->end - range->start;
112 }
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                         if (nla_len(a) > sizeof(match->key->tun_opts)) {
416                                 OVS_NLERR("Geneve option length exceeds "
417                                           "maximum size (len %d, max %zu).\n",
418                                           nla_len(a),
419                                           sizeof(match->key->tun_opts));
420                                 return -EINVAL;
421                         }
422
423                         if (nla_len(a) % 4 != 0) {
424                                 OVS_NLERR("Geneve option length is not "
425                                           "a multiple of 4 (len %d).\n",
426                                           nla_len(a));
427                                 return -EINVAL;
428                         }
429
430                         /* We need to record the length of the options passed
431                          * down, otherwise packets with the same format but
432                          * additional options will be silently matched.
433                          */
434                         if (!is_mask) {
435                                 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
436                                                 false);
437                         } else {
438                                 /* This is somewhat unusual because it looks at
439                                  * both the key and mask while parsing the
440                                  * attributes (and by extension assumes the key
441                                  * is parsed first). Normally, we would verify
442                                  * that each is the correct length and that the
443                                  * attributes line up in the validate function.
444                                  * However, that is difficult because this is
445                                  * variable length and we won't have the
446                                  * information later.
447                                  */
448                                 if (match->key->tun_opts_len != nla_len(a)) {
449                                         OVS_NLERR("Geneve option key length (%d)"
450                                            " is different from mask length (%d).",
451                                            match->key->tun_opts_len, nla_len(a));
452                                         return -EINVAL;
453                                 }
454
455                                 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff,
456                                                 true);
457                         }
458
459                         SW_FLOW_KEY_MEMCPY_OFFSET(match,
460                                 (unsigned long)GENEVE_OPTS((struct sw_flow_key *)0,
461                                                            nla_len(a)),
462                                 nla_data(a), nla_len(a), is_mask);
463                         break;
464                 default:
465                         OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n", type);
466                         return -EINVAL;
467                 }
468         }
469
470         SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
471
472         if (rem > 0) {
473                 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
474                 return -EINVAL;
475         }
476
477         if (!is_mask) {
478                 if (!match->key->tun_key.ipv4_dst) {
479                         OVS_NLERR("IPv4 tunnel destination address is zero.\n");
480                         return -EINVAL;
481                 }
482
483                 if (!ttl) {
484                         OVS_NLERR("IPv4 tunnel TTL not specified.\n");
485                         return -EINVAL;
486                 }
487         }
488
489         return 0;
490 }
491
492 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
493                               const struct ovs_key_ipv4_tunnel *output,
494                               const struct geneve_opt *tun_opts,
495                               int swkey_tun_opts_len)
496 {
497         struct nlattr *nla;
498
499         nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
500         if (!nla)
501                 return -EMSGSIZE;
502
503         if (output->tun_flags & TUNNEL_KEY &&
504             nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
505                 return -EMSGSIZE;
506         if (output->ipv4_src &&
507                 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
508                 return -EMSGSIZE;
509         if (output->ipv4_dst &&
510                 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
511                 return -EMSGSIZE;
512         if (output->ipv4_tos &&
513                 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
514                 return -EMSGSIZE;
515         if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
516                 return -EMSGSIZE;
517         if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
518                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
519                 return -EMSGSIZE;
520         if ((output->tun_flags & TUNNEL_CSUM) &&
521                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
522                 return -EMSGSIZE;
523         if ((output->tun_flags & TUNNEL_OAM) &&
524                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
525                 return -EMSGSIZE;
526         if (tun_opts &&
527             nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
528                     swkey_tun_opts_len, tun_opts))
529                 return -EMSGSIZE;
530
531         nla_nest_end(skb, nla);
532         return 0;
533 }
534
535
536 static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
537                                  const struct nlattr **a, bool is_mask)
538 {
539         if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
540                 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
541
542                 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
543                 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
544         }
545
546         if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
547                 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
548
549                 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
550                 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
551         }
552
553         if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
554                 SW_FLOW_KEY_PUT(match, phy.priority,
555                           nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
556                 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
557         }
558
559         if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
560                 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
561
562                 if (is_mask) {
563                         in_port = 0xffffffff; /* Always exact match in_port. */
564                 } else if (in_port >= DP_MAX_PORTS) {
565                         OVS_NLERR("Input port (%d) exceeds maximum allowable (%d).\n",
566                                   in_port, DP_MAX_PORTS);
567                         return -EINVAL;
568                 }
569
570                 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
571                 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
572         } else if (!is_mask) {
573                 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
574         }
575
576         if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
577                 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
578
579                 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
580                 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
581         }
582         if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
583                 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
584                                          is_mask))
585                         return -EINVAL;
586                 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
587         }
588         return 0;
589 }
590
591 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
592                                 const struct nlattr **a, bool is_mask)
593 {
594         int err;
595
596         err = metadata_from_nlattrs(match, &attrs, a, is_mask);
597         if (err)
598                 return err;
599
600         if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
601                 const struct ovs_key_ethernet *eth_key;
602
603                 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
604                 SW_FLOW_KEY_MEMCPY(match, eth.src,
605                                 eth_key->eth_src, ETH_ALEN, is_mask);
606                 SW_FLOW_KEY_MEMCPY(match, eth.dst,
607                                 eth_key->eth_dst, ETH_ALEN, is_mask);
608                 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
609         }
610
611         if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
612                 __be16 tci;
613
614                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
615                 if (!(tci & htons(VLAN_TAG_PRESENT))) {
616                         if (is_mask)
617                                 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
618                         else
619                                 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
620
621                         return -EINVAL;
622                 }
623
624                 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
625                 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
626         } else if (!is_mask)
627                 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
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 sw_flow_mask_set(struct sw_flow_mask *mask,
818                              struct sw_flow_key_range *range, u8 val)
819 {
820         u8 *m = (u8 *)&mask->key + range->start;
821
822         mask->range = *range;
823         memset(m, val, range_n_bytes(range));
824 }
825
826 /**
827  * ovs_nla_get_match - parses Netlink attributes into a flow key and
828  * mask. In case the 'mask' is NULL, the flow is treated as exact match
829  * flow. Otherwise, it is treated as a wildcarded flow, except the mask
830  * does not include any don't care bit.
831  * @match: receives the extracted flow match information.
832  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
833  * sequence. The fields should of the packet that triggered the creation
834  * of this flow.
835  * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
836  * attribute specifies the mask field of the wildcarded flow.
837  */
838 int ovs_nla_get_match(struct sw_flow_match *match,
839                       const struct nlattr *key,
840                       const struct nlattr *mask)
841 {
842         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
843         const struct nlattr *encap;
844         u64 key_attrs = 0;
845         u64 mask_attrs = 0;
846         bool encap_valid = false;
847         int err;
848
849         err = parse_flow_nlattrs(key, a, &key_attrs);
850         if (err)
851                 return err;
852
853         if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
854             (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
855             (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
856                 __be16 tci;
857
858                 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
859                       (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
860                         OVS_NLERR("Invalid Vlan frame.\n");
861                         return -EINVAL;
862                 }
863
864                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
865                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
866                 encap = a[OVS_KEY_ATTR_ENCAP];
867                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
868                 encap_valid = true;
869
870                 if (tci & htons(VLAN_TAG_PRESENT)) {
871                         err = parse_flow_nlattrs(encap, a, &key_attrs);
872                         if (err)
873                                 return err;
874                 } else if (!tci) {
875                         /* Corner case for truncated 802.1Q header. */
876                         if (nla_len(encap)) {
877                                 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
878                                 return -EINVAL;
879                         }
880                 } else {
881                         OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
882                         return  -EINVAL;
883                 }
884         }
885
886         err = ovs_key_from_nlattrs(match, key_attrs, a, false);
887         if (err)
888                 return err;
889
890         if (mask) {
891                 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
892                 if (err)
893                         return err;
894
895                 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP)  {
896                         __be16 eth_type = 0;
897                         __be16 tci = 0;
898
899                         if (!encap_valid) {
900                                 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
901                                 return  -EINVAL;
902                         }
903
904                         mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
905                         if (a[OVS_KEY_ATTR_ETHERTYPE])
906                                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
907
908                         if (eth_type == htons(0xffff)) {
909                                 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
910                                 encap = a[OVS_KEY_ATTR_ENCAP];
911                                 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
912                         } else {
913                                 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
914                                                 ntohs(eth_type));
915                                 return -EINVAL;
916                         }
917
918                         if (a[OVS_KEY_ATTR_VLAN])
919                                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
920
921                         if (!(tci & htons(VLAN_TAG_PRESENT))) {
922                                 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
923                                 return -EINVAL;
924                         }
925                 }
926
927                 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
928                 if (err)
929                         return err;
930         } else {
931                 /* Populate exact match flow's key mask. */
932                 if (match->mask)
933                         sw_flow_mask_set(match->mask, &match->range, 0xff);
934         }
935
936         if (!match_validate(match, key_attrs, mask_attrs))
937                 return -EINVAL;
938
939         return 0;
940 }
941
942 /**
943  * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
944  * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
945  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
946  * sequence.
947  *
948  * This parses a series of Netlink attributes that form a flow key, which must
949  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
950  * get the metadata, that is, the parts of the flow key that cannot be
951  * extracted from the packet itself.
952  */
953
954 int ovs_nla_get_flow_metadata(struct sw_flow *flow,
955                               const struct nlattr *attr)
956 {
957         struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
958         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
959         u64 attrs = 0;
960         int err;
961         struct sw_flow_match match;
962
963         flow->key.phy.in_port = DP_MAX_PORTS;
964         flow->key.phy.priority = 0;
965         flow->key.phy.skb_mark = 0;
966         flow->key.ovs_flow_hash = 0;
967         flow->key.recirc_id = 0;
968         memset(tun_key, 0, sizeof(flow->key.tun_key));
969
970         err = parse_flow_nlattrs(attr, a, &attrs);
971         if (err)
972                 return -EINVAL;
973
974         memset(&match, 0, sizeof(match));
975         match.key = &flow->key;
976
977         err = metadata_from_nlattrs(&match, &attrs, a, false);
978         if (err)
979                 return err;
980
981         return 0;
982 }
983
984 int ovs_nla_put_flow(struct datapath *dp, const struct sw_flow_key *swkey,
985                      const struct sw_flow_key *output, struct sk_buff *skb)
986 {
987         struct ovs_key_ethernet *eth_key;
988         struct nlattr *nla, *encap;
989         bool is_mask = (swkey != output);
990
991         if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
992                 goto nla_put_failure;
993
994         if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
995                 goto nla_put_failure;
996
997         if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
998                 goto nla_put_failure;
999
1000         if ((swkey->tun_key.ipv4_dst || is_mask)) {
1001                 const struct geneve_opt *opts = NULL;
1002
1003                 if (!is_mask) {
1004                         struct vport *in_port;
1005
1006                         in_port = ovs_vport_ovsl_rcu(dp, swkey->phy.in_port);
1007                         if (in_port->ops->type == OVS_VPORT_TYPE_GENEVE)
1008                                 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1009                 } else {
1010                         if (output->tun_opts_len)
1011                                 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1012                 }
1013
1014                 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1015                                         swkey->tun_opts_len))
1016                         goto nla_put_failure;
1017         }
1018
1019         if (swkey->phy.in_port == DP_MAX_PORTS) {
1020                 if (is_mask && (output->phy.in_port == 0xffff))
1021                         if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1022                                 goto nla_put_failure;
1023         } else {
1024                 u16 upper_u16;
1025                 upper_u16 = !is_mask ? 0 : 0xffff;
1026
1027                 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1028                                 (upper_u16 << 16) | output->phy.in_port))
1029                         goto nla_put_failure;
1030         }
1031
1032         if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1033                 goto nla_put_failure;
1034
1035         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1036         if (!nla)
1037                 goto nla_put_failure;
1038
1039         eth_key = nla_data(nla);
1040         ether_addr_copy(eth_key->eth_src, output->eth.src);
1041         ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1042
1043         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1044                 __be16 eth_type;
1045                 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1046                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1047                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1048                         goto nla_put_failure;
1049                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1050                 if (!swkey->eth.tci)
1051                         goto unencap;
1052         } else
1053                 encap = NULL;
1054
1055         if (swkey->eth.type == htons(ETH_P_802_2)) {
1056                 /*
1057                  * Ethertype 802.2 is represented in the netlink with omitted
1058                  * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1059                  * 0xffff in the mask attribute.  Ethertype can also
1060                  * be wildcarded.
1061                  */
1062                 if (is_mask && output->eth.type)
1063                         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1064                                                 output->eth.type))
1065                                 goto nla_put_failure;
1066                 goto unencap;
1067         }
1068
1069         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1070                 goto nla_put_failure;
1071
1072         if (swkey->eth.type == htons(ETH_P_IP)) {
1073                 struct ovs_key_ipv4 *ipv4_key;
1074
1075                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1076                 if (!nla)
1077                         goto nla_put_failure;
1078                 ipv4_key = nla_data(nla);
1079                 ipv4_key->ipv4_src = output->ipv4.addr.src;
1080                 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1081                 ipv4_key->ipv4_proto = output->ip.proto;
1082                 ipv4_key->ipv4_tos = output->ip.tos;
1083                 ipv4_key->ipv4_ttl = output->ip.ttl;
1084                 ipv4_key->ipv4_frag = output->ip.frag;
1085         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1086                 struct ovs_key_ipv6 *ipv6_key;
1087
1088                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1089                 if (!nla)
1090                         goto nla_put_failure;
1091                 ipv6_key = nla_data(nla);
1092                 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1093                                 sizeof(ipv6_key->ipv6_src));
1094                 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1095                                 sizeof(ipv6_key->ipv6_dst));
1096                 ipv6_key->ipv6_label = output->ipv6.label;
1097                 ipv6_key->ipv6_proto = output->ip.proto;
1098                 ipv6_key->ipv6_tclass = output->ip.tos;
1099                 ipv6_key->ipv6_hlimit = output->ip.ttl;
1100                 ipv6_key->ipv6_frag = output->ip.frag;
1101         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1102                    swkey->eth.type == htons(ETH_P_RARP)) {
1103                 struct ovs_key_arp *arp_key;
1104
1105                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1106                 if (!nla)
1107                         goto nla_put_failure;
1108                 arp_key = nla_data(nla);
1109                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1110                 arp_key->arp_sip = output->ipv4.addr.src;
1111                 arp_key->arp_tip = output->ipv4.addr.dst;
1112                 arp_key->arp_op = htons(output->ip.proto);
1113                 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1114                 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1115         } else if (eth_p_mpls(swkey->eth.type)) {
1116                 struct ovs_key_mpls *mpls_key;
1117
1118                 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1119                 if (!nla)
1120                         goto nla_put_failure;
1121                 mpls_key = nla_data(nla);
1122                 mpls_key->mpls_lse = output->mpls.top_lse;
1123         }
1124
1125         if ((swkey->eth.type == htons(ETH_P_IP) ||
1126              swkey->eth.type == htons(ETH_P_IPV6)) &&
1127              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1128
1129                 if (swkey->ip.proto == IPPROTO_TCP) {
1130                         struct ovs_key_tcp *tcp_key;
1131
1132                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1133                         if (!nla)
1134                                 goto nla_put_failure;
1135                         tcp_key = nla_data(nla);
1136                         tcp_key->tcp_src = output->tp.src;
1137                         tcp_key->tcp_dst = output->tp.dst;
1138                         if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1139                                          output->tp.flags))
1140                                 goto nla_put_failure;
1141                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1142                         struct ovs_key_udp *udp_key;
1143
1144                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1145                         if (!nla)
1146                                 goto nla_put_failure;
1147                         udp_key = nla_data(nla);
1148                         udp_key->udp_src = output->tp.src;
1149                         udp_key->udp_dst = output->tp.dst;
1150                 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1151                         struct ovs_key_sctp *sctp_key;
1152
1153                         nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1154                         if (!nla)
1155                                 goto nla_put_failure;
1156                         sctp_key = nla_data(nla);
1157                         sctp_key->sctp_src = output->tp.src;
1158                         sctp_key->sctp_dst = output->tp.dst;
1159                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1160                            swkey->ip.proto == IPPROTO_ICMP) {
1161                         struct ovs_key_icmp *icmp_key;
1162
1163                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1164                         if (!nla)
1165                                 goto nla_put_failure;
1166                         icmp_key = nla_data(nla);
1167                         icmp_key->icmp_type = ntohs(output->tp.src);
1168                         icmp_key->icmp_code = ntohs(output->tp.dst);
1169                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1170                            swkey->ip.proto == IPPROTO_ICMPV6) {
1171                         struct ovs_key_icmpv6 *icmpv6_key;
1172
1173                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1174                                                 sizeof(*icmpv6_key));
1175                         if (!nla)
1176                                 goto nla_put_failure;
1177                         icmpv6_key = nla_data(nla);
1178                         icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1179                         icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1180
1181                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1182                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1183                                 struct ovs_key_nd *nd_key;
1184
1185                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1186                                 if (!nla)
1187                                         goto nla_put_failure;
1188                                 nd_key = nla_data(nla);
1189                                 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1190                                                         sizeof(nd_key->nd_target));
1191                                 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1192                                 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1193                         }
1194                 }
1195         }
1196
1197 unencap:
1198         if (encap)
1199                 nla_nest_end(skb, encap);
1200
1201         return 0;
1202
1203 nla_put_failure:
1204         return -EMSGSIZE;
1205 }
1206
1207 #define MAX_ACTIONS_BUFSIZE     (32 * 1024)
1208
1209 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1210 {
1211         struct sw_flow_actions *sfa;
1212
1213         if (size > MAX_ACTIONS_BUFSIZE) {
1214                 OVS_NLERR("Flow action size (%u bytes) exceeds maximum "
1215                           "(%u bytes)\n", size, MAX_ACTIONS_BUFSIZE);
1216                 return ERR_PTR(-EINVAL);
1217         }
1218
1219         sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1220         if (!sfa)
1221                 return ERR_PTR(-ENOMEM);
1222
1223         sfa->actions_len = 0;
1224         return sfa;
1225 }
1226
1227 /* RCU callback used by ovs_nla_free_flow_actions. */
1228 static void rcu_free_acts_callback(struct rcu_head *rcu)
1229 {
1230         struct sw_flow_actions *sf_acts = container_of(rcu,
1231                         struct sw_flow_actions, rcu);
1232         kfree(sf_acts);
1233 }
1234
1235 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1236  * The caller must hold rcu_read_lock for this to be sensible. */
1237 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1238 {
1239         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1240 }
1241
1242 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1243                                        int attr_len)
1244 {
1245
1246         struct sw_flow_actions *acts;
1247         int new_acts_size;
1248         int req_size = NLA_ALIGN(attr_len);
1249         int next_offset = offsetof(struct sw_flow_actions, actions) +
1250                                         (*sfa)->actions_len;
1251
1252         if (req_size <= (ksize(*sfa) - next_offset))
1253                 goto out;
1254
1255         new_acts_size = ksize(*sfa) * 2;
1256
1257         if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1258                 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1259                         return ERR_PTR(-EMSGSIZE);
1260                 new_acts_size = MAX_ACTIONS_BUFSIZE;
1261         }
1262
1263         acts = ovs_nla_alloc_flow_actions(new_acts_size);
1264         if (IS_ERR(acts))
1265                 return (void *)acts;
1266
1267         memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1268         acts->actions_len = (*sfa)->actions_len;
1269         kfree(*sfa);
1270         *sfa = acts;
1271
1272 out:
1273         (*sfa)->actions_len += req_size;
1274         return  (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1275 }
1276
1277 static struct nlattr *__add_action(struct sw_flow_actions **sfa, int attrtype,
1278                                    void *data, int len)
1279 {
1280         struct nlattr *a;
1281
1282         a = reserve_sfa_size(sfa, nla_attr_size(len));
1283         if (IS_ERR(a))
1284                 return a;
1285
1286         a->nla_type = attrtype;
1287         a->nla_len = nla_attr_size(len);
1288
1289         if (data)
1290                 memcpy(nla_data(a), data, len);
1291         memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1292
1293         return a;
1294 }
1295
1296 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1297                       void *data, int len)
1298 {
1299         struct nlattr *a;
1300
1301         a = __add_action(sfa, attrtype, data, len);
1302         if (IS_ERR(a))
1303                 return PTR_ERR(a);
1304
1305         return 0;
1306 }
1307
1308 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1309                                           int attrtype)
1310 {
1311         int used = (*sfa)->actions_len;
1312         int err;
1313
1314         err = add_action(sfa, attrtype, NULL, 0);
1315         if (err)
1316                 return err;
1317
1318         return used;
1319 }
1320
1321 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1322                                          int st_offset)
1323 {
1324         struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1325                                                                st_offset);
1326
1327         a->nla_len = sfa->actions_len - st_offset;
1328 }
1329
1330 static int ovs_nla_copy_actions__(const struct nlattr *attr,
1331                                   const struct sw_flow_key *key,
1332                                   int depth, struct sw_flow_actions **sfa,
1333                                   __be16 eth_type, __be16 vlan_tci);
1334
1335 static int validate_and_copy_sample(const struct nlattr *attr,
1336                                     const struct sw_flow_key *key, int depth,
1337                                     struct sw_flow_actions **sfa,
1338                                     __be16 eth_type, __be16 vlan_tci)
1339 {
1340         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1341         const struct nlattr *probability, *actions;
1342         const struct nlattr *a;
1343         int rem, start, err, st_acts;
1344
1345         memset(attrs, 0, sizeof(attrs));
1346         nla_for_each_nested(a, attr, rem) {
1347                 int type = nla_type(a);
1348                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1349                         return -EINVAL;
1350                 attrs[type] = a;
1351         }
1352         if (rem)
1353                 return -EINVAL;
1354
1355         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1356         if (!probability || nla_len(probability) != sizeof(u32))
1357                 return -EINVAL;
1358
1359         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1360         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1361                 return -EINVAL;
1362
1363         /* validation done, copy sample action. */
1364         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1365         if (start < 0)
1366                 return start;
1367         err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1368                          nla_data(probability), sizeof(u32));
1369         if (err)
1370                 return err;
1371         st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1372         if (st_acts < 0)
1373                 return st_acts;
1374
1375         err = ovs_nla_copy_actions__(actions, key, depth + 1, sfa,
1376                                      eth_type, vlan_tci);
1377         if (err)
1378                 return err;
1379
1380         add_nested_action_end(*sfa, st_acts);
1381         add_nested_action_end(*sfa, start);
1382
1383         return 0;
1384 }
1385
1386 static int validate_tp_port(const struct sw_flow_key *flow_key,
1387                             __be16 eth_type)
1388 {
1389         if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1390             (flow_key->tp.src || flow_key->tp.dst))
1391                 return 0;
1392
1393         return -EINVAL;
1394 }
1395
1396 void ovs_match_init(struct sw_flow_match *match,
1397                     struct sw_flow_key *key,
1398                     struct sw_flow_mask *mask)
1399 {
1400         memset(match, 0, sizeof(*match));
1401         match->key = key;
1402         match->mask = mask;
1403
1404         memset(key, 0, sizeof(*key));
1405
1406         if (mask) {
1407                 memset(&mask->key, 0, sizeof(mask->key));
1408                 mask->range.start = mask->range.end = 0;
1409         }
1410 }
1411
1412 static int validate_and_copy_set_tun(const struct nlattr *attr,
1413                                      struct sw_flow_actions **sfa)
1414 {
1415         struct sw_flow_match match;
1416         struct sw_flow_key key;
1417         struct ovs_tunnel_info *tun_info;
1418         struct nlattr *a;
1419         int err, start;
1420
1421         ovs_match_init(&match, &key, NULL);
1422         err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1423         if (err)
1424                 return err;
1425
1426         if (key.tun_opts_len) {
1427                 struct geneve_opt *option = GENEVE_OPTS(&key,
1428                                                         key.tun_opts_len);
1429                 int opts_len = key.tun_opts_len;
1430                 bool crit_opt = false;
1431
1432                 while (opts_len > 0) {
1433                         int len;
1434
1435                         if (opts_len < sizeof(*option))
1436                                 return -EINVAL;
1437
1438                         len = sizeof(*option) + option->length * 4;
1439                         if (len > opts_len)
1440                                 return -EINVAL;
1441
1442                         crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1443
1444                         option = (struct geneve_opt *)((u8 *)option + len);
1445                         opts_len -= len;
1446                 };
1447
1448                 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1449         };
1450
1451         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1452         if (start < 0)
1453                 return start;
1454
1455         a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1456                         sizeof(*tun_info) + key.tun_opts_len);
1457         if (IS_ERR(a))
1458                 return PTR_ERR(a);
1459
1460         tun_info = nla_data(a);
1461         tun_info->tunnel = key.tun_key;
1462         tun_info->options_len = key.tun_opts_len;
1463
1464         if (tun_info->options_len) {
1465                 /* We need to store the options in the action itself since
1466                  * everything else will go away after flow setup. We can append
1467                  * it to tun_info and then point there.
1468                  */
1469                 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1470                 memcpy(tun_info->options, GENEVE_OPTS(&key, key.tun_opts_len),
1471                         key.tun_opts_len);
1472         } else {
1473                 tun_info->options = NULL;
1474         }
1475
1476         add_nested_action_end(*sfa, start);
1477
1478         return err;
1479 }
1480
1481 static int validate_set(const struct nlattr *a,
1482                         const struct sw_flow_key *flow_key,
1483                         struct sw_flow_actions **sfa,
1484                         bool *set_tun, __be16 eth_type)
1485 {
1486         const struct nlattr *ovs_key = nla_data(a);
1487         int key_type = nla_type(ovs_key);
1488
1489         /* There can be only one key in a action */
1490         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1491                 return -EINVAL;
1492
1493         if (key_type > OVS_KEY_ATTR_MAX ||
1494             (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1495              ovs_key_lens[key_type] != -1))
1496                 return -EINVAL;
1497
1498         switch (key_type) {
1499         const struct ovs_key_ipv4 *ipv4_key;
1500         const struct ovs_key_ipv6 *ipv6_key;
1501         int err;
1502
1503         case OVS_KEY_ATTR_PRIORITY:
1504         case OVS_KEY_ATTR_SKB_MARK:
1505         case OVS_KEY_ATTR_ETHERNET:
1506                 break;
1507
1508         case OVS_KEY_ATTR_TUNNEL:
1509                 *set_tun = true;
1510                 err = validate_and_copy_set_tun(a, sfa);
1511                 if (err)
1512                         return err;
1513                 break;
1514
1515         case OVS_KEY_ATTR_IPV4:
1516                 if (eth_type != htons(ETH_P_IP))
1517                         return -EINVAL;
1518
1519                 if (!flow_key->ip.proto)
1520                         return -EINVAL;
1521
1522                 ipv4_key = nla_data(ovs_key);
1523                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1524                         return -EINVAL;
1525
1526                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1527                         return -EINVAL;
1528
1529                 break;
1530
1531         case OVS_KEY_ATTR_IPV6:
1532                 if (eth_type != htons(ETH_P_IPV6))
1533                         return -EINVAL;
1534
1535                 if (!flow_key->ip.proto)
1536                         return -EINVAL;
1537
1538                 ipv6_key = nla_data(ovs_key);
1539                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1540                         return -EINVAL;
1541
1542                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1543                         return -EINVAL;
1544
1545                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1546                         return -EINVAL;
1547
1548                 break;
1549
1550         case OVS_KEY_ATTR_TCP:
1551                 if (flow_key->ip.proto != IPPROTO_TCP)
1552                         return -EINVAL;
1553
1554                 return validate_tp_port(flow_key, eth_type);
1555
1556         case OVS_KEY_ATTR_UDP:
1557                 if (flow_key->ip.proto != IPPROTO_UDP)
1558                         return -EINVAL;
1559
1560                 return validate_tp_port(flow_key, eth_type);
1561
1562         case OVS_KEY_ATTR_MPLS:
1563                 if (!eth_p_mpls(eth_type))
1564                         return -EINVAL;
1565                 break;
1566
1567         case OVS_KEY_ATTR_SCTP:
1568                 if (flow_key->ip.proto != IPPROTO_SCTP)
1569                         return -EINVAL;
1570
1571                 return validate_tp_port(flow_key, eth_type);
1572
1573         default:
1574                 return -EINVAL;
1575         }
1576
1577         return 0;
1578 }
1579
1580 static int validate_userspace(const struct nlattr *attr)
1581 {
1582         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1583                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1584                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1585         };
1586         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1587         int error;
1588
1589         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1590                                  attr, userspace_policy);
1591         if (error)
1592                 return error;
1593
1594         if (!a[OVS_USERSPACE_ATTR_PID] ||
1595             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1596                 return -EINVAL;
1597
1598         return 0;
1599 }
1600
1601 static int copy_action(const struct nlattr *from,
1602                        struct sw_flow_actions **sfa)
1603 {
1604         int totlen = NLA_ALIGN(from->nla_len);
1605         struct nlattr *to;
1606
1607         to = reserve_sfa_size(sfa, from->nla_len);
1608         if (IS_ERR(to))
1609                 return PTR_ERR(to);
1610
1611         memcpy(to, from, totlen);
1612         return 0;
1613 }
1614
1615 static int ovs_nla_copy_actions__(const struct nlattr *attr,
1616                                   const struct sw_flow_key *key,
1617                                   int depth, struct sw_flow_actions **sfa,
1618                                   __be16 eth_type, __be16 vlan_tci)
1619 {
1620         const struct nlattr *a;
1621         int rem, err;
1622
1623         if (depth >= SAMPLE_ACTION_DEPTH)
1624                 return -EOVERFLOW;
1625
1626         nla_for_each_nested(a, attr, rem) {
1627                 /* Expected argument lengths, (u32)-1 for variable length. */
1628                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1629                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1630                         [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1631                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1632                         [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1633                         [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
1634                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1635                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
1636                         [OVS_ACTION_ATTR_SET] = (u32)-1,
1637                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1638                         [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1639                 };
1640                 const struct ovs_action_push_vlan *vlan;
1641                 int type = nla_type(a);
1642                 bool skip_copy;
1643
1644                 if (type > OVS_ACTION_ATTR_MAX ||
1645                     (action_lens[type] != nla_len(a) &&
1646                      action_lens[type] != (u32)-1))
1647                         return -EINVAL;
1648
1649                 skip_copy = false;
1650                 switch (type) {
1651                 case OVS_ACTION_ATTR_UNSPEC:
1652                         return -EINVAL;
1653
1654                 case OVS_ACTION_ATTR_USERSPACE:
1655                         err = validate_userspace(a);
1656                         if (err)
1657                                 return err;
1658                         break;
1659
1660                 case OVS_ACTION_ATTR_OUTPUT:
1661                         if (nla_get_u32(a) >= DP_MAX_PORTS)
1662                                 return -EINVAL;
1663                         break;
1664
1665                 case OVS_ACTION_ATTR_HASH: {
1666                         const struct ovs_action_hash *act_hash = nla_data(a);
1667
1668                         switch (act_hash->hash_alg) {
1669                         case OVS_HASH_ALG_L4:
1670                                 break;
1671                         default:
1672                                 return  -EINVAL;
1673                         }
1674
1675                         break;
1676                 }
1677
1678                 case OVS_ACTION_ATTR_POP_VLAN:
1679                         vlan_tci = htons(0);
1680                         break;
1681
1682                 case OVS_ACTION_ATTR_PUSH_VLAN:
1683                         vlan = nla_data(a);
1684                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1685                                 return -EINVAL;
1686                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1687                                 return -EINVAL;
1688                         vlan_tci = vlan->vlan_tci;
1689                         break;
1690
1691                 case OVS_ACTION_ATTR_RECIRC:
1692                         break;
1693
1694                 case OVS_ACTION_ATTR_PUSH_MPLS: {
1695                         const struct ovs_action_push_mpls *mpls = nla_data(a);
1696
1697                         if (!eth_p_mpls(mpls->mpls_ethertype))
1698                                 return -EINVAL;
1699                         /* Prohibit push MPLS other than to a white list
1700                          * for packets that have a known tag order.
1701                          */
1702                         if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1703                             (eth_type != htons(ETH_P_IP) &&
1704                              eth_type != htons(ETH_P_IPV6) &&
1705                              eth_type != htons(ETH_P_ARP) &&
1706                              eth_type != htons(ETH_P_RARP) &&
1707                              !eth_p_mpls(eth_type)))
1708                                 return -EINVAL;
1709                         eth_type = mpls->mpls_ethertype;
1710                         break;
1711                 }
1712
1713                 case OVS_ACTION_ATTR_POP_MPLS:
1714                         if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1715                             !eth_p_mpls(eth_type))
1716                                 return -EINVAL;
1717
1718                         /* Disallow subsequent L2.5+ set and mpls_pop actions
1719                          * as there is no check here to ensure that the new
1720                          * eth_type is valid and thus set actions could
1721                          * write off the end of the packet or otherwise
1722                          * corrupt it.
1723                          *
1724                          * Support for these actions is planned using packet
1725                          * recirculation.
1726                          */
1727                         eth_type = htons(0);
1728                         break;
1729
1730                 case OVS_ACTION_ATTR_SET:
1731                         err = validate_set(a, key, sfa, &skip_copy, eth_type);
1732                         if (err)
1733                                 return err;
1734                         break;
1735
1736                 case OVS_ACTION_ATTR_SAMPLE:
1737                         err = validate_and_copy_sample(a, key, depth, sfa,
1738                                                        eth_type, vlan_tci);
1739                         if (err)
1740                                 return err;
1741                         skip_copy = true;
1742                         break;
1743
1744                 default:
1745                         return -EINVAL;
1746                 }
1747                 if (!skip_copy) {
1748                         err = copy_action(a, sfa);
1749                         if (err)
1750                                 return err;
1751                 }
1752         }
1753
1754         if (rem > 0)
1755                 return -EINVAL;
1756
1757         return 0;
1758 }
1759
1760 int ovs_nla_copy_actions(const struct nlattr *attr,
1761                          const struct sw_flow_key *key,
1762                          struct sw_flow_actions **sfa)
1763 {
1764         return ovs_nla_copy_actions__(attr, key, 0, sfa, key->eth.type,
1765                                       key->eth.tci);
1766 }
1767
1768 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1769 {
1770         const struct nlattr *a;
1771         struct nlattr *start;
1772         int err = 0, rem;
1773
1774         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1775         if (!start)
1776                 return -EMSGSIZE;
1777
1778         nla_for_each_nested(a, attr, rem) {
1779                 int type = nla_type(a);
1780                 struct nlattr *st_sample;
1781
1782                 switch (type) {
1783                 case OVS_SAMPLE_ATTR_PROBABILITY:
1784                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1785                                     sizeof(u32), nla_data(a)))
1786                                 return -EMSGSIZE;
1787                         break;
1788                 case OVS_SAMPLE_ATTR_ACTIONS:
1789                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1790                         if (!st_sample)
1791                                 return -EMSGSIZE;
1792                         err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1793                         if (err)
1794                                 return err;
1795                         nla_nest_end(skb, st_sample);
1796                         break;
1797                 }
1798         }
1799
1800         nla_nest_end(skb, start);
1801         return err;
1802 }
1803
1804 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1805 {
1806         const struct nlattr *ovs_key = nla_data(a);
1807         int key_type = nla_type(ovs_key);
1808         struct nlattr *start;
1809         int err;
1810
1811         switch (key_type) {
1812         case OVS_KEY_ATTR_TUNNEL_INFO: {
1813                 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1814
1815                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1816                 if (!start)
1817                         return -EMSGSIZE;
1818
1819                 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
1820                                          tun_info->options_len ?
1821                                                 tun_info->options : NULL,
1822                                          tun_info->options_len);
1823                 if (err)
1824                         return err;
1825                 nla_nest_end(skb, start);
1826                 break;
1827         }
1828         default:
1829                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1830                         return -EMSGSIZE;
1831                 break;
1832         }
1833
1834         return 0;
1835 }
1836
1837 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1838 {
1839         const struct nlattr *a;
1840         int rem, err;
1841
1842         nla_for_each_attr(a, attr, len, rem) {
1843                 int type = nla_type(a);
1844
1845                 switch (type) {
1846                 case OVS_ACTION_ATTR_SET:
1847                         err = set_action_to_attr(a, skb);
1848                         if (err)
1849                                 return err;
1850                         break;
1851
1852                 case OVS_ACTION_ATTR_SAMPLE:
1853                         err = sample_action_to_attr(a, skb);
1854                         if (err)
1855                                 return err;
1856                         break;
1857                 default:
1858                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1859                                 return -EMSGSIZE;
1860                         break;
1861                 }
1862         }
1863
1864         return 0;
1865 }