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