blob: 7bd09b75ebaa04aef9caddcfa8037bf66cda81d0 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
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
Joe Perches2235ad12014-02-03 17:18:21 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Pravin B Shelare6445712013-10-03 18:16:47 -070021#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
51static 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
103static u16 range_n_bytes(const struct sw_flow_key_range *range)
104{
105 return range->end - range->start;
106}
107
108static bool match_validate(const struct sw_flow_match *match,
109 u64 key_attrs, u64 mask_attrs)
110{
111 u64 key_expected = 1 << 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 &= ~((1 << OVS_KEY_ATTR_IPV4)
117 | (1 << OVS_KEY_ATTR_IPV6)
118 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700119 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700120 | (1 << OVS_KEY_ATTR_UDP)
121 | (1 << OVS_KEY_ATTR_SCTP)
122 | (1 << OVS_KEY_ATTR_ICMP)
123 | (1 << OVS_KEY_ATTR_ICMPV6)
124 | (1 << OVS_KEY_ATTR_ARP)
125 | (1 << OVS_KEY_ATTR_ND));
126
127 /* Always allowed mask fields. */
128 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
129 | (1 << OVS_KEY_ATTR_IN_PORT)
130 | (1 << 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 |= 1 << OVS_KEY_ATTR_ARP;
136 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
137 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
138 }
139
140 if (match->key->eth.type == htons(ETH_P_IP)) {
141 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
142 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
143 mask_allowed |= 1 << 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 |= 1 << OVS_KEY_ATTR_UDP;
148 if (match->mask && (match->mask->key.ip.proto == 0xff))
149 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
150 }
151
152 if (match->key->ip.proto == IPPROTO_SCTP) {
153 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
154 if (match->mask && (match->mask->key.ip.proto == 0xff))
155 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
156 }
157
158 if (match->key->ip.proto == IPPROTO_TCP) {
159 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700160 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
161 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700162 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700163 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
164 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700165 }
166
167 if (match->key->ip.proto == IPPROTO_ICMP) {
168 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
169 if (match->mask && (match->mask->key.ip.proto == 0xff))
170 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
171 }
172 }
173 }
174
175 if (match->key->eth.type == htons(ETH_P_IPV6)) {
176 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
177 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
178 mask_allowed |= 1 << 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 |= 1 << OVS_KEY_ATTR_UDP;
183 if (match->mask && (match->mask->key.ip.proto == 0xff))
184 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
185 }
186
187 if (match->key->ip.proto == IPPROTO_SCTP) {
188 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
189 if (match->mask && (match->mask->key.ip.proto == 0xff))
190 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
191 }
192
193 if (match->key->ip.proto == IPPROTO_TCP) {
194 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700195 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
196 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700197 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700198 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
199 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700200 }
201
202 if (match->key->ip.proto == IPPROTO_ICMPV6) {
203 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1 << 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 |= 1 << OVS_KEY_ATTR_ND;
211 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
212 mask_allowed |= 1 << 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",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800221 (unsigned long long)key_attrs, (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700222 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",
Daniele Di Proiettocc23ebf2014-02-03 14:09:01 -0800228 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700229 return false;
230 }
231
232 return true;
233}
234
235/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
236static 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),
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700247 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -0700248 [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
257static 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
Pravin B Shelare298e502013-10-29 17:22:21 -0700271static 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
Pravin B Shelare6445712013-10-03 18:16:47 -0700285static 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 & (1 << 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 |= 1 << 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
331static 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
337static 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
343static 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
432static 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
468static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
469 const struct nlattr **a, bool is_mask)
470{
471 if (*attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_PRIORITY);
475 }
476
477 if (*attrs & (1 << 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 &= ~(1 << 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 & (1 << 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 &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
496 }
497 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
498 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
499 is_mask))
500 return -EINVAL;
501 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
502 }
503 return 0;
504}
505
Pravin B Shelare298e502013-10-29 17:22:21 -0700506static int ovs_key_from_nlattrs(struct sw_flow_match *match, bool *exact_5tuple,
507 u64 attrs, const struct nlattr **a,
508 bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -0700509{
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 & (1 << 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 &= ~(1 << OVS_KEY_ATTR_ETHERNET);
526 }
527
528 if (attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_VLAN);
543 } else if (!is_mask)
544 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
545
546 if (attrs & (1 << 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 &= ~(1 << 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
Pravin B Shelare298e502013-10-29 17:22:21 -0700565 if (is_mask && exact_5tuple) {
566 if (match->mask->key.eth.type != htons(0xffff))
567 *exact_5tuple = false;
568 }
569
Pravin B Shelare6445712013-10-03 18:16:47 -0700570 if (attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_IPV4);
Pravin B Shelare298e502013-10-29 17:22:21 -0700592
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 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700599 }
600
601 if (attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_IPV6);
Pravin B Shelare298e502013-10-29 17:22:21 -0700630
631 if (is_mask && exact_5tuple && *exact_5tuple) {
632 if (ipv6_key->ipv6_proto != 0xff ||
Daniele Di Proietto70851302014-01-23 10:56:49 -0800633 !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)))
Pravin B Shelare298e502013-10-29 17:22:21 -0700637 *exact_5tuple = false;
638 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700639 }
640
641 if (attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_ARP);
663 }
664
665 if (attrs & (1 << 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 & (1 << 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 &= ~(1 << OVS_KEY_ATTR_TCP);
Pravin B Shelare298e502013-10-29 17:22:21 -0700681
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;
Pravin B Shelare6445712013-10-03 18:16:47 -0700686 }
687
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700688 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
689 if (orig_attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
699 }
700
Pravin B Shelare6445712013-10-03 18:16:47 -0700701 if (attrs & (1 << 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 & (1 << 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 &= ~(1 << OVS_KEY_ATTR_UDP);
Pravin B Shelare298e502013-10-29 17:22:21 -0700717
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;
Pravin B Shelare6445712013-10-03 18:16:47 -0700722 }
723
724 if (attrs & (1 << 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 & (1 << 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 &= ~(1 << OVS_KEY_ATTR_SCTP);
740 }
741
742 if (attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_ICMP);
751 }
752
753 if (attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_ICMPV6);
762 }
763
764 if (attrs & (1 << 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 &= ~(1 << OVS_KEY_ATTR_ND);
777 }
778
779 if (attrs != 0)
780 return -EINVAL;
781
782 return 0;
783}
784
785static 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 */
806int ovs_nla_get_match(struct sw_flow_match *match,
Pravin B Shelare298e502013-10-29 17:22:21 -0700807 bool *exact_5tuple,
Pravin B Shelare6445712013-10-03 18:16:47 -0700808 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 & (1 << OVS_KEY_ATTR_ETHERNET)) &&
823 (key_attrs & (1 << 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 & (1 << OVS_KEY_ATTR_VLAN)) &&
828 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
829 OVS_NLERR("Invalid Vlan frame.\n");
830 return -EINVAL;
831 }
832
833 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
834 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
835 encap = a[OVS_KEY_ATTR_ENCAP];
836 key_attrs &= ~(1 << 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
Pravin B Shelare298e502013-10-29 17:22:21 -0700855 err = ovs_key_from_nlattrs(match, NULL, key_attrs, a, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700856 if (err)
857 return err;
858
Pravin B Shelare298e502013-10-29 17:22:21 -0700859 if (exact_5tuple)
860 *exact_5tuple = true;
861
Pravin B Shelare6445712013-10-03 18:16:47 -0700862 if (mask) {
863 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
864 if (err)
865 return err;
866
867 if (mask_attrs & 1 << 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 &= ~(1 << 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 &= ~(1 << 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
Pravin B Shelare298e502013-10-29 17:22:21 -0700899 err = ovs_key_from_nlattrs(match, exact_5tuple, mask_attrs, a, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700900 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
926int 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
954int 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;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001080 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1081 output->ipv4.tp.flags))
1082 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001083 } 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;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001086 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1087 output->ipv6.tp.flags))
1088 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001089 }
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
1156unencap:
1157 if (encap)
1158 nla_nest_end(skb, encap);
1159
1160 return 0;
1161
1162nla_put_failure:
1163 return -EMSGSIZE;
1164}
1165
1166#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1167
1168struct 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
Pravin B Shelare6445712013-10-03 18:16:47 -07001183/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1184 * The caller must hold rcu_read_lock for this to be sensible. */
1185void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1186{
Daniel Borkmann11d6c4612013-12-10 12:02:03 +01001187 kfree_rcu(sf_acts, rcu);
Pravin B Shelare6445712013-10-03 18:16:47 -07001188}
1189
1190static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1191 int attr_len)
1192{
1193
1194 struct sw_flow_actions *acts;
1195 int new_acts_size;
1196 int req_size = NLA_ALIGN(attr_len);
1197 int next_offset = offsetof(struct sw_flow_actions, actions) +
1198 (*sfa)->actions_len;
1199
1200 if (req_size <= (ksize(*sfa) - next_offset))
1201 goto out;
1202
1203 new_acts_size = ksize(*sfa) * 2;
1204
1205 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1206 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1207 return ERR_PTR(-EMSGSIZE);
1208 new_acts_size = MAX_ACTIONS_BUFSIZE;
1209 }
1210
1211 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1212 if (IS_ERR(acts))
1213 return (void *)acts;
1214
1215 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1216 acts->actions_len = (*sfa)->actions_len;
1217 kfree(*sfa);
1218 *sfa = acts;
1219
1220out:
1221 (*sfa)->actions_len += req_size;
1222 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1223}
1224
1225static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1226{
1227 struct nlattr *a;
1228
1229 a = reserve_sfa_size(sfa, nla_attr_size(len));
1230 if (IS_ERR(a))
1231 return PTR_ERR(a);
1232
1233 a->nla_type = attrtype;
1234 a->nla_len = nla_attr_size(len);
1235
1236 if (data)
1237 memcpy(nla_data(a), data, len);
1238 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1239
1240 return 0;
1241}
1242
1243static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1244 int attrtype)
1245{
1246 int used = (*sfa)->actions_len;
1247 int err;
1248
1249 err = add_action(sfa, attrtype, NULL, 0);
1250 if (err)
1251 return err;
1252
1253 return used;
1254}
1255
1256static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1257 int st_offset)
1258{
1259 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1260 st_offset);
1261
1262 a->nla_len = sfa->actions_len - st_offset;
1263}
1264
1265static int validate_and_copy_sample(const struct nlattr *attr,
1266 const struct sw_flow_key *key, int depth,
1267 struct sw_flow_actions **sfa)
1268{
1269 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1270 const struct nlattr *probability, *actions;
1271 const struct nlattr *a;
1272 int rem, start, err, st_acts;
1273
1274 memset(attrs, 0, sizeof(attrs));
1275 nla_for_each_nested(a, attr, rem) {
1276 int type = nla_type(a);
1277 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1278 return -EINVAL;
1279 attrs[type] = a;
1280 }
1281 if (rem)
1282 return -EINVAL;
1283
1284 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1285 if (!probability || nla_len(probability) != sizeof(u32))
1286 return -EINVAL;
1287
1288 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1289 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1290 return -EINVAL;
1291
1292 /* validation done, copy sample action. */
1293 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1294 if (start < 0)
1295 return start;
1296 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1297 nla_data(probability), sizeof(u32));
1298 if (err)
1299 return err;
1300 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1301 if (st_acts < 0)
1302 return st_acts;
1303
1304 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1305 if (err)
1306 return err;
1307
1308 add_nested_action_end(*sfa, st_acts);
1309 add_nested_action_end(*sfa, start);
1310
1311 return 0;
1312}
1313
1314static int validate_tp_port(const struct sw_flow_key *flow_key)
1315{
1316 if (flow_key->eth.type == htons(ETH_P_IP)) {
1317 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
1318 return 0;
1319 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
1320 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
1321 return 0;
1322 }
1323
1324 return -EINVAL;
1325}
1326
1327void ovs_match_init(struct sw_flow_match *match,
1328 struct sw_flow_key *key,
1329 struct sw_flow_mask *mask)
1330{
1331 memset(match, 0, sizeof(*match));
1332 match->key = key;
1333 match->mask = mask;
1334
1335 memset(key, 0, sizeof(*key));
1336
1337 if (mask) {
1338 memset(&mask->key, 0, sizeof(mask->key));
1339 mask->range.start = mask->range.end = 0;
1340 }
1341}
1342
1343static int validate_and_copy_set_tun(const struct nlattr *attr,
1344 struct sw_flow_actions **sfa)
1345{
1346 struct sw_flow_match match;
1347 struct sw_flow_key key;
1348 int err, start;
1349
1350 ovs_match_init(&match, &key, NULL);
1351 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1352 if (err)
1353 return err;
1354
1355 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1356 if (start < 0)
1357 return start;
1358
1359 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1360 sizeof(match.key->tun_key));
1361 add_nested_action_end(*sfa, start);
1362
1363 return err;
1364}
1365
1366static int validate_set(const struct nlattr *a,
1367 const struct sw_flow_key *flow_key,
1368 struct sw_flow_actions **sfa,
1369 bool *set_tun)
1370{
1371 const struct nlattr *ovs_key = nla_data(a);
1372 int key_type = nla_type(ovs_key);
1373
1374 /* There can be only one key in a action */
1375 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1376 return -EINVAL;
1377
1378 if (key_type > OVS_KEY_ATTR_MAX ||
1379 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1380 ovs_key_lens[key_type] != -1))
1381 return -EINVAL;
1382
1383 switch (key_type) {
1384 const struct ovs_key_ipv4 *ipv4_key;
1385 const struct ovs_key_ipv6 *ipv6_key;
1386 int err;
1387
1388 case OVS_KEY_ATTR_PRIORITY:
1389 case OVS_KEY_ATTR_SKB_MARK:
1390 case OVS_KEY_ATTR_ETHERNET:
1391 break;
1392
1393 case OVS_KEY_ATTR_TUNNEL:
1394 *set_tun = true;
1395 err = validate_and_copy_set_tun(a, sfa);
1396 if (err)
1397 return err;
1398 break;
1399
1400 case OVS_KEY_ATTR_IPV4:
1401 if (flow_key->eth.type != htons(ETH_P_IP))
1402 return -EINVAL;
1403
1404 if (!flow_key->ip.proto)
1405 return -EINVAL;
1406
1407 ipv4_key = nla_data(ovs_key);
1408 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1409 return -EINVAL;
1410
1411 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1412 return -EINVAL;
1413
1414 break;
1415
1416 case OVS_KEY_ATTR_IPV6:
1417 if (flow_key->eth.type != htons(ETH_P_IPV6))
1418 return -EINVAL;
1419
1420 if (!flow_key->ip.proto)
1421 return -EINVAL;
1422
1423 ipv6_key = nla_data(ovs_key);
1424 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1425 return -EINVAL;
1426
1427 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1428 return -EINVAL;
1429
1430 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1431 return -EINVAL;
1432
1433 break;
1434
1435 case OVS_KEY_ATTR_TCP:
1436 if (flow_key->ip.proto != IPPROTO_TCP)
1437 return -EINVAL;
1438
1439 return validate_tp_port(flow_key);
1440
1441 case OVS_KEY_ATTR_UDP:
1442 if (flow_key->ip.proto != IPPROTO_UDP)
1443 return -EINVAL;
1444
1445 return validate_tp_port(flow_key);
1446
1447 case OVS_KEY_ATTR_SCTP:
1448 if (flow_key->ip.proto != IPPROTO_SCTP)
1449 return -EINVAL;
1450
1451 return validate_tp_port(flow_key);
1452
1453 default:
1454 return -EINVAL;
1455 }
1456
1457 return 0;
1458}
1459
1460static int validate_userspace(const struct nlattr *attr)
1461{
1462 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1463 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1464 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1465 };
1466 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1467 int error;
1468
1469 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1470 attr, userspace_policy);
1471 if (error)
1472 return error;
1473
1474 if (!a[OVS_USERSPACE_ATTR_PID] ||
1475 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1476 return -EINVAL;
1477
1478 return 0;
1479}
1480
1481static int copy_action(const struct nlattr *from,
1482 struct sw_flow_actions **sfa)
1483{
1484 int totlen = NLA_ALIGN(from->nla_len);
1485 struct nlattr *to;
1486
1487 to = reserve_sfa_size(sfa, from->nla_len);
1488 if (IS_ERR(to))
1489 return PTR_ERR(to);
1490
1491 memcpy(to, from, totlen);
1492 return 0;
1493}
1494
1495int ovs_nla_copy_actions(const struct nlattr *attr,
1496 const struct sw_flow_key *key,
1497 int depth,
1498 struct sw_flow_actions **sfa)
1499{
1500 const struct nlattr *a;
1501 int rem, err;
1502
1503 if (depth >= SAMPLE_ACTION_DEPTH)
1504 return -EOVERFLOW;
1505
1506 nla_for_each_nested(a, attr, rem) {
1507 /* Expected argument lengths, (u32)-1 for variable length. */
1508 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1509 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1510 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1511 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1512 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1513 [OVS_ACTION_ATTR_SET] = (u32)-1,
1514 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
1515 };
1516 const struct ovs_action_push_vlan *vlan;
1517 int type = nla_type(a);
1518 bool skip_copy;
1519
1520 if (type > OVS_ACTION_ATTR_MAX ||
1521 (action_lens[type] != nla_len(a) &&
1522 action_lens[type] != (u32)-1))
1523 return -EINVAL;
1524
1525 skip_copy = false;
1526 switch (type) {
1527 case OVS_ACTION_ATTR_UNSPEC:
1528 return -EINVAL;
1529
1530 case OVS_ACTION_ATTR_USERSPACE:
1531 err = validate_userspace(a);
1532 if (err)
1533 return err;
1534 break;
1535
1536 case OVS_ACTION_ATTR_OUTPUT:
1537 if (nla_get_u32(a) >= DP_MAX_PORTS)
1538 return -EINVAL;
1539 break;
1540
1541
1542 case OVS_ACTION_ATTR_POP_VLAN:
1543 break;
1544
1545 case OVS_ACTION_ATTR_PUSH_VLAN:
1546 vlan = nla_data(a);
1547 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1548 return -EINVAL;
1549 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1550 return -EINVAL;
1551 break;
1552
1553 case OVS_ACTION_ATTR_SET:
1554 err = validate_set(a, key, sfa, &skip_copy);
1555 if (err)
1556 return err;
1557 break;
1558
1559 case OVS_ACTION_ATTR_SAMPLE:
1560 err = validate_and_copy_sample(a, key, depth, sfa);
1561 if (err)
1562 return err;
1563 skip_copy = true;
1564 break;
1565
1566 default:
1567 return -EINVAL;
1568 }
1569 if (!skip_copy) {
1570 err = copy_action(a, sfa);
1571 if (err)
1572 return err;
1573 }
1574 }
1575
1576 if (rem > 0)
1577 return -EINVAL;
1578
1579 return 0;
1580}
1581
1582static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1583{
1584 const struct nlattr *a;
1585 struct nlattr *start;
1586 int err = 0, rem;
1587
1588 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1589 if (!start)
1590 return -EMSGSIZE;
1591
1592 nla_for_each_nested(a, attr, rem) {
1593 int type = nla_type(a);
1594 struct nlattr *st_sample;
1595
1596 switch (type) {
1597 case OVS_SAMPLE_ATTR_PROBABILITY:
1598 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1599 sizeof(u32), nla_data(a)))
1600 return -EMSGSIZE;
1601 break;
1602 case OVS_SAMPLE_ATTR_ACTIONS:
1603 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1604 if (!st_sample)
1605 return -EMSGSIZE;
1606 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1607 if (err)
1608 return err;
1609 nla_nest_end(skb, st_sample);
1610 break;
1611 }
1612 }
1613
1614 nla_nest_end(skb, start);
1615 return err;
1616}
1617
1618static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1619{
1620 const struct nlattr *ovs_key = nla_data(a);
1621 int key_type = nla_type(ovs_key);
1622 struct nlattr *start;
1623 int err;
1624
1625 switch (key_type) {
1626 case OVS_KEY_ATTR_IPV4_TUNNEL:
1627 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1628 if (!start)
1629 return -EMSGSIZE;
1630
1631 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1632 nla_data(ovs_key));
1633 if (err)
1634 return err;
1635 nla_nest_end(skb, start);
1636 break;
1637 default:
1638 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1639 return -EMSGSIZE;
1640 break;
1641 }
1642
1643 return 0;
1644}
1645
1646int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1647{
1648 const struct nlattr *a;
1649 int rem, err;
1650
1651 nla_for_each_attr(a, attr, len, rem) {
1652 int type = nla_type(a);
1653
1654 switch (type) {
1655 case OVS_ACTION_ATTR_SET:
1656 err = set_action_to_attr(a, skb);
1657 if (err)
1658 return err;
1659 break;
1660
1661 case OVS_ACTION_ATTR_SAMPLE:
1662 err = sample_action_to_attr(a, skb);
1663 if (err)
1664 return err;
1665 break;
1666 default:
1667 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1668 return -EMSGSIZE;
1669 break;
1670 }
1671 }
1672
1673 return 0;
1674}