]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2007-2011 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 | |
2db65bf7 JG |
50 | static int check_header(struct sk_buff *skb, int len) |
51 | { | |
52 | if (unlikely(skb->len < len)) | |
53 | return -EINVAL; | |
54 | if (unlikely(!pskb_may_pull(skb, len))) | |
55 | return -ENOMEM; | |
56 | return 0; | |
57 | } | |
58 | ||
6455100f | 59 | static bool arphdr_ok(struct sk_buff *skb) |
a26ef517 | 60 | { |
2db65bf7 JG |
61 | return pskb_may_pull(skb, skb_network_offset(skb) + |
62 | sizeof(struct arp_eth_header)); | |
a26ef517 JP |
63 | } |
64 | ||
6455100f | 65 | static int check_iphdr(struct sk_buff *skb) |
064af421 | 66 | { |
4c1ad233 BP |
67 | unsigned int nh_ofs = skb_network_offset(skb); |
68 | unsigned int ip_len; | |
2db65bf7 | 69 | int err; |
4c1ad233 | 70 | |
2db65bf7 JG |
71 | err = check_header(skb, nh_ofs + sizeof(struct iphdr)); |
72 | if (unlikely(err)) | |
73 | return err; | |
4c1ad233 BP |
74 | |
75 | ip_len = ip_hdrlen(skb); | |
2db65bf7 JG |
76 | if (unlikely(ip_len < sizeof(struct iphdr) || |
77 | skb->len < nh_ofs + ip_len)) | |
4c1ad233 BP |
78 | return -EINVAL; |
79 | ||
4c1ad233 BP |
80 | skb_set_transport_header(skb, nh_ofs + ip_len); |
81 | return 0; | |
064af421 BP |
82 | } |
83 | ||
6455100f | 84 | static bool tcphdr_ok(struct sk_buff *skb) |
064af421 BP |
85 | { |
86 | int th_ofs = skb_transport_offset(skb); | |
2db65bf7 JG |
87 | int tcp_len; |
88 | ||
89 | if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr)))) | |
90 | return false; | |
91 | ||
92 | tcp_len = tcp_hdrlen(skb); | |
93 | if (unlikely(tcp_len < sizeof(struct tcphdr) || | |
94 | skb->len < th_ofs + tcp_len)) | |
95 | return false; | |
96 | ||
97 | return true; | |
064af421 BP |
98 | } |
99 | ||
6455100f | 100 | static bool udphdr_ok(struct sk_buff *skb) |
064af421 | 101 | { |
2db65bf7 JG |
102 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
103 | sizeof(struct udphdr)); | |
064af421 BP |
104 | } |
105 | ||
6455100f | 106 | static bool icmphdr_ok(struct sk_buff *skb) |
064af421 | 107 | { |
2db65bf7 JG |
108 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
109 | sizeof(struct icmphdr)); | |
064af421 BP |
110 | } |
111 | ||
850b6b3b | 112 | u64 ovs_flow_used_time(unsigned long flow_jiffies) |
ec58547a JG |
113 | { |
114 | struct timespec cur_ts; | |
115 | u64 cur_ms, idle_ms; | |
116 | ||
117 | ktime_get_ts(&cur_ts); | |
118 | idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); | |
119 | cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC + | |
120 | cur_ts.tv_nsec / NSEC_PER_MSEC; | |
121 | ||
122 | return cur_ms - idle_ms; | |
123 | } | |
124 | ||
76abe283 | 125 | #define SW_FLOW_KEY_OFFSET(field) \ |
6455100f PS |
126 | (offsetof(struct sw_flow_key, field) + \ |
127 | FIELD_SIZEOF(struct sw_flow_key, field)) | |
76abe283 AE |
128 | |
129 | static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key, | |
130 | int *key_lenp) | |
d31f1109 JP |
131 | { |
132 | unsigned int nh_ofs = skb_network_offset(skb); | |
133 | unsigned int nh_len; | |
134 | int payload_ofs; | |
d31f1109 JP |
135 | struct ipv6hdr *nh; |
136 | uint8_t nexthdr; | |
0fd0d083 | 137 | __be16 frag_off; |
2db65bf7 | 138 | int err; |
d31f1109 | 139 | |
fa8223b7 | 140 | *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label); |
76abe283 | 141 | |
2db65bf7 JG |
142 | err = check_header(skb, nh_ofs + sizeof(*nh)); |
143 | if (unlikely(err)) | |
144 | return err; | |
d31f1109 JP |
145 | |
146 | nh = ipv6_hdr(skb); | |
147 | nexthdr = nh->nexthdr; | |
148 | payload_ofs = (u8 *)(nh + 1) - skb->data; | |
d31f1109 | 149 | |
28bad473 | 150 | key->ip.proto = NEXTHDR_NONE; |
530180fd | 151 | key->ip.tos = ipv6_get_dsfield(nh); |
a61680c6 | 152 | key->ip.ttl = nh->hop_limit; |
fa8223b7 | 153 | key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); |
858d1026 JG |
154 | key->ipv6.addr.src = nh->saddr; |
155 | key->ipv6.addr.dst = nh->daddr; | |
d31f1109 | 156 | |
0fd0d083 | 157 | payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off); |
bfef4717 | 158 | if (unlikely(payload_ofs < 0)) |
d31f1109 | 159 | return -EINVAL; |
bfef4717 | 160 | |
0fd0d083 JG |
161 | if (frag_off) { |
162 | if (frag_off & htons(~0x7)) | |
163 | key->ip.frag = OVS_FRAG_TYPE_LATER; | |
164 | else | |
165 | key->ip.frag = OVS_FRAG_TYPE_FIRST; | |
166 | } | |
167 | ||
d31f1109 | 168 | nh_len = payload_ofs - nh_ofs; |
d31f1109 | 169 | skb_set_transport_header(skb, nh_ofs + nh_len); |
28bad473 | 170 | key->ip.proto = nexthdr; |
d31f1109 JP |
171 | return nh_len; |
172 | } | |
173 | ||
174 | static bool icmp6hdr_ok(struct sk_buff *skb) | |
175 | { | |
2db65bf7 JG |
176 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
177 | sizeof(struct icmp6hdr)); | |
d31f1109 | 178 | } |
ec58547a | 179 | |
064af421 BP |
180 | #define TCP_FLAGS_OFFSET 13 |
181 | #define TCP_FLAG_MASK 0x3f | |
182 | ||
850b6b3b | 183 | void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb) |
064af421 | 184 | { |
064af421 BP |
185 | u8 tcp_flags = 0; |
186 | ||
11460e23 JG |
187 | if ((flow->key.eth.type == htons(ETH_P_IP) || |
188 | flow->key.eth.type == htons(ETH_P_IPV6)) && | |
9c47b45a JG |
189 | flow->key.ip.proto == IPPROTO_TCP && |
190 | likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) { | |
abfec865 BP |
191 | u8 *tcp = (u8 *)tcp_hdr(skb); |
192 | tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK; | |
064af421 BP |
193 | } |
194 | ||
e9141eec | 195 | spin_lock(&flow->lock); |
6bfafa55 | 196 | flow->used = jiffies; |
064af421 BP |
197 | flow->packet_count++; |
198 | flow->byte_count += skb->len; | |
199 | flow->tcp_flags |= tcp_flags; | |
e9141eec | 200 | spin_unlock(&flow->lock); |
064af421 BP |
201 | } |
202 | ||
9b405f1a | 203 | struct sw_flow_actions *ovs_flow_actions_alloc(int size) |
064af421 BP |
204 | { |
205 | struct sw_flow_actions *sfa; | |
206 | ||
9b405f1a | 207 | if (size > MAX_ACTIONS_BUFSIZE) |
064af421 BP |
208 | return ERR_PTR(-EINVAL); |
209 | ||
ba400435 | 210 | sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL); |
064af421 BP |
211 | if (!sfa) |
212 | return ERR_PTR(-ENOMEM); | |
213 | ||
9b405f1a | 214 | sfa->actions_len = 0; |
064af421 BP |
215 | return sfa; |
216 | } | |
217 | ||
850b6b3b | 218 | struct sw_flow *ovs_flow_alloc(void) |
560e8022 JG |
219 | { |
220 | struct sw_flow *flow; | |
221 | ||
222 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); | |
223 | if (!flow) | |
224 | return ERR_PTR(-ENOMEM); | |
225 | ||
226 | spin_lock_init(&flow->lock); | |
6f04e94a | 227 | flow->sf_acts = NULL; |
064af421 | 228 | |
560e8022 JG |
229 | return flow; |
230 | } | |
231 | ||
be2ba156 | 232 | static struct hlist_head *find_bucket(struct flow_table *table, u32 hash) |
8d5ebd83 | 233 | { |
acd051f1 | 234 | hash = jhash_1word(hash, table->hash_seed); |
3544358a PS |
235 | return flex_array_get(table->buckets, |
236 | (hash & (table->n_buckets - 1))); | |
237 | } | |
238 | ||
be2ba156 | 239 | static struct flex_array *alloc_buckets(unsigned int n_buckets) |
3544358a | 240 | { |
be2ba156 | 241 | struct flex_array *buckets; |
3544358a PS |
242 | int i, err; |
243 | ||
244 | buckets = flex_array_alloc(sizeof(struct hlist_head *), | |
245 | n_buckets, GFP_KERNEL); | |
246 | if (!buckets) | |
247 | return NULL; | |
248 | ||
249 | err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); | |
250 | if (err) { | |
251 | flex_array_free(buckets); | |
252 | return NULL; | |
253 | } | |
254 | ||
255 | for (i = 0; i < n_buckets; i++) | |
256 | INIT_HLIST_HEAD((struct hlist_head *) | |
257 | flex_array_get(buckets, i)); | |
258 | ||
259 | return buckets; | |
260 | } | |
261 | ||
6455100f | 262 | static void free_buckets(struct flex_array *buckets) |
3544358a PS |
263 | { |
264 | flex_array_free(buckets); | |
265 | } | |
266 | ||
850b6b3b | 267 | struct flow_table *ovs_flow_tbl_alloc(int new_size) |
3544358a PS |
268 | { |
269 | struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL); | |
270 | ||
271 | if (!table) | |
272 | return NULL; | |
273 | ||
274 | table->buckets = alloc_buckets(new_size); | |
275 | ||
276 | if (!table->buckets) { | |
277 | kfree(table); | |
278 | return NULL; | |
279 | } | |
280 | table->n_buckets = new_size; | |
281 | table->count = 0; | |
acd051f1 PS |
282 | table->node_ver = 0; |
283 | table->keep_flows = false; | |
284 | get_random_bytes(&table->hash_seed, sizeof(u32)); | |
3544358a PS |
285 | |
286 | return table; | |
287 | } | |
fb8c9347 | 288 | |
850b6b3b | 289 | void ovs_flow_tbl_destroy(struct flow_table *table) |
3544358a PS |
290 | { |
291 | int i; | |
292 | ||
293 | if (!table) | |
294 | return; | |
295 | ||
acd051f1 PS |
296 | if (table->keep_flows) |
297 | goto skip_flows; | |
298 | ||
3544358a PS |
299 | for (i = 0; i < table->n_buckets; i++) { |
300 | struct sw_flow *flow; | |
301 | struct hlist_head *head = flex_array_get(table->buckets, i); | |
f8dfbcb7 | 302 | struct hlist_node *n; |
acd051f1 | 303 | int ver = table->node_ver; |
3544358a | 304 | |
f8dfbcb7 | 305 | hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) { |
acd051f1 | 306 | hlist_del_rcu(&flow->hash_node[ver]); |
9321954a | 307 | ovs_flow_free(flow); |
3544358a PS |
308 | } |
309 | } | |
310 | ||
acd051f1 | 311 | skip_flows: |
3544358a PS |
312 | free_buckets(table->buckets); |
313 | kfree(table); | |
314 | } | |
315 | ||
316 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) | |
317 | { | |
318 | struct flow_table *table = container_of(rcu, struct flow_table, rcu); | |
319 | ||
850b6b3b | 320 | ovs_flow_tbl_destroy(table); |
3544358a PS |
321 | } |
322 | ||
850b6b3b | 323 | void ovs_flow_tbl_deferred_destroy(struct flow_table *table) |
3544358a | 324 | { |
6455100f PS |
325 | if (!table) |
326 | return; | |
3544358a | 327 | |
6455100f | 328 | call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb); |
3544358a PS |
329 | } |
330 | ||
850b6b3b | 331 | struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last) |
3544358a PS |
332 | { |
333 | struct sw_flow *flow; | |
334 | struct hlist_head *head; | |
acd051f1 | 335 | int ver; |
3544358a PS |
336 | int i; |
337 | ||
acd051f1 | 338 | ver = table->node_ver; |
3544358a PS |
339 | while (*bucket < table->n_buckets) { |
340 | i = 0; | |
341 | head = flex_array_get(table->buckets, *bucket); | |
f8dfbcb7 | 342 | hlist_for_each_entry_rcu(flow, head, hash_node[ver]) { |
3544358a PS |
343 | if (i < *last) { |
344 | i++; | |
345 | continue; | |
346 | } | |
347 | *last = i + 1; | |
348 | return flow; | |
349 | } | |
350 | (*bucket)++; | |
351 | *last = 0; | |
352 | } | |
353 | ||
354 | return NULL; | |
355 | } | |
356 | ||
13e24889 PS |
357 | static void __flow_tbl_insert(struct flow_table *table, struct sw_flow *flow) |
358 | { | |
359 | struct hlist_head *head; | |
360 | head = find_bucket(table, flow->hash); | |
361 | hlist_add_head_rcu(&flow->hash_node[table->node_ver], head); | |
362 | table->count++; | |
363 | } | |
364 | ||
acd051f1 | 365 | static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new) |
3544358a | 366 | { |
acd051f1 | 367 | int old_ver; |
3544358a PS |
368 | int i; |
369 | ||
acd051f1 PS |
370 | old_ver = old->node_ver; |
371 | new->node_ver = !old_ver; | |
3544358a | 372 | |
acd051f1 PS |
373 | /* Insert in new table. */ |
374 | for (i = 0; i < old->n_buckets; i++) { | |
3544358a PS |
375 | struct sw_flow *flow; |
376 | struct hlist_head *head; | |
3544358a | 377 | |
acd051f1 | 378 | head = flex_array_get(old->buckets, i); |
3544358a | 379 | |
f8dfbcb7 | 380 | hlist_for_each_entry(flow, head, hash_node[old_ver]) |
13e24889 | 381 | __flow_tbl_insert(new, flow); |
3544358a | 382 | } |
acd051f1 PS |
383 | old->keep_flows = true; |
384 | } | |
385 | ||
386 | static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets) | |
387 | { | |
388 | struct flow_table *new_table; | |
389 | ||
390 | new_table = ovs_flow_tbl_alloc(n_buckets); | |
391 | if (!new_table) | |
392 | return ERR_PTR(-ENOMEM); | |
393 | ||
394 | flow_table_copy_flows(table, new_table); | |
3544358a PS |
395 | |
396 | return new_table; | |
397 | } | |
398 | ||
acd051f1 PS |
399 | struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table) |
400 | { | |
401 | return __flow_tbl_rehash(table, table->n_buckets); | |
402 | } | |
403 | ||
404 | struct flow_table *ovs_flow_tbl_expand(struct flow_table *table) | |
405 | { | |
406 | return __flow_tbl_rehash(table, table->n_buckets * 2); | |
407 | } | |
408 | ||
9321954a JG |
409 | void ovs_flow_free(struct sw_flow *flow) |
410 | { | |
411 | if (unlikely(!flow)) | |
412 | return; | |
413 | ||
414 | kfree((struct sf_flow_acts __force *)flow->sf_acts); | |
415 | kmem_cache_free(flow_cache, flow); | |
416 | } | |
417 | ||
850b6b3b | 418 | /* RCU callback used by ovs_flow_deferred_free. */ |
064af421 BP |
419 | static void rcu_free_flow_callback(struct rcu_head *rcu) |
420 | { | |
421 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
fb8c9347 | 422 | |
9321954a | 423 | ovs_flow_free(flow); |
064af421 BP |
424 | } |
425 | ||
426 | /* Schedules 'flow' to be freed after the next RCU grace period. | |
427 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
850b6b3b | 428 | void ovs_flow_deferred_free(struct sw_flow *flow) |
064af421 BP |
429 | { |
430 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
431 | } | |
432 | ||
850b6b3b | 433 | /* RCU callback used by ovs_flow_deferred_free_acts. */ |
064af421 BP |
434 | static void rcu_free_acts_callback(struct rcu_head *rcu) |
435 | { | |
d295e8e9 | 436 | struct sw_flow_actions *sf_acts = container_of(rcu, |
064af421 | 437 | struct sw_flow_actions, rcu); |
ba400435 | 438 | kfree(sf_acts); |
064af421 BP |
439 | } |
440 | ||
441 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. | |
442 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
850b6b3b | 443 | void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts) |
064af421 BP |
444 | { |
445 | call_rcu(&sf_acts->rcu, rcu_free_acts_callback); | |
446 | } | |
447 | ||
2db65bf7 | 448 | static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
064af421 | 449 | { |
50f06e16 BP |
450 | struct qtag_prefix { |
451 | __be16 eth_type; /* ETH_P_8021Q */ | |
452 | __be16 tci; | |
453 | }; | |
454 | struct qtag_prefix *qp; | |
455 | ||
8ddc056d BP |
456 | if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))) |
457 | return 0; | |
458 | ||
2db65bf7 JG |
459 | if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) + |
460 | sizeof(__be16)))) | |
461 | return -ENOMEM; | |
50f06e16 BP |
462 | |
463 | qp = (struct qtag_prefix *) skb->data; | |
76abe283 | 464 | key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT); |
50f06e16 | 465 | __skb_pull(skb, sizeof(struct qtag_prefix)); |
2db65bf7 JG |
466 | |
467 | return 0; | |
50f06e16 BP |
468 | } |
469 | ||
470 | static __be16 parse_ethertype(struct sk_buff *skb) | |
064af421 | 471 | { |
50f06e16 BP |
472 | struct llc_snap_hdr { |
473 | u8 dsap; /* Always 0xAA */ | |
474 | u8 ssap; /* Always 0xAA */ | |
475 | u8 ctrl; | |
476 | u8 oui[3]; | |
8dda8c9b | 477 | __be16 ethertype; |
50f06e16 BP |
478 | }; |
479 | struct llc_snap_hdr *llc; | |
480 | __be16 proto; | |
481 | ||
482 | proto = *(__be16 *) skb->data; | |
483 | __skb_pull(skb, sizeof(__be16)); | |
484 | ||
7cd46155 | 485 | if (ntohs(proto) >= ETH_P_802_3_MIN) |
50f06e16 BP |
486 | return proto; |
487 | ||
2db65bf7 | 488 | if (skb->len < sizeof(struct llc_snap_hdr)) |
36956a7d | 489 | return htons(ETH_P_802_2); |
50f06e16 | 490 | |
2db65bf7 JG |
491 | if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) |
492 | return htons(0); | |
493 | ||
50f06e16 BP |
494 | llc = (struct llc_snap_hdr *) skb->data; |
495 | if (llc->dsap != LLC_SAP_SNAP || | |
496 | llc->ssap != LLC_SAP_SNAP || | |
497 | (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) | |
36956a7d | 498 | return htons(ETH_P_802_2); |
50f06e16 BP |
499 | |
500 | __skb_pull(skb, sizeof(struct llc_snap_hdr)); | |
9e69bc5f | 501 | |
7cd46155 | 502 | if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN) |
9e69bc5f RL |
503 | return llc->ethertype; |
504 | ||
505 | return htons(ETH_P_802_2); | |
064af421 BP |
506 | } |
507 | ||
685a51a5 | 508 | static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, |
76abe283 | 509 | int *key_lenp, int nh_len) |
685a51a5 | 510 | { |
685a51a5 | 511 | struct icmp6hdr *icmp = icmp6_hdr(skb); |
76abe283 AE |
512 | int error = 0; |
513 | int key_len; | |
685a51a5 JP |
514 | |
515 | /* The ICMPv6 type and code fields use the 16-bit transport port | |
bfef4717 JG |
516 | * fields, so we need to store them in 16-bit network byte order. |
517 | */ | |
76abe283 AE |
518 | key->ipv6.tp.src = htons(icmp->icmp6_type); |
519 | key->ipv6.tp.dst = htons(icmp->icmp6_code); | |
520 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
685a51a5 | 521 | |
bfef4717 JG |
522 | if (icmp->icmp6_code == 0 && |
523 | (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || | |
524 | icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
e977ba19 | 525 | int icmp_len = skb->len - skb_transport_offset(skb); |
685a51a5 JP |
526 | struct nd_msg *nd; |
527 | int offset; | |
528 | ||
76abe283 AE |
529 | key_len = SW_FLOW_KEY_OFFSET(ipv6.nd); |
530 | ||
685a51a5 | 531 | /* In order to process neighbor discovery options, we need the |
bfef4717 JG |
532 | * entire packet. |
533 | */ | |
534 | if (unlikely(icmp_len < sizeof(*nd))) | |
76abe283 AE |
535 | goto out; |
536 | if (unlikely(skb_linearize(skb))) { | |
537 | error = -ENOMEM; | |
538 | goto out; | |
539 | } | |
685a51a5 JP |
540 | |
541 | nd = (struct nd_msg *)skb_transport_header(skb); | |
858d1026 | 542 | key->ipv6.nd.target = nd->target; |
76abe283 | 543 | key_len = SW_FLOW_KEY_OFFSET(ipv6.nd); |
685a51a5 JP |
544 | |
545 | icmp_len -= sizeof(*nd); | |
546 | offset = 0; | |
547 | while (icmp_len >= 8) { | |
6455100f PS |
548 | struct nd_opt_hdr *nd_opt = |
549 | (struct nd_opt_hdr *)(nd->opt + offset); | |
685a51a5 JP |
550 | int opt_len = nd_opt->nd_opt_len * 8; |
551 | ||
bfef4717 | 552 | if (unlikely(!opt_len || opt_len > icmp_len)) |
685a51a5 JP |
553 | goto invalid; |
554 | ||
bfef4717 JG |
555 | /* Store the link layer address if the appropriate |
556 | * option is provided. It is considered an error if | |
557 | * the same link layer option is specified twice. | |
558 | */ | |
685a51a5 | 559 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR |
bfef4717 | 560 | && opt_len == 8) { |
76abe283 | 561 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll))) |
685a51a5 | 562 | goto invalid; |
76abe283 | 563 | memcpy(key->ipv6.nd.sll, |
bfef4717 | 564 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 | 565 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR |
bfef4717 | 566 | && opt_len == 8) { |
76abe283 | 567 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll))) |
685a51a5 | 568 | goto invalid; |
76abe283 | 569 | memcpy(key->ipv6.nd.tll, |
bfef4717 | 570 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 JP |
571 | } |
572 | ||
573 | icmp_len -= opt_len; | |
574 | offset += opt_len; | |
575 | } | |
576 | } | |
577 | ||
76abe283 | 578 | goto out; |
685a51a5 JP |
579 | |
580 | invalid: | |
76abe283 AE |
581 | memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target)); |
582 | memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll)); | |
583 | memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll)); | |
685a51a5 | 584 | |
76abe283 AE |
585 | out: |
586 | *key_lenp = key_len; | |
587 | return error; | |
685a51a5 JP |
588 | } |
589 | ||
a31e0e31 | 590 | /** |
850b6b3b | 591 | * ovs_flow_extract - extracts a flow key from an Ethernet frame. |
a31e0e31 BP |
592 | * @skb: sk_buff that contains the frame, with skb->data pointing to the |
593 | * Ethernet header | |
594 | * @in_port: port number on which @skb was received. | |
595 | * @key: output flow key | |
76abe283 | 596 | * @key_lenp: length of output flow key |
a31e0e31 BP |
597 | * |
598 | * The caller must ensure that skb->len >= ETH_HLEN. | |
599 | * | |
4c1ad233 BP |
600 | * Returns 0 if successful, otherwise a negative errno value. |
601 | * | |
59a18f80 BP |
602 | * Initializes @skb header pointers as follows: |
603 | * | |
604 | * - skb->mac_header: the Ethernet header. | |
605 | * | |
606 | * - skb->network_header: just past the Ethernet header, or just past the | |
607 | * VLAN header, to the first byte of the Ethernet payload. | |
608 | * | |
d31f1109 JP |
609 | * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6 |
610 | * on output, then just past the IP header, if one is present and | |
611 | * of a correct length, otherwise the same as skb->network_header. | |
612 | * For other key->dl_type values it is left untouched. | |
a31e0e31 | 613 | */ |
850b6b3b | 614 | int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key, |
7257b535 | 615 | int *key_lenp) |
064af421 | 616 | { |
76abe283 AE |
617 | int error = 0; |
618 | int key_len = SW_FLOW_KEY_OFFSET(eth); | |
064af421 | 619 | struct ethhdr *eth; |
064af421 | 620 | |
84c17d98 | 621 | memset(key, 0, sizeof(*key)); |
abff858b PS |
622 | |
623 | key->phy.priority = skb->priority; | |
356af50b | 624 | if (OVS_CB(skb)->tun_key) |
6694e498 | 625 | memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key)); |
abff858b | 626 | key->phy.in_port = in_port; |
72e8bf28 | 627 | key->phy.skb_mark = skb_get_mark(skb); |
064af421 | 628 | |
064af421 | 629 | skb_reset_mac_header(skb); |
064af421 | 630 | |
2db65bf7 JG |
631 | /* Link layer. We are guaranteed to have at least the 14 byte Ethernet |
632 | * header in the linear data area. | |
633 | */ | |
50f06e16 | 634 | eth = eth_hdr(skb); |
76abe283 AE |
635 | memcpy(key->eth.src, eth->h_source, ETH_ALEN); |
636 | memcpy(key->eth.dst, eth->h_dest, ETH_ALEN); | |
637 | ||
50f06e16 | 638 | __skb_pull(skb, 2 * ETH_ALEN); |
6ce39213 JG |
639 | |
640 | if (vlan_tx_tag_present(skb)) | |
76abe283 | 641 | key->eth.tci = htons(vlan_get_tci(skb)); |
6ce39213 | 642 | else if (eth->h_proto == htons(ETH_P_8021Q)) |
2db65bf7 JG |
643 | if (unlikely(parse_vlan(skb, key))) |
644 | return -ENOMEM; | |
6ce39213 | 645 | |
76abe283 AE |
646 | key->eth.type = parse_ethertype(skb); |
647 | if (unlikely(key->eth.type == htons(0))) | |
2db65bf7 JG |
648 | return -ENOMEM; |
649 | ||
50f06e16 | 650 | skb_reset_network_header(skb); |
2db65bf7 | 651 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
064af421 BP |
652 | |
653 | /* Network layer. */ | |
76abe283 | 654 | if (key->eth.type == htons(ETH_P_IP)) { |
4c1ad233 | 655 | struct iphdr *nh; |
7257b535 | 656 | __be16 offset; |
76abe283 AE |
657 | |
658 | key_len = SW_FLOW_KEY_OFFSET(ipv4.addr); | |
4c1ad233 BP |
659 | |
660 | error = check_iphdr(skb); | |
661 | if (unlikely(error)) { | |
662 | if (error == -EINVAL) { | |
663 | skb->transport_header = skb->network_header; | |
76abe283 | 664 | error = 0; |
4c1ad233 | 665 | } |
76abe283 | 666 | goto out; |
4c1ad233 BP |
667 | } |
668 | ||
669 | nh = ip_hdr(skb); | |
76abe283 AE |
670 | key->ipv4.addr.src = nh->saddr; |
671 | key->ipv4.addr.dst = nh->daddr; | |
7257b535 | 672 | |
28bad473 | 673 | key->ip.proto = nh->protocol; |
530180fd | 674 | key->ip.tos = nh->tos; |
a61680c6 | 675 | key->ip.ttl = nh->ttl; |
064af421 | 676 | |
7257b535 BP |
677 | offset = nh->frag_off & htons(IP_OFFSET); |
678 | if (offset) { | |
9e44d715 | 679 | key->ip.frag = OVS_FRAG_TYPE_LATER; |
7257b535 BP |
680 | goto out; |
681 | } | |
682 | if (nh->frag_off & htons(IP_MF) || | |
683 | skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
9e44d715 | 684 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
b7a31ec1 | 685 | |
7257b535 | 686 | /* Transport layer. */ |
28bad473 | 687 | if (key->ip.proto == IPPROTO_TCP) { |
5f4d087c | 688 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 689 | if (tcphdr_ok(skb)) { |
5f4d087c JG |
690 | struct tcphdr *tcp = tcp_hdr(skb); |
691 | key->ipv4.tp.src = tcp->source; | |
692 | key->ipv4.tp.dst = tcp->dest; | |
693 | } | |
28bad473 | 694 | } else if (key->ip.proto == IPPROTO_UDP) { |
5f4d087c | 695 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 696 | if (udphdr_ok(skb)) { |
5f4d087c JG |
697 | struct udphdr *udp = udp_hdr(skb); |
698 | key->ipv4.tp.src = udp->source; | |
699 | key->ipv4.tp.dst = udp->dest; | |
700 | } | |
28bad473 | 701 | } else if (key->ip.proto == IPPROTO_ICMP) { |
5f4d087c | 702 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 703 | if (icmphdr_ok(skb)) { |
5f4d087c JG |
704 | struct icmphdr *icmp = icmp_hdr(skb); |
705 | /* The ICMP type and code fields use the 16-bit | |
6455100f PS |
706 | * transport port fields, so we need to store |
707 | * them in 16-bit network byte order. */ | |
5f4d087c JG |
708 | key->ipv4.tp.src = htons(icmp->type); |
709 | key->ipv4.tp.dst = htons(icmp->code); | |
710 | } | |
711 | } | |
712 | ||
8087f5ff MM |
713 | } else if ((key->eth.type == htons(ETH_P_ARP) || |
714 | key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) { | |
a26ef517 JP |
715 | struct arp_eth_header *arp; |
716 | ||
717 | arp = (struct arp_eth_header *)skb_network_header(skb); | |
718 | ||
f5e86186 | 719 | if (arp->ar_hrd == htons(ARPHRD_ETHER) |
de3f65ea JP |
720 | && arp->ar_pro == htons(ETH_P_IP) |
721 | && arp->ar_hln == ETH_ALEN | |
722 | && arp->ar_pln == 4) { | |
723 | ||
724 | /* We only match on the lower 8 bits of the opcode. */ | |
b7a31ec1 | 725 | if (ntohs(arp->ar_op) <= 0xff) |
28bad473 | 726 | key->ip.proto = ntohs(arp->ar_op); |
a3d3ad0c MM |
727 | memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src)); |
728 | memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst)); | |
729 | memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN); | |
730 | memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN); | |
731 | key_len = SW_FLOW_KEY_OFFSET(ipv4.arp); | |
de3f65ea | 732 | } |
76abe283 | 733 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
d31f1109 JP |
734 | int nh_len; /* IPv6 Header + Extensions */ |
735 | ||
76abe283 | 736 | nh_len = parse_ipv6hdr(skb, key, &key_len); |
d31f1109 | 737 | if (unlikely(nh_len < 0)) { |
76abe283 | 738 | if (nh_len == -EINVAL) |
d31f1109 | 739 | skb->transport_header = skb->network_header; |
76abe283 AE |
740 | else |
741 | error = nh_len; | |
742 | goto out; | |
d31f1109 JP |
743 | } |
744 | ||
9e44d715 | 745 | if (key->ip.frag == OVS_FRAG_TYPE_LATER) |
7257b535 BP |
746 | goto out; |
747 | if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
9e44d715 | 748 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
7257b535 | 749 | |
d31f1109 | 750 | /* Transport layer. */ |
28bad473 | 751 | if (key->ip.proto == NEXTHDR_TCP) { |
76abe283 | 752 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 JP |
753 | if (tcphdr_ok(skb)) { |
754 | struct tcphdr *tcp = tcp_hdr(skb); | |
76abe283 AE |
755 | key->ipv6.tp.src = tcp->source; |
756 | key->ipv6.tp.dst = tcp->dest; | |
d31f1109 | 757 | } |
28bad473 | 758 | } else if (key->ip.proto == NEXTHDR_UDP) { |
76abe283 | 759 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 JP |
760 | if (udphdr_ok(skb)) { |
761 | struct udphdr *udp = udp_hdr(skb); | |
76abe283 AE |
762 | key->ipv6.tp.src = udp->source; |
763 | key->ipv6.tp.dst = udp->dest; | |
d31f1109 | 764 | } |
28bad473 | 765 | } else if (key->ip.proto == NEXTHDR_ICMP) { |
76abe283 | 766 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 | 767 | if (icmp6hdr_ok(skb)) { |
76abe283 | 768 | error = parse_icmpv6(skb, key, &key_len, nh_len); |
685a51a5 | 769 | if (error < 0) |
76abe283 | 770 | goto out; |
d31f1109 JP |
771 | } |
772 | } | |
064af421 | 773 | } |
76abe283 AE |
774 | |
775 | out: | |
776 | *key_lenp = key_len; | |
777 | return error; | |
064af421 BP |
778 | } |
779 | ||
13e24889 | 780 | static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len) |
8d5ebd83 | 781 | { |
13e24889 PS |
782 | return jhash2((u32 *)((u8 *)key + key_start), |
783 | DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0); | |
784 | } | |
785 | ||
786 | static int flow_key_start(struct sw_flow_key *key) | |
787 | { | |
6694e498 | 788 | if (key->tun_key.ipv4_dst) |
13e24889 PS |
789 | return 0; |
790 | else | |
6694e498 | 791 | return offsetof(struct sw_flow_key, phy); |
8d5ebd83 JG |
792 | } |
793 | ||
850b6b3b | 794 | struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table, |
3544358a | 795 | struct sw_flow_key *key, int key_len) |
8d5ebd83 | 796 | { |
3544358a | 797 | struct sw_flow *flow; |
3544358a | 798 | struct hlist_head *head; |
13e24889 PS |
799 | u8 *_key; |
800 | int key_start; | |
3544358a | 801 | u32 hash; |
8d5ebd83 | 802 | |
13e24889 PS |
803 | key_start = flow_key_start(key); |
804 | hash = ovs_flow_hash(key, key_start, key_len); | |
3544358a | 805 | |
13e24889 | 806 | _key = (u8 *) key + key_start; |
3544358a | 807 | head = find_bucket(table, hash); |
f8dfbcb7 | 808 | hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) { |
3544358a PS |
809 | |
810 | if (flow->hash == hash && | |
13e24889 | 811 | !memcmp((u8 *)&flow->key + key_start, _key, key_len - key_start)) { |
3544358a PS |
812 | return flow; |
813 | } | |
814 | } | |
815 | return NULL; | |
816 | } | |
817 | ||
13e24889 PS |
818 | void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow, |
819 | struct sw_flow_key *key, int key_len) | |
3544358a | 820 | { |
13e24889 PS |
821 | flow->hash = ovs_flow_hash(key, flow_key_start(key), key_len); |
822 | memcpy(&flow->key, key, sizeof(flow->key)); | |
823 | __flow_tbl_insert(table, flow); | |
3544358a PS |
824 | } |
825 | ||
850b6b3b | 826 | void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) |
3544358a | 827 | { |
49c7f80c | 828 | BUG_ON(table->count == 0); |
acd051f1 PS |
829 | hlist_del_rcu(&flow->hash_node[table->node_ver]); |
830 | table->count--; | |
36956a7d BP |
831 | } |
832 | ||
df2c07f4 | 833 | /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */ |
4094406f BP |
834 | const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = { |
835 | [OVS_KEY_ATTR_ENCAP] = -1, | |
a1bf209f BP |
836 | [OVS_KEY_ATTR_PRIORITY] = sizeof(u32), |
837 | [OVS_KEY_ATTR_IN_PORT] = sizeof(u32), | |
72e8bf28 | 838 | [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32), |
df2c07f4 | 839 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), |
fea393b1 | 840 | [OVS_KEY_ATTR_VLAN] = sizeof(__be16), |
a1bf209f | 841 | [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16), |
df2c07f4 JP |
842 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), |
843 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), | |
844 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), | |
845 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), | |
846 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), | |
847 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), | |
848 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), | |
849 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), | |
9b405f1a | 850 | [OVS_KEY_ATTR_TUNNEL] = -1, |
80e5eed9 BP |
851 | }; |
852 | ||
34118cae BP |
853 | static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len, |
854 | const struct nlattr *a[], u64 *attrs) | |
855 | { | |
856 | const struct ovs_key_icmp *icmp_key; | |
857 | const struct ovs_key_tcp *tcp_key; | |
858 | const struct ovs_key_udp *udp_key; | |
859 | ||
860 | switch (swkey->ip.proto) { | |
861 | case IPPROTO_TCP: | |
862 | if (!(*attrs & (1 << OVS_KEY_ATTR_TCP))) | |
863 | return -EINVAL; | |
864 | *attrs &= ~(1 << OVS_KEY_ATTR_TCP); | |
865 | ||
866 | *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); | |
867 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); | |
868 | swkey->ipv4.tp.src = tcp_key->tcp_src; | |
869 | swkey->ipv4.tp.dst = tcp_key->tcp_dst; | |
870 | break; | |
871 | ||
872 | case IPPROTO_UDP: | |
873 | if (!(*attrs & (1 << OVS_KEY_ATTR_UDP))) | |
874 | return -EINVAL; | |
875 | *attrs &= ~(1 << OVS_KEY_ATTR_UDP); | |
876 | ||
877 | *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); | |
878 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); | |
879 | swkey->ipv4.tp.src = udp_key->udp_src; | |
880 | swkey->ipv4.tp.dst = udp_key->udp_dst; | |
881 | break; | |
882 | ||
883 | case IPPROTO_ICMP: | |
884 | if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP))) | |
885 | return -EINVAL; | |
886 | *attrs &= ~(1 << OVS_KEY_ATTR_ICMP); | |
887 | ||
888 | *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); | |
889 | icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]); | |
890 | swkey->ipv4.tp.src = htons(icmp_key->icmp_type); | |
891 | swkey->ipv4.tp.dst = htons(icmp_key->icmp_code); | |
892 | break; | |
893 | } | |
894 | ||
895 | return 0; | |
896 | } | |
897 | ||
898 | static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len, | |
899 | const struct nlattr *a[], u64 *attrs) | |
900 | { | |
901 | const struct ovs_key_icmpv6 *icmpv6_key; | |
902 | const struct ovs_key_tcp *tcp_key; | |
903 | const struct ovs_key_udp *udp_key; | |
904 | ||
905 | switch (swkey->ip.proto) { | |
906 | case IPPROTO_TCP: | |
907 | if (!(*attrs & (1 << OVS_KEY_ATTR_TCP))) | |
908 | return -EINVAL; | |
909 | *attrs &= ~(1 << OVS_KEY_ATTR_TCP); | |
910 | ||
911 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
912 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); | |
913 | swkey->ipv6.tp.src = tcp_key->tcp_src; | |
914 | swkey->ipv6.tp.dst = tcp_key->tcp_dst; | |
915 | break; | |
916 | ||
917 | case IPPROTO_UDP: | |
918 | if (!(*attrs & (1 << OVS_KEY_ATTR_UDP))) | |
919 | return -EINVAL; | |
920 | *attrs &= ~(1 << OVS_KEY_ATTR_UDP); | |
921 | ||
922 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
923 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); | |
924 | swkey->ipv6.tp.src = udp_key->udp_src; | |
925 | swkey->ipv6.tp.dst = udp_key->udp_dst; | |
926 | break; | |
927 | ||
928 | case IPPROTO_ICMPV6: | |
929 | if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6))) | |
930 | return -EINVAL; | |
931 | *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6); | |
932 | ||
933 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
934 | icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]); | |
935 | swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type); | |
936 | swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code); | |
937 | ||
938 | if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) || | |
939 | swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
940 | const struct ovs_key_nd *nd_key; | |
941 | ||
942 | if (!(*attrs & (1 << OVS_KEY_ATTR_ND))) | |
943 | return -EINVAL; | |
944 | *attrs &= ~(1 << OVS_KEY_ATTR_ND); | |
945 | ||
946 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd); | |
947 | nd_key = nla_data(a[OVS_KEY_ATTR_ND]); | |
948 | memcpy(&swkey->ipv6.nd.target, nd_key->nd_target, | |
949 | sizeof(swkey->ipv6.nd.target)); | |
950 | memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN); | |
951 | memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN); | |
952 | } | |
953 | break; | |
954 | } | |
955 | ||
956 | return 0; | |
957 | } | |
958 | ||
fea393b1 BP |
959 | static int parse_flow_nlattrs(const struct nlattr *attr, |
960 | const struct nlattr *a[], u64 *attrsp) | |
961 | { | |
962 | const struct nlattr *nla; | |
963 | u64 attrs; | |
964 | int rem; | |
965 | ||
966 | attrs = 0; | |
967 | nla_for_each_nested(nla, attr, rem) { | |
968 | u16 type = nla_type(nla); | |
4094406f | 969 | int expected_len; |
fea393b1 | 970 | |
4094406f | 971 | if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type)) |
fea393b1 | 972 | return -EINVAL; |
4094406f BP |
973 | |
974 | expected_len = ovs_key_lens[type]; | |
975 | if (nla_len(nla) != expected_len && expected_len != -1) | |
976 | return -EINVAL; | |
977 | ||
fea393b1 BP |
978 | attrs |= 1ULL << type; |
979 | a[type] = nla; | |
980 | } | |
981 | if (rem) | |
982 | return -EINVAL; | |
983 | ||
984 | *attrsp = attrs; | |
985 | return 0; | |
986 | } | |
987 | ||
9b405f1a PS |
988 | int ipv4_tun_from_nlattr(const struct nlattr *attr, |
989 | struct ovs_key_ipv4_tunnel *tun_key) | |
990 | { | |
991 | struct nlattr *a; | |
992 | int rem; | |
993 | bool ttl = false; | |
994 | ||
995 | memset(tun_key, 0, sizeof(*tun_key)); | |
996 | ||
997 | nla_for_each_nested(a, attr, rem) { | |
998 | int type = nla_type(a); | |
999 | static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { | |
1000 | [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64), | |
1001 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32), | |
1002 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32), | |
1003 | [OVS_TUNNEL_KEY_ATTR_TOS] = 1, | |
1004 | [OVS_TUNNEL_KEY_ATTR_TTL] = 1, | |
1005 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0, | |
1006 | [OVS_TUNNEL_KEY_ATTR_CSUM] = 0, | |
1007 | }; | |
1008 | ||
1009 | if (type > OVS_TUNNEL_KEY_ATTR_MAX || | |
1010 | ovs_tunnel_key_lens[type] != nla_len(a)) | |
1011 | return -EINVAL; | |
1012 | ||
1013 | switch (type) { | |
1014 | case OVS_TUNNEL_KEY_ATTR_ID: | |
1015 | tun_key->tun_id = nla_get_be64(a); | |
1016 | tun_key->tun_flags |= OVS_TNL_F_KEY; | |
1017 | break; | |
1018 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
1019 | tun_key->ipv4_src = nla_get_be32(a); | |
1020 | break; | |
1021 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
1022 | tun_key->ipv4_dst = nla_get_be32(a); | |
1023 | break; | |
1024 | case OVS_TUNNEL_KEY_ATTR_TOS: | |
1025 | tun_key->ipv4_tos = nla_get_u8(a); | |
1026 | break; | |
1027 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
1028 | tun_key->ipv4_ttl = nla_get_u8(a); | |
1029 | ttl = true; | |
1030 | break; | |
1031 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
1032 | tun_key->tun_flags |= OVS_TNL_F_DONT_FRAGMENT; | |
1033 | break; | |
1034 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
1035 | tun_key->tun_flags |= OVS_TNL_F_CSUM; | |
1036 | break; | |
1037 | default: | |
1038 | return -EINVAL; | |
1039 | ||
1040 | } | |
1041 | } | |
1042 | if (rem > 0) | |
1043 | return -EINVAL; | |
1044 | ||
1045 | if (!tun_key->ipv4_dst) | |
1046 | return -EINVAL; | |
1047 | ||
1048 | if (!ttl) | |
1049 | return -EINVAL; | |
1050 | ||
1051 | return 0; | |
1052 | } | |
1053 | ||
1054 | int ipv4_tun_to_nlattr(struct sk_buff *skb, | |
1055 | const struct ovs_key_ipv4_tunnel *tun_key) | |
1056 | { | |
1057 | struct nlattr *nla; | |
1058 | ||
1059 | nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL); | |
1060 | if (!nla) | |
1061 | return -EMSGSIZE; | |
1062 | ||
1063 | if (tun_key->tun_flags & OVS_TNL_F_KEY && | |
1064 | nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id)) | |
1065 | return -EMSGSIZE; | |
1066 | if (tun_key->ipv4_src && | |
1067 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ipv4_src)) | |
1068 | return -EMSGSIZE; | |
1069 | if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ipv4_dst)) | |
1070 | return -EMSGSIZE; | |
1071 | if (tun_key->ipv4_tos && | |
1072 | nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ipv4_tos)) | |
1073 | return -EMSGSIZE; | |
1074 | if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ipv4_ttl)) | |
1075 | return -EMSGSIZE; | |
1076 | if ((tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT) && | |
1077 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT)) | |
1078 | return -EMSGSIZE; | |
1079 | if ((tun_key->tun_flags & OVS_TNL_F_CSUM) && | |
1080 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM)) | |
1081 | return -EMSGSIZE; | |
1082 | ||
1083 | nla_nest_end(skb, nla); | |
1084 | return 0; | |
1085 | } | |
1086 | ||
36956a7d | 1087 | /** |
850b6b3b | 1088 | * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key. |
36956a7d | 1089 | * @swkey: receives the extracted flow key. |
76abe283 | 1090 | * @key_lenp: number of bytes used in @swkey. |
df2c07f4 | 1091 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
d6569377 | 1092 | * sequence. |
36956a7d | 1093 | */ |
850b6b3b | 1094 | int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp, |
76abe283 | 1095 | const struct nlattr *attr) |
36956a7d | 1096 | { |
34118cae BP |
1097 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
1098 | const struct ovs_key_ethernet *eth_key; | |
76abe283 | 1099 | int key_len; |
34118cae | 1100 | u64 attrs; |
fea393b1 | 1101 | int err; |
36956a7d | 1102 | |
34118cae | 1103 | memset(swkey, 0, sizeof(struct sw_flow_key)); |
76abe283 | 1104 | key_len = SW_FLOW_KEY_OFFSET(eth); |
36956a7d | 1105 | |
fea393b1 BP |
1106 | err = parse_flow_nlattrs(attr, a, &attrs); |
1107 | if (err) | |
1108 | return err; | |
36956a7d | 1109 | |
34118cae BP |
1110 | /* Metadata attributes. */ |
1111 | if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) { | |
1112 | swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]); | |
1113 | attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY); | |
1114 | } | |
1115 | if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) { | |
1116 | u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]); | |
1117 | if (in_port >= DP_MAX_PORTS) | |
1118 | return -EINVAL; | |
1119 | swkey->phy.in_port = in_port; | |
1120 | attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT); | |
1121 | } else { | |
95b1d73a | 1122 | swkey->phy.in_port = DP_MAX_PORTS; |
34118cae | 1123 | } |
72e8bf28 AA |
1124 | if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) { |
1125 | uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]); | |
1126 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER) | |
1127 | if (mark != 0) | |
1128 | return -EINVAL; | |
1129 | #endif | |
1130 | swkey->phy.skb_mark = mark; | |
1131 | attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK); | |
1132 | } | |
36956a7d | 1133 | |
85c9de19 | 1134 | if (attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) { |
9b405f1a PS |
1135 | err = ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], &swkey->tun_key); |
1136 | if (err) | |
1137 | return err; | |
5c7f5883 | 1138 | |
9b405f1a | 1139 | attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL); |
34118cae | 1140 | } |
76abe283 | 1141 | |
34118cae BP |
1142 | /* Data attributes. */ |
1143 | if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET))) | |
1144 | return -EINVAL; | |
1145 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET); | |
36956a7d | 1146 | |
34118cae BP |
1147 | eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]); |
1148 | memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN); | |
1149 | memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN); | |
76abe283 | 1150 | |
8ddc056d | 1151 | if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) && |
fea393b1 | 1152 | nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) { |
8ddc056d BP |
1153 | const struct nlattr *encap; |
1154 | __be16 tci; | |
1155 | ||
1156 | if (attrs != ((1 << OVS_KEY_ATTR_VLAN) | | |
1157 | (1 << OVS_KEY_ATTR_ETHERTYPE) | | |
1158 | (1 << OVS_KEY_ATTR_ENCAP))) | |
34118cae | 1159 | return -EINVAL; |
36956a7d | 1160 | |
8ddc056d BP |
1161 | encap = a[OVS_KEY_ATTR_ENCAP]; |
1162 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
1163 | if (tci & htons(VLAN_TAG_PRESENT)) { | |
1164 | swkey->eth.tci = tci; | |
1165 | ||
1166 | err = parse_flow_nlattrs(encap, a, &attrs); | |
1167 | if (err) | |
1168 | return err; | |
1169 | } else if (!tci) { | |
1170 | /* Corner case for truncated 802.1Q header. */ | |
1171 | if (nla_len(encap)) | |
1172 | return -EINVAL; | |
1173 | ||
1174 | swkey->eth.type = htons(ETH_P_8021Q); | |
1175 | *key_lenp = key_len; | |
1176 | return 0; | |
1177 | } else { | |
1178 | return -EINVAL; | |
1179 | } | |
34118cae BP |
1180 | } |
1181 | ||
1182 | if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) { | |
1183 | swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
7cd46155 | 1184 | if (ntohs(swkey->eth.type) < ETH_P_802_3_MIN) |
34118cae BP |
1185 | return -EINVAL; |
1186 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); | |
1187 | } else { | |
1188 | swkey->eth.type = htons(ETH_P_802_2); | |
1189 | } | |
1190 | ||
1191 | if (swkey->eth.type == htons(ETH_P_IP)) { | |
1192 | const struct ovs_key_ipv4 *ipv4_key; | |
685a51a5 | 1193 | |
34118cae BP |
1194 | if (!(attrs & (1 << OVS_KEY_ATTR_IPV4))) |
1195 | return -EINVAL; | |
1196 | attrs &= ~(1 << OVS_KEY_ATTR_IPV4); | |
1197 | ||
1198 | key_len = SW_FLOW_KEY_OFFSET(ipv4.addr); | |
1199 | ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]); | |
1200 | if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) | |
1201 | return -EINVAL; | |
1202 | swkey->ip.proto = ipv4_key->ipv4_proto; | |
1203 | swkey->ip.tos = ipv4_key->ipv4_tos; | |
1204 | swkey->ip.ttl = ipv4_key->ipv4_ttl; | |
1205 | swkey->ip.frag = ipv4_key->ipv4_frag; | |
1206 | swkey->ipv4.addr.src = ipv4_key->ipv4_src; | |
1207 | swkey->ipv4.addr.dst = ipv4_key->ipv4_dst; | |
1208 | ||
1209 | if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) { | |
fea393b1 | 1210 | err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs); |
34118cae BP |
1211 | if (err) |
1212 | return err; | |
36956a7d | 1213 | } |
34118cae BP |
1214 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
1215 | const struct ovs_key_ipv6 *ipv6_key; | |
36956a7d | 1216 | |
34118cae BP |
1217 | if (!(attrs & (1 << OVS_KEY_ATTR_IPV6))) |
1218 | return -EINVAL; | |
1219 | attrs &= ~(1 << OVS_KEY_ATTR_IPV6); | |
1220 | ||
1221 | key_len = SW_FLOW_KEY_OFFSET(ipv6.label); | |
1222 | ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]); | |
1223 | if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) | |
1224 | return -EINVAL; | |
1225 | swkey->ipv6.label = ipv6_key->ipv6_label; | |
1226 | swkey->ip.proto = ipv6_key->ipv6_proto; | |
1227 | swkey->ip.tos = ipv6_key->ipv6_tclass; | |
1228 | swkey->ip.ttl = ipv6_key->ipv6_hlimit; | |
1229 | swkey->ip.frag = ipv6_key->ipv6_frag; | |
1230 | memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src, | |
1231 | sizeof(swkey->ipv6.addr.src)); | |
1232 | memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst, | |
1233 | sizeof(swkey->ipv6.addr.dst)); | |
1234 | ||
1235 | if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) { | |
fea393b1 | 1236 | err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs); |
34118cae BP |
1237 | if (err) |
1238 | return err; | |
1239 | } | |
8087f5ff MM |
1240 | } else if (swkey->eth.type == htons(ETH_P_ARP) || |
1241 | swkey->eth.type == htons(ETH_P_RARP)) { | |
34118cae | 1242 | const struct ovs_key_arp *arp_key; |
36956a7d | 1243 | |
34118cae BP |
1244 | if (!(attrs & (1 << OVS_KEY_ATTR_ARP))) |
1245 | return -EINVAL; | |
1246 | attrs &= ~(1 << OVS_KEY_ATTR_ARP); | |
1247 | ||
1248 | key_len = SW_FLOW_KEY_OFFSET(ipv4.arp); | |
1249 | arp_key = nla_data(a[OVS_KEY_ATTR_ARP]); | |
1250 | swkey->ipv4.addr.src = arp_key->arp_sip; | |
1251 | swkey->ipv4.addr.dst = arp_key->arp_tip; | |
1252 | if (arp_key->arp_op & htons(0xff00)) | |
1253 | return -EINVAL; | |
1254 | swkey->ip.proto = ntohs(arp_key->arp_op); | |
1255 | memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN); | |
1256 | memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN); | |
1257 | } | |
76abe283 | 1258 | |
34118cae BP |
1259 | if (attrs) |
1260 | return -EINVAL; | |
76abe283 | 1261 | *key_lenp = key_len; |
34118cae BP |
1262 | |
1263 | return 0; | |
36956a7d BP |
1264 | } |
1265 | ||
80e5eed9 | 1266 | /** |
850b6b3b | 1267 | * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key. |
c3b36027 PS |
1268 | * @flow: Receives extracted in_port, priority, tun_key and skb_mark. |
1269 | * @key_len: Length of key in @flow. Used for calculating flow hash. | |
1270 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute | |
80e5eed9 BP |
1271 | * sequence. |
1272 | * | |
1273 | * This parses a series of Netlink attributes that form a flow key, which must | |
1274 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to | |
1275 | * get the metadata, that is, the parts of the flow key that cannot be | |
1276 | * extracted from the packet itself. | |
1277 | */ | |
13e24889 PS |
1278 | |
1279 | int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, int key_len, const struct nlattr *attr) | |
80e5eed9 | 1280 | { |
6694e498 | 1281 | struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key; |
80e5eed9 | 1282 | const struct nlattr *nla; |
80e5eed9 BP |
1283 | int rem; |
1284 | ||
13e24889 PS |
1285 | flow->key.phy.in_port = DP_MAX_PORTS; |
1286 | flow->key.phy.priority = 0; | |
72e8bf28 | 1287 | flow->key.phy.skb_mark = 0; |
6694e498 | 1288 | memset(tun_key, 0, sizeof(flow->key.tun_key)); |
80e5eed9 | 1289 | |
80e5eed9 | 1290 | nla_for_each_nested(nla, attr, rem) { |
6455100f | 1291 | int type = nla_type(nla); |
80e5eed9 | 1292 | |
4094406f | 1293 | if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) { |
9b405f1a PS |
1294 | int err; |
1295 | ||
58828b08 AA |
1296 | if (nla_len(nla) != ovs_key_lens[type]) |
1297 | return -EINVAL; | |
abff858b | 1298 | |
58828b08 AA |
1299 | switch (type) { |
1300 | case OVS_KEY_ATTR_PRIORITY: | |
13e24889 | 1301 | flow->key.phy.priority = nla_get_u32(nla); |
58828b08 | 1302 | break; |
80e5eed9 | 1303 | |
9b405f1a | 1304 | case OVS_KEY_ATTR_TUNNEL: |
85c9de19 PS |
1305 | err = ipv4_tun_from_nlattr(nla, tun_key); |
1306 | if (err) | |
1307 | return err; | |
58828b08 | 1308 | break; |
80e5eed9 | 1309 | |
58828b08 AA |
1310 | case OVS_KEY_ATTR_IN_PORT: |
1311 | if (nla_get_u32(nla) >= DP_MAX_PORTS) | |
1312 | return -EINVAL; | |
13e24889 | 1313 | flow->key.phy.in_port = nla_get_u32(nla); |
58828b08 | 1314 | break; |
72e8bf28 AA |
1315 | |
1316 | case OVS_KEY_ATTR_SKB_MARK: | |
1317 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER) | |
1318 | if (nla_get_u32(nla) != 0) | |
1319 | return -EINVAL; | |
1320 | #endif | |
1321 | flow->key.phy.skb_mark = nla_get_u32(nla); | |
1322 | break; | |
58828b08 | 1323 | } |
80e5eed9 | 1324 | } |
80e5eed9 BP |
1325 | } |
1326 | if (rem) | |
1327 | return -EINVAL; | |
13e24889 PS |
1328 | |
1329 | flow->hash = ovs_flow_hash(&flow->key, | |
1330 | flow_key_start(&flow->key), key_len); | |
1331 | ||
80e5eed9 BP |
1332 | return 0; |
1333 | } | |
1334 | ||
850b6b3b | 1335 | int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb) |
36956a7d | 1336 | { |
df2c07f4 | 1337 | struct ovs_key_ethernet *eth_key; |
fea393b1 | 1338 | struct nlattr *nla, *encap; |
36956a7d | 1339 | |
c3cc8c03 DM |
1340 | if (swkey->phy.priority && |
1341 | nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority)) | |
1342 | goto nla_put_failure; | |
18c43631 | 1343 | |
9b405f1a PS |
1344 | if (swkey->tun_key.ipv4_dst && |
1345 | ipv4_tun_to_nlattr(skb, &swkey->tun_key)) | |
1346 | goto nla_put_failure; | |
1347 | ||
c3cc8c03 DM |
1348 | if (swkey->phy.in_port != DP_MAX_PORTS && |
1349 | nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port)) | |
1350 | goto nla_put_failure; | |
36956a7d | 1351 | |
72e8bf28 AA |
1352 | if (swkey->phy.skb_mark && |
1353 | nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark)) | |
1354 | goto nla_put_failure; | |
1355 | ||
df2c07f4 | 1356 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); |
d6569377 BP |
1357 | if (!nla) |
1358 | goto nla_put_failure; | |
1359 | eth_key = nla_data(nla); | |
76abe283 AE |
1360 | memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN); |
1361 | memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN); | |
36956a7d | 1362 | |
8ddc056d | 1363 | if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) { |
c3cc8c03 DM |
1364 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) || |
1365 | nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci)) | |
1366 | goto nla_put_failure; | |
fea393b1 | 1367 | encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP); |
8ddc056d BP |
1368 | if (!swkey->eth.tci) |
1369 | goto unencap; | |
fea393b1 BP |
1370 | } else { |
1371 | encap = NULL; | |
36956a7d BP |
1372 | } |
1373 | ||
76abe283 | 1374 | if (swkey->eth.type == htons(ETH_P_802_2)) |
fea393b1 | 1375 | goto unencap; |
36956a7d | 1376 | |
c3cc8c03 DM |
1377 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type)) |
1378 | goto nla_put_failure; | |
36956a7d | 1379 | |
76abe283 | 1380 | if (swkey->eth.type == htons(ETH_P_IP)) { |
df2c07f4 | 1381 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 1382 | |
df2c07f4 | 1383 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); |
d6569377 BP |
1384 | if (!nla) |
1385 | goto nla_put_failure; | |
1386 | ipv4_key = nla_data(nla); | |
76abe283 AE |
1387 | ipv4_key->ipv4_src = swkey->ipv4.addr.src; |
1388 | ipv4_key->ipv4_dst = swkey->ipv4.addr.dst; | |
28bad473 | 1389 | ipv4_key->ipv4_proto = swkey->ip.proto; |
530180fd | 1390 | ipv4_key->ipv4_tos = swkey->ip.tos; |
a61680c6 | 1391 | ipv4_key->ipv4_ttl = swkey->ip.ttl; |
9e44d715 | 1392 | ipv4_key->ipv4_frag = swkey->ip.frag; |
76abe283 | 1393 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
df2c07f4 | 1394 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 1395 | |
df2c07f4 | 1396 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); |
d31f1109 JP |
1397 | if (!nla) |
1398 | goto nla_put_failure; | |
1399 | ipv6_key = nla_data(nla); | |
76abe283 | 1400 | memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src, |
d31f1109 | 1401 | sizeof(ipv6_key->ipv6_src)); |
76abe283 | 1402 | memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst, |
d31f1109 | 1403 | sizeof(ipv6_key->ipv6_dst)); |
fa8223b7 | 1404 | ipv6_key->ipv6_label = swkey->ipv6.label; |
28bad473 | 1405 | ipv6_key->ipv6_proto = swkey->ip.proto; |
60258dcb | 1406 | ipv6_key->ipv6_tclass = swkey->ip.tos; |
a61680c6 | 1407 | ipv6_key->ipv6_hlimit = swkey->ip.ttl; |
9e44d715 | 1408 | ipv6_key->ipv6_frag = swkey->ip.frag; |
8087f5ff MM |
1409 | } else if (swkey->eth.type == htons(ETH_P_ARP) || |
1410 | swkey->eth.type == htons(ETH_P_RARP)) { | |
df2c07f4 | 1411 | struct ovs_key_arp *arp_key; |
d31f1109 | 1412 | |
df2c07f4 | 1413 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); |
d31f1109 JP |
1414 | if (!nla) |
1415 | goto nla_put_failure; | |
1416 | arp_key = nla_data(nla); | |
df2c07f4 | 1417 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); |
76abe283 AE |
1418 | arp_key->arp_sip = swkey->ipv4.addr.src; |
1419 | arp_key->arp_tip = swkey->ipv4.addr.dst; | |
28bad473 | 1420 | arp_key->arp_op = htons(swkey->ip.proto); |
76abe283 AE |
1421 | memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN); |
1422 | memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN); | |
d31f1109 JP |
1423 | } |
1424 | ||
7257b535 BP |
1425 | if ((swkey->eth.type == htons(ETH_P_IP) || |
1426 | swkey->eth.type == htons(ETH_P_IPV6)) && | |
9e44d715 | 1427 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { |
36956a7d | 1428 | |
28bad473 | 1429 | if (swkey->ip.proto == IPPROTO_TCP) { |
df2c07f4 | 1430 | struct ovs_key_tcp *tcp_key; |
36956a7d | 1431 | |
df2c07f4 | 1432 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); |
d6569377 BP |
1433 | if (!nla) |
1434 | goto nla_put_failure; | |
1435 | tcp_key = nla_data(nla); | |
76abe283 AE |
1436 | if (swkey->eth.type == htons(ETH_P_IP)) { |
1437 | tcp_key->tcp_src = swkey->ipv4.tp.src; | |
1438 | tcp_key->tcp_dst = swkey->ipv4.tp.dst; | |
1439 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
1440 | tcp_key->tcp_src = swkey->ipv6.tp.src; | |
1441 | tcp_key->tcp_dst = swkey->ipv6.tp.dst; | |
1442 | } | |
28bad473 | 1443 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
df2c07f4 | 1444 | struct ovs_key_udp *udp_key; |
36956a7d | 1445 | |
df2c07f4 | 1446 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); |
d6569377 BP |
1447 | if (!nla) |
1448 | goto nla_put_failure; | |
1449 | udp_key = nla_data(nla); | |
76abe283 AE |
1450 | if (swkey->eth.type == htons(ETH_P_IP)) { |
1451 | udp_key->udp_src = swkey->ipv4.tp.src; | |
1452 | udp_key->udp_dst = swkey->ipv4.tp.dst; | |
1453 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
1454 | udp_key->udp_src = swkey->ipv6.tp.src; | |
1455 | udp_key->udp_dst = swkey->ipv6.tp.dst; | |
1456 | } | |
1457 | } else if (swkey->eth.type == htons(ETH_P_IP) && | |
28bad473 | 1458 | swkey->ip.proto == IPPROTO_ICMP) { |
df2c07f4 | 1459 | struct ovs_key_icmp *icmp_key; |
36956a7d | 1460 | |
df2c07f4 | 1461 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); |
d6569377 BP |
1462 | if (!nla) |
1463 | goto nla_put_failure; | |
1464 | icmp_key = nla_data(nla); | |
76abe283 AE |
1465 | icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src); |
1466 | icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst); | |
1467 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && | |
28bad473 | 1468 | swkey->ip.proto == IPPROTO_ICMPV6) { |
df2c07f4 | 1469 | struct ovs_key_icmpv6 *icmpv6_key; |
36956a7d | 1470 | |
df2c07f4 | 1471 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, |
bfef4717 | 1472 | sizeof(*icmpv6_key)); |
d31f1109 JP |
1473 | if (!nla) |
1474 | goto nla_put_failure; | |
1475 | icmpv6_key = nla_data(nla); | |
76abe283 AE |
1476 | icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src); |
1477 | icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst); | |
685a51a5 | 1478 | |
bfef4717 JG |
1479 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || |
1480 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { | |
df2c07f4 | 1481 | struct ovs_key_nd *nd_key; |
685a51a5 | 1482 | |
df2c07f4 | 1483 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); |
685a51a5 JP |
1484 | if (!nla) |
1485 | goto nla_put_failure; | |
1486 | nd_key = nla_data(nla); | |
76abe283 | 1487 | memcpy(nd_key->nd_target, &swkey->ipv6.nd.target, |
685a51a5 | 1488 | sizeof(nd_key->nd_target)); |
76abe283 AE |
1489 | memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN); |
1490 | memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN); | |
685a51a5 | 1491 | } |
d31f1109 | 1492 | } |
36956a7d BP |
1493 | } |
1494 | ||
fea393b1 BP |
1495 | unencap: |
1496 | if (encap) | |
1497 | nla_nest_end(skb, encap); | |
1498 | ||
d6569377 | 1499 | return 0; |
36956a7d | 1500 | |
d6569377 BP |
1501 | nla_put_failure: |
1502 | return -EMSGSIZE; | |
8d5ebd83 JG |
1503 | } |
1504 | ||
064af421 BP |
1505 | /* Initializes the flow module. |
1506 | * Returns zero if successful or a negative error code. */ | |
850b6b3b | 1507 | int ovs_flow_init(void) |
064af421 BP |
1508 | { |
1509 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, | |
1510 | 0, NULL); | |
1511 | if (flow_cache == NULL) | |
1512 | return -ENOMEM; | |
1513 | ||
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | /* Uninitializes the flow module. */ | |
850b6b3b | 1518 | void ovs_flow_exit(void) |
064af421 BP |
1519 | { |
1520 | kmem_cache_destroy(flow_cache); | |
1521 | } |