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