datapath: Add support for tun_key to Open vSwitch datapath
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Copyright (c) 2007-2011 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include "flow.h"
20 #include "datapath.h"
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/icmp.h>
40 #include <linux/icmpv6.h>
41 #include <linux/rculist.h>
42 #include <net/ip.h>
43 #include <net/ipv6.h>
44 #include <net/ndisc.h>
45
46 #include "vlan.h"
47
48 static struct kmem_cache *flow_cache;
49
50 static int check_header(struct sk_buff *skb, int len)
51 {
52         if (unlikely(skb->len < len))
53                 return -EINVAL;
54         if (unlikely(!pskb_may_pull(skb, len)))
55                 return -ENOMEM;
56         return 0;
57 }
58
59 static bool arphdr_ok(struct sk_buff *skb)
60 {
61         return pskb_may_pull(skb, skb_network_offset(skb) +
62                                   sizeof(struct arp_eth_header));
63 }
64
65 static int check_iphdr(struct sk_buff *skb)
66 {
67         unsigned int nh_ofs = skb_network_offset(skb);
68         unsigned int ip_len;
69         int err;
70
71         err = check_header(skb, nh_ofs + sizeof(struct iphdr));
72         if (unlikely(err))
73                 return err;
74
75         ip_len = ip_hdrlen(skb);
76         if (unlikely(ip_len < sizeof(struct iphdr) ||
77                      skb->len < nh_ofs + ip_len))
78                 return -EINVAL;
79
80         skb_set_transport_header(skb, nh_ofs + ip_len);
81         return 0;
82 }
83
84 static bool tcphdr_ok(struct sk_buff *skb)
85 {
86         int th_ofs = skb_transport_offset(skb);
87         int tcp_len;
88
89         if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
90                 return false;
91
92         tcp_len = tcp_hdrlen(skb);
93         if (unlikely(tcp_len < sizeof(struct tcphdr) ||
94                      skb->len < th_ofs + tcp_len))
95                 return false;
96
97         return true;
98 }
99
100 static bool udphdr_ok(struct sk_buff *skb)
101 {
102         return pskb_may_pull(skb, skb_transport_offset(skb) +
103                                   sizeof(struct udphdr));
104 }
105
106 static bool icmphdr_ok(struct sk_buff *skb)
107 {
108         return pskb_may_pull(skb, skb_transport_offset(skb) +
109                                   sizeof(struct icmphdr));
110 }
111
112 u64 ovs_flow_used_time(unsigned long flow_jiffies)
113 {
114         struct timespec cur_ts;
115         u64 cur_ms, idle_ms;
116
117         ktime_get_ts(&cur_ts);
118         idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
119         cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
120                  cur_ts.tv_nsec / NSEC_PER_MSEC;
121
122         return cur_ms - idle_ms;
123 }
124
125 #define SW_FLOW_KEY_OFFSET(field)               \
126         (offsetof(struct sw_flow_key, field) +  \
127          FIELD_SIZEOF(struct sw_flow_key, field))
128
129 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
130                          int *key_lenp)
131 {
132         unsigned int nh_ofs = skb_network_offset(skb);
133         unsigned int nh_len;
134         int payload_ofs;
135         struct ipv6hdr *nh;
136         uint8_t nexthdr;
137         __be16 frag_off;
138         int err;
139
140         *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.label);
141
142         err = check_header(skb, nh_ofs + sizeof(*nh));
143         if (unlikely(err))
144                 return err;
145
146         nh = ipv6_hdr(skb);
147         nexthdr = nh->nexthdr;
148         payload_ofs = (u8 *)(nh + 1) - skb->data;
149
150         key->ip.proto = NEXTHDR_NONE;
151         key->ip.tos = ipv6_get_dsfield(nh);
152         key->ip.ttl = nh->hop_limit;
153         key->ipv6.label = *(__be32 *)nh & htonl(IPV6_FLOWINFO_FLOWLABEL);
154         key->ipv6.addr.src = nh->saddr;
155         key->ipv6.addr.dst = nh->daddr;
156
157         payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr, &frag_off);
158         if (unlikely(payload_ofs < 0))
159                 return -EINVAL;
160
161         if (frag_off) {
162                 if (frag_off & htons(~0x7))
163                         key->ip.frag = OVS_FRAG_TYPE_LATER;
164                 else
165                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
166         }
167
168         nh_len = payload_ofs - nh_ofs;
169         skb_set_transport_header(skb, nh_ofs + nh_len);
170         key->ip.proto = nexthdr;
171         return nh_len;
172 }
173
174 static bool icmp6hdr_ok(struct sk_buff *skb)
175 {
176         return pskb_may_pull(skb, skb_transport_offset(skb) +
177                                   sizeof(struct icmp6hdr));
178 }
179
180 #define TCP_FLAGS_OFFSET 13
181 #define TCP_FLAG_MASK 0x3f
182
183 void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
184 {
185         u8 tcp_flags = 0;
186
187         if ((flow->key.eth.type == htons(ETH_P_IP) ||
188              flow->key.eth.type == htons(ETH_P_IPV6)) &&
189             flow->key.ip.proto == IPPROTO_TCP &&
190             likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
191                 u8 *tcp = (u8 *)tcp_hdr(skb);
192                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
193         }
194
195         spin_lock(&flow->lock);
196         flow->used = jiffies;
197         flow->packet_count++;
198         flow->byte_count += skb->len;
199         flow->tcp_flags |= tcp_flags;
200         spin_unlock(&flow->lock);
201 }
202
203 struct sw_flow_actions *ovs_flow_actions_alloc(const struct nlattr *actions)
204 {
205         int actions_len = nla_len(actions);
206         struct sw_flow_actions *sfa;
207
208         if (actions_len > MAX_ACTIONS_BUFSIZE)
209                 return ERR_PTR(-EINVAL);
210
211         sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
212         if (!sfa)
213                 return ERR_PTR(-ENOMEM);
214
215         sfa->actions_len = actions_len;
216         memcpy(sfa->actions, nla_data(actions), actions_len);
217         return sfa;
218 }
219
220 struct sw_flow *ovs_flow_alloc(void)
221 {
222         struct sw_flow *flow;
223
224         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
225         if (!flow)
226                 return ERR_PTR(-ENOMEM);
227
228         spin_lock_init(&flow->lock);
229         atomic_set(&flow->refcnt, 1);
230         flow->sf_acts = NULL;
231         flow->dead = false;
232
233         return flow;
234 }
235
236 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
237 {
238         hash = jhash_1word(hash, table->hash_seed);
239         return flex_array_get(table->buckets,
240                                 (hash & (table->n_buckets - 1)));
241 }
242
243 static struct flex_array *alloc_buckets(unsigned int n_buckets)
244 {
245         struct flex_array *buckets;
246         int i, err;
247
248         buckets = flex_array_alloc(sizeof(struct hlist_head *),
249                                    n_buckets, GFP_KERNEL);
250         if (!buckets)
251                 return NULL;
252
253         err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
254         if (err) {
255                 flex_array_free(buckets);
256                 return NULL;
257         }
258
259         for (i = 0; i < n_buckets; i++)
260                 INIT_HLIST_HEAD((struct hlist_head *)
261                                         flex_array_get(buckets, i));
262
263         return buckets;
264 }
265
266 static void free_buckets(struct flex_array *buckets)
267 {
268         flex_array_free(buckets);
269 }
270
271 struct flow_table *ovs_flow_tbl_alloc(int new_size)
272 {
273         struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
274
275         if (!table)
276                 return NULL;
277
278         table->buckets = alloc_buckets(new_size);
279
280         if (!table->buckets) {
281                 kfree(table);
282                 return NULL;
283         }
284         table->n_buckets = new_size;
285         table->count = 0;
286         table->node_ver = 0;
287         table->keep_flows = false;
288         get_random_bytes(&table->hash_seed, sizeof(u32));
289
290         return table;
291 }
292
293 static void flow_free(struct sw_flow *flow)
294 {
295         flow->dead = true;
296         ovs_flow_put(flow);
297 }
298
299 void ovs_flow_tbl_destroy(struct flow_table *table)
300 {
301         int i;
302
303         if (!table)
304                 return;
305
306         if (table->keep_flows)
307                 goto skip_flows;
308
309         for (i = 0; i < table->n_buckets; i++) {
310                 struct sw_flow *flow;
311                 struct hlist_head *head = flex_array_get(table->buckets, i);
312                 struct hlist_node *node, *n;
313                 int ver = table->node_ver;
314
315                 hlist_for_each_entry_safe(flow, node, n, head, hash_node[ver]) {
316                         hlist_del_rcu(&flow->hash_node[ver]);
317                         flow_free(flow);
318                 }
319         }
320
321 skip_flows:
322         free_buckets(table->buckets);
323         kfree(table);
324 }
325
326 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
327 {
328         struct flow_table *table = container_of(rcu, struct flow_table, rcu);
329
330         ovs_flow_tbl_destroy(table);
331 }
332
333 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
334 {
335         if (!table)
336                 return;
337
338         call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
339 }
340
341 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
342 {
343         struct sw_flow *flow;
344         struct hlist_head *head;
345         struct hlist_node *n;
346         int ver;
347         int i;
348
349         ver = table->node_ver;
350         while (*bucket < table->n_buckets) {
351                 i = 0;
352                 head = flex_array_get(table->buckets, *bucket);
353                 hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
354                         if (i < *last) {
355                                 i++;
356                                 continue;
357                         }
358                         *last = i + 1;
359                         return flow;
360                 }
361                 (*bucket)++;
362                 *last = 0;
363         }
364
365         return NULL;
366 }
367
368 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
369 {
370         int old_ver;
371         int i;
372
373         old_ver = old->node_ver;
374         new->node_ver = !old_ver;
375
376         /* Insert in new table. */
377         for (i = 0; i < old->n_buckets; i++) {
378                 struct sw_flow *flow;
379                 struct hlist_head *head;
380                 struct hlist_node *n;
381
382                 head = flex_array_get(old->buckets, i);
383
384                 hlist_for_each_entry(flow, n, head, hash_node[old_ver])
385                         ovs_flow_tbl_insert(new, flow);
386         }
387         old->keep_flows = true;
388 }
389
390 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
391 {
392         struct flow_table *new_table;
393
394         new_table = ovs_flow_tbl_alloc(n_buckets);
395         if (!new_table)
396                 return ERR_PTR(-ENOMEM);
397
398         flow_table_copy_flows(table, new_table);
399
400         return new_table;
401 }
402
403 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
404 {
405         return __flow_tbl_rehash(table, table->n_buckets);
406 }
407
408 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
409 {
410         return __flow_tbl_rehash(table, table->n_buckets * 2);
411 }
412
413 /* RCU callback used by ovs_flow_deferred_free. */
414 static void rcu_free_flow_callback(struct rcu_head *rcu)
415 {
416         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
417
418         flow->dead = true;
419         ovs_flow_put(flow);
420 }
421
422 /* Schedules 'flow' to be freed after the next RCU grace period.
423  * The caller must hold rcu_read_lock for this to be sensible. */
424 void ovs_flow_deferred_free(struct sw_flow *flow)
425 {
426         call_rcu(&flow->rcu, rcu_free_flow_callback);
427 }
428
429 void ovs_flow_hold(struct sw_flow *flow)
430 {
431         atomic_inc(&flow->refcnt);
432 }
433
434 void ovs_flow_put(struct sw_flow *flow)
435 {
436         if (unlikely(!flow))
437                 return;
438
439         if (atomic_dec_and_test(&flow->refcnt)) {
440                 kfree((struct sf_flow_acts __force *)flow->sf_acts);
441                 kmem_cache_free(flow_cache, flow);
442         }
443 }
444
445 /* RCU callback used by ovs_flow_deferred_free_acts. */
446 static void rcu_free_acts_callback(struct rcu_head *rcu)
447 {
448         struct sw_flow_actions *sf_acts = container_of(rcu,
449                         struct sw_flow_actions, rcu);
450         kfree(sf_acts);
451 }
452
453 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
454  * The caller must hold rcu_read_lock for this to be sensible. */
455 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
456 {
457         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
458 }
459
460 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
461 {
462         struct qtag_prefix {
463                 __be16 eth_type; /* ETH_P_8021Q */
464                 __be16 tci;
465         };
466         struct qtag_prefix *qp;
467
468         if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
469                 return 0;
470
471         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
472                                          sizeof(__be16))))
473                 return -ENOMEM;
474
475         qp = (struct qtag_prefix *) skb->data;
476         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
477         __skb_pull(skb, sizeof(struct qtag_prefix));
478
479         return 0;
480 }
481
482 static __be16 parse_ethertype(struct sk_buff *skb)
483 {
484         struct llc_snap_hdr {
485                 u8  dsap;  /* Always 0xAA */
486                 u8  ssap;  /* Always 0xAA */
487                 u8  ctrl;
488                 u8  oui[3];
489                 __be16 ethertype;
490         };
491         struct llc_snap_hdr *llc;
492         __be16 proto;
493
494         proto = *(__be16 *) skb->data;
495         __skb_pull(skb, sizeof(__be16));
496
497         if (ntohs(proto) >= 1536)
498                 return proto;
499
500         if (skb->len < sizeof(struct llc_snap_hdr))
501                 return htons(ETH_P_802_2);
502
503         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
504                 return htons(0);
505
506         llc = (struct llc_snap_hdr *) skb->data;
507         if (llc->dsap != LLC_SAP_SNAP ||
508             llc->ssap != LLC_SAP_SNAP ||
509             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
510                 return htons(ETH_P_802_2);
511
512         __skb_pull(skb, sizeof(struct llc_snap_hdr));
513         return llc->ethertype;
514 }
515
516 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
517                         int *key_lenp, int nh_len)
518 {
519         struct icmp6hdr *icmp = icmp6_hdr(skb);
520         int error = 0;
521         int key_len;
522
523         /* The ICMPv6 type and code fields use the 16-bit transport port
524          * fields, so we need to store them in 16-bit network byte order.
525          */
526         key->ipv6.tp.src = htons(icmp->icmp6_type);
527         key->ipv6.tp.dst = htons(icmp->icmp6_code);
528         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
529
530         if (icmp->icmp6_code == 0 &&
531             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
532              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
533                 int icmp_len = skb->len - skb_transport_offset(skb);
534                 struct nd_msg *nd;
535                 int offset;
536
537                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
538
539                 /* In order to process neighbor discovery options, we need the
540                  * entire packet.
541                  */
542                 if (unlikely(icmp_len < sizeof(*nd)))
543                         goto out;
544                 if (unlikely(skb_linearize(skb))) {
545                         error = -ENOMEM;
546                         goto out;
547                 }
548
549                 nd = (struct nd_msg *)skb_transport_header(skb);
550                 key->ipv6.nd.target = nd->target;
551                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
552
553                 icmp_len -= sizeof(*nd);
554                 offset = 0;
555                 while (icmp_len >= 8) {
556                         struct nd_opt_hdr *nd_opt =
557                                  (struct nd_opt_hdr *)(nd->opt + offset);
558                         int opt_len = nd_opt->nd_opt_len * 8;
559
560                         if (unlikely(!opt_len || opt_len > icmp_len))
561                                 goto invalid;
562
563                         /* Store the link layer address if the appropriate
564                          * option is provided.  It is considered an error if
565                          * the same link layer option is specified twice.
566                          */
567                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
568                             && opt_len == 8) {
569                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
570                                         goto invalid;
571                                 memcpy(key->ipv6.nd.sll,
572                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
573                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
574                                    && opt_len == 8) {
575                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
576                                         goto invalid;
577                                 memcpy(key->ipv6.nd.tll,
578                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
579                         }
580
581                         icmp_len -= opt_len;
582                         offset += opt_len;
583                 }
584         }
585
586         goto out;
587
588 invalid:
589         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
590         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
591         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
592
593 out:
594         *key_lenp = key_len;
595         return error;
596 }
597
598 /**
599  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
600  * @skb: sk_buff that contains the frame, with skb->data pointing to the
601  * Ethernet header
602  * @in_port: port number on which @skb was received.
603  * @key: output flow key
604  * @key_lenp: length of output flow key
605  *
606  * The caller must ensure that skb->len >= ETH_HLEN.
607  *
608  * Returns 0 if successful, otherwise a negative errno value.
609  *
610  * Initializes @skb header pointers as follows:
611  *
612  *    - skb->mac_header: the Ethernet header.
613  *
614  *    - skb->network_header: just past the Ethernet header, or just past the
615  *      VLAN header, to the first byte of the Ethernet payload.
616  *
617  *    - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
618  *      on output, then just past the IP header, if one is present and
619  *      of a correct length, otherwise the same as skb->network_header.
620  *      For other key->dl_type values it is left untouched.
621  */
622 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
623                  int *key_lenp)
624 {
625         int error = 0;
626         int key_len = SW_FLOW_KEY_OFFSET(eth);
627         struct ethhdr *eth;
628
629         memset(key, 0, sizeof(*key));
630
631         key->phy.priority = skb->priority;
632         if (OVS_CB(skb)->tun_key)
633                 memcpy(&key->tun.tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun.tun_key));
634         key->phy.in_port = in_port;
635
636         skb_reset_mac_header(skb);
637
638         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
639          * header in the linear data area.
640          */
641         eth = eth_hdr(skb);
642         memcpy(key->eth.src, eth->h_source, ETH_ALEN);
643         memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
644
645         __skb_pull(skb, 2 * ETH_ALEN);
646
647         if (vlan_tx_tag_present(skb))
648                 key->eth.tci = htons(vlan_get_tci(skb));
649         else if (eth->h_proto == htons(ETH_P_8021Q))
650                 if (unlikely(parse_vlan(skb, key)))
651                         return -ENOMEM;
652
653         key->eth.type = parse_ethertype(skb);
654         if (unlikely(key->eth.type == htons(0)))
655                 return -ENOMEM;
656
657         skb_reset_network_header(skb);
658         __skb_push(skb, skb->data - skb_mac_header(skb));
659
660         /* Network layer. */
661         if (key->eth.type == htons(ETH_P_IP)) {
662                 struct iphdr *nh;
663                 __be16 offset;
664
665                 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
666
667                 error = check_iphdr(skb);
668                 if (unlikely(error)) {
669                         if (error == -EINVAL) {
670                                 skb->transport_header = skb->network_header;
671                                 error = 0;
672                         }
673                         goto out;
674                 }
675
676                 nh = ip_hdr(skb);
677                 key->ipv4.addr.src = nh->saddr;
678                 key->ipv4.addr.dst = nh->daddr;
679
680                 key->ip.proto = nh->protocol;
681                 key->ip.tos = nh->tos;
682                 key->ip.ttl = nh->ttl;
683
684                 offset = nh->frag_off & htons(IP_OFFSET);
685                 if (offset) {
686                         key->ip.frag = OVS_FRAG_TYPE_LATER;
687                         goto out;
688                 }
689                 if (nh->frag_off & htons(IP_MF) ||
690                          skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
691                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
692
693                 /* Transport layer. */
694                 if (key->ip.proto == IPPROTO_TCP) {
695                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
696                         if (tcphdr_ok(skb)) {
697                                 struct tcphdr *tcp = tcp_hdr(skb);
698                                 key->ipv4.tp.src = tcp->source;
699                                 key->ipv4.tp.dst = tcp->dest;
700                         }
701                 } else if (key->ip.proto == IPPROTO_UDP) {
702                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
703                         if (udphdr_ok(skb)) {
704                                 struct udphdr *udp = udp_hdr(skb);
705                                 key->ipv4.tp.src = udp->source;
706                                 key->ipv4.tp.dst = udp->dest;
707                         }
708                 } else if (key->ip.proto == IPPROTO_ICMP) {
709                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
710                         if (icmphdr_ok(skb)) {
711                                 struct icmphdr *icmp = icmp_hdr(skb);
712                                 /* The ICMP type and code fields use the 16-bit
713                                  * transport port fields, so we need to store
714                                  * them in 16-bit network byte order. */
715                                 key->ipv4.tp.src = htons(icmp->type);
716                                 key->ipv4.tp.dst = htons(icmp->code);
717                         }
718                 }
719
720         } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
721                 struct arp_eth_header *arp;
722
723                 arp = (struct arp_eth_header *)skb_network_header(skb);
724
725                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
726                                 && arp->ar_pro == htons(ETH_P_IP)
727                                 && arp->ar_hln == ETH_ALEN
728                                 && arp->ar_pln == 4) {
729
730                         /* We only match on the lower 8 bits of the opcode. */
731                         if (ntohs(arp->ar_op) <= 0xff)
732                                 key->ip.proto = ntohs(arp->ar_op);
733
734                         if (key->ip.proto == ARPOP_REQUEST
735                                         || key->ip.proto == ARPOP_REPLY) {
736                                 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
737                                 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
738                                 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
739                                 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
740                                 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
741                         }
742                 }
743         } else if (key->eth.type == htons(ETH_P_IPV6)) {
744                 int nh_len;             /* IPv6 Header + Extensions */
745
746                 nh_len = parse_ipv6hdr(skb, key, &key_len);
747                 if (unlikely(nh_len < 0)) {
748                         if (nh_len == -EINVAL)
749                                 skb->transport_header = skb->network_header;
750                         else
751                                 error = nh_len;
752                         goto out;
753                 }
754
755                 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
756                         goto out;
757                 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
758                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
759
760                 /* Transport layer. */
761                 if (key->ip.proto == NEXTHDR_TCP) {
762                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
763                         if (tcphdr_ok(skb)) {
764                                 struct tcphdr *tcp = tcp_hdr(skb);
765                                 key->ipv6.tp.src = tcp->source;
766                                 key->ipv6.tp.dst = tcp->dest;
767                         }
768                 } else if (key->ip.proto == NEXTHDR_UDP) {
769                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
770                         if (udphdr_ok(skb)) {
771                                 struct udphdr *udp = udp_hdr(skb);
772                                 key->ipv6.tp.src = udp->source;
773                                 key->ipv6.tp.dst = udp->dest;
774                         }
775                 } else if (key->ip.proto == NEXTHDR_ICMP) {
776                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
777                         if (icmp6hdr_ok(skb)) {
778                                 error = parse_icmpv6(skb, key, &key_len, nh_len);
779                                 if (error < 0)
780                                         goto out;
781                         }
782                 }
783         }
784
785 out:
786         *key_lenp = key_len;
787         return error;
788 }
789
790 u32 ovs_flow_hash(const struct sw_flow_key *key, int key_len)
791 {
792         return jhash2((u32 *)key, DIV_ROUND_UP(key_len, sizeof(u32)), 0);
793 }
794
795 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
796                                 struct sw_flow_key *key, int key_len)
797 {
798         struct sw_flow *flow;
799         struct hlist_node *n;
800         struct hlist_head *head;
801         u32 hash;
802
803         hash = ovs_flow_hash(key, key_len);
804
805         head = find_bucket(table, hash);
806         hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
807
808                 if (flow->hash == hash &&
809                     !memcmp(&flow->key, key, key_len)) {
810                         return flow;
811                 }
812         }
813         return NULL;
814 }
815
816 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
817 {
818         struct hlist_head *head;
819
820         head = find_bucket(table, flow->hash);
821         hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
822         table->count++;
823 }
824
825 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
826 {
827         hlist_del_rcu(&flow->hash_node[table->node_ver]);
828         table->count--;
829         BUG_ON(table->count < 0);
830 }
831
832 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
833 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
834         [OVS_KEY_ATTR_ENCAP] = -1,
835         [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
836         [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
837         [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
838         [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
839         [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
840         [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
841         [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
842         [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
843         [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
844         [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
845         [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
846         [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
847         [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
848
849         /* Not upstream. */
850         [OVS_KEY_ATTR_TUN_ID] = sizeof(__be64),
851         [OVS_KEY_ATTR_IPV4_TUNNEL] = sizeof(struct ovs_key_ipv4_tunnel),
852 };
853
854 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
855                                   const struct nlattr *a[], u64 *attrs)
856 {
857         const struct ovs_key_icmp *icmp_key;
858         const struct ovs_key_tcp *tcp_key;
859         const struct ovs_key_udp *udp_key;
860
861         switch (swkey->ip.proto) {
862         case IPPROTO_TCP:
863                 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
864                         return -EINVAL;
865                 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
866
867                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
868                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
869                 swkey->ipv4.tp.src = tcp_key->tcp_src;
870                 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
871                 break;
872
873         case IPPROTO_UDP:
874                 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
875                         return -EINVAL;
876                 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
877
878                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
879                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
880                 swkey->ipv4.tp.src = udp_key->udp_src;
881                 swkey->ipv4.tp.dst = udp_key->udp_dst;
882                 break;
883
884         case IPPROTO_ICMP:
885                 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
886                         return -EINVAL;
887                 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
888
889                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
890                 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
891                 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
892                 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
893                 break;
894         }
895
896         return 0;
897 }
898
899 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
900                                   const struct nlattr *a[], u64 *attrs)
901 {
902         const struct ovs_key_icmpv6 *icmpv6_key;
903         const struct ovs_key_tcp *tcp_key;
904         const struct ovs_key_udp *udp_key;
905
906         switch (swkey->ip.proto) {
907         case IPPROTO_TCP:
908                 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
909                         return -EINVAL;
910                 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
911
912                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
913                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
914                 swkey->ipv6.tp.src = tcp_key->tcp_src;
915                 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
916                 break;
917
918         case IPPROTO_UDP:
919                 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
920                         return -EINVAL;
921                 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
922
923                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
924                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
925                 swkey->ipv6.tp.src = udp_key->udp_src;
926                 swkey->ipv6.tp.dst = udp_key->udp_dst;
927                 break;
928
929         case IPPROTO_ICMPV6:
930                 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
931                         return -EINVAL;
932                 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
933
934                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
935                 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
936                 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
937                 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
938
939                 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
940                     swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
941                         const struct ovs_key_nd *nd_key;
942
943                         if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
944                                 return -EINVAL;
945                         *attrs &= ~(1 << OVS_KEY_ATTR_ND);
946
947                         *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
948                         nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
949                         memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
950                                sizeof(swkey->ipv6.nd.target));
951                         memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
952                         memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
953                 }
954                 break;
955         }
956
957         return 0;
958 }
959
960 static int parse_flow_nlattrs(const struct nlattr *attr,
961                               const struct nlattr *a[], u64 *attrsp)
962 {
963         const struct nlattr *nla;
964         u64 attrs;
965         int rem;
966
967         attrs = 0;
968         nla_for_each_nested(nla, attr, rem) {
969                 u16 type = nla_type(nla);
970                 int expected_len;
971
972                 if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
973                         return -EINVAL;
974
975                 expected_len = ovs_key_lens[type];
976                 if (nla_len(nla) != expected_len && expected_len != -1)
977                         return -EINVAL;
978
979                 attrs |= 1ULL << type;
980                 a[type] = nla;
981         }
982         if (rem)
983                 return -EINVAL;
984
985         *attrsp = attrs;
986         return 0;
987 }
988
989 /**
990  * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
991  * @swkey: receives the extracted flow key.
992  * @key_lenp: number of bytes used in @swkey.
993  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
994  * sequence.
995  */
996 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
997                       const struct nlattr *attr)
998 {
999         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1000         const struct ovs_key_ethernet *eth_key;
1001         int key_len;
1002         u64 attrs;
1003         int err;
1004
1005         memset(swkey, 0, sizeof(struct sw_flow_key));
1006         key_len = SW_FLOW_KEY_OFFSET(eth);
1007
1008         err = parse_flow_nlattrs(attr, a, &attrs);
1009         if (err)
1010                 return err;
1011
1012         /* Metadata attributes. */
1013         if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1014                 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1015                 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1016         }
1017         if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1018                 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1019                 if (in_port >= DP_MAX_PORTS)
1020                         return -EINVAL;
1021                 swkey->phy.in_port = in_port;
1022                 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1023         } else {
1024                 swkey->phy.in_port = DP_MAX_PORTS;
1025         }
1026
1027         if (attrs & (1ULL << OVS_KEY_ATTR_TUN_ID) &&
1028             attrs & (1ULL << OVS_KEY_ATTR_IPV4_TUNNEL)) {
1029                 struct ovs_key_ipv4_tunnel *tun_key;
1030                 __be64 tun_id;
1031
1032                 tun_key = nla_data(a[OVS_KEY_ATTR_IPV4_TUNNEL]);
1033
1034                 if (!tun_key->ipv4_dst)
1035                         return -EINVAL;
1036                 if (!(tun_key->tun_flags & OVS_FLOW_TNL_F_KEY))
1037                         return -EINVAL;
1038
1039                 tun_id = nla_get_be64(a[OVS_KEY_ATTR_TUN_ID]);
1040                 if (tun_id != tun_key->tun_id)
1041                         return -EINVAL;
1042
1043                 memcpy(&swkey->tun.tun_key, tun_key, sizeof(swkey->tun.tun_key));
1044                 attrs &= ~(1ULL << OVS_KEY_ATTR_TUN_ID);
1045                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4_TUNNEL);
1046         } else if (attrs & (1ULL << OVS_KEY_ATTR_TUN_ID)) {
1047                 swkey->tun.tun_key.tun_id = nla_get_be64(a[OVS_KEY_ATTR_TUN_ID]);
1048                 swkey->tun.tun_key.tun_flags |= OVS_FLOW_TNL_F_KEY;
1049
1050                 attrs &= ~(1ULL << OVS_KEY_ATTR_TUN_ID);
1051         } else if (attrs & (1ULL << OVS_KEY_ATTR_IPV4_TUNNEL)) {
1052                 struct ovs_key_ipv4_tunnel *tun_key;
1053                 tun_key = nla_data(a[OVS_KEY_ATTR_IPV4_TUNNEL]);
1054
1055                 if (!tun_key->ipv4_dst)
1056                         return -EINVAL;
1057
1058                 memcpy(&swkey->tun.tun_key, tun_key, sizeof(swkey->tun.tun_key));
1059                 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4_TUNNEL);
1060         }
1061
1062         /* Data attributes. */
1063         if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1064                 return -EINVAL;
1065         attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1066
1067         eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1068         memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1069         memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1070
1071         if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1072             nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1073                 const struct nlattr *encap;
1074                 __be16 tci;
1075
1076                 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1077                               (1 << OVS_KEY_ATTR_ETHERTYPE) |
1078                               (1 << OVS_KEY_ATTR_ENCAP)))
1079                         return -EINVAL;
1080
1081                 encap = a[OVS_KEY_ATTR_ENCAP];
1082                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1083                 if (tci & htons(VLAN_TAG_PRESENT)) {
1084                         swkey->eth.tci = tci;
1085
1086                         err = parse_flow_nlattrs(encap, a, &attrs);
1087                         if (err)
1088                                 return err;
1089                 } else if (!tci) {
1090                         /* Corner case for truncated 802.1Q header. */
1091                         if (nla_len(encap))
1092                                 return -EINVAL;
1093
1094                         swkey->eth.type = htons(ETH_P_8021Q);
1095                         *key_lenp = key_len;
1096                         return 0;
1097                 } else {
1098                         return -EINVAL;
1099                 }
1100         }
1101
1102         if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1103                 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1104                 if (ntohs(swkey->eth.type) < 1536)
1105                         return -EINVAL;
1106                 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1107         } else {
1108                 swkey->eth.type = htons(ETH_P_802_2);
1109         }
1110
1111         if (swkey->eth.type == htons(ETH_P_IP)) {
1112                 const struct ovs_key_ipv4 *ipv4_key;
1113
1114                 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1115                         return -EINVAL;
1116                 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1117
1118                 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1119                 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1120                 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1121                         return -EINVAL;
1122                 swkey->ip.proto = ipv4_key->ipv4_proto;
1123                 swkey->ip.tos = ipv4_key->ipv4_tos;
1124                 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1125                 swkey->ip.frag = ipv4_key->ipv4_frag;
1126                 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1127                 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1128
1129                 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1130                         err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1131                         if (err)
1132                                 return err;
1133                 }
1134         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1135                 const struct ovs_key_ipv6 *ipv6_key;
1136
1137                 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1138                         return -EINVAL;
1139                 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1140
1141                 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1142                 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1143                 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1144                         return -EINVAL;
1145                 swkey->ipv6.label = ipv6_key->ipv6_label;
1146                 swkey->ip.proto = ipv6_key->ipv6_proto;
1147                 swkey->ip.tos = ipv6_key->ipv6_tclass;
1148                 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1149                 swkey->ip.frag = ipv6_key->ipv6_frag;
1150                 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1151                        sizeof(swkey->ipv6.addr.src));
1152                 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1153                        sizeof(swkey->ipv6.addr.dst));
1154
1155                 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1156                         err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1157                         if (err)
1158                                 return err;
1159                 }
1160         } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1161                 const struct ovs_key_arp *arp_key;
1162
1163                 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1164                         return -EINVAL;
1165                 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1166
1167                 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1168                 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1169                 swkey->ipv4.addr.src = arp_key->arp_sip;
1170                 swkey->ipv4.addr.dst = arp_key->arp_tip;
1171                 if (arp_key->arp_op & htons(0xff00))
1172                         return -EINVAL;
1173                 swkey->ip.proto = ntohs(arp_key->arp_op);
1174                 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1175                 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1176         }
1177
1178         if (attrs)
1179                 return -EINVAL;
1180         *key_lenp = key_len;
1181
1182         return 0;
1183 }
1184
1185 /**
1186  * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1187  * @in_port: receives the extracted input port.
1188  * @tun_id: receives the extracted tunnel ID.
1189  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1190  * sequence.
1191  *
1192  * This parses a series of Netlink attributes that form a flow key, which must
1193  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1194  * get the metadata, that is, the parts of the flow key that cannot be
1195  * extracted from the packet itself.
1196  */
1197 int ovs_flow_metadata_from_nlattrs(u32 *priority, u16 *in_port,
1198                                    struct ovs_key_ipv4_tunnel *tun_key,
1199                                    const struct nlattr *attr)
1200 {
1201         const struct nlattr *nla;
1202         int rem;
1203         __be64 tun_id;
1204
1205         *in_port = DP_MAX_PORTS;
1206         memset(tun_key, 0, sizeof(*tun_key));
1207         *priority = 0;
1208
1209         nla_for_each_nested(nla, attr, rem) {
1210                 int type = nla_type(nla);
1211
1212                 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1213                         if (nla_len(nla) != ovs_key_lens[type])
1214                                 return -EINVAL;
1215
1216                         switch (type) {
1217                         case OVS_KEY_ATTR_PRIORITY:
1218                                 *priority = nla_get_u32(nla);
1219                                 break;
1220
1221                         case OVS_KEY_ATTR_TUN_ID:
1222                                 tun_id = nla_get_be64(nla);
1223
1224                                 if (tun_key->ipv4_dst) {
1225                                         if (!(tun_key->tun_flags & OVS_FLOW_TNL_F_KEY))
1226                                                 return -EINVAL;
1227                                         if (tun_key->tun_id != tun_id)
1228                                                 return -EINVAL;
1229                                         break;
1230                                 }
1231                                 tun_key->tun_id = tun_id;
1232                                 tun_key->tun_flags |= OVS_FLOW_TNL_F_KEY;
1233
1234                                 break;
1235
1236                         case OVS_KEY_ATTR_IPV4_TUNNEL:
1237                                 if (tun_key->tun_flags & OVS_FLOW_TNL_F_KEY) {
1238                                         tun_id = tun_key->tun_id;
1239
1240                                         memcpy(tun_key, nla_data(nla), sizeof(*tun_key));
1241                                         if (!(tun_key->tun_flags & OVS_FLOW_TNL_F_KEY))
1242                                                 return -EINVAL;
1243
1244                                         if (tun_key->tun_id != tun_id)
1245                                                 return -EINVAL;
1246                                 } else
1247                                         memcpy(tun_key, nla_data(nla), sizeof(*tun_key));
1248
1249                                 if (!tun_key->ipv4_dst)
1250                                         return -EINVAL;
1251                                 break;
1252
1253                         case OVS_KEY_ATTR_IN_PORT:
1254                                 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1255                                         return -EINVAL;
1256                                 *in_port = nla_get_u32(nla);
1257                                 break;
1258                         }
1259                 }
1260         }
1261         if (rem)
1262                 return -EINVAL;
1263         return 0;
1264 }
1265
1266 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1267 {
1268         struct ovs_key_ethernet *eth_key;
1269         struct nlattr *nla, *encap;
1270
1271         if (swkey->phy.priority &&
1272             nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1273                 goto nla_put_failure;
1274
1275         if (swkey->tun.tun_key.ipv4_dst) {
1276                 struct ovs_key_ipv4_tunnel *tun_key;
1277                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4_TUNNEL, sizeof(*tun_key));
1278                 if (!nla)
1279                         goto nla_put_failure;
1280                 tun_key = nla_data(nla);
1281                 memcpy(tun_key, &swkey->tun.tun_key, sizeof(*tun_key));
1282         }
1283         if ((swkey->tun.tun_key.tun_flags & OVS_FLOW_TNL_F_KEY) &&
1284             nla_put_be64(skb, OVS_KEY_ATTR_TUN_ID, swkey->tun.tun_key.tun_id))
1285                 goto nla_put_failure;
1286
1287         if (swkey->phy.in_port != DP_MAX_PORTS &&
1288             nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1289                 goto nla_put_failure;
1290
1291         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1292         if (!nla)
1293                 goto nla_put_failure;
1294         eth_key = nla_data(nla);
1295         memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1296         memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1297
1298         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1299                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1300                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1301                         goto nla_put_failure;
1302                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1303                 if (!swkey->eth.tci)
1304                         goto unencap;
1305         } else {
1306                 encap = NULL;
1307         }
1308
1309         if (swkey->eth.type == htons(ETH_P_802_2))
1310                 goto unencap;
1311
1312         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1313                 goto nla_put_failure;
1314
1315         if (swkey->eth.type == htons(ETH_P_IP)) {
1316                 struct ovs_key_ipv4 *ipv4_key;
1317
1318                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1319                 if (!nla)
1320                         goto nla_put_failure;
1321                 ipv4_key = nla_data(nla);
1322                 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1323                 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1324                 ipv4_key->ipv4_proto = swkey->ip.proto;
1325                 ipv4_key->ipv4_tos = swkey->ip.tos;
1326                 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1327                 ipv4_key->ipv4_frag = swkey->ip.frag;
1328         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1329                 struct ovs_key_ipv6 *ipv6_key;
1330
1331                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1332                 if (!nla)
1333                         goto nla_put_failure;
1334                 ipv6_key = nla_data(nla);
1335                 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1336                                 sizeof(ipv6_key->ipv6_src));
1337                 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1338                                 sizeof(ipv6_key->ipv6_dst));
1339                 ipv6_key->ipv6_label = swkey->ipv6.label;
1340                 ipv6_key->ipv6_proto = swkey->ip.proto;
1341                 ipv6_key->ipv6_tclass = swkey->ip.tos;
1342                 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1343                 ipv6_key->ipv6_frag = swkey->ip.frag;
1344         } else if (swkey->eth.type == htons(ETH_P_ARP)) {
1345                 struct ovs_key_arp *arp_key;
1346
1347                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1348                 if (!nla)
1349                         goto nla_put_failure;
1350                 arp_key = nla_data(nla);
1351                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1352                 arp_key->arp_sip = swkey->ipv4.addr.src;
1353                 arp_key->arp_tip = swkey->ipv4.addr.dst;
1354                 arp_key->arp_op = htons(swkey->ip.proto);
1355                 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1356                 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1357         }
1358
1359         if ((swkey->eth.type == htons(ETH_P_IP) ||
1360              swkey->eth.type == htons(ETH_P_IPV6)) &&
1361              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1362
1363                 if (swkey->ip.proto == IPPROTO_TCP) {
1364                         struct ovs_key_tcp *tcp_key;
1365
1366                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1367                         if (!nla)
1368                                 goto nla_put_failure;
1369                         tcp_key = nla_data(nla);
1370                         if (swkey->eth.type == htons(ETH_P_IP)) {
1371                                 tcp_key->tcp_src = swkey->ipv4.tp.src;
1372                                 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1373                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1374                                 tcp_key->tcp_src = swkey->ipv6.tp.src;
1375                                 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1376                         }
1377                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1378                         struct ovs_key_udp *udp_key;
1379
1380                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1381                         if (!nla)
1382                                 goto nla_put_failure;
1383                         udp_key = nla_data(nla);
1384                         if (swkey->eth.type == htons(ETH_P_IP)) {
1385                                 udp_key->udp_src = swkey->ipv4.tp.src;
1386                                 udp_key->udp_dst = swkey->ipv4.tp.dst;
1387                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1388                                 udp_key->udp_src = swkey->ipv6.tp.src;
1389                                 udp_key->udp_dst = swkey->ipv6.tp.dst;
1390                         }
1391                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1392                            swkey->ip.proto == IPPROTO_ICMP) {
1393                         struct ovs_key_icmp *icmp_key;
1394
1395                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1396                         if (!nla)
1397                                 goto nla_put_failure;
1398                         icmp_key = nla_data(nla);
1399                         icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1400                         icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1401                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1402                            swkey->ip.proto == IPPROTO_ICMPV6) {
1403                         struct ovs_key_icmpv6 *icmpv6_key;
1404
1405                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1406                                                 sizeof(*icmpv6_key));
1407                         if (!nla)
1408                                 goto nla_put_failure;
1409                         icmpv6_key = nla_data(nla);
1410                         icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1411                         icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1412
1413                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1414                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1415                                 struct ovs_key_nd *nd_key;
1416
1417                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1418                                 if (!nla)
1419                                         goto nla_put_failure;
1420                                 nd_key = nla_data(nla);
1421                                 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1422                                                         sizeof(nd_key->nd_target));
1423                                 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1424                                 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1425                         }
1426                 }
1427         }
1428
1429 unencap:
1430         if (encap)
1431                 nla_nest_end(skb, encap);
1432
1433         return 0;
1434
1435 nla_put_failure:
1436         return -EMSGSIZE;
1437 }
1438
1439 /* Initializes the flow module.
1440  * Returns zero if successful or a negative error code. */
1441 int ovs_flow_init(void)
1442 {
1443         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1444                                         0, NULL);
1445         if (flow_cache == NULL)
1446                 return -ENOMEM;
1447
1448         return 0;
1449 }
1450
1451 /* Uninitializes the flow module. */
1452 void ovs_flow_exit(void)
1453 {
1454         kmem_cache_destroy(flow_cache);
1455 }