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
51 static bool actions_may_change_flow(const struct nlattr
*actions
)
56 nla_for_each_nested(nla
, actions
, rem
) {
57 u16 action
= nla_type(nla
);
60 case OVS_ACTION_ATTR_OUTPUT
:
61 case OVS_ACTION_ATTR_RECIRC
:
62 case OVS_ACTION_ATTR_TRUNC
:
63 case OVS_ACTION_ATTR_USERSPACE
:
66 case OVS_ACTION_ATTR_CT
:
67 case OVS_ACTION_ATTR_CT_CLEAR
:
68 case OVS_ACTION_ATTR_HASH
:
69 case OVS_ACTION_ATTR_POP_ETH
:
70 case OVS_ACTION_ATTR_POP_MPLS
:
71 case OVS_ACTION_ATTR_POP_NSH
:
72 case OVS_ACTION_ATTR_POP_VLAN
:
73 case OVS_ACTION_ATTR_PUSH_ETH
:
74 case OVS_ACTION_ATTR_PUSH_MPLS
:
75 case OVS_ACTION_ATTR_PUSH_NSH
:
76 case OVS_ACTION_ATTR_PUSH_VLAN
:
77 case OVS_ACTION_ATTR_SAMPLE
:
78 case OVS_ACTION_ATTR_SET
:
79 case OVS_ACTION_ATTR_SET_MASKED
:
80 case OVS_ACTION_ATTR_METER
:
81 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
82 case OVS_ACTION_ATTR_ADD_MPLS
:
90 static void update_range(struct sw_flow_match
*match
,
91 size_t offset
, size_t size
, bool is_mask
)
93 struct sw_flow_key_range
*range
;
94 size_t start
= rounddown(offset
, sizeof(long));
95 size_t end
= roundup(offset
+ size
, sizeof(long));
98 range
= &match
->range
;
100 range
= &match
->mask
->range
;
102 if (range
->start
== range
->end
) {
103 range
->start
= start
;
108 if (range
->start
> start
)
109 range
->start
= start
;
111 if (range
->end
< end
)
115 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
117 update_range(match, offsetof(struct sw_flow_key, field), \
118 sizeof((match)->key->field), is_mask); \
120 (match)->mask->key.field = value; \
122 (match)->key->field = value; \
125 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
127 update_range(match, offset, len, is_mask); \
129 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
132 memcpy((u8 *)(match)->key + offset, value_p, len); \
135 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
136 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
137 value_p, len, is_mask)
139 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
141 update_range(match, offsetof(struct sw_flow_key, field), \
142 sizeof((match)->key->field), is_mask); \
144 memset((u8 *)&(match)->mask->key.field, value, \
145 sizeof((match)->mask->key.field)); \
147 memset((u8 *)&(match)->key->field, value, \
148 sizeof((match)->key->field)); \
151 static bool match_validate(const struct sw_flow_match
*match
,
152 u64 key_attrs
, u64 mask_attrs
, bool log
)
154 u64 key_expected
= 0;
155 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
157 /* The following mask attributes allowed only if they
158 * pass the validation tests. */
159 mask_allowed
&= ~((1 << OVS_KEY_ATTR_IPV4
)
160 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)
161 | (1 << OVS_KEY_ATTR_IPV6
)
162 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)
163 | (1 << OVS_KEY_ATTR_TCP
)
164 | (1 << OVS_KEY_ATTR_TCP_FLAGS
)
165 | (1 << OVS_KEY_ATTR_UDP
)
166 | (1 << OVS_KEY_ATTR_SCTP
)
167 | (1 << OVS_KEY_ATTR_ICMP
)
168 | (1 << OVS_KEY_ATTR_ICMPV6
)
169 | (1 << OVS_KEY_ATTR_ARP
)
170 | (1 << OVS_KEY_ATTR_ND
)
171 | (1 << OVS_KEY_ATTR_MPLS
)
172 | (1 << OVS_KEY_ATTR_NSH
));
174 /* Always allowed mask fields. */
175 mask_allowed
|= ((1 << OVS_KEY_ATTR_TUNNEL
)
176 | (1 << OVS_KEY_ATTR_IN_PORT
)
177 | (1 << OVS_KEY_ATTR_ETHERTYPE
));
179 /* Check key attributes. */
180 if (match
->key
->eth
.type
== htons(ETH_P_ARP
)
181 || match
->key
->eth
.type
== htons(ETH_P_RARP
)) {
182 key_expected
|= 1 << OVS_KEY_ATTR_ARP
;
183 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
184 mask_allowed
|= 1 << OVS_KEY_ATTR_ARP
;
187 if (eth_p_mpls(match
->key
->eth
.type
)) {
188 key_expected
|= 1 << OVS_KEY_ATTR_MPLS
;
189 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
190 mask_allowed
|= 1 << OVS_KEY_ATTR_MPLS
;
193 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
194 key_expected
|= 1 << OVS_KEY_ATTR_IPV4
;
195 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
196 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV4
;
197 mask_allowed
|= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
200 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
201 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
202 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
203 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
204 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
207 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
208 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
209 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
210 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
213 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
214 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
215 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
216 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
217 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
218 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
222 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
223 key_expected
|= 1 << OVS_KEY_ATTR_ICMP
;
224 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
225 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMP
;
230 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
231 key_expected
|= 1 << OVS_KEY_ATTR_IPV6
;
232 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
233 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV6
;
234 mask_allowed
|= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
237 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
238 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
239 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
240 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
241 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
244 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
245 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
246 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
247 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
250 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
251 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
252 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
253 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
254 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
255 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
259 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
260 key_expected
|= 1 << OVS_KEY_ATTR_ICMPV6
;
261 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
262 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMPV6
;
264 if (match
->key
->tp
.src
==
265 htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
266 match
->key
->tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
267 key_expected
|= 1 << OVS_KEY_ATTR_ND
;
268 /* Original direction conntrack tuple
269 * uses the same space as the ND fields
270 * in the key, so both are not allowed
273 mask_allowed
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
274 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xff)))
275 mask_allowed
|= 1 << OVS_KEY_ATTR_ND
;
281 if (match
->key
->eth
.type
== htons(ETH_P_NSH
)) {
282 key_expected
|= 1 << OVS_KEY_ATTR_NSH
;
284 match
->mask
->key
.eth
.type
== htons(0xffff)) {
285 mask_allowed
|= 1 << OVS_KEY_ATTR_NSH
;
289 if ((key_attrs
& key_expected
) != key_expected
) {
290 /* Key attributes check failed. */
291 OVS_NLERR(log
, "Missing key (keys=%llx, expected=%llx)",
292 (unsigned long long)key_attrs
,
293 (unsigned long long)key_expected
);
297 if ((mask_attrs
& mask_allowed
) != mask_attrs
) {
298 /* Mask attributes check failed. */
299 OVS_NLERR(log
, "Unexpected mask (mask=%llx, allowed=%llx)",
300 (unsigned long long)mask_attrs
,
301 (unsigned long long)mask_allowed
);
308 size_t ovs_tun_key_attr_size(void)
310 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
311 * updating this function.
313 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
314 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
315 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
316 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
317 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
318 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
319 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
320 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
321 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
322 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and
323 * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with
324 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
326 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
327 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
330 static size_t ovs_nsh_key_attr_size(void)
332 /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
333 * updating this function.
335 return nla_total_size(NSH_BASE_HDR_LEN
) /* OVS_NSH_KEY_ATTR_BASE */
336 /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
337 * mutually exclusive, so the bigger one can cover
340 + nla_total_size(NSH_CTX_HDRS_MAX_LEN
);
343 size_t ovs_key_attr_size(void)
345 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
346 * updating this function.
348 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO
!= 29);
350 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
351 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
352 + ovs_tun_key_attr_size()
353 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
354 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
355 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
356 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
357 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
358 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
359 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
360 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
361 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
362 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
363 + ovs_nsh_key_attr_size()
364 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
365 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
366 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
367 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
368 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
369 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
371 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
374 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
375 [OVS_VXLAN_EXT_GBP
] = { .len
= sizeof(u32
) },
378 static const struct ovs_len_tbl ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
379 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= sizeof(u64
) },
380 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= sizeof(u32
) },
381 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= sizeof(u32
) },
382 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
383 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
384 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
385 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
386 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= sizeof(u16
) },
387 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= sizeof(u16
) },
388 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
389 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
390 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= OVS_ATTR_NESTED
,
391 .next
= ovs_vxlan_ext_key_lens
},
392 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= sizeof(struct in6_addr
) },
393 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= sizeof(struct in6_addr
) },
394 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
395 [OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE
] = { .len
= 0 },
398 static const struct ovs_len_tbl
399 ovs_nsh_key_attr_lens
[OVS_NSH_KEY_ATTR_MAX
+ 1] = {
400 [OVS_NSH_KEY_ATTR_BASE
] = { .len
= sizeof(struct ovs_nsh_key_base
) },
401 [OVS_NSH_KEY_ATTR_MD1
] = { .len
= sizeof(struct ovs_nsh_key_md1
) },
402 [OVS_NSH_KEY_ATTR_MD2
] = { .len
= OVS_ATTR_VARIABLE
},
405 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
406 static const struct ovs_len_tbl ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
407 [OVS_KEY_ATTR_ENCAP
] = { .len
= OVS_ATTR_NESTED
},
408 [OVS_KEY_ATTR_PRIORITY
] = { .len
= sizeof(u32
) },
409 [OVS_KEY_ATTR_IN_PORT
] = { .len
= sizeof(u32
) },
410 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= sizeof(u32
) },
411 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
412 [OVS_KEY_ATTR_VLAN
] = { .len
= sizeof(__be16
) },
413 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= sizeof(__be16
) },
414 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
415 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
416 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
417 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= sizeof(__be16
) },
418 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
419 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
420 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
421 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
422 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
423 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
424 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= sizeof(u32
) },
425 [OVS_KEY_ATTR_DP_HASH
] = { .len
= sizeof(u32
) },
426 [OVS_KEY_ATTR_TUNNEL
] = { .len
= OVS_ATTR_NESTED
,
427 .next
= ovs_tunnel_key_lens
, },
428 [OVS_KEY_ATTR_MPLS
] = { .len
= OVS_ATTR_VARIABLE
},
429 [OVS_KEY_ATTR_CT_STATE
] = { .len
= sizeof(u32
) },
430 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= sizeof(u16
) },
431 [OVS_KEY_ATTR_CT_MARK
] = { .len
= sizeof(u32
) },
432 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
433 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = {
434 .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
435 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = {
436 .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
437 [OVS_KEY_ATTR_NSH
] = { .len
= OVS_ATTR_NESTED
,
438 .next
= ovs_nsh_key_attr_lens
, },
441 static bool check_attr_len(unsigned int attr_len
, unsigned int expected_len
)
443 return expected_len
== attr_len
||
444 expected_len
== OVS_ATTR_NESTED
||
445 expected_len
== OVS_ATTR_VARIABLE
;
448 static bool is_all_zero(const u8
*fp
, size_t size
)
455 for (i
= 0; i
< size
; i
++)
462 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
463 const struct nlattr
*a
[],
464 u64
*attrsp
, bool log
, bool nz
)
466 const struct nlattr
*nla
;
471 nla_for_each_nested(nla
, attr
, rem
) {
472 u16 type
= nla_type(nla
);
475 if (type
> OVS_KEY_ATTR_MAX
) {
476 OVS_NLERR(log
, "Key type %d is out of range max %d",
477 type
, OVS_KEY_ATTR_MAX
);
481 if (attrs
& (1 << type
)) {
482 OVS_NLERR(log
, "Duplicate key (type %d).", type
);
486 expected_len
= ovs_key_lens
[type
].len
;
487 if (!check_attr_len(nla_len(nla
), expected_len
)) {
488 OVS_NLERR(log
, "Key %d has unexpected len %d expected %d",
489 type
, nla_len(nla
), expected_len
);
493 if (!nz
|| !is_all_zero(nla_data(nla
), nla_len(nla
))) {
499 OVS_NLERR(log
, "Message has %d unknown bytes.", rem
);
507 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
508 const struct nlattr
*a
[], u64
*attrsp
,
511 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, true);
514 int parse_flow_nlattrs(const struct nlattr
*attr
, const struct nlattr
*a
[],
515 u64
*attrsp
, bool log
)
517 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, false);
520 static int genev_tun_opt_from_nlattr(const struct nlattr
*a
,
521 struct sw_flow_match
*match
, bool is_mask
,
524 unsigned long opt_key_offset
;
526 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
527 OVS_NLERR(log
, "Geneve option length err (len %d, max %zu).",
528 nla_len(a
), sizeof(match
->key
->tun_opts
));
532 if (nla_len(a
) % 4 != 0) {
533 OVS_NLERR(log
, "Geneve opt len %d is not a multiple of 4.",
538 /* We need to record the length of the options passed
539 * down, otherwise packets with the same format but
540 * additional options will be silently matched.
543 SW_FLOW_KEY_PUT(match
, tun_opts_len
, nla_len(a
),
546 /* This is somewhat unusual because it looks at
547 * both the key and mask while parsing the
548 * attributes (and by extension assumes the key
549 * is parsed first). Normally, we would verify
550 * that each is the correct length and that the
551 * attributes line up in the validate function.
552 * However, that is difficult because this is
553 * variable length and we won't have the
556 if (match
->key
->tun_opts_len
!= nla_len(a
)) {
557 OVS_NLERR(log
, "Geneve option len %d != mask len %d",
558 match
->key
->tun_opts_len
, nla_len(a
));
562 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
565 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
566 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
567 nla_len(a
), is_mask
);
571 static int vxlan_tun_opt_from_nlattr(const struct nlattr
*attr
,
572 struct sw_flow_match
*match
, bool is_mask
,
577 unsigned long opt_key_offset
;
578 struct vxlan_metadata opts
;
580 BUILD_BUG_ON(sizeof(opts
) > sizeof(match
->key
->tun_opts
));
582 memset(&opts
, 0, sizeof(opts
));
583 nla_for_each_nested(a
, attr
, rem
) {
584 int type
= nla_type(a
);
586 if (type
> OVS_VXLAN_EXT_MAX
) {
587 OVS_NLERR(log
, "VXLAN extension %d out of range max %d",
588 type
, OVS_VXLAN_EXT_MAX
);
592 if (!check_attr_len(nla_len(a
),
593 ovs_vxlan_ext_key_lens
[type
].len
)) {
594 OVS_NLERR(log
, "VXLAN extension %d has unexpected len %d expected %d",
596 ovs_vxlan_ext_key_lens
[type
].len
);
601 case OVS_VXLAN_EXT_GBP
:
602 opts
.gbp
= nla_get_u32(a
);
605 OVS_NLERR(log
, "Unknown VXLAN extension attribute %d",
611 OVS_NLERR(log
, "VXLAN extension message has %d unknown bytes.",
617 SW_FLOW_KEY_PUT(match
, tun_opts_len
, sizeof(opts
), false);
619 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
621 opt_key_offset
= TUN_METADATA_OFFSET(sizeof(opts
));
622 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, &opts
, sizeof(opts
),
627 static int erspan_tun_opt_from_nlattr(const struct nlattr
*a
,
628 struct sw_flow_match
*match
, bool is_mask
,
631 unsigned long opt_key_offset
;
633 BUILD_BUG_ON(sizeof(struct erspan_metadata
) >
634 sizeof(match
->key
->tun_opts
));
636 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
637 OVS_NLERR(log
, "ERSPAN option length err (len %d, max %zu).",
638 nla_len(a
), sizeof(match
->key
->tun_opts
));
643 SW_FLOW_KEY_PUT(match
, tun_opts_len
,
644 sizeof(struct erspan_metadata
), false);
646 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
648 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
649 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
650 nla_len(a
), is_mask
);
654 static int ip_tun_from_nlattr(const struct nlattr
*attr
,
655 struct sw_flow_match
*match
, bool is_mask
,
658 bool ttl
= false, ipv4
= false, ipv6
= false;
659 bool info_bridge_mode
= false;
660 __be16 tun_flags
= 0;
665 nla_for_each_nested(a
, attr
, rem
) {
666 int type
= nla_type(a
);
669 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
670 OVS_NLERR(log
, "Tunnel attr %d out of range max %d",
671 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
675 if (!check_attr_len(nla_len(a
),
676 ovs_tunnel_key_lens
[type
].len
)) {
677 OVS_NLERR(log
, "Tunnel attr %d has unexpected len %d expected %d",
678 type
, nla_len(a
), ovs_tunnel_key_lens
[type
].len
);
683 case OVS_TUNNEL_KEY_ATTR_ID
:
684 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
685 nla_get_be64(a
), is_mask
);
686 tun_flags
|= TUNNEL_KEY
;
688 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
689 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.src
,
690 nla_get_in_addr(a
), is_mask
);
693 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
694 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.dst
,
695 nla_get_in_addr(a
), is_mask
);
698 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
699 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.src
,
700 nla_get_in6_addr(a
), is_mask
);
703 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
704 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.dst
,
705 nla_get_in6_addr(a
), is_mask
);
708 case OVS_TUNNEL_KEY_ATTR_TOS
:
709 SW_FLOW_KEY_PUT(match
, tun_key
.tos
,
710 nla_get_u8(a
), is_mask
);
712 case OVS_TUNNEL_KEY_ATTR_TTL
:
713 SW_FLOW_KEY_PUT(match
, tun_key
.ttl
,
714 nla_get_u8(a
), is_mask
);
717 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
718 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
720 case OVS_TUNNEL_KEY_ATTR_CSUM
:
721 tun_flags
|= TUNNEL_CSUM
;
723 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
724 SW_FLOW_KEY_PUT(match
, tun_key
.tp_src
,
725 nla_get_be16(a
), is_mask
);
727 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
728 SW_FLOW_KEY_PUT(match
, tun_key
.tp_dst
,
729 nla_get_be16(a
), is_mask
);
731 case OVS_TUNNEL_KEY_ATTR_OAM
:
732 tun_flags
|= TUNNEL_OAM
;
734 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
736 OVS_NLERR(log
, "Multiple metadata blocks provided");
740 err
= genev_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
744 tun_flags
|= TUNNEL_GENEVE_OPT
;
747 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
749 OVS_NLERR(log
, "Multiple metadata blocks provided");
753 err
= vxlan_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
757 tun_flags
|= TUNNEL_VXLAN_OPT
;
760 case OVS_TUNNEL_KEY_ATTR_PAD
:
762 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
764 OVS_NLERR(log
, "Multiple metadata blocks provided");
768 err
= erspan_tun_opt_from_nlattr(a
, match
, is_mask
,
773 tun_flags
|= TUNNEL_ERSPAN_OPT
;
776 case OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE
:
777 info_bridge_mode
= true;
781 OVS_NLERR(log
, "Unknown IP tunnel attribute %d",
787 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
789 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_proto
, 0xff, true);
791 SW_FLOW_KEY_PUT(match
, tun_proto
, ipv6
? AF_INET6
: AF_INET
,
795 OVS_NLERR(log
, "IP tunnel attribute has %d unknown bytes.",
801 OVS_NLERR(log
, "Mixed IPv4 and IPv6 tunnel attributes");
806 if (!ipv4
&& !ipv6
) {
807 OVS_NLERR(log
, "IP tunnel dst address not specified");
811 if (info_bridge_mode
) {
812 if (match
->key
->tun_key
.u
.ipv4
.src
||
813 match
->key
->tun_key
.u
.ipv4
.dst
||
814 match
->key
->tun_key
.tp_src
||
815 match
->key
->tun_key
.tp_dst
||
816 match
->key
->tun_key
.ttl
||
817 match
->key
->tun_key
.tos
||
818 tun_flags
& ~TUNNEL_KEY
) {
819 OVS_NLERR(log
, "IPv4 tun info is not correct");
822 } else if (!match
->key
->tun_key
.u
.ipv4
.dst
) {
823 OVS_NLERR(log
, "IPv4 tunnel dst address is zero");
827 if (ipv6
&& ipv6_addr_any(&match
->key
->tun_key
.u
.ipv6
.dst
)) {
828 OVS_NLERR(log
, "IPv6 tunnel dst address is zero");
832 if (!ttl
&& !info_bridge_mode
) {
833 OVS_NLERR(log
, "IP tunnel TTL not specified.");
841 static int vxlan_opt_to_nlattr(struct sk_buff
*skb
,
842 const void *tun_opts
, int swkey_tun_opts_len
)
844 const struct vxlan_metadata
*opts
= tun_opts
;
847 nla
= nla_nest_start_noflag(skb
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
851 if (nla_put_u32(skb
, OVS_VXLAN_EXT_GBP
, opts
->gbp
) < 0)
854 nla_nest_end(skb
, nla
);
858 static int __ip_tun_to_nlattr(struct sk_buff
*skb
,
859 const struct ip_tunnel_key
*output
,
860 const void *tun_opts
, int swkey_tun_opts_len
,
861 unsigned short tun_proto
, u8 mode
)
863 if (output
->tun_flags
& TUNNEL_KEY
&&
864 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
,
865 OVS_TUNNEL_KEY_ATTR_PAD
))
868 if (mode
& IP_TUNNEL_INFO_BRIDGE
)
869 return nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE
)
874 if (output
->u
.ipv4
.src
&&
875 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
,
878 if (output
->u
.ipv4
.dst
&&
879 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
,
884 if (!ipv6_addr_any(&output
->u
.ipv6
.src
) &&
885 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
,
886 &output
->u
.ipv6
.src
))
888 if (!ipv6_addr_any(&output
->u
.ipv6
.dst
) &&
889 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
,
890 &output
->u
.ipv6
.dst
))
895 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->tos
))
897 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ttl
))
899 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
900 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
902 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
903 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
905 if (output
->tp_src
&&
906 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, output
->tp_src
))
908 if (output
->tp_dst
&&
909 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_DST
, output
->tp_dst
))
911 if ((output
->tun_flags
& TUNNEL_OAM
) &&
912 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_OAM
))
914 if (swkey_tun_opts_len
) {
915 if (output
->tun_flags
& TUNNEL_GENEVE_OPT
&&
916 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
917 swkey_tun_opts_len
, tun_opts
))
919 else if (output
->tun_flags
& TUNNEL_VXLAN_OPT
&&
920 vxlan_opt_to_nlattr(skb
, tun_opts
, swkey_tun_opts_len
))
922 else if (output
->tun_flags
& TUNNEL_ERSPAN_OPT
&&
923 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
924 swkey_tun_opts_len
, tun_opts
))
931 static int ip_tun_to_nlattr(struct sk_buff
*skb
,
932 const struct ip_tunnel_key
*output
,
933 const void *tun_opts
, int swkey_tun_opts_len
,
934 unsigned short tun_proto
, u8 mode
)
939 nla
= nla_nest_start_noflag(skb
, OVS_KEY_ATTR_TUNNEL
);
943 err
= __ip_tun_to_nlattr(skb
, output
, tun_opts
, swkey_tun_opts_len
,
948 nla_nest_end(skb
, nla
);
952 int ovs_nla_put_tunnel_info(struct sk_buff
*skb
,
953 struct ip_tunnel_info
*tun_info
)
955 return __ip_tun_to_nlattr(skb
, &tun_info
->key
,
956 ip_tunnel_info_opts(tun_info
),
957 tun_info
->options_len
,
958 ip_tunnel_info_af(tun_info
), tun_info
->mode
);
961 static int encode_vlan_from_nlattrs(struct sw_flow_match
*match
,
962 const struct nlattr
*a
[],
963 bool is_mask
, bool inner
)
968 if (a
[OVS_KEY_ATTR_VLAN
])
969 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
971 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
972 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
974 if (likely(!inner
)) {
975 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tpid
, tpid
, is_mask
);
976 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, tci
, is_mask
);
978 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tpid
, tpid
, is_mask
);
979 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, tci
, is_mask
);
984 static int validate_vlan_from_nlattrs(const struct sw_flow_match
*match
,
985 u64 key_attrs
, bool inner
,
986 const struct nlattr
**a
, bool log
)
990 if (!((key_attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) &&
991 (key_attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) &&
992 eth_type_vlan(nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
])))) {
997 if (!((key_attrs
& (1 << OVS_KEY_ATTR_VLAN
)) &&
998 (key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
)))) {
999 OVS_NLERR(log
, "Invalid %s frame", (inner
) ? "C-VLAN" : "VLAN");
1003 if (a
[OVS_KEY_ATTR_VLAN
])
1004 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1006 if (!(tci
& htons(VLAN_CFI_MASK
))) {
1008 OVS_NLERR(log
, "%s TCI does not have VLAN_CFI_MASK bit set.",
1009 (inner
) ? "C-VLAN" : "VLAN");
1011 } else if (nla_len(a
[OVS_KEY_ATTR_ENCAP
])) {
1012 /* Corner case for truncated VLAN header. */
1013 OVS_NLERR(log
, "Truncated %s header has non-zero encap attribute.",
1014 (inner
) ? "C-VLAN" : "VLAN");
1022 static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match
*match
,
1023 u64 key_attrs
, bool inner
,
1024 const struct nlattr
**a
, bool log
)
1028 bool encap_valid
= !!(match
->key
->eth
.vlan
.tci
&
1029 htons(VLAN_CFI_MASK
));
1030 bool i_encap_valid
= !!(match
->key
->eth
.cvlan
.tci
&
1031 htons(VLAN_CFI_MASK
));
1033 if (!(key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
))) {
1038 if ((!inner
&& !encap_valid
) || (inner
&& !i_encap_valid
)) {
1039 OVS_NLERR(log
, "Encap mask attribute is set for non-%s frame.",
1040 (inner
) ? "C-VLAN" : "VLAN");
1044 if (a
[OVS_KEY_ATTR_VLAN
])
1045 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1047 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
1048 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1050 if (tpid
!= htons(0xffff)) {
1051 OVS_NLERR(log
, "Must have an exact match on %s TPID (mask=%x).",
1052 (inner
) ? "C-VLAN" : "VLAN", ntohs(tpid
));
1055 if (!(tci
& htons(VLAN_CFI_MASK
))) {
1056 OVS_NLERR(log
, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
1057 (inner
) ? "C-VLAN" : "VLAN");
1064 static int __parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
1065 u64
*key_attrs
, bool inner
,
1066 const struct nlattr
**a
, bool is_mask
,
1070 const struct nlattr
*encap
;
1073 err
= validate_vlan_from_nlattrs(match
, *key_attrs
, inner
,
1076 err
= validate_vlan_mask_from_nlattrs(match
, *key_attrs
, inner
,
1081 err
= encode_vlan_from_nlattrs(match
, a
, is_mask
, inner
);
1085 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
1086 *key_attrs
&= ~(1 << OVS_KEY_ATTR_VLAN
);
1087 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1089 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1092 err
= parse_flow_nlattrs(encap
, a
, key_attrs
, log
);
1094 err
= parse_flow_mask_nlattrs(encap
, a
, key_attrs
, log
);
1099 static int parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
1100 u64
*key_attrs
, const struct nlattr
**a
,
1101 bool is_mask
, bool log
)
1104 bool encap_valid
= false;
1106 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, false, a
,
1111 encap_valid
= !!(match
->key
->eth
.vlan
.tci
& htons(VLAN_CFI_MASK
));
1113 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, true, a
,
1122 static int parse_eth_type_from_nlattrs(struct sw_flow_match
*match
,
1123 u64
*attrs
, const struct nlattr
**a
,
1124 bool is_mask
, bool log
)
1128 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1130 /* Always exact match EtherType. */
1131 eth_type
= htons(0xffff);
1132 } else if (!eth_proto_is_802_3(eth_type
)) {
1133 OVS_NLERR(log
, "EtherType %x is less than min %x",
1134 ntohs(eth_type
), ETH_P_802_3_MIN
);
1138 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
1139 *attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1143 static int metadata_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1144 u64
*attrs
, const struct nlattr
**a
,
1145 bool is_mask
, bool log
)
1147 u8 mac_proto
= MAC_PROTO_ETHERNET
;
1149 if (*attrs
& (1 << OVS_KEY_ATTR_DP_HASH
)) {
1150 u32 hash_val
= nla_get_u32(a
[OVS_KEY_ATTR_DP_HASH
]);
1152 SW_FLOW_KEY_PUT(match
, ovs_flow_hash
, hash_val
, is_mask
);
1153 *attrs
&= ~(1 << OVS_KEY_ATTR_DP_HASH
);
1156 if (*attrs
& (1 << OVS_KEY_ATTR_RECIRC_ID
)) {
1157 u32 recirc_id
= nla_get_u32(a
[OVS_KEY_ATTR_RECIRC_ID
]);
1159 SW_FLOW_KEY_PUT(match
, recirc_id
, recirc_id
, is_mask
);
1160 *attrs
&= ~(1 << OVS_KEY_ATTR_RECIRC_ID
);
1163 if (*attrs
& (1 << OVS_KEY_ATTR_PRIORITY
)) {
1164 SW_FLOW_KEY_PUT(match
, phy
.priority
,
1165 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
1166 *attrs
&= ~(1 << OVS_KEY_ATTR_PRIORITY
);
1169 if (*attrs
& (1 << OVS_KEY_ATTR_IN_PORT
)) {
1170 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
1173 in_port
= 0xffffffff; /* Always exact match in_port. */
1174 } else if (in_port
>= DP_MAX_PORTS
) {
1175 OVS_NLERR(log
, "Port %d exceeds max allowable %d",
1176 in_port
, DP_MAX_PORTS
);
1180 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
1181 *attrs
&= ~(1 << OVS_KEY_ATTR_IN_PORT
);
1182 } else if (!is_mask
) {
1183 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
1186 if (*attrs
& (1 << OVS_KEY_ATTR_SKB_MARK
)) {
1187 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
1189 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
1190 *attrs
&= ~(1 << OVS_KEY_ATTR_SKB_MARK
);
1192 if (*attrs
& (1 << OVS_KEY_ATTR_TUNNEL
)) {
1193 if (ip_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
1196 *attrs
&= ~(1 << OVS_KEY_ATTR_TUNNEL
);
1199 if (*attrs
& (1 << OVS_KEY_ATTR_CT_STATE
) &&
1200 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_STATE
)) {
1201 u32 ct_state
= nla_get_u32(a
[OVS_KEY_ATTR_CT_STATE
]);
1203 if (ct_state
& ~CT_SUPPORTED_MASK
) {
1204 OVS_NLERR(log
, "ct_state flags %08x unsupported",
1209 SW_FLOW_KEY_PUT(match
, ct_state
, ct_state
, is_mask
);
1210 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_STATE
);
1212 if (*attrs
& (1 << OVS_KEY_ATTR_CT_ZONE
) &&
1213 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_ZONE
)) {
1214 u16 ct_zone
= nla_get_u16(a
[OVS_KEY_ATTR_CT_ZONE
]);
1216 SW_FLOW_KEY_PUT(match
, ct_zone
, ct_zone
, is_mask
);
1217 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ZONE
);
1219 if (*attrs
& (1 << OVS_KEY_ATTR_CT_MARK
) &&
1220 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_MARK
)) {
1221 u32 mark
= nla_get_u32(a
[OVS_KEY_ATTR_CT_MARK
]);
1223 SW_FLOW_KEY_PUT(match
, ct
.mark
, mark
, is_mask
);
1224 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_MARK
);
1226 if (*attrs
& (1 << OVS_KEY_ATTR_CT_LABELS
) &&
1227 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_LABELS
)) {
1228 const struct ovs_key_ct_labels
*cl
;
1230 cl
= nla_data(a
[OVS_KEY_ATTR_CT_LABELS
]);
1231 SW_FLOW_KEY_MEMCPY(match
, ct
.labels
, cl
->ct_labels
,
1232 sizeof(*cl
), is_mask
);
1233 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_LABELS
);
1235 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
1236 const struct ovs_key_ct_tuple_ipv4
*ct
;
1238 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
1240 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.src
, ct
->ipv4_src
, is_mask
);
1241 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.dst
, ct
->ipv4_dst
, is_mask
);
1242 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1243 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1244 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv4_proto
, is_mask
);
1245 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
1247 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
1248 const struct ovs_key_ct_tuple_ipv6
*ct
;
1250 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
1252 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.src
, &ct
->ipv6_src
,
1253 sizeof(match
->key
->ipv6
.ct_orig
.src
),
1255 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.dst
, &ct
->ipv6_dst
,
1256 sizeof(match
->key
->ipv6
.ct_orig
.dst
),
1258 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1259 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1260 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv6_proto
, is_mask
);
1261 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
1264 /* For layer 3 packets the Ethernet type is provided
1265 * and treated as metadata but no MAC addresses are provided.
1267 if (!(*attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) &&
1268 (*attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)))
1269 mac_proto
= MAC_PROTO_NONE
;
1271 /* Always exact match mac_proto */
1272 SW_FLOW_KEY_PUT(match
, mac_proto
, is_mask
? 0xff : mac_proto
, is_mask
);
1274 if (mac_proto
== MAC_PROTO_NONE
)
1275 return parse_eth_type_from_nlattrs(match
, attrs
, a
, is_mask
,
1281 int nsh_hdr_from_nlattr(const struct nlattr
*attr
,
1282 struct nshhdr
*nh
, size_t size
)
1290 /* validate_nsh has check this, so we needn't do duplicate check here
1292 if (size
< NSH_BASE_HDR_LEN
)
1295 nla_for_each_nested(a
, attr
, rem
) {
1296 int type
= nla_type(a
);
1299 case OVS_NSH_KEY_ATTR_BASE
: {
1300 const struct ovs_nsh_key_base
*base
= nla_data(a
);
1302 flags
= base
->flags
;
1305 nh
->mdtype
= base
->mdtype
;
1306 nh
->path_hdr
= base
->path_hdr
;
1309 case OVS_NSH_KEY_ATTR_MD1
:
1311 if (mdlen
> size
- NSH_BASE_HDR_LEN
)
1313 memcpy(&nh
->md1
, nla_data(a
), mdlen
);
1316 case OVS_NSH_KEY_ATTR_MD2
:
1318 if (mdlen
> size
- NSH_BASE_HDR_LEN
)
1320 memcpy(&nh
->md2
, nla_data(a
), mdlen
);
1328 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1329 nh
->ver_flags_ttl_len
= 0;
1330 nsh_set_flags_ttl_len(nh
, flags
, ttl
, NSH_BASE_HDR_LEN
+ mdlen
);
1335 int nsh_key_from_nlattr(const struct nlattr
*attr
,
1336 struct ovs_key_nsh
*nsh
, struct ovs_key_nsh
*nsh_mask
)
1341 /* validate_nsh has check this, so we needn't do duplicate check here
1343 nla_for_each_nested(a
, attr
, rem
) {
1344 int type
= nla_type(a
);
1347 case OVS_NSH_KEY_ATTR_BASE
: {
1348 const struct ovs_nsh_key_base
*base
= nla_data(a
);
1349 const struct ovs_nsh_key_base
*base_mask
= base
+ 1;
1352 nsh_mask
->base
= *base_mask
;
1355 case OVS_NSH_KEY_ATTR_MD1
: {
1356 const struct ovs_nsh_key_md1
*md1
= nla_data(a
);
1357 const struct ovs_nsh_key_md1
*md1_mask
= md1
+ 1;
1359 memcpy(nsh
->context
, md1
->context
, sizeof(*md1
));
1360 memcpy(nsh_mask
->context
, md1_mask
->context
,
1364 case OVS_NSH_KEY_ATTR_MD2
:
1365 /* Not supported yet */
1375 static int nsh_key_put_from_nlattr(const struct nlattr
*attr
,
1376 struct sw_flow_match
*match
, bool is_mask
,
1377 bool is_push_nsh
, bool log
)
1381 bool has_base
= false;
1382 bool has_md1
= false;
1383 bool has_md2
= false;
1387 if (WARN_ON(is_push_nsh
&& is_mask
))
1390 nla_for_each_nested(a
, attr
, rem
) {
1391 int type
= nla_type(a
);
1394 if (type
> OVS_NSH_KEY_ATTR_MAX
) {
1395 OVS_NLERR(log
, "nsh attr %d is out of range max %d",
1396 type
, OVS_NSH_KEY_ATTR_MAX
);
1400 if (!check_attr_len(nla_len(a
),
1401 ovs_nsh_key_attr_lens
[type
].len
)) {
1404 "nsh attr %d has unexpected len %d expected %d",
1407 ovs_nsh_key_attr_lens
[type
].len
1413 case OVS_NSH_KEY_ATTR_BASE
: {
1414 const struct ovs_nsh_key_base
*base
= nla_data(a
);
1417 mdtype
= base
->mdtype
;
1418 SW_FLOW_KEY_PUT(match
, nsh
.base
.flags
,
1419 base
->flags
, is_mask
);
1420 SW_FLOW_KEY_PUT(match
, nsh
.base
.ttl
,
1421 base
->ttl
, is_mask
);
1422 SW_FLOW_KEY_PUT(match
, nsh
.base
.mdtype
,
1423 base
->mdtype
, is_mask
);
1424 SW_FLOW_KEY_PUT(match
, nsh
.base
.np
,
1426 SW_FLOW_KEY_PUT(match
, nsh
.base
.path_hdr
,
1427 base
->path_hdr
, is_mask
);
1430 case OVS_NSH_KEY_ATTR_MD1
: {
1431 const struct ovs_nsh_key_md1
*md1
= nla_data(a
);
1434 for (i
= 0; i
< NSH_MD1_CONTEXT_SIZE
; i
++)
1435 SW_FLOW_KEY_PUT(match
, nsh
.context
[i
],
1436 md1
->context
[i
], is_mask
);
1439 case OVS_NSH_KEY_ATTR_MD2
:
1440 if (!is_push_nsh
) /* Not supported MD type 2 yet */
1445 if (mdlen
> NSH_CTX_HDRS_MAX_LEN
|| mdlen
<= 0) {
1448 "Invalid MD length %d for MD type %d",
1456 OVS_NLERR(log
, "Unknown nsh attribute %d",
1463 OVS_NLERR(log
, "nsh attribute has %d unknown bytes.", rem
);
1467 if (has_md1
&& has_md2
) {
1470 "invalid nsh attribute: md1 and md2 are exclusive."
1476 if ((has_md1
&& mdtype
!= NSH_M_TYPE1
) ||
1477 (has_md2
&& mdtype
!= NSH_M_TYPE2
)) {
1478 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1484 (!has_base
|| (!has_md1
&& !has_md2
))) {
1487 "push_nsh: missing base or metadata attributes"
1496 static int ovs_key_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1497 u64 attrs
, const struct nlattr
**a
,
1498 bool is_mask
, bool log
)
1502 err
= metadata_from_nlattrs(net
, match
, &attrs
, a
, is_mask
, log
);
1506 if (attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) {
1507 const struct ovs_key_ethernet
*eth_key
;
1509 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
1510 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
1511 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
1512 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
1513 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
1514 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERNET
);
1516 if (attrs
& (1 << OVS_KEY_ATTR_VLAN
)) {
1517 /* VLAN attribute is always parsed before getting here since it
1518 * may occur multiple times.
1520 OVS_NLERR(log
, "VLAN attribute unexpected.");
1524 if (attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) {
1525 err
= parse_eth_type_from_nlattrs(match
, &attrs
, a
, is_mask
,
1529 } else if (!is_mask
) {
1530 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
1532 } else if (!match
->key
->eth
.type
) {
1533 OVS_NLERR(log
, "Either Ethernet header or EtherType is required.");
1537 if (attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
1538 const struct ovs_key_ipv4
*ipv4_key
;
1540 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
1541 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
1542 OVS_NLERR(log
, "IPv4 frag type %d is out of range max %d",
1543 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
1546 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1547 ipv4_key
->ipv4_proto
, is_mask
);
1548 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1549 ipv4_key
->ipv4_tos
, is_mask
);
1550 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1551 ipv4_key
->ipv4_ttl
, is_mask
);
1552 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1553 ipv4_key
->ipv4_frag
, is_mask
);
1554 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1555 ipv4_key
->ipv4_src
, is_mask
);
1556 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1557 ipv4_key
->ipv4_dst
, is_mask
);
1558 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
1561 if (attrs
& (1 << OVS_KEY_ATTR_IPV6
)) {
1562 const struct ovs_key_ipv6
*ipv6_key
;
1564 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
1565 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
1566 OVS_NLERR(log
, "IPv6 frag type %d is out of range max %d",
1567 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
1571 if (!is_mask
&& ipv6_key
->ipv6_label
& htonl(0xFFF00000)) {
1572 OVS_NLERR(log
, "IPv6 flow label %x is out of range (max=%x)",
1573 ntohl(ipv6_key
->ipv6_label
), (1 << 20) - 1);
1577 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
1578 ipv6_key
->ipv6_label
, is_mask
);
1579 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1580 ipv6_key
->ipv6_proto
, is_mask
);
1581 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1582 ipv6_key
->ipv6_tclass
, is_mask
);
1583 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1584 ipv6_key
->ipv6_hlimit
, is_mask
);
1585 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1586 ipv6_key
->ipv6_frag
, is_mask
);
1587 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
1589 sizeof(match
->key
->ipv6
.addr
.src
),
1591 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
1593 sizeof(match
->key
->ipv6
.addr
.dst
),
1596 attrs
&= ~(1 << OVS_KEY_ATTR_IPV6
);
1599 if (attrs
& (1 << OVS_KEY_ATTR_ARP
)) {
1600 const struct ovs_key_arp
*arp_key
;
1602 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
1603 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
1604 OVS_NLERR(log
, "Unknown ARP opcode (opcode=%d).",
1609 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1610 arp_key
->arp_sip
, is_mask
);
1611 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1612 arp_key
->arp_tip
, is_mask
);
1613 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1614 ntohs(arp_key
->arp_op
), is_mask
);
1615 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
1616 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
1617 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
1618 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
1620 attrs
&= ~(1 << OVS_KEY_ATTR_ARP
);
1623 if (attrs
& (1 << OVS_KEY_ATTR_NSH
)) {
1624 if (nsh_key_put_from_nlattr(a
[OVS_KEY_ATTR_NSH
], match
,
1625 is_mask
, false, log
) < 0)
1627 attrs
&= ~(1 << OVS_KEY_ATTR_NSH
);
1630 if (attrs
& (1 << OVS_KEY_ATTR_MPLS
)) {
1631 const struct ovs_key_mpls
*mpls_key
;
1633 u32 label_count
, label_count_mask
, i
;
1635 mpls_key
= nla_data(a
[OVS_KEY_ATTR_MPLS
]);
1636 hdr_len
= nla_len(a
[OVS_KEY_ATTR_MPLS
]);
1637 label_count
= hdr_len
/ sizeof(struct ovs_key_mpls
);
1639 if (label_count
== 0 || label_count
> MPLS_LABEL_DEPTH
||
1640 hdr_len
% sizeof(struct ovs_key_mpls
))
1643 label_count_mask
= GENMASK(label_count
- 1, 0);
1645 for (i
= 0 ; i
< label_count
; i
++)
1646 SW_FLOW_KEY_PUT(match
, mpls
.lse
[i
],
1647 mpls_key
[i
].mpls_lse
, is_mask
);
1649 SW_FLOW_KEY_PUT(match
, mpls
.num_labels_mask
,
1650 label_count_mask
, is_mask
);
1652 attrs
&= ~(1 << OVS_KEY_ATTR_MPLS
);
1655 if (attrs
& (1 << OVS_KEY_ATTR_TCP
)) {
1656 const struct ovs_key_tcp
*tcp_key
;
1658 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
1659 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
1660 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
1661 attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
1664 if (attrs
& (1 << OVS_KEY_ATTR_TCP_FLAGS
)) {
1665 SW_FLOW_KEY_PUT(match
, tp
.flags
,
1666 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
1668 attrs
&= ~(1 << OVS_KEY_ATTR_TCP_FLAGS
);
1671 if (attrs
& (1 << OVS_KEY_ATTR_UDP
)) {
1672 const struct ovs_key_udp
*udp_key
;
1674 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
1675 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
1676 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
1677 attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
1680 if (attrs
& (1 << OVS_KEY_ATTR_SCTP
)) {
1681 const struct ovs_key_sctp
*sctp_key
;
1683 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
1684 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
1685 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
1686 attrs
&= ~(1 << OVS_KEY_ATTR_SCTP
);
1689 if (attrs
& (1 << OVS_KEY_ATTR_ICMP
)) {
1690 const struct ovs_key_icmp
*icmp_key
;
1692 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
1693 SW_FLOW_KEY_PUT(match
, tp
.src
,
1694 htons(icmp_key
->icmp_type
), is_mask
);
1695 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1696 htons(icmp_key
->icmp_code
), is_mask
);
1697 attrs
&= ~(1 << OVS_KEY_ATTR_ICMP
);
1700 if (attrs
& (1 << OVS_KEY_ATTR_ICMPV6
)) {
1701 const struct ovs_key_icmpv6
*icmpv6_key
;
1703 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
1704 SW_FLOW_KEY_PUT(match
, tp
.src
,
1705 htons(icmpv6_key
->icmpv6_type
), is_mask
);
1706 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1707 htons(icmpv6_key
->icmpv6_code
), is_mask
);
1708 attrs
&= ~(1 << OVS_KEY_ATTR_ICMPV6
);
1711 if (attrs
& (1 << OVS_KEY_ATTR_ND
)) {
1712 const struct ovs_key_nd
*nd_key
;
1714 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
1715 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
1717 sizeof(match
->key
->ipv6
.nd
.target
),
1719 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
1720 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
1721 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
1722 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
1723 attrs
&= ~(1 << OVS_KEY_ATTR_ND
);
1727 OVS_NLERR(log
, "Unknown key attributes %llx",
1728 (unsigned long long)attrs
);
1735 static void nlattr_set(struct nlattr
*attr
, u8 val
,
1736 const struct ovs_len_tbl
*tbl
)
1741 /* The nlattr stream should already have been validated */
1742 nla_for_each_nested(nla
, attr
, rem
) {
1743 if (tbl
[nla_type(nla
)].len
== OVS_ATTR_NESTED
)
1744 nlattr_set(nla
, val
, tbl
[nla_type(nla
)].next
? : tbl
);
1746 memset(nla_data(nla
), val
, nla_len(nla
));
1748 if (nla_type(nla
) == OVS_KEY_ATTR_CT_STATE
)
1749 *(u32
*)nla_data(nla
) &= CT_SUPPORTED_MASK
;
1753 static void mask_set_nlattr(struct nlattr
*attr
, u8 val
)
1755 nlattr_set(attr
, val
, ovs_key_lens
);
1759 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1760 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1761 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1762 * does not include any don't care bit.
1763 * @net: Used to determine per-namespace field support.
1764 * @match: receives the extracted flow match information.
1765 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1766 * sequence. The fields should of the packet that triggered the creation
1768 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1769 * attribute specifies the mask field of the wildcarded flow.
1770 * @log: Boolean to allow kernel error logging. Normally true, but when
1771 * probing for feature compatibility this should be passed in as false to
1772 * suppress unnecessary error logging.
1774 int ovs_nla_get_match(struct net
*net
, struct sw_flow_match
*match
,
1775 const struct nlattr
*nla_key
,
1776 const struct nlattr
*nla_mask
,
1779 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1780 struct nlattr
*newmask
= NULL
;
1785 err
= parse_flow_nlattrs(nla_key
, a
, &key_attrs
, log
);
1789 err
= parse_vlan_from_nlattrs(match
, &key_attrs
, a
, false, log
);
1793 err
= ovs_key_from_nlattrs(net
, match
, key_attrs
, a
, false, log
);
1799 /* Create an exact match mask. We need to set to 0xff
1800 * all the 'match->mask' fields that have been touched
1801 * in 'match->key'. We cannot simply memset
1802 * 'match->mask', because padding bytes and fields not
1803 * specified in 'match->key' should be left to 0.
1804 * Instead, we use a stream of netlink attributes,
1805 * copied from 'key' and set to 0xff.
1806 * ovs_key_from_nlattrs() will take care of filling
1807 * 'match->mask' appropriately.
1809 newmask
= kmemdup(nla_key
,
1810 nla_total_size(nla_len(nla_key
)),
1815 mask_set_nlattr(newmask
, 0xff);
1817 /* The userspace does not send tunnel attributes that
1818 * are 0, but we should not wildcard them nonetheless.
1820 if (match
->key
->tun_proto
)
1821 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_key
,
1827 err
= parse_flow_mask_nlattrs(nla_mask
, a
, &mask_attrs
, log
);
1831 /* Always match on tci. */
1832 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, htons(0xffff), true);
1833 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, htons(0xffff), true);
1835 err
= parse_vlan_from_nlattrs(match
, &mask_attrs
, a
, true, log
);
1839 err
= ovs_key_from_nlattrs(net
, match
, mask_attrs
, a
, true,
1845 if (!match_validate(match
, key_attrs
, mask_attrs
, log
))
1853 static size_t get_ufid_len(const struct nlattr
*attr
, bool log
)
1860 len
= nla_len(attr
);
1861 if (len
< 1 || len
> MAX_UFID_LENGTH
) {
1862 OVS_NLERR(log
, "ufid size %u bytes exceeds the range (1, %d)",
1863 nla_len(attr
), MAX_UFID_LENGTH
);
1870 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1871 * or false otherwise.
1873 bool ovs_nla_get_ufid(struct sw_flow_id
*sfid
, const struct nlattr
*attr
,
1876 sfid
->ufid_len
= get_ufid_len(attr
, log
);
1878 memcpy(sfid
->ufid
, nla_data(attr
), sfid
->ufid_len
);
1880 return sfid
->ufid_len
;
1883 int ovs_nla_get_identifier(struct sw_flow_id
*sfid
, const struct nlattr
*ufid
,
1884 const struct sw_flow_key
*key
, bool log
)
1886 struct sw_flow_key
*new_key
;
1888 if (ovs_nla_get_ufid(sfid
, ufid
, log
))
1891 /* If UFID was not provided, use unmasked key. */
1892 new_key
= kmalloc(sizeof(*new_key
), GFP_KERNEL
);
1895 memcpy(new_key
, key
, sizeof(*key
));
1896 sfid
->unmasked_key
= new_key
;
1901 u32
ovs_nla_get_ufid_flags(const struct nlattr
*attr
)
1903 return attr
? nla_get_u32(attr
) : 0;
1907 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1908 * @net: Network namespace.
1909 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1911 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1913 * @attrs: Bit mask for the netlink attributes included in @a.
1914 * @log: Boolean to allow kernel error logging. Normally true, but when
1915 * probing for feature compatibility this should be passed in as false to
1916 * suppress unnecessary error logging.
1918 * This parses a series of Netlink attributes that form a flow key, which must
1919 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1920 * get the metadata, that is, the parts of the flow key that cannot be
1921 * extracted from the packet itself.
1923 * This must be called before the packet key fields are filled in 'key'.
1926 int ovs_nla_get_flow_metadata(struct net
*net
,
1927 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1],
1928 u64 attrs
, struct sw_flow_key
*key
, bool log
)
1930 struct sw_flow_match match
;
1932 memset(&match
, 0, sizeof(match
));
1937 key
->ct_orig_proto
= 0;
1938 memset(&key
->ct
, 0, sizeof(key
->ct
));
1939 memset(&key
->ipv4
.ct_orig
, 0, sizeof(key
->ipv4
.ct_orig
));
1940 memset(&key
->ipv6
.ct_orig
, 0, sizeof(key
->ipv6
.ct_orig
));
1942 key
->phy
.in_port
= DP_MAX_PORTS
;
1944 return metadata_from_nlattrs(net
, &match
, &attrs
, a
, false, log
);
1947 static int ovs_nla_put_vlan(struct sk_buff
*skb
, const struct vlan_head
*vh
,
1950 __be16 eth_type
= !is_mask
? vh
->tpid
: htons(0xffff);
1952 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
1953 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, vh
->tci
))
1958 static int nsh_key_to_nlattr(const struct ovs_key_nsh
*nsh
, bool is_mask
,
1959 struct sk_buff
*skb
)
1961 struct nlattr
*start
;
1963 start
= nla_nest_start_noflag(skb
, OVS_KEY_ATTR_NSH
);
1967 if (nla_put(skb
, OVS_NSH_KEY_ATTR_BASE
, sizeof(nsh
->base
), &nsh
->base
))
1968 goto nla_put_failure
;
1970 if (is_mask
|| nsh
->base
.mdtype
== NSH_M_TYPE1
) {
1971 if (nla_put(skb
, OVS_NSH_KEY_ATTR_MD1
,
1972 sizeof(nsh
->context
), nsh
->context
))
1973 goto nla_put_failure
;
1976 /* Don't support MD type 2 yet */
1978 nla_nest_end(skb
, start
);
1986 static int __ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1987 const struct sw_flow_key
*output
, bool is_mask
,
1988 struct sk_buff
*skb
)
1990 struct ovs_key_ethernet
*eth_key
;
1992 struct nlattr
*encap
= NULL
;
1993 struct nlattr
*in_encap
= NULL
;
1995 if (nla_put_u32(skb
, OVS_KEY_ATTR_RECIRC_ID
, output
->recirc_id
))
1996 goto nla_put_failure
;
1998 if (nla_put_u32(skb
, OVS_KEY_ATTR_DP_HASH
, output
->ovs_flow_hash
))
1999 goto nla_put_failure
;
2001 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
2002 goto nla_put_failure
;
2004 if ((swkey
->tun_proto
|| is_mask
)) {
2005 const void *opts
= NULL
;
2007 if (output
->tun_key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
)
2008 opts
= TUN_METADATA_OPTS(output
, swkey
->tun_opts_len
);
2010 if (ip_tun_to_nlattr(skb
, &output
->tun_key
, opts
,
2011 swkey
->tun_opts_len
, swkey
->tun_proto
, 0))
2012 goto nla_put_failure
;
2015 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
2016 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
2017 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
2018 goto nla_put_failure
;
2021 upper_u16
= !is_mask
? 0 : 0xffff;
2023 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
2024 (upper_u16
<< 16) | output
->phy
.in_port
))
2025 goto nla_put_failure
;
2028 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
2029 goto nla_put_failure
;
2031 if (ovs_ct_put_key(swkey
, output
, skb
))
2032 goto nla_put_failure
;
2034 if (ovs_key_mac_proto(swkey
) == MAC_PROTO_ETHERNET
) {
2035 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
2037 goto nla_put_failure
;
2039 eth_key
= nla_data(nla
);
2040 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
2041 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
2043 if (swkey
->eth
.vlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
2044 if (ovs_nla_put_vlan(skb
, &output
->eth
.vlan
, is_mask
))
2045 goto nla_put_failure
;
2046 encap
= nla_nest_start_noflag(skb
, OVS_KEY_ATTR_ENCAP
);
2047 if (!swkey
->eth
.vlan
.tci
)
2050 if (swkey
->eth
.cvlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
2051 if (ovs_nla_put_vlan(skb
, &output
->eth
.cvlan
, is_mask
))
2052 goto nla_put_failure
;
2053 in_encap
= nla_nest_start_noflag(skb
,
2054 OVS_KEY_ATTR_ENCAP
);
2055 if (!swkey
->eth
.cvlan
.tci
)
2060 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
2062 * Ethertype 802.2 is represented in the netlink with omitted
2063 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
2064 * 0xffff in the mask attribute. Ethertype can also
2067 if (is_mask
&& output
->eth
.type
)
2068 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
2070 goto nla_put_failure
;
2075 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
2076 goto nla_put_failure
;
2078 if (eth_type_vlan(swkey
->eth
.type
)) {
2079 /* There are 3 VLAN tags, we don't know anything about the rest
2080 * of the packet, so truncate here.
2082 WARN_ON_ONCE(!(encap
&& in_encap
));
2086 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
2087 struct ovs_key_ipv4
*ipv4_key
;
2089 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
2091 goto nla_put_failure
;
2092 ipv4_key
= nla_data(nla
);
2093 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
2094 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
2095 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
2096 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
2097 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
2098 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
2099 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
2100 struct ovs_key_ipv6
*ipv6_key
;
2102 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
2104 goto nla_put_failure
;
2105 ipv6_key
= nla_data(nla
);
2106 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
2107 sizeof(ipv6_key
->ipv6_src
));
2108 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
2109 sizeof(ipv6_key
->ipv6_dst
));
2110 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
2111 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
2112 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
2113 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
2114 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
2115 } else if (swkey
->eth
.type
== htons(ETH_P_NSH
)) {
2116 if (nsh_key_to_nlattr(&output
->nsh
, is_mask
, skb
))
2117 goto nla_put_failure
;
2118 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
2119 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
2120 struct ovs_key_arp
*arp_key
;
2122 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
2124 goto nla_put_failure
;
2125 arp_key
= nla_data(nla
);
2126 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
2127 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
2128 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
2129 arp_key
->arp_op
= htons(output
->ip
.proto
);
2130 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
2131 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
2132 } else if (eth_p_mpls(swkey
->eth
.type
)) {
2134 struct ovs_key_mpls
*mpls_key
;
2136 num_labels
= hweight_long(output
->mpls
.num_labels_mask
);
2137 nla
= nla_reserve(skb
, OVS_KEY_ATTR_MPLS
,
2138 num_labels
* sizeof(*mpls_key
));
2140 goto nla_put_failure
;
2142 mpls_key
= nla_data(nla
);
2143 for (i
= 0; i
< num_labels
; i
++)
2144 mpls_key
[i
].mpls_lse
= output
->mpls
.lse
[i
];
2147 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
2148 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
2149 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
2151 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
2152 struct ovs_key_tcp
*tcp_key
;
2154 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
2156 goto nla_put_failure
;
2157 tcp_key
= nla_data(nla
);
2158 tcp_key
->tcp_src
= output
->tp
.src
;
2159 tcp_key
->tcp_dst
= output
->tp
.dst
;
2160 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
2162 goto nla_put_failure
;
2163 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
2164 struct ovs_key_udp
*udp_key
;
2166 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
2168 goto nla_put_failure
;
2169 udp_key
= nla_data(nla
);
2170 udp_key
->udp_src
= output
->tp
.src
;
2171 udp_key
->udp_dst
= output
->tp
.dst
;
2172 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
2173 struct ovs_key_sctp
*sctp_key
;
2175 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
2177 goto nla_put_failure
;
2178 sctp_key
= nla_data(nla
);
2179 sctp_key
->sctp_src
= output
->tp
.src
;
2180 sctp_key
->sctp_dst
= output
->tp
.dst
;
2181 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
2182 swkey
->ip
.proto
== IPPROTO_ICMP
) {
2183 struct ovs_key_icmp
*icmp_key
;
2185 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
2187 goto nla_put_failure
;
2188 icmp_key
= nla_data(nla
);
2189 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
2190 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
2191 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
2192 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
2193 struct ovs_key_icmpv6
*icmpv6_key
;
2195 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
2196 sizeof(*icmpv6_key
));
2198 goto nla_put_failure
;
2199 icmpv6_key
= nla_data(nla
);
2200 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
2201 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
2203 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
2204 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
2205 struct ovs_key_nd
*nd_key
;
2207 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
2209 goto nla_put_failure
;
2210 nd_key
= nla_data(nla
);
2211 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
2212 sizeof(nd_key
->nd_target
));
2213 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
2214 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
2221 nla_nest_end(skb
, in_encap
);
2223 nla_nest_end(skb
, encap
);
2231 int ovs_nla_put_key(const struct sw_flow_key
*swkey
,
2232 const struct sw_flow_key
*output
, int attr
, bool is_mask
,
2233 struct sk_buff
*skb
)
2238 nla
= nla_nest_start_noflag(skb
, attr
);
2241 err
= __ovs_nla_put_key(swkey
, output
, is_mask
, skb
);
2244 nla_nest_end(skb
, nla
);
2249 /* Called with ovs_mutex or RCU read lock. */
2250 int ovs_nla_put_identifier(const struct sw_flow
*flow
, struct sk_buff
*skb
)
2252 if (ovs_identifier_is_ufid(&flow
->id
))
2253 return nla_put(skb
, OVS_FLOW_ATTR_UFID
, flow
->id
.ufid_len
,
2256 return ovs_nla_put_key(flow
->id
.unmasked_key
, flow
->id
.unmasked_key
,
2257 OVS_FLOW_ATTR_KEY
, false, skb
);
2260 /* Called with ovs_mutex or RCU read lock. */
2261 int ovs_nla_put_masked_key(const struct sw_flow
*flow
, struct sk_buff
*skb
)
2263 return ovs_nla_put_key(&flow
->key
, &flow
->key
,
2264 OVS_FLOW_ATTR_KEY
, false, skb
);
2267 /* Called with ovs_mutex or RCU read lock. */
2268 int ovs_nla_put_mask(const struct sw_flow
*flow
, struct sk_buff
*skb
)
2270 return ovs_nla_put_key(&flow
->key
, &flow
->mask
->key
,
2271 OVS_FLOW_ATTR_MASK
, true, skb
);
2274 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
2276 static struct sw_flow_actions
*nla_alloc_flow_actions(int size
)
2278 struct sw_flow_actions
*sfa
;
2280 WARN_ON_ONCE(size
> MAX_ACTIONS_BUFSIZE
);
2282 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
2284 return ERR_PTR(-ENOMEM
);
2286 sfa
->actions_len
= 0;
2290 static void ovs_nla_free_set_action(const struct nlattr
*a
)
2292 const struct nlattr
*ovs_key
= nla_data(a
);
2293 struct ovs_tunnel_info
*ovs_tun
;
2295 switch (nla_type(ovs_key
)) {
2296 case OVS_KEY_ATTR_TUNNEL_INFO
:
2297 ovs_tun
= nla_data(ovs_key
);
2298 dst_release((struct dst_entry
*)ovs_tun
->tun_dst
);
2303 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
2305 const struct nlattr
*a
;
2311 nla_for_each_attr(a
, sf_acts
->actions
, sf_acts
->actions_len
, rem
) {
2312 switch (nla_type(a
)) {
2313 case OVS_ACTION_ATTR_SET
:
2314 ovs_nla_free_set_action(a
);
2316 case OVS_ACTION_ATTR_CT
:
2317 ovs_ct_free_action(a
);
2325 static void __ovs_nla_free_flow_actions(struct rcu_head
*head
)
2327 ovs_nla_free_flow_actions(container_of(head
, struct sw_flow_actions
, rcu
));
2330 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
2331 * The caller must hold rcu_read_lock for this to be sensible. */
2332 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions
*sf_acts
)
2334 call_rcu(&sf_acts
->rcu
, __ovs_nla_free_flow_actions
);
2337 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
2338 int attr_len
, bool log
)
2341 struct sw_flow_actions
*acts
;
2343 size_t req_size
= NLA_ALIGN(attr_len
);
2344 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
2345 (*sfa
)->actions_len
;
2347 if (req_size
<= (ksize(*sfa
) - next_offset
))
2350 new_acts_size
= max(next_offset
+ req_size
, ksize(*sfa
) * 2);
2352 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
2353 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
) {
2354 OVS_NLERR(log
, "Flow action size exceeds max %u",
2355 MAX_ACTIONS_BUFSIZE
);
2356 return ERR_PTR(-EMSGSIZE
);
2358 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
2361 acts
= nla_alloc_flow_actions(new_acts_size
);
2363 return (void *)acts
;
2365 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
2366 acts
->actions_len
= (*sfa
)->actions_len
;
2367 acts
->orig_len
= (*sfa
)->orig_len
;
2372 (*sfa
)->actions_len
+= req_size
;
2373 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
2376 static struct nlattr
*__add_action(struct sw_flow_actions
**sfa
,
2377 int attrtype
, void *data
, int len
, bool log
)
2381 a
= reserve_sfa_size(sfa
, nla_attr_size(len
), log
);
2385 a
->nla_type
= attrtype
;
2386 a
->nla_len
= nla_attr_size(len
);
2389 memcpy(nla_data(a
), data
, len
);
2390 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
2395 int ovs_nla_add_action(struct sw_flow_actions
**sfa
, int attrtype
, void *data
,
2400 a
= __add_action(sfa
, attrtype
, data
, len
, log
);
2402 return PTR_ERR_OR_ZERO(a
);
2405 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
2406 int attrtype
, bool log
)
2408 int used
= (*sfa
)->actions_len
;
2411 err
= ovs_nla_add_action(sfa
, attrtype
, NULL
, 0, log
);
2418 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
2421 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
2424 a
->nla_len
= sfa
->actions_len
- st_offset
;
2427 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2428 const struct sw_flow_key
*key
,
2429 struct sw_flow_actions
**sfa
,
2430 __be16 eth_type
, __be16 vlan_tci
,
2431 u32 mpls_label_count
, bool log
);
2433 static int validate_and_copy_sample(struct net
*net
, const struct nlattr
*attr
,
2434 const struct sw_flow_key
*key
,
2435 struct sw_flow_actions
**sfa
,
2436 __be16 eth_type
, __be16 vlan_tci
,
2437 u32 mpls_label_count
, bool log
, bool last
)
2439 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
2440 const struct nlattr
*probability
, *actions
;
2441 const struct nlattr
*a
;
2442 int rem
, start
, err
;
2443 struct sample_arg arg
;
2445 memset(attrs
, 0, sizeof(attrs
));
2446 nla_for_each_nested(a
, attr
, rem
) {
2447 int type
= nla_type(a
);
2448 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
2455 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
2456 if (!probability
|| nla_len(probability
) != sizeof(u32
))
2459 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
2460 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
2463 /* validation done, copy sample action. */
2464 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
, log
);
2468 /* When both skb and flow may be changed, put the sample
2469 * into a deferred fifo. On the other hand, if only skb
2470 * may be modified, the actions can be executed in place.
2472 * Do this analysis at the flow installation time.
2473 * Set 'clone_action->exec' to true if the actions can be
2474 * executed without being deferred.
2476 * If the sample is the last action, it can always be excuted
2477 * rather than deferred.
2479 arg
.exec
= last
|| !actions_may_change_flow(actions
);
2480 arg
.probability
= nla_get_u32(probability
);
2482 err
= ovs_nla_add_action(sfa
, OVS_SAMPLE_ATTR_ARG
, &arg
, sizeof(arg
),
2487 err
= __ovs_nla_copy_actions(net
, actions
, key
, sfa
,
2488 eth_type
, vlan_tci
, mpls_label_count
, log
);
2493 add_nested_action_end(*sfa
, start
);
2498 static int validate_and_copy_clone(struct net
*net
,
2499 const struct nlattr
*attr
,
2500 const struct sw_flow_key
*key
,
2501 struct sw_flow_actions
**sfa
,
2502 __be16 eth_type
, __be16 vlan_tci
,
2503 u32 mpls_label_count
, bool log
, bool last
)
2508 if (nla_len(attr
) && nla_len(attr
) < NLA_HDRLEN
)
2511 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_CLONE
, log
);
2515 exec
= last
|| !actions_may_change_flow(attr
);
2517 err
= ovs_nla_add_action(sfa
, OVS_CLONE_ATTR_EXEC
, &exec
,
2522 err
= __ovs_nla_copy_actions(net
, attr
, key
, sfa
,
2523 eth_type
, vlan_tci
, mpls_label_count
, log
);
2527 add_nested_action_end(*sfa
, start
);
2532 void ovs_match_init(struct sw_flow_match
*match
,
2533 struct sw_flow_key
*key
,
2535 struct sw_flow_mask
*mask
)
2537 memset(match
, 0, sizeof(*match
));
2542 memset(key
, 0, sizeof(*key
));
2545 memset(&mask
->key
, 0, sizeof(mask
->key
));
2546 mask
->range
.start
= mask
->range
.end
= 0;
2550 static int validate_geneve_opts(struct sw_flow_key
*key
)
2552 struct geneve_opt
*option
;
2553 int opts_len
= key
->tun_opts_len
;
2554 bool crit_opt
= false;
2556 option
= (struct geneve_opt
*)TUN_METADATA_OPTS(key
, key
->tun_opts_len
);
2557 while (opts_len
> 0) {
2560 if (opts_len
< sizeof(*option
))
2563 len
= sizeof(*option
) + option
->length
* 4;
2567 crit_opt
|= !!(option
->type
& GENEVE_CRIT_OPT_TYPE
);
2569 option
= (struct geneve_opt
*)((u8
*)option
+ len
);
2573 key
->tun_key
.tun_flags
|= crit_opt
? TUNNEL_CRIT_OPT
: 0;
2578 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
2579 struct sw_flow_actions
**sfa
, bool log
)
2581 struct sw_flow_match match
;
2582 struct sw_flow_key key
;
2583 struct metadata_dst
*tun_dst
;
2584 struct ip_tunnel_info
*tun_info
;
2585 struct ovs_tunnel_info
*ovs_tun
;
2587 int err
= 0, start
, opts_type
;
2588 __be16 dst_opt_type
;
2591 ovs_match_init(&match
, &key
, true, NULL
);
2592 opts_type
= ip_tun_from_nlattr(nla_data(attr
), &match
, false, log
);
2596 if (key
.tun_opts_len
) {
2597 switch (opts_type
) {
2598 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2599 err
= validate_geneve_opts(&key
);
2602 dst_opt_type
= TUNNEL_GENEVE_OPT
;
2604 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2605 dst_opt_type
= TUNNEL_VXLAN_OPT
;
2607 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
2608 dst_opt_type
= TUNNEL_ERSPAN_OPT
;
2613 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
, log
);
2617 tun_dst
= metadata_dst_alloc(key
.tun_opts_len
, METADATA_IP_TUNNEL
,
2623 err
= dst_cache_init(&tun_dst
->u
.tun_info
.dst_cache
, GFP_KERNEL
);
2625 dst_release((struct dst_entry
*)tun_dst
);
2629 a
= __add_action(sfa
, OVS_KEY_ATTR_TUNNEL_INFO
, NULL
,
2630 sizeof(*ovs_tun
), log
);
2632 dst_release((struct dst_entry
*)tun_dst
);
2636 ovs_tun
= nla_data(a
);
2637 ovs_tun
->tun_dst
= tun_dst
;
2639 tun_info
= &tun_dst
->u
.tun_info
;
2640 tun_info
->mode
= IP_TUNNEL_INFO_TX
;
2641 if (key
.tun_proto
== AF_INET6
)
2642 tun_info
->mode
|= IP_TUNNEL_INFO_IPV6
;
2643 else if (key
.tun_proto
== AF_INET
&& key
.tun_key
.u
.ipv4
.dst
== 0)
2644 tun_info
->mode
|= IP_TUNNEL_INFO_BRIDGE
;
2645 tun_info
->key
= key
.tun_key
;
2647 /* We need to store the options in the action itself since
2648 * everything else will go away after flow setup. We can append
2649 * it to tun_info and then point there.
2651 ip_tunnel_info_opts_set(tun_info
,
2652 TUN_METADATA_OPTS(&key
, key
.tun_opts_len
),
2653 key
.tun_opts_len
, dst_opt_type
);
2654 add_nested_action_end(*sfa
, start
);
2659 static bool validate_nsh(const struct nlattr
*attr
, bool is_mask
,
2660 bool is_push_nsh
, bool log
)
2662 struct sw_flow_match match
;
2663 struct sw_flow_key key
;
2666 ovs_match_init(&match
, &key
, true, NULL
);
2667 ret
= nsh_key_put_from_nlattr(attr
, &match
, is_mask
,
2672 /* Return false if there are any non-masked bits set.
2673 * Mask follows data immediately, before any netlink padding.
2675 static bool validate_masked(u8
*data
, int len
)
2677 u8
*mask
= data
+ len
;
2680 if (*data
++ & ~*mask
++)
2686 static int validate_set(const struct nlattr
*a
,
2687 const struct sw_flow_key
*flow_key
,
2688 struct sw_flow_actions
**sfa
, bool *skip_copy
,
2689 u8 mac_proto
, __be16 eth_type
, bool masked
, bool log
)
2691 const struct nlattr
*ovs_key
= nla_data(a
);
2692 int key_type
= nla_type(ovs_key
);
2695 /* There can be only one key in a action */
2696 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
2699 key_len
= nla_len(ovs_key
);
2703 if (key_type
> OVS_KEY_ATTR_MAX
||
2704 !check_attr_len(key_len
, ovs_key_lens
[key_type
].len
))
2707 if (masked
&& !validate_masked(nla_data(ovs_key
), key_len
))
2711 case OVS_KEY_ATTR_PRIORITY
:
2712 case OVS_KEY_ATTR_SKB_MARK
:
2713 case OVS_KEY_ATTR_CT_MARK
:
2714 case OVS_KEY_ATTR_CT_LABELS
:
2717 case OVS_KEY_ATTR_ETHERNET
:
2718 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2722 case OVS_KEY_ATTR_TUNNEL
: {
2726 return -EINVAL
; /* Masked tunnel set not supported. */
2729 err
= validate_and_copy_set_tun(a
, sfa
, log
);
2734 case OVS_KEY_ATTR_IPV4
: {
2735 const struct ovs_key_ipv4
*ipv4_key
;
2737 if (eth_type
!= htons(ETH_P_IP
))
2740 ipv4_key
= nla_data(ovs_key
);
2743 const struct ovs_key_ipv4
*mask
= ipv4_key
+ 1;
2745 /* Non-writeable fields. */
2746 if (mask
->ipv4_proto
|| mask
->ipv4_frag
)
2749 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
2752 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
2757 case OVS_KEY_ATTR_IPV6
: {
2758 const struct ovs_key_ipv6
*ipv6_key
;
2760 if (eth_type
!= htons(ETH_P_IPV6
))
2763 ipv6_key
= nla_data(ovs_key
);
2766 const struct ovs_key_ipv6
*mask
= ipv6_key
+ 1;
2768 /* Non-writeable fields. */
2769 if (mask
->ipv6_proto
|| mask
->ipv6_frag
)
2772 /* Invalid bits in the flow label mask? */
2773 if (ntohl(mask
->ipv6_label
) & 0xFFF00000)
2776 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
2779 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
2782 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
2787 case OVS_KEY_ATTR_TCP
:
2788 if ((eth_type
!= htons(ETH_P_IP
) &&
2789 eth_type
!= htons(ETH_P_IPV6
)) ||
2790 flow_key
->ip
.proto
!= IPPROTO_TCP
)
2795 case OVS_KEY_ATTR_UDP
:
2796 if ((eth_type
!= htons(ETH_P_IP
) &&
2797 eth_type
!= htons(ETH_P_IPV6
)) ||
2798 flow_key
->ip
.proto
!= IPPROTO_UDP
)
2803 case OVS_KEY_ATTR_MPLS
:
2804 if (!eth_p_mpls(eth_type
))
2808 case OVS_KEY_ATTR_SCTP
:
2809 if ((eth_type
!= htons(ETH_P_IP
) &&
2810 eth_type
!= htons(ETH_P_IPV6
)) ||
2811 flow_key
->ip
.proto
!= IPPROTO_SCTP
)
2816 case OVS_KEY_ATTR_NSH
:
2817 if (eth_type
!= htons(ETH_P_NSH
))
2819 if (!validate_nsh(nla_data(a
), masked
, false, log
))
2827 /* Convert non-masked non-tunnel set actions to masked set actions. */
2828 if (!masked
&& key_type
!= OVS_KEY_ATTR_TUNNEL
) {
2829 int start
, len
= key_len
* 2;
2834 start
= add_nested_action_start(sfa
,
2835 OVS_ACTION_ATTR_SET_TO_MASKED
,
2840 at
= __add_action(sfa
, key_type
, NULL
, len
, log
);
2844 memcpy(nla_data(at
), nla_data(ovs_key
), key_len
); /* Key. */
2845 memset(nla_data(at
) + key_len
, 0xff, key_len
); /* Mask. */
2846 /* Clear non-writeable bits from otherwise writeable fields. */
2847 if (key_type
== OVS_KEY_ATTR_IPV6
) {
2848 struct ovs_key_ipv6
*mask
= nla_data(at
) + key_len
;
2850 mask
->ipv6_label
&= htonl(0x000FFFFF);
2852 add_nested_action_end(*sfa
, start
);
2858 static int validate_userspace(const struct nlattr
*attr
)
2860 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
2861 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
2862 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
2863 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = {.type
= NLA_U32
},
2865 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
2868 error
= nla_parse_nested_deprecated(a
, OVS_USERSPACE_ATTR_MAX
, attr
,
2869 userspace_policy
, NULL
);
2873 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
2874 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
2880 static const struct nla_policy cpl_policy
[OVS_CHECK_PKT_LEN_ATTR_MAX
+ 1] = {
2881 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
] = {.type
= NLA_U16
},
2882 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
] = {.type
= NLA_NESTED
},
2883 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
] = {.type
= NLA_NESTED
},
2886 static int validate_and_copy_check_pkt_len(struct net
*net
,
2887 const struct nlattr
*attr
,
2888 const struct sw_flow_key
*key
,
2889 struct sw_flow_actions
**sfa
,
2890 __be16 eth_type
, __be16 vlan_tci
,
2891 u32 mpls_label_count
,
2892 bool log
, bool last
)
2894 const struct nlattr
*acts_if_greater
, *acts_if_lesser_eq
;
2895 struct nlattr
*a
[OVS_CHECK_PKT_LEN_ATTR_MAX
+ 1];
2896 struct check_pkt_len_arg arg
;
2897 int nested_acts_start
;
2900 err
= nla_parse_deprecated_strict(a
, OVS_CHECK_PKT_LEN_ATTR_MAX
,
2901 nla_data(attr
), nla_len(attr
),
2906 if (!a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
] ||
2907 !nla_get_u16(a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
]))
2910 acts_if_lesser_eq
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
];
2911 acts_if_greater
= a
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
];
2913 /* Both the nested action should be present. */
2914 if (!acts_if_greater
|| !acts_if_lesser_eq
)
2917 /* validation done, copy the nested actions. */
2918 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_CHECK_PKT_LEN
,
2923 arg
.pkt_len
= nla_get_u16(a
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
]);
2924 arg
.exec_for_lesser_equal
=
2925 last
|| !actions_may_change_flow(acts_if_lesser_eq
);
2926 arg
.exec_for_greater
=
2927 last
|| !actions_may_change_flow(acts_if_greater
);
2929 err
= ovs_nla_add_action(sfa
, OVS_CHECK_PKT_LEN_ATTR_ARG
, &arg
,
2934 nested_acts_start
= add_nested_action_start(sfa
,
2935 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
, log
);
2936 if (nested_acts_start
< 0)
2937 return nested_acts_start
;
2939 err
= __ovs_nla_copy_actions(net
, acts_if_lesser_eq
, key
, sfa
,
2940 eth_type
, vlan_tci
, mpls_label_count
, log
);
2945 add_nested_action_end(*sfa
, nested_acts_start
);
2947 nested_acts_start
= add_nested_action_start(sfa
,
2948 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
, log
);
2949 if (nested_acts_start
< 0)
2950 return nested_acts_start
;
2952 err
= __ovs_nla_copy_actions(net
, acts_if_greater
, key
, sfa
,
2953 eth_type
, vlan_tci
, mpls_label_count
, log
);
2958 add_nested_action_end(*sfa
, nested_acts_start
);
2959 add_nested_action_end(*sfa
, start
);
2963 static int copy_action(const struct nlattr
*from
,
2964 struct sw_flow_actions
**sfa
, bool log
)
2966 int totlen
= NLA_ALIGN(from
->nla_len
);
2969 to
= reserve_sfa_size(sfa
, from
->nla_len
, log
);
2973 memcpy(to
, from
, totlen
);
2977 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2978 const struct sw_flow_key
*key
,
2979 struct sw_flow_actions
**sfa
,
2980 __be16 eth_type
, __be16 vlan_tci
,
2981 u32 mpls_label_count
, bool log
)
2983 u8 mac_proto
= ovs_key_mac_proto(key
);
2984 const struct nlattr
*a
;
2987 nla_for_each_nested(a
, attr
, rem
) {
2988 /* Expected argument lengths, (u32)-1 for variable length. */
2989 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
2990 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
2991 [OVS_ACTION_ATTR_RECIRC
] = sizeof(u32
),
2992 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
2993 [OVS_ACTION_ATTR_PUSH_MPLS
] = sizeof(struct ovs_action_push_mpls
),
2994 [OVS_ACTION_ATTR_POP_MPLS
] = sizeof(__be16
),
2995 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
2996 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
2997 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
2998 [OVS_ACTION_ATTR_SET_MASKED
] = (u32
)-1,
2999 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1,
3000 [OVS_ACTION_ATTR_HASH
] = sizeof(struct ovs_action_hash
),
3001 [OVS_ACTION_ATTR_CT
] = (u32
)-1,
3002 [OVS_ACTION_ATTR_CT_CLEAR
] = 0,
3003 [OVS_ACTION_ATTR_TRUNC
] = sizeof(struct ovs_action_trunc
),
3004 [OVS_ACTION_ATTR_PUSH_ETH
] = sizeof(struct ovs_action_push_eth
),
3005 [OVS_ACTION_ATTR_POP_ETH
] = 0,
3006 [OVS_ACTION_ATTR_PUSH_NSH
] = (u32
)-1,
3007 [OVS_ACTION_ATTR_POP_NSH
] = 0,
3008 [OVS_ACTION_ATTR_METER
] = sizeof(u32
),
3009 [OVS_ACTION_ATTR_CLONE
] = (u32
)-1,
3010 [OVS_ACTION_ATTR_CHECK_PKT_LEN
] = (u32
)-1,
3011 [OVS_ACTION_ATTR_ADD_MPLS
] = sizeof(struct ovs_action_add_mpls
),
3013 const struct ovs_action_push_vlan
*vlan
;
3014 int type
= nla_type(a
);
3017 if (type
> OVS_ACTION_ATTR_MAX
||
3018 (action_lens
[type
] != nla_len(a
) &&
3019 action_lens
[type
] != (u32
)-1))
3024 case OVS_ACTION_ATTR_UNSPEC
:
3027 case OVS_ACTION_ATTR_USERSPACE
:
3028 err
= validate_userspace(a
);
3033 case OVS_ACTION_ATTR_OUTPUT
:
3034 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
3038 case OVS_ACTION_ATTR_TRUNC
: {
3039 const struct ovs_action_trunc
*trunc
= nla_data(a
);
3041 if (trunc
->max_len
< ETH_HLEN
)
3046 case OVS_ACTION_ATTR_HASH
: {
3047 const struct ovs_action_hash
*act_hash
= nla_data(a
);
3049 switch (act_hash
->hash_alg
) {
3050 case OVS_HASH_ALG_L4
:
3059 case OVS_ACTION_ATTR_POP_VLAN
:
3060 if (mac_proto
!= MAC_PROTO_ETHERNET
)
3062 vlan_tci
= htons(0);
3065 case OVS_ACTION_ATTR_PUSH_VLAN
:
3066 if (mac_proto
!= MAC_PROTO_ETHERNET
)
3069 if (!eth_type_vlan(vlan
->vlan_tpid
))
3071 if (!(vlan
->vlan_tci
& htons(VLAN_CFI_MASK
)))
3073 vlan_tci
= vlan
->vlan_tci
;
3076 case OVS_ACTION_ATTR_RECIRC
:
3079 case OVS_ACTION_ATTR_ADD_MPLS
: {
3080 const struct ovs_action_add_mpls
*mpls
= nla_data(a
);
3082 if (!eth_p_mpls(mpls
->mpls_ethertype
))
3085 if (mpls
->tun_flags
& OVS_MPLS_L3_TUNNEL_FLAG_MASK
) {
3086 if (vlan_tci
& htons(VLAN_CFI_MASK
) ||
3087 (eth_type
!= htons(ETH_P_IP
) &&
3088 eth_type
!= htons(ETH_P_IPV6
) &&
3089 eth_type
!= htons(ETH_P_ARP
) &&
3090 eth_type
!= htons(ETH_P_RARP
) &&
3091 !eth_p_mpls(eth_type
)))
3095 if (mac_proto
== MAC_PROTO_ETHERNET
) {
3096 mpls_label_count
= 1;
3097 mac_proto
= MAC_PROTO_NONE
;
3102 eth_type
= mpls
->mpls_ethertype
;
3106 case OVS_ACTION_ATTR_PUSH_MPLS
: {
3107 const struct ovs_action_push_mpls
*mpls
= nla_data(a
);
3109 if (!eth_p_mpls(mpls
->mpls_ethertype
))
3111 /* Prohibit push MPLS other than to a white list
3112 * for packets that have a known tag order.
3114 if (vlan_tci
& htons(VLAN_CFI_MASK
) ||
3115 (eth_type
!= htons(ETH_P_IP
) &&
3116 eth_type
!= htons(ETH_P_IPV6
) &&
3117 eth_type
!= htons(ETH_P_ARP
) &&
3118 eth_type
!= htons(ETH_P_RARP
) &&
3119 !eth_p_mpls(eth_type
)))
3121 eth_type
= mpls
->mpls_ethertype
;
3126 case OVS_ACTION_ATTR_POP_MPLS
: {
3128 if (vlan_tci
& htons(VLAN_CFI_MASK
) ||
3129 !eth_p_mpls(eth_type
))
3132 /* Disallow subsequent L2.5+ set actions and mpls_pop
3133 * actions once the last MPLS label in the packet is
3134 * is popped as there is no check here to ensure that
3135 * the new eth type is valid and thus set actions could
3136 * write off the end of the packet or otherwise corrupt
3139 * Support for these actions is planned using packet
3142 proto
= nla_get_be16(a
);
3144 if (proto
== htons(ETH_P_TEB
) &&
3145 mac_proto
!= MAC_PROTO_NONE
)
3150 if (!eth_p_mpls(proto
) || !mpls_label_count
)
3151 eth_type
= htons(0);
3158 case OVS_ACTION_ATTR_SET
:
3159 err
= validate_set(a
, key
, sfa
,
3160 &skip_copy
, mac_proto
, eth_type
,
3166 case OVS_ACTION_ATTR_SET_MASKED
:
3167 err
= validate_set(a
, key
, sfa
,
3168 &skip_copy
, mac_proto
, eth_type
,
3174 case OVS_ACTION_ATTR_SAMPLE
: {
3175 bool last
= nla_is_last(a
, rem
);
3177 err
= validate_and_copy_sample(net
, a
, key
, sfa
,
3187 case OVS_ACTION_ATTR_CT
:
3188 err
= ovs_ct_copy_action(net
, a
, key
, sfa
, log
);
3194 case OVS_ACTION_ATTR_CT_CLEAR
:
3197 case OVS_ACTION_ATTR_PUSH_ETH
:
3198 /* Disallow pushing an Ethernet header if one
3199 * is already present */
3200 if (mac_proto
!= MAC_PROTO_NONE
)
3202 mac_proto
= MAC_PROTO_ETHERNET
;
3205 case OVS_ACTION_ATTR_POP_ETH
:
3206 if (mac_proto
!= MAC_PROTO_ETHERNET
)
3208 if (vlan_tci
& htons(VLAN_CFI_MASK
))
3210 mac_proto
= MAC_PROTO_NONE
;
3213 case OVS_ACTION_ATTR_PUSH_NSH
:
3214 if (mac_proto
!= MAC_PROTO_ETHERNET
) {
3217 next_proto
= tun_p_from_eth_p(eth_type
);
3221 mac_proto
= MAC_PROTO_NONE
;
3222 if (!validate_nsh(nla_data(a
), false, true, true))
3226 case OVS_ACTION_ATTR_POP_NSH
: {
3229 if (eth_type
!= htons(ETH_P_NSH
))
3231 inner_proto
= tun_p_to_eth_p(key
->nsh
.base
.np
);
3234 if (key
->nsh
.base
.np
== TUN_P_ETHERNET
)
3235 mac_proto
= MAC_PROTO_ETHERNET
;
3237 mac_proto
= MAC_PROTO_NONE
;
3241 case OVS_ACTION_ATTR_METER
:
3242 /* Non-existent meters are simply ignored. */
3245 case OVS_ACTION_ATTR_CLONE
: {
3246 bool last
= nla_is_last(a
, rem
);
3248 err
= validate_and_copy_clone(net
, a
, key
, sfa
,
3258 case OVS_ACTION_ATTR_CHECK_PKT_LEN
: {
3259 bool last
= nla_is_last(a
, rem
);
3261 err
= validate_and_copy_check_pkt_len(net
, a
, key
, sfa
,
3273 OVS_NLERR(log
, "Unknown Action type %d", type
);
3277 err
= copy_action(a
, sfa
, log
);
3289 /* 'key' must be the masked key. */
3290 int ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
3291 const struct sw_flow_key
*key
,
3292 struct sw_flow_actions
**sfa
, bool log
)
3295 u32 mpls_label_count
= 0;
3297 *sfa
= nla_alloc_flow_actions(min(nla_len(attr
), MAX_ACTIONS_BUFSIZE
));
3299 return PTR_ERR(*sfa
);
3301 if (eth_p_mpls(key
->eth
.type
))
3302 mpls_label_count
= hweight_long(key
->mpls
.num_labels_mask
);
3304 (*sfa
)->orig_len
= nla_len(attr
);
3305 err
= __ovs_nla_copy_actions(net
, attr
, key
, sfa
, key
->eth
.type
,
3306 key
->eth
.vlan
.tci
, mpls_label_count
, log
);
3308 ovs_nla_free_flow_actions(*sfa
);
3313 static int sample_action_to_attr(const struct nlattr
*attr
,
3314 struct sk_buff
*skb
)
3316 struct nlattr
*start
, *ac_start
= NULL
, *sample_arg
;
3317 int err
= 0, rem
= nla_len(attr
);
3318 const struct sample_arg
*arg
;
3319 struct nlattr
*actions
;
3321 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_SAMPLE
);
3325 sample_arg
= nla_data(attr
);
3326 arg
= nla_data(sample_arg
);
3327 actions
= nla_next(sample_arg
, &rem
);
3329 if (nla_put_u32(skb
, OVS_SAMPLE_ATTR_PROBABILITY
, arg
->probability
)) {
3334 ac_start
= nla_nest_start_noflag(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
3340 err
= ovs_nla_put_actions(actions
, rem
, skb
);
3344 nla_nest_cancel(skb
, ac_start
);
3345 nla_nest_cancel(skb
, start
);
3347 nla_nest_end(skb
, ac_start
);
3348 nla_nest_end(skb
, start
);
3354 static int clone_action_to_attr(const struct nlattr
*attr
,
3355 struct sk_buff
*skb
)
3357 struct nlattr
*start
;
3358 int err
= 0, rem
= nla_len(attr
);
3360 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_CLONE
);
3364 err
= ovs_nla_put_actions(nla_data(attr
), rem
, skb
);
3367 nla_nest_cancel(skb
, start
);
3369 nla_nest_end(skb
, start
);
3374 static int check_pkt_len_action_to_attr(const struct nlattr
*attr
,
3375 struct sk_buff
*skb
)
3377 struct nlattr
*start
, *ac_start
= NULL
;
3378 const struct check_pkt_len_arg
*arg
;
3379 const struct nlattr
*a
, *cpl_arg
;
3380 int err
= 0, rem
= nla_len(attr
);
3382 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_CHECK_PKT_LEN
);
3386 /* The first nested attribute in 'attr' is always
3387 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
3389 cpl_arg
= nla_data(attr
);
3390 arg
= nla_data(cpl_arg
);
3392 if (nla_put_u16(skb
, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN
, arg
->pkt_len
)) {
3397 /* Second nested attribute in 'attr' is always
3398 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
3400 a
= nla_next(cpl_arg
, &rem
);
3401 ac_start
= nla_nest_start_noflag(skb
,
3402 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL
);
3408 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
3410 nla_nest_cancel(skb
, ac_start
);
3413 nla_nest_end(skb
, ac_start
);
3416 /* Third nested attribute in 'attr' is always
3417 * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
3419 a
= nla_next(a
, &rem
);
3420 ac_start
= nla_nest_start_noflag(skb
,
3421 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER
);
3427 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
3429 nla_nest_cancel(skb
, ac_start
);
3432 nla_nest_end(skb
, ac_start
);
3435 nla_nest_end(skb
, start
);
3439 nla_nest_cancel(skb
, start
);
3443 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
3445 const struct nlattr
*ovs_key
= nla_data(a
);
3446 int key_type
= nla_type(ovs_key
);
3447 struct nlattr
*start
;
3451 case OVS_KEY_ATTR_TUNNEL_INFO
: {
3452 struct ovs_tunnel_info
*ovs_tun
= nla_data(ovs_key
);
3453 struct ip_tunnel_info
*tun_info
= &ovs_tun
->tun_dst
->u
.tun_info
;
3455 start
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_SET
);
3459 err
= ip_tun_to_nlattr(skb
, &tun_info
->key
,
3460 ip_tunnel_info_opts(tun_info
),
3461 tun_info
->options_len
,
3462 ip_tunnel_info_af(tun_info
), tun_info
->mode
);
3465 nla_nest_end(skb
, start
);
3469 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
3477 static int masked_set_action_to_set_action_attr(const struct nlattr
*a
,
3478 struct sk_buff
*skb
)
3480 const struct nlattr
*ovs_key
= nla_data(a
);
3482 size_t key_len
= nla_len(ovs_key
) / 2;
3484 /* Revert the conversion we did from a non-masked set action to
3485 * masked set action.
3487 nla
= nla_nest_start_noflag(skb
, OVS_ACTION_ATTR_SET
);
3491 if (nla_put(skb
, nla_type(ovs_key
), key_len
, nla_data(ovs_key
)))
3494 nla_nest_end(skb
, nla
);
3498 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
3500 const struct nlattr
*a
;
3503 nla_for_each_attr(a
, attr
, len
, rem
) {
3504 int type
= nla_type(a
);
3507 case OVS_ACTION_ATTR_SET
:
3508 err
= set_action_to_attr(a
, skb
);
3513 case OVS_ACTION_ATTR_SET_TO_MASKED
:
3514 err
= masked_set_action_to_set_action_attr(a
, skb
);
3519 case OVS_ACTION_ATTR_SAMPLE
:
3520 err
= sample_action_to_attr(a
, skb
);
3525 case OVS_ACTION_ATTR_CT
:
3526 err
= ovs_ct_action_to_attr(nla_data(a
), skb
);
3531 case OVS_ACTION_ATTR_CLONE
:
3532 err
= clone_action_to_attr(a
, skb
);
3537 case OVS_ACTION_ATTR_CHECK_PKT_LEN
:
3538 err
= check_pkt_len_action_to_attr(a
, skb
);
3544 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))