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