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