3802926408e009819916745d237d77a0f19bec9d
[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 *node, *n;
303                 int ver = table->node_ver;
304
305                 hlist_for_each_entry_safe(flow, node, 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         struct hlist_node *n;
336         int ver;
337         int i;
338
339         ver = table->node_ver;
340         while (*bucket < table->n_buckets) {
341                 i = 0;
342                 head = flex_array_get(table->buckets, *bucket);
343                 hlist_for_each_entry_rcu(flow, n, head, hash_node[ver]) {
344                         if (i < *last) {
345                                 i++;
346                                 continue;
347                         }
348                         *last = i + 1;
349                         return flow;
350                 }
351                 (*bucket)++;
352                 *last = 0;
353         }
354
355         return NULL;
356 }
357
358 static void __flow_tbl_insert(struct flow_table *table, struct sw_flow *flow)
359 {
360         struct hlist_head *head;
361         head = find_bucket(table, flow->hash);
362         hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
363         table->count++;
364 }
365
366 static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
367 {
368         int old_ver;
369         int i;
370
371         old_ver = old->node_ver;
372         new->node_ver = !old_ver;
373
374         /* Insert in new table. */
375         for (i = 0; i < old->n_buckets; i++) {
376                 struct sw_flow *flow;
377                 struct hlist_head *head;
378                 struct hlist_node *n;
379
380                 head = flex_array_get(old->buckets, i);
381
382                 hlist_for_each_entry(flow, n, head, hash_node[old_ver])
383                         __flow_tbl_insert(new, flow);
384         }
385         old->keep_flows = true;
386 }
387
388 static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
389 {
390         struct flow_table *new_table;
391
392         new_table = ovs_flow_tbl_alloc(n_buckets);
393         if (!new_table)
394                 return ERR_PTR(-ENOMEM);
395
396         flow_table_copy_flows(table, new_table);
397
398         return new_table;
399 }
400
401 struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
402 {
403         return __flow_tbl_rehash(table, table->n_buckets);
404 }
405
406 struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
407 {
408         return __flow_tbl_rehash(table, table->n_buckets * 2);
409 }
410
411 void ovs_flow_free(struct sw_flow *flow)
412 {
413         if (unlikely(!flow))
414                 return;
415
416         kfree((struct sf_flow_acts __force *)flow->sf_acts);
417         kmem_cache_free(flow_cache, flow);
418 }
419
420 /* RCU callback used by ovs_flow_deferred_free. */
421 static void rcu_free_flow_callback(struct rcu_head *rcu)
422 {
423         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
424
425         ovs_flow_free(flow);
426 }
427
428 /* Schedules 'flow' to be freed after the next RCU grace period.
429  * The caller must hold rcu_read_lock for this to be sensible. */
430 void ovs_flow_deferred_free(struct sw_flow *flow)
431 {
432         call_rcu(&flow->rcu, rcu_free_flow_callback);
433 }
434
435 /* RCU callback used by ovs_flow_deferred_free_acts. */
436 static void rcu_free_acts_callback(struct rcu_head *rcu)
437 {
438         struct sw_flow_actions *sf_acts = container_of(rcu,
439                         struct sw_flow_actions, rcu);
440         kfree(sf_acts);
441 }
442
443 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
444  * The caller must hold rcu_read_lock for this to be sensible. */
445 void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
446 {
447         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
448 }
449
450 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
451 {
452         struct qtag_prefix {
453                 __be16 eth_type; /* ETH_P_8021Q */
454                 __be16 tci;
455         };
456         struct qtag_prefix *qp;
457
458         if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
459                 return 0;
460
461         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
462                                          sizeof(__be16))))
463                 return -ENOMEM;
464
465         qp = (struct qtag_prefix *) skb->data;
466         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
467         __skb_pull(skb, sizeof(struct qtag_prefix));
468
469         return 0;
470 }
471
472 static __be16 parse_ethertype(struct sk_buff *skb)
473 {
474         struct llc_snap_hdr {
475                 u8  dsap;  /* Always 0xAA */
476                 u8  ssap;  /* Always 0xAA */
477                 u8  ctrl;
478                 u8  oui[3];
479                 __be16 ethertype;
480         };
481         struct llc_snap_hdr *llc;
482         __be16 proto;
483
484         proto = *(__be16 *) skb->data;
485         __skb_pull(skb, sizeof(__be16));
486
487         if (ntohs(proto) >= 1536)
488                 return proto;
489
490         if (skb->len < sizeof(struct llc_snap_hdr))
491                 return htons(ETH_P_802_2);
492
493         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
494                 return htons(0);
495
496         llc = (struct llc_snap_hdr *) skb->data;
497         if (llc->dsap != LLC_SAP_SNAP ||
498             llc->ssap != LLC_SAP_SNAP ||
499             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
500                 return htons(ETH_P_802_2);
501
502         __skb_pull(skb, sizeof(struct llc_snap_hdr));
503
504         if (ntohs(llc->ethertype) >= 1536)
505                 return llc->ethertype;
506
507         return htons(ETH_P_802_2);
508 }
509
510 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
511                         int *key_lenp, int nh_len)
512 {
513         struct icmp6hdr *icmp = icmp6_hdr(skb);
514         int error = 0;
515         int key_len;
516
517         /* The ICMPv6 type and code fields use the 16-bit transport port
518          * fields, so we need to store them in 16-bit network byte order.
519          */
520         key->ipv6.tp.src = htons(icmp->icmp6_type);
521         key->ipv6.tp.dst = htons(icmp->icmp6_code);
522         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
523
524         if (icmp->icmp6_code == 0 &&
525             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
526              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
527                 int icmp_len = skb->len - skb_transport_offset(skb);
528                 struct nd_msg *nd;
529                 int offset;
530
531                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
532
533                 /* In order to process neighbor discovery options, we need the
534                  * entire packet.
535                  */
536                 if (unlikely(icmp_len < sizeof(*nd)))
537                         goto out;
538                 if (unlikely(skb_linearize(skb))) {
539                         error = -ENOMEM;
540                         goto out;
541                 }
542
543                 nd = (struct nd_msg *)skb_transport_header(skb);
544                 key->ipv6.nd.target = nd->target;
545                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
546
547                 icmp_len -= sizeof(*nd);
548                 offset = 0;
549                 while (icmp_len >= 8) {
550                         struct nd_opt_hdr *nd_opt =
551                                  (struct nd_opt_hdr *)(nd->opt + offset);
552                         int opt_len = nd_opt->nd_opt_len * 8;
553
554                         if (unlikely(!opt_len || opt_len > icmp_len))
555                                 goto invalid;
556
557                         /* Store the link layer address if the appropriate
558                          * option is provided.  It is considered an error if
559                          * the same link layer option is specified twice.
560                          */
561                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
562                             && opt_len == 8) {
563                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
564                                         goto invalid;
565                                 memcpy(key->ipv6.nd.sll,
566                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
567                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
568                                    && opt_len == 8) {
569                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
570                                         goto invalid;
571                                 memcpy(key->ipv6.nd.tll,
572                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
573                         }
574
575                         icmp_len -= opt_len;
576                         offset += opt_len;
577                 }
578         }
579
580         goto out;
581
582 invalid:
583         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
584         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
585         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
586
587 out:
588         *key_lenp = key_len;
589         return error;
590 }
591
592 /**
593  * ovs_flow_extract - extracts a flow key from an Ethernet frame.
594  * @skb: sk_buff that contains the frame, with skb->data pointing to the
595  * Ethernet header
596  * @in_port: port number on which @skb was received.
597  * @key: output flow key
598  * @key_lenp: length of output flow key
599  *
600  * The caller must ensure that skb->len >= ETH_HLEN.
601  *
602  * Returns 0 if successful, otherwise a negative errno value.
603  *
604  * Initializes @skb header pointers as follows:
605  *
606  *    - skb->mac_header: the Ethernet header.
607  *
608  *    - skb->network_header: just past the Ethernet header, or just past the
609  *      VLAN header, to the first byte of the Ethernet payload.
610  *
611  *    - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
612  *      on output, then just past the IP header, if one is present and
613  *      of a correct length, otherwise the same as skb->network_header.
614  *      For other key->dl_type values it is left untouched.
615  */
616 int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
617                  int *key_lenp)
618 {
619         int error = 0;
620         int key_len = SW_FLOW_KEY_OFFSET(eth);
621         struct ethhdr *eth;
622
623         memset(key, 0, sizeof(*key));
624
625         key->phy.priority = skb->priority;
626         if (OVS_CB(skb)->tun_key)
627                 memcpy(&key->tun_key, OVS_CB(skb)->tun_key, sizeof(key->tun_key));
628         key->phy.in_port = in_port;
629         key->phy.skb_mark = skb_get_mark(skb);
630
631         skb_reset_mac_header(skb);
632
633         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
634          * header in the linear data area.
635          */
636         eth = eth_hdr(skb);
637         memcpy(key->eth.src, eth->h_source, ETH_ALEN);
638         memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
639
640         __skb_pull(skb, 2 * ETH_ALEN);
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_node *n;
801         struct hlist_head *head;
802         u8 *_key;
803         int key_start;
804         u32 hash;
805
806         key_start = flow_key_start(key);
807         hash = ovs_flow_hash(key, key_start, key_len);
808
809         _key = (u8 *) key + key_start;
810         head = find_bucket(table, hash);
811         hlist_for_each_entry_rcu(flow, n, head, hash_node[table->node_ver]) {
812
813                 if (flow->hash == hash &&
814                     !memcmp((u8 *)&flow->key + key_start, _key, key_len - key_start)) {
815                         return flow;
816                 }
817         }
818         return NULL;
819 }
820
821 void ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
822                          struct sw_flow_key *key, int key_len)
823 {
824         flow->hash = ovs_flow_hash(key, flow_key_start(key), key_len);
825         memcpy(&flow->key, key, sizeof(flow->key));
826         __flow_tbl_insert(table, flow);
827 }
828
829 void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
830 {
831         hlist_del_rcu(&flow->hash_node[table->node_ver]);
832         table->count--;
833         BUG_ON(table->count < 0);
834 }
835
836 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute.  */
837 const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
838         [OVS_KEY_ATTR_ENCAP] = -1,
839         [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
840         [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
841         [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
842         [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
843         [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
844         [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
845         [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
846         [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
847         [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
848         [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
849         [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
850         [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
851         [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
852         [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
853         [OVS_KEY_ATTR_TUNNEL] = -1,
854 };
855
856 static int ipv4_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
857                                   const struct nlattr *a[], u64 *attrs)
858 {
859         const struct ovs_key_icmp *icmp_key;
860         const struct ovs_key_tcp *tcp_key;
861         const struct ovs_key_udp *udp_key;
862
863         switch (swkey->ip.proto) {
864         case IPPROTO_TCP:
865                 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
866                         return -EINVAL;
867                 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
868
869                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
870                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
871                 swkey->ipv4.tp.src = tcp_key->tcp_src;
872                 swkey->ipv4.tp.dst = tcp_key->tcp_dst;
873                 break;
874
875         case IPPROTO_UDP:
876                 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
877                         return -EINVAL;
878                 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
879
880                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
881                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
882                 swkey->ipv4.tp.src = udp_key->udp_src;
883                 swkey->ipv4.tp.dst = udp_key->udp_dst;
884                 break;
885
886         case IPPROTO_ICMP:
887                 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMP)))
888                         return -EINVAL;
889                 *attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
890
891                 *key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
892                 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
893                 swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
894                 swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
895                 break;
896         }
897
898         return 0;
899 }
900
901 static int ipv6_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_len,
902                                   const struct nlattr *a[], u64 *attrs)
903 {
904         const struct ovs_key_icmpv6 *icmpv6_key;
905         const struct ovs_key_tcp *tcp_key;
906         const struct ovs_key_udp *udp_key;
907
908         switch (swkey->ip.proto) {
909         case IPPROTO_TCP:
910                 if (!(*attrs & (1 << OVS_KEY_ATTR_TCP)))
911                         return -EINVAL;
912                 *attrs &= ~(1 << OVS_KEY_ATTR_TCP);
913
914                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
915                 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
916                 swkey->ipv6.tp.src = tcp_key->tcp_src;
917                 swkey->ipv6.tp.dst = tcp_key->tcp_dst;
918                 break;
919
920         case IPPROTO_UDP:
921                 if (!(*attrs & (1 << OVS_KEY_ATTR_UDP)))
922                         return -EINVAL;
923                 *attrs &= ~(1 << OVS_KEY_ATTR_UDP);
924
925                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
926                 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
927                 swkey->ipv6.tp.src = udp_key->udp_src;
928                 swkey->ipv6.tp.dst = udp_key->udp_dst;
929                 break;
930
931         case IPPROTO_ICMPV6:
932                 if (!(*attrs & (1 << OVS_KEY_ATTR_ICMPV6)))
933                         return -EINVAL;
934                 *attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
935
936                 *key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
937                 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
938                 swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
939                 swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
940
941                 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
942                     swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
943                         const struct ovs_key_nd *nd_key;
944
945                         if (!(*attrs & (1 << OVS_KEY_ATTR_ND)))
946                                 return -EINVAL;
947                         *attrs &= ~(1 << OVS_KEY_ATTR_ND);
948
949                         *key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
950                         nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
951                         memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
952                                sizeof(swkey->ipv6.nd.target));
953                         memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
954                         memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
955                 }
956                 break;
957         }
958
959         return 0;
960 }
961
962 static int parse_flow_nlattrs(const struct nlattr *attr,
963                               const struct nlattr *a[], u64 *attrsp)
964 {
965         const struct nlattr *nla;
966         u64 attrs;
967         int rem;
968
969         attrs = 0;
970         nla_for_each_nested(nla, attr, rem) {
971                 u16 type = nla_type(nla);
972                 int expected_len;
973
974                 if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
975                         return -EINVAL;
976
977                 expected_len = ovs_key_lens[type];
978                 if (nla_len(nla) != expected_len && expected_len != -1)
979                         return -EINVAL;
980
981                 attrs |= 1ULL << type;
982                 a[type] = nla;
983         }
984         if (rem)
985                 return -EINVAL;
986
987         *attrsp = attrs;
988         return 0;
989 }
990
991 int ipv4_tun_from_nlattr(const struct nlattr *attr,
992                          struct ovs_key_ipv4_tunnel *tun_key)
993 {
994         struct nlattr *a;
995         int rem;
996         bool ttl = false;
997
998         memset(tun_key, 0, sizeof(*tun_key));
999
1000         nla_for_each_nested(a, attr, rem) {
1001                 int type = nla_type(a);
1002                 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1003                         [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1004                         [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1005                         [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1006                         [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1007                         [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1008                         [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1009                         [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1010                 };
1011
1012                 if (type > OVS_TUNNEL_KEY_ATTR_MAX ||
1013                         ovs_tunnel_key_lens[type] != nla_len(a))
1014                         return -EINVAL;
1015
1016                 switch (type) {
1017                 case OVS_TUNNEL_KEY_ATTR_ID:
1018                         tun_key->tun_id = nla_get_be64(a);
1019                         tun_key->tun_flags |= OVS_TNL_F_KEY;
1020                         break;
1021                 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1022                         tun_key->ipv4_src = nla_get_be32(a);
1023                         break;
1024                 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1025                         tun_key->ipv4_dst = nla_get_be32(a);
1026                         break;
1027                 case OVS_TUNNEL_KEY_ATTR_TOS:
1028                         tun_key->ipv4_tos = nla_get_u8(a);
1029                         break;
1030                 case OVS_TUNNEL_KEY_ATTR_TTL:
1031                         tun_key->ipv4_ttl = nla_get_u8(a);
1032                         ttl = true;
1033                         break;
1034                 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1035                         tun_key->tun_flags |= OVS_TNL_F_DONT_FRAGMENT;
1036                         break;
1037                 case OVS_TUNNEL_KEY_ATTR_CSUM:
1038                         tun_key->tun_flags |= OVS_TNL_F_CSUM;
1039                         break;
1040                 default:
1041                         return -EINVAL;
1042
1043                 }
1044         }
1045         if (rem > 0)
1046                 return -EINVAL;
1047
1048         if (!tun_key->ipv4_dst)
1049                 return -EINVAL;
1050
1051         if (!ttl)
1052                 return -EINVAL;
1053
1054         return 0;
1055 }
1056
1057 int ipv4_tun_to_nlattr(struct sk_buff *skb,
1058                         const struct ovs_key_ipv4_tunnel *tun_key)
1059 {
1060         struct nlattr *nla;
1061
1062         nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1063         if (!nla)
1064                 return -EMSGSIZE;
1065
1066         if (tun_key->tun_flags & OVS_TNL_F_KEY &&
1067             nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, tun_key->tun_id))
1068                 return -EMSGSIZE;
1069         if (tun_key->ipv4_src &&
1070             nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, tun_key->ipv4_src))
1071                 return -EMSGSIZE;
1072         if (nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, tun_key->ipv4_dst))
1073                 return -EMSGSIZE;
1074         if (tun_key->ipv4_tos &&
1075             nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, tun_key->ipv4_tos))
1076                 return -EMSGSIZE;
1077         if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, tun_key->ipv4_ttl))
1078                 return -EMSGSIZE;
1079         if ((tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT) &&
1080                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1081                 return -EMSGSIZE;
1082         if ((tun_key->tun_flags & OVS_TNL_F_CSUM) &&
1083                 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1084                 return -EMSGSIZE;
1085
1086         nla_nest_end(skb, nla);
1087         return 0;
1088 }
1089
1090 /**
1091  * ovs_flow_from_nlattrs - parses Netlink attributes into a flow key.
1092  * @swkey: receives the extracted flow key.
1093  * @key_lenp: number of bytes used in @swkey.
1094  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1095  * sequence.
1096  */
1097 int ovs_flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
1098                       const struct nlattr *attr)
1099 {
1100         const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1101         const struct ovs_key_ethernet *eth_key;
1102         int key_len;
1103         u64 attrs;
1104         int err;
1105
1106         memset(swkey, 0, sizeof(struct sw_flow_key));
1107         key_len = SW_FLOW_KEY_OFFSET(eth);
1108
1109         err = parse_flow_nlattrs(attr, a, &attrs);
1110         if (err)
1111                 return err;
1112
1113         /* Metadata attributes. */
1114         if (attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1115                 swkey->phy.priority = nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]);
1116                 attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1117         }
1118         if (attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1119                 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1120                 if (in_port >= DP_MAX_PORTS)
1121                         return -EINVAL;
1122                 swkey->phy.in_port = in_port;
1123                 attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1124         } else {
1125                 swkey->phy.in_port = DP_MAX_PORTS;
1126         }
1127         if (attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1128                 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1129 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1130                 if (mark != 0)
1131                         return -EINVAL;
1132 #endif
1133                 swkey->phy.skb_mark = mark;
1134                 attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1135         }
1136
1137         if (attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1138                 err = ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], &swkey->tun_key);
1139                 if (err)
1140                         return err;
1141
1142                 attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1143         }
1144
1145         /* Data attributes. */
1146         if (!(attrs & (1 << OVS_KEY_ATTR_ETHERNET)))
1147                 return -EINVAL;
1148         attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1149
1150         eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1151         memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
1152         memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
1153
1154         if (attrs & (1u << OVS_KEY_ATTR_ETHERTYPE) &&
1155             nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q)) {
1156                 const struct nlattr *encap;
1157                 __be16 tci;
1158
1159                 if (attrs != ((1 << OVS_KEY_ATTR_VLAN) |
1160                               (1 << OVS_KEY_ATTR_ETHERTYPE) |
1161                               (1 << OVS_KEY_ATTR_ENCAP)))
1162                         return -EINVAL;
1163
1164                 encap = a[OVS_KEY_ATTR_ENCAP];
1165                 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1166                 if (tci & htons(VLAN_TAG_PRESENT)) {
1167                         swkey->eth.tci = tci;
1168
1169                         err = parse_flow_nlattrs(encap, a, &attrs);
1170                         if (err)
1171                                 return err;
1172                 } else if (!tci) {
1173                         /* Corner case for truncated 802.1Q header. */
1174                         if (nla_len(encap))
1175                                 return -EINVAL;
1176
1177                         swkey->eth.type = htons(ETH_P_8021Q);
1178                         *key_lenp = key_len;
1179                         return 0;
1180                 } else {
1181                         return -EINVAL;
1182                 }
1183         }
1184
1185         if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1186                 swkey->eth.type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1187                 if (ntohs(swkey->eth.type) < 1536)
1188                         return -EINVAL;
1189                 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1190         } else {
1191                 swkey->eth.type = htons(ETH_P_802_2);
1192         }
1193
1194         if (swkey->eth.type == htons(ETH_P_IP)) {
1195                 const struct ovs_key_ipv4 *ipv4_key;
1196
1197                 if (!(attrs & (1 << OVS_KEY_ATTR_IPV4)))
1198                         return -EINVAL;
1199                 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1200
1201                 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
1202                 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1203                 if (ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
1204                         return -EINVAL;
1205                 swkey->ip.proto = ipv4_key->ipv4_proto;
1206                 swkey->ip.tos = ipv4_key->ipv4_tos;
1207                 swkey->ip.ttl = ipv4_key->ipv4_ttl;
1208                 swkey->ip.frag = ipv4_key->ipv4_frag;
1209                 swkey->ipv4.addr.src = ipv4_key->ipv4_src;
1210                 swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
1211
1212                 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1213                         err = ipv4_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1214                         if (err)
1215                                 return err;
1216                 }
1217         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1218                 const struct ovs_key_ipv6 *ipv6_key;
1219
1220                 if (!(attrs & (1 << OVS_KEY_ATTR_IPV6)))
1221                         return -EINVAL;
1222                 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1223
1224                 key_len = SW_FLOW_KEY_OFFSET(ipv6.label);
1225                 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1226                 if (ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
1227                         return -EINVAL;
1228                 swkey->ipv6.label = ipv6_key->ipv6_label;
1229                 swkey->ip.proto = ipv6_key->ipv6_proto;
1230                 swkey->ip.tos = ipv6_key->ipv6_tclass;
1231                 swkey->ip.ttl = ipv6_key->ipv6_hlimit;
1232                 swkey->ip.frag = ipv6_key->ipv6_frag;
1233                 memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
1234                        sizeof(swkey->ipv6.addr.src));
1235                 memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
1236                        sizeof(swkey->ipv6.addr.dst));
1237
1238                 if (swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1239                         err = ipv6_flow_from_nlattrs(swkey, &key_len, a, &attrs);
1240                         if (err)
1241                                 return err;
1242                 }
1243         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1244                    swkey->eth.type == htons(ETH_P_RARP)) {
1245                 const struct ovs_key_arp *arp_key;
1246
1247                 if (!(attrs & (1 << OVS_KEY_ATTR_ARP)))
1248                         return -EINVAL;
1249                 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1250
1251                 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
1252                 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1253                 swkey->ipv4.addr.src = arp_key->arp_sip;
1254                 swkey->ipv4.addr.dst = arp_key->arp_tip;
1255                 if (arp_key->arp_op & htons(0xff00))
1256                         return -EINVAL;
1257                 swkey->ip.proto = ntohs(arp_key->arp_op);
1258                 memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
1259                 memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
1260         }
1261
1262         if (attrs)
1263                 return -EINVAL;
1264         *key_lenp = key_len;
1265
1266         return 0;
1267 }
1268
1269 /**
1270  * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1271  * @in_port: receives the extracted input port.
1272  * @tun_id: receives the extracted tunnel ID.
1273  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1274  * sequence.
1275  *
1276  * This parses a series of Netlink attributes that form a flow key, which must
1277  * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1278  * get the metadata, that is, the parts of the flow key that cannot be
1279  * extracted from the packet itself.
1280  */
1281
1282 int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow, int key_len, const struct nlattr *attr)
1283 {
1284         struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1285         const struct nlattr *nla;
1286         int rem;
1287
1288         flow->key.phy.in_port = DP_MAX_PORTS;
1289         flow->key.phy.priority = 0;
1290         flow->key.phy.skb_mark = 0;
1291         memset(tun_key, 0, sizeof(flow->key.tun_key));
1292
1293         nla_for_each_nested(nla, attr, rem) {
1294                 int type = nla_type(nla);
1295
1296                 if (type <= OVS_KEY_ATTR_MAX && ovs_key_lens[type] > 0) {
1297                         int err;
1298
1299                         if (nla_len(nla) != ovs_key_lens[type])
1300                                 return -EINVAL;
1301
1302                         switch (type) {
1303                         case OVS_KEY_ATTR_PRIORITY:
1304                                 flow->key.phy.priority = nla_get_u32(nla);
1305                                 break;
1306
1307                         case OVS_KEY_ATTR_TUNNEL:
1308                                 err = ipv4_tun_from_nlattr(nla, tun_key);
1309                                 if (err)
1310                                         return err;
1311                                 break;
1312
1313                         case OVS_KEY_ATTR_IN_PORT:
1314                                 if (nla_get_u32(nla) >= DP_MAX_PORTS)
1315                                         return -EINVAL;
1316                                 flow->key.phy.in_port = nla_get_u32(nla);
1317                                 break;
1318
1319                         case OVS_KEY_ATTR_SKB_MARK:
1320 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
1321                                 if (nla_get_u32(nla) != 0)
1322                                         return -EINVAL;
1323 #endif
1324                                 flow->key.phy.skb_mark = nla_get_u32(nla);
1325                                 break;
1326                         }
1327                 }
1328         }
1329         if (rem)
1330                 return -EINVAL;
1331
1332         flow->hash = ovs_flow_hash(&flow->key,
1333                                    flow_key_start(&flow->key), key_len);
1334
1335         return 0;
1336 }
1337
1338 int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
1339 {
1340         struct ovs_key_ethernet *eth_key;
1341         struct nlattr *nla, *encap;
1342
1343         if (swkey->phy.priority &&
1344             nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, swkey->phy.priority))
1345                 goto nla_put_failure;
1346
1347         if (swkey->tun_key.ipv4_dst &&
1348             ipv4_tun_to_nlattr(skb, &swkey->tun_key))
1349                 goto nla_put_failure;
1350
1351         if (swkey->phy.in_port != DP_MAX_PORTS &&
1352             nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, swkey->phy.in_port))
1353                 goto nla_put_failure;
1354
1355         if (swkey->phy.skb_mark &&
1356             nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, swkey->phy.skb_mark))
1357                 goto nla_put_failure;
1358
1359         nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1360         if (!nla)
1361                 goto nla_put_failure;
1362         eth_key = nla_data(nla);
1363         memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
1364         memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
1365
1366         if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1367                 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, htons(ETH_P_8021Q)) ||
1368                     nla_put_be16(skb, OVS_KEY_ATTR_VLAN, swkey->eth.tci))
1369                         goto nla_put_failure;
1370                 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1371                 if (!swkey->eth.tci)
1372                         goto unencap;
1373         } else {
1374                 encap = NULL;
1375         }
1376
1377         if (swkey->eth.type == htons(ETH_P_802_2))
1378                 goto unencap;
1379
1380         if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, swkey->eth.type))
1381                 goto nla_put_failure;
1382
1383         if (swkey->eth.type == htons(ETH_P_IP)) {
1384                 struct ovs_key_ipv4 *ipv4_key;
1385
1386                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1387                 if (!nla)
1388                         goto nla_put_failure;
1389                 ipv4_key = nla_data(nla);
1390                 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
1391                 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
1392                 ipv4_key->ipv4_proto = swkey->ip.proto;
1393                 ipv4_key->ipv4_tos = swkey->ip.tos;
1394                 ipv4_key->ipv4_ttl = swkey->ip.ttl;
1395                 ipv4_key->ipv4_frag = swkey->ip.frag;
1396         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1397                 struct ovs_key_ipv6 *ipv6_key;
1398
1399                 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1400                 if (!nla)
1401                         goto nla_put_failure;
1402                 ipv6_key = nla_data(nla);
1403                 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
1404                                 sizeof(ipv6_key->ipv6_src));
1405                 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
1406                                 sizeof(ipv6_key->ipv6_dst));
1407                 ipv6_key->ipv6_label = swkey->ipv6.label;
1408                 ipv6_key->ipv6_proto = swkey->ip.proto;
1409                 ipv6_key->ipv6_tclass = swkey->ip.tos;
1410                 ipv6_key->ipv6_hlimit = swkey->ip.ttl;
1411                 ipv6_key->ipv6_frag = swkey->ip.frag;
1412         } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1413                    swkey->eth.type == htons(ETH_P_RARP)) {
1414                 struct ovs_key_arp *arp_key;
1415
1416                 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1417                 if (!nla)
1418                         goto nla_put_failure;
1419                 arp_key = nla_data(nla);
1420                 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1421                 arp_key->arp_sip = swkey->ipv4.addr.src;
1422                 arp_key->arp_tip = swkey->ipv4.addr.dst;
1423                 arp_key->arp_op = htons(swkey->ip.proto);
1424                 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
1425                 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
1426         }
1427
1428         if ((swkey->eth.type == htons(ETH_P_IP) ||
1429              swkey->eth.type == htons(ETH_P_IPV6)) &&
1430              swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1431
1432                 if (swkey->ip.proto == IPPROTO_TCP) {
1433                         struct ovs_key_tcp *tcp_key;
1434
1435                         nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1436                         if (!nla)
1437                                 goto nla_put_failure;
1438                         tcp_key = nla_data(nla);
1439                         if (swkey->eth.type == htons(ETH_P_IP)) {
1440                                 tcp_key->tcp_src = swkey->ipv4.tp.src;
1441                                 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
1442                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1443                                 tcp_key->tcp_src = swkey->ipv6.tp.src;
1444                                 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
1445                         }
1446                 } else if (swkey->ip.proto == IPPROTO_UDP) {
1447                         struct ovs_key_udp *udp_key;
1448
1449                         nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1450                         if (!nla)
1451                                 goto nla_put_failure;
1452                         udp_key = nla_data(nla);
1453                         if (swkey->eth.type == htons(ETH_P_IP)) {
1454                                 udp_key->udp_src = swkey->ipv4.tp.src;
1455                                 udp_key->udp_dst = swkey->ipv4.tp.dst;
1456                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1457                                 udp_key->udp_src = swkey->ipv6.tp.src;
1458                                 udp_key->udp_dst = swkey->ipv6.tp.dst;
1459                         }
1460                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1461                            swkey->ip.proto == IPPROTO_ICMP) {
1462                         struct ovs_key_icmp *icmp_key;
1463
1464                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1465                         if (!nla)
1466                                 goto nla_put_failure;
1467                         icmp_key = nla_data(nla);
1468                         icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
1469                         icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
1470                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1471                            swkey->ip.proto == IPPROTO_ICMPV6) {
1472                         struct ovs_key_icmpv6 *icmpv6_key;
1473
1474                         nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1475                                                 sizeof(*icmpv6_key));
1476                         if (!nla)
1477                                 goto nla_put_failure;
1478                         icmpv6_key = nla_data(nla);
1479                         icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
1480                         icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
1481
1482                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1483                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1484                                 struct ovs_key_nd *nd_key;
1485
1486                                 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1487                                 if (!nla)
1488                                         goto nla_put_failure;
1489                                 nd_key = nla_data(nla);
1490                                 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1491                                                         sizeof(nd_key->nd_target));
1492                                 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1493                                 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1494                         }
1495                 }
1496         }
1497
1498 unencap:
1499         if (encap)
1500                 nla_nest_end(skb, encap);
1501
1502         return 0;
1503
1504 nla_put_failure:
1505         return -EMSGSIZE;
1506 }
1507
1508 /* Initializes the flow module.
1509  * Returns zero if successful or a negative error code. */
1510 int ovs_flow_init(void)
1511 {
1512         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1513                                         0, NULL);
1514         if (flow_cache == NULL)
1515                 return -ENOMEM;
1516
1517         return 0;
1518 }
1519
1520 /* Uninitializes the flow module. */
1521 void ovs_flow_exit(void)
1522 {
1523         kmem_cache_destroy(flow_cache);
1524 }