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