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