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