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