2 * Copyright (c) 2007-2014 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>
51 #include "flow_netlink.h"
52 #include "vport-vxlan.h"
56 const struct ovs_len_tbl
*next
;
59 #define OVS_ATTR_NESTED -1
60 #define OVS_ATTR_VARIABLE -2
62 static void update_range(struct sw_flow_match
*match
,
63 size_t offset
, size_t size
, bool is_mask
)
65 struct sw_flow_key_range
*range
;
66 size_t start
= rounddown(offset
, sizeof(long));
67 size_t end
= roundup(offset
+ size
, sizeof(long));
70 range
= &match
->range
;
72 range
= &match
->mask
->range
;
74 if (range
->start
== range
->end
) {
80 if (range
->start
> start
)
87 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
89 update_range(match, offsetof(struct sw_flow_key, field), \
90 sizeof((match)->key->field), is_mask); \
92 (match)->mask->key.field = value; \
94 (match)->key->field = value; \
97 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
99 update_range(match, offset, len, is_mask); \
101 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
103 memcpy((u8 *)(match)->key + offset, value_p, len); \
106 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
107 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
108 value_p, len, is_mask)
110 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
112 update_range(match, offsetof(struct sw_flow_key, field), \
113 sizeof((match)->key->field), is_mask); \
115 memset((u8 *)&(match)->mask->key.field, value, \
116 sizeof((match)->mask->key.field)); \
118 memset((u8 *)&(match)->key->field, value, \
119 sizeof((match)->key->field)); \
122 static bool match_validate(const struct sw_flow_match
*match
,
123 u64 key_attrs
, u64 mask_attrs
, bool log
)
125 u64 key_expected
= 1ULL << OVS_KEY_ATTR_ETHERNET
;
126 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
128 /* The following mask attributes allowed only if they
129 * pass the validation tests.
131 mask_allowed
&= ~((1ULL << OVS_KEY_ATTR_IPV4
)
132 | (1ULL << OVS_KEY_ATTR_IPV6
)
133 | (1ULL << OVS_KEY_ATTR_TCP
)
134 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)
135 | (1ULL << OVS_KEY_ATTR_UDP
)
136 | (1ULL << OVS_KEY_ATTR_SCTP
)
137 | (1ULL << OVS_KEY_ATTR_ICMP
)
138 | (1ULL << OVS_KEY_ATTR_ICMPV6
)
139 | (1ULL << OVS_KEY_ATTR_ARP
)
140 | (1ULL << OVS_KEY_ATTR_ND
)
141 | (1ULL << OVS_KEY_ATTR_MPLS
));
143 /* Always allowed mask fields. */
144 mask_allowed
|= ((1ULL << OVS_KEY_ATTR_TUNNEL
)
145 | (1ULL << OVS_KEY_ATTR_IN_PORT
)
146 | (1ULL << OVS_KEY_ATTR_ETHERTYPE
));
148 /* Check key attributes. */
149 if (match
->key
->eth
.type
== htons(ETH_P_ARP
)
150 || match
->key
->eth
.type
== htons(ETH_P_RARP
)) {
151 key_expected
|= 1ULL << OVS_KEY_ATTR_ARP
;
152 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
153 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ARP
;
156 if (eth_p_mpls(match
->key
->eth
.type
)) {
157 key_expected
|= 1ULL << OVS_KEY_ATTR_MPLS
;
158 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
159 mask_allowed
|= 1ULL << OVS_KEY_ATTR_MPLS
;
162 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
163 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV4
;
164 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
165 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV4
;
167 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
168 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
169 key_expected
|= 1ULL << OVS_KEY_ATTR_UDP
;
170 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
171 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
174 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
175 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
176 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
177 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
180 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
181 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
182 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
183 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
184 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
185 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
189 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
190 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMP
;
191 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
192 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ICMP
;
197 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
198 key_expected
|= 1ULL << OVS_KEY_ATTR_IPV6
;
199 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
200 mask_allowed
|= 1ULL << OVS_KEY_ATTR_IPV6
;
202 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
203 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
204 key_expected
|= 1ULL << OVS_KEY_ATTR_UDP
;
205 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
206 mask_allowed
|= 1ULL << OVS_KEY_ATTR_UDP
;
209 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
210 key_expected
|= 1ULL << OVS_KEY_ATTR_SCTP
;
211 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
212 mask_allowed
|= 1ULL << OVS_KEY_ATTR_SCTP
;
215 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
216 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP
;
217 key_expected
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
218 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
219 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP
;
220 mask_allowed
|= 1ULL << OVS_KEY_ATTR_TCP_FLAGS
;
224 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
225 key_expected
|= 1ULL << OVS_KEY_ATTR_ICMPV6
;
226 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
227 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ICMPV6
;
229 if (match
->key
->tp
.src
==
230 htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
231 match
->key
->tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
232 key_expected
|= 1ULL << OVS_KEY_ATTR_ND
;
233 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xff)))
234 mask_allowed
|= 1ULL << OVS_KEY_ATTR_ND
;
240 if ((key_attrs
& key_expected
) != key_expected
) {
241 /* Key attributes check failed. */
242 OVS_NLERR(log
, "Missing key (keys=%llx, expected=%llx)",
243 (unsigned long long)key_attrs
,
244 (unsigned long long)key_expected
);
248 if ((mask_attrs
& mask_allowed
) != mask_attrs
) {
249 /* Mask attributes check failed. */
250 OVS_NLERR(log
, "Unexpected mask (mask=%llx, allowed=%llx)",
251 (unsigned long long)mask_attrs
,
252 (unsigned long long)mask_allowed
);
259 size_t ovs_tun_key_attr_size(void)
261 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
262 * updating this function.
264 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
265 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
266 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
267 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
268 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
269 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
270 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
271 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
272 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
273 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
274 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
276 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
277 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
280 size_t ovs_key_attr_size(void)
282 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
283 * updating this function.
285 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO
!= 26);
287 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
288 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
289 + ovs_tun_key_attr_size()
290 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
291 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
292 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
293 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
294 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
295 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
296 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
297 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
298 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
299 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
300 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
301 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
304 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens
[OVS_VXLAN_EXT_MAX
+ 1] = {
305 [OVS_VXLAN_EXT_GBP
] = { .len
= sizeof(u32
) },
308 static const struct ovs_len_tbl ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
309 [OVS_TUNNEL_KEY_ATTR_ID
] = { .len
= sizeof(u64
) },
310 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = { .len
= sizeof(u32
) },
311 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = { .len
= sizeof(u32
) },
312 [OVS_TUNNEL_KEY_ATTR_TOS
] = { .len
= 1 },
313 [OVS_TUNNEL_KEY_ATTR_TTL
] = { .len
= 1 },
314 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = { .len
= 0 },
315 [OVS_TUNNEL_KEY_ATTR_CSUM
] = { .len
= 0 },
316 [OVS_TUNNEL_KEY_ATTR_TP_SRC
] = { .len
= sizeof(u16
) },
317 [OVS_TUNNEL_KEY_ATTR_TP_DST
] = { .len
= sizeof(u16
) },
318 [OVS_TUNNEL_KEY_ATTR_OAM
] = { .len
= 0 },
319 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = { .len
= OVS_ATTR_VARIABLE
},
320 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
] = { .len
= OVS_ATTR_NESTED
,
321 .next
= ovs_vxlan_ext_key_lens
},
324 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
325 static const struct ovs_len_tbl ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
326 [OVS_KEY_ATTR_ENCAP
] = { .len
= OVS_ATTR_NESTED
},
327 [OVS_KEY_ATTR_PRIORITY
] = { .len
= sizeof(u32
) },
328 [OVS_KEY_ATTR_IN_PORT
] = { .len
= sizeof(u32
) },
329 [OVS_KEY_ATTR_SKB_MARK
] = { .len
= sizeof(u32
) },
330 [OVS_KEY_ATTR_ETHERNET
] = { .len
= sizeof(struct ovs_key_ethernet
) },
331 [OVS_KEY_ATTR_VLAN
] = { .len
= sizeof(__be16
) },
332 [OVS_KEY_ATTR_ETHERTYPE
] = { .len
= sizeof(__be16
) },
333 [OVS_KEY_ATTR_IPV4
] = { .len
= sizeof(struct ovs_key_ipv4
) },
334 [OVS_KEY_ATTR_IPV6
] = { .len
= sizeof(struct ovs_key_ipv6
) },
335 [OVS_KEY_ATTR_TCP
] = { .len
= sizeof(struct ovs_key_tcp
) },
336 [OVS_KEY_ATTR_TCP_FLAGS
] = { .len
= sizeof(__be16
) },
337 [OVS_KEY_ATTR_UDP
] = { .len
= sizeof(struct ovs_key_udp
) },
338 [OVS_KEY_ATTR_SCTP
] = { .len
= sizeof(struct ovs_key_sctp
) },
339 [OVS_KEY_ATTR_ICMP
] = { .len
= sizeof(struct ovs_key_icmp
) },
340 [OVS_KEY_ATTR_ICMPV6
] = { .len
= sizeof(struct ovs_key_icmpv6
) },
341 [OVS_KEY_ATTR_ARP
] = { .len
= sizeof(struct ovs_key_arp
) },
342 [OVS_KEY_ATTR_ND
] = { .len
= sizeof(struct ovs_key_nd
) },
343 [OVS_KEY_ATTR_RECIRC_ID
] = { .len
= sizeof(u32
) },
344 [OVS_KEY_ATTR_DP_HASH
] = { .len
= sizeof(u32
) },
345 [OVS_KEY_ATTR_TUNNEL
] = { .len
= OVS_ATTR_NESTED
,
346 .next
= ovs_tunnel_key_lens
, },
347 [OVS_KEY_ATTR_MPLS
] = { .len
= sizeof(struct ovs_key_mpls
) },
350 static bool check_attr_len(unsigned int attr_len
, unsigned int expected_len
)
352 return expected_len
== attr_len
||
353 expected_len
== OVS_ATTR_NESTED
||
354 expected_len
== OVS_ATTR_VARIABLE
;
357 static bool is_all_zero(const u8
*fp
, size_t size
)
364 for (i
= 0; i
< size
; i
++)
371 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
372 const struct nlattr
*a
[],
373 u64
*attrsp
, bool log
, bool nz
)
375 const struct nlattr
*nla
;
380 nla_for_each_nested(nla
, attr
, rem
) {
381 u16 type
= nla_type(nla
);
384 if (type
> OVS_KEY_ATTR_MAX
) {
385 OVS_NLERR(log
, "Key type %d is out of range max %d",
386 type
, OVS_KEY_ATTR_MAX
);
390 if (attrs
& (1ULL << type
)) {
391 OVS_NLERR(log
, "Duplicate key (type %d).", type
);
395 expected_len
= ovs_key_lens
[type
].len
;
396 if (!check_attr_len(nla_len(nla
), expected_len
)) {
397 OVS_NLERR(log
, "Key %d has unexpected len %d expected %d",
398 type
, nla_len(nla
), expected_len
);
402 if (!nz
|| !is_all_zero(nla_data(nla
), expected_len
)) {
403 attrs
|= 1ULL << type
;
408 OVS_NLERR(log
, "Message has %d unknown bytes.", rem
);
416 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
417 const struct nlattr
*a
[], u64
*attrsp
,
420 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, true);
423 static int parse_flow_nlattrs(const struct nlattr
*attr
,
424 const struct nlattr
*a
[], u64
*attrsp
,
427 return __parse_flow_nlattrs(attr
, a
, attrsp
, log
, false);
430 static int genev_tun_opt_from_nlattr(const struct nlattr
*a
,
431 struct sw_flow_match
*match
, bool is_mask
,
434 unsigned long opt_key_offset
;
436 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
437 OVS_NLERR(log
, "Geneve option length err (len %d, max %zu).",
438 nla_len(a
), sizeof(match
->key
->tun_opts
));
442 if (nla_len(a
) % 4 != 0) {
443 OVS_NLERR(log
, "Geneve opt len %d is not a multiple of 4.",
448 /* We need to record the length of the options passed
449 * down, otherwise packets with the same format but
450 * additional options will be silently matched.
453 SW_FLOW_KEY_PUT(match
, tun_opts_len
, nla_len(a
),
456 /* This is somewhat unusual because it looks at
457 * both the key and mask while parsing the
458 * attributes (and by extension assumes the key
459 * is parsed first). Normally, we would verify
460 * that each is the correct length and that the
461 * attributes line up in the validate function.
462 * However, that is difficult because this is
463 * variable length and we won't have the
466 if (match
->key
->tun_opts_len
!= nla_len(a
)) {
467 OVS_NLERR(log
, "Geneve option len %d != mask len %d",
468 match
->key
->tun_opts_len
, nla_len(a
));
472 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
475 opt_key_offset
= TUN_METADATA_OFFSET(nla_len(a
));
476 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, nla_data(a
),
477 nla_len(a
), is_mask
);
481 static int vxlan_tun_opt_from_nlattr(const struct nlattr
*attr
,
482 struct sw_flow_match
*match
, bool is_mask
,
487 unsigned long opt_key_offset
;
488 struct ovs_vxlan_opts opts
;
490 BUILD_BUG_ON(sizeof(opts
) > sizeof(match
->key
->tun_opts
));
492 memset(&opts
, 0, sizeof(opts
));
493 nla_for_each_nested(a
, attr
, rem
) {
494 int type
= nla_type(a
);
496 if (type
> OVS_VXLAN_EXT_MAX
) {
497 OVS_NLERR(log
, "VXLAN extension %d out of range max %d",
498 type
, OVS_VXLAN_EXT_MAX
);
502 if (!check_attr_len(nla_len(a
),
503 ovs_vxlan_ext_key_lens
[type
].len
)) {
504 OVS_NLERR(log
, "VXLAN extension %d has unexpected len %d expected %d",
506 ovs_vxlan_ext_key_lens
[type
].len
);
511 case OVS_VXLAN_EXT_GBP
:
512 opts
.gbp
= nla_get_u32(a
);
515 OVS_NLERR(log
, "Unknown VXLAN extension attribute %d",
521 OVS_NLERR(log
, "VXLAN extension message has %d unknown bytes.",
527 SW_FLOW_KEY_PUT(match
, tun_opts_len
, sizeof(opts
), false);
529 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff, true);
531 opt_key_offset
= TUN_METADATA_OFFSET(sizeof(opts
));
532 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
, &opts
, sizeof(opts
),
537 static int ipv4_tun_from_nlattr(const struct nlattr
*attr
,
538 struct sw_flow_match
*match
, bool is_mask
,
544 __be16 tun_flags
= 0;
547 nla_for_each_nested(a
, attr
, rem
) {
548 int type
= nla_type(a
);
551 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
552 OVS_NLERR(log
, "Tunnel attr %d out of range max %d",
553 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
557 if (!check_attr_len(nla_len(a
),
558 ovs_tunnel_key_lens
[type
].len
)) {
559 OVS_NLERR(log
, "Tunnel attr %d has unexpected len %d expected %d",
560 type
, nla_len(a
), ovs_tunnel_key_lens
[type
].len
);
565 case OVS_TUNNEL_KEY_ATTR_ID
:
566 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
567 nla_get_be64(a
), is_mask
);
568 tun_flags
|= TUNNEL_KEY
;
570 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
571 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_src
,
572 nla_get_in_addr(a
), is_mask
);
574 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
575 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_dst
,
576 nla_get_in_addr(a
), is_mask
);
578 case OVS_TUNNEL_KEY_ATTR_TOS
:
579 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_tos
,
580 nla_get_u8(a
), is_mask
);
582 case OVS_TUNNEL_KEY_ATTR_TTL
:
583 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_ttl
,
584 nla_get_u8(a
), is_mask
);
587 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
588 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
590 case OVS_TUNNEL_KEY_ATTR_CSUM
:
591 tun_flags
|= TUNNEL_CSUM
;
593 case OVS_TUNNEL_KEY_ATTR_TP_SRC
:
594 SW_FLOW_KEY_PUT(match
, tun_key
.tp_src
,
595 nla_get_be16(a
), is_mask
);
597 case OVS_TUNNEL_KEY_ATTR_TP_DST
:
598 SW_FLOW_KEY_PUT(match
, tun_key
.tp_dst
,
599 nla_get_be16(a
), is_mask
);
601 case OVS_TUNNEL_KEY_ATTR_OAM
:
602 tun_flags
|= TUNNEL_OAM
;
604 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
606 OVS_NLERR(log
, "Multiple metadata blocks provided");
610 err
= genev_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
614 tun_flags
|= TUNNEL_GENEVE_OPT
;
617 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
619 OVS_NLERR(log
, "Multiple metadata blocks provided");
623 err
= vxlan_tun_opt_from_nlattr(a
, match
, is_mask
, log
);
627 tun_flags
|= TUNNEL_VXLAN_OPT
;
631 OVS_NLERR(log
, "Unknown IPv4 tunnel attribute %d",
637 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
640 OVS_NLERR(log
, "IPv4 tunnel attribute has %d unknown bytes.",
646 if (!match
->key
->tun_key
.ipv4_dst
) {
647 OVS_NLERR(log
, "IPv4 tunnel dst address is zero");
652 OVS_NLERR(log
, "IPv4 tunnel TTL not specified.");
660 static int vxlan_opt_to_nlattr(struct sk_buff
*skb
,
661 const void *tun_opts
, int swkey_tun_opts_len
)
663 const struct ovs_vxlan_opts
*opts
= tun_opts
;
666 nla
= nla_nest_start(skb
, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
);
670 if (nla_put_u32(skb
, OVS_VXLAN_EXT_GBP
, opts
->gbp
) < 0)
673 nla_nest_end(skb
, nla
);
677 static int __ipv4_tun_to_nlattr(struct sk_buff
*skb
,
678 const struct ovs_key_ipv4_tunnel
*output
,
679 const void *tun_opts
, int swkey_tun_opts_len
)
681 if (output
->tun_flags
& TUNNEL_KEY
&&
682 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
))
684 if (output
->ipv4_src
&&
685 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, output
->ipv4_src
))
687 if (output
->ipv4_dst
&&
688 nla_put_in_addr(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, output
->ipv4_dst
))
690 if (output
->ipv4_tos
&&
691 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->ipv4_tos
))
693 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ipv4_ttl
))
695 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
696 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
698 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
699 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
701 if (output
->tp_src
&&
702 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_SRC
, output
->tp_src
))
704 if (output
->tp_dst
&&
705 nla_put_be16(skb
, OVS_TUNNEL_KEY_ATTR_TP_DST
, output
->tp_dst
))
707 if ((output
->tun_flags
& TUNNEL_OAM
) &&
708 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_OAM
))
711 if (output
->tun_flags
& TUNNEL_GENEVE_OPT
&&
712 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
713 swkey_tun_opts_len
, tun_opts
))
715 else if (output
->tun_flags
& TUNNEL_VXLAN_OPT
&&
716 vxlan_opt_to_nlattr(skb
, tun_opts
, swkey_tun_opts_len
))
723 static int ipv4_tun_to_nlattr(struct sk_buff
*skb
,
724 const struct ovs_key_ipv4_tunnel
*output
,
725 const void *tun_opts
, int swkey_tun_opts_len
)
730 nla
= nla_nest_start(skb
, OVS_KEY_ATTR_TUNNEL
);
734 err
= __ipv4_tun_to_nlattr(skb
, output
, tun_opts
, swkey_tun_opts_len
);
738 nla_nest_end(skb
, nla
);
742 int ovs_nla_put_egress_tunnel_key(struct sk_buff
*skb
,
743 const struct ovs_tunnel_info
*egress_tun_info
)
745 return __ipv4_tun_to_nlattr(skb
, &egress_tun_info
->tunnel
,
746 egress_tun_info
->options
,
747 egress_tun_info
->options_len
);
750 static int metadata_from_nlattrs(struct sw_flow_match
*match
, u64
*attrs
,
751 const struct nlattr
**a
, bool is_mask
,
754 if (*attrs
& (1ULL << OVS_KEY_ATTR_DP_HASH
)) {
755 u32 hash_val
= nla_get_u32(a
[OVS_KEY_ATTR_DP_HASH
]);
757 SW_FLOW_KEY_PUT(match
, ovs_flow_hash
, hash_val
, is_mask
);
758 *attrs
&= ~(1ULL << OVS_KEY_ATTR_DP_HASH
);
761 if (*attrs
& (1ULL << OVS_KEY_ATTR_RECIRC_ID
)) {
762 u32 recirc_id
= nla_get_u32(a
[OVS_KEY_ATTR_RECIRC_ID
]);
764 SW_FLOW_KEY_PUT(match
, recirc_id
, recirc_id
, is_mask
);
765 *attrs
&= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID
);
768 if (*attrs
& (1ULL << OVS_KEY_ATTR_PRIORITY
)) {
769 SW_FLOW_KEY_PUT(match
, phy
.priority
,
770 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
771 *attrs
&= ~(1ULL << OVS_KEY_ATTR_PRIORITY
);
774 if (*attrs
& (1ULL << OVS_KEY_ATTR_IN_PORT
)) {
775 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
778 in_port
= 0xffffffff; /* Always exact match in_port. */
779 } else if (in_port
>= DP_MAX_PORTS
) {
780 OVS_NLERR(log
, "Port %d exceeds max allowable %d",
781 in_port
, DP_MAX_PORTS
);
785 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
786 *attrs
&= ~(1ULL << OVS_KEY_ATTR_IN_PORT
);
787 } else if (!is_mask
) {
788 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
791 if (*attrs
& (1ULL << OVS_KEY_ATTR_SKB_MARK
)) {
792 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
794 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
795 *attrs
&= ~(1ULL << OVS_KEY_ATTR_SKB_MARK
);
797 if (*attrs
& (1ULL << OVS_KEY_ATTR_TUNNEL
)) {
798 if (ipv4_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
801 *attrs
&= ~(1ULL << OVS_KEY_ATTR_TUNNEL
);
806 static int ovs_key_from_nlattrs(struct sw_flow_match
*match
, u64 attrs
,
807 const struct nlattr
**a
, bool is_mask
,
812 err
= metadata_from_nlattrs(match
, &attrs
, a
, is_mask
, log
);
816 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) {
817 const struct ovs_key_ethernet
*eth_key
;
819 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
820 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
821 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
822 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
823 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
824 attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERNET
);
827 if (attrs
& (1ULL << OVS_KEY_ATTR_VLAN
)) {
830 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
831 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
833 OVS_NLERR(log
, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
835 OVS_NLERR(log
, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
840 SW_FLOW_KEY_PUT(match
, eth
.tci
, tci
, is_mask
);
841 attrs
&= ~(1ULL << OVS_KEY_ATTR_VLAN
);
844 if (attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)) {
847 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
849 /* Always exact match EtherType. */
850 eth_type
= htons(0xffff);
851 } else if (!eth_proto_is_802_3(eth_type
)) {
852 OVS_NLERR(log
, "EtherType %x is less than min %x",
853 ntohs(eth_type
), ETH_P_802_3_MIN
);
857 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
858 attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE
);
859 } else if (!is_mask
) {
860 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
863 if (attrs
& (1ULL << OVS_KEY_ATTR_IPV4
)) {
864 const struct ovs_key_ipv4
*ipv4_key
;
866 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
867 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
868 OVS_NLERR(log
, "IPv4 frag type %d is out of range max %d",
869 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
872 SW_FLOW_KEY_PUT(match
, ip
.proto
,
873 ipv4_key
->ipv4_proto
, is_mask
);
874 SW_FLOW_KEY_PUT(match
, ip
.tos
,
875 ipv4_key
->ipv4_tos
, is_mask
);
876 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
877 ipv4_key
->ipv4_ttl
, is_mask
);
878 SW_FLOW_KEY_PUT(match
, ip
.frag
,
879 ipv4_key
->ipv4_frag
, is_mask
);
880 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
881 ipv4_key
->ipv4_src
, is_mask
);
882 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
883 ipv4_key
->ipv4_dst
, is_mask
);
884 attrs
&= ~(1ULL << OVS_KEY_ATTR_IPV4
);
887 if (attrs
& (1ULL << OVS_KEY_ATTR_IPV6
)) {
888 const struct ovs_key_ipv6
*ipv6_key
;
890 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
891 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
892 OVS_NLERR(log
, "IPv6 frag type %d is out of range max %d",
893 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
897 if (!is_mask
&& ipv6_key
->ipv6_label
& htonl(0xFFF00000)) {
899 "Invalid IPv6 flow label value (value=%x, max=%x).",
900 ntohl(ipv6_key
->ipv6_label
), (1 << 20) - 1);
904 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
905 ipv6_key
->ipv6_label
, is_mask
);
906 SW_FLOW_KEY_PUT(match
, ip
.proto
,
907 ipv6_key
->ipv6_proto
, is_mask
);
908 SW_FLOW_KEY_PUT(match
, ip
.tos
,
909 ipv6_key
->ipv6_tclass
, is_mask
);
910 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
911 ipv6_key
->ipv6_hlimit
, is_mask
);
912 SW_FLOW_KEY_PUT(match
, ip
.frag
,
913 ipv6_key
->ipv6_frag
, is_mask
);
914 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
916 sizeof(match
->key
->ipv6
.addr
.src
),
918 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
920 sizeof(match
->key
->ipv6
.addr
.dst
),
923 attrs
&= ~(1ULL << OVS_KEY_ATTR_IPV6
);
926 if (attrs
& (1ULL << OVS_KEY_ATTR_ARP
)) {
927 const struct ovs_key_arp
*arp_key
;
929 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
930 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
931 OVS_NLERR(log
, "Unknown ARP opcode (opcode=%d).",
936 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
937 arp_key
->arp_sip
, is_mask
);
938 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
939 arp_key
->arp_tip
, is_mask
);
940 SW_FLOW_KEY_PUT(match
, ip
.proto
,
941 ntohs(arp_key
->arp_op
), is_mask
);
942 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
943 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
944 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
945 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
947 attrs
&= ~(1ULL << OVS_KEY_ATTR_ARP
);
950 if (attrs
& (1ULL << OVS_KEY_ATTR_MPLS
)) {
951 const struct ovs_key_mpls
*mpls_key
;
953 mpls_key
= nla_data(a
[OVS_KEY_ATTR_MPLS
]);
954 SW_FLOW_KEY_PUT(match
, mpls
.top_lse
,
955 mpls_key
->mpls_lse
, is_mask
);
957 attrs
&= ~(1ULL << OVS_KEY_ATTR_MPLS
);
960 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP
)) {
961 const struct ovs_key_tcp
*tcp_key
;
963 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
964 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
965 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
966 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP
);
969 if (attrs
& (1ULL << OVS_KEY_ATTR_TCP_FLAGS
)) {
970 SW_FLOW_KEY_PUT(match
, tp
.flags
,
971 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
973 attrs
&= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS
);
976 if (attrs
& (1ULL << OVS_KEY_ATTR_UDP
)) {
977 const struct ovs_key_udp
*udp_key
;
979 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
980 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
981 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
982 attrs
&= ~(1ULL << OVS_KEY_ATTR_UDP
);
985 if (attrs
& (1ULL << OVS_KEY_ATTR_SCTP
)) {
986 const struct ovs_key_sctp
*sctp_key
;
988 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
989 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
990 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
991 attrs
&= ~(1ULL << OVS_KEY_ATTR_SCTP
);
994 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMP
)) {
995 const struct ovs_key_icmp
*icmp_key
;
997 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
998 SW_FLOW_KEY_PUT(match
, tp
.src
,
999 htons(icmp_key
->icmp_type
), is_mask
);
1000 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1001 htons(icmp_key
->icmp_code
), is_mask
);
1002 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMP
);
1005 if (attrs
& (1ULL << OVS_KEY_ATTR_ICMPV6
)) {
1006 const struct ovs_key_icmpv6
*icmpv6_key
;
1008 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
1009 SW_FLOW_KEY_PUT(match
, tp
.src
,
1010 htons(icmpv6_key
->icmpv6_type
), is_mask
);
1011 SW_FLOW_KEY_PUT(match
, tp
.dst
,
1012 htons(icmpv6_key
->icmpv6_code
), is_mask
);
1013 attrs
&= ~(1ULL << OVS_KEY_ATTR_ICMPV6
);
1016 if (attrs
& (1ULL << OVS_KEY_ATTR_ND
)) {
1017 const struct ovs_key_nd
*nd_key
;
1019 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
1020 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
1022 sizeof(match
->key
->ipv6
.nd
.target
),
1024 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
1025 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
1026 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
1027 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
1028 attrs
&= ~(1ULL << OVS_KEY_ATTR_ND
);
1032 OVS_NLERR(log
, "Unknown key attributes %llx",
1033 (unsigned long long)attrs
);
1040 static void nlattr_set(struct nlattr
*attr
, u8 val
,
1041 const struct ovs_len_tbl
*tbl
)
1046 /* The nlattr stream should already have been validated */
1047 nla_for_each_nested(nla
, attr
, rem
) {
1048 if (tbl
[nla_type(nla
)].len
== OVS_ATTR_NESTED
) {
1049 if (tbl
[nla_type(nla
)].next
)
1050 tbl
= tbl
[nla_type(nla
)].next
;
1051 nlattr_set(nla
, val
, tbl
);
1053 memset(nla_data(nla
), val
, nla_len(nla
));
1058 static void mask_set_nlattr(struct nlattr
*attr
, u8 val
)
1060 nlattr_set(attr
, val
, ovs_key_lens
);
1064 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1065 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1066 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1067 * does not include any don't care bit.
1068 * @match: receives the extracted flow match information.
1069 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1070 * sequence. The fields should of the packet that triggered the creation
1072 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1073 * attribute specifies the mask field of the wildcarded flow.
1074 * @log: Boolean to allow kernel error logging. Normally true, but when
1075 * probing for feature compatibility this should be passed in as false to
1076 * suppress unnecessary error logging.
1078 int ovs_nla_get_match(struct sw_flow_match
*match
,
1079 const struct nlattr
*nla_key
,
1080 const struct nlattr
*nla_mask
,
1083 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1084 const struct nlattr
*encap
;
1085 struct nlattr
*newmask
= NULL
;
1088 bool encap_valid
= false;
1091 err
= parse_flow_nlattrs(nla_key
, a
, &key_attrs
, log
);
1095 if ((key_attrs
& (1ULL << OVS_KEY_ATTR_ETHERNET
)) &&
1096 (key_attrs
& (1ULL << OVS_KEY_ATTR_ETHERTYPE
)) &&
1097 (nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]) == htons(ETH_P_8021Q
))) {
1100 if (!((key_attrs
& (1ULL << OVS_KEY_ATTR_VLAN
)) &&
1101 (key_attrs
& (1ULL << OVS_KEY_ATTR_ENCAP
)))) {
1102 OVS_NLERR(log
, "Invalid Vlan frame.");
1106 key_attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE
);
1107 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1108 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1109 key_attrs
&= ~(1ULL << OVS_KEY_ATTR_ENCAP
);
1112 if (tci
& htons(VLAN_TAG_PRESENT
)) {
1113 err
= parse_flow_nlattrs(encap
, a
, &key_attrs
, log
);
1117 /* Corner case for truncated 802.1Q header. */
1118 if (nla_len(encap
)) {
1119 OVS_NLERR(log
, "Truncated 802.1Q header has non-zero encap attribute.");
1123 OVS_NLERR(log
, "Encap attr is set for non-VLAN frame");
1128 err
= ovs_key_from_nlattrs(match
, key_attrs
, a
, false, log
);
1134 /* Create an exact match mask. We need to set to 0xff
1135 * all the 'match->mask' fields that have been touched
1136 * in 'match->key'. We cannot simply memset
1137 * 'match->mask', because padding bytes and fields not
1138 * specified in 'match->key' should be left to 0.
1139 * Instead, we use a stream of netlink attributes,
1140 * copied from 'key' and set to 0xff.
1141 * ovs_key_from_nlattrs() will take care of filling
1142 * 'match->mask' appropriately.
1144 newmask
= kmemdup(nla_key
,
1145 nla_total_size(nla_len(nla_key
)),
1150 mask_set_nlattr(newmask
, 0xff);
1152 /* The userspace does not send tunnel attributes that
1153 * are 0, but we should not wildcard them nonetheless.
1155 if (match
->key
->tun_key
.ipv4_dst
)
1156 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_key
,
1162 err
= parse_flow_mask_nlattrs(nla_mask
, a
, &mask_attrs
, log
);
1166 /* Always match on tci. */
1167 SW_FLOW_KEY_PUT(match
, eth
.tci
, htons(0xffff), true);
1169 if (mask_attrs
& 1ULL << OVS_KEY_ATTR_ENCAP
) {
1170 __be16 eth_type
= 0;
1174 OVS_NLERR(log
, "Encap mask attribute is set for non-VLAN frame.");
1179 mask_attrs
&= ~(1ULL << OVS_KEY_ATTR_ENCAP
);
1180 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
1181 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
1183 if (eth_type
== htons(0xffff)) {
1184 mask_attrs
&= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE
);
1185 encap
= a
[OVS_KEY_ATTR_ENCAP
];
1186 err
= parse_flow_mask_nlattrs(encap
, a
,
1191 OVS_NLERR(log
, "VLAN frames must have an exact match on the TPID (mask=%x).",
1197 if (a
[OVS_KEY_ATTR_VLAN
])
1198 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
1200 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
1201 OVS_NLERR(log
, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1208 err
= ovs_key_from_nlattrs(match
, mask_attrs
, a
, true, log
);
1213 if (!match_validate(match
, key_attrs
, mask_attrs
, log
))
1221 static size_t get_ufid_len(const struct nlattr
*attr
, bool log
)
1228 len
= nla_len(attr
);
1229 if (len
< 1 || len
> MAX_UFID_LENGTH
) {
1230 OVS_NLERR(log
, "ufid size %u bytes exceeds the range (1, %d)",
1231 nla_len(attr
), MAX_UFID_LENGTH
);
1238 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1239 * or false otherwise.
1241 bool ovs_nla_get_ufid(struct sw_flow_id
*sfid
, const struct nlattr
*attr
,
1244 sfid
->ufid_len
= get_ufid_len(attr
, log
);
1246 memcpy(sfid
->ufid
, nla_data(attr
), sfid
->ufid_len
);
1248 return sfid
->ufid_len
;
1251 int ovs_nla_get_identifier(struct sw_flow_id
*sfid
, const struct nlattr
*ufid
,
1252 const struct sw_flow_key
*key
, bool log
)
1254 struct sw_flow_key
*new_key
;
1256 if (ovs_nla_get_ufid(sfid
, ufid
, log
))
1259 /* If UFID was not provided, use unmasked key. */
1260 new_key
= kmalloc(sizeof(*new_key
), GFP_KERNEL
);
1263 memcpy(new_key
, key
, sizeof(*key
));
1264 sfid
->unmasked_key
= new_key
;
1269 u32
ovs_nla_get_ufid_flags(const struct nlattr
*attr
)
1271 return attr
? nla_get_u32(attr
) : 0;
1275 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1276 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
1277 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1279 * @log: Boolean to allow kernel error logging. Normally true, but when
1280 * probing for feature compatibility this should be passed in as false to
1281 * suppress unnecessary error logging.
1283 * This parses a series of Netlink attributes that form a flow key, which must
1284 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1285 * get the metadata, that is, the parts of the flow key that cannot be
1286 * extracted from the packet itself.
1289 int ovs_nla_get_flow_metadata(const struct nlattr
*attr
,
1290 struct sw_flow_key
*key
,
1293 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1294 struct sw_flow_match match
;
1298 err
= parse_flow_nlattrs(attr
, a
, &attrs
, log
);
1302 memset(&match
, 0, sizeof(match
));
1305 memset(key
, 0, OVS_SW_FLOW_KEY_METADATA_SIZE
);
1306 key
->phy
.in_port
= DP_MAX_PORTS
;
1308 return metadata_from_nlattrs(&match
, &attrs
, a
, false, log
);
1311 static int __ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1312 const struct sw_flow_key
*output
, bool is_mask
,
1313 struct sk_buff
*skb
)
1315 struct ovs_key_ethernet
*eth_key
;
1316 struct nlattr
*nla
, *encap
;
1318 if (nla_put_u32(skb
, OVS_KEY_ATTR_RECIRC_ID
, output
->recirc_id
))
1319 goto nla_put_failure
;
1321 if (nla_put_u32(skb
, OVS_KEY_ATTR_DP_HASH
, output
->ovs_flow_hash
))
1322 goto nla_put_failure
;
1324 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
1325 goto nla_put_failure
;
1327 if ((swkey
->tun_key
.ipv4_dst
|| is_mask
)) {
1328 const void *opts
= NULL
;
1330 if (output
->tun_key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
)
1331 opts
= TUN_METADATA_OPTS(output
, swkey
->tun_opts_len
);
1333 if (ipv4_tun_to_nlattr(skb
, &output
->tun_key
, opts
,
1334 swkey
->tun_opts_len
))
1335 goto nla_put_failure
;
1338 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
1339 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
1340 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
1341 goto nla_put_failure
;
1344 upper_u16
= !is_mask
? 0 : 0xffff;
1346 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
1347 (upper_u16
<< 16) | output
->phy
.in_port
))
1348 goto nla_put_failure
;
1351 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
1352 goto nla_put_failure
;
1354 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
1356 goto nla_put_failure
;
1358 eth_key
= nla_data(nla
);
1359 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
1360 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
1362 if (swkey
->eth
.tci
|| swkey
->eth
.type
== htons(ETH_P_8021Q
)) {
1364 eth_type
= !is_mask
? htons(ETH_P_8021Q
) : htons(0xffff);
1365 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
1366 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, output
->eth
.tci
))
1367 goto nla_put_failure
;
1368 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1369 if (!swkey
->eth
.tci
)
1374 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
1376 * Ethertype 802.2 is represented in the netlink with omitted
1377 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1378 * 0xffff in the mask attribute. Ethertype can also
1381 if (is_mask
&& output
->eth
.type
)
1382 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
1384 goto nla_put_failure
;
1388 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
1389 goto nla_put_failure
;
1391 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1392 struct ovs_key_ipv4
*ipv4_key
;
1394 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
1396 goto nla_put_failure
;
1397 ipv4_key
= nla_data(nla
);
1398 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
1399 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
1400 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
1401 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
1402 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
1403 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
1404 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1405 struct ovs_key_ipv6
*ipv6_key
;
1407 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
1409 goto nla_put_failure
;
1410 ipv6_key
= nla_data(nla
);
1411 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
1412 sizeof(ipv6_key
->ipv6_src
));
1413 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
1414 sizeof(ipv6_key
->ipv6_dst
));
1415 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
1416 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
1417 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
1418 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
1419 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
1420 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1421 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1422 struct ovs_key_arp
*arp_key
;
1424 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
1426 goto nla_put_failure
;
1427 arp_key
= nla_data(nla
);
1428 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
1429 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
1430 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
1431 arp_key
->arp_op
= htons(output
->ip
.proto
);
1432 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
1433 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
1434 } else if (eth_p_mpls(swkey
->eth
.type
)) {
1435 struct ovs_key_mpls
*mpls_key
;
1437 nla
= nla_reserve(skb
, OVS_KEY_ATTR_MPLS
, sizeof(*mpls_key
));
1439 goto nla_put_failure
;
1440 mpls_key
= nla_data(nla
);
1441 mpls_key
->mpls_lse
= output
->mpls
.top_lse
;
1444 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
1445 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
1446 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1448 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
1449 struct ovs_key_tcp
*tcp_key
;
1451 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
1453 goto nla_put_failure
;
1454 tcp_key
= nla_data(nla
);
1455 tcp_key
->tcp_src
= output
->tp
.src
;
1456 tcp_key
->tcp_dst
= output
->tp
.dst
;
1457 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
1459 goto nla_put_failure
;
1460 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1461 struct ovs_key_udp
*udp_key
;
1463 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1465 goto nla_put_failure
;
1466 udp_key
= nla_data(nla
);
1467 udp_key
->udp_src
= output
->tp
.src
;
1468 udp_key
->udp_dst
= output
->tp
.dst
;
1469 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
1470 struct ovs_key_sctp
*sctp_key
;
1472 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
1474 goto nla_put_failure
;
1475 sctp_key
= nla_data(nla
);
1476 sctp_key
->sctp_src
= output
->tp
.src
;
1477 sctp_key
->sctp_dst
= output
->tp
.dst
;
1478 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1479 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1480 struct ovs_key_icmp
*icmp_key
;
1482 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1484 goto nla_put_failure
;
1485 icmp_key
= nla_data(nla
);
1486 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
1487 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
1488 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1489 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1490 struct ovs_key_icmpv6
*icmpv6_key
;
1492 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1493 sizeof(*icmpv6_key
));
1495 goto nla_put_failure
;
1496 icmpv6_key
= nla_data(nla
);
1497 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
1498 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
1500 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1501 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1502 struct ovs_key_nd
*nd_key
;
1504 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1506 goto nla_put_failure
;
1507 nd_key
= nla_data(nla
);
1508 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
1509 sizeof(nd_key
->nd_target
));
1510 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
1511 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
1518 nla_nest_end(skb
, encap
);
1526 int ovs_nla_put_key(const struct sw_flow_key
*swkey
,
1527 const struct sw_flow_key
*output
, int attr
, bool is_mask
,
1528 struct sk_buff
*skb
)
1533 nla
= nla_nest_start(skb
, attr
);
1536 err
= __ovs_nla_put_key(swkey
, output
, is_mask
, skb
);
1539 nla_nest_end(skb
, nla
);
1544 /* Called with ovs_mutex or RCU read lock. */
1545 int ovs_nla_put_identifier(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1547 if (ovs_identifier_is_ufid(&flow
->id
))
1548 return nla_put(skb
, OVS_FLOW_ATTR_UFID
, flow
->id
.ufid_len
,
1551 return ovs_nla_put_key(flow
->id
.unmasked_key
, flow
->id
.unmasked_key
,
1552 OVS_FLOW_ATTR_KEY
, false, skb
);
1555 /* Called with ovs_mutex or RCU read lock. */
1556 int ovs_nla_put_masked_key(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1558 return ovs_nla_put_key(&flow
->key
, &flow
->key
,
1559 OVS_FLOW_ATTR_KEY
, false, skb
);
1562 /* Called with ovs_mutex or RCU read lock. */
1563 int ovs_nla_put_mask(const struct sw_flow
*flow
, struct sk_buff
*skb
)
1565 return ovs_nla_put_key(&flow
->key
, &flow
->mask
->key
,
1566 OVS_FLOW_ATTR_MASK
, true, skb
);
1569 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1571 static struct sw_flow_actions
*nla_alloc_flow_actions(int size
, bool log
)
1573 struct sw_flow_actions
*sfa
;
1575 if (size
> MAX_ACTIONS_BUFSIZE
) {
1576 OVS_NLERR(log
, "Flow action size %u bytes exceeds max", size
);
1577 return ERR_PTR(-EINVAL
);
1580 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
1582 return ERR_PTR(-ENOMEM
);
1584 sfa
->actions_len
= 0;
1588 /* RCU callback used by ovs_nla_free_flow_actions. */
1589 static void rcu_free_acts_callback(struct rcu_head
*rcu
)
1591 struct sw_flow_actions
*sf_acts
= container_of(rcu
,
1592 struct sw_flow_actions
, rcu
);
1596 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1597 * The caller must hold rcu_read_lock for this to be sensible.
1599 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
1601 call_rcu(&sf_acts
->rcu
, rcu_free_acts_callback
);
1604 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
1605 int attr_len
, bool log
)
1608 struct sw_flow_actions
*acts
;
1610 int req_size
= NLA_ALIGN(attr_len
);
1611 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
1612 (*sfa
)->actions_len
;
1614 if (req_size
<= (ksize(*sfa
) - next_offset
))
1617 new_acts_size
= ksize(*sfa
) * 2;
1619 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
1620 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
)
1621 return ERR_PTR(-EMSGSIZE
);
1622 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
1625 acts
= nla_alloc_flow_actions(new_acts_size
, log
);
1627 return (void *)acts
;
1629 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
1630 acts
->actions_len
= (*sfa
)->actions_len
;
1635 (*sfa
)->actions_len
+= req_size
;
1636 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
1639 static struct nlattr
*__add_action(struct sw_flow_actions
**sfa
,
1640 int attrtype
, void *data
, int len
, bool log
)
1644 a
= reserve_sfa_size(sfa
, nla_attr_size(len
), log
);
1648 a
->nla_type
= attrtype
;
1649 a
->nla_len
= nla_attr_size(len
);
1652 memcpy(nla_data(a
), data
, len
);
1653 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
1658 static int add_action(struct sw_flow_actions
**sfa
, int attrtype
,
1659 void *data
, int len
, bool log
)
1663 a
= __add_action(sfa
, attrtype
, data
, len
, log
);
1670 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
1671 int attrtype
, bool log
)
1673 int used
= (*sfa
)->actions_len
;
1676 err
= add_action(sfa
, attrtype
, NULL
, 0, log
);
1683 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
1686 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
1689 a
->nla_len
= sfa
->actions_len
- st_offset
;
1692 static int __ovs_nla_copy_actions(const struct nlattr
*attr
,
1693 const struct sw_flow_key
*key
,
1694 int depth
, struct sw_flow_actions
**sfa
,
1695 __be16 eth_type
, __be16 vlan_tci
, bool log
);
1697 static int validate_and_copy_sample(const struct nlattr
*attr
,
1698 const struct sw_flow_key
*key
, int depth
,
1699 struct sw_flow_actions
**sfa
,
1700 __be16 eth_type
, __be16 vlan_tci
, bool log
)
1702 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
1703 const struct nlattr
*probability
, *actions
;
1704 const struct nlattr
*a
;
1705 int rem
, start
, err
, st_acts
;
1707 memset(attrs
, 0, sizeof(attrs
));
1708 nla_for_each_nested(a
, attr
, rem
) {
1709 int type
= nla_type(a
);
1710 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
1717 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
1718 if (!probability
|| nla_len(probability
) != sizeof(u32
))
1721 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
1722 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
1725 /* validation done, copy sample action. */
1726 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
, log
);
1729 err
= add_action(sfa
, OVS_SAMPLE_ATTR_PROBABILITY
,
1730 nla_data(probability
), sizeof(u32
), log
);
1733 st_acts
= add_nested_action_start(sfa
, OVS_SAMPLE_ATTR_ACTIONS
, log
);
1737 err
= __ovs_nla_copy_actions(actions
, key
, depth
+ 1, sfa
,
1738 eth_type
, vlan_tci
, log
);
1742 add_nested_action_end(*sfa
, st_acts
);
1743 add_nested_action_end(*sfa
, start
);
1748 void ovs_match_init(struct sw_flow_match
*match
,
1749 struct sw_flow_key
*key
,
1750 struct sw_flow_mask
*mask
)
1752 memset(match
, 0, sizeof(*match
));
1756 memset(key
, 0, sizeof(*key
));
1759 memset(&mask
->key
, 0, sizeof(mask
->key
));
1760 mask
->range
.start
= mask
->range
.end
= 0;
1764 static int validate_geneve_opts(struct sw_flow_key
*key
)
1766 struct geneve_opt
*option
;
1767 int opts_len
= key
->tun_opts_len
;
1768 bool crit_opt
= false;
1770 option
= (struct geneve_opt
*)TUN_METADATA_OPTS(key
, key
->tun_opts_len
);
1771 while (opts_len
> 0) {
1774 if (opts_len
< sizeof(*option
))
1777 len
= sizeof(*option
) + option
->length
* 4;
1781 crit_opt
|= !!(option
->type
& GENEVE_CRIT_OPT_TYPE
);
1783 option
= (struct geneve_opt
*)((u8
*)option
+ len
);
1787 key
->tun_key
.tun_flags
|= crit_opt
? TUNNEL_CRIT_OPT
: 0;
1792 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
1793 struct sw_flow_actions
**sfa
, bool log
)
1795 struct sw_flow_match match
;
1796 struct sw_flow_key key
;
1797 struct ovs_tunnel_info
*tun_info
;
1799 int start
, opts_type
;
1802 ovs_match_init(&match
, &key
, NULL
);
1803 opts_type
= ipv4_tun_from_nlattr(nla_data(attr
), &match
, false, log
);
1807 if (key
.tun_opts_len
) {
1808 switch (opts_type
) {
1809 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
1810 err
= validate_geneve_opts(&key
);
1814 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS
:
1819 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
, log
);
1823 a
= __add_action(sfa
, OVS_KEY_ATTR_TUNNEL_INFO
, NULL
,
1824 sizeof(*tun_info
) + key
.tun_opts_len
, log
);
1828 tun_info
= nla_data(a
);
1829 tun_info
->tunnel
= key
.tun_key
;
1830 tun_info
->options_len
= key
.tun_opts_len
;
1832 if (tun_info
->options_len
) {
1833 /* We need to store the options in the action itself since
1834 * everything else will go away after flow setup. We can append
1835 * it to tun_info and then point there.
1837 memcpy((tun_info
+ 1),
1838 TUN_METADATA_OPTS(&key
, key
.tun_opts_len
), key
.tun_opts_len
);
1839 tun_info
->options
= (tun_info
+ 1);
1841 tun_info
->options
= NULL
;
1844 add_nested_action_end(*sfa
, start
);
1849 /* Return false if there are any non-masked bits set.
1850 * Mask follows data immediately, before any netlink padding.
1852 static bool validate_masked(u8
*data
, int len
)
1854 u8
*mask
= data
+ len
;
1857 if (*data
++ & ~*mask
++)
1863 static int validate_set(const struct nlattr
*a
,
1864 const struct sw_flow_key
*flow_key
,
1865 struct sw_flow_actions
**sfa
,
1866 bool *skip_copy
, __be16 eth_type
, bool masked
, bool log
)
1868 const struct nlattr
*ovs_key
= nla_data(a
);
1869 int key_type
= nla_type(ovs_key
);
1872 /* There can be only one key in a action */
1873 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
1876 key_len
= nla_len(ovs_key
);
1880 if (key_type
> OVS_KEY_ATTR_MAX
||
1881 !check_attr_len(key_len
, ovs_key_lens
[key_type
].len
))
1884 if (masked
&& !validate_masked(nla_data(ovs_key
), key_len
))
1888 const struct ovs_key_ipv4
*ipv4_key
;
1889 const struct ovs_key_ipv6
*ipv6_key
;
1892 case OVS_KEY_ATTR_PRIORITY
:
1893 case OVS_KEY_ATTR_SKB_MARK
:
1894 case OVS_KEY_ATTR_ETHERNET
:
1897 case OVS_KEY_ATTR_TUNNEL
:
1898 if (eth_p_mpls(eth_type
))
1902 return -EINVAL
; /* Masked tunnel set not supported. */
1905 err
= validate_and_copy_set_tun(a
, sfa
, log
);
1910 case OVS_KEY_ATTR_IPV4
:
1911 if (eth_type
!= htons(ETH_P_IP
))
1914 ipv4_key
= nla_data(ovs_key
);
1917 const struct ovs_key_ipv4
*mask
= ipv4_key
+ 1;
1919 /* Non-writeable fields. */
1920 if (mask
->ipv4_proto
|| mask
->ipv4_frag
)
1923 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
1926 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
1931 case OVS_KEY_ATTR_IPV6
:
1932 if (eth_type
!= htons(ETH_P_IPV6
))
1935 ipv6_key
= nla_data(ovs_key
);
1938 const struct ovs_key_ipv6
*mask
= ipv6_key
+ 1;
1940 /* Non-writeable fields. */
1941 if (mask
->ipv6_proto
|| mask
->ipv6_frag
)
1944 /* Invalid bits in the flow label mask? */
1945 if (ntohl(mask
->ipv6_label
) & 0xFFF00000)
1948 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
1951 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
1954 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
1959 case OVS_KEY_ATTR_TCP
:
1960 if ((eth_type
!= htons(ETH_P_IP
) &&
1961 eth_type
!= htons(ETH_P_IPV6
)) ||
1962 flow_key
->ip
.proto
!= IPPROTO_TCP
)
1967 case OVS_KEY_ATTR_UDP
:
1968 if ((eth_type
!= htons(ETH_P_IP
) &&
1969 eth_type
!= htons(ETH_P_IPV6
)) ||
1970 flow_key
->ip
.proto
!= IPPROTO_UDP
)
1975 case OVS_KEY_ATTR_MPLS
:
1976 if (!eth_p_mpls(eth_type
))
1980 case OVS_KEY_ATTR_SCTP
:
1981 if ((eth_type
!= htons(ETH_P_IP
) &&
1982 eth_type
!= htons(ETH_P_IPV6
)) ||
1983 flow_key
->ip
.proto
!= IPPROTO_SCTP
)
1992 /* Convert non-masked non-tunnel set actions to masked set actions. */
1993 if (!masked
&& key_type
!= OVS_KEY_ATTR_TUNNEL
) {
1994 int start
, len
= key_len
* 2;
1999 start
= add_nested_action_start(sfa
,
2000 OVS_ACTION_ATTR_SET_TO_MASKED
,
2005 at
= __add_action(sfa
, key_type
, NULL
, len
, log
);
2009 memcpy(nla_data(at
), nla_data(ovs_key
), key_len
); /* Key. */
2010 memset(nla_data(at
) + key_len
, 0xff, key_len
); /* Mask. */
2011 /* Clear non-writeable bits from otherwise writeable fields. */
2012 if (key_type
== OVS_KEY_ATTR_IPV6
) {
2013 struct ovs_key_ipv6
*mask
= nla_data(at
) + key_len
;
2015 mask
->ipv6_label
&= htonl(0x000FFFFF);
2017 add_nested_action_end(*sfa
, start
);
2023 static int validate_userspace(const struct nlattr
*attr
)
2025 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
2026 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
2027 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
2028 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT
] = {.type
= NLA_U32
},
2030 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
2033 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
,
2034 attr
, userspace_policy
);
2038 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
2039 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
2045 static int copy_action(const struct nlattr
*from
,
2046 struct sw_flow_actions
**sfa
, bool log
)
2048 int totlen
= NLA_ALIGN(from
->nla_len
);
2051 to
= reserve_sfa_size(sfa
, from
->nla_len
, log
);
2055 memcpy(to
, from
, totlen
);
2059 static int __ovs_nla_copy_actions(const struct nlattr
*attr
,
2060 const struct sw_flow_key
*key
,
2061 int depth
, struct sw_flow_actions
**sfa
,
2062 __be16 eth_type
, __be16 vlan_tci
, bool log
)
2064 const struct nlattr
*a
;
2067 if (depth
>= SAMPLE_ACTION_DEPTH
)
2070 nla_for_each_nested(a
, attr
, rem
) {
2071 /* Expected argument lengths, (u32)-1 for variable length. */
2072 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
2073 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
2074 [OVS_ACTION_ATTR_RECIRC
] = sizeof(u32
),
2075 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
2076 [OVS_ACTION_ATTR_PUSH_MPLS
] = sizeof(struct ovs_action_push_mpls
),
2077 [OVS_ACTION_ATTR_POP_MPLS
] = sizeof(__be16
),
2078 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
2079 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
2080 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
2081 [OVS_ACTION_ATTR_SET_MASKED
] = (u32
)-1,
2082 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1,
2083 [OVS_ACTION_ATTR_HASH
] = sizeof(struct ovs_action_hash
)
2085 const struct ovs_action_push_vlan
*vlan
;
2086 int type
= nla_type(a
);
2089 if (type
> OVS_ACTION_ATTR_MAX
||
2090 (action_lens
[type
] != nla_len(a
) &&
2091 action_lens
[type
] != (u32
)-1))
2096 case OVS_ACTION_ATTR_UNSPEC
:
2099 case OVS_ACTION_ATTR_USERSPACE
:
2100 err
= validate_userspace(a
);
2105 case OVS_ACTION_ATTR_OUTPUT
:
2106 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
2110 case OVS_ACTION_ATTR_HASH
: {
2111 const struct ovs_action_hash
*act_hash
= nla_data(a
);
2113 switch (act_hash
->hash_alg
) {
2114 case OVS_HASH_ALG_L4
:
2123 case OVS_ACTION_ATTR_POP_VLAN
:
2124 vlan_tci
= htons(0);
2127 case OVS_ACTION_ATTR_PUSH_VLAN
:
2129 if (vlan
->vlan_tpid
!= htons(ETH_P_8021Q
))
2131 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
2133 vlan_tci
= vlan
->vlan_tci
;
2136 case OVS_ACTION_ATTR_RECIRC
:
2139 case OVS_ACTION_ATTR_PUSH_MPLS
: {
2140 const struct ovs_action_push_mpls
*mpls
= nla_data(a
);
2142 if (!eth_p_mpls(mpls
->mpls_ethertype
))
2144 /* Prohibit push MPLS other than to a white list
2145 * for packets that have a known tag order.
2147 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2148 (eth_type
!= htons(ETH_P_IP
) &&
2149 eth_type
!= htons(ETH_P_IPV6
) &&
2150 eth_type
!= htons(ETH_P_ARP
) &&
2151 eth_type
!= htons(ETH_P_RARP
) &&
2152 !eth_p_mpls(eth_type
)))
2154 eth_type
= mpls
->mpls_ethertype
;
2158 case OVS_ACTION_ATTR_POP_MPLS
:
2159 if (vlan_tci
& htons(VLAN_TAG_PRESENT
) ||
2160 !eth_p_mpls(eth_type
))
2163 /* Disallow subsequent L2.5+ set and mpls_pop actions
2164 * as there is no check here to ensure that the new
2165 * eth_type is valid and thus set actions could
2166 * write off the end of the packet or otherwise
2169 * Support for these actions is planned using packet
2172 eth_type
= htons(0);
2175 case OVS_ACTION_ATTR_SET
:
2176 err
= validate_set(a
, key
, sfa
,
2177 &skip_copy
, eth_type
, false, log
);
2182 case OVS_ACTION_ATTR_SET_MASKED
:
2183 err
= validate_set(a
, key
, sfa
,
2184 &skip_copy
, eth_type
, true, log
);
2189 case OVS_ACTION_ATTR_SAMPLE
:
2190 err
= validate_and_copy_sample(a
, key
, depth
, sfa
,
2191 eth_type
, vlan_tci
, log
);
2198 OVS_NLERR(log
, "Unknown Action type %d", type
);
2202 err
= copy_action(a
, sfa
, log
);
2214 /* 'key' must be the masked key. */
2215 int ovs_nla_copy_actions(const struct nlattr
*attr
,
2216 const struct sw_flow_key
*key
,
2217 struct sw_flow_actions
**sfa
, bool log
)
2221 *sfa
= nla_alloc_flow_actions(nla_len(attr
), log
);
2223 return PTR_ERR(*sfa
);
2225 err
= __ovs_nla_copy_actions(attr
, key
, 0, sfa
, key
->eth
.type
,
2233 static int sample_action_to_attr(const struct nlattr
*attr
, struct sk_buff
*skb
)
2235 const struct nlattr
*a
;
2236 struct nlattr
*start
;
2239 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SAMPLE
);
2243 nla_for_each_nested(a
, attr
, rem
) {
2244 int type
= nla_type(a
);
2245 struct nlattr
*st_sample
;
2248 case OVS_SAMPLE_ATTR_PROBABILITY
:
2249 if (nla_put(skb
, OVS_SAMPLE_ATTR_PROBABILITY
,
2250 sizeof(u32
), nla_data(a
)))
2253 case OVS_SAMPLE_ATTR_ACTIONS
:
2254 st_sample
= nla_nest_start(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
2257 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
2260 nla_nest_end(skb
, st_sample
);
2265 nla_nest_end(skb
, start
);
2269 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
2271 const struct nlattr
*ovs_key
= nla_data(a
);
2272 int key_type
= nla_type(ovs_key
);
2273 struct nlattr
*start
;
2277 case OVS_KEY_ATTR_TUNNEL_INFO
: {
2278 struct ovs_tunnel_info
*tun_info
= nla_data(ovs_key
);
2280 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
2284 err
= ipv4_tun_to_nlattr(skb
, &tun_info
->tunnel
,
2285 tun_info
->options_len
?
2286 tun_info
->options
: NULL
,
2287 tun_info
->options_len
);
2290 nla_nest_end(skb
, start
);
2294 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
2302 static int masked_set_action_to_set_action_attr(const struct nlattr
*a
,
2303 struct sk_buff
*skb
)
2305 const struct nlattr
*ovs_key
= nla_data(a
);
2306 size_t key_len
= nla_len(ovs_key
) / 2;
2308 /* Revert the conversion we did from a non-masked set action to
2309 * masked set action.
2311 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
) - key_len
, ovs_key
))
2317 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
2319 const struct nlattr
*a
;
2322 nla_for_each_attr(a
, attr
, len
, rem
) {
2323 int type
= nla_type(a
);
2326 case OVS_ACTION_ATTR_SET
:
2327 err
= set_action_to_attr(a
, skb
);
2332 case OVS_ACTION_ATTR_SET_TO_MASKED
:
2333 err
= masked_set_action_to_set_action_attr(a
, skb
);
2338 case OVS_ACTION_ATTR_SAMPLE
:
2339 err
= sample_action_to_attr(a
, skb
);
2344 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))