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