2 * Copyright (c) 2007-2013 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>
47 #include <net/ndisc.h>
49 #include "flow_netlink.h"
51 static void update_range__(struct sw_flow_match
*match
,
52 size_t offset
, size_t size
, bool is_mask
)
54 struct sw_flow_key_range
*range
= NULL
;
55 size_t start
= rounddown(offset
, sizeof(long));
56 size_t end
= roundup(offset
+ size
, sizeof(long));
59 range
= &match
->range
;
61 range
= &match
->mask
->range
;
66 if (range
->start
== range
->end
) {
72 if (range
->start
> start
)
79 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
81 update_range__(match, offsetof(struct sw_flow_key, field), \
82 sizeof((match)->key->field), is_mask); \
85 (match)->mask->key.field = value; \
87 (match)->key->field = value; \
91 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
93 update_range__(match, offsetof(struct sw_flow_key, field), \
97 memcpy(&(match)->mask->key.field, value_p, len);\
99 memcpy(&(match)->key->field, value_p, len); \
103 static u16
range_n_bytes(const struct sw_flow_key_range
*range
)
105 return range
->end
- range
->start
;
108 static bool match_validate(const struct sw_flow_match
*match
,
109 u64 key_attrs
, u64 mask_attrs
)
111 u64 key_expected
= 1 << OVS_KEY_ATTR_ETHERNET
;
112 u64 mask_allowed
= key_attrs
; /* At most allow all key attributes */
114 /* The following mask attributes allowed only if they
115 * pass the validation tests. */
116 mask_allowed
&= ~((1 << OVS_KEY_ATTR_IPV4
)
117 | (1 << OVS_KEY_ATTR_IPV6
)
118 | (1 << OVS_KEY_ATTR_TCP
)
119 | (1 << OVS_KEY_ATTR_TCP_FLAGS
)
120 | (1 << OVS_KEY_ATTR_UDP
)
121 | (1 << OVS_KEY_ATTR_SCTP
)
122 | (1 << OVS_KEY_ATTR_ICMP
)
123 | (1 << OVS_KEY_ATTR_ICMPV6
)
124 | (1 << OVS_KEY_ATTR_ARP
)
125 | (1 << OVS_KEY_ATTR_ND
));
127 /* Always allowed mask fields. */
128 mask_allowed
|= ((1 << OVS_KEY_ATTR_TUNNEL
)
129 | (1 << OVS_KEY_ATTR_IN_PORT
)
130 | (1 << OVS_KEY_ATTR_ETHERTYPE
));
132 /* Check key attributes. */
133 if (match
->key
->eth
.type
== htons(ETH_P_ARP
)
134 || match
->key
->eth
.type
== htons(ETH_P_RARP
)) {
135 key_expected
|= 1 << OVS_KEY_ATTR_ARP
;
136 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
137 mask_allowed
|= 1 << OVS_KEY_ATTR_ARP
;
140 if (match
->key
->eth
.type
== htons(ETH_P_IP
)) {
141 key_expected
|= 1 << OVS_KEY_ATTR_IPV4
;
142 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
143 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV4
;
145 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
146 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
147 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
148 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
149 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
152 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
153 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
154 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
155 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
158 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
159 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
160 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
161 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
162 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
163 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
167 if (match
->key
->ip
.proto
== IPPROTO_ICMP
) {
168 key_expected
|= 1 << OVS_KEY_ATTR_ICMP
;
169 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
170 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMP
;
175 if (match
->key
->eth
.type
== htons(ETH_P_IPV6
)) {
176 key_expected
|= 1 << OVS_KEY_ATTR_IPV6
;
177 if (match
->mask
&& (match
->mask
->key
.eth
.type
== htons(0xffff)))
178 mask_allowed
|= 1 << OVS_KEY_ATTR_IPV6
;
180 if (match
->key
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
181 if (match
->key
->ip
.proto
== IPPROTO_UDP
) {
182 key_expected
|= 1 << OVS_KEY_ATTR_UDP
;
183 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
184 mask_allowed
|= 1 << OVS_KEY_ATTR_UDP
;
187 if (match
->key
->ip
.proto
== IPPROTO_SCTP
) {
188 key_expected
|= 1 << OVS_KEY_ATTR_SCTP
;
189 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
190 mask_allowed
|= 1 << OVS_KEY_ATTR_SCTP
;
193 if (match
->key
->ip
.proto
== IPPROTO_TCP
) {
194 key_expected
|= 1 << OVS_KEY_ATTR_TCP
;
195 key_expected
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
196 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff)) {
197 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP
;
198 mask_allowed
|= 1 << OVS_KEY_ATTR_TCP_FLAGS
;
202 if (match
->key
->ip
.proto
== IPPROTO_ICMPV6
) {
203 key_expected
|= 1 << OVS_KEY_ATTR_ICMPV6
;
204 if (match
->mask
&& (match
->mask
->key
.ip
.proto
== 0xff))
205 mask_allowed
|= 1 << OVS_KEY_ATTR_ICMPV6
;
207 if (match
->key
->tp
.src
==
208 htons(NDISC_NEIGHBOUR_SOLICITATION
) ||
209 match
->key
->tp
.src
== htons(NDISC_NEIGHBOUR_ADVERTISEMENT
)) {
210 key_expected
|= 1 << OVS_KEY_ATTR_ND
;
211 if (match
->mask
&& (match
->mask
->key
.tp
.src
== htons(0xffff)))
212 mask_allowed
|= 1 << OVS_KEY_ATTR_ND
;
218 if ((key_attrs
& key_expected
) != key_expected
) {
219 /* Key attributes check failed. */
220 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
221 (unsigned long long)key_attrs
, (unsigned long long)key_expected
);
225 if ((mask_attrs
& mask_allowed
) != mask_attrs
) {
226 /* Mask attributes check failed. */
227 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
228 (unsigned long long)mask_attrs
, (unsigned long long)mask_allowed
);
235 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
236 static const int ovs_key_lens
[OVS_KEY_ATTR_MAX
+ 1] = {
237 [OVS_KEY_ATTR_ENCAP
] = -1,
238 [OVS_KEY_ATTR_PRIORITY
] = sizeof(u32
),
239 [OVS_KEY_ATTR_IN_PORT
] = sizeof(u32
),
240 [OVS_KEY_ATTR_SKB_MARK
] = sizeof(u32
),
241 [OVS_KEY_ATTR_ETHERNET
] = sizeof(struct ovs_key_ethernet
),
242 [OVS_KEY_ATTR_VLAN
] = sizeof(__be16
),
243 [OVS_KEY_ATTR_ETHERTYPE
] = sizeof(__be16
),
244 [OVS_KEY_ATTR_IPV4
] = sizeof(struct ovs_key_ipv4
),
245 [OVS_KEY_ATTR_IPV6
] = sizeof(struct ovs_key_ipv6
),
246 [OVS_KEY_ATTR_TCP
] = sizeof(struct ovs_key_tcp
),
247 [OVS_KEY_ATTR_TCP_FLAGS
] = sizeof(__be16
),
248 [OVS_KEY_ATTR_UDP
] = sizeof(struct ovs_key_udp
),
249 [OVS_KEY_ATTR_SCTP
] = sizeof(struct ovs_key_sctp
),
250 [OVS_KEY_ATTR_ICMP
] = sizeof(struct ovs_key_icmp
),
251 [OVS_KEY_ATTR_ICMPV6
] = sizeof(struct ovs_key_icmpv6
),
252 [OVS_KEY_ATTR_ARP
] = sizeof(struct ovs_key_arp
),
253 [OVS_KEY_ATTR_ND
] = sizeof(struct ovs_key_nd
),
254 [OVS_KEY_ATTR_TUNNEL
] = -1,
257 static bool is_all_zero(const u8
*fp
, size_t size
)
264 for (i
= 0; i
< size
; i
++)
271 static int __parse_flow_nlattrs(const struct nlattr
*attr
,
272 const struct nlattr
*a
[],
273 u64
*attrsp
, bool nz
)
275 const struct nlattr
*nla
;
280 nla_for_each_nested(nla
, attr
, rem
) {
281 u16 type
= nla_type(nla
);
284 if (type
> OVS_KEY_ATTR_MAX
) {
285 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
286 type
, OVS_KEY_ATTR_MAX
);
290 if (attrs
& (1 << type
)) {
291 OVS_NLERR("Duplicate key attribute (type %d).\n", type
);
295 expected_len
= ovs_key_lens
[type
];
296 if (nla_len(nla
) != expected_len
&& expected_len
!= -1) {
297 OVS_NLERR("Key attribute has unexpected length (type=%d"
298 ", length=%d, expected=%d).\n", type
,
299 nla_len(nla
), expected_len
);
303 if (!nz
|| !is_all_zero(nla_data(nla
), expected_len
)) {
309 OVS_NLERR("Message has %d unknown bytes.\n", rem
);
317 static int parse_flow_mask_nlattrs(const struct nlattr
*attr
,
318 const struct nlattr
*a
[], u64
*attrsp
)
320 return __parse_flow_nlattrs(attr
, a
, attrsp
, true);
323 static int parse_flow_nlattrs(const struct nlattr
*attr
,
324 const struct nlattr
*a
[], u64
*attrsp
)
326 return __parse_flow_nlattrs(attr
, a
, attrsp
, false);
329 static int ipv4_tun_from_nlattr(const struct nlattr
*attr
,
330 struct sw_flow_match
*match
, bool is_mask
)
335 __be16 tun_flags
= 0;
337 nla_for_each_nested(a
, attr
, rem
) {
338 int type
= nla_type(a
);
339 static const u32 ovs_tunnel_key_lens
[OVS_TUNNEL_KEY_ATTR_MAX
+ 1] = {
340 [OVS_TUNNEL_KEY_ATTR_ID
] = sizeof(u64
),
341 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC
] = sizeof(u32
),
342 [OVS_TUNNEL_KEY_ATTR_IPV4_DST
] = sizeof(u32
),
343 [OVS_TUNNEL_KEY_ATTR_TOS
] = 1,
344 [OVS_TUNNEL_KEY_ATTR_TTL
] = 1,
345 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
] = 0,
346 [OVS_TUNNEL_KEY_ATTR_CSUM
] = 0,
349 if (type
> OVS_TUNNEL_KEY_ATTR_MAX
) {
350 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
351 type
, OVS_TUNNEL_KEY_ATTR_MAX
);
355 if (ovs_tunnel_key_lens
[type
] != nla_len(a
)) {
356 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
357 " length (type=%d, length=%d, expected=%d).\n",
358 type
, nla_len(a
), ovs_tunnel_key_lens
[type
]);
363 case OVS_TUNNEL_KEY_ATTR_ID
:
364 SW_FLOW_KEY_PUT(match
, tun_key
.tun_id
,
365 nla_get_be64(a
), is_mask
);
366 tun_flags
|= TUNNEL_KEY
;
368 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC
:
369 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_src
,
370 nla_get_be32(a
), is_mask
);
372 case OVS_TUNNEL_KEY_ATTR_IPV4_DST
:
373 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_dst
,
374 nla_get_be32(a
), is_mask
);
376 case OVS_TUNNEL_KEY_ATTR_TOS
:
377 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_tos
,
378 nla_get_u8(a
), is_mask
);
380 case OVS_TUNNEL_KEY_ATTR_TTL
:
381 SW_FLOW_KEY_PUT(match
, tun_key
.ipv4_ttl
,
382 nla_get_u8(a
), is_mask
);
385 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
:
386 tun_flags
|= TUNNEL_DONT_FRAGMENT
;
388 case OVS_TUNNEL_KEY_ATTR_CSUM
:
389 tun_flags
|= TUNNEL_CSUM
;
396 SW_FLOW_KEY_PUT(match
, tun_key
.tun_flags
, tun_flags
, is_mask
);
399 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem
);
404 if (!match
->key
->tun_key
.ipv4_dst
) {
405 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
410 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
418 static int ipv4_tun_to_nlattr(struct sk_buff
*skb
,
419 const struct ovs_key_ipv4_tunnel
*tun_key
,
420 const struct ovs_key_ipv4_tunnel
*output
)
424 nla
= nla_nest_start(skb
, OVS_KEY_ATTR_TUNNEL
);
428 if (output
->tun_flags
& TUNNEL_KEY
&&
429 nla_put_be64(skb
, OVS_TUNNEL_KEY_ATTR_ID
, output
->tun_id
))
431 if (output
->ipv4_src
&&
432 nla_put_be32(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_SRC
, output
->ipv4_src
))
434 if (output
->ipv4_dst
&&
435 nla_put_be32(skb
, OVS_TUNNEL_KEY_ATTR_IPV4_DST
, output
->ipv4_dst
))
437 if (output
->ipv4_tos
&&
438 nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TOS
, output
->ipv4_tos
))
440 if (nla_put_u8(skb
, OVS_TUNNEL_KEY_ATTR_TTL
, output
->ipv4_ttl
))
442 if ((output
->tun_flags
& TUNNEL_DONT_FRAGMENT
) &&
443 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT
))
445 if ((output
->tun_flags
& TUNNEL_CSUM
) &&
446 nla_put_flag(skb
, OVS_TUNNEL_KEY_ATTR_CSUM
))
449 nla_nest_end(skb
, nla
);
454 static int metadata_from_nlattrs(struct sw_flow_match
*match
, u64
*attrs
,
455 const struct nlattr
**a
, bool is_mask
)
457 if (*attrs
& (1 << OVS_KEY_ATTR_PRIORITY
)) {
458 SW_FLOW_KEY_PUT(match
, phy
.priority
,
459 nla_get_u32(a
[OVS_KEY_ATTR_PRIORITY
]), is_mask
);
460 *attrs
&= ~(1 << OVS_KEY_ATTR_PRIORITY
);
463 if (*attrs
& (1 << OVS_KEY_ATTR_IN_PORT
)) {
464 u32 in_port
= nla_get_u32(a
[OVS_KEY_ATTR_IN_PORT
]);
467 in_port
= 0xffffffff; /* Always exact match in_port. */
468 else if (in_port
>= DP_MAX_PORTS
)
471 SW_FLOW_KEY_PUT(match
, phy
.in_port
, in_port
, is_mask
);
472 *attrs
&= ~(1 << OVS_KEY_ATTR_IN_PORT
);
473 } else if (!is_mask
) {
474 SW_FLOW_KEY_PUT(match
, phy
.in_port
, DP_MAX_PORTS
, is_mask
);
477 if (*attrs
& (1 << OVS_KEY_ATTR_SKB_MARK
)) {
478 uint32_t mark
= nla_get_u32(a
[OVS_KEY_ATTR_SKB_MARK
]);
480 SW_FLOW_KEY_PUT(match
, phy
.skb_mark
, mark
, is_mask
);
481 *attrs
&= ~(1 << OVS_KEY_ATTR_SKB_MARK
);
483 if (*attrs
& (1 << OVS_KEY_ATTR_TUNNEL
)) {
484 if (ipv4_tun_from_nlattr(a
[OVS_KEY_ATTR_TUNNEL
], match
,
487 *attrs
&= ~(1 << OVS_KEY_ATTR_TUNNEL
);
492 static int ovs_key_from_nlattrs(struct sw_flow_match
*match
, u64 attrs
,
493 const struct nlattr
**a
, bool is_mask
)
496 u64 orig_attrs
= attrs
;
498 err
= metadata_from_nlattrs(match
, &attrs
, a
, is_mask
);
502 if (attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) {
503 const struct ovs_key_ethernet
*eth_key
;
505 eth_key
= nla_data(a
[OVS_KEY_ATTR_ETHERNET
]);
506 SW_FLOW_KEY_MEMCPY(match
, eth
.src
,
507 eth_key
->eth_src
, ETH_ALEN
, is_mask
);
508 SW_FLOW_KEY_MEMCPY(match
, eth
.dst
,
509 eth_key
->eth_dst
, ETH_ALEN
, is_mask
);
510 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERNET
);
513 if (attrs
& (1 << OVS_KEY_ATTR_VLAN
)) {
516 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
517 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
519 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
521 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
526 SW_FLOW_KEY_PUT(match
, eth
.tci
, tci
, is_mask
);
527 attrs
&= ~(1 << OVS_KEY_ATTR_VLAN
);
529 SW_FLOW_KEY_PUT(match
, eth
.tci
, htons(0xffff), true);
531 if (attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) {
534 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
536 /* Always exact match EtherType. */
537 eth_type
= htons(0xffff);
538 } else if (ntohs(eth_type
) < ETH_P_802_3_MIN
) {
539 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
540 ntohs(eth_type
), ETH_P_802_3_MIN
);
544 SW_FLOW_KEY_PUT(match
, eth
.type
, eth_type
, is_mask
);
545 attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
546 } else if (!is_mask
) {
547 SW_FLOW_KEY_PUT(match
, eth
.type
, htons(ETH_P_802_2
), is_mask
);
550 if (attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
551 const struct ovs_key_ipv4
*ipv4_key
;
553 ipv4_key
= nla_data(a
[OVS_KEY_ATTR_IPV4
]);
554 if (!is_mask
&& ipv4_key
->ipv4_frag
> OVS_FRAG_TYPE_MAX
) {
555 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
556 ipv4_key
->ipv4_frag
, OVS_FRAG_TYPE_MAX
);
559 SW_FLOW_KEY_PUT(match
, ip
.proto
,
560 ipv4_key
->ipv4_proto
, is_mask
);
561 SW_FLOW_KEY_PUT(match
, ip
.tos
,
562 ipv4_key
->ipv4_tos
, is_mask
);
563 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
564 ipv4_key
->ipv4_ttl
, is_mask
);
565 SW_FLOW_KEY_PUT(match
, ip
.frag
,
566 ipv4_key
->ipv4_frag
, is_mask
);
567 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
568 ipv4_key
->ipv4_src
, is_mask
);
569 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
570 ipv4_key
->ipv4_dst
, is_mask
);
571 attrs
&= ~(1 << OVS_KEY_ATTR_IPV4
);
574 if (attrs
& (1 << OVS_KEY_ATTR_IPV6
)) {
575 const struct ovs_key_ipv6
*ipv6_key
;
577 ipv6_key
= nla_data(a
[OVS_KEY_ATTR_IPV6
]);
578 if (!is_mask
&& ipv6_key
->ipv6_frag
> OVS_FRAG_TYPE_MAX
) {
579 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
580 ipv6_key
->ipv6_frag
, OVS_FRAG_TYPE_MAX
);
583 SW_FLOW_KEY_PUT(match
, ipv6
.label
,
584 ipv6_key
->ipv6_label
, is_mask
);
585 SW_FLOW_KEY_PUT(match
, ip
.proto
,
586 ipv6_key
->ipv6_proto
, is_mask
);
587 SW_FLOW_KEY_PUT(match
, ip
.tos
,
588 ipv6_key
->ipv6_tclass
, is_mask
);
589 SW_FLOW_KEY_PUT(match
, ip
.ttl
,
590 ipv6_key
->ipv6_hlimit
, is_mask
);
591 SW_FLOW_KEY_PUT(match
, ip
.frag
,
592 ipv6_key
->ipv6_frag
, is_mask
);
593 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.src
,
595 sizeof(match
->key
->ipv6
.addr
.src
),
597 SW_FLOW_KEY_MEMCPY(match
, ipv6
.addr
.dst
,
599 sizeof(match
->key
->ipv6
.addr
.dst
),
602 attrs
&= ~(1 << OVS_KEY_ATTR_IPV6
);
605 if (attrs
& (1 << OVS_KEY_ATTR_ARP
)) {
606 const struct ovs_key_arp
*arp_key
;
608 arp_key
= nla_data(a
[OVS_KEY_ATTR_ARP
]);
609 if (!is_mask
&& (arp_key
->arp_op
& htons(0xff00))) {
610 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
615 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.src
,
616 arp_key
->arp_sip
, is_mask
);
617 SW_FLOW_KEY_PUT(match
, ipv4
.addr
.dst
,
618 arp_key
->arp_tip
, is_mask
);
619 SW_FLOW_KEY_PUT(match
, ip
.proto
,
620 ntohs(arp_key
->arp_op
), is_mask
);
621 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.sha
,
622 arp_key
->arp_sha
, ETH_ALEN
, is_mask
);
623 SW_FLOW_KEY_MEMCPY(match
, ipv4
.arp
.tha
,
624 arp_key
->arp_tha
, ETH_ALEN
, is_mask
);
626 attrs
&= ~(1 << OVS_KEY_ATTR_ARP
);
629 if (attrs
& (1 << OVS_KEY_ATTR_TCP
)) {
630 const struct ovs_key_tcp
*tcp_key
;
632 tcp_key
= nla_data(a
[OVS_KEY_ATTR_TCP
]);
633 SW_FLOW_KEY_PUT(match
, tp
.src
, tcp_key
->tcp_src
, is_mask
);
634 SW_FLOW_KEY_PUT(match
, tp
.dst
, tcp_key
->tcp_dst
, is_mask
);
635 attrs
&= ~(1 << OVS_KEY_ATTR_TCP
);
638 if (attrs
& (1 << OVS_KEY_ATTR_TCP_FLAGS
)) {
639 if (orig_attrs
& (1 << OVS_KEY_ATTR_IPV4
)) {
640 SW_FLOW_KEY_PUT(match
, tp
.flags
,
641 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
644 SW_FLOW_KEY_PUT(match
, tp
.flags
,
645 nla_get_be16(a
[OVS_KEY_ATTR_TCP_FLAGS
]),
648 attrs
&= ~(1 << OVS_KEY_ATTR_TCP_FLAGS
);
651 if (attrs
& (1 << OVS_KEY_ATTR_UDP
)) {
652 const struct ovs_key_udp
*udp_key
;
654 udp_key
= nla_data(a
[OVS_KEY_ATTR_UDP
]);
655 SW_FLOW_KEY_PUT(match
, tp
.src
, udp_key
->udp_src
, is_mask
);
656 SW_FLOW_KEY_PUT(match
, tp
.dst
, udp_key
->udp_dst
, is_mask
);
657 attrs
&= ~(1 << OVS_KEY_ATTR_UDP
);
660 if (attrs
& (1 << OVS_KEY_ATTR_SCTP
)) {
661 const struct ovs_key_sctp
*sctp_key
;
663 sctp_key
= nla_data(a
[OVS_KEY_ATTR_SCTP
]);
664 SW_FLOW_KEY_PUT(match
, tp
.src
, sctp_key
->sctp_src
, is_mask
);
665 SW_FLOW_KEY_PUT(match
, tp
.dst
, sctp_key
->sctp_dst
, is_mask
);
666 attrs
&= ~(1 << OVS_KEY_ATTR_SCTP
);
669 if (attrs
& (1 << OVS_KEY_ATTR_ICMP
)) {
670 const struct ovs_key_icmp
*icmp_key
;
672 icmp_key
= nla_data(a
[OVS_KEY_ATTR_ICMP
]);
673 SW_FLOW_KEY_PUT(match
, tp
.src
,
674 htons(icmp_key
->icmp_type
), is_mask
);
675 SW_FLOW_KEY_PUT(match
, tp
.dst
,
676 htons(icmp_key
->icmp_code
), is_mask
);
677 attrs
&= ~(1 << OVS_KEY_ATTR_ICMP
);
680 if (attrs
& (1 << OVS_KEY_ATTR_ICMPV6
)) {
681 const struct ovs_key_icmpv6
*icmpv6_key
;
683 icmpv6_key
= nla_data(a
[OVS_KEY_ATTR_ICMPV6
]);
684 SW_FLOW_KEY_PUT(match
, tp
.src
,
685 htons(icmpv6_key
->icmpv6_type
), is_mask
);
686 SW_FLOW_KEY_PUT(match
, tp
.dst
,
687 htons(icmpv6_key
->icmpv6_code
), is_mask
);
688 attrs
&= ~(1 << OVS_KEY_ATTR_ICMPV6
);
691 if (attrs
& (1 << OVS_KEY_ATTR_ND
)) {
692 const struct ovs_key_nd
*nd_key
;
694 nd_key
= nla_data(a
[OVS_KEY_ATTR_ND
]);
695 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.target
,
697 sizeof(match
->key
->ipv6
.nd
.target
),
699 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.sll
,
700 nd_key
->nd_sll
, ETH_ALEN
, is_mask
);
701 SW_FLOW_KEY_MEMCPY(match
, ipv6
.nd
.tll
,
702 nd_key
->nd_tll
, ETH_ALEN
, is_mask
);
703 attrs
&= ~(1 << OVS_KEY_ATTR_ND
);
712 static void sw_flow_mask_set(struct sw_flow_mask
*mask
,
713 struct sw_flow_key_range
*range
, u8 val
)
715 u8
*m
= (u8
*)&mask
->key
+ range
->start
;
717 mask
->range
= *range
;
718 memset(m
, val
, range_n_bytes(range
));
722 * ovs_nla_get_match - parses Netlink attributes into a flow key and
723 * mask. In case the 'mask' is NULL, the flow is treated as exact match
724 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
725 * does not include any don't care bit.
726 * @match: receives the extracted flow match information.
727 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
728 * sequence. The fields should of the packet that triggered the creation
730 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
731 * attribute specifies the mask field of the wildcarded flow.
733 int ovs_nla_get_match(struct sw_flow_match
*match
,
734 const struct nlattr
*key
,
735 const struct nlattr
*mask
)
737 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
738 const struct nlattr
*encap
;
741 bool encap_valid
= false;
744 err
= parse_flow_nlattrs(key
, a
, &key_attrs
);
748 if ((key_attrs
& (1 << OVS_KEY_ATTR_ETHERNET
)) &&
749 (key_attrs
& (1 << OVS_KEY_ATTR_ETHERTYPE
)) &&
750 (nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]) == htons(ETH_P_8021Q
))) {
753 if (!((key_attrs
& (1 << OVS_KEY_ATTR_VLAN
)) &&
754 (key_attrs
& (1 << OVS_KEY_ATTR_ENCAP
)))) {
755 OVS_NLERR("Invalid Vlan frame.\n");
759 key_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
760 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
761 encap
= a
[OVS_KEY_ATTR_ENCAP
];
762 key_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
765 if (tci
& htons(VLAN_TAG_PRESENT
)) {
766 err
= parse_flow_nlattrs(encap
, a
, &key_attrs
);
770 /* Corner case for truncated 802.1Q header. */
771 if (nla_len(encap
)) {
772 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
776 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
781 err
= ovs_key_from_nlattrs(match
, key_attrs
, a
, false);
786 err
= parse_flow_mask_nlattrs(mask
, a
, &mask_attrs
);
790 if (mask_attrs
& 1 << OVS_KEY_ATTR_ENCAP
) {
795 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
799 mask_attrs
&= ~(1 << OVS_KEY_ATTR_ENCAP
);
800 if (a
[OVS_KEY_ATTR_ETHERTYPE
])
801 eth_type
= nla_get_be16(a
[OVS_KEY_ATTR_ETHERTYPE
]);
803 if (eth_type
== htons(0xffff)) {
804 mask_attrs
&= ~(1 << OVS_KEY_ATTR_ETHERTYPE
);
805 encap
= a
[OVS_KEY_ATTR_ENCAP
];
806 err
= parse_flow_mask_nlattrs(encap
, a
, &mask_attrs
);
808 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
813 if (a
[OVS_KEY_ATTR_VLAN
])
814 tci
= nla_get_be16(a
[OVS_KEY_ATTR_VLAN
]);
816 if (!(tci
& htons(VLAN_TAG_PRESENT
))) {
817 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci
));
822 err
= ovs_key_from_nlattrs(match
, mask_attrs
, a
, true);
826 /* Populate exact match flow's key mask. */
828 sw_flow_mask_set(match
->mask
, &match
->range
, 0xff);
831 if (!match_validate(match
, key_attrs
, mask_attrs
))
838 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
839 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
840 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
843 * This parses a series of Netlink attributes that form a flow key, which must
844 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
845 * get the metadata, that is, the parts of the flow key that cannot be
846 * extracted from the packet itself.
849 int ovs_nla_get_flow_metadata(struct sw_flow
*flow
,
850 const struct nlattr
*attr
)
852 struct ovs_key_ipv4_tunnel
*tun_key
= &flow
->key
.tun_key
;
853 const struct nlattr
*a
[OVS_KEY_ATTR_MAX
+ 1];
856 struct sw_flow_match match
;
858 flow
->key
.phy
.in_port
= DP_MAX_PORTS
;
859 flow
->key
.phy
.priority
= 0;
860 flow
->key
.phy
.skb_mark
= 0;
861 memset(tun_key
, 0, sizeof(flow
->key
.tun_key
));
863 err
= parse_flow_nlattrs(attr
, a
, &attrs
);
867 memset(&match
, 0, sizeof(match
));
868 match
.key
= &flow
->key
;
870 err
= metadata_from_nlattrs(&match
, &attrs
, a
, false);
877 int ovs_nla_put_flow(const struct sw_flow_key
*swkey
,
878 const struct sw_flow_key
*output
, struct sk_buff
*skb
)
880 struct ovs_key_ethernet
*eth_key
;
881 struct nlattr
*nla
, *encap
;
882 bool is_mask
= (swkey
!= output
);
884 if (nla_put_u32(skb
, OVS_KEY_ATTR_PRIORITY
, output
->phy
.priority
))
885 goto nla_put_failure
;
887 if ((swkey
->tun_key
.ipv4_dst
|| is_mask
) &&
888 ipv4_tun_to_nlattr(skb
, &swkey
->tun_key
, &output
->tun_key
))
889 goto nla_put_failure
;
891 if (swkey
->phy
.in_port
== DP_MAX_PORTS
) {
892 if (is_mask
&& (output
->phy
.in_port
== 0xffff))
893 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
, 0xffffffff))
894 goto nla_put_failure
;
897 upper_u16
= !is_mask
? 0 : 0xffff;
899 if (nla_put_u32(skb
, OVS_KEY_ATTR_IN_PORT
,
900 (upper_u16
<< 16) | output
->phy
.in_port
))
901 goto nla_put_failure
;
904 if (nla_put_u32(skb
, OVS_KEY_ATTR_SKB_MARK
, output
->phy
.skb_mark
))
905 goto nla_put_failure
;
907 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ETHERNET
, sizeof(*eth_key
));
909 goto nla_put_failure
;
911 eth_key
= nla_data(nla
);
912 ether_addr_copy(eth_key
->eth_src
, output
->eth
.src
);
913 ether_addr_copy(eth_key
->eth_dst
, output
->eth
.dst
);
915 if (swkey
->eth
.tci
|| swkey
->eth
.type
== htons(ETH_P_8021Q
)) {
917 eth_type
= !is_mask
? htons(ETH_P_8021Q
) : htons(0xffff);
918 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, eth_type
) ||
919 nla_put_be16(skb
, OVS_KEY_ATTR_VLAN
, output
->eth
.tci
))
920 goto nla_put_failure
;
921 encap
= nla_nest_start(skb
, OVS_KEY_ATTR_ENCAP
);
927 if (swkey
->eth
.type
== htons(ETH_P_802_2
)) {
929 * Ethertype 802.2 is represented in the netlink with omitted
930 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
931 * 0xffff in the mask attribute. Ethertype can also
934 if (is_mask
&& output
->eth
.type
)
935 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
,
937 goto nla_put_failure
;
941 if (nla_put_be16(skb
, OVS_KEY_ATTR_ETHERTYPE
, output
->eth
.type
))
942 goto nla_put_failure
;
944 if (swkey
->eth
.type
== htons(ETH_P_IP
)) {
945 struct ovs_key_ipv4
*ipv4_key
;
947 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV4
, sizeof(*ipv4_key
));
949 goto nla_put_failure
;
950 ipv4_key
= nla_data(nla
);
951 ipv4_key
->ipv4_src
= output
->ipv4
.addr
.src
;
952 ipv4_key
->ipv4_dst
= output
->ipv4
.addr
.dst
;
953 ipv4_key
->ipv4_proto
= output
->ip
.proto
;
954 ipv4_key
->ipv4_tos
= output
->ip
.tos
;
955 ipv4_key
->ipv4_ttl
= output
->ip
.ttl
;
956 ipv4_key
->ipv4_frag
= output
->ip
.frag
;
957 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
)) {
958 struct ovs_key_ipv6
*ipv6_key
;
960 nla
= nla_reserve(skb
, OVS_KEY_ATTR_IPV6
, sizeof(*ipv6_key
));
962 goto nla_put_failure
;
963 ipv6_key
= nla_data(nla
);
964 memcpy(ipv6_key
->ipv6_src
, &output
->ipv6
.addr
.src
,
965 sizeof(ipv6_key
->ipv6_src
));
966 memcpy(ipv6_key
->ipv6_dst
, &output
->ipv6
.addr
.dst
,
967 sizeof(ipv6_key
->ipv6_dst
));
968 ipv6_key
->ipv6_label
= output
->ipv6
.label
;
969 ipv6_key
->ipv6_proto
= output
->ip
.proto
;
970 ipv6_key
->ipv6_tclass
= output
->ip
.tos
;
971 ipv6_key
->ipv6_hlimit
= output
->ip
.ttl
;
972 ipv6_key
->ipv6_frag
= output
->ip
.frag
;
973 } else if (swkey
->eth
.type
== htons(ETH_P_ARP
) ||
974 swkey
->eth
.type
== htons(ETH_P_RARP
)) {
975 struct ovs_key_arp
*arp_key
;
977 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ARP
, sizeof(*arp_key
));
979 goto nla_put_failure
;
980 arp_key
= nla_data(nla
);
981 memset(arp_key
, 0, sizeof(struct ovs_key_arp
));
982 arp_key
->arp_sip
= output
->ipv4
.addr
.src
;
983 arp_key
->arp_tip
= output
->ipv4
.addr
.dst
;
984 arp_key
->arp_op
= htons(output
->ip
.proto
);
985 ether_addr_copy(arp_key
->arp_sha
, output
->ipv4
.arp
.sha
);
986 ether_addr_copy(arp_key
->arp_tha
, output
->ipv4
.arp
.tha
);
989 if ((swkey
->eth
.type
== htons(ETH_P_IP
) ||
990 swkey
->eth
.type
== htons(ETH_P_IPV6
)) &&
991 swkey
->ip
.frag
!= OVS_FRAG_TYPE_LATER
) {
993 if (swkey
->ip
.proto
== IPPROTO_TCP
) {
994 struct ovs_key_tcp
*tcp_key
;
996 nla
= nla_reserve(skb
, OVS_KEY_ATTR_TCP
, sizeof(*tcp_key
));
998 goto nla_put_failure
;
999 tcp_key
= nla_data(nla
);
1000 tcp_key
->tcp_src
= output
->tp
.src
;
1001 tcp_key
->tcp_dst
= output
->tp
.dst
;
1002 if (nla_put_be16(skb
, OVS_KEY_ATTR_TCP_FLAGS
,
1004 goto nla_put_failure
;
1005 } else if (swkey
->ip
.proto
== IPPROTO_UDP
) {
1006 struct ovs_key_udp
*udp_key
;
1008 nla
= nla_reserve(skb
, OVS_KEY_ATTR_UDP
, sizeof(*udp_key
));
1010 goto nla_put_failure
;
1011 udp_key
= nla_data(nla
);
1012 udp_key
->udp_src
= output
->tp
.src
;
1013 udp_key
->udp_dst
= output
->tp
.dst
;
1014 } else if (swkey
->ip
.proto
== IPPROTO_SCTP
) {
1015 struct ovs_key_sctp
*sctp_key
;
1017 nla
= nla_reserve(skb
, OVS_KEY_ATTR_SCTP
, sizeof(*sctp_key
));
1019 goto nla_put_failure
;
1020 sctp_key
= nla_data(nla
);
1021 sctp_key
->sctp_src
= output
->tp
.src
;
1022 sctp_key
->sctp_dst
= output
->tp
.dst
;
1023 } else if (swkey
->eth
.type
== htons(ETH_P_IP
) &&
1024 swkey
->ip
.proto
== IPPROTO_ICMP
) {
1025 struct ovs_key_icmp
*icmp_key
;
1027 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMP
, sizeof(*icmp_key
));
1029 goto nla_put_failure
;
1030 icmp_key
= nla_data(nla
);
1031 icmp_key
->icmp_type
= ntohs(output
->tp
.src
);
1032 icmp_key
->icmp_code
= ntohs(output
->tp
.dst
);
1033 } else if (swkey
->eth
.type
== htons(ETH_P_IPV6
) &&
1034 swkey
->ip
.proto
== IPPROTO_ICMPV6
) {
1035 struct ovs_key_icmpv6
*icmpv6_key
;
1037 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ICMPV6
,
1038 sizeof(*icmpv6_key
));
1040 goto nla_put_failure
;
1041 icmpv6_key
= nla_data(nla
);
1042 icmpv6_key
->icmpv6_type
= ntohs(output
->tp
.src
);
1043 icmpv6_key
->icmpv6_code
= ntohs(output
->tp
.dst
);
1045 if (icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_SOLICITATION
||
1046 icmpv6_key
->icmpv6_type
== NDISC_NEIGHBOUR_ADVERTISEMENT
) {
1047 struct ovs_key_nd
*nd_key
;
1049 nla
= nla_reserve(skb
, OVS_KEY_ATTR_ND
, sizeof(*nd_key
));
1051 goto nla_put_failure
;
1052 nd_key
= nla_data(nla
);
1053 memcpy(nd_key
->nd_target
, &output
->ipv6
.nd
.target
,
1054 sizeof(nd_key
->nd_target
));
1055 ether_addr_copy(nd_key
->nd_sll
, output
->ipv6
.nd
.sll
);
1056 ether_addr_copy(nd_key
->nd_tll
, output
->ipv6
.nd
.tll
);
1063 nla_nest_end(skb
, encap
);
1071 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1073 struct sw_flow_actions
*ovs_nla_alloc_flow_actions(int size
)
1075 struct sw_flow_actions
*sfa
;
1077 if (size
> MAX_ACTIONS_BUFSIZE
)
1078 return ERR_PTR(-EINVAL
);
1080 sfa
= kmalloc(sizeof(*sfa
) + size
, GFP_KERNEL
);
1082 return ERR_PTR(-ENOMEM
);
1084 sfa
->actions_len
= 0;
1088 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1089 * The caller must hold rcu_read_lock for this to be sensible. */
1090 void ovs_nla_free_flow_actions(struct sw_flow_actions
*sf_acts
)
1092 kfree_rcu(sf_acts
, rcu
);
1095 static struct nlattr
*reserve_sfa_size(struct sw_flow_actions
**sfa
,
1099 struct sw_flow_actions
*acts
;
1101 int req_size
= NLA_ALIGN(attr_len
);
1102 int next_offset
= offsetof(struct sw_flow_actions
, actions
) +
1103 (*sfa
)->actions_len
;
1105 if (req_size
<= (ksize(*sfa
) - next_offset
))
1108 new_acts_size
= ksize(*sfa
) * 2;
1110 if (new_acts_size
> MAX_ACTIONS_BUFSIZE
) {
1111 if ((MAX_ACTIONS_BUFSIZE
- next_offset
) < req_size
)
1112 return ERR_PTR(-EMSGSIZE
);
1113 new_acts_size
= MAX_ACTIONS_BUFSIZE
;
1116 acts
= ovs_nla_alloc_flow_actions(new_acts_size
);
1118 return (void *)acts
;
1120 memcpy(acts
->actions
, (*sfa
)->actions
, (*sfa
)->actions_len
);
1121 acts
->actions_len
= (*sfa
)->actions_len
;
1126 (*sfa
)->actions_len
+= req_size
;
1127 return (struct nlattr
*) ((unsigned char *)(*sfa
) + next_offset
);
1130 static int add_action(struct sw_flow_actions
**sfa
, int attrtype
, void *data
, int len
)
1134 a
= reserve_sfa_size(sfa
, nla_attr_size(len
));
1138 a
->nla_type
= attrtype
;
1139 a
->nla_len
= nla_attr_size(len
);
1142 memcpy(nla_data(a
), data
, len
);
1143 memset((unsigned char *) a
+ a
->nla_len
, 0, nla_padlen(len
));
1148 static inline int add_nested_action_start(struct sw_flow_actions
**sfa
,
1151 int used
= (*sfa
)->actions_len
;
1154 err
= add_action(sfa
, attrtype
, NULL
, 0);
1161 static inline void add_nested_action_end(struct sw_flow_actions
*sfa
,
1164 struct nlattr
*a
= (struct nlattr
*) ((unsigned char *)sfa
->actions
+
1167 a
->nla_len
= sfa
->actions_len
- st_offset
;
1170 static int validate_and_copy_sample(const struct nlattr
*attr
,
1171 const struct sw_flow_key
*key
, int depth
,
1172 struct sw_flow_actions
**sfa
)
1174 const struct nlattr
*attrs
[OVS_SAMPLE_ATTR_MAX
+ 1];
1175 const struct nlattr
*probability
, *actions
;
1176 const struct nlattr
*a
;
1177 int rem
, start
, err
, st_acts
;
1179 memset(attrs
, 0, sizeof(attrs
));
1180 nla_for_each_nested(a
, attr
, rem
) {
1181 int type
= nla_type(a
);
1182 if (!type
|| type
> OVS_SAMPLE_ATTR_MAX
|| attrs
[type
])
1189 probability
= attrs
[OVS_SAMPLE_ATTR_PROBABILITY
];
1190 if (!probability
|| nla_len(probability
) != sizeof(u32
))
1193 actions
= attrs
[OVS_SAMPLE_ATTR_ACTIONS
];
1194 if (!actions
|| (nla_len(actions
) && nla_len(actions
) < NLA_HDRLEN
))
1197 /* validation done, copy sample action. */
1198 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SAMPLE
);
1201 err
= add_action(sfa
, OVS_SAMPLE_ATTR_PROBABILITY
,
1202 nla_data(probability
), sizeof(u32
));
1205 st_acts
= add_nested_action_start(sfa
, OVS_SAMPLE_ATTR_ACTIONS
);
1209 err
= ovs_nla_copy_actions(actions
, key
, depth
+ 1, sfa
);
1213 add_nested_action_end(*sfa
, st_acts
);
1214 add_nested_action_end(*sfa
, start
);
1219 static int validate_tp_port(const struct sw_flow_key
*flow_key
)
1221 if ((flow_key
->eth
.type
== htons(ETH_P_IP
) ||
1222 flow_key
->eth
.type
== htons(ETH_P_IPV6
)) &&
1223 (flow_key
->tp
.src
|| flow_key
->tp
.dst
))
1229 void ovs_match_init(struct sw_flow_match
*match
,
1230 struct sw_flow_key
*key
,
1231 struct sw_flow_mask
*mask
)
1233 memset(match
, 0, sizeof(*match
));
1237 memset(key
, 0, sizeof(*key
));
1240 memset(&mask
->key
, 0, sizeof(mask
->key
));
1241 mask
->range
.start
= mask
->range
.end
= 0;
1245 static int validate_and_copy_set_tun(const struct nlattr
*attr
,
1246 struct sw_flow_actions
**sfa
)
1248 struct sw_flow_match match
;
1249 struct sw_flow_key key
;
1252 ovs_match_init(&match
, &key
, NULL
);
1253 err
= ipv4_tun_from_nlattr(nla_data(attr
), &match
, false);
1257 start
= add_nested_action_start(sfa
, OVS_ACTION_ATTR_SET
);
1261 err
= add_action(sfa
, OVS_KEY_ATTR_IPV4_TUNNEL
, &match
.key
->tun_key
,
1262 sizeof(match
.key
->tun_key
));
1263 add_nested_action_end(*sfa
, start
);
1268 static int validate_set(const struct nlattr
*a
,
1269 const struct sw_flow_key
*flow_key
,
1270 struct sw_flow_actions
**sfa
,
1273 const struct nlattr
*ovs_key
= nla_data(a
);
1274 int key_type
= nla_type(ovs_key
);
1276 /* There can be only one key in a action */
1277 if (nla_total_size(nla_len(ovs_key
)) != nla_len(a
))
1280 if (key_type
> OVS_KEY_ATTR_MAX
||
1281 (ovs_key_lens
[key_type
] != nla_len(ovs_key
) &&
1282 ovs_key_lens
[key_type
] != -1))
1286 const struct ovs_key_ipv4
*ipv4_key
;
1287 const struct ovs_key_ipv6
*ipv6_key
;
1290 case OVS_KEY_ATTR_PRIORITY
:
1291 case OVS_KEY_ATTR_SKB_MARK
:
1292 case OVS_KEY_ATTR_ETHERNET
:
1295 case OVS_KEY_ATTR_TUNNEL
:
1297 err
= validate_and_copy_set_tun(a
, sfa
);
1302 case OVS_KEY_ATTR_IPV4
:
1303 if (flow_key
->eth
.type
!= htons(ETH_P_IP
))
1306 if (!flow_key
->ip
.proto
)
1309 ipv4_key
= nla_data(ovs_key
);
1310 if (ipv4_key
->ipv4_proto
!= flow_key
->ip
.proto
)
1313 if (ipv4_key
->ipv4_frag
!= flow_key
->ip
.frag
)
1318 case OVS_KEY_ATTR_IPV6
:
1319 if (flow_key
->eth
.type
!= htons(ETH_P_IPV6
))
1322 if (!flow_key
->ip
.proto
)
1325 ipv6_key
= nla_data(ovs_key
);
1326 if (ipv6_key
->ipv6_proto
!= flow_key
->ip
.proto
)
1329 if (ipv6_key
->ipv6_frag
!= flow_key
->ip
.frag
)
1332 if (ntohl(ipv6_key
->ipv6_label
) & 0xFFF00000)
1337 case OVS_KEY_ATTR_TCP
:
1338 if (flow_key
->ip
.proto
!= IPPROTO_TCP
)
1341 return validate_tp_port(flow_key
);
1343 case OVS_KEY_ATTR_UDP
:
1344 if (flow_key
->ip
.proto
!= IPPROTO_UDP
)
1347 return validate_tp_port(flow_key
);
1349 case OVS_KEY_ATTR_SCTP
:
1350 if (flow_key
->ip
.proto
!= IPPROTO_SCTP
)
1353 return validate_tp_port(flow_key
);
1362 static int validate_userspace(const struct nlattr
*attr
)
1364 static const struct nla_policy userspace_policy
[OVS_USERSPACE_ATTR_MAX
+ 1] = {
1365 [OVS_USERSPACE_ATTR_PID
] = {.type
= NLA_U32
},
1366 [OVS_USERSPACE_ATTR_USERDATA
] = {.type
= NLA_UNSPEC
},
1368 struct nlattr
*a
[OVS_USERSPACE_ATTR_MAX
+ 1];
1371 error
= nla_parse_nested(a
, OVS_USERSPACE_ATTR_MAX
,
1372 attr
, userspace_policy
);
1376 if (!a
[OVS_USERSPACE_ATTR_PID
] ||
1377 !nla_get_u32(a
[OVS_USERSPACE_ATTR_PID
]))
1383 static int copy_action(const struct nlattr
*from
,
1384 struct sw_flow_actions
**sfa
)
1386 int totlen
= NLA_ALIGN(from
->nla_len
);
1389 to
= reserve_sfa_size(sfa
, from
->nla_len
);
1393 memcpy(to
, from
, totlen
);
1397 int ovs_nla_copy_actions(const struct nlattr
*attr
,
1398 const struct sw_flow_key
*key
,
1400 struct sw_flow_actions
**sfa
)
1402 const struct nlattr
*a
;
1405 if (depth
>= SAMPLE_ACTION_DEPTH
)
1408 nla_for_each_nested(a
, attr
, rem
) {
1409 /* Expected argument lengths, (u32)-1 for variable length. */
1410 static const u32 action_lens
[OVS_ACTION_ATTR_MAX
+ 1] = {
1411 [OVS_ACTION_ATTR_OUTPUT
] = sizeof(u32
),
1412 [OVS_ACTION_ATTR_USERSPACE
] = (u32
)-1,
1413 [OVS_ACTION_ATTR_PUSH_VLAN
] = sizeof(struct ovs_action_push_vlan
),
1414 [OVS_ACTION_ATTR_POP_VLAN
] = 0,
1415 [OVS_ACTION_ATTR_SET
] = (u32
)-1,
1416 [OVS_ACTION_ATTR_SAMPLE
] = (u32
)-1
1418 const struct ovs_action_push_vlan
*vlan
;
1419 int type
= nla_type(a
);
1422 if (type
> OVS_ACTION_ATTR_MAX
||
1423 (action_lens
[type
] != nla_len(a
) &&
1424 action_lens
[type
] != (u32
)-1))
1429 case OVS_ACTION_ATTR_UNSPEC
:
1432 case OVS_ACTION_ATTR_USERSPACE
:
1433 err
= validate_userspace(a
);
1438 case OVS_ACTION_ATTR_OUTPUT
:
1439 if (nla_get_u32(a
) >= DP_MAX_PORTS
)
1444 case OVS_ACTION_ATTR_POP_VLAN
:
1447 case OVS_ACTION_ATTR_PUSH_VLAN
:
1449 if (vlan
->vlan_tpid
!= htons(ETH_P_8021Q
))
1451 if (!(vlan
->vlan_tci
& htons(VLAN_TAG_PRESENT
)))
1455 case OVS_ACTION_ATTR_SET
:
1456 err
= validate_set(a
, key
, sfa
, &skip_copy
);
1461 case OVS_ACTION_ATTR_SAMPLE
:
1462 err
= validate_and_copy_sample(a
, key
, depth
, sfa
);
1472 err
= copy_action(a
, sfa
);
1484 static int sample_action_to_attr(const struct nlattr
*attr
, struct sk_buff
*skb
)
1486 const struct nlattr
*a
;
1487 struct nlattr
*start
;
1490 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SAMPLE
);
1494 nla_for_each_nested(a
, attr
, rem
) {
1495 int type
= nla_type(a
);
1496 struct nlattr
*st_sample
;
1499 case OVS_SAMPLE_ATTR_PROBABILITY
:
1500 if (nla_put(skb
, OVS_SAMPLE_ATTR_PROBABILITY
,
1501 sizeof(u32
), nla_data(a
)))
1504 case OVS_SAMPLE_ATTR_ACTIONS
:
1505 st_sample
= nla_nest_start(skb
, OVS_SAMPLE_ATTR_ACTIONS
);
1508 err
= ovs_nla_put_actions(nla_data(a
), nla_len(a
), skb
);
1511 nla_nest_end(skb
, st_sample
);
1516 nla_nest_end(skb
, start
);
1520 static int set_action_to_attr(const struct nlattr
*a
, struct sk_buff
*skb
)
1522 const struct nlattr
*ovs_key
= nla_data(a
);
1523 int key_type
= nla_type(ovs_key
);
1524 struct nlattr
*start
;
1528 case OVS_KEY_ATTR_IPV4_TUNNEL
:
1529 start
= nla_nest_start(skb
, OVS_ACTION_ATTR_SET
);
1533 err
= ipv4_tun_to_nlattr(skb
, nla_data(ovs_key
),
1537 nla_nest_end(skb
, start
);
1540 if (nla_put(skb
, OVS_ACTION_ATTR_SET
, nla_len(a
), ovs_key
))
1548 int ovs_nla_put_actions(const struct nlattr
*attr
, int len
, struct sk_buff
*skb
)
1550 const struct nlattr
*a
;
1553 nla_for_each_attr(a
, attr
, len
, rem
) {
1554 int type
= nla_type(a
);
1557 case OVS_ACTION_ATTR_SET
:
1558 err
= set_action_to_attr(a
, skb
);
1563 case OVS_ACTION_ATTR_SAMPLE
:
1564 err
= sample_action_to_attr(a
, skb
);
1569 if (nla_put(skb
, type
, nla_len(a
), nla_data(a
)))