2 * Copyright (c) 2007-2015 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>
48 #include <net/vxlan.h>
52 #include "flow_netlink.h"
57 const struct ovs_len_tbl *next;
60 #define OVS_ATTR_NESTED -1
61 #define OVS_ATTR_VARIABLE -2
63 static void update_range(struct sw_flow_match *match,
64 size_t offset, size_t size, bool is_mask)
66 struct sw_flow_key_range *range;
67 size_t start = rounddown(offset, sizeof(long));
68 size_t end = roundup(offset + size, sizeof(long));
71 range = &match->range;
73 range = &match->mask->range;
75 if (range->start == range->end) {
81 if (range->start > start)
88 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
90 update_range(match, offsetof(struct sw_flow_key, field), \
91 sizeof((match)->key->field), is_mask); \
93 (match)->mask->key.field = value; \
95 (match)->key->field = value; \
98 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
100 update_range(match, offset, len, is_mask); \
102 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
104 memcpy((u8 *)(match)->key + offset, value_p, len); \
107 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
108 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
109 value_p, len, is_mask)
111 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
113 update_range(match, offsetof(struct sw_flow_key, field), \
114 sizeof((match)->key->field), is_mask); \
116 memset((u8 *)&(match)->mask->key.field, value, \
117 sizeof((match)->mask->key.field)); \
119 memset((u8 *)&(match)->key->field, value, \
120 sizeof((match)->key->field)); \
123 static bool match_validate(const struct sw_flow_match *match,
124 u64 key_attrs, u64 mask_attrs, bool log)
126 u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
127 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
129 /* The following mask attributes allowed only if they
130 * pass the validation tests.
132 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
133 | (1ULL << OVS_KEY_ATTR_IPV6)
134 | (1ULL << OVS_KEY_ATTR_TCP)
135 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
136 | (1ULL << OVS_KEY_ATTR_UDP)
137 | (1ULL << OVS_KEY_ATTR_SCTP)
138 | (1ULL << OVS_KEY_ATTR_ICMP)
139 | (1ULL << OVS_KEY_ATTR_ICMPV6)
140 | (1ULL << OVS_KEY_ATTR_ARP)
141 | (1ULL << OVS_KEY_ATTR_ND)
142 | (1ULL << OVS_KEY_ATTR_MPLS));
144 /* Always allowed mask fields. */
145 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
146 | (1ULL << OVS_KEY_ATTR_IN_PORT)
147 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
149 /* Check key attributes. */
150 if (match->key->eth.type == htons(ETH_P_ARP)
151 || match->key->eth.type == htons(ETH_P_RARP)) {
152 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
153 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
154 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
157 if (eth_p_mpls(match->key->eth.type)) {
158 key_expected |= 1ULL << OVS_KEY_ATTR_MPLS;
159 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
160 mask_allowed |= 1ULL << OVS_KEY_ATTR_MPLS;
163 if (match->key->eth.type == htons(ETH_P_IP)) {
164 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
165 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
166 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
168 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
169 if (match->key->ip.proto == IPPROTO_UDP) {
170 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
171 if (match->mask && (match->mask->key.ip.proto == 0xff))
172 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
175 if (match->key->ip.proto == IPPROTO_SCTP) {
176 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
177 if (match->mask && (match->mask->key.ip.proto == 0xff))
178 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
181 if (match->key->ip.proto == IPPROTO_TCP) {
182 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
183 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
184 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
185 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
186 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
190 if (match->key->ip.proto == IPPROTO_ICMP) {
191 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
192 if (match->mask && (match->mask->key.ip.proto == 0xff))
193 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
198 if (match->key->eth.type == htons(ETH_P_IPV6)) {
199 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
200 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
201 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
203 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
204 if (match->key->ip.proto == IPPROTO_UDP) {
205 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
206 if (match->mask && (match->mask->key.ip.proto == 0xff))
207 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
210 if (match->key->ip.proto == IPPROTO_SCTP) {
211 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
212 if (match->mask && (match->mask->key.ip.proto == 0xff))
213 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
216 if (match->key->ip.proto == IPPROTO_TCP) {
217 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
218 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
219 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
220 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
221 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
225 if (match->key->ip.proto == IPPROTO_ICMPV6) {
226 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
227 if (match->mask && (match->mask->key.ip.proto == 0xff))
228 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
230 if (match->key->tp.src ==
231 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
232 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
233 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
234 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
235 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
241 if ((key_attrs & key_expected) != key_expected) {
242 /* Key attributes check failed. */
243 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
244 (unsigned long long)key_attrs,
245 (unsigned long long)key_expected);
249 if ((mask_attrs & mask_allowed) != mask_attrs) {
250 /* Mask attributes check failed. */
251 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
252 (unsigned long long)mask_attrs,
253 (unsigned long long)mask_allowed);
260 size_t ovs_tun_key_attr_size(void)
262 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
263 * updating this function.
265 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
266 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
267 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
268 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
269 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
270 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
271 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
272 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
273 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
274 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
275 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
277 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
278 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
281 size_t ovs_key_attr_size(void)
283 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
284 * updating this function.
286 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 26);
288 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
289 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
290 + ovs_tun_key_attr_size()
291 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
292 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
293 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
294 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
295 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
296 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
297 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
298 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
299 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
300 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
301 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
302 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
305 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
306 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
309 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
310 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
311 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
312 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
313 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
314 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
315 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
316 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
317 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
318 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
319 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
320 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
321 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
322 .next = ovs_vxlan_ext_key_lens },
325 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
326 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
327 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
328 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
329 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
330 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
331 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
332 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
333 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
334 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
335 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
336 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
337 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
338 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
339 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
340 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
341 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
342 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
343 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
344 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
345 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
346 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
347 .next = ovs_tunnel_key_lens, },
348 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
351 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
353 return expected_len == attr_len ||
354 expected_len == OVS_ATTR_NESTED ||
355 expected_len == OVS_ATTR_VARIABLE;
358 static bool is_all_zero(const u8 *fp, size_t size)
365 for (i = 0; i < size; i++)
372 static int __parse_flow_nlattrs(const struct nlattr *attr,
373 const struct nlattr *a[],
374 u64 *attrsp, bool log, bool nz)
376 const struct nlattr *nla;
381 nla_for_each_nested(nla, attr, rem) {
382 u16 type = nla_type(nla);
385 if (type > OVS_KEY_ATTR_MAX) {
386 OVS_NLERR(log, "Key type %d is out of range max %d",
387 type, OVS_KEY_ATTR_MAX);
391 if (attrs & (1ULL << type)) {
392 OVS_NLERR(log, "Duplicate key (type %d).", type);
396 expected_len = ovs_key_lens[type].len;
397 if (!check_attr_len(nla_len(nla), expected_len)) {
398 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
399 type, nla_len(nla), expected_len);
403 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
404 attrs |= 1ULL << type;
409 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
417 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
418 const struct nlattr *a[], u64 *attrsp,
421 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
424 static int parse_flow_nlattrs(const struct nlattr *attr,
425 const struct nlattr *a[], u64 *attrsp,
428 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
431 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
432 struct sw_flow_match *match, bool is_mask,
435 unsigned long opt_key_offset;
437 if (nla_len(a) > sizeof(match->key->tun_opts)) {
438 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
439 nla_len(a), sizeof(match->key->tun_opts));
443 if (nla_len(a) % 4 != 0) {
444 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
449 /* We need to record the length of the options passed
450 * down, otherwise packets with the same format but
451 * additional options will be silently matched.
454 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
457 /* This is somewhat unusual because it looks at
458 * both the key and mask while parsing the
459 * attributes (and by extension assumes the key
460 * is parsed first). Normally, we would verify
461 * that each is the correct length and that the
462 * attributes line up in the validate function.
463 * However, that is difficult because this is
464 * variable length and we won't have the
467 if (match->key->tun_opts_len != nla_len(a)) {
468 OVS_NLERR(log, "Geneve option len %d != mask len %d",
469 match->key->tun_opts_len, nla_len(a));
473 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
476 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
477 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
478 nla_len(a), is_mask);
482 static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
483 struct sw_flow_match *match, bool is_mask,
488 unsigned long opt_key_offset;
489 struct vxlan_metadata opts;
491 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
493 memset(&opts, 0, sizeof(opts));
494 nla_for_each_nested(a, attr, rem) {
495 int type = nla_type(a);
497 if (type > OVS_VXLAN_EXT_MAX) {
498 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
499 type, OVS_VXLAN_EXT_MAX);
503 if (!check_attr_len(nla_len(a),
504 ovs_vxlan_ext_key_lens[type].len)) {
505 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
507 ovs_vxlan_ext_key_lens[type].len);
512 case OVS_VXLAN_EXT_GBP:
513 opts.gbp = nla_get_u32(a);
516 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
522 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
528 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
530 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
532 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
533 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
538 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
539 struct sw_flow_match *match, bool is_mask,
545 __be16 tun_flags = 0;
548 nla_for_each_nested(a, attr, rem) {
549 int type = nla_type(a);
552 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
553 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
554 type, OVS_TUNNEL_KEY_ATTR_MAX);
558 if (!check_attr_len(nla_len(a),
559 ovs_tunnel_key_lens[type].len)) {
560 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
561 type, nla_len(a), ovs_tunnel_key_lens[type].len);
566 case OVS_TUNNEL_KEY_ATTR_ID:
567 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
568 nla_get_be64(a), is_mask);
569 tun_flags |= TUNNEL_KEY;
571 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
572 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
573 nla_get_in_addr(a), is_mask);
575 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
576 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
577 nla_get_in_addr(a), is_mask);
579 case OVS_TUNNEL_KEY_ATTR_TOS:
580 SW_FLOW_KEY_PUT(match, tun_key.tos,
581 nla_get_u8(a), is_mask);
583 case OVS_TUNNEL_KEY_ATTR_TTL:
584 SW_FLOW_KEY_PUT(match, tun_key.ttl,
585 nla_get_u8(a), is_mask);
588 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
589 tun_flags |= TUNNEL_DONT_FRAGMENT;
591 case OVS_TUNNEL_KEY_ATTR_CSUM:
592 tun_flags |= TUNNEL_CSUM;
594 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
595 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
596 nla_get_be16(a), is_mask);
598 case OVS_TUNNEL_KEY_ATTR_TP_DST:
599 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
600 nla_get_be16(a), is_mask);
602 case OVS_TUNNEL_KEY_ATTR_OAM:
603 tun_flags |= TUNNEL_OAM;
605 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
607 OVS_NLERR(log, "Multiple metadata blocks provided");
611 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
615 tun_flags |= TUNNEL_GENEVE_OPT;
618 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
620 OVS_NLERR(log, "Multiple metadata blocks provided");
624 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
628 tun_flags |= TUNNEL_VXLAN_OPT;
632 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
638 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
641 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
647 if (!match->key->tun_key.u.ipv4.dst) {
648 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
653 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
661 static int vxlan_opt_to_nlattr(struct sk_buff *skb,
662 const void *tun_opts, int swkey_tun_opts_len)
664 const struct vxlan_metadata *opts = tun_opts;
667 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
671 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
674 nla_nest_end(skb, nla);
678 static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
679 const struct ip_tunnel_key *output,
680 const void *tun_opts, int swkey_tun_opts_len)
682 if (output->tun_flags & TUNNEL_KEY &&
683 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
685 if (output->u.ipv4.src &&
686 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
689 if (output->u.ipv4.dst &&
690 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
694 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
696 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
698 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
699 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
701 if ((output->tun_flags & TUNNEL_CSUM) &&
702 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
704 if (output->tp_src &&
705 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
707 if (output->tp_dst &&
708 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
710 if ((output->tun_flags & TUNNEL_OAM) &&
711 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
714 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
715 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
716 swkey_tun_opts_len, tun_opts))
718 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
719 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
726 static int ipv4_tun_to_nlattr(struct sk_buff *skb,
727 const struct ip_tunnel_key *output,
728 const void *tun_opts, int swkey_tun_opts_len)
733 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
737 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
741 nla_nest_end(skb, nla);
745 int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
746 const struct ip_tunnel_info *egress_tun_info,
747 const void *egress_tun_opts)
749 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
751 egress_tun_info->options_len);
754 static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
755 const struct nlattr **a, bool is_mask,
758 if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
759 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
761 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
762 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
765 if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
766 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
768 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
769 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
772 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
773 SW_FLOW_KEY_PUT(match, phy.priority,
774 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
775 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
778 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
779 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
782 in_port = 0xffffffff; /* Always exact match in_port. */
783 } else if (in_port >= DP_MAX_PORTS) {
784 OVS_NLERR(log, "Port %d exceeds max allowable %d",
785 in_port, DP_MAX_PORTS);
789 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
790 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
791 } else if (!is_mask) {
792 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
795 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
796 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
798 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
799 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
801 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
802 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
805 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
810 static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
811 const struct nlattr **a, bool is_mask,
816 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
820 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
821 const struct ovs_key_ethernet *eth_key;
823 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
824 SW_FLOW_KEY_MEMCPY(match, eth.src,
825 eth_key->eth_src, ETH_ALEN, is_mask);
826 SW_FLOW_KEY_MEMCPY(match, eth.dst,
827 eth_key->eth_dst, ETH_ALEN, is_mask);
828 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
831 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
834 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
835 if (!(tci & htons(VLAN_TAG_PRESENT))) {
837 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
839 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
844 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
845 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
848 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
851 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
853 /* Always exact match EtherType. */
854 eth_type = htons(0xffff);
855 } else if (!eth_proto_is_802_3(eth_type)) {
856 OVS_NLERR(log, "EtherType %x is less than min %x",
857 ntohs(eth_type), ETH_P_802_3_MIN);
861 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
862 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
863 } else if (!is_mask) {
864 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
867 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
868 const struct ovs_key_ipv4 *ipv4_key;
870 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
871 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
872 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
873 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
876 SW_FLOW_KEY_PUT(match, ip.proto,
877 ipv4_key->ipv4_proto, is_mask);
878 SW_FLOW_KEY_PUT(match, ip.tos,
879 ipv4_key->ipv4_tos, is_mask);
880 SW_FLOW_KEY_PUT(match, ip.ttl,
881 ipv4_key->ipv4_ttl, is_mask);
882 SW_FLOW_KEY_PUT(match, ip.frag,
883 ipv4_key->ipv4_frag, is_mask);
884 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
885 ipv4_key->ipv4_src, is_mask);
886 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
887 ipv4_key->ipv4_dst, is_mask);
888 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
891 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
892 const struct ovs_key_ipv6 *ipv6_key;
894 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
895 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
896 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
897 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
901 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
903 "Invalid IPv6 flow label value (value=%x, max=%x).",
904 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
908 SW_FLOW_KEY_PUT(match, ipv6.label,
909 ipv6_key->ipv6_label, is_mask);
910 SW_FLOW_KEY_PUT(match, ip.proto,
911 ipv6_key->ipv6_proto, is_mask);
912 SW_FLOW_KEY_PUT(match, ip.tos,
913 ipv6_key->ipv6_tclass, is_mask);
914 SW_FLOW_KEY_PUT(match, ip.ttl,
915 ipv6_key->ipv6_hlimit, is_mask);
916 SW_FLOW_KEY_PUT(match, ip.frag,
917 ipv6_key->ipv6_frag, is_mask);
918 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
920 sizeof(match->key->ipv6.addr.src),
922 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
924 sizeof(match->key->ipv6.addr.dst),
927 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
930 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
931 const struct ovs_key_arp *arp_key;
933 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
934 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
935 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
940 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
941 arp_key->arp_sip, is_mask);
942 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
943 arp_key->arp_tip, is_mask);
944 SW_FLOW_KEY_PUT(match, ip.proto,
945 ntohs(arp_key->arp_op), is_mask);
946 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
947 arp_key->arp_sha, ETH_ALEN, is_mask);
948 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
949 arp_key->arp_tha, ETH_ALEN, is_mask);
951 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
954 if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) {
955 const struct ovs_key_mpls *mpls_key;
957 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
958 SW_FLOW_KEY_PUT(match, mpls.top_lse,
959 mpls_key->mpls_lse, is_mask);
961 attrs &= ~(1ULL << OVS_KEY_ATTR_MPLS);
964 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
965 const struct ovs_key_tcp *tcp_key;
967 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
968 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
969 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
970 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
973 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
974 SW_FLOW_KEY_PUT(match, tp.flags,
975 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
977 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
980 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
981 const struct ovs_key_udp *udp_key;
983 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
984 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
985 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
986 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
989 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
990 const struct ovs_key_sctp *sctp_key;
992 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
993 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
994 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
995 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
998 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
999 const struct ovs_key_icmp *icmp_key;
1001 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1002 SW_FLOW_KEY_PUT(match, tp.src,
1003 htons(icmp_key->icmp_type), is_mask);
1004 SW_FLOW_KEY_PUT(match, tp.dst,
1005 htons(icmp_key->icmp_code), is_mask);
1006 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1009 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1010 const struct ovs_key_icmpv6 *icmpv6_key;
1012 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1013 SW_FLOW_KEY_PUT(match, tp.src,
1014 htons(icmpv6_key->icmpv6_type), is_mask);
1015 SW_FLOW_KEY_PUT(match, tp.dst,
1016 htons(icmpv6_key->icmpv6_code), is_mask);
1017 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1020 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1021 const struct ovs_key_nd *nd_key;
1023 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1024 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1026 sizeof(match->key->ipv6.nd.target),
1028 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1029 nd_key->nd_sll, ETH_ALEN, is_mask);
1030 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1031 nd_key->nd_tll, ETH_ALEN, is_mask);
1032 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1036 OVS_NLERR(log, "Unknown key attributes %llx",
1037 (unsigned long long)attrs);
1044 static void nlattr_set(struct nlattr *attr, u8 val,
1045 const struct ovs_len_tbl *tbl)
1050 /* The nlattr stream should already have been validated */
1051 nla_for_each_nested(nla, attr, rem) {
1052 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1053 if (tbl[nla_type(nla)].next)
1054 tbl = tbl[nla_type(nla)].next;
1055 nlattr_set(nla, val, tbl);
1057 memset(nla_data(nla), val, nla_len(nla));
1062 static void mask_set_nlattr(struct nlattr *attr, u8 val)
1064 nlattr_set(attr, val, ovs_key_lens);
1068 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1069 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1070 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1071 * does not include any don't care bit.
1072 * @match: receives the extracted flow match information.
1073 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1074 * sequence. The fields should of the packet that triggered the creation
1076 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1077 * attribute specifies the mask field of the wildcarded flow.
1078 * @log: Boolean to allow kernel error logging. Normally true, but when
1079 * probing for feature compatibility this should be passed in as false to
1080 * suppress unnecessary error logging.
1082 int ovs_nla_get_match(struct sw_flow_match *match,
1083 const struct nlattr *nla_key,
1084 const struct nlattr *nla_mask,
1087 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1088 const struct nlattr *encap;
1089 struct nlattr *newmask = NULL;
1092 bool encap_valid = false;
1095 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
1099 if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1100 (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) &&
1101 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1104 if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) &&
1105 (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) {
1106 OVS_NLERR(log, "Invalid Vlan frame.");
1110 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1111 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1112 encap = a[OVS_KEY_ATTR_ENCAP];
1113 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1116 if (tci & htons(VLAN_TAG_PRESENT)) {
1117 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
1121 /* Corner case for truncated 802.1Q header. */
1122 if (nla_len(encap)) {
1123 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
1127 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
1132 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
1138 /* Create an exact match mask. We need to set to 0xff
1139 * all the 'match->mask' fields that have been touched
1140 * in 'match->key'. We cannot simply memset
1141 * 'match->mask', because padding bytes and fields not
1142 * specified in 'match->key' should be left to 0.
1143 * Instead, we use a stream of netlink attributes,
1144 * copied from 'key' and set to 0xff.
1145 * ovs_key_from_nlattrs() will take care of filling
1146 * 'match->mask' appropriately.
1148 newmask = kmemdup(nla_key,
1149 nla_total_size(nla_len(nla_key)),
1154 mask_set_nlattr(newmask, 0xff);
1156 /* The userspace does not send tunnel attributes that
1157 * are 0, but we should not wildcard them nonetheless.
1159 if (match->key->tun_key.u.ipv4.dst)
1160 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1166 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1170 /* Always match on tci. */
1171 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1173 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1174 __be16 eth_type = 0;
1178 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
1183 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1184 if (a[OVS_KEY_ATTR_ETHERTYPE])
1185 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1187 if (eth_type == htons(0xffff)) {
1188 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1189 encap = a[OVS_KEY_ATTR_ENCAP];
1190 err = parse_flow_mask_nlattrs(encap, a,
1195 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1201 if (a[OVS_KEY_ATTR_VLAN])
1202 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1204 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1205 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1212 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
1217 if (!match_validate(match, key_attrs, mask_attrs, log))
1225 static size_t get_ufid_len(const struct nlattr *attr, bool log)
1232 len = nla_len(attr);
1233 if (len < 1 || len > MAX_UFID_LENGTH) {
1234 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1235 nla_len(attr), MAX_UFID_LENGTH);
1242 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1243 * or false otherwise.
1245 bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1248 sfid->ufid_len = get_ufid_len(attr, log);
1250 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1252 return sfid->ufid_len;
1255 int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1256 const struct sw_flow_key *key, bool log)
1258 struct sw_flow_key *new_key;
1260 if (ovs_nla_get_ufid(sfid, ufid, log))
1263 /* If UFID was not provided, use unmasked key. */
1264 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1267 memcpy(new_key, key, sizeof(*key));
1268 sfid->unmasked_key = new_key;
1273 u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1275 return attr ? nla_get_u32(attr) : 0;
1279 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1280 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
1281 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1283 * @log: Boolean to allow kernel error logging. Normally true, but when
1284 * probing for feature compatibility this should be passed in as false to
1285 * suppress unnecessary error logging.
1287 * This parses a series of Netlink attributes that form a flow key, which must
1288 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1289 * get the metadata, that is, the parts of the flow key that cannot be
1290 * extracted from the packet itself.
1293 int ovs_nla_get_flow_metadata(const struct nlattr *attr,
1294 struct sw_flow_key *key,
1297 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1298 struct sw_flow_match match;
1302 err = parse_flow_nlattrs(attr, a, &attrs, log);
1306 memset(&match, 0, sizeof(match));
1309 memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
1310 key->phy.in_port = DP_MAX_PORTS;
1312 return metadata_from_nlattrs(&match, &attrs, a, false, log);
1315 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1316 const struct sw_flow_key *output, bool is_mask,
1317 struct sk_buff *skb)
1319 struct ovs_key_ethernet *eth_key;
1320 struct nlattr *nla, *encap;
1322 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1323 goto nla_put_failure;
1325 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1326 goto nla_put_failure;
1328 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1329 goto nla_put_failure;
1331 if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
1332 const void *opts = NULL;
1334 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1335 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
1337 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1338 swkey->tun_opts_len))
1339 goto nla_put_failure;
1342 if (swkey->phy.in_port == DP_MAX_PORTS) {
1343 if (is_mask && (output->phy.in_port == 0xffff))
1344 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1345 goto nla_put_failure;
1348 upper_u16 = !is_mask ? 0 : 0xffff;
1350 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1351 (upper_u16 << 16) | output->phy.in_port))
1352 goto nla_put_failure;
1355 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1356 goto nla_put_failure;
1358 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1360 goto nla_put_failure;
1362 eth_key = nla_data(nla);
1363 ether_addr_copy(eth_key->eth_src, output->eth.src);
1364 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1366 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1368 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1369 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1370 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1371 goto nla_put_failure;
1372 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1373 if (!swkey->eth.tci)
1378 if (swkey->eth.type == htons(ETH_P_802_2)) {
1380 * Ethertype 802.2 is represented in the netlink with omitted
1381 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1382 * 0xffff in the mask attribute. Ethertype can also
1385 if (is_mask && output->eth.type)
1386 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1388 goto nla_put_failure;
1392 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1393 goto nla_put_failure;
1395 if (swkey->eth.type == htons(ETH_P_IP)) {
1396 struct ovs_key_ipv4 *ipv4_key;
1398 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1400 goto nla_put_failure;
1401 ipv4_key = nla_data(nla);
1402 ipv4_key->ipv4_src = output->ipv4.addr.src;
1403 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1404 ipv4_key->ipv4_proto = output->ip.proto;
1405 ipv4_key->ipv4_tos = output->ip.tos;
1406 ipv4_key->ipv4_ttl = output->ip.ttl;
1407 ipv4_key->ipv4_frag = output->ip.frag;
1408 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1409 struct ovs_key_ipv6 *ipv6_key;
1411 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1413 goto nla_put_failure;
1414 ipv6_key = nla_data(nla);
1415 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1416 sizeof(ipv6_key->ipv6_src));
1417 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1418 sizeof(ipv6_key->ipv6_dst));
1419 ipv6_key->ipv6_label = output->ipv6.label;
1420 ipv6_key->ipv6_proto = output->ip.proto;
1421 ipv6_key->ipv6_tclass = output->ip.tos;
1422 ipv6_key->ipv6_hlimit = output->ip.ttl;
1423 ipv6_key->ipv6_frag = output->ip.frag;
1424 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1425 swkey->eth.type == htons(ETH_P_RARP)) {
1426 struct ovs_key_arp *arp_key;
1428 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1430 goto nla_put_failure;
1431 arp_key = nla_data(nla);
1432 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1433 arp_key->arp_sip = output->ipv4.addr.src;
1434 arp_key->arp_tip = output->ipv4.addr.dst;
1435 arp_key->arp_op = htons(output->ip.proto);
1436 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1437 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1438 } else if (eth_p_mpls(swkey->eth.type)) {
1439 struct ovs_key_mpls *mpls_key;
1441 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1443 goto nla_put_failure;
1444 mpls_key = nla_data(nla);
1445 mpls_key->mpls_lse = output->mpls.top_lse;
1448 if ((swkey->eth.type == htons(ETH_P_IP) ||
1449 swkey->eth.type == htons(ETH_P_IPV6)) &&
1450 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1452 if (swkey->ip.proto == IPPROTO_TCP) {
1453 struct ovs_key_tcp *tcp_key;
1455 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1457 goto nla_put_failure;
1458 tcp_key = nla_data(nla);
1459 tcp_key->tcp_src = output->tp.src;
1460 tcp_key->tcp_dst = output->tp.dst;
1461 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1463 goto nla_put_failure;
1464 } else if (swkey->ip.proto == IPPROTO_UDP) {
1465 struct ovs_key_udp *udp_key;
1467 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1469 goto nla_put_failure;
1470 udp_key = nla_data(nla);
1471 udp_key->udp_src = output->tp.src;
1472 udp_key->udp_dst = output->tp.dst;
1473 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1474 struct ovs_key_sctp *sctp_key;
1476 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1478 goto nla_put_failure;
1479 sctp_key = nla_data(nla);
1480 sctp_key->sctp_src = output->tp.src;
1481 sctp_key->sctp_dst = output->tp.dst;
1482 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1483 swkey->ip.proto == IPPROTO_ICMP) {
1484 struct ovs_key_icmp *icmp_key;
1486 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1488 goto nla_put_failure;
1489 icmp_key = nla_data(nla);
1490 icmp_key->icmp_type = ntohs(output->tp.src);
1491 icmp_key->icmp_code = ntohs(output->tp.dst);
1492 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1493 swkey->ip.proto == IPPROTO_ICMPV6) {
1494 struct ovs_key_icmpv6 *icmpv6_key;
1496 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1497 sizeof(*icmpv6_key));
1499 goto nla_put_failure;
1500 icmpv6_key = nla_data(nla);
1501 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1502 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1504 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1505 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1506 struct ovs_key_nd *nd_key;
1508 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1510 goto nla_put_failure;
1511 nd_key = nla_data(nla);
1512 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1513 sizeof(nd_key->nd_target));
1514 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1515 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1522 nla_nest_end(skb, encap);
1530 int ovs_nla_put_key(const struct sw_flow_key *swkey,
1531 const struct sw_flow_key *output, int attr, bool is_mask,
1532 struct sk_buff *skb)
1537 nla = nla_nest_start(skb, attr);
1540 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1543 nla_nest_end(skb, nla);
1548 /* Called with ovs_mutex or RCU read lock. */
1549 int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
1551 if (ovs_identifier_is_ufid(&flow->id))
1552 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1555 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1556 OVS_FLOW_ATTR_KEY, false, skb);
1559 /* Called with ovs_mutex or RCU read lock. */
1560 int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1562 return ovs_nla_put_key(&flow->key, &flow->key,
1563 OVS_FLOW_ATTR_KEY, false, skb);
1566 /* Called with ovs_mutex or RCU read lock. */
1567 int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1569 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1570 OVS_FLOW_ATTR_MASK, true, skb);
1573 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1575 static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
1577 struct sw_flow_actions *sfa;
1579 if (size > MAX_ACTIONS_BUFSIZE) {
1580 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
1581 return ERR_PTR(-EINVAL);
1584 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1586 return ERR_PTR(-ENOMEM);
1588 sfa->actions_len = 0;
1592 static void ovs_nla_free_set_action(const struct nlattr *a)
1594 const struct nlattr *ovs_key = nla_data(a);
1595 struct ovs_tunnel_info *ovs_tun;
1597 switch (nla_type(ovs_key)) {
1598 case OVS_KEY_ATTR_TUNNEL_INFO:
1599 ovs_tun = nla_data(ovs_key);
1600 ovs_dst_release((struct dst_entry *)ovs_tun->tun_dst);
1605 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1607 const struct nlattr *a;
1613 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1614 switch (nla_type(a)) {
1615 case OVS_ACTION_ATTR_SET:
1616 ovs_nla_free_set_action(a);
1624 static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1626 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1629 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1630 * The caller must hold rcu_read_lock for this to be sensible.
1632 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1634 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
1637 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1638 int attr_len, bool log)
1641 struct sw_flow_actions *acts;
1643 int req_size = NLA_ALIGN(attr_len);
1644 int next_offset = offsetof(struct sw_flow_actions, actions) +
1645 (*sfa)->actions_len;
1647 if (req_size <= (ksize(*sfa) - next_offset))
1650 new_acts_size = ksize(*sfa) * 2;
1652 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1653 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1654 return ERR_PTR(-EMSGSIZE);
1655 new_acts_size = MAX_ACTIONS_BUFSIZE;
1658 acts = nla_alloc_flow_actions(new_acts_size, log);
1660 return (void *)acts;
1662 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1663 acts->actions_len = (*sfa)->actions_len;
1664 acts->orig_len = (*sfa)->orig_len;
1669 (*sfa)->actions_len += req_size;
1670 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1673 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
1674 int attrtype, void *data, int len, bool log)
1678 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
1682 a->nla_type = attrtype;
1683 a->nla_len = nla_attr_size(len);
1686 memcpy(nla_data(a), data, len);
1687 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1692 static int add_action(struct sw_flow_actions **sfa, int attrtype,
1693 void *data, int len, bool log)
1697 a = __add_action(sfa, attrtype, data, len, log);
1704 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1705 int attrtype, bool log)
1707 int used = (*sfa)->actions_len;
1710 err = add_action(sfa, attrtype, NULL, 0, log);
1717 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1720 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1723 a->nla_len = sfa->actions_len - st_offset;
1726 static int __ovs_nla_copy_actions(const struct nlattr *attr,
1727 const struct sw_flow_key *key,
1728 int depth, struct sw_flow_actions **sfa,
1729 __be16 eth_type, __be16 vlan_tci, bool log);
1731 static int validate_and_copy_sample(const struct nlattr *attr,
1732 const struct sw_flow_key *key, int depth,
1733 struct sw_flow_actions **sfa,
1734 __be16 eth_type, __be16 vlan_tci, bool log)
1736 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1737 const struct nlattr *probability, *actions;
1738 const struct nlattr *a;
1739 int rem, start, err, st_acts;
1741 memset(attrs, 0, sizeof(attrs));
1742 nla_for_each_nested(a, attr, rem) {
1743 int type = nla_type(a);
1744 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1751 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1752 if (!probability || nla_len(probability) != sizeof(u32))
1755 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1756 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1759 /* validation done, copy sample action. */
1760 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
1763 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1764 nla_data(probability), sizeof(u32), log);
1767 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
1771 err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
1772 eth_type, vlan_tci, log);
1776 add_nested_action_end(*sfa, st_acts);
1777 add_nested_action_end(*sfa, start);
1782 void ovs_match_init(struct sw_flow_match *match,
1783 struct sw_flow_key *key,
1784 struct sw_flow_mask *mask)
1786 memset(match, 0, sizeof(*match));
1790 memset(key, 0, sizeof(*key));
1793 memset(&mask->key, 0, sizeof(mask->key));
1794 mask->range.start = mask->range.end = 0;
1798 static int validate_geneve_opts(struct sw_flow_key *key)
1800 struct geneve_opt *option;
1801 int opts_len = key->tun_opts_len;
1802 bool crit_opt = false;
1804 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1805 while (opts_len > 0) {
1808 if (opts_len < sizeof(*option))
1811 len = sizeof(*option) + option->length * 4;
1815 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1817 option = (struct geneve_opt *)((u8 *)option + len);
1821 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1826 static int validate_and_copy_set_tun(const struct nlattr *attr,
1827 struct sw_flow_actions **sfa, bool log)
1829 struct sw_flow_match match;
1830 struct sw_flow_key key;
1831 struct metadata_dst *tun_dst;
1832 struct ip_tunnel_info *tun_info;
1833 struct ovs_tunnel_info *ovs_tun;
1835 int err = 0, start, opts_type;
1837 ovs_match_init(&match, &key, NULL);
1838 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1842 if (key.tun_opts_len) {
1843 switch (opts_type) {
1844 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1845 err = validate_geneve_opts(&key);
1849 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1854 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
1858 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1862 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1863 sizeof(*ovs_tun), log);
1865 ovs_dst_release((struct dst_entry *)tun_dst);
1869 ovs_tun = nla_data(a);
1870 ovs_tun->tun_dst = tun_dst;
1872 tun_info = &tun_dst->u.tun_info;
1873 tun_info->mode = IP_TUNNEL_INFO_TX;
1874 tun_info->key = key.tun_key;
1876 /* We need to store the options in the action itself since
1877 * everything else will go away after flow setup. We can append
1878 * it to tun_info and then point there.
1880 ip_tunnel_info_opts_set(tun_info,
1881 TUN_METADATA_OPTS(&key, key.tun_opts_len),
1883 add_nested_action_end(*sfa, start);
1888 /* Return false if there are any non-masked bits set.
1889 * Mask follows data immediately, before any netlink padding.
1891 static bool validate_masked(u8 *data, int len)
1893 u8 *mask = data + len;
1896 if (*data++ & ~*mask++)
1902 static int validate_set(const struct nlattr *a,
1903 const struct sw_flow_key *flow_key,
1904 struct sw_flow_actions **sfa,
1905 bool *skip_copy, __be16 eth_type, bool masked, bool log)
1907 const struct nlattr *ovs_key = nla_data(a);
1908 int key_type = nla_type(ovs_key);
1911 /* There can be only one key in a action */
1912 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1915 key_len = nla_len(ovs_key);
1919 if (key_type > OVS_KEY_ATTR_MAX ||
1920 !check_attr_len(key_len, ovs_key_lens[key_type].len))
1923 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1927 const struct ovs_key_ipv4 *ipv4_key;
1928 const struct ovs_key_ipv6 *ipv6_key;
1931 case OVS_KEY_ATTR_PRIORITY:
1932 case OVS_KEY_ATTR_SKB_MARK:
1933 case OVS_KEY_ATTR_ETHERNET:
1936 case OVS_KEY_ATTR_TUNNEL:
1937 if (eth_p_mpls(eth_type))
1941 return -EINVAL; /* Masked tunnel set not supported. */
1944 err = validate_and_copy_set_tun(a, sfa, log);
1949 case OVS_KEY_ATTR_IPV4:
1950 if (eth_type != htons(ETH_P_IP))
1953 ipv4_key = nla_data(ovs_key);
1956 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
1958 /* Non-writeable fields. */
1959 if (mask->ipv4_proto || mask->ipv4_frag)
1962 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1965 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1970 case OVS_KEY_ATTR_IPV6:
1971 if (eth_type != htons(ETH_P_IPV6))
1974 ipv6_key = nla_data(ovs_key);
1977 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
1979 /* Non-writeable fields. */
1980 if (mask->ipv6_proto || mask->ipv6_frag)
1983 /* Invalid bits in the flow label mask? */
1984 if (ntohl(mask->ipv6_label) & 0xFFF00000)
1987 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1990 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1993 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1998 case OVS_KEY_ATTR_TCP:
1999 if ((eth_type != htons(ETH_P_IP) &&
2000 eth_type != htons(ETH_P_IPV6)) ||
2001 flow_key->ip.proto != IPPROTO_TCP)
2006 case OVS_KEY_ATTR_UDP:
2007 if ((eth_type != htons(ETH_P_IP) &&
2008 eth_type != htons(ETH_P_IPV6)) ||
2009 flow_key->ip.proto != IPPROTO_UDP)
2014 case OVS_KEY_ATTR_MPLS:
2015 if (!eth_p_mpls(eth_type))
2019 case OVS_KEY_ATTR_SCTP:
2020 if ((eth_type != htons(ETH_P_IP) &&
2021 eth_type != htons(ETH_P_IPV6)) ||
2022 flow_key->ip.proto != IPPROTO_SCTP)
2031 /* Convert non-masked non-tunnel set actions to masked set actions. */
2032 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2033 int start, len = key_len * 2;
2038 start = add_nested_action_start(sfa,
2039 OVS_ACTION_ATTR_SET_TO_MASKED,
2044 at = __add_action(sfa, key_type, NULL, len, log);
2048 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2049 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2050 /* Clear non-writeable bits from otherwise writeable fields. */
2051 if (key_type == OVS_KEY_ATTR_IPV6) {
2052 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2054 mask->ipv6_label &= htonl(0x000FFFFF);
2056 add_nested_action_end(*sfa, start);
2062 static int validate_userspace(const struct nlattr *attr)
2064 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2065 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2066 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
2067 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
2069 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2072 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2073 attr, userspace_policy);
2077 if (!a[OVS_USERSPACE_ATTR_PID] ||
2078 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2084 static int copy_action(const struct nlattr *from,
2085 struct sw_flow_actions **sfa, bool log)
2087 int totlen = NLA_ALIGN(from->nla_len);
2090 to = reserve_sfa_size(sfa, from->nla_len, log);
2094 memcpy(to, from, totlen);
2098 static int __ovs_nla_copy_actions(const struct nlattr *attr,
2099 const struct sw_flow_key *key,
2100 int depth, struct sw_flow_actions **sfa,
2101 __be16 eth_type, __be16 vlan_tci, bool log)
2103 const struct nlattr *a;
2106 if (depth >= SAMPLE_ACTION_DEPTH)
2109 nla_for_each_nested(a, attr, rem) {
2110 /* Expected argument lengths, (u32)-1 for variable length. */
2111 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2112 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
2113 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
2114 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
2115 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2116 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
2117 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2118 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2119 [OVS_ACTION_ATTR_SET] = (u32)-1,
2120 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
2121 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
2122 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
2124 const struct ovs_action_push_vlan *vlan;
2125 int type = nla_type(a);
2128 if (type > OVS_ACTION_ATTR_MAX ||
2129 (action_lens[type] != nla_len(a) &&
2130 action_lens[type] != (u32)-1))
2135 case OVS_ACTION_ATTR_UNSPEC:
2138 case OVS_ACTION_ATTR_USERSPACE:
2139 err = validate_userspace(a);
2144 case OVS_ACTION_ATTR_OUTPUT:
2145 if (nla_get_u32(a) >= DP_MAX_PORTS)
2149 case OVS_ACTION_ATTR_HASH: {
2150 const struct ovs_action_hash *act_hash = nla_data(a);
2152 switch (act_hash->hash_alg) {
2153 case OVS_HASH_ALG_L4:
2162 case OVS_ACTION_ATTR_POP_VLAN:
2163 vlan_tci = htons(0);
2166 case OVS_ACTION_ATTR_PUSH_VLAN:
2168 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2170 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2172 vlan_tci = vlan->vlan_tci;
2175 case OVS_ACTION_ATTR_RECIRC:
2178 case OVS_ACTION_ATTR_PUSH_MPLS: {
2179 const struct ovs_action_push_mpls *mpls = nla_data(a);
2181 if (!eth_p_mpls(mpls->mpls_ethertype))
2183 /* Prohibit push MPLS other than to a white list
2184 * for packets that have a known tag order.
2186 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2187 (eth_type != htons(ETH_P_IP) &&
2188 eth_type != htons(ETH_P_IPV6) &&
2189 eth_type != htons(ETH_P_ARP) &&
2190 eth_type != htons(ETH_P_RARP) &&
2191 !eth_p_mpls(eth_type)))
2193 eth_type = mpls->mpls_ethertype;
2197 case OVS_ACTION_ATTR_POP_MPLS:
2198 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2199 !eth_p_mpls(eth_type))
2202 /* Disallow subsequent L2.5+ set and mpls_pop actions
2203 * as there is no check here to ensure that the new
2204 * eth_type is valid and thus set actions could
2205 * write off the end of the packet or otherwise
2208 * Support for these actions is planned using packet
2211 eth_type = htons(0);
2214 case OVS_ACTION_ATTR_SET:
2215 err = validate_set(a, key, sfa,
2216 &skip_copy, eth_type, false, log);
2221 case OVS_ACTION_ATTR_SET_MASKED:
2222 err = validate_set(a, key, sfa,
2223 &skip_copy, eth_type, true, log);
2228 case OVS_ACTION_ATTR_SAMPLE:
2229 err = validate_and_copy_sample(a, key, depth, sfa,
2230 eth_type, vlan_tci, log);
2237 OVS_NLERR(log, "Unknown Action type %d", type);
2241 err = copy_action(a, sfa, log);
2253 /* 'key' must be the masked key. */
2254 int ovs_nla_copy_actions(const struct nlattr *attr,
2255 const struct sw_flow_key *key,
2256 struct sw_flow_actions **sfa, bool log)
2260 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
2262 return PTR_ERR(*sfa);
2264 (*sfa)->orig_len = nla_len(attr);
2265 err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
2268 ovs_nla_free_flow_actions(*sfa);
2273 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2275 const struct nlattr *a;
2276 struct nlattr *start;
2279 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2283 nla_for_each_nested(a, attr, rem) {
2284 int type = nla_type(a);
2285 struct nlattr *st_sample;
2288 case OVS_SAMPLE_ATTR_PROBABILITY:
2289 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2290 sizeof(u32), nla_data(a)))
2293 case OVS_SAMPLE_ATTR_ACTIONS:
2294 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2297 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2300 nla_nest_end(skb, st_sample);
2305 nla_nest_end(skb, start);
2309 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2311 const struct nlattr *ovs_key = nla_data(a);
2312 int key_type = nla_type(ovs_key);
2313 struct nlattr *start;
2317 case OVS_KEY_ATTR_TUNNEL_INFO: {
2318 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2319 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
2321 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2325 err = ipv4_tun_to_nlattr(skb, &tun_info->key,
2326 tun_info->options_len ?
2327 ip_tunnel_info_opts(tun_info) : NULL,
2328 tun_info->options_len);
2331 nla_nest_end(skb, start);
2335 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2343 static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2344 struct sk_buff *skb)
2346 const struct nlattr *ovs_key = nla_data(a);
2347 size_t key_len = nla_len(ovs_key) / 2;
2349 /* Revert the conversion we did from a non-masked set action to
2350 * masked set action.
2352 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a) - key_len, ovs_key))
2358 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2360 const struct nlattr *a;
2363 nla_for_each_attr(a, attr, len, rem) {
2364 int type = nla_type(a);
2367 case OVS_ACTION_ATTR_SET:
2368 err = set_action_to_attr(a, skb);
2373 case OVS_ACTION_ATTR_SET_TO_MASKED:
2374 err = masked_set_action_to_set_action_attr(a, skb);
2379 case OVS_ACTION_ATTR_SAMPLE:
2380 err = sample_action_to_attr(a, skb);
2385 if (nla_put(skb, type, nla_len(a), nla_data(a)))