blob: f9f211d95ebe379f77d91aaa041009102ffc5411 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Raju Subramaniancaf2ee12012-05-03 18:55:23 -07002 * Copyright (c) 2007-2011 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
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>
Jesse Grossccb13522011-10-25 19:26:31 -070035#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
46static struct kmem_cache *flow_cache;
47
48static 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
57static 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
63static 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
82static 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
98static bool udphdr_ok(struct sk_buff *skb)
99{
100 return pskb_may_pull(skb, skb_transport_offset(skb) +
101 sizeof(struct udphdr));
102}
103
104static bool icmphdr_ok(struct sk_buff *skb)
105{
106 return pskb_may_pull(skb, skb_transport_offset(skb) +
107 sizeof(struct icmphdr));
108}
109
110u64 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
127static 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
172static 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
181void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
182{
183 u8 tcp_flags = 0;
184
Jesse Grossc55177e2012-04-02 15:13:36 -0700185 if ((flow->key.eth.type == htons(ETH_P_IP) ||
186 flow->key.eth.type == htons(ETH_P_IPV6)) &&
Jesse Grossbf32fec2012-04-02 14:26:27 -0700187 flow->key.ip.proto == IPPROTO_TCP &&
188 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
Jesse Grossccb13522011-10-25 19:26:31 -0700189 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
201struct 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
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700206 if (actions_len > MAX_ACTIONS_BUFSIZE)
Jesse Grossccb13522011-10-25 19:26:31 -0700207 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
218struct 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
232static 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
239static 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
262static void free_buckets(struct flex_array *buckets)
263{
264 flex_array_free(buckets);
265}
266
267struct 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
289void 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);
302 struct hlist_node *node, *n;
303 int ver = table->node_ver;
304
305 hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
306 hlist_del_rcu(&flow->hash_node[ver]);
307 ovs_flow_free(flow);
308 }
309 }
310
311skip_flows:
312 free_buckets(table->buckets);
313 kfree(table);
314}
315
316static 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
323void 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
331struct 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;
335 struct hlist_node *n;
336 int ver;
337 int i;
338
339 ver = table->node_ver;
340 while (*bucket < table->n_buckets) {
341 i = 0;
342 head = flex_array_get(table->buckets, *bucket);
343 hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
344 if (i < *last) {
345 i++;
346 continue;
347 }
348 *last = i + 1;
349 return flow;
350 }
351 (*bucket)++;
352 *last = 0;
353 }
354
355 return NULL;
356}
357
358static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
359{
360 int old_ver;
361 int i;
362
363 old_ver = old->node_ver;
364 new->node_ver = !old_ver;
365
366 /* Insert in new table. */
367 for (i = 0; i < old->n_buckets; i++) {
368 struct sw_flow *flow;
369 struct hlist_head *head;
370 struct hlist_node *n;
371
372 head = flex_array_get(old->buckets, i);
373
374 hlist_for_each_entry(flow, n, head, hash_node[old_ver])
375 ovs_flow_tbl_insert(new, flow);
376 }
377 old->keep_flows = true;
378}
379
380static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
381{
382 struct flow_table *new_table;
383
384 new_table = ovs_flow_tbl_alloc(n_buckets);
385 if (!new_table)
386 return ERR_PTR(-ENOMEM);
387
388 flow_table_copy_flows(table, new_table);
389
390 return new_table;
391}
392
393struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
394{
395 return __flow_tbl_rehash(table, table->n_buckets);
396}
397
398struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
399{
400 return __flow_tbl_rehash(table, table->n_buckets * 2);
401}
402
403void ovs_flow_free(struct sw_flow *flow)
404{
405 if (unlikely(!flow))
406 return;
407
408 kfree((struct sf_flow_acts __force *)flow->sf_acts);
409 kmem_cache_free(flow_cache, flow);
410}
411
412/* RCU callback used by ovs_flow_deferred_free. */
413static void rcu_free_flow_callback(struct rcu_head *rcu)
414{
415 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
416
417 ovs_flow_free(flow);
418}
419
420/* Schedules 'flow' to be freed after the next RCU grace period.
421 * The caller must hold rcu_read_lock for this to be sensible. */
422void ovs_flow_deferred_free(struct sw_flow *flow)
423{
424 call_rcu(&flow->rcu, rcu_free_flow_callback);
425}
426
427/* RCU callback used by ovs_flow_deferred_free_acts. */
428static void rcu_free_acts_callback(struct rcu_head *rcu)
429{
430 struct sw_flow_actions *sf_acts = container_of(rcu,
431 struct sw_flow_actions, rcu);
432 kfree(sf_acts);
433}
434
435/* Schedules 'sf_acts' to be freed after the next RCU grace period.
436 * The caller must hold rcu_read_lock for this to be sensible. */
437void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
438{
439 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
440}
441
442static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
443{
444 struct qtag_prefix {
445 __be16 eth_type; /* ETH_P_8021Q */
446 __be16 tci;
447 };
448 struct qtag_prefix *qp;
449
450 if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
451 return 0;
452
453 if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
454 sizeof(__be16))))
455 return -ENOMEM;
456
457 qp = (struct qtag_prefix *) skb->data;
458 key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
459 __skb_pull(skb, sizeof(struct qtag_prefix));
460
461 return 0;
462}
463
464static __be16 parse_ethertype(struct sk_buff *skb)
465{
466 struct llc_snap_hdr {
467 u8 dsap; /* Always 0xAA */
468 u8 ssap; /* Always 0xAA */
469 u8 ctrl;
470 u8 oui[3];
471 __be16 ethertype;
472 };
473 struct llc_snap_hdr *llc;
474 __be16 proto;
475
476 proto = *(__be16 *) skb->data;
477 __skb_pull(skb, sizeof(__be16));
478
479 if (ntohs(proto) >= 1536)
480 return proto;
481
482 if (skb->len < sizeof(struct llc_snap_hdr))
483 return htons(ETH_P_802_2);
484
485 if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
486 return htons(0);
487
488 llc = (struct llc_snap_hdr *) skb->data;
489 if (llc->dsap != LLC_SAP_SNAP ||
490 llc->ssap != LLC_SAP_SNAP ||
491 (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
492 return htons(ETH_P_802_2);
493
494 __skb_pull(skb, sizeof(struct llc_snap_hdr));
495 return llc->ethertype;
496}
497
498static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
499 int *key_lenp, int nh_len)
500{
501 struct icmp6hdr *icmp = icmp6_hdr(skb);
502 int error = 0;
503 int key_len;
504
505 /* The ICMPv6 type and code fields use the 16-bit transport port
506 * fields, so we need to store them in 16-bit network byte order.
507 */
508 key->ipv6.tp.src = htons(icmp->icmp6_type);
509 key->ipv6.tp.dst = htons(icmp->icmp6_code);
510 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
511
512 if (icmp->icmp6_code == 0 &&
513 (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
514 icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
515 int icmp_len = skb->len - skb_transport_offset(skb);
516 struct nd_msg *nd;
517 int offset;
518
519 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
520
521 /* In order to process neighbor discovery options, we need the
522 * entire packet.
523 */
524 if (unlikely(icmp_len < sizeof(*nd)))
525 goto out;
526 if (unlikely(skb_linearize(skb))) {
527 error = -ENOMEM;
528 goto out;
529 }
530
531 nd = (struct nd_msg *)skb_transport_header(skb);
532 key->ipv6.nd.target = nd->target;
533 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
534
535 icmp_len -= sizeof(*nd);
536 offset = 0;
537 while (icmp_len >= 8) {
538 struct nd_opt_hdr *nd_opt =
539 (struct nd_opt_hdr *)(nd->opt + offset);
540 int opt_len = nd_opt->nd_opt_len * 8;
541
542 if (unlikely(!opt_len || opt_len > icmp_len))
543 goto invalid;
544
545 /* Store the link layer address if the appropriate
546 * option is provided. It is considered an error if
547 * the same link layer option is specified twice.
548 */
549 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
550 && opt_len == 8) {
551 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
552 goto invalid;
553 memcpy(key->ipv6.nd.sll,
554 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
555 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
556 && opt_len == 8) {
557 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
558 goto invalid;
559 memcpy(key->ipv6.nd.tll,
560 &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
561 }
562
563 icmp_len -= opt_len;
564 offset += opt_len;
565 }
566 }
567
568 goto out;
569
570invalid:
571 memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
572 memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
573 memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
574
575out:
576 *key_lenp = key_len;
577 return error;
578}
579
580/**
581 * ovs_flow_extract - extracts a flow key from an Ethernet frame.
582 * @skb: sk_buff that contains the frame, with skb->data pointing to the
583 * Ethernet header
584 * @in_port: port number on which @skb was received.
585 * @key: output flow key
586 * @key_lenp: length of output flow key
587 *
588 * The caller must ensure that skb->len >= ETH_HLEN.
589 *
590 * Returns 0 if successful, otherwise a negative errno value.
591 *
592 * Initializes @skb header pointers as follows:
593 *
594 * - skb->mac_header: the Ethernet header.
595 *
596 * - skb->network_header: just past the Ethernet header, or just past the
597 * VLAN header, to the first byte of the Ethernet payload.
598 *
599 * - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
600 * on output, then just past the IP header, if one is present and
601 * of a correct length, otherwise the same as skb->network_header.
602 * For other key->dl_type values it is left untouched.
603 */
604int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
605 int *key_lenp)
606{
607 int error = 0;
608 int key_len = SW_FLOW_KEY_OFFSET(eth);
609 struct ethhdr *eth;
610
611 memset(key, 0, sizeof(*key));
612
613 key->phy.priority = skb->priority;
614 key->phy.in_port = in_port;
615
616 skb_reset_mac_header(skb);
617
618 /* Link layer. We are guaranteed to have at least the 14 byte Ethernet
619 * header in the linear data area.
620 */
621 eth = eth_hdr(skb);
622 memcpy(key->eth.src, eth->h_source, ETH_ALEN);
623 memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
624
625 __skb_pull(skb, 2 * ETH_ALEN);
626
627 if (vlan_tx_tag_present(skb))
628 key->eth.tci = htons(skb->vlan_tci);
629 else if (eth->h_proto == htons(ETH_P_8021Q))
630 if (unlikely(parse_vlan(skb, key)))
631 return -ENOMEM;
632
633 key->eth.type = parse_ethertype(skb);
634 if (unlikely(key->eth.type == htons(0)))
635 return -ENOMEM;
636
637 skb_reset_network_header(skb);
638 __skb_push(skb, skb->data - skb_mac_header(skb));
639
640 /* Network layer. */
641 if (key->eth.type == htons(ETH_P_IP)) {
642 struct iphdr *nh;
643 __be16 offset;
644
645 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
646
647 error = check_iphdr(skb);
648 if (unlikely(error)) {
649 if (error == -EINVAL) {
650 skb->transport_header = skb->network_header;
651 error = 0;
652 }
653 goto out;
654 }
655
656 nh = ip_hdr(skb);
657 key->ipv4.addr.src = nh->saddr;
658 key->ipv4.addr.dst = nh->daddr;
659
660 key->ip.proto = nh->protocol;
661 key->ip.tos = nh->tos;
662 key->ip.ttl = nh->ttl;
663
664 offset = nh->frag_off & htons(IP_OFFSET);
665 if (offset) {
666 key->ip.frag = OVS_FRAG_TYPE_LATER;
667 goto out;
668 }
669 if (nh->frag_off & htons(IP_MF) ||
670 skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
671 key->ip.frag = OVS_FRAG_TYPE_FIRST;
672
673 /* Transport layer. */
674 if (key->ip.proto == IPPROTO_TCP) {
675 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
676 if (tcphdr_ok(skb)) {
677 struct tcphdr *tcp = tcp_hdr(skb);
678 key->ipv4.tp.src = tcp->source;
679 key->ipv4.tp.dst = tcp->dest;
680 }
681 } else if (key->ip.proto == IPPROTO_UDP) {
682 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
683 if (udphdr_ok(skb)) {
684 struct udphdr *udp = udp_hdr(skb);
685 key->ipv4.tp.src = udp->source;
686 key->ipv4.tp.dst = udp->dest;
687 }
688 } else if (key->ip.proto == IPPROTO_ICMP) {
689 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
690 if (icmphdr_ok(skb)) {
691 struct icmphdr *icmp = icmp_hdr(skb);
692 /* The ICMP type and code fields use the 16-bit
693 * transport port fields, so we need to store
694 * them in 16-bit network byte order. */
695 key->ipv4.tp.src = htons(icmp->type);
696 key->ipv4.tp.dst = htons(icmp->code);
697 }
698 }
699
700 } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
701 struct arp_eth_header *arp;
702
703 arp = (struct arp_eth_header *)skb_network_header(skb);
704
705 if (arp->ar_hrd == htons(ARPHRD_ETHER)
706 && arp->ar_pro == htons(ETH_P_IP)
707 && arp->ar_hln == ETH_ALEN
708 && arp->ar_pln == 4) {
709
710 /* We only match on the lower 8 bits of the opcode. */
711 if (ntohs(arp->ar_op) <= 0xff)
712 key->ip.proto = ntohs(arp->ar_op);
713
714 if (key->ip.proto == ARPOP_REQUEST
715 || key->ip.proto == ARPOP_REPLY) {
716 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
717 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
718 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
719 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
720 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
721 }
722 }
723 } else if (key->eth.type == htons(ETH_P_IPV6)) {
724 int nh_len; /* IPv6 Header + Extensions */
725
726 nh_len = parse_ipv6hdr(skb, key, &key_len);
727 if (unlikely(nh_len < 0)) {
728 if (nh_len == -EINVAL)
729 skb->transport_header = skb->network_header;
730 else
731 error = nh_len;
732 goto out;
733 }
734
735 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
736 goto out;
737 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
738 key->ip.frag = OVS_FRAG_TYPE_FIRST;
739
740 /* Transport layer. */
741 if (key->ip.proto == NEXTHDR_TCP) {
742 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
743 if (tcphdr_ok(skb)) {
744 struct tcphdr *tcp = tcp_hdr(skb);
745 key->ipv6.tp.src = tcp->source;
746 key->ipv6.tp.dst = tcp->dest;
747 }
748 } else if (key->ip.proto == NEXTHDR_UDP) {
749 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
750 if (udphdr_ok(skb)) {
751 struct udphdr *udp = udp_hdr(skb);
752 key->ipv6.tp.src = udp->source;
753 key->ipv6.tp.dst = udp->dest;
754 }
755 } else if (key->ip.proto == NEXTHDR_ICMP) {
756 key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
757 if (icmp6hdr_ok(skb)) {
758 error = parse_icmpv6(skb, key, &key_len, nh_len);
759 if (error < 0)
760 goto out;
761 }
762 }
763 }
764
765out:
766 *key_lenp = key_len;
767 return error;
768}
769
770u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
771{
772 return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
773}
774
775struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
776 struct sw_flow_key *key, int key_len)
777{
778 struct sw_flow *flow;
779 struct hlist_node *n;
780 struct hlist_head *head;
781 u32 hash;
782
783 hash = ovs_flow_hash(key, key_len);
784
785 head = find_bucket(table, hash);
786 hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
787
788 if (flow->hash == hash &&
789 !memcmp(&flow->key, key, key_len)) {
790 return flow;
791 }
792 }
793 return NULL;
794}
795
796void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
797{
798 struct hlist_head *head;
799
800 head = find_bucket(table, flow->hash);
801 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
802 table->count++;
803}
804
805void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
806{
807 hlist_del_rcu(&flow->hash_node[table->node_ver]);
808 table->count--;
809 BUG_ON(table->count < 0);
810}
811
812/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
813const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
814 [OVS_KEY_ATTR_ENCAP] = -1,
815 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
816 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
817 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
818 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
819 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
820 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
821 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
822 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
823 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
824 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
825 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
826 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
827 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
828};
829
830static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
831 const struct nlattr *a[], u32 *attrs)
832{
833 const struct ovs_key_icmp *icmp_key;
834 const struct ovs_key_tcp *tcp_key;
835 const struct ovs_key_udp *udp_key;
836
837 switch (swkey->ip.proto) {
838 case IPPROTO_TCP:
839 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
840 return -EINVAL;
841 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
842
843 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
844 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
845 swkey->ipv4.tp.src = tcp_key->tcp_src;
846 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
847 break;
848
849 case IPPROTO_UDP:
850 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
851 return -EINVAL;
852 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
853
854 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
855 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
856 swkey->ipv4.tp.src = udp_key->udp_src;
857 swkey->ipv4.tp.dst = udp_key->udp_dst;
858 break;
859
860 case IPPROTO_ICMP:
861 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
862 return -EINVAL;
863 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
864
865 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
866 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
867 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
868 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
869 break;
870 }
871
872 return 0;
873}
874
875static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
876 const struct nlattr *a[], u32 *attrs)
877{
878 const struct ovs_key_icmpv6 *icmpv6_key;
879 const struct ovs_key_tcp *tcp_key;
880 const struct ovs_key_udp *udp_key;
881
882 switch (swkey->ip.proto) {
883 case IPPROTO_TCP:
884 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
885 return -EINVAL;
886 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
887
888 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
889 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
890 swkey->ipv6.tp.src = tcp_key->tcp_src;
891 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
892 break;
893
894 case IPPROTO_UDP:
895 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
896 return -EINVAL;
897 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
898
899 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
900 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
901 swkey->ipv6.tp.src = udp_key->udp_src;
902 swkey->ipv6.tp.dst = udp_key->udp_dst;
903 break;
904
905 case IPPROTO_ICMPV6:
906 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
907 return -EINVAL;
908 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
909
910 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
911 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
912 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
913 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
914
915 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
916 swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
917 const struct ovs_key_nd *nd_key;
918
919 if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
920 return -EINVAL;
921 *attrs &= ~(1 << OVS_KEY_ATTR_ND);
922
923 *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
924 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
925 memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
926 sizeof(swkey->ipv6.nd.target));
927 memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
928 memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
929 }
930 break;
931 }
932
933 return 0;
934}
935
936static int parse_flow_nlattrs(const struct nlattr *attr,
937 const struct nlattr *a[], u32 *attrsp)
938{
939 const struct nlattr *nla;
940 u32 attrs;
941 int rem;
942
943 attrs = 0;
944 nla_for_each_nested(nla, attr, rem) {
945 u16 type = nla_type(nla);
946 int expected_len;
947
948 if (type > OVS_KEY_ATTR_MAX || attrs & (1 << type))
949 return -EINVAL;
950
951 expected_len = ovs_key_lens[type];
952 if (nla_len(nla) != expected_len && expected_len != -1)
953 return -EINVAL;
954
955 attrs |= 1 << type;
956 a[type] = nla;
957 }
958 if (rem)
959 return -EINVAL;
960
961 *attrsp = attrs;
962 return 0;
963}
964
965/**
966 * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
967 * @swkey: receives the extracted flow key.
968 * @key_lenp: number of bytes used in @swkey.
969 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
970 * sequence.
971 */
972int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
973 const struct nlattr *attr)
974{
975 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
976 const struct ovs_key_ethernet *eth_key;
977 int key_len;
978 u32 attrs;
979 int err;
980
981 memset(swkey, 0, sizeof(struct sw_flow_key));
982 key_len = SW_FLOW_KEY_OFFSET(eth);
983
984 err = parse_flow_nlattrs(attr, a, &attrs);
985 if (err)
986 return err;
987
988 /* Metadata attributes. */
989 if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
990 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
991 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
992 }
993 if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
994 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
995 if (in_port >= DP_MAX_PORTS)
996 return -EINVAL;
997 swkey->phy.in_port = in_port;
998 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
999 } else {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001000 swkey->phy.in_port = DP_MAX_PORTS;
Jesse Grossccb13522011-10-25 19:26:31 -07001001 }
1002
1003 /* Data attributes. */
1004 if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1005 return -EINVAL;
1006 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1007
1008 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1009 memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1010 memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1011
1012 if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1013 nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1014 const struct nlattr *encap;
1015 __be16 tci;
1016
1017 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1018 (1 << OVS_KEY_ATTR_ETHERTYPE) |
1019 (1 << OVS_KEY_ATTR_ENCAP)))
1020 return -EINVAL;
1021
1022 encap = a[OVS_KEY_ATTR_ENCAP];
1023 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1024 if (tci & htons(VLAN_TAG_PRESENT)) {
1025 swkey->eth.tci = tci;
1026
1027 err = parse_flow_nlattrs(encap, a, &attrs);
1028 if (err)
1029 return err;
1030 } else if (!tci) {
1031 /* Corner case for truncated 802.1Q header. */
1032 if (nla_len(encap))
1033 return -EINVAL;
1034
1035 swkey->eth.type = htons(ETH_P_8021Q);
1036 *key_lenp = key_len;
1037 return 0;
1038 } else {
1039 return -EINVAL;
1040 }
1041 }
1042
1043 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1044 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1045 if (ntohs(swkey->eth.type) < 1536)
1046 return -EINVAL;
1047 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1048 } else {
1049 swkey->eth.type = htons(ETH_P_802_2);
1050 }
1051
1052 if (swkey->eth.type == htons(ETH_P_IP)) {
1053 const struct ovs_key_ipv4 *ipv4_key;
1054
1055 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1056 return -EINVAL;
1057 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1058
1059 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1060 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1061 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1062 return -EINVAL;
1063 swkey->ip.proto = ipv4_key->ipv4_proto;
1064 swkey->ip.tos = ipv4_key->ipv4_tos;
1065 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1066 swkey->ip.frag = ipv4_key->ipv4_frag;
1067 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1068 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1069
1070 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1071 err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1072 if (err)
1073 return err;
1074 }
1075 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1076 const struct ovs_key_ipv6 *ipv6_key;
1077
1078 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1079 return -EINVAL;
1080 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1081
1082 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1083 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1084 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1085 return -EINVAL;
1086 swkey->ipv6.label = ipv6_key->ipv6_label;
1087 swkey->ip.proto = ipv6_key->ipv6_proto;
1088 swkey->ip.tos = ipv6_key->ipv6_tclass;
1089 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1090 swkey->ip.frag = ipv6_key->ipv6_frag;
1091 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1092 sizeof(swkey->ipv6.addr.src));
1093 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1094 sizeof(swkey->ipv6.addr.dst));
1095
1096 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1097 err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1098 if (err)
1099 return err;
1100 }
1101 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1102 const struct ovs_key_arp *arp_key;
1103
1104 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1105 return -EINVAL;
1106 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1107
1108 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1109 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1110 swkey->ipv4.addr.src = arp_key->arp_sip;
1111 swkey->ipv4.addr.dst = arp_key->arp_tip;
1112 if (arp_key->arp_op & htons(0xff00))
1113 return -EINVAL;
1114 swkey->ip.proto = ntohs(arp_key->arp_op);
1115 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1116 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1117 }
1118
1119 if (attrs)
1120 return -EINVAL;
1121 *key_lenp = key_len;
1122
1123 return 0;
1124}
1125
1126/**
1127 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1128 * @in_port: receives the extracted input port.
1129 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1130 * sequence.
1131 *
1132 * This parses a series of Netlink attributes that form a flow key, which must
1133 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1134 * get the metadata, that is, the parts of the flow key that cannot be
1135 * extracted from the packet itself.
1136 */
1137int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 *in_port,
1138 const struct nlattr *attr)
1139{
1140 const struct nlattr *nla;
1141 int rem;
1142
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001143 *in_port = DP_MAX_PORTS;
Jesse Grossccb13522011-10-25 19:26:31 -07001144 *priority = 0;
1145
1146 nla_for_each_nested(nla, attr, rem) {
1147 int type = nla_type(nla);
1148
1149 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1150 if (nla_len(nla) != ovs_key_lens[type])
1151 return -EINVAL;
1152
1153 switch (type) {
1154 case OVS_KEY_ATTR_PRIORITY:
1155 *priority = nla_get_u32(nla);
1156 break;
1157
1158 case OVS_KEY_ATTR_IN_PORT:
1159 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1160 return -EINVAL;
1161 *in_port = nla_get_u32(nla);
1162 break;
1163 }
1164 }
1165 }
1166 if (rem)
1167 return -EINVAL;
1168 return 0;
1169}
1170
1171int 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
David S. Miller028d6a62012-03-29 23:20:48 -04001176 if (swkey->phy.priority &&
1177 nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1178 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001179
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001180 if (swkey->phy.in_port != DP_MAX_PORTS &&
David S. Miller028d6a62012-03-29 23:20:48 -04001181 nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1182 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001183
1184 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1185 if (!nla)
1186 goto nla_put_failure;
1187 eth_key = nla_data(nla);
1188 memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1189 memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1190
1191 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
David S. Miller028d6a62012-03-29 23:20:48 -04001192 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1193 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1194 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001195 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1196 if (!swkey->eth.tci)
1197 goto unencap;
1198 } else {
1199 encap = NULL;
1200 }
1201
1202 if (swkey->eth.type == htons(ETH_P_802_2))
1203 goto unencap;
1204
David S. Miller028d6a62012-03-29 23:20:48 -04001205 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1206 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001207
1208 if (swkey->eth.type == htons(ETH_P_IP)) {
1209 struct ovs_key_ipv4 *ipv4_key;
1210
1211 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1212 if (!nla)
1213 goto nla_put_failure;
1214 ipv4_key = nla_data(nla);
1215 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1216 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1217 ipv4_key->ipv4_proto = swkey->ip.proto;
1218 ipv4_key->ipv4_tos = swkey->ip.tos;
1219 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1220 ipv4_key->ipv4_frag = swkey->ip.frag;
1221 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1222 struct ovs_key_ipv6 *ipv6_key;
1223
1224 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1225 if (!nla)
1226 goto nla_put_failure;
1227 ipv6_key = nla_data(nla);
1228 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1229 sizeof(ipv6_key->ipv6_src));
1230 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1231 sizeof(ipv6_key->ipv6_dst));
1232 ipv6_key->ipv6_label = swkey->ipv6.label;
1233 ipv6_key->ipv6_proto = swkey->ip.proto;
1234 ipv6_key->ipv6_tclass = swkey->ip.tos;
1235 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1236 ipv6_key->ipv6_frag = swkey->ip.frag;
1237 } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1238 struct ovs_key_arp *arp_key;
1239
1240 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1241 if (!nla)
1242 goto nla_put_failure;
1243 arp_key = nla_data(nla);
1244 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1245 arp_key->arp_sip = swkey->ipv4.addr.src;
1246 arp_key->arp_tip = swkey->ipv4.addr.dst;
1247 arp_key->arp_op = htons(swkey->ip.proto);
1248 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1249 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1250 }
1251
1252 if ((swkey->eth.type == htons(ETH_P_IP) ||
1253 swkey->eth.type == htons(ETH_P_IPV6)) &&
1254 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1255
1256 if (swkey->ip.proto == IPPROTO_TCP) {
1257 struct ovs_key_tcp *tcp_key;
1258
1259 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1260 if (!nla)
1261 goto nla_put_failure;
1262 tcp_key = nla_data(nla);
1263 if (swkey->eth.type == htons(ETH_P_IP)) {
1264 tcp_key->tcp_src = swkey->ipv4.tp.src;
1265 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1266 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1267 tcp_key->tcp_src = swkey->ipv6.tp.src;
1268 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1269 }
1270 } else if (swkey->ip.proto == IPPROTO_UDP) {
1271 struct ovs_key_udp *udp_key;
1272
1273 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1274 if (!nla)
1275 goto nla_put_failure;
1276 udp_key = nla_data(nla);
1277 if (swkey->eth.type == htons(ETH_P_IP)) {
1278 udp_key->udp_src = swkey->ipv4.tp.src;
1279 udp_key->udp_dst = swkey->ipv4.tp.dst;
1280 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1281 udp_key->udp_src = swkey->ipv6.tp.src;
1282 udp_key->udp_dst = swkey->ipv6.tp.dst;
1283 }
1284 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1285 swkey->ip.proto == IPPROTO_ICMP) {
1286 struct ovs_key_icmp *icmp_key;
1287
1288 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1289 if (!nla)
1290 goto nla_put_failure;
1291 icmp_key = nla_data(nla);
1292 icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1293 icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1294 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1295 swkey->ip.proto == IPPROTO_ICMPV6) {
1296 struct ovs_key_icmpv6 *icmpv6_key;
1297
1298 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1299 sizeof(*icmpv6_key));
1300 if (!nla)
1301 goto nla_put_failure;
1302 icmpv6_key = nla_data(nla);
1303 icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1304 icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1305
1306 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1307 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1308 struct ovs_key_nd *nd_key;
1309
1310 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1311 if (!nla)
1312 goto nla_put_failure;
1313 nd_key = nla_data(nla);
1314 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1315 sizeof(nd_key->nd_target));
1316 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1317 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1318 }
1319 }
1320 }
1321
1322unencap:
1323 if (encap)
1324 nla_nest_end(skb, encap);
1325
1326 return 0;
1327
1328nla_put_failure:
1329 return -EMSGSIZE;
1330}
1331
1332/* Initializes the flow module.
1333 * Returns zero if successful or a negative error code. */
1334int ovs_flow_init(void)
1335{
1336 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1337 0, NULL);
1338 if (flow_cache == NULL)
1339 return -ENOMEM;
1340
1341 return 0;
1342}
1343
1344/* Uninitializes the flow module. */
1345void ovs_flow_exit(void)
1346{
1347 kmem_cache_destroy(flow_cache);
1348}