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