2 * Copyright (c) 2007-2013 Nicira, Inc.
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.
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.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
35 #include <linux/rcupdate.h>
36 #include <linux/if_arp.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>
47 #include <net/ndisc.h>
49 #include "flow_netlink.h"
51 static void update_range__(struct sw_flow_match *match,
52 size_t offset, size_t size, bool is_mask)
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));
59 range = &match->range;
61 range = &match->mask->range;
66 if (range->start == range->end) {
72 if (range->start > start)
79 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
81 update_range__(match, offsetof(struct sw_flow_key, field), \
82 sizeof((match)->key->field), is_mask); \
85 (match)->mask->key.field = value; \
87 (match)->key->field = value; \
91 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
93 update_range__(match, offsetof(struct sw_flow_key, field), \
97 memcpy(&(match)->mask->key.field, value_p, len);\
99 memcpy(&(match)->key->field, value_p, len); \
103 static u16 range_n_bytes(const struct sw_flow_key_range *range)
105 return range->end - range->start;
108 static bool match_validate(const struct sw_flow_match *match,
109 u64 key_attrs, u64 mask_attrs)
111 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
112 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
114 /* The following mask attributes allowed only if they
115 * pass the validation tests. */
116 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
117 | (1ULL << OVS_KEY_ATTR_IPV6)
118 | (1ULL << OVS_KEY_ATTR_TCP)
119 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
120 | (1ULL << OVS_KEY_ATTR_UDP)
121 | (1ULL << OVS_KEY_ATTR_SCTP)
122 | (1ULL << OVS_KEY_ATTR_ICMP)
123 | (1ULL << OVS_KEY_ATTR_ICMPV6)
124 | (1ULL << OVS_KEY_ATTR_ARP)
125 | (1ULL << OVS_KEY_ATTR_ND));
127 /* Always allowed mask fields. */
128 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
129 | (1ULL << OVS_KEY_ATTR_IN_PORT)
130 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
132 /* Check key attributes. */
133 if (match->key->eth.type == htons(ETH_P_ARP)
134 || match->key->eth.type == htons(ETH_P_RARP)) {
135 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
136 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
137 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
140 if (match->key->eth.type == htons(ETH_P_IP)) {
141 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
142 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
143 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
145 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
146 if (match->key->ip.proto == IPPROTO_UDP) {
147 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
148 if (match->mask && (match->mask->key.ip.proto == 0xff))
149 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
152 if (match->key->ip.proto == IPPROTO_SCTP) {
153 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
154 if (match->mask && (match->mask->key.ip.proto == 0xff))
155 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
158 if (match->key->ip.proto == IPPROTO_TCP) {
159 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
160 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
161 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
162 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
163 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
167 if (match->key->ip.proto == IPPROTO_ICMP) {
168 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
169 if (match->mask && (match->mask->key.ip.proto == 0xff))
170 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
175 if (match->key->eth.type == htons(ETH_P_IPV6)) {
176 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
177 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
178 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
180 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
181 if (match->key->ip.proto == IPPROTO_UDP) {
182 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
183 if (match->mask && (match->mask->key.ip.proto == 0xff))
184 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
187 if (match->key->ip.proto == IPPROTO_SCTP) {
188 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
189 if (match->mask && (match->mask->key.ip.proto == 0xff))
190 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
193 if (match->key->ip.proto == IPPROTO_TCP) {
194 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
195 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
196 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
197 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
198 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
202 if (match->key->ip.proto == IPPROTO_ICMPV6) {
203 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
207 if (match->key->ipv6.tp.src ==
208 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
209 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
210 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
211 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
212 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
218 if ((key_attrs & key_expected) != key_expected) {
219 /* Key attributes check failed. */
220 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
221 (unsigned long long)key_attrs, (unsigned long long)key_expected);
225 if ((mask_attrs & mask_allowed) != mask_attrs) {
226 /* Mask attributes check failed. */
227 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
228 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
235 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
236 static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
237 [OVS_KEY_ATTR_ENCAP] = -1,
238 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
239 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
240 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
241 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
242 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
243 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
244 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
245 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
246 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
247 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
248 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
249 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
250 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
251 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
252 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
253 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
254 [OVS_KEY_ATTR_TUNNEL] = -1,
257 static bool is_all_zero(const u8 *fp, size_t size)
264 for (i = 0; i < size; i++)
271 static bool is_all_set(const u8 *fp, size_t size)
278 for (i = 0; i < size; i++)
285 static int __parse_flow_nlattrs(const struct nlattr *attr,
286 const struct nlattr *a[],
287 u64 *attrsp, bool nz)
289 const struct nlattr *nla;
294 nla_for_each_nested(nla, attr, rem) {
295 u16 type = nla_type(nla);
298 if (type > OVS_KEY_ATTR_MAX) {
299 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
300 type, OVS_KEY_ATTR_MAX);
304 if (attrs & (1ULL << type)) {
305 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
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);
317 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
318 attrs |= 1ULL << type;
323 OVS_NLERR("Message has %d unknown bytes.\n", rem);
331 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
332 const struct nlattr *a[], u64 *attrsp)
334 return __parse_flow_nlattrs(attr, a, attrsp, true);
337 static int parse_flow_nlattrs(const struct nlattr *attr,
338 const struct nlattr *a[], u64 *attrsp)
340 return __parse_flow_nlattrs(attr, a, attrsp, false);
343 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
344 struct sw_flow_match *match, bool is_mask)
349 __be16 tun_flags = 0;
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,
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);
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]);
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;
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);
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);
390 case OVS_TUNNEL_KEY_ATTR_TOS:
391 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
392 nla_get_u8(a), is_mask);
394 case OVS_TUNNEL_KEY_ATTR_TTL:
395 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
396 nla_get_u8(a), is_mask);
399 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
400 tun_flags |= TUNNEL_DONT_FRAGMENT;
402 case OVS_TUNNEL_KEY_ATTR_CSUM:
403 tun_flags |= TUNNEL_CSUM;
410 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
413 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
418 if (!match->key->tun_key.ipv4_dst) {
419 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
424 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
432 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
433 const struct ovs_key_ipv4_tunnel *tun_key,
434 const struct ovs_key_ipv4_tunnel *output)
438 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
442 if (output->tun_flags & TUNNEL_KEY &&
443 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
445 if (output->ipv4_src &&
446 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
448 if (output->ipv4_dst &&
449 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
451 if (output->ipv4_tos &&
452 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
454 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
456 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
457 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
459 if ((output->tun_flags & TUNNEL_CSUM) &&
460 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
463 nla_nest_end(skb, nla);
468 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
469 const struct nlattr **a, bool is_mask)
471 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
472 SW_FLOW_KEY_PUT(match, phy.priority,
473 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
474 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
477 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
478 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
481 in_port = 0xffffffff; /* Always exact match in_port. */
482 else if (in_port >= DP_MAX_PORTS)
485 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
486 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
487 } else if (!is_mask) {
488 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
491 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
492 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
494 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
495 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
497 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
498 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
501 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
506 static int ovs_key_from_nlattrs(struct sw_flow_match *match, bool *exact_5tuple,
507 u64 attrs, const struct nlattr **a,
511 u64 orig_attrs = attrs;
513 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
517 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
518 const struct ovs_key_ethernet *eth_key;
520 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
521 SW_FLOW_KEY_MEMCPY(match, eth.src,
522 eth_key->eth_src, ETH_ALEN, is_mask);
523 SW_FLOW_KEY_MEMCPY(match, eth.dst,
524 eth_key->eth_dst, ETH_ALEN, is_mask);
525 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
528 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
531 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
532 if (!(tci & htons(VLAN_TAG_PRESENT))) {
534 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
536 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
541 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
542 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
544 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
546 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
549 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
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);
559 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
560 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
561 } else if (!is_mask) {
562 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
565 if (is_mask && exact_5tuple) {
566 if (match->mask->key.eth.type != htons(0xffff))
567 *exact_5tuple = false;
570 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
571 const struct ovs_key_ipv4 *ipv4_key;
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);
579 SW_FLOW_KEY_PUT(match, ip.proto,
580 ipv4_key->ipv4_proto, is_mask);
581 SW_FLOW_KEY_PUT(match, ip.tos,
582 ipv4_key->ipv4_tos, is_mask);
583 SW_FLOW_KEY_PUT(match, ip.ttl,
584 ipv4_key->ipv4_ttl, is_mask);
585 SW_FLOW_KEY_PUT(match, ip.frag,
586 ipv4_key->ipv4_frag, is_mask);
587 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
588 ipv4_key->ipv4_src, is_mask);
589 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
590 ipv4_key->ipv4_dst, is_mask);
591 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
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;
601 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
602 const struct ovs_key_ipv6 *ipv6_key;
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);
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,
622 sizeof(match->key->ipv6.addr.src),
624 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
626 sizeof(match->key->ipv6.addr.dst),
629 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
631 if (is_mask && exact_5tuple && *exact_5tuple) {
632 if (ipv6_key->ipv6_proto != 0xff ||
633 !is_all_set((const u8 *)ipv6_key->ipv6_src,
634 sizeof(match->key->ipv6.addr.src)) ||
635 !is_all_set((const u8 *)ipv6_key->ipv6_dst,
636 sizeof(match->key->ipv6.addr.dst)))
637 *exact_5tuple = false;
641 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
642 const struct ovs_key_arp *arp_key;
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",
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);
662 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
665 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
666 const struct ovs_key_tcp *tcp_key;
668 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
669 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
670 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
671 tcp_key->tcp_src, is_mask);
672 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
673 tcp_key->tcp_dst, is_mask);
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);
680 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
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;
688 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
689 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
690 SW_FLOW_KEY_PUT(match, ipv4.tp.flags,
691 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
694 SW_FLOW_KEY_PUT(match, ipv6.tp.flags,
695 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
698 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
701 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
702 const struct ovs_key_udp *udp_key;
704 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
705 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
706 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
707 udp_key->udp_src, is_mask);
708 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
709 udp_key->udp_dst, is_mask);
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);
716 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
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;
724 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
725 const struct ovs_key_sctp *sctp_key;
727 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
728 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
729 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
730 sctp_key->sctp_src, is_mask);
731 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
732 sctp_key->sctp_dst, is_mask);
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);
739 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
742 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
743 const struct ovs_key_icmp *icmp_key;
745 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
746 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
747 htons(icmp_key->icmp_type), is_mask);
748 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
749 htons(icmp_key->icmp_code), is_mask);
750 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
753 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
754 const struct ovs_key_icmpv6 *icmpv6_key;
756 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
757 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
758 htons(icmpv6_key->icmpv6_type), is_mask);
759 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
760 htons(icmpv6_key->icmpv6_code), is_mask);
761 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
764 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
765 const struct ovs_key_nd *nd_key;
767 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
768 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
770 sizeof(match->key->ipv6.nd.target),
772 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
773 nd_key->nd_sll, ETH_ALEN, is_mask);
774 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
775 nd_key->nd_tll, ETH_ALEN, is_mask);
776 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
785 static void sw_flow_mask_set(struct sw_flow_mask *mask,
786 struct sw_flow_key_range *range, u8 val)
788 u8 *m = (u8 *)&mask->key + range->start;
790 mask->range = *range;
791 memset(m, val, range_n_bytes(range));
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
803 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
804 * attribute specifies the mask field of the wildcarded flow.
806 int ovs_nla_get_match(struct sw_flow_match *match,
808 const struct nlattr *key,
809 const struct nlattr *mask)
811 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
812 const struct nlattr *encap;
815 bool encap_valid = false;
818 err = parse_flow_nlattrs(key, a, &key_attrs);
822 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
823 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
824 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
827 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
828 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
829 OVS_NLERR("Invalid Vlan frame.\n");
833 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
834 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
835 encap = a[OVS_KEY_ATTR_ENCAP];
836 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
839 if (tci & htons(VLAN_TAG_PRESENT)) {
840 err = parse_flow_nlattrs(encap, a, &key_attrs);
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");
850 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
855 err = ovs_key_from_nlattrs(match, NULL, key_attrs, a, false);
860 *exact_5tuple = true;
863 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
867 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
872 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
876 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
877 if (a[OVS_KEY_ATTR_ETHERTYPE])
878 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
880 if (eth_type == htons(0xffff)) {
881 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
882 encap = a[OVS_KEY_ATTR_ENCAP];
883 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
885 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
890 if (a[OVS_KEY_ATTR_VLAN])
891 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
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));
899 err = ovs_key_from_nlattrs(match, exact_5tuple, mask_attrs, a, true);
903 /* Populate exact match flow's key mask. */
905 sw_flow_mask_set(match->mask, &match->range, 0xff);
908 if (!match_validate(match, key_attrs, mask_attrs))
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
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.
926 int ovs_nla_get_flow_metadata(struct sw_flow *flow,
927 const struct nlattr *attr)
929 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
930 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
933 struct sw_flow_match match;
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));
940 err = parse_flow_nlattrs(attr, a, &attrs);
944 memset(&match, 0, sizeof(match));
945 match.key = &flow->key;
947 err = metadata_from_nlattrs(&match, &attrs, a, false);
954 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
955 const struct sw_flow_key *output, struct sk_buff *skb)
957 struct ovs_key_ethernet *eth_key;
958 struct nlattr *nla, *encap;
959 bool is_mask = (swkey != output);
961 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
962 goto nla_put_failure;
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;
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;
974 upper_u16 = !is_mask ? 0 : 0xffff;
976 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
977 (upper_u16 << 16) | output->phy.in_port))
978 goto nla_put_failure;
981 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
982 goto nla_put_failure;
984 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
986 goto nla_put_failure;
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);
992 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
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);
1004 if (swkey->eth.type == htons(ETH_P_802_2)) {
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
1011 if (is_mask && output->eth.type)
1012 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1014 goto nla_put_failure;
1018 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1019 goto nla_put_failure;
1021 if (swkey->eth.type == htons(ETH_P_IP)) {
1022 struct ovs_key_ipv4 *ipv4_key;
1024 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
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;
1037 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
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;
1054 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
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);
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) {
1070 if (swkey->ip.proto == IPPROTO_TCP) {
1071 struct ovs_key_tcp *tcp_key;
1073 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1075 goto nla_put_failure;
1076 tcp_key = nla_data(nla);
1077 if (swkey->eth.type == htons(ETH_P_IP)) {
1078 tcp_key->tcp_src = output->ipv4.tp.src;
1079 tcp_key->tcp_dst = output->ipv4.tp.dst;
1080 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1081 output->ipv4.tp.flags))
1082 goto nla_put_failure;
1083 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1084 tcp_key->tcp_src = output->ipv6.tp.src;
1085 tcp_key->tcp_dst = output->ipv6.tp.dst;
1086 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1087 output->ipv6.tp.flags))
1088 goto nla_put_failure;
1090 } else if (swkey->ip.proto == IPPROTO_UDP) {
1091 struct ovs_key_udp *udp_key;
1093 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
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;
1104 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1105 struct ovs_key_sctp *sctp_key;
1107 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
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;
1118 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1119 swkey->ip.proto == IPPROTO_ICMP) {
1120 struct ovs_key_icmp *icmp_key;
1122 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
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;
1132 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1133 sizeof(*icmpv6_key));
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);
1140 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1141 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1142 struct ovs_key_nd *nd_key;
1144 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
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);
1158 nla_nest_end(skb, encap);
1166 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1168 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1170 struct sw_flow_actions *sfa;
1172 if (size > MAX_ACTIONS_BUFSIZE)
1173 return ERR_PTR(-EINVAL);
1175 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1177 return ERR_PTR(-ENOMEM);
1179 sfa->actions_len = 0;
1183 /* RCU callback used by ovs_nla_free_flow_actions. */
1184 static void rcu_free_acts_callback(struct rcu_head *rcu)
1186 struct sw_flow_actions *sf_acts = container_of(rcu,
1187 struct sw_flow_actions, rcu);
1191 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1192 * The caller must hold rcu_read_lock for this to be sensible. */
1193 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1195 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1198 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1202 struct sw_flow_actions *acts;
1204 int req_size = NLA_ALIGN(attr_len);
1205 int next_offset = offsetof(struct sw_flow_actions, actions) +
1206 (*sfa)->actions_len;
1208 if (req_size <= (ksize(*sfa) - next_offset))
1211 new_acts_size = ksize(*sfa) * 2;
1213 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1214 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1215 return ERR_PTR(-EMSGSIZE);
1216 new_acts_size = MAX_ACTIONS_BUFSIZE;
1219 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1221 return (void *)acts;
1223 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1224 acts->actions_len = (*sfa)->actions_len;
1229 (*sfa)->actions_len += req_size;
1230 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1233 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1237 a = reserve_sfa_size(sfa, nla_attr_size(len));
1241 a->nla_type = attrtype;
1242 a->nla_len = nla_attr_size(len);
1245 memcpy(nla_data(a), data, len);
1246 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1251 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1254 int used = (*sfa)->actions_len;
1257 err = add_action(sfa, attrtype, NULL, 0);
1264 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1267 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1270 a->nla_len = sfa->actions_len - st_offset;
1273 static int validate_and_copy_sample(const struct nlattr *attr,
1274 const struct sw_flow_key *key, int depth,
1275 struct sw_flow_actions **sfa)
1277 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1278 const struct nlattr *probability, *actions;
1279 const struct nlattr *a;
1280 int rem, start, err, st_acts;
1282 memset(attrs, 0, sizeof(attrs));
1283 nla_for_each_nested(a, attr, rem) {
1284 int type = nla_type(a);
1285 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1292 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1293 if (!probability || nla_len(probability) != sizeof(u32))
1296 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1297 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1300 /* validation done, copy sample action. */
1301 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1304 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1305 nla_data(probability), sizeof(u32));
1308 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1312 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1316 add_nested_action_end(*sfa, st_acts);
1317 add_nested_action_end(*sfa, start);
1322 static int validate_tp_port(const struct sw_flow_key *flow_key)
1324 if (flow_key->eth.type == htons(ETH_P_IP)) {
1325 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
1327 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
1328 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
1335 void ovs_match_init(struct sw_flow_match *match,
1336 struct sw_flow_key *key,
1337 struct sw_flow_mask *mask)
1339 memset(match, 0, sizeof(*match));
1343 memset(key, 0, sizeof(*key));
1346 memset(&mask->key, 0, sizeof(mask->key));
1347 mask->range.start = mask->range.end = 0;
1351 static int validate_and_copy_set_tun(const struct nlattr *attr,
1352 struct sw_flow_actions **sfa)
1354 struct sw_flow_match match;
1355 struct sw_flow_key key;
1358 ovs_match_init(&match, &key, NULL);
1359 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1363 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1367 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1368 sizeof(match.key->tun_key));
1369 add_nested_action_end(*sfa, start);
1374 static int validate_set(const struct nlattr *a,
1375 const struct sw_flow_key *flow_key,
1376 struct sw_flow_actions **sfa,
1379 const struct nlattr *ovs_key = nla_data(a);
1380 int key_type = nla_type(ovs_key);
1382 /* There can be only one key in a action */
1383 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1386 if (key_type > OVS_KEY_ATTR_MAX ||
1387 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1388 ovs_key_lens[key_type] != -1))
1392 const struct ovs_key_ipv4 *ipv4_key;
1393 const struct ovs_key_ipv6 *ipv6_key;
1396 case OVS_KEY_ATTR_PRIORITY:
1397 case OVS_KEY_ATTR_SKB_MARK:
1398 case OVS_KEY_ATTR_ETHERNET:
1401 case OVS_KEY_ATTR_TUNNEL:
1403 err = validate_and_copy_set_tun(a, sfa);
1408 case OVS_KEY_ATTR_IPV4:
1409 if (flow_key->eth.type != htons(ETH_P_IP))
1412 if (!flow_key->ip.proto)
1415 ipv4_key = nla_data(ovs_key);
1416 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1419 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1424 case OVS_KEY_ATTR_IPV6:
1425 if (flow_key->eth.type != htons(ETH_P_IPV6))
1428 if (!flow_key->ip.proto)
1431 ipv6_key = nla_data(ovs_key);
1432 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1435 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1438 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1443 case OVS_KEY_ATTR_TCP:
1444 if (flow_key->ip.proto != IPPROTO_TCP)
1447 return validate_tp_port(flow_key);
1449 case OVS_KEY_ATTR_UDP:
1450 if (flow_key->ip.proto != IPPROTO_UDP)
1453 return validate_tp_port(flow_key);
1455 case OVS_KEY_ATTR_SCTP:
1456 if (flow_key->ip.proto != IPPROTO_SCTP)
1459 return validate_tp_port(flow_key);
1468 static int validate_userspace(const struct nlattr *attr)
1470 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1471 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1472 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1474 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1477 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1478 attr, userspace_policy);
1482 if (!a[OVS_USERSPACE_ATTR_PID] ||
1483 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1489 static int copy_action(const struct nlattr *from,
1490 struct sw_flow_actions **sfa)
1492 int totlen = NLA_ALIGN(from->nla_len);
1495 to = reserve_sfa_size(sfa, from->nla_len);
1499 memcpy(to, from, totlen);
1503 int ovs_nla_copy_actions(const struct nlattr *attr,
1504 const struct sw_flow_key *key,
1506 struct sw_flow_actions **sfa)
1508 const struct nlattr *a;
1511 if (depth >= SAMPLE_ACTION_DEPTH)
1514 nla_for_each_nested(a, attr, rem) {
1515 /* Expected argument lengths, (u32)-1 for variable length. */
1516 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1517 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1518 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1519 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1520 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1521 [OVS_ACTION_ATTR_SET] = (u32)-1,
1522 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
1524 const struct ovs_action_push_vlan *vlan;
1525 int type = nla_type(a);
1528 if (type > OVS_ACTION_ATTR_MAX ||
1529 (action_lens[type] != nla_len(a) &&
1530 action_lens[type] != (u32)-1))
1535 case OVS_ACTION_ATTR_UNSPEC:
1538 case OVS_ACTION_ATTR_USERSPACE:
1539 err = validate_userspace(a);
1544 case OVS_ACTION_ATTR_OUTPUT:
1545 if (nla_get_u32(a) >= DP_MAX_PORTS)
1550 case OVS_ACTION_ATTR_POP_VLAN:
1553 case OVS_ACTION_ATTR_PUSH_VLAN:
1555 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1557 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1561 case OVS_ACTION_ATTR_SET:
1562 err = validate_set(a, key, sfa, &skip_copy);
1567 case OVS_ACTION_ATTR_SAMPLE:
1568 err = validate_and_copy_sample(a, key, depth, sfa);
1578 err = copy_action(a, sfa);
1590 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1592 const struct nlattr *a;
1593 struct nlattr *start;
1596 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1600 nla_for_each_nested(a, attr, rem) {
1601 int type = nla_type(a);
1602 struct nlattr *st_sample;
1605 case OVS_SAMPLE_ATTR_PROBABILITY:
1606 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1607 sizeof(u32), nla_data(a)))
1610 case OVS_SAMPLE_ATTR_ACTIONS:
1611 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1614 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1617 nla_nest_end(skb, st_sample);
1622 nla_nest_end(skb, start);
1626 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1628 const struct nlattr *ovs_key = nla_data(a);
1629 int key_type = nla_type(ovs_key);
1630 struct nlattr *start;
1634 case OVS_KEY_ATTR_IPV4_TUNNEL:
1635 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1639 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1643 nla_nest_end(skb, start);
1646 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1654 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1656 const struct nlattr *a;
1659 nla_for_each_attr(a, attr, len, rem) {
1660 int type = nla_type(a);
1663 case OVS_ACTION_ATTR_SET:
1664 err = set_action_to_attr(a, skb);
1669 case OVS_ACTION_ATTR_SAMPLE:
1670 err = sample_action_to_attr(a, skb);
1675 if (nla_put(skb, type, nla_len(a), nla_data(a)))