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