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