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 = 1 << 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 &= ~((1 << OVS_KEY_ATTR_IPV4)
117 | (1 << OVS_KEY_ATTR_IPV6)
118 | (1 << OVS_KEY_ATTR_TCP)
119 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
120 | (1 << OVS_KEY_ATTR_UDP)
121 | (1 << OVS_KEY_ATTR_SCTP)
122 | (1 << OVS_KEY_ATTR_ICMP)
123 | (1 << OVS_KEY_ATTR_ICMPV6)
124 | (1 << OVS_KEY_ATTR_ARP)
125 | (1 << OVS_KEY_ATTR_ND));
127 /* Always allowed mask fields. */
128 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
129 | (1 << OVS_KEY_ATTR_IN_PORT)
130 | (1 << OVS_KEY_ATTR_ETHERTYPE));
132 /* Check key attributes. */
133 if (match->key->eth.type == htons(ETH_P_ARP)
134 || match->key->eth.type == htons(ETH_P_RARP)) {
135 key_expected |= 1 << OVS_KEY_ATTR_ARP;
136 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
137 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
140 if (match->key->eth.type == htons(ETH_P_IP)) {
141 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
142 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
143 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
145 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
146 if (match->key->ip.proto == IPPROTO_UDP) {
147 key_expected |= 1 << OVS_KEY_ATTR_UDP;
148 if (match->mask && (match->mask->key.ip.proto == 0xff))
149 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
152 if (match->key->ip.proto == IPPROTO_SCTP) {
153 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
154 if (match->mask && (match->mask->key.ip.proto == 0xff))
155 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
158 if (match->key->ip.proto == IPPROTO_TCP) {
159 key_expected |= 1 << OVS_KEY_ATTR_TCP;
160 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
161 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
162 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
163 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
167 if (match->key->ip.proto == IPPROTO_ICMP) {
168 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
169 if (match->mask && (match->mask->key.ip.proto == 0xff))
170 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
175 if (match->key->eth.type == htons(ETH_P_IPV6)) {
176 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
177 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
178 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
180 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
181 if (match->key->ip.proto == IPPROTO_UDP) {
182 key_expected |= 1 << OVS_KEY_ATTR_UDP;
183 if (match->mask && (match->mask->key.ip.proto == 0xff))
184 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
187 if (match->key->ip.proto == IPPROTO_SCTP) {
188 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
189 if (match->mask && (match->mask->key.ip.proto == 0xff))
190 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
193 if (match->key->ip.proto == IPPROTO_TCP) {
194 key_expected |= 1 << OVS_KEY_ATTR_TCP;
195 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
196 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
197 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
198 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
202 if (match->key->ip.proto == IPPROTO_ICMPV6) {
203 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
207 if (match->key->ipv6.tp.src ==
208 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
209 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
210 key_expected |= 1 << OVS_KEY_ATTR_ND;
211 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
212 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
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 int __parse_flow_nlattrs(const struct nlattr *attr,
272 const struct nlattr *a[],
273 u64 *attrsp, bool nz)
275 const struct nlattr *nla;
280 nla_for_each_nested(nla, attr, rem) {
281 u16 type = nla_type(nla);
284 if (type > OVS_KEY_ATTR_MAX) {
285 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
286 type, OVS_KEY_ATTR_MAX);
290 if (attrs & (1 << type)) {
291 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
295 expected_len = ovs_key_lens[type];
296 if (nla_len(nla) != expected_len && expected_len != -1) {
297 OVS_NLERR("Key attribute has unexpected length (type=%d"
298 ", length=%d, expected=%d).\n", type,
299 nla_len(nla), expected_len);
303 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
309 OVS_NLERR("Message has %d unknown bytes.\n", rem);
317 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
318 const struct nlattr *a[], u64 *attrsp)
320 return __parse_flow_nlattrs(attr, a, attrsp, true);
323 static int parse_flow_nlattrs(const struct nlattr *attr,
324 const struct nlattr *a[], u64 *attrsp)
326 return __parse_flow_nlattrs(attr, a, attrsp, false);
329 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
330 struct sw_flow_match *match, bool is_mask)
335 __be16 tun_flags = 0;
337 nla_for_each_nested(a, attr, rem) {
338 int type = nla_type(a);
339 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
340 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
341 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
342 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
343 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
344 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
345 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
346 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
349 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
350 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
351 type, OVS_TUNNEL_KEY_ATTR_MAX);
355 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
356 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
357 " length (type=%d, length=%d, expected=%d).\n",
358 type, nla_len(a), ovs_tunnel_key_lens[type]);
363 case OVS_TUNNEL_KEY_ATTR_ID:
364 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
365 nla_get_be64(a), is_mask);
366 tun_flags |= TUNNEL_KEY;
368 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
369 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
370 nla_get_be32(a), is_mask);
372 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
373 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
374 nla_get_be32(a), is_mask);
376 case OVS_TUNNEL_KEY_ATTR_TOS:
377 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
378 nla_get_u8(a), is_mask);
380 case OVS_TUNNEL_KEY_ATTR_TTL:
381 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
382 nla_get_u8(a), is_mask);
385 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
386 tun_flags |= TUNNEL_DONT_FRAGMENT;
388 case OVS_TUNNEL_KEY_ATTR_CSUM:
389 tun_flags |= TUNNEL_CSUM;
396 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
399 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
404 if (!match->key->tun_key.ipv4_dst) {
405 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
410 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
418 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
419 const struct ovs_key_ipv4_tunnel *tun_key,
420 const struct ovs_key_ipv4_tunnel *output)
424 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
428 if (output->tun_flags & TUNNEL_KEY &&
429 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
431 if (output->ipv4_src &&
432 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
434 if (output->ipv4_dst &&
435 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
437 if (output->ipv4_tos &&
438 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
440 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
442 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
443 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
445 if ((output->tun_flags & TUNNEL_CSUM) &&
446 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
449 nla_nest_end(skb, nla);
454 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
455 const struct nlattr **a, bool is_mask)
457 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
458 SW_FLOW_KEY_PUT(match, phy.priority,
459 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
460 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
463 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
464 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
467 in_port = 0xffffffff; /* Always exact match in_port. */
468 else if (in_port >= DP_MAX_PORTS)
471 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
472 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
473 } else if (!is_mask) {
474 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
477 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
478 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
480 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
481 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
483 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
484 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
487 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
492 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
493 const struct nlattr **a, bool is_mask)
496 u64 orig_attrs = attrs;
498 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
502 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
503 const struct ovs_key_ethernet *eth_key;
505 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
506 SW_FLOW_KEY_MEMCPY(match, eth.src,
507 eth_key->eth_src, ETH_ALEN, is_mask);
508 SW_FLOW_KEY_MEMCPY(match, eth.dst,
509 eth_key->eth_dst, ETH_ALEN, is_mask);
510 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
513 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
516 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
517 if (!(tci & htons(VLAN_TAG_PRESENT))) {
519 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
521 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
526 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
527 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
529 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
531 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
534 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
536 /* Always exact match EtherType. */
537 eth_type = htons(0xffff);
538 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
539 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
540 ntohs(eth_type), ETH_P_802_3_MIN);
544 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
545 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
546 } else if (!is_mask) {
547 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
550 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
551 const struct ovs_key_ipv4 *ipv4_key;
553 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
554 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
555 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
556 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
559 SW_FLOW_KEY_PUT(match, ip.proto,
560 ipv4_key->ipv4_proto, is_mask);
561 SW_FLOW_KEY_PUT(match, ip.tos,
562 ipv4_key->ipv4_tos, is_mask);
563 SW_FLOW_KEY_PUT(match, ip.ttl,
564 ipv4_key->ipv4_ttl, is_mask);
565 SW_FLOW_KEY_PUT(match, ip.frag,
566 ipv4_key->ipv4_frag, is_mask);
567 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
568 ipv4_key->ipv4_src, is_mask);
569 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
570 ipv4_key->ipv4_dst, is_mask);
571 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
574 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
575 const struct ovs_key_ipv6 *ipv6_key;
577 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
578 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
579 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
580 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
583 SW_FLOW_KEY_PUT(match, ipv6.label,
584 ipv6_key->ipv6_label, is_mask);
585 SW_FLOW_KEY_PUT(match, ip.proto,
586 ipv6_key->ipv6_proto, is_mask);
587 SW_FLOW_KEY_PUT(match, ip.tos,
588 ipv6_key->ipv6_tclass, is_mask);
589 SW_FLOW_KEY_PUT(match, ip.ttl,
590 ipv6_key->ipv6_hlimit, is_mask);
591 SW_FLOW_KEY_PUT(match, ip.frag,
592 ipv6_key->ipv6_frag, is_mask);
593 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
595 sizeof(match->key->ipv6.addr.src),
597 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
599 sizeof(match->key->ipv6.addr.dst),
602 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
605 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
606 const struct ovs_key_arp *arp_key;
608 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
609 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
610 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
615 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
616 arp_key->arp_sip, is_mask);
617 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
618 arp_key->arp_tip, is_mask);
619 SW_FLOW_KEY_PUT(match, ip.proto,
620 ntohs(arp_key->arp_op), is_mask);
621 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
622 arp_key->arp_sha, ETH_ALEN, is_mask);
623 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
624 arp_key->arp_tha, ETH_ALEN, is_mask);
626 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
629 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
630 const struct ovs_key_tcp *tcp_key;
632 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
633 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
634 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
635 tcp_key->tcp_src, is_mask);
636 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
637 tcp_key->tcp_dst, is_mask);
639 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
640 tcp_key->tcp_src, is_mask);
641 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
642 tcp_key->tcp_dst, is_mask);
644 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
647 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
648 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
649 SW_FLOW_KEY_PUT(match, ipv4.tp.flags,
650 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
653 SW_FLOW_KEY_PUT(match, ipv6.tp.flags,
654 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
657 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
660 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
661 const struct ovs_key_udp *udp_key;
663 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
664 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
665 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
666 udp_key->udp_src, is_mask);
667 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
668 udp_key->udp_dst, is_mask);
670 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
671 udp_key->udp_src, is_mask);
672 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
673 udp_key->udp_dst, is_mask);
675 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
678 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
679 const struct ovs_key_sctp *sctp_key;
681 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
682 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
683 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
684 sctp_key->sctp_src, is_mask);
685 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
686 sctp_key->sctp_dst, is_mask);
688 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
689 sctp_key->sctp_src, is_mask);
690 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
691 sctp_key->sctp_dst, is_mask);
693 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
696 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
697 const struct ovs_key_icmp *icmp_key;
699 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
700 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
701 htons(icmp_key->icmp_type), is_mask);
702 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
703 htons(icmp_key->icmp_code), is_mask);
704 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
707 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
708 const struct ovs_key_icmpv6 *icmpv6_key;
710 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
711 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
712 htons(icmpv6_key->icmpv6_type), is_mask);
713 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
714 htons(icmpv6_key->icmpv6_code), is_mask);
715 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
718 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
719 const struct ovs_key_nd *nd_key;
721 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
722 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
724 sizeof(match->key->ipv6.nd.target),
726 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
727 nd_key->nd_sll, ETH_ALEN, is_mask);
728 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
729 nd_key->nd_tll, ETH_ALEN, is_mask);
730 attrs &= ~(1 << OVS_KEY_ATTR_ND);
739 static void sw_flow_mask_set(struct sw_flow_mask *mask,
740 struct sw_flow_key_range *range, u8 val)
742 u8 *m = (u8 *)&mask->key + range->start;
744 mask->range = *range;
745 memset(m, val, range_n_bytes(range));
749 * ovs_nla_get_match - parses Netlink attributes into a flow key and
750 * mask. In case the 'mask' is NULL, the flow is treated as exact match
751 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
752 * does not include any don't care bit.
753 * @match: receives the extracted flow match information.
754 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
755 * sequence. The fields should of the packet that triggered the creation
757 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
758 * attribute specifies the mask field of the wildcarded flow.
760 int ovs_nla_get_match(struct sw_flow_match *match,
761 const struct nlattr *key,
762 const struct nlattr *mask)
764 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
765 const struct nlattr *encap;
768 bool encap_valid = false;
771 err = parse_flow_nlattrs(key, a, &key_attrs);
775 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
776 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
777 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
780 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
781 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
782 OVS_NLERR("Invalid Vlan frame.\n");
786 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
787 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
788 encap = a[OVS_KEY_ATTR_ENCAP];
789 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
792 if (tci & htons(VLAN_TAG_PRESENT)) {
793 err = parse_flow_nlattrs(encap, a, &key_attrs);
797 /* Corner case for truncated 802.1Q header. */
798 if (nla_len(encap)) {
799 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
803 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
808 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
813 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
817 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
822 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
826 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
827 if (a[OVS_KEY_ATTR_ETHERTYPE])
828 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
830 if (eth_type == htons(0xffff)) {
831 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
832 encap = a[OVS_KEY_ATTR_ENCAP];
833 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
835 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
840 if (a[OVS_KEY_ATTR_VLAN])
841 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
843 if (!(tci & htons(VLAN_TAG_PRESENT))) {
844 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
849 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
853 /* Populate exact match flow's key mask. */
855 sw_flow_mask_set(match->mask, &match->range, 0xff);
858 if (!match_validate(match, key_attrs, mask_attrs))
865 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
866 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
867 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
870 * This parses a series of Netlink attributes that form a flow key, which must
871 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
872 * get the metadata, that is, the parts of the flow key that cannot be
873 * extracted from the packet itself.
876 int ovs_nla_get_flow_metadata(struct sw_flow *flow,
877 const struct nlattr *attr)
879 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
880 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
883 struct sw_flow_match match;
885 flow->key.phy.in_port = DP_MAX_PORTS;
886 flow->key.phy.priority = 0;
887 flow->key.phy.skb_mark = 0;
888 memset(tun_key, 0, sizeof(flow->key.tun_key));
890 err = parse_flow_nlattrs(attr, a, &attrs);
894 memset(&match, 0, sizeof(match));
895 match.key = &flow->key;
897 err = metadata_from_nlattrs(&match, &attrs, a, false);
904 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
905 const struct sw_flow_key *output, struct sk_buff *skb)
907 struct ovs_key_ethernet *eth_key;
908 struct nlattr *nla, *encap;
909 bool is_mask = (swkey != output);
911 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
912 goto nla_put_failure;
914 if ((swkey->tun_key.ipv4_dst || is_mask) &&
915 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
916 goto nla_put_failure;
918 if (swkey->phy.in_port == DP_MAX_PORTS) {
919 if (is_mask && (output->phy.in_port == 0xffff))
920 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
921 goto nla_put_failure;
924 upper_u16 = !is_mask ? 0 : 0xffff;
926 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
927 (upper_u16 << 16) | output->phy.in_port))
928 goto nla_put_failure;
931 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
932 goto nla_put_failure;
934 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
936 goto nla_put_failure;
938 eth_key = nla_data(nla);
939 ether_addr_copy(eth_key->eth_src, output->eth.src);
940 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
942 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
944 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
945 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
946 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
947 goto nla_put_failure;
948 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
954 if (swkey->eth.type == htons(ETH_P_802_2)) {
956 * Ethertype 802.2 is represented in the netlink with omitted
957 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
958 * 0xffff in the mask attribute. Ethertype can also
961 if (is_mask && output->eth.type)
962 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
964 goto nla_put_failure;
968 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
969 goto nla_put_failure;
971 if (swkey->eth.type == htons(ETH_P_IP)) {
972 struct ovs_key_ipv4 *ipv4_key;
974 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
976 goto nla_put_failure;
977 ipv4_key = nla_data(nla);
978 ipv4_key->ipv4_src = output->ipv4.addr.src;
979 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
980 ipv4_key->ipv4_proto = output->ip.proto;
981 ipv4_key->ipv4_tos = output->ip.tos;
982 ipv4_key->ipv4_ttl = output->ip.ttl;
983 ipv4_key->ipv4_frag = output->ip.frag;
984 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
985 struct ovs_key_ipv6 *ipv6_key;
987 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
989 goto nla_put_failure;
990 ipv6_key = nla_data(nla);
991 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
992 sizeof(ipv6_key->ipv6_src));
993 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
994 sizeof(ipv6_key->ipv6_dst));
995 ipv6_key->ipv6_label = output->ipv6.label;
996 ipv6_key->ipv6_proto = output->ip.proto;
997 ipv6_key->ipv6_tclass = output->ip.tos;
998 ipv6_key->ipv6_hlimit = output->ip.ttl;
999 ipv6_key->ipv6_frag = output->ip.frag;
1000 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1001 swkey->eth.type == htons(ETH_P_RARP)) {
1002 struct ovs_key_arp *arp_key;
1004 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1006 goto nla_put_failure;
1007 arp_key = nla_data(nla);
1008 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1009 arp_key->arp_sip = output->ipv4.addr.src;
1010 arp_key->arp_tip = output->ipv4.addr.dst;
1011 arp_key->arp_op = htons(output->ip.proto);
1012 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1013 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1016 if ((swkey->eth.type == htons(ETH_P_IP) ||
1017 swkey->eth.type == htons(ETH_P_IPV6)) &&
1018 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1020 if (swkey->ip.proto == IPPROTO_TCP) {
1021 struct ovs_key_tcp *tcp_key;
1023 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1025 goto nla_put_failure;
1026 tcp_key = nla_data(nla);
1027 if (swkey->eth.type == htons(ETH_P_IP)) {
1028 tcp_key->tcp_src = output->ipv4.tp.src;
1029 tcp_key->tcp_dst = output->ipv4.tp.dst;
1030 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1031 output->ipv4.tp.flags))
1032 goto nla_put_failure;
1033 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1034 tcp_key->tcp_src = output->ipv6.tp.src;
1035 tcp_key->tcp_dst = output->ipv6.tp.dst;
1036 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1037 output->ipv6.tp.flags))
1038 goto nla_put_failure;
1040 } else if (swkey->ip.proto == IPPROTO_UDP) {
1041 struct ovs_key_udp *udp_key;
1043 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1045 goto nla_put_failure;
1046 udp_key = nla_data(nla);
1047 if (swkey->eth.type == htons(ETH_P_IP)) {
1048 udp_key->udp_src = output->ipv4.tp.src;
1049 udp_key->udp_dst = output->ipv4.tp.dst;
1050 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1051 udp_key->udp_src = output->ipv6.tp.src;
1052 udp_key->udp_dst = output->ipv6.tp.dst;
1054 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1055 struct ovs_key_sctp *sctp_key;
1057 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1059 goto nla_put_failure;
1060 sctp_key = nla_data(nla);
1061 if (swkey->eth.type == htons(ETH_P_IP)) {
1062 sctp_key->sctp_src = output->ipv4.tp.src;
1063 sctp_key->sctp_dst = output->ipv4.tp.dst;
1064 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1065 sctp_key->sctp_src = output->ipv6.tp.src;
1066 sctp_key->sctp_dst = output->ipv6.tp.dst;
1068 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1069 swkey->ip.proto == IPPROTO_ICMP) {
1070 struct ovs_key_icmp *icmp_key;
1072 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1074 goto nla_put_failure;
1075 icmp_key = nla_data(nla);
1076 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1077 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1078 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1079 swkey->ip.proto == IPPROTO_ICMPV6) {
1080 struct ovs_key_icmpv6 *icmpv6_key;
1082 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1083 sizeof(*icmpv6_key));
1085 goto nla_put_failure;
1086 icmpv6_key = nla_data(nla);
1087 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1088 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1090 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1091 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1092 struct ovs_key_nd *nd_key;
1094 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1096 goto nla_put_failure;
1097 nd_key = nla_data(nla);
1098 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1099 sizeof(nd_key->nd_target));
1100 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1101 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1108 nla_nest_end(skb, encap);
1116 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1118 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1120 struct sw_flow_actions *sfa;
1122 if (size > MAX_ACTIONS_BUFSIZE)
1123 return ERR_PTR(-EINVAL);
1125 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1127 return ERR_PTR(-ENOMEM);
1129 sfa->actions_len = 0;
1133 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1134 * The caller must hold rcu_read_lock for this to be sensible. */
1135 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1137 kfree_rcu(sf_acts, rcu);
1140 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1144 struct sw_flow_actions *acts;
1146 int req_size = NLA_ALIGN(attr_len);
1147 int next_offset = offsetof(struct sw_flow_actions, actions) +
1148 (*sfa)->actions_len;
1150 if (req_size <= (ksize(*sfa) - next_offset))
1153 new_acts_size = ksize(*sfa) * 2;
1155 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1156 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1157 return ERR_PTR(-EMSGSIZE);
1158 new_acts_size = MAX_ACTIONS_BUFSIZE;
1161 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1163 return (void *)acts;
1165 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1166 acts->actions_len = (*sfa)->actions_len;
1171 (*sfa)->actions_len += req_size;
1172 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1175 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1179 a = reserve_sfa_size(sfa, nla_attr_size(len));
1183 a->nla_type = attrtype;
1184 a->nla_len = nla_attr_size(len);
1187 memcpy(nla_data(a), data, len);
1188 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1193 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1196 int used = (*sfa)->actions_len;
1199 err = add_action(sfa, attrtype, NULL, 0);
1206 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1209 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1212 a->nla_len = sfa->actions_len - st_offset;
1215 static int validate_and_copy_sample(const struct nlattr *attr,
1216 const struct sw_flow_key *key, int depth,
1217 struct sw_flow_actions **sfa)
1219 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1220 const struct nlattr *probability, *actions;
1221 const struct nlattr *a;
1222 int rem, start, err, st_acts;
1224 memset(attrs, 0, sizeof(attrs));
1225 nla_for_each_nested(a, attr, rem) {
1226 int type = nla_type(a);
1227 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1234 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1235 if (!probability || nla_len(probability) != sizeof(u32))
1238 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1239 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1242 /* validation done, copy sample action. */
1243 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1246 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1247 nla_data(probability), sizeof(u32));
1250 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1254 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1258 add_nested_action_end(*sfa, st_acts);
1259 add_nested_action_end(*sfa, start);
1264 static int validate_tp_port(const struct sw_flow_key *flow_key)
1266 if (flow_key->eth.type == htons(ETH_P_IP)) {
1267 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
1269 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
1270 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
1277 void ovs_match_init(struct sw_flow_match *match,
1278 struct sw_flow_key *key,
1279 struct sw_flow_mask *mask)
1281 memset(match, 0, sizeof(*match));
1285 memset(key, 0, sizeof(*key));
1288 memset(&mask->key, 0, sizeof(mask->key));
1289 mask->range.start = mask->range.end = 0;
1293 static int validate_and_copy_set_tun(const struct nlattr *attr,
1294 struct sw_flow_actions **sfa)
1296 struct sw_flow_match match;
1297 struct sw_flow_key key;
1300 ovs_match_init(&match, &key, NULL);
1301 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1305 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1309 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1310 sizeof(match.key->tun_key));
1311 add_nested_action_end(*sfa, start);
1316 static int validate_set(const struct nlattr *a,
1317 const struct sw_flow_key *flow_key,
1318 struct sw_flow_actions **sfa,
1321 const struct nlattr *ovs_key = nla_data(a);
1322 int key_type = nla_type(ovs_key);
1324 /* There can be only one key in a action */
1325 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1328 if (key_type > OVS_KEY_ATTR_MAX ||
1329 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1330 ovs_key_lens[key_type] != -1))
1334 const struct ovs_key_ipv4 *ipv4_key;
1335 const struct ovs_key_ipv6 *ipv6_key;
1338 case OVS_KEY_ATTR_PRIORITY:
1339 case OVS_KEY_ATTR_SKB_MARK:
1340 case OVS_KEY_ATTR_ETHERNET:
1343 case OVS_KEY_ATTR_TUNNEL:
1345 err = validate_and_copy_set_tun(a, sfa);
1350 case OVS_KEY_ATTR_IPV4:
1351 if (flow_key->eth.type != htons(ETH_P_IP))
1354 if (!flow_key->ip.proto)
1357 ipv4_key = nla_data(ovs_key);
1358 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1361 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1366 case OVS_KEY_ATTR_IPV6:
1367 if (flow_key->eth.type != htons(ETH_P_IPV6))
1370 if (!flow_key->ip.proto)
1373 ipv6_key = nla_data(ovs_key);
1374 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1377 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1380 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1385 case OVS_KEY_ATTR_TCP:
1386 if (flow_key->ip.proto != IPPROTO_TCP)
1389 return validate_tp_port(flow_key);
1391 case OVS_KEY_ATTR_UDP:
1392 if (flow_key->ip.proto != IPPROTO_UDP)
1395 return validate_tp_port(flow_key);
1397 case OVS_KEY_ATTR_SCTP:
1398 if (flow_key->ip.proto != IPPROTO_SCTP)
1401 return validate_tp_port(flow_key);
1410 static int validate_userspace(const struct nlattr *attr)
1412 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1413 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1414 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1416 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1419 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1420 attr, userspace_policy);
1424 if (!a[OVS_USERSPACE_ATTR_PID] ||
1425 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1431 static int copy_action(const struct nlattr *from,
1432 struct sw_flow_actions **sfa)
1434 int totlen = NLA_ALIGN(from->nla_len);
1437 to = reserve_sfa_size(sfa, from->nla_len);
1441 memcpy(to, from, totlen);
1445 int ovs_nla_copy_actions(const struct nlattr *attr,
1446 const struct sw_flow_key *key,
1448 struct sw_flow_actions **sfa)
1450 const struct nlattr *a;
1453 if (depth >= SAMPLE_ACTION_DEPTH)
1456 nla_for_each_nested(a, attr, rem) {
1457 /* Expected argument lengths, (u32)-1 for variable length. */
1458 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1459 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1460 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1461 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1462 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1463 [OVS_ACTION_ATTR_SET] = (u32)-1,
1464 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
1466 const struct ovs_action_push_vlan *vlan;
1467 int type = nla_type(a);
1470 if (type > OVS_ACTION_ATTR_MAX ||
1471 (action_lens[type] != nla_len(a) &&
1472 action_lens[type] != (u32)-1))
1477 case OVS_ACTION_ATTR_UNSPEC:
1480 case OVS_ACTION_ATTR_USERSPACE:
1481 err = validate_userspace(a);
1486 case OVS_ACTION_ATTR_OUTPUT:
1487 if (nla_get_u32(a) >= DP_MAX_PORTS)
1492 case OVS_ACTION_ATTR_POP_VLAN:
1495 case OVS_ACTION_ATTR_PUSH_VLAN:
1497 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1499 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1503 case OVS_ACTION_ATTR_SET:
1504 err = validate_set(a, key, sfa, &skip_copy);
1509 case OVS_ACTION_ATTR_SAMPLE:
1510 err = validate_and_copy_sample(a, key, depth, sfa);
1520 err = copy_action(a, sfa);
1532 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1534 const struct nlattr *a;
1535 struct nlattr *start;
1538 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1542 nla_for_each_nested(a, attr, rem) {
1543 int type = nla_type(a);
1544 struct nlattr *st_sample;
1547 case OVS_SAMPLE_ATTR_PROBABILITY:
1548 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1549 sizeof(u32), nla_data(a)))
1552 case OVS_SAMPLE_ATTR_ACTIONS:
1553 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1556 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1559 nla_nest_end(skb, st_sample);
1564 nla_nest_end(skb, start);
1568 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1570 const struct nlattr *ovs_key = nla_data(a);
1571 int key_type = nla_type(ovs_key);
1572 struct nlattr *start;
1576 case OVS_KEY_ATTR_IPV4_TUNNEL:
1577 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1581 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1585 nla_nest_end(skb, start);
1588 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1596 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1598 const struct nlattr *a;
1601 nla_for_each_attr(a, attr, len, rem) {
1602 int type = nla_type(a);
1605 case OVS_ACTION_ATTR_SET:
1606 err = set_action_to_attr(a, skb);
1611 case OVS_ACTION_ATTR_SAMPLE:
1612 err = sample_action_to_attr(a, skb);
1617 if (nla_put(skb, type, nla_len(a), nla_data(a)))