2 * Copyright (c) 2007-2017 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>
49 #include <net/tun_proto.h>
50 #include <net/erspan.h>
53 #include "conntrack.h"
55 #include "flow_netlink.h"
60 const struct ovs_len_tbl
*next
;
63 #define OVS_ATTR_NESTED -1
64 #define OVS_ATTR_VARIABLE -2
66 static bool actions_may_change_flow(const struct nlattr
*actions
)
71 nla_for_each_nested(nla
, actions
, rem
) {
72 u16 action
= nla_type(nla
);
75 case OVS_ACTION_ATTR_OUTPUT
:
76 case OVS_ACTION_ATTR_RECIRC
:
77 case OVS_ACTION_ATTR_TRUNC
:
78 case OVS_ACTION_ATTR_USERSPACE
:
81 case OVS_ACTION_ATTR_CT
:
82 case OVS_ACTION_ATTR_CT_CLEAR
:
83 case OVS_ACTION_ATTR_HASH
:
84 case OVS_ACTION_ATTR_POP_ETH
:
85 case OVS_ACTION_ATTR_POP_MPLS
:
86 case OVS_ACTION_ATTR_POP_NSH
:
87 case OVS_ACTION_ATTR_POP_VLAN
:
88 case OVS_ACTION_ATTR_PUSH_ETH
:
89 case OVS_ACTION_ATTR_PUSH_MPLS
:
90 case OVS_ACTION_ATTR_PUSH_NSH
:
91 case OVS_ACTION_ATTR_PUSH_VLAN
:
92 case OVS_ACTION_ATTR_SAMPLE
:
93 case OVS_ACTION_ATTR_SET
:
94 case OVS_ACTION_ATTR_SET_MASKED
:
95 case OVS_ACTION_ATTR_METER
:
96 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
104 static void update_range(struct sw_flow_match
*match
,
105 size_t offset
, size_t size
, bool is_mask
)
107 struct sw_flow_key_range
*range
;
108 size_t start
= rounddown(offset
, sizeof(long));
109 size_t end
= roundup(offset
+ size
, sizeof(long));
112 range
= &match
->range
;
114 range
= &match
->mask
->range
;
116 if (range
->start
== range
->end
) {
117 range
->start
= start
;
122 if (range
->start
> start
)
123 range
->start
= start
;
125 if (range
->end
< end
)
129 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
131 update_range(match, offsetof(struct sw_flow_key, field), \
132 sizeof((match)->key->field), is_mask); \
134 (match)->mask->key.field = value; \
136 (match)->key->field = value; \
139 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
141 update_range(match, offset, len, is_mask); \
143 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
145 memcpy((u8 *)(match)->key + offset, value_p, len); \
148 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
149 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
150 value_p, len, is_mask)
152 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
154 update_range(match, offsetof(struct sw_flow_key, field), \
155 sizeof((match)->key->field), is_mask); \
157 memset((u8 *)&(match)->mask->key.field, value, \
158 sizeof((match)->mask->key.field)); \
160 memset((u8 *)&(match)->key->field, value, \
161 sizeof((match)->key->field)); \
164 static bool match_validate(const struct sw_flow_match
*match
,
165 u64 key_attrs
, u64 mask_attrs
, bool log
)
167 u64 key_expected
= 0;
168 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
170 /* The following mask attributes allowed only if they
171 * pass the validation tests.
173 mask_allowed
&= ~((1ULL << OVS_KEY_ATTR_IPV4
)
174 | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)
175 | (1ULL << OVS_KEY_ATTR_IPV6
)
176 | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)
177 | (1ULL << OVS_KEY_ATTR_TCP
)
178 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)
179 | (1ULL << OVS_KEY_ATTR_UDP
)
180 | (1ULL << OVS_KEY_ATTR_SCTP
)
181 | (1ULL << OVS_KEY_ATTR_ICMP
)
182 | (1ULL << OVS_KEY_ATTR_ICMPV6
)
183 | (1ULL << OVS_KEY_ATTR_ARP
)
184 | (1ULL << OVS_KEY_ATTR_ND
)
185 | (1ULL << OVS_KEY_ATTR_MPLS
)
186 | (1ULL << OVS_KEY_ATTR_NSH
));
188 /* Always allowed mask fields. */
189 mask_allowed
|= ((1ULL << OVS_KEY_ATTR_TUNNEL
)
190 | (1ULL << OVS_KEY_ATTR_IN_PORT
)
191 | (1ULL << OVS_KEY_ATTR_ETHERTYPE
));
193 /* Check key attributes. */
194 if (match
->key
->eth
.type
== htons(ETH_P_ARP
)
195 || match
->key
->eth
.type
== htons(ETH_P_RARP
)) {
196 key_expected
|= 1ULL << OVS_KEY_ATTR_ARP
;
197 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
198 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ARP
;
201 if (eth_p_mpls(match
->key
->eth
.type
)) {
202 key_expected
|= 1ULL << OVS_KEY_ATTR_MPLS
;
203 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
204 mask_allowed
|= 1ULL << OVS_KEY_ATTR_MPLS
;
207 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
208 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV4
;
209 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
210 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV4
;
211 mask_allowed
|= 1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
214 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
215 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
216 key_expected
|= 1ULL << OVS_KEY_ATTR_UDP
;
217 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
218 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
221 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
222 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
223 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
224 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
227 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
228 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
229 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
230 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
231 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
232 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
236 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
237 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMP
;
238 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
239 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ICMP
;
244 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
245 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV6
;
246 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
247 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV6
;
248 mask_allowed
|= 1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
251 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
252 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
253 key_expected
|= 1ULL << OVS_KEY_ATTR_UDP
;
254 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
255 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
258 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
259 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
260 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
261 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
264 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
265 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
266 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
267 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
268 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
269 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
273 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
274 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMPV6
;
275 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
276 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ICMPV6
;
278 if (match
->key
->tp
.src
==
279 htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
280 match
->key
->tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
281 key_expected
|= 1ULL << OVS_KEY_ATTR_ND
;
282 /* Original direction conntrack tuple
283 * uses the same space as the ND fields
284 * in the key, so both are not allowed
287 mask_allowed
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
288 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xff)))
289 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ND
;
295 if (match
->key
->eth
.type
== htons(ETH_P_NSH
)) {
296 key_expected
|= 1 << OVS_KEY_ATTR_NSH
;
298 match
->mask
->key
.eth
.type
== htons(0xffff)) {
299 mask_allowed
|= 1 << OVS_KEY_ATTR_NSH
;
303 if ((key_attrs
& key_expected
) != key_expected
) {
304 /* Key attributes check failed. */
305 OVS_NLERR(log
, "Missing key (keys=%llx, expected=%llx)",
306 (unsigned long long)key_attrs
,
307 (unsigned long long)key_expected
);
311 if ((mask_attrs
& mask_allowed
) != mask_attrs
) {
312 /* Mask attributes check failed. */
313 OVS_NLERR(log
, "Unexpected mask (mask=%llx, allowed=%llx)",
314 (unsigned long long)mask_attrs
,
315 (unsigned long long)mask_allowed
);
322 size_t ovs_tun_key_attr_size(void)
324 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
325 * updating this function.
327 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
328 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
329 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
330 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
331 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
332 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
333 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
334 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
335 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
336 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and
337 * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with
338 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
340 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
341 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
344 static size_t ovs_nsh_key_attr_size(void)
346 /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
347 * updating this function.
349 return nla_total_size(NSH_BASE_HDR_LEN
) /* OVS_NSH_KEY_ATTR_BASE */
350 /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
351 * mutually exclusive, so the bigger one can cover
354 + nla_total_size(NSH_CTX_HDRS_MAX_LEN
);
357 size_t ovs_key_attr_size(void)
359 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
360 * updating this function.
362 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO
!= 29);
364 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
365 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
366 + ovs_tun_key_attr_size()
367 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
368 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
369 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
370 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
371 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
372 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
373 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
374 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
375 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
376 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
377 + ovs_nsh_key_attr_size()
378 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
381 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
383 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
385 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
388 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
389 [OVS_VXLAN_EXT_GBP
] = { .len
= sizeof(u32
) },
392 static const struct ovs_len_tbl ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
393 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= sizeof(u64
) },
394 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= sizeof(u32
) },
395 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= sizeof(u32
) },
396 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
397 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
398 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
399 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
400 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= sizeof(u16
) },
401 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= sizeof(u16
) },
402 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
403 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
404 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= OVS_ATTR_NESTED
,
405 .next
= ovs_vxlan_ext_key_lens
},
406 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= sizeof(struct in6_addr
) },
407 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= sizeof(struct in6_addr
) },
408 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
411 static const struct ovs_len_tbl
412 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
413 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= sizeof(struct ovs_nsh_key_base
) },
414 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= sizeof(struct ovs_nsh_key_md1
) },
415 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= OVS_ATTR_VARIABLE
},
418 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
419 static const struct ovs_len_tbl ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
420 [OVS_KEY_ATTR_ENCAP
] = { .len
= OVS_ATTR_NESTED
},
421 [OVS_KEY_ATTR_PRIORITY
] = { .len
= sizeof(u32
) },
422 [OVS_KEY_ATTR_IN_PORT
] = { .len
= sizeof(u32
) },
423 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= sizeof(u32
) },
424 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
425 [OVS_KEY_ATTR_VLAN
] = { .len
= sizeof(__be16
) },
426 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= sizeof(__be16
) },
427 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
428 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
429 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
430 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= sizeof(__be16
) },
431 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
432 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
433 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
434 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
435 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
436 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
437 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= sizeof(u32
) },
438 [OVS_KEY_ATTR_DP_HASH
] = { .len
= sizeof(u32
) },
439 [OVS_KEY_ATTR_TUNNEL
] = { .len
= OVS_ATTR_NESTED
,
440 .next
= ovs_tunnel_key_lens
, },
441 [OVS_KEY_ATTR_MPLS
] = { .len
= OVS_ATTR_VARIABLE
},
442 [OVS_KEY_ATTR_CT_STATE
] = { .len
= sizeof(u32
) },
443 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= sizeof(u16
) },
444 [OVS_KEY_ATTR_CT_MARK
] = { .len
= sizeof(u32
) },
445 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
446 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = {
447 .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
448 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = {
449 .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
450 [OVS_KEY_ATTR_NSH
] = { .len
= OVS_ATTR_NESTED
,
451 .next
= ovs_nsh_key_attr_lens
, },
454 static bool check_attr_len(unsigned int attr_len
, unsigned int expected_len
)
456 return expected_len
== attr_len
||
457 expected_len
== OVS_ATTR_NESTED
||
458 expected_len
== OVS_ATTR_VARIABLE
;
461 static bool is_all_zero(const u8
*fp
, size_t size
)
468 for (i
= 0; i
< size
; i
++)
475 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
476 const struct nlattr
*a
[],
477 u64
*attrsp
, bool log
, bool nz
)
479 const struct nlattr
*nla
;
484 nla_for_each_nested(nla
, attr
, rem
) {
485 u16 type
= nla_type(nla
);
488 if (type
> OVS_KEY_ATTR_MAX
) {
489 OVS_NLERR(log
, "Key type %d is out of range max %d",
490 type
, OVS_KEY_ATTR_MAX
);
494 if (attrs
& (1ULL << type
)) {
495 OVS_NLERR(log
, "Duplicate key (type %d).", type
);
499 expected_len
= ovs_key_lens
[type
].len
;
500 if (!check_attr_len(nla_len(nla
), expected_len
)) {
501 OVS_NLERR(log
, "Key %d has unexpected len %d expected %d",
502 type
, nla_len(nla
), expected_len
);
506 if (!nz
|| !is_all_zero(nla_data(nla
), nla_len(nla
))) {
507 attrs
|= 1ULL << type
;
512 OVS_NLERR(log
, "Message has %d unknown bytes.", rem
);
520 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
521 const struct nlattr
*a
[], u64
*attrsp
,
524 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, true);
527 int parse_flow_nlattrs(const struct nlattr
*attr
, const struct nlattr
*a
[],
528 u64
*attrsp
, bool log
)
530 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, false);
533 static int genev_tun_opt_from_nlattr(const struct nlattr
*a
,
534 struct sw_flow_match
*match
, bool is_mask
,
537 unsigned long opt_key_offset
;
539 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
540 OVS_NLERR(log
, "Geneve option length err (len %d, max %zu).",
541 nla_len(a
), sizeof(match
->key
->tun_opts
));
545 if (nla_len(a
) % 4 != 0) {
546 OVS_NLERR(log
, "Geneve opt len %d is not a multiple of 4.",
551 /* We need to record the length of the options passed
552 * down, otherwise packets with the same format but
553 * additional options will be silently matched.
556 SW_FLOW_KEY_PUT(match
, tun_opts_len
, nla_len(a
),
559 /* This is somewhat unusual because it looks at
560 * both the key and mask while parsing the
561 * attributes (and by extension assumes the key
562 * is parsed first). Normally, we would verify
563 * that each is the correct length and that the
564 * attributes line up in the validate function.
565 * However, that is difficult because this is
566 * variable length and we won't have the
569 if (match
->key
->tun_opts_len
!= nla_len(a
)) {
570 OVS_NLERR(log
, "Geneve option len %d != mask len %d",
571 match
->key
->tun_opts_len
, nla_len(a
));
575 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
578 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
579 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
580 nla_len(a
), is_mask
);
584 static int vxlan_tun_opt_from_nlattr(const struct nlattr
*attr
,
585 struct sw_flow_match
*match
, bool is_mask
,
590 unsigned long opt_key_offset
;
591 struct vxlan_metadata opts
;
593 BUILD_BUG_ON(sizeof(opts
) > sizeof(match
->key
->tun_opts
));
595 memset(&opts
, 0, sizeof(opts
));
596 nla_for_each_nested(a
, attr
, rem
) {
597 int type
= nla_type(a
);
599 if (type
> OVS_VXLAN_EXT_MAX
) {
600 OVS_NLERR(log
, "VXLAN extension %d out of range max %d",
601 type
, OVS_VXLAN_EXT_MAX
);
605 if (!check_attr_len(nla_len(a
),
606 ovs_vxlan_ext_key_lens
[type
].len
)) {
607 OVS_NLERR(log
, "VXLAN extension %d has unexpected len %d expected %d",
609 ovs_vxlan_ext_key_lens
[type
].len
);
614 case OVS_VXLAN_EXT_GBP
:
615 opts
.gbp
= nla_get_u32(a
);
618 OVS_NLERR(log
, "Unknown VXLAN extension attribute %d",
624 OVS_NLERR(log
, "VXLAN extension message has %d unknown bytes.",
630 SW_FLOW_KEY_PUT(match
, tun_opts_len
, sizeof(opts
), false);
632 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
634 opt_key_offset
= TUN_METADATA_OFFSET(sizeof(opts
));
635 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, &opts
, sizeof(opts
),
640 static int erspan_tun_opt_from_nlattr(const struct nlattr
*a
,
641 struct sw_flow_match
*match
, bool is_mask
,
644 unsigned long opt_key_offset
;
646 BUILD_BUG_ON(sizeof(struct erspan_metadata
) >
647 sizeof(match
->key
->tun_opts
));
649 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
650 OVS_NLERR(log
, "ERSPAN option length err (len %d, max %zu).",
651 nla_len(a
), sizeof(match
->key
->tun_opts
));
656 SW_FLOW_KEY_PUT(match
, tun_opts_len
,
657 sizeof(struct erspan_metadata
), false);
659 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
661 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
662 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
663 nla_len(a
), is_mask
);
667 static int ip_tun_from_nlattr(const struct nlattr
*attr
,
668 struct sw_flow_match
*match
, bool is_mask
,
671 bool ttl
= false, ipv4
= false, ipv6
= false;
672 __be16 tun_flags
= 0;
677 nla_for_each_nested(a
, attr
, rem
) {
678 int type
= nla_type(a
);
681 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
682 OVS_NLERR(log
, "Tunnel attr %d out of range max %d",
683 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
687 if (!check_attr_len(nla_len(a
),
688 ovs_tunnel_key_lens
[type
].len
)) {
689 OVS_NLERR(log
, "Tunnel attr %d has unexpected len %d expected %d",
690 type
, nla_len(a
), ovs_tunnel_key_lens
[type
].len
);
695 case OVS_TUNNEL_KEY_ATTR_ID
:
696 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
697 nla_get_be64(a
), is_mask
);
698 tun_flags
|= TUNNEL_KEY
;
700 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
701 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.src
,
702 nla_get_in_addr(a
), is_mask
);
705 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
706 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.dst
,
707 nla_get_in_addr(a
), is_mask
);
710 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
711 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.src
,
712 nla_get_in6_addr(a
), is_mask
);
715 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
716 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.dst
,
717 nla_get_in6_addr(a
), is_mask
);
720 case OVS_TUNNEL_KEY_ATTR_TOS
:
721 SW_FLOW_KEY_PUT(match
, tun_key
.tos
,
722 nla_get_u8(a
), is_mask
);
724 case OVS_TUNNEL_KEY_ATTR_TTL
:
725 SW_FLOW_KEY_PUT(match
, tun_key
.ttl
,
726 nla_get_u8(a
), is_mask
);
729 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
730 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
732 case OVS_TUNNEL_KEY_ATTR_CSUM
:
733 tun_flags
|= TUNNEL_CSUM
;
735 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
736 SW_FLOW_KEY_PUT(match
, tun_key
.tp_src
,
737 nla_get_be16(a
), is_mask
);
739 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
740 SW_FLOW_KEY_PUT(match
, tun_key
.tp_dst
,
741 nla_get_be16(a
), is_mask
);
743 case OVS_TUNNEL_KEY_ATTR_OAM
:
744 tun_flags
|= TUNNEL_OAM
;
746 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
748 OVS_NLERR(log
, "Multiple metadata blocks provided");
752 err
= genev_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
756 tun_flags
|= TUNNEL_GENEVE_OPT
;
759 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
761 OVS_NLERR(log
, "Multiple metadata blocks provided");
765 err
= vxlan_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
769 tun_flags
|= TUNNEL_VXLAN_OPT
;
772 case OVS_TUNNEL_KEY_ATTR_PAD
:
774 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
776 OVS_NLERR(log
, "Multiple metadata blocks provided");
780 err
= erspan_tun_opt_from_nlattr(a
, match
, is_mask
,
785 tun_flags
|= TUNNEL_ERSPAN_OPT
;
789 OVS_NLERR(log
, "Unknown IP tunnel attribute %d",
795 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
797 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_proto
, 0xff, true);
799 SW_FLOW_KEY_PUT(match
, tun_proto
, ipv6
? AF_INET6
: AF_INET
,
803 OVS_NLERR(log
, "IP tunnel attribute has %d unknown bytes.",
809 OVS_NLERR(log
, "Mixed IPv4 and IPv6 tunnel attributes");
814 if (!ipv4
&& !ipv6
) {
815 OVS_NLERR(log
, "IP tunnel dst address not specified");
818 if (ipv4
&& !match
->key
->tun_key
.u
.ipv4
.dst
) {
819 OVS_NLERR(log
, "IPv4 tunnel dst address is zero");
822 if (ipv6
&& ipv6_addr_any(&match
->key
->tun_key
.u
.ipv6
.dst
)) {
823 OVS_NLERR(log
, "IPv6 tunnel dst address is zero");
828 OVS_NLERR(log
, "IP tunnel TTL not specified.");
836 static int vxlan_opt_to_nlattr(struct sk_buff
*skb
,
837 const void *tun_opts
, int swkey_tun_opts_len
)
839 const struct vxlan_metadata
*opts
= tun_opts
;
842 nla
= nla_nest_start_noflag(skb
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
846 if (nla_put_u32(skb
, OVS_VXLAN_EXT_GBP
, opts
->gbp
) < 0)
849 nla_nest_end(skb
, nla
);
853 static int __ip_tun_to_nlattr(struct sk_buff
*skb
,
854 const struct ip_tunnel_key
*output
,
855 const void *tun_opts
, int swkey_tun_opts_len
,
856 unsigned short tun_proto
)
858 if (output
->tun_flags
& TUNNEL_KEY
&&
859 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
,
860 OVS_TUNNEL_KEY_ATTR_PAD
))
864 if (output
->u
.ipv4
.src
&&
865 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
,
868 if (output
->u
.ipv4
.dst
&&
869 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
,
874 if (!ipv6_addr_any(&output
->u
.ipv6
.src
) &&
875 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
,
876 &output
->u
.ipv6
.src
))
878 if (!ipv6_addr_any(&output
->u
.ipv6
.dst
) &&
879 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
,
880 &output
->u
.ipv6
.dst
))
885 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->tos
))
887 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ttl
))
889 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
890 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
892 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
893 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
895 if (output
->tp_src
&&
896 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, output
->tp_src
))
898 if (output
->tp_dst
&&
899 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_DST
, output
->tp_dst
))
901 if ((output
->tun_flags
& TUNNEL_OAM
) &&
902 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_OAM
))
904 if (swkey_tun_opts_len
) {
905 if (output
->tun_flags
& TUNNEL_GENEVE_OPT
&&
906 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
907 swkey_tun_opts_len
, tun_opts
))
909 else if (output
->tun_flags
& TUNNEL_VXLAN_OPT
&&
910 vxlan_opt_to_nlattr(skb
, tun_opts
, swkey_tun_opts_len
))
912 else if (output
->tun_flags
& TUNNEL_ERSPAN_OPT
&&
913 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
914 swkey_tun_opts_len
, tun_opts
))
921 static int ip_tun_to_nlattr(struct sk_buff
*skb
,
922 const struct ip_tunnel_key
*output
,
923 const void *tun_opts
, int swkey_tun_opts_len
,
924 unsigned short tun_proto
)
929 nla
= nla_nest_start_noflag(skb
, OVS_KEY_ATTR_TUNNEL
);
933 err
= __ip_tun_to_nlattr(skb
, output
, tun_opts
, swkey_tun_opts_len
,
938 nla_nest_end(skb
, nla
);
942 int ovs_nla_put_tunnel_info(struct sk_buff
*skb
,
943 struct ip_tunnel_info
*tun_info
)
945 return __ip_tun_to_nlattr(skb
, &tun_info
->key
,
946 ip_tunnel_info_opts(tun_info
),
947 tun_info
->options_len
,
948 ip_tunnel_info_af(tun_info
));
951 static int encode_vlan_from_nlattrs(struct sw_flow_match
*match
,
952 const struct nlattr
*a
[],
953 bool is_mask
, bool inner
)
958 if (a
[OVS_KEY_ATTR_VLAN
])
959 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
961 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
962 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
964 if (likely(!inner
)) {
965 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tpid
, tpid
, is_mask
);
966 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, tci
, is_mask
);
968 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tpid
, tpid
, is_mask
);
969 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, tci
, is_mask
);
974 static int validate_vlan_from_nlattrs(const struct sw_flow_match
*match
,
975 u64 key_attrs
, bool inner
,
976 const struct nlattr
**a
, bool log
)
980 if (!((key_attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) &&
981 (key_attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) &&
982 eth_type_vlan(nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
])))) {
987 if (!((key_attrs
& (1 << OVS_KEY_ATTR_VLAN
)) &&
988 (key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
)))) {
989 OVS_NLERR(log
, "Invalid %s frame", (inner
) ? "C-VLAN" : "VLAN");
993 if (a
[OVS_KEY_ATTR_VLAN
])
994 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
996 if (!(tci
& htons(VLAN_CFI_MASK
))) {
998 OVS_NLERR(log
, "%s TCI does not have VLAN_CFI_MASK bit set.",
999 (inner
) ? "C-VLAN" : "VLAN");
1001 } else if (nla_len(a
[OVS_KEY_ATTR_ENCAP
])) {
1002 /* Corner case for truncated VLAN header. */
1003 OVS_NLERR(log
, "Truncated %s header has non-zero encap attribute.",
1004 (inner
) ? "C-VLAN" : "VLAN");
1012 static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match
*match
,
1013 u64 key_attrs
, bool inner
,
1014 const struct nlattr
**a
, bool log
)
1018 bool encap_valid
= !!(match
->key
->eth
.vlan
.tci
&
1019 htons(VLAN_CFI_MASK
));
1020 bool i_encap_valid
= !!(match
->key
->eth
.cvlan
.tci
&
1021 htons(VLAN_CFI_MASK
));
1023 if (!(key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
))) {
1028 if ((!inner
&& !encap_valid
) || (inner
&& !i_encap_valid
)) {
1029 OVS_NLERR(log
, "Encap mask attribute is set for non-%s frame.",
1030 (inner
) ? "C-VLAN" : "VLAN");
1034 if (a
[OVS_KEY_ATTR_VLAN
])
1035 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1037 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
1038 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1040 if (tpid
!= htons(0xffff)) {
1041 OVS_NLERR(log
, "Must have an exact match on %s TPID (mask=%x).",
1042 (inner
) ? "C-VLAN" : "VLAN", ntohs(tpid
));
1045 if (!(tci
& htons(VLAN_CFI_MASK
))) {
1046 OVS_NLERR(log
, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
1047 (inner
) ? "C-VLAN" : "VLAN");
1054 static int __parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
1055 u64
*key_attrs
, bool inner
,
1056 const struct nlattr
**a
, bool is_mask
,
1060 const struct nlattr
*encap
;
1063 err
= validate_vlan_from_nlattrs(match
, *key_attrs
, inner
,
1066 err
= validate_vlan_mask_from_nlattrs(match
, *key_attrs
, inner
,
1071 err
= encode_vlan_from_nlattrs(match
, a
, is_mask
, inner
);
1075 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
1076 *key_attrs
&= ~(1 << OVS_KEY_ATTR_VLAN
);
1077 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1079 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1082 err
= parse_flow_nlattrs(encap
, a
, key_attrs
, log
);
1084 err
= parse_flow_mask_nlattrs(encap
, a
, key_attrs
, log
);
1089 static int parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
1090 u64
*key_attrs
, const struct nlattr
**a
,
1091 bool is_mask
, bool log
)
1094 bool encap_valid
= false;
1096 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, false, a
,
1101 encap_valid
= !!(match
->key
->eth
.vlan
.tci
& htons(VLAN_CFI_MASK
));
1103 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, true, a
,
1112 static int parse_eth_type_from_nlattrs(struct sw_flow_match
*match
,
1113 u64
*attrs
, const struct nlattr
**a
,
1114 bool is_mask
, bool log
)
1118 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1120 /* Always exact match EtherType. */
1121 eth_type
= htons(0xffff);
1122 } else if (!eth_proto_is_802_3(eth_type
)) {
1123 OVS_NLERR(log
, "EtherType %x is less than min %x",
1124 ntohs(eth_type
), ETH_P_802_3_MIN
);
1128 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
1129 *attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1133 static int metadata_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1134 u64
*attrs
, const struct nlattr
**a
,
1135 bool is_mask
, bool log
)
1137 u8 mac_proto
= MAC_PROTO_ETHERNET
;
1139 if (*attrs
& (1ULL << OVS_KEY_ATTR_DP_HASH
)) {
1140 u32 hash_val
= nla_get_u32(a
[OVS_KEY_ATTR_DP_HASH
]);
1142 SW_FLOW_KEY_PUT(match
, ovs_flow_hash
, hash_val
, is_mask
);
1143 *attrs
&= ~(1ULL << OVS_KEY_ATTR_DP_HASH
);
1146 if (*attrs
& (1ULL << OVS_KEY_ATTR_RECIRC_ID
)) {
1147 u32 recirc_id
= nla_get_u32(a
[OVS_KEY_ATTR_RECIRC_ID
]);
1149 SW_FLOW_KEY_PUT(match
, recirc_id
, recirc_id
, is_mask
);
1150 *attrs
&= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID
);
1153 if (*attrs
& (1ULL << OVS_KEY_ATTR_PRIORITY
)) {
1154 SW_FLOW_KEY_PUT(match
, phy
.priority
,
1155 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
1156 *attrs
&= ~(1ULL << OVS_KEY_ATTR_PRIORITY
);
1159 if (*attrs
& (1ULL << OVS_KEY_ATTR_IN_PORT
)) {
1160 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
1163 in_port
= 0xffffffff; /* Always exact match in_port. */
1164 } else if (in_port
>= DP_MAX_PORTS
) {
1165 OVS_NLERR(log
, "Port %d exceeds max allowable %d",
1166 in_port
, DP_MAX_PORTS
);
1170 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
1171 *attrs
&= ~(1ULL << OVS_KEY_ATTR_IN_PORT
);
1172 } else if (!is_mask
) {
1173 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
1176 if (*attrs
& (1ULL << OVS_KEY_ATTR_SKB_MARK
)) {
1177 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
1179 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
1180 *attrs
&= ~(1ULL << OVS_KEY_ATTR_SKB_MARK
);
1182 if (*attrs
& (1ULL << OVS_KEY_ATTR_TUNNEL
)) {
1183 if (ip_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
1186 *attrs
&= ~(1ULL << OVS_KEY_ATTR_TUNNEL
);
1189 if (*attrs
& (1 << OVS_KEY_ATTR_CT_STATE
) &&
1190 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_STATE
)) {
1191 u32 ct_state
= nla_get_u32(a
[OVS_KEY_ATTR_CT_STATE
]);
1193 if (ct_state
& ~CT_SUPPORTED_MASK
) {
1194 OVS_NLERR(log
, "ct_state flags %08x unsupported",
1199 SW_FLOW_KEY_PUT(match
, ct_state
, ct_state
, is_mask
);
1200 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_STATE
);
1202 if (*attrs
& (1 << OVS_KEY_ATTR_CT_ZONE
) &&
1203 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_ZONE
)) {
1204 u16 ct_zone
= nla_get_u16(a
[OVS_KEY_ATTR_CT_ZONE
]);
1206 SW_FLOW_KEY_PUT(match
, ct_zone
, ct_zone
, is_mask
);
1207 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ZONE
);
1209 if (*attrs
& (1 << OVS_KEY_ATTR_CT_MARK
) &&
1210 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_MARK
)) {
1211 u32 mark
= nla_get_u32(a
[OVS_KEY_ATTR_CT_MARK
]);
1213 SW_FLOW_KEY_PUT(match
, ct
.mark
, mark
, is_mask
);
1214 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_MARK
);
1216 if (*attrs
& (1 << OVS_KEY_ATTR_CT_LABELS
) &&
1217 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_LABELS
)) {
1218 const struct ovs_key_ct_labels
*cl
;
1220 cl
= nla_data(a
[OVS_KEY_ATTR_CT_LABELS
]);
1221 SW_FLOW_KEY_MEMCPY(match
, ct
.labels
, cl
->ct_labels
,
1222 sizeof(*cl
), is_mask
);
1223 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_LABELS
);
1225 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
1226 const struct ovs_key_ct_tuple_ipv4
*ct
;
1228 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
1230 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.src
, ct
->ipv4_src
, is_mask
);
1231 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.dst
, ct
->ipv4_dst
, is_mask
);
1232 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1233 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1234 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv4_proto
, is_mask
);
1235 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
1237 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
1238 const struct ovs_key_ct_tuple_ipv6
*ct
;
1240 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
1242 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.src
, &ct
->ipv6_src
,
1243 sizeof(match
->key
->ipv6
.ct_orig
.src
),
1245 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.dst
, &ct
->ipv6_dst
,
1246 sizeof(match
->key
->ipv6
.ct_orig
.dst
),
1248 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1249 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1250 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv6_proto
, is_mask
);
1251 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
1254 /* For layer 3 packets the Ethernet type is provided
1255 * and treated as metadata but no MAC addresses are provided.
1257 if (!(*attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) &&
1258 (*attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)))
1259 mac_proto
= MAC_PROTO_NONE
;
1261 /* Always exact match mac_proto */
1262 SW_FLOW_KEY_PUT(match
, mac_proto
, is_mask
? 0xff : mac_proto
, is_mask
);
1264 if (mac_proto
== MAC_PROTO_NONE
)
1265 return parse_eth_type_from_nlattrs(match
, attrs
, a
, is_mask
,
1271 int nsh_hdr_from_nlattr(const struct nlattr
*attr
,
1272 struct nshhdr
*nh
, size_t size
)
1280 /* validate_nsh has check this, so we needn't do duplicate check here
1282 if (size
< NSH_BASE_HDR_LEN
)
1285 nla_for_each_nested(a
, attr
, rem
) {
1286 int type
= nla_type(a
);
1289 case OVS_NSH_KEY_ATTR_BASE
: {
1290 const struct ovs_nsh_key_base
*base
= nla_data(a
);
1292 flags
= base
->flags
;
1295 nh
->mdtype
= base
->mdtype
;
1296 nh
->path_hdr
= base
->path_hdr
;
1299 case OVS_NSH_KEY_ATTR_MD1
:
1301 if (mdlen
> size
- NSH_BASE_HDR_LEN
)
1303 memcpy(&nh
->md1
, nla_data(a
), mdlen
);
1306 case OVS_NSH_KEY_ATTR_MD2
:
1308 if (mdlen
> size
- NSH_BASE_HDR_LEN
)
1310 memcpy(&nh
->md2
, nla_data(a
), mdlen
);
1318 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1319 nh
->ver_flags_ttl_len
= 0;
1320 nsh_set_flags_ttl_len(nh
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
1325 int nsh_key_from_nlattr(const struct nlattr
*attr
,
1326 struct ovs_key_nsh
*nsh
, struct ovs_key_nsh
*nsh_mask
)
1331 /* validate_nsh has check this, so we needn't do duplicate check here
1333 nla_for_each_nested(a
, attr
, rem
) {
1334 int type
= nla_type(a
);
1337 case OVS_NSH_KEY_ATTR_BASE
: {
1338 const struct ovs_nsh_key_base
*base
= nla_data(a
);
1339 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1342 nsh_mask
->base
= *base_mask
;
1345 case OVS_NSH_KEY_ATTR_MD1
: {
1346 const struct ovs_nsh_key_md1
*md1
= nla_data(a
);
1347 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1349 memcpy(nsh
->context
, md1
->context
, sizeof(*md1
));
1350 memcpy(nsh_mask
->context
, md1_mask
->context
,
1354 case OVS_NSH_KEY_ATTR_MD2
:
1355 /* Not supported yet */
1365 static int nsh_key_put_from_nlattr(const struct nlattr
*attr
,
1366 struct sw_flow_match
*match
, bool is_mask
,
1367 bool is_push_nsh
, bool log
)
1371 bool has_base
= false;
1372 bool has_md1
= false;
1373 bool has_md2
= false;
1377 if (WARN_ON(is_push_nsh
&& is_mask
))
1380 nla_for_each_nested(a
, attr
, rem
) {
1381 int type
= nla_type(a
);
1384 if (type
> OVS_NSH_KEY_ATTR_MAX
) {
1385 OVS_NLERR(log
, "nsh attr %d is out of range max %d",
1386 type
, OVS_NSH_KEY_ATTR_MAX
);
1390 if (!check_attr_len(nla_len(a
),
1391 ovs_nsh_key_attr_lens
[type
].len
)) {
1394 "nsh attr %d has unexpected len %d expected %d",
1397 ovs_nsh_key_attr_lens
[type
].len
1403 case OVS_NSH_KEY_ATTR_BASE
: {
1404 const struct ovs_nsh_key_base
*base
= nla_data(a
);
1407 mdtype
= base
->mdtype
;
1408 SW_FLOW_KEY_PUT(match
, nsh
.base
.flags
,
1409 base
->flags
, is_mask
);
1410 SW_FLOW_KEY_PUT(match
, nsh
.base
.ttl
,
1411 base
->ttl
, is_mask
);
1412 SW_FLOW_KEY_PUT(match
, nsh
.base
.mdtype
,
1413 base
->mdtype
, is_mask
);
1414 SW_FLOW_KEY_PUT(match
, nsh
.base
.np
,
1416 SW_FLOW_KEY_PUT(match
, nsh
.base
.path_hdr
,
1417 base
->path_hdr
, is_mask
);
1420 case OVS_NSH_KEY_ATTR_MD1
: {
1421 const struct ovs_nsh_key_md1
*md1
= nla_data(a
);
1424 for (i
= 0; i
< NSH_MD1_CONTEXT_SIZE
; i
++)
1425 SW_FLOW_KEY_PUT(match
, nsh
.context
[i
],
1426 md1
->context
[i
], is_mask
);
1429 case OVS_NSH_KEY_ATTR_MD2
:
1430 if (!is_push_nsh
) /* Not supported MD type 2 yet */
1435 if (mdlen
> NSH_CTX_HDRS_MAX_LEN
|| mdlen
<= 0) {
1438 "Invalid MD length %d for MD type %d",
1446 OVS_NLERR(log
, "Unknown nsh attribute %d",
1453 OVS_NLERR(log
, "nsh attribute has %d unknown bytes.", rem
);
1457 if (has_md1
&& has_md2
) {
1460 "invalid nsh attribute: md1 and md2 are exclusive."
1466 if ((has_md1
&& mdtype
!= NSH_M_TYPE1
) ||
1467 (has_md2
&& mdtype
!= NSH_M_TYPE2
)) {
1468 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1474 (!has_base
|| (!has_md1
&& !has_md2
))) {
1477 "push_nsh: missing base or metadata attributes"
1486 static int ovs_key_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1487 u64 attrs
, const struct nlattr
**a
,
1488 bool is_mask
, bool log
)
1492 err
= metadata_from_nlattrs(net
, match
, &attrs
, a
, is_mask
, log
);
1496 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) {
1497 const struct ovs_key_ethernet
*eth_key
;
1499 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
1500 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
1501 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
1502 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
1503 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
1504 attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERNET
);
1506 if (attrs
& (1ULL << OVS_KEY_ATTR_VLAN
)) {
1507 /* VLAN attribute is always parsed before getting here since it
1508 * may occur multiple times.
1510 OVS_NLERR(log
, "VLAN attribute unexpected.");
1514 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)) {
1515 err
= parse_eth_type_from_nlattrs(match
, &attrs
, a
, is_mask
,
1519 } else if (!is_mask
) {
1520 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
1522 } else if (!match
->key
->eth
.type
) {
1523 OVS_NLERR(log
, "Either Ethernet header or EtherType is required.");
1527 if (attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
1528 const struct ovs_key_ipv4
*ipv4_key
;
1530 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
1531 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
1532 OVS_NLERR(log
, "IPv4 frag type %d is out of range max %d",
1533 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
1536 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1537 ipv4_key
->ipv4_proto
, is_mask
);
1538 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1539 ipv4_key
->ipv4_tos
, is_mask
);
1540 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1541 ipv4_key
->ipv4_ttl
, is_mask
);
1542 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1543 ipv4_key
->ipv4_frag
, is_mask
);
1544 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1545 ipv4_key
->ipv4_src
, is_mask
);
1546 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1547 ipv4_key
->ipv4_dst
, is_mask
);
1548 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
1551 if (attrs
& (1ULL << OVS_KEY_ATTR_IPV6
)) {
1552 const struct ovs_key_ipv6
*ipv6_key
;
1554 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
1555 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
1556 OVS_NLERR(log
, "IPv6 frag type %d is out of range max %d",
1557 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
1561 if (!is_mask
&& ipv6_key
->ipv6_label
& htonl(0xFFF00000)) {
1562 OVS_NLERR(log
, "IPv6 flow label %x is out of range (max=%x)",
1563 ntohl(ipv6_key
->ipv6_label
), (1 << 20) - 1);
1567 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
1568 ipv6_key
->ipv6_label
, is_mask
);
1569 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1570 ipv6_key
->ipv6_proto
, is_mask
);
1571 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1572 ipv6_key
->ipv6_tclass
, is_mask
);
1573 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1574 ipv6_key
->ipv6_hlimit
, is_mask
);
1575 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1576 ipv6_key
->ipv6_frag
, is_mask
);
1577 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
1579 sizeof(match
->key
->ipv6
.addr
.src
),
1581 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
1583 sizeof(match
->key
->ipv6
.addr
.dst
),
1586 attrs
&= ~(1ULL << OVS_KEY_ATTR_IPV6
);
1589 if (attrs
& (1ULL << OVS_KEY_ATTR_ARP
)) {
1590 const struct ovs_key_arp
*arp_key
;
1592 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
1593 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
1594 OVS_NLERR(log
, "Unknown ARP opcode (opcode=%d).",
1599 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1600 arp_key
->arp_sip
, is_mask
);
1601 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1602 arp_key
->arp_tip
, is_mask
);
1603 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1604 ntohs(arp_key
->arp_op
), is_mask
);
1605 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
1606 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
1607 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
1608 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
1610 attrs
&= ~(1ULL << OVS_KEY_ATTR_ARP
);
1613 if (attrs
& (1 << OVS_KEY_ATTR_NSH
)) {
1614 if (nsh_key_put_from_nlattr(a
[OVS_KEY_ATTR_NSH
], match
,
1615 is_mask
, false, log
) < 0)
1617 attrs
&= ~(1 << OVS_KEY_ATTR_NSH
);
1620 if (attrs
& (1ULL << OVS_KEY_ATTR_MPLS
)) {
1621 const struct ovs_key_mpls
*mpls_key
;
1623 u32 label_count
, label_count_mask
, i
;
1626 mpls_key
= nla_data(a
[OVS_KEY_ATTR_MPLS
]);
1627 hdr_len
= nla_len(a
[OVS_KEY_ATTR_MPLS
]);
1628 label_count
= hdr_len
/ sizeof(struct ovs_key_mpls
);
1630 if (label_count
== 0 || label_count
> MPLS_LABEL_DEPTH
||
1631 hdr_len
% sizeof(struct ovs_key_mpls
))
1634 label_count_mask
= GENMASK(label_count
- 1, 0);
1636 for (i
= 0 ; i
< label_count
; i
++)
1637 SW_FLOW_KEY_PUT(match
, mpls
.lse
[i
],
1638 mpls_key
[i
].mpls_lse
, is_mask
);
1640 SW_FLOW_KEY_PUT(match
, mpls
.num_labels_mask
,
1641 label_count_mask
, is_mask
);
1644 attrs
&= ~(1ULL << OVS_KEY_ATTR_MPLS
);
1647 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP
)) {
1648 const struct ovs_key_tcp
*tcp_key
;
1650 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
1651 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
1652 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
1653 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP
);
1656 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)) {
1657 SW_FLOW_KEY_PUT(match
, tp
.flags
,
1658 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
1660 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS
);
1663 if (attrs
& (1ULL << OVS_KEY_ATTR_UDP
)) {
1664 const struct ovs_key_udp
*udp_key
;
1666 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
1667 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
1668 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
1669 attrs
&= ~(1ULL << OVS_KEY_ATTR_UDP
);
1672 if (attrs
& (1ULL << OVS_KEY_ATTR_SCTP
)) {
1673 const struct ovs_key_sctp
*sctp_key
;
1675 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
1676 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
1677 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
1678 attrs
&= ~(1ULL << OVS_KEY_ATTR_SCTP
);
1681 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMP
)) {
1682 const struct ovs_key_icmp
*icmp_key
;
1684 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
1685 SW_FLOW_KEY_PUT(match
, tp
.src
,
1686 htons(icmp_key
->icmp_type
), is_mask
);
1687 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1688 htons(icmp_key
->icmp_code
), is_mask
);
1689 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMP
);
1692 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMPV6
)) {
1693 const struct ovs_key_icmpv6
*icmpv6_key
;
1695 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
1696 SW_FLOW_KEY_PUT(match
, tp
.src
,
1697 htons(icmpv6_key
->icmpv6_type
), is_mask
);
1698 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1699 htons(icmpv6_key
->icmpv6_code
), is_mask
);
1700 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMPV6
);
1703 if (attrs
& (1ULL << OVS_KEY_ATTR_ND
)) {
1704 const struct ovs_key_nd
*nd_key
;
1706 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
1707 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
1709 sizeof(match
->key
->ipv6
.nd
.target
),
1711 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
1712 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
1713 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
1714 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
1715 attrs
&= ~(1ULL << OVS_KEY_ATTR_ND
);
1719 OVS_NLERR(log
, "Unknown key attributes %llx",
1720 (unsigned long long)attrs
);
1727 static void nlattr_set(struct nlattr
*attr
, u8 val
,
1728 const struct ovs_len_tbl
*tbl
)
1733 /* The nlattr stream should already have been validated */
1734 nla_for_each_nested(nla
, attr
, rem
) {
1735 if (tbl
[nla_type(nla
)].len
== OVS_ATTR_NESTED
)
1736 nlattr_set(nla
, val
, tbl
[nla_type(nla
)].next
? : tbl
);
1738 memset(nla_data(nla
), val
, nla_len(nla
));
1740 if (nla_type(nla
) == OVS_KEY_ATTR_CT_STATE
)
1741 *(u32
*)nla_data(nla
) &= CT_SUPPORTED_MASK
;
1745 static void mask_set_nlattr(struct nlattr
*attr
, u8 val
)
1747 nlattr_set(attr
, val
, ovs_key_lens
);
1751 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1752 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1753 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1754 * does not include any don't care bit.
1755 * @net: Used to determine per-namespace field support.
1756 * @match: receives the extracted flow match information.
1757 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1758 * sequence. The fields should of the packet that triggered the creation
1760 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1761 * attribute specifies the mask field of the wildcarded flow.
1762 * @log: Boolean to allow kernel error logging. Normally true, but when
1763 * probing for feature compatibility this should be passed in as false to
1764 * suppress unnecessary error logging.
1766 int ovs_nla_get_match(struct net
*net
, struct sw_flow_match
*match
,
1767 const struct nlattr
*nla_key
,
1768 const struct nlattr
*nla_mask
,
1771 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1772 struct nlattr
*newmask
= NULL
;
1777 err
= parse_flow_nlattrs(nla_key
, a
, &key_attrs
, log
);
1781 err
= parse_vlan_from_nlattrs(match
, &key_attrs
, a
, false, log
);
1785 err
= ovs_key_from_nlattrs(net
, match
, key_attrs
, a
, false, log
);
1791 /* Create an exact match mask. We need to set to 0xff
1792 * all the 'match->mask' fields that have been touched
1793 * in 'match->key'. We cannot simply memset
1794 * 'match->mask', because padding bytes and fields not
1795 * specified in 'match->key' should be left to 0.
1796 * Instead, we use a stream of netlink attributes,
1797 * copied from 'key' and set to 0xff.
1798 * ovs_key_from_nlattrs() will take care of filling
1799 * 'match->mask' appropriately.
1801 newmask
= kmemdup(nla_key
,
1802 nla_total_size(nla_len(nla_key
)),
1807 mask_set_nlattr(newmask
, 0xff);
1809 /* The userspace does not send tunnel attributes that
1810 * are 0, but we should not wildcard them nonetheless.
1812 if (match
->key
->tun_proto
)
1813 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_key
,
1819 err
= parse_flow_mask_nlattrs(nla_mask
, a
, &mask_attrs
, log
);
1823 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, htons(0xffff), true);
1824 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, htons(0xffff), true);
1826 err
= parse_vlan_from_nlattrs(match
, &mask_attrs
, a
, true, log
);
1830 err
= ovs_key_from_nlattrs(net
, match
, mask_attrs
, a
, true,
1836 if (!match_validate(match
, key_attrs
, mask_attrs
, log
))
1844 static size_t get_ufid_len(const struct nlattr
*attr
, bool log
)
1851 len
= nla_len(attr
);
1852 if (len
< 1 || len
> MAX_UFID_LENGTH
) {
1853 OVS_NLERR(log
, "ufid size %u bytes exceeds the range (1, %d)",
1854 nla_len(attr
), MAX_UFID_LENGTH
);
1861 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1862 * or false otherwise.
1864 bool ovs_nla_get_ufid(struct sw_flow_id
*sfid
, const struct nlattr
*attr
,
1867 sfid
->ufid_len
= get_ufid_len(attr
, log
);
1869 memcpy(sfid
->ufid
, nla_data(attr
), sfid
->ufid_len
);
1871 return sfid
->ufid_len
;
1874 int ovs_nla_get_identifier(struct sw_flow_id
*sfid
, const struct nlattr
*ufid
,
1875 const struct sw_flow_key
*key
, bool log
)
1877 struct sw_flow_key
*new_key
;
1879 if (ovs_nla_get_ufid(sfid
, ufid
, log
))
1882 /* If UFID was not provided, use unmasked key. */
1883 new_key
= kmalloc(sizeof(*new_key
), GFP_KERNEL
);
1886 memcpy(new_key
, key
, sizeof(*key
));
1887 sfid
->unmasked_key
= new_key
;
1892 u32
ovs_nla_get_ufid_flags(const struct nlattr
*attr
)
1894 return attr
? nla_get_u32(attr
) : 0;
1898 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1899 * @net: Network namespace.
1900 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1902 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1904 * @attrs: Bit mask for the netlink attributes included in @a.
1905 * @log: Boolean to allow kernel error logging. Normally true, but when
1906 * probing for feature compatibility this should be passed in as false to
1907 * suppress unnecessary error logging.
1909 * This parses a series of Netlink attributes that form a flow key, which must
1910 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1911 * get the metadata, that is, the parts of the flow key that cannot be
1912 * extracted from the packet itself.
1914 * This must be called before the packet key fields are filled in 'key'.
1917 int ovs_nla_get_flow_metadata(struct net
*net
,
1918 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1],
1919 u64 attrs
, struct sw_flow_key
*key
, bool log
)
1921 struct sw_flow_match match
;
1923 memset(&match
, 0, sizeof(match
));
1928 key
->ct_orig_proto
= 0;
1929 memset(&key
->ct
, 0, sizeof(key
->ct
));
1930 memset(&key
->ipv4
.ct_orig
, 0, sizeof(key
->ipv4
.ct_orig
));
1931 memset(&key
->ipv6
.ct_orig
, 0, sizeof(key
->ipv6
.ct_orig
));
1933 key
->phy
.in_port
= DP_MAX_PORTS
;
1935 return metadata_from_nlattrs(net
, &match
, &attrs
, a
, false, log
);
1938 static int ovs_nla_put_vlan(struct sk_buff
*skb
, const struct vlan_head
*vh
,
1941 __be16 eth_type
= !is_mask
? vh
->tpid
: htons(0xffff);
1943 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
1944 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, vh
->tci
))
1949 static int nsh_key_to_nlattr(const struct ovs_key_nsh
*nsh
, bool is_mask
,
1950 struct sk_buff
*skb
)
1952 struct nlattr
*start
;
1954 start
= nla_nest_start_noflag(skb
, OVS_KEY_ATTR_NSH
);
1958 if (nla_put(skb
, OVS_NSH_KEY_ATTR_BASE
, sizeof(nsh
->base
), &nsh
->base
))
1959 goto nla_put_failure
;
1961 if (is_mask
|| nsh
->base
.mdtype
== NSH_M_TYPE1
) {
1962 if (nla_put(skb
, OVS_NSH_KEY_ATTR_MD1
,
1963 sizeof(nsh
->context
), nsh
->context
))
1964 goto nla_put_failure
;
1967 /* Don't support MD type 2 yet */
1969 nla_nest_end(skb
, start
);
1977 static int __ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1978 const struct sw_flow_key
*output
, bool is_mask
,
1979 struct sk_buff
*skb
)
1981 struct ovs_key_ethernet
*eth_key
;
1983 struct nlattr
*encap
= NULL
;
1984 struct nlattr
*in_encap
= NULL
;
1986 if (nla_put_u32(skb
, OVS_KEY_ATTR_RECIRC_ID
, output
->recirc_id
))
1987 goto nla_put_failure
;
1989 if (nla_put_u32(skb
, OVS_KEY_ATTR_DP_HASH
, output
->ovs_flow_hash
))
1990 goto nla_put_failure
;
1992 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
1993 goto nla_put_failure
;
1995 if ((swkey
->tun_proto
|| is_mask
)) {
1996 const void *opts
= NULL
;
1998 if (output
->tun_key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
)
1999 opts
= TUN_METADATA_OPTS(output
, swkey
->tun_opts_len
);
2001 if (ip_tun_to_nlattr(skb
, &output
->tun_key
, opts
,
2002 swkey
->tun_opts_len
, swkey
->tun_proto
))
2003 goto nla_put_failure
;
2006 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
2007 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
2008 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
2009 goto nla_put_failure
;
2012 upper_u16
= !is_mask
? 0 : 0xffff;
2014 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
2015 (upper_u16
<< 16) | output
->phy
.in_port
))
2016 goto nla_put_failure
;
2019 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
2020 goto nla_put_failure
;
2022 if (ovs_ct_put_key(swkey
, output
, skb
))
2023 goto nla_put_failure
;
2025 if (ovs_key_mac_proto(swkey
) == MAC_PROTO_ETHERNET
) {
2026 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
2028 goto nla_put_failure
;
2030 eth_key
= nla_data(nla
);
2031 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
2032 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
2034 if (swkey
->eth
.vlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
2035 if (ovs_nla_put_vlan(skb
, &output
->eth
.vlan
, is_mask
))
2036 goto nla_put_failure
;
2037 encap
= nla_nest_start_noflag(skb
, OVS_KEY_ATTR_ENCAP
);
2038 if (!swkey
->eth
.vlan
.tci
)
2041 if (swkey
->eth
.cvlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
2042 if (ovs_nla_put_vlan(skb
, &output
->eth
.cvlan
, is_mask
))
2043 goto nla_put_failure
;
2044 in_encap
= nla_nest_start_noflag(skb
,
2045 OVS_KEY_ATTR_ENCAP
);
2046 if (!swkey
->eth
.cvlan
.tci
)
2051 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
2053 * Ethertype 802.2 is represented in the netlink with omitted
2054 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
2055 * 0xffff in the mask attribute. Ethertype can also
2058 if (is_mask
&& output
->eth
.type
)
2059 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
2061 goto nla_put_failure
;
2066 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
2067 goto nla_put_failure
;
2069 if (eth_type_vlan(swkey
->eth
.type
)) {
2070 /* There are 3 VLAN tags, we don't know anything about the rest
2071 * of the packet, so truncate here.
2073 WARN_ON_ONCE(!(encap
&& in_encap
));
2077 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
2078 struct ovs_key_ipv4
*ipv4_key
;
2080 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
2082 goto nla_put_failure
;
2083 ipv4_key
= nla_data(nla
);
2084 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
2085 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
2086 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
2087 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
2088 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
2089 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
2090 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
2091 struct ovs_key_ipv6
*ipv6_key
;
2093 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
2095 goto nla_put_failure
;
2096 ipv6_key
= nla_data(nla
);
2097 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
2098 sizeof(ipv6_key
->ipv6_src
));
2099 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
2100 sizeof(ipv6_key
->ipv6_dst
));
2101 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
2102 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
2103 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
2104 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
2105 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
2106 } else if (swkey
->eth
.type
== htons(ETH_P_NSH
)) {
2107 if (nsh_key_to_nlattr(&output
->nsh
, is_mask
, skb
))
2108 goto nla_put_failure
;
2109 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
2110 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
2111 struct ovs_key_arp
*arp_key
;
2113 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
2115 goto nla_put_failure
;
2116 arp_key
= nla_data(nla
);
2117 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
2118 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
2119 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
2120 arp_key
->arp_op
= htons(output
->ip
.proto
);
2121 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
2122 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
2123 } else if (eth_p_mpls(swkey
->eth
.type
)) {
2125 struct ovs_key_mpls
*mpls_key
;
2127 num_labels
= hweight_long(output
->mpls
.num_labels_mask
);
2128 nla
= nla_reserve(skb
, OVS_KEY_ATTR_MPLS
,
2129 num_labels
* sizeof(*mpls_key
));
2131 goto nla_put_failure
;
2133 mpls_key
= nla_data(nla
);
2134 for (i
= 0; i
< num_labels
; i
++)
2135 mpls_key
[i
].mpls_lse
= output
->mpls
.lse
[i
];
2138 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
2139 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
2140 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
2142 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
2143 struct ovs_key_tcp
*tcp_key
;
2145 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
2147 goto nla_put_failure
;
2148 tcp_key
= nla_data(nla
);
2149 tcp_key
->tcp_src
= output
->tp
.src
;
2150 tcp_key
->tcp_dst
= output
->tp
.dst
;
2151 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
2153 goto nla_put_failure
;
2154 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
2155 struct ovs_key_udp
*udp_key
;
2157 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
2159 goto nla_put_failure
;
2160 udp_key
= nla_data(nla
);
2161 udp_key
->udp_src
= output
->tp
.src
;
2162 udp_key
->udp_dst
= output
->tp
.dst
;
2163 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
2164 struct ovs_key_sctp
*sctp_key
;
2166 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
2168 goto nla_put_failure
;
2169 sctp_key
= nla_data(nla
);
2170 sctp_key
->sctp_src
= output
->tp
.src
;
2171 sctp_key
->sctp_dst
= output
->tp
.dst
;
2172 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
2173 swkey
->ip
.proto
== IPPROTO_ICMP
) {
2174 struct ovs_key_icmp
*icmp_key
;
2176 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
2178 goto nla_put_failure
;
2179 icmp_key
= nla_data(nla
);
2180 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
2181 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
2182 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
2183 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
2184 struct ovs_key_icmpv6
*icmpv6_key
;
2186 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
2187 sizeof(*icmpv6_key
));
2189 goto nla_put_failure
;
2190 icmpv6_key
= nla_data(nla
);
2191 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
2192 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
2194 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
2195 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
2196 struct ovs_key_nd
*nd_key
;
2198 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
2200 goto nla_put_failure
;
2201 nd_key
= nla_data(nla
);
2202 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
2203 sizeof(nd_key
->nd_target
));
2204 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
2205 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
2212 nla_nest_end(skb
, in_encap
);
2214 nla_nest_end(skb
, encap
);
2222 int ovs_nla_put_key(const struct sw_flow_key
*swkey
,
2223 const struct sw_flow_key
*output
, int attr
, bool is_mask
,
2224 struct sk_buff
*skb
)
2229 nla
= nla_nest_start_noflag(skb
, attr
);
2232 err
= __ovs_nla_put_key(swkey
, output
, is_mask
, skb
);
2235 nla_nest_end(skb
, nla
);
2240 /* Called with ovs_mutex or RCU read lock. */
2241 int ovs_nla_put_identifier(const struct sw_flow
*flow
, struct sk_buff
*skb
)
2243 if (ovs_identifier_is_ufid(&flow
->id
))
2244 return nla_put(skb
, OVS_FLOW_ATTR_UFID
, flow
->id
.ufid_len
,
2247 return ovs_nla_put_key(flow
->id
.unmasked_key
, flow
->id
.unmasked_key
,
2248 OVS_FLOW_ATTR_KEY
, false, skb
);
2251 /* Called with ovs_mutex or RCU read lock. */
2252 int ovs_nla_put_masked_key(const struct sw_flow
*flow
, struct sk_buff
*skb
)
2254 return ovs_nla_put_key(&flow
->key
, &flow
->key
,
2255 OVS_FLOW_ATTR_KEY
, false, skb
);
2258 /* Called with ovs_mutex or RCU read lock. */
2259 int ovs_nla_put_mask(const struct sw_flow
*flow
, struct sk_buff
*skb
)
2261 return ovs_nla_put_key(&flow
->key
, &flow
->mask
->key
,
2262 OVS_FLOW_ATTR_MASK
, true, skb
);
2265 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,9,0)
2266 #define MAX_ACTIONS_BUFSIZE (16 * 1024)
2268 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
2271 static struct sw_flow_actions
*nla_alloc_flow_actions(int size
)
2273 struct sw_flow_actions
*sfa
;
2275 WARN_ON_ONCE(size
> MAX_ACTIONS_BUFSIZE
);
2277 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
2279 return ERR_PTR(-ENOMEM
);
2281 sfa
->actions_len
= 0;
2285 static void ovs_nla_free_set_action(const struct nlattr
*a
)
2287 const struct nlattr
*ovs_key
= nla_data(a
);
2288 struct ovs_tunnel_info
*ovs_tun
;
2290 switch (nla_type(ovs_key
)) {
2291 case OVS_KEY_ATTR_TUNNEL_INFO
:
2292 ovs_tun
= nla_data(ovs_key
);
2293 ovs_dst_release((struct dst_entry
*)ovs_tun
->tun_dst
);
2298 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
2300 const struct nlattr
*a
;
2306 nla_for_each_attr(a
, sf_acts
->actions
, sf_acts
->actions_len
, rem
) {
2307 switch (nla_type(a
)) {
2308 case OVS_ACTION_ATTR_SET
:
2309 ovs_nla_free_set_action(a
);
2311 case OVS_ACTION_ATTR_CT
:
2312 ovs_ct_free_action(a
);
2320 static void __ovs_nla_free_flow_actions(struct rcu_head
*head
)
2322 ovs_nla_free_flow_actions(container_of(head
, struct sw_flow_actions
, rcu
));
2325 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
2326 * The caller must hold rcu_read_lock for this to be sensible. */
2327 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions
*sf_acts
)
2329 call_rcu(&sf_acts
->rcu
, __ovs_nla_free_flow_actions
);
2332 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
2333 int attr_len
, bool log
)
2336 struct sw_flow_actions
*acts
;
2338 size_t req_size
= NLA_ALIGN(attr_len
);
2339 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
2340 (*sfa
)->actions_len
;
2342 if (req_size
<= (ksize(*sfa
) - next_offset
))
2345 new_acts_size
= max(next_offset
+ req_size
, ksize(*sfa
) * 2);
2347 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
2348 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
) {
2349 OVS_NLERR(log
, "Flow action size exceeds max %u",
2350 MAX_ACTIONS_BUFSIZE
);
2351 return ERR_PTR(-EMSGSIZE
);
2353 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
2356 acts
= nla_alloc_flow_actions(new_acts_size
);
2358 return (void *)acts
;
2360 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
2361 acts
->actions_len
= (*sfa
)->actions_len
;
2362 acts
->orig_len
= (*sfa
)->orig_len
;
2367 (*sfa
)->actions_len
+= req_size
;
2368 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
2371 static struct nlattr
*__add_action(struct sw_flow_actions
**sfa
,
2372 int attrtype
, void *data
, int len
, bool log
)
2376 a
= reserve_sfa_size(sfa
, nla_attr_size(len
), log
);
2380 a
->nla_type
= attrtype
;
2381 a
->nla_len
= nla_attr_size(len
);
2384 memcpy(nla_data(a
), data
, len
);
2385 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
2390 int ovs_nla_add_action(struct sw_flow_actions
**sfa
, int attrtype
, void *data
,
2395 a
= __add_action(sfa
, attrtype
, data
, len
, log
);
2397 return PTR_ERR_OR_ZERO(a
);
2400 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
2401 int attrtype
, bool log
)
2403 int used
= (*sfa
)->actions_len
;
2406 err
= ovs_nla_add_action(sfa
, attrtype
, NULL
, 0, log
);
2413 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
2416 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
2419 a
->nla_len
= sfa
->actions_len
- st_offset
;
2422 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2423 const struct sw_flow_key
*key
,
2424 struct sw_flow_actions
**sfa
,
2425 __be16 eth_type
, __be16 vlan_tci
,
2426 u32 mpls_label_count
, bool log
);
2428 static int validate_and_copy_sample(struct net
*net
, const struct nlattr
*attr
,
2429 const struct sw_flow_key
*key
,
2430 struct sw_flow_actions
**sfa
,
2431 __be16 eth_type
, __be16 vlan_tci
,
2432 u32 mpls_label_count
, bool log
, bool last
)
2434 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
2435 const struct nlattr
*probability
, *actions
;
2436 const struct nlattr
*a
;
2437 int rem
, start
, err
;
2438 struct sample_arg arg
;
2440 memset(attrs
, 0, sizeof(attrs
));
2441 nla_for_each_nested(a
, attr
, rem
) {
2442 int type
= nla_type(a
);
2443 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
2450 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
2451 if (!probability
|| nla_len(probability
) != sizeof(u32
))
2454 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
2455 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
2458 /* validation done, copy sample action. */
2459 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
, log
);
2463 /* When both skb and flow may be changed, put the sample
2464 * into a deferred fifo. On the other hand, if only skb
2465 * may be modified, the actions can be executed in place.
2467 * Do this analysis at the flow installation time.
2468 * Set 'clone_action->exec' to true if the actions can be
2469 * executed without being deferred.
2471 * If the sample is the last action, it can always be excuted
2472 * rather than deferred.
2474 arg
.exec
= last
|| !actions_may_change_flow(actions
);
2475 arg
.probability
= nla_get_u32(probability
);
2477 err
= ovs_nla_add_action(sfa
, OVS_SAMPLE_ATTR_ARG
, &arg
, sizeof(arg
),
2482 err
= __ovs_nla_copy_actions(net
, actions
, key
, sfa
,
2483 eth_type
, vlan_tci
, mpls_label_count
, log
);
2488 add_nested_action_end(*sfa
, start
);
2493 static int validate_and_copy_clone(struct net
*net
,
2494 const struct nlattr
*attr
,
2495 const struct sw_flow_key
*key
,
2496 struct sw_flow_actions
**sfa
,
2497 __be16 eth_type
, __be16 vlan_tci
,
2498 u32 mpls_label_count
, bool log
, bool last
)
2503 if (nla_len(attr
) && nla_len(attr
) < NLA_HDRLEN
)
2506 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_CLONE
, log
);
2510 exec
= last
|| !actions_may_change_flow(attr
);
2512 err
= ovs_nla_add_action(sfa
, OVS_CLONE_ATTR_EXEC
, &exec
,
2517 err
= __ovs_nla_copy_actions(net
, attr
, key
, sfa
,
2518 eth_type
, vlan_tci
, mpls_label_count
, log
);
2522 add_nested_action_end(*sfa
, start
);
2527 void ovs_match_init(struct sw_flow_match
*match
,
2528 struct sw_flow_key
*key
,
2530 struct sw_flow_mask
*mask
)
2532 memset(match
, 0, sizeof(*match
));
2537 memset(key
, 0, sizeof(*key
));
2540 memset(&mask
->key
, 0, sizeof(mask
->key
));
2541 mask
->range
.start
= mask
->range
.end
= 0;
2545 static int validate_geneve_opts(struct sw_flow_key
*key
)
2547 struct geneve_opt
*option
;
2548 int opts_len
= key
->tun_opts_len
;
2549 bool crit_opt
= false;
2551 option
= (struct geneve_opt
*)TUN_METADATA_OPTS(key
, key
->tun_opts_len
);
2552 while (opts_len
> 0) {
2555 if (opts_len
< sizeof(*option
))
2558 len
= sizeof(*option
) + option
->length
* 4;
2562 crit_opt
|= !!(option
->type
& GENEVE_CRIT_OPT_TYPE
);
2564 option
= (struct geneve_opt
*)((u8
*)option
+ len
);
2568 key
->tun_key
.tun_flags
|= crit_opt
? TUNNEL_CRIT_OPT
: 0;
2573 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
2574 struct sw_flow_actions
**sfa
, bool log
)
2576 struct sw_flow_match match
;
2577 struct sw_flow_key key
;
2578 struct metadata_dst
*tun_dst
;
2579 struct ip_tunnel_info
*tun_info
;
2580 struct ovs_tunnel_info
*ovs_tun
;
2582 int err
= 0, start
, opts_type
;
2583 __be16 dst_opt_type
;
2586 ovs_match_init(&match
, &key
, true, NULL
);
2587 opts_type
= ip_tun_from_nlattr(nla_data(attr
), &match
, false, log
);
2591 if (key
.tun_opts_len
) {
2592 switch (opts_type
) {
2593 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2594 err
= validate_geneve_opts(&key
);
2597 dst_opt_type
= TUNNEL_GENEVE_OPT
;
2599 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2600 dst_opt_type
= TUNNEL_VXLAN_OPT
;
2602 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
2603 dst_opt_type
= TUNNEL_ERSPAN_OPT
;
2608 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
, log
);
2612 tun_dst
= metadata_dst_alloc(key
.tun_opts_len
, METADATA_IP_TUNNEL
,
2618 err
= dst_cache_init(&tun_dst
->u
.tun_info
.dst_cache
, GFP_KERNEL
);
2620 dst_release((struct dst_entry
*)tun_dst
);
2623 a
= __add_action(sfa
, OVS_KEY_ATTR_TUNNEL_INFO
, NULL
,
2624 sizeof(*ovs_tun
), log
);
2626 ovs_dst_release((struct dst_entry
*)tun_dst
);
2630 ovs_tun
= nla_data(a
);
2631 ovs_tun
->tun_dst
= tun_dst
;
2633 tun_info
= &tun_dst
->u
.tun_info
;
2634 tun_info
->mode
= IP_TUNNEL_INFO_TX
;
2635 if (key
.tun_proto
== AF_INET6
)
2636 tun_info
->mode
|= IP_TUNNEL_INFO_IPV6
;
2637 tun_info
->key
= key
.tun_key
;
2639 /* We need to store the options in the action itself since
2640 * everything else will go away after flow setup. We can append
2641 * it to tun_info and then point there.
2643 ip_tunnel_info_opts_set(tun_info
,
2644 TUN_METADATA_OPTS(&key
, key
.tun_opts_len
),
2645 key
.tun_opts_len
, dst_opt_type
);
2646 add_nested_action_end(*sfa
, start
);
2651 static bool validate_nsh(const struct nlattr
*attr
, bool is_mask
,
2652 bool is_push_nsh
, bool log
)
2654 struct sw_flow_match match
;
2655 struct sw_flow_key key
;
2658 ovs_match_init(&match
, &key
, true, NULL
);
2659 ret
= nsh_key_put_from_nlattr(attr
, &match
, is_mask
,
2664 /* Return false if there are any non-masked bits set.
2665 * Mask follows data immediately, before any netlink padding.
2667 static bool validate_masked(u8
*data
, int len
)
2669 u8
*mask
= data
+ len
;
2672 if (*data
++ & ~*mask
++)
2678 static int validate_set(const struct nlattr
*a
,
2679 const struct sw_flow_key
*flow_key
,
2680 struct sw_flow_actions
**sfa
, bool *skip_copy
,
2681 u8 mac_proto
, __be16 eth_type
, bool masked
, bool log
)
2683 const struct nlattr
*ovs_key
= nla_data(a
);
2684 int key_type
= nla_type(ovs_key
);
2687 /* There can be only one key in a action */
2688 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
2691 key_len
= nla_len(ovs_key
);
2695 if (key_type
> OVS_KEY_ATTR_MAX
||
2696 !check_attr_len(key_len
, ovs_key_lens
[key_type
].len
))
2699 if (masked
&& !validate_masked(nla_data(ovs_key
), key_len
))
2703 case OVS_KEY_ATTR_PRIORITY
:
2704 case OVS_KEY_ATTR_SKB_MARK
:
2705 case OVS_KEY_ATTR_CT_MARK
:
2706 case OVS_KEY_ATTR_CT_LABELS
:
2709 case OVS_KEY_ATTR_ETHERNET
:
2710 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2714 case OVS_KEY_ATTR_TUNNEL
: {
2717 #ifndef USE_UPSTREAM_TUNNEL
2718 if (eth_p_mpls(eth_type
))
2722 return -EINVAL
; /* Masked tunnel set not supported. */
2725 err
= validate_and_copy_set_tun(a
, sfa
, log
);
2730 case OVS_KEY_ATTR_IPV4
: {
2731 const struct ovs_key_ipv4
*ipv4_key
;
2733 if (eth_type
!= htons(ETH_P_IP
))
2736 ipv4_key
= nla_data(ovs_key
);
2739 const struct ovs_key_ipv4
*mask
= ipv4_key
+ 1;
2741 /* Non-writeable fields. */
2742 if (mask
->ipv4_proto
|| mask
->ipv4_frag
)
2745 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
2748 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
2753 case OVS_KEY_ATTR_IPV6
: {
2754 const struct ovs_key_ipv6
*ipv6_key
;
2756 if (eth_type
!= htons(ETH_P_IPV6
))
2759 ipv6_key
= nla_data(ovs_key
);
2762 const struct ovs_key_ipv6
*mask
= ipv6_key
+ 1;
2764 /* Non-writeable fields. */
2765 if (mask
->ipv6_proto
|| mask
->ipv6_frag
)
2768 /* Invalid bits in the flow label mask? */
2769 if (ntohl(mask
->ipv6_label
) & 0xFFF00000)
2772 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
2775 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
2778 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
2783 case OVS_KEY_ATTR_TCP
:
2784 if ((eth_type
!= htons(ETH_P_IP
) &&
2785 eth_type
!= htons(ETH_P_IPV6
)) ||
2786 flow_key
->ip
.proto
!= IPPROTO_TCP
)
2791 case OVS_KEY_ATTR_UDP
:
2792 if ((eth_type
!= htons(ETH_P_IP
) &&
2793 eth_type
!= htons(ETH_P_IPV6
)) ||
2794 flow_key
->ip
.proto
!= IPPROTO_UDP
)
2799 case OVS_KEY_ATTR_MPLS
:
2800 if (!eth_p_mpls(eth_type
))
2804 case OVS_KEY_ATTR_SCTP
:
2805 if ((eth_type
!= htons(ETH_P_IP
) &&
2806 eth_type
!= htons(ETH_P_IPV6
)) ||
2807 flow_key
->ip
.proto
!= IPPROTO_SCTP
)
2812 case OVS_KEY_ATTR_NSH
:
2813 if (eth_type
!= htons(ETH_P_NSH
))
2815 if (!validate_nsh(nla_data(a
), masked
, false, log
))
2823 /* Convert non-masked non-tunnel set actions to masked set actions. */
2824 if (!masked
&& key_type
!= OVS_KEY_ATTR_TUNNEL
) {
2825 int start
, len
= key_len
* 2;
2830 start
= add_nested_action_start(sfa
,
2831 OVS_ACTION_ATTR_SET_TO_MASKED
,
2836 at
= __add_action(sfa
, key_type
, NULL
, len
, log
);
2840 memcpy(nla_data(at
), nla_data(ovs_key
), key_len
); /* Key. */
2841 memset(nla_data(at
) + key_len
, 0xff, key_len
); /* Mask. */
2842 /* Clear non-writeable bits from otherwise writeable fields. */
2843 if (key_type
== OVS_KEY_ATTR_IPV6
) {
2844 struct ovs_key_ipv6
*mask
= nla_data(at
) + key_len
;
2846 mask
->ipv6_label
&= htonl(0x000FFFFF);
2848 add_nested_action_end(*sfa
, start
);
2854 static int validate_userspace(const struct nlattr
*attr
)
2856 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
2857 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
2858 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
2859 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = {.type
= NLA_U32
},
2861 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
2864 error
= nla_parse_nested_deprecated(a
, OVS_USERSPACE_ATTR_MAX
, attr
,
2865 userspace_policy
, NULL
);
2869 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
2870 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
2876 static const struct nla_policy cpl_policy
[OVS_CHECK_PKT_LEN_ATTR_MAX
+ 1] = {
2877 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
] = {.type
= NLA_U16
},
2878 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
] = {.type
= NLA_NESTED
},
2879 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
] = {.type
= NLA_NESTED
},
2882 static int validate_and_copy_check_pkt_len(struct net
*net
,
2883 const struct nlattr
*attr
,
2884 const struct sw_flow_key
*key
,
2885 struct sw_flow_actions
**sfa
,
2886 __be16 eth_type
, __be16 vlan_tci
,
2887 u32 mpls_label_count
,
2888 bool log
, bool last
)
2890 const struct nlattr
*acts_if_greater
, *acts_if_lesser_eq
;
2891 struct nlattr
*a
[OVS_CHECK_PKT_LEN_ATTR_MAX
+ 1];
2892 struct check_pkt_len_arg arg
;
2893 int nested_acts_start
;
2896 err
= nla_parse_deprecated_strict(a
, OVS_CHECK_PKT_LEN_ATTR_MAX
,
2897 nla_data(attr
), nla_len(attr
),
2902 if (!a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
] ||
2903 !nla_get_u16(a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
]))
2906 acts_if_lesser_eq
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
];
2907 acts_if_greater
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
];
2909 /* Both the nested action should be present. */
2910 if (!acts_if_greater
|| !acts_if_lesser_eq
)
2913 /* validation done, copy the nested actions. */
2914 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_CHECK_PKT_LEN
,
2919 arg
.pkt_len
= nla_get_u16(a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
]);
2920 arg
.exec_for_lesser_equal
=
2921 last
|| !actions_may_change_flow(acts_if_lesser_eq
);
2922 arg
.exec_for_greater
=
2923 last
|| !actions_may_change_flow(acts_if_greater
);
2925 err
= ovs_nla_add_action(sfa
, OVS_CHECK_PKT_LEN_ATTR_ARG
, &arg
,
2930 nested_acts_start
= add_nested_action_start(sfa
,
2931 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
, log
);
2932 if (nested_acts_start
< 0)
2933 return nested_acts_start
;
2935 err
= __ovs_nla_copy_actions(net
, acts_if_lesser_eq
, key
, sfa
,
2936 eth_type
, vlan_tci
, mpls_label_count
, log
);
2941 add_nested_action_end(*sfa
, nested_acts_start
);
2943 nested_acts_start
= add_nested_action_start(sfa
,
2944 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
, log
);
2945 if (nested_acts_start
< 0)
2946 return nested_acts_start
;
2948 err
= __ovs_nla_copy_actions(net
, acts_if_greater
, key
, sfa
,
2949 eth_type
, vlan_tci
, mpls_label_count
, log
);
2954 add_nested_action_end(*sfa
, nested_acts_start
);
2955 add_nested_action_end(*sfa
, start
);
2959 static int copy_action(const struct nlattr
*from
,
2960 struct sw_flow_actions
**sfa
, bool log
)
2962 int totlen
= NLA_ALIGN(from
->nla_len
);
2965 to
= reserve_sfa_size(sfa
, from
->nla_len
, log
);
2969 memcpy(to
, from
, totlen
);
2973 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2974 const struct sw_flow_key
*key
,
2975 struct sw_flow_actions
**sfa
,
2976 __be16 eth_type
, __be16 vlan_tci
,
2977 u32 mpls_label_count
, bool log
)
2979 u8 mac_proto
= ovs_key_mac_proto(key
);
2980 const struct nlattr
*a
;
2983 nla_for_each_nested(a
, attr
, rem
) {
2984 /* Expected argument lengths, (u32)-1 for variable length. */
2985 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
2986 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
2987 [OVS_ACTION_ATTR_RECIRC
] = sizeof(u32
),
2988 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
2989 [OVS_ACTION_ATTR_PUSH_MPLS
] = sizeof(struct ovs_action_push_mpls
),
2990 [OVS_ACTION_ATTR_POP_MPLS
] = sizeof(__be16
),
2991 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
2992 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
2993 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
2994 [OVS_ACTION_ATTR_SET_MASKED
] = (u32
)-1,
2995 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1,
2996 [OVS_ACTION_ATTR_HASH
] = sizeof(struct ovs_action_hash
),
2997 [OVS_ACTION_ATTR_CT
] = (u32
)-1,
2998 [OVS_ACTION_ATTR_CT_CLEAR
] = 0,
2999 [OVS_ACTION_ATTR_TRUNC
] = sizeof(struct ovs_action_trunc
),
3000 [OVS_ACTION_ATTR_PUSH_ETH
] = sizeof(struct ovs_action_push_eth
),
3001 [OVS_ACTION_ATTR_POP_ETH
] = 0,
3002 [OVS_ACTION_ATTR_PUSH_NSH
] = (u32
)-1,
3003 [OVS_ACTION_ATTR_POP_NSH
] = 0,
3004 [OVS_ACTION_ATTR_METER
] = sizeof(u32
),
3005 [OVS_ACTION_ATTR_CLONE
] = (u32
)-1,
3006 [OVS_ACTION_ATTR_CHECK_PKT_LEN
] = (u32
)-1,
3008 const struct ovs_action_push_vlan
*vlan
;
3009 int type
= nla_type(a
);
3012 if (type
> OVS_ACTION_ATTR_MAX
||
3013 (action_lens
[type
] != nla_len(a
) &&
3014 action_lens
[type
] != (u32
)-1))
3019 case OVS_ACTION_ATTR_UNSPEC
:
3022 case OVS_ACTION_ATTR_USERSPACE
:
3023 err
= validate_userspace(a
);
3028 case OVS_ACTION_ATTR_OUTPUT
:
3029 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
3033 case OVS_ACTION_ATTR_TRUNC
: {
3034 const struct ovs_action_trunc
*trunc
= nla_data(a
);
3036 if (trunc
->max_len
< ETH_HLEN
)
3041 case OVS_ACTION_ATTR_HASH
: {
3042 const struct ovs_action_hash
*act_hash
= nla_data(a
);
3044 switch (act_hash
->hash_alg
) {
3045 case OVS_HASH_ALG_L4
:
3054 case OVS_ACTION_ATTR_POP_VLAN
:
3055 if (mac_proto
!= MAC_PROTO_ETHERNET
)
3057 vlan_tci
= htons(0);
3060 case OVS_ACTION_ATTR_PUSH_VLAN
:
3061 if (mac_proto
!= MAC_PROTO_ETHERNET
)
3064 if (!eth_type_vlan(vlan
->vlan_tpid
))
3066 if (!(vlan
->vlan_tci
& htons(VLAN_CFI_MASK
)))
3068 vlan_tci
= vlan
->vlan_tci
;
3071 case OVS_ACTION_ATTR_RECIRC
:
3074 case OVS_ACTION_ATTR_PUSH_MPLS
: {
3075 const struct ovs_action_push_mpls
*mpls
= nla_data(a
);
3077 if (!eth_p_mpls(mpls
->mpls_ethertype
))
3079 /* Prohibit push MPLS other than to a white list
3080 * for packets that have a known tag order.
3082 if (vlan_tci
& htons(VLAN_CFI_MASK
) ||
3083 (eth_type
!= htons(ETH_P_IP
) &&
3084 eth_type
!= htons(ETH_P_IPV6
) &&
3085 eth_type
!= htons(ETH_P_ARP
) &&
3086 eth_type
!= htons(ETH_P_RARP
) &&
3087 !eth_p_mpls(eth_type
)))
3089 eth_type
= mpls
->mpls_ethertype
;
3094 case OVS_ACTION_ATTR_POP_MPLS
: {
3096 if (vlan_tci
& htons(VLAN_CFI_MASK
) ||
3097 !eth_p_mpls(eth_type
))
3100 /* Disallow subsequent L2.5+ set actions and mpls_pop
3101 * actions once the last MPLS label in the packet is
3102 * popped as there is no check here to ensure that
3103 * the new eth type is valid and thus set actions could
3104 * write off the end of the packet or otherwise corrupt
3107 * Support for these actions is planned using packet
3110 proto
= nla_get_be16(a
);
3113 if (!eth_p_mpls(proto
) || !mpls_label_count
)
3114 eth_type
= htons(0);
3119 case OVS_ACTION_ATTR_SET
:
3120 err
= validate_set(a
, key
, sfa
,
3121 &skip_copy
, mac_proto
, eth_type
,
3127 case OVS_ACTION_ATTR_SET_MASKED
:
3128 err
= validate_set(a
, key
, sfa
,
3129 &skip_copy
, mac_proto
, eth_type
,
3135 case OVS_ACTION_ATTR_SAMPLE
: {
3136 bool last
= nla_is_last(a
, rem
);
3138 err
= validate_and_copy_sample(net
, a
, key
, sfa
,
3148 case OVS_ACTION_ATTR_CT
:
3149 err
= ovs_ct_copy_action(net
, a
, key
, sfa
, log
);
3155 case OVS_ACTION_ATTR_CT_CLEAR
:
3158 case OVS_ACTION_ATTR_PUSH_ETH
:
3159 /* Disallow pushing an Ethernet header if one
3160 * is already present */
3161 if (mac_proto
!= MAC_PROTO_NONE
)
3163 mac_proto
= MAC_PROTO_ETHERNET
;
3166 case OVS_ACTION_ATTR_POP_ETH
:
3167 if (mac_proto
!= MAC_PROTO_ETHERNET
)
3169 if (vlan_tci
& htons(VLAN_CFI_MASK
))
3171 mac_proto
= MAC_PROTO_NONE
;
3174 case OVS_ACTION_ATTR_PUSH_NSH
:
3175 if (mac_proto
!= MAC_PROTO_ETHERNET
) {
3178 next_proto
= tun_p_from_eth_p(eth_type
);
3182 mac_proto
= MAC_PROTO_NONE
;
3183 if (!validate_nsh(nla_data(a
), false, true, true))
3187 case OVS_ACTION_ATTR_POP_NSH
: {
3190 if (eth_type
!= htons(ETH_P_NSH
))
3192 inner_proto
= tun_p_to_eth_p(key
->nsh
.base
.np
);
3195 if (key
->nsh
.base
.np
== TUN_P_ETHERNET
)
3196 mac_proto
= MAC_PROTO_ETHERNET
;
3198 mac_proto
= MAC_PROTO_NONE
;
3202 case OVS_ACTION_ATTR_METER
:
3203 /* Non-existent meters are simply ignored. */
3206 case OVS_ACTION_ATTR_CLONE
: {
3207 bool last
= nla_is_last(a
, rem
);
3209 err
= validate_and_copy_clone(net
, a
, key
, sfa
,
3219 case OVS_ACTION_ATTR_CHECK_PKT_LEN
: {
3220 bool last
= nla_is_last(a
, rem
);
3222 err
= validate_and_copy_check_pkt_len(net
, a
, key
, sfa
,
3234 OVS_NLERR(log
, "Unknown Action type %d", type
);
3238 err
= copy_action(a
, sfa
, log
);
3250 /* 'key' must be the masked key. */
3251 int ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
3252 const struct sw_flow_key
*key
,
3253 struct sw_flow_actions
**sfa
, bool log
)
3256 u32 mpls_label_count
= 0;
3258 *sfa
= nla_alloc_flow_actions(min(nla_len(attr
), MAX_ACTIONS_BUFSIZE
));
3260 return PTR_ERR(*sfa
);
3262 if (eth_p_mpls(key
->eth
.type
))
3263 mpls_label_count
= hweight_long(key
->mpls
.num_labels_mask
);
3265 (*sfa
)->orig_len
= nla_len(attr
);
3266 err
= __ovs_nla_copy_actions(net
, attr
, key
, sfa
, key
->eth
.type
,
3267 key
->eth
.vlan
.tci
, mpls_label_count
, log
);
3269 ovs_nla_free_flow_actions(*sfa
);
3274 static int sample_action_to_attr(const struct nlattr
*attr
,
3275 struct sk_buff
*skb
)
3277 struct nlattr
*start
, *ac_start
= NULL
, *sample_arg
;
3278 int err
= 0, rem
= nla_len(attr
);
3279 const struct sample_arg
*arg
;
3280 struct nlattr
*actions
;
3282 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_SAMPLE
);
3286 sample_arg
= nla_data(attr
);
3287 arg
= nla_data(sample_arg
);
3288 actions
= nla_next(sample_arg
, &rem
);
3290 if (nla_put_u32(skb
, OVS_SAMPLE_ATTR_PROBABILITY
, arg
->probability
)) {
3295 ac_start
= nla_nest_start_noflag(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
3301 err
= ovs_nla_put_actions(actions
, rem
, skb
);
3305 nla_nest_cancel(skb
, ac_start
);
3306 nla_nest_cancel(skb
, start
);
3308 nla_nest_end(skb
, ac_start
);
3309 nla_nest_end(skb
, start
);
3315 static int clone_action_to_attr(const struct nlattr
*attr
,
3316 struct sk_buff
*skb
)
3318 struct nlattr
*start
;
3319 int err
= 0, rem
= nla_len(attr
);
3321 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_CLONE
);
3325 err
= ovs_nla_put_actions(nla_data(attr
), rem
, skb
);
3328 nla_nest_cancel(skb
, start
);
3330 nla_nest_end(skb
, start
);
3335 static int check_pkt_len_action_to_attr(const struct nlattr
*attr
,
3336 struct sk_buff
*skb
)
3338 struct nlattr
*start
, *ac_start
= NULL
;
3339 const struct check_pkt_len_arg
*arg
;
3340 const struct nlattr
*a
, *cpl_arg
;
3341 int err
= 0, rem
= nla_len(attr
);
3343 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_CHECK_PKT_LEN
);
3347 /* The first nested attribute in 'attr' is always
3348 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
3350 cpl_arg
= nla_data(attr
);
3351 arg
= nla_data(cpl_arg
);
3353 if (nla_put_u16(skb
, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
, arg
->pkt_len
)) {
3358 /* Second nested attribute in 'attr' is always
3359 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
3361 a
= nla_next(cpl_arg
, &rem
);
3362 ac_start
= nla_nest_start_noflag(skb
,
3363 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
);
3369 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
3371 nla_nest_cancel(skb
, ac_start
);
3374 nla_nest_end(skb
, ac_start
);
3377 /* Third nested attribute in 'attr' is always
3378 * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
3380 a
= nla_next(a
, &rem
);
3381 ac_start
= nla_nest_start_noflag(skb
,
3382 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
);
3388 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
3390 nla_nest_cancel(skb
, ac_start
);
3393 nla_nest_end(skb
, ac_start
);
3396 nla_nest_end(skb
, start
);
3400 nla_nest_cancel(skb
, start
);
3404 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
3406 const struct nlattr
*ovs_key
= nla_data(a
);
3407 int key_type
= nla_type(ovs_key
);
3408 struct nlattr
*start
;
3412 case OVS_KEY_ATTR_TUNNEL_INFO
: {
3413 struct ovs_tunnel_info
*ovs_tun
= nla_data(ovs_key
);
3414 struct ip_tunnel_info
*tun_info
= &ovs_tun
->tun_dst
->u
.tun_info
;
3416 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_SET
);
3420 err
= ip_tun_to_nlattr(skb
, &tun_info
->key
,
3421 ip_tunnel_info_opts(tun_info
),
3422 tun_info
->options_len
,
3423 ip_tunnel_info_af(tun_info
));
3426 nla_nest_end(skb
, start
);
3430 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
3438 static int masked_set_action_to_set_action_attr(const struct nlattr
*a
,
3439 struct sk_buff
*skb
)
3441 const struct nlattr
*ovs_key
= nla_data(a
);
3443 size_t key_len
= nla_len(ovs_key
) / 2;
3445 /* Revert the conversion we did from a non-masked set action to
3446 * masked set action.
3448 nla
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_SET
);
3452 if (nla_put(skb
, nla_type(ovs_key
), key_len
, nla_data(ovs_key
)))
3455 nla_nest_end(skb
, nla
);
3459 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
3461 const struct nlattr
*a
;
3464 nla_for_each_attr(a
, attr
, len
, rem
) {
3465 int type
= nla_type(a
);
3468 case OVS_ACTION_ATTR_SET
:
3469 err
= set_action_to_attr(a
, skb
);
3474 case OVS_ACTION_ATTR_SET_TO_MASKED
:
3475 err
= masked_set_action_to_set_action_attr(a
, skb
);
3480 case OVS_ACTION_ATTR_SAMPLE
:
3481 err
= sample_action_to_attr(a
, skb
);
3486 case OVS_ACTION_ATTR_CT
:
3487 err
= ovs_ct_action_to_attr(nla_data(a
), skb
);
3492 case OVS_ACTION_ATTR_CLONE
:
3493 err
= clone_action_to_attr(a
, skb
);
3498 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
3499 err
= check_pkt_len_action_to_attr(a
, skb
);
3505 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))