datapath: Report kernel's flow key when passing packets up to userspace.
[sliver-openvswitch.git] / datapath / flow.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include "flow.h"
10 #include "datapath.h"
11 #include <asm/uaccess.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/if_ether.h>
15 #include <linux/if_vlan.h>
16 #include <net/llc_pdu.h>
17 #include <linux/kernel.h>
18 #include <linux/jhash.h>
19 #include <linux/jiffies.h>
20 #include <linux/llc.h>
21 #include <linux/module.h>
22 #include <linux/in.h>
23 #include <linux/rcupdate.h>
24 #include <linux/if_arp.h>
25 #include <linux/if_ether.h>
26 #include <linux/ip.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/icmp.h>
30 #include <net/inet_ecn.h>
31 #include <net/ip.h>
32
33 static struct kmem_cache *flow_cache;
34 static unsigned int hash_seed __read_mostly;
35
36 static inline bool arphdr_ok(struct sk_buff *skb)
37 {
38         return skb->len >= skb_network_offset(skb) + sizeof(struct arp_eth_header);
39 }
40
41 static inline int check_iphdr(struct sk_buff *skb)
42 {
43         unsigned int nh_ofs = skb_network_offset(skb);
44         unsigned int ip_len;
45
46         if (skb->len < nh_ofs + sizeof(struct iphdr))
47                 return -EINVAL;
48
49         ip_len = ip_hdrlen(skb);
50         if (ip_len < sizeof(struct iphdr) || skb->len < nh_ofs + ip_len)
51                 return -EINVAL;
52
53         /*
54          * Pull enough header bytes to account for the IP header plus the
55          * longest transport header that we parse, currently 20 bytes for TCP.
56          */
57         if (!pskb_may_pull(skb, min(nh_ofs + ip_len + 20, skb->len)))
58                 return -ENOMEM;
59
60         skb_set_transport_header(skb, nh_ofs + ip_len);
61         return 0;
62 }
63
64 static inline bool tcphdr_ok(struct sk_buff *skb)
65 {
66         int th_ofs = skb_transport_offset(skb);
67         if (skb->len >= th_ofs + sizeof(struct tcphdr)) {
68                 int tcp_len = tcp_hdrlen(skb);
69                 return (tcp_len >= sizeof(struct tcphdr)
70                         && skb->len >= th_ofs + tcp_len);
71         }
72         return false;
73 }
74
75 static inline bool udphdr_ok(struct sk_buff *skb)
76 {
77         return skb->len >= skb_transport_offset(skb) + sizeof(struct udphdr);
78 }
79
80 static inline bool icmphdr_ok(struct sk_buff *skb)
81 {
82         return skb->len >= skb_transport_offset(skb) + sizeof(struct icmphdr);
83 }
84
85 #define TCP_FLAGS_OFFSET 13
86 #define TCP_FLAG_MASK 0x3f
87
88 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
89 {
90         u8 tcp_flags = 0;
91
92         if (flow->key.dl_type == htons(ETH_P_IP) &&
93             flow->key.nw_proto == IPPROTO_TCP) {
94                 u8 *tcp = (u8 *)tcp_hdr(skb);
95                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
96         }
97
98         spin_lock_bh(&flow->lock);
99         flow->used = jiffies;
100         flow->packet_count++;
101         flow->byte_count += skb->len;
102         flow->tcp_flags |= tcp_flags;
103         spin_unlock_bh(&flow->lock);
104 }
105
106 struct sw_flow_actions *flow_actions_alloc(u32 actions_len)
107 {
108         struct sw_flow_actions *sfa;
109
110         if (actions_len % NLA_ALIGNTO)
111                 return ERR_PTR(-EINVAL);
112
113         /* At least DP_MAX_PORTS actions are required to be able to flood a
114          * packet to every port.  Factor of 2 allows for setting VLAN tags,
115          * etc. */
116         if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
117                 return ERR_PTR(-EINVAL);
118
119         sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
120         if (!sfa)
121                 return ERR_PTR(-ENOMEM);
122
123         sfa->actions_len = actions_len;
124         return sfa;
125 }
126
127 struct sw_flow *flow_alloc(void)
128 {
129         struct sw_flow *flow;
130
131         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
132         if (!flow)
133                 return ERR_PTR(-ENOMEM);
134
135         spin_lock_init(&flow->lock);
136         atomic_set(&flow->refcnt, 1);
137         flow->dead = false;
138
139         return flow;
140 }
141
142 void flow_free_tbl(struct tbl_node *node)
143 {
144         struct sw_flow *flow = flow_cast(node);
145
146         flow->dead = true;
147         flow_put(flow);
148 }
149
150 /* RCU callback used by flow_deferred_free. */
151 static void rcu_free_flow_callback(struct rcu_head *rcu)
152 {
153         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
154
155         flow->dead = true;
156         flow_put(flow);
157 }
158
159 /* Schedules 'flow' to be freed after the next RCU grace period.
160  * The caller must hold rcu_read_lock for this to be sensible. */
161 void flow_deferred_free(struct sw_flow *flow)
162 {
163         call_rcu(&flow->rcu, rcu_free_flow_callback);
164 }
165
166 void flow_hold(struct sw_flow *flow)
167 {
168         atomic_inc(&flow->refcnt);
169 }
170
171 void flow_put(struct sw_flow *flow)
172 {
173         if (unlikely(!flow))
174                 return;
175
176         if (atomic_dec_and_test(&flow->refcnt)) {
177                 kfree((struct sf_flow_acts __force *)flow->sf_acts);
178                 kmem_cache_free(flow_cache, flow);
179         }
180 }
181
182 /* RCU callback used by flow_deferred_free_acts. */
183 static void rcu_free_acts_callback(struct rcu_head *rcu)
184 {
185         struct sw_flow_actions *sf_acts = container_of(rcu,
186                         struct sw_flow_actions, rcu);
187         kfree(sf_acts);
188 }
189
190 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
191  * The caller must hold rcu_read_lock for this to be sensible. */
192 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
193 {
194         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
195 }
196
197 static void parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
198 {
199         struct qtag_prefix {
200                 __be16 eth_type; /* ETH_P_8021Q */
201                 __be16 tci;
202         };
203         struct qtag_prefix *qp;
204
205         if (skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))
206                 return;
207
208         qp = (struct qtag_prefix *) skb->data;
209         key->dl_tci = qp->tci | htons(VLAN_TAG_PRESENT);
210         __skb_pull(skb, sizeof(struct qtag_prefix));
211 }
212
213 static __be16 parse_ethertype(struct sk_buff *skb)
214 {
215         struct llc_snap_hdr {
216                 u8  dsap;  /* Always 0xAA */
217                 u8  ssap;  /* Always 0xAA */
218                 u8  ctrl;
219                 u8  oui[3];
220                 __be16 ethertype;
221         };
222         struct llc_snap_hdr *llc;
223         __be16 proto;
224
225         proto = *(__be16 *) skb->data;
226         __skb_pull(skb, sizeof(__be16));
227
228         if (ntohs(proto) >= 1536)
229                 return proto;
230
231         if (unlikely(skb->len < sizeof(struct llc_snap_hdr)))
232                 return htons(ETH_P_802_2);
233
234         llc = (struct llc_snap_hdr *) skb->data;
235         if (llc->dsap != LLC_SAP_SNAP ||
236             llc->ssap != LLC_SAP_SNAP ||
237             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
238                 return htons(ETH_P_802_2);
239
240         __skb_pull(skb, sizeof(struct llc_snap_hdr));
241         return llc->ethertype;
242 }
243
244 /**
245  * flow_extract - extracts a flow key from an Ethernet frame.
246  * @skb: sk_buff that contains the frame, with skb->data pointing to the
247  * Ethernet header
248  * @in_port: port number on which @skb was received.
249  * @key: output flow key
250  * @is_frag: set to 1 if @skb contains an IPv4 fragment, or to 0 if @skb does
251  * not contain an IPv4 packet or if it is not a fragment.
252  *
253  * The caller must ensure that skb->len >= ETH_HLEN.
254  *
255  * Returns 0 if successful, otherwise a negative errno value.
256  *
257  * Initializes @skb header pointers as follows:
258  *
259  *    - skb->mac_header: the Ethernet header.
260  *
261  *    - skb->network_header: just past the Ethernet header, or just past the
262  *      VLAN header, to the first byte of the Ethernet payload.
263  *
264  *    - skb->transport_header: If key->dl_type is ETH_P_IP on output, then just
265  *      past the IPv4 header, if one is present and of a correct length,
266  *      otherwise the same as skb->network_header.  For other key->dl_type
267  *      values it is left untouched.
268  */
269 int flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
270                  bool *is_frag)
271 {
272         struct ethhdr *eth;
273
274         memset(key, 0, sizeof(*key));
275         key->tun_id = OVS_CB(skb)->tun_id;
276         key->in_port = in_port;
277         *is_frag = false;
278
279         /*
280          * We would really like to pull as many bytes as we could possibly
281          * want to parse into the linear data area.  Currently that is:
282          *
283          *    14     Ethernet header
284          *     4     VLAN header
285          *    60     max IP header with options
286          *    20     max TCP/UDP/ICMP header (don't care about options)
287          *    --
288          *    98
289          *
290          * But Xen only allocates 64 or 72 bytes for the linear data area in
291          * netback, which means that we would reallocate and copy the skb's
292          * linear data on every packet if we did that.  So instead just pull 64
293          * bytes, which is always sufficient without IP options, and then check
294          * whether we need to pull more later when we look at the IP header.
295          */
296         if (!pskb_may_pull(skb, min(skb->len, 64u)))
297                 return -ENOMEM;
298
299         skb_reset_mac_header(skb);
300
301         /* Link layer. */
302         eth = eth_hdr(skb);
303         memcpy(key->dl_src, eth->h_source, ETH_ALEN);
304         memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
305
306         /* dl_type, dl_vlan, dl_vlan_pcp. */
307         __skb_pull(skb, 2 * ETH_ALEN);
308         if (eth->h_proto == htons(ETH_P_8021Q))
309                 parse_vlan(skb, key);
310         key->dl_type = parse_ethertype(skb);
311         skb_reset_network_header(skb);
312         __skb_push(skb, skb->data - (unsigned char *)eth);
313
314         /* Network layer. */
315         if (key->dl_type == htons(ETH_P_IP)) {
316                 struct iphdr *nh;
317                 int error;
318
319                 error = check_iphdr(skb);
320                 if (unlikely(error)) {
321                         if (error == -EINVAL) {
322                                 skb->transport_header = skb->network_header;
323                                 return 0;
324                         }
325                         return error;
326                 }
327
328                 nh = ip_hdr(skb);
329                 key->nw_src = nh->saddr;
330                 key->nw_dst = nh->daddr;
331                 key->nw_tos = nh->tos & ~INET_ECN_MASK;
332                 key->nw_proto = nh->protocol;
333
334                 /* Transport layer. */
335                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET)) &&
336                     !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP)) {
337                         if (key->nw_proto == IPPROTO_TCP) {
338                                 if (tcphdr_ok(skb)) {
339                                         struct tcphdr *tcp = tcp_hdr(skb);
340                                         key->tp_src = tcp->source;
341                                         key->tp_dst = tcp->dest;
342                                 }
343                         } else if (key->nw_proto == IPPROTO_UDP) {
344                                 if (udphdr_ok(skb)) {
345                                         struct udphdr *udp = udp_hdr(skb);
346                                         key->tp_src = udp->source;
347                                         key->tp_dst = udp->dest;
348                                 }
349                         } else if (key->nw_proto == IPPROTO_ICMP) {
350                                 if (icmphdr_ok(skb)) {
351                                         struct icmphdr *icmp = icmp_hdr(skb);
352                                         /* The ICMP type and code fields use the 16-bit
353                                          * transport port fields, so we need to store them
354                                          * in 16-bit network byte order. */
355                                         key->tp_src = htons(icmp->type);
356                                         key->tp_dst = htons(icmp->code);
357                                 }
358                         }
359                 } else
360                         *is_frag = true;
361
362         } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
363                 struct arp_eth_header *arp;
364
365                 arp = (struct arp_eth_header *)skb_network_header(skb);
366
367                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
368                                 && arp->ar_pro == htons(ETH_P_IP)
369                                 && arp->ar_hln == ETH_ALEN
370                                 && arp->ar_pln == 4) {
371
372                         /* We only match on the lower 8 bits of the opcode. */
373                         if (ntohs(arp->ar_op) <= 0xff)
374                                 key->nw_proto = ntohs(arp->ar_op);
375
376                         if (key->nw_proto == ARPOP_REQUEST
377                                         || key->nw_proto == ARPOP_REPLY) {
378                                 memcpy(&key->nw_src, arp->ar_sip, sizeof(key->nw_src));
379                                 memcpy(&key->nw_dst, arp->ar_tip, sizeof(key->nw_dst));
380                         }
381                 }
382         }
383         return 0;
384 }
385
386 u32 flow_hash(const struct sw_flow_key *key)
387 {
388         return jhash2((u32*)key, sizeof(*key) / sizeof(u32), hash_seed);
389 }
390
391 int flow_cmp(const struct tbl_node *node, void *key2_)
392 {
393         const struct sw_flow_key *key1 = &flow_cast(node)->key;
394         const struct sw_flow_key *key2 = key2_;
395
396         return !memcmp(key1, key2, sizeof(struct sw_flow_key));
397 }
398
399 /**
400  * flow_from_nlattrs - parses Netlink attributes into a flow key.
401  * @swkey: receives the extracted flow key.
402  * @key: start of %ODP_KEY_ATTR_* Netlink attribute sequence.
403  * @key_len: number of bytes in @key.
404  *
405  * This state machine accepts the following forms, with [] for optional
406  * elements and | for alternatives:
407  *
408  * [tun_id] in_port ethernet [8021q] [ethertype [IP [TCP|UDP|ICMP] | ARP]
409  */
410 static int flow_from_nlattrs(struct sw_flow_key *swkey, const struct nlattr *key, u32 key_len)
411 {
412         const struct nlattr *nla;
413         u16 prev_type;
414         int rem;
415
416         memset(swkey, 0, sizeof(*swkey));
417         swkey->dl_type = htons(ETH_P_802_2);
418
419         prev_type = ODP_KEY_ATTR_UNSPEC;
420         nla_for_each_attr(nla, key, key_len, rem) {
421                 static const u32 key_lens[ODP_KEY_ATTR_MAX + 1] = {
422                         [ODP_KEY_ATTR_TUN_ID] = 8,
423                         [ODP_KEY_ATTR_IN_PORT] = 4,
424                         [ODP_KEY_ATTR_ETHERNET] = sizeof(struct odp_key_ethernet),
425                         [ODP_KEY_ATTR_8021Q] = sizeof(struct odp_key_8021q),
426                         [ODP_KEY_ATTR_ETHERTYPE] = 2,
427                         [ODP_KEY_ATTR_IPV4] = sizeof(struct odp_key_ipv4),
428                         [ODP_KEY_ATTR_TCP] = sizeof(struct odp_key_tcp),
429                         [ODP_KEY_ATTR_UDP] = sizeof(struct odp_key_udp),
430                         [ODP_KEY_ATTR_ICMP] = sizeof(struct odp_key_icmp),
431                         [ODP_KEY_ATTR_ARP] = sizeof(struct odp_key_arp),
432                 };
433
434                 const struct odp_key_ethernet *eth_key;
435                 const struct odp_key_8021q *q_key;
436                 const struct odp_key_ipv4 *ipv4_key;
437                 const struct odp_key_tcp *tcp_key;
438                 const struct odp_key_udp *udp_key;
439                 const struct odp_key_icmp *icmp_key;
440                 const struct odp_key_arp *arp_key;
441
442                 int type = nla_type(nla);
443
444                 if (type > ODP_KEY_ATTR_MAX || nla_len(nla) != key_lens[type])
445                         return -EINVAL;
446
447 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
448                 switch (TRANSITION(prev_type, type)) {
449                 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID):
450                         swkey->tun_id = nla_get_be64(nla);
451                         break;
452
453                 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT):
454                 case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT):
455                         if (nla_get_u32(nla) >= DP_MAX_PORTS)
456                                 return -EINVAL;
457                         swkey->in_port = nla_get_u32(nla);
458                         break;
459
460                 case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET):
461                         eth_key = nla_data(nla);
462                         memcpy(swkey->dl_src, eth_key->eth_src, ETH_ALEN);
463                         memcpy(swkey->dl_dst, eth_key->eth_dst, ETH_ALEN);
464                         break;
465
466                 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q):
467                         q_key = nla_data(nla);
468                         /* Only standard 0x8100 VLANs currently supported. */
469                         if (q_key->q_tpid != htons(ETH_P_8021Q))
470                                 return -EINVAL;
471                         if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
472                                 return -EINVAL;
473                         swkey->dl_tci = q_key->q_tci | htons(VLAN_TAG_PRESENT);
474                         break;
475
476                 case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE):
477                 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE):
478                         swkey->dl_type = nla_get_be16(nla);
479                         if (ntohs(swkey->dl_type) < 1536)
480                                 return -EINVAL;
481                         break;
482
483                 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4):
484                         if (swkey->dl_type != htons(ETH_P_IP))
485                                 return -EINVAL;
486                         ipv4_key = nla_data(nla);
487                         swkey->nw_src = ipv4_key->ipv4_src;
488                         swkey->nw_dst = ipv4_key->ipv4_dst;
489                         swkey->nw_proto = ipv4_key->ipv4_proto;
490                         swkey->nw_tos = ipv4_key->ipv4_tos;
491                         if (swkey->nw_tos & INET_ECN_MASK)
492                                 return -EINVAL;
493                         break;
494
495                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP):
496                         if (swkey->nw_proto != IPPROTO_TCP)
497                                 return -EINVAL;
498                         tcp_key = nla_data(nla);
499                         swkey->tp_src = tcp_key->tcp_src;
500                         swkey->tp_dst = tcp_key->tcp_dst;
501                         break;
502
503                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP):
504                         if (swkey->nw_proto != IPPROTO_UDP)
505                                 return -EINVAL;
506                         udp_key = nla_data(nla);
507                         swkey->tp_src = udp_key->udp_src;
508                         swkey->tp_dst = udp_key->udp_dst;
509                         break;
510
511                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP):
512                         if (swkey->nw_proto != IPPROTO_ICMP)
513                                 return -EINVAL;
514                         icmp_key = nla_data(nla);
515                         swkey->tp_src = htons(icmp_key->icmp_type);
516                         swkey->tp_dst = htons(icmp_key->icmp_code);
517                         break;
518
519                 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP):
520                         if (swkey->dl_type != htons(ETH_P_ARP))
521                                 return -EINVAL;
522                         arp_key = nla_data(nla);
523                         swkey->nw_src = arp_key->arp_sip;
524                         swkey->nw_dst = arp_key->arp_tip;
525                         if (arp_key->arp_op & htons(0xff00))
526                                 return -EINVAL;
527                         swkey->nw_proto = ntohs(arp_key->arp_op);
528                         break;
529
530                 default:
531                         return -EINVAL;
532                 }
533
534                 prev_type = type;
535         }
536         if (rem)
537                 return -EINVAL;
538
539         switch (prev_type) {
540         case ODP_KEY_ATTR_UNSPEC:
541                 return -EINVAL;
542
543         case ODP_KEY_ATTR_TUN_ID:
544         case ODP_KEY_ATTR_IN_PORT:
545                 return -EINVAL;
546
547         case ODP_KEY_ATTR_ETHERNET:
548         case ODP_KEY_ATTR_8021Q:
549                 return 0;
550
551         case ODP_KEY_ATTR_ETHERTYPE:
552                 if (swkey->dl_type == htons(ETH_P_IP) ||
553                     swkey->dl_type == htons(ETH_P_ARP))
554                         return -EINVAL;
555                 return 0;
556
557         case ODP_KEY_ATTR_IPV4:
558                 if (swkey->nw_proto == IPPROTO_TCP ||
559                     swkey->nw_proto == IPPROTO_UDP ||
560                     swkey->nw_proto == IPPROTO_ICMP)
561                         return -EINVAL;
562                 return 0;
563
564         case ODP_KEY_ATTR_TCP:
565         case ODP_KEY_ATTR_UDP:
566         case ODP_KEY_ATTR_ICMP:
567         case ODP_KEY_ATTR_ARP:
568                 return 0;
569         }
570
571         WARN_ON_ONCE(1);
572         return -EINVAL;
573 }
574
575 u32 flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
576 {
577         struct odp_key_ethernet *eth_key;
578
579         if (skb_tailroom(skb) < FLOW_BUFSIZE)
580                 return -EMSGSIZE;
581
582         if (swkey->tun_id != cpu_to_be64(0))
583                 nla_put_be64(skb, ODP_KEY_ATTR_TUN_ID, swkey->tun_id);
584
585         nla_put_u32(skb, ODP_KEY_ATTR_IN_PORT, swkey->in_port);
586
587         eth_key = nla_data(__nla_reserve(skb, ODP_KEY_ATTR_ETHERNET, sizeof(*eth_key)));
588         memcpy(eth_key->eth_src, swkey->dl_src, ETH_ALEN);
589         memcpy(eth_key->eth_dst, swkey->dl_dst, ETH_ALEN);
590
591         if (swkey->dl_tci != htons(0)) {
592                 struct odp_key_8021q *q_key;
593
594                 q_key = nla_data(__nla_reserve(skb, ODP_KEY_ATTR_8021Q, sizeof(*q_key)));
595                 q_key->q_tpid = htons(ETH_P_8021Q);
596                 q_key->q_tci = swkey->dl_tci & ~htons(VLAN_TAG_PRESENT);
597         }
598
599         if (swkey->dl_type == htons(ETH_P_802_2))
600                 goto exit;
601
602         nla_put_be16(skb, ODP_KEY_ATTR_ETHERTYPE, swkey->dl_type);
603
604         if (swkey->dl_type == htons(ETH_P_IP)) {
605                 struct odp_key_ipv4 *ipv4_key;
606
607                 ipv4_key = nla_data(__nla_reserve(skb, ODP_KEY_ATTR_IPV4, sizeof(*ipv4_key)));
608                 ipv4_key->ipv4_src = swkey->nw_src;
609                 ipv4_key->ipv4_dst = swkey->nw_dst;
610                 ipv4_key->ipv4_proto = swkey->nw_proto;
611                 ipv4_key->ipv4_tos = swkey->nw_tos;
612
613                 if (swkey->nw_proto == IPPROTO_TCP) {
614                         struct odp_key_tcp *tcp_key;
615
616                         tcp_key = nla_data(__nla_reserve(skb, ODP_KEY_ATTR_TCP, sizeof(*tcp_key)));
617                         tcp_key->tcp_src = swkey->tp_src;
618                         tcp_key->tcp_dst = swkey->tp_dst;
619                 } else if (swkey->nw_proto == IPPROTO_UDP) {
620                         struct odp_key_udp *udp_key;
621
622                         udp_key = nla_data(__nla_reserve(skb, ODP_KEY_ATTR_UDP, sizeof(*udp_key)));
623                         udp_key->udp_src = swkey->tp_src;
624                         udp_key->udp_dst = swkey->tp_dst;
625                 } else if (swkey->nw_proto == IPPROTO_ICMP) {
626                         struct odp_key_icmp *icmp_key;
627
628                         icmp_key = nla_data(__nla_reserve(skb, ODP_KEY_ATTR_ICMP, sizeof(*icmp_key)));
629                         icmp_key->icmp_type = ntohs(swkey->tp_src);
630                         icmp_key->icmp_code = ntohs(swkey->tp_dst);
631                 }
632         } else if (swkey->dl_type == htons(ETH_P_ARP)) {
633                 struct odp_key_arp *arp_key;
634
635                 arp_key = nla_data(__nla_reserve(skb, ODP_KEY_ATTR_ARP, sizeof(*arp_key)));
636                 arp_key->arp_sip = swkey->nw_src;
637                 arp_key->arp_tip = swkey->nw_dst;
638                 arp_key->arp_op = htons(swkey->nw_proto);
639         }
640
641 exit:
642         return skb->len;
643 }
644
645 int flow_copy_from_user(struct sw_flow_key *swkey, const struct nlattr __user *ukey, u32 ukey_len)
646 {
647         char key[FLOW_BUFSIZE] __aligned(NLA_ALIGNTO);
648
649         if (ukey_len > FLOW_BUFSIZE || ukey_len % NLA_ALIGNTO)
650                 return -EINVAL;
651
652         if (copy_from_user(key, ukey, ukey_len))
653                 return -EFAULT;
654
655         return flow_from_nlattrs(swkey, (const struct nlattr *)key, ukey_len);
656 }
657
658 int flow_copy_to_user(struct nlattr __user *ukey, const struct sw_flow_key *swkey, u32 ukey_len)
659 {
660         struct sk_buff *skb;
661         int retval;
662
663         skb = alloc_skb(FLOW_BUFSIZE, GFP_KERNEL);
664         if (!skb)
665                 return -ENOMEM;
666
667         retval = flow_to_nlattrs(swkey, skb);
668         if (copy_to_user(ukey, skb->data, min(skb->len, ukey_len)))
669                 retval = -EFAULT;
670         kfree_skb(skb);
671
672         return retval;
673 }
674
675 /* Initializes the flow module.
676  * Returns zero if successful or a negative error code. */
677 int flow_init(void)
678 {
679         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
680                                         0, NULL);
681         if (flow_cache == NULL)
682                 return -ENOMEM;
683
684         get_random_bytes(&hash_seed, sizeof(hash_seed));
685
686         return 0;
687 }
688
689 /* Uninitializes the flow module. */
690 void flow_exit(void)
691 {
692         kmem_cache_destroy(flow_cache);
693 }