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