]>
Commit | Line | Data |
---|---|---|
064af421 BP |
1 | /* |
2 | * Distributed under the terms of the GNU GPL version 2. | |
f632c8fc | 3 | * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks. |
a14bc59f BP |
4 | * |
5 | * Significant portions of this file may be copied from parts of the Linux | |
6 | * kernel, by Linus Torvalds and others. | |
064af421 BP |
7 | */ |
8 | ||
9 | #include "flow.h" | |
f5e86186 | 10 | #include "datapath.h" |
6455100f | 11 | #include <linux/uaccess.h> |
064af421 BP |
12 | #include <linux/netdevice.h> |
13 | #include <linux/etherdevice.h> | |
14 | #include <linux/if_ether.h> | |
15 | #include <linux/if_vlan.h> | |
16 | #include <net/llc_pdu.h> | |
17 | #include <linux/kernel.h> | |
8d5ebd83 | 18 | #include <linux/jhash.h> |
064af421 BP |
19 | #include <linux/jiffies.h> |
20 | #include <linux/llc.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/in.h> | |
23 | #include <linux/rcupdate.h> | |
a26ef517 | 24 | #include <linux/if_arp.h> |
064af421 BP |
25 | #include <linux/if_ether.h> |
26 | #include <linux/ip.h> | |
d31f1109 | 27 | #include <linux/ipv6.h> |
064af421 BP |
28 | #include <linux/tcp.h> |
29 | #include <linux/udp.h> | |
30 | #include <linux/icmp.h> | |
d31f1109 | 31 | #include <linux/icmpv6.h> |
3544358a | 32 | #include <linux/rculist.h> |
064af421 | 33 | #include <net/ip.h> |
d31f1109 | 34 | #include <net/ipv6.h> |
685a51a5 | 35 | #include <net/ndisc.h> |
064af421 | 36 | |
6ce39213 JG |
37 | #include "vlan.h" |
38 | ||
33b38b63 | 39 | static struct kmem_cache *flow_cache; |
83e3e75b | 40 | static unsigned int hash_seed __read_mostly; |
064af421 | 41 | |
2db65bf7 JG |
42 | static int check_header(struct sk_buff *skb, int len) |
43 | { | |
44 | if (unlikely(skb->len < len)) | |
45 | return -EINVAL; | |
46 | if (unlikely(!pskb_may_pull(skb, len))) | |
47 | return -ENOMEM; | |
48 | return 0; | |
49 | } | |
50 | ||
6455100f | 51 | static bool arphdr_ok(struct sk_buff *skb) |
a26ef517 | 52 | { |
2db65bf7 JG |
53 | return pskb_may_pull(skb, skb_network_offset(skb) + |
54 | sizeof(struct arp_eth_header)); | |
a26ef517 JP |
55 | } |
56 | ||
6455100f | 57 | static int check_iphdr(struct sk_buff *skb) |
064af421 | 58 | { |
4c1ad233 BP |
59 | unsigned int nh_ofs = skb_network_offset(skb); |
60 | unsigned int ip_len; | |
2db65bf7 | 61 | int err; |
4c1ad233 | 62 | |
2db65bf7 JG |
63 | err = check_header(skb, nh_ofs + sizeof(struct iphdr)); |
64 | if (unlikely(err)) | |
65 | return err; | |
4c1ad233 BP |
66 | |
67 | ip_len = ip_hdrlen(skb); | |
2db65bf7 JG |
68 | if (unlikely(ip_len < sizeof(struct iphdr) || |
69 | skb->len < nh_ofs + ip_len)) | |
4c1ad233 BP |
70 | return -EINVAL; |
71 | ||
4c1ad233 BP |
72 | skb_set_transport_header(skb, nh_ofs + ip_len); |
73 | return 0; | |
064af421 BP |
74 | } |
75 | ||
6455100f | 76 | static bool tcphdr_ok(struct sk_buff *skb) |
064af421 BP |
77 | { |
78 | int th_ofs = skb_transport_offset(skb); | |
2db65bf7 JG |
79 | int tcp_len; |
80 | ||
81 | if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr)))) | |
82 | return false; | |
83 | ||
84 | tcp_len = tcp_hdrlen(skb); | |
85 | if (unlikely(tcp_len < sizeof(struct tcphdr) || | |
86 | skb->len < th_ofs + tcp_len)) | |
87 | return false; | |
88 | ||
89 | return true; | |
064af421 BP |
90 | } |
91 | ||
6455100f | 92 | static bool udphdr_ok(struct sk_buff *skb) |
064af421 | 93 | { |
2db65bf7 JG |
94 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
95 | sizeof(struct udphdr)); | |
064af421 BP |
96 | } |
97 | ||
6455100f | 98 | static bool icmphdr_ok(struct sk_buff *skb) |
064af421 | 99 | { |
2db65bf7 JG |
100 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
101 | sizeof(struct icmphdr)); | |
064af421 BP |
102 | } |
103 | ||
ec58547a JG |
104 | u64 flow_used_time(unsigned long flow_jiffies) |
105 | { | |
106 | struct timespec cur_ts; | |
107 | u64 cur_ms, idle_ms; | |
108 | ||
109 | ktime_get_ts(&cur_ts); | |
110 | idle_ms = jiffies_to_msecs(jiffies - flow_jiffies); | |
111 | cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC + | |
112 | cur_ts.tv_nsec / NSEC_PER_MSEC; | |
113 | ||
114 | return cur_ms - idle_ms; | |
115 | } | |
116 | ||
76abe283 | 117 | #define SW_FLOW_KEY_OFFSET(field) \ |
6455100f PS |
118 | (offsetof(struct sw_flow_key, field) + \ |
119 | FIELD_SIZEOF(struct sw_flow_key, field)) | |
76abe283 | 120 | |
7257b535 BP |
121 | /** |
122 | * skip_exthdr - skip any IPv6 extension headers | |
123 | * @skb: skbuff to parse | |
124 | * @start: offset of first extension header | |
125 | * @nexthdrp: Initially, points to the type of the extension header at @start. | |
126 | * This function updates it to point to the extension header at the final | |
127 | * offset. | |
9e44d715 | 128 | * @frag: Points to the @frag member in a &struct sw_flow_key. This |
7257b535 BP |
129 | * function sets an appropriate %OVS_FRAG_TYPE_* value. |
130 | * | |
9e44d715 | 131 | * This is based on ipv6_skip_exthdr() but adds the updates to *@frag. |
7257b535 BP |
132 | * |
133 | * When there is more than one fragment header, this version reports whether | |
134 | * the final fragment header that it examines is a first fragment. | |
135 | * | |
136 | * Returns the final payload offset, or -1 on error. | |
137 | */ | |
138 | static int skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp, | |
9e44d715 | 139 | u8 *frag) |
7257b535 BP |
140 | { |
141 | u8 nexthdr = *nexthdrp; | |
142 | ||
143 | while (ipv6_ext_hdr(nexthdr)) { | |
144 | struct ipv6_opt_hdr _hdr, *hp; | |
145 | int hdrlen; | |
146 | ||
147 | if (nexthdr == NEXTHDR_NONE) | |
148 | return -1; | |
149 | hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr); | |
150 | if (hp == NULL) | |
151 | return -1; | |
152 | if (nexthdr == NEXTHDR_FRAGMENT) { | |
153 | __be16 _frag_off, *fp; | |
154 | fp = skb_header_pointer(skb, | |
155 | start+offsetof(struct frag_hdr, | |
156 | frag_off), | |
157 | sizeof(_frag_off), | |
158 | &_frag_off); | |
159 | if (fp == NULL) | |
160 | return -1; | |
161 | ||
7257b535 | 162 | if (ntohs(*fp) & ~0x7) { |
9e44d715 | 163 | *frag = OVS_FRAG_TYPE_LATER; |
7257b535 BP |
164 | break; |
165 | } | |
9e44d715 | 166 | *frag = OVS_FRAG_TYPE_FIRST; |
7257b535 BP |
167 | hdrlen = 8; |
168 | } else if (nexthdr == NEXTHDR_AUTH) | |
169 | hdrlen = (hp->hdrlen+2)<<2; | |
170 | else | |
171 | hdrlen = ipv6_optlen(hp); | |
172 | ||
173 | nexthdr = hp->nexthdr; | |
174 | start += hdrlen; | |
175 | } | |
176 | ||
177 | *nexthdrp = nexthdr; | |
178 | return start; | |
179 | } | |
180 | ||
76abe283 AE |
181 | static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key, |
182 | int *key_lenp) | |
d31f1109 JP |
183 | { |
184 | unsigned int nh_ofs = skb_network_offset(skb); | |
185 | unsigned int nh_len; | |
186 | int payload_ofs; | |
d31f1109 JP |
187 | struct ipv6hdr *nh; |
188 | uint8_t nexthdr; | |
2db65bf7 | 189 | int err; |
d31f1109 | 190 | |
fa8223b7 | 191 | *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label); |
76abe283 | 192 | |
2db65bf7 JG |
193 | err = check_header(skb, nh_ofs + sizeof(*nh)); |
194 | if (unlikely(err)) | |
195 | return err; | |
d31f1109 JP |
196 | |
197 | nh = ipv6_hdr(skb); | |
198 | nexthdr = nh->nexthdr; | |
199 | payload_ofs = (u8 *)(nh + 1) - skb->data; | |
d31f1109 | 200 | |
28bad473 | 201 | key->ip.proto = NEXTHDR_NONE; |
530180fd | 202 | key->ip.tos = ipv6_get_dsfield(nh); |
a61680c6 | 203 | key->ip.ttl = nh->hop_limit; |
fa8223b7 | 204 | key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL); |
76abe283 AE |
205 | ipv6_addr_copy(&key->ipv6.addr.src, &nh->saddr); |
206 | ipv6_addr_copy(&key->ipv6.addr.dst, &nh->daddr); | |
d31f1109 | 207 | |
9e44d715 | 208 | payload_ofs = skip_exthdr(skb, payload_ofs, &nexthdr, &key->ip.frag); |
bfef4717 | 209 | if (unlikely(payload_ofs < 0)) |
d31f1109 | 210 | return -EINVAL; |
bfef4717 | 211 | |
d31f1109 | 212 | nh_len = payload_ofs - nh_ofs; |
d31f1109 | 213 | skb_set_transport_header(skb, nh_ofs + nh_len); |
28bad473 | 214 | key->ip.proto = nexthdr; |
d31f1109 JP |
215 | return nh_len; |
216 | } | |
217 | ||
218 | static bool icmp6hdr_ok(struct sk_buff *skb) | |
219 | { | |
2db65bf7 JG |
220 | return pskb_may_pull(skb, skb_transport_offset(skb) + |
221 | sizeof(struct icmp6hdr)); | |
d31f1109 | 222 | } |
ec58547a | 223 | |
064af421 BP |
224 | #define TCP_FLAGS_OFFSET 13 |
225 | #define TCP_FLAG_MASK 0x3f | |
226 | ||
064af421 BP |
227 | void flow_used(struct sw_flow *flow, struct sk_buff *skb) |
228 | { | |
064af421 BP |
229 | u8 tcp_flags = 0; |
230 | ||
76abe283 | 231 | if (flow->key.eth.type == htons(ETH_P_IP) && |
28bad473 | 232 | flow->key.ip.proto == IPPROTO_TCP) { |
abfec865 BP |
233 | u8 *tcp = (u8 *)tcp_hdr(skb); |
234 | tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK; | |
064af421 BP |
235 | } |
236 | ||
e9141eec | 237 | spin_lock(&flow->lock); |
6bfafa55 | 238 | flow->used = jiffies; |
064af421 BP |
239 | flow->packet_count++; |
240 | flow->byte_count += skb->len; | |
241 | flow->tcp_flags |= tcp_flags; | |
e9141eec | 242 | spin_unlock(&flow->lock); |
064af421 BP |
243 | } |
244 | ||
37a1300c | 245 | struct sw_flow_actions *flow_actions_alloc(const struct nlattr *actions) |
064af421 | 246 | { |
37a1300c | 247 | int actions_len = nla_len(actions); |
064af421 BP |
248 | struct sw_flow_actions *sfa; |
249 | ||
722d19c5 BP |
250 | /* At least DP_MAX_PORTS actions are required to be able to flood a |
251 | * packet to every port. Factor of 2 allows for setting VLAN tags, | |
252 | * etc. */ | |
cdee00fd | 253 | if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4)) |
064af421 BP |
254 | return ERR_PTR(-EINVAL); |
255 | ||
84c17d98 | 256 | sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL); |
064af421 BP |
257 | if (!sfa) |
258 | return ERR_PTR(-ENOMEM); | |
259 | ||
cdee00fd | 260 | sfa->actions_len = actions_len; |
37a1300c | 261 | memcpy(sfa->actions, nla_data(actions), actions_len); |
064af421 BP |
262 | return sfa; |
263 | } | |
264 | ||
560e8022 JG |
265 | struct sw_flow *flow_alloc(void) |
266 | { | |
267 | struct sw_flow *flow; | |
268 | ||
269 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); | |
270 | if (!flow) | |
271 | return ERR_PTR(-ENOMEM); | |
272 | ||
273 | spin_lock_init(&flow->lock); | |
fb8c9347 | 274 | atomic_set(&flow->refcnt, 1); |
6f04e94a | 275 | flow->sf_acts = NULL; |
fb8c9347 | 276 | flow->dead = false; |
064af421 | 277 | |
560e8022 JG |
278 | return flow; |
279 | } | |
280 | ||
3544358a | 281 | static struct hlist_head __rcu *find_bucket(struct flow_table * table, u32 hash) |
8d5ebd83 | 282 | { |
3544358a PS |
283 | return flex_array_get(table->buckets, |
284 | (hash & (table->n_buckets - 1))); | |
285 | } | |
286 | ||
287 | static struct flex_array __rcu *alloc_buckets(unsigned int n_buckets) | |
288 | { | |
6455100f | 289 | struct flex_array __rcu *buckets; |
3544358a PS |
290 | int i, err; |
291 | ||
292 | buckets = flex_array_alloc(sizeof(struct hlist_head *), | |
293 | n_buckets, GFP_KERNEL); | |
294 | if (!buckets) | |
295 | return NULL; | |
296 | ||
297 | err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); | |
298 | if (err) { | |
299 | flex_array_free(buckets); | |
300 | return NULL; | |
301 | } | |
302 | ||
303 | for (i = 0; i < n_buckets; i++) | |
304 | INIT_HLIST_HEAD((struct hlist_head *) | |
305 | flex_array_get(buckets, i)); | |
306 | ||
307 | return buckets; | |
308 | } | |
309 | ||
6455100f | 310 | static void free_buckets(struct flex_array *buckets) |
3544358a PS |
311 | { |
312 | flex_array_free(buckets); | |
313 | } | |
314 | ||
315 | struct flow_table *flow_tbl_alloc(int new_size) | |
316 | { | |
317 | struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL); | |
318 | ||
319 | if (!table) | |
320 | return NULL; | |
321 | ||
322 | table->buckets = alloc_buckets(new_size); | |
323 | ||
324 | if (!table->buckets) { | |
325 | kfree(table); | |
326 | return NULL; | |
327 | } | |
328 | table->n_buckets = new_size; | |
329 | table->count = 0; | |
330 | ||
331 | return table; | |
332 | } | |
fb8c9347 | 333 | |
3544358a PS |
334 | static void flow_free(struct sw_flow *flow) |
335 | { | |
fb8c9347 JG |
336 | flow->dead = true; |
337 | flow_put(flow); | |
8d5ebd83 JG |
338 | } |
339 | ||
3544358a PS |
340 | void flow_tbl_destroy(struct flow_table *table) |
341 | { | |
342 | int i; | |
343 | ||
344 | if (!table) | |
345 | return; | |
346 | ||
347 | for (i = 0; i < table->n_buckets; i++) { | |
348 | struct sw_flow *flow; | |
349 | struct hlist_head *head = flex_array_get(table->buckets, i); | |
350 | struct hlist_node *node, *n; | |
351 | ||
352 | hlist_for_each_entry_safe(flow, node, n, head, hash_node) { | |
353 | hlist_del_init_rcu(&flow->hash_node); | |
354 | flow_free(flow); | |
355 | } | |
356 | } | |
357 | ||
358 | free_buckets(table->buckets); | |
359 | kfree(table); | |
360 | } | |
361 | ||
362 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) | |
363 | { | |
364 | struct flow_table *table = container_of(rcu, struct flow_table, rcu); | |
365 | ||
366 | flow_tbl_destroy(table); | |
367 | } | |
368 | ||
369 | void flow_tbl_deferred_destroy(struct flow_table *table) | |
370 | { | |
6455100f PS |
371 | if (!table) |
372 | return; | |
3544358a | 373 | |
6455100f | 374 | call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb); |
3544358a PS |
375 | } |
376 | ||
377 | struct sw_flow *flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last) | |
378 | { | |
379 | struct sw_flow *flow; | |
380 | struct hlist_head *head; | |
381 | struct hlist_node *n; | |
382 | int i; | |
383 | ||
384 | while (*bucket < table->n_buckets) { | |
385 | i = 0; | |
386 | head = flex_array_get(table->buckets, *bucket); | |
387 | hlist_for_each_entry_rcu(flow, n, head, hash_node) { | |
388 | if (i < *last) { | |
389 | i++; | |
390 | continue; | |
391 | } | |
392 | *last = i + 1; | |
393 | return flow; | |
394 | } | |
395 | (*bucket)++; | |
396 | *last = 0; | |
397 | } | |
398 | ||
399 | return NULL; | |
400 | } | |
401 | ||
402 | struct flow_table *flow_tbl_expand(struct flow_table *table) | |
403 | { | |
404 | struct flow_table *new_table; | |
405 | int n_buckets = table->n_buckets * 2; | |
406 | int i; | |
407 | ||
408 | new_table = flow_tbl_alloc(n_buckets); | |
409 | if (!new_table) | |
410 | return ERR_PTR(-ENOMEM); | |
411 | ||
412 | for (i = 0; i < table->n_buckets; i++) { | |
413 | struct sw_flow *flow; | |
414 | struct hlist_head *head; | |
415 | struct hlist_node *n, *pos; | |
416 | ||
417 | head = flex_array_get(table->buckets, i); | |
418 | ||
419 | hlist_for_each_entry_safe(flow, n, pos, head, hash_node) { | |
420 | hlist_del_init_rcu(&flow->hash_node); | |
421 | flow_tbl_insert(new_table, flow); | |
422 | } | |
423 | } | |
424 | ||
425 | return new_table; | |
426 | } | |
427 | ||
064af421 BP |
428 | /* RCU callback used by flow_deferred_free. */ |
429 | static void rcu_free_flow_callback(struct rcu_head *rcu) | |
430 | { | |
431 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
fb8c9347 JG |
432 | |
433 | flow->dead = true; | |
434 | flow_put(flow); | |
064af421 BP |
435 | } |
436 | ||
437 | /* Schedules 'flow' to be freed after the next RCU grace period. | |
438 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
439 | void flow_deferred_free(struct sw_flow *flow) | |
440 | { | |
441 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
442 | } | |
443 | ||
fb8c9347 JG |
444 | void flow_hold(struct sw_flow *flow) |
445 | { | |
446 | atomic_inc(&flow->refcnt); | |
447 | } | |
448 | ||
449 | void flow_put(struct sw_flow *flow) | |
450 | { | |
451 | if (unlikely(!flow)) | |
452 | return; | |
453 | ||
454 | if (atomic_dec_and_test(&flow->refcnt)) { | |
39872c70 | 455 | kfree((struct sf_flow_acts __force *)flow->sf_acts); |
fb8c9347 JG |
456 | kmem_cache_free(flow_cache, flow); |
457 | } | |
458 | } | |
459 | ||
064af421 BP |
460 | /* RCU callback used by flow_deferred_free_acts. */ |
461 | static void rcu_free_acts_callback(struct rcu_head *rcu) | |
462 | { | |
d295e8e9 | 463 | struct sw_flow_actions *sf_acts = container_of(rcu, |
064af421 BP |
464 | struct sw_flow_actions, rcu); |
465 | kfree(sf_acts); | |
466 | } | |
467 | ||
468 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. | |
469 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
470 | void flow_deferred_free_acts(struct sw_flow_actions *sf_acts) | |
471 | { | |
472 | call_rcu(&sf_acts->rcu, rcu_free_acts_callback); | |
473 | } | |
474 | ||
2db65bf7 | 475 | static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
064af421 | 476 | { |
50f06e16 BP |
477 | struct qtag_prefix { |
478 | __be16 eth_type; /* ETH_P_8021Q */ | |
479 | __be16 tci; | |
480 | }; | |
481 | struct qtag_prefix *qp; | |
482 | ||
2db65bf7 JG |
483 | if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) + |
484 | sizeof(__be16)))) | |
485 | return -ENOMEM; | |
50f06e16 BP |
486 | |
487 | qp = (struct qtag_prefix *) skb->data; | |
76abe283 | 488 | key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT); |
50f06e16 | 489 | __skb_pull(skb, sizeof(struct qtag_prefix)); |
2db65bf7 JG |
490 | |
491 | return 0; | |
50f06e16 BP |
492 | } |
493 | ||
494 | static __be16 parse_ethertype(struct sk_buff *skb) | |
064af421 | 495 | { |
50f06e16 BP |
496 | struct llc_snap_hdr { |
497 | u8 dsap; /* Always 0xAA */ | |
498 | u8 ssap; /* Always 0xAA */ | |
499 | u8 ctrl; | |
500 | u8 oui[3]; | |
8dda8c9b | 501 | __be16 ethertype; |
50f06e16 BP |
502 | }; |
503 | struct llc_snap_hdr *llc; | |
504 | __be16 proto; | |
505 | ||
506 | proto = *(__be16 *) skb->data; | |
507 | __skb_pull(skb, sizeof(__be16)); | |
508 | ||
36956a7d | 509 | if (ntohs(proto) >= 1536) |
50f06e16 BP |
510 | return proto; |
511 | ||
2db65bf7 | 512 | if (skb->len < sizeof(struct llc_snap_hdr)) |
36956a7d | 513 | return htons(ETH_P_802_2); |
50f06e16 | 514 | |
2db65bf7 JG |
515 | if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) |
516 | return htons(0); | |
517 | ||
50f06e16 BP |
518 | llc = (struct llc_snap_hdr *) skb->data; |
519 | if (llc->dsap != LLC_SAP_SNAP || | |
520 | llc->ssap != LLC_SAP_SNAP || | |
521 | (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) | |
36956a7d | 522 | return htons(ETH_P_802_2); |
50f06e16 BP |
523 | |
524 | __skb_pull(skb, sizeof(struct llc_snap_hdr)); | |
525 | return llc->ethertype; | |
064af421 BP |
526 | } |
527 | ||
685a51a5 | 528 | static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key, |
76abe283 | 529 | int *key_lenp, int nh_len) |
685a51a5 | 530 | { |
685a51a5 | 531 | struct icmp6hdr *icmp = icmp6_hdr(skb); |
76abe283 AE |
532 | int error = 0; |
533 | int key_len; | |
685a51a5 JP |
534 | |
535 | /* The ICMPv6 type and code fields use the 16-bit transport port | |
bfef4717 JG |
536 | * fields, so we need to store them in 16-bit network byte order. |
537 | */ | |
76abe283 AE |
538 | key->ipv6.tp.src = htons(icmp->icmp6_type); |
539 | key->ipv6.tp.dst = htons(icmp->icmp6_code); | |
540 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
685a51a5 | 541 | |
bfef4717 JG |
542 | if (icmp->icmp6_code == 0 && |
543 | (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION || | |
544 | icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
e977ba19 | 545 | int icmp_len = skb->len - skb_transport_offset(skb); |
685a51a5 JP |
546 | struct nd_msg *nd; |
547 | int offset; | |
548 | ||
76abe283 AE |
549 | key_len = SW_FLOW_KEY_OFFSET(ipv6.nd); |
550 | ||
685a51a5 | 551 | /* In order to process neighbor discovery options, we need the |
bfef4717 JG |
552 | * entire packet. |
553 | */ | |
554 | if (unlikely(icmp_len < sizeof(*nd))) | |
76abe283 AE |
555 | goto out; |
556 | if (unlikely(skb_linearize(skb))) { | |
557 | error = -ENOMEM; | |
558 | goto out; | |
559 | } | |
685a51a5 JP |
560 | |
561 | nd = (struct nd_msg *)skb_transport_header(skb); | |
76abe283 AE |
562 | ipv6_addr_copy(&key->ipv6.nd.target, &nd->target); |
563 | key_len = SW_FLOW_KEY_OFFSET(ipv6.nd); | |
685a51a5 JP |
564 | |
565 | icmp_len -= sizeof(*nd); | |
566 | offset = 0; | |
567 | while (icmp_len >= 8) { | |
6455100f PS |
568 | struct nd_opt_hdr *nd_opt = |
569 | (struct nd_opt_hdr *)(nd->opt + offset); | |
685a51a5 JP |
570 | int opt_len = nd_opt->nd_opt_len * 8; |
571 | ||
bfef4717 | 572 | if (unlikely(!opt_len || opt_len > icmp_len)) |
685a51a5 JP |
573 | goto invalid; |
574 | ||
bfef4717 JG |
575 | /* Store the link layer address if the appropriate |
576 | * option is provided. It is considered an error if | |
577 | * the same link layer option is specified twice. | |
578 | */ | |
685a51a5 | 579 | if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR |
bfef4717 | 580 | && opt_len == 8) { |
76abe283 | 581 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll))) |
685a51a5 | 582 | goto invalid; |
76abe283 | 583 | memcpy(key->ipv6.nd.sll, |
bfef4717 | 584 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 | 585 | } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR |
bfef4717 | 586 | && opt_len == 8) { |
76abe283 | 587 | if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll))) |
685a51a5 | 588 | goto invalid; |
76abe283 | 589 | memcpy(key->ipv6.nd.tll, |
bfef4717 | 590 | &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN); |
685a51a5 JP |
591 | } |
592 | ||
593 | icmp_len -= opt_len; | |
594 | offset += opt_len; | |
595 | } | |
596 | } | |
597 | ||
76abe283 | 598 | goto out; |
685a51a5 JP |
599 | |
600 | invalid: | |
76abe283 AE |
601 | memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target)); |
602 | memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll)); | |
603 | memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll)); | |
685a51a5 | 604 | |
76abe283 AE |
605 | out: |
606 | *key_lenp = key_len; | |
607 | return error; | |
685a51a5 JP |
608 | } |
609 | ||
a31e0e31 BP |
610 | /** |
611 | * flow_extract - extracts a flow key from an Ethernet frame. | |
612 | * @skb: sk_buff that contains the frame, with skb->data pointing to the | |
613 | * Ethernet header | |
614 | * @in_port: port number on which @skb was received. | |
615 | * @key: output flow key | |
76abe283 | 616 | * @key_lenp: length of output flow key |
a31e0e31 BP |
617 | * |
618 | * The caller must ensure that skb->len >= ETH_HLEN. | |
619 | * | |
4c1ad233 BP |
620 | * Returns 0 if successful, otherwise a negative errno value. |
621 | * | |
59a18f80 BP |
622 | * Initializes @skb header pointers as follows: |
623 | * | |
624 | * - skb->mac_header: the Ethernet header. | |
625 | * | |
626 | * - skb->network_header: just past the Ethernet header, or just past the | |
627 | * VLAN header, to the first byte of the Ethernet payload. | |
628 | * | |
d31f1109 JP |
629 | * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6 |
630 | * on output, then just past the IP header, if one is present and | |
631 | * of a correct length, otherwise the same as skb->network_header. | |
632 | * For other key->dl_type values it is left untouched. | |
a31e0e31 | 633 | */ |
36956a7d | 634 | int flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key, |
7257b535 | 635 | int *key_lenp) |
064af421 | 636 | { |
76abe283 AE |
637 | int error = 0; |
638 | int key_len = SW_FLOW_KEY_OFFSET(eth); | |
064af421 | 639 | struct ethhdr *eth; |
064af421 | 640 | |
84c17d98 | 641 | memset(key, 0, sizeof(*key)); |
abff858b PS |
642 | |
643 | key->phy.priority = skb->priority; | |
644 | key->phy.tun_id = OVS_CB(skb)->tun_id; | |
645 | key->phy.in_port = in_port; | |
064af421 | 646 | |
064af421 | 647 | skb_reset_mac_header(skb); |
064af421 | 648 | |
2db65bf7 JG |
649 | /* Link layer. We are guaranteed to have at least the 14 byte Ethernet |
650 | * header in the linear data area. | |
651 | */ | |
50f06e16 | 652 | eth = eth_hdr(skb); |
76abe283 AE |
653 | memcpy(key->eth.src, eth->h_source, ETH_ALEN); |
654 | memcpy(key->eth.dst, eth->h_dest, ETH_ALEN); | |
655 | ||
50f06e16 | 656 | __skb_pull(skb, 2 * ETH_ALEN); |
6ce39213 JG |
657 | |
658 | if (vlan_tx_tag_present(skb)) | |
76abe283 | 659 | key->eth.tci = htons(vlan_get_tci(skb)); |
6ce39213 | 660 | else if (eth->h_proto == htons(ETH_P_8021Q)) |
2db65bf7 JG |
661 | if (unlikely(parse_vlan(skb, key))) |
662 | return -ENOMEM; | |
6ce39213 | 663 | |
76abe283 AE |
664 | key->eth.type = parse_ethertype(skb); |
665 | if (unlikely(key->eth.type == htons(0))) | |
2db65bf7 JG |
666 | return -ENOMEM; |
667 | ||
50f06e16 | 668 | skb_reset_network_header(skb); |
2db65bf7 | 669 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
064af421 BP |
670 | |
671 | /* Network layer. */ | |
76abe283 | 672 | if (key->eth.type == htons(ETH_P_IP)) { |
4c1ad233 | 673 | struct iphdr *nh; |
7257b535 | 674 | __be16 offset; |
76abe283 AE |
675 | |
676 | key_len = SW_FLOW_KEY_OFFSET(ipv4.addr); | |
4c1ad233 BP |
677 | |
678 | error = check_iphdr(skb); | |
679 | if (unlikely(error)) { | |
680 | if (error == -EINVAL) { | |
681 | skb->transport_header = skb->network_header; | |
76abe283 | 682 | error = 0; |
4c1ad233 | 683 | } |
76abe283 | 684 | goto out; |
4c1ad233 BP |
685 | } |
686 | ||
687 | nh = ip_hdr(skb); | |
76abe283 AE |
688 | key->ipv4.addr.src = nh->saddr; |
689 | key->ipv4.addr.dst = nh->daddr; | |
7257b535 | 690 | |
28bad473 | 691 | key->ip.proto = nh->protocol; |
530180fd | 692 | key->ip.tos = nh->tos; |
a61680c6 | 693 | key->ip.ttl = nh->ttl; |
064af421 | 694 | |
7257b535 BP |
695 | offset = nh->frag_off & htons(IP_OFFSET); |
696 | if (offset) { | |
9e44d715 | 697 | key->ip.frag = OVS_FRAG_TYPE_LATER; |
7257b535 BP |
698 | goto out; |
699 | } | |
700 | if (nh->frag_off & htons(IP_MF) || | |
701 | skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
9e44d715 | 702 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
b7a31ec1 | 703 | |
7257b535 | 704 | /* Transport layer. */ |
28bad473 | 705 | if (key->ip.proto == IPPROTO_TCP) { |
5f4d087c | 706 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 707 | if (tcphdr_ok(skb)) { |
5f4d087c JG |
708 | struct tcphdr *tcp = tcp_hdr(skb); |
709 | key->ipv4.tp.src = tcp->source; | |
710 | key->ipv4.tp.dst = tcp->dest; | |
711 | } | |
28bad473 | 712 | } else if (key->ip.proto == IPPROTO_UDP) { |
5f4d087c | 713 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 714 | if (udphdr_ok(skb)) { |
5f4d087c JG |
715 | struct udphdr *udp = udp_hdr(skb); |
716 | key->ipv4.tp.src = udp->source; | |
717 | key->ipv4.tp.dst = udp->dest; | |
718 | } | |
28bad473 | 719 | } else if (key->ip.proto == IPPROTO_ICMP) { |
5f4d087c | 720 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 721 | if (icmphdr_ok(skb)) { |
5f4d087c JG |
722 | struct icmphdr *icmp = icmp_hdr(skb); |
723 | /* The ICMP type and code fields use the 16-bit | |
6455100f PS |
724 | * transport port fields, so we need to store |
725 | * them in 16-bit network byte order. */ | |
5f4d087c JG |
726 | key->ipv4.tp.src = htons(icmp->type); |
727 | key->ipv4.tp.dst = htons(icmp->code); | |
728 | } | |
729 | } | |
730 | ||
76abe283 | 731 | } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) { |
a26ef517 JP |
732 | struct arp_eth_header *arp; |
733 | ||
734 | arp = (struct arp_eth_header *)skb_network_header(skb); | |
735 | ||
f5e86186 | 736 | if (arp->ar_hrd == htons(ARPHRD_ETHER) |
de3f65ea JP |
737 | && arp->ar_pro == htons(ETH_P_IP) |
738 | && arp->ar_hln == ETH_ALEN | |
739 | && arp->ar_pln == 4) { | |
740 | ||
741 | /* We only match on the lower 8 bits of the opcode. */ | |
b7a31ec1 | 742 | if (ntohs(arp->ar_op) <= 0xff) |
28bad473 | 743 | key->ip.proto = ntohs(arp->ar_op); |
76abe283 | 744 | |
28bad473 JG |
745 | if (key->ip.proto == ARPOP_REQUEST |
746 | || key->ip.proto == ARPOP_REPLY) { | |
76abe283 AE |
747 | memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src)); |
748 | memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst)); | |
749 | memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN); | |
750 | memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN); | |
751 | key_len = SW_FLOW_KEY_OFFSET(ipv4.arp); | |
de3f65ea JP |
752 | } |
753 | } | |
76abe283 | 754 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
d31f1109 JP |
755 | int nh_len; /* IPv6 Header + Extensions */ |
756 | ||
76abe283 | 757 | nh_len = parse_ipv6hdr(skb, key, &key_len); |
d31f1109 | 758 | if (unlikely(nh_len < 0)) { |
76abe283 | 759 | if (nh_len == -EINVAL) |
d31f1109 | 760 | skb->transport_header = skb->network_header; |
76abe283 AE |
761 | else |
762 | error = nh_len; | |
763 | goto out; | |
d31f1109 JP |
764 | } |
765 | ||
9e44d715 | 766 | if (key->ip.frag == OVS_FRAG_TYPE_LATER) |
7257b535 BP |
767 | goto out; |
768 | if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
9e44d715 | 769 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
7257b535 | 770 | |
d31f1109 | 771 | /* Transport layer. */ |
28bad473 | 772 | if (key->ip.proto == NEXTHDR_TCP) { |
76abe283 | 773 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 JP |
774 | if (tcphdr_ok(skb)) { |
775 | struct tcphdr *tcp = tcp_hdr(skb); | |
76abe283 AE |
776 | key->ipv6.tp.src = tcp->source; |
777 | key->ipv6.tp.dst = tcp->dest; | |
d31f1109 | 778 | } |
28bad473 | 779 | } else if (key->ip.proto == NEXTHDR_UDP) { |
76abe283 | 780 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 JP |
781 | if (udphdr_ok(skb)) { |
782 | struct udphdr *udp = udp_hdr(skb); | |
76abe283 AE |
783 | key->ipv6.tp.src = udp->source; |
784 | key->ipv6.tp.dst = udp->dest; | |
d31f1109 | 785 | } |
28bad473 | 786 | } else if (key->ip.proto == NEXTHDR_ICMP) { |
76abe283 | 787 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 | 788 | if (icmp6hdr_ok(skb)) { |
76abe283 | 789 | error = parse_icmpv6(skb, key, &key_len, nh_len); |
685a51a5 | 790 | if (error < 0) |
76abe283 | 791 | goto out; |
d31f1109 JP |
792 | } |
793 | } | |
064af421 | 794 | } |
76abe283 AE |
795 | |
796 | out: | |
797 | *key_lenp = key_len; | |
798 | return error; | |
064af421 BP |
799 | } |
800 | ||
76abe283 | 801 | u32 flow_hash(const struct sw_flow_key *key, int key_len) |
8d5ebd83 | 802 | { |
6455100f | 803 | return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), hash_seed); |
8d5ebd83 JG |
804 | } |
805 | ||
6455100f | 806 | struct sw_flow *flow_tbl_lookup(struct flow_table *table, |
3544358a | 807 | struct sw_flow_key *key, int key_len) |
8d5ebd83 | 808 | { |
3544358a PS |
809 | struct sw_flow *flow; |
810 | struct hlist_node *n; | |
811 | struct hlist_head *head; | |
812 | u32 hash; | |
8d5ebd83 | 813 | |
3544358a PS |
814 | hash = flow_hash(key, key_len); |
815 | ||
816 | head = find_bucket(table, hash); | |
817 | hlist_for_each_entry_rcu(flow, n, head, hash_node) { | |
818 | ||
819 | if (flow->hash == hash && | |
820 | !memcmp(&flow->key, key, key_len)) { | |
821 | return flow; | |
822 | } | |
823 | } | |
824 | return NULL; | |
825 | } | |
826 | ||
827 | void flow_tbl_insert(struct flow_table *table, struct sw_flow *flow) | |
828 | { | |
829 | struct hlist_head *head; | |
830 | ||
831 | head = find_bucket(table, flow->hash); | |
832 | hlist_add_head_rcu(&flow->hash_node, head); | |
833 | table->count++; | |
834 | } | |
835 | ||
836 | void flow_tbl_remove(struct flow_table *table, struct sw_flow *flow) | |
837 | { | |
838 | if (!hlist_unhashed(&flow->hash_node)) { | |
839 | hlist_del_init_rcu(&flow->hash_node); | |
840 | table->count--; | |
841 | BUG_ON(table->count < 0); | |
842 | } | |
36956a7d BP |
843 | } |
844 | ||
df2c07f4 | 845 | /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */ |
4edb9ae9 | 846 | const u32 ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = { |
abff858b | 847 | [OVS_KEY_ATTR_PRIORITY] = 4, |
df2c07f4 JP |
848 | [OVS_KEY_ATTR_TUN_ID] = 8, |
849 | [OVS_KEY_ATTR_IN_PORT] = 4, | |
850 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), | |
851 | [OVS_KEY_ATTR_8021Q] = sizeof(struct ovs_key_8021q), | |
852 | [OVS_KEY_ATTR_ETHERTYPE] = 2, | |
853 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), | |
854 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), | |
855 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), | |
856 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), | |
857 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), | |
858 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), | |
859 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), | |
860 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), | |
80e5eed9 BP |
861 | }; |
862 | ||
36956a7d BP |
863 | /** |
864 | * flow_from_nlattrs - parses Netlink attributes into a flow key. | |
865 | * @swkey: receives the extracted flow key. | |
76abe283 | 866 | * @key_lenp: number of bytes used in @swkey. |
df2c07f4 | 867 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
d6569377 | 868 | * sequence. |
36956a7d BP |
869 | * |
870 | * This state machine accepts the following forms, with [] for optional | |
871 | * elements and | for alternatives: | |
872 | * | |
abff858b | 873 | * [priority] [tun_id] [in_port] ethernet [8021q] [ethertype \ |
685a51a5 | 874 | * [IPv4 [TCP|UDP|ICMP] | IPv6 [TCP|UDP|ICMPv6 [ND]] | ARP]] |
7257b535 BP |
875 | * |
876 | * except that IPv4 or IPv6 terminates the sequence if its @ipv4_frag or | |
877 | * @ipv6_frag member, respectively, equals %OVS_FRAG_TYPE_LATER. | |
36956a7d | 878 | */ |
76abe283 AE |
879 | int flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp, |
880 | const struct nlattr *attr) | |
36956a7d | 881 | { |
76abe283 | 882 | int error = 0; |
36956a7d BP |
883 | const struct nlattr *nla; |
884 | u16 prev_type; | |
885 | int rem; | |
76abe283 | 886 | int key_len; |
36956a7d BP |
887 | |
888 | memset(swkey, 0, sizeof(*swkey)); | |
abff858b | 889 | swkey->phy.in_port = USHRT_MAX; |
76abe283 AE |
890 | swkey->eth.type = htons(ETH_P_802_2); |
891 | key_len = SW_FLOW_KEY_OFFSET(eth); | |
36956a7d | 892 | |
df2c07f4 | 893 | prev_type = OVS_KEY_ATTR_UNSPEC; |
d6569377 | 894 | nla_for_each_nested(nla, attr, rem) { |
df2c07f4 JP |
895 | const struct ovs_key_ethernet *eth_key; |
896 | const struct ovs_key_8021q *q_key; | |
897 | const struct ovs_key_ipv4 *ipv4_key; | |
898 | const struct ovs_key_ipv6 *ipv6_key; | |
899 | const struct ovs_key_tcp *tcp_key; | |
900 | const struct ovs_key_udp *udp_key; | |
901 | const struct ovs_key_icmp *icmp_key; | |
902 | const struct ovs_key_icmpv6 *icmpv6_key; | |
903 | const struct ovs_key_arp *arp_key; | |
904 | const struct ovs_key_nd *nd_key; | |
36956a7d | 905 | |
6455100f | 906 | int type = nla_type(nla); |
36956a7d | 907 | |
6455100f PS |
908 | if (type > OVS_KEY_ATTR_MAX || |
909 | nla_len(nla) != ovs_key_lens[type]) | |
4edb9ae9 | 910 | goto invalid; |
36956a7d BP |
911 | |
912 | #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE)) | |
913 | switch (TRANSITION(prev_type, type)) { | |
abff858b PS |
914 | case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_PRIORITY): |
915 | swkey->phy.priority = nla_get_u32(nla); | |
916 | break; | |
917 | ||
df2c07f4 | 918 | case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_TUN_ID): |
abff858b PS |
919 | case TRANSITION(OVS_KEY_ATTR_PRIORITY, OVS_KEY_ATTR_TUN_ID): |
920 | swkey->phy.tun_id = nla_get_be64(nla); | |
36956a7d BP |
921 | break; |
922 | ||
df2c07f4 | 923 | case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_IN_PORT): |
abff858b | 924 | case TRANSITION(OVS_KEY_ATTR_PRIORITY, OVS_KEY_ATTR_IN_PORT): |
df2c07f4 | 925 | case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_IN_PORT): |
36956a7d | 926 | if (nla_get_u32(nla) >= DP_MAX_PORTS) |
76abe283 | 927 | goto invalid; |
abff858b | 928 | swkey->phy.in_port = nla_get_u32(nla); |
36956a7d BP |
929 | break; |
930 | ||
18886b60 | 931 | case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_ETHERNET): |
abff858b | 932 | case TRANSITION(OVS_KEY_ATTR_PRIORITY, OVS_KEY_ATTR_ETHERNET): |
18886b60 | 933 | case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_ETHERNET): |
df2c07f4 | 934 | case TRANSITION(OVS_KEY_ATTR_IN_PORT, OVS_KEY_ATTR_ETHERNET): |
36956a7d | 935 | eth_key = nla_data(nla); |
76abe283 AE |
936 | memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN); |
937 | memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN); | |
36956a7d BP |
938 | break; |
939 | ||
df2c07f4 | 940 | case TRANSITION(OVS_KEY_ATTR_ETHERNET, OVS_KEY_ATTR_8021Q): |
36956a7d BP |
941 | q_key = nla_data(nla); |
942 | /* Only standard 0x8100 VLANs currently supported. */ | |
943 | if (q_key->q_tpid != htons(ETH_P_8021Q)) | |
76abe283 | 944 | goto invalid; |
36956a7d | 945 | if (q_key->q_tci & htons(VLAN_TAG_PRESENT)) |
76abe283 AE |
946 | goto invalid; |
947 | swkey->eth.tci = q_key->q_tci | htons(VLAN_TAG_PRESENT); | |
36956a7d BP |
948 | break; |
949 | ||
df2c07f4 JP |
950 | case TRANSITION(OVS_KEY_ATTR_8021Q, OVS_KEY_ATTR_ETHERTYPE): |
951 | case TRANSITION(OVS_KEY_ATTR_ETHERNET, OVS_KEY_ATTR_ETHERTYPE): | |
76abe283 AE |
952 | swkey->eth.type = nla_get_be16(nla); |
953 | if (ntohs(swkey->eth.type) < 1536) | |
954 | goto invalid; | |
36956a7d BP |
955 | break; |
956 | ||
df2c07f4 | 957 | case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_IPV4): |
76abe283 AE |
958 | key_len = SW_FLOW_KEY_OFFSET(ipv4.addr); |
959 | if (swkey->eth.type != htons(ETH_P_IP)) | |
960 | goto invalid; | |
36956a7d | 961 | ipv4_key = nla_data(nla); |
9e44d715 | 962 | if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) |
7257b535 | 963 | goto invalid; |
9e44d715 JP |
964 | swkey->ip.proto = ipv4_key->ipv4_proto; |
965 | swkey->ip.tos = ipv4_key->ipv4_tos; | |
a61680c6 | 966 | swkey->ip.ttl = ipv4_key->ipv4_ttl; |
9e44d715 | 967 | swkey->ip.frag = ipv4_key->ipv4_frag; |
76abe283 AE |
968 | swkey->ipv4.addr.src = ipv4_key->ipv4_src; |
969 | swkey->ipv4.addr.dst = ipv4_key->ipv4_dst; | |
36956a7d BP |
970 | break; |
971 | ||
df2c07f4 | 972 | case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_IPV6): |
fa8223b7 | 973 | key_len = SW_FLOW_KEY_OFFSET(ipv6.label); |
76abe283 AE |
974 | if (swkey->eth.type != htons(ETH_P_IPV6)) |
975 | goto invalid; | |
d31f1109 | 976 | ipv6_key = nla_data(nla); |
9e44d715 JP |
977 | if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) |
978 | goto invalid; | |
fa8223b7 | 979 | swkey->ipv6.label = ipv6_key->ipv6_label; |
28bad473 | 980 | swkey->ip.proto = ipv6_key->ipv6_proto; |
60258dcb | 981 | swkey->ip.tos = ipv6_key->ipv6_tclass; |
a61680c6 | 982 | swkey->ip.ttl = ipv6_key->ipv6_hlimit; |
9e44d715 | 983 | swkey->ip.frag = ipv6_key->ipv6_frag; |
76abe283 AE |
984 | memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src, |
985 | sizeof(swkey->ipv6.addr.src)); | |
986 | memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst, | |
987 | sizeof(swkey->ipv6.addr.dst)); | |
d31f1109 JP |
988 | break; |
989 | ||
df2c07f4 | 990 | case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_TCP): |
76abe283 | 991 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
28bad473 | 992 | if (swkey->ip.proto != IPPROTO_TCP) |
76abe283 AE |
993 | goto invalid; |
994 | tcp_key = nla_data(nla); | |
995 | swkey->ipv4.tp.src = tcp_key->tcp_src; | |
996 | swkey->ipv4.tp.dst = tcp_key->tcp_dst; | |
997 | break; | |
998 | ||
df2c07f4 | 999 | case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_TCP): |
76abe283 | 1000 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
28bad473 | 1001 | if (swkey->ip.proto != IPPROTO_TCP) |
76abe283 | 1002 | goto invalid; |
36956a7d | 1003 | tcp_key = nla_data(nla); |
76abe283 AE |
1004 | swkey->ipv6.tp.src = tcp_key->tcp_src; |
1005 | swkey->ipv6.tp.dst = tcp_key->tcp_dst; | |
36956a7d BP |
1006 | break; |
1007 | ||
df2c07f4 | 1008 | case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_UDP): |
76abe283 | 1009 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
28bad473 | 1010 | if (swkey->ip.proto != IPPROTO_UDP) |
76abe283 AE |
1011 | goto invalid; |
1012 | udp_key = nla_data(nla); | |
1013 | swkey->ipv4.tp.src = udp_key->udp_src; | |
1014 | swkey->ipv4.tp.dst = udp_key->udp_dst; | |
1015 | break; | |
1016 | ||
df2c07f4 | 1017 | case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_UDP): |
76abe283 | 1018 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
28bad473 | 1019 | if (swkey->ip.proto != IPPROTO_UDP) |
76abe283 | 1020 | goto invalid; |
36956a7d | 1021 | udp_key = nla_data(nla); |
76abe283 AE |
1022 | swkey->ipv6.tp.src = udp_key->udp_src; |
1023 | swkey->ipv6.tp.dst = udp_key->udp_dst; | |
36956a7d BP |
1024 | break; |
1025 | ||
df2c07f4 | 1026 | case TRANSITION(OVS_KEY_ATTR_IPV4, OVS_KEY_ATTR_ICMP): |
76abe283 | 1027 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
28bad473 | 1028 | if (swkey->ip.proto != IPPROTO_ICMP) |
76abe283 | 1029 | goto invalid; |
36956a7d | 1030 | icmp_key = nla_data(nla); |
76abe283 AE |
1031 | swkey->ipv4.tp.src = htons(icmp_key->icmp_type); |
1032 | swkey->ipv4.tp.dst = htons(icmp_key->icmp_code); | |
36956a7d BP |
1033 | break; |
1034 | ||
df2c07f4 | 1035 | case TRANSITION(OVS_KEY_ATTR_IPV6, OVS_KEY_ATTR_ICMPV6): |
76abe283 | 1036 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
28bad473 | 1037 | if (swkey->ip.proto != IPPROTO_ICMPV6) |
76abe283 | 1038 | goto invalid; |
d31f1109 | 1039 | icmpv6_key = nla_data(nla); |
76abe283 AE |
1040 | swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type); |
1041 | swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code); | |
d31f1109 JP |
1042 | break; |
1043 | ||
df2c07f4 | 1044 | case TRANSITION(OVS_KEY_ATTR_ETHERTYPE, OVS_KEY_ATTR_ARP): |
76abe283 AE |
1045 | key_len = SW_FLOW_KEY_OFFSET(ipv4.arp); |
1046 | if (swkey->eth.type != htons(ETH_P_ARP)) | |
1047 | goto invalid; | |
36956a7d | 1048 | arp_key = nla_data(nla); |
76abe283 AE |
1049 | swkey->ipv4.addr.src = arp_key->arp_sip; |
1050 | swkey->ipv4.addr.dst = arp_key->arp_tip; | |
36956a7d | 1051 | if (arp_key->arp_op & htons(0xff00)) |
76abe283 | 1052 | goto invalid; |
28bad473 | 1053 | swkey->ip.proto = ntohs(arp_key->arp_op); |
76abe283 AE |
1054 | memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN); |
1055 | memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN); | |
36956a7d BP |
1056 | break; |
1057 | ||
df2c07f4 | 1058 | case TRANSITION(OVS_KEY_ATTR_ICMPV6, OVS_KEY_ATTR_ND): |
76abe283 AE |
1059 | key_len = SW_FLOW_KEY_OFFSET(ipv6.nd); |
1060 | if (swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_SOLICITATION) | |
1061 | && swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) | |
1062 | goto invalid; | |
685a51a5 | 1063 | nd_key = nla_data(nla); |
76abe283 AE |
1064 | memcpy(&swkey->ipv6.nd.target, nd_key->nd_target, |
1065 | sizeof(swkey->ipv6.nd.target)); | |
1066 | memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN); | |
1067 | memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN); | |
685a51a5 JP |
1068 | break; |
1069 | ||
36956a7d | 1070 | default: |
76abe283 | 1071 | goto invalid; |
36956a7d BP |
1072 | } |
1073 | ||
1074 | prev_type = type; | |
1075 | } | |
1076 | if (rem) | |
76abe283 | 1077 | goto invalid; |
36956a7d BP |
1078 | |
1079 | switch (prev_type) { | |
df2c07f4 | 1080 | case OVS_KEY_ATTR_UNSPEC: |
76abe283 | 1081 | goto invalid; |
36956a7d | 1082 | |
abff858b | 1083 | case OVS_KEY_ATTR_PRIORITY: |
df2c07f4 JP |
1084 | case OVS_KEY_ATTR_TUN_ID: |
1085 | case OVS_KEY_ATTR_IN_PORT: | |
76abe283 | 1086 | goto invalid; |
36956a7d | 1087 | |
df2c07f4 JP |
1088 | case OVS_KEY_ATTR_ETHERNET: |
1089 | case OVS_KEY_ATTR_8021Q: | |
76abe283 | 1090 | goto ok; |
36956a7d | 1091 | |
df2c07f4 | 1092 | case OVS_KEY_ATTR_ETHERTYPE: |
76abe283 | 1093 | if (swkey->eth.type == htons(ETH_P_IP) || |
515c382d | 1094 | swkey->eth.type == htons(ETH_P_IPV6) || |
76abe283 AE |
1095 | swkey->eth.type == htons(ETH_P_ARP)) |
1096 | goto invalid; | |
1097 | goto ok; | |
36956a7d | 1098 | |
df2c07f4 | 1099 | case OVS_KEY_ATTR_IPV4: |
9e44d715 | 1100 | if (swkey->ip.frag == OVS_FRAG_TYPE_LATER) |
7257b535 | 1101 | goto ok; |
28bad473 JG |
1102 | if (swkey->ip.proto == IPPROTO_TCP || |
1103 | swkey->ip.proto == IPPROTO_UDP || | |
1104 | swkey->ip.proto == IPPROTO_ICMP) | |
76abe283 AE |
1105 | goto invalid; |
1106 | goto ok; | |
36956a7d | 1107 | |
df2c07f4 | 1108 | case OVS_KEY_ATTR_IPV6: |
9e44d715 | 1109 | if (swkey->ip.frag == OVS_FRAG_TYPE_LATER) |
7257b535 | 1110 | goto ok; |
28bad473 JG |
1111 | if (swkey->ip.proto == IPPROTO_TCP || |
1112 | swkey->ip.proto == IPPROTO_UDP || | |
1113 | swkey->ip.proto == IPPROTO_ICMPV6) | |
76abe283 AE |
1114 | goto invalid; |
1115 | goto ok; | |
d31f1109 | 1116 | |
df2c07f4 | 1117 | case OVS_KEY_ATTR_ICMPV6: |
76abe283 | 1118 | if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) || |
7257b535 | 1119 | swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT) || |
9e44d715 | 1120 | swkey->ip.frag == OVS_FRAG_TYPE_LATER) |
76abe283 AE |
1121 | goto invalid; |
1122 | goto ok; | |
685a51a5 | 1123 | |
df2c07f4 JP |
1124 | case OVS_KEY_ATTR_TCP: |
1125 | case OVS_KEY_ATTR_UDP: | |
1126 | case OVS_KEY_ATTR_ICMP: | |
df2c07f4 | 1127 | case OVS_KEY_ATTR_ND: |
9e44d715 | 1128 | if (swkey->ip.frag == OVS_FRAG_TYPE_LATER) |
7257b535 BP |
1129 | goto invalid; |
1130 | goto ok; | |
1131 | ||
1132 | case OVS_KEY_ATTR_ARP: | |
76abe283 AE |
1133 | goto ok; |
1134 | ||
1135 | default: | |
1136 | WARN_ON_ONCE(1); | |
36956a7d BP |
1137 | } |
1138 | ||
76abe283 AE |
1139 | invalid: |
1140 | error = -EINVAL; | |
1141 | ||
1142 | ok: | |
76abe283 AE |
1143 | *key_lenp = key_len; |
1144 | return error; | |
36956a7d BP |
1145 | } |
1146 | ||
80e5eed9 BP |
1147 | /** |
1148 | * flow_metadata_from_nlattrs - parses Netlink attributes into a flow key. | |
1149 | * @in_port: receives the extracted input port. | |
1150 | * @tun_id: receives the extracted tunnel ID. | |
df2c07f4 | 1151 | * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
80e5eed9 BP |
1152 | * sequence. |
1153 | * | |
1154 | * This parses a series of Netlink attributes that form a flow key, which must | |
1155 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to | |
1156 | * get the metadata, that is, the parts of the flow key that cannot be | |
1157 | * extracted from the packet itself. | |
1158 | */ | |
abff858b | 1159 | int flow_metadata_from_nlattrs(u32 *priority, u16 *in_port, __be64 *tun_id, |
80e5eed9 BP |
1160 | const struct nlattr *attr) |
1161 | { | |
1162 | const struct nlattr *nla; | |
1163 | u16 prev_type; | |
1164 | int rem; | |
1165 | ||
18886b60 | 1166 | *in_port = USHRT_MAX; |
80e5eed9 | 1167 | *tun_id = 0; |
abff858b | 1168 | *priority = 0; |
80e5eed9 | 1169 | |
df2c07f4 | 1170 | prev_type = OVS_KEY_ATTR_UNSPEC; |
80e5eed9 | 1171 | nla_for_each_nested(nla, attr, rem) { |
6455100f | 1172 | int type = nla_type(nla); |
80e5eed9 | 1173 | |
4edb9ae9 PS |
1174 | if (type > OVS_KEY_ATTR_MAX || nla_len(nla) != ovs_key_lens[type]) |
1175 | return -EINVAL; | |
80e5eed9 BP |
1176 | |
1177 | switch (TRANSITION(prev_type, type)) { | |
abff858b PS |
1178 | case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_PRIORITY): |
1179 | *priority = nla_get_u32(nla); | |
1180 | break; | |
1181 | ||
df2c07f4 | 1182 | case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_TUN_ID): |
abff858b | 1183 | case TRANSITION(OVS_KEY_ATTR_PRIORITY, OVS_KEY_ATTR_TUN_ID): |
80e5eed9 BP |
1184 | *tun_id = nla_get_be64(nla); |
1185 | break; | |
1186 | ||
df2c07f4 | 1187 | case TRANSITION(OVS_KEY_ATTR_UNSPEC, OVS_KEY_ATTR_IN_PORT): |
abff858b | 1188 | case TRANSITION(OVS_KEY_ATTR_PRIORITY, OVS_KEY_ATTR_IN_PORT): |
df2c07f4 | 1189 | case TRANSITION(OVS_KEY_ATTR_TUN_ID, OVS_KEY_ATTR_IN_PORT): |
80e5eed9 BP |
1190 | if (nla_get_u32(nla) >= DP_MAX_PORTS) |
1191 | return -EINVAL; | |
1192 | *in_port = nla_get_u32(nla); | |
1193 | break; | |
1194 | ||
1195 | default: | |
18886b60 | 1196 | return 0; |
80e5eed9 BP |
1197 | } |
1198 | ||
1199 | prev_type = type; | |
1200 | } | |
1201 | if (rem) | |
1202 | return -EINVAL; | |
80e5eed9 BP |
1203 | return 0; |
1204 | } | |
1205 | ||
d6569377 | 1206 | int flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb) |
36956a7d | 1207 | { |
df2c07f4 | 1208 | struct ovs_key_ethernet *eth_key; |
d6569377 | 1209 | struct nlattr *nla; |
36956a7d | 1210 | |
abff858b PS |
1211 | if (swkey->phy.priority) |
1212 | NLA_PUT_U32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority); | |
18c43631 | 1213 | |
abff858b PS |
1214 | if (swkey->phy.tun_id != cpu_to_be64(0)) |
1215 | NLA_PUT_BE64(skb, OVS_KEY_ATTR_TUN_ID, swkey->phy.tun_id); | |
36956a7d | 1216 | |
abff858b PS |
1217 | if (swkey->phy.in_port != USHRT_MAX) |
1218 | NLA_PUT_U32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port); | |
36956a7d | 1219 | |
df2c07f4 | 1220 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); |
d6569377 BP |
1221 | if (!nla) |
1222 | goto nla_put_failure; | |
1223 | eth_key = nla_data(nla); | |
76abe283 AE |
1224 | memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN); |
1225 | memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN); | |
36956a7d | 1226 | |
76abe283 | 1227 | if (swkey->eth.tci != htons(0)) { |
df2c07f4 | 1228 | struct ovs_key_8021q q_key; |
36956a7d | 1229 | |
d6569377 | 1230 | q_key.q_tpid = htons(ETH_P_8021Q); |
76abe283 | 1231 | q_key.q_tci = swkey->eth.tci & ~htons(VLAN_TAG_PRESENT); |
df2c07f4 | 1232 | NLA_PUT(skb, OVS_KEY_ATTR_8021Q, sizeof(q_key), &q_key); |
36956a7d BP |
1233 | } |
1234 | ||
76abe283 | 1235 | if (swkey->eth.type == htons(ETH_P_802_2)) |
d6569377 | 1236 | return 0; |
36956a7d | 1237 | |
df2c07f4 | 1238 | NLA_PUT_BE16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type); |
36956a7d | 1239 | |
76abe283 | 1240 | if (swkey->eth.type == htons(ETH_P_IP)) { |
df2c07f4 | 1241 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 1242 | |
df2c07f4 | 1243 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); |
d6569377 BP |
1244 | if (!nla) |
1245 | goto nla_put_failure; | |
1246 | ipv4_key = nla_data(nla); | |
76abe283 AE |
1247 | ipv4_key->ipv4_src = swkey->ipv4.addr.src; |
1248 | ipv4_key->ipv4_dst = swkey->ipv4.addr.dst; | |
28bad473 | 1249 | ipv4_key->ipv4_proto = swkey->ip.proto; |
530180fd | 1250 | ipv4_key->ipv4_tos = swkey->ip.tos; |
a61680c6 | 1251 | ipv4_key->ipv4_ttl = swkey->ip.ttl; |
9e44d715 | 1252 | ipv4_key->ipv4_frag = swkey->ip.frag; |
76abe283 | 1253 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
df2c07f4 | 1254 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 1255 | |
df2c07f4 | 1256 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); |
d31f1109 JP |
1257 | if (!nla) |
1258 | goto nla_put_failure; | |
1259 | ipv6_key = nla_data(nla); | |
76abe283 | 1260 | memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src, |
d31f1109 | 1261 | sizeof(ipv6_key->ipv6_src)); |
76abe283 | 1262 | memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst, |
d31f1109 | 1263 | sizeof(ipv6_key->ipv6_dst)); |
fa8223b7 | 1264 | ipv6_key->ipv6_label = swkey->ipv6.label; |
28bad473 | 1265 | ipv6_key->ipv6_proto = swkey->ip.proto; |
60258dcb | 1266 | ipv6_key->ipv6_tclass = swkey->ip.tos; |
a61680c6 | 1267 | ipv6_key->ipv6_hlimit = swkey->ip.ttl; |
9e44d715 | 1268 | ipv6_key->ipv6_frag = swkey->ip.frag; |
76abe283 | 1269 | } else if (swkey->eth.type == htons(ETH_P_ARP)) { |
df2c07f4 | 1270 | struct ovs_key_arp *arp_key; |
d31f1109 | 1271 | |
df2c07f4 | 1272 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); |
d31f1109 JP |
1273 | if (!nla) |
1274 | goto nla_put_failure; | |
1275 | arp_key = nla_data(nla); | |
df2c07f4 | 1276 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); |
76abe283 AE |
1277 | arp_key->arp_sip = swkey->ipv4.addr.src; |
1278 | arp_key->arp_tip = swkey->ipv4.addr.dst; | |
28bad473 | 1279 | arp_key->arp_op = htons(swkey->ip.proto); |
76abe283 AE |
1280 | memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN); |
1281 | memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN); | |
d31f1109 JP |
1282 | } |
1283 | ||
7257b535 BP |
1284 | if ((swkey->eth.type == htons(ETH_P_IP) || |
1285 | swkey->eth.type == htons(ETH_P_IPV6)) && | |
9e44d715 | 1286 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { |
36956a7d | 1287 | |
28bad473 | 1288 | if (swkey->ip.proto == IPPROTO_TCP) { |
df2c07f4 | 1289 | struct ovs_key_tcp *tcp_key; |
36956a7d | 1290 | |
df2c07f4 | 1291 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); |
d6569377 BP |
1292 | if (!nla) |
1293 | goto nla_put_failure; | |
1294 | tcp_key = nla_data(nla); | |
76abe283 AE |
1295 | if (swkey->eth.type == htons(ETH_P_IP)) { |
1296 | tcp_key->tcp_src = swkey->ipv4.tp.src; | |
1297 | tcp_key->tcp_dst = swkey->ipv4.tp.dst; | |
1298 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
1299 | tcp_key->tcp_src = swkey->ipv6.tp.src; | |
1300 | tcp_key->tcp_dst = swkey->ipv6.tp.dst; | |
1301 | } | |
28bad473 | 1302 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
df2c07f4 | 1303 | struct ovs_key_udp *udp_key; |
36956a7d | 1304 | |
df2c07f4 | 1305 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); |
d6569377 BP |
1306 | if (!nla) |
1307 | goto nla_put_failure; | |
1308 | udp_key = nla_data(nla); | |
76abe283 AE |
1309 | if (swkey->eth.type == htons(ETH_P_IP)) { |
1310 | udp_key->udp_src = swkey->ipv4.tp.src; | |
1311 | udp_key->udp_dst = swkey->ipv4.tp.dst; | |
1312 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
1313 | udp_key->udp_src = swkey->ipv6.tp.src; | |
1314 | udp_key->udp_dst = swkey->ipv6.tp.dst; | |
1315 | } | |
1316 | } else if (swkey->eth.type == htons(ETH_P_IP) && | |
28bad473 | 1317 | swkey->ip.proto == IPPROTO_ICMP) { |
df2c07f4 | 1318 | struct ovs_key_icmp *icmp_key; |
36956a7d | 1319 | |
df2c07f4 | 1320 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); |
d6569377 BP |
1321 | if (!nla) |
1322 | goto nla_put_failure; | |
1323 | icmp_key = nla_data(nla); | |
76abe283 AE |
1324 | icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src); |
1325 | icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst); | |
1326 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && | |
28bad473 | 1327 | swkey->ip.proto == IPPROTO_ICMPV6) { |
df2c07f4 | 1328 | struct ovs_key_icmpv6 *icmpv6_key; |
36956a7d | 1329 | |
df2c07f4 | 1330 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, |
bfef4717 | 1331 | sizeof(*icmpv6_key)); |
d31f1109 JP |
1332 | if (!nla) |
1333 | goto nla_put_failure; | |
1334 | icmpv6_key = nla_data(nla); | |
76abe283 AE |
1335 | icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src); |
1336 | icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst); | |
685a51a5 | 1337 | |
bfef4717 JG |
1338 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || |
1339 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { | |
df2c07f4 | 1340 | struct ovs_key_nd *nd_key; |
685a51a5 | 1341 | |
df2c07f4 | 1342 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); |
685a51a5 JP |
1343 | if (!nla) |
1344 | goto nla_put_failure; | |
1345 | nd_key = nla_data(nla); | |
76abe283 | 1346 | memcpy(nd_key->nd_target, &swkey->ipv6.nd.target, |
685a51a5 | 1347 | sizeof(nd_key->nd_target)); |
76abe283 AE |
1348 | memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN); |
1349 | memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN); | |
685a51a5 | 1350 | } |
d31f1109 | 1351 | } |
36956a7d BP |
1352 | } |
1353 | ||
d6569377 | 1354 | return 0; |
36956a7d | 1355 | |
d6569377 BP |
1356 | nla_put_failure: |
1357 | return -EMSGSIZE; | |
8d5ebd83 JG |
1358 | } |
1359 | ||
064af421 BP |
1360 | /* Initializes the flow module. |
1361 | * Returns zero if successful or a negative error code. */ | |
1362 | int flow_init(void) | |
1363 | { | |
1364 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, | |
1365 | 0, NULL); | |
1366 | if (flow_cache == NULL) | |
1367 | return -ENOMEM; | |
1368 | ||
84c17d98 | 1369 | get_random_bytes(&hash_seed, sizeof(hash_seed)); |
8d5ebd83 | 1370 | |
064af421 BP |
1371 | return 0; |
1372 | } | |
1373 | ||
1374 | /* Uninitializes the flow module. */ | |
1375 | void flow_exit(void) | |
1376 | { | |
1377 | kmem_cache_destroy(flow_cache); | |
1378 | } |