2 * Copyright (c) 2007-2015 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 void update_range(struct sw_flow_match
*match
,
65 size_t offset
, size_t size
, bool is_mask
)
67 struct sw_flow_key_range
*range
;
68 size_t start
= rounddown(offset
, sizeof(long));
69 size_t end
= roundup(offset
+ size
, sizeof(long));
72 range
= &match
->range
;
74 range
= &match
->mask
->range
;
76 if (range
->start
== range
->end
) {
82 if (range
->start
> start
)
89 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
91 update_range(match, offsetof(struct sw_flow_key, field), \
92 sizeof((match)->key->field), is_mask); \
94 (match)->mask->key.field = value; \
96 (match)->key->field = value; \
99 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
101 update_range(match, offset, len, is_mask); \
103 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
105 memcpy((u8 *)(match)->key + offset, value_p, len); \
108 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
109 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
110 value_p, len, is_mask)
112 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
114 update_range(match, offsetof(struct sw_flow_key, field), \
115 sizeof((match)->key->field), is_mask); \
117 memset((u8 *)&(match)->mask->key.field, value, \
118 sizeof((match)->mask->key.field)); \
120 memset((u8 *)&(match)->key->field, value, \
121 sizeof((match)->key->field)); \
124 static bool match_validate(const struct sw_flow_match
*match
,
125 u64 key_attrs
, u64 mask_attrs
, bool log
)
127 u64 key_expected
= 1ULL << OVS_KEY_ATTR_ETHERNET
;
128 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
130 /* The following mask attributes allowed only if they
131 * pass the validation tests.
133 mask_allowed
&= ~((1ULL << OVS_KEY_ATTR_IPV4
)
134 | (1ULL << OVS_KEY_ATTR_IPV6
)
135 | (1ULL << OVS_KEY_ATTR_TCP
)
136 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)
137 | (1ULL << OVS_KEY_ATTR_UDP
)
138 | (1ULL << OVS_KEY_ATTR_SCTP
)
139 | (1ULL << OVS_KEY_ATTR_ICMP
)
140 | (1ULL << OVS_KEY_ATTR_ICMPV6
)
141 | (1ULL << OVS_KEY_ATTR_ARP
)
142 | (1ULL << OVS_KEY_ATTR_ND
)
143 | (1ULL << OVS_KEY_ATTR_MPLS
));
145 /* Always allowed mask fields. */
146 mask_allowed
|= ((1ULL << OVS_KEY_ATTR_TUNNEL
)
147 | (1ULL << OVS_KEY_ATTR_IN_PORT
)
148 | (1ULL << OVS_KEY_ATTR_ETHERTYPE
));
150 /* Check key attributes. */
151 if (match
->key
->eth
.type
== htons(ETH_P_ARP
)
152 || match
->key
->eth
.type
== htons(ETH_P_RARP
)) {
153 key_expected
|= 1ULL << OVS_KEY_ATTR_ARP
;
154 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
155 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ARP
;
158 if (eth_p_mpls(match
->key
->eth
.type
)) {
159 key_expected
|= 1ULL << OVS_KEY_ATTR_MPLS
;
160 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
161 mask_allowed
|= 1ULL << OVS_KEY_ATTR_MPLS
;
164 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
165 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV4
;
166 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
167 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV4
;
169 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
170 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
171 key_expected
|= 1ULL << OVS_KEY_ATTR_UDP
;
172 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
173 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
176 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
177 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
178 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
179 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
182 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
183 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
184 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
185 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
186 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
187 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
191 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
192 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMP
;
193 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
194 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ICMP
;
199 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
200 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV6
;
201 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
202 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV6
;
204 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
205 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
206 key_expected
|= 1ULL << OVS_KEY_ATTR_UDP
;
207 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
208 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
211 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
212 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
213 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
214 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
217 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
218 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
219 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
220 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
221 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
222 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
226 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
227 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMPV6
;
228 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
229 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ICMPV6
;
231 if (match
->key
->tp
.src
==
232 htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
233 match
->key
->tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
234 key_expected
|= 1ULL << OVS_KEY_ATTR_ND
;
235 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xff)))
236 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ND
;
242 if ((key_attrs
& key_expected
) != key_expected
) {
243 /* Key attributes check failed. */
244 OVS_NLERR(log
, "Missing key (keys=%llx, expected=%llx)",
245 (unsigned long long)key_attrs
,
246 (unsigned long long)key_expected
);
250 if ((mask_attrs
& mask_allowed
) != mask_attrs
) {
251 /* Mask attributes check failed. */
252 OVS_NLERR(log
, "Unexpected mask (mask=%llx, allowed=%llx)",
253 (unsigned long long)mask_attrs
,
254 (unsigned long long)mask_allowed
);
261 size_t ovs_tun_key_attr_size(void)
263 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
264 * updating this function.
266 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
267 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
268 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
269 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
270 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
271 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
272 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
273 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
274 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
275 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
276 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
278 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
279 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
282 size_t ovs_key_attr_size(void)
284 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
285 * updating this function.
287 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO
!= 26);
289 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
290 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
291 + ovs_tun_key_attr_size()
292 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
293 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
294 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
295 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
296 + nla_total_size(1) /* OVS_KEY_ATTR_CT_STATE */
297 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
298 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
299 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
300 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
301 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
302 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
303 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
304 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
305 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
308 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
309 [OVS_VXLAN_EXT_GBP
] = { .len
= sizeof(u32
) },
312 static const struct ovs_len_tbl ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
313 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= sizeof(u64
) },
314 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= sizeof(u32
) },
315 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= sizeof(u32
) },
316 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
317 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
318 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
319 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
320 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= sizeof(u16
) },
321 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= sizeof(u16
) },
322 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
323 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
324 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= OVS_ATTR_NESTED
,
325 .next
= ovs_vxlan_ext_key_lens
},
328 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
329 static const struct ovs_len_tbl ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
330 [OVS_KEY_ATTR_ENCAP
] = { .len
= OVS_ATTR_NESTED
},
331 [OVS_KEY_ATTR_PRIORITY
] = { .len
= sizeof(u32
) },
332 [OVS_KEY_ATTR_IN_PORT
] = { .len
= sizeof(u32
) },
333 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= sizeof(u32
) },
334 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
335 [OVS_KEY_ATTR_VLAN
] = { .len
= sizeof(__be16
) },
336 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= sizeof(__be16
) },
337 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
338 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
339 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
340 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= sizeof(__be16
) },
341 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
342 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
343 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
344 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
345 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
346 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
347 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= sizeof(u32
) },
348 [OVS_KEY_ATTR_DP_HASH
] = { .len
= sizeof(u32
) },
349 [OVS_KEY_ATTR_TUNNEL
] = { .len
= OVS_ATTR_NESTED
,
350 .next
= ovs_tunnel_key_lens
, },
351 [OVS_KEY_ATTR_MPLS
] = { .len
= sizeof(struct ovs_key_mpls
) },
352 [OVS_KEY_ATTR_CT_STATE
] = { .len
= sizeof(u8
) },
353 [OVS_KEY_ATTR_CT_ZONE
] = { .len
= sizeof(u16
) },
356 static bool check_attr_len(unsigned int attr_len
, unsigned int expected_len
)
358 return expected_len
== attr_len
||
359 expected_len
== OVS_ATTR_NESTED
||
360 expected_len
== OVS_ATTR_VARIABLE
;
363 static bool is_all_zero(const u8
*fp
, size_t size
)
370 for (i
= 0; i
< size
; i
++)
377 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
378 const struct nlattr
*a
[],
379 u64
*attrsp
, bool log
, bool nz
)
381 const struct nlattr
*nla
;
386 nla_for_each_nested(nla
, attr
, rem
) {
387 u16 type
= nla_type(nla
);
390 if (type
> OVS_KEY_ATTR_MAX
) {
391 OVS_NLERR(log
, "Key type %d is out of range max %d",
392 type
, OVS_KEY_ATTR_MAX
);
396 if (attrs
& (1ULL << type
)) {
397 OVS_NLERR(log
, "Duplicate key (type %d).", type
);
401 expected_len
= ovs_key_lens
[type
].len
;
402 if (!check_attr_len(nla_len(nla
), expected_len
)) {
403 OVS_NLERR(log
, "Key %d has unexpected len %d expected %d",
404 type
, nla_len(nla
), expected_len
);
408 if (!nz
|| !is_all_zero(nla_data(nla
), expected_len
)) {
409 attrs
|= 1ULL << type
;
414 OVS_NLERR(log
, "Message has %d unknown bytes.", rem
);
422 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
423 const struct nlattr
*a
[], u64
*attrsp
,
426 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, true);
429 static int parse_flow_nlattrs(const struct nlattr
*attr
,
430 const struct nlattr
*a
[], u64
*attrsp
,
433 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, false);
436 static int genev_tun_opt_from_nlattr(const struct nlattr
*a
,
437 struct sw_flow_match
*match
, bool is_mask
,
440 unsigned long opt_key_offset
;
442 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
443 OVS_NLERR(log
, "Geneve option length err (len %d, max %zu).",
444 nla_len(a
), sizeof(match
->key
->tun_opts
));
448 if (nla_len(a
) % 4 != 0) {
449 OVS_NLERR(log
, "Geneve opt len %d is not a multiple of 4.",
454 /* We need to record the length of the options passed
455 * down, otherwise packets with the same format but
456 * additional options will be silently matched.
459 SW_FLOW_KEY_PUT(match
, tun_opts_len
, nla_len(a
),
462 /* This is somewhat unusual because it looks at
463 * both the key and mask while parsing the
464 * attributes (and by extension assumes the key
465 * is parsed first). Normally, we would verify
466 * that each is the correct length and that the
467 * attributes line up in the validate function.
468 * However, that is difficult because this is
469 * variable length and we won't have the
472 if (match
->key
->tun_opts_len
!= nla_len(a
)) {
473 OVS_NLERR(log
, "Geneve option len %d != mask len %d",
474 match
->key
->tun_opts_len
, nla_len(a
));
478 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
481 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
482 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
483 nla_len(a
), is_mask
);
487 static int vxlan_tun_opt_from_nlattr(const struct nlattr
*attr
,
488 struct sw_flow_match
*match
, bool is_mask
,
493 unsigned long opt_key_offset
;
494 struct vxlan_metadata opts
;
496 BUILD_BUG_ON(sizeof(opts
) > sizeof(match
->key
->tun_opts
));
498 memset(&opts
, 0, sizeof(opts
));
499 nla_for_each_nested(a
, attr
, rem
) {
500 int type
= nla_type(a
);
502 if (type
> OVS_VXLAN_EXT_MAX
) {
503 OVS_NLERR(log
, "VXLAN extension %d out of range max %d",
504 type
, OVS_VXLAN_EXT_MAX
);
508 if (!check_attr_len(nla_len(a
),
509 ovs_vxlan_ext_key_lens
[type
].len
)) {
510 OVS_NLERR(log
, "VXLAN extension %d has unexpected len %d expected %d",
512 ovs_vxlan_ext_key_lens
[type
].len
);
517 case OVS_VXLAN_EXT_GBP
:
518 opts
.gbp
= nla_get_u32(a
);
521 OVS_NLERR(log
, "Unknown VXLAN extension attribute %d",
527 OVS_NLERR(log
, "VXLAN extension message has %d unknown bytes.",
533 SW_FLOW_KEY_PUT(match
, tun_opts_len
, sizeof(opts
), false);
535 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
537 opt_key_offset
= TUN_METADATA_OFFSET(sizeof(opts
));
538 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, &opts
, sizeof(opts
),
543 static int ipv4_tun_from_nlattr(const struct nlattr
*attr
,
544 struct sw_flow_match
*match
, bool is_mask
,
550 __be16 tun_flags
= 0;
553 nla_for_each_nested(a
, attr
, rem
) {
554 int type
= nla_type(a
);
557 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
558 OVS_NLERR(log
, "Tunnel attr %d out of range max %d",
559 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
563 if (!check_attr_len(nla_len(a
),
564 ovs_tunnel_key_lens
[type
].len
)) {
565 OVS_NLERR(log
, "Tunnel attr %d has unexpected len %d expected %d",
566 type
, nla_len(a
), ovs_tunnel_key_lens
[type
].len
);
571 case OVS_TUNNEL_KEY_ATTR_ID
:
572 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
573 nla_get_be64(a
), is_mask
);
574 tun_flags
|= TUNNEL_KEY
;
576 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
577 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.src
,
578 nla_get_in_addr(a
), is_mask
);
580 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
581 SW_FLOW_KEY_PUT(match
, tun_key
.u
.ipv4
.dst
,
582 nla_get_in_addr(a
), is_mask
);
584 case OVS_TUNNEL_KEY_ATTR_TOS
:
585 SW_FLOW_KEY_PUT(match
, tun_key
.tos
,
586 nla_get_u8(a
), is_mask
);
588 case OVS_TUNNEL_KEY_ATTR_TTL
:
589 SW_FLOW_KEY_PUT(match
, tun_key
.ttl
,
590 nla_get_u8(a
), is_mask
);
593 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
594 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
596 case OVS_TUNNEL_KEY_ATTR_CSUM
:
597 tun_flags
|= TUNNEL_CSUM
;
599 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
600 SW_FLOW_KEY_PUT(match
, tun_key
.tp_src
,
601 nla_get_be16(a
), is_mask
);
603 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
604 SW_FLOW_KEY_PUT(match
, tun_key
.tp_dst
,
605 nla_get_be16(a
), is_mask
);
607 case OVS_TUNNEL_KEY_ATTR_OAM
:
608 tun_flags
|= TUNNEL_OAM
;
610 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
612 OVS_NLERR(log
, "Multiple metadata blocks provided");
616 err
= genev_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
620 tun_flags
|= TUNNEL_GENEVE_OPT
;
623 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
625 OVS_NLERR(log
, "Multiple metadata blocks provided");
629 err
= vxlan_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
633 tun_flags
|= TUNNEL_VXLAN_OPT
;
637 OVS_NLERR(log
, "Unknown IPv4 tunnel attribute %d",
643 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
646 OVS_NLERR(log
, "IPv4 tunnel attribute has %d unknown bytes.",
652 if (!match
->key
->tun_key
.u
.ipv4
.dst
) {
653 OVS_NLERR(log
, "IPv4 tunnel dst address is zero");
658 OVS_NLERR(log
, "IPv4 tunnel TTL not specified.");
666 static int vxlan_opt_to_nlattr(struct sk_buff
*skb
,
667 const void *tun_opts
, int swkey_tun_opts_len
)
669 const struct vxlan_metadata
*opts
= tun_opts
;
672 nla
= nla_nest_start(skb
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
676 if (nla_put_u32(skb
, OVS_VXLAN_EXT_GBP
, opts
->gbp
) < 0)
679 nla_nest_end(skb
, nla
);
683 static int __ipv4_tun_to_nlattr(struct sk_buff
*skb
,
684 const struct ip_tunnel_key
*output
,
685 const void *tun_opts
, int swkey_tun_opts_len
)
687 if (output
->tun_flags
& TUNNEL_KEY
&&
688 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
))
690 if (output
->u
.ipv4
.src
&&
691 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
,
694 if (output
->u
.ipv4
.dst
&&
695 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
,
699 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->tos
))
701 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ttl
))
703 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
704 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
706 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
707 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
709 if (output
->tp_src
&&
710 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, output
->tp_src
))
712 if (output
->tp_dst
&&
713 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_DST
, output
->tp_dst
))
715 if ((output
->tun_flags
& TUNNEL_OAM
) &&
716 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_OAM
))
719 if (output
->tun_flags
& TUNNEL_GENEVE_OPT
&&
720 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
721 swkey_tun_opts_len
, tun_opts
))
723 else if (output
->tun_flags
& TUNNEL_VXLAN_OPT
&&
724 vxlan_opt_to_nlattr(skb
, tun_opts
, swkey_tun_opts_len
))
731 static int ipv4_tun_to_nlattr(struct sk_buff
*skb
,
732 const struct ip_tunnel_key
*output
,
733 const void *tun_opts
, int swkey_tun_opts_len
)
738 nla
= nla_nest_start(skb
, OVS_KEY_ATTR_TUNNEL
);
742 err
= __ipv4_tun_to_nlattr(skb
, output
, tun_opts
, swkey_tun_opts_len
);
746 nla_nest_end(skb
, nla
);
750 int ovs_nla_put_egress_tunnel_key(struct sk_buff
*skb
,
751 const struct ip_tunnel_info
*egress_tun_info
,
752 const void *egress_tun_opts
)
754 return __ipv4_tun_to_nlattr(skb
, &egress_tun_info
->key
,
756 egress_tun_info
->options_len
);
759 static int metadata_from_nlattrs(struct sw_flow_match
*match
, u64
*attrs
,
760 const struct nlattr
**a
, bool is_mask
,
763 if (*attrs
& (1ULL << OVS_KEY_ATTR_DP_HASH
)) {
764 u32 hash_val
= nla_get_u32(a
[OVS_KEY_ATTR_DP_HASH
]);
766 SW_FLOW_KEY_PUT(match
, ovs_flow_hash
, hash_val
, is_mask
);
767 *attrs
&= ~(1ULL << OVS_KEY_ATTR_DP_HASH
);
770 if (*attrs
& (1ULL << OVS_KEY_ATTR_RECIRC_ID
)) {
771 u32 recirc_id
= nla_get_u32(a
[OVS_KEY_ATTR_RECIRC_ID
]);
773 SW_FLOW_KEY_PUT(match
, recirc_id
, recirc_id
, is_mask
);
774 *attrs
&= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID
);
777 if (*attrs
& (1ULL << OVS_KEY_ATTR_PRIORITY
)) {
778 SW_FLOW_KEY_PUT(match
, phy
.priority
,
779 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
780 *attrs
&= ~(1ULL << OVS_KEY_ATTR_PRIORITY
);
783 if (*attrs
& (1ULL << OVS_KEY_ATTR_IN_PORT
)) {
784 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
787 in_port
= 0xffffffff; /* Always exact match in_port. */
788 } else if (in_port
>= DP_MAX_PORTS
) {
789 OVS_NLERR(log
, "Port %d exceeds max allowable %d",
790 in_port
, DP_MAX_PORTS
);
794 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
795 *attrs
&= ~(1ULL << OVS_KEY_ATTR_IN_PORT
);
796 } else if (!is_mask
) {
797 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
800 if (*attrs
& (1ULL << OVS_KEY_ATTR_SKB_MARK
)) {
801 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
803 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
804 *attrs
&= ~(1ULL << OVS_KEY_ATTR_SKB_MARK
);
806 if (*attrs
& (1ULL << OVS_KEY_ATTR_TUNNEL
)) {
807 if (ipv4_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
810 *attrs
&= ~(1ULL << OVS_KEY_ATTR_TUNNEL
);
813 if (*attrs
& (1 << OVS_KEY_ATTR_CT_STATE
) &&
814 ovs_ct_verify(OVS_KEY_ATTR_CT_STATE
)) {
815 u8 ct_state
= nla_get_u8(a
[OVS_KEY_ATTR_CT_STATE
]);
817 SW_FLOW_KEY_PUT(match
, ct
.state
, ct_state
, is_mask
);
818 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_STATE
);
820 if (*attrs
& (1 << OVS_KEY_ATTR_CT_ZONE
) &&
821 ovs_ct_verify(OVS_KEY_ATTR_CT_ZONE
)) {
822 u16 ct_zone
= nla_get_u16(a
[OVS_KEY_ATTR_CT_ZONE
]);
824 SW_FLOW_KEY_PUT(match
, ct
.zone
, ct_zone
, is_mask
);
825 *attrs
&= ~(1ULL << OVS_KEY_ATTR_CT_ZONE
);
830 static int ovs_key_from_nlattrs(struct sw_flow_match
*match
, u64 attrs
,
831 const struct nlattr
**a
, bool is_mask
,
836 err
= metadata_from_nlattrs(match
, &attrs
, a
, is_mask
, log
);
840 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) {
841 const struct ovs_key_ethernet
*eth_key
;
843 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
844 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
845 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
846 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
847 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
848 attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERNET
);
851 if (attrs
& (1ULL << OVS_KEY_ATTR_VLAN
)) {
854 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
855 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
857 OVS_NLERR(log
, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
859 OVS_NLERR(log
, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
864 SW_FLOW_KEY_PUT(match
, eth
.tci
, tci
, is_mask
);
865 attrs
&= ~(1ULL << OVS_KEY_ATTR_VLAN
);
868 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)) {
871 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
873 /* Always exact match EtherType. */
874 eth_type
= htons(0xffff);
875 } else if (!eth_proto_is_802_3(eth_type
)) {
876 OVS_NLERR(log
, "EtherType %x is less than min %x",
877 ntohs(eth_type
), ETH_P_802_3_MIN
);
881 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
882 attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE
);
883 } else if (!is_mask
) {
884 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
887 if (attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
888 const struct ovs_key_ipv4
*ipv4_key
;
890 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
891 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
892 OVS_NLERR(log
, "IPv4 frag type %d is out of range max %d",
893 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
896 SW_FLOW_KEY_PUT(match
, ip
.proto
,
897 ipv4_key
->ipv4_proto
, is_mask
);
898 SW_FLOW_KEY_PUT(match
, ip
.tos
,
899 ipv4_key
->ipv4_tos
, is_mask
);
900 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
901 ipv4_key
->ipv4_ttl
, is_mask
);
902 SW_FLOW_KEY_PUT(match
, ip
.frag
,
903 ipv4_key
->ipv4_frag
, is_mask
);
904 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
905 ipv4_key
->ipv4_src
, is_mask
);
906 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
907 ipv4_key
->ipv4_dst
, is_mask
);
908 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
911 if (attrs
& (1ULL << OVS_KEY_ATTR_IPV6
)) {
912 const struct ovs_key_ipv6
*ipv6_key
;
914 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
915 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
916 OVS_NLERR(log
, "IPv6 frag type %d is out of range max %d",
917 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
921 if (!is_mask
&& ipv6_key
->ipv6_label
& htonl(0xFFF00000)) {
923 "Invalid IPv6 flow label value (value=%x, max=%x).",
924 ntohl(ipv6_key
->ipv6_label
), (1 << 20) - 1);
928 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
929 ipv6_key
->ipv6_label
, is_mask
);
930 SW_FLOW_KEY_PUT(match
, ip
.proto
,
931 ipv6_key
->ipv6_proto
, is_mask
);
932 SW_FLOW_KEY_PUT(match
, ip
.tos
,
933 ipv6_key
->ipv6_tclass
, is_mask
);
934 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
935 ipv6_key
->ipv6_hlimit
, is_mask
);
936 SW_FLOW_KEY_PUT(match
, ip
.frag
,
937 ipv6_key
->ipv6_frag
, is_mask
);
938 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
940 sizeof(match
->key
->ipv6
.addr
.src
),
942 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
944 sizeof(match
->key
->ipv6
.addr
.dst
),
947 attrs
&= ~(1ULL << OVS_KEY_ATTR_IPV6
);
950 if (attrs
& (1ULL << OVS_KEY_ATTR_ARP
)) {
951 const struct ovs_key_arp
*arp_key
;
953 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
954 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
955 OVS_NLERR(log
, "Unknown ARP opcode (opcode=%d).",
960 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
961 arp_key
->arp_sip
, is_mask
);
962 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
963 arp_key
->arp_tip
, is_mask
);
964 SW_FLOW_KEY_PUT(match
, ip
.proto
,
965 ntohs(arp_key
->arp_op
), is_mask
);
966 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
967 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
968 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
969 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
971 attrs
&= ~(1ULL << OVS_KEY_ATTR_ARP
);
974 if (attrs
& (1ULL << OVS_KEY_ATTR_MPLS
)) {
975 const struct ovs_key_mpls
*mpls_key
;
977 mpls_key
= nla_data(a
[OVS_KEY_ATTR_MPLS
]);
978 SW_FLOW_KEY_PUT(match
, mpls
.top_lse
,
979 mpls_key
->mpls_lse
, is_mask
);
981 attrs
&= ~(1ULL << OVS_KEY_ATTR_MPLS
);
984 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP
)) {
985 const struct ovs_key_tcp
*tcp_key
;
987 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
988 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
989 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
990 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP
);
993 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)) {
994 SW_FLOW_KEY_PUT(match
, tp
.flags
,
995 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
997 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS
);
1000 if (attrs
& (1ULL << OVS_KEY_ATTR_UDP
)) {
1001 const struct ovs_key_udp
*udp_key
;
1003 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
1004 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
1005 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
1006 attrs
&= ~(1ULL << OVS_KEY_ATTR_UDP
);
1009 if (attrs
& (1ULL << OVS_KEY_ATTR_SCTP
)) {
1010 const struct ovs_key_sctp
*sctp_key
;
1012 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
1013 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
1014 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
1015 attrs
&= ~(1ULL << OVS_KEY_ATTR_SCTP
);
1018 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMP
)) {
1019 const struct ovs_key_icmp
*icmp_key
;
1021 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
1022 SW_FLOW_KEY_PUT(match
, tp
.src
,
1023 htons(icmp_key
->icmp_type
), is_mask
);
1024 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1025 htons(icmp_key
->icmp_code
), is_mask
);
1026 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMP
);
1029 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMPV6
)) {
1030 const struct ovs_key_icmpv6
*icmpv6_key
;
1032 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
1033 SW_FLOW_KEY_PUT(match
, tp
.src
,
1034 htons(icmpv6_key
->icmpv6_type
), is_mask
);
1035 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1036 htons(icmpv6_key
->icmpv6_code
), is_mask
);
1037 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMPV6
);
1040 if (attrs
& (1ULL << OVS_KEY_ATTR_ND
)) {
1041 const struct ovs_key_nd
*nd_key
;
1043 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
1044 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
1046 sizeof(match
->key
->ipv6
.nd
.target
),
1048 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
1049 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
1050 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
1051 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
1052 attrs
&= ~(1ULL << OVS_KEY_ATTR_ND
);
1056 OVS_NLERR(log
, "Unknown key attributes %llx",
1057 (unsigned long long)attrs
);
1064 static void nlattr_set(struct nlattr
*attr
, u8 val
,
1065 const struct ovs_len_tbl
*tbl
)
1070 /* The nlattr stream should already have been validated */
1071 nla_for_each_nested(nla
, attr
, rem
) {
1072 if (tbl
[nla_type(nla
)].len
== OVS_ATTR_NESTED
) {
1073 if (tbl
[nla_type(nla
)].next
)
1074 tbl
= tbl
[nla_type(nla
)].next
;
1075 nlattr_set(nla
, val
, tbl
);
1077 memset(nla_data(nla
), val
, nla_len(nla
));
1082 static void mask_set_nlattr(struct nlattr
*attr
, u8 val
)
1084 nlattr_set(attr
, val
, ovs_key_lens
);
1088 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1089 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1090 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1091 * does not include any don't care bit.
1092 * @match: receives the extracted flow match information.
1093 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1094 * sequence. The fields should of the packet that triggered the creation
1096 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1097 * attribute specifies the mask field of the wildcarded flow.
1098 * @log: Boolean to allow kernel error logging. Normally true, but when
1099 * probing for feature compatibility this should be passed in as false to
1100 * suppress unnecessary error logging.
1102 int ovs_nla_get_match(struct sw_flow_match
*match
,
1103 const struct nlattr
*nla_key
,
1104 const struct nlattr
*nla_mask
,
1107 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1108 const struct nlattr
*encap
;
1109 struct nlattr
*newmask
= NULL
;
1112 bool encap_valid
= false;
1115 err
= parse_flow_nlattrs(nla_key
, a
, &key_attrs
, log
);
1119 if ((key_attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) &&
1120 (key_attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)) &&
1121 (nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]) == htons(ETH_P_8021Q
))) {
1124 if (!((key_attrs
& (1ULL << OVS_KEY_ATTR_VLAN
)) &&
1125 (key_attrs
& (1ULL << OVS_KEY_ATTR_ENCAP
)))) {
1126 OVS_NLERR(log
, "Invalid Vlan frame.");
1130 key_attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE
);
1131 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1132 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1133 key_attrs
&= ~(1ULL << OVS_KEY_ATTR_ENCAP
);
1136 if (tci
& htons(VLAN_TAG_PRESENT
)) {
1137 err
= parse_flow_nlattrs(encap
, a
, &key_attrs
, log
);
1141 /* Corner case for truncated 802.1Q header. */
1142 if (nla_len(encap
)) {
1143 OVS_NLERR(log
, "Truncated 802.1Q header has non-zero encap attribute.");
1147 OVS_NLERR(log
, "Encap attr is set for non-VLAN frame");
1152 err
= ovs_key_from_nlattrs(match
, key_attrs
, a
, false, log
);
1158 /* Create an exact match mask. We need to set to 0xff
1159 * all the 'match->mask' fields that have been touched
1160 * in 'match->key'. We cannot simply memset
1161 * 'match->mask', because padding bytes and fields not
1162 * specified in 'match->key' should be left to 0.
1163 * Instead, we use a stream of netlink attributes,
1164 * copied from 'key' and set to 0xff.
1165 * ovs_key_from_nlattrs() will take care of filling
1166 * 'match->mask' appropriately.
1168 newmask
= kmemdup(nla_key
,
1169 nla_total_size(nla_len(nla_key
)),
1174 mask_set_nlattr(newmask
, 0xff);
1176 /* The userspace does not send tunnel attributes that
1177 * are 0, but we should not wildcard them nonetheless.
1179 if (match
->key
->tun_key
.u
.ipv4
.dst
)
1180 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_key
,
1186 err
= parse_flow_mask_nlattrs(nla_mask
, a
, &mask_attrs
, log
);
1190 /* Always match on tci. */
1191 SW_FLOW_KEY_PUT(match
, eth
.tci
, htons(0xffff), true);
1193 if (mask_attrs
& 1ULL << OVS_KEY_ATTR_ENCAP
) {
1194 __be16 eth_type
= 0;
1198 OVS_NLERR(log
, "Encap mask attribute is set for non-VLAN frame.");
1203 mask_attrs
&= ~(1ULL << OVS_KEY_ATTR_ENCAP
);
1204 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
1205 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1207 if (eth_type
== htons(0xffff)) {
1208 mask_attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE
);
1209 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1210 err
= parse_flow_mask_nlattrs(encap
, a
,
1215 OVS_NLERR(log
, "VLAN frames must have an exact match on the TPID (mask=%x).",
1221 if (a
[OVS_KEY_ATTR_VLAN
])
1222 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1224 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
1225 OVS_NLERR(log
, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1232 err
= ovs_key_from_nlattrs(match
, mask_attrs
, a
, true, log
);
1237 if (!match_validate(match
, key_attrs
, mask_attrs
, log
))
1245 static size_t get_ufid_len(const struct nlattr
*attr
, bool log
)
1252 len
= nla_len(attr
);
1253 if (len
< 1 || len
> MAX_UFID_LENGTH
) {
1254 OVS_NLERR(log
, "ufid size %u bytes exceeds the range (1, %d)",
1255 nla_len(attr
), MAX_UFID_LENGTH
);
1262 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1263 * or false otherwise.
1265 bool ovs_nla_get_ufid(struct sw_flow_id
*sfid
, const struct nlattr
*attr
,
1268 sfid
->ufid_len
= get_ufid_len(attr
, log
);
1270 memcpy(sfid
->ufid
, nla_data(attr
), sfid
->ufid_len
);
1272 return sfid
->ufid_len
;
1275 int ovs_nla_get_identifier(struct sw_flow_id
*sfid
, const struct nlattr
*ufid
,
1276 const struct sw_flow_key
*key
, bool log
)
1278 struct sw_flow_key
*new_key
;
1280 if (ovs_nla_get_ufid(sfid
, ufid
, log
))
1283 /* If UFID was not provided, use unmasked key. */
1284 new_key
= kmalloc(sizeof(*new_key
), GFP_KERNEL
);
1287 memcpy(new_key
, key
, sizeof(*key
));
1288 sfid
->unmasked_key
= new_key
;
1293 u32
ovs_nla_get_ufid_flags(const struct nlattr
*attr
)
1295 return attr
? nla_get_u32(attr
) : 0;
1299 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1300 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
1301 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1303 * @log: Boolean to allow kernel error logging. Normally true, but when
1304 * probing for feature compatibility this should be passed in as false to
1305 * suppress unnecessary error logging.
1307 * This parses a series of Netlink attributes that form a flow key, which must
1308 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1309 * get the metadata, that is, the parts of the flow key that cannot be
1310 * extracted from the packet itself.
1313 int ovs_nla_get_flow_metadata(const struct nlattr
*attr
,
1314 struct sw_flow_key
*key
,
1317 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1318 struct sw_flow_match match
;
1322 err
= parse_flow_nlattrs(attr
, a
, &attrs
, log
);
1326 memset(&match
, 0, sizeof(match
));
1329 memset(key
, 0, OVS_SW_FLOW_KEY_METADATA_SIZE
);
1330 memset(&key
->ct
, 0, sizeof(key
->ct
));
1331 key
->phy
.in_port
= DP_MAX_PORTS
;
1333 return metadata_from_nlattrs(&match
, &attrs
, a
, false, log
);
1336 static int __ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1337 const struct sw_flow_key
*output
, bool is_mask
,
1338 struct sk_buff
*skb
)
1340 struct ovs_key_ethernet
*eth_key
;
1341 struct nlattr
*nla
, *encap
;
1343 if (nla_put_u32(skb
, OVS_KEY_ATTR_RECIRC_ID
, output
->recirc_id
))
1344 goto nla_put_failure
;
1346 if (nla_put_u32(skb
, OVS_KEY_ATTR_DP_HASH
, output
->ovs_flow_hash
))
1347 goto nla_put_failure
;
1349 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
1350 goto nla_put_failure
;
1352 if ((swkey
->tun_key
.u
.ipv4
.dst
|| is_mask
)) {
1353 const void *opts
= NULL
;
1355 if (output
->tun_key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
)
1356 opts
= TUN_METADATA_OPTS(output
, swkey
->tun_opts_len
);
1358 if (ipv4_tun_to_nlattr(skb
, &output
->tun_key
, opts
,
1359 swkey
->tun_opts_len
))
1360 goto nla_put_failure
;
1363 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
1364 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
1365 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
1366 goto nla_put_failure
;
1369 upper_u16
= !is_mask
? 0 : 0xffff;
1371 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
1372 (upper_u16
<< 16) | output
->phy
.in_port
))
1373 goto nla_put_failure
;
1376 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
1377 goto nla_put_failure
;
1379 if (ovs_ct_put_key(output
, skb
))
1380 goto nla_put_failure
;
1382 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
1384 goto nla_put_failure
;
1386 eth_key
= nla_data(nla
);
1387 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
1388 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
1390 if (swkey
->eth
.tci
|| swkey
->eth
.type
== htons(ETH_P_8021Q
)) {
1392 eth_type
= !is_mask
? htons(ETH_P_8021Q
) : htons(0xffff);
1393 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
1394 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, output
->eth
.tci
))
1395 goto nla_put_failure
;
1396 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1397 if (!swkey
->eth
.tci
)
1402 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
1404 * Ethertype 802.2 is represented in the netlink with omitted
1405 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1406 * 0xffff in the mask attribute. Ethertype can also
1409 if (is_mask
&& output
->eth
.type
)
1410 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
1412 goto nla_put_failure
;
1416 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
1417 goto nla_put_failure
;
1419 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1420 struct ovs_key_ipv4
*ipv4_key
;
1422 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
1424 goto nla_put_failure
;
1425 ipv4_key
= nla_data(nla
);
1426 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
1427 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
1428 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
1429 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
1430 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
1431 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
1432 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1433 struct ovs_key_ipv6
*ipv6_key
;
1435 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
1437 goto nla_put_failure
;
1438 ipv6_key
= nla_data(nla
);
1439 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
1440 sizeof(ipv6_key
->ipv6_src
));
1441 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
1442 sizeof(ipv6_key
->ipv6_dst
));
1443 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
1444 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
1445 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
1446 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
1447 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
1448 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1449 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1450 struct ovs_key_arp
*arp_key
;
1452 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
1454 goto nla_put_failure
;
1455 arp_key
= nla_data(nla
);
1456 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
1457 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
1458 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
1459 arp_key
->arp_op
= htons(output
->ip
.proto
);
1460 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
1461 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
1462 } else if (eth_p_mpls(swkey
->eth
.type
)) {
1463 struct ovs_key_mpls
*mpls_key
;
1465 nla
= nla_reserve(skb
, OVS_KEY_ATTR_MPLS
, sizeof(*mpls_key
));
1467 goto nla_put_failure
;
1468 mpls_key
= nla_data(nla
);
1469 mpls_key
->mpls_lse
= output
->mpls
.top_lse
;
1472 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
1473 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
1474 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1476 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
1477 struct ovs_key_tcp
*tcp_key
;
1479 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
1481 goto nla_put_failure
;
1482 tcp_key
= nla_data(nla
);
1483 tcp_key
->tcp_src
= output
->tp
.src
;
1484 tcp_key
->tcp_dst
= output
->tp
.dst
;
1485 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
1487 goto nla_put_failure
;
1488 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1489 struct ovs_key_udp
*udp_key
;
1491 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1493 goto nla_put_failure
;
1494 udp_key
= nla_data(nla
);
1495 udp_key
->udp_src
= output
->tp
.src
;
1496 udp_key
->udp_dst
= output
->tp
.dst
;
1497 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
1498 struct ovs_key_sctp
*sctp_key
;
1500 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
1502 goto nla_put_failure
;
1503 sctp_key
= nla_data(nla
);
1504 sctp_key
->sctp_src
= output
->tp
.src
;
1505 sctp_key
->sctp_dst
= output
->tp
.dst
;
1506 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1507 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1508 struct ovs_key_icmp
*icmp_key
;
1510 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1512 goto nla_put_failure
;
1513 icmp_key
= nla_data(nla
);
1514 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
1515 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
1516 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1517 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1518 struct ovs_key_icmpv6
*icmpv6_key
;
1520 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1521 sizeof(*icmpv6_key
));
1523 goto nla_put_failure
;
1524 icmpv6_key
= nla_data(nla
);
1525 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
1526 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
1528 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1529 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1530 struct ovs_key_nd
*nd_key
;
1532 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1534 goto nla_put_failure
;
1535 nd_key
= nla_data(nla
);
1536 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
1537 sizeof(nd_key
->nd_target
));
1538 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
1539 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
1546 nla_nest_end(skb
, encap
);
1554 int ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1555 const struct sw_flow_key
*output
, int attr
, bool is_mask
,
1556 struct sk_buff
*skb
)
1561 nla
= nla_nest_start(skb
, attr
);
1564 err
= __ovs_nla_put_key(swkey
, output
, is_mask
, skb
);
1567 nla_nest_end(skb
, nla
);
1572 /* Called with ovs_mutex or RCU read lock. */
1573 int ovs_nla_put_identifier(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1575 if (ovs_identifier_is_ufid(&flow
->id
))
1576 return nla_put(skb
, OVS_FLOW_ATTR_UFID
, flow
->id
.ufid_len
,
1579 return ovs_nla_put_key(flow
->id
.unmasked_key
, flow
->id
.unmasked_key
,
1580 OVS_FLOW_ATTR_KEY
, false, skb
);
1583 /* Called with ovs_mutex or RCU read lock. */
1584 int ovs_nla_put_masked_key(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1586 return ovs_nla_put_key(&flow
->key
, &flow
->key
,
1587 OVS_FLOW_ATTR_KEY
, false, skb
);
1590 /* Called with ovs_mutex or RCU read lock. */
1591 int ovs_nla_put_mask(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1593 return ovs_nla_put_key(&flow
->key
, &flow
->mask
->key
,
1594 OVS_FLOW_ATTR_MASK
, true, skb
);
1597 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1599 static struct sw_flow_actions
*nla_alloc_flow_actions(int size
, bool log
)
1601 struct sw_flow_actions
*sfa
;
1603 if (size
> MAX_ACTIONS_BUFSIZE
) {
1604 OVS_NLERR(log
, "Flow action size %u bytes exceeds max", size
);
1605 return ERR_PTR(-EINVAL
);
1608 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
1610 return ERR_PTR(-ENOMEM
);
1612 sfa
->actions_len
= 0;
1616 static void ovs_nla_free_set_action(const struct nlattr
*a
)
1618 const struct nlattr
*ovs_key
= nla_data(a
);
1619 struct ovs_tunnel_info
*ovs_tun
;
1621 switch (nla_type(ovs_key
)) {
1622 case OVS_KEY_ATTR_TUNNEL_INFO
:
1623 ovs_tun
= nla_data(ovs_key
);
1624 ovs_dst_release((struct dst_entry
*)ovs_tun
->tun_dst
);
1629 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
1631 const struct nlattr
*a
;
1637 nla_for_each_attr(a
, sf_acts
->actions
, sf_acts
->actions_len
, rem
) {
1638 switch (nla_type(a
)) {
1639 case OVS_ACTION_ATTR_SET
:
1640 ovs_nla_free_set_action(a
);
1642 case OVS_ACTION_ATTR_CT
:
1643 ovs_ct_free_action(a
);
1651 static void __ovs_nla_free_flow_actions(struct rcu_head
*head
)
1653 ovs_nla_free_flow_actions(container_of(head
, struct sw_flow_actions
, rcu
));
1656 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1657 * The caller must hold rcu_read_lock for this to be sensible.
1659 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions
*sf_acts
)
1661 call_rcu(&sf_acts
->rcu
, __ovs_nla_free_flow_actions
);
1664 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
1665 int attr_len
, bool log
)
1668 struct sw_flow_actions
*acts
;
1670 int req_size
= NLA_ALIGN(attr_len
);
1671 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
1672 (*sfa
)->actions_len
;
1674 if (req_size
<= (ksize(*sfa
) - next_offset
))
1677 new_acts_size
= ksize(*sfa
) * 2;
1679 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
1680 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
)
1681 return ERR_PTR(-EMSGSIZE
);
1682 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
1685 acts
= nla_alloc_flow_actions(new_acts_size
, log
);
1687 return (void *)acts
;
1689 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
1690 acts
->actions_len
= (*sfa
)->actions_len
;
1691 acts
->orig_len
= (*sfa
)->orig_len
;
1696 (*sfa
)->actions_len
+= req_size
;
1697 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
1700 static struct nlattr
*__add_action(struct sw_flow_actions
**sfa
,
1701 int attrtype
, void *data
, int len
, bool log
)
1705 a
= reserve_sfa_size(sfa
, nla_attr_size(len
), log
);
1709 a
->nla_type
= attrtype
;
1710 a
->nla_len
= nla_attr_size(len
);
1713 memcpy(nla_data(a
), data
, len
);
1714 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
1719 int ovs_nla_add_action(struct sw_flow_actions
**sfa
, int attrtype
, void *data
,
1724 a
= __add_action(sfa
, attrtype
, data
, len
, log
);
1731 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
1732 int attrtype
, bool log
)
1734 int used
= (*sfa
)->actions_len
;
1737 err
= ovs_nla_add_action(sfa
, attrtype
, NULL
, 0, log
);
1744 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
1747 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
1750 a
->nla_len
= sfa
->actions_len
- st_offset
;
1753 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
1754 const struct sw_flow_key
*key
,
1755 int depth
, struct sw_flow_actions
**sfa
,
1756 __be16 eth_type
, __be16 vlan_tci
, bool log
);
1758 static int validate_and_copy_sample(struct net
*net
, const struct nlattr
*attr
,
1759 const struct sw_flow_key
*key
, int depth
,
1760 struct sw_flow_actions
**sfa
,
1761 __be16 eth_type
, __be16 vlan_tci
, bool log
)
1763 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
1764 const struct nlattr
*probability
, *actions
;
1765 const struct nlattr
*a
;
1766 int rem
, start
, err
, st_acts
;
1768 memset(attrs
, 0, sizeof(attrs
));
1769 nla_for_each_nested(a
, attr
, rem
) {
1770 int type
= nla_type(a
);
1771 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
1778 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
1779 if (!probability
|| nla_len(probability
) != sizeof(u32
))
1782 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
1783 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
1786 /* validation done, copy sample action. */
1787 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
, log
);
1790 err
= ovs_nla_add_action(sfa
, OVS_SAMPLE_ATTR_PROBABILITY
,
1791 nla_data(probability
), sizeof(u32
), log
);
1794 st_acts
= add_nested_action_start(sfa
, OVS_SAMPLE_ATTR_ACTIONS
, log
);
1798 err
= __ovs_nla_copy_actions(net
, actions
, key
, depth
+ 1, sfa
,
1799 eth_type
, vlan_tci
, log
);
1803 add_nested_action_end(*sfa
, st_acts
);
1804 add_nested_action_end(*sfa
, start
);
1809 void ovs_match_init(struct sw_flow_match
*match
,
1810 struct sw_flow_key
*key
,
1811 struct sw_flow_mask
*mask
)
1813 memset(match
, 0, sizeof(*match
));
1817 memset(key
, 0, sizeof(*key
));
1820 memset(&mask
->key
, 0, sizeof(mask
->key
));
1821 mask
->range
.start
= mask
->range
.end
= 0;
1825 static int validate_geneve_opts(struct sw_flow_key
*key
)
1827 struct geneve_opt
*option
;
1828 int opts_len
= key
->tun_opts_len
;
1829 bool crit_opt
= false;
1831 option
= (struct geneve_opt
*)TUN_METADATA_OPTS(key
, key
->tun_opts_len
);
1832 while (opts_len
> 0) {
1835 if (opts_len
< sizeof(*option
))
1838 len
= sizeof(*option
) + option
->length
* 4;
1842 crit_opt
|= !!(option
->type
& GENEVE_CRIT_OPT_TYPE
);
1844 option
= (struct geneve_opt
*)((u8
*)option
+ len
);
1848 key
->tun_key
.tun_flags
|= crit_opt
? TUNNEL_CRIT_OPT
: 0;
1853 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
1854 struct sw_flow_actions
**sfa
, bool log
)
1856 struct sw_flow_match match
;
1857 struct sw_flow_key key
;
1858 struct metadata_dst
*tun_dst
;
1859 struct ip_tunnel_info
*tun_info
;
1860 struct ovs_tunnel_info
*ovs_tun
;
1862 int err
= 0, start
, opts_type
;
1864 ovs_match_init(&match
, &key
, NULL
);
1865 opts_type
= ipv4_tun_from_nlattr(nla_data(attr
), &match
, false, log
);
1869 if (key
.tun_opts_len
) {
1870 switch (opts_type
) {
1871 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
1872 err
= validate_geneve_opts(&key
);
1876 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
1881 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
, log
);
1885 tun_dst
= metadata_dst_alloc(key
.tun_opts_len
, GFP_KERNEL
);
1889 a
= __add_action(sfa
, OVS_KEY_ATTR_TUNNEL_INFO
, NULL
,
1890 sizeof(*ovs_tun
), log
);
1892 ovs_dst_release((struct dst_entry
*)tun_dst
);
1896 ovs_tun
= nla_data(a
);
1897 ovs_tun
->tun_dst
= tun_dst
;
1899 tun_info
= &tun_dst
->u
.tun_info
;
1900 tun_info
->mode
= IP_TUNNEL_INFO_TX
;
1901 tun_info
->key
= key
.tun_key
;
1903 /* We need to store the options in the action itself since
1904 * everything else will go away after flow setup. We can append
1905 * it to tun_info and then point there.
1907 ip_tunnel_info_opts_set(tun_info
,
1908 TUN_METADATA_OPTS(&key
, key
.tun_opts_len
),
1910 add_nested_action_end(*sfa
, start
);
1915 /* Return false if there are any non-masked bits set.
1916 * Mask follows data immediately, before any netlink padding.
1918 static bool validate_masked(u8
*data
, int len
)
1920 u8
*mask
= data
+ len
;
1923 if (*data
++ & ~*mask
++)
1929 static int validate_set(const struct nlattr
*a
,
1930 const struct sw_flow_key
*flow_key
,
1931 struct sw_flow_actions
**sfa
,
1932 bool *skip_copy
, __be16 eth_type
, bool masked
, bool log
)
1934 const struct nlattr
*ovs_key
= nla_data(a
);
1935 int key_type
= nla_type(ovs_key
);
1938 /* There can be only one key in a action */
1939 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
1942 key_len
= nla_len(ovs_key
);
1946 if (key_type
> OVS_KEY_ATTR_MAX
||
1947 !check_attr_len(key_len
, ovs_key_lens
[key_type
].len
))
1950 if (masked
&& !validate_masked(nla_data(ovs_key
), key_len
))
1954 const struct ovs_key_ipv4
*ipv4_key
;
1955 const struct ovs_key_ipv6
*ipv6_key
;
1958 case OVS_KEY_ATTR_PRIORITY
:
1959 case OVS_KEY_ATTR_SKB_MARK
:
1960 case OVS_KEY_ATTR_ETHERNET
:
1963 case OVS_KEY_ATTR_TUNNEL
:
1964 if (eth_p_mpls(eth_type
))
1968 return -EINVAL
; /* Masked tunnel set not supported. */
1971 err
= validate_and_copy_set_tun(a
, sfa
, log
);
1976 case OVS_KEY_ATTR_IPV4
:
1977 if (eth_type
!= htons(ETH_P_IP
))
1980 ipv4_key
= nla_data(ovs_key
);
1983 const struct ovs_key_ipv4
*mask
= ipv4_key
+ 1;
1985 /* Non-writeable fields. */
1986 if (mask
->ipv4_proto
|| mask
->ipv4_frag
)
1989 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
1992 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
1997 case OVS_KEY_ATTR_IPV6
:
1998 if (eth_type
!= htons(ETH_P_IPV6
))
2001 ipv6_key
= nla_data(ovs_key
);
2004 const struct ovs_key_ipv6
*mask
= ipv6_key
+ 1;
2006 /* Non-writeable fields. */
2007 if (mask
->ipv6_proto
|| mask
->ipv6_frag
)
2010 /* Invalid bits in the flow label mask? */
2011 if (ntohl(mask
->ipv6_label
) & 0xFFF00000)
2014 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
2017 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
2020 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
2025 case OVS_KEY_ATTR_TCP
:
2026 if ((eth_type
!= htons(ETH_P_IP
) &&
2027 eth_type
!= htons(ETH_P_IPV6
)) ||
2028 flow_key
->ip
.proto
!= IPPROTO_TCP
)
2033 case OVS_KEY_ATTR_UDP
:
2034 if ((eth_type
!= htons(ETH_P_IP
) &&
2035 eth_type
!= htons(ETH_P_IPV6
)) ||
2036 flow_key
->ip
.proto
!= IPPROTO_UDP
)
2041 case OVS_KEY_ATTR_MPLS
:
2042 if (!eth_p_mpls(eth_type
))
2046 case OVS_KEY_ATTR_SCTP
:
2047 if ((eth_type
!= htons(ETH_P_IP
) &&
2048 eth_type
!= htons(ETH_P_IPV6
)) ||
2049 flow_key
->ip
.proto
!= IPPROTO_SCTP
)
2058 /* Convert non-masked non-tunnel set actions to masked set actions. */
2059 if (!masked
&& key_type
!= OVS_KEY_ATTR_TUNNEL
) {
2060 int start
, len
= key_len
* 2;
2065 start
= add_nested_action_start(sfa
,
2066 OVS_ACTION_ATTR_SET_TO_MASKED
,
2071 at
= __add_action(sfa
, key_type
, NULL
, len
, log
);
2075 memcpy(nla_data(at
), nla_data(ovs_key
), key_len
); /* Key. */
2076 memset(nla_data(at
) + key_len
, 0xff, key_len
); /* Mask. */
2077 /* Clear non-writeable bits from otherwise writeable fields. */
2078 if (key_type
== OVS_KEY_ATTR_IPV6
) {
2079 struct ovs_key_ipv6
*mask
= nla_data(at
) + key_len
;
2081 mask
->ipv6_label
&= htonl(0x000FFFFF);
2083 add_nested_action_end(*sfa
, start
);
2089 static int validate_userspace(const struct nlattr
*attr
)
2091 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
2092 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
2093 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
2094 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = {.type
= NLA_U32
},
2096 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
2099 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
,
2100 attr
, userspace_policy
);
2104 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
2105 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
2111 static int copy_action(const struct nlattr
*from
,
2112 struct sw_flow_actions
**sfa
, bool log
)
2114 int totlen
= NLA_ALIGN(from
->nla_len
);
2117 to
= reserve_sfa_size(sfa
, from
->nla_len
, log
);
2121 memcpy(to
, from
, totlen
);
2125 static int __ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2126 const struct sw_flow_key
*key
,
2127 int depth
, struct sw_flow_actions
**sfa
,
2128 __be16 eth_type
, __be16 vlan_tci
, bool log
)
2130 const struct nlattr
*a
;
2133 if (depth
>= SAMPLE_ACTION_DEPTH
)
2136 nla_for_each_nested(a
, attr
, rem
) {
2137 /* Expected argument lengths, (u32)-1 for variable length. */
2138 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
2139 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
2140 [OVS_ACTION_ATTR_RECIRC
] = sizeof(u32
),
2141 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
2142 [OVS_ACTION_ATTR_PUSH_MPLS
] = sizeof(struct ovs_action_push_mpls
),
2143 [OVS_ACTION_ATTR_POP_MPLS
] = sizeof(__be16
),
2144 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
2145 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
2146 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
2147 [OVS_ACTION_ATTR_SET_MASKED
] = (u32
)-1,
2148 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1,
2149 [OVS_ACTION_ATTR_HASH
] = sizeof(struct ovs_action_hash
),
2150 [OVS_ACTION_ATTR_CT
] = (u32
)-1,
2152 const struct ovs_action_push_vlan
*vlan
;
2153 int type
= nla_type(a
);
2156 if (type
> OVS_ACTION_ATTR_MAX
||
2157 (action_lens
[type
] != nla_len(a
) &&
2158 action_lens
[type
] != (u32
)-1))
2163 case OVS_ACTION_ATTR_UNSPEC
:
2166 case OVS_ACTION_ATTR_USERSPACE
:
2167 err
= validate_userspace(a
);
2172 case OVS_ACTION_ATTR_OUTPUT
:
2173 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
2177 case OVS_ACTION_ATTR_HASH
: {
2178 const struct ovs_action_hash
*act_hash
= nla_data(a
);
2180 switch (act_hash
->hash_alg
) {
2181 case OVS_HASH_ALG_L4
:
2190 case OVS_ACTION_ATTR_POP_VLAN
:
2191 vlan_tci
= htons(0);
2194 case OVS_ACTION_ATTR_PUSH_VLAN
:
2196 if (vlan
->vlan_tpid
!= htons(ETH_P_8021Q
))
2198 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
2200 vlan_tci
= vlan
->vlan_tci
;
2203 case OVS_ACTION_ATTR_RECIRC
:
2206 case OVS_ACTION_ATTR_PUSH_MPLS
: {
2207 const struct ovs_action_push_mpls
*mpls
= nla_data(a
);
2209 if (!eth_p_mpls(mpls
->mpls_ethertype
))
2211 /* Prohibit push MPLS other than to a white list
2212 * for packets that have a known tag order.
2214 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2215 (eth_type
!= htons(ETH_P_IP
) &&
2216 eth_type
!= htons(ETH_P_IPV6
) &&
2217 eth_type
!= htons(ETH_P_ARP
) &&
2218 eth_type
!= htons(ETH_P_RARP
) &&
2219 !eth_p_mpls(eth_type
)))
2221 eth_type
= mpls
->mpls_ethertype
;
2225 case OVS_ACTION_ATTR_POP_MPLS
:
2226 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2227 !eth_p_mpls(eth_type
))
2230 /* Disallow subsequent L2.5+ set and mpls_pop actions
2231 * as there is no check here to ensure that the new
2232 * eth_type is valid and thus set actions could
2233 * write off the end of the packet or otherwise
2236 * Support for these actions is planned using packet
2239 eth_type
= htons(0);
2242 case OVS_ACTION_ATTR_SET
:
2243 err
= validate_set(a
, key
, sfa
,
2244 &skip_copy
, eth_type
, false, log
);
2249 case OVS_ACTION_ATTR_SET_MASKED
:
2250 err
= validate_set(a
, key
, sfa
,
2251 &skip_copy
, eth_type
, true, log
);
2256 case OVS_ACTION_ATTR_SAMPLE
:
2257 err
= validate_and_copy_sample(net
, a
, key
, depth
, sfa
,
2258 eth_type
, vlan_tci
, log
);
2264 case OVS_ACTION_ATTR_CT
:
2265 err
= ovs_ct_copy_action(net
, a
, key
, sfa
, log
);
2272 OVS_NLERR(log
, "Unknown Action type %d", type
);
2276 err
= copy_action(a
, sfa
, log
);
2288 /* 'key' must be the masked key. */
2289 int ovs_nla_copy_actions(struct net
*net
, const struct nlattr
*attr
,
2290 const struct sw_flow_key
*key
,
2291 struct sw_flow_actions
**sfa
, bool log
)
2295 *sfa
= nla_alloc_flow_actions(nla_len(attr
), log
);
2297 return PTR_ERR(*sfa
);
2299 (*sfa
)->orig_len
= nla_len(attr
);
2300 err
= __ovs_nla_copy_actions(net
, attr
, key
, 0, sfa
, key
->eth
.type
,
2303 ovs_nla_free_flow_actions(*sfa
);
2308 static int sample_action_to_attr(const struct nlattr
*attr
, struct sk_buff
*skb
)
2310 const struct nlattr
*a
;
2311 struct nlattr
*start
;
2314 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SAMPLE
);
2318 nla_for_each_nested(a
, attr
, rem
) {
2319 int type
= nla_type(a
);
2320 struct nlattr
*st_sample
;
2323 case OVS_SAMPLE_ATTR_PROBABILITY
:
2324 if (nla_put(skb
, OVS_SAMPLE_ATTR_PROBABILITY
,
2325 sizeof(u32
), nla_data(a
)))
2328 case OVS_SAMPLE_ATTR_ACTIONS
:
2329 st_sample
= nla_nest_start(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
2332 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
2335 nla_nest_end(skb
, st_sample
);
2340 nla_nest_end(skb
, start
);
2344 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
2346 const struct nlattr
*ovs_key
= nla_data(a
);
2347 int key_type
= nla_type(ovs_key
);
2348 struct nlattr
*start
;
2352 case OVS_KEY_ATTR_TUNNEL_INFO
: {
2353 struct ovs_tunnel_info
*ovs_tun
= nla_data(ovs_key
);
2354 struct ip_tunnel_info
*tun_info
= &ovs_tun
->tun_dst
->u
.tun_info
;
2356 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
2360 err
= ipv4_tun_to_nlattr(skb
, &tun_info
->key
,
2361 tun_info
->options_len
?
2362 ip_tunnel_info_opts(tun_info
) : NULL
,
2363 tun_info
->options_len
);
2366 nla_nest_end(skb
, start
);
2370 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
2378 static int masked_set_action_to_set_action_attr(const struct nlattr
*a
,
2379 struct sk_buff
*skb
)
2381 const struct nlattr
*ovs_key
= nla_data(a
);
2382 size_t key_len
= nla_len(ovs_key
) / 2;
2384 /* Revert the conversion we did from a non-masked set action to
2385 * masked set action.
2387 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
) - key_len
, ovs_key
))
2393 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
2395 const struct nlattr
*a
;
2398 nla_for_each_attr(a
, attr
, len
, rem
) {
2399 int type
= nla_type(a
);
2402 case OVS_ACTION_ATTR_SET
:
2403 err
= set_action_to_attr(a
, skb
);
2408 case OVS_ACTION_ATTR_SET_TO_MASKED
:
2409 err
= masked_set_action_to_set_action_attr(a
, skb
);
2414 case OVS_ACTION_ATTR_SAMPLE
:
2415 err
= sample_action_to_attr(a
, skb
);
2420 case OVS_ACTION_ATTR_CT
:
2421 err
= ovs_ct_action_to_attr(nla_data(a
), skb
);
2427 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))