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