2 * Copyright (c) 2007-2014 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>
45 #include <net/geneve.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_OFFSET(match, offset, value_p, len, is_mask) \
94 update_range__(match, offset, len, is_mask); \
96 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
99 memcpy((u8 *)(match)->key + offset, value_p, len); \
102 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
103 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
104 value_p, len, is_mask)
106 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
108 update_range__(match, offsetof(struct sw_flow_key, field), \
109 sizeof((match)->key->field), is_mask); \
112 memset((u8 *)&(match)->mask->key.field, value,\
113 sizeof((match)->mask->key.field)); \
115 memset((u8 *)&(match)->key->field, value, \
116 sizeof((match)->key->field)); \
120 static bool match_validate(const struct sw_flow_match *match,
121 u64 key_attrs, u64 mask_attrs)
123 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
124 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
126 /* The following mask attributes allowed only if they
127 * pass the validation tests. */
128 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
129 | (1 << OVS_KEY_ATTR_IPV6)
130 | (1 << OVS_KEY_ATTR_TCP)
131 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
132 | (1 << OVS_KEY_ATTR_UDP)
133 | (1 << OVS_KEY_ATTR_SCTP)
134 | (1 << OVS_KEY_ATTR_ICMP)
135 | (1 << OVS_KEY_ATTR_ICMPV6)
136 | (1 << OVS_KEY_ATTR_ARP)
137 | (1 << OVS_KEY_ATTR_ND));
139 /* Always allowed mask fields. */
140 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
141 | (1 << OVS_KEY_ATTR_IN_PORT)
142 | (1 << OVS_KEY_ATTR_ETHERTYPE));
144 /* Check key attributes. */
145 if (match->key->eth.type == htons(ETH_P_ARP)
146 || match->key->eth.type == htons(ETH_P_RARP)) {
147 key_expected |= 1 << OVS_KEY_ATTR_ARP;
148 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
149 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
152 if (match->key->eth.type == htons(ETH_P_IP)) {
153 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
155 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
157 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
158 if (match->key->ip.proto == IPPROTO_UDP) {
159 key_expected |= 1 << OVS_KEY_ATTR_UDP;
160 if (match->mask && (match->mask->key.ip.proto == 0xff))
161 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
164 if (match->key->ip.proto == IPPROTO_SCTP) {
165 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
166 if (match->mask && (match->mask->key.ip.proto == 0xff))
167 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
170 if (match->key->ip.proto == IPPROTO_TCP) {
171 key_expected |= 1 << OVS_KEY_ATTR_TCP;
172 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
173 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
174 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
175 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
179 if (match->key->ip.proto == IPPROTO_ICMP) {
180 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
181 if (match->mask && (match->mask->key.ip.proto == 0xff))
182 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
187 if (match->key->eth.type == htons(ETH_P_IPV6)) {
188 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
189 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
190 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
192 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
193 if (match->key->ip.proto == IPPROTO_UDP) {
194 key_expected |= 1 << OVS_KEY_ATTR_UDP;
195 if (match->mask && (match->mask->key.ip.proto == 0xff))
196 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
199 if (match->key->ip.proto == IPPROTO_SCTP) {
200 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
201 if (match->mask && (match->mask->key.ip.proto == 0xff))
202 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
205 if (match->key->ip.proto == IPPROTO_TCP) {
206 key_expected |= 1 << OVS_KEY_ATTR_TCP;
207 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
208 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
209 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
210 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
214 if (match->key->ip.proto == IPPROTO_ICMPV6) {
215 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
216 if (match->mask && (match->mask->key.ip.proto == 0xff))
217 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
219 if (match->key->tp.src ==
220 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
221 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
222 key_expected |= 1 << OVS_KEY_ATTR_ND;
223 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
224 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
230 if ((key_attrs & key_expected) != key_expected) {
231 /* Key attributes check failed. */
232 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
233 (unsigned long long)key_attrs, (unsigned long long)key_expected);
237 if ((mask_attrs & mask_allowed) != mask_attrs) {
238 /* Mask attributes check failed. */
239 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
240 (unsigned long long)mask_attrs, (unsigned long long)mask_allowed);
247 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
248 static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
249 [OVS_KEY_ATTR_ENCAP] = -1,
250 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
251 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
252 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
253 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
254 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
255 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
256 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
257 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
258 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
259 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
260 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
261 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
262 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
263 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
264 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
265 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
266 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
267 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
268 [OVS_KEY_ATTR_TUNNEL] = -1,
271 static bool is_all_zero(const u8 *fp, size_t size)
278 for (i = 0; i < size; i++)
285 static int __parse_flow_nlattrs(const struct nlattr *attr,
286 const struct nlattr *a[],
287 u64 *attrsp, bool nz)
289 const struct nlattr *nla;
294 nla_for_each_nested(nla, attr, rem) {
295 u16 type = nla_type(nla);
298 if (type > OVS_KEY_ATTR_MAX) {
299 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
300 type, OVS_KEY_ATTR_MAX);
304 if (attrs & (1 << type)) {
305 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
309 expected_len = ovs_key_lens[type];
310 if (nla_len(nla) != expected_len && expected_len != -1) {
311 OVS_NLERR("Key attribute has unexpected length (type=%d"
312 ", length=%d, expected=%d).\n", type,
313 nla_len(nla), expected_len);
317 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
323 OVS_NLERR("Message has %d unknown bytes.\n", rem);
331 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
332 const struct nlattr *a[], u64 *attrsp)
334 return __parse_flow_nlattrs(attr, a, attrsp, true);
337 static int parse_flow_nlattrs(const struct nlattr *attr,
338 const struct nlattr *a[], u64 *attrsp)
340 return __parse_flow_nlattrs(attr, a, attrsp, false);
343 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
344 struct sw_flow_match *match, bool is_mask)
349 __be16 tun_flags = 0;
350 unsigned long opt_key_offset;
352 nla_for_each_nested(a, attr, rem) {
353 int type = nla_type(a);
354 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
355 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
356 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
357 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
358 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
359 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
360 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
361 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
362 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
363 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
366 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
367 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
368 type, OVS_TUNNEL_KEY_ATTR_MAX);
372 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
373 ovs_tunnel_key_lens[type] != -1) {
374 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
375 " length (type=%d, length=%d, expected=%d).\n",
376 type, nla_len(a), ovs_tunnel_key_lens[type]);
381 case OVS_TUNNEL_KEY_ATTR_ID:
382 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
383 nla_get_be64(a), is_mask);
384 tun_flags |= TUNNEL_KEY;
386 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
387 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
388 nla_get_be32(a), is_mask);
390 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
391 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
392 nla_get_be32(a), is_mask);
394 case OVS_TUNNEL_KEY_ATTR_TOS:
395 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
396 nla_get_u8(a), is_mask);
398 case OVS_TUNNEL_KEY_ATTR_TTL:
399 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
400 nla_get_u8(a), is_mask);
403 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
404 tun_flags |= TUNNEL_DONT_FRAGMENT;
406 case OVS_TUNNEL_KEY_ATTR_CSUM:
407 tun_flags |= TUNNEL_CSUM;
409 case OVS_TUNNEL_KEY_ATTR_OAM:
410 tun_flags |= TUNNEL_OAM;
412 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
413 tun_flags |= TUNNEL_OPTIONS_PRESENT;
414 if (nla_len(a) > sizeof(match->key->tun_opts)) {
415 OVS_NLERR("Geneve option length exceeds maximum size (len %d, max %zu).\n",
417 sizeof(match->key->tun_opts));
421 if (nla_len(a) % 4 != 0) {
422 OVS_NLERR("Geneve option length is not a multiple of 4 (len %d).\n",
427 /* We need to record the length of the options passed
428 * down, otherwise packets with the same format but
429 * additional options will be silently matched.
432 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
435 /* This is somewhat unusual because it looks at
436 * both the key and mask while parsing the
437 * attributes (and by extension assumes the key
438 * is parsed first). Normally, we would verify
439 * that each is the correct length and that the
440 * attributes line up in the validate function.
441 * However, that is difficult because this is
442 * variable length and we won't have the
445 if (match->key->tun_opts_len != nla_len(a)) {
446 OVS_NLERR("Geneve option key length (%d) is different from mask length (%d).",
447 match->key->tun_opts_len,
452 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff,
456 opt_key_offset = (unsigned long)GENEVE_OPTS(
457 (struct sw_flow_key *)0,
459 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset,
460 nla_data(a), nla_len(a),
464 OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n",
470 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
473 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
478 if (!match->key->tun_key.ipv4_dst) {
479 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
484 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
492 static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
493 const struct ovs_key_ipv4_tunnel *output,
494 const struct geneve_opt *tun_opts,
495 int swkey_tun_opts_len)
497 if (output->tun_flags & TUNNEL_KEY &&
498 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
500 if (output->ipv4_src &&
501 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
503 if (output->ipv4_dst &&
504 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
506 if (output->ipv4_tos &&
507 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
509 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
511 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
512 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
514 if ((output->tun_flags & TUNNEL_CSUM) &&
515 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
517 if ((output->tun_flags & TUNNEL_OAM) &&
518 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
521 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
522 swkey_tun_opts_len, tun_opts))
529 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
530 const struct ovs_key_ipv4_tunnel *output,
531 const struct geneve_opt *tun_opts,
532 int swkey_tun_opts_len)
537 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
541 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
545 nla_nest_end(skb, nla);
549 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
550 const struct nlattr **a, bool is_mask)
552 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
553 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
555 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
556 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
559 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
560 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
562 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
563 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
566 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
567 SW_FLOW_KEY_PUT(match, phy.priority,
568 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
569 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
572 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
573 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
576 in_port = 0xffffffff; /* Always exact match in_port. */
577 else if (in_port >= DP_MAX_PORTS)
580 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
581 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
582 } else if (!is_mask) {
583 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
586 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
587 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
589 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
590 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
592 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
593 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
596 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
601 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
602 const struct nlattr **a, bool is_mask)
605 u64 orig_attrs = attrs;
607 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
611 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
612 const struct ovs_key_ethernet *eth_key;
614 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
615 SW_FLOW_KEY_MEMCPY(match, eth.src,
616 eth_key->eth_src, ETH_ALEN, is_mask);
617 SW_FLOW_KEY_MEMCPY(match, eth.dst,
618 eth_key->eth_dst, ETH_ALEN, is_mask);
619 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
622 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
625 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
626 if (!(tci & htons(VLAN_TAG_PRESENT))) {
628 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
630 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
635 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
636 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
638 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
640 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
643 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
645 /* Always exact match EtherType. */
646 eth_type = htons(0xffff);
647 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
648 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
649 ntohs(eth_type), ETH_P_802_3_MIN);
653 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
654 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
655 } else if (!is_mask) {
656 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
659 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
660 const struct ovs_key_ipv4 *ipv4_key;
662 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
663 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
664 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
665 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
668 SW_FLOW_KEY_PUT(match, ip.proto,
669 ipv4_key->ipv4_proto, is_mask);
670 SW_FLOW_KEY_PUT(match, ip.tos,
671 ipv4_key->ipv4_tos, is_mask);
672 SW_FLOW_KEY_PUT(match, ip.ttl,
673 ipv4_key->ipv4_ttl, is_mask);
674 SW_FLOW_KEY_PUT(match, ip.frag,
675 ipv4_key->ipv4_frag, is_mask);
676 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
677 ipv4_key->ipv4_src, is_mask);
678 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
679 ipv4_key->ipv4_dst, is_mask);
680 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
683 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
684 const struct ovs_key_ipv6 *ipv6_key;
686 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
687 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
688 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
689 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
692 SW_FLOW_KEY_PUT(match, ipv6.label,
693 ipv6_key->ipv6_label, is_mask);
694 SW_FLOW_KEY_PUT(match, ip.proto,
695 ipv6_key->ipv6_proto, is_mask);
696 SW_FLOW_KEY_PUT(match, ip.tos,
697 ipv6_key->ipv6_tclass, is_mask);
698 SW_FLOW_KEY_PUT(match, ip.ttl,
699 ipv6_key->ipv6_hlimit, is_mask);
700 SW_FLOW_KEY_PUT(match, ip.frag,
701 ipv6_key->ipv6_frag, is_mask);
702 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
704 sizeof(match->key->ipv6.addr.src),
706 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
708 sizeof(match->key->ipv6.addr.dst),
711 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
714 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
715 const struct ovs_key_arp *arp_key;
717 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
718 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
719 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
724 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
725 arp_key->arp_sip, is_mask);
726 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
727 arp_key->arp_tip, is_mask);
728 SW_FLOW_KEY_PUT(match, ip.proto,
729 ntohs(arp_key->arp_op), is_mask);
730 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
731 arp_key->arp_sha, ETH_ALEN, is_mask);
732 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
733 arp_key->arp_tha, ETH_ALEN, is_mask);
735 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
738 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
739 const struct ovs_key_tcp *tcp_key;
741 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
742 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
743 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
744 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
747 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
748 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
749 SW_FLOW_KEY_PUT(match, tp.flags,
750 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
753 SW_FLOW_KEY_PUT(match, tp.flags,
754 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
757 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
760 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
761 const struct ovs_key_udp *udp_key;
763 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
764 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
765 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
766 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
769 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
770 const struct ovs_key_sctp *sctp_key;
772 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
773 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
774 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
775 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
778 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
779 const struct ovs_key_icmp *icmp_key;
781 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
782 SW_FLOW_KEY_PUT(match, tp.src,
783 htons(icmp_key->icmp_type), is_mask);
784 SW_FLOW_KEY_PUT(match, tp.dst,
785 htons(icmp_key->icmp_code), is_mask);
786 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
789 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
790 const struct ovs_key_icmpv6 *icmpv6_key;
792 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
793 SW_FLOW_KEY_PUT(match, tp.src,
794 htons(icmpv6_key->icmpv6_type), is_mask);
795 SW_FLOW_KEY_PUT(match, tp.dst,
796 htons(icmpv6_key->icmpv6_code), is_mask);
797 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
800 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
801 const struct ovs_key_nd *nd_key;
803 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
804 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
806 sizeof(match->key->ipv6.nd.target),
808 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
809 nd_key->nd_sll, ETH_ALEN, is_mask);
810 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
811 nd_key->nd_tll, ETH_ALEN, is_mask);
812 attrs &= ~(1 << OVS_KEY_ATTR_ND);
821 static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
826 /* The nlattr stream should already have been validated */
827 nla_for_each_nested(nla, attr, rem) {
828 /* We assume that ovs_key_lens[type] == -1 means that type is a
831 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
832 nlattr_set(nla, val, false);
834 memset(nla_data(nla), val, nla_len(nla));
838 static void mask_set_nlattr(struct nlattr *attr, u8 val)
840 nlattr_set(attr, val, true);
844 * ovs_nla_get_match - parses Netlink attributes into a flow key and
845 * mask. In case the 'mask' is NULL, the flow is treated as exact match
846 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
847 * does not include any don't care bit.
848 * @match: receives the extracted flow match information.
849 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
850 * sequence. The fields should of the packet that triggered the creation
852 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
853 * attribute specifies the mask field of the wildcarded flow.
855 int ovs_nla_get_match(struct sw_flow_match *match,
856 const struct nlattr *key,
857 const struct nlattr *mask)
859 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
860 const struct nlattr *encap;
861 struct nlattr *newmask = NULL;
864 bool encap_valid = false;
867 err = parse_flow_nlattrs(key, a, &key_attrs);
871 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
872 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
873 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
876 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
877 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
878 OVS_NLERR("Invalid Vlan frame.\n");
882 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
883 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
884 encap = a[OVS_KEY_ATTR_ENCAP];
885 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
888 if (tci & htons(VLAN_TAG_PRESENT)) {
889 err = parse_flow_nlattrs(encap, a, &key_attrs);
893 /* Corner case for truncated 802.1Q header. */
894 if (nla_len(encap)) {
895 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
899 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
904 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
908 if (match->mask && !mask) {
909 /* Create an exact match mask. We need to set to 0xff all the
910 * 'match->mask' fields that have been touched in 'match->key'.
911 * We cannot simply memset 'match->mask', because padding bytes
912 * and fields not specified in 'match->key' should be left to 0.
913 * Instead, we use a stream of netlink attributes, copied from
914 * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
915 * of filling 'match->mask' appropriately.
917 newmask = kmemdup(key, nla_total_size(nla_len(key)),
922 mask_set_nlattr(newmask, 0xff);
924 /* The userspace does not send tunnel attributes that are 0,
925 * but we should not wildcard them nonetheless.
927 if (match->key->tun_key.ipv4_dst)
928 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, 0xff, true);
934 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
938 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
943 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
948 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
949 if (a[OVS_KEY_ATTR_ETHERTYPE])
950 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
952 if (eth_type == htons(0xffff)) {
953 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
954 encap = a[OVS_KEY_ATTR_ENCAP];
955 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
959 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
965 if (a[OVS_KEY_ATTR_VLAN])
966 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
968 if (!(tci & htons(VLAN_TAG_PRESENT))) {
969 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
975 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
980 if (!match_validate(match, key_attrs, mask_attrs))
989 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
990 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
991 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
994 * This parses a series of Netlink attributes that form a flow key, which must
995 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
996 * get the metadata, that is, the parts of the flow key that cannot be
997 * extracted from the packet itself.
1000 int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1001 struct sw_flow_key *key)
1003 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1004 struct sw_flow_match match;
1008 err = parse_flow_nlattrs(attr, a, &attrs);
1012 memset(&match, 0, sizeof(match));
1015 key->phy.in_port = DP_MAX_PORTS;
1017 return metadata_from_nlattrs(&match, &attrs, a, false);
1020 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1021 const struct sw_flow_key *output, struct sk_buff *skb)
1023 struct ovs_key_ethernet *eth_key;
1024 struct nlattr *nla, *encap;
1025 bool is_mask = (swkey != output);
1027 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1028 goto nla_put_failure;
1030 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1031 goto nla_put_failure;
1033 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1034 goto nla_put_failure;
1036 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1037 const struct geneve_opt *opts = NULL;
1039 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1040 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1042 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1043 swkey->tun_opts_len))
1044 goto nla_put_failure;
1047 if (swkey->phy.in_port == DP_MAX_PORTS) {
1048 if (is_mask && (output->phy.in_port == 0xffff))
1049 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1050 goto nla_put_failure;
1053 upper_u16 = !is_mask ? 0 : 0xffff;
1055 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1056 (upper_u16 << 16) | output->phy.in_port))
1057 goto nla_put_failure;
1060 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1061 goto nla_put_failure;
1063 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1065 goto nla_put_failure;
1067 eth_key = nla_data(nla);
1068 ether_addr_copy(eth_key->eth_src, output->eth.src);
1069 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1071 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1073 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1074 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1075 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1076 goto nla_put_failure;
1077 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1078 if (!swkey->eth.tci)
1083 if (swkey->eth.type == htons(ETH_P_802_2)) {
1085 * Ethertype 802.2 is represented in the netlink with omitted
1086 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1087 * 0xffff in the mask attribute. Ethertype can also
1090 if (is_mask && output->eth.type)
1091 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1093 goto nla_put_failure;
1097 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1098 goto nla_put_failure;
1100 if (swkey->eth.type == htons(ETH_P_IP)) {
1101 struct ovs_key_ipv4 *ipv4_key;
1103 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1105 goto nla_put_failure;
1106 ipv4_key = nla_data(nla);
1107 ipv4_key->ipv4_src = output->ipv4.addr.src;
1108 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1109 ipv4_key->ipv4_proto = output->ip.proto;
1110 ipv4_key->ipv4_tos = output->ip.tos;
1111 ipv4_key->ipv4_ttl = output->ip.ttl;
1112 ipv4_key->ipv4_frag = output->ip.frag;
1113 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1114 struct ovs_key_ipv6 *ipv6_key;
1116 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1118 goto nla_put_failure;
1119 ipv6_key = nla_data(nla);
1120 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1121 sizeof(ipv6_key->ipv6_src));
1122 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1123 sizeof(ipv6_key->ipv6_dst));
1124 ipv6_key->ipv6_label = output->ipv6.label;
1125 ipv6_key->ipv6_proto = output->ip.proto;
1126 ipv6_key->ipv6_tclass = output->ip.tos;
1127 ipv6_key->ipv6_hlimit = output->ip.ttl;
1128 ipv6_key->ipv6_frag = output->ip.frag;
1129 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1130 swkey->eth.type == htons(ETH_P_RARP)) {
1131 struct ovs_key_arp *arp_key;
1133 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1135 goto nla_put_failure;
1136 arp_key = nla_data(nla);
1137 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1138 arp_key->arp_sip = output->ipv4.addr.src;
1139 arp_key->arp_tip = output->ipv4.addr.dst;
1140 arp_key->arp_op = htons(output->ip.proto);
1141 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1142 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1145 if ((swkey->eth.type == htons(ETH_P_IP) ||
1146 swkey->eth.type == htons(ETH_P_IPV6)) &&
1147 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1149 if (swkey->ip.proto == IPPROTO_TCP) {
1150 struct ovs_key_tcp *tcp_key;
1152 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1154 goto nla_put_failure;
1155 tcp_key = nla_data(nla);
1156 tcp_key->tcp_src = output->tp.src;
1157 tcp_key->tcp_dst = output->tp.dst;
1158 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1160 goto nla_put_failure;
1161 } else if (swkey->ip.proto == IPPROTO_UDP) {
1162 struct ovs_key_udp *udp_key;
1164 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1166 goto nla_put_failure;
1167 udp_key = nla_data(nla);
1168 udp_key->udp_src = output->tp.src;
1169 udp_key->udp_dst = output->tp.dst;
1170 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1171 struct ovs_key_sctp *sctp_key;
1173 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1175 goto nla_put_failure;
1176 sctp_key = nla_data(nla);
1177 sctp_key->sctp_src = output->tp.src;
1178 sctp_key->sctp_dst = output->tp.dst;
1179 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1180 swkey->ip.proto == IPPROTO_ICMP) {
1181 struct ovs_key_icmp *icmp_key;
1183 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1185 goto nla_put_failure;
1186 icmp_key = nla_data(nla);
1187 icmp_key->icmp_type = ntohs(output->tp.src);
1188 icmp_key->icmp_code = ntohs(output->tp.dst);
1189 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1190 swkey->ip.proto == IPPROTO_ICMPV6) {
1191 struct ovs_key_icmpv6 *icmpv6_key;
1193 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1194 sizeof(*icmpv6_key));
1196 goto nla_put_failure;
1197 icmpv6_key = nla_data(nla);
1198 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1199 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1201 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1202 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1203 struct ovs_key_nd *nd_key;
1205 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1207 goto nla_put_failure;
1208 nd_key = nla_data(nla);
1209 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1210 sizeof(nd_key->nd_target));
1211 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1212 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1219 nla_nest_end(skb, encap);
1227 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1229 struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1231 struct sw_flow_actions *sfa;
1233 if (size > MAX_ACTIONS_BUFSIZE)
1234 return ERR_PTR(-EINVAL);
1236 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1238 return ERR_PTR(-ENOMEM);
1240 sfa->actions_len = 0;
1244 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1245 * The caller must hold rcu_read_lock for this to be sensible. */
1246 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1248 kfree_rcu(sf_acts, rcu);
1251 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1255 struct sw_flow_actions *acts;
1257 int req_size = NLA_ALIGN(attr_len);
1258 int next_offset = offsetof(struct sw_flow_actions, actions) +
1259 (*sfa)->actions_len;
1261 if (req_size <= (ksize(*sfa) - next_offset))
1264 new_acts_size = ksize(*sfa) * 2;
1266 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1267 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1268 return ERR_PTR(-EMSGSIZE);
1269 new_acts_size = MAX_ACTIONS_BUFSIZE;
1272 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1274 return (void *)acts;
1276 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1277 acts->actions_len = (*sfa)->actions_len;
1282 (*sfa)->actions_len += req_size;
1283 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1286 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1287 int attrtype, void *data, int len)
1291 a = reserve_sfa_size(sfa, nla_attr_size(len));
1295 a->nla_type = attrtype;
1296 a->nla_len = nla_attr_size(len);
1299 memcpy(nla_data(a), data, len);
1300 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1305 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1306 void *data, int len)
1310 a = __add_action(sfa, attrtype, data, len);
1317 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1320 int used = (*sfa)->actions_len;
1323 err = add_action(sfa, attrtype, NULL, 0);
1330 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1333 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1336 a->nla_len = sfa->actions_len - st_offset;
1339 static int validate_and_copy_sample(const struct nlattr *attr,
1340 const struct sw_flow_key *key, int depth,
1341 struct sw_flow_actions **sfa)
1343 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1344 const struct nlattr *probability, *actions;
1345 const struct nlattr *a;
1346 int rem, start, err, st_acts;
1348 memset(attrs, 0, sizeof(attrs));
1349 nla_for_each_nested(a, attr, rem) {
1350 int type = nla_type(a);
1351 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1358 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1359 if (!probability || nla_len(probability) != sizeof(u32))
1362 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1363 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1366 /* validation done, copy sample action. */
1367 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1370 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1371 nla_data(probability), sizeof(u32));
1374 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1378 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1382 add_nested_action_end(*sfa, st_acts);
1383 add_nested_action_end(*sfa, start);
1388 static int validate_tp_port(const struct sw_flow_key *flow_key)
1390 if ((flow_key->eth.type == htons(ETH_P_IP) ||
1391 flow_key->eth.type == htons(ETH_P_IPV6)) &&
1392 (flow_key->tp.src || flow_key->tp.dst))
1398 void ovs_match_init(struct sw_flow_match *match,
1399 struct sw_flow_key *key,
1400 struct sw_flow_mask *mask)
1402 memset(match, 0, sizeof(*match));
1406 memset(key, 0, sizeof(*key));
1409 memset(&mask->key, 0, sizeof(mask->key));
1410 mask->range.start = mask->range.end = 0;
1414 static int validate_and_copy_set_tun(const struct nlattr *attr,
1415 struct sw_flow_actions **sfa)
1417 struct sw_flow_match match;
1418 struct sw_flow_key key;
1419 struct ovs_tunnel_info *tun_info;
1423 ovs_match_init(&match, &key, NULL);
1424 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1428 if (key.tun_opts_len) {
1429 struct geneve_opt *option = GENEVE_OPTS(&key,
1431 int opts_len = key.tun_opts_len;
1432 bool crit_opt = false;
1434 while (opts_len > 0) {
1437 if (opts_len < sizeof(*option))
1440 len = sizeof(*option) + option->length * 4;
1444 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1446 option = (struct geneve_opt *)((u8 *)option + len);
1450 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1453 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1457 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1458 sizeof(*tun_info) + key.tun_opts_len);
1462 tun_info = nla_data(a);
1463 tun_info->tunnel = key.tun_key;
1464 tun_info->options_len = key.tun_opts_len;
1466 if (tun_info->options_len) {
1467 /* We need to store the options in the action itself since
1468 * everything else will go away after flow setup. We can append
1469 * it to tun_info and then point there.
1471 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1473 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1475 tun_info->options = NULL;
1478 add_nested_action_end(*sfa, start);
1483 static int validate_set(const struct nlattr *a,
1484 const struct sw_flow_key *flow_key,
1485 struct sw_flow_actions **sfa,
1488 const struct nlattr *ovs_key = nla_data(a);
1489 int key_type = nla_type(ovs_key);
1491 /* There can be only one key in a action */
1492 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1495 if (key_type > OVS_KEY_ATTR_MAX ||
1496 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1497 ovs_key_lens[key_type] != -1))
1501 const struct ovs_key_ipv4 *ipv4_key;
1502 const struct ovs_key_ipv6 *ipv6_key;
1505 case OVS_KEY_ATTR_PRIORITY:
1506 case OVS_KEY_ATTR_SKB_MARK:
1507 case OVS_KEY_ATTR_ETHERNET:
1510 case OVS_KEY_ATTR_TUNNEL:
1512 err = validate_and_copy_set_tun(a, sfa);
1517 case OVS_KEY_ATTR_IPV4:
1518 if (flow_key->eth.type != htons(ETH_P_IP))
1521 if (!flow_key->ip.proto)
1524 ipv4_key = nla_data(ovs_key);
1525 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1528 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1533 case OVS_KEY_ATTR_IPV6:
1534 if (flow_key->eth.type != htons(ETH_P_IPV6))
1537 if (!flow_key->ip.proto)
1540 ipv6_key = nla_data(ovs_key);
1541 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1544 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1547 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1552 case OVS_KEY_ATTR_TCP:
1553 if (flow_key->ip.proto != IPPROTO_TCP)
1556 return validate_tp_port(flow_key);
1558 case OVS_KEY_ATTR_UDP:
1559 if (flow_key->ip.proto != IPPROTO_UDP)
1562 return validate_tp_port(flow_key);
1564 case OVS_KEY_ATTR_SCTP:
1565 if (flow_key->ip.proto != IPPROTO_SCTP)
1568 return validate_tp_port(flow_key);
1577 static int validate_userspace(const struct nlattr *attr)
1579 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1580 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1581 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1583 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1586 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1587 attr, userspace_policy);
1591 if (!a[OVS_USERSPACE_ATTR_PID] ||
1592 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1598 static int copy_action(const struct nlattr *from,
1599 struct sw_flow_actions **sfa)
1601 int totlen = NLA_ALIGN(from->nla_len);
1604 to = reserve_sfa_size(sfa, from->nla_len);
1608 memcpy(to, from, totlen);
1612 int ovs_nla_copy_actions(const struct nlattr *attr,
1613 const struct sw_flow_key *key,
1615 struct sw_flow_actions **sfa)
1617 const struct nlattr *a;
1620 if (depth >= SAMPLE_ACTION_DEPTH)
1623 nla_for_each_nested(a, attr, rem) {
1624 /* Expected argument lengths, (u32)-1 for variable length. */
1625 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1626 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1627 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1628 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1629 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1630 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1631 [OVS_ACTION_ATTR_SET] = (u32)-1,
1632 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1633 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1635 const struct ovs_action_push_vlan *vlan;
1636 int type = nla_type(a);
1639 if (type > OVS_ACTION_ATTR_MAX ||
1640 (action_lens[type] != nla_len(a) &&
1641 action_lens[type] != (u32)-1))
1646 case OVS_ACTION_ATTR_UNSPEC:
1649 case OVS_ACTION_ATTR_USERSPACE:
1650 err = validate_userspace(a);
1655 case OVS_ACTION_ATTR_OUTPUT:
1656 if (nla_get_u32(a) >= DP_MAX_PORTS)
1660 case OVS_ACTION_ATTR_HASH: {
1661 const struct ovs_action_hash *act_hash = nla_data(a);
1663 switch (act_hash->hash_alg) {
1664 case OVS_HASH_ALG_L4:
1673 case OVS_ACTION_ATTR_POP_VLAN:
1676 case OVS_ACTION_ATTR_PUSH_VLAN:
1678 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1680 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1684 case OVS_ACTION_ATTR_RECIRC:
1687 case OVS_ACTION_ATTR_SET:
1688 err = validate_set(a, key, sfa, &skip_copy);
1693 case OVS_ACTION_ATTR_SAMPLE:
1694 err = validate_and_copy_sample(a, key, depth, sfa);
1704 err = copy_action(a, sfa);
1716 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1718 const struct nlattr *a;
1719 struct nlattr *start;
1722 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1726 nla_for_each_nested(a, attr, rem) {
1727 int type = nla_type(a);
1728 struct nlattr *st_sample;
1731 case OVS_SAMPLE_ATTR_PROBABILITY:
1732 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1733 sizeof(u32), nla_data(a)))
1736 case OVS_SAMPLE_ATTR_ACTIONS:
1737 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1740 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1743 nla_nest_end(skb, st_sample);
1748 nla_nest_end(skb, start);
1752 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1754 const struct nlattr *ovs_key = nla_data(a);
1755 int key_type = nla_type(ovs_key);
1756 struct nlattr *start;
1760 case OVS_KEY_ATTR_TUNNEL_INFO: {
1761 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1763 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1767 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
1768 tun_info->options_len ?
1769 tun_info->options : NULL,
1770 tun_info->options_len);
1773 nla_nest_end(skb, start);
1777 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1785 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1787 const struct nlattr *a;
1790 nla_for_each_attr(a, attr, len, rem) {
1791 int type = nla_type(a);
1794 case OVS_ACTION_ATTR_SET:
1795 err = set_action_to_attr(a, skb);
1800 case OVS_ACTION_ATTR_SAMPLE:
1801 err = sample_action_to_attr(a, skb);
1806 if (nla_put(skb, type, nla_len(a), nla_data(a)))