]>
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 | ||
850b6b3b | 203 | struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions) |
064af421 | 204 | { |
37a1300c | 205 | int actions_len = nla_len(actions); |
064af421 BP |
206 | struct sw_flow_actions *sfa; |
207 | ||
95b1d73a | 208 | if (actions_len > MAX_ACTIONS_BUFSIZE) |
064af421 BP |
209 | return ERR_PTR(-EINVAL); |
210 | ||
84c17d98 | 211 | sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL); |
064af421 BP |
212 | if (!sfa) |
213 | return ERR_PTR(-ENOMEM); | |
214 | ||
cdee00fd | 215 | sfa->actions_len = actions_len; |
37a1300c | 216 | memcpy(sfa->actions, nla_data(actions), actions_len); |
064af421 BP |
217 | return sfa; |
218 | } | |
219 | ||
850b6b3b | 220 | struct sw_flow *ovs_flow_alloc(void) |
560e8022 JG |
221 | { |
222 | struct sw_flow *flow; | |
223 | ||
224 | flow = kmem_cache_alloc(flow_cache, GFP_KERNEL); | |
225 | if (!flow) | |
226 | return ERR_PTR(-ENOMEM); | |
227 | ||
228 | spin_lock_init(&flow->lock); | |
6f04e94a | 229 | flow->sf_acts = NULL; |
064af421 | 230 | |
560e8022 JG |
231 | return flow; |
232 | } | |
233 | ||
be2ba156 | 234 | static struct hlist_head *find_bucket(struct flow_table *table, u32 hash) |
8d5ebd83 | 235 | { |
acd051f1 | 236 | hash = jhash_1word(hash, table->hash_seed); |
3544358a PS |
237 | return flex_array_get(table->buckets, |
238 | (hash & (table->n_buckets - 1))); | |
239 | } | |
240 | ||
be2ba156 | 241 | static struct flex_array *alloc_buckets(unsigned int n_buckets) |
3544358a | 242 | { |
be2ba156 | 243 | struct flex_array *buckets; |
3544358a PS |
244 | int i, err; |
245 | ||
246 | buckets = flex_array_alloc(sizeof(struct hlist_head *), | |
247 | n_buckets, GFP_KERNEL); | |
248 | if (!buckets) | |
249 | return NULL; | |
250 | ||
251 | err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL); | |
252 | if (err) { | |
253 | flex_array_free(buckets); | |
254 | return NULL; | |
255 | } | |
256 | ||
257 | for (i = 0; i < n_buckets; i++) | |
258 | INIT_HLIST_HEAD((struct hlist_head *) | |
259 | flex_array_get(buckets, i)); | |
260 | ||
261 | return buckets; | |
262 | } | |
263 | ||
6455100f | 264 | static void free_buckets(struct flex_array *buckets) |
3544358a PS |
265 | { |
266 | flex_array_free(buckets); | |
267 | } | |
268 | ||
850b6b3b | 269 | struct flow_table *ovs_flow_tbl_alloc(int new_size) |
3544358a PS |
270 | { |
271 | struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL); | |
272 | ||
273 | if (!table) | |
274 | return NULL; | |
275 | ||
276 | table->buckets = alloc_buckets(new_size); | |
277 | ||
278 | if (!table->buckets) { | |
279 | kfree(table); | |
280 | return NULL; | |
281 | } | |
282 | table->n_buckets = new_size; | |
283 | table->count = 0; | |
acd051f1 PS |
284 | table->node_ver = 0; |
285 | table->keep_flows = false; | |
286 | get_random_bytes(&table->hash_seed, sizeof(u32)); | |
3544358a PS |
287 | |
288 | return table; | |
289 | } | |
fb8c9347 | 290 | |
850b6b3b | 291 | void ovs_flow_tbl_destroy(struct flow_table *table) |
3544358a PS |
292 | { |
293 | int i; | |
294 | ||
295 | if (!table) | |
296 | return; | |
297 | ||
acd051f1 PS |
298 | if (table->keep_flows) |
299 | goto skip_flows; | |
300 | ||
3544358a PS |
301 | for (i = 0; i < table->n_buckets; i++) { |
302 | struct sw_flow *flow; | |
303 | struct hlist_head *head = flex_array_get(table->buckets, i); | |
304 | struct hlist_node *node, *n; | |
acd051f1 | 305 | int ver = table->node_ver; |
3544358a | 306 | |
acd051f1 PS |
307 | hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) { |
308 | hlist_del_rcu(&flow->hash_node[ver]); | |
9321954a | 309 | ovs_flow_free(flow); |
3544358a PS |
310 | } |
311 | } | |
312 | ||
acd051f1 | 313 | skip_flows: |
3544358a PS |
314 | free_buckets(table->buckets); |
315 | kfree(table); | |
316 | } | |
317 | ||
318 | static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu) | |
319 | { | |
320 | struct flow_table *table = container_of(rcu, struct flow_table, rcu); | |
321 | ||
850b6b3b | 322 | ovs_flow_tbl_destroy(table); |
3544358a PS |
323 | } |
324 | ||
850b6b3b | 325 | void ovs_flow_tbl_deferred_destroy(struct flow_table *table) |
3544358a | 326 | { |
6455100f PS |
327 | if (!table) |
328 | return; | |
3544358a | 329 | |
6455100f | 330 | call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb); |
3544358a PS |
331 | } |
332 | ||
850b6b3b | 333 | struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last) |
3544358a PS |
334 | { |
335 | struct sw_flow *flow; | |
336 | struct hlist_head *head; | |
337 | struct hlist_node *n; | |
acd051f1 | 338 | int ver; |
3544358a PS |
339 | int i; |
340 | ||
acd051f1 | 341 | ver = table->node_ver; |
3544358a PS |
342 | while (*bucket < table->n_buckets) { |
343 | i = 0; | |
344 | head = flex_array_get(table->buckets, *bucket); | |
acd051f1 | 345 | hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) { |
3544358a PS |
346 | if (i < *last) { |
347 | i++; | |
348 | continue; | |
349 | } | |
350 | *last = i + 1; | |
351 | return flow; | |
352 | } | |
353 | (*bucket)++; | |
354 | *last = 0; | |
355 | } | |
356 | ||
357 | return NULL; | |
358 | } | |
359 | ||
13e24889 PS |
360 | static void __flow_tbl_insert(struct flow_table *table, struct sw_flow *flow) |
361 | { | |
362 | struct hlist_head *head; | |
363 | head = find_bucket(table, flow->hash); | |
364 | hlist_add_head_rcu(&flow->hash_node[table->node_ver], head); | |
365 | table->count++; | |
366 | } | |
367 | ||
acd051f1 | 368 | static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new) |
3544358a | 369 | { |
acd051f1 | 370 | int old_ver; |
3544358a PS |
371 | int i; |
372 | ||
acd051f1 PS |
373 | old_ver = old->node_ver; |
374 | new->node_ver = !old_ver; | |
3544358a | 375 | |
acd051f1 PS |
376 | /* Insert in new table. */ |
377 | for (i = 0; i < old->n_buckets; i++) { | |
3544358a PS |
378 | struct sw_flow *flow; |
379 | struct hlist_head *head; | |
acd051f1 | 380 | struct hlist_node *n; |
3544358a | 381 | |
acd051f1 | 382 | head = flex_array_get(old->buckets, i); |
3544358a | 383 | |
acd051f1 | 384 | hlist_for_each_entry(flow, n, head, hash_node[old_ver]) |
13e24889 | 385 | __flow_tbl_insert(new, flow); |
3544358a | 386 | } |
acd051f1 PS |
387 | old->keep_flows = true; |
388 | } | |
389 | ||
390 | static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets) | |
391 | { | |
392 | struct flow_table *new_table; | |
393 | ||
394 | new_table = ovs_flow_tbl_alloc(n_buckets); | |
395 | if (!new_table) | |
396 | return ERR_PTR(-ENOMEM); | |
397 | ||
398 | flow_table_copy_flows(table, new_table); | |
3544358a PS |
399 | |
400 | return new_table; | |
401 | } | |
402 | ||
acd051f1 PS |
403 | struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table) |
404 | { | |
405 | return __flow_tbl_rehash(table, table->n_buckets); | |
406 | } | |
407 | ||
408 | struct flow_table *ovs_flow_tbl_expand(struct flow_table *table) | |
409 | { | |
410 | return __flow_tbl_rehash(table, table->n_buckets * 2); | |
411 | } | |
412 | ||
9321954a JG |
413 | void ovs_flow_free(struct sw_flow *flow) |
414 | { | |
415 | if (unlikely(!flow)) | |
416 | return; | |
417 | ||
418 | kfree((struct sf_flow_acts __force *)flow->sf_acts); | |
419 | kmem_cache_free(flow_cache, flow); | |
420 | } | |
421 | ||
850b6b3b | 422 | /* RCU callback used by ovs_flow_deferred_free. */ |
064af421 BP |
423 | static void rcu_free_flow_callback(struct rcu_head *rcu) |
424 | { | |
425 | struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu); | |
fb8c9347 | 426 | |
9321954a | 427 | ovs_flow_free(flow); |
064af421 BP |
428 | } |
429 | ||
430 | /* Schedules 'flow' to be freed after the next RCU grace period. | |
431 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
850b6b3b | 432 | void ovs_flow_deferred_free(struct sw_flow *flow) |
064af421 BP |
433 | { |
434 | call_rcu(&flow->rcu, rcu_free_flow_callback); | |
435 | } | |
436 | ||
850b6b3b | 437 | /* RCU callback used by ovs_flow_deferred_free_acts. */ |
064af421 BP |
438 | static void rcu_free_acts_callback(struct rcu_head *rcu) |
439 | { | |
d295e8e9 | 440 | struct sw_flow_actions *sf_acts = container_of(rcu, |
064af421 BP |
441 | struct sw_flow_actions, rcu); |
442 | kfree(sf_acts); | |
443 | } | |
444 | ||
445 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. | |
446 | * The caller must hold rcu_read_lock for this to be sensible. */ | |
850b6b3b | 447 | void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts) |
064af421 BP |
448 | { |
449 | call_rcu(&sf_acts->rcu, rcu_free_acts_callback); | |
450 | } | |
451 | ||
2db65bf7 | 452 | static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) |
064af421 | 453 | { |
50f06e16 BP |
454 | struct qtag_prefix { |
455 | __be16 eth_type; /* ETH_P_8021Q */ | |
456 | __be16 tci; | |
457 | }; | |
458 | struct qtag_prefix *qp; | |
459 | ||
8ddc056d BP |
460 | if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))) |
461 | return 0; | |
462 | ||
2db65bf7 JG |
463 | if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) + |
464 | sizeof(__be16)))) | |
465 | return -ENOMEM; | |
50f06e16 BP |
466 | |
467 | qp = (struct qtag_prefix *) skb->data; | |
76abe283 | 468 | key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT); |
50f06e16 | 469 | __skb_pull(skb, sizeof(struct qtag_prefix)); |
2db65bf7 JG |
470 | |
471 | return 0; | |
50f06e16 BP |
472 | } |
473 | ||
474 | static __be16 parse_ethertype(struct sk_buff *skb) | |
064af421 | 475 | { |
50f06e16 BP |
476 | struct llc_snap_hdr { |
477 | u8 dsap; /* Always 0xAA */ | |
478 | u8 ssap; /* Always 0xAA */ | |
479 | u8 ctrl; | |
480 | u8 oui[3]; | |
8dda8c9b | 481 | __be16 ethertype; |
50f06e16 BP |
482 | }; |
483 | struct llc_snap_hdr *llc; | |
484 | __be16 proto; | |
485 | ||
486 | proto = *(__be16 *) skb->data; | |
487 | __skb_pull(skb, sizeof(__be16)); | |
488 | ||
36956a7d | 489 | if (ntohs(proto) >= 1536) |
50f06e16 BP |
490 | return proto; |
491 | ||
2db65bf7 | 492 | if (skb->len < sizeof(struct llc_snap_hdr)) |
36956a7d | 493 | return htons(ETH_P_802_2); |
50f06e16 | 494 | |
2db65bf7 JG |
495 | if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr)))) |
496 | return htons(0); | |
497 | ||
50f06e16 BP |
498 | llc = (struct llc_snap_hdr *) skb->data; |
499 | if (llc->dsap != LLC_SAP_SNAP || | |
500 | llc->ssap != LLC_SAP_SNAP || | |
501 | (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0) | |
36956a7d | 502 | return htons(ETH_P_802_2); |
50f06e16 BP |
503 | |
504 | __skb_pull(skb, sizeof(struct llc_snap_hdr)); | |
505 | return llc->ethertype; | |
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) |
13e24889 | 625 | memcpy(&key->phy.tun.tun_key, OVS_CB(skb)->tun_key, sizeof(key->phy.tun.tun_key)); |
abff858b | 626 | key->phy.in_port = in_port; |
064af421 | 627 | |
064af421 | 628 | skb_reset_mac_header(skb); |
064af421 | 629 | |
2db65bf7 JG |
630 | /* Link layer. We are guaranteed to have at least the 14 byte Ethernet |
631 | * header in the linear data area. | |
632 | */ | |
50f06e16 | 633 | eth = eth_hdr(skb); |
76abe283 AE |
634 | memcpy(key->eth.src, eth->h_source, ETH_ALEN); |
635 | memcpy(key->eth.dst, eth->h_dest, ETH_ALEN); | |
636 | ||
50f06e16 | 637 | __skb_pull(skb, 2 * ETH_ALEN); |
6ce39213 JG |
638 | |
639 | if (vlan_tx_tag_present(skb)) | |
76abe283 | 640 | key->eth.tci = htons(vlan_get_tci(skb)); |
6ce39213 | 641 | else if (eth->h_proto == htons(ETH_P_8021Q)) |
2db65bf7 JG |
642 | if (unlikely(parse_vlan(skb, key))) |
643 | return -ENOMEM; | |
6ce39213 | 644 | |
76abe283 AE |
645 | key->eth.type = parse_ethertype(skb); |
646 | if (unlikely(key->eth.type == htons(0))) | |
2db65bf7 JG |
647 | return -ENOMEM; |
648 | ||
50f06e16 | 649 | skb_reset_network_header(skb); |
2db65bf7 | 650 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
064af421 BP |
651 | |
652 | /* Network layer. */ | |
76abe283 | 653 | if (key->eth.type == htons(ETH_P_IP)) { |
4c1ad233 | 654 | struct iphdr *nh; |
7257b535 | 655 | __be16 offset; |
76abe283 AE |
656 | |
657 | key_len = SW_FLOW_KEY_OFFSET(ipv4.addr); | |
4c1ad233 BP |
658 | |
659 | error = check_iphdr(skb); | |
660 | if (unlikely(error)) { | |
661 | if (error == -EINVAL) { | |
662 | skb->transport_header = skb->network_header; | |
76abe283 | 663 | error = 0; |
4c1ad233 | 664 | } |
76abe283 | 665 | goto out; |
4c1ad233 BP |
666 | } |
667 | ||
668 | nh = ip_hdr(skb); | |
76abe283 AE |
669 | key->ipv4.addr.src = nh->saddr; |
670 | key->ipv4.addr.dst = nh->daddr; | |
7257b535 | 671 | |
28bad473 | 672 | key->ip.proto = nh->protocol; |
530180fd | 673 | key->ip.tos = nh->tos; |
a61680c6 | 674 | key->ip.ttl = nh->ttl; |
064af421 | 675 | |
7257b535 BP |
676 | offset = nh->frag_off & htons(IP_OFFSET); |
677 | if (offset) { | |
9e44d715 | 678 | key->ip.frag = OVS_FRAG_TYPE_LATER; |
7257b535 BP |
679 | goto out; |
680 | } | |
681 | if (nh->frag_off & htons(IP_MF) || | |
682 | skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
9e44d715 | 683 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
b7a31ec1 | 684 | |
7257b535 | 685 | /* Transport layer. */ |
28bad473 | 686 | if (key->ip.proto == IPPROTO_TCP) { |
5f4d087c | 687 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 688 | if (tcphdr_ok(skb)) { |
5f4d087c JG |
689 | struct tcphdr *tcp = tcp_hdr(skb); |
690 | key->ipv4.tp.src = tcp->source; | |
691 | key->ipv4.tp.dst = tcp->dest; | |
692 | } | |
28bad473 | 693 | } else if (key->ip.proto == IPPROTO_UDP) { |
5f4d087c | 694 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 695 | if (udphdr_ok(skb)) { |
5f4d087c JG |
696 | struct udphdr *udp = udp_hdr(skb); |
697 | key->ipv4.tp.src = udp->source; | |
698 | key->ipv4.tp.dst = udp->dest; | |
699 | } | |
28bad473 | 700 | } else if (key->ip.proto == IPPROTO_ICMP) { |
5f4d087c | 701 | key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); |
7257b535 | 702 | if (icmphdr_ok(skb)) { |
5f4d087c JG |
703 | struct icmphdr *icmp = icmp_hdr(skb); |
704 | /* The ICMP type and code fields use the 16-bit | |
6455100f PS |
705 | * transport port fields, so we need to store |
706 | * them in 16-bit network byte order. */ | |
5f4d087c JG |
707 | key->ipv4.tp.src = htons(icmp->type); |
708 | key->ipv4.tp.dst = htons(icmp->code); | |
709 | } | |
710 | } | |
711 | ||
8087f5ff MM |
712 | } else if ((key->eth.type == htons(ETH_P_ARP) || |
713 | key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) { | |
a26ef517 JP |
714 | struct arp_eth_header *arp; |
715 | ||
716 | arp = (struct arp_eth_header *)skb_network_header(skb); | |
717 | ||
f5e86186 | 718 | if (arp->ar_hrd == htons(ARPHRD_ETHER) |
de3f65ea JP |
719 | && arp->ar_pro == htons(ETH_P_IP) |
720 | && arp->ar_hln == ETH_ALEN | |
721 | && arp->ar_pln == 4) { | |
722 | ||
723 | /* We only match on the lower 8 bits of the opcode. */ | |
b7a31ec1 | 724 | if (ntohs(arp->ar_op) <= 0xff) |
28bad473 | 725 | key->ip.proto = ntohs(arp->ar_op); |
a3d3ad0c MM |
726 | memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src)); |
727 | memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst)); | |
728 | memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN); | |
729 | memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN); | |
730 | key_len = SW_FLOW_KEY_OFFSET(ipv4.arp); | |
de3f65ea | 731 | } |
76abe283 | 732 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
d31f1109 JP |
733 | int nh_len; /* IPv6 Header + Extensions */ |
734 | ||
76abe283 | 735 | nh_len = parse_ipv6hdr(skb, key, &key_len); |
d31f1109 | 736 | if (unlikely(nh_len < 0)) { |
76abe283 | 737 | if (nh_len == -EINVAL) |
d31f1109 | 738 | skb->transport_header = skb->network_header; |
76abe283 AE |
739 | else |
740 | error = nh_len; | |
741 | goto out; | |
d31f1109 JP |
742 | } |
743 | ||
9e44d715 | 744 | if (key->ip.frag == OVS_FRAG_TYPE_LATER) |
7257b535 BP |
745 | goto out; |
746 | if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP) | |
9e44d715 | 747 | key->ip.frag = OVS_FRAG_TYPE_FIRST; |
7257b535 | 748 | |
d31f1109 | 749 | /* Transport layer. */ |
28bad473 | 750 | if (key->ip.proto == NEXTHDR_TCP) { |
76abe283 | 751 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 JP |
752 | if (tcphdr_ok(skb)) { |
753 | struct tcphdr *tcp = tcp_hdr(skb); | |
76abe283 AE |
754 | key->ipv6.tp.src = tcp->source; |
755 | key->ipv6.tp.dst = tcp->dest; | |
d31f1109 | 756 | } |
28bad473 | 757 | } else if (key->ip.proto == NEXTHDR_UDP) { |
76abe283 | 758 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 JP |
759 | if (udphdr_ok(skb)) { |
760 | struct udphdr *udp = udp_hdr(skb); | |
76abe283 AE |
761 | key->ipv6.tp.src = udp->source; |
762 | key->ipv6.tp.dst = udp->dest; | |
d31f1109 | 763 | } |
28bad473 | 764 | } else if (key->ip.proto == NEXTHDR_ICMP) { |
76abe283 | 765 | key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); |
d31f1109 | 766 | if (icmp6hdr_ok(skb)) { |
76abe283 | 767 | error = parse_icmpv6(skb, key, &key_len, nh_len); |
685a51a5 | 768 | if (error < 0) |
76abe283 | 769 | goto out; |
d31f1109 JP |
770 | } |
771 | } | |
064af421 | 772 | } |
76abe283 AE |
773 | |
774 | out: | |
775 | *key_lenp = key_len; | |
776 | return error; | |
064af421 BP |
777 | } |
778 | ||
13e24889 | 779 | static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len) |
8d5ebd83 | 780 | { |
13e24889 PS |
781 | return jhash2((u32 *)((u8 *)key + key_start), |
782 | DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0); | |
783 | } | |
784 | ||
785 | static int flow_key_start(struct sw_flow_key *key) | |
786 | { | |
787 | if (key->phy.tun.tun_key.ipv4_dst) | |
788 | return 0; | |
789 | else | |
790 | return offsetof(struct sw_flow_key, phy.priority); | |
8d5ebd83 JG |
791 | } |
792 | ||
850b6b3b | 793 | struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table, |
3544358a | 794 | struct sw_flow_key *key, int key_len) |
8d5ebd83 | 795 | { |
3544358a PS |
796 | struct sw_flow *flow; |
797 | struct hlist_node *n; | |
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); |
acd051f1 | 808 | hlist_for_each_entry_rcu(flow, n, 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 | { |
acd051f1 PS |
828 | hlist_del_rcu(&flow->hash_node[table->node_ver]); |
829 | table->count--; | |
830 | BUG_ON(table->count < 0); | |
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), | |
df2c07f4 | 838 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), |
fea393b1 | 839 | [OVS_KEY_ATTR_VLAN] = sizeof(__be16), |
a1bf209f | 840 | [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16), |
df2c07f4 JP |
841 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), |
842 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), | |
843 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), | |
844 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), | |
845 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), | |
846 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), | |
847 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), | |
848 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), | |
3f30071d | 849 | [OVS_KEY_ATTR_IPV4_TUNNEL] = sizeof(struct ovs_key_ipv4_tunnel), |
a1bf209f BP |
850 | |
851 | /* Not upstream. */ | |
852 | [OVS_KEY_ATTR_TUN_ID] = sizeof(__be64), | |
80e5eed9 BP |
853 | }; |
854 | ||
34118cae BP |
855 | static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len, |
856 | const struct nlattr *a[], u64 *attrs) | |
857 | { | |
858 | const struct ovs_key_icmp *icmp_key; | |
859 | const struct ovs_key_tcp *tcp_key; | |
860 | const struct ovs_key_udp *udp_key; | |
861 | ||
862 | switch (swkey->ip.proto) { | |
863 | case IPPROTO_TCP: | |
864 | if (!(*attrs & (1 << OVS_KEY_ATTR_TCP))) | |
865 | return -EINVAL; | |
866 | *attrs &= ~(1 << OVS_KEY_ATTR_TCP); | |
867 | ||
868 | *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); | |
869 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); | |
870 | swkey->ipv4.tp.src = tcp_key->tcp_src; | |
871 | swkey->ipv4.tp.dst = tcp_key->tcp_dst; | |
872 | break; | |
873 | ||
874 | case IPPROTO_UDP: | |
875 | if (!(*attrs & (1 << OVS_KEY_ATTR_UDP))) | |
876 | return -EINVAL; | |
877 | *attrs &= ~(1 << OVS_KEY_ATTR_UDP); | |
878 | ||
879 | *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); | |
880 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); | |
881 | swkey->ipv4.tp.src = udp_key->udp_src; | |
882 | swkey->ipv4.tp.dst = udp_key->udp_dst; | |
883 | break; | |
884 | ||
885 | case IPPROTO_ICMP: | |
886 | if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP))) | |
887 | return -EINVAL; | |
888 | *attrs &= ~(1 << OVS_KEY_ATTR_ICMP); | |
889 | ||
890 | *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp); | |
891 | icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]); | |
892 | swkey->ipv4.tp.src = htons(icmp_key->icmp_type); | |
893 | swkey->ipv4.tp.dst = htons(icmp_key->icmp_code); | |
894 | break; | |
895 | } | |
896 | ||
897 | return 0; | |
898 | } | |
899 | ||
900 | static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len, | |
901 | const struct nlattr *a[], u64 *attrs) | |
902 | { | |
903 | const struct ovs_key_icmpv6 *icmpv6_key; | |
904 | const struct ovs_key_tcp *tcp_key; | |
905 | const struct ovs_key_udp *udp_key; | |
906 | ||
907 | switch (swkey->ip.proto) { | |
908 | case IPPROTO_TCP: | |
909 | if (!(*attrs & (1 << OVS_KEY_ATTR_TCP))) | |
910 | return -EINVAL; | |
911 | *attrs &= ~(1 << OVS_KEY_ATTR_TCP); | |
912 | ||
913 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
914 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); | |
915 | swkey->ipv6.tp.src = tcp_key->tcp_src; | |
916 | swkey->ipv6.tp.dst = tcp_key->tcp_dst; | |
917 | break; | |
918 | ||
919 | case IPPROTO_UDP: | |
920 | if (!(*attrs & (1 << OVS_KEY_ATTR_UDP))) | |
921 | return -EINVAL; | |
922 | *attrs &= ~(1 << OVS_KEY_ATTR_UDP); | |
923 | ||
924 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
925 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); | |
926 | swkey->ipv6.tp.src = udp_key->udp_src; | |
927 | swkey->ipv6.tp.dst = udp_key->udp_dst; | |
928 | break; | |
929 | ||
930 | case IPPROTO_ICMPV6: | |
931 | if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6))) | |
932 | return -EINVAL; | |
933 | *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6); | |
934 | ||
935 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp); | |
936 | icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]); | |
937 | swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type); | |
938 | swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code); | |
939 | ||
940 | if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) || | |
941 | swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) { | |
942 | const struct ovs_key_nd *nd_key; | |
943 | ||
944 | if (!(*attrs & (1 << OVS_KEY_ATTR_ND))) | |
945 | return -EINVAL; | |
946 | *attrs &= ~(1 << OVS_KEY_ATTR_ND); | |
947 | ||
948 | *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd); | |
949 | nd_key = nla_data(a[OVS_KEY_ATTR_ND]); | |
950 | memcpy(&swkey->ipv6.nd.target, nd_key->nd_target, | |
951 | sizeof(swkey->ipv6.nd.target)); | |
952 | memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN); | |
953 | memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN); | |
954 | } | |
955 | break; | |
956 | } | |
957 | ||
958 | return 0; | |
959 | } | |
960 | ||
fea393b1 BP |
961 | static int parse_flow_nlattrs(const struct nlattr *attr, |
962 | const struct nlattr *a[], u64 *attrsp) | |
963 | { | |
964 | const struct nlattr *nla; | |
965 | u64 attrs; | |
966 | int rem; | |
967 | ||
968 | attrs = 0; | |
969 | nla_for_each_nested(nla, attr, rem) { | |
970 | u16 type = nla_type(nla); | |
4094406f | 971 | int expected_len; |
fea393b1 | 972 | |
4094406f | 973 | if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type)) |
fea393b1 | 974 | return -EINVAL; |
4094406f BP |
975 | |
976 | expected_len = ovs_key_lens[type]; | |
977 | if (nla_len(nla) != expected_len && expected_len != -1) | |
978 | return -EINVAL; | |
979 | ||
fea393b1 BP |
980 | attrs |= 1ULL << type; |
981 | a[type] = nla; | |
982 | } | |
983 | if (rem) | |
984 | return -EINVAL; | |
985 | ||
986 | *attrsp = attrs; | |
987 | return 0; | |
988 | } | |
989 | ||
36956a7d | 990 | /** |
850b6b3b | 991 | * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key. |
36956a7d | 992 | * @swkey: receives the extracted flow key. |
76abe283 | 993 | * @key_lenp: number of bytes used in @swkey. |
df2c07f4 | 994 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
d6569377 | 995 | * sequence. |
36956a7d | 996 | */ |
850b6b3b | 997 | int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp, |
76abe283 | 998 | const struct nlattr *attr) |
36956a7d | 999 | { |
34118cae BP |
1000 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
1001 | const struct ovs_key_ethernet *eth_key; | |
76abe283 | 1002 | int key_len; |
34118cae | 1003 | u64 attrs; |
fea393b1 | 1004 | int err; |
36956a7d | 1005 | |
34118cae | 1006 | memset(swkey, 0, sizeof(struct sw_flow_key)); |
76abe283 | 1007 | key_len = SW_FLOW_KEY_OFFSET(eth); |
36956a7d | 1008 | |
fea393b1 BP |
1009 | err = parse_flow_nlattrs(attr, a, &attrs); |
1010 | if (err) | |
1011 | return err; | |
36956a7d | 1012 | |
34118cae BP |
1013 | /* Metadata attributes. */ |
1014 | if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) { | |
1015 | swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]); | |
1016 | attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY); | |
1017 | } | |
1018 | if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) { | |
1019 | u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]); | |
1020 | if (in_port >= DP_MAX_PORTS) | |
1021 | return -EINVAL; | |
1022 | swkey->phy.in_port = in_port; | |
1023 | attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT); | |
1024 | } else { | |
95b1d73a | 1025 | swkey->phy.in_port = DP_MAX_PORTS; |
34118cae | 1026 | } |
36956a7d | 1027 | |
356af50b KM |
1028 | if (attrs & (1ULL << OVS_KEY_ATTR_TUN_ID) && |
1029 | attrs & (1ULL << OVS_KEY_ATTR_IPV4_TUNNEL)) { | |
1030 | struct ovs_key_ipv4_tunnel *tun_key; | |
1031 | __be64 tun_id; | |
1032 | ||
1033 | tun_key = nla_data(a[OVS_KEY_ATTR_IPV4_TUNNEL]); | |
1034 | ||
1035 | if (!tun_key->ipv4_dst) | |
1036 | return -EINVAL; | |
49a4902d | 1037 | if (!(tun_key->tun_flags & OVS_TNL_F_KEY)) |
356af50b KM |
1038 | return -EINVAL; |
1039 | ||
1040 | tun_id = nla_get_be64(a[OVS_KEY_ATTR_TUN_ID]); | |
1041 | if (tun_id != tun_key->tun_id) | |
1042 | return -EINVAL; | |
1043 | ||
5c7f5883 JG |
1044 | memcpy(&swkey->phy.tun.tun_key, tun_key, |
1045 | sizeof(swkey->phy.tun.tun_key)); | |
356af50b KM |
1046 | |
1047 | attrs &= ~(1ULL << OVS_KEY_ATTR_TUN_ID); | |
5c7f5883 | 1048 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4_TUNNEL); |
356af50b KM |
1049 | } else if (attrs & (1ULL << OVS_KEY_ATTR_IPV4_TUNNEL)) { |
1050 | struct ovs_key_ipv4_tunnel *tun_key; | |
1051 | tun_key = nla_data(a[OVS_KEY_ATTR_IPV4_TUNNEL]); | |
1052 | ||
1053 | if (!tun_key->ipv4_dst) | |
1054 | return -EINVAL; | |
1055 | ||
5c7f5883 JG |
1056 | memcpy(&swkey->phy.tun.tun_key, tun_key, |
1057 | sizeof(swkey->phy.tun.tun_key)); | |
1058 | ||
356af50b | 1059 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4_TUNNEL); |
34118cae | 1060 | } |
76abe283 | 1061 | |
34118cae BP |
1062 | /* Data attributes. */ |
1063 | if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET))) | |
1064 | return -EINVAL; | |
1065 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET); | |
36956a7d | 1066 | |
34118cae BP |
1067 | eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]); |
1068 | memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN); | |
1069 | memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN); | |
76abe283 | 1070 | |
8ddc056d | 1071 | if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) && |
fea393b1 | 1072 | nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) { |
8ddc056d BP |
1073 | const struct nlattr *encap; |
1074 | __be16 tci; | |
1075 | ||
1076 | if (attrs != ((1 << OVS_KEY_ATTR_VLAN) | | |
1077 | (1 << OVS_KEY_ATTR_ETHERTYPE) | | |
1078 | (1 << OVS_KEY_ATTR_ENCAP))) | |
34118cae | 1079 | return -EINVAL; |
36956a7d | 1080 | |
8ddc056d BP |
1081 | encap = a[OVS_KEY_ATTR_ENCAP]; |
1082 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
1083 | if (tci & htons(VLAN_TAG_PRESENT)) { | |
1084 | swkey->eth.tci = tci; | |
1085 | ||
1086 | err = parse_flow_nlattrs(encap, a, &attrs); | |
1087 | if (err) | |
1088 | return err; | |
1089 | } else if (!tci) { | |
1090 | /* Corner case for truncated 802.1Q header. */ | |
1091 | if (nla_len(encap)) | |
1092 | return -EINVAL; | |
1093 | ||
1094 | swkey->eth.type = htons(ETH_P_8021Q); | |
1095 | *key_lenp = key_len; | |
1096 | return 0; | |
1097 | } else { | |
1098 | return -EINVAL; | |
1099 | } | |
34118cae BP |
1100 | } |
1101 | ||
1102 | if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) { | |
1103 | swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
1104 | if (ntohs(swkey->eth.type) < 1536) | |
1105 | return -EINVAL; | |
1106 | attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE); | |
1107 | } else { | |
1108 | swkey->eth.type = htons(ETH_P_802_2); | |
1109 | } | |
1110 | ||
1111 | if (swkey->eth.type == htons(ETH_P_IP)) { | |
1112 | const struct ovs_key_ipv4 *ipv4_key; | |
685a51a5 | 1113 | |
34118cae BP |
1114 | if (!(attrs & (1 << OVS_KEY_ATTR_IPV4))) |
1115 | return -EINVAL; | |
1116 | attrs &= ~(1 << OVS_KEY_ATTR_IPV4); | |
1117 | ||
1118 | key_len = SW_FLOW_KEY_OFFSET(ipv4.addr); | |
1119 | ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]); | |
1120 | if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) | |
1121 | return -EINVAL; | |
1122 | swkey->ip.proto = ipv4_key->ipv4_proto; | |
1123 | swkey->ip.tos = ipv4_key->ipv4_tos; | |
1124 | swkey->ip.ttl = ipv4_key->ipv4_ttl; | |
1125 | swkey->ip.frag = ipv4_key->ipv4_frag; | |
1126 | swkey->ipv4.addr.src = ipv4_key->ipv4_src; | |
1127 | swkey->ipv4.addr.dst = ipv4_key->ipv4_dst; | |
1128 | ||
1129 | if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) { | |
fea393b1 | 1130 | err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs); |
34118cae BP |
1131 | if (err) |
1132 | return err; | |
36956a7d | 1133 | } |
34118cae BP |
1134 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
1135 | const struct ovs_key_ipv6 *ipv6_key; | |
36956a7d | 1136 | |
34118cae BP |
1137 | if (!(attrs & (1 << OVS_KEY_ATTR_IPV6))) |
1138 | return -EINVAL; | |
1139 | attrs &= ~(1 << OVS_KEY_ATTR_IPV6); | |
1140 | ||
1141 | key_len = SW_FLOW_KEY_OFFSET(ipv6.label); | |
1142 | ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]); | |
1143 | if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) | |
1144 | return -EINVAL; | |
1145 | swkey->ipv6.label = ipv6_key->ipv6_label; | |
1146 | swkey->ip.proto = ipv6_key->ipv6_proto; | |
1147 | swkey->ip.tos = ipv6_key->ipv6_tclass; | |
1148 | swkey->ip.ttl = ipv6_key->ipv6_hlimit; | |
1149 | swkey->ip.frag = ipv6_key->ipv6_frag; | |
1150 | memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src, | |
1151 | sizeof(swkey->ipv6.addr.src)); | |
1152 | memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst, | |
1153 | sizeof(swkey->ipv6.addr.dst)); | |
1154 | ||
1155 | if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) { | |
fea393b1 | 1156 | err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs); |
34118cae BP |
1157 | if (err) |
1158 | return err; | |
1159 | } | |
8087f5ff MM |
1160 | } else if (swkey->eth.type == htons(ETH_P_ARP) || |
1161 | swkey->eth.type == htons(ETH_P_RARP)) { | |
34118cae | 1162 | const struct ovs_key_arp *arp_key; |
36956a7d | 1163 | |
34118cae BP |
1164 | if (!(attrs & (1 << OVS_KEY_ATTR_ARP))) |
1165 | return -EINVAL; | |
1166 | attrs &= ~(1 << OVS_KEY_ATTR_ARP); | |
1167 | ||
1168 | key_len = SW_FLOW_KEY_OFFSET(ipv4.arp); | |
1169 | arp_key = nla_data(a[OVS_KEY_ATTR_ARP]); | |
1170 | swkey->ipv4.addr.src = arp_key->arp_sip; | |
1171 | swkey->ipv4.addr.dst = arp_key->arp_tip; | |
1172 | if (arp_key->arp_op & htons(0xff00)) | |
1173 | return -EINVAL; | |
1174 | swkey->ip.proto = ntohs(arp_key->arp_op); | |
1175 | memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN); | |
1176 | memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN); | |
1177 | } | |
76abe283 | 1178 | |
34118cae BP |
1179 | if (attrs) |
1180 | return -EINVAL; | |
76abe283 | 1181 | *key_lenp = key_len; |
34118cae BP |
1182 | |
1183 | return 0; | |
36956a7d BP |
1184 | } |
1185 | ||
80e5eed9 | 1186 | /** |
850b6b3b | 1187 | * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key. |
80e5eed9 BP |
1188 | * @in_port: receives the extracted input port. |
1189 | * @tun_id: receives the extracted tunnel ID. | |
df2c07f4 | 1190 | * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
80e5eed9 BP |
1191 | * sequence. |
1192 | * | |
1193 | * This parses a series of Netlink attributes that form a flow key, which must | |
1194 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to | |
1195 | * get the metadata, that is, the parts of the flow key that cannot be | |
1196 | * extracted from the packet itself. | |
1197 | */ | |
13e24889 PS |
1198 | |
1199 | int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, int key_len, const struct nlattr *attr) | |
80e5eed9 | 1200 | { |
13e24889 | 1201 | struct ovs_key_ipv4_tunnel *tun_key = &flow->key.phy.tun.tun_key; |
80e5eed9 | 1202 | const struct nlattr *nla; |
80e5eed9 | 1203 | int rem; |
13e24889 | 1204 | __be64 tun_id = 0; |
80e5eed9 | 1205 | |
13e24889 PS |
1206 | flow->key.phy.in_port = DP_MAX_PORTS; |
1207 | flow->key.phy.priority = 0; | |
1208 | memset(tun_key, 0, sizeof(flow->key.phy.tun.tun_key)); | |
80e5eed9 | 1209 | |
80e5eed9 | 1210 | nla_for_each_nested(nla, attr, rem) { |
6455100f | 1211 | int type = nla_type(nla); |
80e5eed9 | 1212 | |
4094406f | 1213 | if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) { |
58828b08 AA |
1214 | if (nla_len(nla) != ovs_key_lens[type]) |
1215 | return -EINVAL; | |
abff858b | 1216 | |
58828b08 AA |
1217 | switch (type) { |
1218 | case OVS_KEY_ATTR_PRIORITY: | |
13e24889 | 1219 | flow->key.phy.priority = nla_get_u32(nla); |
58828b08 | 1220 | break; |
80e5eed9 | 1221 | |
58828b08 | 1222 | case OVS_KEY_ATTR_TUN_ID: |
356af50b KM |
1223 | tun_id = nla_get_be64(nla); |
1224 | ||
1225 | if (tun_key->ipv4_dst) { | |
49a4902d | 1226 | if (!(tun_key->tun_flags & OVS_TNL_F_KEY)) |
356af50b KM |
1227 | return -EINVAL; |
1228 | if (tun_key->tun_id != tun_id) | |
1229 | return -EINVAL; | |
1230 | break; | |
1231 | } | |
1232 | tun_key->tun_id = tun_id; | |
49a4902d | 1233 | tun_key->tun_flags |= OVS_TNL_F_KEY; |
356af50b KM |
1234 | |
1235 | break; | |
1236 | ||
1237 | case OVS_KEY_ATTR_IPV4_TUNNEL: | |
49a4902d | 1238 | if (tun_key->tun_flags & OVS_TNL_F_KEY) { |
356af50b KM |
1239 | tun_id = tun_key->tun_id; |
1240 | ||
1241 | memcpy(tun_key, nla_data(nla), sizeof(*tun_key)); | |
49a4902d | 1242 | if (!(tun_key->tun_flags & OVS_TNL_F_KEY)) |
356af50b KM |
1243 | return -EINVAL; |
1244 | ||
1245 | if (tun_key->tun_id != tun_id) | |
1246 | return -EINVAL; | |
1247 | } else | |
1248 | memcpy(tun_key, nla_data(nla), sizeof(*tun_key)); | |
1249 | ||
1250 | if (!tun_key->ipv4_dst) | |
1251 | return -EINVAL; | |
58828b08 | 1252 | break; |
80e5eed9 | 1253 | |
58828b08 AA |
1254 | case OVS_KEY_ATTR_IN_PORT: |
1255 | if (nla_get_u32(nla) >= DP_MAX_PORTS) | |
1256 | return -EINVAL; | |
13e24889 | 1257 | flow->key.phy.in_port = nla_get_u32(nla); |
58828b08 AA |
1258 | break; |
1259 | } | |
80e5eed9 | 1260 | } |
80e5eed9 BP |
1261 | } |
1262 | if (rem) | |
1263 | return -EINVAL; | |
13e24889 PS |
1264 | |
1265 | flow->hash = ovs_flow_hash(&flow->key, | |
1266 | flow_key_start(&flow->key), key_len); | |
1267 | ||
80e5eed9 BP |
1268 | return 0; |
1269 | } | |
1270 | ||
850b6b3b | 1271 | int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb) |
36956a7d | 1272 | { |
df2c07f4 | 1273 | struct ovs_key_ethernet *eth_key; |
fea393b1 | 1274 | struct nlattr *nla, *encap; |
36956a7d | 1275 | |
c3cc8c03 DM |
1276 | if (swkey->phy.priority && |
1277 | nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority)) | |
1278 | goto nla_put_failure; | |
18c43631 | 1279 | |
13e24889 | 1280 | if (swkey->phy.tun.tun_key.ipv4_dst) { |
356af50b KM |
1281 | struct ovs_key_ipv4_tunnel *tun_key; |
1282 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4_TUNNEL, sizeof(*tun_key)); | |
1283 | if (!nla) | |
1284 | goto nla_put_failure; | |
1285 | tun_key = nla_data(nla); | |
13e24889 | 1286 | memcpy(tun_key, &swkey->phy.tun.tun_key, sizeof(*tun_key)); |
356af50b | 1287 | } |
49a4902d | 1288 | if ((swkey->phy.tun.tun_key.tun_flags & OVS_TNL_F_KEY) && |
13e24889 | 1289 | nla_put_be64(skb, OVS_KEY_ATTR_TUN_ID, swkey->phy.tun.tun_key.tun_id)) |
c3cc8c03 | 1290 | goto nla_put_failure; |
36956a7d | 1291 | |
c3cc8c03 DM |
1292 | if (swkey->phy.in_port != DP_MAX_PORTS && |
1293 | nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port)) | |
1294 | goto nla_put_failure; | |
36956a7d | 1295 | |
df2c07f4 | 1296 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); |
d6569377 BP |
1297 | if (!nla) |
1298 | goto nla_put_failure; | |
1299 | eth_key = nla_data(nla); | |
76abe283 AE |
1300 | memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN); |
1301 | memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN); | |
36956a7d | 1302 | |
8ddc056d | 1303 | if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) { |
c3cc8c03 DM |
1304 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) || |
1305 | nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci)) | |
1306 | goto nla_put_failure; | |
fea393b1 | 1307 | encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP); |
8ddc056d BP |
1308 | if (!swkey->eth.tci) |
1309 | goto unencap; | |
fea393b1 BP |
1310 | } else { |
1311 | encap = NULL; | |
36956a7d BP |
1312 | } |
1313 | ||
76abe283 | 1314 | if (swkey->eth.type == htons(ETH_P_802_2)) |
fea393b1 | 1315 | goto unencap; |
36956a7d | 1316 | |
c3cc8c03 DM |
1317 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type)) |
1318 | goto nla_put_failure; | |
36956a7d | 1319 | |
76abe283 | 1320 | if (swkey->eth.type == htons(ETH_P_IP)) { |
df2c07f4 | 1321 | struct ovs_key_ipv4 *ipv4_key; |
36956a7d | 1322 | |
df2c07f4 | 1323 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); |
d6569377 BP |
1324 | if (!nla) |
1325 | goto nla_put_failure; | |
1326 | ipv4_key = nla_data(nla); | |
76abe283 AE |
1327 | ipv4_key->ipv4_src = swkey->ipv4.addr.src; |
1328 | ipv4_key->ipv4_dst = swkey->ipv4.addr.dst; | |
28bad473 | 1329 | ipv4_key->ipv4_proto = swkey->ip.proto; |
530180fd | 1330 | ipv4_key->ipv4_tos = swkey->ip.tos; |
a61680c6 | 1331 | ipv4_key->ipv4_ttl = swkey->ip.ttl; |
9e44d715 | 1332 | ipv4_key->ipv4_frag = swkey->ip.frag; |
76abe283 | 1333 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { |
df2c07f4 | 1334 | struct ovs_key_ipv6 *ipv6_key; |
d31f1109 | 1335 | |
df2c07f4 | 1336 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); |
d31f1109 JP |
1337 | if (!nla) |
1338 | goto nla_put_failure; | |
1339 | ipv6_key = nla_data(nla); | |
76abe283 | 1340 | memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src, |
d31f1109 | 1341 | sizeof(ipv6_key->ipv6_src)); |
76abe283 | 1342 | memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst, |
d31f1109 | 1343 | sizeof(ipv6_key->ipv6_dst)); |
fa8223b7 | 1344 | ipv6_key->ipv6_label = swkey->ipv6.label; |
28bad473 | 1345 | ipv6_key->ipv6_proto = swkey->ip.proto; |
60258dcb | 1346 | ipv6_key->ipv6_tclass = swkey->ip.tos; |
a61680c6 | 1347 | ipv6_key->ipv6_hlimit = swkey->ip.ttl; |
9e44d715 | 1348 | ipv6_key->ipv6_frag = swkey->ip.frag; |
8087f5ff MM |
1349 | } else if (swkey->eth.type == htons(ETH_P_ARP) || |
1350 | swkey->eth.type == htons(ETH_P_RARP)) { | |
df2c07f4 | 1351 | struct ovs_key_arp *arp_key; |
d31f1109 | 1352 | |
df2c07f4 | 1353 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); |
d31f1109 JP |
1354 | if (!nla) |
1355 | goto nla_put_failure; | |
1356 | arp_key = nla_data(nla); | |
df2c07f4 | 1357 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); |
76abe283 AE |
1358 | arp_key->arp_sip = swkey->ipv4.addr.src; |
1359 | arp_key->arp_tip = swkey->ipv4.addr.dst; | |
28bad473 | 1360 | arp_key->arp_op = htons(swkey->ip.proto); |
76abe283 AE |
1361 | memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN); |
1362 | memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN); | |
d31f1109 JP |
1363 | } |
1364 | ||
7257b535 BP |
1365 | if ((swkey->eth.type == htons(ETH_P_IP) || |
1366 | swkey->eth.type == htons(ETH_P_IPV6)) && | |
9e44d715 | 1367 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { |
36956a7d | 1368 | |
28bad473 | 1369 | if (swkey->ip.proto == IPPROTO_TCP) { |
df2c07f4 | 1370 | struct ovs_key_tcp *tcp_key; |
36956a7d | 1371 | |
df2c07f4 | 1372 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); |
d6569377 BP |
1373 | if (!nla) |
1374 | goto nla_put_failure; | |
1375 | tcp_key = nla_data(nla); | |
76abe283 AE |
1376 | if (swkey->eth.type == htons(ETH_P_IP)) { |
1377 | tcp_key->tcp_src = swkey->ipv4.tp.src; | |
1378 | tcp_key->tcp_dst = swkey->ipv4.tp.dst; | |
1379 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
1380 | tcp_key->tcp_src = swkey->ipv6.tp.src; | |
1381 | tcp_key->tcp_dst = swkey->ipv6.tp.dst; | |
1382 | } | |
28bad473 | 1383 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
df2c07f4 | 1384 | struct ovs_key_udp *udp_key; |
36956a7d | 1385 | |
df2c07f4 | 1386 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); |
d6569377 BP |
1387 | if (!nla) |
1388 | goto nla_put_failure; | |
1389 | udp_key = nla_data(nla); | |
76abe283 AE |
1390 | if (swkey->eth.type == htons(ETH_P_IP)) { |
1391 | udp_key->udp_src = swkey->ipv4.tp.src; | |
1392 | udp_key->udp_dst = swkey->ipv4.tp.dst; | |
1393 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
1394 | udp_key->udp_src = swkey->ipv6.tp.src; | |
1395 | udp_key->udp_dst = swkey->ipv6.tp.dst; | |
1396 | } | |
1397 | } else if (swkey->eth.type == htons(ETH_P_IP) && | |
28bad473 | 1398 | swkey->ip.proto == IPPROTO_ICMP) { |
df2c07f4 | 1399 | struct ovs_key_icmp *icmp_key; |
36956a7d | 1400 | |
df2c07f4 | 1401 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); |
d6569377 BP |
1402 | if (!nla) |
1403 | goto nla_put_failure; | |
1404 | icmp_key = nla_data(nla); | |
76abe283 AE |
1405 | icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src); |
1406 | icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst); | |
1407 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && | |
28bad473 | 1408 | swkey->ip.proto == IPPROTO_ICMPV6) { |
df2c07f4 | 1409 | struct ovs_key_icmpv6 *icmpv6_key; |
36956a7d | 1410 | |
df2c07f4 | 1411 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, |
bfef4717 | 1412 | sizeof(*icmpv6_key)); |
d31f1109 JP |
1413 | if (!nla) |
1414 | goto nla_put_failure; | |
1415 | icmpv6_key = nla_data(nla); | |
76abe283 AE |
1416 | icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src); |
1417 | icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst); | |
685a51a5 | 1418 | |
bfef4717 JG |
1419 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || |
1420 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { | |
df2c07f4 | 1421 | struct ovs_key_nd *nd_key; |
685a51a5 | 1422 | |
df2c07f4 | 1423 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); |
685a51a5 JP |
1424 | if (!nla) |
1425 | goto nla_put_failure; | |
1426 | nd_key = nla_data(nla); | |
76abe283 | 1427 | memcpy(nd_key->nd_target, &swkey->ipv6.nd.target, |
685a51a5 | 1428 | sizeof(nd_key->nd_target)); |
76abe283 AE |
1429 | memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN); |
1430 | memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN); | |
685a51a5 | 1431 | } |
d31f1109 | 1432 | } |
36956a7d BP |
1433 | } |
1434 | ||
fea393b1 BP |
1435 | unencap: |
1436 | if (encap) | |
1437 | nla_nest_end(skb, encap); | |
1438 | ||
d6569377 | 1439 | return 0; |
36956a7d | 1440 | |
d6569377 BP |
1441 | nla_put_failure: |
1442 | return -EMSGSIZE; | |
8d5ebd83 JG |
1443 | } |
1444 | ||
064af421 BP |
1445 | /* Initializes the flow module. |
1446 | * Returns zero if successful or a negative error code. */ | |
850b6b3b | 1447 | int ovs_flow_init(void) |
064af421 BP |
1448 | { |
1449 | flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0, | |
1450 | 0, NULL); | |
1451 | if (flow_cache == NULL) | |
1452 | return -ENOMEM; | |
1453 | ||
1454 | return 0; | |
1455 | } | |
1456 | ||
1457 | /* Uninitializes the flow module. */ | |
850b6b3b | 1458 | void ovs_flow_exit(void) |
064af421 BP |
1459 | { |
1460 | kmem_cache_destroy(flow_cache); | |
1461 | } |