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