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>
46 #include <net/ip_tunnels.h>
48 #include <net/ndisc.h>
50 #include "flow_netlink.h"
52 static void update_range__(struct sw_flow_match *match,
53 size_t offset, size_t size, bool is_mask)
55 struct sw_flow_key_range *range = NULL;
56 size_t start = rounddown(offset, sizeof(long));
57 size_t end = roundup(offset + size, sizeof(long));
60 range = &match->range;
62 range = &match->mask->range;
67 if (range->start == range->end) {
73 if (range->start > start)
80 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82 update_range__(match, offsetof(struct sw_flow_key, field), \
83 sizeof((match)->key->field), is_mask); \
86 (match)->mask->key.field = value; \
88 (match)->key->field = value; \
92 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
94 update_range__(match, offsetof(struct sw_flow_key, field), \
98 memcpy(&(match)->mask->key.field, value_p, len);\
100 memcpy(&(match)->key->field, value_p, len); \
104 static u16 range_n_bytes(const struct sw_flow_key_range *range)
106 return range->end - range->start;
109 static bool match_validate(const struct sw_flow_match *match,
110 u64 key_attrs, u64 mask_attrs)
112 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
113 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
115 /* The following mask attributes allowed only if they
116 * pass the validation tests. */
117 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
118 | (1ULL << OVS_KEY_ATTR_IPV6)
119 | (1ULL << OVS_KEY_ATTR_TCP)
120 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
121 | (1ULL << OVS_KEY_ATTR_UDP)
122 | (1ULL << OVS_KEY_ATTR_SCTP)
123 | (1ULL << OVS_KEY_ATTR_ICMP)
124 | (1ULL << OVS_KEY_ATTR_ICMPV6)
125 | (1ULL << OVS_KEY_ATTR_ARP)
126 | (1ULL << OVS_KEY_ATTR_ND));
128 /* Always allowed mask fields. */
129 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
130 | (1ULL << OVS_KEY_ATTR_IN_PORT)
131 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
133 /* Check key attributes. */
134 if (match->key->eth.type == htons(ETH_P_ARP)
135 || match->key->eth.type == htons(ETH_P_RARP)) {
136 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
137 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
138 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
141 if (match->key->eth.type == htons(ETH_P_IP)) {
142 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
143 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
144 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
146 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
147 if (match->key->ip.proto == IPPROTO_UDP) {
148 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
149 if (match->mask && (match->mask->key.ip.proto == 0xff))
150 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
153 if (match->key->ip.proto == IPPROTO_SCTP) {
154 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
155 if (match->mask && (match->mask->key.ip.proto == 0xff))
156 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
159 if (match->key->ip.proto == IPPROTO_TCP) {
160 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
161 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
162 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
163 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
164 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
168 if (match->key->ip.proto == IPPROTO_ICMP) {
169 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
170 if (match->mask && (match->mask->key.ip.proto == 0xff))
171 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
176 if (match->key->eth.type == htons(ETH_P_IPV6)) {
177 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
178 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
179 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
181 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
182 if (match->key->ip.proto == IPPROTO_UDP) {
183 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
184 if (match->mask && (match->mask->key.ip.proto == 0xff))
185 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
188 if (match->key->ip.proto == IPPROTO_SCTP) {
189 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
190 if (match->mask && (match->mask->key.ip.proto == 0xff))
191 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
194 if (match->key->ip.proto == IPPROTO_TCP) {
195 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
196 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
197 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
198 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
199 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
203 if (match->key->ip.proto == IPPROTO_ICMPV6) {
204 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
205 if (match->mask && (match->mask->key.ip.proto == 0xff))
206 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
208 if (match->key->tp.src ==
209 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
210 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
211 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
212 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
213 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
219 if ((key_attrs & key_expected) != key_expected) {
220 /* Key attributes check failed. */
221 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
222 (unsigned long long)key_attrs, (unsigned long long)key_expected);
226 if ((mask_attrs & mask_allowed) != mask_attrs) {
227 /* Mask attributes check failed. */
228 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
229 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
236 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
237 static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
238 [OVS_KEY_ATTR_ENCAP] = -1,
239 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
240 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
241 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
242 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
243 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
244 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
245 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
246 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
247 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
248 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
249 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
250 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
251 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
252 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
253 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
254 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
255 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
256 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
257 [OVS_KEY_ATTR_TUNNEL] = -1,
260 static bool is_all_zero(const u8 *fp, size_t size)
267 for (i = 0; i < size; i++)
274 static int __parse_flow_nlattrs(const struct nlattr *attr,
275 const struct nlattr *a[],
276 u64 *attrsp, bool nz)
278 const struct nlattr *nla;
283 nla_for_each_nested(nla, attr, rem) {
284 u16 type = nla_type(nla);
287 if (type > OVS_KEY_ATTR_MAX) {
288 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
289 type, OVS_KEY_ATTR_MAX);
293 if (attrs & (1ULL << type)) {
294 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
298 expected_len = ovs_key_lens[type];
299 if (nla_len(nla) != expected_len && expected_len != -1) {
300 OVS_NLERR("Key attribute has unexpected length (type=%d"
301 ", length=%d, expected=%d).\n", type,
302 nla_len(nla), expected_len);
306 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
307 attrs |= 1ULL << type;
312 OVS_NLERR("Message has %d unknown bytes.\n", rem);
320 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
321 const struct nlattr *a[], u64 *attrsp)
323 return __parse_flow_nlattrs(attr, a, attrsp, true);
326 static int parse_flow_nlattrs(const struct nlattr *attr,
327 const struct nlattr *a[], u64 *attrsp)
329 return __parse_flow_nlattrs(attr, a, attrsp, false);
332 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
333 struct sw_flow_match *match, bool is_mask)
338 __be16 tun_flags = 0;
340 nla_for_each_nested(a, attr, rem) {
341 int type = nla_type(a);
342 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
343 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
344 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
345 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
346 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
347 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
348 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
349 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
352 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
353 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
354 type, OVS_TUNNEL_KEY_ATTR_MAX);
358 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
359 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
360 " length (type=%d, length=%d, expected=%d).\n",
361 type, nla_len(a), ovs_tunnel_key_lens[type]);
366 case OVS_TUNNEL_KEY_ATTR_ID:
367 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
368 nla_get_be64(a), is_mask);
369 tun_flags |= TUNNEL_KEY;
371 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
372 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
373 nla_get_be32(a), is_mask);
375 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
376 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
377 nla_get_be32(a), is_mask);
379 case OVS_TUNNEL_KEY_ATTR_TOS:
380 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
381 nla_get_u8(a), is_mask);
383 case OVS_TUNNEL_KEY_ATTR_TTL:
384 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
385 nla_get_u8(a), is_mask);
388 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
389 tun_flags |= TUNNEL_DONT_FRAGMENT;
391 case OVS_TUNNEL_KEY_ATTR_CSUM:
392 tun_flags |= TUNNEL_CSUM;
399 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
402 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
407 if (!match->key->tun_key.ipv4_dst) {
408 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
413 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
421 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
422 const struct ovs_key_ipv4_tunnel *tun_key,
423 const struct ovs_key_ipv4_tunnel *output)
427 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
431 if (output->tun_flags & TUNNEL_KEY &&
432 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
434 if (output->ipv4_src &&
435 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
437 if (output->ipv4_dst &&
438 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
440 if (output->ipv4_tos &&
441 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
443 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
445 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
446 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
448 if ((output->tun_flags & TUNNEL_CSUM) &&
449 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
452 nla_nest_end(skb, nla);
457 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
458 const struct nlattr **a, bool is_mask)
460 if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
461 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
463 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
464 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
467 if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
468 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
470 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
471 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
474 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
475 SW_FLOW_KEY_PUT(match, phy.priority,
476 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
477 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
480 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
481 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
484 in_port = 0xffffffff; /* Always exact match in_port. */
485 else if (in_port >= DP_MAX_PORTS)
488 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
489 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
490 } else if (!is_mask) {
491 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
494 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
495 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
497 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
498 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
500 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
501 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
504 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
509 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
510 const struct nlattr **a, bool is_mask)
513 u64 orig_attrs = attrs;
515 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
519 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
520 const struct ovs_key_ethernet *eth_key;
522 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
523 SW_FLOW_KEY_MEMCPY(match, eth.src,
524 eth_key->eth_src, ETH_ALEN, is_mask);
525 SW_FLOW_KEY_MEMCPY(match, eth.dst,
526 eth_key->eth_dst, ETH_ALEN, is_mask);
527 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
530 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
533 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
534 if (!(tci & htons(VLAN_TAG_PRESENT))) {
536 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
538 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
543 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
544 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
546 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
548 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
551 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
553 /* Always exact match EtherType. */
554 eth_type = htons(0xffff);
555 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
556 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
557 ntohs(eth_type), ETH_P_802_3_MIN);
561 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
562 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
563 } else if (!is_mask) {
564 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
567 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
568 const struct ovs_key_ipv4 *ipv4_key;
570 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
571 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
572 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
573 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
576 SW_FLOW_KEY_PUT(match, ip.proto,
577 ipv4_key->ipv4_proto, is_mask);
578 SW_FLOW_KEY_PUT(match, ip.tos,
579 ipv4_key->ipv4_tos, is_mask);
580 SW_FLOW_KEY_PUT(match, ip.ttl,
581 ipv4_key->ipv4_ttl, is_mask);
582 SW_FLOW_KEY_PUT(match, ip.frag,
583 ipv4_key->ipv4_frag, is_mask);
584 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
585 ipv4_key->ipv4_src, is_mask);
586 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
587 ipv4_key->ipv4_dst, is_mask);
588 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
591 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
592 const struct ovs_key_ipv6 *ipv6_key;
594 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
595 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
596 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
597 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
600 SW_FLOW_KEY_PUT(match, ipv6.label,
601 ipv6_key->ipv6_label, is_mask);
602 SW_FLOW_KEY_PUT(match, ip.proto,
603 ipv6_key->ipv6_proto, is_mask);
604 SW_FLOW_KEY_PUT(match, ip.tos,
605 ipv6_key->ipv6_tclass, is_mask);
606 SW_FLOW_KEY_PUT(match, ip.ttl,
607 ipv6_key->ipv6_hlimit, is_mask);
608 SW_FLOW_KEY_PUT(match, ip.frag,
609 ipv6_key->ipv6_frag, is_mask);
610 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
612 sizeof(match->key->ipv6.addr.src),
614 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
616 sizeof(match->key->ipv6.addr.dst),
619 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
622 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
623 const struct ovs_key_arp *arp_key;
625 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
626 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
627 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
632 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
633 arp_key->arp_sip, is_mask);
634 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
635 arp_key->arp_tip, is_mask);
636 SW_FLOW_KEY_PUT(match, ip.proto,
637 ntohs(arp_key->arp_op), is_mask);
638 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
639 arp_key->arp_sha, ETH_ALEN, is_mask);
640 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
641 arp_key->arp_tha, ETH_ALEN, is_mask);
643 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
646 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
647 const struct ovs_key_tcp *tcp_key;
649 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
650 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
651 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
652 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
655 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
656 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
657 SW_FLOW_KEY_PUT(match, tp.flags,
658 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
661 SW_FLOW_KEY_PUT(match, tp.flags,
662 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
665 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
668 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
669 const struct ovs_key_udp *udp_key;
671 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
672 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
673 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
674 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
677 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
678 const struct ovs_key_sctp *sctp_key;
680 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
681 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
682 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
683 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
686 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
687 const struct ovs_key_icmp *icmp_key;
689 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
690 SW_FLOW_KEY_PUT(match, tp.src,
691 htons(icmp_key->icmp_type), is_mask);
692 SW_FLOW_KEY_PUT(match, tp.dst,
693 htons(icmp_key->icmp_code), is_mask);
694 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
697 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
698 const struct ovs_key_icmpv6 *icmpv6_key;
700 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
701 SW_FLOW_KEY_PUT(match, tp.src,
702 htons(icmpv6_key->icmpv6_type), is_mask);
703 SW_FLOW_KEY_PUT(match, tp.dst,
704 htons(icmpv6_key->icmpv6_code), is_mask);
705 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
708 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
709 const struct ovs_key_nd *nd_key;
711 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
712 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
714 sizeof(match->key->ipv6.nd.target),
716 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
717 nd_key->nd_sll, ETH_ALEN, is_mask);
718 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
719 nd_key->nd_tll, ETH_ALEN, is_mask);
720 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
729 static void sw_flow_mask_set(struct sw_flow_mask *mask,
730 struct sw_flow_key_range *range, u8 val)
732 u8 *m = (u8 *)&mask->key + range->start;
734 mask->range = *range;
735 memset(m, val, range_n_bytes(range));
739 * ovs_nla_get_match - parses Netlink attributes into a flow key and
740 * mask. In case the 'mask' is NULL, the flow is treated as exact match
741 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
742 * does not include any don't care bit.
743 * @match: receives the extracted flow match information.
744 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
745 * sequence. The fields should of the packet that triggered the creation
747 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
748 * attribute specifies the mask field of the wildcarded flow.
750 int ovs_nla_get_match(struct sw_flow_match *match,
751 const struct nlattr *key,
752 const struct nlattr *mask)
754 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
755 const struct nlattr *encap;
758 bool encap_valid = false;
761 err = parse_flow_nlattrs(key, a, &key_attrs);
765 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
766 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
767 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
770 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
771 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
772 OVS_NLERR("Invalid Vlan frame.\n");
776 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
777 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
778 encap = a[OVS_KEY_ATTR_ENCAP];
779 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
782 if (tci & htons(VLAN_TAG_PRESENT)) {
783 err = parse_flow_nlattrs(encap, a, &key_attrs);
787 /* Corner case for truncated 802.1Q header. */
788 if (nla_len(encap)) {
789 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
793 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
798 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
803 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
807 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
812 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
816 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
817 if (a[OVS_KEY_ATTR_ETHERTYPE])
818 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
820 if (eth_type == htons(0xffff)) {
821 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
822 encap = a[OVS_KEY_ATTR_ENCAP];
823 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
825 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
830 if (a[OVS_KEY_ATTR_VLAN])
831 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
833 if (!(tci & htons(VLAN_TAG_PRESENT))) {
834 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
839 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
843 /* Populate exact match flow's key mask. */
845 sw_flow_mask_set(match->mask, &match->range, 0xff);
848 if (!match_validate(match, key_attrs, mask_attrs))
855 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
856 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
857 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
860 * This parses a series of Netlink attributes that form a flow key, which must
861 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
862 * get the metadata, that is, the parts of the flow key that cannot be
863 * extracted from the packet itself.
866 int ovs_nla_get_flow_metadata(struct sw_flow *flow,
867 const struct nlattr *attr)
869 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
870 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
873 struct sw_flow_match match;
875 flow->key.phy.in_port = DP_MAX_PORTS;
876 flow->key.phy.priority = 0;
877 flow->key.phy.skb_mark = 0;
878 flow->key.ovs_flow_hash = 0;
879 flow->key.recirc_id = 0;
880 memset(tun_key, 0, sizeof(flow->key.tun_key));
882 err = parse_flow_nlattrs(attr, a, &attrs);
886 memset(&match, 0, sizeof(match));
887 match.key = &flow->key;
889 err = metadata_from_nlattrs(&match, &attrs, a, false);
896 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
897 const struct sw_flow_key *output, struct sk_buff *skb)
899 struct ovs_key_ethernet *eth_key;
900 struct nlattr *nla, *encap;
901 bool is_mask = (swkey != output);
903 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
904 goto nla_put_failure;
906 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
907 goto nla_put_failure;
909 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
910 goto nla_put_failure;
912 if ((swkey->tun_key.ipv4_dst || is_mask) &&
913 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
914 goto nla_put_failure;
916 if (swkey->phy.in_port == DP_MAX_PORTS) {
917 if (is_mask && (output->phy.in_port == 0xffff))
918 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
919 goto nla_put_failure;
922 upper_u16 = !is_mask ? 0 : 0xffff;
924 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
925 (upper_u16 << 16) | output->phy.in_port))
926 goto nla_put_failure;
929 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
930 goto nla_put_failure;
932 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
934 goto nla_put_failure;
936 eth_key = nla_data(nla);
937 ether_addr_copy(eth_key->eth_src, output->eth.src);
938 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
940 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
942 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
943 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
944 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
945 goto nla_put_failure;
946 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
952 if (swkey->eth.type == htons(ETH_P_802_2)) {
954 * Ethertype 802.2 is represented in the netlink with omitted
955 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
956 * 0xffff in the mask attribute. Ethertype can also
959 if (is_mask && output->eth.type)
960 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
962 goto nla_put_failure;
966 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
967 goto nla_put_failure;
969 if (swkey->eth.type == htons(ETH_P_IP)) {
970 struct ovs_key_ipv4 *ipv4_key;
972 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
974 goto nla_put_failure;
975 ipv4_key = nla_data(nla);
976 ipv4_key->ipv4_src = output->ipv4.addr.src;
977 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
978 ipv4_key->ipv4_proto = output->ip.proto;
979 ipv4_key->ipv4_tos = output->ip.tos;
980 ipv4_key->ipv4_ttl = output->ip.ttl;
981 ipv4_key->ipv4_frag = output->ip.frag;
982 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
983 struct ovs_key_ipv6 *ipv6_key;
985 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
987 goto nla_put_failure;
988 ipv6_key = nla_data(nla);
989 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
990 sizeof(ipv6_key->ipv6_src));
991 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
992 sizeof(ipv6_key->ipv6_dst));
993 ipv6_key->ipv6_label = output->ipv6.label;
994 ipv6_key->ipv6_proto = output->ip.proto;
995 ipv6_key->ipv6_tclass = output->ip.tos;
996 ipv6_key->ipv6_hlimit = output->ip.ttl;
997 ipv6_key->ipv6_frag = output->ip.frag;
998 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
999 swkey->eth.type == htons(ETH_P_RARP)) {
1000 struct ovs_key_arp *arp_key;
1002 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1004 goto nla_put_failure;
1005 arp_key = nla_data(nla);
1006 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1007 arp_key->arp_sip = output->ipv4.addr.src;
1008 arp_key->arp_tip = output->ipv4.addr.dst;
1009 arp_key->arp_op = htons(output->ip.proto);
1010 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1011 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1014 if ((swkey->eth.type == htons(ETH_P_IP) ||
1015 swkey->eth.type == htons(ETH_P_IPV6)) &&
1016 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1018 if (swkey->ip.proto == IPPROTO_TCP) {
1019 struct ovs_key_tcp *tcp_key;
1021 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1023 goto nla_put_failure;
1024 tcp_key = nla_data(nla);
1025 tcp_key->tcp_src = output->tp.src;
1026 tcp_key->tcp_dst = output->tp.dst;
1027 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1029 goto nla_put_failure;
1030 } else if (swkey->ip.proto == IPPROTO_UDP) {
1031 struct ovs_key_udp *udp_key;
1033 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1035 goto nla_put_failure;
1036 udp_key = nla_data(nla);
1037 udp_key->udp_src = output->tp.src;
1038 udp_key->udp_dst = output->tp.dst;
1039 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1040 struct ovs_key_sctp *sctp_key;
1042 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1044 goto nla_put_failure;
1045 sctp_key = nla_data(nla);
1046 sctp_key->sctp_src = output->tp.src;
1047 sctp_key->sctp_dst = output->tp.dst;
1048 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1049 swkey->ip.proto == IPPROTO_ICMP) {
1050 struct ovs_key_icmp *icmp_key;
1052 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1054 goto nla_put_failure;
1055 icmp_key = nla_data(nla);
1056 icmp_key->icmp_type = ntohs(output->tp.src);
1057 icmp_key->icmp_code = ntohs(output->tp.dst);
1058 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1059 swkey->ip.proto == IPPROTO_ICMPV6) {
1060 struct ovs_key_icmpv6 *icmpv6_key;
1062 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1063 sizeof(*icmpv6_key));
1065 goto nla_put_failure;
1066 icmpv6_key = nla_data(nla);
1067 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1068 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1070 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1071 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1072 struct ovs_key_nd *nd_key;
1074 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1076 goto nla_put_failure;
1077 nd_key = nla_data(nla);
1078 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1079 sizeof(nd_key->nd_target));
1080 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1081 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1088 nla_nest_end(skb, encap);
1096 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1098 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1100 struct sw_flow_actions *sfa;
1102 if (size > MAX_ACTIONS_BUFSIZE)
1103 return ERR_PTR(-EINVAL);
1105 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1107 return ERR_PTR(-ENOMEM);
1109 sfa->actions_len = 0;
1113 /* RCU callback used by ovs_nla_free_flow_actions. */
1114 static void rcu_free_acts_callback(struct rcu_head *rcu)
1116 struct sw_flow_actions *sf_acts = container_of(rcu,
1117 struct sw_flow_actions, rcu);
1121 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1122 * The caller must hold rcu_read_lock for this to be sensible. */
1123 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1125 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1128 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1132 struct sw_flow_actions *acts;
1134 int req_size = NLA_ALIGN(attr_len);
1135 int next_offset = offsetof(struct sw_flow_actions, actions) +
1136 (*sfa)->actions_len;
1138 if (req_size <= (ksize(*sfa) - next_offset))
1141 new_acts_size = ksize(*sfa) * 2;
1143 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1144 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1145 return ERR_PTR(-EMSGSIZE);
1146 new_acts_size = MAX_ACTIONS_BUFSIZE;
1149 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1151 return (void *)acts;
1153 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1154 acts->actions_len = (*sfa)->actions_len;
1159 (*sfa)->actions_len += req_size;
1160 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1163 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1167 a = reserve_sfa_size(sfa, nla_attr_size(len));
1171 a->nla_type = attrtype;
1172 a->nla_len = nla_attr_size(len);
1175 memcpy(nla_data(a), data, len);
1176 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1181 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1184 int used = (*sfa)->actions_len;
1187 err = add_action(sfa, attrtype, NULL, 0);
1194 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1197 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1200 a->nla_len = sfa->actions_len - st_offset;
1203 static int validate_and_copy_sample(const struct nlattr *attr,
1204 const struct sw_flow_key *key, int depth,
1205 struct sw_flow_actions **sfa)
1207 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1208 const struct nlattr *probability, *actions;
1209 const struct nlattr *a;
1210 int rem, start, err, st_acts;
1212 memset(attrs, 0, sizeof(attrs));
1213 nla_for_each_nested(a, attr, rem) {
1214 int type = nla_type(a);
1215 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1222 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1223 if (!probability || nla_len(probability) != sizeof(u32))
1226 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1227 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1230 /* validation done, copy sample action. */
1231 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1234 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1235 nla_data(probability), sizeof(u32));
1238 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1242 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1246 add_nested_action_end(*sfa, st_acts);
1247 add_nested_action_end(*sfa, start);
1252 static int validate_tp_port(const struct sw_flow_key *flow_key)
1254 if ((flow_key->eth.type == htons(ETH_P_IP) ||
1255 flow_key->eth.type == htons(ETH_P_IPV6)) &&
1256 (flow_key->tp.src || flow_key->tp.dst))
1262 void ovs_match_init(struct sw_flow_match *match,
1263 struct sw_flow_key *key,
1264 struct sw_flow_mask *mask)
1266 memset(match, 0, sizeof(*match));
1270 memset(key, 0, sizeof(*key));
1273 memset(&mask->key, 0, sizeof(mask->key));
1274 mask->range.start = mask->range.end = 0;
1278 static int validate_and_copy_set_tun(const struct nlattr *attr,
1279 struct sw_flow_actions **sfa)
1281 struct sw_flow_match match;
1282 struct sw_flow_key key;
1285 ovs_match_init(&match, &key, NULL);
1286 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1290 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1294 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1295 sizeof(match.key->tun_key));
1296 add_nested_action_end(*sfa, start);
1301 static int validate_set(const struct nlattr *a,
1302 const struct sw_flow_key *flow_key,
1303 struct sw_flow_actions **sfa,
1306 const struct nlattr *ovs_key = nla_data(a);
1307 int key_type = nla_type(ovs_key);
1309 /* There can be only one key in a action */
1310 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1313 if (key_type > OVS_KEY_ATTR_MAX ||
1314 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1315 ovs_key_lens[key_type] != -1))
1319 const struct ovs_key_ipv4 *ipv4_key;
1320 const struct ovs_key_ipv6 *ipv6_key;
1323 case OVS_KEY_ATTR_PRIORITY:
1324 case OVS_KEY_ATTR_SKB_MARK:
1325 case OVS_KEY_ATTR_ETHERNET:
1328 case OVS_KEY_ATTR_TUNNEL:
1330 err = validate_and_copy_set_tun(a, sfa);
1335 case OVS_KEY_ATTR_IPV4:
1336 if (flow_key->eth.type != htons(ETH_P_IP))
1339 if (!flow_key->ip.proto)
1342 ipv4_key = nla_data(ovs_key);
1343 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1346 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1351 case OVS_KEY_ATTR_IPV6:
1352 if (flow_key->eth.type != htons(ETH_P_IPV6))
1355 if (!flow_key->ip.proto)
1358 ipv6_key = nla_data(ovs_key);
1359 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1362 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1365 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1370 case OVS_KEY_ATTR_TCP:
1371 if (flow_key->ip.proto != IPPROTO_TCP)
1374 return validate_tp_port(flow_key);
1376 case OVS_KEY_ATTR_UDP:
1377 if (flow_key->ip.proto != IPPROTO_UDP)
1380 return validate_tp_port(flow_key);
1382 case OVS_KEY_ATTR_SCTP:
1383 if (flow_key->ip.proto != IPPROTO_SCTP)
1386 return validate_tp_port(flow_key);
1395 static int validate_userspace(const struct nlattr *attr)
1397 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1398 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1399 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1401 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1404 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1405 attr, userspace_policy);
1409 if (!a[OVS_USERSPACE_ATTR_PID] ||
1410 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1416 static int copy_action(const struct nlattr *from,
1417 struct sw_flow_actions **sfa)
1419 int totlen = NLA_ALIGN(from->nla_len);
1422 to = reserve_sfa_size(sfa, from->nla_len);
1426 memcpy(to, from, totlen);
1430 int ovs_nla_copy_actions(const struct nlattr *attr,
1431 const struct sw_flow_key *key,
1433 struct sw_flow_actions **sfa)
1435 const struct nlattr *a;
1438 if (depth >= SAMPLE_ACTION_DEPTH)
1441 nla_for_each_nested(a, attr, rem) {
1442 /* Expected argument lengths, (u32)-1 for variable length. */
1443 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1444 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1445 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1446 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1447 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1448 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1449 [OVS_ACTION_ATTR_SET] = (u32)-1,
1450 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1451 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1453 const struct ovs_action_push_vlan *vlan;
1454 int type = nla_type(a);
1457 if (type > OVS_ACTION_ATTR_MAX ||
1458 (action_lens[type] != nla_len(a) &&
1459 action_lens[type] != (u32)-1))
1464 case OVS_ACTION_ATTR_UNSPEC:
1467 case OVS_ACTION_ATTR_USERSPACE:
1468 err = validate_userspace(a);
1473 case OVS_ACTION_ATTR_OUTPUT:
1474 if (nla_get_u32(a) >= DP_MAX_PORTS)
1478 case OVS_ACTION_ATTR_HASH: {
1479 const struct ovs_action_hash *act_hash = nla_data(a);
1481 switch (act_hash->hash_alg) {
1482 case OVS_HASH_ALG_L4:
1491 case OVS_ACTION_ATTR_POP_VLAN:
1494 case OVS_ACTION_ATTR_PUSH_VLAN:
1496 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1498 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1502 case OVS_ACTION_ATTR_RECIRC:
1505 case OVS_ACTION_ATTR_SET:
1506 err = validate_set(a, key, sfa, &skip_copy);
1511 case OVS_ACTION_ATTR_SAMPLE:
1512 err = validate_and_copy_sample(a, key, depth, sfa);
1522 err = copy_action(a, sfa);
1534 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1536 const struct nlattr *a;
1537 struct nlattr *start;
1540 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1544 nla_for_each_nested(a, attr, rem) {
1545 int type = nla_type(a);
1546 struct nlattr *st_sample;
1549 case OVS_SAMPLE_ATTR_PROBABILITY:
1550 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1551 sizeof(u32), nla_data(a)))
1554 case OVS_SAMPLE_ATTR_ACTIONS:
1555 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1558 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1561 nla_nest_end(skb, st_sample);
1566 nla_nest_end(skb, start);
1570 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1572 const struct nlattr *ovs_key = nla_data(a);
1573 int key_type = nla_type(ovs_key);
1574 struct nlattr *start;
1578 case OVS_KEY_ATTR_IPV4_TUNNEL:
1579 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1583 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1587 nla_nest_end(skb, start);
1590 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1598 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1600 const struct nlattr *a;
1603 nla_for_each_attr(a, attr, len, rem) {
1604 int type = nla_type(a);
1607 case OVS_ACTION_ATTR_SET:
1608 err = set_action_to_attr(a, skb);
1613 case OVS_ACTION_ATTR_SAMPLE:
1614 err = sample_action_to_attr(a, skb);
1619 if (nla_put(skb, type, nla_len(a), nla_data(a)))