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