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