]>
Commit | Line | Data |
---|---|---|
a097c0b2 PS |
1 | /* |
2 | * Copyright (c) 2007-2013 Nicira, Inc. | |
3 | * | |
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. | |
7 | * | |
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. | |
12 | * | |
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 | |
16 | * 02110-1301, USA | |
17 | */ | |
18 | ||
0a0857df JP |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
a097c0b2 PS |
21 | #include "flow.h" |
22 | #include "datapath.h" | |
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> | |
34 | #include <linux/in.h> | |
35 | #include <linux/rcupdate.h> | |
36 | #include <linux/if_arp.h> | |
37 | #include <linux/ip.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/ip.h> | |
29c71cfa | 46 | #include <net/ip_tunnels.h> |
a097c0b2 PS |
47 | #include <net/ipv6.h> |
48 | #include <net/ndisc.h> | |
49 | ||
50 | #include "flow_netlink.h" | |
51 | ||
52 | static void update_range__(struct sw_flow_match *match, | |
53 | size_t offset, size_t size, bool is_mask) | |
54 | { | |
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)); | |
58 | ||
59 | if (!is_mask) | |
60 | range = &match->range; | |
61 | else if (match->mask) | |
62 | range = &match->mask->range; | |
63 | ||
64 | if (!range) | |
65 | return; | |
66 | ||
67 | if (range->start == range->end) { | |
68 | range->start = start; | |
69 | range->end = end; | |
70 | return; | |
71 | } | |
72 | ||
73 | if (range->start > start) | |
74 | range->start = start; | |
75 | ||
76 | if (range->end < end) | |
77 | range->end = end; | |
78 | } | |
79 | ||
80 | #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \ | |
81 | do { \ | |
82 | update_range__(match, offsetof(struct sw_flow_key, field), \ | |
83 | sizeof((match)->key->field), is_mask); \ | |
84 | if (is_mask) { \ | |
85 | if ((match)->mask) \ | |
86 | (match)->mask->key.field = value; \ | |
87 | } else { \ | |
88 | (match)->key->field = value; \ | |
89 | } \ | |
90 | } while (0) | |
91 | ||
92 | #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \ | |
93 | do { \ | |
94 | update_range__(match, offsetof(struct sw_flow_key, field), \ | |
95 | len, is_mask); \ | |
96 | if (is_mask) { \ | |
97 | if ((match)->mask) \ | |
98 | memcpy(&(match)->mask->key.field, value_p, len);\ | |
99 | } else { \ | |
100 | memcpy(&(match)->key->field, value_p, len); \ | |
101 | } \ | |
102 | } while (0) | |
103 | ||
104 | static u16 range_n_bytes(const struct sw_flow_key_range *range) | |
105 | { | |
106 | return range->end - range->start; | |
107 | } | |
108 | ||
109 | static bool match_validate(const struct sw_flow_match *match, | |
110 | u64 key_attrs, u64 mask_attrs) | |
111 | { | |
112 | u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET; | |
113 | u64 mask_allowed = key_attrs; /* At most allow all key attributes */ | |
114 | ||
115 | /* The following mask attributes allowed only if they | |
116 | * pass the validation tests. */ | |
117 | mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4) | |
118 | | (1ULL << OVS_KEY_ATTR_IPV6) | |
119 | | (1ULL << OVS_KEY_ATTR_TCP) | |
dc235f7f | 120 | | (1ULL << OVS_KEY_ATTR_TCP_FLAGS) |
a097c0b2 PS |
121 | | (1ULL << OVS_KEY_ATTR_UDP) |
122 | | (1ULL << OVS_KEY_ATTR_SCTP) | |
123 | | (1ULL << OVS_KEY_ATTR_ICMP) | |
124 | | (1ULL << OVS_KEY_ATTR_ICMPV6) | |
125 | | (1ULL << OVS_KEY_ATTR_ARP) | |
126 | | (1ULL << OVS_KEY_ATTR_ND)); | |
127 | ||
128 | /* Always allowed mask fields. */ | |
129 | mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL) | |
130 | | (1ULL << OVS_KEY_ATTR_IN_PORT) | |
131 | | (1ULL << OVS_KEY_ATTR_ETHERTYPE)); | |
132 | ||
133 | /* Check key attributes. */ | |
134 | if (match->key->eth.type == htons(ETH_P_ARP) | |
135 | || match->key->eth.type == htons(ETH_P_RARP)) { | |
136 | key_expected |= 1ULL << OVS_KEY_ATTR_ARP; | |
137 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
138 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP; | |
139 | } | |
140 | ||
141 | if (match->key->eth.type == htons(ETH_P_IP)) { | |
142 | key_expected |= 1ULL << OVS_KEY_ATTR_IPV4; | |
143 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
144 | mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4; | |
145 | ||
146 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { | |
147 | if (match->key->ip.proto == IPPROTO_UDP) { | |
148 | key_expected |= 1ULL << OVS_KEY_ATTR_UDP; | |
149 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
150 | mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP; | |
151 | } | |
152 | ||
153 | if (match->key->ip.proto == IPPROTO_SCTP) { | |
154 | key_expected |= 1ULL << OVS_KEY_ATTR_SCTP; | |
155 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
156 | mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP; | |
157 | } | |
158 | ||
159 | if (match->key->ip.proto == IPPROTO_TCP) { | |
160 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP; | |
dc235f7f JR |
161 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
162 | if (match->mask && (match->mask->key.ip.proto == 0xff)) { | |
a097c0b2 | 163 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP; |
dc235f7f JR |
164 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
165 | } | |
a097c0b2 PS |
166 | } |
167 | ||
168 | if (match->key->ip.proto == IPPROTO_ICMP) { | |
169 | key_expected |= 1ULL << OVS_KEY_ATTR_ICMP; | |
170 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
171 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP; | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | if (match->key->eth.type == htons(ETH_P_IPV6)) { | |
177 | key_expected |= 1ULL << OVS_KEY_ATTR_IPV6; | |
178 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
179 | mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6; | |
180 | ||
181 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { | |
182 | if (match->key->ip.proto == IPPROTO_UDP) { | |
183 | key_expected |= 1ULL << OVS_KEY_ATTR_UDP; | |
184 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
185 | mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP; | |
186 | } | |
187 | ||
188 | if (match->key->ip.proto == IPPROTO_SCTP) { | |
189 | key_expected |= 1ULL << OVS_KEY_ATTR_SCTP; | |
190 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
191 | mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP; | |
192 | } | |
193 | ||
194 | if (match->key->ip.proto == IPPROTO_TCP) { | |
195 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP; | |
dc235f7f JR |
196 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
197 | if (match->mask && (match->mask->key.ip.proto == 0xff)) { | |
a097c0b2 | 198 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP; |
dc235f7f JR |
199 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
200 | } | |
a097c0b2 PS |
201 | } |
202 | ||
203 | if (match->key->ip.proto == IPPROTO_ICMPV6) { | |
204 | key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6; | |
205 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
206 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6; | |
207 | ||
708fb4c5 | 208 | if (match->key->tp.src == |
a097c0b2 | 209 | htons(NDISC_NEIGHBOUR_SOLICITATION) || |
708fb4c5 | 210 | match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) { |
a097c0b2 | 211 | key_expected |= 1ULL << OVS_KEY_ATTR_ND; |
708fb4c5 | 212 | if (match->mask && (match->mask->key.tp.src == htons(0xffff))) |
a097c0b2 PS |
213 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ND; |
214 | } | |
215 | } | |
216 | } | |
217 | } | |
218 | ||
219 | if ((key_attrs & key_expected) != key_expected) { | |
220 | /* Key attributes check failed. */ | |
221 | OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n", | |
29dd5cb7 | 222 | (unsigned long long)key_attrs, (unsigned long long)key_expected); |
a097c0b2 PS |
223 | return false; |
224 | } | |
225 | ||
226 | if ((mask_attrs & mask_allowed) != mask_attrs) { | |
227 | /* Mask attributes check failed. */ | |
228 | OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n", | |
29dd5cb7 | 229 | (unsigned long long)mask_attrs, (unsigned long long)mask_allowed); |
a097c0b2 PS |
230 | return false; |
231 | } | |
232 | ||
233 | return true; | |
234 | } | |
235 | ||
236 | /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */ | |
237 | static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = { | |
238 | [OVS_KEY_ATTR_ENCAP] = -1, | |
239 | [OVS_KEY_ATTR_PRIORITY] = sizeof(u32), | |
240 | [OVS_KEY_ATTR_IN_PORT] = sizeof(u32), | |
241 | [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32), | |
242 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), | |
243 | [OVS_KEY_ATTR_VLAN] = sizeof(__be16), | |
244 | [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16), | |
245 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), | |
246 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), | |
247 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), | |
dc235f7f | 248 | [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16), |
a097c0b2 PS |
249 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), |
250 | [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp), | |
251 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), | |
252 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), | |
253 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), | |
254 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), | |
7804df20 | 255 | [OVS_KEY_ATTR_DP_HASH] = sizeof(u32), |
a6059080 | 256 | [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32), |
a097c0b2 PS |
257 | [OVS_KEY_ATTR_TUNNEL] = -1, |
258 | }; | |
259 | ||
260 | static bool is_all_zero(const u8 *fp, size_t size) | |
261 | { | |
262 | int i; | |
263 | ||
264 | if (!fp) | |
265 | return false; | |
266 | ||
267 | for (i = 0; i < size; i++) | |
268 | if (fp[i]) | |
269 | return false; | |
270 | ||
271 | return true; | |
272 | } | |
273 | ||
274 | static int __parse_flow_nlattrs(const struct nlattr *attr, | |
275 | const struct nlattr *a[], | |
276 | u64 *attrsp, bool nz) | |
277 | { | |
278 | const struct nlattr *nla; | |
279 | u64 attrs; | |
280 | int rem; | |
281 | ||
282 | attrs = *attrsp; | |
283 | nla_for_each_nested(nla, attr, rem) { | |
284 | u16 type = nla_type(nla); | |
285 | int expected_len; | |
286 | ||
287 | if (type > OVS_KEY_ATTR_MAX) { | |
288 | OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n", | |
289 | type, OVS_KEY_ATTR_MAX); | |
290 | return -EINVAL; | |
291 | } | |
292 | ||
293 | if (attrs & (1ULL << type)) { | |
294 | OVS_NLERR("Duplicate key attribute (type %d).\n", type); | |
295 | return -EINVAL; | |
296 | } | |
297 | ||
298 | expected_len = ovs_key_lens[type]; | |
299 | if (nla_len(nla) != expected_len && expected_len != -1) { | |
300 | OVS_NLERR("Key attribute has unexpected length (type=%d" | |
301 | ", length=%d, expected=%d).\n", type, | |
302 | nla_len(nla), expected_len); | |
303 | return -EINVAL; | |
304 | } | |
305 | ||
306 | if (!nz || !is_all_zero(nla_data(nla), expected_len)) { | |
307 | attrs |= 1ULL << type; | |
308 | a[type] = nla; | |
309 | } | |
310 | } | |
311 | if (rem) { | |
312 | OVS_NLERR("Message has %d unknown bytes.\n", rem); | |
313 | return -EINVAL; | |
314 | } | |
315 | ||
316 | *attrsp = attrs; | |
317 | return 0; | |
318 | } | |
319 | ||
320 | static int parse_flow_mask_nlattrs(const struct nlattr *attr, | |
321 | const struct nlattr *a[], u64 *attrsp) | |
322 | { | |
323 | return __parse_flow_nlattrs(attr, a, attrsp, true); | |
324 | } | |
325 | ||
326 | static int parse_flow_nlattrs(const struct nlattr *attr, | |
327 | const struct nlattr *a[], u64 *attrsp) | |
328 | { | |
329 | return __parse_flow_nlattrs(attr, a, attrsp, false); | |
330 | } | |
331 | ||
332 | static int ipv4_tun_from_nlattr(const struct nlattr *attr, | |
333 | struct sw_flow_match *match, bool is_mask) | |
334 | { | |
335 | struct nlattr *a; | |
336 | int rem; | |
337 | bool ttl = false; | |
338 | __be16 tun_flags = 0; | |
339 | ||
340 | nla_for_each_nested(a, attr, rem) { | |
341 | int type = nla_type(a); | |
342 | static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { | |
343 | [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64), | |
344 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32), | |
345 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32), | |
346 | [OVS_TUNNEL_KEY_ATTR_TOS] = 1, | |
347 | [OVS_TUNNEL_KEY_ATTR_TTL] = 1, | |
348 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0, | |
349 | [OVS_TUNNEL_KEY_ATTR_CSUM] = 0, | |
350 | }; | |
351 | ||
352 | if (type > OVS_TUNNEL_KEY_ATTR_MAX) { | |
353 | OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n", | |
354 | type, OVS_TUNNEL_KEY_ATTR_MAX); | |
355 | return -EINVAL; | |
356 | } | |
357 | ||
358 | if (ovs_tunnel_key_lens[type] != nla_len(a)) { | |
359 | OVS_NLERR("IPv4 tunnel attribute type has unexpected " | |
360 | " length (type=%d, length=%d, expected=%d).\n", | |
361 | type, nla_len(a), ovs_tunnel_key_lens[type]); | |
362 | return -EINVAL; | |
363 | } | |
364 | ||
365 | switch (type) { | |
366 | case OVS_TUNNEL_KEY_ATTR_ID: | |
367 | SW_FLOW_KEY_PUT(match, tun_key.tun_id, | |
368 | nla_get_be64(a), is_mask); | |
369 | tun_flags |= TUNNEL_KEY; | |
370 | break; | |
371 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
372 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_src, | |
373 | nla_get_be32(a), is_mask); | |
374 | break; | |
375 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
376 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst, | |
377 | nla_get_be32(a), is_mask); | |
378 | break; | |
379 | case OVS_TUNNEL_KEY_ATTR_TOS: | |
380 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos, | |
381 | nla_get_u8(a), is_mask); | |
382 | break; | |
383 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
384 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl, | |
385 | nla_get_u8(a), is_mask); | |
386 | ttl = true; | |
387 | break; | |
388 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
389 | tun_flags |= TUNNEL_DONT_FRAGMENT; | |
390 | break; | |
391 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
392 | tun_flags |= TUNNEL_CSUM; | |
393 | break; | |
394 | default: | |
395 | return -EINVAL; | |
396 | } | |
397 | } | |
398 | ||
399 | SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask); | |
400 | ||
401 | if (rem > 0) { | |
402 | OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem); | |
403 | return -EINVAL; | |
404 | } | |
405 | ||
406 | if (!is_mask) { | |
407 | if (!match->key->tun_key.ipv4_dst) { | |
408 | OVS_NLERR("IPv4 tunnel destination address is zero.\n"); | |
409 | return -EINVAL; | |
410 | } | |
411 | ||
412 | if (!ttl) { | |
413 | OVS_NLERR("IPv4 tunnel TTL not specified.\n"); | |
414 | return -EINVAL; | |
415 | } | |
416 | } | |
417 | ||
418 | return 0; | |
419 | } | |
420 | ||
421 | static int ipv4_tun_to_nlattr(struct sk_buff *skb, | |
422 | const struct ovs_key_ipv4_tunnel *tun_key, | |
423 | const struct ovs_key_ipv4_tunnel *output) | |
424 | { | |
425 | struct nlattr *nla; | |
426 | ||
427 | nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL); | |
428 | if (!nla) | |
429 | return -EMSGSIZE; | |
430 | ||
431 | if (output->tun_flags & TUNNEL_KEY && | |
432 | nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id)) | |
433 | return -EMSGSIZE; | |
434 | if (output->ipv4_src && | |
435 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src)) | |
436 | return -EMSGSIZE; | |
437 | if (output->ipv4_dst && | |
438 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst)) | |
439 | return -EMSGSIZE; | |
440 | if (output->ipv4_tos && | |
441 | nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos)) | |
442 | return -EMSGSIZE; | |
443 | if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl)) | |
444 | return -EMSGSIZE; | |
445 | if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) && | |
446 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT)) | |
447 | return -EMSGSIZE; | |
448 | if ((output->tun_flags & TUNNEL_CSUM) && | |
449 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM)) | |
450 | return -EMSGSIZE; | |
451 | ||
452 | nla_nest_end(skb, nla); | |
453 | return 0; | |
454 | } | |
455 | ||
456 | ||
457 | static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs, | |
458 | const struct nlattr **a, bool is_mask) | |
459 | { | |
7804df20 AZ |
460 | if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) { |
461 | u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]); | |
462 | ||
463 | SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask); | |
464 | *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH); | |
465 | } | |
466 | ||
a6059080 AZ |
467 | if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) { |
468 | u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]); | |
469 | ||
470 | SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask); | |
471 | *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID); | |
472 | } | |
473 | ||
a097c0b2 PS |
474 | if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) { |
475 | SW_FLOW_KEY_PUT(match, phy.priority, | |
476 | nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask); | |
477 | *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY); | |
478 | } | |
479 | ||
480 | if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) { | |
481 | u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]); | |
482 | ||
483 | if (is_mask) | |
484 | in_port = 0xffffffff; /* Always exact match in_port. */ | |
485 | else if (in_port >= DP_MAX_PORTS) | |
486 | return -EINVAL; | |
487 | ||
488 | SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask); | |
489 | *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT); | |
490 | } else if (!is_mask) { | |
491 | SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask); | |
492 | } | |
493 | ||
494 | if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) { | |
495 | uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]); | |
496 | ||
497 | SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask); | |
498 | *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK); | |
499 | } | |
500 | if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) { | |
501 | if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match, | |
502 | is_mask)) | |
503 | return -EINVAL; | |
504 | *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL); | |
505 | } | |
506 | return 0; | |
507 | } | |
508 | ||
df65fec1 JR |
509 | static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs, |
510 | const struct nlattr **a, bool is_mask) | |
a097c0b2 PS |
511 | { |
512 | int err; | |
513 | u64 orig_attrs = attrs; | |
514 | ||
515 | err = metadata_from_nlattrs(match, &attrs, a, is_mask); | |
516 | if (err) | |
517 | return err; | |
518 | ||
519 | if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) { | |
520 | const struct ovs_key_ethernet *eth_key; | |
521 | ||
522 | eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]); | |
523 | SW_FLOW_KEY_MEMCPY(match, eth.src, | |
524 | eth_key->eth_src, ETH_ALEN, is_mask); | |
525 | SW_FLOW_KEY_MEMCPY(match, eth.dst, | |
526 | eth_key->eth_dst, ETH_ALEN, is_mask); | |
527 | attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET); | |
528 | } | |
529 | ||
530 | if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) { | |
531 | __be16 tci; | |
532 | ||
533 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
534 | if (!(tci & htons(VLAN_TAG_PRESENT))) { | |
535 | if (is_mask) | |
536 | OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n"); | |
537 | else | |
538 | OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n"); | |
539 | ||
540 | return -EINVAL; | |
541 | } | |
542 | ||
543 | SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask); | |
544 | attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN); | |
545 | } else if (!is_mask) | |
546 | SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true); | |
547 | ||
548 | if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) { | |
549 | __be16 eth_type; | |
550 | ||
551 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
552 | if (is_mask) { | |
553 | /* Always exact match EtherType. */ | |
554 | eth_type = htons(0xffff); | |
555 | } else if (ntohs(eth_type) < ETH_P_802_3_MIN) { | |
556 | OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n", | |
557 | ntohs(eth_type), ETH_P_802_3_MIN); | |
558 | return -EINVAL; | |
559 | } | |
560 | ||
561 | SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask); | |
562 | attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
563 | } else if (!is_mask) { | |
564 | SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask); | |
565 | } | |
566 | ||
567 | if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) { | |
568 | const struct ovs_key_ipv4 *ipv4_key; | |
569 | ||
570 | ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]); | |
571 | if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) { | |
572 | OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n", | |
573 | ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX); | |
574 | return -EINVAL; | |
575 | } | |
576 | SW_FLOW_KEY_PUT(match, ip.proto, | |
577 | ipv4_key->ipv4_proto, is_mask); | |
578 | SW_FLOW_KEY_PUT(match, ip.tos, | |
579 | ipv4_key->ipv4_tos, is_mask); | |
580 | SW_FLOW_KEY_PUT(match, ip.ttl, | |
581 | ipv4_key->ipv4_ttl, is_mask); | |
582 | SW_FLOW_KEY_PUT(match, ip.frag, | |
583 | ipv4_key->ipv4_frag, is_mask); | |
584 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, | |
585 | ipv4_key->ipv4_src, is_mask); | |
586 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, | |
587 | ipv4_key->ipv4_dst, is_mask); | |
588 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4); | |
589 | } | |
590 | ||
591 | if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) { | |
592 | const struct ovs_key_ipv6 *ipv6_key; | |
593 | ||
594 | ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]); | |
595 | if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) { | |
596 | OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n", | |
597 | ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX); | |
598 | return -EINVAL; | |
599 | } | |
600 | SW_FLOW_KEY_PUT(match, ipv6.label, | |
601 | ipv6_key->ipv6_label, is_mask); | |
602 | SW_FLOW_KEY_PUT(match, ip.proto, | |
603 | ipv6_key->ipv6_proto, is_mask); | |
604 | SW_FLOW_KEY_PUT(match, ip.tos, | |
605 | ipv6_key->ipv6_tclass, is_mask); | |
606 | SW_FLOW_KEY_PUT(match, ip.ttl, | |
607 | ipv6_key->ipv6_hlimit, is_mask); | |
608 | SW_FLOW_KEY_PUT(match, ip.frag, | |
609 | ipv6_key->ipv6_frag, is_mask); | |
610 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src, | |
611 | ipv6_key->ipv6_src, | |
612 | sizeof(match->key->ipv6.addr.src), | |
613 | is_mask); | |
614 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst, | |
615 | ipv6_key->ipv6_dst, | |
616 | sizeof(match->key->ipv6.addr.dst), | |
617 | is_mask); | |
618 | ||
619 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6); | |
620 | } | |
621 | ||
622 | if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) { | |
623 | const struct ovs_key_arp *arp_key; | |
624 | ||
625 | arp_key = nla_data(a[OVS_KEY_ATTR_ARP]); | |
626 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { | |
627 | OVS_NLERR("Unknown ARP opcode (opcode=%d).\n", | |
628 | arp_key->arp_op); | |
629 | return -EINVAL; | |
630 | } | |
631 | ||
632 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, | |
633 | arp_key->arp_sip, is_mask); | |
634 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, | |
635 | arp_key->arp_tip, is_mask); | |
636 | SW_FLOW_KEY_PUT(match, ip.proto, | |
637 | ntohs(arp_key->arp_op), is_mask); | |
638 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha, | |
639 | arp_key->arp_sha, ETH_ALEN, is_mask); | |
640 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha, | |
641 | arp_key->arp_tha, ETH_ALEN, is_mask); | |
642 | ||
643 | attrs &= ~(1ULL << OVS_KEY_ATTR_ARP); | |
644 | } | |
645 | ||
646 | if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) { | |
647 | const struct ovs_key_tcp *tcp_key; | |
648 | ||
649 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); | |
708fb4c5 JR |
650 | SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask); |
651 | SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask); | |
a097c0b2 PS |
652 | attrs &= ~(1ULL << OVS_KEY_ATTR_TCP); |
653 | } | |
654 | ||
dc235f7f JR |
655 | if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) { |
656 | if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) { | |
708fb4c5 | 657 | SW_FLOW_KEY_PUT(match, tp.flags, |
dc235f7f JR |
658 | nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]), |
659 | is_mask); | |
660 | } else { | |
708fb4c5 | 661 | SW_FLOW_KEY_PUT(match, tp.flags, |
dc235f7f JR |
662 | nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]), |
663 | is_mask); | |
664 | } | |
665 | attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS); | |
666 | } | |
667 | ||
a097c0b2 PS |
668 | if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) { |
669 | const struct ovs_key_udp *udp_key; | |
670 | ||
671 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); | |
708fb4c5 JR |
672 | SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask); |
673 | SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask); | |
a097c0b2 PS |
674 | attrs &= ~(1ULL << OVS_KEY_ATTR_UDP); |
675 | } | |
676 | ||
677 | if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) { | |
678 | const struct ovs_key_sctp *sctp_key; | |
679 | ||
680 | sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]); | |
708fb4c5 JR |
681 | SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask); |
682 | SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask); | |
a097c0b2 PS |
683 | attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP); |
684 | } | |
685 | ||
686 | if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) { | |
687 | const struct ovs_key_icmp *icmp_key; | |
688 | ||
689 | icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]); | |
708fb4c5 | 690 | SW_FLOW_KEY_PUT(match, tp.src, |
a097c0b2 | 691 | htons(icmp_key->icmp_type), is_mask); |
708fb4c5 | 692 | SW_FLOW_KEY_PUT(match, tp.dst, |
a097c0b2 PS |
693 | htons(icmp_key->icmp_code), is_mask); |
694 | attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP); | |
695 | } | |
696 | ||
697 | if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) { | |
698 | const struct ovs_key_icmpv6 *icmpv6_key; | |
699 | ||
700 | icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]); | |
708fb4c5 | 701 | SW_FLOW_KEY_PUT(match, tp.src, |
a097c0b2 | 702 | htons(icmpv6_key->icmpv6_type), is_mask); |
708fb4c5 | 703 | SW_FLOW_KEY_PUT(match, tp.dst, |
a097c0b2 PS |
704 | htons(icmpv6_key->icmpv6_code), is_mask); |
705 | attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6); | |
706 | } | |
707 | ||
708 | if (attrs & (1ULL << OVS_KEY_ATTR_ND)) { | |
709 | const struct ovs_key_nd *nd_key; | |
710 | ||
711 | nd_key = nla_data(a[OVS_KEY_ATTR_ND]); | |
712 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target, | |
713 | nd_key->nd_target, | |
714 | sizeof(match->key->ipv6.nd.target), | |
715 | is_mask); | |
716 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll, | |
717 | nd_key->nd_sll, ETH_ALEN, is_mask); | |
718 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll, | |
719 | nd_key->nd_tll, ETH_ALEN, is_mask); | |
720 | attrs &= ~(1ULL << OVS_KEY_ATTR_ND); | |
721 | } | |
722 | ||
723 | if (attrs != 0) | |
724 | return -EINVAL; | |
725 | ||
726 | return 0; | |
727 | } | |
728 | ||
729 | static void sw_flow_mask_set(struct sw_flow_mask *mask, | |
730 | struct sw_flow_key_range *range, u8 val) | |
731 | { | |
732 | u8 *m = (u8 *)&mask->key + range->start; | |
733 | ||
734 | mask->range = *range; | |
735 | memset(m, val, range_n_bytes(range)); | |
736 | } | |
737 | ||
738 | /** | |
739 | * ovs_nla_get_match - parses Netlink attributes into a flow key and | |
740 | * mask. In case the 'mask' is NULL, the flow is treated as exact match | |
741 | * flow. Otherwise, it is treated as a wildcarded flow, except the mask | |
742 | * does not include any don't care bit. | |
743 | * @match: receives the extracted flow match information. | |
744 | * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute | |
745 | * sequence. The fields should of the packet that triggered the creation | |
746 | * of this flow. | |
747 | * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink | |
748 | * attribute specifies the mask field of the wildcarded flow. | |
749 | */ | |
750 | int ovs_nla_get_match(struct sw_flow_match *match, | |
751 | const struct nlattr *key, | |
752 | const struct nlattr *mask) | |
753 | { | |
754 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; | |
755 | const struct nlattr *encap; | |
756 | u64 key_attrs = 0; | |
757 | u64 mask_attrs = 0; | |
758 | bool encap_valid = false; | |
759 | int err; | |
760 | ||
761 | err = parse_flow_nlattrs(key, a, &key_attrs); | |
762 | if (err) | |
763 | return err; | |
764 | ||
765 | if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) && | |
766 | (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) && | |
767 | (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) { | |
768 | __be16 tci; | |
769 | ||
770 | if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) && | |
771 | (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) { | |
772 | OVS_NLERR("Invalid Vlan frame.\n"); | |
773 | return -EINVAL; | |
774 | } | |
775 | ||
776 | key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
777 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
778 | encap = a[OVS_KEY_ATTR_ENCAP]; | |
779 | key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP); | |
780 | encap_valid = true; | |
781 | ||
782 | if (tci & htons(VLAN_TAG_PRESENT)) { | |
783 | err = parse_flow_nlattrs(encap, a, &key_attrs); | |
784 | if (err) | |
785 | return err; | |
786 | } else if (!tci) { | |
787 | /* Corner case for truncated 802.1Q header. */ | |
788 | if (nla_len(encap)) { | |
789 | OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n"); | |
790 | return -EINVAL; | |
791 | } | |
792 | } else { | |
793 | OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n"); | |
794 | return -EINVAL; | |
795 | } | |
796 | } | |
797 | ||
df65fec1 | 798 | err = ovs_key_from_nlattrs(match, key_attrs, a, false); |
a097c0b2 PS |
799 | if (err) |
800 | return err; | |
801 | ||
802 | if (mask) { | |
803 | err = parse_flow_mask_nlattrs(mask, a, &mask_attrs); | |
804 | if (err) | |
805 | return err; | |
806 | ||
807 | if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) { | |
808 | __be16 eth_type = 0; | |
809 | __be16 tci = 0; | |
810 | ||
811 | if (!encap_valid) { | |
812 | OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n"); | |
813 | return -EINVAL; | |
814 | } | |
815 | ||
816 | mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP); | |
817 | if (a[OVS_KEY_ATTR_ETHERTYPE]) | |
818 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
819 | ||
820 | if (eth_type == htons(0xffff)) { | |
821 | mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
822 | encap = a[OVS_KEY_ATTR_ENCAP]; | |
823 | err = parse_flow_mask_nlattrs(encap, a, &mask_attrs); | |
824 | } else { | |
825 | OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n", | |
826 | ntohs(eth_type)); | |
827 | return -EINVAL; | |
828 | } | |
829 | ||
830 | if (a[OVS_KEY_ATTR_VLAN]) | |
831 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
832 | ||
833 | if (!(tci & htons(VLAN_TAG_PRESENT))) { | |
834 | OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci)); | |
835 | return -EINVAL; | |
836 | } | |
837 | } | |
838 | ||
df65fec1 | 839 | err = ovs_key_from_nlattrs(match, mask_attrs, a, true); |
a097c0b2 PS |
840 | if (err) |
841 | return err; | |
842 | } else { | |
843 | /* Populate exact match flow's key mask. */ | |
844 | if (match->mask) | |
845 | sw_flow_mask_set(match->mask, &match->range, 0xff); | |
846 | } | |
847 | ||
848 | if (!match_validate(match, key_attrs, mask_attrs)) | |
849 | return -EINVAL; | |
850 | ||
851 | return 0; | |
852 | } | |
853 | ||
854 | /** | |
855 | * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key. | |
856 | * @flow: Receives extracted in_port, priority, tun_key and skb_mark. | |
857 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute | |
858 | * sequence. | |
859 | * | |
860 | * This parses a series of Netlink attributes that form a flow key, which must | |
861 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to | |
862 | * get the metadata, that is, the parts of the flow key that cannot be | |
863 | * extracted from the packet itself. | |
864 | */ | |
865 | ||
866 | int ovs_nla_get_flow_metadata(struct sw_flow *flow, | |
867 | const struct nlattr *attr) | |
868 | { | |
869 | struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key; | |
870 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; | |
871 | u64 attrs = 0; | |
872 | int err; | |
873 | struct sw_flow_match match; | |
874 | ||
875 | flow->key.phy.in_port = DP_MAX_PORTS; | |
876 | flow->key.phy.priority = 0; | |
877 | flow->key.phy.skb_mark = 0; | |
7804df20 | 878 | flow->key.ovs_flow_hash = 0; |
a6059080 | 879 | flow->key.recirc_id = 0; |
a097c0b2 PS |
880 | memset(tun_key, 0, sizeof(flow->key.tun_key)); |
881 | ||
882 | err = parse_flow_nlattrs(attr, a, &attrs); | |
883 | if (err) | |
884 | return -EINVAL; | |
885 | ||
886 | memset(&match, 0, sizeof(match)); | |
887 | match.key = &flow->key; | |
888 | ||
889 | err = metadata_from_nlattrs(&match, &attrs, a, false); | |
890 | if (err) | |
891 | return err; | |
892 | ||
893 | return 0; | |
894 | } | |
895 | ||
896 | int ovs_nla_put_flow(const struct sw_flow_key *swkey, | |
897 | const struct sw_flow_key *output, struct sk_buff *skb) | |
898 | { | |
899 | struct ovs_key_ethernet *eth_key; | |
900 | struct nlattr *nla, *encap; | |
901 | bool is_mask = (swkey != output); | |
902 | ||
7804df20 AZ |
903 | if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash)) |
904 | goto nla_put_failure; | |
905 | ||
a6059080 AZ |
906 | if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id)) |
907 | goto nla_put_failure; | |
908 | ||
a097c0b2 PS |
909 | if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority)) |
910 | goto nla_put_failure; | |
911 | ||
912 | if ((swkey->tun_key.ipv4_dst || is_mask) && | |
913 | ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key)) | |
914 | goto nla_put_failure; | |
915 | ||
916 | if (swkey->phy.in_port == DP_MAX_PORTS) { | |
917 | if (is_mask && (output->phy.in_port == 0xffff)) | |
918 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff)) | |
919 | goto nla_put_failure; | |
920 | } else { | |
921 | u16 upper_u16; | |
922 | upper_u16 = !is_mask ? 0 : 0xffff; | |
923 | ||
924 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, | |
925 | (upper_u16 << 16) | output->phy.in_port)) | |
926 | goto nla_put_failure; | |
927 | } | |
928 | ||
929 | if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark)) | |
930 | goto nla_put_failure; | |
931 | ||
932 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); | |
933 | if (!nla) | |
934 | goto nla_put_failure; | |
935 | ||
936 | eth_key = nla_data(nla); | |
982a47ec JP |
937 | ether_addr_copy(eth_key->eth_src, output->eth.src); |
938 | ether_addr_copy(eth_key->eth_dst, output->eth.dst); | |
a097c0b2 PS |
939 | |
940 | if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) { | |
941 | __be16 eth_type; | |
942 | eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff); | |
943 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) || | |
944 | nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci)) | |
945 | goto nla_put_failure; | |
946 | encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP); | |
947 | if (!swkey->eth.tci) | |
948 | goto unencap; | |
949 | } else | |
950 | encap = NULL; | |
951 | ||
952 | if (swkey->eth.type == htons(ETH_P_802_2)) { | |
953 | /* | |
954 | * Ethertype 802.2 is represented in the netlink with omitted | |
955 | * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and | |
956 | * 0xffff in the mask attribute. Ethertype can also | |
957 | * be wildcarded. | |
958 | */ | |
959 | if (is_mask && output->eth.type) | |
960 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, | |
961 | output->eth.type)) | |
962 | goto nla_put_failure; | |
963 | goto unencap; | |
964 | } | |
965 | ||
966 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type)) | |
967 | goto nla_put_failure; | |
968 | ||
969 | if (swkey->eth.type == htons(ETH_P_IP)) { | |
970 | struct ovs_key_ipv4 *ipv4_key; | |
971 | ||
972 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); | |
973 | if (!nla) | |
974 | goto nla_put_failure; | |
975 | ipv4_key = nla_data(nla); | |
976 | ipv4_key->ipv4_src = output->ipv4.addr.src; | |
977 | ipv4_key->ipv4_dst = output->ipv4.addr.dst; | |
978 | ipv4_key->ipv4_proto = output->ip.proto; | |
979 | ipv4_key->ipv4_tos = output->ip.tos; | |
980 | ipv4_key->ipv4_ttl = output->ip.ttl; | |
981 | ipv4_key->ipv4_frag = output->ip.frag; | |
982 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
983 | struct ovs_key_ipv6 *ipv6_key; | |
984 | ||
985 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); | |
986 | if (!nla) | |
987 | goto nla_put_failure; | |
988 | ipv6_key = nla_data(nla); | |
989 | memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src, | |
990 | sizeof(ipv6_key->ipv6_src)); | |
991 | memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst, | |
992 | sizeof(ipv6_key->ipv6_dst)); | |
993 | ipv6_key->ipv6_label = output->ipv6.label; | |
994 | ipv6_key->ipv6_proto = output->ip.proto; | |
995 | ipv6_key->ipv6_tclass = output->ip.tos; | |
996 | ipv6_key->ipv6_hlimit = output->ip.ttl; | |
997 | ipv6_key->ipv6_frag = output->ip.frag; | |
998 | } else if (swkey->eth.type == htons(ETH_P_ARP) || | |
999 | swkey->eth.type == htons(ETH_P_RARP)) { | |
1000 | struct ovs_key_arp *arp_key; | |
1001 | ||
1002 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); | |
1003 | if (!nla) | |
1004 | goto nla_put_failure; | |
1005 | arp_key = nla_data(nla); | |
1006 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); | |
1007 | arp_key->arp_sip = output->ipv4.addr.src; | |
1008 | arp_key->arp_tip = output->ipv4.addr.dst; | |
1009 | arp_key->arp_op = htons(output->ip.proto); | |
982a47ec JP |
1010 | ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha); |
1011 | ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha); | |
a097c0b2 PS |
1012 | } |
1013 | ||
1014 | if ((swkey->eth.type == htons(ETH_P_IP) || | |
1015 | swkey->eth.type == htons(ETH_P_IPV6)) && | |
1016 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { | |
1017 | ||
1018 | if (swkey->ip.proto == IPPROTO_TCP) { | |
1019 | struct ovs_key_tcp *tcp_key; | |
1020 | ||
1021 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); | |
1022 | if (!nla) | |
1023 | goto nla_put_failure; | |
1024 | tcp_key = nla_data(nla); | |
708fb4c5 JR |
1025 | tcp_key->tcp_src = output->tp.src; |
1026 | tcp_key->tcp_dst = output->tp.dst; | |
1027 | if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS, | |
1028 | output->tp.flags)) | |
1029 | goto nla_put_failure; | |
a097c0b2 PS |
1030 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
1031 | struct ovs_key_udp *udp_key; | |
1032 | ||
1033 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); | |
1034 | if (!nla) | |
1035 | goto nla_put_failure; | |
1036 | udp_key = nla_data(nla); | |
708fb4c5 JR |
1037 | udp_key->udp_src = output->tp.src; |
1038 | udp_key->udp_dst = output->tp.dst; | |
a097c0b2 PS |
1039 | } else if (swkey->ip.proto == IPPROTO_SCTP) { |
1040 | struct ovs_key_sctp *sctp_key; | |
1041 | ||
1042 | nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key)); | |
1043 | if (!nla) | |
1044 | goto nla_put_failure; | |
1045 | sctp_key = nla_data(nla); | |
708fb4c5 JR |
1046 | sctp_key->sctp_src = output->tp.src; |
1047 | sctp_key->sctp_dst = output->tp.dst; | |
a097c0b2 PS |
1048 | } else if (swkey->eth.type == htons(ETH_P_IP) && |
1049 | swkey->ip.proto == IPPROTO_ICMP) { | |
1050 | struct ovs_key_icmp *icmp_key; | |
1051 | ||
1052 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); | |
1053 | if (!nla) | |
1054 | goto nla_put_failure; | |
1055 | icmp_key = nla_data(nla); | |
708fb4c5 JR |
1056 | icmp_key->icmp_type = ntohs(output->tp.src); |
1057 | icmp_key->icmp_code = ntohs(output->tp.dst); | |
a097c0b2 PS |
1058 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && |
1059 | swkey->ip.proto == IPPROTO_ICMPV6) { | |
1060 | struct ovs_key_icmpv6 *icmpv6_key; | |
1061 | ||
1062 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, | |
1063 | sizeof(*icmpv6_key)); | |
1064 | if (!nla) | |
1065 | goto nla_put_failure; | |
1066 | icmpv6_key = nla_data(nla); | |
708fb4c5 JR |
1067 | icmpv6_key->icmpv6_type = ntohs(output->tp.src); |
1068 | icmpv6_key->icmpv6_code = ntohs(output->tp.dst); | |
a097c0b2 PS |
1069 | |
1070 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || | |
1071 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { | |
1072 | struct ovs_key_nd *nd_key; | |
1073 | ||
1074 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); | |
1075 | if (!nla) | |
1076 | goto nla_put_failure; | |
1077 | nd_key = nla_data(nla); | |
1078 | memcpy(nd_key->nd_target, &output->ipv6.nd.target, | |
1079 | sizeof(nd_key->nd_target)); | |
982a47ec JP |
1080 | ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll); |
1081 | ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll); | |
a097c0b2 PS |
1082 | } |
1083 | } | |
1084 | } | |
1085 | ||
1086 | unencap: | |
1087 | if (encap) | |
1088 | nla_nest_end(skb, encap); | |
1089 | ||
1090 | return 0; | |
1091 | ||
1092 | nla_put_failure: | |
1093 | return -EMSGSIZE; | |
1094 | } | |
1095 | ||
1096 | #define MAX_ACTIONS_BUFSIZE (32 * 1024) | |
1097 | ||
1098 | struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size) | |
1099 | { | |
1100 | struct sw_flow_actions *sfa; | |
1101 | ||
1102 | if (size > MAX_ACTIONS_BUFSIZE) | |
1103 | return ERR_PTR(-EINVAL); | |
1104 | ||
1105 | sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL); | |
1106 | if (!sfa) | |
1107 | return ERR_PTR(-ENOMEM); | |
1108 | ||
1109 | sfa->actions_len = 0; | |
1110 | return sfa; | |
1111 | } | |
1112 | ||
1113 | /* RCU callback used by ovs_nla_free_flow_actions. */ | |
1114 | static void rcu_free_acts_callback(struct rcu_head *rcu) | |
1115 | { | |
1116 | struct sw_flow_actions *sf_acts = container_of(rcu, | |
1117 | struct sw_flow_actions, rcu); | |
1118 | kfree(sf_acts); | |
1119 | } | |
1120 | ||
1121 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. | |
1122 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
1123 | void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts) | |
1124 | { | |
1125 | call_rcu(&sf_acts->rcu, rcu_free_acts_callback); | |
1126 | } | |
1127 | ||
1128 | static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, | |
1129 | int attr_len) | |
1130 | { | |
1131 | ||
1132 | struct sw_flow_actions *acts; | |
1133 | int new_acts_size; | |
1134 | int req_size = NLA_ALIGN(attr_len); | |
1135 | int next_offset = offsetof(struct sw_flow_actions, actions) + | |
1136 | (*sfa)->actions_len; | |
1137 | ||
1138 | if (req_size <= (ksize(*sfa) - next_offset)) | |
1139 | goto out; | |
1140 | ||
1141 | new_acts_size = ksize(*sfa) * 2; | |
1142 | ||
1143 | if (new_acts_size > MAX_ACTIONS_BUFSIZE) { | |
1144 | if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) | |
1145 | return ERR_PTR(-EMSGSIZE); | |
1146 | new_acts_size = MAX_ACTIONS_BUFSIZE; | |
1147 | } | |
1148 | ||
1149 | acts = ovs_nla_alloc_flow_actions(new_acts_size); | |
1150 | if (IS_ERR(acts)) | |
1151 | return (void *)acts; | |
1152 | ||
1153 | memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len); | |
1154 | acts->actions_len = (*sfa)->actions_len; | |
1155 | kfree(*sfa); | |
1156 | *sfa = acts; | |
1157 | ||
1158 | out: | |
1159 | (*sfa)->actions_len += req_size; | |
1160 | return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset); | |
1161 | } | |
1162 | ||
1163 | static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len) | |
1164 | { | |
1165 | struct nlattr *a; | |
1166 | ||
1167 | a = reserve_sfa_size(sfa, nla_attr_size(len)); | |
1168 | if (IS_ERR(a)) | |
1169 | return PTR_ERR(a); | |
1170 | ||
1171 | a->nla_type = attrtype; | |
1172 | a->nla_len = nla_attr_size(len); | |
1173 | ||
1174 | if (data) | |
1175 | memcpy(nla_data(a), data, len); | |
1176 | memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len)); | |
1177 | ||
1178 | return 0; | |
1179 | } | |
1180 | ||
1181 | static inline int add_nested_action_start(struct sw_flow_actions **sfa, | |
1182 | int attrtype) | |
1183 | { | |
1184 | int used = (*sfa)->actions_len; | |
1185 | int err; | |
1186 | ||
1187 | err = add_action(sfa, attrtype, NULL, 0); | |
1188 | if (err) | |
1189 | return err; | |
1190 | ||
1191 | return used; | |
1192 | } | |
1193 | ||
1194 | static inline void add_nested_action_end(struct sw_flow_actions *sfa, | |
1195 | int st_offset) | |
1196 | { | |
1197 | struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + | |
1198 | st_offset); | |
1199 | ||
1200 | a->nla_len = sfa->actions_len - st_offset; | |
1201 | } | |
1202 | ||
1203 | static int validate_and_copy_sample(const struct nlattr *attr, | |
1204 | const struct sw_flow_key *key, int depth, | |
1205 | struct sw_flow_actions **sfa) | |
1206 | { | |
1207 | const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; | |
1208 | const struct nlattr *probability, *actions; | |
1209 | const struct nlattr *a; | |
1210 | int rem, start, err, st_acts; | |
1211 | ||
1212 | memset(attrs, 0, sizeof(attrs)); | |
1213 | nla_for_each_nested(a, attr, rem) { | |
1214 | int type = nla_type(a); | |
1215 | if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type]) | |
1216 | return -EINVAL; | |
1217 | attrs[type] = a; | |
1218 | } | |
1219 | if (rem) | |
1220 | return -EINVAL; | |
1221 | ||
1222 | probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY]; | |
1223 | if (!probability || nla_len(probability) != sizeof(u32)) | |
1224 | return -EINVAL; | |
1225 | ||
1226 | actions = attrs[OVS_SAMPLE_ATTR_ACTIONS]; | |
1227 | if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) | |
1228 | return -EINVAL; | |
1229 | ||
1230 | /* validation done, copy sample action. */ | |
1231 | start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE); | |
1232 | if (start < 0) | |
1233 | return start; | |
1234 | err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, | |
1235 | nla_data(probability), sizeof(u32)); | |
1236 | if (err) | |
1237 | return err; | |
1238 | st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS); | |
1239 | if (st_acts < 0) | |
1240 | return st_acts; | |
1241 | ||
1242 | err = ovs_nla_copy_actions(actions, key, depth + 1, sfa); | |
1243 | if (err) | |
1244 | return err; | |
1245 | ||
1246 | add_nested_action_end(*sfa, st_acts); | |
1247 | add_nested_action_end(*sfa, start); | |
1248 | ||
1249 | return 0; | |
1250 | } | |
1251 | ||
1252 | static int validate_tp_port(const struct sw_flow_key *flow_key) | |
1253 | { | |
708fb4c5 JR |
1254 | if ((flow_key->eth.type == htons(ETH_P_IP) || |
1255 | flow_key->eth.type == htons(ETH_P_IPV6)) && | |
1256 | (flow_key->tp.src || flow_key->tp.dst)) | |
1257 | return 0; | |
a097c0b2 PS |
1258 | |
1259 | return -EINVAL; | |
1260 | } | |
1261 | ||
1262 | void ovs_match_init(struct sw_flow_match *match, | |
1263 | struct sw_flow_key *key, | |
1264 | struct sw_flow_mask *mask) | |
1265 | { | |
1266 | memset(match, 0, sizeof(*match)); | |
1267 | match->key = key; | |
1268 | match->mask = mask; | |
1269 | ||
1270 | memset(key, 0, sizeof(*key)); | |
1271 | ||
1272 | if (mask) { | |
1273 | memset(&mask->key, 0, sizeof(mask->key)); | |
1274 | mask->range.start = mask->range.end = 0; | |
1275 | } | |
1276 | } | |
1277 | ||
1278 | static int validate_and_copy_set_tun(const struct nlattr *attr, | |
1279 | struct sw_flow_actions **sfa) | |
1280 | { | |
1281 | struct sw_flow_match match; | |
1282 | struct sw_flow_key key; | |
1283 | int err, start; | |
1284 | ||
1285 | ovs_match_init(&match, &key, NULL); | |
1286 | err = ipv4_tun_from_nlattr(nla_data(attr), &match, false); | |
1287 | if (err) | |
1288 | return err; | |
1289 | ||
1290 | start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET); | |
1291 | if (start < 0) | |
1292 | return start; | |
1293 | ||
1294 | err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key, | |
1295 | sizeof(match.key->tun_key)); | |
1296 | add_nested_action_end(*sfa, start); | |
1297 | ||
1298 | return err; | |
1299 | } | |
1300 | ||
1301 | static int validate_set(const struct nlattr *a, | |
1302 | const struct sw_flow_key *flow_key, | |
1303 | struct sw_flow_actions **sfa, | |
1304 | bool *set_tun) | |
1305 | { | |
1306 | const struct nlattr *ovs_key = nla_data(a); | |
1307 | int key_type = nla_type(ovs_key); | |
1308 | ||
1309 | /* There can be only one key in a action */ | |
1310 | if (nla_total_size(nla_len(ovs_key)) != nla_len(a)) | |
1311 | return -EINVAL; | |
1312 | ||
1313 | if (key_type > OVS_KEY_ATTR_MAX || | |
1314 | (ovs_key_lens[key_type] != nla_len(ovs_key) && | |
1315 | ovs_key_lens[key_type] != -1)) | |
1316 | return -EINVAL; | |
1317 | ||
1318 | switch (key_type) { | |
1319 | const struct ovs_key_ipv4 *ipv4_key; | |
1320 | const struct ovs_key_ipv6 *ipv6_key; | |
1321 | int err; | |
1322 | ||
1323 | case OVS_KEY_ATTR_PRIORITY: | |
1324 | case OVS_KEY_ATTR_SKB_MARK: | |
1325 | case OVS_KEY_ATTR_ETHERNET: | |
1326 | break; | |
1327 | ||
1328 | case OVS_KEY_ATTR_TUNNEL: | |
1329 | *set_tun = true; | |
1330 | err = validate_and_copy_set_tun(a, sfa); | |
1331 | if (err) | |
1332 | return err; | |
1333 | break; | |
1334 | ||
1335 | case OVS_KEY_ATTR_IPV4: | |
1336 | if (flow_key->eth.type != htons(ETH_P_IP)) | |
1337 | return -EINVAL; | |
1338 | ||
1339 | if (!flow_key->ip.proto) | |
1340 | return -EINVAL; | |
1341 | ||
1342 | ipv4_key = nla_data(ovs_key); | |
1343 | if (ipv4_key->ipv4_proto != flow_key->ip.proto) | |
1344 | return -EINVAL; | |
1345 | ||
1346 | if (ipv4_key->ipv4_frag != flow_key->ip.frag) | |
1347 | return -EINVAL; | |
1348 | ||
1349 | break; | |
1350 | ||
1351 | case OVS_KEY_ATTR_IPV6: | |
1352 | if (flow_key->eth.type != htons(ETH_P_IPV6)) | |
1353 | return -EINVAL; | |
1354 | ||
1355 | if (!flow_key->ip.proto) | |
1356 | return -EINVAL; | |
1357 | ||
1358 | ipv6_key = nla_data(ovs_key); | |
1359 | if (ipv6_key->ipv6_proto != flow_key->ip.proto) | |
1360 | return -EINVAL; | |
1361 | ||
1362 | if (ipv6_key->ipv6_frag != flow_key->ip.frag) | |
1363 | return -EINVAL; | |
1364 | ||
1365 | if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000) | |
1366 | return -EINVAL; | |
1367 | ||
1368 | break; | |
1369 | ||
1370 | case OVS_KEY_ATTR_TCP: | |
1371 | if (flow_key->ip.proto != IPPROTO_TCP) | |
1372 | return -EINVAL; | |
1373 | ||
1374 | return validate_tp_port(flow_key); | |
1375 | ||
1376 | case OVS_KEY_ATTR_UDP: | |
1377 | if (flow_key->ip.proto != IPPROTO_UDP) | |
1378 | return -EINVAL; | |
1379 | ||
1380 | return validate_tp_port(flow_key); | |
1381 | ||
1382 | case OVS_KEY_ATTR_SCTP: | |
1383 | if (flow_key->ip.proto != IPPROTO_SCTP) | |
1384 | return -EINVAL; | |
1385 | ||
1386 | return validate_tp_port(flow_key); | |
1387 | ||
1388 | default: | |
1389 | return -EINVAL; | |
1390 | } | |
1391 | ||
1392 | return 0; | |
1393 | } | |
1394 | ||
1395 | static int validate_userspace(const struct nlattr *attr) | |
1396 | { | |
1397 | static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = { | |
1398 | [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 }, | |
1399 | [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC }, | |
1400 | }; | |
1401 | struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1]; | |
1402 | int error; | |
1403 | ||
1404 | error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, | |
1405 | attr, userspace_policy); | |
1406 | if (error) | |
1407 | return error; | |
1408 | ||
1409 | if (!a[OVS_USERSPACE_ATTR_PID] || | |
1410 | !nla_get_u32(a[OVS_USERSPACE_ATTR_PID])) | |
1411 | return -EINVAL; | |
1412 | ||
1413 | return 0; | |
1414 | } | |
1415 | ||
1416 | static int copy_action(const struct nlattr *from, | |
1417 | struct sw_flow_actions **sfa) | |
1418 | { | |
1419 | int totlen = NLA_ALIGN(from->nla_len); | |
1420 | struct nlattr *to; | |
1421 | ||
1422 | to = reserve_sfa_size(sfa, from->nla_len); | |
1423 | if (IS_ERR(to)) | |
1424 | return PTR_ERR(to); | |
1425 | ||
1426 | memcpy(to, from, totlen); | |
1427 | return 0; | |
1428 | } | |
1429 | ||
1430 | int ovs_nla_copy_actions(const struct nlattr *attr, | |
1431 | const struct sw_flow_key *key, | |
1432 | int depth, | |
1433 | struct sw_flow_actions **sfa) | |
1434 | { | |
1435 | const struct nlattr *a; | |
1436 | int rem, err; | |
1437 | ||
1438 | if (depth >= SAMPLE_ACTION_DEPTH) | |
1439 | return -EOVERFLOW; | |
1440 | ||
1441 | nla_for_each_nested(a, attr, rem) { | |
1442 | /* Expected argument lengths, (u32)-1 for variable length. */ | |
1443 | static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = { | |
1444 | [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32), | |
a6059080 | 1445 | [OVS_ACTION_ATTR_RECIRC] = sizeof(u32), |
a097c0b2 PS |
1446 | [OVS_ACTION_ATTR_USERSPACE] = (u32)-1, |
1447 | [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan), | |
1448 | [OVS_ACTION_ATTR_POP_VLAN] = 0, | |
1449 | [OVS_ACTION_ATTR_SET] = (u32)-1, | |
7804df20 AZ |
1450 | [OVS_ACTION_ATTR_SAMPLE] = (u32)-1, |
1451 | [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash) | |
a097c0b2 PS |
1452 | }; |
1453 | const struct ovs_action_push_vlan *vlan; | |
1454 | int type = nla_type(a); | |
1455 | bool skip_copy; | |
1456 | ||
1457 | if (type > OVS_ACTION_ATTR_MAX || | |
1458 | (action_lens[type] != nla_len(a) && | |
1459 | action_lens[type] != (u32)-1)) | |
1460 | return -EINVAL; | |
1461 | ||
1462 | skip_copy = false; | |
1463 | switch (type) { | |
1464 | case OVS_ACTION_ATTR_UNSPEC: | |
1465 | return -EINVAL; | |
1466 | ||
1467 | case OVS_ACTION_ATTR_USERSPACE: | |
1468 | err = validate_userspace(a); | |
1469 | if (err) | |
1470 | return err; | |
1471 | break; | |
1472 | ||
1473 | case OVS_ACTION_ATTR_OUTPUT: | |
1474 | if (nla_get_u32(a) >= DP_MAX_PORTS) | |
1475 | return -EINVAL; | |
1476 | break; | |
1477 | ||
7804df20 AZ |
1478 | case OVS_ACTION_ATTR_HASH: { |
1479 | const struct ovs_action_hash *act_hash = nla_data(a); | |
1480 | ||
1481 | switch (act_hash->hash_alg) { | |
1482 | case OVS_HASH_ALG_L4: | |
1483 | break; | |
1484 | default: | |
1485 | return -EINVAL; | |
1486 | } | |
1487 | ||
1488 | break; | |
1489 | } | |
a097c0b2 PS |
1490 | |
1491 | case OVS_ACTION_ATTR_POP_VLAN: | |
1492 | break; | |
1493 | ||
1494 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
1495 | vlan = nla_data(a); | |
1496 | if (vlan->vlan_tpid != htons(ETH_P_8021Q)) | |
1497 | return -EINVAL; | |
1498 | if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT))) | |
1499 | return -EINVAL; | |
1500 | break; | |
1501 | ||
a6059080 AZ |
1502 | case OVS_ACTION_ATTR_RECIRC: |
1503 | break; | |
1504 | ||
a097c0b2 PS |
1505 | case OVS_ACTION_ATTR_SET: |
1506 | err = validate_set(a, key, sfa, &skip_copy); | |
1507 | if (err) | |
1508 | return err; | |
1509 | break; | |
1510 | ||
1511 | case OVS_ACTION_ATTR_SAMPLE: | |
1512 | err = validate_and_copy_sample(a, key, depth, sfa); | |
1513 | if (err) | |
1514 | return err; | |
1515 | skip_copy = true; | |
1516 | break; | |
1517 | ||
1518 | default: | |
1519 | return -EINVAL; | |
1520 | } | |
1521 | if (!skip_copy) { | |
1522 | err = copy_action(a, sfa); | |
1523 | if (err) | |
1524 | return err; | |
1525 | } | |
1526 | } | |
1527 | ||
1528 | if (rem > 0) | |
1529 | return -EINVAL; | |
1530 | ||
1531 | return 0; | |
1532 | } | |
1533 | ||
1534 | static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb) | |
1535 | { | |
1536 | const struct nlattr *a; | |
1537 | struct nlattr *start; | |
1538 | int err = 0, rem; | |
1539 | ||
1540 | start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE); | |
1541 | if (!start) | |
1542 | return -EMSGSIZE; | |
1543 | ||
1544 | nla_for_each_nested(a, attr, rem) { | |
1545 | int type = nla_type(a); | |
1546 | struct nlattr *st_sample; | |
1547 | ||
1548 | switch (type) { | |
1549 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
1550 | if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, | |
1551 | sizeof(u32), nla_data(a))) | |
1552 | return -EMSGSIZE; | |
1553 | break; | |
1554 | case OVS_SAMPLE_ATTR_ACTIONS: | |
1555 | st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS); | |
1556 | if (!st_sample) | |
1557 | return -EMSGSIZE; | |
1558 | err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb); | |
1559 | if (err) | |
1560 | return err; | |
1561 | nla_nest_end(skb, st_sample); | |
1562 | break; | |
1563 | } | |
1564 | } | |
1565 | ||
1566 | nla_nest_end(skb, start); | |
1567 | return err; | |
1568 | } | |
1569 | ||
1570 | static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb) | |
1571 | { | |
1572 | const struct nlattr *ovs_key = nla_data(a); | |
1573 | int key_type = nla_type(ovs_key); | |
1574 | struct nlattr *start; | |
1575 | int err; | |
1576 | ||
1577 | switch (key_type) { | |
1578 | case OVS_KEY_ATTR_IPV4_TUNNEL: | |
1579 | start = nla_nest_start(skb, OVS_ACTION_ATTR_SET); | |
1580 | if (!start) | |
1581 | return -EMSGSIZE; | |
1582 | ||
1583 | err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key), | |
1584 | nla_data(ovs_key)); | |
1585 | if (err) | |
1586 | return err; | |
1587 | nla_nest_end(skb, start); | |
1588 | break; | |
1589 | default: | |
1590 | if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key)) | |
1591 | return -EMSGSIZE; | |
1592 | break; | |
1593 | } | |
1594 | ||
1595 | return 0; | |
1596 | } | |
1597 | ||
1598 | int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb) | |
1599 | { | |
1600 | const struct nlattr *a; | |
1601 | int rem, err; | |
1602 | ||
1603 | nla_for_each_attr(a, attr, len, rem) { | |
1604 | int type = nla_type(a); | |
1605 | ||
1606 | switch (type) { | |
1607 | case OVS_ACTION_ATTR_SET: | |
1608 | err = set_action_to_attr(a, skb); | |
1609 | if (err) | |
1610 | return err; | |
1611 | break; | |
1612 | ||
1613 | case OVS_ACTION_ATTR_SAMPLE: | |
1614 | err = sample_action_to_attr(a, skb); | |
1615 | if (err) | |
1616 | return err; | |
1617 | break; | |
1618 | default: | |
1619 | if (nla_put(skb, type, nla_len(a), nla_data(a))) | |
1620 | return -EMSGSIZE; | |
1621 | break; | |
1622 | } | |
1623 | } | |
1624 | ||
1625 | return 0; | |
1626 | } |