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
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/geneve.h>
46 #include <net/ndisc.h>
51 #include "flow_netlink.h"
55 const struct ovs_len_tbl *next;
58 #define OVS_ATTR_NESTED -1
60 static void update_range(struct sw_flow_match *match,
61 size_t offset, size_t size, bool is_mask)
63 struct sw_flow_key_range *range;
64 size_t start = rounddown(offset, sizeof(long));
65 size_t end = roundup(offset + size, sizeof(long));
68 range = &match->range;
70 range = &match->mask->range;
72 if (range->start == range->end) {
78 if (range->start > start)
85 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
87 update_range(match, offsetof(struct sw_flow_key, field), \
88 sizeof((match)->key->field), is_mask); \
90 (match)->mask->key.field = value; \
92 (match)->key->field = value; \
95 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
97 update_range(match, offset, len, is_mask); \
99 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
101 memcpy((u8 *)(match)->key + offset, value_p, len); \
104 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
105 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
106 value_p, len, is_mask)
108 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
110 update_range(match, offsetof(struct sw_flow_key, field), \
111 sizeof((match)->key->field), is_mask); \
113 memset((u8 *)&(match)->mask->key.field, value, \
114 sizeof((match)->mask->key.field)); \
116 memset((u8 *)&(match)->key->field, value, \
117 sizeof((match)->key->field)); \
120 static bool match_validate(const struct sw_flow_match *match,
121 u64 key_attrs, u64 mask_attrs, bool log)
123 u64 key_expected = 1ULL << 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.
129 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
130 | (1ULL << OVS_KEY_ATTR_IPV6)
131 | (1ULL << OVS_KEY_ATTR_TCP)
132 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
133 | (1ULL << OVS_KEY_ATTR_UDP)
134 | (1ULL << OVS_KEY_ATTR_SCTP)
135 | (1ULL << OVS_KEY_ATTR_ICMP)
136 | (1ULL << OVS_KEY_ATTR_ICMPV6)
137 | (1ULL << OVS_KEY_ATTR_ARP)
138 | (1ULL << OVS_KEY_ATTR_ND)
139 | (1ULL << OVS_KEY_ATTR_MPLS));
141 /* Always allowed mask fields. */
142 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
143 | (1ULL << OVS_KEY_ATTR_IN_PORT)
144 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
146 /* Check key attributes. */
147 if (match->key->eth.type == htons(ETH_P_ARP)
148 || match->key->eth.type == htons(ETH_P_RARP)) {
149 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
150 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
151 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
154 if (eth_p_mpls(match->key->eth.type)) {
155 key_expected |= 1ULL << OVS_KEY_ATTR_MPLS;
156 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
157 mask_allowed |= 1ULL << OVS_KEY_ATTR_MPLS;
160 if (match->key->eth.type == htons(ETH_P_IP)) {
161 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
162 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
163 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
165 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
166 if (match->key->ip.proto == IPPROTO_UDP) {
167 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
168 if (match->mask && (match->mask->key.ip.proto == 0xff))
169 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
172 if (match->key->ip.proto == IPPROTO_SCTP) {
173 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
174 if (match->mask && (match->mask->key.ip.proto == 0xff))
175 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
178 if (match->key->ip.proto == IPPROTO_TCP) {
179 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
180 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
181 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
182 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
183 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
187 if (match->key->ip.proto == IPPROTO_ICMP) {
188 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
189 if (match->mask && (match->mask->key.ip.proto == 0xff))
190 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
195 if (match->key->eth.type == htons(ETH_P_IPV6)) {
196 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
197 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
198 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
200 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
201 if (match->key->ip.proto == IPPROTO_UDP) {
202 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
203 if (match->mask && (match->mask->key.ip.proto == 0xff))
204 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
207 if (match->key->ip.proto == IPPROTO_SCTP) {
208 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
209 if (match->mask && (match->mask->key.ip.proto == 0xff))
210 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
213 if (match->key->ip.proto == IPPROTO_TCP) {
214 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
215 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
216 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
217 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
218 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
222 if (match->key->ip.proto == IPPROTO_ICMPV6) {
223 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
224 if (match->mask && (match->mask->key.ip.proto == 0xff))
225 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
227 if (match->key->tp.src ==
228 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
229 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
230 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
231 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
232 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
238 if ((key_attrs & key_expected) != key_expected) {
239 /* Key attributes check failed. */
240 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
241 (unsigned long long)key_attrs,
242 (unsigned long long)key_expected);
246 if ((mask_attrs & mask_allowed) != mask_attrs) {
247 /* Mask attributes check failed. */
248 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
249 (unsigned long long)mask_attrs,
250 (unsigned long long)mask_allowed);
257 size_t ovs_tun_key_attr_size(void)
259 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
260 * updating this function.
262 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
263 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
264 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
265 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
266 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
267 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
268 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
269 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
270 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
271 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
272 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
275 size_t ovs_key_attr_size(void)
277 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
278 * updating this function.
280 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
282 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
283 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
284 + ovs_tun_key_attr_size()
285 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
286 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
287 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
288 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
289 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
290 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
291 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
292 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
293 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
294 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
295 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
296 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
299 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
300 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
301 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
302 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
303 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
304 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
305 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
306 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
307 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
308 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
309 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
310 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_NESTED },
313 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
314 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
315 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
316 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
317 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
318 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
319 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
320 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
321 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
322 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
323 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
324 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
325 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
326 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
327 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
328 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
329 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
330 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
331 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
332 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
333 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
334 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
335 .next = ovs_tunnel_key_lens, },
336 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
338 static bool is_all_zero(const u8 *fp, size_t size)
345 for (i = 0; i < size; i++)
352 static int __parse_flow_nlattrs(const struct nlattr *attr,
353 const struct nlattr *a[],
354 u64 *attrsp, bool log, bool nz)
356 const struct nlattr *nla;
361 nla_for_each_nested(nla, attr, rem) {
362 u16 type = nla_type(nla);
365 if (type > OVS_KEY_ATTR_MAX) {
366 OVS_NLERR(log, "Key type %d is out of range max %d",
367 type, OVS_KEY_ATTR_MAX);
371 if (attrs & (1ULL << type)) {
372 OVS_NLERR(log, "Duplicate key (type %d).", type);
376 expected_len = ovs_key_lens[type].len;
377 if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
378 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
379 type, nla_len(nla), expected_len);
383 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
384 attrs |= 1ULL << type;
389 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
397 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
398 const struct nlattr *a[], u64 *attrsp,
401 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
404 static int parse_flow_nlattrs(const struct nlattr *attr,
405 const struct nlattr *a[], u64 *attrsp,
408 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
411 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
412 struct sw_flow_match *match, bool is_mask,
415 unsigned long opt_key_offset;
417 if (nla_len(a) > sizeof(match->key->tun_opts)) {
418 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
419 nla_len(a), sizeof(match->key->tun_opts));
423 if (nla_len(a) % 4 != 0) {
424 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
429 /* We need to record the length of the options passed
430 * down, otherwise packets with the same format but
431 * additional options will be silently matched.
434 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
437 /* This is somewhat unusual because it looks at
438 * both the key and mask while parsing the
439 * attributes (and by extension assumes the key
440 * is parsed first). Normally, we would verify
441 * that each is the correct length and that the
442 * attributes line up in the validate function.
443 * However, that is difficult because this is
444 * variable length and we won't have the
447 if (match->key->tun_opts_len != nla_len(a)) {
448 OVS_NLERR(log, "Geneve option len %d != mask len %d",
449 match->key->tun_opts_len, nla_len(a));
453 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
456 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
457 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
458 nla_len(a), is_mask);
462 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
463 struct sw_flow_match *match, bool is_mask,
469 __be16 tun_flags = 0;
471 nla_for_each_nested(a, attr, rem) {
472 int type = nla_type(a);
475 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
476 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
477 type, OVS_TUNNEL_KEY_ATTR_MAX);
481 if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
482 ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
483 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
484 type, nla_len(a), ovs_tunnel_key_lens[type].len);
489 case OVS_TUNNEL_KEY_ATTR_ID:
490 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
491 nla_get_be64(a), is_mask);
492 tun_flags |= TUNNEL_KEY;
494 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
495 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
496 nla_get_be32(a), is_mask);
498 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
499 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
500 nla_get_be32(a), is_mask);
502 case OVS_TUNNEL_KEY_ATTR_TOS:
503 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
504 nla_get_u8(a), is_mask);
506 case OVS_TUNNEL_KEY_ATTR_TTL:
507 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
508 nla_get_u8(a), is_mask);
511 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
512 tun_flags |= TUNNEL_DONT_FRAGMENT;
514 case OVS_TUNNEL_KEY_ATTR_CSUM:
515 tun_flags |= TUNNEL_CSUM;
517 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
518 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
519 nla_get_be16(a), is_mask);
521 case OVS_TUNNEL_KEY_ATTR_TP_DST:
522 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
523 nla_get_be16(a), is_mask);
525 case OVS_TUNNEL_KEY_ATTR_OAM:
526 tun_flags |= TUNNEL_OAM;
528 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
529 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
533 tun_flags |= TUNNEL_OPTIONS_PRESENT;
536 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
542 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
545 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
551 if (!match->key->tun_key.ipv4_dst) {
552 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
557 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
565 static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
566 const struct ovs_key_ipv4_tunnel *output,
567 const void *tun_opts, int swkey_tun_opts_len)
569 if (output->tun_flags & TUNNEL_KEY &&
570 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
572 if (output->ipv4_src &&
573 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
575 if (output->ipv4_dst &&
576 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
578 if (output->ipv4_tos &&
579 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
581 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
583 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
584 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
586 if ((output->tun_flags & TUNNEL_CSUM) &&
587 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
589 if (output->tp_src &&
590 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
592 if (output->tp_dst &&
593 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
595 if ((output->tun_flags & TUNNEL_OAM) &&
596 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
599 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
600 swkey_tun_opts_len, tun_opts))
606 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
607 const struct ovs_key_ipv4_tunnel *output,
608 const void *tun_opts, int swkey_tun_opts_len)
613 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
617 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
621 nla_nest_end(skb, nla);
625 int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
626 const struct ovs_tunnel_info *egress_tun_info)
628 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
629 egress_tun_info->options,
630 egress_tun_info->options_len);
633 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
634 const struct nlattr **a, bool is_mask,
637 if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
638 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
640 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
641 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
644 if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
645 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
647 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
648 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
651 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
652 SW_FLOW_KEY_PUT(match, phy.priority,
653 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
654 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
657 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
658 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
661 in_port = 0xffffffff; /* Always exact match in_port. */
662 } else if (in_port >= DP_MAX_PORTS) {
663 OVS_NLERR(log, "Port %d exceeds max allowable %d",
664 in_port, DP_MAX_PORTS);
668 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
669 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
670 } else if (!is_mask) {
671 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
674 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
675 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
677 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
678 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
680 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
681 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
684 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
689 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
690 const struct nlattr **a, bool is_mask,
695 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
699 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
700 const struct ovs_key_ethernet *eth_key;
702 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
703 SW_FLOW_KEY_MEMCPY(match, eth.src,
704 eth_key->eth_src, ETH_ALEN, is_mask);
705 SW_FLOW_KEY_MEMCPY(match, eth.dst,
706 eth_key->eth_dst, ETH_ALEN, is_mask);
707 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
710 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
713 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
714 if (!(tci & htons(VLAN_TAG_PRESENT))) {
716 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
718 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
723 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
724 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
727 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
730 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
732 /* Always exact match EtherType. */
733 eth_type = htons(0xffff);
734 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
735 OVS_NLERR(log, "EtherType %x is less than min %x",
736 ntohs(eth_type), ETH_P_802_3_MIN);
740 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
741 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
742 } else if (!is_mask) {
743 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
746 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
747 const struct ovs_key_ipv4 *ipv4_key;
749 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
750 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
751 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
752 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
755 SW_FLOW_KEY_PUT(match, ip.proto,
756 ipv4_key->ipv4_proto, is_mask);
757 SW_FLOW_KEY_PUT(match, ip.tos,
758 ipv4_key->ipv4_tos, is_mask);
759 SW_FLOW_KEY_PUT(match, ip.ttl,
760 ipv4_key->ipv4_ttl, is_mask);
761 SW_FLOW_KEY_PUT(match, ip.frag,
762 ipv4_key->ipv4_frag, is_mask);
763 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
764 ipv4_key->ipv4_src, is_mask);
765 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
766 ipv4_key->ipv4_dst, is_mask);
767 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
770 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
771 const struct ovs_key_ipv6 *ipv6_key;
773 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
774 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
775 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
776 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
779 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
781 "Invalid IPv6 flow label value (value=%x, max=%x).",
782 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
785 SW_FLOW_KEY_PUT(match, ipv6.label,
786 ipv6_key->ipv6_label, is_mask);
787 SW_FLOW_KEY_PUT(match, ip.proto,
788 ipv6_key->ipv6_proto, is_mask);
789 SW_FLOW_KEY_PUT(match, ip.tos,
790 ipv6_key->ipv6_tclass, is_mask);
791 SW_FLOW_KEY_PUT(match, ip.ttl,
792 ipv6_key->ipv6_hlimit, is_mask);
793 SW_FLOW_KEY_PUT(match, ip.frag,
794 ipv6_key->ipv6_frag, is_mask);
795 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
797 sizeof(match->key->ipv6.addr.src),
799 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
801 sizeof(match->key->ipv6.addr.dst),
804 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
807 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
808 const struct ovs_key_arp *arp_key;
810 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
811 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
812 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
817 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
818 arp_key->arp_sip, is_mask);
819 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
820 arp_key->arp_tip, is_mask);
821 SW_FLOW_KEY_PUT(match, ip.proto,
822 ntohs(arp_key->arp_op), is_mask);
823 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
824 arp_key->arp_sha, ETH_ALEN, is_mask);
825 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
826 arp_key->arp_tha, ETH_ALEN, is_mask);
828 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
831 if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) {
832 const struct ovs_key_mpls *mpls_key;
834 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
835 SW_FLOW_KEY_PUT(match, mpls.top_lse,
836 mpls_key->mpls_lse, is_mask);
838 attrs &= ~(1ULL << OVS_KEY_ATTR_MPLS);
841 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
842 const struct ovs_key_tcp *tcp_key;
844 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
845 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
846 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
847 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
850 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
851 SW_FLOW_KEY_PUT(match, tp.flags,
852 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
854 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
857 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
858 const struct ovs_key_udp *udp_key;
860 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
861 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
862 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
863 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
866 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
867 const struct ovs_key_sctp *sctp_key;
869 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
870 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
871 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
872 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
875 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
876 const struct ovs_key_icmp *icmp_key;
878 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
879 SW_FLOW_KEY_PUT(match, tp.src,
880 htons(icmp_key->icmp_type), is_mask);
881 SW_FLOW_KEY_PUT(match, tp.dst,
882 htons(icmp_key->icmp_code), is_mask);
883 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
886 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
887 const struct ovs_key_icmpv6 *icmpv6_key;
889 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
890 SW_FLOW_KEY_PUT(match, tp.src,
891 htons(icmpv6_key->icmpv6_type), is_mask);
892 SW_FLOW_KEY_PUT(match, tp.dst,
893 htons(icmpv6_key->icmpv6_code), is_mask);
894 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
897 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
898 const struct ovs_key_nd *nd_key;
900 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
901 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
903 sizeof(match->key->ipv6.nd.target),
905 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
906 nd_key->nd_sll, ETH_ALEN, is_mask);
907 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
908 nd_key->nd_tll, ETH_ALEN, is_mask);
909 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
913 OVS_NLERR(log, "Unknown key attributes %llx",
914 (unsigned long long)attrs);
921 static void nlattr_set(struct nlattr *attr, u8 val,
922 const struct ovs_len_tbl *tbl)
927 /* The nlattr stream should already have been validated */
928 nla_for_each_nested(nla, attr, rem) {
929 if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
930 nlattr_set(nla, val, tbl[nla_type(nla)].next);
932 memset(nla_data(nla), val, nla_len(nla));
936 static void mask_set_nlattr(struct nlattr *attr, u8 val)
938 nlattr_set(attr, val, ovs_key_lens);
942 * ovs_nla_get_match - parses Netlink attributes into a flow key and
943 * mask. In case the 'mask' is NULL, the flow is treated as exact match
944 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
945 * does not include any don't care bit.
946 * @match: receives the extracted flow match information.
947 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
948 * sequence. The fields should of the packet that triggered the creation
950 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
951 * attribute specifies the mask field of the wildcarded flow.
952 * @log: Boolean to allow kernel error logging. Normally true, but when
953 * probing for feature compatibility this should be passed in as false to
954 * suppress unnecessary error logging.
956 int ovs_nla_get_match(struct sw_flow_match *match,
957 const struct nlattr *nla_key,
958 const struct nlattr *nla_mask,
961 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
962 const struct nlattr *encap;
963 struct nlattr *newmask = NULL;
966 bool encap_valid = false;
969 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
973 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
974 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
975 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
978 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
979 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
980 OVS_NLERR(log, "Invalid Vlan frame.");
984 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
985 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
986 encap = a[OVS_KEY_ATTR_ENCAP];
987 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
990 if (tci & htons(VLAN_TAG_PRESENT)) {
991 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
995 /* Corner case for truncated 802.1Q header. */
996 if (nla_len(encap)) {
997 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
1001 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
1006 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
1012 /* Create an exact match mask. We need to set to 0xff
1013 * all the 'match->mask' fields that have been touched
1014 * in 'match->key'. We cannot simply memset
1015 * 'match->mask', because padding bytes and fields not
1016 * specified in 'match->key' should be left to 0.
1017 * Instead, we use a stream of netlink attributes,
1018 * copied from 'key' and set to 0xff.
1019 * ovs_key_from_nlattrs() will take care of filling
1020 * 'match->mask' appropriately.
1022 newmask = kmemdup(nla_key,
1023 nla_total_size(nla_len(nla_key)),
1028 mask_set_nlattr(newmask, 0xff);
1030 /* The userspace does not send tunnel attributes that
1031 * are 0, but we should not wildcard them nonetheless.
1033 if (match->key->tun_key.ipv4_dst)
1034 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1040 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1044 /* Always match on tci. */
1045 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1047 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1048 __be16 eth_type = 0;
1052 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
1057 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1058 if (a[OVS_KEY_ATTR_ETHERTYPE])
1059 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1061 if (eth_type == htons(0xffff)) {
1062 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1063 encap = a[OVS_KEY_ATTR_ENCAP];
1064 err = parse_flow_mask_nlattrs(encap, a,
1069 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1075 if (a[OVS_KEY_ATTR_VLAN])
1076 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1078 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1079 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1086 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
1091 if (!match_validate(match, key_attrs, mask_attrs, log))
1100 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1101 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
1102 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1104 * @log: Boolean to allow kernel error logging. Normally true, but when
1105 * probing for feature compatibility this should be passed in as false to
1106 * suppress unnecessary error logging.
1108 * This parses a series of Netlink attributes that form a flow key, which must
1109 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1110 * get the metadata, that is, the parts of the flow key that cannot be
1111 * extracted from the packet itself.
1114 int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1115 struct sw_flow_key *key,
1118 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1119 struct sw_flow_match match;
1123 err = parse_flow_nlattrs(attr, a, &attrs, log);
1127 memset(&match, 0, sizeof(match));
1130 memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
1131 key->phy.in_port = DP_MAX_PORTS;
1133 return metadata_from_nlattrs(&match, &attrs, a, false, log);
1136 int ovs_nla_put_flow(const struct sw_flow_key *swkey,
1137 const struct sw_flow_key *output, struct sk_buff *skb)
1139 struct ovs_key_ethernet *eth_key;
1140 struct nlattr *nla, *encap;
1141 bool is_mask = (swkey != output);
1143 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1144 goto nla_put_failure;
1146 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1147 goto nla_put_failure;
1149 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1150 goto nla_put_failure;
1152 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1153 const void *opts = NULL;
1155 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1156 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
1158 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1159 swkey->tun_opts_len))
1160 goto nla_put_failure;
1163 if (swkey->phy.in_port == DP_MAX_PORTS) {
1164 if (is_mask && (output->phy.in_port == 0xffff))
1165 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1166 goto nla_put_failure;
1169 upper_u16 = !is_mask ? 0 : 0xffff;
1171 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1172 (upper_u16 << 16) | output->phy.in_port))
1173 goto nla_put_failure;
1176 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1177 goto nla_put_failure;
1179 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1181 goto nla_put_failure;
1183 eth_key = nla_data(nla);
1184 ether_addr_copy(eth_key->eth_src, output->eth.src);
1185 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1187 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1189 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1190 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1191 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1192 goto nla_put_failure;
1193 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1194 if (!swkey->eth.tci)
1199 if (swkey->eth.type == htons(ETH_P_802_2)) {
1201 * Ethertype 802.2 is represented in the netlink with omitted
1202 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1203 * 0xffff in the mask attribute. Ethertype can also
1206 if (is_mask && output->eth.type)
1207 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1209 goto nla_put_failure;
1213 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1214 goto nla_put_failure;
1216 if (swkey->eth.type == htons(ETH_P_IP)) {
1217 struct ovs_key_ipv4 *ipv4_key;
1219 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1221 goto nla_put_failure;
1222 ipv4_key = nla_data(nla);
1223 ipv4_key->ipv4_src = output->ipv4.addr.src;
1224 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1225 ipv4_key->ipv4_proto = output->ip.proto;
1226 ipv4_key->ipv4_tos = output->ip.tos;
1227 ipv4_key->ipv4_ttl = output->ip.ttl;
1228 ipv4_key->ipv4_frag = output->ip.frag;
1229 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1230 struct ovs_key_ipv6 *ipv6_key;
1232 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1234 goto nla_put_failure;
1235 ipv6_key = nla_data(nla);
1236 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1237 sizeof(ipv6_key->ipv6_src));
1238 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1239 sizeof(ipv6_key->ipv6_dst));
1240 ipv6_key->ipv6_label = output->ipv6.label;
1241 ipv6_key->ipv6_proto = output->ip.proto;
1242 ipv6_key->ipv6_tclass = output->ip.tos;
1243 ipv6_key->ipv6_hlimit = output->ip.ttl;
1244 ipv6_key->ipv6_frag = output->ip.frag;
1245 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1246 swkey->eth.type == htons(ETH_P_RARP)) {
1247 struct ovs_key_arp *arp_key;
1249 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1251 goto nla_put_failure;
1252 arp_key = nla_data(nla);
1253 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1254 arp_key->arp_sip = output->ipv4.addr.src;
1255 arp_key->arp_tip = output->ipv4.addr.dst;
1256 arp_key->arp_op = htons(output->ip.proto);
1257 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1258 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1259 } else if (eth_p_mpls(swkey->eth.type)) {
1260 struct ovs_key_mpls *mpls_key;
1262 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1264 goto nla_put_failure;
1265 mpls_key = nla_data(nla);
1266 mpls_key->mpls_lse = output->mpls.top_lse;
1269 if ((swkey->eth.type == htons(ETH_P_IP) ||
1270 swkey->eth.type == htons(ETH_P_IPV6)) &&
1271 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1273 if (swkey->ip.proto == IPPROTO_TCP) {
1274 struct ovs_key_tcp *tcp_key;
1276 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1278 goto nla_put_failure;
1279 tcp_key = nla_data(nla);
1280 tcp_key->tcp_src = output->tp.src;
1281 tcp_key->tcp_dst = output->tp.dst;
1282 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1284 goto nla_put_failure;
1285 } else if (swkey->ip.proto == IPPROTO_UDP) {
1286 struct ovs_key_udp *udp_key;
1288 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1290 goto nla_put_failure;
1291 udp_key = nla_data(nla);
1292 udp_key->udp_src = output->tp.src;
1293 udp_key->udp_dst = output->tp.dst;
1294 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1295 struct ovs_key_sctp *sctp_key;
1297 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1299 goto nla_put_failure;
1300 sctp_key = nla_data(nla);
1301 sctp_key->sctp_src = output->tp.src;
1302 sctp_key->sctp_dst = output->tp.dst;
1303 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1304 swkey->ip.proto == IPPROTO_ICMP) {
1305 struct ovs_key_icmp *icmp_key;
1307 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1309 goto nla_put_failure;
1310 icmp_key = nla_data(nla);
1311 icmp_key->icmp_type = ntohs(output->tp.src);
1312 icmp_key->icmp_code = ntohs(output->tp.dst);
1313 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1314 swkey->ip.proto == IPPROTO_ICMPV6) {
1315 struct ovs_key_icmpv6 *icmpv6_key;
1317 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1318 sizeof(*icmpv6_key));
1320 goto nla_put_failure;
1321 icmpv6_key = nla_data(nla);
1322 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1323 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1325 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1326 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1327 struct ovs_key_nd *nd_key;
1329 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1331 goto nla_put_failure;
1332 nd_key = nla_data(nla);
1333 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1334 sizeof(nd_key->nd_target));
1335 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1336 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1343 nla_nest_end(skb, encap);
1351 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1353 static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
1355 struct sw_flow_actions *sfa;
1357 if (size > MAX_ACTIONS_BUFSIZE) {
1358 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
1359 return ERR_PTR(-EINVAL);
1362 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1364 return ERR_PTR(-ENOMEM);
1366 sfa->actions_len = 0;
1370 /* RCU callback used by ovs_nla_free_flow_actions. */
1371 static void rcu_free_acts_callback(struct rcu_head *rcu)
1373 struct sw_flow_actions *sf_acts = container_of(rcu,
1374 struct sw_flow_actions, rcu);
1378 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1379 * The caller must hold rcu_read_lock for this to be sensible.
1381 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1383 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1386 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1387 int attr_len, bool log)
1390 struct sw_flow_actions *acts;
1392 int req_size = NLA_ALIGN(attr_len);
1393 int next_offset = offsetof(struct sw_flow_actions, actions) +
1394 (*sfa)->actions_len;
1396 if (req_size <= (ksize(*sfa) - next_offset))
1399 new_acts_size = ksize(*sfa) * 2;
1401 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1402 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1403 return ERR_PTR(-EMSGSIZE);
1404 new_acts_size = MAX_ACTIONS_BUFSIZE;
1407 acts = nla_alloc_flow_actions(new_acts_size, log);
1409 return (void *)acts;
1411 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1412 acts->actions_len = (*sfa)->actions_len;
1417 (*sfa)->actions_len += req_size;
1418 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1421 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1422 int attrtype, void *data, int len, bool log)
1426 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
1430 a->nla_type = attrtype;
1431 a->nla_len = nla_attr_size(len);
1434 memcpy(nla_data(a), data, len);
1435 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1440 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1441 void *data, int len, bool log)
1445 a = __add_action(sfa, attrtype, data, len, log);
1452 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1453 int attrtype, bool log)
1455 int used = (*sfa)->actions_len;
1458 err = add_action(sfa, attrtype, NULL, 0, log);
1465 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1468 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1471 a->nla_len = sfa->actions_len - st_offset;
1474 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1475 const struct sw_flow_key *key,
1476 int depth, struct sw_flow_actions **sfa,
1477 __be16 eth_type, __be16 vlan_tci, bool log);
1479 static int validate_and_copy_sample(const struct nlattr *attr,
1480 const struct sw_flow_key *key, int depth,
1481 struct sw_flow_actions **sfa,
1482 __be16 eth_type, __be16 vlan_tci, bool log)
1484 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1485 const struct nlattr *probability, *actions;
1486 const struct nlattr *a;
1487 int rem, start, err, st_acts;
1489 memset(attrs, 0, sizeof(attrs));
1490 nla_for_each_nested(a, attr, rem) {
1491 int type = nla_type(a);
1492 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1499 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1500 if (!probability || nla_len(probability) != sizeof(u32))
1503 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1504 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1507 /* validation done, copy sample action. */
1508 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
1511 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1512 nla_data(probability), sizeof(u32), log);
1515 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
1519 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
1520 eth_type, vlan_tci, log);
1524 add_nested_action_end(*sfa, st_acts);
1525 add_nested_action_end(*sfa, start);
1530 static int validate_tp_port(const struct sw_flow_key *flow_key,
1533 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1534 (flow_key->tp.src || flow_key->tp.dst))
1540 void ovs_match_init(struct sw_flow_match *match,
1541 struct sw_flow_key *key,
1542 struct sw_flow_mask *mask)
1544 memset(match, 0, sizeof(*match));
1548 memset(key, 0, sizeof(*key));
1551 memset(&mask->key, 0, sizeof(mask->key));
1552 mask->range.start = mask->range.end = 0;
1556 static int validate_geneve_opts(struct sw_flow_key *key)
1558 struct geneve_opt *option;
1559 int opts_len = key->tun_opts_len;
1560 bool crit_opt = false;
1562 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1563 while (opts_len > 0) {
1566 if (opts_len < sizeof(*option))
1569 len = sizeof(*option) + option->length * 4;
1573 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1575 option = (struct geneve_opt *)((u8 *)option + len);
1579 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1584 static int validate_and_copy_set_tun(const struct nlattr *attr,
1585 struct sw_flow_actions **sfa, bool log)
1587 struct sw_flow_match match;
1588 struct sw_flow_key key;
1589 struct ovs_tunnel_info *tun_info;
1593 ovs_match_init(&match, &key, NULL);
1594 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1598 if (key.tun_opts_len) {
1599 err = validate_geneve_opts(&key);
1604 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
1608 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1609 sizeof(*tun_info) + key.tun_opts_len, log);
1613 tun_info = nla_data(a);
1614 tun_info->tunnel = key.tun_key;
1615 tun_info->options_len = key.tun_opts_len;
1617 if (tun_info->options_len) {
1618 /* We need to store the options in the action itself since
1619 * everything else will go away after flow setup. We can append
1620 * it to tun_info and then point there.
1622 memcpy((tun_info + 1),
1623 TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
1624 tun_info->options = (tun_info + 1);
1627 tun_info->options = NULL;
1630 add_nested_action_end(*sfa, start);
1635 static int validate_set(const struct nlattr *a,
1636 const struct sw_flow_key *flow_key,
1637 struct sw_flow_actions **sfa,
1638 bool *set_tun, __be16 eth_type, bool log)
1640 const struct nlattr *ovs_key = nla_data(a);
1641 int key_type = nla_type(ovs_key);
1643 /* There can be only one key in a action */
1644 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1647 if (key_type > OVS_KEY_ATTR_MAX ||
1648 (ovs_key_lens[key_type].len != nla_len(ovs_key) &&
1649 ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
1653 const struct ovs_key_ipv4 *ipv4_key;
1654 const struct ovs_key_ipv6 *ipv6_key;
1657 case OVS_KEY_ATTR_PRIORITY:
1658 case OVS_KEY_ATTR_SKB_MARK:
1659 case OVS_KEY_ATTR_ETHERNET:
1662 case OVS_KEY_ATTR_TUNNEL:
1663 if (eth_p_mpls(eth_type))
1667 err = validate_and_copy_set_tun(a, sfa, log);
1672 case OVS_KEY_ATTR_IPV4:
1673 if (eth_type != htons(ETH_P_IP))
1676 if (!flow_key->ip.proto)
1679 ipv4_key = nla_data(ovs_key);
1680 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1683 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1688 case OVS_KEY_ATTR_IPV6:
1689 if (eth_type != htons(ETH_P_IPV6))
1692 if (!flow_key->ip.proto)
1695 ipv6_key = nla_data(ovs_key);
1696 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1699 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1702 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1707 case OVS_KEY_ATTR_TCP:
1708 if (flow_key->ip.proto != IPPROTO_TCP)
1711 return validate_tp_port(flow_key, eth_type);
1713 case OVS_KEY_ATTR_UDP:
1714 if (flow_key->ip.proto != IPPROTO_UDP)
1717 return validate_tp_port(flow_key, eth_type);
1719 case OVS_KEY_ATTR_MPLS:
1720 if (!eth_p_mpls(eth_type))
1724 case OVS_KEY_ATTR_SCTP:
1725 if (flow_key->ip.proto != IPPROTO_SCTP)
1728 return validate_tp_port(flow_key, eth_type);
1737 static int validate_userspace(const struct nlattr *attr)
1739 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1740 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1741 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1742 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
1744 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1747 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1748 attr, userspace_policy);
1752 if (!a[OVS_USERSPACE_ATTR_PID] ||
1753 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1759 static int copy_action(const struct nlattr *from,
1760 struct sw_flow_actions **sfa, bool log)
1762 int totlen = NLA_ALIGN(from->nla_len);
1765 to = reserve_sfa_size(sfa, from->nla_len, log);
1769 memcpy(to, from, totlen);
1773 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1774 const struct sw_flow_key *key,
1775 int depth, struct sw_flow_actions **sfa,
1776 __be16 eth_type, __be16 vlan_tci, bool log)
1778 const struct nlattr *a;
1781 if (depth >= SAMPLE_ACTION_DEPTH)
1784 nla_for_each_nested(a, attr, rem) {
1785 /* Expected argument lengths, (u32)-1 for variable length. */
1786 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1787 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1788 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1789 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1790 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1791 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
1792 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1793 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1794 [OVS_ACTION_ATTR_SET] = (u32)-1,
1795 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1796 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1798 const struct ovs_action_push_vlan *vlan;
1799 int type = nla_type(a);
1802 if (type > OVS_ACTION_ATTR_MAX ||
1803 (action_lens[type] != nla_len(a) &&
1804 action_lens[type] != (u32)-1))
1809 case OVS_ACTION_ATTR_UNSPEC:
1812 case OVS_ACTION_ATTR_USERSPACE:
1813 err = validate_userspace(a);
1818 case OVS_ACTION_ATTR_OUTPUT:
1819 if (nla_get_u32(a) >= DP_MAX_PORTS)
1824 case OVS_ACTION_ATTR_HASH: {
1825 const struct ovs_action_hash *act_hash = nla_data(a);
1827 switch (act_hash->hash_alg) {
1828 case OVS_HASH_ALG_L4:
1837 case OVS_ACTION_ATTR_POP_VLAN:
1838 vlan_tci = htons(0);
1841 case OVS_ACTION_ATTR_PUSH_VLAN:
1843 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1845 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1847 vlan_tci = vlan->vlan_tci;
1850 case OVS_ACTION_ATTR_RECIRC:
1853 case OVS_ACTION_ATTR_PUSH_MPLS: {
1854 const struct ovs_action_push_mpls *mpls = nla_data(a);
1856 if (!eth_p_mpls(mpls->mpls_ethertype))
1859 /* Prohibit push MPLS other than to a white list
1860 * for packets that have a known tag order.
1862 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1863 (eth_type != htons(ETH_P_IP) &&
1864 eth_type != htons(ETH_P_IPV6) &&
1865 eth_type != htons(ETH_P_ARP) &&
1866 eth_type != htons(ETH_P_RARP) &&
1867 !eth_p_mpls(eth_type)))
1869 eth_type = mpls->mpls_ethertype;
1873 case OVS_ACTION_ATTR_POP_MPLS:
1874 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
1875 !eth_p_mpls(eth_type))
1878 /* Disallow subsequent L2.5+ set and mpls_pop actions
1879 * as there is no check here to ensure that the new
1880 * eth_type is valid and thus set actions could
1881 * write off the end of the packet or otherwise
1884 * Support for these actions is planned using packet
1887 eth_type = htons(0);
1890 case OVS_ACTION_ATTR_SET:
1891 err = validate_set(a, key, sfa,
1892 &skip_copy, eth_type, log);
1897 case OVS_ACTION_ATTR_SAMPLE:
1898 err = validate_and_copy_sample(a, key, depth, sfa,
1899 eth_type, vlan_tci, log);
1906 OVS_NLERR(log, "Unknown Action type %d", type);
1910 err = copy_action(a, sfa, log);
1922 int ovs_nla_copy_actions(const struct nlattr *attr,
1923 const struct sw_flow_key *key,
1924 struct sw_flow_actions **sfa, bool log)
1928 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
1930 return PTR_ERR(*sfa);
1932 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
1940 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1942 const struct nlattr *a;
1943 struct nlattr *start;
1946 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1950 nla_for_each_nested(a, attr, rem) {
1951 int type = nla_type(a);
1952 struct nlattr *st_sample;
1955 case OVS_SAMPLE_ATTR_PROBABILITY:
1956 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1957 sizeof(u32), nla_data(a)))
1960 case OVS_SAMPLE_ATTR_ACTIONS:
1961 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1964 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1967 nla_nest_end(skb, st_sample);
1972 nla_nest_end(skb, start);
1976 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1978 const struct nlattr *ovs_key = nla_data(a);
1979 int key_type = nla_type(ovs_key);
1980 struct nlattr *start;
1984 case OVS_KEY_ATTR_TUNNEL_INFO: {
1985 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
1987 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1991 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
1992 tun_info->options_len ?
1993 tun_info->options : NULL,
1994 tun_info->options_len);
1997 nla_nest_end(skb, start);
2001 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2009 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2011 const struct nlattr *a;
2014 nla_for_each_attr(a, attr, len, rem) {
2015 int type = nla_type(a);
2018 case OVS_ACTION_ATTR_SET:
2019 err = set_action_to_attr(a, skb);
2024 case OVS_ACTION_ATTR_SAMPLE:
2025 err = sample_action_to_attr(a, skb);
2030 if (nla_put(skb, type, nla_len(a), nla_data(a)))