1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007-2017 Nicira, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/uaccess.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_vlan.h>
15 #include <net/llc_pdu.h>
16 #include <linux/kernel.h>
17 #include <linux/jhash.h>
18 #include <linux/jiffies.h>
19 #include <linux/llc.h>
20 #include <linux/module.h>
22 #include <linux/rcupdate.h>
23 #include <linux/if_arp.h>
25 #include <linux/ipv6.h>
26 #include <linux/sctp.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/icmp.h>
30 #include <linux/icmpv6.h>
31 #include <linux/rculist.h>
32 #include <net/geneve.h>
35 #include <net/ndisc.h>
37 #include <net/vxlan.h>
38 #include <net/tun_proto.h>
39 #include <net/erspan.h>
41 #include "flow_netlink.h"
45 const struct ovs_len_tbl *next;
48 #define OVS_ATTR_NESTED -1
49 #define OVS_ATTR_VARIABLE -2
50 #define OVS_COPY_ACTIONS_MAX_DEPTH 16
52 static bool actions_may_change_flow(const struct nlattr *actions)
57 nla_for_each_nested(nla, actions, rem) {
58 u16 action = nla_type(nla);
61 case OVS_ACTION_ATTR_OUTPUT:
62 case OVS_ACTION_ATTR_RECIRC:
63 case OVS_ACTION_ATTR_TRUNC:
64 case OVS_ACTION_ATTR_USERSPACE:
67 case OVS_ACTION_ATTR_CT:
68 case OVS_ACTION_ATTR_CT_CLEAR:
69 case OVS_ACTION_ATTR_HASH:
70 case OVS_ACTION_ATTR_POP_ETH:
71 case OVS_ACTION_ATTR_POP_MPLS:
72 case OVS_ACTION_ATTR_POP_NSH:
73 case OVS_ACTION_ATTR_POP_VLAN:
74 case OVS_ACTION_ATTR_PUSH_ETH:
75 case OVS_ACTION_ATTR_PUSH_MPLS:
76 case OVS_ACTION_ATTR_PUSH_NSH:
77 case OVS_ACTION_ATTR_PUSH_VLAN:
78 case OVS_ACTION_ATTR_SAMPLE:
79 case OVS_ACTION_ATTR_SET:
80 case OVS_ACTION_ATTR_SET_MASKED:
81 case OVS_ACTION_ATTR_METER:
82 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
83 case OVS_ACTION_ATTR_ADD_MPLS:
84 case OVS_ACTION_ATTR_DEC_TTL:
92 static void update_range(struct sw_flow_match *match,
93 size_t offset, size_t size, bool is_mask)
95 struct sw_flow_key_range *range;
96 size_t start = rounddown(offset, sizeof(long));
97 size_t end = roundup(offset + size, sizeof(long));
100 range = &match->range;
102 range = &match->mask->range;
104 if (range->start == range->end) {
105 range->start = start;
110 if (range->start > start)
111 range->start = start;
113 if (range->end < end)
117 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
119 update_range(match, offsetof(struct sw_flow_key, field), \
120 sizeof((match)->key->field), is_mask); \
122 (match)->mask->key.field = value; \
124 (match)->key->field = value; \
127 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
129 update_range(match, offset, len, is_mask); \
131 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
134 memcpy((u8 *)(match)->key + offset, value_p, len); \
137 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
138 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
139 value_p, len, is_mask)
141 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
143 update_range(match, offsetof(struct sw_flow_key, field), \
144 sizeof((match)->key->field), is_mask); \
146 memset((u8 *)&(match)->mask->key.field, value, \
147 sizeof((match)->mask->key.field)); \
149 memset((u8 *)&(match)->key->field, value, \
150 sizeof((match)->key->field)); \
153 static bool match_validate(const struct sw_flow_match *match,
154 u64 key_attrs, u64 mask_attrs, bool log)
156 u64 key_expected = 0;
157 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
159 /* The following mask attributes allowed only if they
160 * pass the validation tests. */
161 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
162 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
163 | (1 << OVS_KEY_ATTR_IPV6)
164 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
165 | (1 << OVS_KEY_ATTR_TCP)
166 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
167 | (1 << OVS_KEY_ATTR_UDP)
168 | (1 << OVS_KEY_ATTR_SCTP)
169 | (1 << OVS_KEY_ATTR_ICMP)
170 | (1 << OVS_KEY_ATTR_ICMPV6)
171 | (1 << OVS_KEY_ATTR_ARP)
172 | (1 << OVS_KEY_ATTR_ND)
173 | (1 << OVS_KEY_ATTR_MPLS)
174 | (1 << OVS_KEY_ATTR_NSH));
176 /* Always allowed mask fields. */
177 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
178 | (1 << OVS_KEY_ATTR_IN_PORT)
179 | (1 << OVS_KEY_ATTR_ETHERTYPE));
181 /* Check key attributes. */
182 if (match->key->eth.type == htons(ETH_P_ARP)
183 || match->key->eth.type == htons(ETH_P_RARP)) {
184 key_expected |= 1 << OVS_KEY_ATTR_ARP;
185 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
186 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
189 if (eth_p_mpls(match->key->eth.type)) {
190 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
191 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
192 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
195 if (match->key->eth.type == htons(ETH_P_IP)) {
196 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
197 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
198 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
199 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
202 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
203 if (match->key->ip.proto == IPPROTO_UDP) {
204 key_expected |= 1 << OVS_KEY_ATTR_UDP;
205 if (match->mask && (match->mask->key.ip.proto == 0xff))
206 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
209 if (match->key->ip.proto == IPPROTO_SCTP) {
210 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
211 if (match->mask && (match->mask->key.ip.proto == 0xff))
212 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
215 if (match->key->ip.proto == IPPROTO_TCP) {
216 key_expected |= 1 << OVS_KEY_ATTR_TCP;
217 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
218 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
219 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
220 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
224 if (match->key->ip.proto == IPPROTO_ICMP) {
225 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
226 if (match->mask && (match->mask->key.ip.proto == 0xff))
227 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
232 if (match->key->eth.type == htons(ETH_P_IPV6)) {
233 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
234 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
235 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
236 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
239 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
240 if (match->key->ip.proto == IPPROTO_UDP) {
241 key_expected |= 1 << OVS_KEY_ATTR_UDP;
242 if (match->mask && (match->mask->key.ip.proto == 0xff))
243 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
246 if (match->key->ip.proto == IPPROTO_SCTP) {
247 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
248 if (match->mask && (match->mask->key.ip.proto == 0xff))
249 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
252 if (match->key->ip.proto == IPPROTO_TCP) {
253 key_expected |= 1 << OVS_KEY_ATTR_TCP;
254 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
255 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
256 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
257 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
261 if (match->key->ip.proto == IPPROTO_ICMPV6) {
262 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
263 if (match->mask && (match->mask->key.ip.proto == 0xff))
264 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
266 if (match->key->tp.src ==
267 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
268 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
269 key_expected |= 1 << OVS_KEY_ATTR_ND;
270 /* Original direction conntrack tuple
271 * uses the same space as the ND fields
272 * in the key, so both are not allowed
275 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
276 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
277 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
283 if (match->key->eth.type == htons(ETH_P_NSH)) {
284 key_expected |= 1 << OVS_KEY_ATTR_NSH;
286 match->mask->key.eth.type == htons(0xffff)) {
287 mask_allowed |= 1 << OVS_KEY_ATTR_NSH;
291 if ((key_attrs & key_expected) != key_expected) {
292 /* Key attributes check failed. */
293 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
294 (unsigned long long)key_attrs,
295 (unsigned long long)key_expected);
299 if ((mask_attrs & mask_allowed) != mask_attrs) {
300 /* Mask attributes check failed. */
301 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
302 (unsigned long long)mask_attrs,
303 (unsigned long long)mask_allowed);
310 size_t ovs_tun_key_attr_size(void)
312 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
313 * updating this function.
315 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
316 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
317 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
318 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
319 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
320 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
321 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
322 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
323 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
324 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and
325 * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with
326 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
328 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
329 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
332 static size_t ovs_nsh_key_attr_size(void)
334 /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
335 * updating this function.
337 return nla_total_size(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
338 /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
339 * mutually exclusive, so the bigger one can cover
342 + nla_total_size(NSH_CTX_HDRS_MAX_LEN);
345 size_t ovs_key_attr_size(void)
347 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
348 * updating this function.
350 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
352 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
353 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
354 + ovs_tun_key_attr_size()
355 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
356 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
357 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
358 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
359 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
360 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
361 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
362 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
363 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
364 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
365 + ovs_nsh_key_attr_size()
366 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
367 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
368 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
369 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
371 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
372 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
373 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
376 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
377 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
380 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
381 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
382 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
383 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
384 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
385 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
386 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
387 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
388 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
389 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
390 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
391 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
392 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
393 .next = ovs_vxlan_ext_key_lens },
394 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
395 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
396 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = OVS_ATTR_VARIABLE },
397 [OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE] = { .len = 0 },
400 static const struct ovs_len_tbl
401 ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
402 [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
403 [OVS_NSH_KEY_ATTR_MD1] = { .len = sizeof(struct ovs_nsh_key_md1) },
404 [OVS_NSH_KEY_ATTR_MD2] = { .len = OVS_ATTR_VARIABLE },
407 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
408 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
409 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
410 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
411 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
412 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
413 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
414 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
415 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
416 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
417 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
418 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
419 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
420 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
421 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
422 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
423 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
424 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
425 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
426 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
427 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
428 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
429 .next = ovs_tunnel_key_lens, },
430 [OVS_KEY_ATTR_MPLS] = { .len = OVS_ATTR_VARIABLE },
431 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
432 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
433 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
434 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
435 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
436 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
437 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
438 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
439 [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
440 .next = ovs_nsh_key_attr_lens, },
443 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
445 return expected_len == attr_len ||
446 expected_len == OVS_ATTR_NESTED ||
447 expected_len == OVS_ATTR_VARIABLE;
450 static bool is_all_zero(const u8 *fp, size_t size)
457 for (i = 0; i < size; i++)
464 static int __parse_flow_nlattrs(const struct nlattr *attr,
465 const struct nlattr *a[],
466 u64 *attrsp, bool log, bool nz)
468 const struct nlattr *nla;
473 nla_for_each_nested(nla, attr, rem) {
474 u16 type = nla_type(nla);
477 if (type > OVS_KEY_ATTR_MAX) {
478 OVS_NLERR(log, "Key type %d is out of range max %d",
479 type, OVS_KEY_ATTR_MAX);
483 if (attrs & (1 << type)) {
484 OVS_NLERR(log, "Duplicate key (type %d).", type);
488 expected_len = ovs_key_lens[type].len;
489 if (!check_attr_len(nla_len(nla), expected_len)) {
490 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
491 type, nla_len(nla), expected_len);
495 if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
501 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
509 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
510 const struct nlattr *a[], u64 *attrsp,
513 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
516 int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
517 u64 *attrsp, bool log)
519 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
522 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
523 struct sw_flow_match *match, bool is_mask,
526 unsigned long opt_key_offset;
528 if (nla_len(a) > sizeof(match->key->tun_opts)) {
529 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
530 nla_len(a), sizeof(match->key->tun_opts));
534 if (nla_len(a) % 4 != 0) {
535 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
540 /* We need to record the length of the options passed
541 * down, otherwise packets with the same format but
542 * additional options will be silently matched.
545 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
548 /* This is somewhat unusual because it looks at
549 * both the key and mask while parsing the
550 * attributes (and by extension assumes the key
551 * is parsed first). Normally, we would verify
552 * that each is the correct length and that the
553 * attributes line up in the validate function.
554 * However, that is difficult because this is
555 * variable length and we won't have the
558 if (match->key->tun_opts_len != nla_len(a)) {
559 OVS_NLERR(log, "Geneve option len %d != mask len %d",
560 match->key->tun_opts_len, nla_len(a));
564 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
567 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
568 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
569 nla_len(a), is_mask);
573 static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
574 struct sw_flow_match *match, bool is_mask,
579 unsigned long opt_key_offset;
580 struct vxlan_metadata opts;
582 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
584 memset(&opts, 0, sizeof(opts));
585 nla_for_each_nested(a, attr, rem) {
586 int type = nla_type(a);
588 if (type > OVS_VXLAN_EXT_MAX) {
589 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
590 type, OVS_VXLAN_EXT_MAX);
594 if (!check_attr_len(nla_len(a),
595 ovs_vxlan_ext_key_lens[type].len)) {
596 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
598 ovs_vxlan_ext_key_lens[type].len);
603 case OVS_VXLAN_EXT_GBP:
604 opts.gbp = nla_get_u32(a);
607 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
613 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
619 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
621 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
623 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
624 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
629 static int erspan_tun_opt_from_nlattr(const struct nlattr *a,
630 struct sw_flow_match *match, bool is_mask,
633 unsigned long opt_key_offset;
635 BUILD_BUG_ON(sizeof(struct erspan_metadata) >
636 sizeof(match->key->tun_opts));
638 if (nla_len(a) > sizeof(match->key->tun_opts)) {
639 OVS_NLERR(log, "ERSPAN option length err (len %d, max %zu).",
640 nla_len(a), sizeof(match->key->tun_opts));
645 SW_FLOW_KEY_PUT(match, tun_opts_len,
646 sizeof(struct erspan_metadata), false);
648 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
650 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
651 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
652 nla_len(a), is_mask);
656 static int ip_tun_from_nlattr(const struct nlattr *attr,
657 struct sw_flow_match *match, bool is_mask,
660 bool ttl = false, ipv4 = false, ipv6 = false;
661 bool info_bridge_mode = false;
662 __be16 tun_flags = 0;
667 nla_for_each_nested(a, attr, rem) {
668 int type = nla_type(a);
671 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
672 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
673 type, OVS_TUNNEL_KEY_ATTR_MAX);
677 if (!check_attr_len(nla_len(a),
678 ovs_tunnel_key_lens[type].len)) {
679 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
680 type, nla_len(a), ovs_tunnel_key_lens[type].len);
685 case OVS_TUNNEL_KEY_ATTR_ID:
686 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
687 nla_get_be64(a), is_mask);
688 tun_flags |= TUNNEL_KEY;
690 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
691 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
692 nla_get_in_addr(a), is_mask);
695 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
696 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
697 nla_get_in_addr(a), is_mask);
700 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
701 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
702 nla_get_in6_addr(a), is_mask);
705 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
706 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
707 nla_get_in6_addr(a), is_mask);
710 case OVS_TUNNEL_KEY_ATTR_TOS:
711 SW_FLOW_KEY_PUT(match, tun_key.tos,
712 nla_get_u8(a), is_mask);
714 case OVS_TUNNEL_KEY_ATTR_TTL:
715 SW_FLOW_KEY_PUT(match, tun_key.ttl,
716 nla_get_u8(a), is_mask);
719 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
720 tun_flags |= TUNNEL_DONT_FRAGMENT;
722 case OVS_TUNNEL_KEY_ATTR_CSUM:
723 tun_flags |= TUNNEL_CSUM;
725 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
726 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
727 nla_get_be16(a), is_mask);
729 case OVS_TUNNEL_KEY_ATTR_TP_DST:
730 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
731 nla_get_be16(a), is_mask);
733 case OVS_TUNNEL_KEY_ATTR_OAM:
734 tun_flags |= TUNNEL_OAM;
736 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
738 OVS_NLERR(log, "Multiple metadata blocks provided");
742 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
746 tun_flags |= TUNNEL_GENEVE_OPT;
749 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
751 OVS_NLERR(log, "Multiple metadata blocks provided");
755 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
759 tun_flags |= TUNNEL_VXLAN_OPT;
762 case OVS_TUNNEL_KEY_ATTR_PAD:
764 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
766 OVS_NLERR(log, "Multiple metadata blocks provided");
770 err = erspan_tun_opt_from_nlattr(a, match, is_mask,
775 tun_flags |= TUNNEL_ERSPAN_OPT;
778 case OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE:
779 info_bridge_mode = true;
783 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
789 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
791 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
793 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
797 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
803 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
808 if (!ipv4 && !ipv6) {
809 OVS_NLERR(log, "IP tunnel dst address not specified");
813 if (info_bridge_mode) {
814 if (match->key->tun_key.u.ipv4.src ||
815 match->key->tun_key.u.ipv4.dst ||
816 match->key->tun_key.tp_src ||
817 match->key->tun_key.tp_dst ||
818 match->key->tun_key.ttl ||
819 match->key->tun_key.tos ||
820 tun_flags & ~TUNNEL_KEY) {
821 OVS_NLERR(log, "IPv4 tun info is not correct");
824 } else if (!match->key->tun_key.u.ipv4.dst) {
825 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
829 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
830 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
834 if (!ttl && !info_bridge_mode) {
835 OVS_NLERR(log, "IP tunnel TTL not specified.");
843 static int vxlan_opt_to_nlattr(struct sk_buff *skb,
844 const void *tun_opts, int swkey_tun_opts_len)
846 const struct vxlan_metadata *opts = tun_opts;
849 nla = nla_nest_start_noflag(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
853 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
856 nla_nest_end(skb, nla);
860 static int __ip_tun_to_nlattr(struct sk_buff *skb,
861 const struct ip_tunnel_key *output,
862 const void *tun_opts, int swkey_tun_opts_len,
863 unsigned short tun_proto, u8 mode)
865 if (output->tun_flags & TUNNEL_KEY &&
866 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
867 OVS_TUNNEL_KEY_ATTR_PAD))
870 if (mode & IP_TUNNEL_INFO_BRIDGE)
871 return nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE)
876 if (output->u.ipv4.src &&
877 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
880 if (output->u.ipv4.dst &&
881 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
886 if (!ipv6_addr_any(&output->u.ipv6.src) &&
887 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
888 &output->u.ipv6.src))
890 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
891 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
892 &output->u.ipv6.dst))
897 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
899 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
901 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
902 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
904 if ((output->tun_flags & TUNNEL_CSUM) &&
905 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
907 if (output->tp_src &&
908 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
910 if (output->tp_dst &&
911 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
913 if ((output->tun_flags & TUNNEL_OAM) &&
914 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
916 if (swkey_tun_opts_len) {
917 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
918 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
919 swkey_tun_opts_len, tun_opts))
921 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
922 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
924 else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
925 nla_put(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
926 swkey_tun_opts_len, tun_opts))
933 static int ip_tun_to_nlattr(struct sk_buff *skb,
934 const struct ip_tunnel_key *output,
935 const void *tun_opts, int swkey_tun_opts_len,
936 unsigned short tun_proto, u8 mode)
941 nla = nla_nest_start_noflag(skb, OVS_KEY_ATTR_TUNNEL);
945 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
950 nla_nest_end(skb, nla);
954 int ovs_nla_put_tunnel_info(struct sk_buff *skb,
955 struct ip_tunnel_info *tun_info)
957 return __ip_tun_to_nlattr(skb, &tun_info->key,
958 ip_tunnel_info_opts(tun_info),
959 tun_info->options_len,
960 ip_tunnel_info_af(tun_info), tun_info->mode);
963 static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
964 const struct nlattr *a[],
965 bool is_mask, bool inner)
970 if (a[OVS_KEY_ATTR_VLAN])
971 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
973 if (a[OVS_KEY_ATTR_ETHERTYPE])
974 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
976 if (likely(!inner)) {
977 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
978 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
980 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
981 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
986 static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
987 u64 key_attrs, bool inner,
988 const struct nlattr **a, bool log)
992 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
993 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
994 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
999 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1000 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
1001 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
1005 if (a[OVS_KEY_ATTR_VLAN])
1006 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1008 if (!(tci & htons(VLAN_CFI_MASK))) {
1010 OVS_NLERR(log, "%s TCI does not have VLAN_CFI_MASK bit set.",
1011 (inner) ? "C-VLAN" : "VLAN");
1013 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
1014 /* Corner case for truncated VLAN header. */
1015 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
1016 (inner) ? "C-VLAN" : "VLAN");
1024 static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
1025 u64 key_attrs, bool inner,
1026 const struct nlattr **a, bool log)
1030 bool encap_valid = !!(match->key->eth.vlan.tci &
1031 htons(VLAN_CFI_MASK));
1032 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
1033 htons(VLAN_CFI_MASK));
1035 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
1040 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
1041 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
1042 (inner) ? "C-VLAN" : "VLAN");
1046 if (a[OVS_KEY_ATTR_VLAN])
1047 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1049 if (a[OVS_KEY_ATTR_ETHERTYPE])
1050 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1052 if (tpid != htons(0xffff)) {
1053 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
1054 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
1057 if (!(tci & htons(VLAN_CFI_MASK))) {
1058 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
1059 (inner) ? "C-VLAN" : "VLAN");
1066 static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1067 u64 *key_attrs, bool inner,
1068 const struct nlattr **a, bool is_mask,
1072 const struct nlattr *encap;
1075 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1078 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1083 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1087 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1088 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1089 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1091 encap = a[OVS_KEY_ATTR_ENCAP];
1094 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1096 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1101 static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1102 u64 *key_attrs, const struct nlattr **a,
1103 bool is_mask, bool log)
1106 bool encap_valid = false;
1108 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1113 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_CFI_MASK));
1115 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1124 static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1125 u64 *attrs, const struct nlattr **a,
1126 bool is_mask, bool log)
1130 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1132 /* Always exact match EtherType. */
1133 eth_type = htons(0xffff);
1134 } else if (!eth_proto_is_802_3(eth_type)) {
1135 OVS_NLERR(log, "EtherType %x is less than min %x",
1136 ntohs(eth_type), ETH_P_802_3_MIN);
1140 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1141 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1145 static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1146 u64 *attrs, const struct nlattr **a,
1147 bool is_mask, bool log)
1149 u8 mac_proto = MAC_PROTO_ETHERNET;
1151 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1152 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1154 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1155 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1158 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1159 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1161 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1162 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1165 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1166 SW_FLOW_KEY_PUT(match, phy.priority,
1167 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1168 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1171 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1172 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1175 in_port = 0xffffffff; /* Always exact match in_port. */
1176 } else if (in_port >= DP_MAX_PORTS) {
1177 OVS_NLERR(log, "Port %d exceeds max allowable %d",
1178 in_port, DP_MAX_PORTS);
1182 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1183 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1184 } else if (!is_mask) {
1185 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1188 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1189 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1191 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1192 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1194 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
1195 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1198 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1201 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
1202 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
1203 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
1205 if (ct_state & ~CT_SUPPORTED_MASK) {
1206 OVS_NLERR(log, "ct_state flags %08x unsupported",
1211 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
1212 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1214 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
1215 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
1216 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1218 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
1219 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1221 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
1222 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
1223 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1225 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1226 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1228 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1229 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1230 const struct ovs_key_ct_labels *cl;
1232 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1233 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
1234 sizeof(*cl), is_mask);
1235 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
1237 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1238 const struct ovs_key_ct_tuple_ipv4 *ct;
1240 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1242 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1243 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1244 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1245 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1246 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
1247 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1249 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1250 const struct ovs_key_ct_tuple_ipv6 *ct;
1252 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1254 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1255 sizeof(match->key->ipv6.ct_orig.src),
1257 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1258 sizeof(match->key->ipv6.ct_orig.dst),
1260 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1261 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1262 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
1263 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1266 /* For layer 3 packets the Ethernet type is provided
1267 * and treated as metadata but no MAC addresses are provided.
1269 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1270 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1271 mac_proto = MAC_PROTO_NONE;
1273 /* Always exact match mac_proto */
1274 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1276 if (mac_proto == MAC_PROTO_NONE)
1277 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1283 int nsh_hdr_from_nlattr(const struct nlattr *attr,
1284 struct nshhdr *nh, size_t size)
1292 /* validate_nsh has check this, so we needn't do duplicate check here
1294 if (size < NSH_BASE_HDR_LEN)
1297 nla_for_each_nested(a, attr, rem) {
1298 int type = nla_type(a);
1301 case OVS_NSH_KEY_ATTR_BASE: {
1302 const struct ovs_nsh_key_base *base = nla_data(a);
1304 flags = base->flags;
1307 nh->mdtype = base->mdtype;
1308 nh->path_hdr = base->path_hdr;
1311 case OVS_NSH_KEY_ATTR_MD1:
1313 if (mdlen > size - NSH_BASE_HDR_LEN)
1315 memcpy(&nh->md1, nla_data(a), mdlen);
1318 case OVS_NSH_KEY_ATTR_MD2:
1320 if (mdlen > size - NSH_BASE_HDR_LEN)
1322 memcpy(&nh->md2, nla_data(a), mdlen);
1330 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1331 nh->ver_flags_ttl_len = 0;
1332 nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
1337 int nsh_key_from_nlattr(const struct nlattr *attr,
1338 struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask)
1343 /* validate_nsh has check this, so we needn't do duplicate check here
1345 nla_for_each_nested(a, attr, rem) {
1346 int type = nla_type(a);
1349 case OVS_NSH_KEY_ATTR_BASE: {
1350 const struct ovs_nsh_key_base *base = nla_data(a);
1351 const struct ovs_nsh_key_base *base_mask = base + 1;
1354 nsh_mask->base = *base_mask;
1357 case OVS_NSH_KEY_ATTR_MD1: {
1358 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1359 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1361 memcpy(nsh->context, md1->context, sizeof(*md1));
1362 memcpy(nsh_mask->context, md1_mask->context,
1366 case OVS_NSH_KEY_ATTR_MD2:
1367 /* Not supported yet */
1377 static int nsh_key_put_from_nlattr(const struct nlattr *attr,
1378 struct sw_flow_match *match, bool is_mask,
1379 bool is_push_nsh, bool log)
1383 bool has_base = false;
1384 bool has_md1 = false;
1385 bool has_md2 = false;
1389 if (WARN_ON(is_push_nsh && is_mask))
1392 nla_for_each_nested(a, attr, rem) {
1393 int type = nla_type(a);
1396 if (type > OVS_NSH_KEY_ATTR_MAX) {
1397 OVS_NLERR(log, "nsh attr %d is out of range max %d",
1398 type, OVS_NSH_KEY_ATTR_MAX);
1402 if (!check_attr_len(nla_len(a),
1403 ovs_nsh_key_attr_lens[type].len)) {
1406 "nsh attr %d has unexpected len %d expected %d",
1409 ovs_nsh_key_attr_lens[type].len
1415 case OVS_NSH_KEY_ATTR_BASE: {
1416 const struct ovs_nsh_key_base *base = nla_data(a);
1419 mdtype = base->mdtype;
1420 SW_FLOW_KEY_PUT(match, nsh.base.flags,
1421 base->flags, is_mask);
1422 SW_FLOW_KEY_PUT(match, nsh.base.ttl,
1423 base->ttl, is_mask);
1424 SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
1425 base->mdtype, is_mask);
1426 SW_FLOW_KEY_PUT(match, nsh.base.np,
1428 SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
1429 base->path_hdr, is_mask);
1432 case OVS_NSH_KEY_ATTR_MD1: {
1433 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1436 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
1437 SW_FLOW_KEY_PUT(match, nsh.context[i],
1438 md1->context[i], is_mask);
1441 case OVS_NSH_KEY_ATTR_MD2:
1442 if (!is_push_nsh) /* Not supported MD type 2 yet */
1447 if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
1450 "Invalid MD length %d for MD type %d",
1458 OVS_NLERR(log, "Unknown nsh attribute %d",
1465 OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
1469 if (has_md1 && has_md2) {
1472 "invalid nsh attribute: md1 and md2 are exclusive."
1478 if ((has_md1 && mdtype != NSH_M_TYPE1) ||
1479 (has_md2 && mdtype != NSH_M_TYPE2)) {
1480 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1486 (!has_base || (!has_md1 && !has_md2))) {
1489 "push_nsh: missing base or metadata attributes"
1498 static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1499 u64 attrs, const struct nlattr **a,
1500 bool is_mask, bool log)
1504 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
1508 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1509 const struct ovs_key_ethernet *eth_key;
1511 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1512 SW_FLOW_KEY_MEMCPY(match, eth.src,
1513 eth_key->eth_src, ETH_ALEN, is_mask);
1514 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1515 eth_key->eth_dst, ETH_ALEN, is_mask);
1516 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1518 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1519 /* VLAN attribute is always parsed before getting here since it
1520 * may occur multiple times.
1522 OVS_NLERR(log, "VLAN attribute unexpected.");
1526 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1527 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1531 } else if (!is_mask) {
1532 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1534 } else if (!match->key->eth.type) {
1535 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1539 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1540 const struct ovs_key_ipv4 *ipv4_key;
1542 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1543 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1544 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1545 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1548 SW_FLOW_KEY_PUT(match, ip.proto,
1549 ipv4_key->ipv4_proto, is_mask);
1550 SW_FLOW_KEY_PUT(match, ip.tos,
1551 ipv4_key->ipv4_tos, is_mask);
1552 SW_FLOW_KEY_PUT(match, ip.ttl,
1553 ipv4_key->ipv4_ttl, is_mask);
1554 SW_FLOW_KEY_PUT(match, ip.frag,
1555 ipv4_key->ipv4_frag, is_mask);
1556 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1557 ipv4_key->ipv4_src, is_mask);
1558 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1559 ipv4_key->ipv4_dst, is_mask);
1560 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1563 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1564 const struct ovs_key_ipv6 *ipv6_key;
1566 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1567 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1568 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1569 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1573 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
1574 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
1575 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1579 SW_FLOW_KEY_PUT(match, ipv6.label,
1580 ipv6_key->ipv6_label, is_mask);
1581 SW_FLOW_KEY_PUT(match, ip.proto,
1582 ipv6_key->ipv6_proto, is_mask);
1583 SW_FLOW_KEY_PUT(match, ip.tos,
1584 ipv6_key->ipv6_tclass, is_mask);
1585 SW_FLOW_KEY_PUT(match, ip.ttl,
1586 ipv6_key->ipv6_hlimit, is_mask);
1587 SW_FLOW_KEY_PUT(match, ip.frag,
1588 ipv6_key->ipv6_frag, is_mask);
1589 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1591 sizeof(match->key->ipv6.addr.src),
1593 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1595 sizeof(match->key->ipv6.addr.dst),
1598 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1601 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1602 const struct ovs_key_arp *arp_key;
1604 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1605 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1606 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
1611 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1612 arp_key->arp_sip, is_mask);
1613 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1614 arp_key->arp_tip, is_mask);
1615 SW_FLOW_KEY_PUT(match, ip.proto,
1616 ntohs(arp_key->arp_op), is_mask);
1617 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1618 arp_key->arp_sha, ETH_ALEN, is_mask);
1619 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1620 arp_key->arp_tha, ETH_ALEN, is_mask);
1622 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1625 if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
1626 if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
1627 is_mask, false, log) < 0)
1629 attrs &= ~(1 << OVS_KEY_ATTR_NSH);
1632 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1633 const struct ovs_key_mpls *mpls_key;
1635 u32 label_count, label_count_mask, i;
1637 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1638 hdr_len = nla_len(a[OVS_KEY_ATTR_MPLS]);
1639 label_count = hdr_len / sizeof(struct ovs_key_mpls);
1641 if (label_count == 0 || label_count > MPLS_LABEL_DEPTH ||
1642 hdr_len % sizeof(struct ovs_key_mpls))
1645 label_count_mask = GENMASK(label_count - 1, 0);
1647 for (i = 0 ; i < label_count; i++)
1648 SW_FLOW_KEY_PUT(match, mpls.lse[i],
1649 mpls_key[i].mpls_lse, is_mask);
1651 SW_FLOW_KEY_PUT(match, mpls.num_labels_mask,
1652 label_count_mask, is_mask);
1654 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1657 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1658 const struct ovs_key_tcp *tcp_key;
1660 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1661 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1662 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
1663 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1666 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
1667 SW_FLOW_KEY_PUT(match, tp.flags,
1668 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1670 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1673 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1674 const struct ovs_key_udp *udp_key;
1676 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1677 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1678 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
1679 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1682 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1683 const struct ovs_key_sctp *sctp_key;
1685 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1686 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1687 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
1688 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1691 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1692 const struct ovs_key_icmp *icmp_key;
1694 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1695 SW_FLOW_KEY_PUT(match, tp.src,
1696 htons(icmp_key->icmp_type), is_mask);
1697 SW_FLOW_KEY_PUT(match, tp.dst,
1698 htons(icmp_key->icmp_code), is_mask);
1699 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1702 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1703 const struct ovs_key_icmpv6 *icmpv6_key;
1705 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1706 SW_FLOW_KEY_PUT(match, tp.src,
1707 htons(icmpv6_key->icmpv6_type), is_mask);
1708 SW_FLOW_KEY_PUT(match, tp.dst,
1709 htons(icmpv6_key->icmpv6_code), is_mask);
1710 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1713 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1714 const struct ovs_key_nd *nd_key;
1716 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1717 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1719 sizeof(match->key->ipv6.nd.target),
1721 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1722 nd_key->nd_sll, ETH_ALEN, is_mask);
1723 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1724 nd_key->nd_tll, ETH_ALEN, is_mask);
1725 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1729 OVS_NLERR(log, "Unknown key attributes %llx",
1730 (unsigned long long)attrs);
1737 static void nlattr_set(struct nlattr *attr, u8 val,
1738 const struct ovs_len_tbl *tbl)
1743 /* The nlattr stream should already have been validated */
1744 nla_for_each_nested(nla, attr, rem) {
1745 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1746 nlattr_set(nla, val, tbl[nla_type(nla)].next ? : tbl);
1748 memset(nla_data(nla), val, nla_len(nla));
1750 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1751 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
1755 static void mask_set_nlattr(struct nlattr *attr, u8 val)
1757 nlattr_set(attr, val, ovs_key_lens);
1761 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1762 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1763 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1764 * does not include any don't care bit.
1765 * @net: Used to determine per-namespace field support.
1766 * @match: receives the extracted flow match information.
1767 * @nla_key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1768 * sequence. The fields should of the packet that triggered the creation
1770 * @nla_mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_*
1771 * Netlink attribute specifies the mask field of the wildcarded flow.
1772 * @log: Boolean to allow kernel error logging. Normally true, but when
1773 * probing for feature compatibility this should be passed in as false to
1774 * suppress unnecessary error logging.
1776 int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
1777 const struct nlattr *nla_key,
1778 const struct nlattr *nla_mask,
1781 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1782 struct nlattr *newmask = NULL;
1787 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
1791 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1795 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
1801 /* Create an exact match mask. We need to set to 0xff
1802 * all the 'match->mask' fields that have been touched
1803 * in 'match->key'. We cannot simply memset
1804 * 'match->mask', because padding bytes and fields not
1805 * specified in 'match->key' should be left to 0.
1806 * Instead, we use a stream of netlink attributes,
1807 * copied from 'key' and set to 0xff.
1808 * ovs_key_from_nlattrs() will take care of filling
1809 * 'match->mask' appropriately.
1811 newmask = kmemdup(nla_key,
1812 nla_total_size(nla_len(nla_key)),
1817 mask_set_nlattr(newmask, 0xff);
1819 /* The userspace does not send tunnel attributes that
1820 * are 0, but we should not wildcard them nonetheless.
1822 if (match->key->tun_proto)
1823 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1829 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1833 /* Always match on tci. */
1834 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1835 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
1837 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1841 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1847 if (!match_validate(match, key_attrs, mask_attrs, log))
1855 static size_t get_ufid_len(const struct nlattr *attr, bool log)
1862 len = nla_len(attr);
1863 if (len < 1 || len > MAX_UFID_LENGTH) {
1864 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1865 nla_len(attr), MAX_UFID_LENGTH);
1872 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1873 * or false otherwise.
1875 bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1878 sfid->ufid_len = get_ufid_len(attr, log);
1880 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1882 return sfid->ufid_len;
1885 int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1886 const struct sw_flow_key *key, bool log)
1888 struct sw_flow_key *new_key;
1890 if (ovs_nla_get_ufid(sfid, ufid, log))
1893 /* If UFID was not provided, use unmasked key. */
1894 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1897 memcpy(new_key, key, sizeof(*key));
1898 sfid->unmasked_key = new_key;
1903 u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1905 return attr ? nla_get_u32(attr) : 0;
1909 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1910 * @net: Network namespace.
1911 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1913 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1915 * @attrs: Bit mask for the netlink attributes included in @a.
1916 * @log: Boolean to allow kernel error logging. Normally true, but when
1917 * probing for feature compatibility this should be passed in as false to
1918 * suppress unnecessary error logging.
1920 * This parses a series of Netlink attributes that form a flow key, which must
1921 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1922 * get the metadata, that is, the parts of the flow key that cannot be
1923 * extracted from the packet itself.
1925 * This must be called before the packet key fields are filled in 'key'.
1928 int ovs_nla_get_flow_metadata(struct net *net,
1929 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1930 u64 attrs, struct sw_flow_key *key, bool log)
1932 struct sw_flow_match match;
1934 memset(&match, 0, sizeof(match));
1939 key->ct_orig_proto = 0;
1940 memset(&key->ct, 0, sizeof(key->ct));
1941 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1942 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1944 key->phy.in_port = DP_MAX_PORTS;
1946 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
1949 static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1952 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1954 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1955 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1960 static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
1961 struct sk_buff *skb)
1963 struct nlattr *start;
1965 start = nla_nest_start_noflag(skb, OVS_KEY_ATTR_NSH);
1969 if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
1970 goto nla_put_failure;
1972 if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
1973 if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
1974 sizeof(nsh->context), nsh->context))
1975 goto nla_put_failure;
1978 /* Don't support MD type 2 yet */
1980 nla_nest_end(skb, start);
1988 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1989 const struct sw_flow_key *output, bool is_mask,
1990 struct sk_buff *skb)
1992 struct ovs_key_ethernet *eth_key;
1994 struct nlattr *encap = NULL;
1995 struct nlattr *in_encap = NULL;
1997 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1998 goto nla_put_failure;
2000 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
2001 goto nla_put_failure;
2003 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
2004 goto nla_put_failure;
2006 if ((swkey->tun_proto || is_mask)) {
2007 const void *opts = NULL;
2009 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
2010 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
2012 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
2013 swkey->tun_opts_len, swkey->tun_proto, 0))
2014 goto nla_put_failure;
2017 if (swkey->phy.in_port == DP_MAX_PORTS) {
2018 if (is_mask && (output->phy.in_port == 0xffff))
2019 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
2020 goto nla_put_failure;
2023 upper_u16 = !is_mask ? 0 : 0xffff;
2025 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
2026 (upper_u16 << 16) | output->phy.in_port))
2027 goto nla_put_failure;
2030 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
2031 goto nla_put_failure;
2033 if (ovs_ct_put_key(swkey, output, skb))
2034 goto nla_put_failure;
2036 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
2037 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
2039 goto nla_put_failure;
2041 eth_key = nla_data(nla);
2042 ether_addr_copy(eth_key->eth_src, output->eth.src);
2043 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
2045 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
2046 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
2047 goto nla_put_failure;
2048 encap = nla_nest_start_noflag(skb, OVS_KEY_ATTR_ENCAP);
2049 if (!swkey->eth.vlan.tci)
2052 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
2053 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
2054 goto nla_put_failure;
2055 in_encap = nla_nest_start_noflag(skb,
2056 OVS_KEY_ATTR_ENCAP);
2057 if (!swkey->eth.cvlan.tci)
2062 if (swkey->eth.type == htons(ETH_P_802_2)) {
2064 * Ethertype 802.2 is represented in the netlink with omitted
2065 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
2066 * 0xffff in the mask attribute. Ethertype can also
2069 if (is_mask && output->eth.type)
2070 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
2072 goto nla_put_failure;
2077 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
2078 goto nla_put_failure;
2080 if (eth_type_vlan(swkey->eth.type)) {
2081 /* There are 3 VLAN tags, we don't know anything about the rest
2082 * of the packet, so truncate here.
2084 WARN_ON_ONCE(!(encap && in_encap));
2088 if (swkey->eth.type == htons(ETH_P_IP)) {
2089 struct ovs_key_ipv4 *ipv4_key;
2091 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
2093 goto nla_put_failure;
2094 ipv4_key = nla_data(nla);
2095 ipv4_key->ipv4_src = output->ipv4.addr.src;
2096 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
2097 ipv4_key->ipv4_proto = output->ip.proto;
2098 ipv4_key->ipv4_tos = output->ip.tos;
2099 ipv4_key->ipv4_ttl = output->ip.ttl;
2100 ipv4_key->ipv4_frag = output->ip.frag;
2101 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
2102 struct ovs_key_ipv6 *ipv6_key;
2104 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
2106 goto nla_put_failure;
2107 ipv6_key = nla_data(nla);
2108 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
2109 sizeof(ipv6_key->ipv6_src));
2110 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
2111 sizeof(ipv6_key->ipv6_dst));
2112 ipv6_key->ipv6_label = output->ipv6.label;
2113 ipv6_key->ipv6_proto = output->ip.proto;
2114 ipv6_key->ipv6_tclass = output->ip.tos;
2115 ipv6_key->ipv6_hlimit = output->ip.ttl;
2116 ipv6_key->ipv6_frag = output->ip.frag;
2117 } else if (swkey->eth.type == htons(ETH_P_NSH)) {
2118 if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
2119 goto nla_put_failure;
2120 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
2121 swkey->eth.type == htons(ETH_P_RARP)) {
2122 struct ovs_key_arp *arp_key;
2124 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
2126 goto nla_put_failure;
2127 arp_key = nla_data(nla);
2128 memset(arp_key, 0, sizeof(struct ovs_key_arp));
2129 arp_key->arp_sip = output->ipv4.addr.src;
2130 arp_key->arp_tip = output->ipv4.addr.dst;
2131 arp_key->arp_op = htons(output->ip.proto);
2132 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
2133 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
2134 } else if (eth_p_mpls(swkey->eth.type)) {
2136 struct ovs_key_mpls *mpls_key;
2138 num_labels = hweight_long(output->mpls.num_labels_mask);
2139 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS,
2140 num_labels * sizeof(*mpls_key));
2142 goto nla_put_failure;
2144 mpls_key = nla_data(nla);
2145 for (i = 0; i < num_labels; i++)
2146 mpls_key[i].mpls_lse = output->mpls.lse[i];
2149 if ((swkey->eth.type == htons(ETH_P_IP) ||
2150 swkey->eth.type == htons(ETH_P_IPV6)) &&
2151 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
2153 if (swkey->ip.proto == IPPROTO_TCP) {
2154 struct ovs_key_tcp *tcp_key;
2156 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
2158 goto nla_put_failure;
2159 tcp_key = nla_data(nla);
2160 tcp_key->tcp_src = output->tp.src;
2161 tcp_key->tcp_dst = output->tp.dst;
2162 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
2164 goto nla_put_failure;
2165 } else if (swkey->ip.proto == IPPROTO_UDP) {
2166 struct ovs_key_udp *udp_key;
2168 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
2170 goto nla_put_failure;
2171 udp_key = nla_data(nla);
2172 udp_key->udp_src = output->tp.src;
2173 udp_key->udp_dst = output->tp.dst;
2174 } else if (swkey->ip.proto == IPPROTO_SCTP) {
2175 struct ovs_key_sctp *sctp_key;
2177 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
2179 goto nla_put_failure;
2180 sctp_key = nla_data(nla);
2181 sctp_key->sctp_src = output->tp.src;
2182 sctp_key->sctp_dst = output->tp.dst;
2183 } else if (swkey->eth.type == htons(ETH_P_IP) &&
2184 swkey->ip.proto == IPPROTO_ICMP) {
2185 struct ovs_key_icmp *icmp_key;
2187 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
2189 goto nla_put_failure;
2190 icmp_key = nla_data(nla);
2191 icmp_key->icmp_type = ntohs(output->tp.src);
2192 icmp_key->icmp_code = ntohs(output->tp.dst);
2193 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
2194 swkey->ip.proto == IPPROTO_ICMPV6) {
2195 struct ovs_key_icmpv6 *icmpv6_key;
2197 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
2198 sizeof(*icmpv6_key));
2200 goto nla_put_failure;
2201 icmpv6_key = nla_data(nla);
2202 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
2203 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
2205 if (swkey->tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
2206 swkey->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
2207 struct ovs_key_nd *nd_key;
2209 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
2211 goto nla_put_failure;
2212 nd_key = nla_data(nla);
2213 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
2214 sizeof(nd_key->nd_target));
2215 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
2216 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
2223 nla_nest_end(skb, in_encap);
2225 nla_nest_end(skb, encap);
2233 int ovs_nla_put_key(const struct sw_flow_key *swkey,
2234 const struct sw_flow_key *output, int attr, bool is_mask,
2235 struct sk_buff *skb)
2240 nla = nla_nest_start_noflag(skb, attr);
2243 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
2246 nla_nest_end(skb, nla);
2251 /* Called with ovs_mutex or RCU read lock. */
2252 int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
2254 if (ovs_identifier_is_ufid(&flow->id))
2255 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
2258 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
2259 OVS_FLOW_ATTR_KEY, false, skb);
2262 /* Called with ovs_mutex or RCU read lock. */
2263 int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
2265 return ovs_nla_put_key(&flow->key, &flow->key,
2266 OVS_FLOW_ATTR_KEY, false, skb);
2269 /* Called with ovs_mutex or RCU read lock. */
2270 int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
2272 return ovs_nla_put_key(&flow->key, &flow->mask->key,
2273 OVS_FLOW_ATTR_MASK, true, skb);
2276 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
2278 static struct sw_flow_actions *nla_alloc_flow_actions(int size)
2280 struct sw_flow_actions *sfa;
2282 WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
2284 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
2286 return ERR_PTR(-ENOMEM);
2288 sfa->actions_len = 0;
2292 static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len);
2294 static void ovs_nla_free_check_pkt_len_action(const struct nlattr *action)
2296 const struct nlattr *a;
2299 nla_for_each_nested(a, action, rem) {
2300 switch (nla_type(a)) {
2301 case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL:
2302 case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER:
2303 ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
2309 static void ovs_nla_free_clone_action(const struct nlattr *action)
2311 const struct nlattr *a = nla_data(action);
2312 int rem = nla_len(action);
2314 switch (nla_type(a)) {
2315 case OVS_CLONE_ATTR_EXEC:
2316 /* The real list of actions follows this attribute. */
2317 a = nla_next(a, &rem);
2318 ovs_nla_free_nested_actions(a, rem);
2323 static void ovs_nla_free_dec_ttl_action(const struct nlattr *action)
2325 const struct nlattr *a = nla_data(action);
2327 switch (nla_type(a)) {
2328 case OVS_DEC_TTL_ATTR_ACTION:
2329 ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
2334 static void ovs_nla_free_sample_action(const struct nlattr *action)
2336 const struct nlattr *a = nla_data(action);
2337 int rem = nla_len(action);
2339 switch (nla_type(a)) {
2340 case OVS_SAMPLE_ATTR_ARG:
2341 /* The real list of actions follows this attribute. */
2342 a = nla_next(a, &rem);
2343 ovs_nla_free_nested_actions(a, rem);
2348 static void ovs_nla_free_set_action(const struct nlattr *a)
2350 const struct nlattr *ovs_key = nla_data(a);
2351 struct ovs_tunnel_info *ovs_tun;
2353 switch (nla_type(ovs_key)) {
2354 case OVS_KEY_ATTR_TUNNEL_INFO:
2355 ovs_tun = nla_data(ovs_key);
2356 dst_release((struct dst_entry *)ovs_tun->tun_dst);
2361 static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len)
2363 const struct nlattr *a;
2366 /* Whenever new actions are added, the need to update this
2367 * function should be considered.
2369 BUILD_BUG_ON(OVS_ACTION_ATTR_MAX != 23);
2374 nla_for_each_attr(a, actions, len, rem) {
2375 switch (nla_type(a)) {
2376 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
2377 ovs_nla_free_check_pkt_len_action(a);
2380 case OVS_ACTION_ATTR_CLONE:
2381 ovs_nla_free_clone_action(a);
2384 case OVS_ACTION_ATTR_CT:
2385 ovs_ct_free_action(a);
2388 case OVS_ACTION_ATTR_DEC_TTL:
2389 ovs_nla_free_dec_ttl_action(a);
2392 case OVS_ACTION_ATTR_SAMPLE:
2393 ovs_nla_free_sample_action(a);
2396 case OVS_ACTION_ATTR_SET:
2397 ovs_nla_free_set_action(a);
2403 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2408 ovs_nla_free_nested_actions(sf_acts->actions, sf_acts->actions_len);
2412 static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2414 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2417 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
2418 * The caller must hold rcu_read_lock for this to be sensible. */
2419 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
2421 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
2424 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
2425 int attr_len, bool log)
2428 struct sw_flow_actions *acts;
2430 size_t req_size = NLA_ALIGN(attr_len);
2431 int next_offset = offsetof(struct sw_flow_actions, actions) +
2432 (*sfa)->actions_len;
2434 if (req_size <= (ksize(*sfa) - next_offset))
2437 new_acts_size = max(next_offset + req_size, ksize(*sfa) * 2);
2439 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
2440 if ((next_offset + req_size) > MAX_ACTIONS_BUFSIZE) {
2441 OVS_NLERR(log, "Flow action size exceeds max %u",
2442 MAX_ACTIONS_BUFSIZE);
2443 return ERR_PTR(-EMSGSIZE);
2445 new_acts_size = MAX_ACTIONS_BUFSIZE;
2448 acts = nla_alloc_flow_actions(new_acts_size);
2450 return (void *)acts;
2452 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2453 acts->actions_len = (*sfa)->actions_len;
2454 acts->orig_len = (*sfa)->orig_len;
2459 (*sfa)->actions_len += req_size;
2460 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2463 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
2464 int attrtype, void *data, int len, bool log)
2468 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
2472 a->nla_type = attrtype;
2473 a->nla_len = nla_attr_size(len);
2476 memcpy(nla_data(a), data, len);
2477 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2482 int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2487 a = __add_action(sfa, attrtype, data, len, log);
2489 return PTR_ERR_OR_ZERO(a);
2492 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
2493 int attrtype, bool log)
2495 int used = (*sfa)->actions_len;
2498 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
2505 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2508 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2511 a->nla_len = sfa->actions_len - st_offset;
2514 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
2515 const struct sw_flow_key *key,
2516 struct sw_flow_actions **sfa,
2517 __be16 eth_type, __be16 vlan_tci,
2518 u32 mpls_label_count, bool log,
2521 static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
2522 const struct sw_flow_key *key,
2523 struct sw_flow_actions **sfa,
2524 __be16 eth_type, __be16 vlan_tci,
2525 u32 mpls_label_count, bool log, bool last,
2528 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2529 const struct nlattr *probability, *actions;
2530 const struct nlattr *a;
2531 int rem, start, err;
2532 struct sample_arg arg;
2534 memset(attrs, 0, sizeof(attrs));
2535 nla_for_each_nested(a, attr, rem) {
2536 int type = nla_type(a);
2537 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2544 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2545 if (!probability || nla_len(probability) != sizeof(u32))
2548 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2549 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2552 /* validation done, copy sample action. */
2553 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
2557 /* When both skb and flow may be changed, put the sample
2558 * into a deferred fifo. On the other hand, if only skb
2559 * may be modified, the actions can be executed in place.
2561 * Do this analysis at the flow installation time.
2562 * Set 'clone_action->exec' to true if the actions can be
2563 * executed without being deferred.
2565 * If the sample is the last action, it can always be excuted
2566 * rather than deferred.
2568 arg.exec = last || !actions_may_change_flow(actions);
2569 arg.probability = nla_get_u32(probability);
2571 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2576 err = __ovs_nla_copy_actions(net, actions, key, sfa,
2577 eth_type, vlan_tci, mpls_label_count, log,
2583 add_nested_action_end(*sfa, start);
2588 static int validate_and_copy_dec_ttl(struct net *net,
2589 const struct nlattr *attr,
2590 const struct sw_flow_key *key,
2591 struct sw_flow_actions **sfa,
2592 __be16 eth_type, __be16 vlan_tci,
2593 u32 mpls_label_count, bool log,
2596 const struct nlattr *attrs[OVS_DEC_TTL_ATTR_MAX + 1];
2597 int start, action_start, err, rem;
2598 const struct nlattr *a, *actions;
2600 memset(attrs, 0, sizeof(attrs));
2601 nla_for_each_nested(a, attr, rem) {
2602 int type = nla_type(a);
2604 /* Ignore unknown attributes to be future proof. */
2605 if (type > OVS_DEC_TTL_ATTR_MAX)
2608 if (!type || attrs[type])
2614 actions = attrs[OVS_DEC_TTL_ATTR_ACTION];
2615 if (rem || !actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2618 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_DEC_TTL, log);
2622 action_start = add_nested_action_start(sfa, OVS_DEC_TTL_ATTR_ACTION, log);
2623 if (action_start < 0)
2624 return action_start;
2626 err = __ovs_nla_copy_actions(net, actions, key, sfa, eth_type,
2627 vlan_tci, mpls_label_count, log,
2632 add_nested_action_end(*sfa, action_start);
2633 add_nested_action_end(*sfa, start);
2637 static int validate_and_copy_clone(struct net *net,
2638 const struct nlattr *attr,
2639 const struct sw_flow_key *key,
2640 struct sw_flow_actions **sfa,
2641 __be16 eth_type, __be16 vlan_tci,
2642 u32 mpls_label_count, bool log, bool last,
2648 if (nla_len(attr) && nla_len(attr) < NLA_HDRLEN)
2651 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CLONE, log);
2655 exec = last || !actions_may_change_flow(attr);
2657 err = ovs_nla_add_action(sfa, OVS_CLONE_ATTR_EXEC, &exec,
2662 err = __ovs_nla_copy_actions(net, attr, key, sfa,
2663 eth_type, vlan_tci, mpls_label_count, log,
2668 add_nested_action_end(*sfa, start);
2673 void ovs_match_init(struct sw_flow_match *match,
2674 struct sw_flow_key *key,
2676 struct sw_flow_mask *mask)
2678 memset(match, 0, sizeof(*match));
2683 memset(key, 0, sizeof(*key));
2686 memset(&mask->key, 0, sizeof(mask->key));
2687 mask->range.start = mask->range.end = 0;
2691 static int validate_geneve_opts(struct sw_flow_key *key)
2693 struct geneve_opt *option;
2694 int opts_len = key->tun_opts_len;
2695 bool crit_opt = false;
2697 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2698 while (opts_len > 0) {
2701 if (opts_len < sizeof(*option))
2704 len = sizeof(*option) + option->length * 4;
2708 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2710 option = (struct geneve_opt *)((u8 *)option + len);
2714 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2719 static int validate_and_copy_set_tun(const struct nlattr *attr,
2720 struct sw_flow_actions **sfa, bool log)
2722 struct sw_flow_match match;
2723 struct sw_flow_key key;
2724 struct metadata_dst *tun_dst;
2725 struct ip_tunnel_info *tun_info;
2726 struct ovs_tunnel_info *ovs_tun;
2728 int err = 0, start, opts_type;
2729 __be16 dst_opt_type;
2732 ovs_match_init(&match, &key, true, NULL);
2733 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
2737 if (key.tun_opts_len) {
2738 switch (opts_type) {
2739 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2740 err = validate_geneve_opts(&key);
2743 dst_opt_type = TUNNEL_GENEVE_OPT;
2745 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2746 dst_opt_type = TUNNEL_VXLAN_OPT;
2748 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
2749 dst_opt_type = TUNNEL_ERSPAN_OPT;
2754 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
2758 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2764 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2766 dst_release((struct dst_entry *)tun_dst);
2770 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2771 sizeof(*ovs_tun), log);
2773 dst_release((struct dst_entry *)tun_dst);
2777 ovs_tun = nla_data(a);
2778 ovs_tun->tun_dst = tun_dst;
2780 tun_info = &tun_dst->u.tun_info;
2781 tun_info->mode = IP_TUNNEL_INFO_TX;
2782 if (key.tun_proto == AF_INET6)
2783 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
2784 else if (key.tun_proto == AF_INET && key.tun_key.u.ipv4.dst == 0)
2785 tun_info->mode |= IP_TUNNEL_INFO_BRIDGE;
2786 tun_info->key = key.tun_key;
2788 /* We need to store the options in the action itself since
2789 * everything else will go away after flow setup. We can append
2790 * it to tun_info and then point there.
2792 ip_tunnel_info_opts_set(tun_info,
2793 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2794 key.tun_opts_len, dst_opt_type);
2795 add_nested_action_end(*sfa, start);
2800 static bool validate_nsh(const struct nlattr *attr, bool is_mask,
2801 bool is_push_nsh, bool log)
2803 struct sw_flow_match match;
2804 struct sw_flow_key key;
2807 ovs_match_init(&match, &key, true, NULL);
2808 ret = nsh_key_put_from_nlattr(attr, &match, is_mask,
2813 /* Return false if there are any non-masked bits set.
2814 * Mask follows data immediately, before any netlink padding.
2816 static bool validate_masked(u8 *data, int len)
2818 u8 *mask = data + len;
2821 if (*data++ & ~*mask++)
2827 static int validate_set(const struct nlattr *a,
2828 const struct sw_flow_key *flow_key,
2829 struct sw_flow_actions **sfa, bool *skip_copy,
2830 u8 mac_proto, __be16 eth_type, bool masked, bool log)
2832 const struct nlattr *ovs_key = nla_data(a);
2833 int key_type = nla_type(ovs_key);
2836 /* There can be only one key in a action */
2837 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2840 key_len = nla_len(ovs_key);
2844 if (key_type > OVS_KEY_ATTR_MAX ||
2845 !check_attr_len(key_len, ovs_key_lens[key_type].len))
2848 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2852 case OVS_KEY_ATTR_PRIORITY:
2853 case OVS_KEY_ATTR_SKB_MARK:
2854 case OVS_KEY_ATTR_CT_MARK:
2855 case OVS_KEY_ATTR_CT_LABELS:
2858 case OVS_KEY_ATTR_ETHERNET:
2859 if (mac_proto != MAC_PROTO_ETHERNET)
2863 case OVS_KEY_ATTR_TUNNEL: {
2867 return -EINVAL; /* Masked tunnel set not supported. */
2870 err = validate_and_copy_set_tun(a, sfa, log);
2875 case OVS_KEY_ATTR_IPV4: {
2876 const struct ovs_key_ipv4 *ipv4_key;
2878 if (eth_type != htons(ETH_P_IP))
2881 ipv4_key = nla_data(ovs_key);
2884 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
2886 /* Non-writeable fields. */
2887 if (mask->ipv4_proto || mask->ipv4_frag)
2890 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2893 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2898 case OVS_KEY_ATTR_IPV6: {
2899 const struct ovs_key_ipv6 *ipv6_key;
2901 if (eth_type != htons(ETH_P_IPV6))
2904 ipv6_key = nla_data(ovs_key);
2907 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
2909 /* Non-writeable fields. */
2910 if (mask->ipv6_proto || mask->ipv6_frag)
2913 /* Invalid bits in the flow label mask? */
2914 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2917 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2920 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2923 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2928 case OVS_KEY_ATTR_TCP:
2929 if ((eth_type != htons(ETH_P_IP) &&
2930 eth_type != htons(ETH_P_IPV6)) ||
2931 flow_key->ip.proto != IPPROTO_TCP)
2936 case OVS_KEY_ATTR_UDP:
2937 if ((eth_type != htons(ETH_P_IP) &&
2938 eth_type != htons(ETH_P_IPV6)) ||
2939 flow_key->ip.proto != IPPROTO_UDP)
2944 case OVS_KEY_ATTR_MPLS:
2945 if (!eth_p_mpls(eth_type))
2949 case OVS_KEY_ATTR_SCTP:
2950 if ((eth_type != htons(ETH_P_IP) &&
2951 eth_type != htons(ETH_P_IPV6)) ||
2952 flow_key->ip.proto != IPPROTO_SCTP)
2957 case OVS_KEY_ATTR_NSH:
2958 if (eth_type != htons(ETH_P_NSH))
2960 if (!validate_nsh(nla_data(a), masked, false, log))
2968 /* Convert non-masked non-tunnel set actions to masked set actions. */
2969 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2970 int start, len = key_len * 2;
2975 start = add_nested_action_start(sfa,
2976 OVS_ACTION_ATTR_SET_TO_MASKED,
2981 at = __add_action(sfa, key_type, NULL, len, log);
2985 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2986 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2987 /* Clear non-writeable bits from otherwise writeable fields. */
2988 if (key_type == OVS_KEY_ATTR_IPV6) {
2989 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2991 mask->ipv6_label &= htonl(0x000FFFFF);
2993 add_nested_action_end(*sfa, start);
2999 static int validate_userspace(const struct nlattr *attr)
3001 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
3002 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
3003 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
3004 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
3006 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
3009 error = nla_parse_nested_deprecated(a, OVS_USERSPACE_ATTR_MAX, attr,
3010 userspace_policy, NULL);
3014 if (!a[OVS_USERSPACE_ATTR_PID] ||
3015 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
3021 static const struct nla_policy cpl_policy[OVS_CHECK_PKT_LEN_ATTR_MAX + 1] = {
3022 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = {.type = NLA_U16 },
3023 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = {.type = NLA_NESTED },
3024 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] = {.type = NLA_NESTED },
3027 static int validate_and_copy_check_pkt_len(struct net *net,
3028 const struct nlattr *attr,
3029 const struct sw_flow_key *key,
3030 struct sw_flow_actions **sfa,
3031 __be16 eth_type, __be16 vlan_tci,
3032 u32 mpls_label_count,
3033 bool log, bool last, u32 depth)
3035 const struct nlattr *acts_if_greater, *acts_if_lesser_eq;
3036 struct nlattr *a[OVS_CHECK_PKT_LEN_ATTR_MAX + 1];
3037 struct check_pkt_len_arg arg;
3038 int nested_acts_start;
3041 err = nla_parse_deprecated_strict(a, OVS_CHECK_PKT_LEN_ATTR_MAX,
3042 nla_data(attr), nla_len(attr),
3047 if (!a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] ||
3048 !nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]))
3051 acts_if_lesser_eq = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
3052 acts_if_greater = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
3054 /* Both the nested action should be present. */
3055 if (!acts_if_greater || !acts_if_lesser_eq)
3058 /* validation done, copy the nested actions. */
3059 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CHECK_PKT_LEN,
3064 arg.pkt_len = nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
3065 arg.exec_for_lesser_equal =
3066 last || !actions_may_change_flow(acts_if_lesser_eq);
3067 arg.exec_for_greater =
3068 last || !actions_may_change_flow(acts_if_greater);
3070 err = ovs_nla_add_action(sfa, OVS_CHECK_PKT_LEN_ATTR_ARG, &arg,
3075 nested_acts_start = add_nested_action_start(sfa,
3076 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL, log);
3077 if (nested_acts_start < 0)
3078 return nested_acts_start;
3080 err = __ovs_nla_copy_actions(net, acts_if_lesser_eq, key, sfa,
3081 eth_type, vlan_tci, mpls_label_count, log,
3087 add_nested_action_end(*sfa, nested_acts_start);
3089 nested_acts_start = add_nested_action_start(sfa,
3090 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER, log);
3091 if (nested_acts_start < 0)
3092 return nested_acts_start;
3094 err = __ovs_nla_copy_actions(net, acts_if_greater, key, sfa,
3095 eth_type, vlan_tci, mpls_label_count, log,
3101 add_nested_action_end(*sfa, nested_acts_start);
3102 add_nested_action_end(*sfa, start);
3106 static int copy_action(const struct nlattr *from,
3107 struct sw_flow_actions **sfa, bool log)
3109 int totlen = NLA_ALIGN(from->nla_len);
3112 to = reserve_sfa_size(sfa, from->nla_len, log);
3116 memcpy(to, from, totlen);
3120 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
3121 const struct sw_flow_key *key,
3122 struct sw_flow_actions **sfa,
3123 __be16 eth_type, __be16 vlan_tci,
3124 u32 mpls_label_count, bool log,
3127 u8 mac_proto = ovs_key_mac_proto(key);
3128 const struct nlattr *a;
3131 if (depth > OVS_COPY_ACTIONS_MAX_DEPTH)
3134 nla_for_each_nested(a, attr, rem) {
3135 /* Expected argument lengths, (u32)-1 for variable length. */
3136 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
3137 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
3138 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
3139 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
3140 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
3141 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
3142 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
3143 [OVS_ACTION_ATTR_POP_VLAN] = 0,
3144 [OVS_ACTION_ATTR_SET] = (u32)-1,
3145 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
3146 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
3147 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
3148 [OVS_ACTION_ATTR_CT] = (u32)-1,
3149 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
3150 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
3151 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
3152 [OVS_ACTION_ATTR_POP_ETH] = 0,
3153 [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
3154 [OVS_ACTION_ATTR_POP_NSH] = 0,
3155 [OVS_ACTION_ATTR_METER] = sizeof(u32),
3156 [OVS_ACTION_ATTR_CLONE] = (u32)-1,
3157 [OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
3158 [OVS_ACTION_ATTR_ADD_MPLS] = sizeof(struct ovs_action_add_mpls),
3159 [OVS_ACTION_ATTR_DEC_TTL] = (u32)-1,
3161 const struct ovs_action_push_vlan *vlan;
3162 int type = nla_type(a);
3165 if (type > OVS_ACTION_ATTR_MAX ||
3166 (action_lens[type] != nla_len(a) &&
3167 action_lens[type] != (u32)-1))
3172 case OVS_ACTION_ATTR_UNSPEC:
3175 case OVS_ACTION_ATTR_USERSPACE:
3176 err = validate_userspace(a);
3181 case OVS_ACTION_ATTR_OUTPUT:
3182 if (nla_get_u32(a) >= DP_MAX_PORTS)
3186 case OVS_ACTION_ATTR_TRUNC: {
3187 const struct ovs_action_trunc *trunc = nla_data(a);
3189 if (trunc->max_len < ETH_HLEN)
3194 case OVS_ACTION_ATTR_HASH: {
3195 const struct ovs_action_hash *act_hash = nla_data(a);
3197 switch (act_hash->hash_alg) {
3198 case OVS_HASH_ALG_L4:
3207 case OVS_ACTION_ATTR_POP_VLAN:
3208 if (mac_proto != MAC_PROTO_ETHERNET)
3210 vlan_tci = htons(0);
3213 case OVS_ACTION_ATTR_PUSH_VLAN:
3214 if (mac_proto != MAC_PROTO_ETHERNET)
3217 if (!eth_type_vlan(vlan->vlan_tpid))
3219 if (!(vlan->vlan_tci & htons(VLAN_CFI_MASK)))
3221 vlan_tci = vlan->vlan_tci;
3224 case OVS_ACTION_ATTR_RECIRC:
3227 case OVS_ACTION_ATTR_ADD_MPLS: {
3228 const struct ovs_action_add_mpls *mpls = nla_data(a);
3230 if (!eth_p_mpls(mpls->mpls_ethertype))
3233 if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK) {
3234 if (vlan_tci & htons(VLAN_CFI_MASK) ||
3235 (eth_type != htons(ETH_P_IP) &&
3236 eth_type != htons(ETH_P_IPV6) &&
3237 eth_type != htons(ETH_P_ARP) &&
3238 eth_type != htons(ETH_P_RARP) &&
3239 !eth_p_mpls(eth_type)))
3243 if (mac_proto == MAC_PROTO_ETHERNET) {
3244 mpls_label_count = 1;
3245 mac_proto = MAC_PROTO_NONE;
3250 eth_type = mpls->mpls_ethertype;
3254 case OVS_ACTION_ATTR_PUSH_MPLS: {
3255 const struct ovs_action_push_mpls *mpls = nla_data(a);
3257 if (!eth_p_mpls(mpls->mpls_ethertype))
3259 /* Prohibit push MPLS other than to a white list
3260 * for packets that have a known tag order.
3262 if (vlan_tci & htons(VLAN_CFI_MASK) ||
3263 (eth_type != htons(ETH_P_IP) &&
3264 eth_type != htons(ETH_P_IPV6) &&
3265 eth_type != htons(ETH_P_ARP) &&
3266 eth_type != htons(ETH_P_RARP) &&
3267 !eth_p_mpls(eth_type)))
3269 eth_type = mpls->mpls_ethertype;
3274 case OVS_ACTION_ATTR_POP_MPLS: {
3276 if (vlan_tci & htons(VLAN_CFI_MASK) ||
3277 !eth_p_mpls(eth_type))
3280 /* Disallow subsequent L2.5+ set actions and mpls_pop
3281 * actions once the last MPLS label in the packet is
3282 * is popped as there is no check here to ensure that
3283 * the new eth type is valid and thus set actions could
3284 * write off the end of the packet or otherwise corrupt
3287 * Support for these actions is planned using packet
3290 proto = nla_get_be16(a);
3292 if (proto == htons(ETH_P_TEB) &&
3293 mac_proto != MAC_PROTO_NONE)
3298 if (!eth_p_mpls(proto) || !mpls_label_count)
3299 eth_type = htons(0);
3306 case OVS_ACTION_ATTR_SET:
3307 err = validate_set(a, key, sfa,
3308 &skip_copy, mac_proto, eth_type,
3314 case OVS_ACTION_ATTR_SET_MASKED:
3315 err = validate_set(a, key, sfa,
3316 &skip_copy, mac_proto, eth_type,
3322 case OVS_ACTION_ATTR_SAMPLE: {
3323 bool last = nla_is_last(a, rem);
3325 err = validate_and_copy_sample(net, a, key, sfa,
3335 case OVS_ACTION_ATTR_CT:
3336 err = ovs_ct_copy_action(net, a, key, sfa, log);
3342 case OVS_ACTION_ATTR_CT_CLEAR:
3345 case OVS_ACTION_ATTR_PUSH_ETH:
3346 /* Disallow pushing an Ethernet header if one
3347 * is already present */
3348 if (mac_proto != MAC_PROTO_NONE)
3350 mac_proto = MAC_PROTO_ETHERNET;
3353 case OVS_ACTION_ATTR_POP_ETH:
3354 if (mac_proto != MAC_PROTO_ETHERNET)
3356 if (vlan_tci & htons(VLAN_CFI_MASK))
3358 mac_proto = MAC_PROTO_NONE;
3361 case OVS_ACTION_ATTR_PUSH_NSH:
3362 if (mac_proto != MAC_PROTO_ETHERNET) {
3365 next_proto = tun_p_from_eth_p(eth_type);
3369 mac_proto = MAC_PROTO_NONE;
3370 if (!validate_nsh(nla_data(a), false, true, true))
3374 case OVS_ACTION_ATTR_POP_NSH: {
3377 if (eth_type != htons(ETH_P_NSH))
3379 inner_proto = tun_p_to_eth_p(key->nsh.base.np);
3382 if (key->nsh.base.np == TUN_P_ETHERNET)
3383 mac_proto = MAC_PROTO_ETHERNET;
3385 mac_proto = MAC_PROTO_NONE;
3389 case OVS_ACTION_ATTR_METER:
3390 /* Non-existent meters are simply ignored. */
3393 case OVS_ACTION_ATTR_CLONE: {
3394 bool last = nla_is_last(a, rem);
3396 err = validate_and_copy_clone(net, a, key, sfa,
3406 case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
3407 bool last = nla_is_last(a, rem);
3409 err = validate_and_copy_check_pkt_len(net, a, key, sfa,
3421 case OVS_ACTION_ATTR_DEC_TTL:
3422 err = validate_and_copy_dec_ttl(net, a, key, sfa,
3424 mpls_label_count, log,
3432 OVS_NLERR(log, "Unknown Action type %d", type);
3436 err = copy_action(a, sfa, log);
3448 /* 'key' must be the masked key. */
3449 int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
3450 const struct sw_flow_key *key,
3451 struct sw_flow_actions **sfa, bool log)
3454 u32 mpls_label_count = 0;
3456 *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
3458 return PTR_ERR(*sfa);
3460 if (eth_p_mpls(key->eth.type))
3461 mpls_label_count = hweight_long(key->mpls.num_labels_mask);
3463 (*sfa)->orig_len = nla_len(attr);
3464 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
3465 key->eth.vlan.tci, mpls_label_count, log,
3468 ovs_nla_free_flow_actions(*sfa);
3473 static int sample_action_to_attr(const struct nlattr *attr,
3474 struct sk_buff *skb)
3476 struct nlattr *start, *ac_start = NULL, *sample_arg;
3477 int err = 0, rem = nla_len(attr);
3478 const struct sample_arg *arg;
3479 struct nlattr *actions;
3481 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SAMPLE);
3485 sample_arg = nla_data(attr);
3486 arg = nla_data(sample_arg);
3487 actions = nla_next(sample_arg, &rem);
3489 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
3494 ac_start = nla_nest_start_noflag(skb, OVS_SAMPLE_ATTR_ACTIONS);
3500 err = ovs_nla_put_actions(actions, rem, skb);
3504 nla_nest_cancel(skb, ac_start);
3505 nla_nest_cancel(skb, start);
3507 nla_nest_end(skb, ac_start);
3508 nla_nest_end(skb, start);
3514 static int clone_action_to_attr(const struct nlattr *attr,
3515 struct sk_buff *skb)
3517 struct nlattr *start;
3518 int err = 0, rem = nla_len(attr);
3520 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CLONE);
3524 /* Skipping the OVS_CLONE_ATTR_EXEC that is always the first attribute. */
3525 attr = nla_next(nla_data(attr), &rem);
3526 err = ovs_nla_put_actions(attr, rem, skb);
3529 nla_nest_cancel(skb, start);
3531 nla_nest_end(skb, start);
3536 static int check_pkt_len_action_to_attr(const struct nlattr *attr,
3537 struct sk_buff *skb)
3539 struct nlattr *start, *ac_start = NULL;
3540 const struct check_pkt_len_arg *arg;
3541 const struct nlattr *a, *cpl_arg;
3542 int err = 0, rem = nla_len(attr);
3544 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CHECK_PKT_LEN);
3548 /* The first nested attribute in 'attr' is always
3549 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
3551 cpl_arg = nla_data(attr);
3552 arg = nla_data(cpl_arg);
3554 if (nla_put_u16(skb, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, arg->pkt_len)) {
3559 /* Second nested attribute in 'attr' is always
3560 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
3562 a = nla_next(cpl_arg, &rem);
3563 ac_start = nla_nest_start_noflag(skb,
3564 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
3570 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3572 nla_nest_cancel(skb, ac_start);
3575 nla_nest_end(skb, ac_start);
3578 /* Third nested attribute in 'attr' is always
3579 * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
3581 a = nla_next(a, &rem);
3582 ac_start = nla_nest_start_noflag(skb,
3583 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
3589 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3591 nla_nest_cancel(skb, ac_start);
3594 nla_nest_end(skb, ac_start);
3597 nla_nest_end(skb, start);
3601 nla_nest_cancel(skb, start);
3605 static int dec_ttl_action_to_attr(const struct nlattr *attr,
3606 struct sk_buff *skb)
3608 struct nlattr *start, *action_start;
3609 const struct nlattr *a;
3612 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_DEC_TTL);
3616 nla_for_each_attr(a, nla_data(attr), nla_len(attr), rem) {
3617 switch (nla_type(a)) {
3618 case OVS_DEC_TTL_ATTR_ACTION:
3620 action_start = nla_nest_start_noflag(skb, OVS_DEC_TTL_ATTR_ACTION);
3621 if (!action_start) {
3626 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3630 nla_nest_end(skb, action_start);
3634 /* Ignore all other option to be future compatible */
3639 nla_nest_end(skb, start);
3643 nla_nest_cancel(skb, start);
3647 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
3649 const struct nlattr *ovs_key = nla_data(a);
3650 int key_type = nla_type(ovs_key);
3651 struct nlattr *start;
3655 case OVS_KEY_ATTR_TUNNEL_INFO: {
3656 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
3657 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
3659 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
3663 err = ip_tun_to_nlattr(skb, &tun_info->key,
3664 ip_tunnel_info_opts(tun_info),
3665 tun_info->options_len,
3666 ip_tunnel_info_af(tun_info), tun_info->mode);
3669 nla_nest_end(skb, start);
3673 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
3681 static int masked_set_action_to_set_action_attr(const struct nlattr *a,
3682 struct sk_buff *skb)
3684 const struct nlattr *ovs_key = nla_data(a);
3686 size_t key_len = nla_len(ovs_key) / 2;
3688 /* Revert the conversion we did from a non-masked set action to
3689 * masked set action.
3691 nla = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
3695 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
3698 nla_nest_end(skb, nla);
3702 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
3704 const struct nlattr *a;
3707 nla_for_each_attr(a, attr, len, rem) {
3708 int type = nla_type(a);
3711 case OVS_ACTION_ATTR_SET:
3712 err = set_action_to_attr(a, skb);
3717 case OVS_ACTION_ATTR_SET_TO_MASKED:
3718 err = masked_set_action_to_set_action_attr(a, skb);
3723 case OVS_ACTION_ATTR_SAMPLE:
3724 err = sample_action_to_attr(a, skb);
3729 case OVS_ACTION_ATTR_CT:
3730 err = ovs_ct_action_to_attr(nla_data(a), skb);
3735 case OVS_ACTION_ATTR_CLONE:
3736 err = clone_action_to_attr(a, skb);
3741 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
3742 err = check_pkt_len_action_to_attr(a, skb);
3747 case OVS_ACTION_ATTR_DEC_TTL:
3748 err = dec_ttl_action_to_attr(a, skb);
3754 if (nla_put(skb, type, nla_len(a), nla_data(a)))