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>
51 #include "flow_netlink.h"
53 static void update_range(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
56 struct sw_flow_key_range *range;
57 size_t start = rounddown(offset, sizeof(long));
58 size_t end = roundup(offset + size, sizeof(long));
61 range = &match->range;
63 range = &match->mask->range;
65 if (range->start == range->end) {
71 if (range->start > start)
78 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
80 update_range(match, offsetof(struct sw_flow_key, field), \
81 sizeof((match)->key->field), is_mask); \
83 (match)->mask->key.field = value; \
85 (match)->key->field = value; \
88 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
90 update_range(match, offset, len, is_mask); \
92 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
95 memcpy((u8 *)(match)->key + offset, value_p, len); \
98 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
99 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
100 value_p, len, is_mask)
102 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
104 update_range(match, offsetof(struct sw_flow_key, field), \
105 sizeof((match)->key->field), is_mask); \
107 memset((u8 *)&(match)->mask->key.field, value, \
108 sizeof((match)->mask->key.field)); \
110 memset((u8 *)&(match)->key->field, value, \
111 sizeof((match)->key->field)); \
114 static bool match_validate(const struct sw_flow_match *match,
115 u64 key_attrs, u64 mask_attrs, bool log)
117 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
118 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
120 /* The following mask attributes allowed only if they
121 * pass the validation tests. */
122 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
123 | (1 << OVS_KEY_ATTR_IPV6)
124 | (1 << OVS_KEY_ATTR_TCP)
125 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
126 | (1 << OVS_KEY_ATTR_UDP)
127 | (1 << OVS_KEY_ATTR_SCTP)
128 | (1 << OVS_KEY_ATTR_ICMP)
129 | (1 << OVS_KEY_ATTR_ICMPV6)
130 | (1 << OVS_KEY_ATTR_ARP)
131 | (1 << OVS_KEY_ATTR_ND)
132 | (1 << OVS_KEY_ATTR_MPLS));
134 /* Always allowed mask fields. */
135 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
136 | (1 << OVS_KEY_ATTR_IN_PORT)
137 | (1 << OVS_KEY_ATTR_ETHERTYPE));
139 /* Check key attributes. */
140 if (match->key->eth.type == htons(ETH_P_ARP)
141 || match->key->eth.type == htons(ETH_P_RARP)) {
142 key_expected |= 1 << OVS_KEY_ATTR_ARP;
143 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
144 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
147 if (eth_p_mpls(match->key->eth.type)) {
148 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
149 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
150 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
153 if (match->key->eth.type == htons(ETH_P_IP)) {
154 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
155 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
156 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
158 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
159 if (match->key->ip.proto == IPPROTO_UDP) {
160 key_expected |= 1 << OVS_KEY_ATTR_UDP;
161 if (match->mask && (match->mask->key.ip.proto == 0xff))
162 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
165 if (match->key->ip.proto == IPPROTO_SCTP) {
166 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
167 if (match->mask && (match->mask->key.ip.proto == 0xff))
168 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
171 if (match->key->ip.proto == IPPROTO_TCP) {
172 key_expected |= 1 << OVS_KEY_ATTR_TCP;
173 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
174 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
175 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
176 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
180 if (match->key->ip.proto == IPPROTO_ICMP) {
181 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
182 if (match->mask && (match->mask->key.ip.proto == 0xff))
183 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
188 if (match->key->eth.type == htons(ETH_P_IPV6)) {
189 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
190 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
191 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
193 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
194 if (match->key->ip.proto == IPPROTO_UDP) {
195 key_expected |= 1 << OVS_KEY_ATTR_UDP;
196 if (match->mask && (match->mask->key.ip.proto == 0xff))
197 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
200 if (match->key->ip.proto == IPPROTO_SCTP) {
201 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
202 if (match->mask && (match->mask->key.ip.proto == 0xff))
203 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
206 if (match->key->ip.proto == IPPROTO_TCP) {
207 key_expected |= 1 << OVS_KEY_ATTR_TCP;
208 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
209 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
210 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
211 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
215 if (match->key->ip.proto == IPPROTO_ICMPV6) {
216 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
217 if (match->mask && (match->mask->key.ip.proto == 0xff))
218 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
220 if (match->key->tp.src ==
221 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
222 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
223 key_expected |= 1 << OVS_KEY_ATTR_ND;
224 if (match->mask && (match->mask->key.tp.src == htons(0xffff)))
225 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
231 if ((key_attrs & key_expected) != key_expected) {
232 /* Key attributes check failed. */
233 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
234 (unsigned long long)key_attrs,
235 (unsigned long long)key_expected);
239 if ((mask_attrs & mask_allowed) != mask_attrs) {
240 /* Mask attributes check failed. */
241 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
242 (unsigned long long)mask_attrs,
243 (unsigned long long)mask_allowed);
250 size_t ovs_tun_key_attr_size(void)
252 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
253 * updating this function.
255 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
256 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
257 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
258 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
259 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
260 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
261 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
262 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
263 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
264 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
265 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
268 size_t ovs_key_attr_size(void)
270 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
271 * updating this function.
273 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
275 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
276 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
277 + ovs_tun_key_attr_size()
278 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
279 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
280 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
281 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
282 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
283 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
284 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
285 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
286 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
287 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
288 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
289 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
292 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
293 static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
294 [OVS_KEY_ATTR_ENCAP] = -1,
295 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
296 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
297 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
298 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
299 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
300 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
301 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
302 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
303 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
304 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
305 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
306 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
307 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
308 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
309 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
310 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
311 [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
312 [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
313 [OVS_KEY_ATTR_TUNNEL] = -1,
314 [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls),
317 static bool is_all_zero(const u8 *fp, size_t size)
324 for (i = 0; i < size; i++)
331 static int __parse_flow_nlattrs(const struct nlattr *attr,
332 const struct nlattr *a[],
333 u64 *attrsp, bool log, bool nz)
335 const struct nlattr *nla;
340 nla_for_each_nested(nla, attr, rem) {
341 u16 type = nla_type(nla);
344 if (type > OVS_KEY_ATTR_MAX) {
345 OVS_NLERR(log, "Key type %d is out of range max %d",
346 type, OVS_KEY_ATTR_MAX);
350 if (attrs & (1 << type)) {
351 OVS_NLERR(log, "Duplicate key (type %d).", type);
355 expected_len = ovs_key_lens[type];
356 if (nla_len(nla) != expected_len && expected_len != -1) {
357 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
358 type, nla_len(nla), expected_len);
362 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
368 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
376 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
377 const struct nlattr *a[], u64 *attrsp,
380 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
383 static int parse_flow_nlattrs(const struct nlattr *attr,
384 const struct nlattr *a[], u64 *attrsp,
387 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
390 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
391 struct sw_flow_match *match, bool is_mask,
394 unsigned long opt_key_offset;
396 if (nla_len(a) > sizeof(match->key->tun_opts)) {
397 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
398 nla_len(a), sizeof(match->key->tun_opts));
402 if (nla_len(a) % 4 != 0) {
403 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
408 /* We need to record the length of the options passed
409 * down, otherwise packets with the same format but
410 * additional options will be silently matched.
413 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
416 /* This is somewhat unusual because it looks at
417 * both the key and mask while parsing the
418 * attributes (and by extension assumes the key
419 * is parsed first). Normally, we would verify
420 * that each is the correct length and that the
421 * attributes line up in the validate function.
422 * However, that is difficult because this is
423 * variable length and we won't have the
426 if (match->key->tun_opts_len != nla_len(a)) {
427 OVS_NLERR(log, "Geneve option len %d != mask len %d",
428 match->key->tun_opts_len, nla_len(a));
432 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
435 opt_key_offset = (unsigned long)GENEVE_OPTS((struct sw_flow_key *)0,
437 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
438 nla_len(a), is_mask);
442 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
443 struct sw_flow_match *match, bool is_mask,
449 __be16 tun_flags = 0;
451 nla_for_each_nested(a, attr, rem) {
452 int type = nla_type(a);
455 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
456 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
457 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
458 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
459 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
460 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
461 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
462 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
463 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = sizeof(u16),
464 [OVS_TUNNEL_KEY_ATTR_TP_DST] = sizeof(u16),
465 [OVS_TUNNEL_KEY_ATTR_OAM] = 0,
466 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1,
469 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
470 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
471 type, OVS_TUNNEL_KEY_ATTR_MAX);
475 if (ovs_tunnel_key_lens[type] != nla_len(a) &&
476 ovs_tunnel_key_lens[type] != -1) {
477 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
478 type, nla_len(a), ovs_tunnel_key_lens[type]);
483 case OVS_TUNNEL_KEY_ATTR_ID:
484 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
485 nla_get_be64(a), is_mask);
486 tun_flags |= TUNNEL_KEY;
488 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
489 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
490 nla_get_be32(a), is_mask);
492 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
493 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
494 nla_get_be32(a), is_mask);
496 case OVS_TUNNEL_KEY_ATTR_TOS:
497 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
498 nla_get_u8(a), is_mask);
500 case OVS_TUNNEL_KEY_ATTR_TTL:
501 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
502 nla_get_u8(a), is_mask);
505 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
506 tun_flags |= TUNNEL_DONT_FRAGMENT;
508 case OVS_TUNNEL_KEY_ATTR_CSUM:
509 tun_flags |= TUNNEL_CSUM;
511 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
512 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
513 nla_get_be16(a), is_mask);
515 case OVS_TUNNEL_KEY_ATTR_TP_DST:
516 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
517 nla_get_be16(a), is_mask);
519 case OVS_TUNNEL_KEY_ATTR_OAM:
520 tun_flags |= TUNNEL_OAM;
522 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
523 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
527 tun_flags |= TUNNEL_OPTIONS_PRESENT;
530 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
536 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
539 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
545 if (!match->key->tun_key.ipv4_dst) {
546 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
551 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
559 static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
560 const struct ovs_key_ipv4_tunnel *output,
561 const struct geneve_opt *tun_opts,
562 int swkey_tun_opts_len)
564 if (output->tun_flags & TUNNEL_KEY &&
565 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
567 if (output->ipv4_src &&
568 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
570 if (output->ipv4_dst &&
571 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
573 if (output->ipv4_tos &&
574 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
576 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
578 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
579 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
581 if ((output->tun_flags & TUNNEL_CSUM) &&
582 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
584 if (output->tp_src &&
585 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
587 if (output->tp_dst &&
588 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
590 if ((output->tun_flags & TUNNEL_OAM) &&
591 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
594 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
595 swkey_tun_opts_len, tun_opts))
601 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
602 const struct ovs_key_ipv4_tunnel *output,
603 const struct geneve_opt *tun_opts,
604 int swkey_tun_opts_len)
609 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
613 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
617 nla_nest_end(skb, nla);
621 int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
622 const struct ovs_tunnel_info *egress_tun_info)
624 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
625 egress_tun_info->options,
626 egress_tun_info->options_len);
629 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
630 const struct nlattr **a, bool is_mask,
633 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
634 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
636 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
637 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
640 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
641 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
643 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
644 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
647 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
648 SW_FLOW_KEY_PUT(match, phy.priority,
649 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
650 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
653 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
654 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
657 in_port = 0xffffffff; /* Always exact match in_port. */
658 } else if (in_port >= DP_MAX_PORTS) {
659 OVS_NLERR(log, "Port %d exceeds max allowable %d",
660 in_port, DP_MAX_PORTS);
664 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
665 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
666 } else if (!is_mask) {
667 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
670 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
671 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
673 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
674 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
676 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
677 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
680 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
685 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
686 const struct nlattr **a, bool is_mask,
691 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
695 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
696 const struct ovs_key_ethernet *eth_key;
698 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
699 SW_FLOW_KEY_MEMCPY(match, eth.src,
700 eth_key->eth_src, ETH_ALEN, is_mask);
701 SW_FLOW_KEY_MEMCPY(match, eth.dst,
702 eth_key->eth_dst, ETH_ALEN, is_mask);
703 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
706 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
709 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
710 if (!(tci & htons(VLAN_TAG_PRESENT))) {
712 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
714 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
719 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
720 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
723 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
726 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
728 /* Always exact match EtherType. */
729 eth_type = htons(0xffff);
730 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
731 OVS_NLERR(log, "EtherType %x is less than min %x",
732 ntohs(eth_type), ETH_P_802_3_MIN);
736 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
737 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
738 } else if (!is_mask) {
739 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
742 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
743 const struct ovs_key_ipv4 *ipv4_key;
745 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
746 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
747 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
748 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
751 SW_FLOW_KEY_PUT(match, ip.proto,
752 ipv4_key->ipv4_proto, is_mask);
753 SW_FLOW_KEY_PUT(match, ip.tos,
754 ipv4_key->ipv4_tos, is_mask);
755 SW_FLOW_KEY_PUT(match, ip.ttl,
756 ipv4_key->ipv4_ttl, is_mask);
757 SW_FLOW_KEY_PUT(match, ip.frag,
758 ipv4_key->ipv4_frag, is_mask);
759 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
760 ipv4_key->ipv4_src, is_mask);
761 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
762 ipv4_key->ipv4_dst, is_mask);
763 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
766 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
767 const struct ovs_key_ipv6 *ipv6_key;
769 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
770 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
771 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
772 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
775 SW_FLOW_KEY_PUT(match, ipv6.label,
776 ipv6_key->ipv6_label, is_mask);
777 SW_FLOW_KEY_PUT(match, ip.proto,
778 ipv6_key->ipv6_proto, is_mask);
779 SW_FLOW_KEY_PUT(match, ip.tos,
780 ipv6_key->ipv6_tclass, is_mask);
781 SW_FLOW_KEY_PUT(match, ip.ttl,
782 ipv6_key->ipv6_hlimit, is_mask);
783 SW_FLOW_KEY_PUT(match, ip.frag,
784 ipv6_key->ipv6_frag, is_mask);
785 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
787 sizeof(match->key->ipv6.addr.src),
789 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
791 sizeof(match->key->ipv6.addr.dst),
794 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
797 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
798 const struct ovs_key_arp *arp_key;
800 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
801 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
802 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
807 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
808 arp_key->arp_sip, is_mask);
809 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
810 arp_key->arp_tip, is_mask);
811 SW_FLOW_KEY_PUT(match, ip.proto,
812 ntohs(arp_key->arp_op), is_mask);
813 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
814 arp_key->arp_sha, ETH_ALEN, is_mask);
815 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
816 arp_key->arp_tha, ETH_ALEN, is_mask);
818 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
821 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
822 const struct ovs_key_mpls *mpls_key;
824 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
825 SW_FLOW_KEY_PUT(match, mpls.top_lse,
826 mpls_key->mpls_lse, is_mask);
828 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
831 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
832 const struct ovs_key_tcp *tcp_key;
834 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
835 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
836 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
837 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
840 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
841 SW_FLOW_KEY_PUT(match, tp.flags,
842 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
844 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
847 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
848 const struct ovs_key_udp *udp_key;
850 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
851 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
852 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
853 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
856 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
857 const struct ovs_key_sctp *sctp_key;
859 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
860 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
861 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
862 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
865 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
866 const struct ovs_key_icmp *icmp_key;
868 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
869 SW_FLOW_KEY_PUT(match, tp.src,
870 htons(icmp_key->icmp_type), is_mask);
871 SW_FLOW_KEY_PUT(match, tp.dst,
872 htons(icmp_key->icmp_code), is_mask);
873 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
876 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
877 const struct ovs_key_icmpv6 *icmpv6_key;
879 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
880 SW_FLOW_KEY_PUT(match, tp.src,
881 htons(icmpv6_key->icmpv6_type), is_mask);
882 SW_FLOW_KEY_PUT(match, tp.dst,
883 htons(icmpv6_key->icmpv6_code), is_mask);
884 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
887 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
888 const struct ovs_key_nd *nd_key;
890 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
891 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
893 sizeof(match->key->ipv6.nd.target),
895 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
896 nd_key->nd_sll, ETH_ALEN, is_mask);
897 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
898 nd_key->nd_tll, ETH_ALEN, is_mask);
899 attrs &= ~(1 << OVS_KEY_ATTR_ND);
903 OVS_NLERR(log, "Unknown key attributes %llx",
904 (unsigned long long)attrs);
911 static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key)
916 /* The nlattr stream should already have been validated */
917 nla_for_each_nested(nla, attr, rem) {
918 /* We assume that ovs_key_lens[type] == -1 means that type is a
921 if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1)
922 nlattr_set(nla, val, false);
924 memset(nla_data(nla), val, nla_len(nla));
928 static void mask_set_nlattr(struct nlattr *attr, u8 val)
930 nlattr_set(attr, val, true);
934 * ovs_nla_get_match - parses Netlink attributes into a flow key and
935 * mask. In case the 'mask' is NULL, the flow is treated as exact match
936 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
937 * does not include any don't care bit.
938 * @match: receives the extracted flow match information.
939 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
940 * sequence. The fields should of the packet that triggered the creation
942 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
943 * attribute specifies the mask field of the wildcarded flow.
944 * @log: Boolean to allow kernel error logging. Normally true, but when
945 * probing for feature compatibility this should be passed in as false to
946 * suppress unnecessary error logging.
948 int ovs_nla_get_match(struct sw_flow_match *match,
949 const struct nlattr *nla_key,
950 const struct nlattr *nla_mask,
953 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
954 const struct nlattr *encap;
955 struct nlattr *newmask = NULL;
958 bool encap_valid = false;
961 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
965 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
966 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
967 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
970 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
971 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
972 OVS_NLERR(log, "Invalid Vlan frame.");
976 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
977 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
978 encap = a[OVS_KEY_ATTR_ENCAP];
979 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
982 if (tci & htons(VLAN_TAG_PRESENT)) {
983 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
987 /* Corner case for truncated 802.1Q header. */
988 if (nla_len(encap)) {
989 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
993 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
998 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
1004 /* Create an exact match mask. We need to set to 0xff
1005 * all the 'match->mask' fields that have been touched
1006 * in 'match->key'. We cannot simply memset
1007 * 'match->mask', because padding bytes and fields not
1008 * specified in 'match->key' should be left to 0.
1009 * Instead, we use a stream of netlink attributes,
1010 * copied from 'key' and set to 0xff.
1011 * ovs_key_from_nlattrs() will take care of filling
1012 * 'match->mask' appropriately.
1014 newmask = kmemdup(nla_key,
1015 nla_total_size(nla_len(nla_key)),
1020 mask_set_nlattr(newmask, 0xff);
1022 /* The userspace does not send tunnel attributes that
1023 * are 0, but we should not wildcard them nonetheless.
1025 if (match->key->tun_key.ipv4_dst)
1026 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1032 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1036 /* Always match on tci. */
1037 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1039 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
1040 __be16 eth_type = 0;
1044 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
1049 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1050 if (a[OVS_KEY_ATTR_ETHERTYPE])
1051 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1053 if (eth_type == htons(0xffff)) {
1054 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1055 encap = a[OVS_KEY_ATTR_ENCAP];
1056 err = parse_flow_mask_nlattrs(encap, a,
1061 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1067 if (a[OVS_KEY_ATTR_VLAN])
1068 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1070 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1071 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1078 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
1083 if (!match_validate(match, key_attrs, mask_attrs, log))
1092 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1093 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
1094 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1096 * @log: Boolean to allow kernel error logging. Normally true, but when
1097 * probing for feature compatibility this should be passed in as false to
1098 * suppress unnecessary error logging.
1100 * This parses a series of Netlink attributes that form a flow key, which must
1101 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1102 * get the metadata, that is, the parts of the flow key that cannot be
1103 * extracted from the packet itself.
1106 int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1107 struct sw_flow_key *key,
1110 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1111 struct sw_flow_match match;
1115 err = parse_flow_nlattrs(attr, a, &attrs, log);
1119 memset(&match, 0, sizeof(match));
1122 key->phy.in_port = DP_MAX_PORTS;
1124 return metadata_from_nlattrs(&match, &attrs, a, false, log);
1127 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1128 const struct sw_flow_key *output, struct sk_buff *skb)
1130 struct ovs_key_ethernet *eth_key;
1131 struct nlattr *nla, *encap;
1132 bool is_mask = (swkey != output);
1134 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1135 goto nla_put_failure;
1137 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1138 goto nla_put_failure;
1140 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1141 goto nla_put_failure;
1143 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1144 const struct geneve_opt *opts = NULL;
1146 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1147 opts = GENEVE_OPTS(output, swkey->tun_opts_len);
1149 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1150 swkey->tun_opts_len))
1151 goto nla_put_failure;
1154 if (swkey->phy.in_port == DP_MAX_PORTS) {
1155 if (is_mask && (output->phy.in_port == 0xffff))
1156 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1157 goto nla_put_failure;
1160 upper_u16 = !is_mask ? 0 : 0xffff;
1162 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1163 (upper_u16 << 16) | output->phy.in_port))
1164 goto nla_put_failure;
1167 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1168 goto nla_put_failure;
1170 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1172 goto nla_put_failure;
1174 eth_key = nla_data(nla);
1175 ether_addr_copy(eth_key->eth_src, output->eth.src);
1176 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1178 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1180 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1181 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1182 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1183 goto nla_put_failure;
1184 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1185 if (!swkey->eth.tci)
1190 if (swkey->eth.type == htons(ETH_P_802_2)) {
1192 * Ethertype 802.2 is represented in the netlink with omitted
1193 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1194 * 0xffff in the mask attribute. Ethertype can also
1197 if (is_mask && output->eth.type)
1198 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1200 goto nla_put_failure;
1204 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1205 goto nla_put_failure;
1207 if (swkey->eth.type == htons(ETH_P_IP)) {
1208 struct ovs_key_ipv4 *ipv4_key;
1210 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1212 goto nla_put_failure;
1213 ipv4_key = nla_data(nla);
1214 ipv4_key->ipv4_src = output->ipv4.addr.src;
1215 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1216 ipv4_key->ipv4_proto = output->ip.proto;
1217 ipv4_key->ipv4_tos = output->ip.tos;
1218 ipv4_key->ipv4_ttl = output->ip.ttl;
1219 ipv4_key->ipv4_frag = output->ip.frag;
1220 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1221 struct ovs_key_ipv6 *ipv6_key;
1223 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1225 goto nla_put_failure;
1226 ipv6_key = nla_data(nla);
1227 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1228 sizeof(ipv6_key->ipv6_src));
1229 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1230 sizeof(ipv6_key->ipv6_dst));
1231 ipv6_key->ipv6_label = output->ipv6.label;
1232 ipv6_key->ipv6_proto = output->ip.proto;
1233 ipv6_key->ipv6_tclass = output->ip.tos;
1234 ipv6_key->ipv6_hlimit = output->ip.ttl;
1235 ipv6_key->ipv6_frag = output->ip.frag;
1236 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1237 swkey->eth.type == htons(ETH_P_RARP)) {
1238 struct ovs_key_arp *arp_key;
1240 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1242 goto nla_put_failure;
1243 arp_key = nla_data(nla);
1244 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1245 arp_key->arp_sip = output->ipv4.addr.src;
1246 arp_key->arp_tip = output->ipv4.addr.dst;
1247 arp_key->arp_op = htons(output->ip.proto);
1248 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1249 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1250 } else if (eth_p_mpls(swkey->eth.type)) {
1251 struct ovs_key_mpls *mpls_key;
1253 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1255 goto nla_put_failure;
1256 mpls_key = nla_data(nla);
1257 mpls_key->mpls_lse = output->mpls.top_lse;
1260 if ((swkey->eth.type == htons(ETH_P_IP) ||
1261 swkey->eth.type == htons(ETH_P_IPV6)) &&
1262 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1264 if (swkey->ip.proto == IPPROTO_TCP) {
1265 struct ovs_key_tcp *tcp_key;
1267 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1269 goto nla_put_failure;
1270 tcp_key = nla_data(nla);
1271 tcp_key->tcp_src = output->tp.src;
1272 tcp_key->tcp_dst = output->tp.dst;
1273 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1275 goto nla_put_failure;
1276 } else if (swkey->ip.proto == IPPROTO_UDP) {
1277 struct ovs_key_udp *udp_key;
1279 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1281 goto nla_put_failure;
1282 udp_key = nla_data(nla);
1283 udp_key->udp_src = output->tp.src;
1284 udp_key->udp_dst = output->tp.dst;
1285 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1286 struct ovs_key_sctp *sctp_key;
1288 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1290 goto nla_put_failure;
1291 sctp_key = nla_data(nla);
1292 sctp_key->sctp_src = output->tp.src;
1293 sctp_key->sctp_dst = output->tp.dst;
1294 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1295 swkey->ip.proto == IPPROTO_ICMP) {
1296 struct ovs_key_icmp *icmp_key;
1298 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1300 goto nla_put_failure;
1301 icmp_key = nla_data(nla);
1302 icmp_key->icmp_type = ntohs(output->tp.src);
1303 icmp_key->icmp_code = ntohs(output->tp.dst);
1304 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1305 swkey->ip.proto == IPPROTO_ICMPV6) {
1306 struct ovs_key_icmpv6 *icmpv6_key;
1308 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1309 sizeof(*icmpv6_key));
1311 goto nla_put_failure;
1312 icmpv6_key = nla_data(nla);
1313 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1314 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1316 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1317 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1318 struct ovs_key_nd *nd_key;
1320 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1322 goto nla_put_failure;
1323 nd_key = nla_data(nla);
1324 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1325 sizeof(nd_key->nd_target));
1326 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1327 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1334 nla_nest_end(skb, encap);
1342 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1344 static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
1346 struct sw_flow_actions *sfa;
1348 if (size > MAX_ACTIONS_BUFSIZE) {
1349 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
1350 return ERR_PTR(-EINVAL);
1353 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1355 return ERR_PTR(-ENOMEM);
1357 sfa->actions_len = 0;
1361 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1362 * The caller must hold rcu_read_lock for this to be sensible. */
1363 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1365 kfree_rcu(sf_acts, rcu);
1368 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1369 int attr_len, bool log)
1372 struct sw_flow_actions *acts;
1374 int req_size = NLA_ALIGN(attr_len);
1375 int next_offset = offsetof(struct sw_flow_actions, actions) +
1376 (*sfa)->actions_len;
1378 if (req_size <= (ksize(*sfa) - next_offset))
1381 new_acts_size = ksize(*sfa) * 2;
1383 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1384 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1385 return ERR_PTR(-EMSGSIZE);
1386 new_acts_size = MAX_ACTIONS_BUFSIZE;
1389 acts = nla_alloc_flow_actions(new_acts_size, log);
1391 return (void *)acts;
1393 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1394 acts->actions_len = (*sfa)->actions_len;
1399 (*sfa)->actions_len += req_size;
1400 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1403 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1404 int attrtype, void *data, int len, bool log)
1408 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
1412 a->nla_type = attrtype;
1413 a->nla_len = nla_attr_size(len);
1416 memcpy(nla_data(a), data, len);
1417 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1422 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1423 void *data, int len, bool log)
1427 a = __add_action(sfa, attrtype, data, len, log);
1434 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1435 int attrtype, bool log)
1437 int used = (*sfa)->actions_len;
1440 err = add_action(sfa, attrtype, NULL, 0, log);
1447 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1450 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1453 a->nla_len = sfa->actions_len - st_offset;
1456 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1457 const struct sw_flow_key *key,
1458 int depth, struct sw_flow_actions **sfa,
1459 __be16 eth_type, __be16 vlan_tci, bool log);
1461 static int validate_and_copy_sample(const struct nlattr *attr,
1462 const struct sw_flow_key *key, int depth,
1463 struct sw_flow_actions **sfa,
1464 __be16 eth_type, __be16 vlan_tci, bool log)
1466 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1467 const struct nlattr *probability, *actions;
1468 const struct nlattr *a;
1469 int rem, start, err, st_acts;
1471 memset(attrs, 0, sizeof(attrs));
1472 nla_for_each_nested(a, attr, rem) {
1473 int type = nla_type(a);
1474 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1481 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1482 if (!probability || nla_len(probability) != sizeof(u32))
1485 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1486 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1489 /* validation done, copy sample action. */
1490 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
1493 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1494 nla_data(probability), sizeof(u32), log);
1497 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
1501 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
1502 eth_type, vlan_tci, log);
1506 add_nested_action_end(*sfa, st_acts);
1507 add_nested_action_end(*sfa, start);
1512 static int validate_tp_port(const struct sw_flow_key *flow_key,
1515 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1516 (flow_key->tp.src || flow_key->tp.dst))
1522 void ovs_match_init(struct sw_flow_match *match,
1523 struct sw_flow_key *key,
1524 struct sw_flow_mask *mask)
1526 memset(match, 0, sizeof(*match));
1530 memset(key, 0, sizeof(*key));
1533 memset(&mask->key, 0, sizeof(mask->key));
1534 mask->range.start = mask->range.end = 0;
1538 static int validate_and_copy_set_tun(const struct nlattr *attr,
1539 struct sw_flow_actions **sfa, bool log)
1541 struct sw_flow_match match;
1542 struct sw_flow_key key;
1543 struct ovs_tunnel_info *tun_info;
1547 ovs_match_init(&match, &key, NULL);
1548 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1552 if (key.tun_opts_len) {
1553 struct geneve_opt *option = GENEVE_OPTS(&key,
1555 int opts_len = key.tun_opts_len;
1556 bool crit_opt = false;
1558 while (opts_len > 0) {
1561 if (opts_len < sizeof(*option))
1564 len = sizeof(*option) + option->length * 4;
1568 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1570 option = (struct geneve_opt *)((u8 *)option + len);
1574 key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1577 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
1581 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1582 sizeof(*tun_info) + key.tun_opts_len, log);
1586 tun_info = nla_data(a);
1587 tun_info->tunnel = key.tun_key;
1588 tun_info->options_len = key.tun_opts_len;
1590 if (tun_info->options_len) {
1591 /* We need to store the options in the action itself since
1592 * everything else will go away after flow setup. We can append
1593 * it to tun_info and then point there.
1595 memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len),
1597 tun_info->options = (struct geneve_opt *)(tun_info + 1);
1599 tun_info->options = NULL;
1602 add_nested_action_end(*sfa, start);
1607 static int validate_set(const struct nlattr *a,
1608 const struct sw_flow_key *flow_key,
1609 struct sw_flow_actions **sfa,
1610 bool *set_tun, __be16 eth_type, bool log)
1612 const struct nlattr *ovs_key = nla_data(a);
1613 int key_type = nla_type(ovs_key);
1615 /* There can be only one key in a action */
1616 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1619 if (key_type > OVS_KEY_ATTR_MAX ||
1620 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1621 ovs_key_lens[key_type] != -1))
1625 const struct ovs_key_ipv4 *ipv4_key;
1626 const struct ovs_key_ipv6 *ipv6_key;
1629 case OVS_KEY_ATTR_PRIORITY:
1630 case OVS_KEY_ATTR_SKB_MARK:
1631 case OVS_KEY_ATTR_ETHERNET:
1634 case OVS_KEY_ATTR_TUNNEL:
1635 if (eth_p_mpls(eth_type))
1639 err = validate_and_copy_set_tun(a, sfa, log);
1644 case OVS_KEY_ATTR_IPV4:
1645 if (eth_type != htons(ETH_P_IP))
1648 if (!flow_key->ip.proto)
1651 ipv4_key = nla_data(ovs_key);
1652 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1655 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1660 case OVS_KEY_ATTR_IPV6:
1661 if (eth_type != htons(ETH_P_IPV6))
1664 if (!flow_key->ip.proto)
1667 ipv6_key = nla_data(ovs_key);
1668 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1671 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1674 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1679 case OVS_KEY_ATTR_TCP:
1680 if (flow_key->ip.proto != IPPROTO_TCP)
1683 return validate_tp_port(flow_key, eth_type);
1685 case OVS_KEY_ATTR_UDP:
1686 if (flow_key->ip.proto != IPPROTO_UDP)
1689 return validate_tp_port(flow_key, eth_type);
1691 case OVS_KEY_ATTR_MPLS:
1692 if (!eth_p_mpls(eth_type))
1696 case OVS_KEY_ATTR_SCTP:
1697 if (flow_key->ip.proto != IPPROTO_SCTP)
1700 return validate_tp_port(flow_key, eth_type);
1709 static int validate_userspace(const struct nlattr *attr)
1711 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1712 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1713 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1714 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
1716 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1719 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1720 attr, userspace_policy);
1724 if (!a[OVS_USERSPACE_ATTR_PID] ||
1725 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1731 static int copy_action(const struct nlattr *from,
1732 struct sw_flow_actions **sfa, bool log)
1734 int totlen = NLA_ALIGN(from->nla_len);
1737 to = reserve_sfa_size(sfa, from->nla_len, log);
1741 memcpy(to, from, totlen);
1745 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1746 const struct sw_flow_key *key,
1747 int depth, struct sw_flow_actions **sfa,
1748 __be16 eth_type, __be16 vlan_tci, bool log)
1750 const struct nlattr *a;
1751 bool out_tnl_port = false;
1754 if (depth >= SAMPLE_ACTION_DEPTH)
1757 nla_for_each_nested(a, attr, rem) {
1758 /* Expected argument lengths, (u32)-1 for variable length. */
1759 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1760 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1761 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1762 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1763 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1764 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
1765 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1766 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1767 [OVS_ACTION_ATTR_SET] = (u32)-1,
1768 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1769 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1771 const struct ovs_action_push_vlan *vlan;
1772 int type = nla_type(a);
1775 if (type > OVS_ACTION_ATTR_MAX ||
1776 (action_lens[type] != nla_len(a) &&
1777 action_lens[type] != (u32)-1))
1782 case OVS_ACTION_ATTR_UNSPEC:
1785 case OVS_ACTION_ATTR_USERSPACE:
1786 err = validate_userspace(a);
1791 case OVS_ACTION_ATTR_OUTPUT:
1792 if (nla_get_u32(a) >= DP_MAX_PORTS)
1794 out_tnl_port = false;
1798 case OVS_ACTION_ATTR_HASH: {
1799 const struct ovs_action_hash *act_hash = nla_data(a);
1801 switch (act_hash->hash_alg) {
1802 case OVS_HASH_ALG_L4:
1811 case OVS_ACTION_ATTR_POP_VLAN:
1812 vlan_tci = htons(0);
1815 case OVS_ACTION_ATTR_PUSH_VLAN:
1817 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1819 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1821 vlan_tci = vlan->vlan_tci;
1824 case OVS_ACTION_ATTR_RECIRC:
1827 case OVS_ACTION_ATTR_PUSH_MPLS: {
1828 const struct ovs_action_push_mpls *mpls = nla_data(a);
1830 /* Networking stack do not allow simultaneous Tunnel
1836 if (!eth_p_mpls(mpls->mpls_ethertype))
1838 /* Prohibit push MPLS other than to a white list
1839 * for packets that have a known tag order.
1841 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1842 (eth_type != htons(ETH_P_IP) &&
1843 eth_type != htons(ETH_P_IPV6) &&
1844 eth_type != htons(ETH_P_ARP) &&
1845 eth_type != htons(ETH_P_RARP) &&
1846 !eth_p_mpls(eth_type)))
1848 eth_type = mpls->mpls_ethertype;
1852 case OVS_ACTION_ATTR_POP_MPLS:
1853 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1854 !eth_p_mpls(eth_type))
1857 /* Disallow subsequent L2.5+ set and mpls_pop actions
1858 * as there is no check here to ensure that the new
1859 * eth_type is valid and thus set actions could
1860 * write off the end of the packet or otherwise
1863 * Support for these actions is planned using packet
1866 eth_type = htons(0);
1869 case OVS_ACTION_ATTR_SET:
1870 err = validate_set(a, key, sfa,
1871 &out_tnl_port, eth_type, log);
1875 skip_copy = out_tnl_port;
1878 case OVS_ACTION_ATTR_SAMPLE:
1879 err = validate_and_copy_sample(a, key, depth, sfa,
1880 eth_type, vlan_tci, log);
1887 OVS_NLERR(log, "Unknown Action type %d", type);
1891 err = copy_action(a, sfa, log);
1903 int ovs_nla_copy_actions(const struct nlattr *attr,
1904 const struct sw_flow_key *key,
1905 struct sw_flow_actions **sfa, bool log)
1909 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
1911 return PTR_ERR(*sfa);
1913 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
1921 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1923 const struct nlattr *a;
1924 struct nlattr *start;
1927 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1931 nla_for_each_nested(a, attr, rem) {
1932 int type = nla_type(a);
1933 struct nlattr *st_sample;
1936 case OVS_SAMPLE_ATTR_PROBABILITY:
1937 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1938 sizeof(u32), nla_data(a)))
1941 case OVS_SAMPLE_ATTR_ACTIONS:
1942 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1945 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1948 nla_nest_end(skb, st_sample);
1953 nla_nest_end(skb, start);
1957 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1959 const struct nlattr *ovs_key = nla_data(a);
1960 int key_type = nla_type(ovs_key);
1961 struct nlattr *start;
1965 case OVS_KEY_ATTR_TUNNEL_INFO: {
1966 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1968 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1972 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
1973 tun_info->options_len ?
1974 tun_info->options : NULL,
1975 tun_info->options_len);
1978 nla_nest_end(skb, start);
1982 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1990 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1992 const struct nlattr *a;
1995 nla_for_each_attr(a, attr, len, rem) {
1996 int type = nla_type(a);
1999 case OVS_ACTION_ATTR_SET:
2000 err = set_action_to_attr(a, skb);
2005 case OVS_ACTION_ATTR_SAMPLE:
2006 err = sample_action_to_attr(a, skb);
2011 if (nla_put(skb, type, nla_len(a), nla_data(a)))