cfm: Log sequence number of incoming CCMs.
[openvswitch] / 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/ipv6.h>
28 #include <linux/tcp.h>
29 #include <linux/udp.h>
30 #include <linux/icmp.h>
31 #include <linux/icmpv6.h>
32 #include <net/inet_ecn.h>
33 #include <net/ip.h>
34 #include <net/ipv6.h>
35 #include <net/ndisc.h>
36
37 #include "vlan.h"
38
39 static struct kmem_cache *flow_cache;
40 static unsigned int hash_seed __read_mostly;
41
42 static int check_header(struct sk_buff *skb, int len)
43 {
44         if (unlikely(skb->len < len))
45                 return -EINVAL;
46         if (unlikely(!pskb_may_pull(skb, len)))
47                 return -ENOMEM;
48         return 0;
49 }
50
51 static inline bool arphdr_ok(struct sk_buff *skb)
52 {
53         return pskb_may_pull(skb, skb_network_offset(skb) +
54                                   sizeof(struct arp_eth_header));
55 }
56
57 static inline int check_iphdr(struct sk_buff *skb)
58 {
59         unsigned int nh_ofs = skb_network_offset(skb);
60         unsigned int ip_len;
61         int err;
62
63         err = check_header(skb, nh_ofs + sizeof(struct iphdr));
64         if (unlikely(err))
65                 return err;
66
67         ip_len = ip_hdrlen(skb);
68         if (unlikely(ip_len < sizeof(struct iphdr) ||
69                      skb->len < nh_ofs + ip_len))
70                 return -EINVAL;
71
72         skb_set_transport_header(skb, nh_ofs + ip_len);
73         return 0;
74 }
75
76 static inline bool tcphdr_ok(struct sk_buff *skb)
77 {
78         int th_ofs = skb_transport_offset(skb);
79         int tcp_len;
80
81         if (unlikely(!pskb_may_pull(skb, th_ofs + sizeof(struct tcphdr))))
82                 return false;
83
84         tcp_len = tcp_hdrlen(skb);
85         if (unlikely(tcp_len < sizeof(struct tcphdr) ||
86                      skb->len < th_ofs + tcp_len))
87                 return false;
88
89         return true;
90 }
91
92 static inline bool udphdr_ok(struct sk_buff *skb)
93 {
94         return pskb_may_pull(skb, skb_transport_offset(skb) +
95                                   sizeof(struct udphdr));
96 }
97
98 static inline bool icmphdr_ok(struct sk_buff *skb)
99 {
100         return pskb_may_pull(skb, skb_transport_offset(skb) +
101                                   sizeof(struct icmphdr));
102 }
103
104 u64 flow_used_time(unsigned long flow_jiffies)
105 {
106         struct timespec cur_ts;
107         u64 cur_ms, idle_ms;
108
109         ktime_get_ts(&cur_ts);
110         idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
111         cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
112                  cur_ts.tv_nsec / NSEC_PER_MSEC;
113
114         return cur_ms - idle_ms;
115 }
116
117 #define SW_FLOW_KEY_OFFSET(field)               \
118         offsetof(struct sw_flow_key, field) +   \
119         FIELD_SIZEOF(struct sw_flow_key, field)
120
121 static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key,
122                          int *key_lenp)
123 {
124         unsigned int nh_ofs = skb_network_offset(skb);
125         unsigned int nh_len;
126         int payload_ofs;
127         struct ipv6hdr *nh;
128         uint8_t nexthdr;
129         int err;
130
131         *key_lenp = SW_FLOW_KEY_OFFSET(ipv6.addr);
132
133         err = check_header(skb, nh_ofs + sizeof(*nh));
134         if (unlikely(err))
135                 return err;
136
137         nh = ipv6_hdr(skb);
138         nexthdr = nh->nexthdr;
139         payload_ofs = (u8 *)(nh + 1) - skb->data;
140
141         key->ip.nw_proto = NEXTHDR_NONE;
142         key->ip.nw_tos = ipv6_get_dsfield(nh) & ~INET_ECN_MASK;
143         ipv6_addr_copy(&key->ipv6.addr.src, &nh->saddr);
144         ipv6_addr_copy(&key->ipv6.addr.dst, &nh->daddr);
145
146         payload_ofs = ipv6_skip_exthdr(skb, payload_ofs, &nexthdr);
147         if (unlikely(payload_ofs < 0))
148                 return -EINVAL;
149
150         nh_len = payload_ofs - nh_ofs;
151         skb_set_transport_header(skb, nh_ofs + nh_len);
152         key->ip.nw_proto = nexthdr;
153         return nh_len;
154 }
155
156 static bool icmp6hdr_ok(struct sk_buff *skb)
157 {
158         return pskb_may_pull(skb, skb_transport_offset(skb) +
159                                   sizeof(struct icmp6hdr));
160 }
161
162 #define TCP_FLAGS_OFFSET 13
163 #define TCP_FLAG_MASK 0x3f
164
165 void flow_used(struct sw_flow *flow, struct sk_buff *skb)
166 {
167         u8 tcp_flags = 0;
168
169         if (flow->key.eth.type == htons(ETH_P_IP) &&
170             flow->key.ip.nw_proto == IPPROTO_TCP) {
171                 u8 *tcp = (u8 *)tcp_hdr(skb);
172                 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
173         }
174
175         spin_lock_bh(&flow->lock);
176         flow->used = jiffies;
177         flow->packet_count++;
178         flow->byte_count += skb->len;
179         flow->tcp_flags |= tcp_flags;
180         spin_unlock_bh(&flow->lock);
181 }
182
183 struct sw_flow_actions *flow_actions_alloc(const struct nlattr *actions)
184 {
185         int actions_len = nla_len(actions);
186         struct sw_flow_actions *sfa;
187
188         /* At least DP_MAX_PORTS actions are required to be able to flood a
189          * packet to every port.  Factor of 2 allows for setting VLAN tags,
190          * etc. */
191         if (actions_len > 2 * DP_MAX_PORTS * nla_total_size(4))
192                 return ERR_PTR(-EINVAL);
193
194         sfa = kmalloc(sizeof(*sfa) + actions_len, GFP_KERNEL);
195         if (!sfa)
196                 return ERR_PTR(-ENOMEM);
197
198         sfa->actions_len = actions_len;
199         memcpy(sfa->actions, nla_data(actions), actions_len);
200         return sfa;
201 }
202
203 struct sw_flow *flow_alloc(void)
204 {
205         struct sw_flow *flow;
206
207         flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
208         if (!flow)
209                 return ERR_PTR(-ENOMEM);
210
211         spin_lock_init(&flow->lock);
212         atomic_set(&flow->refcnt, 1);
213         flow->sf_acts = NULL;
214         flow->dead = false;
215
216         return flow;
217 }
218
219 void flow_free_tbl(struct tbl_node *node)
220 {
221         struct sw_flow *flow = flow_cast(node);
222
223         flow->dead = true;
224         flow_put(flow);
225 }
226
227 /* RCU callback used by flow_deferred_free. */
228 static void rcu_free_flow_callback(struct rcu_head *rcu)
229 {
230         struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
231
232         flow->dead = true;
233         flow_put(flow);
234 }
235
236 /* Schedules 'flow' to be freed after the next RCU grace period.
237  * The caller must hold rcu_read_lock for this to be sensible. */
238 void flow_deferred_free(struct sw_flow *flow)
239 {
240         call_rcu(&flow->rcu, rcu_free_flow_callback);
241 }
242
243 void flow_hold(struct sw_flow *flow)
244 {
245         atomic_inc(&flow->refcnt);
246 }
247
248 void flow_put(struct sw_flow *flow)
249 {
250         if (unlikely(!flow))
251                 return;
252
253         if (atomic_dec_and_test(&flow->refcnt)) {
254                 kfree((struct sf_flow_acts __force *)flow->sf_acts);
255                 kmem_cache_free(flow_cache, flow);
256         }
257 }
258
259 /* RCU callback used by flow_deferred_free_acts. */
260 static void rcu_free_acts_callback(struct rcu_head *rcu)
261 {
262         struct sw_flow_actions *sf_acts = container_of(rcu,
263                         struct sw_flow_actions, rcu);
264         kfree(sf_acts);
265 }
266
267 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
268  * The caller must hold rcu_read_lock for this to be sensible. */
269 void flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
270 {
271         call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
272 }
273
274 static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
275 {
276         struct qtag_prefix {
277                 __be16 eth_type; /* ETH_P_8021Q */
278                 __be16 tci;
279         };
280         struct qtag_prefix *qp;
281
282         if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
283                                          sizeof(__be16))))
284                 return -ENOMEM;
285
286         qp = (struct qtag_prefix *) skb->data;
287         key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
288         __skb_pull(skb, sizeof(struct qtag_prefix));
289
290         return 0;
291 }
292
293 static __be16 parse_ethertype(struct sk_buff *skb)
294 {
295         struct llc_snap_hdr {
296                 u8  dsap;  /* Always 0xAA */
297                 u8  ssap;  /* Always 0xAA */
298                 u8  ctrl;
299                 u8  oui[3];
300                 __be16 ethertype;
301         };
302         struct llc_snap_hdr *llc;
303         __be16 proto;
304
305         proto = *(__be16 *) skb->data;
306         __skb_pull(skb, sizeof(__be16));
307
308         if (ntohs(proto) >= 1536)
309                 return proto;
310
311         if (skb->len < sizeof(struct llc_snap_hdr))
312                 return htons(ETH_P_802_2);
313
314         if (unlikely(!pskb_may_pull(skb, sizeof(struct llc_snap_hdr))))
315                 return htons(0);
316
317         llc = (struct llc_snap_hdr *) skb->data;
318         if (llc->dsap != LLC_SAP_SNAP ||
319             llc->ssap != LLC_SAP_SNAP ||
320             (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
321                 return htons(ETH_P_802_2);
322
323         __skb_pull(skb, sizeof(struct llc_snap_hdr));
324         return llc->ethertype;
325 }
326
327 static int parse_icmpv6(struct sk_buff *skb, struct sw_flow_key *key,
328                         int *key_lenp, int nh_len)
329 {
330         struct icmp6hdr *icmp = icmp6_hdr(skb);
331         int error = 0;
332         int key_len;
333
334         /* The ICMPv6 type and code fields use the 16-bit transport port
335          * fields, so we need to store them in 16-bit network byte order.
336          */
337         key->ipv6.tp.src = htons(icmp->icmp6_type);
338         key->ipv6.tp.dst = htons(icmp->icmp6_code);
339         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
340
341         if (icmp->icmp6_code == 0 &&
342             (icmp->icmp6_type == NDISC_NEIGHBOUR_SOLICITATION ||
343              icmp->icmp6_type == NDISC_NEIGHBOUR_ADVERTISEMENT)) {
344                 int icmp_len = skb->len - skb_transport_offset(skb);
345                 struct nd_msg *nd;
346                 int offset;
347
348                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
349
350                 /* In order to process neighbor discovery options, we need the
351                  * entire packet.
352                  */
353                 if (unlikely(icmp_len < sizeof(*nd)))
354                         goto out;
355                 if (unlikely(skb_linearize(skb))) {
356                         error = -ENOMEM;
357                         goto out;
358                 }
359
360                 nd = (struct nd_msg *)skb_transport_header(skb);
361                 ipv6_addr_copy(&key->ipv6.nd.target, &nd->target);
362                 key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
363
364                 icmp_len -= sizeof(*nd);
365                 offset = 0;
366                 while (icmp_len >= 8) {
367                         struct nd_opt_hdr *nd_opt = (struct nd_opt_hdr *)(nd->opt + offset);
368                         int opt_len = nd_opt->nd_opt_len * 8;
369
370                         if (unlikely(!opt_len || opt_len > icmp_len))
371                                 goto invalid;
372
373                         /* Store the link layer address if the appropriate
374                          * option is provided.  It is considered an error if
375                          * the same link layer option is specified twice.
376                          */
377                         if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LL_ADDR
378                             && opt_len == 8) {
379                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.sll)))
380                                         goto invalid;
381                                 memcpy(key->ipv6.nd.sll,
382                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
383                         } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LL_ADDR
384                                    && opt_len == 8) {
385                                 if (unlikely(!is_zero_ether_addr(key->ipv6.nd.tll)))
386                                         goto invalid;
387                                 memcpy(key->ipv6.nd.tll,
388                                     &nd->opt[offset+sizeof(*nd_opt)], ETH_ALEN);
389                         }
390
391                         icmp_len -= opt_len;
392                         offset += opt_len;
393                 }
394         }
395
396         goto out;
397
398 invalid:
399         memset(&key->ipv6.nd.target, 0, sizeof(key->ipv6.nd.target));
400         memset(key->ipv6.nd.sll, 0, sizeof(key->ipv6.nd.sll));
401         memset(key->ipv6.nd.tll, 0, sizeof(key->ipv6.nd.tll));
402
403 out:
404         *key_lenp = key_len;
405         return error;
406 }
407
408 /**
409  * flow_extract - extracts a flow key from an Ethernet frame.
410  * @skb: sk_buff that contains the frame, with skb->data pointing to the
411  * Ethernet header
412  * @in_port: port number on which @skb was received.
413  * @key: output flow key
414  * @key_lenp: length of output flow key
415  * @is_frag: set to 1 if @skb contains an IPv4 fragment, or to 0 if @skb does
416  * not contain an IPv4 packet or if it is not a fragment.
417  *
418  * The caller must ensure that skb->len >= ETH_HLEN.
419  *
420  * Returns 0 if successful, otherwise a negative errno value.
421  *
422  * Initializes @skb header pointers as follows:
423  *
424  *    - skb->mac_header: the Ethernet header.
425  *
426  *    - skb->network_header: just past the Ethernet header, or just past the
427  *      VLAN header, to the first byte of the Ethernet payload.
428  *
429  *    - skb->transport_header: If key->dl_type is ETH_P_IP or ETH_P_IPV6
430  *      on output, then just past the IP header, if one is present and
431  *      of a correct length, otherwise the same as skb->network_header.
432  *      For other key->dl_type values it is left untouched.
433  */
434 int flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key,
435                  int *key_lenp, bool *is_frag)
436 {
437         int error = 0;
438         int key_len = SW_FLOW_KEY_OFFSET(eth);
439         struct ethhdr *eth;
440
441         memset(key, 0, sizeof(*key));
442         key->eth.tun_id = OVS_CB(skb)->tun_id;
443         key->eth.in_port = in_port;
444         *is_frag = false;
445
446         skb_reset_mac_header(skb);
447
448         /* Link layer.  We are guaranteed to have at least the 14 byte Ethernet
449          * header in the linear data area.
450          */
451         eth = eth_hdr(skb);
452         memcpy(key->eth.src, eth->h_source, ETH_ALEN);
453         memcpy(key->eth.dst, eth->h_dest, ETH_ALEN);
454
455         __skb_pull(skb, 2 * ETH_ALEN);
456
457         if (vlan_tx_tag_present(skb))
458                 key->eth.tci = htons(vlan_get_tci(skb));
459         else if (eth->h_proto == htons(ETH_P_8021Q))
460                 if (unlikely(parse_vlan(skb, key)))
461                         return -ENOMEM;
462
463         key->eth.type = parse_ethertype(skb);
464         if (unlikely(key->eth.type == htons(0)))
465                 return -ENOMEM;
466
467         skb_reset_network_header(skb);
468         __skb_push(skb, skb->data - skb_mac_header(skb));
469
470         /* Network layer. */
471         if (key->eth.type == htons(ETH_P_IP)) {
472                 struct iphdr *nh;
473
474                 key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
475
476                 error = check_iphdr(skb);
477                 if (unlikely(error)) {
478                         if (error == -EINVAL) {
479                                 skb->transport_header = skb->network_header;
480                                 error = 0;
481                         }
482                         goto out;
483                 }
484
485                 nh = ip_hdr(skb);
486                 key->ipv4.addr.src = nh->saddr;
487                 key->ipv4.addr.dst = nh->daddr;
488                 key->ip.nw_tos = nh->tos & ~INET_ECN_MASK;
489                 key->ip.nw_proto = nh->protocol;
490
491                 /* Transport layer. */
492                 if (!(nh->frag_off & htons(IP_MF | IP_OFFSET)) &&
493                     !(skb_shinfo(skb)->gso_type & SKB_GSO_UDP)) {
494                         if (key->ip.nw_proto == IPPROTO_TCP) {
495                                 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
496                                 if (tcphdr_ok(skb)) {
497                                         struct tcphdr *tcp = tcp_hdr(skb);
498                                         key->ipv4.tp.src = tcp->source;
499                                         key->ipv4.tp.dst = tcp->dest;
500                                 }
501                         } else if (key->ip.nw_proto == IPPROTO_UDP) {
502                                 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
503                                 if (udphdr_ok(skb)) {
504                                         struct udphdr *udp = udp_hdr(skb);
505                                         key->ipv4.tp.src = udp->source;
506                                         key->ipv4.tp.dst = udp->dest;
507                                 }
508                         } else if (key->ip.nw_proto == IPPROTO_ICMP) {
509                                 key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
510                                 if (icmphdr_ok(skb)) {
511                                         struct icmphdr *icmp = icmp_hdr(skb);
512                                         /* The ICMP type and code fields use the 16-bit
513                                          * transport port fields, so we need to store them
514                                          * in 16-bit network byte order. */
515                                         key->ipv4.tp.src = htons(icmp->type);
516                                         key->ipv4.tp.dst = htons(icmp->code);
517                                 }
518                         }
519                 } else
520                         *is_frag = true;
521
522         } else if (key->eth.type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
523                 struct arp_eth_header *arp;
524
525                 arp = (struct arp_eth_header *)skb_network_header(skb);
526
527                 if (arp->ar_hrd == htons(ARPHRD_ETHER)
528                                 && arp->ar_pro == htons(ETH_P_IP)
529                                 && arp->ar_hln == ETH_ALEN
530                                 && arp->ar_pln == 4) {
531
532                         /* We only match on the lower 8 bits of the opcode. */
533                         if (ntohs(arp->ar_op) <= 0xff)
534                                 key->ip.nw_proto = ntohs(arp->ar_op);
535
536                         if (key->ip.nw_proto == ARPOP_REQUEST
537                                         || key->ip.nw_proto == ARPOP_REPLY) {
538                                 memcpy(&key->ipv4.addr.src, arp->ar_sip, sizeof(key->ipv4.addr.src));
539                                 memcpy(&key->ipv4.addr.dst, arp->ar_tip, sizeof(key->ipv4.addr.dst));
540                                 memcpy(key->ipv4.arp.sha, arp->ar_sha, ETH_ALEN);
541                                 memcpy(key->ipv4.arp.tha, arp->ar_tha, ETH_ALEN);
542                                 key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
543                         }
544                 }
545         } else if (key->eth.type == htons(ETH_P_IPV6)) {
546                 int nh_len;             /* IPv6 Header + Extensions */
547
548                 nh_len = parse_ipv6hdr(skb, key, &key_len);
549                 if (unlikely(nh_len < 0)) {
550                         if (nh_len == -EINVAL)
551                                 skb->transport_header = skb->network_header;
552                         else
553                                 error = nh_len;
554                         goto out;
555                 }
556
557                 /* Transport layer. */
558                 if (key->ip.nw_proto == NEXTHDR_TCP) {
559                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
560                         if (tcphdr_ok(skb)) {
561                                 struct tcphdr *tcp = tcp_hdr(skb);
562                                 key->ipv6.tp.src = tcp->source;
563                                 key->ipv6.tp.dst = tcp->dest;
564                         }
565                 } else if (key->ip.nw_proto == NEXTHDR_UDP) {
566                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
567                         if (udphdr_ok(skb)) {
568                                 struct udphdr *udp = udp_hdr(skb);
569                                 key->ipv6.tp.src = udp->source;
570                                 key->ipv6.tp.dst = udp->dest;
571                         }
572                 } else if (key->ip.nw_proto == NEXTHDR_ICMP) {
573                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
574                         if (icmp6hdr_ok(skb)) {
575                                 error = parse_icmpv6(skb, key, &key_len, nh_len);
576                                 if (error < 0)
577                                         goto out;
578                         }
579                 }
580         }
581
582 out:
583         *key_lenp = key_len;
584         return error;
585 }
586
587 u32 flow_hash(const struct sw_flow_key *key, int key_len)
588 {
589         return jhash2((u32*)key, DIV_ROUND_UP(key_len, sizeof(u32)), hash_seed);
590 }
591
592 int flow_cmp(const struct tbl_node *node, void *key2_, int len)
593 {
594         const struct sw_flow_key *key1 = &flow_cast(node)->key;
595         const struct sw_flow_key *key2 = key2_;
596
597         return !memcmp(key1, key2, len);
598 }
599
600 /**
601  * flow_from_nlattrs - parses Netlink attributes into a flow key.
602  * @swkey: receives the extracted flow key.
603  * @key_lenp: number of bytes used in @swkey.
604  * @attr: Netlink attribute holding nested %ODP_KEY_ATTR_* Netlink attribute
605  * sequence.
606  *
607  * This state machine accepts the following forms, with [] for optional
608  * elements and | for alternatives:
609  *
610  * [tun_id] in_port ethernet [8021q] [ethertype \
611  *              [IPv4 [TCP|UDP|ICMP] | IPv6 [TCP|UDP|ICMPv6 [ND]] | ARP]]
612  */
613 int flow_from_nlattrs(struct sw_flow_key *swkey, int *key_lenp,
614                       const struct nlattr *attr)
615 {
616         int error = 0;
617         const struct nlattr *nla;
618         u16 prev_type;
619         int rem;
620         int key_len;
621
622         memset(swkey, 0, sizeof(*swkey));
623         swkey->eth.type = htons(ETH_P_802_2);
624         key_len = SW_FLOW_KEY_OFFSET(eth);
625
626         prev_type = ODP_KEY_ATTR_UNSPEC;
627         nla_for_each_nested(nla, attr, rem) {
628                 static const u32 key_lens[ODP_KEY_ATTR_MAX + 1] = {
629                         [ODP_KEY_ATTR_TUN_ID] = 8,
630                         [ODP_KEY_ATTR_IN_PORT] = 4,
631                         [ODP_KEY_ATTR_ETHERNET] = sizeof(struct odp_key_ethernet),
632                         [ODP_KEY_ATTR_8021Q] = sizeof(struct odp_key_8021q),
633                         [ODP_KEY_ATTR_ETHERTYPE] = 2,
634                         [ODP_KEY_ATTR_IPV4] = sizeof(struct odp_key_ipv4),
635                         [ODP_KEY_ATTR_IPV6] = sizeof(struct odp_key_ipv6),
636                         [ODP_KEY_ATTR_TCP] = sizeof(struct odp_key_tcp),
637                         [ODP_KEY_ATTR_UDP] = sizeof(struct odp_key_udp),
638                         [ODP_KEY_ATTR_ICMP] = sizeof(struct odp_key_icmp),
639                         [ODP_KEY_ATTR_ICMPV6] = sizeof(struct odp_key_icmpv6),
640                         [ODP_KEY_ATTR_ARP] = sizeof(struct odp_key_arp),
641                         [ODP_KEY_ATTR_ND] = sizeof(struct odp_key_nd),
642                 };
643
644                 const struct odp_key_ethernet *eth_key;
645                 const struct odp_key_8021q *q_key;
646                 const struct odp_key_ipv4 *ipv4_key;
647                 const struct odp_key_ipv6 *ipv6_key;
648                 const struct odp_key_tcp *tcp_key;
649                 const struct odp_key_udp *udp_key;
650                 const struct odp_key_icmp *icmp_key;
651                 const struct odp_key_icmpv6 *icmpv6_key;
652                 const struct odp_key_arp *arp_key;
653                 const struct odp_key_nd *nd_key;
654
655                 int type = nla_type(nla);
656
657                 if (type > ODP_KEY_ATTR_MAX || nla_len(nla) != key_lens[type])
658                         goto invalid;
659
660 #define TRANSITION(PREV_TYPE, TYPE) (((PREV_TYPE) << 16) | (TYPE))
661                 switch (TRANSITION(prev_type, type)) {
662                 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_TUN_ID):
663                         swkey->eth.tun_id = nla_get_be64(nla);
664                         break;
665
666                 case TRANSITION(ODP_KEY_ATTR_UNSPEC, ODP_KEY_ATTR_IN_PORT):
667                 case TRANSITION(ODP_KEY_ATTR_TUN_ID, ODP_KEY_ATTR_IN_PORT):
668                         if (nla_get_u32(nla) >= DP_MAX_PORTS)
669                                 goto invalid;
670                         swkey->eth.in_port = nla_get_u32(nla);
671                         break;
672
673                 case TRANSITION(ODP_KEY_ATTR_IN_PORT, ODP_KEY_ATTR_ETHERNET):
674                         eth_key = nla_data(nla);
675                         memcpy(swkey->eth.src, eth_key->eth_src, ETH_ALEN);
676                         memcpy(swkey->eth.dst, eth_key->eth_dst, ETH_ALEN);
677                         break;
678
679                 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_8021Q):
680                         q_key = nla_data(nla);
681                         /* Only standard 0x8100 VLANs currently supported. */
682                         if (q_key->q_tpid != htons(ETH_P_8021Q))
683                                 goto invalid;
684                         if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
685                                 goto invalid;
686                         swkey->eth.tci = q_key->q_tci | htons(VLAN_TAG_PRESENT);
687                         break;
688
689                 case TRANSITION(ODP_KEY_ATTR_8021Q, ODP_KEY_ATTR_ETHERTYPE):
690                 case TRANSITION(ODP_KEY_ATTR_ETHERNET, ODP_KEY_ATTR_ETHERTYPE):
691                         swkey->eth.type = nla_get_be16(nla);
692                         if (ntohs(swkey->eth.type) < 1536)
693                                 goto invalid;
694                         break;
695
696                 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV4):
697                         key_len = SW_FLOW_KEY_OFFSET(ipv4.addr);
698                         if (swkey->eth.type != htons(ETH_P_IP))
699                                 goto invalid;
700                         ipv4_key = nla_data(nla);
701                         swkey->ip.nw_proto = ipv4_key->ipv4_proto;
702                         swkey->ip.nw_tos = ipv4_key->ipv4_tos;
703                         swkey->ipv4.addr.src = ipv4_key->ipv4_src;
704                         swkey->ipv4.addr.dst = ipv4_key->ipv4_dst;
705                         if (swkey->ip.nw_tos & INET_ECN_MASK)
706                                 goto invalid;
707                         break;
708
709                 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_IPV6):
710                         key_len = SW_FLOW_KEY_OFFSET(ipv6.addr);
711                         if (swkey->eth.type != htons(ETH_P_IPV6))
712                                 goto invalid;
713                         ipv6_key = nla_data(nla);
714                         swkey->ip.nw_proto = ipv6_key->ipv6_proto;
715                         swkey->ip.nw_tos = ipv6_key->ipv6_tos;
716                         memcpy(&swkey->ipv6.addr.src, ipv6_key->ipv6_src,
717                                         sizeof(swkey->ipv6.addr.src));
718                         memcpy(&swkey->ipv6.addr.dst, ipv6_key->ipv6_dst,
719                                         sizeof(swkey->ipv6.addr.dst));
720                         if (swkey->ip.nw_tos & INET_ECN_MASK)
721                                 goto invalid;
722                         break;
723
724                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_TCP):
725                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
726                         if (swkey->ip.nw_proto != IPPROTO_TCP)
727                                 goto invalid;
728                         tcp_key = nla_data(nla);
729                         swkey->ipv4.tp.src = tcp_key->tcp_src;
730                         swkey->ipv4.tp.dst = tcp_key->tcp_dst;
731                         break;
732
733                 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_TCP):
734                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
735                         if (swkey->ip.nw_proto != IPPROTO_TCP)
736                                 goto invalid;
737                         tcp_key = nla_data(nla);
738                         swkey->ipv6.tp.src = tcp_key->tcp_src;
739                         swkey->ipv6.tp.dst = tcp_key->tcp_dst;
740                         break;
741
742                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_UDP):
743                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
744                         if (swkey->ip.nw_proto != IPPROTO_UDP)
745                                 goto invalid;
746                         udp_key = nla_data(nla);
747                         swkey->ipv4.tp.src = udp_key->udp_src;
748                         swkey->ipv4.tp.dst = udp_key->udp_dst;
749                         break;
750
751                 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_UDP):
752                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
753                         if (swkey->ip.nw_proto != IPPROTO_UDP)
754                                 goto invalid;
755                         udp_key = nla_data(nla);
756                         swkey->ipv6.tp.src = udp_key->udp_src;
757                         swkey->ipv6.tp.dst = udp_key->udp_dst;
758                         break;
759
760                 case TRANSITION(ODP_KEY_ATTR_IPV4, ODP_KEY_ATTR_ICMP):
761                         key_len = SW_FLOW_KEY_OFFSET(ipv4.tp);
762                         if (swkey->ip.nw_proto != IPPROTO_ICMP)
763                                 goto invalid;
764                         icmp_key = nla_data(nla);
765                         swkey->ipv4.tp.src = htons(icmp_key->icmp_type);
766                         swkey->ipv4.tp.dst = htons(icmp_key->icmp_code);
767                         break;
768
769                 case TRANSITION(ODP_KEY_ATTR_IPV6, ODP_KEY_ATTR_ICMPV6):
770                         key_len = SW_FLOW_KEY_OFFSET(ipv6.tp);
771                         if (swkey->ip.nw_proto != IPPROTO_ICMPV6)
772                                 goto invalid;
773                         icmpv6_key = nla_data(nla);
774                         swkey->ipv6.tp.src = htons(icmpv6_key->icmpv6_type);
775                         swkey->ipv6.tp.dst = htons(icmpv6_key->icmpv6_code);
776                         break;
777
778                 case TRANSITION(ODP_KEY_ATTR_ETHERTYPE, ODP_KEY_ATTR_ARP):
779                         key_len = SW_FLOW_KEY_OFFSET(ipv4.arp);
780                         if (swkey->eth.type != htons(ETH_P_ARP))
781                                 goto invalid;
782                         arp_key = nla_data(nla);
783                         swkey->ipv4.addr.src = arp_key->arp_sip;
784                         swkey->ipv4.addr.dst = arp_key->arp_tip;
785                         if (arp_key->arp_op & htons(0xff00))
786                                 goto invalid;
787                         swkey->ip.nw_proto = ntohs(arp_key->arp_op);
788                         memcpy(swkey->ipv4.arp.sha, arp_key->arp_sha, ETH_ALEN);
789                         memcpy(swkey->ipv4.arp.tha, arp_key->arp_tha, ETH_ALEN);
790                         break;
791
792                 case TRANSITION(ODP_KEY_ATTR_ICMPV6, ODP_KEY_ATTR_ND):
793                         key_len = SW_FLOW_KEY_OFFSET(ipv6.nd);
794                         if (swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_SOLICITATION)
795                             && swkey->ipv6.tp.src != htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
796                                 goto invalid;
797                         nd_key = nla_data(nla);
798                         memcpy(&swkey->ipv6.nd.target, nd_key->nd_target,
799                                         sizeof(swkey->ipv6.nd.target));
800                         memcpy(swkey->ipv6.nd.sll, nd_key->nd_sll, ETH_ALEN);
801                         memcpy(swkey->ipv6.nd.tll, nd_key->nd_tll, ETH_ALEN);
802                         break;
803
804                 default:
805                         goto invalid;
806                 }
807
808                 prev_type = type;
809         }
810         if (rem)
811                 goto invalid;
812
813         switch (prev_type) {
814         case ODP_KEY_ATTR_UNSPEC:
815                 goto invalid;
816
817         case ODP_KEY_ATTR_TUN_ID:
818         case ODP_KEY_ATTR_IN_PORT:
819                 goto invalid;
820
821         case ODP_KEY_ATTR_ETHERNET:
822         case ODP_KEY_ATTR_8021Q:
823                 goto ok;
824
825         case ODP_KEY_ATTR_ETHERTYPE:
826                 if (swkey->eth.type == htons(ETH_P_IP) ||
827                     swkey->eth.type == htons(ETH_P_ARP))
828                         goto invalid;
829                 goto ok;
830
831         case ODP_KEY_ATTR_IPV4:
832                 if (swkey->ip.nw_proto == IPPROTO_TCP ||
833                     swkey->ip.nw_proto == IPPROTO_UDP ||
834                     swkey->ip.nw_proto == IPPROTO_ICMP)
835                         goto invalid;
836                 goto ok;
837
838         case ODP_KEY_ATTR_IPV6:
839                 if (swkey->ip.nw_proto == IPPROTO_TCP ||
840                     swkey->ip.nw_proto == IPPROTO_UDP ||
841                     swkey->ip.nw_proto == IPPROTO_ICMPV6)
842                         goto invalid;
843                 goto ok;
844
845         case ODP_KEY_ATTR_ICMPV6:
846                 if (swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
847                     swkey->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT))
848                         goto invalid;
849                 goto ok;
850
851         case ODP_KEY_ATTR_TCP:
852         case ODP_KEY_ATTR_UDP:
853         case ODP_KEY_ATTR_ICMP:
854         case ODP_KEY_ATTR_ARP:
855         case ODP_KEY_ATTR_ND:
856                 goto ok;
857
858         default:
859                 WARN_ON_ONCE(1);
860         }
861
862 invalid:
863         error = -EINVAL;
864
865 ok:
866         WARN_ON_ONCE(!key_len && !error);
867         *key_lenp = key_len;
868         return error;
869 }
870
871 int flow_to_nlattrs(const struct sw_flow_key *swkey, struct sk_buff *skb)
872 {
873         struct odp_key_ethernet *eth_key;
874         struct nlattr *nla;
875
876         /* This is an imperfect sanity-check that FLOW_BUFSIZE doesn't need
877          * to be updated, but will at least raise awareness when new ODP key
878          * types are added. */
879         BUILD_BUG_ON(__ODP_KEY_ATTR_MAX != 14);
880
881         if (swkey->eth.tun_id != cpu_to_be64(0))
882                 NLA_PUT_BE64(skb, ODP_KEY_ATTR_TUN_ID, swkey->eth.tun_id);
883
884         NLA_PUT_U32(skb, ODP_KEY_ATTR_IN_PORT, swkey->eth.in_port);
885
886         nla = nla_reserve(skb, ODP_KEY_ATTR_ETHERNET, sizeof(*eth_key));
887         if (!nla)
888                 goto nla_put_failure;
889         eth_key = nla_data(nla);
890         memcpy(eth_key->eth_src, swkey->eth.src, ETH_ALEN);
891         memcpy(eth_key->eth_dst, swkey->eth.dst, ETH_ALEN);
892
893         if (swkey->eth.tci != htons(0)) {
894                 struct odp_key_8021q q_key;
895
896                 q_key.q_tpid = htons(ETH_P_8021Q);
897                 q_key.q_tci = swkey->eth.tci & ~htons(VLAN_TAG_PRESENT);
898                 NLA_PUT(skb, ODP_KEY_ATTR_8021Q, sizeof(q_key), &q_key);
899         }
900
901         if (swkey->eth.type == htons(ETH_P_802_2))
902                 return 0;
903
904         NLA_PUT_BE16(skb, ODP_KEY_ATTR_ETHERTYPE, swkey->eth.type);
905
906         if (swkey->eth.type == htons(ETH_P_IP)) {
907                 struct odp_key_ipv4 *ipv4_key;
908
909                 nla = nla_reserve(skb, ODP_KEY_ATTR_IPV4, sizeof(*ipv4_key));
910                 if (!nla)
911                         goto nla_put_failure;
912                 ipv4_key = nla_data(nla);
913                 memset(ipv4_key, 0, sizeof(struct odp_key_ipv4));
914                 ipv4_key->ipv4_src = swkey->ipv4.addr.src;
915                 ipv4_key->ipv4_dst = swkey->ipv4.addr.dst;
916                 ipv4_key->ipv4_proto = swkey->ip.nw_proto;
917                 ipv4_key->ipv4_tos = swkey->ip.nw_tos;
918         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
919                 struct odp_key_ipv6 *ipv6_key;
920
921                 nla = nla_reserve(skb, ODP_KEY_ATTR_IPV6, sizeof(*ipv6_key));
922                 if (!nla)
923                         goto nla_put_failure;
924                 ipv6_key = nla_data(nla);
925                 memset(ipv6_key, 0, sizeof(struct odp_key_ipv6));
926                 memcpy(ipv6_key->ipv6_src, &swkey->ipv6.addr.src,
927                                 sizeof(ipv6_key->ipv6_src));
928                 memcpy(ipv6_key->ipv6_dst, &swkey->ipv6.addr.dst,
929                                 sizeof(ipv6_key->ipv6_dst));
930                 ipv6_key->ipv6_proto = swkey->ip.nw_proto;
931                 ipv6_key->ipv6_tos = swkey->ip.nw_tos;
932         } else if (swkey->eth.type == htons(ETH_P_ARP)) {
933                 struct odp_key_arp *arp_key;
934
935                 nla = nla_reserve(skb, ODP_KEY_ATTR_ARP, sizeof(*arp_key));
936                 if (!nla)
937                         goto nla_put_failure;
938                 arp_key = nla_data(nla);
939                 memset(arp_key, 0, sizeof(struct odp_key_arp));
940                 arp_key->arp_sip = swkey->ipv4.addr.src;
941                 arp_key->arp_tip = swkey->ipv4.addr.dst;
942                 arp_key->arp_op = htons(swkey->ip.nw_proto);
943                 memcpy(arp_key->arp_sha, swkey->ipv4.arp.sha, ETH_ALEN);
944                 memcpy(arp_key->arp_tha, swkey->ipv4.arp.tha, ETH_ALEN);
945         }
946
947         if (swkey->eth.type == htons(ETH_P_IP) ||
948             swkey->eth.type == htons(ETH_P_IPV6)) {
949
950                 if (swkey->ip.nw_proto == IPPROTO_TCP) {
951                         struct odp_key_tcp *tcp_key;
952
953                         nla = nla_reserve(skb, ODP_KEY_ATTR_TCP, sizeof(*tcp_key));
954                         if (!nla)
955                                 goto nla_put_failure;
956                         tcp_key = nla_data(nla);
957                         if (swkey->eth.type == htons(ETH_P_IP)) {
958                                 tcp_key->tcp_src = swkey->ipv4.tp.src;
959                                 tcp_key->tcp_dst = swkey->ipv4.tp.dst;
960                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
961                                 tcp_key->tcp_src = swkey->ipv6.tp.src;
962                                 tcp_key->tcp_dst = swkey->ipv6.tp.dst;
963                         }
964                 } else if (swkey->ip.nw_proto == IPPROTO_UDP) {
965                         struct odp_key_udp *udp_key;
966
967                         nla = nla_reserve(skb, ODP_KEY_ATTR_UDP, sizeof(*udp_key));
968                         if (!nla)
969                                 goto nla_put_failure;
970                         udp_key = nla_data(nla);
971                         if (swkey->eth.type == htons(ETH_P_IP)) {
972                                 udp_key->udp_src = swkey->ipv4.tp.src;
973                                 udp_key->udp_dst = swkey->ipv4.tp.dst;
974                         } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
975                                 udp_key->udp_src = swkey->ipv6.tp.src;
976                                 udp_key->udp_dst = swkey->ipv6.tp.dst;
977                         }
978                 } else if (swkey->eth.type == htons(ETH_P_IP) &&
979                            swkey->ip.nw_proto == IPPROTO_ICMP) {
980                         struct odp_key_icmp *icmp_key;
981
982                         nla = nla_reserve(skb, ODP_KEY_ATTR_ICMP, sizeof(*icmp_key));
983                         if (!nla)
984                                 goto nla_put_failure;
985                         icmp_key = nla_data(nla);
986                         icmp_key->icmp_type = ntohs(swkey->ipv4.tp.src);
987                         icmp_key->icmp_code = ntohs(swkey->ipv4.tp.dst);
988                 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
989                            swkey->ip.nw_proto == IPPROTO_ICMPV6) {
990                         struct odp_key_icmpv6 *icmpv6_key;
991
992                         nla = nla_reserve(skb, ODP_KEY_ATTR_ICMPV6,
993                                                 sizeof(*icmpv6_key));
994                         if (!nla)
995                                 goto nla_put_failure;
996                         icmpv6_key = nla_data(nla);
997                         icmpv6_key->icmpv6_type = ntohs(swkey->ipv6.tp.src);
998                         icmpv6_key->icmpv6_code = ntohs(swkey->ipv6.tp.dst);
999
1000                         if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1001                             icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1002                                 struct odp_key_nd *nd_key;
1003
1004                                 nla = nla_reserve(skb, ODP_KEY_ATTR_ND, sizeof(*nd_key));
1005                                 if (!nla)
1006                                         goto nla_put_failure;
1007                                 nd_key = nla_data(nla);
1008                                 memcpy(nd_key->nd_target, &swkey->ipv6.nd.target,
1009                                                         sizeof(nd_key->nd_target));
1010                                 memcpy(nd_key->nd_sll, swkey->ipv6.nd.sll, ETH_ALEN);
1011                                 memcpy(nd_key->nd_tll, swkey->ipv6.nd.tll, ETH_ALEN);
1012                         }
1013                 }
1014         }
1015
1016         return 0;
1017
1018 nla_put_failure:
1019         return -EMSGSIZE;
1020 }
1021
1022 /* Initializes the flow module.
1023  * Returns zero if successful or a negative error code. */
1024 int flow_init(void)
1025 {
1026         flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1027                                         0, NULL);
1028         if (flow_cache == NULL)
1029                 return -ENOMEM;
1030
1031         get_random_bytes(&hash_seed, sizeof(hash_seed));
1032
1033         return 0;
1034 }
1035
1036 /* Uninitializes the flow module. */
1037 void flow_exit(void)
1038 {
1039         kmem_cache_destroy(flow_cache);
1040 }