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