datapath: add netlink error message to help kernel userspace integration.
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Copyright (c) 2007-2013 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include "flow.h"
20 #include "datapath.h"
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/icmp.h>
40 #include <linux/icmpv6.h>
41 #include <linux/rculist.h>
42 #include <net/ip.h>
43 #include <net/ipv6.h>
44 #include <net/ndisc.h>
45
46 #include "vlan.h"
47
48 static struct kmem_cache *flow_cache;
49
50 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51                 struct sw_flow_key_range *range, u8 val);
52
53 static void update_range__(struct sw_flow_match *match,
54                           size_t offset, size_t size, bool is_mask)
55 {
56         struct sw_flow_key_range *range = NULL;
57         size_t start = offset;
58         size_t end = offset + size;
59
60         if (!is_mask)
61                 range = &match->range;
62         else if (match->mask)
63                 range = &match->mask->range;
64
65         if (!range)
66                 return;
67
68         if (range->start == range->end) {
69                 range->start = start;
70                 range->end = end;
71                 return;
72         }
73
74         if (range->start > start)
75                 range->start = start;
76
77         if (range->end < end)
78                 range->end = end;
79 }
80
81 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
82         do { \
83                 update_range__(match, offsetof(struct sw_flow_key, field),  \
84                                      sizeof((match)->key->field), is_mask); \
85                 if (is_mask && match->mask != NULL) {                       \
86                         (match)->mask->key.field = value;                   \
87                 } else {                                                    \
88                         (match)->key->field = value;                        \
89                 }                                                           \
90         } while (0)
91
92 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
93         do { \
94                 update_range__(match, offsetof(struct sw_flow_key, field),  \
95                                 len, is_mask);                              \
96                 if (is_mask && match->mask != NULL) {                       \
97                         memcpy(&(match)->mask->key.field, value_p, len);    \
98                 } else {                                                    \
99                         memcpy(&(match)->key->field, value_p, len);         \
100                 }                                                           \
101         } while (0)
102
103 void ovs_match_init(struct sw_flow_match *match,
104                     struct sw_flow_key *key,
105                     struct sw_flow_mask *mask)
106 {
107         memset(match, 0, sizeof(*match));
108         match->key = key;
109         match->mask = mask;
110
111         memset(key, 0, sizeof(*key));
112
113         if (mask) {
114                 memset(&mask->key, 0, sizeof(mask->key));
115                 mask->range.start = mask->range.end = 0;
116         }
117 }
118
119 static bool ovs_match_validate(const struct sw_flow_match *match,
120                 u64 key_attrs, u64 mask_attrs)
121 {
122         u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET;
123         u64 mask_allowed = key_attrs;  /* At most allow all key attributes */
124
125         /* The following mask attributes allowed only if they
126          * pass the validation tests. */
127         mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
128                         | (1ULL << OVS_KEY_ATTR_IPV6)
129                         | (1ULL << OVS_KEY_ATTR_TCP)
130                         | (1ULL << OVS_KEY_ATTR_UDP)
131                         | (1ULL << OVS_KEY_ATTR_ICMP)
132                         | (1ULL << OVS_KEY_ATTR_ICMPV6)
133                         | (1ULL << OVS_KEY_ATTR_ARP)
134                         | (1ULL << OVS_KEY_ATTR_ND));
135
136         if (match->key->eth.type == htons(ETH_P_802_2) &&
137             match->mask && (match->mask->key.eth.type == htons(0xffff)))
138                 mask_allowed |= (1ULL << OVS_KEY_ATTR_ETHERTYPE);
139
140         /* Check key attributes. */
141         if (match->key->eth.type == htons(ETH_P_ARP)
142                         || match->key->eth.type == htons(ETH_P_RARP)) {
143                 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
144                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
145                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
146         }
147
148         if (match->key->eth.type == htons(ETH_P_IP)) {
149                 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
150                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
151                         mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
152
153                 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
154                         if (match->key->ip.proto == IPPROTO_UDP) {
155                                 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
156                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
157                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
158                         }
159
160                         if (match->key->ip.proto == IPPROTO_TCP) {
161                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
162                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
163                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
164                         }
165
166                         if (match->key->ip.proto == IPPROTO_ICMP) {
167                                 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
168                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
169                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
170                         }
171                 }
172         }
173
174         if (match->key->eth.type == htons(ETH_P_IPV6)) {
175                 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
176                 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
177                         mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
178
179                 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
180                         if (match->key->ip.proto == IPPROTO_UDP) {
181                                 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
182                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
183                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
184                         }
185
186                         if (match->key->ip.proto == IPPROTO_TCP) {
187                                 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
188                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
189                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
190                         }
191
192                         if (match->key->ip.proto == IPPROTO_ICMPV6) {
193                                 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
194                                 if (match->mask && (match->mask->key.ip.proto == 0xff))
195                                         mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
196
197                                 if (match->key->ipv6.tp.src ==
198                                                 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
199                                     match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
200                                         key_expected |= 1ULL << OVS_KEY_ATTR_ND;
201                                         if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
202                                                 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
203                                 }
204                         }
205                 }
206         }
207
208         if ((key_attrs & key_expected) != key_expected) {
209                 /* Key attributes check failed. */
210                 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
211                                 key_attrs, key_expected);
212                 return false;
213         }
214
215         if ((mask_attrs & mask_allowed) != mask_attrs) {
216                 /* Mask attributes check failed. */
217                 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
218                                 mask_attrs, mask_allowed);
219                 return false;
220         }
221
222         return true;
223 }
224
225 static int check_header(struct sk_buff *skb, int len)
226 {
227         if (unlikely(skb->len < len))
228                 return -EINVAL;
229         if (unlikely(!pskb_may_pull(skb, len)))
230                 return -ENOMEM;
231         return 0;
232 }
233
234 static bool arphdr_ok(struct sk_buff *skb)
235 {
236         return pskb_may_pull(skb, skb_network_offset(skb) +
237                                   sizeof(struct arp_eth_header));
238 }
239
240 static int check_iphdr(struct sk_buff *skb)
241 {
242         unsigned int nh_ofs = skb_network_offset(skb);
243         unsigned int ip_len;
244         int err;
245
246         err = check_header(skb, nh_ofs + sizeof(struct iphdr));
247         if (unlikely(err))
248                 return err;
249
250         ip_len = ip_hdrlen(skb);
251         if (unlikely(ip_len < sizeof(struct iphdr) ||
252                      skb->len < nh_ofs + ip_len))
253                 return -EINVAL;
254
255         skb_set_transport_header(skb, nh_ofs + ip_len);
256         return 0;
257 }
258
259 static bool tcphdr_ok(struct sk_buff *skb)
260 {
261         int th_ofs = skb_transport_offset(skb);
262         int tcp_len;
263
264         if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
265                 return false;
266
267         tcp_len = tcp_hdrlen(skb);
268         if (unlikely(tcp_len < sizeof(struct tcphdr) ||
269                      skb->len < th_ofs + tcp_len))
270                 return false;
271
272         return true;
273 }
274
275 static bool udphdr_ok(struct sk_buff *skb)
276 {
277         return pskb_may_pull(skb, skb_transport_offset(skb) +
278                                   sizeof(struct udphdr));
279 }
280
281 static bool icmphdr_ok(struct sk_buff *skb)
282 {
283         return pskb_may_pull(skb, skb_transport_offset(skb) +
284                                   sizeof(struct icmphdr));
285 }
286
287 u64 ovs_flow_used_time(unsigned long flow_jiffies)
288 {
289         struct timespec cur_ts;
290         u64 cur_ms, idle_ms;
291
292         ktime_get_ts(&cur_ts);
293         idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
294         cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
295                  cur_ts.tv_nsec / NSEC_PER_MSEC;
296
297         return cur_ms - idle_ms;
298 }
299
300 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
301 {
302         unsigned int nh_ofs = skb_network_offset(skb);
303         unsigned int nh_len;
304         int payload_ofs;
305         struct ipv6hdr *nh;
306         uint8_t nexthdr;
307         __be16 frag_off;
308         int err;
309
310         err = check_header(skb, nh_ofs + sizeof(*nh));
311         if (unlikely(err))
312                 return err;
313
314         nh = ipv6_hdr(skb);
315         nexthdr = nh->nexthdr;
316         payload_ofs = (u8 *)(nh + 1) - skb->data;
317
318         key->ip.proto = NEXTHDR_NONE;
319         key->ip.tos = ipv6_get_dsfield(nh);
320         key->ip.ttl = nh->hop_limit;
321         key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
322         key->ipv6.addr.src = nh->saddr;
323         key->ipv6.addr.dst = nh->daddr;
324
325         payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
326         if (unlikely(payload_ofs < 0))
327                 return -EINVAL;
328
329         if (frag_off) {
330                 if (frag_off & htons(~0x7))
331                         key->ip.frag = OVS_FRAG_TYPE_LATER;
332                 else
333                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
334         }
335
336         nh_len = payload_ofs - nh_ofs;
337         skb_set_transport_header(skb, nh_ofs + nh_len);
338         key->ip.proto = nexthdr;
339         return nh_len;
340 }
341
342 static bool icmp6hdr_ok(struct sk_buff *skb)
343 {
344         return pskb_may_pull(skb, skb_transport_offset(skb) +
345                                   sizeof(struct icmp6hdr));
346 }
347
348 static void flow_key_mask(struct sw_flow_key *dst,
349                           const struct sw_flow_key *src,
350                           const struct sw_flow_mask *mask)
351 {
352         u8 *m = (u8 *)&mask->key + mask->range.start;
353         u8 *s = (u8 *)src + mask->range.start;
354         u8 *d = (u8 *)dst + mask->range.start;
355         int i;
356
357         memset(dst, 0, sizeof(*dst));
358         for (i = 0; i < ovs_sw_flow_mask_size_roundup(mask); i++) {
359                 *d = *s & *m;
360                 d++, s++, m++;
361         }
362 }
363
364 #define TCP_FLAGS_OFFSET 13
365 #define TCP_FLAG_MASK 0x3f
366
367 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
368 {
369         u8 tcp_flags = 0;
370
371         if ((flow->key.eth.type == htons(ETH_P_IP) ||
372              flow->key.eth.type == htons(ETH_P_IPV6)) &&
373             flow->key.ip.proto == IPPROTO_TCP &&
374             likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
375                 u8 *tcp = (u8 *)tcp_hdr(skb);
376                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
377         }
378
379         spin_lock(&flow->lock);
380         flow->used = jiffies;
381         flow->packet_count++;
382         flow->byte_count += skb->len;
383         flow->tcp_flags |= tcp_flags;
384         spin_unlock(&flow->lock);
385 }
386
387 struct sw_flow_actions *ovs_flow_actions_alloc(int size)
388 {
389         struct sw_flow_actions *sfa;
390
391         if (size > MAX_ACTIONS_BUFSIZE)
392                 return ERR_PTR(-EINVAL);
393
394         sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
395         if (!sfa)
396                 return ERR_PTR(-ENOMEM);
397
398         sfa->actions_len = 0;
399         return sfa;
400 }
401
402 struct sw_flow *ovs_flow_alloc(void)
403 {
404         struct sw_flow *flow;
405
406         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
407         if (!flow)
408                 return ERR_PTR(-ENOMEM);
409
410         spin_lock_init(&flow->lock);
411         flow->sf_acts = NULL;
412         flow->mask = NULL;
413
414         return flow;
415 }
416
417 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
418 {
419         hash = jhash_1word(hash, table->hash_seed);
420         return flex_array_get(table->buckets,
421                                 (hash & (table->n_buckets - 1)));
422 }
423
424 static struct flex_array *alloc_buckets(unsigned int n_buckets)
425 {
426         struct flex_array *buckets;
427         int i, err;
428
429         buckets = flex_array_alloc(sizeof(struct hlist_head *),
430                                    n_buckets, GFP_KERNEL);
431         if (!buckets)
432                 return NULL;
433
434         err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
435         if (err) {
436                 flex_array_free(buckets);
437                 return NULL;
438         }
439
440         for (i = 0; i < n_buckets; i++)
441                 INIT_HLIST_HEAD((struct hlist_head *)
442                                         flex_array_get(buckets, i));
443
444         return buckets;
445 }
446
447 static void free_buckets(struct flex_array *buckets)
448 {
449         flex_array_free(buckets);
450 }
451
452 static struct flow_table *__flow_tbl_alloc(int new_size)
453 {
454         struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
455
456         if (!table)
457                 return NULL;
458
459         table->buckets = alloc_buckets(new_size);
460
461         if (!table->buckets) {
462                 kfree(table);
463                 return NULL;
464         }
465         table->n_buckets = new_size;
466         table->count = 0;
467         table->node_ver = 0;
468         table->keep_flows = false;
469         get_random_bytes(&table->hash_seed, sizeof(u32));
470         table->mask_list = NULL;
471
472         return table;
473 }
474
475 static void __flow_tbl_destroy(struct flow_table *table)
476 {
477         int i;
478
479         if (table->keep_flows)
480                 goto skip_flows;
481
482         for (i = 0; i < table->n_buckets; i++) {
483                 struct sw_flow *flow;
484                 struct hlist_head *head = flex_array_get(table->buckets, i);
485                 struct hlist_node *n;
486                 int ver = table->node_ver;
487
488                 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
489                         hlist_del_rcu(&flow->hash_node[ver]);
490                         ovs_flow_free(flow, false);
491                 }
492         }
493
494         BUG_ON(!list_empty(table->mask_list));
495         kfree(table->mask_list);
496
497 skip_flows:
498         free_buckets(table->buckets);
499         kfree(table);
500 }
501
502 struct flow_table *ovs_flow_tbl_alloc(int new_size)
503 {
504         struct flow_table *table = __flow_tbl_alloc(new_size);
505
506         if (!table)
507                 return NULL;
508
509         table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
510         if (!table->mask_list) {
511                 table->keep_flows = true;
512                 __flow_tbl_destroy(table);
513                 return NULL;
514         }
515         INIT_LIST_HEAD(table->mask_list);
516
517         return table;
518 }
519
520 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
521 {
522         struct flow_table *table = container_of(rcu, struct flow_table, rcu);
523
524         __flow_tbl_destroy(table);
525 }
526
527 void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
528 {
529         if (!table)
530                 return;
531
532         if (deferred)
533                 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
534         else
535                 __flow_tbl_destroy(table);
536 }
537
538 struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
539 {
540         struct sw_flow *flow;
541         struct hlist_head *head;
542         int ver;
543         int i;
544
545         ver = table->node_ver;
546         while (*bucket < table->n_buckets) {
547                 i = 0;
548                 head = flex_array_get(table->buckets, *bucket);
549                 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
550                         if (i < *last) {
551                                 i++;
552                                 continue;
553                         }
554                         *last = i + 1;
555                         return flow;
556                 }
557                 (*bucket)++;
558                 *last = 0;
559         }
560
561         return NULL;
562 }
563
564 static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
565 {
566         struct hlist_head *head;
567
568         head = find_bucket(table, flow->hash);
569         hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
570
571         table->count++;
572 }
573
574 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
575 {
576         int old_ver;
577         int i;
578
579         old_ver = old->node_ver;
580         new->node_ver = !old_ver;
581
582         /* Insert in new table. */
583         for (i = 0; i < old->n_buckets; i++) {
584                 struct sw_flow *flow;
585                 struct hlist_head *head;
586
587                 head = flex_array_get(old->buckets, i);
588
589                 hlist_for_each_entry(flow, head, hash_node[old_ver])
590                         __tbl_insert(new, flow);
591         }
592
593         new->mask_list = old->mask_list;
594         old->keep_flows = true;
595 }
596
597 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
598 {
599         struct flow_table *new_table;
600
601         new_table = __flow_tbl_alloc(n_buckets);
602         if (!new_table)
603                 return ERR_PTR(-ENOMEM);
604
605         flow_table_copy_flows(table, new_table);
606
607         return new_table;
608 }
609
610 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
611 {
612         return __flow_tbl_rehash(table, table->n_buckets);
613 }
614
615 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
616 {
617         return __flow_tbl_rehash(table, table->n_buckets * 2);
618 }
619
620 static void __flow_free(struct sw_flow *flow)
621 {
622         kfree((struct sf_flow_acts __force *)flow->sf_acts);
623         kmem_cache_free(flow_cache, flow);
624 }
625
626 static void rcu_free_flow_callback(struct rcu_head *rcu)
627 {
628         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
629
630         __flow_free(flow);
631 }
632
633 void ovs_flow_free(struct sw_flow *flow, bool deferred)
634 {
635         if (!flow)
636                 return;
637
638         ovs_sw_flow_mask_del_ref((struct sw_flow_mask __force *)flow->mask,
639                                  deferred);
640
641         if (deferred)
642                 call_rcu(&flow->rcu, rcu_free_flow_callback);
643         else
644                 __flow_free(flow);
645 }
646
647 /* RCU callback used by ovs_flow_deferred_free_acts. */
648 static void rcu_free_acts_callback(struct rcu_head *rcu)
649 {
650         struct sw_flow_actions *sf_acts = container_of(rcu,
651                         struct sw_flow_actions, rcu);
652         kfree(sf_acts);
653 }
654
655 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
656  * The caller must hold rcu_read_lock for this to be sensible. */
657 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
658 {
659         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
660 }
661
662 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
663 {
664         struct qtag_prefix {
665                 __be16 eth_type; /* ETH_P_8021Q */
666                 __be16 tci;
667         };
668         struct qtag_prefix *qp;
669
670         if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
671                 return 0;
672
673         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
674                                          sizeof(__be16))))
675                 return -ENOMEM;
676
677         qp = (struct qtag_prefix *) skb->data;
678         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
679         __skb_pull(skb, sizeof(struct qtag_prefix));
680
681         return 0;
682 }
683
684 static __be16 parse_ethertype(struct sk_buff *skb)
685 {
686         struct llc_snap_hdr {
687                 u8  dsap;  /* Always 0xAA */
688                 u8  ssap;  /* Always 0xAA */
689                 u8  ctrl;
690                 u8  oui[3];
691                 __be16 ethertype;
692         };
693         struct llc_snap_hdr *llc;
694         __be16 proto;
695
696         proto = *(__be16 *) skb->data;
697         __skb_pull(skb, sizeof(__be16));
698
699         if (ntohs(proto) >= ETH_P_802_3_MIN)
700                 return proto;
701
702         if (skb->len < sizeof(struct llc_snap_hdr))
703                 return htons(ETH_P_802_2);
704
705         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
706                 return htons(0);
707
708         llc = (struct llc_snap_hdr *) skb->data;
709         if (llc->dsap != LLC_SAP_SNAP ||
710             llc->ssap != LLC_SAP_SNAP ||
711             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
712                 return htons(ETH_P_802_2);
713
714         __skb_pull(skb, sizeof(struct llc_snap_hdr));
715
716         if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
717                 return llc->ethertype;
718
719         return htons(ETH_P_802_2);
720 }
721
722 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
723                         int nh_len)
724 {
725         struct icmp6hdr *icmp = icmp6_hdr(skb);
726
727         /* The ICMPv6 type and code fields use the 16-bit transport port
728          * fields, so we need to store them in 16-bit network byte order.
729          */
730         key->ipv6.tp.src = htons(icmp->icmp6_type);
731         key->ipv6.tp.dst = htons(icmp->icmp6_code);
732
733         if (icmp->icmp6_code == 0 &&
734             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
735              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
736                 int icmp_len = skb->len - skb_transport_offset(skb);
737                 struct nd_msg *nd;
738                 int offset;
739
740                 /* In order to process neighbor discovery options, we need the
741                  * entire packet.
742                  */
743                 if (unlikely(icmp_len < sizeof(*nd)))
744                         return 0;
745
746                 if (unlikely(skb_linearize(skb)))
747                         return -ENOMEM;
748
749                 nd = (struct nd_msg *)skb_transport_header(skb);
750                 key->ipv6.nd.target = nd->target;
751
752                 icmp_len -= sizeof(*nd);
753                 offset = 0;
754                 while (icmp_len >= 8) {
755                         struct nd_opt_hdr *nd_opt =
756                                  (struct nd_opt_hdr *)(nd->opt + offset);
757                         int opt_len = nd_opt->nd_opt_len * 8;
758
759                         if (unlikely(!opt_len || opt_len > icmp_len))
760                                 return 0;
761
762                         /* Store the link layer address if the appropriate
763                          * option is provided.  It is considered an error if
764                          * the same link layer option is specified twice.
765                          */
766                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
767                             && opt_len == 8) {
768                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
769                                         goto invalid;
770                                 memcpy(key->ipv6.nd.sll,
771                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
772                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
773                                    && opt_len == 8) {
774                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
775                                         goto invalid;
776                                 memcpy(key->ipv6.nd.tll,
777                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
778                         }
779
780                         icmp_len -= opt_len;
781                         offset += opt_len;
782                 }
783         }
784
785         return 0;
786
787 invalid:
788         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
789         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
790         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
791
792         return 0;
793 }
794
795 /**
796  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
797  * @skb: sk_buff that contains the frame, with skb->data pointing to the
798  * Ethernet header
799  * @in_port: port number on which @skb was received.
800  * @key: output flow key
801  * @key_lenp: length of output flow key
802  *
803  * The caller must ensure that skb->len >= ETH_HLEN.
804  *
805  * Returns 0 if successful, otherwise a negative errno value.
806  *
807  * Initializes @skb header pointers as follows:
808  *
809  *    - skb->mac_header: the Ethernet header.
810  *
811  *    - skb->network_header: just past the Ethernet header, or just past the
812  *      VLAN header, to the first byte of the Ethernet payload.
813  *
814  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
815  *      on output, then just past the IP header, if one is present and
816  *      of a correct length, otherwise the same as skb->network_header.
817  *      For other key->eth.type values it is left untouched.
818  */
819 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
820 {
821         int error;
822         struct ethhdr *eth;
823
824         memset(key, 0, sizeof(*key));
825
826         key->phy.priority = skb->priority;
827         if (OVS_CB(skb)->tun_key)
828                 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
829         key->phy.in_port = in_port;
830         key->phy.skb_mark = skb_get_mark(skb);
831
832         skb_reset_mac_header(skb);
833
834         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
835          * header in the linear data area.
836          */
837         eth = eth_hdr(skb);
838         memcpy(key->eth.src, eth->h_source, ETH_ALEN);
839         memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
840
841         __skb_pull(skb, 2 * ETH_ALEN);
842         /* We are going to push all headers that we pull, so no need to
843          * update skb->csum here. */
844
845         if (vlan_tx_tag_present(skb))
846                 key->eth.tci = htons(vlan_get_tci(skb));
847         else if (eth->h_proto == htons(ETH_P_8021Q))
848                 if (unlikely(parse_vlan(skb, key)))
849                         return -ENOMEM;
850
851         key->eth.type = parse_ethertype(skb);
852         if (unlikely(key->eth.type == htons(0)))
853                 return -ENOMEM;
854
855         skb_reset_network_header(skb);
856         __skb_push(skb, skb->data - skb_mac_header(skb));
857
858         /* Network layer. */
859         if (key->eth.type == htons(ETH_P_IP)) {
860                 struct iphdr *nh;
861                 __be16 offset;
862
863                 error = check_iphdr(skb);
864                 if (unlikely(error)) {
865                         if (error == -EINVAL) {
866                                 skb->transport_header = skb->network_header;
867                                 error = 0;
868                         }
869                         return error;
870                 }
871
872                 nh = ip_hdr(skb);
873                 key->ipv4.addr.src = nh->saddr;
874                 key->ipv4.addr.dst = nh->daddr;
875
876                 key->ip.proto = nh->protocol;
877                 key->ip.tos = nh->tos;
878                 key->ip.ttl = nh->ttl;
879
880                 offset = nh->frag_off & htons(IP_OFFSET);
881                 if (offset) {
882                         key->ip.frag = OVS_FRAG_TYPE_LATER;
883                         return 0;
884                 }
885                 if (nh->frag_off & htons(IP_MF) ||
886                          skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
887                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
888
889                 /* Transport layer. */
890                 if (key->ip.proto == IPPROTO_TCP) {
891                         if (tcphdr_ok(skb)) {
892                                 struct tcphdr *tcp = tcp_hdr(skb);
893                                 key->ipv4.tp.src = tcp->source;
894                                 key->ipv4.tp.dst = tcp->dest;
895                         }
896                 } else if (key->ip.proto == IPPROTO_UDP) {
897                         if (udphdr_ok(skb)) {
898                                 struct udphdr *udp = udp_hdr(skb);
899                                 key->ipv4.tp.src = udp->source;
900                                 key->ipv4.tp.dst = udp->dest;
901                         }
902                 } else if (key->ip.proto == IPPROTO_ICMP) {
903                         if (icmphdr_ok(skb)) {
904                                 struct icmphdr *icmp = icmp_hdr(skb);
905                                 /* The ICMP type and code fields use the 16-bit
906                                  * transport port fields, so we need to store
907                                  * them in 16-bit network byte order. */
908                                 key->ipv4.tp.src = htons(icmp->type);
909                                 key->ipv4.tp.dst = htons(icmp->code);
910                         }
911                 }
912
913         } else if ((key->eth.type == htons(ETH_P_ARP) ||
914                    key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
915                 struct arp_eth_header *arp;
916
917                 arp = (struct arp_eth_header *)skb_network_header(skb);
918
919                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
920                                 && arp->ar_pro == htons(ETH_P_IP)
921                                 && arp->ar_hln == ETH_ALEN
922                                 && arp->ar_pln == 4) {
923
924                         /* We only match on the lower 8 bits of the opcode. */
925                         if (ntohs(arp->ar_op) <= 0xff)
926                                 key->ip.proto = ntohs(arp->ar_op);
927                         memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
928                         memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
929                         memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
930                         memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
931                 }
932         } else if (key->eth.type == htons(ETH_P_IPV6)) {
933                 int nh_len;             /* IPv6 Header + Extensions */
934
935                 nh_len = parse_ipv6hdr(skb, key);
936                 if (unlikely(nh_len < 0)) {
937                         if (nh_len == -EINVAL) {
938                                 skb->transport_header = skb->network_header;
939                                 error = 0;
940                         } else {
941                                 error = nh_len;
942                         }
943                         return error;
944                 }
945
946                 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
947                         return 0;
948                 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
949                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
950
951                 /* Transport layer. */
952                 if (key->ip.proto == NEXTHDR_TCP) {
953                         if (tcphdr_ok(skb)) {
954                                 struct tcphdr *tcp = tcp_hdr(skb);
955                                 key->ipv6.tp.src = tcp->source;
956                                 key->ipv6.tp.dst = tcp->dest;
957                         }
958                 } else if (key->ip.proto == NEXTHDR_UDP) {
959                         if (udphdr_ok(skb)) {
960                                 struct udphdr *udp = udp_hdr(skb);
961                                 key->ipv6.tp.src = udp->source;
962                                 key->ipv6.tp.dst = udp->dest;
963                         }
964                 } else if (key->ip.proto == NEXTHDR_ICMP) {
965                         if (icmp6hdr_ok(skb)) {
966                                 error = parse_icmpv6(skb, key, nh_len);
967                                 if (error)
968                                         return error;
969                         }
970                 }
971         }
972
973         return 0;
974 }
975
976 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
977 {
978         return jhash2((u32 *)((u8 *)key + key_start),
979                       DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
980 }
981
982 static int flow_key_start(const struct sw_flow_key *key)
983 {
984         if (key->tun_key.ipv4_dst)
985                 return 0;
986         else
987                 return offsetof(struct sw_flow_key, phy);
988 }
989
990 static bool __cmp_key(const struct sw_flow_key *key1,
991                 const struct sw_flow_key *key2,  int key_start, int key_len)
992 {
993         return !memcmp((u8 *)key1 + key_start,
994                         (u8 *)key2 + key_start, (key_len - key_start));
995 }
996
997 static bool __flow_cmp_key(const struct sw_flow *flow,
998                 const struct sw_flow_key *key, int key_start, int key_len)
999 {
1000         return __cmp_key(&flow->key, key, key_start, key_len);
1001 }
1002
1003 static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1004                   const struct sw_flow_key *key, int key_start, int key_len)
1005 {
1006         return __cmp_key(&flow->unmasked_key, key, key_start, key_len);
1007 }
1008
1009 bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1010                 const struct sw_flow_key *key, int key_len)
1011 {
1012         int key_start;
1013         key_start = flow_key_start(key);
1014
1015         return __flow_cmp_unmasked_key(flow, key, key_start, key_len);
1016
1017 }
1018
1019 struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1020                                        struct sw_flow_match *match)
1021 {
1022         struct sw_flow_key *unmasked = match->key;
1023         int key_len = match->range.end;
1024         struct sw_flow *flow;
1025
1026         flow = ovs_flow_lookup(table, unmasked);
1027         if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_len)))
1028                 flow = NULL;
1029
1030         return flow;
1031 }
1032
1033 static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1034                                     const struct sw_flow_key *flow_key,
1035                                     struct sw_flow_mask *mask)
1036 {
1037         struct sw_flow *flow;
1038         struct hlist_head *head;
1039         int key_start = mask->range.start;
1040         int key_len = mask->range.end;
1041         u32 hash;
1042         struct sw_flow_key masked_key;
1043
1044         flow_key_mask(&masked_key, flow_key, mask);
1045         hash = ovs_flow_hash(&masked_key, key_start, key_len);
1046         head = find_bucket(table, hash);
1047         hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1048                 if (__flow_cmp_key(flow, &masked_key, key_start, key_len))
1049                         return flow;
1050         }
1051         return NULL;
1052 }
1053
1054 struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1055                                 const struct sw_flow_key *key)
1056 {
1057         struct sw_flow *flow = NULL;
1058         struct sw_flow_mask *mask;
1059
1060         list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1061                 flow = ovs_masked_flow_lookup(tbl, key, mask);
1062                 if (flow)  /* Found */
1063                         break;
1064         }
1065
1066         return flow;
1067 }
1068
1069
1070 void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow,
1071                          const struct sw_flow_key *key, int key_len)
1072 {
1073         flow->unmasked_key = *key;
1074         flow_key_mask(&flow->key, &flow->unmasked_key, ovsl_dereference(flow->mask));
1075         flow->hash = ovs_flow_hash(&flow->key,
1076                         ovsl_dereference(flow->mask)->range.start,
1077                         ovsl_dereference(flow->mask)->range.end);
1078         __tbl_insert(table, flow);
1079 }
1080
1081 void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1082 {
1083         BUG_ON(table->count == 0);
1084         hlist_del_rcu(&flow->hash_node[table->node_ver]);
1085         table->count--;
1086 }
1087
1088 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
1089 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1090         [OVS_KEY_ATTR_ENCAP] = -1,
1091         [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1092         [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1093         [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1094         [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1095         [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1096         [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1097         [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1098         [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1099         [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1100         [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1101         [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1102         [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1103         [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1104         [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1105         [OVS_KEY_ATTR_TUNNEL] = -1,
1106 };
1107
1108 static bool is_all_zero(const u8 *fp, size_t size)
1109 {
1110         int i;
1111
1112         if (!fp)
1113                 return false;
1114
1115         for (i = 0; i < size; i++)
1116                 if (fp[i])
1117                         return false;
1118
1119         return true;
1120 }
1121
1122 static int __parse_flow_nlattrs(const struct nlattr *attr,
1123                               const struct nlattr *a[],
1124                               u64 *attrsp, bool nz)
1125 {
1126         const struct nlattr *nla;
1127         u64 attrs;
1128         int rem;
1129
1130         attrs = *attrsp;
1131         nla_for_each_nested(nla, attr, rem) {
1132                 u16 type = nla_type(nla);
1133                 int expected_len;
1134
1135                 if (type > OVS_KEY_ATTR_MAX) {
1136                         OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1137                                   type, OVS_KEY_ATTR_MAX);
1138                 }
1139
1140                 if (attrs & (1ULL << type)) {
1141                         OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1142                         return -EINVAL;
1143                 }
1144
1145                 expected_len = ovs_key_lens[type];
1146                 if (nla_len(nla) != expected_len && expected_len != -1) {
1147                         OVS_NLERR("Key attribute has unexpected length (type=%d"
1148                                   ", length=%d, expected=%d).\n", type,
1149                                   nla_len(nla), expected_len);
1150                         return -EINVAL;
1151                 }
1152
1153                 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1154                         attrs |= 1ULL << type;
1155                         a[type] = nla;
1156                 }
1157         }
1158         if (rem) {
1159                 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1160                 return -EINVAL;
1161         }
1162
1163         *attrsp = attrs;
1164         return 0;
1165 }
1166
1167 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1168                               const struct nlattr *a[], u64 *attrsp)
1169 {
1170         return __parse_flow_nlattrs(attr, a, attrsp, true);
1171 }
1172
1173 static int parse_flow_nlattrs(const struct nlattr *attr,
1174                               const struct nlattr *a[], u64 *attrsp)
1175 {
1176         return __parse_flow_nlattrs(attr, a, attrsp, false);
1177 }
1178
1179 int ipv4_tun_from_nlattr(const struct nlattr *attr,
1180                          struct sw_flow_match *match, bool is_mask)
1181 {
1182         struct nlattr *a;
1183         int rem;
1184         bool ttl = false;
1185         __be16 tun_flags = 0;
1186
1187         nla_for_each_nested(a, attr, rem) {
1188                 int type = nla_type(a);
1189                 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1190                         [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1191                         [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1192                         [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1193                         [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1194                         [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1195                         [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1196                         [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1197                 };
1198
1199                 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1200                         OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d)\n",
1201                         type, OVS_TUNNEL_KEY_ATTR_MAX);
1202                         return -EINVAL;
1203                 }
1204
1205                 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1206                         OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1207                                   " legnth (type=%d, length=%d, expected=%d.)\n",
1208                                   type, nla_len(a), ovs_tunnel_key_lens[type]);
1209                         return -EINVAL;
1210                 }
1211
1212                 switch (type) {
1213                 case OVS_TUNNEL_KEY_ATTR_ID:
1214                         SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1215                                         nla_get_be64(a), is_mask);
1216                         tun_flags |= TUNNEL_KEY;
1217                         break;
1218                 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1219                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1220                                         nla_get_be32(a), is_mask);
1221                         break;
1222                 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1223                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1224                                         nla_get_be32(a), is_mask);
1225                         break;
1226                 case OVS_TUNNEL_KEY_ATTR_TOS:
1227                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1228                                         nla_get_u8(a), is_mask);
1229                         break;
1230                 case OVS_TUNNEL_KEY_ATTR_TTL:
1231                         SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1232                                         nla_get_u8(a), is_mask);
1233                         ttl = true;
1234                         break;
1235                 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1236                         tun_flags |= TUNNEL_DONT_FRAGMENT;
1237                         break;
1238                 case OVS_TUNNEL_KEY_ATTR_CSUM:
1239                         tun_flags |= TUNNEL_CSUM;
1240                         break;
1241                 default:
1242                         return -EINVAL;
1243                 }
1244         }
1245
1246         SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1247
1248         if (rem > 0) {
1249                 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1250                 return -EINVAL;
1251         }
1252
1253         if (!match->key->tun_key.ipv4_dst) {
1254                 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1255                 return -EINVAL;
1256         }
1257
1258         if (!ttl) {
1259                 OVS_NLERR("IPv4 tunnel TTL is zero.\n");
1260                 return -EINVAL;
1261         }
1262
1263         return 0;
1264 }
1265
1266 int ipv4_tun_to_nlattr(struct sk_buff *skb,
1267                         const struct ovs_key_ipv4_tunnel *tun_key,
1268                         const struct ovs_key_ipv4_tunnel *output)
1269 {
1270         struct nlattr *nla;
1271
1272         nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1273         if (!nla)
1274                 return -EMSGSIZE;
1275
1276         if (tun_key->tun_flags & TUNNEL_KEY &&
1277             nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1278                 return -EMSGSIZE;
1279         if (tun_key->ipv4_src &&
1280             nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1281                 return -EMSGSIZE;
1282         if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1283                 return -EMSGSIZE;
1284         if (tun_key->ipv4_tos &&
1285             nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1286                 return -EMSGSIZE;
1287         if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1288                 return -EMSGSIZE;
1289         if ((tun_key->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1290                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1291                 return -EMSGSIZE;
1292         if ((tun_key->tun_flags & TUNNEL_CSUM) &&
1293                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1294                 return -EMSGSIZE;
1295
1296         nla_nest_end(skb, nla);
1297         return 0;
1298 }
1299
1300
1301 static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
1302                 const struct nlattr **a, bool is_mask)
1303 {
1304         if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1305                 SW_FLOW_KEY_PUT(match, phy.priority,
1306                           nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1307                 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1308         }
1309
1310         if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1311                 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1312
1313                 if (!is_mask && in_port >= DP_MAX_PORTS)
1314                         return -EINVAL;
1315                 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1316                 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1317         }
1318
1319         if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1320                 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1321 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1322                 if (!is_mask && mark != 0) {
1323                         OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
1324                         return -EINVAL;
1325                 }
1326 #endif
1327                 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1328                 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1329         }
1330         if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1331                 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1332                                         is_mask))
1333                         return -EINVAL;
1334                 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1335         }
1336         return 0;
1337 }
1338
1339 static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
1340                 const struct nlattr **a, bool is_mask)
1341 {
1342         int err;
1343         u64 orig_attrs = attrs;
1344
1345         err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1346         if (err)
1347                 return err;
1348
1349         if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1350                 const struct ovs_key_ethernet *eth_key;
1351
1352                 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1353                 SW_FLOW_KEY_MEMCPY(match, eth.src,
1354                                 eth_key->eth_src, ETH_ALEN, is_mask);
1355                 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1356                                 eth_key->eth_dst, ETH_ALEN, is_mask);
1357                 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1358         }
1359
1360         if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1361                 __be16 tci;
1362
1363                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1364                 if (!is_mask)
1365                         if (!(tci & htons(VLAN_TAG_PRESENT))) {
1366                                 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1367                                 return -EINVAL;
1368                         }
1369
1370                 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1371                 attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
1372         }
1373
1374         if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1375                 __be16 eth_type;
1376
1377                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1378                 if (!is_mask && ntohs(eth_type) < ETH_P_802_3_MIN) {
1379                         OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
1380                                         ntohs(eth_type), ETH_P_802_3_MIN);
1381                         return -EINVAL;
1382                 }
1383
1384                 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1385                 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1386         } else if (!is_mask) {
1387                 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1388         }
1389
1390         if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1391                 const struct ovs_key_ipv4 *ipv4_key;
1392
1393                 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1394                 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1395                         OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1396                                 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1397                         return -EINVAL;
1398                 }
1399                 SW_FLOW_KEY_PUT(match, ip.proto,
1400                                 ipv4_key->ipv4_proto, is_mask);
1401                 SW_FLOW_KEY_PUT(match, ip.tos,
1402                                 ipv4_key->ipv4_tos, is_mask);
1403                 SW_FLOW_KEY_PUT(match, ip.ttl,
1404                                 ipv4_key->ipv4_ttl, is_mask);
1405                 SW_FLOW_KEY_PUT(match, ip.frag,
1406                                 ipv4_key->ipv4_frag, is_mask);
1407                 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1408                                 ipv4_key->ipv4_src, is_mask);
1409                 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1410                                 ipv4_key->ipv4_dst, is_mask);
1411                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4);
1412         }
1413
1414         if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1415                 const struct ovs_key_ipv6 *ipv6_key;
1416
1417                 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1418                 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1419                         OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1420                                 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1421                         return -EINVAL;
1422                 }
1423                 SW_FLOW_KEY_PUT(match, ipv6.label,
1424                                 ipv6_key->ipv6_label, is_mask);
1425                 SW_FLOW_KEY_PUT(match, ip.proto,
1426                                 ipv6_key->ipv6_proto, is_mask);
1427                 SW_FLOW_KEY_PUT(match, ip.tos,
1428                                 ipv6_key->ipv6_tclass, is_mask);
1429                 SW_FLOW_KEY_PUT(match, ip.ttl,
1430                                 ipv6_key->ipv6_hlimit, is_mask);
1431                 SW_FLOW_KEY_PUT(match, ip.frag,
1432                                 ipv6_key->ipv6_frag, is_mask);
1433                 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1434                                 ipv6_key->ipv6_src,
1435                                 sizeof(match->key->ipv6.addr.src),
1436                                 is_mask);
1437                 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1438                                 ipv6_key->ipv6_dst,
1439                                 sizeof(match->key->ipv6.addr.dst),
1440                                 is_mask);
1441
1442                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1443         }
1444
1445         if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1446                 const struct ovs_key_arp *arp_key;
1447
1448                 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1449                 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1450                         OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1451                                   arp_key->arp_op);
1452                         return -EINVAL;
1453                 }
1454
1455                 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1456                                 arp_key->arp_sip, is_mask);
1457                 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1458                         arp_key->arp_tip, is_mask);
1459                 SW_FLOW_KEY_PUT(match, ip.proto,
1460                                 ntohs(arp_key->arp_op), is_mask);
1461                 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1462                                 arp_key->arp_sha, ETH_ALEN, is_mask);
1463                 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1464                                 arp_key->arp_tha, ETH_ALEN, is_mask);
1465
1466                 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1467         }
1468
1469         if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1470                 const struct ovs_key_tcp *tcp_key;
1471
1472                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1473                 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1474                         SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1475                                         tcp_key->tcp_src, is_mask);
1476                         SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1477                                         tcp_key->tcp_dst, is_mask);
1478                 } else {
1479                         SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1480                                         tcp_key->tcp_src, is_mask);
1481                         SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1482                                         tcp_key->tcp_dst, is_mask);
1483                 }
1484                 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1485         }
1486
1487         if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1488                 const struct ovs_key_udp *udp_key;
1489
1490                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1491                 if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
1492                         SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1493                                         udp_key->udp_src, is_mask);
1494                         SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1495                                         udp_key->udp_dst, is_mask);
1496                 } else {
1497                         SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1498                                         udp_key->udp_src, is_mask);
1499                         SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1500                                         udp_key->udp_dst, is_mask);
1501                 }
1502                 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1503         }
1504
1505         if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1506                 const struct ovs_key_icmp *icmp_key;
1507
1508                 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1509                 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1510                                 htons(icmp_key->icmp_type), is_mask);
1511                 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1512                                 htons(icmp_key->icmp_code), is_mask);
1513                 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1514         }
1515
1516         if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1517                 const struct ovs_key_icmpv6 *icmpv6_key;
1518
1519                 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1520                 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1521                                 htons(icmpv6_key->icmpv6_type), is_mask);
1522                 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1523                                 htons(icmpv6_key->icmpv6_code), is_mask);
1524                 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1525         }
1526
1527         if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1528                 const struct ovs_key_nd *nd_key;
1529
1530                 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1531                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1532                         nd_key->nd_target,
1533                         sizeof(match->key->ipv6.nd.target),
1534                         is_mask);
1535                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1536                         nd_key->nd_sll, ETH_ALEN, is_mask);
1537                 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1538                                 nd_key->nd_tll, ETH_ALEN, is_mask);
1539                 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1540         }
1541
1542         if (attrs != 0)
1543                 return -EINVAL;
1544
1545         return 0;
1546 }
1547
1548 /**
1549  * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1550  * mask. In case the 'mask' is NULL, the flow is treated as exact match
1551  * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1552  * does not include any don't care bit.
1553  * @match: receives the extracted flow match information.
1554  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1555  * sequence. The fields should of the packet that triggered the creation
1556  * of this flow.
1557  * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1558  * attribute specifies the mask field of the wildcarded flow.
1559  */
1560 int ovs_match_from_nlattrs(struct sw_flow_match *match,
1561                            const struct nlattr *key,
1562                            const struct nlattr *mask)
1563 {
1564         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1565         const struct nlattr *encap;
1566         u64 key_attrs = 0;
1567         u64 mask_attrs = 0;
1568         bool encap_valid = false;
1569         int err;
1570
1571         err = parse_flow_nlattrs(key, a, &key_attrs);
1572         if (err)
1573                 return err;
1574
1575         if (key_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1576                 encap = a[OVS_KEY_ATTR_ENCAP];
1577                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1578                 if (nla_len(encap)) {
1579                         __be16 eth_type = 0; /* ETH_P_8021Q */
1580
1581                         if (a[OVS_KEY_ATTR_ETHERTYPE])
1582                                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1583
1584                         if  ((eth_type == htons(ETH_P_8021Q)) && (a[OVS_KEY_ATTR_VLAN])) {
1585                                 encap_valid = true;
1586                                 key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1587                                 err = parse_flow_nlattrs(encap, a, &key_attrs);
1588                         } else {
1589                                 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1590                                 err = -EINVAL;
1591                         }
1592
1593                         if (err)
1594                                 return err;
1595                 }
1596         }
1597
1598         err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1599         if (err)
1600                 return err;
1601
1602         if (mask) {
1603                 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1604                 if (err)
1605                         return err;
1606
1607                 if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
1608                         __be16 eth_type = 0;
1609
1610                         mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
1611                         if (a[OVS_KEY_ATTR_ETHERTYPE])
1612                                 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1613                         if (eth_type == htons(0xffff)) {
1614                                 mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
1615                                 encap = a[OVS_KEY_ATTR_ENCAP];
1616                                 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1617                         } else {
1618                                 OVS_NLERR("VLAN frames must have an exact match"
1619                                          " on the TPID (mask=%x).\n",
1620                                          ntohs(eth_type));
1621                                 err = -EINVAL;
1622                         }
1623
1624                         if (err)
1625                                 return err;
1626                 }
1627
1628                 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1629                 if (err)
1630                         return err;
1631         } else {
1632                 /* Populate exact match flow's key mask. */
1633                 if (match->mask)
1634                         ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1635         }
1636
1637         if (!ovs_match_validate(match, key_attrs, mask_attrs))
1638                 return -EINVAL;
1639
1640         return 0;
1641 }
1642
1643 /**
1644  * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1645  * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1646  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1647  * sequence.
1648  *
1649  * This parses a series of Netlink attributes that form a flow key, which must
1650  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1651  * get the metadata, that is, the parts of the flow key that cannot be
1652  * extracted from the packet itself.
1653  */
1654
1655 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1656                 const struct nlattr *attr)
1657 {
1658         struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1659         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1660         u64 attrs = 0;
1661         int err;
1662         struct sw_flow_match match;
1663
1664         flow->key.phy.in_port = DP_MAX_PORTS;
1665         flow->key.phy.priority = 0;
1666         flow->key.phy.skb_mark = 0;
1667         memset(tun_key, 0, sizeof(flow->key.tun_key));
1668
1669         err = parse_flow_nlattrs(attr, a, &attrs);
1670         if (err)
1671                 return -EINVAL;
1672
1673         memset(&match, 0, sizeof(match));
1674         match.key = &flow->key;
1675
1676         err = metadata_from_nlattrs(&match, &attrs, a, false);
1677         if (err)
1678                 return err;
1679
1680         return 0;
1681 }
1682
1683 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1684                 const struct sw_flow_key *output, struct sk_buff *skb)
1685 {
1686         struct ovs_key_ethernet *eth_key;
1687         struct nlattr *nla, *encap;
1688
1689         if (swkey->phy.priority &&
1690             nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1691                 goto nla_put_failure;
1692
1693         if (swkey->tun_key.ipv4_dst &&
1694             ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1695                 goto nla_put_failure;
1696
1697         if (swkey->phy.in_port != DP_MAX_PORTS) {
1698                 /* Exact match upper 16 bits. */
1699                 u16 upper_u16;
1700                 upper_u16 = (swkey == output) ? 0 : 0xffff;
1701
1702                 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1703                                         (upper_u16 << 16) | output->phy.in_port))
1704                         goto nla_put_failure;
1705         }
1706
1707         if (swkey->phy.skb_mark &&
1708             nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1709                 goto nla_put_failure;
1710
1711         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1712         if (!nla)
1713                 goto nla_put_failure;
1714
1715         eth_key = nla_data(nla);
1716         memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1717         memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1718
1719         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1720                 __be16 eth_type;
1721                 eth_type = (swkey == output) ? htons(ETH_P_8021Q) : htons(0xffff) ;
1722                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1723                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1724                         goto nla_put_failure;
1725                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1726                 if (!swkey->eth.tci)
1727                         goto unencap;
1728         } else
1729                 encap = NULL;
1730
1731         if ((swkey == output) && (swkey->eth.type == htons(ETH_P_802_2)))
1732                 goto unencap;
1733
1734         if (output->eth.type != 0)
1735                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1736                         goto nla_put_failure;
1737
1738         if (swkey->eth.type == htons(ETH_P_IP)) {
1739                 struct ovs_key_ipv4 *ipv4_key;
1740
1741                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1742                 if (!nla)
1743                         goto nla_put_failure;
1744                 ipv4_key = nla_data(nla);
1745                 ipv4_key->ipv4_src = output->ipv4.addr.src;
1746                 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1747                 ipv4_key->ipv4_proto = output->ip.proto;
1748                 ipv4_key->ipv4_tos = output->ip.tos;
1749                 ipv4_key->ipv4_ttl = output->ip.ttl;
1750                 ipv4_key->ipv4_frag = output->ip.frag;
1751         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1752                 struct ovs_key_ipv6 *ipv6_key;
1753
1754                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1755                 if (!nla)
1756                         goto nla_put_failure;
1757                 ipv6_key = nla_data(nla);
1758                 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1759                                 sizeof(ipv6_key->ipv6_src));
1760                 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1761                                 sizeof(ipv6_key->ipv6_dst));
1762                 ipv6_key->ipv6_label = output->ipv6.label;
1763                 ipv6_key->ipv6_proto = output->ip.proto;
1764                 ipv6_key->ipv6_tclass = output->ip.tos;
1765                 ipv6_key->ipv6_hlimit = output->ip.ttl;
1766                 ipv6_key->ipv6_frag = output->ip.frag;
1767         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1768                    swkey->eth.type == htons(ETH_P_RARP)) {
1769                 struct ovs_key_arp *arp_key;
1770
1771                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1772                 if (!nla)
1773                         goto nla_put_failure;
1774                 arp_key = nla_data(nla);
1775                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1776                 arp_key->arp_sip = output->ipv4.addr.src;
1777                 arp_key->arp_tip = output->ipv4.addr.dst;
1778                 arp_key->arp_op = htons(output->ip.proto);
1779                 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1780                 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1781         }
1782
1783         if ((swkey->eth.type == htons(ETH_P_IP) ||
1784              swkey->eth.type == htons(ETH_P_IPV6)) &&
1785              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1786
1787                 if (swkey->ip.proto == IPPROTO_TCP) {
1788                         struct ovs_key_tcp *tcp_key;
1789
1790                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1791                         if (!nla)
1792                                 goto nla_put_failure;
1793                         tcp_key = nla_data(nla);
1794                         if (swkey->eth.type == htons(ETH_P_IP)) {
1795                                 tcp_key->tcp_src = output->ipv4.tp.src;
1796                                 tcp_key->tcp_dst = output->ipv4.tp.dst;
1797                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1798                                 tcp_key->tcp_src = output->ipv6.tp.src;
1799                                 tcp_key->tcp_dst = output->ipv6.tp.dst;
1800                         }
1801                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1802                         struct ovs_key_udp *udp_key;
1803
1804                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1805                         if (!nla)
1806                                 goto nla_put_failure;
1807                         udp_key = nla_data(nla);
1808                         if (swkey->eth.type == htons(ETH_P_IP)) {
1809                                 udp_key->udp_src = output->ipv4.tp.src;
1810                                 udp_key->udp_dst = output->ipv4.tp.dst;
1811                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1812                                 udp_key->udp_src = output->ipv6.tp.src;
1813                                 udp_key->udp_dst = output->ipv6.tp.dst;
1814                         }
1815                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1816                            swkey->ip.proto == IPPROTO_ICMP) {
1817                         struct ovs_key_icmp *icmp_key;
1818
1819                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1820                         if (!nla)
1821                                 goto nla_put_failure;
1822                         icmp_key = nla_data(nla);
1823                         icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1824                         icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1825                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1826                            swkey->ip.proto == IPPROTO_ICMPV6) {
1827                         struct ovs_key_icmpv6 *icmpv6_key;
1828
1829                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1830                                                 sizeof(*icmpv6_key));
1831                         if (!nla)
1832                                 goto nla_put_failure;
1833                         icmpv6_key = nla_data(nla);
1834                         icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1835                         icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1836
1837                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1838                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1839                                 struct ovs_key_nd *nd_key;
1840
1841                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1842                                 if (!nla)
1843                                         goto nla_put_failure;
1844                                 nd_key = nla_data(nla);
1845                                 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1846                                                         sizeof(nd_key->nd_target));
1847                                 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1848                                 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1849                         }
1850                 }
1851         }
1852
1853 unencap:
1854         if (encap)
1855                 nla_nest_end(skb, encap);
1856
1857         return 0;
1858
1859 nla_put_failure:
1860         return -EMSGSIZE;
1861 }
1862
1863 /* Initializes the flow module.
1864  * Returns zero if successful or a negative error code. */
1865 int ovs_flow_init(void)
1866 {
1867         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1868                                         0, NULL);
1869         if (flow_cache == NULL)
1870                 return -ENOMEM;
1871
1872         return 0;
1873 }
1874
1875 /* Uninitializes the flow module. */
1876 void ovs_flow_exit(void)
1877 {
1878         kmem_cache_destroy(flow_cache);
1879 }
1880
1881 struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
1882 {
1883         struct sw_flow_mask *mask;
1884
1885         mask = kmalloc(sizeof(*mask), GFP_KERNEL);
1886         if (mask)
1887                 mask->ref_count = 0;
1888
1889         return mask;
1890 }
1891
1892 void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
1893 {
1894         mask->ref_count++;
1895 }
1896
1897 static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
1898 {
1899         struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
1900
1901         kfree(mask);
1902 }
1903
1904 void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
1905 {
1906         if (!mask)
1907                 return;
1908
1909         BUG_ON(!mask->ref_count);
1910         mask->ref_count--;
1911
1912         if (!mask->ref_count) {
1913                 list_del_rcu(&mask->list);
1914                 if (deferred)
1915                         call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
1916                 else
1917                         kfree(mask);
1918         }
1919 }
1920
1921 static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
1922                 const struct sw_flow_mask *b)
1923 {
1924         u8 *a_ = (u8 *)&a->key + a->range.start;
1925         u8 *b_ = (u8 *)&b->key + b->range.start;
1926
1927         return  (a->range.end == b->range.end)
1928                 && (a->range.start == b->range.start)
1929                 && (memcmp(a_, b_, ovs_sw_flow_mask_actual_size(a)) == 0);
1930 }
1931
1932 struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
1933                                            const struct sw_flow_mask *mask)
1934 {
1935         struct list_head *ml;
1936
1937         list_for_each(ml, tbl->mask_list) {
1938                 struct sw_flow_mask *m;
1939                 m = container_of(ml, struct sw_flow_mask, list);
1940                 if (ovs_sw_flow_mask_equal(mask, m))
1941                         return m;
1942         }
1943
1944         return NULL;
1945 }
1946
1947 /**
1948  * add a new mask into the mask list.
1949  * The caller needs to make sure that 'mask' is not the same
1950  * as any masks that are already on the list.
1951  */
1952 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
1953 {
1954         list_add_rcu(&mask->list, tbl->mask_list);
1955 }
1956
1957 /**
1958  * Set 'range' fields in the mask to the value of 'val'.
1959  */
1960 static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
1961                 struct sw_flow_key_range *range, u8 val)
1962 {
1963         u8 *m = (u8 *)&mask->key + range->start;
1964
1965         mask->range = *range;
1966         memset(m, val, ovs_sw_flow_mask_size_roundup(mask));
1967 }