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_TUNNEL] = -1,
258 static bool is_all_zero(const u8 *fp, size_t size)
265 for (i = 0; i < size; i++)
272 static int __parse_flow_nlattrs(const struct nlattr *attr,
273 const struct nlattr *a[],
274 u64 *attrsp, bool nz)
276 const struct nlattr *nla;
281 nla_for_each_nested(nla, attr, rem) {
282 u16 type = nla_type(nla);
285 if (type > OVS_KEY_ATTR_MAX) {
286 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
287 type, OVS_KEY_ATTR_MAX);
291 if (attrs & (1ULL << type)) {
292 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
296 expected_len = ovs_key_lens[type];
297 if (nla_len(nla) != expected_len && expected_len != -1) {
298 OVS_NLERR("Key attribute has unexpected length (type=%d"
299 ", length=%d, expected=%d).\n", type,
300 nla_len(nla), expected_len);
304 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
305 attrs |= 1ULL << type;
310 OVS_NLERR("Message has %d unknown bytes.\n", rem);
318 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
319 const struct nlattr *a[], u64 *attrsp)
321 return __parse_flow_nlattrs(attr, a, attrsp, true);
324 static int parse_flow_nlattrs(const struct nlattr *attr,
325 const struct nlattr *a[], u64 *attrsp)
327 return __parse_flow_nlattrs(attr, a, attrsp, false);
330 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
331 struct sw_flow_match *match, bool is_mask)
336 __be16 tun_flags = 0;
338 nla_for_each_nested(a, attr, rem) {
339 int type = nla_type(a);
340 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
341 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
342 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
343 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
344 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
345 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
346 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
347 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
350 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
351 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
352 type, OVS_TUNNEL_KEY_ATTR_MAX);
356 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
357 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
358 " length (type=%d, length=%d, expected=%d).\n",
359 type, nla_len(a), ovs_tunnel_key_lens[type]);
364 case OVS_TUNNEL_KEY_ATTR_ID:
365 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
366 nla_get_be64(a), is_mask);
367 tun_flags |= TUNNEL_KEY;
369 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
370 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
371 nla_get_be32(a), is_mask);
373 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
374 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
375 nla_get_be32(a), is_mask);
377 case OVS_TUNNEL_KEY_ATTR_TOS:
378 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
379 nla_get_u8(a), is_mask);
381 case OVS_TUNNEL_KEY_ATTR_TTL:
382 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
383 nla_get_u8(a), is_mask);
386 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
387 tun_flags |= TUNNEL_DONT_FRAGMENT;
389 case OVS_TUNNEL_KEY_ATTR_CSUM:
390 tun_flags |= TUNNEL_CSUM;
397 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
400 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
405 if (!match->key->tun_key.ipv4_dst) {
406 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
411 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
419 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
420 const struct ovs_key_ipv4_tunnel *tun_key,
421 const struct ovs_key_ipv4_tunnel *output)
425 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
429 if (output->tun_flags & TUNNEL_KEY &&
430 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
432 if (output->ipv4_src &&
433 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
435 if (output->ipv4_dst &&
436 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
438 if (output->ipv4_tos &&
439 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
441 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
443 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
444 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
446 if ((output->tun_flags & TUNNEL_CSUM) &&
447 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
450 nla_nest_end(skb, nla);
455 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
456 const struct nlattr **a, bool is_mask)
458 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
459 SW_FLOW_KEY_PUT(match, phy.priority,
460 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
461 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
464 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
465 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
468 in_port = 0xffffffff; /* Always exact match in_port. */
469 else if (in_port >= DP_MAX_PORTS)
472 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
473 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
474 } else if (!is_mask) {
475 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
478 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
479 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
481 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
482 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
484 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
485 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
488 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
493 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
494 const struct nlattr **a, bool is_mask)
497 u64 orig_attrs = attrs;
499 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
503 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
504 const struct ovs_key_ethernet *eth_key;
506 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
507 SW_FLOW_KEY_MEMCPY(match, eth.src,
508 eth_key->eth_src, ETH_ALEN, is_mask);
509 SW_FLOW_KEY_MEMCPY(match, eth.dst,
510 eth_key->eth_dst, ETH_ALEN, is_mask);
511 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
514 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
517 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
518 if (!(tci & htons(VLAN_TAG_PRESENT))) {
520 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
522 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
527 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
528 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
530 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
532 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
535 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
537 /* Always exact match EtherType. */
538 eth_type = htons(0xffff);
539 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
540 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
541 ntohs(eth_type), ETH_P_802_3_MIN);
545 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
546 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
547 } else if (!is_mask) {
548 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
551 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
552 const struct ovs_key_ipv4 *ipv4_key;
554 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
555 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
556 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
557 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
560 SW_FLOW_KEY_PUT(match, ip.proto,
561 ipv4_key->ipv4_proto, is_mask);
562 SW_FLOW_KEY_PUT(match, ip.tos,
563 ipv4_key->ipv4_tos, is_mask);
564 SW_FLOW_KEY_PUT(match, ip.ttl,
565 ipv4_key->ipv4_ttl, is_mask);
566 SW_FLOW_KEY_PUT(match, ip.frag,
567 ipv4_key->ipv4_frag, is_mask);
568 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
569 ipv4_key->ipv4_src, is_mask);
570 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
571 ipv4_key->ipv4_dst, is_mask);
572 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
575 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
576 const struct ovs_key_ipv6 *ipv6_key;
578 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
579 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
580 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
581 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
584 SW_FLOW_KEY_PUT(match, ipv6.label,
585 ipv6_key->ipv6_label, is_mask);
586 SW_FLOW_KEY_PUT(match, ip.proto,
587 ipv6_key->ipv6_proto, is_mask);
588 SW_FLOW_KEY_PUT(match, ip.tos,
589 ipv6_key->ipv6_tclass, is_mask);
590 SW_FLOW_KEY_PUT(match, ip.ttl,
591 ipv6_key->ipv6_hlimit, is_mask);
592 SW_FLOW_KEY_PUT(match, ip.frag,
593 ipv6_key->ipv6_frag, is_mask);
594 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
596 sizeof(match->key->ipv6.addr.src),
598 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
600 sizeof(match->key->ipv6.addr.dst),
603 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
606 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
607 const struct ovs_key_arp *arp_key;
609 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
610 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
611 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
616 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
617 arp_key->arp_sip, is_mask);
618 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
619 arp_key->arp_tip, is_mask);
620 SW_FLOW_KEY_PUT(match, ip.proto,
621 ntohs(arp_key->arp_op), is_mask);
622 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
623 arp_key->arp_sha, ETH_ALEN, is_mask);
624 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
625 arp_key->arp_tha, ETH_ALEN, is_mask);
627 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
630 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
631 const struct ovs_key_tcp *tcp_key;
633 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
634 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
635 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
636 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
639 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
640 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
641 SW_FLOW_KEY_PUT(match, tp.flags,
642 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
645 SW_FLOW_KEY_PUT(match, tp.flags,
646 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
649 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
652 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
653 const struct ovs_key_udp *udp_key;
655 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
656 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
657 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
658 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
661 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
662 const struct ovs_key_sctp *sctp_key;
664 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
665 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
666 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
667 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
670 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
671 const struct ovs_key_icmp *icmp_key;
673 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
674 SW_FLOW_KEY_PUT(match, tp.src,
675 htons(icmp_key->icmp_type), is_mask);
676 SW_FLOW_KEY_PUT(match, tp.dst,
677 htons(icmp_key->icmp_code), is_mask);
678 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
681 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
682 const struct ovs_key_icmpv6 *icmpv6_key;
684 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
685 SW_FLOW_KEY_PUT(match, tp.src,
686 htons(icmpv6_key->icmpv6_type), is_mask);
687 SW_FLOW_KEY_PUT(match, tp.dst,
688 htons(icmpv6_key->icmpv6_code), is_mask);
689 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
692 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
693 const struct ovs_key_nd *nd_key;
695 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
696 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
698 sizeof(match->key->ipv6.nd.target),
700 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
701 nd_key->nd_sll, ETH_ALEN, is_mask);
702 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
703 nd_key->nd_tll, ETH_ALEN, is_mask);
704 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
713 static void sw_flow_mask_set(struct sw_flow_mask *mask,
714 struct sw_flow_key_range *range, u8 val)
716 u8 *m = (u8 *)&mask->key + range->start;
718 mask->range = *range;
719 memset(m, val, range_n_bytes(range));
723 * ovs_nla_get_match - parses Netlink attributes into a flow key and
724 * mask. In case the 'mask' is NULL, the flow is treated as exact match
725 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
726 * does not include any don't care bit.
727 * @match: receives the extracted flow match information.
728 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
729 * sequence. The fields should of the packet that triggered the creation
731 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
732 * attribute specifies the mask field of the wildcarded flow.
734 int ovs_nla_get_match(struct sw_flow_match *match,
735 const struct nlattr *key,
736 const struct nlattr *mask)
738 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
739 const struct nlattr *encap;
742 bool encap_valid = false;
745 err = parse_flow_nlattrs(key, a, &key_attrs);
749 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
750 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
751 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
754 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
755 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
756 OVS_NLERR("Invalid Vlan frame.\n");
760 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
761 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
762 encap = a[OVS_KEY_ATTR_ENCAP];
763 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
766 if (tci & htons(VLAN_TAG_PRESENT)) {
767 err = parse_flow_nlattrs(encap, a, &key_attrs);
771 /* Corner case for truncated 802.1Q header. */
772 if (nla_len(encap)) {
773 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
777 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
782 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
787 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
791 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
796 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
800 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
801 if (a[OVS_KEY_ATTR_ETHERTYPE])
802 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
804 if (eth_type == htons(0xffff)) {
805 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
806 encap = a[OVS_KEY_ATTR_ENCAP];
807 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
809 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
814 if (a[OVS_KEY_ATTR_VLAN])
815 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
817 if (!(tci & htons(VLAN_TAG_PRESENT))) {
818 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
823 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
827 /* Populate exact match flow's key mask. */
829 sw_flow_mask_set(match->mask, &match->range, 0xff);
832 if (!match_validate(match, key_attrs, mask_attrs))
839 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
840 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
841 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
844 * This parses a series of Netlink attributes that form a flow key, which must
845 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
846 * get the metadata, that is, the parts of the flow key that cannot be
847 * extracted from the packet itself.
850 int ovs_nla_get_flow_metadata(struct sw_flow *flow,
851 const struct nlattr *attr)
853 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
854 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
857 struct sw_flow_match match;
859 flow->key.phy.in_port = DP_MAX_PORTS;
860 flow->key.phy.priority = 0;
861 flow->key.phy.skb_mark = 0;
862 memset(tun_key, 0, sizeof(flow->key.tun_key));
864 err = parse_flow_nlattrs(attr, a, &attrs);
868 memset(&match, 0, sizeof(match));
869 match.key = &flow->key;
871 err = metadata_from_nlattrs(&match, &attrs, a, false);
878 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
879 const struct sw_flow_key *output, struct sk_buff *skb)
881 struct ovs_key_ethernet *eth_key;
882 struct nlattr *nla, *encap;
883 bool is_mask = (swkey != output);
885 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
886 goto nla_put_failure;
888 if ((swkey->tun_key.ipv4_dst || is_mask) &&
889 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
890 goto nla_put_failure;
892 if (swkey->phy.in_port == DP_MAX_PORTS) {
893 if (is_mask && (output->phy.in_port == 0xffff))
894 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
895 goto nla_put_failure;
898 upper_u16 = !is_mask ? 0 : 0xffff;
900 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
901 (upper_u16 << 16) | output->phy.in_port))
902 goto nla_put_failure;
905 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
906 goto nla_put_failure;
908 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
910 goto nla_put_failure;
912 eth_key = nla_data(nla);
913 ether_addr_copy(eth_key->eth_src, output->eth.src);
914 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
916 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
918 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
919 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
920 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
921 goto nla_put_failure;
922 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
928 if (swkey->eth.type == htons(ETH_P_802_2)) {
930 * Ethertype 802.2 is represented in the netlink with omitted
931 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
932 * 0xffff in the mask attribute. Ethertype can also
935 if (is_mask && output->eth.type)
936 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
938 goto nla_put_failure;
942 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
943 goto nla_put_failure;
945 if (swkey->eth.type == htons(ETH_P_IP)) {
946 struct ovs_key_ipv4 *ipv4_key;
948 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
950 goto nla_put_failure;
951 ipv4_key = nla_data(nla);
952 ipv4_key->ipv4_src = output->ipv4.addr.src;
953 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
954 ipv4_key->ipv4_proto = output->ip.proto;
955 ipv4_key->ipv4_tos = output->ip.tos;
956 ipv4_key->ipv4_ttl = output->ip.ttl;
957 ipv4_key->ipv4_frag = output->ip.frag;
958 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
959 struct ovs_key_ipv6 *ipv6_key;
961 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
963 goto nla_put_failure;
964 ipv6_key = nla_data(nla);
965 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
966 sizeof(ipv6_key->ipv6_src));
967 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
968 sizeof(ipv6_key->ipv6_dst));
969 ipv6_key->ipv6_label = output->ipv6.label;
970 ipv6_key->ipv6_proto = output->ip.proto;
971 ipv6_key->ipv6_tclass = output->ip.tos;
972 ipv6_key->ipv6_hlimit = output->ip.ttl;
973 ipv6_key->ipv6_frag = output->ip.frag;
974 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
975 swkey->eth.type == htons(ETH_P_RARP)) {
976 struct ovs_key_arp *arp_key;
978 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
980 goto nla_put_failure;
981 arp_key = nla_data(nla);
982 memset(arp_key, 0, sizeof(struct ovs_key_arp));
983 arp_key->arp_sip = output->ipv4.addr.src;
984 arp_key->arp_tip = output->ipv4.addr.dst;
985 arp_key->arp_op = htons(output->ip.proto);
986 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
987 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
990 if ((swkey->eth.type == htons(ETH_P_IP) ||
991 swkey->eth.type == htons(ETH_P_IPV6)) &&
992 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
994 if (swkey->ip.proto == IPPROTO_TCP) {
995 struct ovs_key_tcp *tcp_key;
997 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
999 goto nla_put_failure;
1000 tcp_key = nla_data(nla);
1001 tcp_key->tcp_src = output->tp.src;
1002 tcp_key->tcp_dst = output->tp.dst;
1003 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1005 goto nla_put_failure;
1006 } else if (swkey->ip.proto == IPPROTO_UDP) {
1007 struct ovs_key_udp *udp_key;
1009 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1011 goto nla_put_failure;
1012 udp_key = nla_data(nla);
1013 udp_key->udp_src = output->tp.src;
1014 udp_key->udp_dst = output->tp.dst;
1015 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1016 struct ovs_key_sctp *sctp_key;
1018 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1020 goto nla_put_failure;
1021 sctp_key = nla_data(nla);
1022 sctp_key->sctp_src = output->tp.src;
1023 sctp_key->sctp_dst = output->tp.dst;
1024 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1025 swkey->ip.proto == IPPROTO_ICMP) {
1026 struct ovs_key_icmp *icmp_key;
1028 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1030 goto nla_put_failure;
1031 icmp_key = nla_data(nla);
1032 icmp_key->icmp_type = ntohs(output->tp.src);
1033 icmp_key->icmp_code = ntohs(output->tp.dst);
1034 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1035 swkey->ip.proto == IPPROTO_ICMPV6) {
1036 struct ovs_key_icmpv6 *icmpv6_key;
1038 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1039 sizeof(*icmpv6_key));
1041 goto nla_put_failure;
1042 icmpv6_key = nla_data(nla);
1043 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1044 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1046 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1047 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1048 struct ovs_key_nd *nd_key;
1050 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1052 goto nla_put_failure;
1053 nd_key = nla_data(nla);
1054 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1055 sizeof(nd_key->nd_target));
1056 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1057 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1064 nla_nest_end(skb, encap);
1072 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1074 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1076 struct sw_flow_actions *sfa;
1078 if (size > MAX_ACTIONS_BUFSIZE)
1079 return ERR_PTR(-EINVAL);
1081 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1083 return ERR_PTR(-ENOMEM);
1085 sfa->actions_len = 0;
1089 /* RCU callback used by ovs_nla_free_flow_actions. */
1090 static void rcu_free_acts_callback(struct rcu_head *rcu)
1092 struct sw_flow_actions *sf_acts = container_of(rcu,
1093 struct sw_flow_actions, rcu);
1097 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1098 * The caller must hold rcu_read_lock for this to be sensible. */
1099 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1101 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1104 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1108 struct sw_flow_actions *acts;
1110 int req_size = NLA_ALIGN(attr_len);
1111 int next_offset = offsetof(struct sw_flow_actions, actions) +
1112 (*sfa)->actions_len;
1114 if (req_size <= (ksize(*sfa) - next_offset))
1117 new_acts_size = ksize(*sfa) * 2;
1119 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1120 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1121 return ERR_PTR(-EMSGSIZE);
1122 new_acts_size = MAX_ACTIONS_BUFSIZE;
1125 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1127 return (void *)acts;
1129 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1130 acts->actions_len = (*sfa)->actions_len;
1135 (*sfa)->actions_len += req_size;
1136 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1139 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1143 a = reserve_sfa_size(sfa, nla_attr_size(len));
1147 a->nla_type = attrtype;
1148 a->nla_len = nla_attr_size(len);
1151 memcpy(nla_data(a), data, len);
1152 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1157 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1160 int used = (*sfa)->actions_len;
1163 err = add_action(sfa, attrtype, NULL, 0);
1170 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1173 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1176 a->nla_len = sfa->actions_len - st_offset;
1179 static int validate_and_copy_sample(const struct nlattr *attr,
1180 const struct sw_flow_key *key, int depth,
1181 struct sw_flow_actions **sfa)
1183 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1184 const struct nlattr *probability, *actions;
1185 const struct nlattr *a;
1186 int rem, start, err, st_acts;
1188 memset(attrs, 0, sizeof(attrs));
1189 nla_for_each_nested(a, attr, rem) {
1190 int type = nla_type(a);
1191 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1198 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1199 if (!probability || nla_len(probability) != sizeof(u32))
1202 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1203 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1206 /* validation done, copy sample action. */
1207 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1210 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1211 nla_data(probability), sizeof(u32));
1214 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1218 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1222 add_nested_action_end(*sfa, st_acts);
1223 add_nested_action_end(*sfa, start);
1228 static int validate_tp_port(const struct sw_flow_key *flow_key)
1230 if ((flow_key->eth.type == htons(ETH_P_IP) ||
1231 flow_key->eth.type == htons(ETH_P_IPV6)) &&
1232 (flow_key->tp.src || flow_key->tp.dst))
1238 void ovs_match_init(struct sw_flow_match *match,
1239 struct sw_flow_key *key,
1240 struct sw_flow_mask *mask)
1242 memset(match, 0, sizeof(*match));
1246 memset(key, 0, sizeof(*key));
1249 memset(&mask->key, 0, sizeof(mask->key));
1250 mask->range.start = mask->range.end = 0;
1254 static int validate_and_copy_set_tun(const struct nlattr *attr,
1255 struct sw_flow_actions **sfa)
1257 struct sw_flow_match match;
1258 struct sw_flow_key key;
1261 ovs_match_init(&match, &key, NULL);
1262 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1266 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1270 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1271 sizeof(match.key->tun_key));
1272 add_nested_action_end(*sfa, start);
1277 static int validate_set(const struct nlattr *a,
1278 const struct sw_flow_key *flow_key,
1279 struct sw_flow_actions **sfa,
1282 const struct nlattr *ovs_key = nla_data(a);
1283 int key_type = nla_type(ovs_key);
1285 /* There can be only one key in a action */
1286 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1289 if (key_type > OVS_KEY_ATTR_MAX ||
1290 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1291 ovs_key_lens[key_type] != -1))
1295 const struct ovs_key_ipv4 *ipv4_key;
1296 const struct ovs_key_ipv6 *ipv6_key;
1299 case OVS_KEY_ATTR_PRIORITY:
1300 case OVS_KEY_ATTR_SKB_MARK:
1301 case OVS_KEY_ATTR_ETHERNET:
1304 case OVS_KEY_ATTR_TUNNEL:
1306 err = validate_and_copy_set_tun(a, sfa);
1311 case OVS_KEY_ATTR_IPV4:
1312 if (flow_key->eth.type != htons(ETH_P_IP))
1315 if (!flow_key->ip.proto)
1318 ipv4_key = nla_data(ovs_key);
1319 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1322 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1327 case OVS_KEY_ATTR_IPV6:
1328 if (flow_key->eth.type != htons(ETH_P_IPV6))
1331 if (!flow_key->ip.proto)
1334 ipv6_key = nla_data(ovs_key);
1335 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1338 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1341 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1346 case OVS_KEY_ATTR_TCP:
1347 if (flow_key->ip.proto != IPPROTO_TCP)
1350 return validate_tp_port(flow_key);
1352 case OVS_KEY_ATTR_UDP:
1353 if (flow_key->ip.proto != IPPROTO_UDP)
1356 return validate_tp_port(flow_key);
1358 case OVS_KEY_ATTR_SCTP:
1359 if (flow_key->ip.proto != IPPROTO_SCTP)
1362 return validate_tp_port(flow_key);
1371 static int validate_userspace(const struct nlattr *attr)
1373 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1374 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1375 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1377 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1380 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1381 attr, userspace_policy);
1385 if (!a[OVS_USERSPACE_ATTR_PID] ||
1386 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1392 static int copy_action(const struct nlattr *from,
1393 struct sw_flow_actions **sfa)
1395 int totlen = NLA_ALIGN(from->nla_len);
1398 to = reserve_sfa_size(sfa, from->nla_len);
1402 memcpy(to, from, totlen);
1406 int ovs_nla_copy_actions(const struct nlattr *attr,
1407 const struct sw_flow_key *key,
1409 struct sw_flow_actions **sfa)
1411 const struct nlattr *a;
1414 if (depth >= SAMPLE_ACTION_DEPTH)
1417 nla_for_each_nested(a, attr, rem) {
1418 /* Expected argument lengths, (u32)-1 for variable length. */
1419 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1420 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1421 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1422 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1423 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1424 [OVS_ACTION_ATTR_SET] = (u32)-1,
1425 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
1427 const struct ovs_action_push_vlan *vlan;
1428 int type = nla_type(a);
1431 if (type > OVS_ACTION_ATTR_MAX ||
1432 (action_lens[type] != nla_len(a) &&
1433 action_lens[type] != (u32)-1))
1438 case OVS_ACTION_ATTR_UNSPEC:
1441 case OVS_ACTION_ATTR_USERSPACE:
1442 err = validate_userspace(a);
1447 case OVS_ACTION_ATTR_OUTPUT:
1448 if (nla_get_u32(a) >= DP_MAX_PORTS)
1453 case OVS_ACTION_ATTR_POP_VLAN:
1456 case OVS_ACTION_ATTR_PUSH_VLAN:
1458 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1460 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1464 case OVS_ACTION_ATTR_SET:
1465 err = validate_set(a, key, sfa, &skip_copy);
1470 case OVS_ACTION_ATTR_SAMPLE:
1471 err = validate_and_copy_sample(a, key, depth, sfa);
1481 err = copy_action(a, sfa);
1493 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1495 const struct nlattr *a;
1496 struct nlattr *start;
1499 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1503 nla_for_each_nested(a, attr, rem) {
1504 int type = nla_type(a);
1505 struct nlattr *st_sample;
1508 case OVS_SAMPLE_ATTR_PROBABILITY:
1509 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1510 sizeof(u32), nla_data(a)))
1513 case OVS_SAMPLE_ATTR_ACTIONS:
1514 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1517 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1520 nla_nest_end(skb, st_sample);
1525 nla_nest_end(skb, start);
1529 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1531 const struct nlattr *ovs_key = nla_data(a);
1532 int key_type = nla_type(ovs_key);
1533 struct nlattr *start;
1537 case OVS_KEY_ATTR_IPV4_TUNNEL:
1538 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1542 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1546 nla_nest_end(skb, start);
1549 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1557 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1559 const struct nlattr *a;
1562 nla_for_each_attr(a, attr, len, rem) {
1563 int type = nla_type(a);
1566 case OVS_ACTION_ATTR_SET:
1567 err = set_action_to_attr(a, skb);
1572 case OVS_ACTION_ATTR_SAMPLE:
1573 err = sample_action_to_attr(a, skb);
1578 if (nla_put(skb, type, nla_len(a), nla_data(a)))