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