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