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
23 #include <linux/uaccess.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <net/llc_pdu.h>
29 #include <linux/kernel.h>
30 #include <linux/jhash.h>
31 #include <linux/jiffies.h>
32 #include <linux/llc.h>
33 #include <linux/module.h>
35 #include <linux/rcupdate.h>
36 #include <linux/if_arp.h>
38 #include <linux/ipv6.h>
39 #include <linux/sctp.h>
40 #include <linux/tcp.h>
41 #include <linux/udp.h>
42 #include <linux/icmp.h>
43 #include <linux/icmpv6.h>
44 #include <linux/rculist.h>
45 #include <net/geneve.h>
48 #include <net/ndisc.h>
50 #include <net/vxlan.h>
51 #include <net/erspan.h>
53 #include "flow_netlink.h"
57 const struct ovs_len_tbl
*next
;
60 #define OVS_ATTR_NESTED -1
61 #define OVS_ATTR_VARIABLE -2
63 static bool actions_may_change_flow(const struct nlattr
*actions
)
68 nla_for_each_nested(nla
, actions
, rem
) {
69 u16 action
= nla_type(nla
);
72 case OVS_ACTION_ATTR_OUTPUT
:
73 case OVS_ACTION_ATTR_RECIRC
:
74 case OVS_ACTION_ATTR_TRUNC
:
75 case OVS_ACTION_ATTR_USERSPACE
:
78 case OVS_ACTION_ATTR_CT
:
79 case OVS_ACTION_ATTR_CT_CLEAR
:
80 case OVS_ACTION_ATTR_HASH
:
81 case OVS_ACTION_ATTR_POP_ETH
:
82 case OVS_ACTION_ATTR_POP_MPLS
:
83 case OVS_ACTION_ATTR_POP_VLAN
:
84 case OVS_ACTION_ATTR_PUSH_ETH
:
85 case OVS_ACTION_ATTR_PUSH_MPLS
:
86 case OVS_ACTION_ATTR_PUSH_VLAN
:
87 case OVS_ACTION_ATTR_SAMPLE
:
88 case OVS_ACTION_ATTR_SET
:
89 case OVS_ACTION_ATTR_SET_MASKED
:
97 static void update_range(struct sw_flow_match
*match
,
98 size_t offset
, size_t size
, bool is_mask
)
100 struct sw_flow_key_range
*range
;
101 size_t start
= rounddown(offset
, sizeof(long));
102 size_t end
= roundup(offset
+ size
, sizeof(long));
105 range
= &match
->range
;
107 range
= &match
->mask
->range
;
109 if (range
->start
== range
->end
) {
110 range
->start
= start
;
115 if (range
->start
> start
)
116 range
->start
= start
;
118 if (range
->end
< end
)
122 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
124 update_range(match, offsetof(struct sw_flow_key, field), \
125 sizeof((match)->key->field), is_mask); \
127 (match)->mask->key.field = value; \
129 (match)->key->field = value; \
132 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
134 update_range(match, offset, len, is_mask); \
136 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
139 memcpy((u8 *)(match)->key + offset, value_p, len); \
142 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
143 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
144 value_p, len, is_mask)
146 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
148 update_range(match, offsetof(struct sw_flow_key, field), \
149 sizeof((match)->key->field), is_mask); \
151 memset((u8 *)&(match)->mask->key.field, value, \
152 sizeof((match)->mask->key.field)); \
154 memset((u8 *)&(match)->key->field, value, \
155 sizeof((match)->key->field)); \
158 static bool match_validate(const struct sw_flow_match
*match
,
159 u64 key_attrs
, u64 mask_attrs
, bool log
)
161 u64 key_expected
= 0;
162 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
164 /* The following mask attributes allowed only if they
165 * pass the validation tests. */
166 mask_allowed
&= ~((1 << OVS_KEY_ATTR_IPV4
)
167 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)
168 | (1 << OVS_KEY_ATTR_IPV6
)
169 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)
170 | (1 << OVS_KEY_ATTR_TCP
)
171 | (1 << OVS_KEY_ATTR_TCP_FLAGS
)
172 | (1 << OVS_KEY_ATTR_UDP
)
173 | (1 << OVS_KEY_ATTR_SCTP
)
174 | (1 << OVS_KEY_ATTR_ICMP
)
175 | (1 << OVS_KEY_ATTR_ICMPV6
)
176 | (1 << OVS_KEY_ATTR_ARP
)
177 | (1 << OVS_KEY_ATTR_ND
)
178 | (1 << OVS_KEY_ATTR_MPLS
));
180 /* Always allowed mask fields. */
181 mask_allowed
|= ((1 << OVS_KEY_ATTR_TUNNEL
)
182 | (1 << OVS_KEY_ATTR_IN_PORT
)
183 | (1 << OVS_KEY_ATTR_ETHERTYPE
));
185 /* Check key attributes. */
186 if (match
->key
->eth
.type
== htons(ETH_P_ARP
)
187 || match
->key
->eth
.type
== htons(ETH_P_RARP
)) {
188 key_expected
|= 1 << OVS_KEY_ATTR_ARP
;
189 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
190 mask_allowed
|= 1 << OVS_KEY_ATTR_ARP
;
193 if (eth_p_mpls(match
->key
->eth
.type
)) {
194 key_expected
|= 1 << OVS_KEY_ATTR_MPLS
;
195 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
196 mask_allowed
|= 1 << OVS_KEY_ATTR_MPLS
;
199 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
200 key_expected
|= 1 << OVS_KEY_ATTR_IPV4
;
201 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
202 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV4
;
203 mask_allowed
|= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
;
206 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
207 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
208 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
209 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
210 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
213 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
214 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
215 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
216 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
219 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
220 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
221 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
222 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
223 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
224 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
228 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
229 key_expected
|= 1 << OVS_KEY_ATTR_ICMP
;
230 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
231 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMP
;
236 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
237 key_expected
|= 1 << OVS_KEY_ATTR_IPV6
;
238 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
239 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV6
;
240 mask_allowed
|= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
;
243 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
244 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
245 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
246 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
247 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
250 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
251 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
252 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
253 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
256 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
257 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
258 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
259 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
260 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
261 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
265 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
266 key_expected
|= 1 << OVS_KEY_ATTR_ICMPV6
;
267 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
268 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMPV6
;
270 if (match
->key
->tp
.src
==
271 htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
272 match
->key
->tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
273 key_expected
|= 1 << OVS_KEY_ATTR_ND
;
274 /* Original direction conntrack tuple
275 * uses the same space as the ND fields
276 * in the key, so both are not allowed
279 mask_allowed
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
280 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xff)))
281 mask_allowed
|= 1 << OVS_KEY_ATTR_ND
;
287 if ((key_attrs
& key_expected
) != key_expected
) {
288 /* Key attributes check failed. */
289 OVS_NLERR(log
, "Missing key (keys=%llx, expected=%llx)",
290 (unsigned long long)key_attrs
,
291 (unsigned long long)key_expected
);
295 if ((mask_attrs
& mask_allowed
) != mask_attrs
) {
296 /* Mask attributes check failed. */
297 OVS_NLERR(log
, "Unexpected mask (mask=%llx, allowed=%llx)",
298 (unsigned long long)mask_attrs
,
299 (unsigned long long)mask_allowed
);
306 size_t ovs_tun_key_attr_size(void)
308 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
309 * updating this function.
311 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
312 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
313 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
314 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
315 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
316 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
317 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
318 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
319 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
320 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
321 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
323 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
324 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_DST */
325 + nla_total_size(4); /* OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS */
328 size_t ovs_key_attr_size(void)
330 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
331 * updating this function.
333 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO
!= 28);
335 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
336 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
337 + ovs_tun_key_attr_size()
338 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
339 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
340 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
341 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
342 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
343 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
344 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
345 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
346 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
347 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
348 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
349 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
350 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
351 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
352 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
353 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
354 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
357 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
358 [OVS_VXLAN_EXT_GBP
] = { .len
= sizeof(u32
) },
361 static const struct ovs_len_tbl ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
362 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= sizeof(u64
) },
363 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= sizeof(u32
) },
364 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= sizeof(u32
) },
365 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
366 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
367 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
368 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
369 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= sizeof(u16
) },
370 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= sizeof(u16
) },
371 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
372 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
373 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= OVS_ATTR_NESTED
,
374 .next
= ovs_vxlan_ext_key_lens
},
375 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= sizeof(struct in6_addr
) },
376 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= sizeof(struct in6_addr
) },
377 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
] = { .len
= sizeof(u32
) },
380 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
381 static const struct ovs_len_tbl ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
382 [OVS_KEY_ATTR_ENCAP
] = { .len
= OVS_ATTR_NESTED
},
383 [OVS_KEY_ATTR_PRIORITY
] = { .len
= sizeof(u32
) },
384 [OVS_KEY_ATTR_IN_PORT
] = { .len
= sizeof(u32
) },
385 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= sizeof(u32
) },
386 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
387 [OVS_KEY_ATTR_VLAN
] = { .len
= sizeof(__be16
) },
388 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= sizeof(__be16
) },
389 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
390 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
391 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
392 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= sizeof(__be16
) },
393 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
394 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
395 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
396 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
397 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
398 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
399 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= sizeof(u32
) },
400 [OVS_KEY_ATTR_DP_HASH
] = { .len
= sizeof(u32
) },
401 [OVS_KEY_ATTR_TUNNEL
] = { .len
= OVS_ATTR_NESTED
,
402 .next
= ovs_tunnel_key_lens
, },
403 [OVS_KEY_ATTR_MPLS
] = { .len
= sizeof(struct ovs_key_mpls
) },
404 [OVS_KEY_ATTR_CT_STATE
] = { .len
= sizeof(u32
) },
405 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= sizeof(u16
) },
406 [OVS_KEY_ATTR_CT_MARK
] = { .len
= sizeof(u32
) },
407 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
408 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = {
409 .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
410 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = {
411 .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
414 static bool check_attr_len(unsigned int attr_len
, unsigned int expected_len
)
416 return expected_len
== attr_len
||
417 expected_len
== OVS_ATTR_NESTED
||
418 expected_len
== OVS_ATTR_VARIABLE
;
421 static bool is_all_zero(const u8
*fp
, size_t size
)
428 for (i
= 0; i
< size
; i
++)
435 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
436 const struct nlattr
*a
[],
437 u64
*attrsp
, bool log
, bool nz
)
439 const struct nlattr
*nla
;
444 nla_for_each_nested(nla
, attr
, rem
) {
445 u16 type
= nla_type(nla
);
448 if (type
> OVS_KEY_ATTR_MAX
) {
449 OVS_NLERR(log
, "Key type %d is out of range max %d",
450 type
, OVS_KEY_ATTR_MAX
);
454 if (attrs
& (1 << type
)) {
455 OVS_NLERR(log
, "Duplicate key (type %d).", type
);
459 expected_len
= ovs_key_lens
[type
].len
;
460 if (!check_attr_len(nla_len(nla
), expected_len
)) {
461 OVS_NLERR(log
, "Key %d has unexpected len %d expected %d",
462 type
, nla_len(nla
), expected_len
);
466 if (!nz
|| !is_all_zero(nla_data(nla
), expected_len
)) {
472 OVS_NLERR(log
, "Message has %d unknown bytes.", rem
);
480 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
481 const struct nlattr
*a
[], u64
*attrsp
,
484 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, true);
487 int parse_flow_nlattrs(const struct nlattr
*attr
, const struct nlattr
*a
[],
488 u64
*attrsp
, bool log
)
490 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, false);
493 static int genev_tun_opt_from_nlattr(const struct nlattr
*a
,
494 struct sw_flow_match
*match
, bool is_mask
,
497 unsigned long opt_key_offset
;
499 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
500 OVS_NLERR(log
, "Geneve option length err (len %d, max %zu).",
501 nla_len(a
), sizeof(match
->key
->tun_opts
));
505 if (nla_len(a
) % 4 != 0) {
506 OVS_NLERR(log
, "Geneve opt len %d is not a multiple of 4.",
511 /* We need to record the length of the options passed
512 * down, otherwise packets with the same format but
513 * additional options will be silently matched.
516 SW_FLOW_KEY_PUT(match
, tun_opts_len
, nla_len(a
),
519 /* This is somewhat unusual because it looks at
520 * both the key and mask while parsing the
521 * attributes (and by extension assumes the key
522 * is parsed first). Normally, we would verify
523 * that each is the correct length and that the
524 * attributes line up in the validate function.
525 * However, that is difficult because this is
526 * variable length and we won't have the
529 if (match
->key
->tun_opts_len
!= nla_len(a
)) {
530 OVS_NLERR(log
, "Geneve option len %d != mask len %d",
531 match
->key
->tun_opts_len
, nla_len(a
));
535 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
538 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
539 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
540 nla_len(a
), is_mask
);
544 static int vxlan_tun_opt_from_nlattr(const struct nlattr
*attr
,
545 struct sw_flow_match
*match
, bool is_mask
,
550 unsigned long opt_key_offset
;
551 struct vxlan_metadata opts
;
553 BUILD_BUG_ON(sizeof(opts
) > sizeof(match
->key
->tun_opts
));
555 memset(&opts
, 0, sizeof(opts
));
556 nla_for_each_nested(a
, attr
, rem
) {
557 int type
= nla_type(a
);
559 if (type
> OVS_VXLAN_EXT_MAX
) {
560 OVS_NLERR(log
, "VXLAN extension %d out of range max %d",
561 type
, OVS_VXLAN_EXT_MAX
);
565 if (!check_attr_len(nla_len(a
),
566 ovs_vxlan_ext_key_lens
[type
].len
)) {
567 OVS_NLERR(log
, "VXLAN extension %d has unexpected len %d expected %d",
569 ovs_vxlan_ext_key_lens
[type
].len
);
574 case OVS_VXLAN_EXT_GBP
:
575 opts
.gbp
= nla_get_u32(a
);
578 OVS_NLERR(log
, "Unknown VXLAN extension attribute %d",
584 OVS_NLERR(log
, "VXLAN extension message has %d unknown bytes.",
590 SW_FLOW_KEY_PUT(match
, tun_opts_len
, sizeof(opts
), false);
592 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
594 opt_key_offset
= TUN_METADATA_OFFSET(sizeof(opts
));
595 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, &opts
, sizeof(opts
),
600 static int erspan_tun_opt_from_nlattr(const struct nlattr
*attr
,
601 struct sw_flow_match
*match
, bool is_mask
,
604 unsigned long opt_key_offset
;
605 struct erspan_metadata opts
;
607 BUILD_BUG_ON(sizeof(opts
) > sizeof(match
->key
->tun_opts
));
609 memset(&opts
, 0, sizeof(opts
));
610 opts
.index
= nla_get_be32(attr
);
612 /* Index has only 20-bit */
613 if (ntohl(opts
.index
) & ~INDEX_MASK
) {
614 OVS_NLERR(log
, "ERSPAN index number %x too large.",
619 SW_FLOW_KEY_PUT(match
, tun_opts_len
, sizeof(opts
), is_mask
);
620 opt_key_offset
= TUN_METADATA_OFFSET(sizeof(opts
));
621 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, &opts
, sizeof(opts
),
627 static int ip_tun_from_nlattr(const struct nlattr
*attr
,
628 struct sw_flow_match
*match
, bool is_mask
,
631 bool ttl
= false, ipv4
= false, ipv6
= false;
632 __be16 tun_flags
= 0;
637 nla_for_each_nested(a
, attr
, rem
) {
638 int type
= nla_type(a
);
641 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
642 OVS_NLERR(log
, "Tunnel attr %d out of range max %d",
643 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
647 if (!check_attr_len(nla_len(a
),
648 ovs_tunnel_key_lens
[type
].len
)) {
649 OVS_NLERR(log
, "Tunnel attr %d has unexpected len %d expected %d",
650 type
, nla_len(a
), ovs_tunnel_key_lens
[type
].len
);
655 case OVS_TUNNEL_KEY_ATTR_ID
:
656 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
657 nla_get_be64(a
), is_mask
);
658 tun_flags
|= TUNNEL_KEY
;
660 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
661 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.src
,
662 nla_get_in_addr(a
), is_mask
);
665 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
666 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.dst
,
667 nla_get_in_addr(a
), is_mask
);
670 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
671 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.src
,
672 nla_get_in6_addr(a
), is_mask
);
675 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
676 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.dst
,
677 nla_get_in6_addr(a
), is_mask
);
680 case OVS_TUNNEL_KEY_ATTR_TOS
:
681 SW_FLOW_KEY_PUT(match
, tun_key
.tos
,
682 nla_get_u8(a
), is_mask
);
684 case OVS_TUNNEL_KEY_ATTR_TTL
:
685 SW_FLOW_KEY_PUT(match
, tun_key
.ttl
,
686 nla_get_u8(a
), is_mask
);
689 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
690 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
692 case OVS_TUNNEL_KEY_ATTR_CSUM
:
693 tun_flags
|= TUNNEL_CSUM
;
695 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
696 SW_FLOW_KEY_PUT(match
, tun_key
.tp_src
,
697 nla_get_be16(a
), is_mask
);
699 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
700 SW_FLOW_KEY_PUT(match
, tun_key
.tp_dst
,
701 nla_get_be16(a
), is_mask
);
703 case OVS_TUNNEL_KEY_ATTR_OAM
:
704 tun_flags
|= TUNNEL_OAM
;
706 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
708 OVS_NLERR(log
, "Multiple metadata blocks provided");
712 err
= genev_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
716 tun_flags
|= TUNNEL_GENEVE_OPT
;
719 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
721 OVS_NLERR(log
, "Multiple metadata blocks provided");
725 err
= vxlan_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
729 tun_flags
|= TUNNEL_VXLAN_OPT
;
732 case OVS_TUNNEL_KEY_ATTR_PAD
:
734 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
736 OVS_NLERR(log
, "Multiple metadata blocks provided");
740 err
= erspan_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
744 tun_flags
|= TUNNEL_ERSPAN_OPT
;
748 OVS_NLERR(log
, "Unknown IP tunnel attribute %d",
754 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
756 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_proto
, 0xff, true);
758 SW_FLOW_KEY_PUT(match
, tun_proto
, ipv6
? AF_INET6
: AF_INET
,
762 OVS_NLERR(log
, "IP tunnel attribute has %d unknown bytes.",
768 OVS_NLERR(log
, "Mixed IPv4 and IPv6 tunnel attributes");
773 if (!ipv4
&& !ipv6
) {
774 OVS_NLERR(log
, "IP tunnel dst address not specified");
777 if (ipv4
&& !match
->key
->tun_key
.u
.ipv4
.dst
) {
778 OVS_NLERR(log
, "IPv4 tunnel dst address is zero");
781 if (ipv6
&& ipv6_addr_any(&match
->key
->tun_key
.u
.ipv6
.dst
)) {
782 OVS_NLERR(log
, "IPv6 tunnel dst address is zero");
787 OVS_NLERR(log
, "IP tunnel TTL not specified.");
795 static int vxlan_opt_to_nlattr(struct sk_buff
*skb
,
796 const void *tun_opts
, int swkey_tun_opts_len
)
798 const struct vxlan_metadata
*opts
= tun_opts
;
801 nla
= nla_nest_start(skb
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
805 if (nla_put_u32(skb
, OVS_VXLAN_EXT_GBP
, opts
->gbp
) < 0)
808 nla_nest_end(skb
, nla
);
812 static int __ip_tun_to_nlattr(struct sk_buff
*skb
,
813 const struct ip_tunnel_key
*output
,
814 const void *tun_opts
, int swkey_tun_opts_len
,
815 unsigned short tun_proto
)
817 if (output
->tun_flags
& TUNNEL_KEY
&&
818 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
,
819 OVS_TUNNEL_KEY_ATTR_PAD
))
823 if (output
->u
.ipv4
.src
&&
824 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
,
827 if (output
->u
.ipv4
.dst
&&
828 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
,
833 if (!ipv6_addr_any(&output
->u
.ipv6
.src
) &&
834 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
,
835 &output
->u
.ipv6
.src
))
837 if (!ipv6_addr_any(&output
->u
.ipv6
.dst
) &&
838 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
,
839 &output
->u
.ipv6
.dst
))
844 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->tos
))
846 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ttl
))
848 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
849 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
851 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
852 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
854 if (output
->tp_src
&&
855 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, output
->tp_src
))
857 if (output
->tp_dst
&&
858 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_DST
, output
->tp_dst
))
860 if ((output
->tun_flags
& TUNNEL_OAM
) &&
861 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_OAM
))
863 if (swkey_tun_opts_len
) {
864 if (output
->tun_flags
& TUNNEL_GENEVE_OPT
&&
865 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
866 swkey_tun_opts_len
, tun_opts
))
868 else if (output
->tun_flags
& TUNNEL_VXLAN_OPT
&&
869 vxlan_opt_to_nlattr(skb
, tun_opts
, swkey_tun_opts_len
))
871 else if (output
->tun_flags
& TUNNEL_ERSPAN_OPT
&&
872 nla_put_be32(skb
, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
,
873 ((struct erspan_metadata
*)tun_opts
)->index
))
880 static int ip_tun_to_nlattr(struct sk_buff
*skb
,
881 const struct ip_tunnel_key
*output
,
882 const void *tun_opts
, int swkey_tun_opts_len
,
883 unsigned short tun_proto
)
888 nla
= nla_nest_start(skb
, OVS_KEY_ATTR_TUNNEL
);
892 err
= __ip_tun_to_nlattr(skb
, output
, tun_opts
, swkey_tun_opts_len
,
897 nla_nest_end(skb
, nla
);
901 int ovs_nla_put_tunnel_info(struct sk_buff
*skb
,
902 struct ip_tunnel_info
*tun_info
)
904 return __ip_tun_to_nlattr(skb
, &tun_info
->key
,
905 ip_tunnel_info_opts(tun_info
),
906 tun_info
->options_len
,
907 ip_tunnel_info_af(tun_info
));
910 static int encode_vlan_from_nlattrs(struct sw_flow_match
*match
,
911 const struct nlattr
*a
[],
912 bool is_mask
, bool inner
)
917 if (a
[OVS_KEY_ATTR_VLAN
])
918 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
920 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
921 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
923 if (likely(!inner
)) {
924 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tpid
, tpid
, is_mask
);
925 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, tci
, is_mask
);
927 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tpid
, tpid
, is_mask
);
928 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, tci
, is_mask
);
933 static int validate_vlan_from_nlattrs(const struct sw_flow_match
*match
,
934 u64 key_attrs
, bool inner
,
935 const struct nlattr
**a
, bool log
)
939 if (!((key_attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) &&
940 (key_attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) &&
941 eth_type_vlan(nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
])))) {
946 if (!((key_attrs
& (1 << OVS_KEY_ATTR_VLAN
)) &&
947 (key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
)))) {
948 OVS_NLERR(log
, "Invalid %s frame", (inner
) ? "C-VLAN" : "VLAN");
952 if (a
[OVS_KEY_ATTR_VLAN
])
953 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
955 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
957 OVS_NLERR(log
, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
958 (inner
) ? "C-VLAN" : "VLAN");
960 } else if (nla_len(a
[OVS_KEY_ATTR_ENCAP
])) {
961 /* Corner case for truncated VLAN header. */
962 OVS_NLERR(log
, "Truncated %s header has non-zero encap attribute.",
963 (inner
) ? "C-VLAN" : "VLAN");
971 static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match
*match
,
972 u64 key_attrs
, bool inner
,
973 const struct nlattr
**a
, bool log
)
977 bool encap_valid
= !!(match
->key
->eth
.vlan
.tci
&
978 htons(VLAN_TAG_PRESENT
));
979 bool i_encap_valid
= !!(match
->key
->eth
.cvlan
.tci
&
980 htons(VLAN_TAG_PRESENT
));
982 if (!(key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
))) {
987 if ((!inner
&& !encap_valid
) || (inner
&& !i_encap_valid
)) {
988 OVS_NLERR(log
, "Encap mask attribute is set for non-%s frame.",
989 (inner
) ? "C-VLAN" : "VLAN");
993 if (a
[OVS_KEY_ATTR_VLAN
])
994 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
996 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
997 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
999 if (tpid
!= htons(0xffff)) {
1000 OVS_NLERR(log
, "Must have an exact match on %s TPID (mask=%x).",
1001 (inner
) ? "C-VLAN" : "VLAN", ntohs(tpid
));
1004 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
1005 OVS_NLERR(log
, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
1006 (inner
) ? "C-VLAN" : "VLAN");
1013 static int __parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
1014 u64
*key_attrs
, bool inner
,
1015 const struct nlattr
**a
, bool is_mask
,
1019 const struct nlattr
*encap
;
1022 err
= validate_vlan_from_nlattrs(match
, *key_attrs
, inner
,
1025 err
= validate_vlan_mask_from_nlattrs(match
, *key_attrs
, inner
,
1030 err
= encode_vlan_from_nlattrs(match
, a
, is_mask
, inner
);
1034 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
1035 *key_attrs
&= ~(1 << OVS_KEY_ATTR_VLAN
);
1036 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1038 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1041 err
= parse_flow_nlattrs(encap
, a
, key_attrs
, log
);
1043 err
= parse_flow_mask_nlattrs(encap
, a
, key_attrs
, log
);
1048 static int parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
1049 u64
*key_attrs
, const struct nlattr
**a
,
1050 bool is_mask
, bool log
)
1053 bool encap_valid
= false;
1055 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, false, a
,
1060 encap_valid
= !!(match
->key
->eth
.vlan
.tci
& htons(VLAN_TAG_PRESENT
));
1062 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, true, a
,
1071 static int parse_eth_type_from_nlattrs(struct sw_flow_match
*match
,
1072 u64
*attrs
, const struct nlattr
**a
,
1073 bool is_mask
, bool log
)
1077 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1079 /* Always exact match EtherType. */
1080 eth_type
= htons(0xffff);
1081 } else if (!eth_proto_is_802_3(eth_type
)) {
1082 OVS_NLERR(log
, "EtherType %x is less than min %x",
1083 ntohs(eth_type
), ETH_P_802_3_MIN
);
1087 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
1088 *attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1092 static int metadata_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1093 u64
*attrs
, const struct nlattr
**a
,
1094 bool is_mask
, bool log
)
1096 u8 mac_proto
= MAC_PROTO_ETHERNET
;
1098 if (*attrs
& (1 << OVS_KEY_ATTR_DP_HASH
)) {
1099 u32 hash_val
= nla_get_u32(a
[OVS_KEY_ATTR_DP_HASH
]);
1101 SW_FLOW_KEY_PUT(match
, ovs_flow_hash
, hash_val
, is_mask
);
1102 *attrs
&= ~(1 << OVS_KEY_ATTR_DP_HASH
);
1105 if (*attrs
& (1 << OVS_KEY_ATTR_RECIRC_ID
)) {
1106 u32 recirc_id
= nla_get_u32(a
[OVS_KEY_ATTR_RECIRC_ID
]);
1108 SW_FLOW_KEY_PUT(match
, recirc_id
, recirc_id
, is_mask
);
1109 *attrs
&= ~(1 << OVS_KEY_ATTR_RECIRC_ID
);
1112 if (*attrs
& (1 << OVS_KEY_ATTR_PRIORITY
)) {
1113 SW_FLOW_KEY_PUT(match
, phy
.priority
,
1114 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
1115 *attrs
&= ~(1 << OVS_KEY_ATTR_PRIORITY
);
1118 if (*attrs
& (1 << OVS_KEY_ATTR_IN_PORT
)) {
1119 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
1122 in_port
= 0xffffffff; /* Always exact match in_port. */
1123 } else if (in_port
>= DP_MAX_PORTS
) {
1124 OVS_NLERR(log
, "Port %d exceeds max allowable %d",
1125 in_port
, DP_MAX_PORTS
);
1129 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
1130 *attrs
&= ~(1 << OVS_KEY_ATTR_IN_PORT
);
1131 } else if (!is_mask
) {
1132 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
1135 if (*attrs
& (1 << OVS_KEY_ATTR_SKB_MARK
)) {
1136 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
1138 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
1139 *attrs
&= ~(1 << OVS_KEY_ATTR_SKB_MARK
);
1141 if (*attrs
& (1 << OVS_KEY_ATTR_TUNNEL
)) {
1142 if (ip_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
1145 *attrs
&= ~(1 << OVS_KEY_ATTR_TUNNEL
);
1148 if (*attrs
& (1 << OVS_KEY_ATTR_CT_STATE
) &&
1149 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_STATE
)) {
1150 u32 ct_state
= nla_get_u32(a
[OVS_KEY_ATTR_CT_STATE
]);
1152 if (ct_state
& ~CT_SUPPORTED_MASK
) {
1153 OVS_NLERR(log
, "ct_state flags %08x unsupported",
1158 SW_FLOW_KEY_PUT(match
, ct_state
, ct_state
, is_mask
);
1159 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_STATE
);
1161 if (*attrs
& (1 << OVS_KEY_ATTR_CT_ZONE
) &&
1162 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_ZONE
)) {
1163 u16 ct_zone
= nla_get_u16(a
[OVS_KEY_ATTR_CT_ZONE
]);
1165 SW_FLOW_KEY_PUT(match
, ct_zone
, ct_zone
, is_mask
);
1166 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ZONE
);
1168 if (*attrs
& (1 << OVS_KEY_ATTR_CT_MARK
) &&
1169 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_MARK
)) {
1170 u32 mark
= nla_get_u32(a
[OVS_KEY_ATTR_CT_MARK
]);
1172 SW_FLOW_KEY_PUT(match
, ct
.mark
, mark
, is_mask
);
1173 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_MARK
);
1175 if (*attrs
& (1 << OVS_KEY_ATTR_CT_LABELS
) &&
1176 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_LABELS
)) {
1177 const struct ovs_key_ct_labels
*cl
;
1179 cl
= nla_data(a
[OVS_KEY_ATTR_CT_LABELS
]);
1180 SW_FLOW_KEY_MEMCPY(match
, ct
.labels
, cl
->ct_labels
,
1181 sizeof(*cl
), is_mask
);
1182 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_LABELS
);
1184 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
1185 const struct ovs_key_ct_tuple_ipv4
*ct
;
1187 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
1189 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.src
, ct
->ipv4_src
, is_mask
);
1190 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.dst
, ct
->ipv4_dst
, is_mask
);
1191 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1192 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1193 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv4_proto
, is_mask
);
1194 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
1196 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
1197 const struct ovs_key_ct_tuple_ipv6
*ct
;
1199 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
1201 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.src
, &ct
->ipv6_src
,
1202 sizeof(match
->key
->ipv6
.ct_orig
.src
),
1204 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.dst
, &ct
->ipv6_dst
,
1205 sizeof(match
->key
->ipv6
.ct_orig
.dst
),
1207 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1208 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1209 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv6_proto
, is_mask
);
1210 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
1213 /* For layer 3 packets the Ethernet type is provided
1214 * and treated as metadata but no MAC addresses are provided.
1216 if (!(*attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) &&
1217 (*attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)))
1218 mac_proto
= MAC_PROTO_NONE
;
1220 /* Always exact match mac_proto */
1221 SW_FLOW_KEY_PUT(match
, mac_proto
, is_mask
? 0xff : mac_proto
, is_mask
);
1223 if (mac_proto
== MAC_PROTO_NONE
)
1224 return parse_eth_type_from_nlattrs(match
, attrs
, a
, is_mask
,
1230 static int ovs_key_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1231 u64 attrs
, const struct nlattr
**a
,
1232 bool is_mask
, bool log
)
1236 err
= metadata_from_nlattrs(net
, match
, &attrs
, a
, is_mask
, log
);
1240 if (attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) {
1241 const struct ovs_key_ethernet
*eth_key
;
1243 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
1244 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
1245 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
1246 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
1247 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
1248 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERNET
);
1250 if (attrs
& (1 << OVS_KEY_ATTR_VLAN
)) {
1251 /* VLAN attribute is always parsed before getting here since it
1252 * may occur multiple times.
1254 OVS_NLERR(log
, "VLAN attribute unexpected.");
1258 if (attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) {
1259 err
= parse_eth_type_from_nlattrs(match
, &attrs
, a
, is_mask
,
1263 } else if (!is_mask
) {
1264 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
1266 } else if (!match
->key
->eth
.type
) {
1267 OVS_NLERR(log
, "Either Ethernet header or EtherType is required.");
1271 if (attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
1272 const struct ovs_key_ipv4
*ipv4_key
;
1274 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
1275 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
1276 OVS_NLERR(log
, "IPv4 frag type %d is out of range max %d",
1277 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
1280 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1281 ipv4_key
->ipv4_proto
, is_mask
);
1282 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1283 ipv4_key
->ipv4_tos
, is_mask
);
1284 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1285 ipv4_key
->ipv4_ttl
, is_mask
);
1286 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1287 ipv4_key
->ipv4_frag
, is_mask
);
1288 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1289 ipv4_key
->ipv4_src
, is_mask
);
1290 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1291 ipv4_key
->ipv4_dst
, is_mask
);
1292 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
1295 if (attrs
& (1 << OVS_KEY_ATTR_IPV6
)) {
1296 const struct ovs_key_ipv6
*ipv6_key
;
1298 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
1299 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
1300 OVS_NLERR(log
, "IPv6 frag type %d is out of range max %d",
1301 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
1305 if (!is_mask
&& ipv6_key
->ipv6_label
& htonl(0xFFF00000)) {
1306 OVS_NLERR(log
, "IPv6 flow label %x is out of range (max=%x)",
1307 ntohl(ipv6_key
->ipv6_label
), (1 << 20) - 1);
1311 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
1312 ipv6_key
->ipv6_label
, is_mask
);
1313 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1314 ipv6_key
->ipv6_proto
, is_mask
);
1315 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1316 ipv6_key
->ipv6_tclass
, is_mask
);
1317 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1318 ipv6_key
->ipv6_hlimit
, is_mask
);
1319 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1320 ipv6_key
->ipv6_frag
, is_mask
);
1321 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
1323 sizeof(match
->key
->ipv6
.addr
.src
),
1325 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
1327 sizeof(match
->key
->ipv6
.addr
.dst
),
1330 attrs
&= ~(1 << OVS_KEY_ATTR_IPV6
);
1333 if (attrs
& (1 << OVS_KEY_ATTR_ARP
)) {
1334 const struct ovs_key_arp
*arp_key
;
1336 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
1337 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
1338 OVS_NLERR(log
, "Unknown ARP opcode (opcode=%d).",
1343 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1344 arp_key
->arp_sip
, is_mask
);
1345 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1346 arp_key
->arp_tip
, is_mask
);
1347 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1348 ntohs(arp_key
->arp_op
), is_mask
);
1349 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
1350 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
1351 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
1352 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
1354 attrs
&= ~(1 << OVS_KEY_ATTR_ARP
);
1357 if (attrs
& (1 << OVS_KEY_ATTR_MPLS
)) {
1358 const struct ovs_key_mpls
*mpls_key
;
1360 mpls_key
= nla_data(a
[OVS_KEY_ATTR_MPLS
]);
1361 SW_FLOW_KEY_PUT(match
, mpls
.top_lse
,
1362 mpls_key
->mpls_lse
, is_mask
);
1364 attrs
&= ~(1 << OVS_KEY_ATTR_MPLS
);
1367 if (attrs
& (1 << OVS_KEY_ATTR_TCP
)) {
1368 const struct ovs_key_tcp
*tcp_key
;
1370 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
1371 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
1372 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
1373 attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
1376 if (attrs
& (1 << OVS_KEY_ATTR_TCP_FLAGS
)) {
1377 SW_FLOW_KEY_PUT(match
, tp
.flags
,
1378 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
1380 attrs
&= ~(1 << OVS_KEY_ATTR_TCP_FLAGS
);
1383 if (attrs
& (1 << OVS_KEY_ATTR_UDP
)) {
1384 const struct ovs_key_udp
*udp_key
;
1386 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
1387 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
1388 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
1389 attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
1392 if (attrs
& (1 << OVS_KEY_ATTR_SCTP
)) {
1393 const struct ovs_key_sctp
*sctp_key
;
1395 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
1396 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
1397 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
1398 attrs
&= ~(1 << OVS_KEY_ATTR_SCTP
);
1401 if (attrs
& (1 << OVS_KEY_ATTR_ICMP
)) {
1402 const struct ovs_key_icmp
*icmp_key
;
1404 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
1405 SW_FLOW_KEY_PUT(match
, tp
.src
,
1406 htons(icmp_key
->icmp_type
), is_mask
);
1407 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1408 htons(icmp_key
->icmp_code
), is_mask
);
1409 attrs
&= ~(1 << OVS_KEY_ATTR_ICMP
);
1412 if (attrs
& (1 << OVS_KEY_ATTR_ICMPV6
)) {
1413 const struct ovs_key_icmpv6
*icmpv6_key
;
1415 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
1416 SW_FLOW_KEY_PUT(match
, tp
.src
,
1417 htons(icmpv6_key
->icmpv6_type
), is_mask
);
1418 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1419 htons(icmpv6_key
->icmpv6_code
), is_mask
);
1420 attrs
&= ~(1 << OVS_KEY_ATTR_ICMPV6
);
1423 if (attrs
& (1 << OVS_KEY_ATTR_ND
)) {
1424 const struct ovs_key_nd
*nd_key
;
1426 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
1427 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
1429 sizeof(match
->key
->ipv6
.nd
.target
),
1431 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
1432 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
1433 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
1434 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
1435 attrs
&= ~(1 << OVS_KEY_ATTR_ND
);
1439 OVS_NLERR(log
, "Unknown key attributes %llx",
1440 (unsigned long long)attrs
);
1447 static void nlattr_set(struct nlattr
*attr
, u8 val
,
1448 const struct ovs_len_tbl
*tbl
)
1453 /* The nlattr stream should already have been validated */
1454 nla_for_each_nested(nla
, attr
, rem
) {
1455 if (tbl
[nla_type(nla
)].len
== OVS_ATTR_NESTED
) {
1456 if (tbl
[nla_type(nla
)].next
)
1457 tbl
= tbl
[nla_type(nla
)].next
;
1458 nlattr_set(nla
, val
, tbl
);
1460 memset(nla_data(nla
), val
, nla_len(nla
));
1463 if (nla_type(nla
) == OVS_KEY_ATTR_CT_STATE
)
1464 *(u32
*)nla_data(nla
) &= CT_SUPPORTED_MASK
;
1468 static void mask_set_nlattr(struct nlattr
*attr
, u8 val
)
1470 nlattr_set(attr
, val
, ovs_key_lens
);
1474 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1475 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1476 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1477 * does not include any don't care bit.
1478 * @net: Used to determine per-namespace field support.
1479 * @match: receives the extracted flow match information.
1480 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1481 * sequence. The fields should of the packet that triggered the creation
1483 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1484 * attribute specifies the mask field of the wildcarded flow.
1485 * @log: Boolean to allow kernel error logging. Normally true, but when
1486 * probing for feature compatibility this should be passed in as false to
1487 * suppress unnecessary error logging.
1489 int ovs_nla_get_match(struct net
*net
, struct sw_flow_match
*match
,
1490 const struct nlattr
*nla_key
,
1491 const struct nlattr
*nla_mask
,
1494 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1495 struct nlattr
*newmask
= NULL
;
1500 err
= parse_flow_nlattrs(nla_key
, a
, &key_attrs
, log
);
1504 err
= parse_vlan_from_nlattrs(match
, &key_attrs
, a
, false, log
);
1508 err
= ovs_key_from_nlattrs(net
, match
, key_attrs
, a
, false, log
);
1514 /* Create an exact match mask. We need to set to 0xff
1515 * all the 'match->mask' fields that have been touched
1516 * in 'match->key'. We cannot simply memset
1517 * 'match->mask', because padding bytes and fields not
1518 * specified in 'match->key' should be left to 0.
1519 * Instead, we use a stream of netlink attributes,
1520 * copied from 'key' and set to 0xff.
1521 * ovs_key_from_nlattrs() will take care of filling
1522 * 'match->mask' appropriately.
1524 newmask
= kmemdup(nla_key
,
1525 nla_total_size(nla_len(nla_key
)),
1530 mask_set_nlattr(newmask
, 0xff);
1532 /* The userspace does not send tunnel attributes that
1533 * are 0, but we should not wildcard them nonetheless.
1535 if (match
->key
->tun_proto
)
1536 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_key
,
1542 err
= parse_flow_mask_nlattrs(nla_mask
, a
, &mask_attrs
, log
);
1546 /* Always match on tci. */
1547 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, htons(0xffff), true);
1548 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, htons(0xffff), true);
1550 err
= parse_vlan_from_nlattrs(match
, &mask_attrs
, a
, true, log
);
1554 err
= ovs_key_from_nlattrs(net
, match
, mask_attrs
, a
, true,
1560 if (!match_validate(match
, key_attrs
, mask_attrs
, log
))
1568 static size_t get_ufid_len(const struct nlattr
*attr
, bool log
)
1575 len
= nla_len(attr
);
1576 if (len
< 1 || len
> MAX_UFID_LENGTH
) {
1577 OVS_NLERR(log
, "ufid size %u bytes exceeds the range (1, %d)",
1578 nla_len(attr
), MAX_UFID_LENGTH
);
1585 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1586 * or false otherwise.
1588 bool ovs_nla_get_ufid(struct sw_flow_id
*sfid
, const struct nlattr
*attr
,
1591 sfid
->ufid_len
= get_ufid_len(attr
, log
);
1593 memcpy(sfid
->ufid
, nla_data(attr
), sfid
->ufid_len
);
1595 return sfid
->ufid_len
;
1598 int ovs_nla_get_identifier(struct sw_flow_id
*sfid
, const struct nlattr
*ufid
,
1599 const struct sw_flow_key
*key
, bool log
)
1601 struct sw_flow_key
*new_key
;
1603 if (ovs_nla_get_ufid(sfid
, ufid
, log
))
1606 /* If UFID was not provided, use unmasked key. */
1607 new_key
= kmalloc(sizeof(*new_key
), GFP_KERNEL
);
1610 memcpy(new_key
, key
, sizeof(*key
));
1611 sfid
->unmasked_key
= new_key
;
1616 u32
ovs_nla_get_ufid_flags(const struct nlattr
*attr
)
1618 return attr
? nla_get_u32(attr
) : 0;
1622 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1623 * @net: Network namespace.
1624 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1626 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1628 * @attrs: Bit mask for the netlink attributes included in @a.
1629 * @log: Boolean to allow kernel error logging. Normally true, but when
1630 * probing for feature compatibility this should be passed in as false to
1631 * suppress unnecessary error logging.
1633 * This parses a series of Netlink attributes that form a flow key, which must
1634 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1635 * get the metadata, that is, the parts of the flow key that cannot be
1636 * extracted from the packet itself.
1638 * This must be called before the packet key fields are filled in 'key'.
1641 int ovs_nla_get_flow_metadata(struct net
*net
,
1642 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1],
1643 u64 attrs
, struct sw_flow_key
*key
, bool log
)
1645 struct sw_flow_match match
;
1647 memset(&match
, 0, sizeof(match
));
1652 key
->ct_orig_proto
= 0;
1653 memset(&key
->ct
, 0, sizeof(key
->ct
));
1654 memset(&key
->ipv4
.ct_orig
, 0, sizeof(key
->ipv4
.ct_orig
));
1655 memset(&key
->ipv6
.ct_orig
, 0, sizeof(key
->ipv6
.ct_orig
));
1657 key
->phy
.in_port
= DP_MAX_PORTS
;
1659 return metadata_from_nlattrs(net
, &match
, &attrs
, a
, false, log
);
1662 static int ovs_nla_put_vlan(struct sk_buff
*skb
, const struct vlan_head
*vh
,
1665 __be16 eth_type
= !is_mask
? vh
->tpid
: htons(0xffff);
1667 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
1668 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, vh
->tci
))
1673 static int __ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1674 const struct sw_flow_key
*output
, bool is_mask
,
1675 struct sk_buff
*skb
)
1677 struct ovs_key_ethernet
*eth_key
;
1679 struct nlattr
*encap
= NULL
;
1680 struct nlattr
*in_encap
= NULL
;
1682 if (nla_put_u32(skb
, OVS_KEY_ATTR_RECIRC_ID
, output
->recirc_id
))
1683 goto nla_put_failure
;
1685 if (nla_put_u32(skb
, OVS_KEY_ATTR_DP_HASH
, output
->ovs_flow_hash
))
1686 goto nla_put_failure
;
1688 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
1689 goto nla_put_failure
;
1691 if ((swkey
->tun_proto
|| is_mask
)) {
1692 const void *opts
= NULL
;
1694 if (output
->tun_key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
)
1695 opts
= TUN_METADATA_OPTS(output
, swkey
->tun_opts_len
);
1697 if (ip_tun_to_nlattr(skb
, &output
->tun_key
, opts
,
1698 swkey
->tun_opts_len
, swkey
->tun_proto
))
1699 goto nla_put_failure
;
1702 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
1703 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
1704 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
1705 goto nla_put_failure
;
1708 upper_u16
= !is_mask
? 0 : 0xffff;
1710 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
1711 (upper_u16
<< 16) | output
->phy
.in_port
))
1712 goto nla_put_failure
;
1715 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
1716 goto nla_put_failure
;
1718 if (ovs_ct_put_key(swkey
, output
, skb
))
1719 goto nla_put_failure
;
1721 if (ovs_key_mac_proto(swkey
) == MAC_PROTO_ETHERNET
) {
1722 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
1724 goto nla_put_failure
;
1726 eth_key
= nla_data(nla
);
1727 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
1728 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
1730 if (swkey
->eth
.vlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
1731 if (ovs_nla_put_vlan(skb
, &output
->eth
.vlan
, is_mask
))
1732 goto nla_put_failure
;
1733 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1734 if (!swkey
->eth
.vlan
.tci
)
1737 if (swkey
->eth
.cvlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
1738 if (ovs_nla_put_vlan(skb
, &output
->eth
.cvlan
, is_mask
))
1739 goto nla_put_failure
;
1740 in_encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1741 if (!swkey
->eth
.cvlan
.tci
)
1746 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
1748 * Ethertype 802.2 is represented in the netlink with omitted
1749 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1750 * 0xffff in the mask attribute. Ethertype can also
1753 if (is_mask
&& output
->eth
.type
)
1754 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
1756 goto nla_put_failure
;
1761 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
1762 goto nla_put_failure
;
1764 if (eth_type_vlan(swkey
->eth
.type
)) {
1765 /* There are 3 VLAN tags, we don't know anything about the rest
1766 * of the packet, so truncate here.
1768 WARN_ON_ONCE(!(encap
&& in_encap
));
1772 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1773 struct ovs_key_ipv4
*ipv4_key
;
1775 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
1777 goto nla_put_failure
;
1778 ipv4_key
= nla_data(nla
);
1779 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
1780 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
1781 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
1782 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
1783 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
1784 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
1785 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1786 struct ovs_key_ipv6
*ipv6_key
;
1788 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
1790 goto nla_put_failure
;
1791 ipv6_key
= nla_data(nla
);
1792 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
1793 sizeof(ipv6_key
->ipv6_src
));
1794 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
1795 sizeof(ipv6_key
->ipv6_dst
));
1796 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
1797 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
1798 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
1799 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
1800 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
1801 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1802 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1803 struct ovs_key_arp
*arp_key
;
1805 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
1807 goto nla_put_failure
;
1808 arp_key
= nla_data(nla
);
1809 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
1810 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
1811 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
1812 arp_key
->arp_op
= htons(output
->ip
.proto
);
1813 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
1814 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
1815 } else if (eth_p_mpls(swkey
->eth
.type
)) {
1816 struct ovs_key_mpls
*mpls_key
;
1818 nla
= nla_reserve(skb
, OVS_KEY_ATTR_MPLS
, sizeof(*mpls_key
));
1820 goto nla_put_failure
;
1821 mpls_key
= nla_data(nla
);
1822 mpls_key
->mpls_lse
= output
->mpls
.top_lse
;
1825 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
1826 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
1827 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1829 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
1830 struct ovs_key_tcp
*tcp_key
;
1832 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
1834 goto nla_put_failure
;
1835 tcp_key
= nla_data(nla
);
1836 tcp_key
->tcp_src
= output
->tp
.src
;
1837 tcp_key
->tcp_dst
= output
->tp
.dst
;
1838 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
1840 goto nla_put_failure
;
1841 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1842 struct ovs_key_udp
*udp_key
;
1844 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1846 goto nla_put_failure
;
1847 udp_key
= nla_data(nla
);
1848 udp_key
->udp_src
= output
->tp
.src
;
1849 udp_key
->udp_dst
= output
->tp
.dst
;
1850 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
1851 struct ovs_key_sctp
*sctp_key
;
1853 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
1855 goto nla_put_failure
;
1856 sctp_key
= nla_data(nla
);
1857 sctp_key
->sctp_src
= output
->tp
.src
;
1858 sctp_key
->sctp_dst
= output
->tp
.dst
;
1859 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1860 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1861 struct ovs_key_icmp
*icmp_key
;
1863 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1865 goto nla_put_failure
;
1866 icmp_key
= nla_data(nla
);
1867 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
1868 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
1869 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1870 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1871 struct ovs_key_icmpv6
*icmpv6_key
;
1873 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1874 sizeof(*icmpv6_key
));
1876 goto nla_put_failure
;
1877 icmpv6_key
= nla_data(nla
);
1878 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
1879 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
1881 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1882 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1883 struct ovs_key_nd
*nd_key
;
1885 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1887 goto nla_put_failure
;
1888 nd_key
= nla_data(nla
);
1889 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
1890 sizeof(nd_key
->nd_target
));
1891 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
1892 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
1899 nla_nest_end(skb
, in_encap
);
1901 nla_nest_end(skb
, encap
);
1909 int ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1910 const struct sw_flow_key
*output
, int attr
, bool is_mask
,
1911 struct sk_buff
*skb
)
1916 nla
= nla_nest_start(skb
, attr
);
1919 err
= __ovs_nla_put_key(swkey
, output
, is_mask
, skb
);
1922 nla_nest_end(skb
, nla
);
1927 /* Called with ovs_mutex or RCU read lock. */
1928 int ovs_nla_put_identifier(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1930 if (ovs_identifier_is_ufid(&flow
->id
))
1931 return nla_put(skb
, OVS_FLOW_ATTR_UFID
, flow
->id
.ufid_len
,
1934 return ovs_nla_put_key(flow
->id
.unmasked_key
, flow
->id
.unmasked_key
,
1935 OVS_FLOW_ATTR_KEY
, false, skb
);
1938 /* Called with ovs_mutex or RCU read lock. */
1939 int ovs_nla_put_masked_key(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1941 return ovs_nla_put_key(&flow
->key
, &flow
->key
,
1942 OVS_FLOW_ATTR_KEY
, false, skb
);
1945 /* Called with ovs_mutex or RCU read lock. */
1946 int ovs_nla_put_mask(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1948 return ovs_nla_put_key(&flow
->key
, &flow
->mask
->key
,
1949 OVS_FLOW_ATTR_MASK
, true, skb
);
1952 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1954 static struct sw_flow_actions
*nla_alloc_flow_actions(int size
, bool log
)
1956 struct sw_flow_actions
*sfa
;
1958 if (size
> MAX_ACTIONS_BUFSIZE
) {
1959 OVS_NLERR(log
, "Flow action size %u bytes exceeds max", size
);
1960 return ERR_PTR(-EINVAL
);
1963 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
1965 return ERR_PTR(-ENOMEM
);
1967 sfa
->actions_len
= 0;
1971 static void ovs_nla_free_set_action(const struct nlattr
*a
)
1973 const struct nlattr
*ovs_key
= nla_data(a
);
1974 struct ovs_tunnel_info
*ovs_tun
;
1976 switch (nla_type(ovs_key
)) {
1977 case OVS_KEY_ATTR_TUNNEL_INFO
:
1978 ovs_tun
= nla_data(ovs_key
);
1979 dst_release((struct dst_entry
*)ovs_tun
->tun_dst
);
1984 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
1986 const struct nlattr
*a
;
1992 nla_for_each_attr(a
, sf_acts
->actions
, sf_acts
->actions_len
, rem
) {
1993 switch (nla_type(a
)) {
1994 case OVS_ACTION_ATTR_SET
:
1995 ovs_nla_free_set_action(a
);
1997 case OVS_ACTION_ATTR_CT
:
1998 ovs_ct_free_action(a
);
2006 static void __ovs_nla_free_flow_actions(struct rcu_head
*head
)
2008 ovs_nla_free_flow_actions(container_of(head
, struct sw_flow_actions
, rcu
));
2011 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
2012 * The caller must hold rcu_read_lock for this to be sensible. */
2013 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions
*sf_acts
)
2015 call_rcu(&sf_acts
->rcu
, __ovs_nla_free_flow_actions
);
2018 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
2019 int attr_len
, bool log
)
2022 struct sw_flow_actions
*acts
;
2024 int req_size
= NLA_ALIGN(attr_len
);
2025 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
2026 (*sfa
)->actions_len
;
2028 if (req_size
<= (ksize(*sfa
) - next_offset
))
2031 new_acts_size
= ksize(*sfa
) * 2;
2033 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
2034 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
)
2035 return ERR_PTR(-EMSGSIZE
);
2036 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
2039 acts
= nla_alloc_flow_actions(new_acts_size
, log
);
2041 return (void *)acts
;
2043 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
2044 acts
->actions_len
= (*sfa
)->actions_len
;
2045 acts
->orig_len
= (*sfa
)->orig_len
;
2050 (*sfa
)->actions_len
+= req_size
;
2051 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
2054 static struct nlattr
*__add_action(struct sw_flow_actions
**sfa
,
2055 int attrtype
, void *data
, int len
, bool log
)
2059 a
= reserve_sfa_size(sfa
, nla_attr_size(len
), log
);
2063 a
->nla_type
= attrtype
;
2064 a
->nla_len
= nla_attr_size(len
);
2067 memcpy(nla_data(a
), data
, len
);
2068 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
2073 int ovs_nla_add_action(struct sw_flow_actions
**sfa
, int attrtype
, void *data
,
2078 a
= __add_action(sfa
, attrtype
, data
, len
, log
);
2080 return PTR_ERR_OR_ZERO(a
);
2083 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
2084 int attrtype
, bool log
)
2086 int used
= (*sfa
)->actions_len
;
2089 err
= ovs_nla_add_action(sfa
, attrtype
, NULL
, 0, log
);
2096 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
2099 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
2102 a
->nla_len
= sfa
->actions_len
- st_offset
;
2105 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2106 const struct sw_flow_key
*key
,
2107 struct sw_flow_actions
**sfa
,
2108 __be16 eth_type
, __be16 vlan_tci
, bool log
);
2110 static int validate_and_copy_sample(struct net
*net
, const struct nlattr
*attr
,
2111 const struct sw_flow_key
*key
,
2112 struct sw_flow_actions
**sfa
,
2113 __be16 eth_type
, __be16 vlan_tci
,
2114 bool log
, bool last
)
2116 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
2117 const struct nlattr
*probability
, *actions
;
2118 const struct nlattr
*a
;
2119 int rem
, start
, err
;
2120 struct sample_arg arg
;
2122 memset(attrs
, 0, sizeof(attrs
));
2123 nla_for_each_nested(a
, attr
, rem
) {
2124 int type
= nla_type(a
);
2125 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
2132 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
2133 if (!probability
|| nla_len(probability
) != sizeof(u32
))
2136 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
2137 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
2140 /* validation done, copy sample action. */
2141 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
, log
);
2145 /* When both skb and flow may be changed, put the sample
2146 * into a deferred fifo. On the other hand, if only skb
2147 * may be modified, the actions can be executed in place.
2149 * Do this analysis at the flow installation time.
2150 * Set 'clone_action->exec' to true if the actions can be
2151 * executed without being deferred.
2153 * If the sample is the last action, it can always be excuted
2154 * rather than deferred.
2156 arg
.exec
= last
|| !actions_may_change_flow(actions
);
2157 arg
.probability
= nla_get_u32(probability
);
2159 err
= ovs_nla_add_action(sfa
, OVS_SAMPLE_ATTR_ARG
, &arg
, sizeof(arg
),
2164 err
= __ovs_nla_copy_actions(net
, actions
, key
, sfa
,
2165 eth_type
, vlan_tci
, log
);
2170 add_nested_action_end(*sfa
, start
);
2175 void ovs_match_init(struct sw_flow_match
*match
,
2176 struct sw_flow_key
*key
,
2178 struct sw_flow_mask
*mask
)
2180 memset(match
, 0, sizeof(*match
));
2185 memset(key
, 0, sizeof(*key
));
2188 memset(&mask
->key
, 0, sizeof(mask
->key
));
2189 mask
->range
.start
= mask
->range
.end
= 0;
2193 static int validate_geneve_opts(struct sw_flow_key
*key
)
2195 struct geneve_opt
*option
;
2196 int opts_len
= key
->tun_opts_len
;
2197 bool crit_opt
= false;
2199 option
= (struct geneve_opt
*)TUN_METADATA_OPTS(key
, key
->tun_opts_len
);
2200 while (opts_len
> 0) {
2203 if (opts_len
< sizeof(*option
))
2206 len
= sizeof(*option
) + option
->length
* 4;
2210 crit_opt
|= !!(option
->type
& GENEVE_CRIT_OPT_TYPE
);
2212 option
= (struct geneve_opt
*)((u8
*)option
+ len
);
2216 key
->tun_key
.tun_flags
|= crit_opt
? TUNNEL_CRIT_OPT
: 0;
2221 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
2222 struct sw_flow_actions
**sfa
, bool log
)
2224 struct sw_flow_match match
;
2225 struct sw_flow_key key
;
2226 struct metadata_dst
*tun_dst
;
2227 struct ip_tunnel_info
*tun_info
;
2228 struct ovs_tunnel_info
*ovs_tun
;
2230 int err
= 0, start
, opts_type
;
2232 ovs_match_init(&match
, &key
, true, NULL
);
2233 opts_type
= ip_tun_from_nlattr(nla_data(attr
), &match
, false, log
);
2237 if (key
.tun_opts_len
) {
2238 switch (opts_type
) {
2239 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2240 err
= validate_geneve_opts(&key
);
2244 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2246 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS
:
2251 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
, log
);
2255 tun_dst
= metadata_dst_alloc(key
.tun_opts_len
, METADATA_IP_TUNNEL
,
2261 err
= dst_cache_init(&tun_dst
->u
.tun_info
.dst_cache
, GFP_KERNEL
);
2263 dst_release((struct dst_entry
*)tun_dst
);
2267 a
= __add_action(sfa
, OVS_KEY_ATTR_TUNNEL_INFO
, NULL
,
2268 sizeof(*ovs_tun
), log
);
2270 dst_release((struct dst_entry
*)tun_dst
);
2274 ovs_tun
= nla_data(a
);
2275 ovs_tun
->tun_dst
= tun_dst
;
2277 tun_info
= &tun_dst
->u
.tun_info
;
2278 tun_info
->mode
= IP_TUNNEL_INFO_TX
;
2279 if (key
.tun_proto
== AF_INET6
)
2280 tun_info
->mode
|= IP_TUNNEL_INFO_IPV6
;
2281 tun_info
->key
= key
.tun_key
;
2283 /* We need to store the options in the action itself since
2284 * everything else will go away after flow setup. We can append
2285 * it to tun_info and then point there.
2287 ip_tunnel_info_opts_set(tun_info
,
2288 TUN_METADATA_OPTS(&key
, key
.tun_opts_len
),
2290 add_nested_action_end(*sfa
, start
);
2295 /* Return false if there are any non-masked bits set.
2296 * Mask follows data immediately, before any netlink padding.
2298 static bool validate_masked(u8
*data
, int len
)
2300 u8
*mask
= data
+ len
;
2303 if (*data
++ & ~*mask
++)
2309 static int validate_set(const struct nlattr
*a
,
2310 const struct sw_flow_key
*flow_key
,
2311 struct sw_flow_actions
**sfa
, bool *skip_copy
,
2312 u8 mac_proto
, __be16 eth_type
, bool masked
, bool log
)
2314 const struct nlattr
*ovs_key
= nla_data(a
);
2315 int key_type
= nla_type(ovs_key
);
2318 /* There can be only one key in a action */
2319 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
2322 key_len
= nla_len(ovs_key
);
2326 if (key_type
> OVS_KEY_ATTR_MAX
||
2327 !check_attr_len(key_len
, ovs_key_lens
[key_type
].len
))
2330 if (masked
&& !validate_masked(nla_data(ovs_key
), key_len
))
2334 const struct ovs_key_ipv4
*ipv4_key
;
2335 const struct ovs_key_ipv6
*ipv6_key
;
2338 case OVS_KEY_ATTR_PRIORITY
:
2339 case OVS_KEY_ATTR_SKB_MARK
:
2340 case OVS_KEY_ATTR_CT_MARK
:
2341 case OVS_KEY_ATTR_CT_LABELS
:
2344 case OVS_KEY_ATTR_ETHERNET
:
2345 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2349 case OVS_KEY_ATTR_TUNNEL
:
2351 return -EINVAL
; /* Masked tunnel set not supported. */
2354 err
= validate_and_copy_set_tun(a
, sfa
, log
);
2359 case OVS_KEY_ATTR_IPV4
:
2360 if (eth_type
!= htons(ETH_P_IP
))
2363 ipv4_key
= nla_data(ovs_key
);
2366 const struct ovs_key_ipv4
*mask
= ipv4_key
+ 1;
2368 /* Non-writeable fields. */
2369 if (mask
->ipv4_proto
|| mask
->ipv4_frag
)
2372 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
2375 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
2380 case OVS_KEY_ATTR_IPV6
:
2381 if (eth_type
!= htons(ETH_P_IPV6
))
2384 ipv6_key
= nla_data(ovs_key
);
2387 const struct ovs_key_ipv6
*mask
= ipv6_key
+ 1;
2389 /* Non-writeable fields. */
2390 if (mask
->ipv6_proto
|| mask
->ipv6_frag
)
2393 /* Invalid bits in the flow label mask? */
2394 if (ntohl(mask
->ipv6_label
) & 0xFFF00000)
2397 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
2400 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
2403 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
2408 case OVS_KEY_ATTR_TCP
:
2409 if ((eth_type
!= htons(ETH_P_IP
) &&
2410 eth_type
!= htons(ETH_P_IPV6
)) ||
2411 flow_key
->ip
.proto
!= IPPROTO_TCP
)
2416 case OVS_KEY_ATTR_UDP
:
2417 if ((eth_type
!= htons(ETH_P_IP
) &&
2418 eth_type
!= htons(ETH_P_IPV6
)) ||
2419 flow_key
->ip
.proto
!= IPPROTO_UDP
)
2424 case OVS_KEY_ATTR_MPLS
:
2425 if (!eth_p_mpls(eth_type
))
2429 case OVS_KEY_ATTR_SCTP
:
2430 if ((eth_type
!= htons(ETH_P_IP
) &&
2431 eth_type
!= htons(ETH_P_IPV6
)) ||
2432 flow_key
->ip
.proto
!= IPPROTO_SCTP
)
2441 /* Convert non-masked non-tunnel set actions to masked set actions. */
2442 if (!masked
&& key_type
!= OVS_KEY_ATTR_TUNNEL
) {
2443 int start
, len
= key_len
* 2;
2448 start
= add_nested_action_start(sfa
,
2449 OVS_ACTION_ATTR_SET_TO_MASKED
,
2454 at
= __add_action(sfa
, key_type
, NULL
, len
, log
);
2458 memcpy(nla_data(at
), nla_data(ovs_key
), key_len
); /* Key. */
2459 memset(nla_data(at
) + key_len
, 0xff, key_len
); /* Mask. */
2460 /* Clear non-writeable bits from otherwise writeable fields. */
2461 if (key_type
== OVS_KEY_ATTR_IPV6
) {
2462 struct ovs_key_ipv6
*mask
= nla_data(at
) + key_len
;
2464 mask
->ipv6_label
&= htonl(0x000FFFFF);
2466 add_nested_action_end(*sfa
, start
);
2472 static int validate_userspace(const struct nlattr
*attr
)
2474 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
2475 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
2476 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
2477 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = {.type
= NLA_U32
},
2479 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
2482 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
, attr
,
2483 userspace_policy
, NULL
);
2487 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
2488 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
2494 static int copy_action(const struct nlattr
*from
,
2495 struct sw_flow_actions
**sfa
, bool log
)
2497 int totlen
= NLA_ALIGN(from
->nla_len
);
2500 to
= reserve_sfa_size(sfa
, from
->nla_len
, log
);
2504 memcpy(to
, from
, totlen
);
2508 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2509 const struct sw_flow_key
*key
,
2510 struct sw_flow_actions
**sfa
,
2511 __be16 eth_type
, __be16 vlan_tci
, bool log
)
2513 u8 mac_proto
= ovs_key_mac_proto(key
);
2514 const struct nlattr
*a
;
2517 nla_for_each_nested(a
, attr
, rem
) {
2518 /* Expected argument lengths, (u32)-1 for variable length. */
2519 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
2520 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
2521 [OVS_ACTION_ATTR_RECIRC
] = sizeof(u32
),
2522 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
2523 [OVS_ACTION_ATTR_PUSH_MPLS
] = sizeof(struct ovs_action_push_mpls
),
2524 [OVS_ACTION_ATTR_POP_MPLS
] = sizeof(__be16
),
2525 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
2526 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
2527 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
2528 [OVS_ACTION_ATTR_SET_MASKED
] = (u32
)-1,
2529 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1,
2530 [OVS_ACTION_ATTR_HASH
] = sizeof(struct ovs_action_hash
),
2531 [OVS_ACTION_ATTR_CT
] = (u32
)-1,
2532 [OVS_ACTION_ATTR_CT_CLEAR
] = 0,
2533 [OVS_ACTION_ATTR_TRUNC
] = sizeof(struct ovs_action_trunc
),
2534 [OVS_ACTION_ATTR_PUSH_ETH
] = sizeof(struct ovs_action_push_eth
),
2535 [OVS_ACTION_ATTR_POP_ETH
] = 0,
2537 const struct ovs_action_push_vlan
*vlan
;
2538 int type
= nla_type(a
);
2541 if (type
> OVS_ACTION_ATTR_MAX
||
2542 (action_lens
[type
] != nla_len(a
) &&
2543 action_lens
[type
] != (u32
)-1))
2548 case OVS_ACTION_ATTR_UNSPEC
:
2551 case OVS_ACTION_ATTR_USERSPACE
:
2552 err
= validate_userspace(a
);
2557 case OVS_ACTION_ATTR_OUTPUT
:
2558 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
2562 case OVS_ACTION_ATTR_TRUNC
: {
2563 const struct ovs_action_trunc
*trunc
= nla_data(a
);
2565 if (trunc
->max_len
< ETH_HLEN
)
2570 case OVS_ACTION_ATTR_HASH
: {
2571 const struct ovs_action_hash
*act_hash
= nla_data(a
);
2573 switch (act_hash
->hash_alg
) {
2574 case OVS_HASH_ALG_L4
:
2583 case OVS_ACTION_ATTR_POP_VLAN
:
2584 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2586 vlan_tci
= htons(0);
2589 case OVS_ACTION_ATTR_PUSH_VLAN
:
2590 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2593 if (!eth_type_vlan(vlan
->vlan_tpid
))
2595 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
2597 vlan_tci
= vlan
->vlan_tci
;
2600 case OVS_ACTION_ATTR_RECIRC
:
2603 case OVS_ACTION_ATTR_PUSH_MPLS
: {
2604 const struct ovs_action_push_mpls
*mpls
= nla_data(a
);
2606 if (!eth_p_mpls(mpls
->mpls_ethertype
))
2608 /* Prohibit push MPLS other than to a white list
2609 * for packets that have a known tag order.
2611 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2612 (eth_type
!= htons(ETH_P_IP
) &&
2613 eth_type
!= htons(ETH_P_IPV6
) &&
2614 eth_type
!= htons(ETH_P_ARP
) &&
2615 eth_type
!= htons(ETH_P_RARP
) &&
2616 !eth_p_mpls(eth_type
)))
2618 eth_type
= mpls
->mpls_ethertype
;
2622 case OVS_ACTION_ATTR_POP_MPLS
:
2623 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2624 !eth_p_mpls(eth_type
))
2627 /* Disallow subsequent L2.5+ set and mpls_pop actions
2628 * as there is no check here to ensure that the new
2629 * eth_type is valid and thus set actions could
2630 * write off the end of the packet or otherwise
2633 * Support for these actions is planned using packet
2636 eth_type
= htons(0);
2639 case OVS_ACTION_ATTR_SET
:
2640 err
= validate_set(a
, key
, sfa
,
2641 &skip_copy
, mac_proto
, eth_type
,
2647 case OVS_ACTION_ATTR_SET_MASKED
:
2648 err
= validate_set(a
, key
, sfa
,
2649 &skip_copy
, mac_proto
, eth_type
,
2655 case OVS_ACTION_ATTR_SAMPLE
: {
2656 bool last
= nla_is_last(a
, rem
);
2658 err
= validate_and_copy_sample(net
, a
, key
, sfa
,
2667 case OVS_ACTION_ATTR_CT
:
2668 err
= ovs_ct_copy_action(net
, a
, key
, sfa
, log
);
2674 case OVS_ACTION_ATTR_CT_CLEAR
:
2677 case OVS_ACTION_ATTR_PUSH_ETH
:
2678 /* Disallow pushing an Ethernet header if one
2679 * is already present */
2680 if (mac_proto
!= MAC_PROTO_NONE
)
2682 mac_proto
= MAC_PROTO_NONE
;
2685 case OVS_ACTION_ATTR_POP_ETH
:
2686 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2688 if (vlan_tci
& htons(VLAN_TAG_PRESENT
))
2690 mac_proto
= MAC_PROTO_ETHERNET
;
2694 OVS_NLERR(log
, "Unknown Action type %d", type
);
2698 err
= copy_action(a
, sfa
, log
);
2710 /* 'key' must be the masked key. */
2711 int ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2712 const struct sw_flow_key
*key
,
2713 struct sw_flow_actions
**sfa
, bool log
)
2717 *sfa
= nla_alloc_flow_actions(nla_len(attr
), log
);
2719 return PTR_ERR(*sfa
);
2721 (*sfa
)->orig_len
= nla_len(attr
);
2722 err
= __ovs_nla_copy_actions(net
, attr
, key
, sfa
, key
->eth
.type
,
2723 key
->eth
.vlan
.tci
, log
);
2725 ovs_nla_free_flow_actions(*sfa
);
2730 static int sample_action_to_attr(const struct nlattr
*attr
,
2731 struct sk_buff
*skb
)
2733 struct nlattr
*start
, *ac_start
= NULL
, *sample_arg
;
2734 int err
= 0, rem
= nla_len(attr
);
2735 const struct sample_arg
*arg
;
2736 struct nlattr
*actions
;
2738 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SAMPLE
);
2742 sample_arg
= nla_data(attr
);
2743 arg
= nla_data(sample_arg
);
2744 actions
= nla_next(sample_arg
, &rem
);
2746 if (nla_put_u32(skb
, OVS_SAMPLE_ATTR_PROBABILITY
, arg
->probability
)) {
2751 ac_start
= nla_nest_start(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
2757 err
= ovs_nla_put_actions(actions
, rem
, skb
);
2761 nla_nest_cancel(skb
, ac_start
);
2762 nla_nest_cancel(skb
, start
);
2764 nla_nest_end(skb
, ac_start
);
2765 nla_nest_end(skb
, start
);
2771 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
2773 const struct nlattr
*ovs_key
= nla_data(a
);
2774 int key_type
= nla_type(ovs_key
);
2775 struct nlattr
*start
;
2779 case OVS_KEY_ATTR_TUNNEL_INFO
: {
2780 struct ovs_tunnel_info
*ovs_tun
= nla_data(ovs_key
);
2781 struct ip_tunnel_info
*tun_info
= &ovs_tun
->tun_dst
->u
.tun_info
;
2783 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
2787 err
= ip_tun_to_nlattr(skb
, &tun_info
->key
,
2788 ip_tunnel_info_opts(tun_info
),
2789 tun_info
->options_len
,
2790 ip_tunnel_info_af(tun_info
));
2793 nla_nest_end(skb
, start
);
2797 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
2805 static int masked_set_action_to_set_action_attr(const struct nlattr
*a
,
2806 struct sk_buff
*skb
)
2808 const struct nlattr
*ovs_key
= nla_data(a
);
2810 size_t key_len
= nla_len(ovs_key
) / 2;
2812 /* Revert the conversion we did from a non-masked set action to
2813 * masked set action.
2815 nla
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
2819 if (nla_put(skb
, nla_type(ovs_key
), key_len
, nla_data(ovs_key
)))
2822 nla_nest_end(skb
, nla
);
2826 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
2828 const struct nlattr
*a
;
2831 nla_for_each_attr(a
, attr
, len
, rem
) {
2832 int type
= nla_type(a
);
2835 case OVS_ACTION_ATTR_SET
:
2836 err
= set_action_to_attr(a
, skb
);
2841 case OVS_ACTION_ATTR_SET_TO_MASKED
:
2842 err
= masked_set_action_to_set_action_attr(a
, skb
);
2847 case OVS_ACTION_ATTR_SAMPLE
:
2848 err
= sample_action_to_attr(a
, skb
);
2853 case OVS_ACTION_ATTR_CT
:
2854 err
= ovs_ct_action_to_attr(nla_data(a
), skb
);
2860 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))