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