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