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"
52 #include "vport-vxlan.h"
56 const struct ovs_len_tbl *next;
59 #define OVS_ATTR_NESTED -1
61 static void update_range(struct sw_flow_match *match,
62 size_t offset, size_t size, bool is_mask)
64 struct sw_flow_key_range *range;
65 size_t start = rounddown(offset, sizeof(long));
66 size_t end = roundup(offset + size, sizeof(long));
69 range = &match->range;
71 range = &match->mask->range;
73 if (range->start == range->end) {
79 if (range->start > start)
86 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
88 update_range(match, offsetof(struct sw_flow_key, field), \
89 sizeof((match)->key->field), is_mask); \
91 (match)->mask->key.field = value; \
93 (match)->key->field = value; \
96 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
98 update_range(match, offset, len, is_mask); \
100 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
102 memcpy((u8 *)(match)->key + offset, value_p, len); \
105 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
106 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
107 value_p, len, is_mask)
109 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
111 update_range(match, offsetof(struct sw_flow_key, field), \
112 sizeof((match)->key->field), is_mask); \
114 memset((u8 *)&(match)->mask->key.field, value, \
115 sizeof((match)->mask->key.field)); \
117 memset((u8 *)&(match)->key->field, value, \
118 sizeof((match)->key->field)); \
121 static bool match_validate(const struct sw_flow_match *match,
122 u64 key_attrs, u64 mask_attrs, bool log)
124 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
125 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
127 /* The following mask attributes allowed only if they
128 * pass the validation tests.
130 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
131 | (1ULL << OVS_KEY_ATTR_IPV6)
132 | (1ULL << OVS_KEY_ATTR_TCP)
133 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
134 | (1ULL << OVS_KEY_ATTR_UDP)
135 | (1ULL << OVS_KEY_ATTR_SCTP)
136 | (1ULL << OVS_KEY_ATTR_ICMP)
137 | (1ULL << OVS_KEY_ATTR_ICMPV6)
138 | (1ULL << OVS_KEY_ATTR_ARP)
139 | (1ULL << OVS_KEY_ATTR_ND)
140 | (1ULL << OVS_KEY_ATTR_MPLS));
142 /* Always allowed mask fields. */
143 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
144 | (1ULL << OVS_KEY_ATTR_IN_PORT)
145 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
147 /* Check key attributes. */
148 if (match->key->eth.type == htons(ETH_P_ARP)
149 || match->key->eth.type == htons(ETH_P_RARP)) {
150 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
151 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
152 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
155 if (eth_p_mpls(match->key->eth.type)) {
156 key_expected |= 1ULL << OVS_KEY_ATTR_MPLS;
157 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
158 mask_allowed |= 1ULL << OVS_KEY_ATTR_MPLS;
161 if (match->key->eth.type == htons(ETH_P_IP)) {
162 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
163 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
164 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
166 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
167 if (match->key->ip.proto == IPPROTO_UDP) {
168 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
169 if (match->mask && (match->mask->key.ip.proto == 0xff))
170 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
173 if (match->key->ip.proto == IPPROTO_SCTP) {
174 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
175 if (match->mask && (match->mask->key.ip.proto == 0xff))
176 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
179 if (match->key->ip.proto == IPPROTO_TCP) {
180 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
181 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
182 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
183 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
184 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
188 if (match->key->ip.proto == IPPROTO_ICMP) {
189 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
190 if (match->mask && (match->mask->key.ip.proto == 0xff))
191 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
196 if (match->key->eth.type == htons(ETH_P_IPV6)) {
197 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
198 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
199 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
201 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
202 if (match->key->ip.proto == IPPROTO_UDP) {
203 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
208 if (match->key->ip.proto == IPPROTO_SCTP) {
209 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
210 if (match->mask && (match->mask->key.ip.proto == 0xff))
211 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
214 if (match->key->ip.proto == IPPROTO_TCP) {
215 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
216 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
217 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
218 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
219 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
223 if (match->key->ip.proto == IPPROTO_ICMPV6) {
224 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
225 if (match->mask && (match->mask->key.ip.proto == 0xff))
226 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
228 if (match->key->tp.src ==
229 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
230 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
231 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
232 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
233 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
239 if ((key_attrs & key_expected) != key_expected) {
240 /* Key attributes check failed. */
241 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
242 (unsigned long long)key_attrs,
243 (unsigned long long)key_expected);
247 if ((mask_attrs & mask_allowed) != mask_attrs) {
248 /* Mask attributes check failed. */
249 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
250 (unsigned long long)mask_attrs,
251 (unsigned long long)mask_allowed);
258 size_t ovs_tun_key_attr_size(void)
260 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
261 * updating this function.
263 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
264 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
265 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
266 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
267 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
268 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
269 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
270 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
271 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
272 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
273 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
275 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
276 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
279 size_t ovs_key_attr_size(void)
281 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
282 * updating this function.
284 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
286 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
287 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
288 + ovs_tun_key_attr_size()
289 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
290 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
291 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
292 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
293 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
294 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
295 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
296 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
297 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
298 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
299 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
300 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
303 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
304 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
305 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
306 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
307 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
308 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
309 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
310 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
311 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
312 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
313 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
314 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_NESTED },
315 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED },
318 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
319 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
320 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
321 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
322 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
323 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
324 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
325 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
326 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
327 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
328 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
329 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
330 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
331 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
332 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
333 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
334 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
335 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
336 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
337 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
338 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
339 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
340 .next = ovs_tunnel_key_lens, },
341 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
343 static bool is_all_zero(const u8 *fp, size_t size)
350 for (i = 0; i < size; i++)
357 static int __parse_flow_nlattrs(const struct nlattr *attr,
358 const struct nlattr *a[],
359 u64 *attrsp, bool log, bool nz)
361 const struct nlattr *nla;
366 nla_for_each_nested(nla, attr, rem) {
367 u16 type = nla_type(nla);
370 if (type > OVS_KEY_ATTR_MAX) {
371 OVS_NLERR(log, "Key type %d is out of range max %d",
372 type, OVS_KEY_ATTR_MAX);
376 if (attrs & (1ULL << type)) {
377 OVS_NLERR(log, "Duplicate key (type %d).", type);
381 expected_len = ovs_key_lens[type].len;
382 if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
383 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
384 type, nla_len(nla), expected_len);
388 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
389 attrs |= 1ULL << type;
394 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
402 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
403 const struct nlattr *a[], u64 *attrsp,
406 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
409 static int parse_flow_nlattrs(const struct nlattr *attr,
410 const struct nlattr *a[], u64 *attrsp,
413 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
416 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
417 struct sw_flow_match *match, bool is_mask,
420 unsigned long opt_key_offset;
422 if (nla_len(a) > sizeof(match->key->tun_opts)) {
423 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
424 nla_len(a), sizeof(match->key->tun_opts));
428 if (nla_len(a) % 4 != 0) {
429 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
434 /* We need to record the length of the options passed
435 * down, otherwise packets with the same format but
436 * additional options will be silently matched.
439 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
442 /* This is somewhat unusual because it looks at
443 * both the key and mask while parsing the
444 * attributes (and by extension assumes the key
445 * is parsed first). Normally, we would verify
446 * that each is the correct length and that the
447 * attributes line up in the validate function.
448 * However, that is difficult because this is
449 * variable length and we won't have the
452 if (match->key->tun_opts_len != nla_len(a)) {
453 OVS_NLERR(log, "Geneve option len %d != mask len %d",
454 match->key->tun_opts_len, nla_len(a));
458 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
461 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
462 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
463 nla_len(a), is_mask);
467 static const struct nla_policy vxlan_opt_policy[OVS_VXLAN_EXT_MAX + 1] = {
468 [OVS_VXLAN_EXT_GBP] = { .type = NLA_U32 },
471 static int vxlan_tun_opt_from_nlattr(const struct nlattr *a,
472 struct sw_flow_match *match, bool is_mask,
475 struct nlattr *tb[OVS_VXLAN_EXT_MAX+1];
476 unsigned long opt_key_offset;
477 struct ovs_vxlan_opts opts;
480 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
482 err = nla_parse_nested(tb, OVS_VXLAN_EXT_MAX, a, vxlan_opt_policy);
486 memset(&opts, 0, sizeof(opts));
488 if (tb[OVS_VXLAN_EXT_GBP])
489 opts.gbp = nla_get_u32(tb[OVS_VXLAN_EXT_GBP]);
492 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
494 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
496 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
497 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
502 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
503 struct sw_flow_match *match, bool is_mask,
509 __be16 tun_flags = 0;
512 nla_for_each_nested(a, attr, rem) {
513 int type = nla_type(a);
516 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
517 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
518 type, OVS_TUNNEL_KEY_ATTR_MAX);
522 if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
523 ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
524 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
525 type, nla_len(a), ovs_tunnel_key_lens[type].len);
530 case OVS_TUNNEL_KEY_ATTR_ID:
531 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
532 nla_get_be64(a), is_mask);
533 tun_flags |= TUNNEL_KEY;
535 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
536 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
537 nla_get_be32(a), is_mask);
539 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
540 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
541 nla_get_be32(a), is_mask);
543 case OVS_TUNNEL_KEY_ATTR_TOS:
544 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
545 nla_get_u8(a), is_mask);
547 case OVS_TUNNEL_KEY_ATTR_TTL:
548 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
549 nla_get_u8(a), is_mask);
552 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
553 tun_flags |= TUNNEL_DONT_FRAGMENT;
555 case OVS_TUNNEL_KEY_ATTR_CSUM:
556 tun_flags |= TUNNEL_CSUM;
558 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
559 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
560 nla_get_be16(a), is_mask);
562 case OVS_TUNNEL_KEY_ATTR_TP_DST:
563 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
564 nla_get_be16(a), is_mask);
566 case OVS_TUNNEL_KEY_ATTR_OAM:
567 tun_flags |= TUNNEL_OAM;
569 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
571 OVS_NLERR(log, "Multiple metadata blocks provided");
575 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
579 tun_flags |= TUNNEL_GENEVE_OPT;
582 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
584 OVS_NLERR(log, "Multiple metadata blocks provided");
588 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
592 tun_flags |= TUNNEL_VXLAN_OPT;
596 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
602 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
605 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
611 if (!match->key->tun_key.ipv4_dst) {
612 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
617 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
625 static int vxlan_opt_to_nlattr(struct sk_buff *skb,
626 const void *tun_opts, int swkey_tun_opts_len)
628 const struct ovs_vxlan_opts *opts = tun_opts;
631 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
635 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
638 nla_nest_end(skb, nla);
642 static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
643 const struct ovs_key_ipv4_tunnel *output,
644 const void *tun_opts, int swkey_tun_opts_len)
646 if (output->tun_flags & TUNNEL_KEY &&
647 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
649 if (output->ipv4_src &&
650 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
652 if (output->ipv4_dst &&
653 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
655 if (output->ipv4_tos &&
656 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
658 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
660 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
661 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
663 if ((output->tun_flags & TUNNEL_CSUM) &&
664 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
666 if (output->tp_src &&
667 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
669 if (output->tp_dst &&
670 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
672 if ((output->tun_flags & TUNNEL_OAM) &&
673 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
676 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
677 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
678 swkey_tun_opts_len, tun_opts))
680 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
681 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
688 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
689 const struct ovs_key_ipv4_tunnel *output,
690 const void *tun_opts, int swkey_tun_opts_len)
695 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
699 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
703 nla_nest_end(skb, nla);
707 int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
708 const struct ovs_tunnel_info *egress_tun_info)
710 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
711 egress_tun_info->options,
712 egress_tun_info->options_len);
715 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
716 const struct nlattr **a, bool is_mask,
719 if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
720 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
722 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
723 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
726 if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
727 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
729 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
730 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
733 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
734 SW_FLOW_KEY_PUT(match, phy.priority,
735 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
736 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
739 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
740 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
743 in_port = 0xffffffff; /* Always exact match in_port. */
744 } else if (in_port >= DP_MAX_PORTS) {
745 OVS_NLERR(log, "Port %d exceeds max allowable %d",
746 in_port, DP_MAX_PORTS);
750 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
751 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
752 } else if (!is_mask) {
753 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
756 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
757 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
759 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
760 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
762 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
763 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
766 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
771 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
772 const struct nlattr **a, bool is_mask,
777 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
781 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
782 const struct ovs_key_ethernet *eth_key;
784 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
785 SW_FLOW_KEY_MEMCPY(match, eth.src,
786 eth_key->eth_src, ETH_ALEN, is_mask);
787 SW_FLOW_KEY_MEMCPY(match, eth.dst,
788 eth_key->eth_dst, ETH_ALEN, is_mask);
789 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
792 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
795 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
796 if (!(tci & htons(VLAN_TAG_PRESENT))) {
798 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
800 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
805 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
806 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
809 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
812 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
814 /* Always exact match EtherType. */
815 eth_type = htons(0xffff);
816 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
817 OVS_NLERR(log, "EtherType %x is less than min %x",
818 ntohs(eth_type), ETH_P_802_3_MIN);
822 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
823 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
824 } else if (!is_mask) {
825 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
828 if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
829 const struct ovs_key_ipv4 *ipv4_key;
831 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
832 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
833 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
834 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
837 SW_FLOW_KEY_PUT(match, ip.proto,
838 ipv4_key->ipv4_proto, is_mask);
839 SW_FLOW_KEY_PUT(match, ip.tos,
840 ipv4_key->ipv4_tos, is_mask);
841 SW_FLOW_KEY_PUT(match, ip.ttl,
842 ipv4_key->ipv4_ttl, is_mask);
843 SW_FLOW_KEY_PUT(match, ip.frag,
844 ipv4_key->ipv4_frag, is_mask);
845 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
846 ipv4_key->ipv4_src, is_mask);
847 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
848 ipv4_key->ipv4_dst, is_mask);
849 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
852 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
853 const struct ovs_key_ipv6 *ipv6_key;
855 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
856 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
857 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
858 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
861 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
863 "Invalid IPv6 flow label value (value=%x, max=%x).",
864 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
867 SW_FLOW_KEY_PUT(match, ipv6.label,
868 ipv6_key->ipv6_label, is_mask);
869 SW_FLOW_KEY_PUT(match, ip.proto,
870 ipv6_key->ipv6_proto, is_mask);
871 SW_FLOW_KEY_PUT(match, ip.tos,
872 ipv6_key->ipv6_tclass, is_mask);
873 SW_FLOW_KEY_PUT(match, ip.ttl,
874 ipv6_key->ipv6_hlimit, is_mask);
875 SW_FLOW_KEY_PUT(match, ip.frag,
876 ipv6_key->ipv6_frag, is_mask);
877 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
879 sizeof(match->key->ipv6.addr.src),
881 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
883 sizeof(match->key->ipv6.addr.dst),
886 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
889 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
890 const struct ovs_key_arp *arp_key;
892 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
893 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
894 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
899 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
900 arp_key->arp_sip, is_mask);
901 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
902 arp_key->arp_tip, is_mask);
903 SW_FLOW_KEY_PUT(match, ip.proto,
904 ntohs(arp_key->arp_op), is_mask);
905 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
906 arp_key->arp_sha, ETH_ALEN, is_mask);
907 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
908 arp_key->arp_tha, ETH_ALEN, is_mask);
910 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
913 if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) {
914 const struct ovs_key_mpls *mpls_key;
916 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
917 SW_FLOW_KEY_PUT(match, mpls.top_lse,
918 mpls_key->mpls_lse, is_mask);
920 attrs &= ~(1ULL << OVS_KEY_ATTR_MPLS);
923 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
924 const struct ovs_key_tcp *tcp_key;
926 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
927 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
928 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
929 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
932 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
933 SW_FLOW_KEY_PUT(match, tp.flags,
934 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
936 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
939 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
940 const struct ovs_key_udp *udp_key;
942 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
943 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
944 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
945 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
948 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
949 const struct ovs_key_sctp *sctp_key;
951 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
952 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
953 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
954 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
957 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
958 const struct ovs_key_icmp *icmp_key;
960 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
961 SW_FLOW_KEY_PUT(match, tp.src,
962 htons(icmp_key->icmp_type), is_mask);
963 SW_FLOW_KEY_PUT(match, tp.dst,
964 htons(icmp_key->icmp_code), is_mask);
965 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
968 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
969 const struct ovs_key_icmpv6 *icmpv6_key;
971 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
972 SW_FLOW_KEY_PUT(match, tp.src,
973 htons(icmpv6_key->icmpv6_type), is_mask);
974 SW_FLOW_KEY_PUT(match, tp.dst,
975 htons(icmpv6_key->icmpv6_code), is_mask);
976 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
979 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
980 const struct ovs_key_nd *nd_key;
982 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
983 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
985 sizeof(match->key->ipv6.nd.target),
987 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
988 nd_key->nd_sll, ETH_ALEN, is_mask);
989 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
990 nd_key->nd_tll, ETH_ALEN, is_mask);
991 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
995 OVS_NLERR(log, "Unknown key attributes %llx",
996 (unsigned long long)attrs);
1003 static void nlattr_set(struct nlattr *attr, u8 val,
1004 const struct ovs_len_tbl *tbl)
1009 /* The nlattr stream should already have been validated */
1010 nla_for_each_nested(nla, attr, rem) {
1011 if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1012 nlattr_set(nla, val, tbl[nla_type(nla)].next);
1014 memset(nla_data(nla), val, nla_len(nla));
1018 static void mask_set_nlattr(struct nlattr *attr, u8 val)
1020 nlattr_set(attr, val, ovs_key_lens);
1024 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1025 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1026 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1027 * does not include any don't care bit.
1028 * @match: receives the extracted flow match information.
1029 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1030 * sequence. The fields should of the packet that triggered the creation
1032 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1033 * attribute specifies the mask field of the wildcarded flow.
1034 * @log: Boolean to allow kernel error logging. Normally true, but when
1035 * probing for feature compatibility this should be passed in as false to
1036 * suppress unnecessary error logging.
1038 int ovs_nla_get_match(struct sw_flow_match *match,
1039 const struct nlattr *nla_key,
1040 const struct nlattr *nla_mask,
1043 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1044 const struct nlattr *encap;
1045 struct nlattr *newmask = NULL;
1048 bool encap_valid = false;
1051 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
1055 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1056 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
1057 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1060 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
1061 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
1062 OVS_NLERR(log, "Invalid Vlan frame.");
1066 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1067 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1068 encap = a[OVS_KEY_ATTR_ENCAP];
1069 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1072 if (tci & htons(VLAN_TAG_PRESENT)) {
1073 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
1077 /* Corner case for truncated 802.1Q header. */
1078 if (nla_len(encap)) {
1079 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
1083 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
1088 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
1094 /* Create an exact match mask. We need to set to 0xff
1095 * all the 'match->mask' fields that have been touched
1096 * in 'match->key'. We cannot simply memset
1097 * 'match->mask', because padding bytes and fields not
1098 * specified in 'match->key' should be left to 0.
1099 * Instead, we use a stream of netlink attributes,
1100 * copied from 'key' and set to 0xff.
1101 * ovs_key_from_nlattrs() will take care of filling
1102 * 'match->mask' appropriately.
1104 newmask = kmemdup(nla_key,
1105 nla_total_size(nla_len(nla_key)),
1110 mask_set_nlattr(newmask, 0xff);
1112 /* The userspace does not send tunnel attributes that
1113 * are 0, but we should not wildcard them nonetheless.
1115 if (match->key->tun_key.ipv4_dst)
1116 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1122 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1126 /* Always match on tci. */
1127 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1129 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1130 __be16 eth_type = 0;
1134 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
1139 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1140 if (a[OVS_KEY_ATTR_ETHERTYPE])
1141 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1143 if (eth_type == htons(0xffff)) {
1144 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1145 encap = a[OVS_KEY_ATTR_ENCAP];
1146 err = parse_flow_mask_nlattrs(encap, a,
1151 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1157 if (a[OVS_KEY_ATTR_VLAN])
1158 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1160 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1161 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1168 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
1173 if (!match_validate(match, key_attrs, mask_attrs, log))
1181 static size_t get_ufid_len(const struct nlattr *attr, bool log)
1188 len = nla_len(attr);
1189 if (len < 1 || len > MAX_UFID_LENGTH) {
1190 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1191 nla_len(attr), MAX_UFID_LENGTH);
1198 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1199 * or false otherwise.
1201 bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1204 sfid->ufid_len = get_ufid_len(attr, log);
1206 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1208 return sfid->ufid_len;
1211 int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1212 const struct sw_flow_key *key, bool log)
1214 struct sw_flow_key *new_key;
1216 if (ovs_nla_get_ufid(sfid, ufid, log))
1219 /* If UFID was not provided, use unmasked key. */
1220 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1223 memcpy(new_key, key, sizeof(*key));
1224 sfid->unmasked_key = new_key;
1229 u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1231 return attr ? nla_get_u32(attr) : 0;
1235 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1236 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
1237 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1239 * @log: Boolean to allow kernel error logging. Normally true, but when
1240 * probing for feature compatibility this should be passed in as false to
1241 * suppress unnecessary error logging.
1243 * This parses a series of Netlink attributes that form a flow key, which must
1244 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1245 * get the metadata, that is, the parts of the flow key that cannot be
1246 * extracted from the packet itself.
1249 int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1250 struct sw_flow_key *key,
1253 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1254 struct sw_flow_match match;
1258 err = parse_flow_nlattrs(attr, a, &attrs, log);
1262 memset(&match, 0, sizeof(match));
1265 memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
1266 key->phy.in_port = DP_MAX_PORTS;
1268 return metadata_from_nlattrs(&match, &attrs, a, false, log);
1271 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1272 const struct sw_flow_key *output, bool is_mask,
1273 struct sk_buff *skb)
1275 struct ovs_key_ethernet *eth_key;
1276 struct nlattr *nla, *encap;
1278 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1279 goto nla_put_failure;
1281 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1282 goto nla_put_failure;
1284 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1285 goto nla_put_failure;
1287 if ((swkey->tun_key.ipv4_dst || is_mask)) {
1288 const void *opts = NULL;
1290 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1291 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
1293 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1294 swkey->tun_opts_len))
1295 goto nla_put_failure;
1298 if (swkey->phy.in_port == DP_MAX_PORTS) {
1299 if (is_mask && (output->phy.in_port == 0xffff))
1300 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1301 goto nla_put_failure;
1304 upper_u16 = !is_mask ? 0 : 0xffff;
1306 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1307 (upper_u16 << 16) | output->phy.in_port))
1308 goto nla_put_failure;
1311 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1312 goto nla_put_failure;
1314 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1316 goto nla_put_failure;
1318 eth_key = nla_data(nla);
1319 ether_addr_copy(eth_key->eth_src, output->eth.src);
1320 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1322 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1324 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1325 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1326 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1327 goto nla_put_failure;
1328 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1329 if (!swkey->eth.tci)
1334 if (swkey->eth.type == htons(ETH_P_802_2)) {
1336 * Ethertype 802.2 is represented in the netlink with omitted
1337 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1338 * 0xffff in the mask attribute. Ethertype can also
1341 if (is_mask && output->eth.type)
1342 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1344 goto nla_put_failure;
1348 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1349 goto nla_put_failure;
1351 if (swkey->eth.type == htons(ETH_P_IP)) {
1352 struct ovs_key_ipv4 *ipv4_key;
1354 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1356 goto nla_put_failure;
1357 ipv4_key = nla_data(nla);
1358 ipv4_key->ipv4_src = output->ipv4.addr.src;
1359 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1360 ipv4_key->ipv4_proto = output->ip.proto;
1361 ipv4_key->ipv4_tos = output->ip.tos;
1362 ipv4_key->ipv4_ttl = output->ip.ttl;
1363 ipv4_key->ipv4_frag = output->ip.frag;
1364 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1365 struct ovs_key_ipv6 *ipv6_key;
1367 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1369 goto nla_put_failure;
1370 ipv6_key = nla_data(nla);
1371 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1372 sizeof(ipv6_key->ipv6_src));
1373 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1374 sizeof(ipv6_key->ipv6_dst));
1375 ipv6_key->ipv6_label = output->ipv6.label;
1376 ipv6_key->ipv6_proto = output->ip.proto;
1377 ipv6_key->ipv6_tclass = output->ip.tos;
1378 ipv6_key->ipv6_hlimit = output->ip.ttl;
1379 ipv6_key->ipv6_frag = output->ip.frag;
1380 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1381 swkey->eth.type == htons(ETH_P_RARP)) {
1382 struct ovs_key_arp *arp_key;
1384 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1386 goto nla_put_failure;
1387 arp_key = nla_data(nla);
1388 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1389 arp_key->arp_sip = output->ipv4.addr.src;
1390 arp_key->arp_tip = output->ipv4.addr.dst;
1391 arp_key->arp_op = htons(output->ip.proto);
1392 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1393 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1394 } else if (eth_p_mpls(swkey->eth.type)) {
1395 struct ovs_key_mpls *mpls_key;
1397 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1399 goto nla_put_failure;
1400 mpls_key = nla_data(nla);
1401 mpls_key->mpls_lse = output->mpls.top_lse;
1404 if ((swkey->eth.type == htons(ETH_P_IP) ||
1405 swkey->eth.type == htons(ETH_P_IPV6)) &&
1406 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1408 if (swkey->ip.proto == IPPROTO_TCP) {
1409 struct ovs_key_tcp *tcp_key;
1411 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1413 goto nla_put_failure;
1414 tcp_key = nla_data(nla);
1415 tcp_key->tcp_src = output->tp.src;
1416 tcp_key->tcp_dst = output->tp.dst;
1417 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1419 goto nla_put_failure;
1420 } else if (swkey->ip.proto == IPPROTO_UDP) {
1421 struct ovs_key_udp *udp_key;
1423 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1425 goto nla_put_failure;
1426 udp_key = nla_data(nla);
1427 udp_key->udp_src = output->tp.src;
1428 udp_key->udp_dst = output->tp.dst;
1429 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1430 struct ovs_key_sctp *sctp_key;
1432 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1434 goto nla_put_failure;
1435 sctp_key = nla_data(nla);
1436 sctp_key->sctp_src = output->tp.src;
1437 sctp_key->sctp_dst = output->tp.dst;
1438 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1439 swkey->ip.proto == IPPROTO_ICMP) {
1440 struct ovs_key_icmp *icmp_key;
1442 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1444 goto nla_put_failure;
1445 icmp_key = nla_data(nla);
1446 icmp_key->icmp_type = ntohs(output->tp.src);
1447 icmp_key->icmp_code = ntohs(output->tp.dst);
1448 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1449 swkey->ip.proto == IPPROTO_ICMPV6) {
1450 struct ovs_key_icmpv6 *icmpv6_key;
1452 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1453 sizeof(*icmpv6_key));
1455 goto nla_put_failure;
1456 icmpv6_key = nla_data(nla);
1457 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1458 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1460 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1461 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1462 struct ovs_key_nd *nd_key;
1464 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1466 goto nla_put_failure;
1467 nd_key = nla_data(nla);
1468 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1469 sizeof(nd_key->nd_target));
1470 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1471 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1478 nla_nest_end(skb, encap);
1486 int ovs_nla_put_key(const struct sw_flow_key *swkey,
1487 const struct sw_flow_key *output, int attr, bool is_mask,
1488 struct sk_buff *skb)
1493 nla = nla_nest_start(skb, attr);
1496 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1499 nla_nest_end(skb, nla);
1504 /* Called with ovs_mutex or RCU read lock. */
1505 int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
1507 if (ovs_identifier_is_ufid(&flow->id))
1508 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1511 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1512 OVS_FLOW_ATTR_KEY, false, skb);
1515 /* Called with ovs_mutex or RCU read lock. */
1516 int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1518 return ovs_nla_put_key(&flow->key, &flow->key,
1519 OVS_FLOW_ATTR_KEY, false, skb);
1522 /* Called with ovs_mutex or RCU read lock. */
1523 int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1525 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1526 OVS_FLOW_ATTR_MASK, true, skb);
1529 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1531 static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
1533 struct sw_flow_actions *sfa;
1535 if (size > MAX_ACTIONS_BUFSIZE) {
1536 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
1537 return ERR_PTR(-EINVAL);
1540 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1542 return ERR_PTR(-ENOMEM);
1544 sfa->actions_len = 0;
1548 /* RCU callback used by ovs_nla_free_flow_actions. */
1549 static void rcu_free_acts_callback(struct rcu_head *rcu)
1551 struct sw_flow_actions *sf_acts = container_of(rcu,
1552 struct sw_flow_actions, rcu);
1556 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1557 * The caller must hold rcu_read_lock for this to be sensible.
1559 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1561 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1564 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1565 int attr_len, bool log)
1568 struct sw_flow_actions *acts;
1570 int req_size = NLA_ALIGN(attr_len);
1571 int next_offset = offsetof(struct sw_flow_actions, actions) +
1572 (*sfa)->actions_len;
1574 if (req_size <= (ksize(*sfa) - next_offset))
1577 new_acts_size = ksize(*sfa) * 2;
1579 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1580 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1581 return ERR_PTR(-EMSGSIZE);
1582 new_acts_size = MAX_ACTIONS_BUFSIZE;
1585 acts = nla_alloc_flow_actions(new_acts_size, log);
1587 return (void *)acts;
1589 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1590 acts->actions_len = (*sfa)->actions_len;
1595 (*sfa)->actions_len += req_size;
1596 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1599 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1600 int attrtype, void *data, int len, bool log)
1604 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
1608 a->nla_type = attrtype;
1609 a->nla_len = nla_attr_size(len);
1612 memcpy(nla_data(a), data, len);
1613 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1618 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1619 void *data, int len, bool log)
1623 a = __add_action(sfa, attrtype, data, len, log);
1630 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1631 int attrtype, bool log)
1633 int used = (*sfa)->actions_len;
1636 err = add_action(sfa, attrtype, NULL, 0, log);
1643 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1646 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1649 a->nla_len = sfa->actions_len - st_offset;
1652 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1653 const struct sw_flow_key *key,
1654 int depth, struct sw_flow_actions **sfa,
1655 __be16 eth_type, __be16 vlan_tci, bool log);
1657 static int validate_and_copy_sample(const struct nlattr *attr,
1658 const struct sw_flow_key *key, int depth,
1659 struct sw_flow_actions **sfa,
1660 __be16 eth_type, __be16 vlan_tci, bool log)
1662 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1663 const struct nlattr *probability, *actions;
1664 const struct nlattr *a;
1665 int rem, start, err, st_acts;
1667 memset(attrs, 0, sizeof(attrs));
1668 nla_for_each_nested(a, attr, rem) {
1669 int type = nla_type(a);
1670 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1677 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1678 if (!probability || nla_len(probability) != sizeof(u32))
1681 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1682 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1685 /* validation done, copy sample action. */
1686 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
1689 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1690 nla_data(probability), sizeof(u32), log);
1693 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
1697 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
1698 eth_type, vlan_tci, log);
1702 add_nested_action_end(*sfa, st_acts);
1703 add_nested_action_end(*sfa, start);
1708 static int validate_tp_port(const struct sw_flow_key *flow_key,
1711 if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) &&
1712 (flow_key->tp.src || flow_key->tp.dst))
1718 void ovs_match_init(struct sw_flow_match *match,
1719 struct sw_flow_key *key,
1720 struct sw_flow_mask *mask)
1722 memset(match, 0, sizeof(*match));
1726 memset(key, 0, sizeof(*key));
1729 memset(&mask->key, 0, sizeof(mask->key));
1730 mask->range.start = mask->range.end = 0;
1734 static int validate_geneve_opts(struct sw_flow_key *key)
1736 struct geneve_opt *option;
1737 int opts_len = key->tun_opts_len;
1738 bool crit_opt = false;
1740 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1741 while (opts_len > 0) {
1744 if (opts_len < sizeof(*option))
1747 len = sizeof(*option) + option->length * 4;
1751 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1753 option = (struct geneve_opt *)((u8 *)option + len);
1757 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1762 static int validate_and_copy_set_tun(const struct nlattr *attr,
1763 struct sw_flow_actions **sfa, bool log)
1765 struct sw_flow_match match;
1766 struct sw_flow_key key;
1767 struct ovs_tunnel_info *tun_info;
1769 int start, opts_type;
1772 ovs_match_init(&match, &key, NULL);
1773 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1777 if (key.tun_opts_len) {
1778 switch (opts_type) {
1779 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1780 err = validate_geneve_opts(&key);
1784 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1789 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
1793 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1794 sizeof(*tun_info) + key.tun_opts_len, log);
1798 tun_info = nla_data(a);
1799 tun_info->tunnel = key.tun_key;
1800 tun_info->options_len = key.tun_opts_len;
1802 if (tun_info->options_len) {
1803 /* We need to store the options in the action itself since
1804 * everything else will go away after flow setup. We can append
1805 * it to tun_info and then point there.
1807 memcpy((tun_info + 1),
1808 TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
1809 tun_info->options = (tun_info + 1);
1812 tun_info->options = NULL;
1815 add_nested_action_end(*sfa, start);
1820 static int validate_set(const struct nlattr *a,
1821 const struct sw_flow_key *flow_key,
1822 struct sw_flow_actions **sfa,
1823 bool *set_tun, __be16 eth_type, bool log)
1825 const struct nlattr *ovs_key = nla_data(a);
1826 int key_type = nla_type(ovs_key);
1828 /* There can be only one key in a action */
1829 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1832 if (key_type > OVS_KEY_ATTR_MAX ||
1833 (ovs_key_lens[key_type].len != nla_len(ovs_key) &&
1834 ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
1838 const struct ovs_key_ipv4 *ipv4_key;
1839 const struct ovs_key_ipv6 *ipv6_key;
1842 case OVS_KEY_ATTR_PRIORITY:
1843 case OVS_KEY_ATTR_SKB_MARK:
1844 case OVS_KEY_ATTR_ETHERNET:
1847 case OVS_KEY_ATTR_TUNNEL:
1848 if (eth_p_mpls(eth_type))
1852 err = validate_and_copy_set_tun(a, sfa, log);
1857 case OVS_KEY_ATTR_IPV4:
1858 if (eth_type != htons(ETH_P_IP))
1861 if (!flow_key->ip.proto)
1864 ipv4_key = nla_data(ovs_key);
1865 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1868 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1873 case OVS_KEY_ATTR_IPV6:
1874 if (eth_type != htons(ETH_P_IPV6))
1877 if (!flow_key->ip.proto)
1880 ipv6_key = nla_data(ovs_key);
1881 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1884 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1887 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1892 case OVS_KEY_ATTR_TCP:
1893 if (flow_key->ip.proto != IPPROTO_TCP)
1896 return validate_tp_port(flow_key, eth_type);
1898 case OVS_KEY_ATTR_UDP:
1899 if (flow_key->ip.proto != IPPROTO_UDP)
1902 return validate_tp_port(flow_key, eth_type);
1904 case OVS_KEY_ATTR_MPLS:
1905 if (!eth_p_mpls(eth_type))
1909 case OVS_KEY_ATTR_SCTP:
1910 if (flow_key->ip.proto != IPPROTO_SCTP)
1913 return validate_tp_port(flow_key, eth_type);
1922 static int validate_userspace(const struct nlattr *attr)
1924 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1925 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1926 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1927 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
1929 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1932 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1933 attr, userspace_policy);
1937 if (!a[OVS_USERSPACE_ATTR_PID] ||
1938 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1944 static int copy_action(const struct nlattr *from,
1945 struct sw_flow_actions **sfa, bool log)
1947 int totlen = NLA_ALIGN(from->nla_len);
1950 to = reserve_sfa_size(sfa, from->nla_len, log);
1954 memcpy(to, from, totlen);
1958 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1959 const struct sw_flow_key *key,
1960 int depth, struct sw_flow_actions **sfa,
1961 __be16 eth_type, __be16 vlan_tci, bool log)
1963 const struct nlattr *a;
1966 if (depth >= SAMPLE_ACTION_DEPTH)
1969 nla_for_each_nested(a, attr, rem) {
1970 /* Expected argument lengths, (u32)-1 for variable length. */
1971 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1972 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1973 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
1974 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1975 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
1976 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
1977 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1978 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1979 [OVS_ACTION_ATTR_SET] = (u32)-1,
1980 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
1981 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
1983 const struct ovs_action_push_vlan *vlan;
1984 int type = nla_type(a);
1987 if (type > OVS_ACTION_ATTR_MAX ||
1988 (action_lens[type] != nla_len(a) &&
1989 action_lens[type] != (u32)-1))
1994 case OVS_ACTION_ATTR_UNSPEC:
1997 case OVS_ACTION_ATTR_USERSPACE:
1998 err = validate_userspace(a);
2003 case OVS_ACTION_ATTR_OUTPUT:
2004 if (nla_get_u32(a) >= DP_MAX_PORTS)
2009 case OVS_ACTION_ATTR_HASH: {
2010 const struct ovs_action_hash *act_hash = nla_data(a);
2012 switch (act_hash->hash_alg) {
2013 case OVS_HASH_ALG_L4:
2022 case OVS_ACTION_ATTR_POP_VLAN:
2023 vlan_tci = htons(0);
2026 case OVS_ACTION_ATTR_PUSH_VLAN:
2028 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2030 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2032 vlan_tci = vlan->vlan_tci;
2035 case OVS_ACTION_ATTR_RECIRC:
2038 case OVS_ACTION_ATTR_PUSH_MPLS: {
2039 const struct ovs_action_push_mpls *mpls = nla_data(a);
2041 if (!eth_p_mpls(mpls->mpls_ethertype))
2044 /* Prohibit push MPLS other than to a white list
2045 * for packets that have a known tag order.
2047 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2048 (eth_type != htons(ETH_P_IP) &&
2049 eth_type != htons(ETH_P_IPV6) &&
2050 eth_type != htons(ETH_P_ARP) &&
2051 eth_type != htons(ETH_P_RARP) &&
2052 !eth_p_mpls(eth_type)))
2054 eth_type = mpls->mpls_ethertype;
2058 case OVS_ACTION_ATTR_POP_MPLS:
2059 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2060 !eth_p_mpls(eth_type))
2063 /* Disallow subsequent L2.5+ set and mpls_pop actions
2064 * as there is no check here to ensure that the new
2065 * eth_type is valid and thus set actions could
2066 * write off the end of the packet or otherwise
2069 * Support for these actions is planned using packet
2072 eth_type = htons(0);
2075 case OVS_ACTION_ATTR_SET:
2076 err = validate_set(a, key, sfa,
2077 &skip_copy, eth_type, log);
2082 case OVS_ACTION_ATTR_SAMPLE:
2083 err = validate_and_copy_sample(a, key, depth, sfa,
2084 eth_type, vlan_tci, log);
2091 OVS_NLERR(log, "Unknown Action type %d", type);
2095 err = copy_action(a, sfa, log);
2107 int ovs_nla_copy_actions(const struct nlattr *attr,
2108 const struct sw_flow_key *key,
2109 struct sw_flow_actions **sfa, bool log)
2113 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
2115 return PTR_ERR(*sfa);
2117 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
2125 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2127 const struct nlattr *a;
2128 struct nlattr *start;
2131 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2135 nla_for_each_nested(a, attr, rem) {
2136 int type = nla_type(a);
2137 struct nlattr *st_sample;
2140 case OVS_SAMPLE_ATTR_PROBABILITY:
2141 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2142 sizeof(u32), nla_data(a)))
2145 case OVS_SAMPLE_ATTR_ACTIONS:
2146 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2149 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2152 nla_nest_end(skb, st_sample);
2157 nla_nest_end(skb, start);
2161 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2163 const struct nlattr *ovs_key = nla_data(a);
2164 int key_type = nla_type(ovs_key);
2165 struct nlattr *start;
2169 case OVS_KEY_ATTR_TUNNEL_INFO: {
2170 struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
2172 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2176 err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
2177 tun_info->options_len ?
2178 tun_info->options : NULL,
2179 tun_info->options_len);
2182 nla_nest_end(skb, start);
2186 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2194 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2196 const struct nlattr *a;
2199 nla_for_each_attr(a, attr, len, rem) {
2200 int type = nla_type(a);
2203 case OVS_ACTION_ATTR_SET:
2204 err = set_action_to_attr(a, skb);
2209 case OVS_ACTION_ATTR_SAMPLE:
2210 err = sample_action_to_attr(a, skb);
2215 if (nla_put(skb, type, nla_len(a), nla_data(a)))