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