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