datapath: Avoid using wrong metadata for recic action.
[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         memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
1020         key->phy.in_port = DP_MAX_PORTS;
1021
1022         return metadata_from_nlattrs(&match, &attrs, a, false);
1023 }
1024
1025 int ovs_nla_put_flow(struct datapath *dp, const struct sw_flow_key *swkey,
1026                      const struct sw_flow_key *output, struct sk_buff *skb)
1027 {
1028         struct ovs_key_ethernet *eth_key;
1029         struct nlattr *nla, *encap;
1030         bool is_mask = (swkey != output);
1031
1032         if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1033                 goto nla_put_failure;
1034
1035         if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1036                 goto nla_put_failure;
1037
1038         if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1039                 goto nla_put_failure;
1040
1041         if ((swkey->tun_key.ipv4_dst || is_mask)) {
1042                 const struct geneve_opt *opts = NULL;
1043
1044                 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1045                         opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1046
1047                 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1048                                         swkey->tun_opts_len))
1049                         goto nla_put_failure;
1050         }
1051
1052         if (swkey->phy.in_port == DP_MAX_PORTS) {
1053                 if (is_mask && (output->phy.in_port == 0xffff))
1054                         if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1055                                 goto nla_put_failure;
1056         } else {
1057                 u16 upper_u16;
1058                 upper_u16 = !is_mask ? 0 : 0xffff;
1059
1060                 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1061                                 (upper_u16 << 16) | output->phy.in_port))
1062                         goto nla_put_failure;
1063         }
1064
1065         if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1066                 goto nla_put_failure;
1067
1068         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1069         if (!nla)
1070                 goto nla_put_failure;
1071
1072         eth_key = nla_data(nla);
1073         ether_addr_copy(eth_key->eth_src, output->eth.src);
1074         ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1075
1076         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1077                 __be16 eth_type;
1078                 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1079                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1080                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1081                         goto nla_put_failure;
1082                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1083                 if (!swkey->eth.tci)
1084                         goto unencap;
1085         } else
1086                 encap = NULL;
1087
1088         if (swkey->eth.type == htons(ETH_P_802_2)) {
1089                 /*
1090                  * Ethertype 802.2 is represented in the netlink with omitted
1091                  * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1092                  * 0xffff in the mask attribute.  Ethertype can also
1093                  * be wildcarded.
1094                  */
1095                 if (is_mask && output->eth.type)
1096                         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1097                                                 output->eth.type))
1098                                 goto nla_put_failure;
1099                 goto unencap;
1100         }
1101
1102         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1103                 goto nla_put_failure;
1104
1105         if (swkey->eth.type == htons(ETH_P_IP)) {
1106                 struct ovs_key_ipv4 *ipv4_key;
1107
1108                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1109                 if (!nla)
1110                         goto nla_put_failure;
1111                 ipv4_key = nla_data(nla);
1112                 ipv4_key->ipv4_src = output->ipv4.addr.src;
1113                 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1114                 ipv4_key->ipv4_proto = output->ip.proto;
1115                 ipv4_key->ipv4_tos = output->ip.tos;
1116                 ipv4_key->ipv4_ttl = output->ip.ttl;
1117                 ipv4_key->ipv4_frag = output->ip.frag;
1118         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1119                 struct ovs_key_ipv6 *ipv6_key;
1120
1121                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1122                 if (!nla)
1123                         goto nla_put_failure;
1124                 ipv6_key = nla_data(nla);
1125                 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1126                                 sizeof(ipv6_key->ipv6_src));
1127                 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1128                                 sizeof(ipv6_key->ipv6_dst));
1129                 ipv6_key->ipv6_label = output->ipv6.label;
1130                 ipv6_key->ipv6_proto = output->ip.proto;
1131                 ipv6_key->ipv6_tclass = output->ip.tos;
1132                 ipv6_key->ipv6_hlimit = output->ip.ttl;
1133                 ipv6_key->ipv6_frag = output->ip.frag;
1134         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1135                    swkey->eth.type == htons(ETH_P_RARP)) {
1136                 struct ovs_key_arp *arp_key;
1137
1138                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1139                 if (!nla)
1140                         goto nla_put_failure;
1141                 arp_key = nla_data(nla);
1142                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1143                 arp_key->arp_sip = output->ipv4.addr.src;
1144                 arp_key->arp_tip = output->ipv4.addr.dst;
1145                 arp_key->arp_op = htons(output->ip.proto);
1146                 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1147                 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1148         } else if (eth_p_mpls(swkey->eth.type)) {
1149                 struct ovs_key_mpls *mpls_key;
1150
1151                 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1152                 if (!nla)
1153                         goto nla_put_failure;
1154                 mpls_key = nla_data(nla);
1155                 mpls_key->mpls_lse = output->mpls.top_lse;
1156         }
1157
1158         if ((swkey->eth.type == htons(ETH_P_IP) ||
1159              swkey->eth.type == htons(ETH_P_IPV6)) &&
1160              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1161
1162                 if (swkey->ip.proto == IPPROTO_TCP) {
1163                         struct ovs_key_tcp *tcp_key;
1164
1165                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1166                         if (!nla)
1167                                 goto nla_put_failure;
1168                         tcp_key = nla_data(nla);
1169                         tcp_key->tcp_src = output->tp.src;
1170                         tcp_key->tcp_dst = output->tp.dst;
1171                         if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1172                                          output->tp.flags))
1173                                 goto nla_put_failure;
1174                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1175                         struct ovs_key_udp *udp_key;
1176
1177                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1178                         if (!nla)
1179                                 goto nla_put_failure;
1180                         udp_key = nla_data(nla);
1181                         udp_key->udp_src = output->tp.src;
1182                         udp_key->udp_dst = output->tp.dst;
1183                 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1184                         struct ovs_key_sctp *sctp_key;
1185
1186                         nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1187                         if (!nla)
1188                                 goto nla_put_failure;
1189                         sctp_key = nla_data(nla);
1190                         sctp_key->sctp_src = output->tp.src;
1191                         sctp_key->sctp_dst = output->tp.dst;
1192                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1193                            swkey->ip.proto == IPPROTO_ICMP) {
1194                         struct ovs_key_icmp *icmp_key;
1195
1196                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1197                         if (!nla)
1198                                 goto nla_put_failure;
1199                         icmp_key = nla_data(nla);
1200                         icmp_key->icmp_type = ntohs(output->tp.src);
1201                         icmp_key->icmp_code = ntohs(output->tp.dst);
1202                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1203                            swkey->ip.proto == IPPROTO_ICMPV6) {
1204                         struct ovs_key_icmpv6 *icmpv6_key;
1205
1206                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1207                                                 sizeof(*icmpv6_key));
1208                         if (!nla)
1209                                 goto nla_put_failure;
1210                         icmpv6_key = nla_data(nla);
1211                         icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1212                         icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1213
1214                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1215                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1216                                 struct ovs_key_nd *nd_key;
1217
1218                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1219                                 if (!nla)
1220                                         goto nla_put_failure;
1221                                 nd_key = nla_data(nla);
1222                                 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1223                                                         sizeof(nd_key->nd_target));
1224                                 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1225                                 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1226                         }
1227                 }
1228         }
1229
1230 unencap:
1231         if (encap)
1232                 nla_nest_end(skb, encap);
1233
1234         return 0;
1235
1236 nla_put_failure:
1237         return -EMSGSIZE;
1238 }
1239
1240 #define MAX_ACTIONS_BUFSIZE     (32 * 1024)
1241
1242 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1243 {
1244         struct sw_flow_actions *sfa;
1245
1246         if (size > MAX_ACTIONS_BUFSIZE) {
1247                 OVS_NLERR("Flow action size (%u bytes) exceeds maximum "
1248                           "(%u bytes)\n", size, MAX_ACTIONS_BUFSIZE);
1249                 return ERR_PTR(-EINVAL);
1250         }
1251
1252         sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1253         if (!sfa)
1254                 return ERR_PTR(-ENOMEM);
1255
1256         sfa->actions_len = 0;
1257         return sfa;
1258 }
1259
1260 /* RCU callback used by ovs_nla_free_flow_actions. */
1261 static void rcu_free_acts_callback(struct rcu_head *rcu)
1262 {
1263         struct sw_flow_actions *sf_acts = container_of(rcu,
1264                         struct sw_flow_actions, rcu);
1265         kfree(sf_acts);
1266 }
1267
1268 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1269  * The caller must hold rcu_read_lock for this to be sensible. */
1270 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1271 {
1272         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1273 }
1274
1275 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1276                                        int attr_len)
1277 {
1278
1279         struct sw_flow_actions *acts;
1280         int new_acts_size;
1281         int req_size = NLA_ALIGN(attr_len);
1282         int next_offset = offsetof(struct sw_flow_actions, actions) +
1283                                         (*sfa)->actions_len;
1284
1285         if (req_size <= (ksize(*sfa) - next_offset))
1286                 goto out;
1287
1288         new_acts_size = ksize(*sfa) * 2;
1289
1290         if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1291                 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1292                         return ERR_PTR(-EMSGSIZE);
1293                 new_acts_size = MAX_ACTIONS_BUFSIZE;
1294         }
1295
1296         acts = ovs_nla_alloc_flow_actions(new_acts_size);
1297         if (IS_ERR(acts))
1298                 return (void *)acts;
1299
1300         memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1301         acts->actions_len = (*sfa)->actions_len;
1302         kfree(*sfa);
1303         *sfa = acts;
1304
1305 out:
1306         (*sfa)->actions_len += req_size;
1307         return  (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1308 }
1309
1310 static struct nlattr *__add_action(struct sw_flow_actions **sfa, int attrtype,
1311                                    void *data, int len)
1312 {
1313         struct nlattr *a;
1314
1315         a = reserve_sfa_size(sfa, nla_attr_size(len));
1316         if (IS_ERR(a))
1317                 return a;
1318
1319         a->nla_type = attrtype;
1320         a->nla_len = nla_attr_size(len);
1321
1322         if (data)
1323                 memcpy(nla_data(a), data, len);
1324         memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1325
1326         return a;
1327 }
1328
1329 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1330                       void *data, int len)
1331 {
1332         struct nlattr *a;
1333
1334         a = __add_action(sfa, attrtype, data, len);
1335         if (IS_ERR(a))
1336                 return PTR_ERR(a);
1337
1338         return 0;
1339 }
1340
1341 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1342                                           int attrtype)
1343 {
1344         int used = (*sfa)->actions_len;
1345         int err;
1346
1347         err = add_action(sfa, attrtype, NULL, 0);
1348         if (err)
1349                 return err;
1350
1351         return used;
1352 }
1353
1354 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1355                                          int st_offset)
1356 {
1357         struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1358                                                                st_offset);
1359
1360         a->nla_len = sfa->actions_len - st_offset;
1361 }
1362
1363 static int ovs_nla_copy_actions__(const struct nlattr *attr,
1364                                   const struct sw_flow_key *key,
1365                                   int depth, struct sw_flow_actions **sfa,
1366                                   __be16 eth_type, __be16 vlan_tci);
1367
1368 static int validate_and_copy_sample(const struct nlattr *attr,
1369                                     const struct sw_flow_key *key, int depth,
1370                                     struct sw_flow_actions **sfa,
1371                                     __be16 eth_type, __be16 vlan_tci)
1372 {
1373         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1374         const struct nlattr *probability, *actions;
1375         const struct nlattr *a;
1376         int rem, start, err, st_acts;
1377
1378         memset(attrs, 0, sizeof(attrs));
1379         nla_for_each_nested(a, attr, rem) {
1380                 int type = nla_type(a);
1381                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1382                         return -EINVAL;
1383                 attrs[type] = a;
1384         }
1385         if (rem)
1386                 return -EINVAL;
1387
1388         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1389         if (!probability || nla_len(probability) != sizeof(u32))
1390                 return -EINVAL;
1391
1392         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1393         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1394                 return -EINVAL;
1395
1396         /* validation done, copy sample action. */
1397         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1398         if (start < 0)
1399                 return start;
1400         err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1401                          nla_data(probability), sizeof(u32));
1402         if (err)
1403                 return err;
1404         st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1405         if (st_acts < 0)
1406                 return st_acts;
1407
1408         err = ovs_nla_copy_actions__(actions, key, depth + 1, sfa,
1409                                      eth_type, vlan_tci);
1410         if (err)
1411                 return err;
1412
1413         add_nested_action_end(*sfa, st_acts);
1414         add_nested_action_end(*sfa, start);
1415
1416         return 0;
1417 }
1418
1419 static int validate_tp_port(const struct sw_flow_key *flow_key,
1420                             __be16 eth_type)
1421 {
1422         if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1423             (flow_key->tp.src || flow_key->tp.dst))
1424                 return 0;
1425
1426         return -EINVAL;
1427 }
1428
1429 void ovs_match_init(struct sw_flow_match *match,
1430                     struct sw_flow_key *key,
1431                     struct sw_flow_mask *mask)
1432 {
1433         memset(match, 0, sizeof(*match));
1434         match->key = key;
1435         match->mask = mask;
1436
1437         memset(key, 0, sizeof(*key));
1438
1439         if (mask) {
1440                 memset(&mask->key, 0, sizeof(mask->key));
1441                 mask->range.start = mask->range.end = 0;
1442         }
1443 }
1444
1445 static int validate_and_copy_set_tun(const struct nlattr *attr,
1446                                      struct sw_flow_actions **sfa)
1447 {
1448         struct sw_flow_match match;
1449         struct sw_flow_key key;
1450         struct ovs_tunnel_info *tun_info;
1451         struct nlattr *a;
1452         int err, start;
1453
1454         ovs_match_init(&match, &key, NULL);
1455         err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1456         if (err)
1457                 return err;
1458
1459         if (key.tun_opts_len) {
1460                 struct geneve_opt *option = GENEVE_OPTS(&key,
1461                                                         key.tun_opts_len);
1462                 int opts_len = key.tun_opts_len;
1463                 bool crit_opt = false;
1464
1465                 while (opts_len > 0) {
1466                         int len;
1467
1468                         if (opts_len < sizeof(*option))
1469                                 return -EINVAL;
1470
1471                         len = sizeof(*option) + option->length * 4;
1472                         if (len > opts_len)
1473                                 return -EINVAL;
1474
1475                         crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1476
1477                         option = (struct geneve_opt *)((u8 *)option + len);
1478                         opts_len -= len;
1479                 };
1480
1481                 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1482         };
1483
1484         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1485         if (start < 0)
1486                 return start;
1487
1488         a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1489                         sizeof(*tun_info) + key.tun_opts_len);
1490         if (IS_ERR(a))
1491                 return PTR_ERR(a);
1492
1493         tun_info = nla_data(a);
1494         tun_info->tunnel = key.tun_key;
1495         tun_info->options_len = key.tun_opts_len;
1496
1497         if (tun_info->options_len) {
1498                 /* We need to store the options in the action itself since
1499                  * everything else will go away after flow setup. We can append
1500                  * it to tun_info and then point there.
1501                  */
1502                 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1503                 memcpy(tun_info->options, GENEVE_OPTS(&key, key.tun_opts_len),
1504                         key.tun_opts_len);
1505         } else {
1506                 tun_info->options = NULL;
1507         }
1508
1509         add_nested_action_end(*sfa, start);
1510
1511         return err;
1512 }
1513
1514 static int validate_set(const struct nlattr *a,
1515                         const struct sw_flow_key *flow_key,
1516                         struct sw_flow_actions **sfa,
1517                         bool *set_tun, __be16 eth_type)
1518 {
1519         const struct nlattr *ovs_key = nla_data(a);
1520         int key_type = nla_type(ovs_key);
1521
1522         /* There can be only one key in a action */
1523         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1524                 return -EINVAL;
1525
1526         if (key_type > OVS_KEY_ATTR_MAX ||
1527             (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1528              ovs_key_lens[key_type] != -1))
1529                 return -EINVAL;
1530
1531         switch (key_type) {
1532         const struct ovs_key_ipv4 *ipv4_key;
1533         const struct ovs_key_ipv6 *ipv6_key;
1534         int err;
1535
1536         case OVS_KEY_ATTR_PRIORITY:
1537         case OVS_KEY_ATTR_SKB_MARK:
1538         case OVS_KEY_ATTR_ETHERNET:
1539                 break;
1540
1541         case OVS_KEY_ATTR_TUNNEL:
1542                 *set_tun = true;
1543                 err = validate_and_copy_set_tun(a, sfa);
1544                 if (err)
1545                         return err;
1546                 break;
1547
1548         case OVS_KEY_ATTR_IPV4:
1549                 if (eth_type != htons(ETH_P_IP))
1550                         return -EINVAL;
1551
1552                 if (!flow_key->ip.proto)
1553                         return -EINVAL;
1554
1555                 ipv4_key = nla_data(ovs_key);
1556                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1557                         return -EINVAL;
1558
1559                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1560                         return -EINVAL;
1561
1562                 break;
1563
1564         case OVS_KEY_ATTR_IPV6:
1565                 if (eth_type != htons(ETH_P_IPV6))
1566                         return -EINVAL;
1567
1568                 if (!flow_key->ip.proto)
1569                         return -EINVAL;
1570
1571                 ipv6_key = nla_data(ovs_key);
1572                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1573                         return -EINVAL;
1574
1575                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1576                         return -EINVAL;
1577
1578                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1579                         return -EINVAL;
1580
1581                 break;
1582
1583         case OVS_KEY_ATTR_TCP:
1584                 if (flow_key->ip.proto != IPPROTO_TCP)
1585                         return -EINVAL;
1586
1587                 return validate_tp_port(flow_key, eth_type);
1588
1589         case OVS_KEY_ATTR_UDP:
1590                 if (flow_key->ip.proto != IPPROTO_UDP)
1591                         return -EINVAL;
1592
1593                 return validate_tp_port(flow_key, eth_type);
1594
1595         case OVS_KEY_ATTR_MPLS:
1596                 if (!eth_p_mpls(eth_type))
1597                         return -EINVAL;
1598                 break;
1599
1600         case OVS_KEY_ATTR_SCTP:
1601                 if (flow_key->ip.proto != IPPROTO_SCTP)
1602                         return -EINVAL;
1603
1604                 return validate_tp_port(flow_key, eth_type);
1605
1606         default:
1607                 return -EINVAL;
1608         }
1609
1610         return 0;
1611 }
1612
1613 static int validate_userspace(const struct nlattr *attr)
1614 {
1615         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1616                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1617                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1618         };
1619         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1620         int error;
1621
1622         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1623                                  attr, userspace_policy);
1624         if (error)
1625                 return error;
1626
1627         if (!a[OVS_USERSPACE_ATTR_PID] ||
1628             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1629                 return -EINVAL;
1630
1631         return 0;
1632 }
1633
1634 static int copy_action(const struct nlattr *from,
1635                        struct sw_flow_actions **sfa)
1636 {
1637         int totlen = NLA_ALIGN(from->nla_len);
1638         struct nlattr *to;
1639
1640         to = reserve_sfa_size(sfa, from->nla_len);
1641         if (IS_ERR(to))
1642                 return PTR_ERR(to);
1643
1644         memcpy(to, from, totlen);
1645         return 0;
1646 }
1647
1648 static int ovs_nla_copy_actions__(const struct nlattr *attr,
1649                                   const struct sw_flow_key *key,
1650                                   int depth, struct sw_flow_actions **sfa,
1651                                   __be16 eth_type, __be16 vlan_tci)
1652 {
1653         const struct nlattr *a;
1654         int rem, err;
1655
1656         if (depth >= SAMPLE_ACTION_DEPTH)
1657                 return -EOVERFLOW;
1658
1659         nla_for_each_nested(a, attr, rem) {
1660                 /* Expected argument lengths, (u32)-1 for variable length. */
1661                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1662                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1663                         [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1664                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1665                         [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1666                         [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
1667                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1668                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
1669                         [OVS_ACTION_ATTR_SET] = (u32)-1,
1670                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1671                         [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1672                 };
1673                 const struct ovs_action_push_vlan *vlan;
1674                 int type = nla_type(a);
1675                 bool skip_copy;
1676
1677                 if (type > OVS_ACTION_ATTR_MAX ||
1678                     (action_lens[type] != nla_len(a) &&
1679                      action_lens[type] != (u32)-1))
1680                         return -EINVAL;
1681
1682                 skip_copy = false;
1683                 switch (type) {
1684                 case OVS_ACTION_ATTR_UNSPEC:
1685                         return -EINVAL;
1686
1687                 case OVS_ACTION_ATTR_USERSPACE:
1688                         err = validate_userspace(a);
1689                         if (err)
1690                                 return err;
1691                         break;
1692
1693                 case OVS_ACTION_ATTR_OUTPUT:
1694                         if (nla_get_u32(a) >= DP_MAX_PORTS)
1695                                 return -EINVAL;
1696                         break;
1697
1698                 case OVS_ACTION_ATTR_HASH: {
1699                         const struct ovs_action_hash *act_hash = nla_data(a);
1700
1701                         switch (act_hash->hash_alg) {
1702                         case OVS_HASH_ALG_L4:
1703                                 break;
1704                         default:
1705                                 return  -EINVAL;
1706                         }
1707
1708                         break;
1709                 }
1710
1711                 case OVS_ACTION_ATTR_POP_VLAN:
1712                         vlan_tci = htons(0);
1713                         break;
1714
1715                 case OVS_ACTION_ATTR_PUSH_VLAN:
1716                         vlan = nla_data(a);
1717                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1718                                 return -EINVAL;
1719                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1720                                 return -EINVAL;
1721                         vlan_tci = vlan->vlan_tci;
1722                         break;
1723
1724                 case OVS_ACTION_ATTR_RECIRC:
1725                         break;
1726
1727                 case OVS_ACTION_ATTR_PUSH_MPLS: {
1728                         const struct ovs_action_push_mpls *mpls = nla_data(a);
1729
1730                         if (!eth_p_mpls(mpls->mpls_ethertype))
1731                                 return -EINVAL;
1732                         /* Prohibit push MPLS other than to a white list
1733                          * for packets that have a known tag order.
1734                          */
1735                         if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1736                             (eth_type != htons(ETH_P_IP) &&
1737                              eth_type != htons(ETH_P_IPV6) &&
1738                              eth_type != htons(ETH_P_ARP) &&
1739                              eth_type != htons(ETH_P_RARP) &&
1740                              !eth_p_mpls(eth_type)))
1741                                 return -EINVAL;
1742                         eth_type = mpls->mpls_ethertype;
1743                         break;
1744                 }
1745
1746                 case OVS_ACTION_ATTR_POP_MPLS:
1747                         if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1748                             !eth_p_mpls(eth_type))
1749                                 return -EINVAL;
1750
1751                         /* Disallow subsequent L2.5+ set and mpls_pop actions
1752                          * as there is no check here to ensure that the new
1753                          * eth_type is valid and thus set actions could
1754                          * write off the end of the packet or otherwise
1755                          * corrupt it.
1756                          *
1757                          * Support for these actions is planned using packet
1758                          * recirculation.
1759                          */
1760                         eth_type = htons(0);
1761                         break;
1762
1763                 case OVS_ACTION_ATTR_SET:
1764                         err = validate_set(a, key, sfa, &skip_copy, eth_type);
1765                         if (err)
1766                                 return err;
1767                         break;
1768
1769                 case OVS_ACTION_ATTR_SAMPLE:
1770                         err = validate_and_copy_sample(a, key, depth, sfa,
1771                                                        eth_type, vlan_tci);
1772                         if (err)
1773                                 return err;
1774                         skip_copy = true;
1775                         break;
1776
1777                 default:
1778                         return -EINVAL;
1779                 }
1780                 if (!skip_copy) {
1781                         err = copy_action(a, sfa);
1782                         if (err)
1783                                 return err;
1784                 }
1785         }
1786
1787         if (rem > 0)
1788                 return -EINVAL;
1789
1790         return 0;
1791 }
1792
1793 int ovs_nla_copy_actions(const struct nlattr *attr,
1794                          const struct sw_flow_key *key,
1795                          struct sw_flow_actions **sfa)
1796 {
1797         return ovs_nla_copy_actions__(attr, key, 0, sfa, key->eth.type,
1798                                       key->eth.tci);
1799 }
1800
1801 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1802 {
1803         const struct nlattr *a;
1804         struct nlattr *start;
1805         int err = 0, rem;
1806
1807         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1808         if (!start)
1809                 return -EMSGSIZE;
1810
1811         nla_for_each_nested(a, attr, rem) {
1812                 int type = nla_type(a);
1813                 struct nlattr *st_sample;
1814
1815                 switch (type) {
1816                 case OVS_SAMPLE_ATTR_PROBABILITY:
1817                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1818                                     sizeof(u32), nla_data(a)))
1819                                 return -EMSGSIZE;
1820                         break;
1821                 case OVS_SAMPLE_ATTR_ACTIONS:
1822                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1823                         if (!st_sample)
1824                                 return -EMSGSIZE;
1825                         err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1826                         if (err)
1827                                 return err;
1828                         nla_nest_end(skb, st_sample);
1829                         break;
1830                 }
1831         }
1832
1833         nla_nest_end(skb, start);
1834         return err;
1835 }
1836
1837 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1838 {
1839         const struct nlattr *ovs_key = nla_data(a);
1840         int key_type = nla_type(ovs_key);
1841         struct nlattr *start;
1842         int err;
1843
1844         switch (key_type) {
1845         case OVS_KEY_ATTR_TUNNEL_INFO: {
1846                 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1847
1848                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1849                 if (!start)
1850                         return -EMSGSIZE;
1851
1852                 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
1853                                          tun_info->options_len ?
1854                                                 tun_info->options : NULL,
1855                                          tun_info->options_len);
1856                 if (err)
1857                         return err;
1858                 nla_nest_end(skb, start);
1859                 break;
1860         }
1861         default:
1862                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1863                         return -EMSGSIZE;
1864                 break;
1865         }
1866
1867         return 0;
1868 }
1869
1870 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1871 {
1872         const struct nlattr *a;
1873         int rem, err;
1874
1875         nla_for_each_attr(a, attr, len, rem) {
1876                 int type = nla_type(a);
1877
1878                 switch (type) {
1879                 case OVS_ACTION_ATTR_SET:
1880                         err = set_action_to_attr(a, skb);
1881                         if (err)
1882                                 return err;
1883                         break;
1884
1885                 case OVS_ACTION_ATTR_SAMPLE:
1886                         err = sample_action_to_attr(a, skb);
1887                         if (err)
1888                                 return err;
1889                         break;
1890                 default:
1891                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1892                                 return -EMSGSIZE;
1893                         break;
1894                 }
1895         }
1896
1897         return 0;
1898 }