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