2 * Copyright (c) 2007-2017 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/geneve.h>
46 #include <net/ndisc.h>
48 #include <net/vxlan.h>
51 #include "conntrack.h"
53 #include "flow_netlink.h"
58 const struct ovs_len_tbl
*next
;
61 #define OVS_ATTR_NESTED -1
62 #define OVS_ATTR_VARIABLE -2
64 static bool actions_may_change_flow(const struct nlattr
*actions
)
69 nla_for_each_nested(nla
, actions
, rem
) {
70 u16 action
= nla_type(nla
);
73 case OVS_ACTION_ATTR_OUTPUT
:
74 case OVS_ACTION_ATTR_RECIRC
:
75 case OVS_ACTION_ATTR_TRUNC
:
76 case OVS_ACTION_ATTR_USERSPACE
:
79 case OVS_ACTION_ATTR_CT
:
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, len);\
138 memcpy((u8 *)(match)->key + offset, value_p, len); \
141 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
142 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
143 value_p, len, is_mask)
145 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
147 update_range(match, offsetof(struct sw_flow_key, field), \
148 sizeof((match)->key->field), is_mask); \
150 memset((u8 *)&(match)->mask->key.field, value, \
151 sizeof((match)->mask->key.field)); \
153 memset((u8 *)&(match)->key->field, value, \
154 sizeof((match)->key->field)); \
157 static bool match_validate(const struct sw_flow_match
*match
,
158 u64 key_attrs
, u64 mask_attrs
, bool log
)
160 u64 key_expected
= 0;
161 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
163 /* The following mask attributes allowed only if they
164 * pass the validation tests.
166 mask_allowed
&= ~((1ULL << OVS_KEY_ATTR_IPV4
)
167 | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)
168 | (1ULL << OVS_KEY_ATTR_IPV6
)
169 | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)
170 | (1ULL << OVS_KEY_ATTR_TCP
)
171 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)
172 | (1ULL << OVS_KEY_ATTR_UDP
)
173 | (1ULL << OVS_KEY_ATTR_SCTP
)
174 | (1ULL << OVS_KEY_ATTR_ICMP
)
175 | (1ULL << OVS_KEY_ATTR_ICMPV6
)
176 | (1ULL << OVS_KEY_ATTR_ARP
)
177 | (1ULL << OVS_KEY_ATTR_ND
)
178 | (1ULL << OVS_KEY_ATTR_MPLS
));
180 /* Always allowed mask fields. */
181 mask_allowed
|= ((1ULL << OVS_KEY_ATTR_TUNNEL
)
182 | (1ULL << OVS_KEY_ATTR_IN_PORT
)
183 | (1ULL << 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
|= 1ULL << OVS_KEY_ATTR_ARP
;
189 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
190 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ARP
;
193 if (eth_p_mpls(match
->key
->eth
.type
)) {
194 key_expected
|= 1ULL << OVS_KEY_ATTR_MPLS
;
195 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
196 mask_allowed
|= 1ULL << OVS_KEY_ATTR_MPLS
;
199 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
200 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV4
;
201 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
202 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV4
;
203 mask_allowed
|= 1ULL << 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
|= 1ULL << OVS_KEY_ATTR_UDP
;
209 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
210 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
213 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
214 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
215 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
216 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
219 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
220 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
221 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
222 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
223 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
224 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
228 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
229 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMP
;
230 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
231 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ICMP
;
236 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
237 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV6
;
238 if (match
->mask
&& match
->mask
->key
.eth
.type
== htons(0xffff)) {
239 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV6
;
240 mask_allowed
|= 1ULL << 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
|= 1ULL << OVS_KEY_ATTR_UDP
;
246 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
247 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
250 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
251 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
252 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
253 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
256 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
257 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
258 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
259 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
260 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
261 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
265 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
266 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMPV6
;
267 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
268 mask_allowed
|= 1ULL << 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
|= 1ULL << 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
|= 1ULL << 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 */
327 size_t ovs_key_attr_size(void)
329 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
330 * updating this function.
332 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO
!= 28);
334 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
335 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
336 + ovs_tun_key_attr_size()
337 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
338 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
339 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
340 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
341 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
342 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
343 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
344 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
345 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
346 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
347 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
348 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
349 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
350 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
351 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
352 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
353 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
356 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
357 [OVS_VXLAN_EXT_GBP
] = { .len
= sizeof(u32
) },
360 static const struct ovs_len_tbl ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
361 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= sizeof(u64
) },
362 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= sizeof(u32
) },
363 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= sizeof(u32
) },
364 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
365 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
366 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
367 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
368 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= sizeof(u16
) },
369 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= sizeof(u16
) },
370 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
371 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
372 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= OVS_ATTR_NESTED
,
373 .next
= ovs_vxlan_ext_key_lens
},
374 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC
] = { .len
= sizeof(struct in6_addr
) },
375 [OVS_TUNNEL_KEY_ATTR_IPV6_DST
] = { .len
= sizeof(struct in6_addr
) },
378 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
379 static const struct ovs_len_tbl ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
380 [OVS_KEY_ATTR_ENCAP
] = { .len
= OVS_ATTR_NESTED
},
381 [OVS_KEY_ATTR_PRIORITY
] = { .len
= sizeof(u32
) },
382 [OVS_KEY_ATTR_IN_PORT
] = { .len
= sizeof(u32
) },
383 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= sizeof(u32
) },
384 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
385 [OVS_KEY_ATTR_VLAN
] = { .len
= sizeof(__be16
) },
386 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= sizeof(__be16
) },
387 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
388 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
389 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
390 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= sizeof(__be16
) },
391 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
392 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
393 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
394 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
395 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
396 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
397 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= sizeof(u32
) },
398 [OVS_KEY_ATTR_DP_HASH
] = { .len
= sizeof(u32
) },
399 [OVS_KEY_ATTR_TUNNEL
] = { .len
= OVS_ATTR_NESTED
,
400 .next
= ovs_tunnel_key_lens
, },
401 [OVS_KEY_ATTR_MPLS
] = { .len
= sizeof(struct ovs_key_mpls
) },
402 [OVS_KEY_ATTR_CT_STATE
] = { .len
= sizeof(u32
) },
403 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= sizeof(u16
) },
404 [OVS_KEY_ATTR_CT_MARK
] = { .len
= sizeof(u32
) },
405 [OVS_KEY_ATTR_CT_LABELS
] = { .len
= sizeof(struct ovs_key_ct_labels
) },
406 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
] = {
407 .len
= sizeof(struct ovs_key_ct_tuple_ipv4
) },
408 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
] = {
409 .len
= sizeof(struct ovs_key_ct_tuple_ipv6
) },
412 static bool check_attr_len(unsigned int attr_len
, unsigned int expected_len
)
414 return expected_len
== attr_len
||
415 expected_len
== OVS_ATTR_NESTED
||
416 expected_len
== OVS_ATTR_VARIABLE
;
419 static bool is_all_zero(const u8
*fp
, size_t size
)
426 for (i
= 0; i
< size
; i
++)
433 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
434 const struct nlattr
*a
[],
435 u64
*attrsp
, bool log
, bool nz
)
437 const struct nlattr
*nla
;
442 nla_for_each_nested(nla
, attr
, rem
) {
443 u16 type
= nla_type(nla
);
446 if (type
> OVS_KEY_ATTR_MAX
) {
447 OVS_NLERR(log
, "Key type %d is out of range max %d",
448 type
, OVS_KEY_ATTR_MAX
);
452 if (attrs
& (1ULL << type
)) {
453 OVS_NLERR(log
, "Duplicate key (type %d).", type
);
457 expected_len
= ovs_key_lens
[type
].len
;
458 if (!check_attr_len(nla_len(nla
), expected_len
)) {
459 OVS_NLERR(log
, "Key %d has unexpected len %d expected %d",
460 type
, nla_len(nla
), expected_len
);
464 if (!nz
|| !is_all_zero(nla_data(nla
), expected_len
)) {
465 attrs
|= 1ULL << type
;
470 OVS_NLERR(log
, "Message has %d unknown bytes.", rem
);
478 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
479 const struct nlattr
*a
[], u64
*attrsp
,
482 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, true);
485 int parse_flow_nlattrs(const struct nlattr
*attr
, const struct nlattr
*a
[],
486 u64
*attrsp
, bool log
)
488 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, false);
491 static int genev_tun_opt_from_nlattr(const struct nlattr
*a
,
492 struct sw_flow_match
*match
, bool is_mask
,
495 unsigned long opt_key_offset
;
497 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
498 OVS_NLERR(log
, "Geneve option length err (len %d, max %zu).",
499 nla_len(a
), sizeof(match
->key
->tun_opts
));
503 if (nla_len(a
) % 4 != 0) {
504 OVS_NLERR(log
, "Geneve opt len %d is not a multiple of 4.",
509 /* We need to record the length of the options passed
510 * down, otherwise packets with the same format but
511 * additional options will be silently matched.
514 SW_FLOW_KEY_PUT(match
, tun_opts_len
, nla_len(a
),
517 /* This is somewhat unusual because it looks at
518 * both the key and mask while parsing the
519 * attributes (and by extension assumes the key
520 * is parsed first). Normally, we would verify
521 * that each is the correct length and that the
522 * attributes line up in the validate function.
523 * However, that is difficult because this is
524 * variable length and we won't have the
527 if (match
->key
->tun_opts_len
!= nla_len(a
)) {
528 OVS_NLERR(log
, "Geneve option len %d != mask len %d",
529 match
->key
->tun_opts_len
, nla_len(a
));
533 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
536 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
537 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
538 nla_len(a
), is_mask
);
542 static int vxlan_tun_opt_from_nlattr(const struct nlattr
*attr
,
543 struct sw_flow_match
*match
, bool is_mask
,
548 unsigned long opt_key_offset
;
549 struct vxlan_metadata opts
;
551 BUILD_BUG_ON(sizeof(opts
) > sizeof(match
->key
->tun_opts
));
553 memset(&opts
, 0, sizeof(opts
));
554 nla_for_each_nested(a
, attr
, rem
) {
555 int type
= nla_type(a
);
557 if (type
> OVS_VXLAN_EXT_MAX
) {
558 OVS_NLERR(log
, "VXLAN extension %d out of range max %d",
559 type
, OVS_VXLAN_EXT_MAX
);
563 if (!check_attr_len(nla_len(a
),
564 ovs_vxlan_ext_key_lens
[type
].len
)) {
565 OVS_NLERR(log
, "VXLAN extension %d has unexpected len %d expected %d",
567 ovs_vxlan_ext_key_lens
[type
].len
);
572 case OVS_VXLAN_EXT_GBP
:
573 opts
.gbp
= nla_get_u32(a
);
576 OVS_NLERR(log
, "Unknown VXLAN extension attribute %d",
582 OVS_NLERR(log
, "VXLAN extension message has %d unknown bytes.",
588 SW_FLOW_KEY_PUT(match
, tun_opts_len
, sizeof(opts
), false);
590 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
592 opt_key_offset
= TUN_METADATA_OFFSET(sizeof(opts
));
593 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, &opts
, sizeof(opts
),
598 static int ip_tun_from_nlattr(const struct nlattr
*attr
,
599 struct sw_flow_match
*match
, bool is_mask
,
602 bool ttl
= false, ipv4
= false, ipv6
= false;
603 __be16 tun_flags
= 0;
608 nla_for_each_nested(a
, attr
, rem
) {
609 int type
= nla_type(a
);
612 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
613 OVS_NLERR(log
, "Tunnel attr %d out of range max %d",
614 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
618 if (!check_attr_len(nla_len(a
),
619 ovs_tunnel_key_lens
[type
].len
)) {
620 OVS_NLERR(log
, "Tunnel attr %d has unexpected len %d expected %d",
621 type
, nla_len(a
), ovs_tunnel_key_lens
[type
].len
);
626 case OVS_TUNNEL_KEY_ATTR_ID
:
627 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
628 nla_get_be64(a
), is_mask
);
629 tun_flags
|= TUNNEL_KEY
;
631 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
632 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.src
,
633 nla_get_in_addr(a
), is_mask
);
636 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
637 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.dst
,
638 nla_get_in_addr(a
), is_mask
);
641 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC
:
642 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.src
,
643 nla_get_in6_addr(a
), is_mask
);
646 case OVS_TUNNEL_KEY_ATTR_IPV6_DST
:
647 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv6
.dst
,
648 nla_get_in6_addr(a
), is_mask
);
651 case OVS_TUNNEL_KEY_ATTR_TOS
:
652 SW_FLOW_KEY_PUT(match
, tun_key
.tos
,
653 nla_get_u8(a
), is_mask
);
655 case OVS_TUNNEL_KEY_ATTR_TTL
:
656 SW_FLOW_KEY_PUT(match
, tun_key
.ttl
,
657 nla_get_u8(a
), is_mask
);
660 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
661 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
663 case OVS_TUNNEL_KEY_ATTR_CSUM
:
664 tun_flags
|= TUNNEL_CSUM
;
666 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
667 SW_FLOW_KEY_PUT(match
, tun_key
.tp_src
,
668 nla_get_be16(a
), is_mask
);
670 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
671 SW_FLOW_KEY_PUT(match
, tun_key
.tp_dst
,
672 nla_get_be16(a
), is_mask
);
674 case OVS_TUNNEL_KEY_ATTR_OAM
:
675 tun_flags
|= TUNNEL_OAM
;
677 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
679 OVS_NLERR(log
, "Multiple metadata blocks provided");
683 err
= genev_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
687 tun_flags
|= TUNNEL_GENEVE_OPT
;
690 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
692 OVS_NLERR(log
, "Multiple metadata blocks provided");
696 err
= vxlan_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
700 tun_flags
|= TUNNEL_VXLAN_OPT
;
703 case OVS_TUNNEL_KEY_ATTR_PAD
:
706 OVS_NLERR(log
, "Unknown IP tunnel attribute %d",
712 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
714 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_proto
, 0xff, true);
716 SW_FLOW_KEY_PUT(match
, tun_proto
, ipv6
? AF_INET6
: AF_INET
,
720 OVS_NLERR(log
, "IP tunnel attribute has %d unknown bytes.",
726 OVS_NLERR(log
, "Mixed IPv4 and IPv6 tunnel attributes");
731 if (!ipv4
&& !ipv6
) {
732 OVS_NLERR(log
, "IP tunnel dst address not specified");
735 if (ipv4
&& !match
->key
->tun_key
.u
.ipv4
.dst
) {
736 OVS_NLERR(log
, "IPv4 tunnel dst address is zero");
739 if (ipv6
&& ipv6_addr_any(&match
->key
->tun_key
.u
.ipv6
.dst
)) {
740 OVS_NLERR(log
, "IPv6 tunnel dst address is zero");
745 OVS_NLERR(log
, "IP tunnel TTL not specified.");
753 static int vxlan_opt_to_nlattr(struct sk_buff
*skb
,
754 const void *tun_opts
, int swkey_tun_opts_len
)
756 const struct vxlan_metadata
*opts
= tun_opts
;
759 nla
= nla_nest_start(skb
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
763 if (nla_put_u32(skb
, OVS_VXLAN_EXT_GBP
, opts
->gbp
) < 0)
766 nla_nest_end(skb
, nla
);
770 static int __ip_tun_to_nlattr(struct sk_buff
*skb
,
771 const struct ip_tunnel_key
*output
,
772 const void *tun_opts
, int swkey_tun_opts_len
,
773 unsigned short tun_proto
)
775 if (output
->tun_flags
& TUNNEL_KEY
&&
776 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
,
777 OVS_TUNNEL_KEY_ATTR_PAD
))
781 if (output
->u
.ipv4
.src
&&
782 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
,
785 if (output
->u
.ipv4
.dst
&&
786 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
,
791 if (!ipv6_addr_any(&output
->u
.ipv6
.src
) &&
792 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_SRC
,
793 &output
->u
.ipv6
.src
))
795 if (!ipv6_addr_any(&output
->u
.ipv6
.dst
) &&
796 nla_put_in6_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV6_DST
,
797 &output
->u
.ipv6
.dst
))
802 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->tos
))
804 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ttl
))
806 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
807 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
809 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
810 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
812 if (output
->tp_src
&&
813 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, output
->tp_src
))
815 if (output
->tp_dst
&&
816 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_DST
, output
->tp_dst
))
818 if ((output
->tun_flags
& TUNNEL_OAM
) &&
819 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_OAM
))
821 if (swkey_tun_opts_len
) {
822 if (output
->tun_flags
& TUNNEL_GENEVE_OPT
&&
823 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
824 swkey_tun_opts_len
, tun_opts
))
826 else if (output
->tun_flags
& TUNNEL_VXLAN_OPT
&&
827 vxlan_opt_to_nlattr(skb
, tun_opts
, swkey_tun_opts_len
))
834 static int ip_tun_to_nlattr(struct sk_buff
*skb
,
835 const struct ip_tunnel_key
*output
,
836 const void *tun_opts
, int swkey_tun_opts_len
,
837 unsigned short tun_proto
)
842 nla
= nla_nest_start(skb
, OVS_KEY_ATTR_TUNNEL
);
846 err
= __ip_tun_to_nlattr(skb
, output
, tun_opts
, swkey_tun_opts_len
,
851 nla_nest_end(skb
, nla
);
855 int ovs_nla_put_tunnel_info(struct sk_buff
*skb
,
856 struct ip_tunnel_info
*tun_info
)
858 return __ip_tun_to_nlattr(skb
, &tun_info
->key
,
859 ip_tunnel_info_opts(tun_info
),
860 tun_info
->options_len
,
861 ip_tunnel_info_af(tun_info
));
864 static int encode_vlan_from_nlattrs(struct sw_flow_match
*match
,
865 const struct nlattr
*a
[],
866 bool is_mask
, bool inner
)
871 if (a
[OVS_KEY_ATTR_VLAN
])
872 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
874 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
875 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
877 if (likely(!inner
)) {
878 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tpid
, tpid
, is_mask
);
879 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, tci
, is_mask
);
881 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tpid
, tpid
, is_mask
);
882 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, tci
, is_mask
);
887 static int validate_vlan_from_nlattrs(const struct sw_flow_match
*match
,
888 u64 key_attrs
, bool inner
,
889 const struct nlattr
**a
, bool log
)
893 if (!((key_attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) &&
894 (key_attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) &&
895 eth_type_vlan(nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
])))) {
900 if (!((key_attrs
& (1 << OVS_KEY_ATTR_VLAN
)) &&
901 (key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
)))) {
902 OVS_NLERR(log
, "Invalid %s frame", (inner
) ? "C-VLAN" : "VLAN");
906 if (a
[OVS_KEY_ATTR_VLAN
])
907 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
909 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
911 OVS_NLERR(log
, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
912 (inner
) ? "C-VLAN" : "VLAN");
914 } else if (nla_len(a
[OVS_KEY_ATTR_ENCAP
])) {
915 /* Corner case for truncated VLAN header. */
916 OVS_NLERR(log
, "Truncated %s header has non-zero encap attribute.",
917 (inner
) ? "C-VLAN" : "VLAN");
925 static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match
*match
,
926 u64 key_attrs
, bool inner
,
927 const struct nlattr
**a
, bool log
)
931 bool encap_valid
= !!(match
->key
->eth
.vlan
.tci
&
932 htons(VLAN_TAG_PRESENT
));
933 bool i_encap_valid
= !!(match
->key
->eth
.cvlan
.tci
&
934 htons(VLAN_TAG_PRESENT
));
936 if (!(key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
))) {
941 if ((!inner
&& !encap_valid
) || (inner
&& !i_encap_valid
)) {
942 OVS_NLERR(log
, "Encap mask attribute is set for non-%s frame.",
943 (inner
) ? "C-VLAN" : "VLAN");
947 if (a
[OVS_KEY_ATTR_VLAN
])
948 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
950 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
951 tpid
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
953 if (tpid
!= htons(0xffff)) {
954 OVS_NLERR(log
, "Must have an exact match on %s TPID (mask=%x).",
955 (inner
) ? "C-VLAN" : "VLAN", ntohs(tpid
));
958 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
959 OVS_NLERR(log
, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
960 (inner
) ? "C-VLAN" : "VLAN");
967 static int __parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
968 u64
*key_attrs
, bool inner
,
969 const struct nlattr
**a
, bool is_mask
,
973 const struct nlattr
*encap
;
976 err
= validate_vlan_from_nlattrs(match
, *key_attrs
, inner
,
979 err
= validate_vlan_mask_from_nlattrs(match
, *key_attrs
, inner
,
984 err
= encode_vlan_from_nlattrs(match
, a
, is_mask
, inner
);
988 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
989 *key_attrs
&= ~(1 << OVS_KEY_ATTR_VLAN
);
990 *key_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
992 encap
= a
[OVS_KEY_ATTR_ENCAP
];
995 err
= parse_flow_nlattrs(encap
, a
, key_attrs
, log
);
997 err
= parse_flow_mask_nlattrs(encap
, a
, key_attrs
, log
);
1002 static int parse_vlan_from_nlattrs(struct sw_flow_match
*match
,
1003 u64
*key_attrs
, const struct nlattr
**a
,
1004 bool is_mask
, bool log
)
1007 bool encap_valid
= false;
1009 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, false, a
,
1014 encap_valid
= !!(match
->key
->eth
.vlan
.tci
& htons(VLAN_TAG_PRESENT
));
1016 err
= __parse_vlan_from_nlattrs(match
, key_attrs
, true, a
,
1025 static int parse_eth_type_from_nlattrs(struct sw_flow_match
*match
,
1026 u64
*attrs
, const struct nlattr
**a
,
1027 bool is_mask
, bool log
)
1031 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1033 /* Always exact match EtherType. */
1034 eth_type
= htons(0xffff);
1035 } else if (!eth_proto_is_802_3(eth_type
)) {
1036 OVS_NLERR(log
, "EtherType %x is less than min %x",
1037 ntohs(eth_type
), ETH_P_802_3_MIN
);
1041 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
1042 *attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
1046 static int metadata_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1047 u64
*attrs
, const struct nlattr
**a
,
1048 bool is_mask
, bool log
)
1050 u8 mac_proto
= MAC_PROTO_ETHERNET
;
1052 if (*attrs
& (1ULL << OVS_KEY_ATTR_DP_HASH
)) {
1053 u32 hash_val
= nla_get_u32(a
[OVS_KEY_ATTR_DP_HASH
]);
1055 SW_FLOW_KEY_PUT(match
, ovs_flow_hash
, hash_val
, is_mask
);
1056 *attrs
&= ~(1ULL << OVS_KEY_ATTR_DP_HASH
);
1059 if (*attrs
& (1ULL << OVS_KEY_ATTR_RECIRC_ID
)) {
1060 u32 recirc_id
= nla_get_u32(a
[OVS_KEY_ATTR_RECIRC_ID
]);
1062 SW_FLOW_KEY_PUT(match
, recirc_id
, recirc_id
, is_mask
);
1063 *attrs
&= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID
);
1066 if (*attrs
& (1ULL << OVS_KEY_ATTR_PRIORITY
)) {
1067 SW_FLOW_KEY_PUT(match
, phy
.priority
,
1068 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
1069 *attrs
&= ~(1ULL << OVS_KEY_ATTR_PRIORITY
);
1072 if (*attrs
& (1ULL << OVS_KEY_ATTR_IN_PORT
)) {
1073 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
1076 in_port
= 0xffffffff; /* Always exact match in_port. */
1077 } else if (in_port
>= DP_MAX_PORTS
) {
1078 OVS_NLERR(log
, "Port %d exceeds max allowable %d",
1079 in_port
, DP_MAX_PORTS
);
1083 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
1084 *attrs
&= ~(1ULL << OVS_KEY_ATTR_IN_PORT
);
1085 } else if (!is_mask
) {
1086 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
1089 if (*attrs
& (1ULL << OVS_KEY_ATTR_SKB_MARK
)) {
1090 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
1092 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
1093 *attrs
&= ~(1ULL << OVS_KEY_ATTR_SKB_MARK
);
1095 if (*attrs
& (1ULL << OVS_KEY_ATTR_TUNNEL
)) {
1096 if (ip_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
1099 *attrs
&= ~(1ULL << OVS_KEY_ATTR_TUNNEL
);
1102 if (*attrs
& (1 << OVS_KEY_ATTR_CT_STATE
) &&
1103 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_STATE
)) {
1104 u32 ct_state
= nla_get_u32(a
[OVS_KEY_ATTR_CT_STATE
]);
1106 if (ct_state
& ~CT_SUPPORTED_MASK
) {
1107 OVS_NLERR(log
, "ct_state flags %08x unsupported",
1112 SW_FLOW_KEY_PUT(match
, ct_state
, ct_state
, is_mask
);
1113 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_STATE
);
1115 if (*attrs
& (1 << OVS_KEY_ATTR_CT_ZONE
) &&
1116 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_ZONE
)) {
1117 u16 ct_zone
= nla_get_u16(a
[OVS_KEY_ATTR_CT_ZONE
]);
1119 SW_FLOW_KEY_PUT(match
, ct_zone
, ct_zone
, is_mask
);
1120 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ZONE
);
1122 if (*attrs
& (1 << OVS_KEY_ATTR_CT_MARK
) &&
1123 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_MARK
)) {
1124 u32 mark
= nla_get_u32(a
[OVS_KEY_ATTR_CT_MARK
]);
1126 SW_FLOW_KEY_PUT(match
, ct
.mark
, mark
, is_mask
);
1127 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_MARK
);
1129 if (*attrs
& (1 << OVS_KEY_ATTR_CT_LABELS
) &&
1130 ovs_ct_verify(net
, OVS_KEY_ATTR_CT_LABELS
)) {
1131 const struct ovs_key_ct_labels
*cl
;
1133 cl
= nla_data(a
[OVS_KEY_ATTR_CT_LABELS
]);
1134 SW_FLOW_KEY_MEMCPY(match
, ct
.labels
, cl
->ct_labels
,
1135 sizeof(*cl
), is_mask
);
1136 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_LABELS
);
1138 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
)) {
1139 const struct ovs_key_ct_tuple_ipv4
*ct
;
1141 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
]);
1143 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.src
, ct
->ipv4_src
, is_mask
);
1144 SW_FLOW_KEY_PUT(match
, ipv4
.ct_orig
.dst
, ct
->ipv4_dst
, is_mask
);
1145 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1146 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1147 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv4_proto
, is_mask
);
1148 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4
);
1150 if (*attrs
& (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
)) {
1151 const struct ovs_key_ct_tuple_ipv6
*ct
;
1153 ct
= nla_data(a
[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
]);
1155 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.src
, &ct
->ipv6_src
,
1156 sizeof(match
->key
->ipv6
.ct_orig
.src
),
1158 SW_FLOW_KEY_MEMCPY(match
, ipv6
.ct_orig
.dst
, &ct
->ipv6_dst
,
1159 sizeof(match
->key
->ipv6
.ct_orig
.dst
),
1161 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.src
, ct
->src_port
, is_mask
);
1162 SW_FLOW_KEY_PUT(match
, ct
.orig_tp
.dst
, ct
->dst_port
, is_mask
);
1163 SW_FLOW_KEY_PUT(match
, ct_orig_proto
, ct
->ipv6_proto
, is_mask
);
1164 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6
);
1167 /* For layer 3 packets the Ethernet type is provided
1168 * and treated as metadata but no MAC addresses are provided.
1170 if (!(*attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) &&
1171 (*attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)))
1172 mac_proto
= MAC_PROTO_NONE
;
1174 /* Always exact match mac_proto */
1175 SW_FLOW_KEY_PUT(match
, mac_proto
, is_mask
? 0xff : mac_proto
, is_mask
);
1177 if (mac_proto
== MAC_PROTO_NONE
)
1178 return parse_eth_type_from_nlattrs(match
, attrs
, a
, is_mask
,
1184 static int ovs_key_from_nlattrs(struct net
*net
, struct sw_flow_match
*match
,
1185 u64 attrs
, const struct nlattr
**a
,
1186 bool is_mask
, bool log
)
1190 err
= metadata_from_nlattrs(net
, match
, &attrs
, a
, is_mask
, log
);
1194 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) {
1195 const struct ovs_key_ethernet
*eth_key
;
1197 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
1198 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
1199 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
1200 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
1201 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
1202 attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERNET
);
1204 if (attrs
& (1ULL << OVS_KEY_ATTR_VLAN
)) {
1205 /* VLAN attribute is always parsed before getting here since it
1206 * may occur multiple times.
1208 OVS_NLERR(log
, "VLAN attribute unexpected.");
1212 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)) {
1213 err
= parse_eth_type_from_nlattrs(match
, &attrs
, a
, is_mask
,
1217 } else if (!is_mask
) {
1218 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
1220 } else if (!match
->key
->eth
.type
) {
1221 OVS_NLERR(log
, "Either Ethernet header or EtherType is required.");
1225 if (attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
1226 const struct ovs_key_ipv4
*ipv4_key
;
1228 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
1229 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
1230 OVS_NLERR(log
, "IPv4 frag type %d is out of range max %d",
1231 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
1234 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1235 ipv4_key
->ipv4_proto
, is_mask
);
1236 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1237 ipv4_key
->ipv4_tos
, is_mask
);
1238 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1239 ipv4_key
->ipv4_ttl
, is_mask
);
1240 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1241 ipv4_key
->ipv4_frag
, is_mask
);
1242 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1243 ipv4_key
->ipv4_src
, is_mask
);
1244 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1245 ipv4_key
->ipv4_dst
, is_mask
);
1246 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
1249 if (attrs
& (1ULL << OVS_KEY_ATTR_IPV6
)) {
1250 const struct ovs_key_ipv6
*ipv6_key
;
1252 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
1253 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
1254 OVS_NLERR(log
, "IPv6 frag type %d is out of range max %d",
1255 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
1259 if (!is_mask
&& ipv6_key
->ipv6_label
& htonl(0xFFF00000)) {
1260 OVS_NLERR(log
, "IPv6 flow label %x is out of range (max=%x)",
1261 ntohl(ipv6_key
->ipv6_label
), (1 << 20) - 1);
1265 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
1266 ipv6_key
->ipv6_label
, is_mask
);
1267 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1268 ipv6_key
->ipv6_proto
, is_mask
);
1269 SW_FLOW_KEY_PUT(match
, ip
.tos
,
1270 ipv6_key
->ipv6_tclass
, is_mask
);
1271 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
1272 ipv6_key
->ipv6_hlimit
, is_mask
);
1273 SW_FLOW_KEY_PUT(match
, ip
.frag
,
1274 ipv6_key
->ipv6_frag
, is_mask
);
1275 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
1277 sizeof(match
->key
->ipv6
.addr
.src
),
1279 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
1281 sizeof(match
->key
->ipv6
.addr
.dst
),
1284 attrs
&= ~(1ULL << OVS_KEY_ATTR_IPV6
);
1287 if (attrs
& (1ULL << OVS_KEY_ATTR_ARP
)) {
1288 const struct ovs_key_arp
*arp_key
;
1290 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
1291 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
1292 OVS_NLERR(log
, "Unknown ARP opcode (opcode=%d).",
1297 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
1298 arp_key
->arp_sip
, is_mask
);
1299 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
1300 arp_key
->arp_tip
, is_mask
);
1301 SW_FLOW_KEY_PUT(match
, ip
.proto
,
1302 ntohs(arp_key
->arp_op
), is_mask
);
1303 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
1304 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
1305 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
1306 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
1308 attrs
&= ~(1ULL << OVS_KEY_ATTR_ARP
);
1311 if (attrs
& (1ULL << OVS_KEY_ATTR_MPLS
)) {
1312 const struct ovs_key_mpls
*mpls_key
;
1314 mpls_key
= nla_data(a
[OVS_KEY_ATTR_MPLS
]);
1315 SW_FLOW_KEY_PUT(match
, mpls
.top_lse
,
1316 mpls_key
->mpls_lse
, is_mask
);
1318 attrs
&= ~(1ULL << OVS_KEY_ATTR_MPLS
);
1321 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP
)) {
1322 const struct ovs_key_tcp
*tcp_key
;
1324 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
1325 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
1326 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
1327 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP
);
1330 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)) {
1331 SW_FLOW_KEY_PUT(match
, tp
.flags
,
1332 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
1334 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS
);
1337 if (attrs
& (1ULL << OVS_KEY_ATTR_UDP
)) {
1338 const struct ovs_key_udp
*udp_key
;
1340 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
1341 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
1342 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
1343 attrs
&= ~(1ULL << OVS_KEY_ATTR_UDP
);
1346 if (attrs
& (1ULL << OVS_KEY_ATTR_SCTP
)) {
1347 const struct ovs_key_sctp
*sctp_key
;
1349 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
1350 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
1351 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
1352 attrs
&= ~(1ULL << OVS_KEY_ATTR_SCTP
);
1355 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMP
)) {
1356 const struct ovs_key_icmp
*icmp_key
;
1358 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
1359 SW_FLOW_KEY_PUT(match
, tp
.src
,
1360 htons(icmp_key
->icmp_type
), is_mask
);
1361 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1362 htons(icmp_key
->icmp_code
), is_mask
);
1363 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMP
);
1366 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMPV6
)) {
1367 const struct ovs_key_icmpv6
*icmpv6_key
;
1369 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
1370 SW_FLOW_KEY_PUT(match
, tp
.src
,
1371 htons(icmpv6_key
->icmpv6_type
), is_mask
);
1372 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1373 htons(icmpv6_key
->icmpv6_code
), is_mask
);
1374 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMPV6
);
1377 if (attrs
& (1ULL << OVS_KEY_ATTR_ND
)) {
1378 const struct ovs_key_nd
*nd_key
;
1380 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
1381 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
1383 sizeof(match
->key
->ipv6
.nd
.target
),
1385 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
1386 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
1387 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
1388 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
1389 attrs
&= ~(1ULL << OVS_KEY_ATTR_ND
);
1393 OVS_NLERR(log
, "Unknown key attributes %llx",
1394 (unsigned long long)attrs
);
1401 static void nlattr_set(struct nlattr
*attr
, u8 val
,
1402 const struct ovs_len_tbl
*tbl
)
1407 /* The nlattr stream should already have been validated */
1408 nla_for_each_nested(nla
, attr
, rem
) {
1409 if (tbl
[nla_type(nla
)].len
== OVS_ATTR_NESTED
) {
1410 if (tbl
[nla_type(nla
)].next
)
1411 tbl
= tbl
[nla_type(nla
)].next
;
1412 nlattr_set(nla
, val
, tbl
);
1414 memset(nla_data(nla
), val
, nla_len(nla
));
1417 if (nla_type(nla
) == OVS_KEY_ATTR_CT_STATE
)
1418 *(u32
*)nla_data(nla
) &= CT_SUPPORTED_MASK
;
1422 static void mask_set_nlattr(struct nlattr
*attr
, u8 val
)
1424 nlattr_set(attr
, val
, ovs_key_lens
);
1428 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1429 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1430 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1431 * does not include any don't care bit.
1432 * @net: Used to determine per-namespace field support.
1433 * @match: receives the extracted flow match information.
1434 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1435 * sequence. The fields should of the packet that triggered the creation
1437 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1438 * attribute specifies the mask field of the wildcarded flow.
1439 * @log: Boolean to allow kernel error logging. Normally true, but when
1440 * probing for feature compatibility this should be passed in as false to
1441 * suppress unnecessary error logging.
1443 int ovs_nla_get_match(struct net
*net
, struct sw_flow_match
*match
,
1444 const struct nlattr
*nla_key
,
1445 const struct nlattr
*nla_mask
,
1448 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1449 struct nlattr
*newmask
= NULL
;
1454 err
= parse_flow_nlattrs(nla_key
, a
, &key_attrs
, log
);
1458 err
= parse_vlan_from_nlattrs(match
, &key_attrs
, a
, false, log
);
1462 err
= ovs_key_from_nlattrs(net
, match
, key_attrs
, a
, false, log
);
1468 /* Create an exact match mask. We need to set to 0xff
1469 * all the 'match->mask' fields that have been touched
1470 * in 'match->key'. We cannot simply memset
1471 * 'match->mask', because padding bytes and fields not
1472 * specified in 'match->key' should be left to 0.
1473 * Instead, we use a stream of netlink attributes,
1474 * copied from 'key' and set to 0xff.
1475 * ovs_key_from_nlattrs() will take care of filling
1476 * 'match->mask' appropriately.
1478 newmask
= kmemdup(nla_key
,
1479 nla_total_size(nla_len(nla_key
)),
1484 mask_set_nlattr(newmask
, 0xff);
1486 /* The userspace does not send tunnel attributes that
1487 * are 0, but we should not wildcard them nonetheless.
1489 if (match
->key
->tun_proto
)
1490 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_key
,
1496 err
= parse_flow_mask_nlattrs(nla_mask
, a
, &mask_attrs
, log
);
1500 SW_FLOW_KEY_PUT(match
, eth
.vlan
.tci
, htons(0xffff), true);
1501 SW_FLOW_KEY_PUT(match
, eth
.cvlan
.tci
, htons(0xffff), true);
1503 err
= parse_vlan_from_nlattrs(match
, &mask_attrs
, a
, true, log
);
1507 err
= ovs_key_from_nlattrs(net
, match
, mask_attrs
, a
, true,
1513 if (!match_validate(match
, key_attrs
, mask_attrs
, log
))
1521 static size_t get_ufid_len(const struct nlattr
*attr
, bool log
)
1528 len
= nla_len(attr
);
1529 if (len
< 1 || len
> MAX_UFID_LENGTH
) {
1530 OVS_NLERR(log
, "ufid size %u bytes exceeds the range (1, %d)",
1531 nla_len(attr
), MAX_UFID_LENGTH
);
1538 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1539 * or false otherwise.
1541 bool ovs_nla_get_ufid(struct sw_flow_id
*sfid
, const struct nlattr
*attr
,
1544 sfid
->ufid_len
= get_ufid_len(attr
, log
);
1546 memcpy(sfid
->ufid
, nla_data(attr
), sfid
->ufid_len
);
1548 return sfid
->ufid_len
;
1551 int ovs_nla_get_identifier(struct sw_flow_id
*sfid
, const struct nlattr
*ufid
,
1552 const struct sw_flow_key
*key
, bool log
)
1554 struct sw_flow_key
*new_key
;
1556 if (ovs_nla_get_ufid(sfid
, ufid
, log
))
1559 /* If UFID was not provided, use unmasked key. */
1560 new_key
= kmalloc(sizeof(*new_key
), GFP_KERNEL
);
1563 memcpy(new_key
, key
, sizeof(*key
));
1564 sfid
->unmasked_key
= new_key
;
1569 u32
ovs_nla_get_ufid_flags(const struct nlattr
*attr
)
1571 return attr
? nla_get_u32(attr
) : 0;
1575 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1576 * @net: Network namespace.
1577 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1579 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1581 * @attrs: Bit mask for the netlink attributes included in @a.
1582 * @log: Boolean to allow kernel error logging. Normally true, but when
1583 * probing for feature compatibility this should be passed in as false to
1584 * suppress unnecessary error logging.
1586 * This parses a series of Netlink attributes that form a flow key, which must
1587 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1588 * get the metadata, that is, the parts of the flow key that cannot be
1589 * extracted from the packet itself.
1591 * This must be called before the packet key fields are filled in 'key'.
1594 int ovs_nla_get_flow_metadata(struct net
*net
,
1595 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1],
1596 u64 attrs
, struct sw_flow_key
*key
, bool log
)
1598 struct sw_flow_match match
;
1600 memset(&match
, 0, sizeof(match
));
1605 key
->ct_orig_proto
= 0;
1606 memset(&key
->ct
, 0, sizeof(key
->ct
));
1607 memset(&key
->ipv4
.ct_orig
, 0, sizeof(key
->ipv4
.ct_orig
));
1608 memset(&key
->ipv6
.ct_orig
, 0, sizeof(key
->ipv6
.ct_orig
));
1610 key
->phy
.in_port
= DP_MAX_PORTS
;
1612 return metadata_from_nlattrs(net
, &match
, &attrs
, a
, false, log
);
1615 static int ovs_nla_put_vlan(struct sk_buff
*skb
, const struct vlan_head
*vh
,
1618 __be16 eth_type
= !is_mask
? vh
->tpid
: htons(0xffff);
1620 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
1621 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, vh
->tci
))
1626 static int __ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1627 const struct sw_flow_key
*output
, bool is_mask
,
1628 struct sk_buff
*skb
)
1630 struct ovs_key_ethernet
*eth_key
;
1632 struct nlattr
*encap
= NULL
;
1633 struct nlattr
*in_encap
= NULL
;
1635 if (nla_put_u32(skb
, OVS_KEY_ATTR_RECIRC_ID
, output
->recirc_id
))
1636 goto nla_put_failure
;
1638 if (nla_put_u32(skb
, OVS_KEY_ATTR_DP_HASH
, output
->ovs_flow_hash
))
1639 goto nla_put_failure
;
1641 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
1642 goto nla_put_failure
;
1644 if ((swkey
->tun_proto
|| is_mask
)) {
1645 const void *opts
= NULL
;
1647 if (output
->tun_key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
)
1648 opts
= TUN_METADATA_OPTS(output
, swkey
->tun_opts_len
);
1650 if (ip_tun_to_nlattr(skb
, &output
->tun_key
, opts
,
1651 swkey
->tun_opts_len
, swkey
->tun_proto
))
1652 goto nla_put_failure
;
1655 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
1656 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
1657 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
1658 goto nla_put_failure
;
1661 upper_u16
= !is_mask
? 0 : 0xffff;
1663 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
1664 (upper_u16
<< 16) | output
->phy
.in_port
))
1665 goto nla_put_failure
;
1668 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
1669 goto nla_put_failure
;
1671 if (ovs_ct_put_key(swkey
, output
, skb
))
1672 goto nla_put_failure
;
1674 if (ovs_key_mac_proto(swkey
) == MAC_PROTO_ETHERNET
) {
1675 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
1677 goto nla_put_failure
;
1679 eth_key
= nla_data(nla
);
1680 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
1681 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
1683 if (swkey
->eth
.vlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
1684 if (ovs_nla_put_vlan(skb
, &output
->eth
.vlan
, is_mask
))
1685 goto nla_put_failure
;
1686 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1687 if (!swkey
->eth
.vlan
.tci
)
1690 if (swkey
->eth
.cvlan
.tci
|| eth_type_vlan(swkey
->eth
.type
)) {
1691 if (ovs_nla_put_vlan(skb
, &output
->eth
.cvlan
, is_mask
))
1692 goto nla_put_failure
;
1693 in_encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1694 if (!swkey
->eth
.cvlan
.tci
)
1699 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
1701 * Ethertype 802.2 is represented in the netlink with omitted
1702 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1703 * 0xffff in the mask attribute. Ethertype can also
1706 if (is_mask
&& output
->eth
.type
)
1707 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
1709 goto nla_put_failure
;
1714 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
1715 goto nla_put_failure
;
1717 if (eth_type_vlan(swkey
->eth
.type
)) {
1718 /* There are 3 VLAN tags, we don't know anything about the rest
1719 * of the packet, so truncate here.
1721 WARN_ON_ONCE(!(encap
&& in_encap
));
1725 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1726 struct ovs_key_ipv4
*ipv4_key
;
1728 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
1730 goto nla_put_failure
;
1731 ipv4_key
= nla_data(nla
);
1732 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
1733 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
1734 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
1735 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
1736 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
1737 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
1738 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1739 struct ovs_key_ipv6
*ipv6_key
;
1741 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
1743 goto nla_put_failure
;
1744 ipv6_key
= nla_data(nla
);
1745 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
1746 sizeof(ipv6_key
->ipv6_src
));
1747 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
1748 sizeof(ipv6_key
->ipv6_dst
));
1749 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
1750 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
1751 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
1752 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
1753 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
1754 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1755 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1756 struct ovs_key_arp
*arp_key
;
1758 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
1760 goto nla_put_failure
;
1761 arp_key
= nla_data(nla
);
1762 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
1763 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
1764 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
1765 arp_key
->arp_op
= htons(output
->ip
.proto
);
1766 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
1767 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
1768 } else if (eth_p_mpls(swkey
->eth
.type
)) {
1769 struct ovs_key_mpls
*mpls_key
;
1771 nla
= nla_reserve(skb
, OVS_KEY_ATTR_MPLS
, sizeof(*mpls_key
));
1773 goto nla_put_failure
;
1774 mpls_key
= nla_data(nla
);
1775 mpls_key
->mpls_lse
= output
->mpls
.top_lse
;
1778 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
1779 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
1780 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1782 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
1783 struct ovs_key_tcp
*tcp_key
;
1785 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
1787 goto nla_put_failure
;
1788 tcp_key
= nla_data(nla
);
1789 tcp_key
->tcp_src
= output
->tp
.src
;
1790 tcp_key
->tcp_dst
= output
->tp
.dst
;
1791 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
1793 goto nla_put_failure
;
1794 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1795 struct ovs_key_udp
*udp_key
;
1797 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1799 goto nla_put_failure
;
1800 udp_key
= nla_data(nla
);
1801 udp_key
->udp_src
= output
->tp
.src
;
1802 udp_key
->udp_dst
= output
->tp
.dst
;
1803 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
1804 struct ovs_key_sctp
*sctp_key
;
1806 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
1808 goto nla_put_failure
;
1809 sctp_key
= nla_data(nla
);
1810 sctp_key
->sctp_src
= output
->tp
.src
;
1811 sctp_key
->sctp_dst
= output
->tp
.dst
;
1812 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1813 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1814 struct ovs_key_icmp
*icmp_key
;
1816 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1818 goto nla_put_failure
;
1819 icmp_key
= nla_data(nla
);
1820 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
1821 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
1822 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1823 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1824 struct ovs_key_icmpv6
*icmpv6_key
;
1826 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1827 sizeof(*icmpv6_key
));
1829 goto nla_put_failure
;
1830 icmpv6_key
= nla_data(nla
);
1831 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
1832 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
1834 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1835 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1836 struct ovs_key_nd
*nd_key
;
1838 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1840 goto nla_put_failure
;
1841 nd_key
= nla_data(nla
);
1842 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
1843 sizeof(nd_key
->nd_target
));
1844 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
1845 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
1852 nla_nest_end(skb
, in_encap
);
1854 nla_nest_end(skb
, encap
);
1862 int ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1863 const struct sw_flow_key
*output
, int attr
, bool is_mask
,
1864 struct sk_buff
*skb
)
1869 nla
= nla_nest_start(skb
, attr
);
1872 err
= __ovs_nla_put_key(swkey
, output
, is_mask
, skb
);
1875 nla_nest_end(skb
, nla
);
1880 /* Called with ovs_mutex or RCU read lock. */
1881 int ovs_nla_put_identifier(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1883 if (ovs_identifier_is_ufid(&flow
->id
))
1884 return nla_put(skb
, OVS_FLOW_ATTR_UFID
, flow
->id
.ufid_len
,
1887 return ovs_nla_put_key(flow
->id
.unmasked_key
, flow
->id
.unmasked_key
,
1888 OVS_FLOW_ATTR_KEY
, false, skb
);
1891 /* Called with ovs_mutex or RCU read lock. */
1892 int ovs_nla_put_masked_key(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1894 return ovs_nla_put_key(&flow
->key
, &flow
->key
,
1895 OVS_FLOW_ATTR_KEY
, false, skb
);
1898 /* Called with ovs_mutex or RCU read lock. */
1899 int ovs_nla_put_mask(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1901 return ovs_nla_put_key(&flow
->key
, &flow
->mask
->key
,
1902 OVS_FLOW_ATTR_MASK
, true, skb
);
1905 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1907 static struct sw_flow_actions
*nla_alloc_flow_actions(int size
, bool log
)
1909 struct sw_flow_actions
*sfa
;
1911 if (size
> MAX_ACTIONS_BUFSIZE
) {
1912 OVS_NLERR(log
, "Flow action size %u bytes exceeds max", size
);
1913 return ERR_PTR(-EINVAL
);
1916 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
1918 return ERR_PTR(-ENOMEM
);
1920 sfa
->actions_len
= 0;
1924 static void ovs_nla_free_set_action(const struct nlattr
*a
)
1926 const struct nlattr
*ovs_key
= nla_data(a
);
1927 struct ovs_tunnel_info
*ovs_tun
;
1929 switch (nla_type(ovs_key
)) {
1930 case OVS_KEY_ATTR_TUNNEL_INFO
:
1931 ovs_tun
= nla_data(ovs_key
);
1932 ovs_dst_release((struct dst_entry
*)ovs_tun
->tun_dst
);
1937 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
1939 const struct nlattr
*a
;
1945 nla_for_each_attr(a
, sf_acts
->actions
, sf_acts
->actions_len
, rem
) {
1946 switch (nla_type(a
)) {
1947 case OVS_ACTION_ATTR_SET
:
1948 ovs_nla_free_set_action(a
);
1950 case OVS_ACTION_ATTR_CT
:
1951 ovs_ct_free_action(a
);
1959 static void __ovs_nla_free_flow_actions(struct rcu_head
*head
)
1961 ovs_nla_free_flow_actions(container_of(head
, struct sw_flow_actions
, rcu
));
1964 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1965 * The caller must hold rcu_read_lock for this to be sensible. */
1966 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions
*sf_acts
)
1968 call_rcu(&sf_acts
->rcu
, __ovs_nla_free_flow_actions
);
1971 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
1972 int attr_len
, bool log
)
1975 struct sw_flow_actions
*acts
;
1977 int req_size
= NLA_ALIGN(attr_len
);
1978 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
1979 (*sfa
)->actions_len
;
1981 if (req_size
<= (ksize(*sfa
) - next_offset
))
1984 new_acts_size
= ksize(*sfa
) * 2;
1986 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
1987 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
)
1988 return ERR_PTR(-EMSGSIZE
);
1989 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
1992 acts
= nla_alloc_flow_actions(new_acts_size
, log
);
1994 return (void *)acts
;
1996 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
1997 acts
->actions_len
= (*sfa
)->actions_len
;
1998 acts
->orig_len
= (*sfa
)->orig_len
;
2003 (*sfa
)->actions_len
+= req_size
;
2004 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
2007 static struct nlattr
*__add_action(struct sw_flow_actions
**sfa
,
2008 int attrtype
, void *data
, int len
, bool log
)
2012 a
= reserve_sfa_size(sfa
, nla_attr_size(len
), log
);
2016 a
->nla_type
= attrtype
;
2017 a
->nla_len
= nla_attr_size(len
);
2020 memcpy(nla_data(a
), data
, len
);
2021 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
2026 int ovs_nla_add_action(struct sw_flow_actions
**sfa
, int attrtype
, void *data
,
2031 a
= __add_action(sfa
, attrtype
, data
, len
, log
);
2033 return PTR_ERR_OR_ZERO(a
);
2036 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
2037 int attrtype
, bool log
)
2039 int used
= (*sfa
)->actions_len
;
2042 err
= ovs_nla_add_action(sfa
, attrtype
, NULL
, 0, log
);
2049 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
2052 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
2055 a
->nla_len
= sfa
->actions_len
- st_offset
;
2058 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2059 const struct sw_flow_key
*key
,
2060 struct sw_flow_actions
**sfa
,
2061 __be16 eth_type
, __be16 vlan_tci
, bool log
);
2063 static int validate_and_copy_sample(struct net
*net
, const struct nlattr
*attr
,
2064 const struct sw_flow_key
*key
,
2065 struct sw_flow_actions
**sfa
,
2066 __be16 eth_type
, __be16 vlan_tci
,
2067 bool log
, bool last
)
2069 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
2070 const struct nlattr
*probability
, *actions
;
2071 const struct nlattr
*a
;
2072 int rem
, start
, err
;
2073 struct sample_arg arg
;
2075 memset(attrs
, 0, sizeof(attrs
));
2076 nla_for_each_nested(a
, attr
, rem
) {
2077 int type
= nla_type(a
);
2078 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
2085 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
2086 if (!probability
|| nla_len(probability
) != sizeof(u32
))
2089 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
2090 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
2093 /* validation done, copy sample action. */
2094 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
, log
);
2098 /* When both skb and flow may be changed, put the sample
2099 * into a deferred fifo. On the other hand, if only skb
2100 * may be modified, the actions can be executed in place.
2102 * Do this analysis at the flow installation time.
2103 * Set 'clone_action->exec' to true if the actions can be
2104 * executed without being deferred.
2106 * If the sample is the last action, it can always be excuted
2107 * rather than deferred.
2109 arg
.exec
= last
|| !actions_may_change_flow(actions
);
2110 arg
.probability
= nla_get_u32(probability
);
2112 err
= ovs_nla_add_action(sfa
, OVS_SAMPLE_ATTR_ARG
, &arg
, sizeof(arg
),
2117 err
= __ovs_nla_copy_actions(net
, actions
, key
, sfa
,
2118 eth_type
, vlan_tci
, log
);
2123 add_nested_action_end(*sfa
, start
);
2128 void ovs_match_init(struct sw_flow_match
*match
,
2129 struct sw_flow_key
*key
,
2131 struct sw_flow_mask
*mask
)
2133 memset(match
, 0, sizeof(*match
));
2138 memset(key
, 0, sizeof(*key
));
2141 memset(&mask
->key
, 0, sizeof(mask
->key
));
2142 mask
->range
.start
= mask
->range
.end
= 0;
2146 static int validate_geneve_opts(struct sw_flow_key
*key
)
2148 struct geneve_opt
*option
;
2149 int opts_len
= key
->tun_opts_len
;
2150 bool crit_opt
= false;
2152 option
= (struct geneve_opt
*)TUN_METADATA_OPTS(key
, key
->tun_opts_len
);
2153 while (opts_len
> 0) {
2156 if (opts_len
< sizeof(*option
))
2159 len
= sizeof(*option
) + option
->length
* 4;
2163 crit_opt
|= !!(option
->type
& GENEVE_CRIT_OPT_TYPE
);
2165 option
= (struct geneve_opt
*)((u8
*)option
+ len
);
2169 key
->tun_key
.tun_flags
|= crit_opt
? TUNNEL_CRIT_OPT
: 0;
2174 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
2175 struct sw_flow_actions
**sfa
, bool log
)
2177 struct sw_flow_match match
;
2178 struct sw_flow_key key
;
2179 struct metadata_dst
*tun_dst
;
2180 struct ip_tunnel_info
*tun_info
;
2181 struct ovs_tunnel_info
*ovs_tun
;
2183 int err
= 0, start
, opts_type
;
2185 ovs_match_init(&match
, &key
, true, NULL
);
2186 opts_type
= ip_tun_from_nlattr(nla_data(attr
), &match
, false, log
);
2190 if (key
.tun_opts_len
) {
2191 switch (opts_type
) {
2192 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
2193 err
= validate_geneve_opts(&key
);
2197 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
2202 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
, log
);
2206 tun_dst
= metadata_dst_alloc(key
.tun_opts_len
, METADATA_IP_TUNNEL
,
2212 err
= dst_cache_init(&tun_dst
->u
.tun_info
.dst_cache
, GFP_KERNEL
);
2214 dst_release((struct dst_entry
*)tun_dst
);
2217 a
= __add_action(sfa
, OVS_KEY_ATTR_TUNNEL_INFO
, NULL
,
2218 sizeof(*ovs_tun
), log
);
2220 ovs_dst_release((struct dst_entry
*)tun_dst
);
2224 ovs_tun
= nla_data(a
);
2225 ovs_tun
->tun_dst
= tun_dst
;
2227 tun_info
= &tun_dst
->u
.tun_info
;
2228 tun_info
->mode
= IP_TUNNEL_INFO_TX
;
2229 if (key
.tun_proto
== AF_INET6
)
2230 tun_info
->mode
|= IP_TUNNEL_INFO_IPV6
;
2231 tun_info
->key
= key
.tun_key
;
2233 /* We need to store the options in the action itself since
2234 * everything else will go away after flow setup. We can append
2235 * it to tun_info and then point there.
2237 ip_tunnel_info_opts_set(tun_info
,
2238 TUN_METADATA_OPTS(&key
, key
.tun_opts_len
),
2240 add_nested_action_end(*sfa
, start
);
2245 /* Return false if there are any non-masked bits set.
2246 * Mask follows data immediately, before any netlink padding.
2248 static bool validate_masked(u8
*data
, int len
)
2250 u8
*mask
= data
+ len
;
2253 if (*data
++ & ~*mask
++)
2259 static int validate_set(const struct nlattr
*a
,
2260 const struct sw_flow_key
*flow_key
,
2261 struct sw_flow_actions
**sfa
, bool *skip_copy
,
2262 u8 mac_proto
, __be16 eth_type
, bool masked
, bool log
)
2264 const struct nlattr
*ovs_key
= nla_data(a
);
2265 int key_type
= nla_type(ovs_key
);
2268 /* There can be only one key in a action */
2269 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
2272 key_len
= nla_len(ovs_key
);
2276 if (key_type
> OVS_KEY_ATTR_MAX
||
2277 !check_attr_len(key_len
, ovs_key_lens
[key_type
].len
))
2280 if (masked
&& !validate_masked(nla_data(ovs_key
), key_len
))
2284 const struct ovs_key_ipv4
*ipv4_key
;
2285 const struct ovs_key_ipv6
*ipv6_key
;
2288 case OVS_KEY_ATTR_PRIORITY
:
2289 case OVS_KEY_ATTR_SKB_MARK
:
2290 case OVS_KEY_ATTR_CT_MARK
:
2291 case OVS_KEY_ATTR_CT_LABELS
:
2294 case OVS_KEY_ATTR_ETHERNET
:
2295 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2299 case OVS_KEY_ATTR_TUNNEL
:
2300 #ifndef USE_UPSTREAM_TUNNEL
2301 if (eth_p_mpls(eth_type
))
2305 return -EINVAL
; /* Masked tunnel set not supported. */
2308 err
= validate_and_copy_set_tun(a
, sfa
, log
);
2313 case OVS_KEY_ATTR_IPV4
:
2314 if (eth_type
!= htons(ETH_P_IP
))
2317 ipv4_key
= nla_data(ovs_key
);
2320 const struct ovs_key_ipv4
*mask
= ipv4_key
+ 1;
2322 /* Non-writeable fields. */
2323 if (mask
->ipv4_proto
|| mask
->ipv4_frag
)
2326 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
2329 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
2334 case OVS_KEY_ATTR_IPV6
:
2335 if (eth_type
!= htons(ETH_P_IPV6
))
2338 ipv6_key
= nla_data(ovs_key
);
2341 const struct ovs_key_ipv6
*mask
= ipv6_key
+ 1;
2343 /* Non-writeable fields. */
2344 if (mask
->ipv6_proto
|| mask
->ipv6_frag
)
2347 /* Invalid bits in the flow label mask? */
2348 if (ntohl(mask
->ipv6_label
) & 0xFFF00000)
2351 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
2354 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
2357 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
2362 case OVS_KEY_ATTR_TCP
:
2363 if ((eth_type
!= htons(ETH_P_IP
) &&
2364 eth_type
!= htons(ETH_P_IPV6
)) ||
2365 flow_key
->ip
.proto
!= IPPROTO_TCP
)
2370 case OVS_KEY_ATTR_UDP
:
2371 if ((eth_type
!= htons(ETH_P_IP
) &&
2372 eth_type
!= htons(ETH_P_IPV6
)) ||
2373 flow_key
->ip
.proto
!= IPPROTO_UDP
)
2378 case OVS_KEY_ATTR_MPLS
:
2379 if (!eth_p_mpls(eth_type
))
2383 case OVS_KEY_ATTR_SCTP
:
2384 if ((eth_type
!= htons(ETH_P_IP
) &&
2385 eth_type
!= htons(ETH_P_IPV6
)) ||
2386 flow_key
->ip
.proto
!= IPPROTO_SCTP
)
2395 /* Convert non-masked non-tunnel set actions to masked set actions. */
2396 if (!masked
&& key_type
!= OVS_KEY_ATTR_TUNNEL
) {
2397 int start
, len
= key_len
* 2;
2402 start
= add_nested_action_start(sfa
,
2403 OVS_ACTION_ATTR_SET_TO_MASKED
,
2408 at
= __add_action(sfa
, key_type
, NULL
, len
, log
);
2412 memcpy(nla_data(at
), nla_data(ovs_key
), key_len
); /* Key. */
2413 memset(nla_data(at
) + key_len
, 0xff, key_len
); /* Mask. */
2414 /* Clear non-writeable bits from otherwise writeable fields. */
2415 if (key_type
== OVS_KEY_ATTR_IPV6
) {
2416 struct ovs_key_ipv6
*mask
= nla_data(at
) + key_len
;
2418 mask
->ipv6_label
&= htonl(0x000FFFFF);
2420 add_nested_action_end(*sfa
, start
);
2426 static int validate_userspace(const struct nlattr
*attr
)
2428 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
2429 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
2430 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
2431 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = {.type
= NLA_U32
},
2433 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
2436 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
, attr
,
2437 userspace_policy
, NULL
);
2441 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
2442 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
2448 static int copy_action(const struct nlattr
*from
,
2449 struct sw_flow_actions
**sfa
, bool log
)
2451 int totlen
= NLA_ALIGN(from
->nla_len
);
2454 to
= reserve_sfa_size(sfa
, from
->nla_len
, log
);
2458 memcpy(to
, from
, totlen
);
2462 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2463 const struct sw_flow_key
*key
,
2464 struct sw_flow_actions
**sfa
,
2465 __be16 eth_type
, __be16 vlan_tci
, bool log
)
2467 u8 mac_proto
= ovs_key_mac_proto(key
);
2468 const struct nlattr
*a
;
2471 nla_for_each_nested(a
, attr
, rem
) {
2472 /* Expected argument lengths, (u32)-1 for variable length. */
2473 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
2474 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
2475 [OVS_ACTION_ATTR_RECIRC
] = sizeof(u32
),
2476 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
2477 [OVS_ACTION_ATTR_PUSH_MPLS
] = sizeof(struct ovs_action_push_mpls
),
2478 [OVS_ACTION_ATTR_POP_MPLS
] = sizeof(__be16
),
2479 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
2480 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
2481 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
2482 [OVS_ACTION_ATTR_SET_MASKED
] = (u32
)-1,
2483 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1,
2484 [OVS_ACTION_ATTR_HASH
] = sizeof(struct ovs_action_hash
),
2485 [OVS_ACTION_ATTR_CT
] = (u32
)-1,
2486 [OVS_ACTION_ATTR_TRUNC
] = sizeof(struct ovs_action_trunc
),
2487 [OVS_ACTION_ATTR_PUSH_ETH
] = sizeof(struct ovs_action_push_eth
),
2488 [OVS_ACTION_ATTR_POP_ETH
] = 0,
2490 const struct ovs_action_push_vlan
*vlan
;
2491 int type
= nla_type(a
);
2494 if (type
> OVS_ACTION_ATTR_MAX
||
2495 (action_lens
[type
] != nla_len(a
) &&
2496 action_lens
[type
] != (u32
)-1))
2501 case OVS_ACTION_ATTR_UNSPEC
:
2504 case OVS_ACTION_ATTR_USERSPACE
:
2505 err
= validate_userspace(a
);
2510 case OVS_ACTION_ATTR_OUTPUT
:
2511 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
2515 case OVS_ACTION_ATTR_TRUNC
: {
2516 const struct ovs_action_trunc
*trunc
= nla_data(a
);
2518 if (trunc
->max_len
< ETH_HLEN
)
2523 case OVS_ACTION_ATTR_HASH
: {
2524 const struct ovs_action_hash
*act_hash
= nla_data(a
);
2526 switch (act_hash
->hash_alg
) {
2527 case OVS_HASH_ALG_L4
:
2536 case OVS_ACTION_ATTR_POP_VLAN
:
2537 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2539 vlan_tci
= htons(0);
2542 case OVS_ACTION_ATTR_PUSH_VLAN
:
2543 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2546 if (!eth_type_vlan(vlan
->vlan_tpid
))
2548 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
2550 vlan_tci
= vlan
->vlan_tci
;
2553 case OVS_ACTION_ATTR_RECIRC
:
2556 case OVS_ACTION_ATTR_PUSH_MPLS
: {
2557 const struct ovs_action_push_mpls
*mpls
= nla_data(a
);
2559 if (!eth_p_mpls(mpls
->mpls_ethertype
))
2561 /* Prohibit push MPLS other than to a white list
2562 * for packets that have a known tag order.
2564 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2565 (eth_type
!= htons(ETH_P_IP
) &&
2566 eth_type
!= htons(ETH_P_IPV6
) &&
2567 eth_type
!= htons(ETH_P_ARP
) &&
2568 eth_type
!= htons(ETH_P_RARP
) &&
2569 !eth_p_mpls(eth_type
)))
2571 eth_type
= mpls
->mpls_ethertype
;
2575 case OVS_ACTION_ATTR_POP_MPLS
:
2576 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2577 !eth_p_mpls(eth_type
))
2580 /* Disallow subsequent L2.5+ set and mpls_pop actions
2581 * as there is no check here to ensure that the new
2582 * eth_type is valid and thus set actions could
2583 * write off the end of the packet or otherwise
2586 * Support for these actions is planned using packet
2589 eth_type
= htons(0);
2592 case OVS_ACTION_ATTR_SET
:
2593 err
= validate_set(a
, key
, sfa
,
2594 &skip_copy
, mac_proto
, eth_type
,
2600 case OVS_ACTION_ATTR_SET_MASKED
:
2601 err
= validate_set(a
, key
, sfa
,
2602 &skip_copy
, mac_proto
, eth_type
,
2608 case OVS_ACTION_ATTR_SAMPLE
: {
2609 bool last
= nla_is_last(a
, rem
);
2611 err
= validate_and_copy_sample(net
, a
, key
, sfa
,
2620 case OVS_ACTION_ATTR_CT
:
2621 err
= ovs_ct_copy_action(net
, a
, key
, sfa
, log
);
2627 case OVS_ACTION_ATTR_PUSH_ETH
:
2628 /* Disallow pushing an Ethernet header if one
2629 * is already present */
2630 if (mac_proto
!= MAC_PROTO_NONE
)
2632 mac_proto
= MAC_PROTO_NONE
;
2635 case OVS_ACTION_ATTR_POP_ETH
:
2636 if (mac_proto
!= MAC_PROTO_ETHERNET
)
2638 if (vlan_tci
& htons(VLAN_TAG_PRESENT
))
2640 mac_proto
= MAC_PROTO_ETHERNET
;
2644 OVS_NLERR(log
, "Unknown Action type %d", type
);
2648 err
= copy_action(a
, sfa
, log
);
2660 /* 'key' must be the masked key. */
2661 int ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2662 const struct sw_flow_key
*key
,
2663 struct sw_flow_actions
**sfa
, bool log
)
2667 *sfa
= nla_alloc_flow_actions(nla_len(attr
), log
);
2669 return PTR_ERR(*sfa
);
2671 (*sfa
)->orig_len
= nla_len(attr
);
2672 err
= __ovs_nla_copy_actions(net
, attr
, key
, sfa
, key
->eth
.type
,
2673 key
->eth
.vlan
.tci
, log
);
2675 ovs_nla_free_flow_actions(*sfa
);
2680 static int sample_action_to_attr(const struct nlattr
*attr
,
2681 struct sk_buff
*skb
)
2683 struct nlattr
*start
, *ac_start
= NULL
, *sample_arg
;
2684 int err
= 0, rem
= nla_len(attr
);
2685 const struct sample_arg
*arg
;
2686 struct nlattr
*actions
;
2688 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SAMPLE
);
2692 sample_arg
= nla_data(attr
);
2693 arg
= nla_data(sample_arg
);
2694 actions
= nla_next(sample_arg
, &rem
);
2696 if (nla_put_u32(skb
, OVS_SAMPLE_ATTR_PROBABILITY
, arg
->probability
)) {
2701 ac_start
= nla_nest_start(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
2707 err
= ovs_nla_put_actions(actions
, rem
, skb
);
2711 nla_nest_cancel(skb
, ac_start
);
2712 nla_nest_cancel(skb
, start
);
2714 nla_nest_end(skb
, ac_start
);
2715 nla_nest_end(skb
, start
);
2721 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
2723 const struct nlattr
*ovs_key
= nla_data(a
);
2724 int key_type
= nla_type(ovs_key
);
2725 struct nlattr
*start
;
2729 case OVS_KEY_ATTR_TUNNEL_INFO
: {
2730 struct ovs_tunnel_info
*ovs_tun
= nla_data(ovs_key
);
2731 struct ip_tunnel_info
*tun_info
= &ovs_tun
->tun_dst
->u
.tun_info
;
2733 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
2737 err
= ip_tun_to_nlattr(skb
, &tun_info
->key
,
2738 ip_tunnel_info_opts(tun_info
),
2739 tun_info
->options_len
,
2740 ip_tunnel_info_af(tun_info
));
2743 nla_nest_end(skb
, start
);
2747 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
2755 static int masked_set_action_to_set_action_attr(const struct nlattr
*a
,
2756 struct sk_buff
*skb
)
2758 const struct nlattr
*ovs_key
= nla_data(a
);
2760 size_t key_len
= nla_len(ovs_key
) / 2;
2762 /* Revert the conversion we did from a non-masked set action to
2763 * masked set action.
2765 nla
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
2769 if (nla_put(skb
, nla_type(ovs_key
), key_len
, nla_data(ovs_key
)))
2772 nla_nest_end(skb
, nla
);
2776 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
2778 const struct nlattr
*a
;
2781 nla_for_each_attr(a
, attr
, len
, rem
) {
2782 int type
= nla_type(a
);
2785 case OVS_ACTION_ATTR_SET
:
2786 err
= set_action_to_attr(a
, skb
);
2791 case OVS_ACTION_ATTR_SET_TO_MASKED
:
2792 err
= masked_set_action_to_set_action_attr(a
, skb
);
2797 case OVS_ACTION_ATTR_SAMPLE
:
2798 err
= sample_action_to_attr(a
, skb
);
2803 case OVS_ACTION_ATTR_CT
:
2804 err
= ovs_ct_action_to_attr(nla_data(a
), skb
);
2810 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))