]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
a1c564be | 2 | * Copyright (c) 2007-2013 Nicira, Inc. |
a14bc59f | 3 | * |
a9a29d22 JG |
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 | |
064af421 BP |
17 | */ |
18 | ||
19 | #include "flow.h" | |
f5e86186 | 20 | #include "datapath.h" |
6455100f | 21 | #include <linux/uaccess.h> |
064af421 BP |
22 | #include <linux/netdevice.h> |
23 | #include <linux/etherdevice.h> | |
24 | #include <linux/if_ether.h> | |
25 | #include <linux/if_vlan.h> | |
26 | #include <net/llc_pdu.h> | |
27 | #include <linux/kernel.h> | |
8d5ebd83 | 28 | #include <linux/jhash.h> |
064af421 BP |
29 | #include <linux/jiffies.h> |
30 | #include <linux/llc.h> | |
31 | #include <linux/module.h> | |
32 | #include <linux/in.h> | |
33 | #include <linux/rcupdate.h> | |
a26ef517 | 34 | #include <linux/if_arp.h> |
064af421 | 35 | #include <linux/ip.h> |
d31f1109 | 36 | #include <linux/ipv6.h> |
064af421 BP |
37 | #include <linux/tcp.h> |
38 | #include <linux/udp.h> | |
39 | #include <linux/icmp.h> | |
d31f1109 | 40 | #include <linux/icmpv6.h> |
3544358a | 41 | #include <linux/rculist.h> |
064af421 | 42 | #include <net/ip.h> |
d31f1109 | 43 | #include <net/ipv6.h> |
685a51a5 | 44 | #include <net/ndisc.h> |
064af421 | 45 | |
6ce39213 JG |
46 | #include "vlan.h" |
47 | ||
33b38b63 | 48 | static struct kmem_cache *flow_cache; |
064af421 | 49 | |
a1c564be AZ |
50 | static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask, |
51 | struct sw_flow_key_range *range, u8 val); | |
52 | ||
53 | static void update_range__(struct sw_flow_match *match, | |
54 | size_t offset, size_t size, bool is_mask) | |
55 | { | |
56 | struct sw_flow_key_range *range = NULL; | |
57 | size_t start = offset; | |
58 | size_t end = offset + size; | |
59 | ||
60 | if (!is_mask) | |
61 | range = &match->range; | |
62 | else if (match->mask) | |
63 | range = &match->mask->range; | |
64 | ||
65 | if (!range) | |
66 | return; | |
67 | ||
68 | if (range->start == range->end) { | |
69 | range->start = start; | |
70 | range->end = end; | |
71 | return; | |
72 | } | |
73 | ||
74 | if (range->start > start) | |
75 | range->start = start; | |
76 | ||
77 | if (range->end < end) | |
78 | range->end = end; | |
79 | } | |
80 | ||
81 | #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \ | |
82 | do { \ | |
83 | update_range__(match, offsetof(struct sw_flow_key, field), \ | |
84 | sizeof((match)->key->field), is_mask); \ | |
388c13d8 AZ |
85 | if (is_mask) { \ |
86 | if ((match)->mask) \ | |
87 | (match)->mask->key.field = value; \ | |
a1c564be AZ |
88 | } else { \ |
89 | (match)->key->field = value; \ | |
90 | } \ | |
91 | } while (0) | |
92 | ||
93 | #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \ | |
94 | do { \ | |
95 | update_range__(match, offsetof(struct sw_flow_key, field), \ | |
96 | len, is_mask); \ | |
388c13d8 AZ |
97 | if (is_mask) { \ |
98 | if ((match)->mask) \ | |
99 | memcpy(&(match)->mask->key.field, value_p, len);\ | |
a1c564be AZ |
100 | } else { \ |
101 | memcpy(&(match)->key->field, value_p, len); \ | |
102 | } \ | |
103 | } while (0) | |
104 | ||
105 | void ovs_match_init(struct sw_flow_match *match, | |
106 | struct sw_flow_key *key, | |
107 | struct sw_flow_mask *mask) | |
108 | { | |
109 | memset(match, 0, sizeof(*match)); | |
110 | match->key = key; | |
111 | match->mask = mask; | |
112 | ||
113 | memset(key, 0, sizeof(*key)); | |
114 | ||
115 | if (mask) { | |
116 | memset(&mask->key, 0, sizeof(mask->key)); | |
117 | mask->range.start = mask->range.end = 0; | |
118 | } | |
119 | } | |
120 | ||
121 | static bool ovs_match_validate(const struct sw_flow_match *match, | |
122 | u64 key_attrs, u64 mask_attrs) | |
123 | { | |
124 | u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET; | |
125 | u64 mask_allowed = key_attrs; /* At most allow all key attributes */ | |
126 | ||
127 | /* The following mask attributes allowed only if they | |
128 | * pass the validation tests. */ | |
129 | mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4) | |
130 | | (1ULL << OVS_KEY_ATTR_IPV6) | |
131 | | (1ULL << OVS_KEY_ATTR_TCP) | |
132 | | (1ULL << OVS_KEY_ATTR_UDP) | |
133 | | (1ULL << OVS_KEY_ATTR_ICMP) | |
134 | | (1ULL << OVS_KEY_ATTR_ICMPV6) | |
135 | | (1ULL << OVS_KEY_ATTR_ARP) | |
136 | | (1ULL << OVS_KEY_ATTR_ND)); | |
137 | ||
202c1051 AZ |
138 | /* Always allowed mask fields. */ |
139 | mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL) | |
33fd9f59 AZ |
140 | | (1ULL << OVS_KEY_ATTR_IN_PORT) |
141 | | (11ULL << OVS_KEY_ATTR_ETHERTYPE)); | |
a1c564be AZ |
142 | |
143 | /* Check key attributes. */ | |
144 | if (match->key->eth.type == htons(ETH_P_ARP) | |
145 | || match->key->eth.type == htons(ETH_P_RARP)) { | |
146 | key_expected |= 1ULL << OVS_KEY_ATTR_ARP; | |
147 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
148 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP; | |
149 | } | |
150 | ||
151 | if (match->key->eth.type == htons(ETH_P_IP)) { | |
152 | key_expected |= 1ULL << OVS_KEY_ATTR_IPV4; | |
153 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
154 | mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4; | |
155 | ||
156 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { | |
157 | if (match->key->ip.proto == IPPROTO_UDP) { | |
158 | key_expected |= 1ULL << OVS_KEY_ATTR_UDP; | |
159 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
160 | mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP; | |
161 | } | |
162 | ||
163 | if (match->key->ip.proto == IPPROTO_TCP) { | |
164 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP; | |
165 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
166 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP; | |
167 | } | |
168 | ||
169 | if (match->key->ip.proto == IPPROTO_ICMP) { | |
170 | key_expected |= 1ULL << OVS_KEY_ATTR_ICMP; | |
171 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
172 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP; | |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | if (match->key->eth.type == htons(ETH_P_IPV6)) { | |
178 | key_expected |= 1ULL << OVS_KEY_ATTR_IPV6; | |
179 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
180 | mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6; | |
181 | ||
182 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { | |
183 | if (match->key->ip.proto == IPPROTO_UDP) { | |
184 | key_expected |= 1ULL << OVS_KEY_ATTR_UDP; | |
185 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
186 | mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP; | |
187 | } | |
188 | ||
189 | if (match->key->ip.proto == IPPROTO_TCP) { | |
190 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP; | |
191 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
192 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP; | |
193 | } | |
194 | ||
195 | if (match->key->ip.proto == IPPROTO_ICMPV6) { | |
196 | key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6; | |
197 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
198 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6; | |
199 | ||
200 | if (match->key->ipv6.tp.src == | |
201 | htons(NDISC_NEIGHBOUR_SOLICITATION) || | |
202 | match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
203 | key_expected |= 1ULL << OVS_KEY_ATTR_ND; | |
204 | if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff))) | |
205 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ND; | |
206 | } | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
1b936472 | 211 | if ((key_attrs & key_expected) != key_expected) { |
a1c564be | 212 | /* Key attributes check failed. */ |
1b936472 AZ |
213 | OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n", |
214 | key_attrs, key_expected); | |
a1c564be | 215 | return false; |
1b936472 | 216 | } |
a1c564be | 217 | |
1b936472 | 218 | if ((mask_attrs & mask_allowed) != mask_attrs) { |
a1c564be | 219 | /* Mask attributes check failed. */ |
1b936472 AZ |
220 | OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n", |
221 | mask_attrs, mask_allowed); | |
a1c564be | 222 | return false; |
1b936472 | 223 | } |
a1c564be AZ |
224 | |
225 | return true; | |
226 | } | |
227 | ||
2db65bf7 JG |
228 | static int check_header(struct sk_buff *skb, int len) |
229 | { | |
230 | if (unlikely(skb->len < len)) | |
231 | return -EINVAL; | |
232 | if (unlikely(!pskb_may_pull(skb, len))) | |
233 | return -ENOMEM; | |
234 | return 0; | |
235 | } | |
236 | ||
6455100f | 237 | static bool arphdr_ok(struct sk_buff *skb) |
a26ef517 | 238 | { |
2db65bf7 JG |
239 | return pskb_may_pull(skb, skb_network_offset(skb) + |
240 | sizeof(struct arp_eth_header)); | |
a26ef517 JP |
241 | } |
242 | ||
6455100f | 243 | static int check_iphdr(struct sk_buff *skb) |
064af421 | 244 | { |
4c1ad233 BP |
245 | unsigned int nh_ofs = skb_network_offset(skb); |
246 | unsigned int ip_len; | |
2db65bf7 | 247 | int err; |
4c1ad233 | 248 | |
2db65bf7 JG |
249 | err = check_header(skb, nh_ofs + sizeof(struct iphdr)); |
250 | if (unlikely(err)) | |
251 | return err; | |
4c1ad233 BP |
252 | |
253 | ip_len = ip_hdrlen(skb); | |
2db65bf7 JG |
254 | if (unlikely(ip_len < sizeof(struct iphdr) || |
255 | skb->len < nh_ofs + ip_len)) | |
4c1ad233 BP |
256 | return -EINVAL; |
257 | ||
4c1ad233 BP |
258 | skb_set_transport_header(skb, nh_ofs + ip_len); |
259 | return 0; | |
064af421 BP |
260 | } |
261 | ||
6455100f | 262 | static bool tcphdr_ok(struct sk_buff *skb) |
064af421 BP |
263 | { |
264 | int th_ofs = skb_transport_offset(skb); | |
2db65bf7 JG |
265 | int tcp_len; |
266 | ||
267 | if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr)))) | |
268 | return false; | |
269 | ||
270 | tcp_len = tcp_hdrlen(skb); | |
271 | if (unlikely(tcp_len < sizeof(struct tcphdr) || | |
272 | skb->len < th_ofs + tcp_len)) | |
273 | return false; | |
274 | ||
275 | return true; | |
064af421 BP |
276 | } |
277 | ||
6455100f | 278 | static bool udphdr_ok(struct sk_buff *skb) |
064af421 | 279 | { |
2db65bf7 JG |
280 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
281 | sizeof(struct udphdr)); | |
064af421 BP |
282 | } |
283 | ||
6455100f | 284 | static bool icmphdr_ok(struct sk_buff *skb) |
064af421 | 285 | { |
2db65bf7 JG |
286 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
287 | sizeof(struct icmphdr)); | |
064af421 BP |
288 | } |
289 | ||
850b6b3b | 290 | u64 ovs_flow_used_time(unsigned long flow_jiffies) |
ec58547a JG |
291 | { |
292 | struct timespec cur_ts; | |
293 | u64 cur_ms, idle_ms; | |
294 | ||
295 | ktime_get_ts(&cur_ts); | |
296 | idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); | |
297 | cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC + | |
298 | cur_ts.tv_nsec / NSEC_PER_MSEC; | |
299 | ||
300 | return cur_ms - idle_ms; | |
301 | } | |
302 | ||
a1c564be | 303 | static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key) |
d31f1109 JP |
304 | { |
305 | unsigned int nh_ofs = skb_network_offset(skb); | |
306 | unsigned int nh_len; | |
307 | int payload_ofs; | |
d31f1109 JP |
308 | struct ipv6hdr *nh; |
309 | uint8_t nexthdr; | |
0fd0d083 | 310 | __be16 frag_off; |
2db65bf7 | 311 | int err; |
d31f1109 | 312 | |
2db65bf7 JG |
313 | err = check_header(skb, nh_ofs + sizeof(*nh)); |
314 | if (unlikely(err)) | |
315 | return err; | |
d31f1109 JP |
316 | |
317 | nh = ipv6_hdr(skb); | |
318 | nexthdr = nh->nexthdr; | |
319 | payload_ofs = (u8 *)(nh + 1) - skb->data; | |
d31f1109 | 320 | |
28bad473 | 321 | key->ip.proto = NEXTHDR_NONE; |
530180fd | 322 | key->ip.tos = ipv6_get_dsfield(nh); |
a61680c6 | 323 | key->ip.ttl = nh->hop_limit; |
fa8223b7 | 324 | key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); |
858d1026 JG |
325 | key->ipv6.addr.src = nh->saddr; |
326 | key->ipv6.addr.dst = nh->daddr; | |
d31f1109 | 327 | |
0fd0d083 | 328 | payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off); |
bfef4717 | 329 | if (unlikely(payload_ofs < 0)) |
d31f1109 | 330 | return -EINVAL; |
bfef4717 | 331 | |
0fd0d083 JG |
332 | if (frag_off) { |
333 | if (frag_off & htons(~0x7)) | |
334 | key->ip.frag = OVS_FRAG_TYPE_LATER; | |
335 | else | |
336 | key->ip.frag = OVS_FRAG_TYPE_FIRST; | |
337 | } | |
338 | ||
d31f1109 | 339 | nh_len = payload_ofs - nh_ofs; |
d31f1109 | 340 | skb_set_transport_header(skb, nh_ofs + nh_len); |
28bad473 | 341 | key->ip.proto = nexthdr; |
d31f1109 JP |
342 | return nh_len; |
343 | } | |
344 | ||
345 | static bool icmp6hdr_ok(struct sk_buff *skb) | |
346 | { | |
2db65bf7 JG |
347 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
348 | sizeof(struct icmp6hdr)); | |
d31f1109 | 349 | } |
ec58547a | 350 | |
529db635 JG |
351 | void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src, |
352 | const struct sw_flow_mask *mask) | |
a1c564be AZ |
353 | { |
354 | u8 *m = (u8 *)&mask->key + mask->range.start; | |
355 | u8 *s = (u8 *)src + mask->range.start; | |
356 | u8 *d = (u8 *)dst + mask->range.start; | |
357 | int i; | |
358 | ||
359 | memset(dst, 0, sizeof(*dst)); | |
360 | for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) { | |
361 | *d = *s & *m; | |
362 | d++, s++, m++; | |
363 | } | |
364 | } | |
365 | ||
064af421 BP |
366 | #define TCP_FLAGS_OFFSET 13 |
367 | #define TCP_FLAG_MASK 0x3f | |
368 | ||
850b6b3b | 369 | void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb) |
064af421 | 370 | { |
064af421 BP |
371 | u8 tcp_flags = 0; |
372 | ||
11460e23 JG |
373 | if ((flow->key.eth.type == htons(ETH_P_IP) || |
374 | flow->key.eth.type == htons(ETH_P_IPV6)) && | |
9c47b45a JG |
375 | flow->key.ip.proto == IPPROTO_TCP && |
376 | likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) { | |
abfec865 BP |
377 | u8 *tcp = (u8 *)tcp_hdr(skb); |
378 | tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK; | |
064af421 BP |
379 | } |
380 | ||
e9141eec | 381 | spin_lock(&flow->lock); |
6bfafa55 | 382 | flow->used = jiffies; |
064af421 BP |
383 | flow->packet_count++; |
384 | flow->byte_count += skb->len; | |
385 | flow->tcp_flags |= tcp_flags; | |
e9141eec | 386 | spin_unlock(&flow->lock); |
064af421 BP |
387 | } |
388 | ||
9b405f1a | 389 | struct sw_flow_actions *ovs_flow_actions_alloc(int size) |
064af421 BP |
390 | { |
391 | struct sw_flow_actions *sfa; | |
392 | ||
9b405f1a | 393 | if (size > MAX_ACTIONS_BUFSIZE) |
064af421 BP |
394 | return ERR_PTR(-EINVAL); |
395 | ||
ba400435 | 396 | sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL); |
064af421 BP |
397 | if (!sfa) |
398 | return ERR_PTR(-ENOMEM); | |
399 | ||
9b405f1a | 400 | sfa->actions_len = 0; |
064af421 BP |
401 | return sfa; |
402 | } | |
403 | ||
850b6b3b | 404 | struct sw_flow *ovs_flow_alloc(void) |
560e8022 JG |
405 | { |
406 | struct sw_flow *flow; | |
407 | ||
408 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); | |
409 | if (!flow) | |
410 | return ERR_PTR(-ENOMEM); | |
411 | ||
412 | spin_lock_init(&flow->lock); | |
6f04e94a | 413 | flow->sf_acts = NULL; |
a1c564be | 414 | flow->mask = NULL; |
064af421 | 415 | |
560e8022 JG |
416 | return flow; |
417 | } | |
418 | ||
be2ba156 | 419 | static struct hlist_head *find_bucket(struct flow_table *table, u32 hash) |
8d5ebd83 | 420 | { |
acd051f1 | 421 | hash = jhash_1word(hash, table->hash_seed); |
3544358a PS |
422 | return flex_array_get(table->buckets, |
423 | (hash & (table->n_buckets - 1))); | |
424 | } | |
425 | ||
be2ba156 | 426 | static struct flex_array *alloc_buckets(unsigned int n_buckets) |
3544358a | 427 | { |
be2ba156 | 428 | struct flex_array *buckets; |
3544358a PS |
429 | int i, err; |
430 | ||
8e1a83d8 | 431 | buckets = flex_array_alloc(sizeof(struct hlist_head), |
3544358a PS |
432 | n_buckets, GFP_KERNEL); |
433 | if (!buckets) | |
434 | return NULL; | |
435 | ||
436 | err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); | |
437 | if (err) { | |
438 | flex_array_free(buckets); | |
439 | return NULL; | |
440 | } | |
441 | ||
442 | for (i = 0; i < n_buckets; i++) | |
443 | INIT_HLIST_HEAD((struct hlist_head *) | |
444 | flex_array_get(buckets, i)); | |
445 | ||
446 | return buckets; | |
447 | } | |
448 | ||
6455100f | 449 | static void free_buckets(struct flex_array *buckets) |
3544358a PS |
450 | { |
451 | flex_array_free(buckets); | |
452 | } | |
453 | ||
ede77a46 | 454 | static struct flow_table *__flow_tbl_alloc(int new_size) |
3544358a PS |
455 | { |
456 | struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL); | |
457 | ||
458 | if (!table) | |
459 | return NULL; | |
460 | ||
461 | table->buckets = alloc_buckets(new_size); | |
462 | ||
463 | if (!table->buckets) { | |
464 | kfree(table); | |
465 | return NULL; | |
466 | } | |
467 | table->n_buckets = new_size; | |
468 | table->count = 0; | |
acd051f1 PS |
469 | table->node_ver = 0; |
470 | table->keep_flows = false; | |
471 | get_random_bytes(&table->hash_seed, sizeof(u32)); | |
ede77a46 | 472 | table->mask_list = NULL; |
3544358a PS |
473 | |
474 | return table; | |
475 | } | |
fb8c9347 | 476 | |
a1c564be | 477 | static void __flow_tbl_destroy(struct flow_table *table) |
3544358a PS |
478 | { |
479 | int i; | |
480 | ||
acd051f1 PS |
481 | if (table->keep_flows) |
482 | goto skip_flows; | |
483 | ||
3544358a PS |
484 | for (i = 0; i < table->n_buckets; i++) { |
485 | struct sw_flow *flow; | |
486 | struct hlist_head *head = flex_array_get(table->buckets, i); | |
f8dfbcb7 | 487 | struct hlist_node *n; |
acd051f1 | 488 | int ver = table->node_ver; |
3544358a | 489 | |
f8dfbcb7 | 490 | hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) { |
16c6b921 | 491 | hlist_del(&flow->hash_node[ver]); |
a1c564be | 492 | ovs_flow_free(flow, false); |
3544358a PS |
493 | } |
494 | } | |
495 | ||
ede77a46 AZ |
496 | BUG_ON(!list_empty(table->mask_list)); |
497 | kfree(table->mask_list); | |
498 | ||
acd051f1 | 499 | skip_flows: |
3544358a PS |
500 | free_buckets(table->buckets); |
501 | kfree(table); | |
502 | } | |
503 | ||
ede77a46 AZ |
504 | struct flow_table *ovs_flow_tbl_alloc(int new_size) |
505 | { | |
506 | struct flow_table *table = __flow_tbl_alloc(new_size); | |
507 | ||
508 | if (!table) | |
509 | return NULL; | |
510 | ||
511 | table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL); | |
512 | if (!table->mask_list) { | |
513 | table->keep_flows = true; | |
514 | __flow_tbl_destroy(table); | |
515 | return NULL; | |
516 | } | |
517 | INIT_LIST_HEAD(table->mask_list); | |
518 | ||
519 | return table; | |
520 | } | |
521 | ||
3544358a PS |
522 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) |
523 | { | |
524 | struct flow_table *table = container_of(rcu, struct flow_table, rcu); | |
525 | ||
a1c564be | 526 | __flow_tbl_destroy(table); |
3544358a PS |
527 | } |
528 | ||
a1c564be | 529 | void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred) |
3544358a | 530 | { |
6455100f PS |
531 | if (!table) |
532 | return; | |
3544358a | 533 | |
a1c564be AZ |
534 | if (deferred) |
535 | call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb); | |
536 | else | |
537 | __flow_tbl_destroy(table); | |
3544358a PS |
538 | } |
539 | ||
a1c564be | 540 | struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last) |
3544358a PS |
541 | { |
542 | struct sw_flow *flow; | |
543 | struct hlist_head *head; | |
acd051f1 | 544 | int ver; |
3544358a PS |
545 | int i; |
546 | ||
acd051f1 | 547 | ver = table->node_ver; |
3544358a PS |
548 | while (*bucket < table->n_buckets) { |
549 | i = 0; | |
550 | head = flex_array_get(table->buckets, *bucket); | |
f8dfbcb7 | 551 | hlist_for_each_entry_rcu(flow, head, hash_node[ver]) { |
3544358a PS |
552 | if (i < *last) { |
553 | i++; | |
554 | continue; | |
555 | } | |
556 | *last = i + 1; | |
557 | return flow; | |
558 | } | |
559 | (*bucket)++; | |
560 | *last = 0; | |
561 | } | |
562 | ||
563 | return NULL; | |
564 | } | |
565 | ||
a1c564be | 566 | static void __tbl_insert(struct flow_table *table, struct sw_flow *flow) |
13e24889 PS |
567 | { |
568 | struct hlist_head *head; | |
a1c564be | 569 | |
13e24889 PS |
570 | head = find_bucket(table, flow->hash); |
571 | hlist_add_head_rcu(&flow->hash_node[table->node_ver], head); | |
a1c564be | 572 | |
13e24889 PS |
573 | table->count++; |
574 | } | |
575 | ||
acd051f1 | 576 | static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new) |
3544358a | 577 | { |
acd051f1 | 578 | int old_ver; |
3544358a PS |
579 | int i; |
580 | ||
acd051f1 PS |
581 | old_ver = old->node_ver; |
582 | new->node_ver = !old_ver; | |
3544358a | 583 | |
acd051f1 PS |
584 | /* Insert in new table. */ |
585 | for (i = 0; i < old->n_buckets; i++) { | |
3544358a PS |
586 | struct sw_flow *flow; |
587 | struct hlist_head *head; | |
3544358a | 588 | |
acd051f1 | 589 | head = flex_array_get(old->buckets, i); |
3544358a | 590 | |
f8dfbcb7 | 591 | hlist_for_each_entry(flow, head, hash_node[old_ver]) |
a1c564be | 592 | __tbl_insert(new, flow); |
3544358a | 593 | } |
a1c564be AZ |
594 | |
595 | new->mask_list = old->mask_list; | |
acd051f1 PS |
596 | old->keep_flows = true; |
597 | } | |
598 | ||
599 | static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets) | |
600 | { | |
601 | struct flow_table *new_table; | |
602 | ||
ede77a46 | 603 | new_table = __flow_tbl_alloc(n_buckets); |
acd051f1 PS |
604 | if (!new_table) |
605 | return ERR_PTR(-ENOMEM); | |
606 | ||
607 | flow_table_copy_flows(table, new_table); | |
3544358a PS |
608 | |
609 | return new_table; | |
610 | } | |
611 | ||
acd051f1 PS |
612 | struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table) |
613 | { | |
614 | return __flow_tbl_rehash(table, table->n_buckets); | |
615 | } | |
616 | ||
617 | struct flow_table *ovs_flow_tbl_expand(struct flow_table *table) | |
618 | { | |
619 | return __flow_tbl_rehash(table, table->n_buckets * 2); | |
620 | } | |
621 | ||
a1c564be | 622 | static void __flow_free(struct sw_flow *flow) |
9321954a | 623 | { |
9321954a JG |
624 | kfree((struct sf_flow_acts __force *)flow->sf_acts); |
625 | kmem_cache_free(flow_cache, flow); | |
626 | } | |
627 | ||
064af421 BP |
628 | static void rcu_free_flow_callback(struct rcu_head *rcu) |
629 | { | |
630 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
fb8c9347 | 631 | |
a1c564be | 632 | __flow_free(flow); |
064af421 BP |
633 | } |
634 | ||
a1c564be | 635 | void ovs_flow_free(struct sw_flow *flow, bool deferred) |
064af421 | 636 | { |
a1c564be AZ |
637 | if (!flow) |
638 | return; | |
639 | ||
3d097064 | 640 | ovs_sw_flow_mask_del_ref(flow->mask, deferred); |
a1c564be AZ |
641 | |
642 | if (deferred) | |
643 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
644 | else | |
645 | __flow_free(flow); | |
064af421 BP |
646 | } |
647 | ||
850b6b3b | 648 | /* RCU callback used by ovs_flow_deferred_free_acts. */ |
064af421 BP |
649 | static void rcu_free_acts_callback(struct rcu_head *rcu) |
650 | { | |
d295e8e9 | 651 | struct sw_flow_actions *sf_acts = container_of(rcu, |
064af421 | 652 | struct sw_flow_actions, rcu); |
ba400435 | 653 | kfree(sf_acts); |
064af421 BP |
654 | } |
655 | ||
656 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. | |
657 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
850b6b3b | 658 | void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts) |
064af421 BP |
659 | { |
660 | call_rcu(&sf_acts->rcu, rcu_free_acts_callback); | |
661 | } | |
662 | ||
2db65bf7 | 663 | static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
064af421 | 664 | { |
50f06e16 BP |
665 | struct qtag_prefix { |
666 | __be16 eth_type; /* ETH_P_8021Q */ | |
667 | __be16 tci; | |
668 | }; | |
669 | struct qtag_prefix *qp; | |
670 | ||
8ddc056d BP |
671 | if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))) |
672 | return 0; | |
673 | ||
2db65bf7 JG |
674 | if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) + |
675 | sizeof(__be16)))) | |
676 | return -ENOMEM; | |
50f06e16 BP |
677 | |
678 | qp = (struct qtag_prefix *) skb->data; | |
76abe283 | 679 | key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT); |
50f06e16 | 680 | __skb_pull(skb, sizeof(struct qtag_prefix)); |
2db65bf7 JG |
681 | |
682 | return 0; | |
50f06e16 BP |
683 | } |
684 | ||
685 | static __be16 parse_ethertype(struct sk_buff *skb) | |
064af421 | 686 | { |
50f06e16 BP |
687 | struct llc_snap_hdr { |
688 | u8 dsap; /* Always 0xAA */ | |
689 | u8 ssap; /* Always 0xAA */ | |
690 | u8 ctrl; | |
691 | u8 oui[3]; | |
8dda8c9b | 692 | __be16 ethertype; |
50f06e16 BP |
693 | }; |
694 | struct llc_snap_hdr *llc; | |
695 | __be16 proto; | |
696 | ||
697 | proto = *(__be16 *) skb->data; | |
698 | __skb_pull(skb, sizeof(__be16)); | |
699 | ||
7cd46155 | 700 | if (ntohs(proto) >= ETH_P_802_3_MIN) |
50f06e16 BP |
701 | return proto; |
702 | ||
2db65bf7 | 703 | if (skb->len < sizeof(struct llc_snap_hdr)) |
36956a7d | 704 | return htons(ETH_P_802_2); |
50f06e16 | 705 | |
2db65bf7 JG |
706 | if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) |
707 | return htons(0); | |
708 | ||
50f06e16 BP |
709 | llc = (struct llc_snap_hdr *) skb->data; |
710 | if (llc->dsap != LLC_SAP_SNAP || | |
711 | llc->ssap != LLC_SAP_SNAP || | |
712 | (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) | |
36956a7d | 713 | return htons(ETH_P_802_2); |
50f06e16 BP |
714 | |
715 | __skb_pull(skb, sizeof(struct llc_snap_hdr)); | |
9e69bc5f | 716 | |
7cd46155 | 717 | if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN) |
9e69bc5f RL |
718 | return llc->ethertype; |
719 | ||
720 | return htons(ETH_P_802_2); | |
064af421 BP |
721 | } |
722 | ||
685a51a5 | 723 | static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, |
a1c564be | 724 | int nh_len) |
685a51a5 | 725 | { |
685a51a5 JP |
726 | struct icmp6hdr *icmp = icmp6_hdr(skb); |
727 | ||
728 | /* The ICMPv6 type and code fields use the 16-bit transport port | |
bfef4717 JG |
729 | * fields, so we need to store them in 16-bit network byte order. |
730 | */ | |
76abe283 AE |
731 | key->ipv6.tp.src = htons(icmp->icmp6_type); |
732 | key->ipv6.tp.dst = htons(icmp->icmp6_code); | |
685a51a5 | 733 | |
bfef4717 JG |
734 | if (icmp->icmp6_code == 0 && |
735 | (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || | |
736 | icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
e977ba19 | 737 | int icmp_len = skb->len - skb_transport_offset(skb); |
685a51a5 JP |
738 | struct nd_msg *nd; |
739 | int offset; | |
740 | ||
741 | /* In order to process neighbor discovery options, we need the | |
bfef4717 JG |
742 | * entire packet. |
743 | */ | |
744 | if (unlikely(icmp_len < sizeof(*nd))) | |
a1c564be AZ |
745 | return 0; |
746 | ||
747 | if (unlikely(skb_linearize(skb))) | |
748 | return -ENOMEM; | |
685a51a5 JP |
749 | |
750 | nd = (struct nd_msg *)skb_transport_header(skb); | |
858d1026 | 751 | key->ipv6.nd.target = nd->target; |
685a51a5 JP |
752 | |
753 | icmp_len -= sizeof(*nd); | |
754 | offset = 0; | |
755 | while (icmp_len >= 8) { | |
6455100f PS |
756 | struct nd_opt_hdr *nd_opt = |
757 | (struct nd_opt_hdr *)(nd->opt + offset); | |
685a51a5 JP |
758 | int opt_len = nd_opt->nd_opt_len * 8; |
759 | ||
bfef4717 | 760 | if (unlikely(!opt_len || opt_len > icmp_len)) |
a1c564be | 761 | return 0; |
685a51a5 | 762 | |
bfef4717 JG |
763 | /* Store the link layer address if the appropriate |
764 | * option is provided. It is considered an error if | |
765 | * the same link layer option is specified twice. | |
766 | */ | |
685a51a5 | 767 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR |
bfef4717 | 768 | && opt_len == 8) { |
76abe283 | 769 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll))) |
685a51a5 | 770 | goto invalid; |
76abe283 | 771 | memcpy(key->ipv6.nd.sll, |
bfef4717 | 772 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 | 773 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR |
bfef4717 | 774 | && opt_len == 8) { |
76abe283 | 775 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll))) |
685a51a5 | 776 | goto invalid; |
76abe283 | 777 | memcpy(key->ipv6.nd.tll, |
bfef4717 | 778 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 JP |
779 | } |
780 | ||
781 | icmp_len -= opt_len; | |
782 | offset += opt_len; | |
783 | } | |
784 | } | |
785 | ||
a1c564be | 786 | return 0; |
685a51a5 JP |
787 | |
788 | invalid: | |
76abe283 AE |
789 | memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target)); |
790 | memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll)); | |
791 | memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll)); | |
685a51a5 | 792 | |
a1c564be | 793 | return 0; |
685a51a5 JP |
794 | } |
795 | ||
a31e0e31 | 796 | /** |
850b6b3b | 797 | * ovs_flow_extract - extracts a flow key from an Ethernet frame. |
a31e0e31 BP |
798 | * @skb: sk_buff that contains the frame, with skb->data pointing to the |
799 | * Ethernet header | |
800 | * @in_port: port number on which @skb was received. | |
801 | * @key: output flow key | |
76abe283 | 802 | * @key_lenp: length of output flow key |
a31e0e31 BP |
803 | * |
804 | * The caller must ensure that skb->len >= ETH_HLEN. | |
805 | * | |
4c1ad233 BP |
806 | * Returns 0 if successful, otherwise a negative errno value. |
807 | * | |
59a18f80 BP |
808 | * Initializes @skb header pointers as follows: |
809 | * | |
810 | * - skb->mac_header: the Ethernet header. | |
811 | * | |
812 | * - skb->network_header: just past the Ethernet header, or just past the | |
813 | * VLAN header, to the first byte of the Ethernet payload. | |
814 | * | |
ae9020cf | 815 | * - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6 |
d31f1109 JP |
816 | * on output, then just past the IP header, if one is present and |
817 | * of a correct length, otherwise the same as skb->network_header. | |
ae9020cf | 818 | * For other key->eth.type values it is left untouched. |
a31e0e31 | 819 | */ |
a1c564be | 820 | int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key) |
064af421 | 821 | { |
a1c564be | 822 | int error; |
064af421 | 823 | struct ethhdr *eth; |
064af421 | 824 | |
84c17d98 | 825 | memset(key, 0, sizeof(*key)); |
abff858b PS |
826 | |
827 | key->phy.priority = skb->priority; | |
356af50b | 828 | if (OVS_CB(skb)->tun_key) |
6694e498 | 829 | memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key)); |
abff858b | 830 | key->phy.in_port = in_port; |
72e8bf28 | 831 | key->phy.skb_mark = skb_get_mark(skb); |
064af421 | 832 | |
064af421 | 833 | skb_reset_mac_header(skb); |
064af421 | 834 | |
2db65bf7 JG |
835 | /* Link layer. We are guaranteed to have at least the 14 byte Ethernet |
836 | * header in the linear data area. | |
837 | */ | |
50f06e16 | 838 | eth = eth_hdr(skb); |
76abe283 AE |
839 | memcpy(key->eth.src, eth->h_source, ETH_ALEN); |
840 | memcpy(key->eth.dst, eth->h_dest, ETH_ALEN); | |
841 | ||
50f06e16 | 842 | __skb_pull(skb, 2 * ETH_ALEN); |
3cfede14 PS |
843 | /* We are going to push all headers that we pull, so no need to |
844 | * update skb->csum here. */ | |
6ce39213 JG |
845 | |
846 | if (vlan_tx_tag_present(skb)) | |
76abe283 | 847 | key->eth.tci = htons(vlan_get_tci(skb)); |
6ce39213 | 848 | else if (eth->h_proto == htons(ETH_P_8021Q)) |
2db65bf7 JG |
849 | if (unlikely(parse_vlan(skb, key))) |
850 | return -ENOMEM; | |
6ce39213 | 851 | |
76abe283 AE |
852 | key->eth.type = parse_ethertype(skb); |
853 | if (unlikely(key->eth.type == htons(0))) | |
2db65bf7 JG |
854 | return -ENOMEM; |
855 | ||
50f06e16 | 856 | skb_reset_network_header(skb); |
2db65bf7 | 857 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
064af421 BP |
858 | |
859 | /* Network layer. */ | |
76abe283 | 860 | if (key->eth.type == htons(ETH_P_IP)) { |
4c1ad233 | 861 | struct iphdr *nh; |
7257b535 | 862 | __be16 offset; |
76abe283 | 863 | |
4c1ad233 BP |
864 | error = check_iphdr(skb); |
865 | if (unlikely(error)) { | |
866 | if (error == -EINVAL) { | |
867 | skb->transport_header = skb->network_header; | |
76abe283 | 868 | error = 0; |
4c1ad233 | 869 | } |
a1c564be | 870 | return error; |
4c1ad233 BP |
871 | } |
872 | ||
873 | nh = ip_hdr(skb); | |
76abe283 AE |
874 | key->ipv4.addr.src = nh->saddr; |
875 | key->ipv4.addr.dst = nh->daddr; | |
7257b535 | 876 | |
28bad473 | 877 | key->ip.proto = nh->protocol; |
530180fd | 878 | key->ip.tos = nh->tos; |
a61680c6 | 879 | key->ip.ttl = nh->ttl; |
064af421 | 880 | |
7257b535 BP |
881 | offset = nh->frag_off & htons(IP_OFFSET); |
882 | if (offset) { | |
9e44d715 | 883 | key->ip.frag = OVS_FRAG_TYPE_LATER; |
a1c564be | 884 | return 0; |
7257b535 BP |
885 | } |
886 | if (nh->frag_off & htons(IP_MF) || | |
887 | skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
9e44d715 | 888 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
b7a31ec1 | 889 | |
7257b535 | 890 | /* Transport layer. */ |
28bad473 | 891 | if (key->ip.proto == IPPROTO_TCP) { |
7257b535 | 892 | if (tcphdr_ok(skb)) { |
5f4d087c JG |
893 | struct tcphdr *tcp = tcp_hdr(skb); |
894 | key->ipv4.tp.src = tcp->source; | |
895 | key->ipv4.tp.dst = tcp->dest; | |
896 | } | |
28bad473 | 897 | } else if (key->ip.proto == IPPROTO_UDP) { |
7257b535 | 898 | if (udphdr_ok(skb)) { |
5f4d087c JG |
899 | struct udphdr *udp = udp_hdr(skb); |
900 | key->ipv4.tp.src = udp->source; | |
901 | key->ipv4.tp.dst = udp->dest; | |
902 | } | |
28bad473 | 903 | } else if (key->ip.proto == IPPROTO_ICMP) { |
7257b535 | 904 | if (icmphdr_ok(skb)) { |
5f4d087c JG |
905 | struct icmphdr *icmp = icmp_hdr(skb); |
906 | /* The ICMP type and code fields use the 16-bit | |
6455100f PS |
907 | * transport port fields, so we need to store |
908 | * them in 16-bit network byte order. */ | |
5f4d087c JG |
909 | key->ipv4.tp.src = htons(icmp->type); |
910 | key->ipv4.tp.dst = htons(icmp->code); | |
911 | } | |
912 | } | |
913 | ||
8087f5ff MM |
914 | } else if ((key->eth.type == htons(ETH_P_ARP) || |
915 | key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) { | |
a26ef517 JP |
916 | struct arp_eth_header *arp; |
917 | ||
918 | arp = (struct arp_eth_header *)skb_network_header(skb); | |
919 | ||
f5e86186 | 920 | if (arp->ar_hrd == htons(ARPHRD_ETHER) |
de3f65ea JP |
921 | && arp->ar_pro == htons(ETH_P_IP) |
922 | && arp->ar_hln == ETH_ALEN | |
923 | && arp->ar_pln == 4) { | |
924 | ||
925 | /* We only match on the lower 8 bits of the opcode. */ | |
b7a31ec1 | 926 | if (ntohs(arp->ar_op) <= 0xff) |
28bad473 | 927 | key->ip.proto = ntohs(arp->ar_op); |
a3d3ad0c MM |
928 | memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src)); |
929 | memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst)); | |
930 | memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN); | |
931 | memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN); | |
de3f65ea | 932 | } |
76abe283 | 933 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
d31f1109 JP |
934 | int nh_len; /* IPv6 Header + Extensions */ |
935 | ||
a1c564be | 936 | nh_len = parse_ipv6hdr(skb, key); |
d31f1109 | 937 | if (unlikely(nh_len < 0)) { |
a1c564be | 938 | if (nh_len == -EINVAL) { |
d31f1109 | 939 | skb->transport_header = skb->network_header; |
a1c564be AZ |
940 | error = 0; |
941 | } else { | |
76abe283 | 942 | error = nh_len; |
a1c564be AZ |
943 | } |
944 | return error; | |
d31f1109 JP |
945 | } |
946 | ||
9e44d715 | 947 | if (key->ip.frag == OVS_FRAG_TYPE_LATER) |
a1c564be | 948 | return 0; |
7257b535 | 949 | if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
9e44d715 | 950 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
7257b535 | 951 | |
d31f1109 | 952 | /* Transport layer. */ |
28bad473 | 953 | if (key->ip.proto == NEXTHDR_TCP) { |
d31f1109 JP |
954 | if (tcphdr_ok(skb)) { |
955 | struct tcphdr *tcp = tcp_hdr(skb); | |
76abe283 AE |
956 | key->ipv6.tp.src = tcp->source; |
957 | key->ipv6.tp.dst = tcp->dest; | |
d31f1109 | 958 | } |
28bad473 | 959 | } else if (key->ip.proto == NEXTHDR_UDP) { |
d31f1109 JP |
960 | if (udphdr_ok(skb)) { |
961 | struct udphdr *udp = udp_hdr(skb); | |
76abe283 AE |
962 | key->ipv6.tp.src = udp->source; |
963 | key->ipv6.tp.dst = udp->dest; | |
d31f1109 | 964 | } |
28bad473 | 965 | } else if (key->ip.proto == NEXTHDR_ICMP) { |
d31f1109 | 966 | if (icmp6hdr_ok(skb)) { |
a1c564be AZ |
967 | error = parse_icmpv6(skb, key, nh_len); |
968 | if (error) | |
969 | return error; | |
d31f1109 JP |
970 | } |
971 | } | |
064af421 | 972 | } |
76abe283 | 973 | |
a1c564be | 974 | return 0; |
064af421 BP |
975 | } |
976 | ||
13e24889 | 977 | static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len) |
8d5ebd83 | 978 | { |
13e24889 PS |
979 | return jhash2((u32 *)((u8 *)key + key_start), |
980 | DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0); | |
981 | } | |
982 | ||
a1c564be | 983 | static int flow_key_start(const struct sw_flow_key *key) |
13e24889 | 984 | { |
6694e498 | 985 | if (key->tun_key.ipv4_dst) |
13e24889 PS |
986 | return 0; |
987 | else | |
6694e498 | 988 | return offsetof(struct sw_flow_key, phy); |
8d5ebd83 JG |
989 | } |
990 | ||
a1c564be AZ |
991 | static bool __cmp_key(const struct sw_flow_key *key1, |
992 | const struct sw_flow_key *key2, int key_start, int key_len) | |
993 | { | |
994 | return !memcmp((u8 *)key1 + key_start, | |
995 | (u8 *)key2 + key_start, (key_len - key_start)); | |
996 | } | |
997 | ||
998 | static bool __flow_cmp_key(const struct sw_flow *flow, | |
999 | const struct sw_flow_key *key, int key_start, int key_len) | |
1000 | { | |
1001 | return __cmp_key(&flow->key, key, key_start, key_len); | |
1002 | } | |
1003 | ||
1004 | static bool __flow_cmp_unmasked_key(const struct sw_flow *flow, | |
1005 | const struct sw_flow_key *key, int key_start, int key_len) | |
1006 | { | |
1007 | return __cmp_key(&flow->unmasked_key, key, key_start, key_len); | |
1008 | } | |
1009 | ||
1010 | bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow, | |
1011 | const struct sw_flow_key *key, int key_len) | |
1012 | { | |
1013 | int key_start; | |
1014 | key_start = flow_key_start(key); | |
1015 | ||
1016 | return __flow_cmp_unmasked_key(flow, key, key_start, key_len); | |
1017 | ||
1018 | } | |
1019 | ||
1020 | struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table, | |
1021 | struct sw_flow_match *match) | |
1022 | { | |
1023 | struct sw_flow_key *unmasked = match->key; | |
1024 | int key_len = match->range.end; | |
1025 | struct sw_flow *flow; | |
1026 | ||
1027 | flow = ovs_flow_lookup(table, unmasked); | |
1028 | if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len))) | |
1029 | flow = NULL; | |
1030 | ||
1031 | return flow; | |
1032 | } | |
1033 | ||
1034 | static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table, | |
1035 | const struct sw_flow_key *flow_key, | |
1036 | struct sw_flow_mask *mask) | |
8d5ebd83 | 1037 | { |
3544358a | 1038 | struct sw_flow *flow; |
3544358a | 1039 | struct hlist_head *head; |
a1c564be AZ |
1040 | int key_start = mask->range.start; |
1041 | int key_len = mask->range.end; | |
3544358a | 1042 | u32 hash; |
a1c564be | 1043 | struct sw_flow_key masked_key; |
8d5ebd83 | 1044 | |
529db635 | 1045 | ovs_flow_key_mask(&masked_key, flow_key, mask); |
a1c564be | 1046 | hash = ovs_flow_hash(&masked_key, key_start, key_len); |
3544358a | 1047 | head = find_bucket(table, hash); |
f8dfbcb7 | 1048 | hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) { |
a99c219c AZ |
1049 | if (flow->mask == mask && |
1050 | __flow_cmp_key(flow, &masked_key, key_start, key_len)) | |
3544358a | 1051 | return flow; |
3544358a PS |
1052 | } |
1053 | return NULL; | |
1054 | } | |
1055 | ||
a1c564be AZ |
1056 | struct sw_flow *ovs_flow_lookup(struct flow_table *tbl, |
1057 | const struct sw_flow_key *key) | |
3544358a | 1058 | { |
a1c564be AZ |
1059 | struct sw_flow *flow = NULL; |
1060 | struct sw_flow_mask *mask; | |
1061 | ||
ede77a46 | 1062 | list_for_each_entry_rcu(mask, tbl->mask_list, list) { |
a1c564be AZ |
1063 | flow = ovs_masked_flow_lookup(tbl, key, mask); |
1064 | if (flow) /* Found */ | |
1065 | break; | |
1066 | } | |
1067 | ||
1068 | return flow; | |
3544358a PS |
1069 | } |
1070 | ||
a1c564be | 1071 | |
529db635 | 1072 | void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow) |
a1c564be | 1073 | { |
3d097064 AZ |
1074 | flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start, |
1075 | flow->mask->range.end); | |
a1c564be AZ |
1076 | __tbl_insert(table, flow); |
1077 | } | |
1078 | ||
1079 | void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow) | |
3544358a | 1080 | { |
49c7f80c | 1081 | BUG_ON(table->count == 0); |
acd051f1 PS |
1082 | hlist_del_rcu(&flow->hash_node[table->node_ver]); |
1083 | table->count--; | |
36956a7d BP |
1084 | } |
1085 | ||
df2c07f4 | 1086 | /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */ |
4094406f BP |
1087 | const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = { |
1088 | [OVS_KEY_ATTR_ENCAP] = -1, | |
a1bf209f BP |
1089 | [OVS_KEY_ATTR_PRIORITY] = sizeof(u32), |
1090 | [OVS_KEY_ATTR_IN_PORT] = sizeof(u32), | |
72e8bf28 | 1091 | [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32), |
df2c07f4 | 1092 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), |
fea393b1 | 1093 | [OVS_KEY_ATTR_VLAN] = sizeof(__be16), |
a1bf209f | 1094 | [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16), |
df2c07f4 JP |
1095 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), |
1096 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), | |
1097 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), | |
1098 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), | |
1099 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), | |
1100 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), | |
1101 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), | |
1102 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), | |
9b405f1a | 1103 | [OVS_KEY_ATTR_TUNNEL] = -1, |
80e5eed9 BP |
1104 | }; |
1105 | ||
a1c564be | 1106 | static bool is_all_zero(const u8 *fp, size_t size) |
34118cae | 1107 | { |
a1c564be | 1108 | int i; |
34118cae | 1109 | |
a1c564be AZ |
1110 | if (!fp) |
1111 | return false; | |
34118cae | 1112 | |
a1c564be AZ |
1113 | for (i = 0; i < size; i++) |
1114 | if (fp[i]) | |
1115 | return false; | |
34118cae | 1116 | |
a1c564be | 1117 | return true; |
34118cae BP |
1118 | } |
1119 | ||
a1c564be AZ |
1120 | static int __parse_flow_nlattrs(const struct nlattr *attr, |
1121 | const struct nlattr *a[], | |
1122 | u64 *attrsp, bool nz) | |
fea393b1 BP |
1123 | { |
1124 | const struct nlattr *nla; | |
1125 | u64 attrs; | |
1126 | int rem; | |
1127 | ||
a1c564be | 1128 | attrs = *attrsp; |
fea393b1 BP |
1129 | nla_for_each_nested(nla, attr, rem) { |
1130 | u16 type = nla_type(nla); | |
4094406f | 1131 | int expected_len; |
fea393b1 | 1132 | |
1b936472 AZ |
1133 | if (type > OVS_KEY_ATTR_MAX) { |
1134 | OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n", | |
1135 | type, OVS_KEY_ATTR_MAX); | |
1136 | } | |
4094406f | 1137 | |
1b936472 AZ |
1138 | if (attrs & (1ULL << type)) { |
1139 | OVS_NLERR("Duplicate key attribute (type %d).\n", type); | |
4094406f | 1140 | return -EINVAL; |
1b936472 | 1141 | } |
4094406f | 1142 | |
1b936472 AZ |
1143 | expected_len = ovs_key_lens[type]; |
1144 | if (nla_len(nla) != expected_len && expected_len != -1) { | |
1145 | OVS_NLERR("Key attribute has unexpected length (type=%d" | |
1146 | ", length=%d, expected=%d).\n", type, | |
1147 | nla_len(nla), expected_len); | |
a1c564be | 1148 | return -EINVAL; |
1b936472 | 1149 | } |
a1c564be AZ |
1150 | |
1151 | if (!nz || !is_all_zero(nla_data(nla), expected_len)) { | |
1152 | attrs |= 1ULL << type; | |
1153 | a[type] = nla; | |
1154 | } | |
fea393b1 | 1155 | } |
1b936472 AZ |
1156 | if (rem) { |
1157 | OVS_NLERR("Message has %d unknown bytes.\n", rem); | |
fea393b1 | 1158 | return -EINVAL; |
1b936472 | 1159 | } |
fea393b1 BP |
1160 | |
1161 | *attrsp = attrs; | |
1162 | return 0; | |
1163 | } | |
1164 | ||
a1c564be AZ |
1165 | static int parse_flow_mask_nlattrs(const struct nlattr *attr, |
1166 | const struct nlattr *a[], u64 *attrsp) | |
1167 | { | |
1168 | return __parse_flow_nlattrs(attr, a, attrsp, true); | |
1169 | } | |
1170 | ||
1171 | static int parse_flow_nlattrs(const struct nlattr *attr, | |
1172 | const struct nlattr *a[], u64 *attrsp) | |
1173 | { | |
1174 | return __parse_flow_nlattrs(attr, a, attrsp, false); | |
1175 | } | |
1176 | ||
9b405f1a | 1177 | int ipv4_tun_from_nlattr(const struct nlattr *attr, |
a1c564be | 1178 | struct sw_flow_match *match, bool is_mask) |
9b405f1a PS |
1179 | { |
1180 | struct nlattr *a; | |
1181 | int rem; | |
1182 | bool ttl = false; | |
5ebaf571 | 1183 | __be16 tun_flags = 0; |
9b405f1a PS |
1184 | |
1185 | nla_for_each_nested(a, attr, rem) { | |
1186 | int type = nla_type(a); | |
1187 | static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { | |
1188 | [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64), | |
1189 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32), | |
1190 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32), | |
1191 | [OVS_TUNNEL_KEY_ATTR_TOS] = 1, | |
1192 | [OVS_TUNNEL_KEY_ATTR_TTL] = 1, | |
1193 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0, | |
1194 | [OVS_TUNNEL_KEY_ATTR_CSUM] = 0, | |
1195 | }; | |
1196 | ||
1b936472 | 1197 | if (type > OVS_TUNNEL_KEY_ATTR_MAX) { |
17ce1965 | 1198 | OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n", |
1b936472 AZ |
1199 | type, OVS_TUNNEL_KEY_ATTR_MAX); |
1200 | return -EINVAL; | |
1201 | } | |
1202 | ||
1203 | if (ovs_tunnel_key_lens[type] != nla_len(a)) { | |
1204 | OVS_NLERR("IPv4 tunnel attribute type has unexpected " | |
17ce1965 | 1205 | " legnth (type=%d, length=%d, expected=%d).\n", |
1b936472 | 1206 | type, nla_len(a), ovs_tunnel_key_lens[type]); |
9b405f1a | 1207 | return -EINVAL; |
1b936472 | 1208 | } |
9b405f1a PS |
1209 | |
1210 | switch (type) { | |
1211 | case OVS_TUNNEL_KEY_ATTR_ID: | |
a1c564be AZ |
1212 | SW_FLOW_KEY_PUT(match, tun_key.tun_id, |
1213 | nla_get_be64(a), is_mask); | |
5ebaf571 | 1214 | tun_flags |= TUNNEL_KEY; |
9b405f1a PS |
1215 | break; |
1216 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
a1c564be AZ |
1217 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_src, |
1218 | nla_get_be32(a), is_mask); | |
9b405f1a PS |
1219 | break; |
1220 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
a1c564be AZ |
1221 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst, |
1222 | nla_get_be32(a), is_mask); | |
9b405f1a PS |
1223 | break; |
1224 | case OVS_TUNNEL_KEY_ATTR_TOS: | |
a1c564be AZ |
1225 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos, |
1226 | nla_get_u8(a), is_mask); | |
9b405f1a PS |
1227 | break; |
1228 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
a1c564be AZ |
1229 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl, |
1230 | nla_get_u8(a), is_mask); | |
9b405f1a PS |
1231 | ttl = true; |
1232 | break; | |
1233 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
5ebaf571 | 1234 | tun_flags |= TUNNEL_DONT_FRAGMENT; |
9b405f1a PS |
1235 | break; |
1236 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
5ebaf571 | 1237 | tun_flags |= TUNNEL_CSUM; |
9b405f1a PS |
1238 | break; |
1239 | default: | |
1240 | return -EINVAL; | |
9b405f1a PS |
1241 | } |
1242 | } | |
a1c564be AZ |
1243 | |
1244 | SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask); | |
1245 | ||
1b936472 AZ |
1246 | if (rem > 0) { |
1247 | OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem); | |
9b405f1a | 1248 | return -EINVAL; |
1b936472 | 1249 | } |
9b405f1a | 1250 | |
1b936472 AZ |
1251 | if (!match->key->tun_key.ipv4_dst) { |
1252 | OVS_NLERR("IPv4 tunnel destination address is zero.\n"); | |
9b405f1a | 1253 | return -EINVAL; |
1b936472 | 1254 | } |
9b405f1a | 1255 | |
1b936472 | 1256 | if (!ttl) { |
9450a6e0 | 1257 | OVS_NLERR("IPv4 tunnel TTL not specified.\n"); |
9b405f1a | 1258 | return -EINVAL; |
1b936472 | 1259 | } |
9b405f1a PS |
1260 | |
1261 | return 0; | |
1262 | } | |
1263 | ||
1264 | int ipv4_tun_to_nlattr(struct sk_buff *skb, | |
a1c564be AZ |
1265 | const struct ovs_key_ipv4_tunnel *tun_key, |
1266 | const struct ovs_key_ipv4_tunnel *output) | |
9b405f1a PS |
1267 | { |
1268 | struct nlattr *nla; | |
1269 | ||
1270 | nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL); | |
1271 | if (!nla) | |
1272 | return -EMSGSIZE; | |
1273 | ||
04b13268 | 1274 | if (output->tun_flags & TUNNEL_KEY && |
a1c564be | 1275 | nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id)) |
9b405f1a | 1276 | return -EMSGSIZE; |
04b13268 AZ |
1277 | if (output->ipv4_src && |
1278 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src)) | |
9b405f1a | 1279 | return -EMSGSIZE; |
04b13268 AZ |
1280 | if (output->ipv4_dst && |
1281 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst)) | |
9b405f1a | 1282 | return -EMSGSIZE; |
04b13268 AZ |
1283 | if (output->ipv4_tos && |
1284 | nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos)) | |
9b405f1a | 1285 | return -EMSGSIZE; |
aa20ff9d | 1286 | if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl)) |
9b405f1a | 1287 | return -EMSGSIZE; |
04b13268 | 1288 | if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) && |
9b405f1a PS |
1289 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT)) |
1290 | return -EMSGSIZE; | |
04b13268 | 1291 | if ((output->tun_flags & TUNNEL_CSUM) && |
9b405f1a PS |
1292 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM)) |
1293 | return -EMSGSIZE; | |
1294 | ||
1295 | nla_nest_end(skb, nla); | |
1296 | return 0; | |
1297 | } | |
1298 | ||
36956a7d | 1299 | |
a1c564be AZ |
1300 | static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs, |
1301 | const struct nlattr **a, bool is_mask) | |
1302 | { | |
1303 | if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) { | |
1304 | SW_FLOW_KEY_PUT(match, phy.priority, | |
1305 | nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask); | |
1306 | *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY); | |
34118cae | 1307 | } |
a1c564be AZ |
1308 | |
1309 | if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) { | |
34118cae | 1310 | u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]); |
a1c564be | 1311 | |
202c1051 AZ |
1312 | if (is_mask) |
1313 | in_port = 0xffffffff; /* Always exact match in_port. */ | |
1314 | else if (in_port >= DP_MAX_PORTS) | |
34118cae | 1315 | return -EINVAL; |
202c1051 | 1316 | |
a1c564be AZ |
1317 | SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask); |
1318 | *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT); | |
861687d4 JG |
1319 | } else if (!is_mask) { |
1320 | SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask); | |
34118cae | 1321 | } |
a1c564be AZ |
1322 | |
1323 | if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) { | |
72e8bf28 AA |
1324 | uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]); |
1325 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER) | |
1b936472 AZ |
1326 | if (!is_mask && mark != 0) { |
1327 | OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark); | |
72e8bf28 | 1328 | return -EINVAL; |
1b936472 | 1329 | } |
72e8bf28 | 1330 | #endif |
a1c564be AZ |
1331 | SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask); |
1332 | *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK); | |
1333 | } | |
1334 | if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) { | |
1335 | if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match, | |
1336 | is_mask)) | |
1337 | return -EINVAL; | |
1338 | *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL); | |
72e8bf28 | 1339 | } |
a1c564be AZ |
1340 | return 0; |
1341 | } | |
36956a7d | 1342 | |
a1c564be AZ |
1343 | static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs, |
1344 | const struct nlattr **a, bool is_mask) | |
1345 | { | |
1346 | int err; | |
828f5f64 | 1347 | u64 orig_attrs = attrs; |
5c7f5883 | 1348 | |
a1c564be AZ |
1349 | err = metadata_from_nlattrs(match, &attrs, a, is_mask); |
1350 | if (err) | |
1351 | return err; | |
76abe283 | 1352 | |
a1c564be AZ |
1353 | if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) { |
1354 | const struct ovs_key_ethernet *eth_key; | |
36956a7d | 1355 | |
a1c564be AZ |
1356 | eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]); |
1357 | SW_FLOW_KEY_MEMCPY(match, eth.src, | |
1358 | eth_key->eth_src, ETH_ALEN, is_mask); | |
1359 | SW_FLOW_KEY_MEMCPY(match, eth.dst, | |
1360 | eth_key->eth_dst, ETH_ALEN, is_mask); | |
1361 | attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET); | |
1362 | } | |
76abe283 | 1363 | |
a1c564be | 1364 | if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) { |
8ddc056d BP |
1365 | __be16 tci; |
1366 | ||
a1c564be | 1367 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); |
62f13eb2 AZ |
1368 | if (!(tci & htons(VLAN_TAG_PRESENT))) { |
1369 | if (is_mask) | |
1370 | OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n"); | |
1371 | else | |
1b936472 | 1372 | OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n"); |
62f13eb2 AZ |
1373 | |
1374 | return -EINVAL; | |
1375 | } | |
36956a7d | 1376 | |
a1c564be AZ |
1377 | SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask); |
1378 | attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN); | |
62f13eb2 AZ |
1379 | } else if (!is_mask) |
1380 | SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true); | |
8ddc056d | 1381 | |
a1c564be AZ |
1382 | if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) { |
1383 | __be16 eth_type; | |
8ddc056d | 1384 | |
a1c564be | 1385 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); |
33fd9f59 AZ |
1386 | if (is_mask) { |
1387 | /* Always exact match EtherType. */ | |
1388 | eth_type = htons(0xffff); | |
1389 | } else if (ntohs(eth_type) < ETH_P_802_3_MIN) { | |
1b936472 AZ |
1390 | OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n", |
1391 | ntohs(eth_type), ETH_P_802_3_MIN); | |
8ddc056d | 1392 | return -EINVAL; |
1b936472 | 1393 | } |
a1c564be AZ |
1394 | |
1395 | SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask); | |
1396 | attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
1397 | } else if (!is_mask) { | |
1398 | SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask); | |
34118cae BP |
1399 | } |
1400 | ||
a1c564be AZ |
1401 | if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) { |
1402 | const struct ovs_key_ipv4 *ipv4_key; | |
1403 | ||
1404 | ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]); | |
1b936472 AZ |
1405 | if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) { |
1406 | OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n", | |
1407 | ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX); | |
34118cae | 1408 | return -EINVAL; |
1b936472 | 1409 | } |
a1c564be AZ |
1410 | SW_FLOW_KEY_PUT(match, ip.proto, |
1411 | ipv4_key->ipv4_proto, is_mask); | |
1412 | SW_FLOW_KEY_PUT(match, ip.tos, | |
1413 | ipv4_key->ipv4_tos, is_mask); | |
1414 | SW_FLOW_KEY_PUT(match, ip.ttl, | |
1415 | ipv4_key->ipv4_ttl, is_mask); | |
1416 | SW_FLOW_KEY_PUT(match, ip.frag, | |
1417 | ipv4_key->ipv4_frag, is_mask); | |
1418 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, | |
1419 | ipv4_key->ipv4_src, is_mask); | |
1420 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, | |
1421 | ipv4_key->ipv4_dst, is_mask); | |
1422 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4); | |
34118cae BP |
1423 | } |
1424 | ||
a1c564be AZ |
1425 | if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) { |
1426 | const struct ovs_key_ipv6 *ipv6_key; | |
685a51a5 | 1427 | |
a1c564be | 1428 | ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]); |
1b936472 AZ |
1429 | if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) { |
1430 | OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n", | |
1431 | ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX); | |
34118cae | 1432 | return -EINVAL; |
1b936472 | 1433 | } |
a1c564be AZ |
1434 | SW_FLOW_KEY_PUT(match, ipv6.label, |
1435 | ipv6_key->ipv6_label, is_mask); | |
1436 | SW_FLOW_KEY_PUT(match, ip.proto, | |
1437 | ipv6_key->ipv6_proto, is_mask); | |
1438 | SW_FLOW_KEY_PUT(match, ip.tos, | |
1439 | ipv6_key->ipv6_tclass, is_mask); | |
1440 | SW_FLOW_KEY_PUT(match, ip.ttl, | |
1441 | ipv6_key->ipv6_hlimit, is_mask); | |
1442 | SW_FLOW_KEY_PUT(match, ip.frag, | |
1443 | ipv6_key->ipv6_frag, is_mask); | |
1444 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src, | |
1445 | ipv6_key->ipv6_src, | |
1446 | sizeof(match->key->ipv6.addr.src), | |
1447 | is_mask); | |
1448 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst, | |
1449 | ipv6_key->ipv6_dst, | |
1450 | sizeof(match->key->ipv6.addr.dst), | |
1451 | is_mask); | |
1452 | ||
1453 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6); | |
1454 | } | |
34118cae | 1455 | |
a1c564be AZ |
1456 | if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) { |
1457 | const struct ovs_key_arp *arp_key; | |
1458 | ||
1459 | arp_key = nla_data(a[OVS_KEY_ATTR_ARP]); | |
1b936472 AZ |
1460 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { |
1461 | OVS_NLERR("Unknown ARP opcode (opcode=%d).\n", | |
1462 | arp_key->arp_op); | |
34118cae | 1463 | return -EINVAL; |
1b936472 | 1464 | } |
a1c564be AZ |
1465 | |
1466 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, | |
1467 | arp_key->arp_sip, is_mask); | |
1468 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, | |
1469 | arp_key->arp_tip, is_mask); | |
1470 | SW_FLOW_KEY_PUT(match, ip.proto, | |
1471 | ntohs(arp_key->arp_op), is_mask); | |
1472 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha, | |
1473 | arp_key->arp_sha, ETH_ALEN, is_mask); | |
1474 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha, | |
1475 | arp_key->arp_tha, ETH_ALEN, is_mask); | |
1476 | ||
1477 | attrs &= ~(1ULL << OVS_KEY_ATTR_ARP); | |
1478 | } | |
1479 | ||
1480 | if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) { | |
1481 | const struct ovs_key_tcp *tcp_key; | |
1482 | ||
1483 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); | |
828f5f64 JP |
1484 | if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) { |
1485 | SW_FLOW_KEY_PUT(match, ipv4.tp.src, | |
1486 | tcp_key->tcp_src, is_mask); | |
1487 | SW_FLOW_KEY_PUT(match, ipv4.tp.dst, | |
1488 | tcp_key->tcp_dst, is_mask); | |
1489 | } else { | |
1490 | SW_FLOW_KEY_PUT(match, ipv6.tp.src, | |
1491 | tcp_key->tcp_src, is_mask); | |
1492 | SW_FLOW_KEY_PUT(match, ipv6.tp.dst, | |
1493 | tcp_key->tcp_dst, is_mask); | |
1494 | } | |
a1c564be AZ |
1495 | attrs &= ~(1ULL << OVS_KEY_ATTR_TCP); |
1496 | } | |
1497 | ||
1498 | if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) { | |
1499 | const struct ovs_key_udp *udp_key; | |
1500 | ||
1501 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); | |
828f5f64 JP |
1502 | if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) { |
1503 | SW_FLOW_KEY_PUT(match, ipv4.tp.src, | |
1504 | udp_key->udp_src, is_mask); | |
1505 | SW_FLOW_KEY_PUT(match, ipv4.tp.dst, | |
1506 | udp_key->udp_dst, is_mask); | |
1507 | } else { | |
1508 | SW_FLOW_KEY_PUT(match, ipv6.tp.src, | |
1509 | udp_key->udp_src, is_mask); | |
1510 | SW_FLOW_KEY_PUT(match, ipv6.tp.dst, | |
1511 | udp_key->udp_dst, is_mask); | |
1512 | } | |
a1c564be AZ |
1513 | attrs &= ~(1ULL << OVS_KEY_ATTR_UDP); |
1514 | } | |
1515 | ||
1516 | if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) { | |
1517 | const struct ovs_key_icmp *icmp_key; | |
1518 | ||
1519 | icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]); | |
1520 | SW_FLOW_KEY_PUT(match, ipv4.tp.src, | |
1521 | htons(icmp_key->icmp_type), is_mask); | |
1522 | SW_FLOW_KEY_PUT(match, ipv4.tp.dst, | |
1523 | htons(icmp_key->icmp_code), is_mask); | |
1524 | attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP); | |
1525 | } | |
1526 | ||
1527 | if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) { | |
1528 | const struct ovs_key_icmpv6 *icmpv6_key; | |
1529 | ||
1530 | icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]); | |
1531 | SW_FLOW_KEY_PUT(match, ipv6.tp.src, | |
1532 | htons(icmpv6_key->icmpv6_type), is_mask); | |
1533 | SW_FLOW_KEY_PUT(match, ipv6.tp.dst, | |
1534 | htons(icmpv6_key->icmpv6_code), is_mask); | |
1535 | attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6); | |
1536 | } | |
1537 | ||
1538 | if (attrs & (1ULL << OVS_KEY_ATTR_ND)) { | |
1539 | const struct ovs_key_nd *nd_key; | |
1540 | ||
1541 | nd_key = nla_data(a[OVS_KEY_ATTR_ND]); | |
1542 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target, | |
1543 | nd_key->nd_target, | |
1544 | sizeof(match->key->ipv6.nd.target), | |
1545 | is_mask); | |
1546 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll, | |
1547 | nd_key->nd_sll, ETH_ALEN, is_mask); | |
1548 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll, | |
1549 | nd_key->nd_tll, ETH_ALEN, is_mask); | |
1550 | attrs &= ~(1ULL << OVS_KEY_ATTR_ND); | |
1551 | } | |
1552 | ||
1553 | if (attrs != 0) | |
1554 | return -EINVAL; | |
1555 | ||
1556 | return 0; | |
1557 | } | |
1558 | ||
1559 | /** | |
1560 | * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and | |
1561 | * mask. In case the 'mask' is NULL, the flow is treated as exact match | |
1562 | * flow. Otherwise, it is treated as a wildcarded flow, except the mask | |
1563 | * does not include any don't care bit. | |
1564 | * @match: receives the extracted flow match information. | |
1565 | * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute | |
1566 | * sequence. The fields should of the packet that triggered the creation | |
1567 | * of this flow. | |
1568 | * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink | |
1569 | * attribute specifies the mask field of the wildcarded flow. | |
1570 | */ | |
1571 | int ovs_match_from_nlattrs(struct sw_flow_match *match, | |
1572 | const struct nlattr *key, | |
1573 | const struct nlattr *mask) | |
1574 | { | |
1575 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; | |
a1c564be AZ |
1576 | const struct nlattr *encap; |
1577 | u64 key_attrs = 0; | |
1578 | u64 mask_attrs = 0; | |
1579 | bool encap_valid = false; | |
1580 | int err; | |
1581 | ||
1582 | err = parse_flow_nlattrs(key, a, &key_attrs); | |
1583 | if (err) | |
1584 | return err; | |
1585 | ||
1586 | if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) { | |
1587 | encap = a[OVS_KEY_ATTR_ENCAP]; | |
1588 | key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP); | |
1589 | if (nla_len(encap)) { | |
1590 | __be16 eth_type = 0; /* ETH_P_8021Q */ | |
1591 | ||
1592 | if (a[OVS_KEY_ATTR_ETHERTYPE]) | |
1593 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
1594 | ||
1595 | if ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) { | |
1596 | encap_valid = true; | |
1597 | key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
1598 | err = parse_flow_nlattrs(encap, a, &key_attrs); | |
1b936472 AZ |
1599 | } else { |
1600 | OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n"); | |
a1c564be | 1601 | err = -EINVAL; |
1b936472 | 1602 | } |
a1c564be | 1603 | |
34118cae BP |
1604 | if (err) |
1605 | return err; | |
36956a7d | 1606 | } |
a1c564be | 1607 | } |
36956a7d | 1608 | |
a1c564be AZ |
1609 | err = ovs_key_from_nlattrs(match, key_attrs, a, false); |
1610 | if (err) | |
1611 | return err; | |
1612 | ||
1613 | if (mask) { | |
1a02b768 | 1614 | err = parse_flow_mask_nlattrs(mask, a, &mask_attrs); |
a1c564be AZ |
1615 | if (err) |
1616 | return err; | |
1617 | ||
1618 | if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) { | |
1619 | __be16 eth_type = 0; | |
1620 | ||
063236f1 | 1621 | mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP); |
1a02b768 JG |
1622 | if (a[OVS_KEY_ATTR_ETHERTYPE]) |
1623 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
a1c564be AZ |
1624 | if (eth_type == htons(0xffff)) { |
1625 | mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
1a02b768 JG |
1626 | encap = a[OVS_KEY_ATTR_ENCAP]; |
1627 | err = parse_flow_mask_nlattrs(encap, a, &mask_attrs); | |
1b936472 AZ |
1628 | } else { |
1629 | OVS_NLERR("VLAN frames must have an exact match" | |
1630 | " on the TPID (mask=%x).\n", | |
1631 | ntohs(eth_type)); | |
a1c564be | 1632 | err = -EINVAL; |
1b936472 | 1633 | } |
34118cae | 1634 | |
34118cae BP |
1635 | if (err) |
1636 | return err; | |
1637 | } | |
36956a7d | 1638 | |
1a02b768 | 1639 | err = ovs_key_from_nlattrs(match, mask_attrs, a, true); |
a1c564be AZ |
1640 | if (err) |
1641 | return err; | |
1642 | } else { | |
1643 | /* Populate exact match flow's key mask. */ | |
1644 | if (match->mask) | |
1645 | ovs_sw_flow_mask_set(match->mask, &match->range, 0xff); | |
34118cae | 1646 | } |
76abe283 | 1647 | |
a1c564be | 1648 | if (!ovs_match_validate(match, key_attrs, mask_attrs)) |
34118cae | 1649 | return -EINVAL; |
34118cae BP |
1650 | |
1651 | return 0; | |
36956a7d BP |
1652 | } |
1653 | ||
80e5eed9 | 1654 | /** |
850b6b3b | 1655 | * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key. |
c3b36027 | 1656 | * @flow: Receives extracted in_port, priority, tun_key and skb_mark. |
c3b36027 | 1657 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
80e5eed9 BP |
1658 | * sequence. |
1659 | * | |
1660 | * This parses a series of Netlink attributes that form a flow key, which must | |
1661 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to | |
1662 | * get the metadata, that is, the parts of the flow key that cannot be | |
1663 | * extracted from the packet itself. | |
1664 | */ | |
13e24889 | 1665 | |
a1c564be AZ |
1666 | int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, |
1667 | const struct nlattr *attr) | |
80e5eed9 | 1668 | { |
6694e498 | 1669 | struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key; |
a1c564be AZ |
1670 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
1671 | u64 attrs = 0; | |
1672 | int err; | |
1673 | struct sw_flow_match match; | |
80e5eed9 | 1674 | |
13e24889 PS |
1675 | flow->key.phy.in_port = DP_MAX_PORTS; |
1676 | flow->key.phy.priority = 0; | |
72e8bf28 | 1677 | flow->key.phy.skb_mark = 0; |
6694e498 | 1678 | memset(tun_key, 0, sizeof(flow->key.tun_key)); |
80e5eed9 | 1679 | |
a1c564be AZ |
1680 | err = parse_flow_nlattrs(attr, a, &attrs); |
1681 | if (err) | |
80e5eed9 | 1682 | return -EINVAL; |
13e24889 | 1683 | |
46d4c97f JG |
1684 | memset(&match, 0, sizeof(match)); |
1685 | match.key = &flow->key; | |
a1c564be AZ |
1686 | |
1687 | err = metadata_from_nlattrs(&match, &attrs, a, false); | |
1688 | if (err) | |
1689 | return err; | |
13e24889 | 1690 | |
80e5eed9 BP |
1691 | return 0; |
1692 | } | |
1693 | ||
a1c564be AZ |
1694 | int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, |
1695 | const struct sw_flow_key *output, struct sk_buff *skb) | |
36956a7d | 1696 | { |
df2c07f4 | 1697 | struct ovs_key_ethernet *eth_key; |
fea393b1 | 1698 | struct nlattr *nla, *encap; |
36956a7d | 1699 | |
dd62b991 | 1700 | if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority)) |
c3cc8c03 | 1701 | goto nla_put_failure; |
18c43631 | 1702 | |
9b405f1a | 1703 | if (swkey->tun_key.ipv4_dst && |
a1c564be | 1704 | ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key)) |
9b405f1a PS |
1705 | goto nla_put_failure; |
1706 | ||
04b13268 AZ |
1707 | if (swkey->phy.in_port == DP_MAX_PORTS) { |
1708 | if ((swkey != output) && (output->phy.in_port == 0xffff)) | |
1709 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff)) | |
1710 | goto nla_put_failure; | |
1711 | } else { | |
a1c564be AZ |
1712 | u16 upper_u16; |
1713 | upper_u16 = (swkey == output) ? 0 : 0xffff; | |
1714 | ||
1715 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, | |
04b13268 | 1716 | (upper_u16 << 16) | output->phy.in_port)) |
a1c564be AZ |
1717 | goto nla_put_failure; |
1718 | } | |
36956a7d | 1719 | |
dd62b991 | 1720 | if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark)) |
72e8bf28 AA |
1721 | goto nla_put_failure; |
1722 | ||
df2c07f4 | 1723 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); |
d6569377 BP |
1724 | if (!nla) |
1725 | goto nla_put_failure; | |
a1c564be | 1726 | |
d6569377 | 1727 | eth_key = nla_data(nla); |
a1c564be AZ |
1728 | memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN); |
1729 | memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN); | |
36956a7d | 1730 | |
8ddc056d | 1731 | if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) { |
a1c564be AZ |
1732 | __be16 eth_type; |
1733 | eth_type = (swkey == output) ? htons(ETH_P_8021Q) : htons(0xffff) ; | |
1734 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) || | |
1735 | nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci)) | |
c3cc8c03 | 1736 | goto nla_put_failure; |
fea393b1 | 1737 | encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP); |
8ddc056d BP |
1738 | if (!swkey->eth.tci) |
1739 | goto unencap; | |
a1c564be | 1740 | } else |
fea393b1 | 1741 | encap = NULL; |
36956a7d | 1742 | |
04b13268 AZ |
1743 | if (swkey->eth.type == htons(ETH_P_802_2)) { |
1744 | /* | |
1745 | * Ethertype 802.2 is represented in the netlink with omitted | |
1746 | * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and | |
1747 | * 0xffff in the mask attribute. Ethertype can also | |
1748 | * be wildcarded. | |
1749 | */ | |
1750 | if (swkey != output && output->eth.type) | |
1751 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, | |
1752 | output->eth.type)) | |
1753 | goto nla_put_failure; | |
fea393b1 | 1754 | goto unencap; |
04b13268 | 1755 | } |
36956a7d | 1756 | |
04b13268 AZ |
1757 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type)) |
1758 | goto nla_put_failure; | |
36956a7d | 1759 | |
76abe283 | 1760 | if (swkey->eth.type == htons(ETH_P_IP)) { |
df2c07f4 | 1761 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 1762 | |
df2c07f4 | 1763 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); |
d6569377 BP |
1764 | if (!nla) |
1765 | goto nla_put_failure; | |
1766 | ipv4_key = nla_data(nla); | |
a1c564be AZ |
1767 | ipv4_key->ipv4_src = output->ipv4.addr.src; |
1768 | ipv4_key->ipv4_dst = output->ipv4.addr.dst; | |
1769 | ipv4_key->ipv4_proto = output->ip.proto; | |
1770 | ipv4_key->ipv4_tos = output->ip.tos; | |
1771 | ipv4_key->ipv4_ttl = output->ip.ttl; | |
1772 | ipv4_key->ipv4_frag = output->ip.frag; | |
76abe283 | 1773 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
df2c07f4 | 1774 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 1775 | |
df2c07f4 | 1776 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); |
d31f1109 JP |
1777 | if (!nla) |
1778 | goto nla_put_failure; | |
1779 | ipv6_key = nla_data(nla); | |
a1c564be | 1780 | memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src, |
d31f1109 | 1781 | sizeof(ipv6_key->ipv6_src)); |
a1c564be | 1782 | memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst, |
d31f1109 | 1783 | sizeof(ipv6_key->ipv6_dst)); |
a1c564be AZ |
1784 | ipv6_key->ipv6_label = output->ipv6.label; |
1785 | ipv6_key->ipv6_proto = output->ip.proto; | |
1786 | ipv6_key->ipv6_tclass = output->ip.tos; | |
1787 | ipv6_key->ipv6_hlimit = output->ip.ttl; | |
1788 | ipv6_key->ipv6_frag = output->ip.frag; | |
8087f5ff MM |
1789 | } else if (swkey->eth.type == htons(ETH_P_ARP) || |
1790 | swkey->eth.type == htons(ETH_P_RARP)) { | |
df2c07f4 | 1791 | struct ovs_key_arp *arp_key; |
d31f1109 | 1792 | |
df2c07f4 | 1793 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); |
d31f1109 JP |
1794 | if (!nla) |
1795 | goto nla_put_failure; | |
1796 | arp_key = nla_data(nla); | |
df2c07f4 | 1797 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); |
a1c564be AZ |
1798 | arp_key->arp_sip = output->ipv4.addr.src; |
1799 | arp_key->arp_tip = output->ipv4.addr.dst; | |
1800 | arp_key->arp_op = htons(output->ip.proto); | |
1801 | memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN); | |
1802 | memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN); | |
d31f1109 JP |
1803 | } |
1804 | ||
7257b535 BP |
1805 | if ((swkey->eth.type == htons(ETH_P_IP) || |
1806 | swkey->eth.type == htons(ETH_P_IPV6)) && | |
9e44d715 | 1807 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { |
36956a7d | 1808 | |
28bad473 | 1809 | if (swkey->ip.proto == IPPROTO_TCP) { |
df2c07f4 | 1810 | struct ovs_key_tcp *tcp_key; |
36956a7d | 1811 | |
df2c07f4 | 1812 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); |
d6569377 BP |
1813 | if (!nla) |
1814 | goto nla_put_failure; | |
1815 | tcp_key = nla_data(nla); | |
76abe283 | 1816 | if (swkey->eth.type == htons(ETH_P_IP)) { |
a1c564be AZ |
1817 | tcp_key->tcp_src = output->ipv4.tp.src; |
1818 | tcp_key->tcp_dst = output->ipv4.tp.dst; | |
76abe283 | 1819 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
a1c564be AZ |
1820 | tcp_key->tcp_src = output->ipv6.tp.src; |
1821 | tcp_key->tcp_dst = output->ipv6.tp.dst; | |
76abe283 | 1822 | } |
28bad473 | 1823 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
df2c07f4 | 1824 | struct ovs_key_udp *udp_key; |
36956a7d | 1825 | |
df2c07f4 | 1826 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); |
d6569377 BP |
1827 | if (!nla) |
1828 | goto nla_put_failure; | |
1829 | udp_key = nla_data(nla); | |
76abe283 | 1830 | if (swkey->eth.type == htons(ETH_P_IP)) { |
a1c564be AZ |
1831 | udp_key->udp_src = output->ipv4.tp.src; |
1832 | udp_key->udp_dst = output->ipv4.tp.dst; | |
76abe283 | 1833 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
a1c564be AZ |
1834 | udp_key->udp_src = output->ipv6.tp.src; |
1835 | udp_key->udp_dst = output->ipv6.tp.dst; | |
76abe283 AE |
1836 | } |
1837 | } else if (swkey->eth.type == htons(ETH_P_IP) && | |
28bad473 | 1838 | swkey->ip.proto == IPPROTO_ICMP) { |
df2c07f4 | 1839 | struct ovs_key_icmp *icmp_key; |
36956a7d | 1840 | |
df2c07f4 | 1841 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); |
d6569377 BP |
1842 | if (!nla) |
1843 | goto nla_put_failure; | |
1844 | icmp_key = nla_data(nla); | |
a1c564be AZ |
1845 | icmp_key->icmp_type = ntohs(output->ipv4.tp.src); |
1846 | icmp_key->icmp_code = ntohs(output->ipv4.tp.dst); | |
76abe283 | 1847 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && |
28bad473 | 1848 | swkey->ip.proto == IPPROTO_ICMPV6) { |
df2c07f4 | 1849 | struct ovs_key_icmpv6 *icmpv6_key; |
36956a7d | 1850 | |
df2c07f4 | 1851 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, |
bfef4717 | 1852 | sizeof(*icmpv6_key)); |
d31f1109 JP |
1853 | if (!nla) |
1854 | goto nla_put_failure; | |
1855 | icmpv6_key = nla_data(nla); | |
a1c564be AZ |
1856 | icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src); |
1857 | icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst); | |
685a51a5 | 1858 | |
bfef4717 JG |
1859 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || |
1860 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { | |
df2c07f4 | 1861 | struct ovs_key_nd *nd_key; |
685a51a5 | 1862 | |
df2c07f4 | 1863 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); |
685a51a5 JP |
1864 | if (!nla) |
1865 | goto nla_put_failure; | |
1866 | nd_key = nla_data(nla); | |
a1c564be | 1867 | memcpy(nd_key->nd_target, &output->ipv6.nd.target, |
685a51a5 | 1868 | sizeof(nd_key->nd_target)); |
a1c564be AZ |
1869 | memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN); |
1870 | memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN); | |
685a51a5 | 1871 | } |
d31f1109 | 1872 | } |
36956a7d BP |
1873 | } |
1874 | ||
fea393b1 BP |
1875 | unencap: |
1876 | if (encap) | |
1877 | nla_nest_end(skb, encap); | |
1878 | ||
d6569377 | 1879 | return 0; |
36956a7d | 1880 | |
d6569377 BP |
1881 | nla_put_failure: |
1882 | return -EMSGSIZE; | |
8d5ebd83 JG |
1883 | } |
1884 | ||
064af421 BP |
1885 | /* Initializes the flow module. |
1886 | * Returns zero if successful or a negative error code. */ | |
850b6b3b | 1887 | int ovs_flow_init(void) |
064af421 BP |
1888 | { |
1889 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, | |
1890 | 0, NULL); | |
1891 | if (flow_cache == NULL) | |
1892 | return -ENOMEM; | |
1893 | ||
1894 | return 0; | |
1895 | } | |
1896 | ||
1897 | /* Uninitializes the flow module. */ | |
850b6b3b | 1898 | void ovs_flow_exit(void) |
064af421 BP |
1899 | { |
1900 | kmem_cache_destroy(flow_cache); | |
1901 | } | |
a1c564be AZ |
1902 | |
1903 | struct sw_flow_mask *ovs_sw_flow_mask_alloc(void) | |
1904 | { | |
1905 | struct sw_flow_mask *mask; | |
1906 | ||
1907 | mask = kmalloc(sizeof(*mask), GFP_KERNEL); | |
1908 | if (mask) | |
1909 | mask->ref_count = 0; | |
1910 | ||
1911 | return mask; | |
1912 | } | |
1913 | ||
1914 | void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask) | |
1915 | { | |
1916 | mask->ref_count++; | |
1917 | } | |
1918 | ||
1919 | static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu) | |
1920 | { | |
1921 | struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu); | |
1922 | ||
1923 | kfree(mask); | |
1924 | } | |
1925 | ||
1926 | void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred) | |
1927 | { | |
1928 | if (!mask) | |
1929 | return; | |
1930 | ||
1931 | BUG_ON(!mask->ref_count); | |
1932 | mask->ref_count--; | |
1933 | ||
1934 | if (!mask->ref_count) { | |
1935 | list_del_rcu(&mask->list); | |
1936 | if (deferred) | |
1937 | call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb); | |
1938 | else | |
1939 | kfree(mask); | |
1940 | } | |
1941 | } | |
1942 | ||
1943 | static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a, | |
1944 | const struct sw_flow_mask *b) | |
1945 | { | |
1946 | u8 *a_ = (u8 *)&a->key + a->range.start; | |
1947 | u8 *b_ = (u8 *)&b->key + b->range.start; | |
1948 | ||
1949 | return (a->range.end == b->range.end) | |
1950 | && (a->range.start == b->range.start) | |
1951 | && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0); | |
1952 | } | |
1953 | ||
1954 | struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl, | |
1955 | const struct sw_flow_mask *mask) | |
1956 | { | |
1957 | struct list_head *ml; | |
1958 | ||
ede77a46 | 1959 | list_for_each(ml, tbl->mask_list) { |
a1c564be AZ |
1960 | struct sw_flow_mask *m; |
1961 | m = container_of(ml, struct sw_flow_mask, list); | |
1962 | if (ovs_sw_flow_mask_equal(mask, m)) | |
1963 | return m; | |
1964 | } | |
1965 | ||
1966 | return NULL; | |
1967 | } | |
1968 | ||
1969 | /** | |
1970 | * add a new mask into the mask list. | |
1971 | * The caller needs to make sure that 'mask' is not the same | |
1972 | * as any masks that are already on the list. | |
1973 | */ | |
1974 | void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask) | |
1975 | { | |
ede77a46 | 1976 | list_add_rcu(&mask->list, tbl->mask_list); |
a1c564be AZ |
1977 | } |
1978 | ||
1979 | /** | |
1980 | * Set 'range' fields in the mask to the value of 'val'. | |
1981 | */ | |
1982 | static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask, | |
1983 | struct sw_flow_key_range *range, u8 val) | |
1984 | { | |
1985 | u8 *m = (u8 *)&mask->key + range->start; | |
1986 | ||
1987 | mask->range = *range; | |
1988 | memset(m, val, ovs_sw_flow_mask_size_roundup(mask)); | |
1989 | } |