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