7604405c5ba49c1db257e6eb51cc00a283eb343f
[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(int size)
204 {
205         struct sw_flow_actions *sfa;
206
207         if (size > MAX_ACTIONS_BUFSIZE)
208                 return ERR_PTR(-EINVAL);
209
210         sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
211         if (!sfa)
212                 return ERR_PTR(-ENOMEM);
213
214         sfa->actions_len = 0;
215         return sfa;
216 }
217
218 struct sw_flow *ovs_flow_alloc(void)
219 {
220         struct sw_flow *flow;
221
222         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
223         if (!flow)
224                 return ERR_PTR(-ENOMEM);
225
226         spin_lock_init(&flow->lock);
227         flow->sf_acts = NULL;
228
229         return flow;
230 }
231
232 static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
233 {
234         hash = jhash_1word(hash, table->hash_seed);
235         return flex_array_get(table->buckets,
236                                 (hash & (table->n_buckets - 1)));
237 }
238
239 static struct flex_array *alloc_buckets(unsigned int n_buckets)
240 {
241         struct flex_array *buckets;
242         int i, err;
243
244         buckets = flex_array_alloc(sizeof(struct hlist_head *),
245                                    n_buckets, GFP_KERNEL);
246         if (!buckets)
247                 return NULL;
248
249         err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
250         if (err) {
251                 flex_array_free(buckets);
252                 return NULL;
253         }
254
255         for (i = 0; i < n_buckets; i++)
256                 INIT_HLIST_HEAD((struct hlist_head *)
257                                         flex_array_get(buckets, i));
258
259         return buckets;
260 }
261
262 static void free_buckets(struct flex_array *buckets)
263 {
264         flex_array_free(buckets);
265 }
266
267 struct flow_table *ovs_flow_tbl_alloc(int new_size)
268 {
269         struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
270
271         if (!table)
272                 return NULL;
273
274         table->buckets = alloc_buckets(new_size);
275
276         if (!table->buckets) {
277                 kfree(table);
278                 return NULL;
279         }
280         table->n_buckets = new_size;
281         table->count = 0;
282         table->node_ver = 0;
283         table->keep_flows = false;
284         get_random_bytes(&table->hash_seed, sizeof(u32));
285
286         return table;
287 }
288
289 void ovs_flow_tbl_destroy(struct flow_table *table)
290 {
291         int i;
292
293         if (!table)
294                 return;
295
296         if (table->keep_flows)
297                 goto skip_flows;
298
299         for (i = 0; i < table->n_buckets; i++) {
300                 struct sw_flow *flow;
301                 struct hlist_head *head = flex_array_get(table->buckets, i);
302                 struct hlist_node *n;
303                 int ver = table->node_ver;
304
305                 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
306                         hlist_del_rcu(&flow->hash_node[ver]);
307                         ovs_flow_free(flow);
308                 }
309         }
310
311 skip_flows:
312         free_buckets(table->buckets);
313         kfree(table);
314 }
315
316 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
317 {
318         struct flow_table *table = container_of(rcu, struct flow_table, rcu);
319
320         ovs_flow_tbl_destroy(table);
321 }
322
323 void ovs_flow_tbl_deferred_destroy(struct flow_table *table)
324 {
325         if (!table)
326                 return;
327
328         call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
329 }
330
331 struct sw_flow *ovs_flow_tbl_next(struct flow_table *table, u32 *bucket, u32 *last)
332 {
333         struct sw_flow *flow;
334         struct hlist_head *head;
335         int ver;
336         int i;
337
338         ver = table->node_ver;
339         while (*bucket < table->n_buckets) {
340                 i = 0;
341                 head = flex_array_get(table->buckets, *bucket);
342                 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
343                         if (i < *last) {
344                                 i++;
345                                 continue;
346                         }
347                         *last = i + 1;
348                         return flow;
349                 }
350                 (*bucket)++;
351                 *last = 0;
352         }
353
354         return NULL;
355 }
356
357 static void __flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
358 {
359         struct hlist_head *head;
360         head = find_bucket(table, flow->hash);
361         hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
362         table->count++;
363 }
364
365 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
366 {
367         int old_ver;
368         int i;
369
370         old_ver = old->node_ver;
371         new->node_ver = !old_ver;
372
373         /* Insert in new table. */
374         for (i = 0; i < old->n_buckets; i++) {
375                 struct sw_flow *flow;
376                 struct hlist_head *head;
377
378                 head = flex_array_get(old->buckets, i);
379
380                 hlist_for_each_entry(flow, head, hash_node[old_ver])
381                         __flow_tbl_insert(new, flow);
382         }
383         old->keep_flows = true;
384 }
385
386 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
387 {
388         struct flow_table *new_table;
389
390         new_table = ovs_flow_tbl_alloc(n_buckets);
391         if (!new_table)
392                 return ERR_PTR(-ENOMEM);
393
394         flow_table_copy_flows(table, new_table);
395
396         return new_table;
397 }
398
399 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
400 {
401         return __flow_tbl_rehash(table, table->n_buckets);
402 }
403
404 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
405 {
406         return __flow_tbl_rehash(table, table->n_buckets * 2);
407 }
408
409 void ovs_flow_free(struct sw_flow *flow)
410 {
411         if (unlikely(!flow))
412                 return;
413
414         kfree((struct sf_flow_acts __force *)flow->sf_acts);
415         kmem_cache_free(flow_cache, flow);
416 }
417
418 /* RCU callback used by ovs_flow_deferred_free. */
419 static void rcu_free_flow_callback(struct rcu_head *rcu)
420 {
421         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
422
423         ovs_flow_free(flow);
424 }
425
426 /* Schedules 'flow' to be freed after the next RCU grace period.
427  * The caller must hold rcu_read_lock for this to be sensible. */
428 void ovs_flow_deferred_free(struct sw_flow *flow)
429 {
430         call_rcu(&flow->rcu, rcu_free_flow_callback);
431 }
432
433 /* RCU callback used by ovs_flow_deferred_free_acts. */
434 static void rcu_free_acts_callback(struct rcu_head *rcu)
435 {
436         struct sw_flow_actions *sf_acts = container_of(rcu,
437                         struct sw_flow_actions, rcu);
438         kfree(sf_acts);
439 }
440
441 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
442  * The caller must hold rcu_read_lock for this to be sensible. */
443 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
444 {
445         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
446 }
447
448 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
449 {
450         struct qtag_prefix {
451                 __be16 eth_type; /* ETH_P_8021Q */
452                 __be16 tci;
453         };
454         struct qtag_prefix *qp;
455
456         if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
457                 return 0;
458
459         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
460                                          sizeof(__be16))))
461                 return -ENOMEM;
462
463         qp = (struct qtag_prefix *) skb->data;
464         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
465         __skb_pull(skb, sizeof(struct qtag_prefix));
466
467         return 0;
468 }
469
470 static __be16 parse_ethertype(struct sk_buff *skb)
471 {
472         struct llc_snap_hdr {
473                 u8  dsap;  /* Always 0xAA */
474                 u8  ssap;  /* Always 0xAA */
475                 u8  ctrl;
476                 u8  oui[3];
477                 __be16 ethertype;
478         };
479         struct llc_snap_hdr *llc;
480         __be16 proto;
481
482         proto = *(__be16 *) skb->data;
483         __skb_pull(skb, sizeof(__be16));
484
485         if (ntohs(proto) >= ETH_P_802_3_MIN)
486                 return proto;
487
488         if (skb->len < sizeof(struct llc_snap_hdr))
489                 return htons(ETH_P_802_2);
490
491         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
492                 return htons(0);
493
494         llc = (struct llc_snap_hdr *) skb->data;
495         if (llc->dsap != LLC_SAP_SNAP ||
496             llc->ssap != LLC_SAP_SNAP ||
497             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
498                 return htons(ETH_P_802_2);
499
500         __skb_pull(skb, sizeof(struct llc_snap_hdr));
501
502         if (ntohs(llc->ethertype) >= ETH_P_802_3_MIN)
503                 return llc->ethertype;
504
505         return htons(ETH_P_802_2);
506 }
507
508 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
509                         int *key_lenp, int nh_len)
510 {
511         struct icmp6hdr *icmp = icmp6_hdr(skb);
512         int error = 0;
513         int key_len;
514
515         /* The ICMPv6 type and code fields use the 16-bit transport port
516          * fields, so we need to store them in 16-bit network byte order.
517          */
518         key->ipv6.tp.src = htons(icmp->icmp6_type);
519         key->ipv6.tp.dst = htons(icmp->icmp6_code);
520         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
521
522         if (icmp->icmp6_code == 0 &&
523             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
524              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
525                 int icmp_len = skb->len - skb_transport_offset(skb);
526                 struct nd_msg *nd;
527                 int offset;
528
529                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
530
531                 /* In order to process neighbor discovery options, we need the
532                  * entire packet.
533                  */
534                 if (unlikely(icmp_len < sizeof(*nd)))
535                         goto out;
536                 if (unlikely(skb_linearize(skb))) {
537                         error = -ENOMEM;
538                         goto out;
539                 }
540
541                 nd = (struct nd_msg *)skb_transport_header(skb);
542                 key->ipv6.nd.target = nd->target;
543                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
544
545                 icmp_len -= sizeof(*nd);
546                 offset = 0;
547                 while (icmp_len >= 8) {
548                         struct nd_opt_hdr *nd_opt =
549                                  (struct nd_opt_hdr *)(nd->opt + offset);
550                         int opt_len = nd_opt->nd_opt_len * 8;
551
552                         if (unlikely(!opt_len || opt_len > icmp_len))
553                                 goto invalid;
554
555                         /* Store the link layer address if the appropriate
556                          * option is provided.  It is considered an error if
557                          * the same link layer option is specified twice.
558                          */
559                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
560                             && opt_len == 8) {
561                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
562                                         goto invalid;
563                                 memcpy(key->ipv6.nd.sll,
564                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
565                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
566                                    && opt_len == 8) {
567                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
568                                         goto invalid;
569                                 memcpy(key->ipv6.nd.tll,
570                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
571                         }
572
573                         icmp_len -= opt_len;
574                         offset += opt_len;
575                 }
576         }
577
578         goto out;
579
580 invalid:
581         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
582         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
583         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
584
585 out:
586         *key_lenp = key_len;
587         return error;
588 }
589
590 /**
591  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
592  * @skb: sk_buff that contains the frame, with skb->data pointing to the
593  * Ethernet header
594  * @in_port: port number on which @skb was received.
595  * @key: output flow key
596  * @key_lenp: length of output flow key
597  *
598  * The caller must ensure that skb->len >= ETH_HLEN.
599  *
600  * Returns 0 if successful, otherwise a negative errno value.
601  *
602  * Initializes @skb header pointers as follows:
603  *
604  *    - skb->mac_header: the Ethernet header.
605  *
606  *    - skb->network_header: just past the Ethernet header, or just past the
607  *      VLAN header, to the first byte of the Ethernet payload.
608  *
609  *    - skb->transport_header: If key->eth.type is ETH_P_IP or ETH_P_IPV6
610  *      on output, then just past the IP header, if one is present and
611  *      of a correct length, otherwise the same as skb->network_header.
612  *      For other key->eth.type values it is left untouched.
613  */
614 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
615                  int *key_lenp)
616 {
617         int error = 0;
618         int key_len = SW_FLOW_KEY_OFFSET(eth);
619         struct ethhdr *eth;
620
621         memset(key, 0, sizeof(*key));
622
623         key->phy.priority = skb->priority;
624         if (OVS_CB(skb)->tun_key)
625                 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
626         key->phy.in_port = in_port;
627         key->phy.skb_mark = skb_get_mark(skb);
628
629         skb_reset_mac_header(skb);
630
631         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
632          * header in the linear data area.
633          */
634         eth = eth_hdr(skb);
635         memcpy(key->eth.src, eth->h_source, ETH_ALEN);
636         memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
637
638         __skb_pull(skb, 2 * ETH_ALEN);
639         /* We are going to push all headers that we pull, so no need to
640          * update skb->csum here. */
641
642         if (vlan_tx_tag_present(skb))
643                 key->eth.tci = htons(vlan_get_tci(skb));
644         else if (eth->h_proto == htons(ETH_P_8021Q))
645                 if (unlikely(parse_vlan(skb, key)))
646                         return -ENOMEM;
647
648         key->eth.type = parse_ethertype(skb);
649         if (unlikely(key->eth.type == htons(0)))
650                 return -ENOMEM;
651
652         skb_reset_network_header(skb);
653         __skb_push(skb, skb->data - skb_mac_header(skb));
654
655         /* Network layer. */
656         if (key->eth.type == htons(ETH_P_IP)) {
657                 struct iphdr *nh;
658                 __be16 offset;
659
660                 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
661
662                 error = check_iphdr(skb);
663                 if (unlikely(error)) {
664                         if (error == -EINVAL) {
665                                 skb->transport_header = skb->network_header;
666                                 error = 0;
667                         }
668                         goto out;
669                 }
670
671                 nh = ip_hdr(skb);
672                 key->ipv4.addr.src = nh->saddr;
673                 key->ipv4.addr.dst = nh->daddr;
674
675                 key->ip.proto = nh->protocol;
676                 key->ip.tos = nh->tos;
677                 key->ip.ttl = nh->ttl;
678
679                 offset = nh->frag_off & htons(IP_OFFSET);
680                 if (offset) {
681                         key->ip.frag = OVS_FRAG_TYPE_LATER;
682                         goto out;
683                 }
684                 if (nh->frag_off & htons(IP_MF) ||
685                          skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
686                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
687
688                 /* Transport layer. */
689                 if (key->ip.proto == IPPROTO_TCP) {
690                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
691                         if (tcphdr_ok(skb)) {
692                                 struct tcphdr *tcp = tcp_hdr(skb);
693                                 key->ipv4.tp.src = tcp->source;
694                                 key->ipv4.tp.dst = tcp->dest;
695                         }
696                 } else if (key->ip.proto == IPPROTO_UDP) {
697                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
698                         if (udphdr_ok(skb)) {
699                                 struct udphdr *udp = udp_hdr(skb);
700                                 key->ipv4.tp.src = udp->source;
701                                 key->ipv4.tp.dst = udp->dest;
702                         }
703                 } else if (key->ip.proto == IPPROTO_ICMP) {
704                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
705                         if (icmphdr_ok(skb)) {
706                                 struct icmphdr *icmp = icmp_hdr(skb);
707                                 /* The ICMP type and code fields use the 16-bit
708                                  * transport port fields, so we need to store
709                                  * them in 16-bit network byte order. */
710                                 key->ipv4.tp.src = htons(icmp->type);
711                                 key->ipv4.tp.dst = htons(icmp->code);
712                         }
713                 }
714
715         } else if ((key->eth.type == htons(ETH_P_ARP) ||
716                    key->eth.type == htons(ETH_P_RARP)) && arphdr_ok(skb)) {
717                 struct arp_eth_header *arp;
718
719                 arp = (struct arp_eth_header *)skb_network_header(skb);
720
721                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
722                                 && arp->ar_pro == htons(ETH_P_IP)
723                                 && arp->ar_hln == ETH_ALEN
724                                 && arp->ar_pln == 4) {
725
726                         /* We only match on the lower 8 bits of the opcode. */
727                         if (ntohs(arp->ar_op) <= 0xff)
728                                 key->ip.proto = ntohs(arp->ar_op);
729                         memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
730                         memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
731                         memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
732                         memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
733                         key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
734                 }
735         } else if (key->eth.type == htons(ETH_P_IPV6)) {
736                 int nh_len;             /* IPv6 Header + Extensions */
737
738                 nh_len = parse_ipv6hdr(skb, key, &key_len);
739                 if (unlikely(nh_len < 0)) {
740                         if (nh_len == -EINVAL)
741                                 skb->transport_header = skb->network_header;
742                         else
743                                 error = nh_len;
744                         goto out;
745                 }
746
747                 if (key->ip.frag == OVS_FRAG_TYPE_LATER)
748                         goto out;
749                 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
750                         key->ip.frag = OVS_FRAG_TYPE_FIRST;
751
752                 /* Transport layer. */
753                 if (key->ip.proto == NEXTHDR_TCP) {
754                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
755                         if (tcphdr_ok(skb)) {
756                                 struct tcphdr *tcp = tcp_hdr(skb);
757                                 key->ipv6.tp.src = tcp->source;
758                                 key->ipv6.tp.dst = tcp->dest;
759                         }
760                 } else if (key->ip.proto == NEXTHDR_UDP) {
761                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
762                         if (udphdr_ok(skb)) {
763                                 struct udphdr *udp = udp_hdr(skb);
764                                 key->ipv6.tp.src = udp->source;
765                                 key->ipv6.tp.dst = udp->dest;
766                         }
767                 } else if (key->ip.proto == NEXTHDR_ICMP) {
768                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
769                         if (icmp6hdr_ok(skb)) {
770                                 error = parse_icmpv6(skb, key, &key_len, nh_len);
771                                 if (error < 0)
772                                         goto out;
773                         }
774                 }
775         }
776
777 out:
778         *key_lenp = key_len;
779         return error;
780 }
781
782 static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start, int key_len)
783 {
784         return jhash2((u32 *)((u8 *)key + key_start),
785                       DIV_ROUND_UP(key_len - key_start, sizeof(u32)), 0);
786 }
787
788 static int flow_key_start(struct sw_flow_key *key)
789 {
790         if (key->tun_key.ipv4_dst)
791                 return 0;
792         else
793                 return offsetof(struct sw_flow_key, phy);
794 }
795
796 struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *table,
797                                 struct sw_flow_key *key, int key_len)
798 {
799         struct sw_flow *flow;
800         struct hlist_head *head;
801         u8 *_key;
802         int key_start;
803         u32 hash;
804
805         key_start = flow_key_start(key);
806         hash = ovs_flow_hash(key, key_start, key_len);
807
808         _key = (u8 *) key + key_start;
809         head = find_bucket(table, hash);
810         hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
811
812                 if (flow->hash == hash &&
813                     !memcmp((u8 *)&flow->key + key_start, _key, key_len - key_start)) {
814                         return flow;
815                 }
816         }
817         return NULL;
818 }
819
820 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
821                          struct sw_flow_key *key, int key_len)
822 {
823         flow->hash = ovs_flow_hash(key, flow_key_start(key), key_len);
824         memcpy(&flow->key, key, sizeof(flow->key));
825         __flow_tbl_insert(table, flow);
826 }
827
828 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
829 {
830         BUG_ON(table->count == 0);
831         hlist_del_rcu(&flow->hash_node[table->node_ver]);
832         table->count--;
833 }
834
835 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
836 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
837         [OVS_KEY_ATTR_ENCAP] = -1,
838         [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
839         [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
840         [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
841         [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
842         [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
843         [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
844         [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
845         [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
846         [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
847         [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
848         [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
849         [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
850         [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
851         [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
852         [OVS_KEY_ATTR_TUNNEL] = -1,
853 };
854
855 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
856                                   const struct nlattr *a[], u64 *attrs)
857 {
858         const struct ovs_key_icmp *icmp_key;
859         const struct ovs_key_tcp *tcp_key;
860         const struct ovs_key_udp *udp_key;
861
862         switch (swkey->ip.proto) {
863         case IPPROTO_TCP:
864                 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
865                         return -EINVAL;
866                 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
867
868                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
869                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
870                 swkey->ipv4.tp.src = tcp_key->tcp_src;
871                 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
872                 break;
873
874         case IPPROTO_UDP:
875                 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
876                         return -EINVAL;
877                 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
878
879                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
880                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
881                 swkey->ipv4.tp.src = udp_key->udp_src;
882                 swkey->ipv4.tp.dst = udp_key->udp_dst;
883                 break;
884
885         case IPPROTO_ICMP:
886                 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
887                         return -EINVAL;
888                 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
889
890                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
891                 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
892                 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
893                 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
894                 break;
895         }
896
897         return 0;
898 }
899
900 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
901                                   const struct nlattr *a[], u64 *attrs)
902 {
903         const struct ovs_key_icmpv6 *icmpv6_key;
904         const struct ovs_key_tcp *tcp_key;
905         const struct ovs_key_udp *udp_key;
906
907         switch (swkey->ip.proto) {
908         case IPPROTO_TCP:
909                 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
910                         return -EINVAL;
911                 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
912
913                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
914                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
915                 swkey->ipv6.tp.src = tcp_key->tcp_src;
916                 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
917                 break;
918
919         case IPPROTO_UDP:
920                 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
921                         return -EINVAL;
922                 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
923
924                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
925                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
926                 swkey->ipv6.tp.src = udp_key->udp_src;
927                 swkey->ipv6.tp.dst = udp_key->udp_dst;
928                 break;
929
930         case IPPROTO_ICMPV6:
931                 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
932                         return -EINVAL;
933                 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
934
935                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
936                 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
937                 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
938                 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
939
940                 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
941                     swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
942                         const struct ovs_key_nd *nd_key;
943
944                         if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
945                                 return -EINVAL;
946                         *attrs &= ~(1 << OVS_KEY_ATTR_ND);
947
948                         *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
949                         nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
950                         memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
951                                sizeof(swkey->ipv6.nd.target));
952                         memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
953                         memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
954                 }
955                 break;
956         }
957
958         return 0;
959 }
960
961 static int parse_flow_nlattrs(const struct nlattr *attr,
962                               const struct nlattr *a[], u64 *attrsp)
963 {
964         const struct nlattr *nla;
965         u64 attrs;
966         int rem;
967
968         attrs = 0;
969         nla_for_each_nested(nla, attr, rem) {
970                 u16 type = nla_type(nla);
971                 int expected_len;
972
973                 if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
974                         return -EINVAL;
975
976                 expected_len = ovs_key_lens[type];
977                 if (nla_len(nla) != expected_len && expected_len != -1)
978                         return -EINVAL;
979
980                 attrs |= 1ULL << type;
981                 a[type] = nla;
982         }
983         if (rem)
984                 return -EINVAL;
985
986         *attrsp = attrs;
987         return 0;
988 }
989
990 int ipv4_tun_from_nlattr(const struct nlattr *attr,
991                          struct ovs_key_ipv4_tunnel *tun_key)
992 {
993         struct nlattr *a;
994         int rem;
995         bool ttl = false;
996
997         memset(tun_key, 0, sizeof(*tun_key));
998
999         nla_for_each_nested(a, attr, rem) {
1000                 int type = nla_type(a);
1001                 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1002                         [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1003                         [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1004                         [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1005                         [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1006                         [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1007                         [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1008                         [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1009                 };
1010
1011                 if (type > OVS_TUNNEL_KEY_ATTR_MAX ||
1012                         ovs_tunnel_key_lens[type] != nla_len(a))
1013                         return -EINVAL;
1014
1015                 switch (type) {
1016                 case OVS_TUNNEL_KEY_ATTR_ID:
1017                         tun_key->tun_id = nla_get_be64(a);
1018                         tun_key->tun_flags |= OVS_TNL_F_KEY;
1019                         break;
1020                 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1021                         tun_key->ipv4_src = nla_get_be32(a);
1022                         break;
1023                 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1024                         tun_key->ipv4_dst = nla_get_be32(a);
1025                         break;
1026                 case OVS_TUNNEL_KEY_ATTR_TOS:
1027                         tun_key->ipv4_tos = nla_get_u8(a);
1028                         break;
1029                 case OVS_TUNNEL_KEY_ATTR_TTL:
1030                         tun_key->ipv4_ttl = nla_get_u8(a);
1031                         ttl = true;
1032                         break;
1033                 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1034                         tun_key->tun_flags |= OVS_TNL_F_DONT_FRAGMENT;
1035                         break;
1036                 case OVS_TUNNEL_KEY_ATTR_CSUM:
1037                         tun_key->tun_flags |= OVS_TNL_F_CSUM;
1038                         break;
1039                 default:
1040                         return -EINVAL;
1041
1042                 }
1043         }
1044         if (rem > 0)
1045                 return -EINVAL;
1046
1047         if (!tun_key->ipv4_dst)
1048                 return -EINVAL;
1049
1050         if (!ttl)
1051                 return -EINVAL;
1052
1053         return 0;
1054 }
1055
1056 int ipv4_tun_to_nlattr(struct sk_buff *skb,
1057                         const struct ovs_key_ipv4_tunnel *tun_key)
1058 {
1059         struct nlattr *nla;
1060
1061         nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1062         if (!nla)
1063                 return -EMSGSIZE;
1064
1065         if (tun_key->tun_flags & OVS_TNL_F_KEY &&
1066             nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id))
1067                 return -EMSGSIZE;
1068         if (tun_key->ipv4_src &&
1069             nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ipv4_src))
1070                 return -EMSGSIZE;
1071         if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ipv4_dst))
1072                 return -EMSGSIZE;
1073         if (tun_key->ipv4_tos &&
1074             nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ipv4_tos))
1075                 return -EMSGSIZE;
1076         if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ipv4_ttl))
1077                 return -EMSGSIZE;
1078         if ((tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT) &&
1079                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1080                 return -EMSGSIZE;
1081         if ((tun_key->tun_flags & OVS_TNL_F_CSUM) &&
1082                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1083                 return -EMSGSIZE;
1084
1085         nla_nest_end(skb, nla);
1086         return 0;
1087 }
1088
1089 /**
1090  * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
1091  * @swkey: receives the extracted flow key.
1092  * @key_lenp: number of bytes used in @swkey.
1093  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1094  * sequence.
1095  */
1096 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
1097                       const struct nlattr *attr)
1098 {
1099         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1100         const struct ovs_key_ethernet *eth_key;
1101         int key_len;
1102         u64 attrs;
1103         int err;
1104
1105         memset(swkey, 0, sizeof(struct sw_flow_key));
1106         key_len = SW_FLOW_KEY_OFFSET(eth);
1107
1108         err = parse_flow_nlattrs(attr, a, &attrs);
1109         if (err)
1110                 return err;
1111
1112         /* Metadata attributes. */
1113         if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1114                 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1115                 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1116         }
1117         if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1118                 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1119                 if (in_port >= DP_MAX_PORTS)
1120                         return -EINVAL;
1121                 swkey->phy.in_port = in_port;
1122                 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1123         } else {
1124                 swkey->phy.in_port = DP_MAX_PORTS;
1125         }
1126         if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1127                 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1128 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1129                 if (mark != 0)
1130                         return -EINVAL;
1131 #endif
1132                 swkey->phy.skb_mark = mark;
1133                 attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1134         }
1135
1136         if (attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1137                 err = ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], &swkey->tun_key);
1138                 if (err)
1139                         return err;
1140
1141                 attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1142         }
1143
1144         /* Data attributes. */
1145         if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1146                 return -EINVAL;
1147         attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1148
1149         eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1150         memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1151         memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1152
1153         if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1154             nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1155                 const struct nlattr *encap;
1156                 __be16 tci;
1157
1158                 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1159                               (1 << OVS_KEY_ATTR_ETHERTYPE) |
1160                               (1 << OVS_KEY_ATTR_ENCAP)))
1161                         return -EINVAL;
1162
1163                 encap = a[OVS_KEY_ATTR_ENCAP];
1164                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1165                 if (tci & htons(VLAN_TAG_PRESENT)) {
1166                         swkey->eth.tci = tci;
1167
1168                         err = parse_flow_nlattrs(encap, a, &attrs);
1169                         if (err)
1170                                 return err;
1171                 } else if (!tci) {
1172                         /* Corner case for truncated 802.1Q header. */
1173                         if (nla_len(encap))
1174                                 return -EINVAL;
1175
1176                         swkey->eth.type = htons(ETH_P_8021Q);
1177                         *key_lenp = key_len;
1178                         return 0;
1179                 } else {
1180                         return -EINVAL;
1181                 }
1182         }
1183
1184         if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1185                 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1186                 if (ntohs(swkey->eth.type) < ETH_P_802_3_MIN)
1187                         return -EINVAL;
1188                 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1189         } else {
1190                 swkey->eth.type = htons(ETH_P_802_2);
1191         }
1192
1193         if (swkey->eth.type == htons(ETH_P_IP)) {
1194                 const struct ovs_key_ipv4 *ipv4_key;
1195
1196                 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1197                         return -EINVAL;
1198                 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1199
1200                 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1201                 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1202                 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1203                         return -EINVAL;
1204                 swkey->ip.proto = ipv4_key->ipv4_proto;
1205                 swkey->ip.tos = ipv4_key->ipv4_tos;
1206                 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1207                 swkey->ip.frag = ipv4_key->ipv4_frag;
1208                 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1209                 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1210
1211                 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1212                         err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1213                         if (err)
1214                                 return err;
1215                 }
1216         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1217                 const struct ovs_key_ipv6 *ipv6_key;
1218
1219                 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1220                         return -EINVAL;
1221                 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1222
1223                 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1224                 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1225                 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1226                         return -EINVAL;
1227                 swkey->ipv6.label = ipv6_key->ipv6_label;
1228                 swkey->ip.proto = ipv6_key->ipv6_proto;
1229                 swkey->ip.tos = ipv6_key->ipv6_tclass;
1230                 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1231                 swkey->ip.frag = ipv6_key->ipv6_frag;
1232                 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1233                        sizeof(swkey->ipv6.addr.src));
1234                 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1235                        sizeof(swkey->ipv6.addr.dst));
1236
1237                 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1238                         err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1239                         if (err)
1240                                 return err;
1241                 }
1242         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1243                    swkey->eth.type == htons(ETH_P_RARP)) {
1244                 const struct ovs_key_arp *arp_key;
1245
1246                 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1247                         return -EINVAL;
1248                 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1249
1250                 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1251                 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1252                 swkey->ipv4.addr.src = arp_key->arp_sip;
1253                 swkey->ipv4.addr.dst = arp_key->arp_tip;
1254                 if (arp_key->arp_op & htons(0xff00))
1255                         return -EINVAL;
1256                 swkey->ip.proto = ntohs(arp_key->arp_op);
1257                 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1258                 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1259         }
1260
1261         if (attrs)
1262                 return -EINVAL;
1263         *key_lenp = key_len;
1264
1265         return 0;
1266 }
1267
1268 /**
1269  * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1270  * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1271  * @key_len: Length of key in @flow.  Used for calculating flow hash.
1272  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1273  * sequence.
1274  *
1275  * This parses a series of Netlink attributes that form a flow key, which must
1276  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1277  * get the metadata, that is, the parts of the flow key that cannot be
1278  * extracted from the packet itself.
1279  */
1280
1281 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, int key_len, const struct nlattr *attr)
1282 {
1283         struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1284         const struct nlattr *nla;
1285         int rem;
1286
1287         flow->key.phy.in_port = DP_MAX_PORTS;
1288         flow->key.phy.priority = 0;
1289         flow->key.phy.skb_mark = 0;
1290         memset(tun_key, 0, sizeof(flow->key.tun_key));
1291
1292         nla_for_each_nested(nla, attr, rem) {
1293                 int type = nla_type(nla);
1294
1295                 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1296                         int err;
1297
1298                         if (nla_len(nla) != ovs_key_lens[type])
1299                                 return -EINVAL;
1300
1301                         switch (type) {
1302                         case OVS_KEY_ATTR_PRIORITY:
1303                                 flow->key.phy.priority = nla_get_u32(nla);
1304                                 break;
1305
1306                         case OVS_KEY_ATTR_TUNNEL:
1307                                 err = ipv4_tun_from_nlattr(nla, tun_key);
1308                                 if (err)
1309                                         return err;
1310                                 break;
1311
1312                         case OVS_KEY_ATTR_IN_PORT:
1313                                 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1314                                         return -EINVAL;
1315                                 flow->key.phy.in_port = nla_get_u32(nla);
1316                                 break;
1317
1318                         case OVS_KEY_ATTR_SKB_MARK:
1319 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1320                                 if (nla_get_u32(nla) != 0)
1321                                         return -EINVAL;
1322 #endif
1323                                 flow->key.phy.skb_mark = nla_get_u32(nla);
1324                                 break;
1325                         }
1326                 }
1327         }
1328         if (rem)
1329                 return -EINVAL;
1330
1331         flow->hash = ovs_flow_hash(&flow->key,
1332                                    flow_key_start(&flow->key), key_len);
1333
1334         return 0;
1335 }
1336
1337 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1338 {
1339         struct ovs_key_ethernet *eth_key;
1340         struct nlattr *nla, *encap;
1341
1342         if (swkey->phy.priority &&
1343             nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1344                 goto nla_put_failure;
1345
1346         if (swkey->tun_key.ipv4_dst &&
1347             ipv4_tun_to_nlattr(skb, &swkey->tun_key))
1348                 goto nla_put_failure;
1349
1350         if (swkey->phy.in_port != DP_MAX_PORTS &&
1351             nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1352                 goto nla_put_failure;
1353
1354         if (swkey->phy.skb_mark &&
1355             nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1356                 goto nla_put_failure;
1357
1358         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1359         if (!nla)
1360                 goto nla_put_failure;
1361         eth_key = nla_data(nla);
1362         memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1363         memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1364
1365         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1366                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1367                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1368                         goto nla_put_failure;
1369                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1370                 if (!swkey->eth.tci)
1371                         goto unencap;
1372         } else {
1373                 encap = NULL;
1374         }
1375
1376         if (swkey->eth.type == htons(ETH_P_802_2))
1377                 goto unencap;
1378
1379         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1380                 goto nla_put_failure;
1381
1382         if (swkey->eth.type == htons(ETH_P_IP)) {
1383                 struct ovs_key_ipv4 *ipv4_key;
1384
1385                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1386                 if (!nla)
1387                         goto nla_put_failure;
1388                 ipv4_key = nla_data(nla);
1389                 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1390                 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1391                 ipv4_key->ipv4_proto = swkey->ip.proto;
1392                 ipv4_key->ipv4_tos = swkey->ip.tos;
1393                 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1394                 ipv4_key->ipv4_frag = swkey->ip.frag;
1395         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1396                 struct ovs_key_ipv6 *ipv6_key;
1397
1398                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1399                 if (!nla)
1400                         goto nla_put_failure;
1401                 ipv6_key = nla_data(nla);
1402                 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1403                                 sizeof(ipv6_key->ipv6_src));
1404                 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1405                                 sizeof(ipv6_key->ipv6_dst));
1406                 ipv6_key->ipv6_label = swkey->ipv6.label;
1407                 ipv6_key->ipv6_proto = swkey->ip.proto;
1408                 ipv6_key->ipv6_tclass = swkey->ip.tos;
1409                 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1410                 ipv6_key->ipv6_frag = swkey->ip.frag;
1411         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1412                    swkey->eth.type == htons(ETH_P_RARP)) {
1413                 struct ovs_key_arp *arp_key;
1414
1415                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1416                 if (!nla)
1417                         goto nla_put_failure;
1418                 arp_key = nla_data(nla);
1419                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1420                 arp_key->arp_sip = swkey->ipv4.addr.src;
1421                 arp_key->arp_tip = swkey->ipv4.addr.dst;
1422                 arp_key->arp_op = htons(swkey->ip.proto);
1423                 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1424                 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1425         }
1426
1427         if ((swkey->eth.type == htons(ETH_P_IP) ||
1428              swkey->eth.type == htons(ETH_P_IPV6)) &&
1429              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1430
1431                 if (swkey->ip.proto == IPPROTO_TCP) {
1432                         struct ovs_key_tcp *tcp_key;
1433
1434                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1435                         if (!nla)
1436                                 goto nla_put_failure;
1437                         tcp_key = nla_data(nla);
1438                         if (swkey->eth.type == htons(ETH_P_IP)) {
1439                                 tcp_key->tcp_src = swkey->ipv4.tp.src;
1440                                 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1441                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1442                                 tcp_key->tcp_src = swkey->ipv6.tp.src;
1443                                 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1444                         }
1445                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1446                         struct ovs_key_udp *udp_key;
1447
1448                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1449                         if (!nla)
1450                                 goto nla_put_failure;
1451                         udp_key = nla_data(nla);
1452                         if (swkey->eth.type == htons(ETH_P_IP)) {
1453                                 udp_key->udp_src = swkey->ipv4.tp.src;
1454                                 udp_key->udp_dst = swkey->ipv4.tp.dst;
1455                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1456                                 udp_key->udp_src = swkey->ipv6.tp.src;
1457                                 udp_key->udp_dst = swkey->ipv6.tp.dst;
1458                         }
1459                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1460                            swkey->ip.proto == IPPROTO_ICMP) {
1461                         struct ovs_key_icmp *icmp_key;
1462
1463                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1464                         if (!nla)
1465                                 goto nla_put_failure;
1466                         icmp_key = nla_data(nla);
1467                         icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1468                         icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1469                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1470                            swkey->ip.proto == IPPROTO_ICMPV6) {
1471                         struct ovs_key_icmpv6 *icmpv6_key;
1472
1473                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1474                                                 sizeof(*icmpv6_key));
1475                         if (!nla)
1476                                 goto nla_put_failure;
1477                         icmpv6_key = nla_data(nla);
1478                         icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1479                         icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1480
1481                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1482                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1483                                 struct ovs_key_nd *nd_key;
1484
1485                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1486                                 if (!nla)
1487                                         goto nla_put_failure;
1488                                 nd_key = nla_data(nla);
1489                                 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1490                                                         sizeof(nd_key->nd_target));
1491                                 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1492                                 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1493                         }
1494                 }
1495         }
1496
1497 unencap:
1498         if (encap)
1499                 nla_nest_end(skb, encap);
1500
1501         return 0;
1502
1503 nla_put_failure:
1504         return -EMSGSIZE;
1505 }
1506
1507 /* Initializes the flow module.
1508  * Returns zero if successful or a negative error code. */
1509 int ovs_flow_init(void)
1510 {
1511         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1512                                         0, NULL);
1513         if (flow_cache == NULL)
1514                 return -ENOMEM;
1515
1516         return 0;
1517 }
1518
1519 /* Uninitializes the flow module. */
1520 void ovs_flow_exit(void)
1521 {
1522         kmem_cache_destroy(flow_cache);
1523 }