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