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