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