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