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