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