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