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
23 #include <linux/uaccess.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <net/llc_pdu.h>
29 #include <linux/kernel.h>
30 #include <linux/jhash.h>
31 #include <linux/jiffies.h>
32 #include <linux/llc.h>
33 #include <linux/module.h>
35 #include <linux/rcupdate.h>
36 #include <linux/if_arp.h>
38 #include <linux/ipv6.h>
39 #include <linux/sctp.h>
40 #include <linux/tcp.h>
41 #include <linux/udp.h>
42 #include <linux/icmp.h>
43 #include <linux/icmpv6.h>
44 #include <linux/rculist.h>
45 #include <net/geneve.h>
48 #include <net/ndisc.h>
50 #include "flow_netlink.h"
52 static void update_range__(struct sw_flow_match
*match
,
53 size_t offset
, size_t size
, bool is_mask
)
55 struct sw_flow_key_range
*range
= NULL
;
56 size_t start
= rounddown(offset
, sizeof(long));
57 size_t end
= roundup(offset
+ size
, sizeof(long));
60 range
= &match
->range
;
62 range
= &match
->mask
->range
;
67 if (range
->start
== range
->end
) {
73 if (range
->start
> start
)
80 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82 update_range__(match, offsetof(struct sw_flow_key, field), \
83 sizeof((match)->key->field), is_mask); \
86 (match)->mask->key.field = value; \
88 (match)->key->field = value; \
92 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
94 update_range__(match, offset, len, is_mask); \
96 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
99 memcpy((u8 *)(match)->key + offset, value_p, len); \
102 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
103 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
104 value_p, len, is_mask)
106 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
108 update_range__(match, offsetof(struct sw_flow_key, field), \
109 sizeof((match)->key->field), is_mask); \
112 memset((u8 *)&(match)->mask->key.field, value,\
113 sizeof((match)->mask->key.field)); \
115 memset((u8 *)&(match)->key->field, value, \
116 sizeof((match)->key->field)); \
120 static bool match_validate(const struct sw_flow_match
*match
,
121 u64 key_attrs
, u64 mask_attrs
)
123 u64 key_expected
= 1 << OVS_KEY_ATTR_ETHERNET
;
124 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
126 /* The following mask attributes allowed only if they
127 * pass the validation tests. */
128 mask_allowed
&= ~((1 << OVS_KEY_ATTR_IPV4
)
129 | (1 << OVS_KEY_ATTR_IPV6
)
130 | (1 << OVS_KEY_ATTR_TCP
)
131 | (1 << OVS_KEY_ATTR_TCP_FLAGS
)
132 | (1 << OVS_KEY_ATTR_UDP
)
133 | (1 << OVS_KEY_ATTR_SCTP
)
134 | (1 << OVS_KEY_ATTR_ICMP
)
135 | (1 << OVS_KEY_ATTR_ICMPV6
)
136 | (1 << OVS_KEY_ATTR_ARP
)
137 | (1 << OVS_KEY_ATTR_ND
));
139 /* Always allowed mask fields. */
140 mask_allowed
|= ((1 << OVS_KEY_ATTR_TUNNEL
)
141 | (1 << OVS_KEY_ATTR_IN_PORT
)
142 | (1 << OVS_KEY_ATTR_ETHERTYPE
));
144 /* Check key attributes. */
145 if (match
->key
->eth
.type
== htons(ETH_P_ARP
)
146 || match
->key
->eth
.type
== htons(ETH_P_RARP
)) {
147 key_expected
|= 1 << OVS_KEY_ATTR_ARP
;
148 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xff)))
149 mask_allowed
|= 1 << OVS_KEY_ATTR_ARP
;
152 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
153 key_expected
|= 1 << OVS_KEY_ATTR_IPV4
;
154 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
155 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV4
;
157 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
158 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
159 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
160 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
161 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
164 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
165 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
166 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
167 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
170 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
171 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
172 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
173 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
174 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
175 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
179 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
180 key_expected
|= 1 << OVS_KEY_ATTR_ICMP
;
181 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
182 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMP
;
187 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
188 key_expected
|= 1 << OVS_KEY_ATTR_IPV6
;
189 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
190 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV6
;
192 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
193 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
194 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
195 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
196 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
199 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
200 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
201 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
202 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
205 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
206 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
207 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
208 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
209 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
210 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
214 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
215 key_expected
|= 1 << OVS_KEY_ATTR_ICMPV6
;
216 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
217 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMPV6
;
219 if (match
->key
->tp
.src
==
220 htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
221 match
->key
->tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
222 key_expected
|= 1 << OVS_KEY_ATTR_ND
;
223 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xffff)))
224 mask_allowed
|= 1 << OVS_KEY_ATTR_ND
;
230 if ((key_attrs
& key_expected
) != key_expected
) {
231 /* Key attributes check failed. */
232 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
233 (unsigned long long)key_attrs
, (unsigned long long)key_expected
);
237 if ((mask_attrs
& mask_allowed
) != mask_attrs
) {
238 /* Mask attributes check failed. */
239 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
240 (unsigned long long)mask_attrs
, (unsigned long long)mask_allowed
);
247 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
248 static const int ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
249 [OVS_KEY_ATTR_ENCAP
] = -1,
250 [OVS_KEY_ATTR_PRIORITY
] = sizeof(u32
),
251 [OVS_KEY_ATTR_IN_PORT
] = sizeof(u32
),
252 [OVS_KEY_ATTR_SKB_MARK
] = sizeof(u32
),
253 [OVS_KEY_ATTR_ETHERNET
] = sizeof(struct ovs_key_ethernet
),
254 [OVS_KEY_ATTR_VLAN
] = sizeof(__be16
),
255 [OVS_KEY_ATTR_ETHERTYPE
] = sizeof(__be16
),
256 [OVS_KEY_ATTR_IPV4
] = sizeof(struct ovs_key_ipv4
),
257 [OVS_KEY_ATTR_IPV6
] = sizeof(struct ovs_key_ipv6
),
258 [OVS_KEY_ATTR_TCP
] = sizeof(struct ovs_key_tcp
),
259 [OVS_KEY_ATTR_TCP_FLAGS
] = sizeof(__be16
),
260 [OVS_KEY_ATTR_UDP
] = sizeof(struct ovs_key_udp
),
261 [OVS_KEY_ATTR_SCTP
] = sizeof(struct ovs_key_sctp
),
262 [OVS_KEY_ATTR_ICMP
] = sizeof(struct ovs_key_icmp
),
263 [OVS_KEY_ATTR_ICMPV6
] = sizeof(struct ovs_key_icmpv6
),
264 [OVS_KEY_ATTR_ARP
] = sizeof(struct ovs_key_arp
),
265 [OVS_KEY_ATTR_ND
] = sizeof(struct ovs_key_nd
),
266 [OVS_KEY_ATTR_RECIRC_ID
] = sizeof(u32
),
267 [OVS_KEY_ATTR_DP_HASH
] = sizeof(u32
),
268 [OVS_KEY_ATTR_TUNNEL
] = -1,
271 static bool is_all_zero(const u8
*fp
, size_t size
)
278 for (i
= 0; i
< size
; i
++)
285 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
286 const struct nlattr
*a
[],
287 u64
*attrsp
, bool nz
)
289 const struct nlattr
*nla
;
294 nla_for_each_nested(nla
, attr
, rem
) {
295 u16 type
= nla_type(nla
);
298 if (type
> OVS_KEY_ATTR_MAX
) {
299 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
300 type
, OVS_KEY_ATTR_MAX
);
304 if (attrs
& (1 << type
)) {
305 OVS_NLERR("Duplicate key attribute (type %d).\n", type
);
309 expected_len
= ovs_key_lens
[type
];
310 if (nla_len(nla
) != expected_len
&& expected_len
!= -1) {
311 OVS_NLERR("Key attribute has unexpected length (type=%d"
312 ", length=%d, expected=%d).\n", type
,
313 nla_len(nla
), expected_len
);
317 if (!nz
|| !is_all_zero(nla_data(nla
), expected_len
)) {
323 OVS_NLERR("Message has %d unknown bytes.\n", rem
);
331 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
332 const struct nlattr
*a
[], u64
*attrsp
)
334 return __parse_flow_nlattrs(attr
, a
, attrsp
, true);
337 static int parse_flow_nlattrs(const struct nlattr
*attr
,
338 const struct nlattr
*a
[], u64
*attrsp
)
340 return __parse_flow_nlattrs(attr
, a
, attrsp
, false);
343 static int ipv4_tun_from_nlattr(const struct nlattr
*attr
,
344 struct sw_flow_match
*match
, bool is_mask
)
349 __be16 tun_flags
= 0;
350 unsigned long opt_key_offset
;
352 nla_for_each_nested(a
, attr
, rem
) {
353 int type
= nla_type(a
);
354 static const u32 ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
355 [OVS_TUNNEL_KEY_ATTR_ID
] = sizeof(u64
),
356 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = sizeof(u32
),
357 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = sizeof(u32
),
358 [OVS_TUNNEL_KEY_ATTR_TOS
] = 1,
359 [OVS_TUNNEL_KEY_ATTR_TTL
] = 1,
360 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = 0,
361 [OVS_TUNNEL_KEY_ATTR_CSUM
] = 0,
362 [OVS_TUNNEL_KEY_ATTR_OAM
] = 0,
363 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
] = -1,
366 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
367 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
368 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
372 if (ovs_tunnel_key_lens
[type
] != nla_len(a
) &&
373 ovs_tunnel_key_lens
[type
] != -1) {
374 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
375 " length (type=%d, length=%d, expected=%d).\n",
376 type
, nla_len(a
), ovs_tunnel_key_lens
[type
]);
381 case OVS_TUNNEL_KEY_ATTR_ID
:
382 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
383 nla_get_be64(a
), is_mask
);
384 tun_flags
|= TUNNEL_KEY
;
386 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
387 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_src
,
388 nla_get_be32(a
), is_mask
);
390 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
391 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_dst
,
392 nla_get_be32(a
), is_mask
);
394 case OVS_TUNNEL_KEY_ATTR_TOS
:
395 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_tos
,
396 nla_get_u8(a
), is_mask
);
398 case OVS_TUNNEL_KEY_ATTR_TTL
:
399 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_ttl
,
400 nla_get_u8(a
), is_mask
);
403 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
404 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
406 case OVS_TUNNEL_KEY_ATTR_CSUM
:
407 tun_flags
|= TUNNEL_CSUM
;
409 case OVS_TUNNEL_KEY_ATTR_OAM
:
410 tun_flags
|= TUNNEL_OAM
;
412 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
:
413 tun_flags
|= TUNNEL_OPTIONS_PRESENT
;
414 if (nla_len(a
) > sizeof(match
->key
->tun_opts
)) {
415 OVS_NLERR("Geneve option length exceeds maximum size (len %d, max %zu).\n",
417 sizeof(match
->key
->tun_opts
));
421 if (nla_len(a
) % 4 != 0) {
422 OVS_NLERR("Geneve option length is not a multiple of 4 (len %d).\n",
427 /* We need to record the length of the options passed
428 * down, otherwise packets with the same format but
429 * additional options will be silently matched.
432 SW_FLOW_KEY_PUT(match
, tun_opts_len
, nla_len(a
),
435 /* This is somewhat unusual because it looks at
436 * both the key and mask while parsing the
437 * attributes (and by extension assumes the key
438 * is parsed first). Normally, we would verify
439 * that each is the correct length and that the
440 * attributes line up in the validate function.
441 * However, that is difficult because this is
442 * variable length and we won't have the
445 if (match
->key
->tun_opts_len
!= nla_len(a
)) {
446 OVS_NLERR("Geneve option key length (%d) is different from mask length (%d).",
447 match
->key
->tun_opts_len
,
452 SW_FLOW_KEY_PUT(match
, tun_opts_len
, 0xff,
456 opt_key_offset
= (unsigned long)GENEVE_OPTS(
457 (struct sw_flow_key
*)0,
459 SW_FLOW_KEY_MEMCPY_OFFSET(match
, opt_key_offset
,
460 nla_data(a
), nla_len(a
),
464 OVS_NLERR("Unknown IPv4 tunnel attribute (%d).\n",
470 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
473 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem
);
478 if (!match
->key
->tun_key
.ipv4_dst
) {
479 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
484 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
492 static int __ipv4_tun_to_nlattr(struct sk_buff
*skb
,
493 const struct ovs_key_ipv4_tunnel
*output
,
494 const struct geneve_opt
*tun_opts
,
495 int swkey_tun_opts_len
)
497 if (output
->tun_flags
& TUNNEL_KEY
&&
498 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
))
500 if (output
->ipv4_src
&&
501 nla_put_be32(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, output
->ipv4_src
))
503 if (output
->ipv4_dst
&&
504 nla_put_be32(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, output
->ipv4_dst
))
506 if (output
->ipv4_tos
&&
507 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->ipv4_tos
))
509 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ipv4_ttl
))
511 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
512 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
514 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
515 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
517 if ((output
->tun_flags
& TUNNEL_OAM
) &&
518 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_OAM
))
521 nla_put(skb
, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS
,
522 swkey_tun_opts_len
, tun_opts
))
529 static int ipv4_tun_to_nlattr(struct sk_buff
*skb
,
530 const struct ovs_key_ipv4_tunnel
*output
,
531 const struct geneve_opt
*tun_opts
,
532 int swkey_tun_opts_len
)
537 nla
= nla_nest_start(skb
, OVS_KEY_ATTR_TUNNEL
);
541 err
= __ipv4_tun_to_nlattr(skb
, output
, tun_opts
, swkey_tun_opts_len
);
545 nla_nest_end(skb
, nla
);
549 static int metadata_from_nlattrs(struct sw_flow_match
*match
, u64
*attrs
,
550 const struct nlattr
**a
, bool is_mask
)
552 if (*attrs
& (1 << OVS_KEY_ATTR_DP_HASH
)) {
553 u32 hash_val
= nla_get_u32(a
[OVS_KEY_ATTR_DP_HASH
]);
555 SW_FLOW_KEY_PUT(match
, ovs_flow_hash
, hash_val
, is_mask
);
556 *attrs
&= ~(1 << OVS_KEY_ATTR_DP_HASH
);
559 if (*attrs
& (1 << OVS_KEY_ATTR_RECIRC_ID
)) {
560 u32 recirc_id
= nla_get_u32(a
[OVS_KEY_ATTR_RECIRC_ID
]);
562 SW_FLOW_KEY_PUT(match
, recirc_id
, recirc_id
, is_mask
);
563 *attrs
&= ~(1 << OVS_KEY_ATTR_RECIRC_ID
);
566 if (*attrs
& (1 << OVS_KEY_ATTR_PRIORITY
)) {
567 SW_FLOW_KEY_PUT(match
, phy
.priority
,
568 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
569 *attrs
&= ~(1 << OVS_KEY_ATTR_PRIORITY
);
572 if (*attrs
& (1 << OVS_KEY_ATTR_IN_PORT
)) {
573 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
576 in_port
= 0xffffffff; /* Always exact match in_port. */
577 else if (in_port
>= DP_MAX_PORTS
)
580 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
581 *attrs
&= ~(1 << OVS_KEY_ATTR_IN_PORT
);
582 } else if (!is_mask
) {
583 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
586 if (*attrs
& (1 << OVS_KEY_ATTR_SKB_MARK
)) {
587 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
589 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
590 *attrs
&= ~(1 << OVS_KEY_ATTR_SKB_MARK
);
592 if (*attrs
& (1 << OVS_KEY_ATTR_TUNNEL
)) {
593 if (ipv4_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
596 *attrs
&= ~(1 << OVS_KEY_ATTR_TUNNEL
);
601 static int ovs_key_from_nlattrs(struct sw_flow_match
*match
, u64 attrs
,
602 const struct nlattr
**a
, bool is_mask
)
605 u64 orig_attrs
= attrs
;
607 err
= metadata_from_nlattrs(match
, &attrs
, a
, is_mask
);
611 if (attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) {
612 const struct ovs_key_ethernet
*eth_key
;
614 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
615 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
616 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
617 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
618 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
619 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERNET
);
622 if (attrs
& (1 << OVS_KEY_ATTR_VLAN
)) {
625 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
626 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
628 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
630 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
635 SW_FLOW_KEY_PUT(match
, eth
.tci
, tci
, is_mask
);
636 attrs
&= ~(1 << OVS_KEY_ATTR_VLAN
);
638 SW_FLOW_KEY_PUT(match
, eth
.tci
, htons(0xffff), true);
640 if (attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) {
643 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
645 /* Always exact match EtherType. */
646 eth_type
= htons(0xffff);
647 } else if (ntohs(eth_type
) < ETH_P_802_3_MIN
) {
648 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
649 ntohs(eth_type
), ETH_P_802_3_MIN
);
653 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
654 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
655 } else if (!is_mask
) {
656 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
659 if (attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
660 const struct ovs_key_ipv4
*ipv4_key
;
662 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
663 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
664 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
665 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
668 SW_FLOW_KEY_PUT(match
, ip
.proto
,
669 ipv4_key
->ipv4_proto
, is_mask
);
670 SW_FLOW_KEY_PUT(match
, ip
.tos
,
671 ipv4_key
->ipv4_tos
, is_mask
);
672 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
673 ipv4_key
->ipv4_ttl
, is_mask
);
674 SW_FLOW_KEY_PUT(match
, ip
.frag
,
675 ipv4_key
->ipv4_frag
, is_mask
);
676 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
677 ipv4_key
->ipv4_src
, is_mask
);
678 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
679 ipv4_key
->ipv4_dst
, is_mask
);
680 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
683 if (attrs
& (1 << OVS_KEY_ATTR_IPV6
)) {
684 const struct ovs_key_ipv6
*ipv6_key
;
686 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
687 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
688 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
689 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
693 if (!is_mask
&& ipv6_key
->ipv6_label
& htonl(0xFFF00000)) {
694 OVS_NLERR("IPv6 flow label %x is out of range (max=%x).\n",
695 ntohl(ipv6_key
->ipv6_label
), (1 << 20) - 1);
699 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
700 ipv6_key
->ipv6_label
, is_mask
);
701 SW_FLOW_KEY_PUT(match
, ip
.proto
,
702 ipv6_key
->ipv6_proto
, is_mask
);
703 SW_FLOW_KEY_PUT(match
, ip
.tos
,
704 ipv6_key
->ipv6_tclass
, is_mask
);
705 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
706 ipv6_key
->ipv6_hlimit
, is_mask
);
707 SW_FLOW_KEY_PUT(match
, ip
.frag
,
708 ipv6_key
->ipv6_frag
, is_mask
);
709 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
711 sizeof(match
->key
->ipv6
.addr
.src
),
713 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
715 sizeof(match
->key
->ipv6
.addr
.dst
),
718 attrs
&= ~(1 << OVS_KEY_ATTR_IPV6
);
721 if (attrs
& (1 << OVS_KEY_ATTR_ARP
)) {
722 const struct ovs_key_arp
*arp_key
;
724 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
725 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
726 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
731 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
732 arp_key
->arp_sip
, is_mask
);
733 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
734 arp_key
->arp_tip
, is_mask
);
735 SW_FLOW_KEY_PUT(match
, ip
.proto
,
736 ntohs(arp_key
->arp_op
), is_mask
);
737 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
738 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
739 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
740 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
742 attrs
&= ~(1 << OVS_KEY_ATTR_ARP
);
745 if (attrs
& (1 << OVS_KEY_ATTR_TCP
)) {
746 const struct ovs_key_tcp
*tcp_key
;
748 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
749 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
750 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
751 attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
754 if (attrs
& (1 << OVS_KEY_ATTR_TCP_FLAGS
)) {
755 if (orig_attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
756 SW_FLOW_KEY_PUT(match
, tp
.flags
,
757 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
760 SW_FLOW_KEY_PUT(match
, tp
.flags
,
761 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
764 attrs
&= ~(1 << OVS_KEY_ATTR_TCP_FLAGS
);
767 if (attrs
& (1 << OVS_KEY_ATTR_UDP
)) {
768 const struct ovs_key_udp
*udp_key
;
770 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
771 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
772 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
773 attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
776 if (attrs
& (1 << OVS_KEY_ATTR_SCTP
)) {
777 const struct ovs_key_sctp
*sctp_key
;
779 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
780 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
781 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
782 attrs
&= ~(1 << OVS_KEY_ATTR_SCTP
);
785 if (attrs
& (1 << OVS_KEY_ATTR_ICMP
)) {
786 const struct ovs_key_icmp
*icmp_key
;
788 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
789 SW_FLOW_KEY_PUT(match
, tp
.src
,
790 htons(icmp_key
->icmp_type
), is_mask
);
791 SW_FLOW_KEY_PUT(match
, tp
.dst
,
792 htons(icmp_key
->icmp_code
), is_mask
);
793 attrs
&= ~(1 << OVS_KEY_ATTR_ICMP
);
796 if (attrs
& (1 << OVS_KEY_ATTR_ICMPV6
)) {
797 const struct ovs_key_icmpv6
*icmpv6_key
;
799 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
800 SW_FLOW_KEY_PUT(match
, tp
.src
,
801 htons(icmpv6_key
->icmpv6_type
), is_mask
);
802 SW_FLOW_KEY_PUT(match
, tp
.dst
,
803 htons(icmpv6_key
->icmpv6_code
), is_mask
);
804 attrs
&= ~(1 << OVS_KEY_ATTR_ICMPV6
);
807 if (attrs
& (1 << OVS_KEY_ATTR_ND
)) {
808 const struct ovs_key_nd
*nd_key
;
810 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
811 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
813 sizeof(match
->key
->ipv6
.nd
.target
),
815 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
816 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
817 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
818 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
819 attrs
&= ~(1 << OVS_KEY_ATTR_ND
);
828 static void nlattr_set(struct nlattr
*attr
, u8 val
, bool is_attr_mask_key
)
833 /* The nlattr stream should already have been validated */
834 nla_for_each_nested(nla
, attr
, rem
) {
835 /* We assume that ovs_key_lens[type] == -1 means that type is a
838 if (is_attr_mask_key
&& ovs_key_lens
[nla_type(nla
)] == -1)
839 nlattr_set(nla
, val
, false);
841 memset(nla_data(nla
), val
, nla_len(nla
));
845 static void mask_set_nlattr(struct nlattr
*attr
, u8 val
)
847 nlattr_set(attr
, val
, true);
851 * ovs_nla_get_match - parses Netlink attributes into a flow key and
852 * mask. In case the 'mask' is NULL, the flow is treated as exact match
853 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
854 * does not include any don't care bit.
855 * @match: receives the extracted flow match information.
856 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
857 * sequence. The fields should of the packet that triggered the creation
859 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
860 * attribute specifies the mask field of the wildcarded flow.
862 int ovs_nla_get_match(struct sw_flow_match
*match
,
863 const struct nlattr
*key
,
864 const struct nlattr
*mask
)
866 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
867 const struct nlattr
*encap
;
868 struct nlattr
*newmask
= NULL
;
871 bool encap_valid
= false;
874 err
= parse_flow_nlattrs(key
, a
, &key_attrs
);
878 if ((key_attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) &&
879 (key_attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) &&
880 (nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]) == htons(ETH_P_8021Q
))) {
883 if (!((key_attrs
& (1 << OVS_KEY_ATTR_VLAN
)) &&
884 (key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
)))) {
885 OVS_NLERR("Invalid Vlan frame.\n");
889 key_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
890 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
891 encap
= a
[OVS_KEY_ATTR_ENCAP
];
892 key_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
895 if (tci
& htons(VLAN_TAG_PRESENT
)) {
896 err
= parse_flow_nlattrs(encap
, a
, &key_attrs
);
900 /* Corner case for truncated 802.1Q header. */
901 if (nla_len(encap
)) {
902 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
906 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
911 err
= ovs_key_from_nlattrs(match
, key_attrs
, a
, false);
915 if (match
->mask
&& !mask
) {
916 /* Create an exact match mask. We need to set to 0xff all the
917 * 'match->mask' fields that have been touched in 'match->key'.
918 * We cannot simply memset 'match->mask', because padding bytes
919 * and fields not specified in 'match->key' should be left to 0.
920 * Instead, we use a stream of netlink attributes, copied from
921 * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
922 * of filling 'match->mask' appropriately.
924 newmask
= kmemdup(key
, nla_total_size(nla_len(key
)),
929 mask_set_nlattr(newmask
, 0xff);
931 /* The userspace does not send tunnel attributes that are 0,
932 * but we should not wildcard them nonetheless.
934 if (match
->key
->tun_key
.ipv4_dst
)
935 SW_FLOW_KEY_MEMSET_FIELD(match
, tun_key
, 0xff, true);
941 err
= parse_flow_mask_nlattrs(mask
, a
, &mask_attrs
);
945 if (mask_attrs
& 1 << OVS_KEY_ATTR_ENCAP
) {
950 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
955 mask_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
956 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
957 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
959 if (eth_type
== htons(0xffff)) {
960 mask_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
961 encap
= a
[OVS_KEY_ATTR_ENCAP
];
962 err
= parse_flow_mask_nlattrs(encap
, a
, &mask_attrs
);
966 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
972 if (a
[OVS_KEY_ATTR_VLAN
])
973 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
975 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
976 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci
));
982 err
= ovs_key_from_nlattrs(match
, mask_attrs
, a
, true);
987 if (!match_validate(match
, key_attrs
, mask_attrs
))
996 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
997 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
998 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1001 * This parses a series of Netlink attributes that form a flow key, which must
1002 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1003 * get the metadata, that is, the parts of the flow key that cannot be
1004 * extracted from the packet itself.
1007 int ovs_nla_get_flow_metadata(const struct nlattr
*attr
,
1008 struct sw_flow_key
*key
)
1010 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
1011 struct sw_flow_match match
;
1015 err
= parse_flow_nlattrs(attr
, a
, &attrs
);
1019 memset(&match
, 0, sizeof(match
));
1022 key
->phy
.in_port
= DP_MAX_PORTS
;
1024 return metadata_from_nlattrs(&match
, &attrs
, a
, false);
1027 int ovs_nla_put_flow(const struct sw_flow_key
*swkey
,
1028 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
1030 struct ovs_key_ethernet
*eth_key
;
1031 struct nlattr
*nla
, *encap
;
1032 bool is_mask
= (swkey
!= output
);
1034 if (nla_put_u32(skb
, OVS_KEY_ATTR_RECIRC_ID
, output
->recirc_id
))
1035 goto nla_put_failure
;
1037 if (nla_put_u32(skb
, OVS_KEY_ATTR_DP_HASH
, output
->ovs_flow_hash
))
1038 goto nla_put_failure
;
1040 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
1041 goto nla_put_failure
;
1043 if ((swkey
->tun_key
.ipv4_dst
|| is_mask
)) {
1044 const struct geneve_opt
*opts
= NULL
;
1046 if (output
->tun_key
.tun_flags
& TUNNEL_OPTIONS_PRESENT
)
1047 opts
= GENEVE_OPTS(output
, swkey
->tun_opts_len
);
1049 if (ipv4_tun_to_nlattr(skb
, &output
->tun_key
, opts
,
1050 swkey
->tun_opts_len
))
1051 goto nla_put_failure
;
1054 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
1055 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
1056 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
1057 goto nla_put_failure
;
1060 upper_u16
= !is_mask
? 0 : 0xffff;
1062 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
1063 (upper_u16
<< 16) | output
->phy
.in_port
))
1064 goto nla_put_failure
;
1067 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
1068 goto nla_put_failure
;
1070 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
1072 goto nla_put_failure
;
1074 eth_key
= nla_data(nla
);
1075 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
1076 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
1078 if (swkey
->eth
.tci
|| swkey
->eth
.type
== htons(ETH_P_8021Q
)) {
1080 eth_type
= !is_mask
? htons(ETH_P_8021Q
) : htons(0xffff);
1081 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
1082 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, output
->eth
.tci
))
1083 goto nla_put_failure
;
1084 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
1085 if (!swkey
->eth
.tci
)
1090 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
1092 * Ethertype 802.2 is represented in the netlink with omitted
1093 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1094 * 0xffff in the mask attribute. Ethertype can also
1097 if (is_mask
&& output
->eth
.type
)
1098 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
1100 goto nla_put_failure
;
1104 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
1105 goto nla_put_failure
;
1107 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
1108 struct ovs_key_ipv4
*ipv4_key
;
1110 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
1112 goto nla_put_failure
;
1113 ipv4_key
= nla_data(nla
);
1114 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
1115 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
1116 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
1117 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
1118 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
1119 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
1120 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
1121 struct ovs_key_ipv6
*ipv6_key
;
1123 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
1125 goto nla_put_failure
;
1126 ipv6_key
= nla_data(nla
);
1127 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
1128 sizeof(ipv6_key
->ipv6_src
));
1129 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
1130 sizeof(ipv6_key
->ipv6_dst
));
1131 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
1132 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
1133 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
1134 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
1135 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
1136 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
1137 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
1138 struct ovs_key_arp
*arp_key
;
1140 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
1142 goto nla_put_failure
;
1143 arp_key
= nla_data(nla
);
1144 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
1145 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
1146 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
1147 arp_key
->arp_op
= htons(output
->ip
.proto
);
1148 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
1149 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
1152 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
1153 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
1154 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
1156 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
1157 struct ovs_key_tcp
*tcp_key
;
1159 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
1161 goto nla_put_failure
;
1162 tcp_key
= nla_data(nla
);
1163 tcp_key
->tcp_src
= output
->tp
.src
;
1164 tcp_key
->tcp_dst
= output
->tp
.dst
;
1165 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
1167 goto nla_put_failure
;
1168 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1169 struct ovs_key_udp
*udp_key
;
1171 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1173 goto nla_put_failure
;
1174 udp_key
= nla_data(nla
);
1175 udp_key
->udp_src
= output
->tp
.src
;
1176 udp_key
->udp_dst
= output
->tp
.dst
;
1177 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
1178 struct ovs_key_sctp
*sctp_key
;
1180 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
1182 goto nla_put_failure
;
1183 sctp_key
= nla_data(nla
);
1184 sctp_key
->sctp_src
= output
->tp
.src
;
1185 sctp_key
->sctp_dst
= output
->tp
.dst
;
1186 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1187 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1188 struct ovs_key_icmp
*icmp_key
;
1190 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1192 goto nla_put_failure
;
1193 icmp_key
= nla_data(nla
);
1194 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
1195 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
1196 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1197 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1198 struct ovs_key_icmpv6
*icmpv6_key
;
1200 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1201 sizeof(*icmpv6_key
));
1203 goto nla_put_failure
;
1204 icmpv6_key
= nla_data(nla
);
1205 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
1206 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
1208 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1209 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1210 struct ovs_key_nd
*nd_key
;
1212 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1214 goto nla_put_failure
;
1215 nd_key
= nla_data(nla
);
1216 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
1217 sizeof(nd_key
->nd_target
));
1218 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
1219 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
1226 nla_nest_end(skb
, encap
);
1234 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1236 struct sw_flow_actions
*ovs_nla_alloc_flow_actions(int size
)
1238 struct sw_flow_actions
*sfa
;
1240 if (size
> MAX_ACTIONS_BUFSIZE
)
1241 return ERR_PTR(-EINVAL
);
1243 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
1245 return ERR_PTR(-ENOMEM
);
1247 sfa
->actions_len
= 0;
1251 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1252 * The caller must hold rcu_read_lock for this to be sensible. */
1253 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
1255 kfree_rcu(sf_acts
, rcu
);
1258 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
1262 struct sw_flow_actions
*acts
;
1264 int req_size
= NLA_ALIGN(attr_len
);
1265 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
1266 (*sfa
)->actions_len
;
1268 if (req_size
<= (ksize(*sfa
) - next_offset
))
1271 new_acts_size
= ksize(*sfa
) * 2;
1273 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
1274 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
)
1275 return ERR_PTR(-EMSGSIZE
);
1276 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
1279 acts
= ovs_nla_alloc_flow_actions(new_acts_size
);
1281 return (void *)acts
;
1283 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
1284 acts
->actions_len
= (*sfa
)->actions_len
;
1289 (*sfa
)->actions_len
+= req_size
;
1290 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
1293 static struct nlattr
*__add_action(struct sw_flow_actions
**sfa
,
1294 int attrtype
, void *data
, int len
)
1298 a
= reserve_sfa_size(sfa
, nla_attr_size(len
));
1302 a
->nla_type
= attrtype
;
1303 a
->nla_len
= nla_attr_size(len
);
1306 memcpy(nla_data(a
), data
, len
);
1307 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
1312 static int add_action(struct sw_flow_actions
**sfa
, int attrtype
,
1313 void *data
, int len
)
1317 a
= __add_action(sfa
, attrtype
, data
, len
);
1324 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
1327 int used
= (*sfa
)->actions_len
;
1330 err
= add_action(sfa
, attrtype
, NULL
, 0);
1337 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
1340 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
1343 a
->nla_len
= sfa
->actions_len
- st_offset
;
1346 static int validate_and_copy_sample(const struct nlattr
*attr
,
1347 const struct sw_flow_key
*key
, int depth
,
1348 struct sw_flow_actions
**sfa
)
1350 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
1351 const struct nlattr
*probability
, *actions
;
1352 const struct nlattr
*a
;
1353 int rem
, start
, err
, st_acts
;
1355 memset(attrs
, 0, sizeof(attrs
));
1356 nla_for_each_nested(a
, attr
, rem
) {
1357 int type
= nla_type(a
);
1358 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
1365 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
1366 if (!probability
|| nla_len(probability
) != sizeof(u32
))
1369 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
1370 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
1373 /* validation done, copy sample action. */
1374 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
);
1377 err
= add_action(sfa
, OVS_SAMPLE_ATTR_PROBABILITY
,
1378 nla_data(probability
), sizeof(u32
));
1381 st_acts
= add_nested_action_start(sfa
, OVS_SAMPLE_ATTR_ACTIONS
);
1385 err
= ovs_nla_copy_actions(actions
, key
, depth
+ 1, sfa
);
1389 add_nested_action_end(*sfa
, st_acts
);
1390 add_nested_action_end(*sfa
, start
);
1395 static int validate_tp_port(const struct sw_flow_key
*flow_key
)
1397 if ((flow_key
->eth
.type
== htons(ETH_P_IP
) ||
1398 flow_key
->eth
.type
== htons(ETH_P_IPV6
)) &&
1399 (flow_key
->tp
.src
|| flow_key
->tp
.dst
))
1405 void ovs_match_init(struct sw_flow_match
*match
,
1406 struct sw_flow_key
*key
,
1407 struct sw_flow_mask
*mask
)
1409 memset(match
, 0, sizeof(*match
));
1413 memset(key
, 0, sizeof(*key
));
1416 memset(&mask
->key
, 0, sizeof(mask
->key
));
1417 mask
->range
.start
= mask
->range
.end
= 0;
1421 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
1422 struct sw_flow_actions
**sfa
)
1424 struct sw_flow_match match
;
1425 struct sw_flow_key key
;
1426 struct ovs_tunnel_info
*tun_info
;
1430 ovs_match_init(&match
, &key
, NULL
);
1431 err
= ipv4_tun_from_nlattr(nla_data(attr
), &match
, false);
1435 if (key
.tun_opts_len
) {
1436 struct geneve_opt
*option
= GENEVE_OPTS(&key
,
1438 int opts_len
= key
.tun_opts_len
;
1439 bool crit_opt
= false;
1441 while (opts_len
> 0) {
1444 if (opts_len
< sizeof(*option
))
1447 len
= sizeof(*option
) + option
->length
* 4;
1451 crit_opt
|= !!(option
->type
& GENEVE_CRIT_OPT_TYPE
);
1453 option
= (struct geneve_opt
*)((u8
*)option
+ len
);
1457 key
.tun_key
.tun_flags
|= crit_opt
? TUNNEL_CRIT_OPT
: 0;
1460 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
);
1464 a
= __add_action(sfa
, OVS_KEY_ATTR_TUNNEL_INFO
, NULL
,
1465 sizeof(*tun_info
) + key
.tun_opts_len
);
1469 tun_info
= nla_data(a
);
1470 tun_info
->tunnel
= key
.tun_key
;
1471 tun_info
->options_len
= key
.tun_opts_len
;
1473 if (tun_info
->options_len
) {
1474 /* We need to store the options in the action itself since
1475 * everything else will go away after flow setup. We can append
1476 * it to tun_info and then point there.
1478 memcpy((tun_info
+ 1), GENEVE_OPTS(&key
, key
.tun_opts_len
),
1480 tun_info
->options
= (struct geneve_opt
*)(tun_info
+ 1);
1482 tun_info
->options
= NULL
;
1485 add_nested_action_end(*sfa
, start
);
1490 static int validate_set(const struct nlattr
*a
,
1491 const struct sw_flow_key
*flow_key
,
1492 struct sw_flow_actions
**sfa
,
1495 const struct nlattr
*ovs_key
= nla_data(a
);
1496 int key_type
= nla_type(ovs_key
);
1498 /* There can be only one key in a action */
1499 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
1502 if (key_type
> OVS_KEY_ATTR_MAX
||
1503 (ovs_key_lens
[key_type
] != nla_len(ovs_key
) &&
1504 ovs_key_lens
[key_type
] != -1))
1508 const struct ovs_key_ipv4
*ipv4_key
;
1509 const struct ovs_key_ipv6
*ipv6_key
;
1512 case OVS_KEY_ATTR_PRIORITY
:
1513 case OVS_KEY_ATTR_SKB_MARK
:
1514 case OVS_KEY_ATTR_ETHERNET
:
1517 case OVS_KEY_ATTR_TUNNEL
:
1519 err
= validate_and_copy_set_tun(a
, sfa
);
1524 case OVS_KEY_ATTR_IPV4
:
1525 if (flow_key
->eth
.type
!= htons(ETH_P_IP
))
1528 if (!flow_key
->ip
.proto
)
1531 ipv4_key
= nla_data(ovs_key
);
1532 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
1535 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
1540 case OVS_KEY_ATTR_IPV6
:
1541 if (flow_key
->eth
.type
!= htons(ETH_P_IPV6
))
1544 if (!flow_key
->ip
.proto
)
1547 ipv6_key
= nla_data(ovs_key
);
1548 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
1551 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
1554 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
1559 case OVS_KEY_ATTR_TCP
:
1560 if (flow_key
->ip
.proto
!= IPPROTO_TCP
)
1563 return validate_tp_port(flow_key
);
1565 case OVS_KEY_ATTR_UDP
:
1566 if (flow_key
->ip
.proto
!= IPPROTO_UDP
)
1569 return validate_tp_port(flow_key
);
1571 case OVS_KEY_ATTR_SCTP
:
1572 if (flow_key
->ip
.proto
!= IPPROTO_SCTP
)
1575 return validate_tp_port(flow_key
);
1584 static int validate_userspace(const struct nlattr
*attr
)
1586 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
1587 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
1588 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
1590 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
1593 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
,
1594 attr
, userspace_policy
);
1598 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
1599 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
1605 static int copy_action(const struct nlattr
*from
,
1606 struct sw_flow_actions
**sfa
)
1608 int totlen
= NLA_ALIGN(from
->nla_len
);
1611 to
= reserve_sfa_size(sfa
, from
->nla_len
);
1615 memcpy(to
, from
, totlen
);
1619 int ovs_nla_copy_actions(const struct nlattr
*attr
,
1620 const struct sw_flow_key
*key
,
1622 struct sw_flow_actions
**sfa
)
1624 const struct nlattr
*a
;
1627 if (depth
>= SAMPLE_ACTION_DEPTH
)
1630 nla_for_each_nested(a
, attr
, rem
) {
1631 /* Expected argument lengths, (u32)-1 for variable length. */
1632 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
1633 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
1634 [OVS_ACTION_ATTR_RECIRC
] = sizeof(u32
),
1635 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
1636 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
1637 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
1638 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
1639 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1,
1640 [OVS_ACTION_ATTR_HASH
] = sizeof(struct ovs_action_hash
)
1642 const struct ovs_action_push_vlan
*vlan
;
1643 int type
= nla_type(a
);
1646 if (type
> OVS_ACTION_ATTR_MAX
||
1647 (action_lens
[type
] != nla_len(a
) &&
1648 action_lens
[type
] != (u32
)-1))
1653 case OVS_ACTION_ATTR_UNSPEC
:
1656 case OVS_ACTION_ATTR_USERSPACE
:
1657 err
= validate_userspace(a
);
1662 case OVS_ACTION_ATTR_OUTPUT
:
1663 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
1667 case OVS_ACTION_ATTR_HASH
: {
1668 const struct ovs_action_hash
*act_hash
= nla_data(a
);
1670 switch (act_hash
->hash_alg
) {
1671 case OVS_HASH_ALG_L4
:
1680 case OVS_ACTION_ATTR_POP_VLAN
:
1683 case OVS_ACTION_ATTR_PUSH_VLAN
:
1685 if (vlan
->vlan_tpid
!= htons(ETH_P_8021Q
))
1687 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
1691 case OVS_ACTION_ATTR_RECIRC
:
1694 case OVS_ACTION_ATTR_SET
:
1695 err
= validate_set(a
, key
, sfa
, &skip_copy
);
1700 case OVS_ACTION_ATTR_SAMPLE
:
1701 err
= validate_and_copy_sample(a
, key
, depth
, sfa
);
1711 err
= copy_action(a
, sfa
);
1723 static int sample_action_to_attr(const struct nlattr
*attr
, struct sk_buff
*skb
)
1725 const struct nlattr
*a
;
1726 struct nlattr
*start
;
1729 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SAMPLE
);
1733 nla_for_each_nested(a
, attr
, rem
) {
1734 int type
= nla_type(a
);
1735 struct nlattr
*st_sample
;
1738 case OVS_SAMPLE_ATTR_PROBABILITY
:
1739 if (nla_put(skb
, OVS_SAMPLE_ATTR_PROBABILITY
,
1740 sizeof(u32
), nla_data(a
)))
1743 case OVS_SAMPLE_ATTR_ACTIONS
:
1744 st_sample
= nla_nest_start(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
1747 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
1750 nla_nest_end(skb
, st_sample
);
1755 nla_nest_end(skb
, start
);
1759 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
1761 const struct nlattr
*ovs_key
= nla_data(a
);
1762 int key_type
= nla_type(ovs_key
);
1763 struct nlattr
*start
;
1767 case OVS_KEY_ATTR_TUNNEL_INFO
: {
1768 struct ovs_tunnel_info
*tun_info
= nla_data(ovs_key
);
1770 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
1774 err
= ipv4_tun_to_nlattr(skb
, &tun_info
->tunnel
,
1775 tun_info
->options_len
?
1776 tun_info
->options
: NULL
,
1777 tun_info
->options_len
);
1780 nla_nest_end(skb
, start
);
1784 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
1792 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
1794 const struct nlattr
*a
;
1797 nla_for_each_attr(a
, attr
, len
, rem
) {
1798 int type
= nla_type(a
);
1801 case OVS_ACTION_ATTR_SET
:
1802 err
= set_action_to_attr(a
, skb
);
1807 case OVS_ACTION_ATTR_SAMPLE
:
1808 err
= sample_action_to_attr(a
, skb
);
1813 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))