80fe309194a69b52a4148417aec826042782657e
[sliver-openvswitch.git] / datapath / vport-capwap.c
1 /*
2  * Copyright (c) 2010 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
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 <linux/version.h>
10 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
11
12 #include <linux/if.h>
13 #include <linux/in.h>
14 #include <linux/ip.h>
15 #include <linux/list.h>
16 #include <linux/net.h>
17
18 #include <net/icmp.h>
19 #include <net/inet_frag.h>
20 #include <net/ip.h>
21 #include <net/protocol.h>
22
23 #include "tunnel.h"
24 #include "vport.h"
25 #include "vport-generic.h"
26
27 #define CAPWAP_SRC_PORT 58881
28 #define CAPWAP_DST_PORT 58882
29
30 #define CAPWAP_FRAG_TIMEOUT (30 * HZ)
31 #define CAPWAP_FRAG_MAX_MEM (256 * 1024)
32 #define CAPWAP_FRAG_PRUNE_MEM (192 *1024)
33 #define CAPWAP_FRAG_SECRET_INTERVAL (10 * 60 * HZ)
34
35 /*
36  * The CAPWAP header is a mess, with all kinds of odd size bit fields that
37  * cross byte boundaries, which are difficult to represent correctly in
38  * various byte orderings.  Luckily we only care about a few permutations, so
39  * statically create them and we can do very fast parsing by checking all 12
40  * fields in one go.
41  */
42 #define CAPWAP_BEGIN_HLEN __cpu_to_be32(0x00100000)
43 #define CAPWAP_BEGIN_WBID __cpu_to_be32(0x00000200)
44 #define CAPWAP_BEGIN_FRAG __cpu_to_be32(0x00000080)
45 #define CAPWAP_BEGIN_LAST __cpu_to_be32(0x00000040)
46
47 #define NO_FRAG_HDR (CAPWAP_BEGIN_HLEN | CAPWAP_BEGIN_WBID)
48 #define FRAG_HDR (NO_FRAG_HDR | CAPWAP_BEGIN_FRAG)
49 #define FRAG_LAST_HDR (FRAG_HDR | CAPWAP_BEGIN_LAST)
50
51 struct capwaphdr {
52         __be32 begin;
53         __be16 frag_id;
54         __be16 frag_off;
55 };
56
57 static inline struct capwaphdr *capwap_hdr(const struct sk_buff *skb)
58 {
59         return (struct capwaphdr *)(udp_hdr(skb) + 1);
60 }
61
62 /*
63  * The fragment offset is actually the high 13 bits of the last 16 bit field,
64  * so we would normally need to right shift 3 places.  However, it stores the
65  * offset in 8 byte chunks, which would involve a 3 place left shift.  So we
66  * just mask off the last 3 bits and be done with it.
67  */
68 #define FRAG_OFF_MASK (~0x7U)
69
70 #define CAPWAP_HLEN (sizeof(struct udphdr) + sizeof(struct capwaphdr))
71
72 struct frag_match {
73         __be32 saddr;
74         __be32 daddr;
75         __be16 id;
76 };
77
78 struct frag_queue {
79         struct inet_frag_queue ifq;
80         struct frag_match match;
81 };
82
83 struct frag_skb_cb {
84         u16 offset;
85 };
86 #define FRAG_CB(skb) ((struct frag_skb_cb *)(skb)->cb)
87
88 static struct sk_buff *fragment(struct sk_buff *, const struct vport *,
89                                 struct dst_entry *);
90 static void defrag_init(void);
91 static void defrag_exit(void);
92 static struct sk_buff *defrag(struct sk_buff *, bool frag_last);
93
94 static void capwap_frag_init(struct inet_frag_queue *, void *match);
95 static unsigned int capwap_frag_hash(struct inet_frag_queue *);
96 static int capwap_frag_match(struct inet_frag_queue *, void *match);
97 static void capwap_frag_expire(unsigned long ifq);
98
99 static struct inet_frags frag_state = {
100         .constructor    = capwap_frag_init,
101         .qsize          = sizeof(struct frag_queue),
102         .hashfn         = capwap_frag_hash,
103         .match          = capwap_frag_match,
104         .frag_expire    = capwap_frag_expire,
105         .secret_interval = CAPWAP_FRAG_SECRET_INTERVAL,
106 };
107 static struct netns_frags frag_netns_state = {
108         .timeout        = CAPWAP_FRAG_TIMEOUT,
109         .high_thresh    = CAPWAP_FRAG_MAX_MEM,
110         .low_thresh     = CAPWAP_FRAG_PRUNE_MEM,
111 };
112
113 static struct socket *capwap_rcv_socket;
114
115 static int capwap_hdr_len(const struct tnl_port_config *port_config)
116 {
117         /* CAPWAP has neither checksums nor keys, so reject ports with those. */
118         if (port_config->flags & (TNL_F_CSUM | TNL_F_IN_KEY_MATCH |
119                                   TNL_F_OUT_KEY_ACTION))
120                 return -EINVAL;
121
122         if (port_config->in_key != 0 || port_config->out_key != 0)
123                 return -EINVAL;
124
125         return CAPWAP_HLEN;
126 }
127
128 static struct sk_buff *capwap_build_header(struct sk_buff *skb,
129                                            const struct vport *vport,
130                                            const struct tnl_mutable_config *mutable,
131                                            struct dst_entry *dst)
132 {
133         struct udphdr *udph = udp_hdr(skb);
134         struct capwaphdr *cwh = capwap_hdr(skb);
135
136         udph->source = htons(CAPWAP_SRC_PORT);
137         udph->dest = htons(CAPWAP_DST_PORT);
138         udph->len = htons(skb->len - sizeof(struct iphdr));
139         udph->check = 0;
140
141         cwh->begin = NO_FRAG_HDR;
142         cwh->frag_id = 0;
143         cwh->frag_off = 0;
144
145         if (unlikely(skb->len > dst_mtu(dst)))
146                 skb = fragment(skb, vport, dst);
147
148         return skb;
149 }
150
151 static inline struct sk_buff *process_capwap_proto(struct sk_buff *skb)
152 {
153         struct capwaphdr *cwh = capwap_hdr(skb);
154
155         if (likely(cwh->begin == NO_FRAG_HDR))
156                 return skb;
157         else if (cwh->begin == FRAG_HDR)
158                 return defrag(skb, false);
159         else if (cwh->begin == FRAG_LAST_HDR)
160                 return defrag(skb, true);
161         else {
162                 if (net_ratelimit())
163                         printk(KERN_WARNING "openvswitch: unparsable packet receive on capwap socket\n");
164
165                 kfree_skb(skb);
166                 return NULL;
167         }
168 }
169
170 /* Called with rcu_read_lock and BH disabled. */
171 static int capwap_rcv(struct sock *sk, struct sk_buff *skb)
172 {
173         struct vport *vport;
174         const struct tnl_mutable_config *mutable;
175         struct iphdr *iph;
176
177         if (unlikely(!pskb_may_pull(skb, CAPWAP_HLEN + ETH_HLEN)))
178                 goto error;
179
180         __skb_pull(skb, CAPWAP_HLEN);
181         skb_postpull_rcsum(skb, skb_transport_header(skb), CAPWAP_HLEN + ETH_HLEN);
182
183         skb = process_capwap_proto(skb);
184         if (unlikely(!skb))
185                 goto out;
186
187         iph = ip_hdr(skb);
188         vport = tnl_find_port(iph->daddr, iph->saddr, 0,
189                               TNL_T_PROTO_CAPWAP | TNL_T_KEY_EXACT, &mutable);
190         if (unlikely(!vport)) {
191                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
192                 goto error;
193         }
194
195         tnl_rcv(vport, skb);
196         goto out;
197
198 error:
199         kfree_skb(skb);
200 out:
201         return 0;
202 }
203
204 struct tnl_ops capwap_tnl_ops = {
205         .tunnel_type    = TNL_T_PROTO_CAPWAP,
206         .ipproto        = IPPROTO_UDP,
207         .hdr_len        = capwap_hdr_len,
208         .build_header   = capwap_build_header,
209 };
210
211 static struct vport *capwap_create(const char *name, const void __user *config)
212 {
213         return tnl_create(name, config, &capwap_vport_ops, &capwap_tnl_ops);
214 }
215
216 /* Random value.  Irrelevant as long as it's not 0 since we set the handler. */
217 #define UDP_ENCAP_CAPWAP 10
218 static int capwap_init(void)
219 {
220         int err;
221         struct sockaddr_in sin;
222
223         err = sock_create(AF_INET, SOCK_DGRAM, 0, &capwap_rcv_socket);
224         if (err)
225                 goto error;
226         
227         sin.sin_family = AF_INET;
228         sin.sin_addr.s_addr = INADDR_ANY;
229         sin.sin_port = htons(CAPWAP_DST_PORT);
230
231         err = kernel_bind(capwap_rcv_socket, (struct sockaddr *)&sin,
232                           sizeof(struct sockaddr_in));
233         if (err)
234                 goto error_sock;
235
236         udp_sk(capwap_rcv_socket->sk)->encap_type = UDP_ENCAP_CAPWAP;
237         udp_sk(capwap_rcv_socket->sk)->encap_rcv = capwap_rcv;
238
239         defrag_init();
240
241         return tnl_init();
242
243 error_sock:
244         sock_release(capwap_rcv_socket);
245 error:
246         printk(KERN_WARNING "openvswitch: cannot register capwap protocol handler\n");
247         return err;
248 }
249
250 static void capwap_exit(void)
251 {
252         tnl_exit();
253         defrag_exit();
254         sock_release(capwap_rcv_socket);
255 }
256
257 static void copy_skb_metadata(struct sk_buff *from, struct sk_buff *to)
258 {
259         to->pkt_type = from->pkt_type;
260         to->priority = from->priority;
261         to->protocol = from->protocol;
262         skb_dst_set(to, dst_clone(skb_dst(from)));
263         to->dev = from->dev;
264         to->mark = from->mark;
265
266         if (from->sk)
267                 skb_set_owner_w(to, from->sk);
268
269 #ifdef CONFIG_NET_SCHED
270         to->tc_index = from->tc_index;
271 #endif
272 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
273         to->ipvs_property = from->ipvs_property;
274 #endif
275         skb_copy_secmark(to, from);
276 }
277
278 static struct sk_buff *fragment(struct sk_buff *skb, const struct vport *vport,
279                                 struct dst_entry *dst)
280 {
281         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
282         unsigned int hlen = sizeof(struct iphdr) + CAPWAP_HLEN;
283         unsigned int headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len;
284         struct sk_buff *result = NULL, *list_cur = NULL;
285         unsigned int remaining;
286         unsigned int offset;
287         __be16 frag_id;
288
289         if (hlen + ~FRAG_OFF_MASK + 1 > dst_mtu(dst)) {
290                 if (net_ratelimit())
291                         printk(KERN_WARNING "openvswitch: capwap link mtu (%d) is less than minimum packet (%d)\n",
292                                 dst_mtu(dst), hlen + ~FRAG_OFF_MASK + 1);
293                 goto error;
294         }
295
296         remaining = skb->len - hlen;
297         offset = 0;
298         frag_id = htons(atomic_inc_return(&tnl_vport->frag_id));
299
300         while (remaining) {
301                 struct sk_buff *skb2;
302                 int frag_size;
303                 struct iphdr *iph;
304                 struct udphdr *udph;
305                 struct capwaphdr *cwh;
306
307                 frag_size = min(remaining, dst_mtu(dst) - hlen);
308                 if (remaining > frag_size)
309                         frag_size &= FRAG_OFF_MASK;
310
311                 skb2 = alloc_skb(headroom + hlen + frag_size, GFP_ATOMIC);
312                 if (!skb2)
313                         goto error;
314
315                 skb_reserve(skb2, headroom);
316                 __skb_put(skb2, hlen + frag_size);
317                 skb_reset_network_header(skb2);
318                 skb_set_transport_header(skb2, sizeof(struct iphdr));
319
320                 /* Copy IP/UDP/CAPWAP header. */
321                 copy_skb_metadata(skb, skb2);
322                 skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
323
324                 /* Copy this data chunk. */
325                 if (skb_copy_bits(skb, hlen + offset, skb2->data + hlen, frag_size))
326                         BUG();
327
328                 iph = ip_hdr(skb2);
329                 iph->tot_len = hlen + frag_size;
330                 ip_send_check(iph);
331
332                 udph = udp_hdr(skb2);
333                 udph->len = htons(skb2->len - sizeof(struct iphdr));
334
335                 cwh = capwap_hdr(skb2);
336                 if (remaining > frag_size)
337                         cwh->begin = FRAG_HDR;
338                 else
339                         cwh->begin = FRAG_LAST_HDR;
340                 cwh->frag_id = frag_id;
341                 cwh->frag_off = htons(offset);
342
343                 if (result) {
344                         list_cur->next = skb2;
345                         list_cur = skb2;
346                 } else
347                         result = list_cur = skb2;
348
349                 offset += frag_size;
350                 remaining -= frag_size;
351         }
352
353         goto out;
354
355 error:
356         while (result) {
357                 list_cur = result->next;
358                 kfree_skb(result);
359                 result = list_cur;
360         }
361 out:
362         kfree_skb(skb);
363         return result;
364 }
365
366 /* All of the following functions relate to fragmentation reassembly. */
367
368 static inline struct frag_queue *ifq_cast(struct inet_frag_queue *ifq)
369 {
370         return container_of(ifq, struct frag_queue, ifq);
371 }
372
373 static u32 frag_hash(struct frag_match *match)
374 {
375         return jhash_3words((__force u16)match->id, (__force u32)match->saddr,
376                             (__force u32)match->daddr,
377                             frag_state.rnd) & (INETFRAGS_HASHSZ - 1);
378 }
379
380 static struct frag_queue *queue_find(struct frag_match *match)
381 {
382         struct inet_frag_queue *ifq;
383
384         read_lock(&frag_state.lock);
385
386         ifq = inet_frag_find(&frag_netns_state, &frag_state, match, frag_hash(match));
387         if (!ifq)
388                 return NULL;
389
390         /* Unlock happens inside inet_frag_find(). */
391
392         return ifq_cast(ifq);
393 }
394
395 static struct sk_buff *frag_reasm(struct frag_queue *fq, struct net_device *dev)
396 {
397         struct sk_buff *head = fq->ifq.fragments;
398         struct sk_buff *frag;
399
400         /* Succeed or fail, we're done with this queue. */
401         inet_frag_kill(&fq->ifq, &frag_state);
402
403         if (fq->ifq.len > 65535)
404                 return NULL;
405
406         /* Can't have the head be a clone. */
407         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
408                 return NULL;
409
410         /*
411          * We're about to build frag list for this SKB.  If it already has a
412          * frag list, alloc a new SKB and put the existing frag list there.
413          */
414         if (skb_shinfo(head)->frag_list) {
415                 int i;
416                 int paged_len = 0;
417
418                 frag = alloc_skb(0, GFP_ATOMIC);
419                 if (!frag)
420                         return NULL;
421
422                 frag->next = head->next;
423                 head->next = frag;
424                 skb_shinfo(frag)->frag_list = skb_shinfo(head)->frag_list;
425                 skb_shinfo(head)->frag_list = NULL;
426
427                 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
428                         paged_len += skb_shinfo(head)->frags[i].size;
429                 frag->len = frag->data_len = head->data_len - paged_len;
430                 head->data_len -= frag->len;
431                 head->len -= frag->len;
432
433                 frag->ip_summed = head->ip_summed;
434                 atomic_add(frag->truesize, &fq->ifq.net->mem);
435         }
436
437         skb_shinfo(head)->frag_list = head->next;
438         atomic_sub(head->truesize, &fq->ifq.net->mem);
439
440         /* Properly account for data in various packets. */
441         for (frag = head->next; frag; frag = frag->next) {
442                 head->data_len += frag->len;
443                 head->len += frag->len;
444
445                 if (head->ip_summed != frag->ip_summed)
446                         head->ip_summed = CHECKSUM_NONE;
447                 else if (head->ip_summed == CHECKSUM_COMPLETE)
448                         head->csum = csum_add(head->csum, frag->csum);
449
450                 head->truesize += frag->truesize;
451                 atomic_sub(frag->truesize, &fq->ifq.net->mem);
452         }
453
454         head->next = NULL;
455         head->dev = dev;
456         head->tstamp = fq->ifq.stamp;
457         fq->ifq.fragments = NULL;
458
459         return head;
460 }
461
462 static struct sk_buff *frag_queue(struct frag_queue *fq, struct sk_buff *skb,
463                                   u16 offset, bool frag_last)
464 {
465         struct sk_buff *prev, *next;
466         struct net_device *dev;
467         int end;
468
469         if (fq->ifq.last_in & INET_FRAG_COMPLETE)
470                 goto error;
471
472         if (!skb->len)
473                 goto error;
474
475         end = offset + skb->len;
476
477         if (frag_last) {
478                 /*
479                  * Last fragment, shouldn't already have data past our end or
480                  * have another last fragment.
481                  */
482                 if (end < fq->ifq.len || fq->ifq.last_in & INET_FRAG_LAST_IN)
483                         goto error;
484
485                 fq->ifq.last_in |= INET_FRAG_LAST_IN;
486                 fq->ifq.len = end;
487         } else {
488                 /* Fragments should align to 8 byte chunks. */
489                 if (end & ~FRAG_OFF_MASK)
490                         goto error;
491
492                 if (end > fq->ifq.len) {
493                         /*
494                          * Shouldn't have data past the end, if we already
495                          * have one.
496                          */
497                         if (fq->ifq.last_in & INET_FRAG_LAST_IN)
498                                 goto error;
499
500                         fq->ifq.len = end;
501                 }
502         }
503
504         /* Find where we fit in. */
505         prev = NULL;
506         for (next = fq->ifq.fragments; next != NULL; next = next->next) {
507                 if (FRAG_CB(next)->offset >= offset)
508                         break;
509                 prev = next;
510         }
511
512         /*
513          * Overlapping fragments aren't allowed.  We shouldn't start before
514          * the end of the previous fragment.
515          */
516         if (prev && FRAG_CB(prev)->offset + prev->len > offset)
517                 goto error;
518
519         /* We also shouldn't end after the beginning of the next fragment. */
520         if (next && end > FRAG_CB(next)->offset)
521                 goto error;
522
523         FRAG_CB(skb)->offset = offset;
524
525         /* Link into list. */
526         skb->next = next;
527         if (prev)
528                 prev->next = skb;
529         else
530                 fq->ifq.fragments = skb;
531
532         dev = skb->dev;
533         skb->dev = NULL;
534
535         fq->ifq.stamp = skb->tstamp;
536         fq->ifq.meat += skb->len;
537         atomic_add(skb->truesize, &fq->ifq.net->mem);
538         if (offset == 0)
539                 fq->ifq.last_in |= INET_FRAG_FIRST_IN;
540
541         /* If we have all fragments do reassembly. */
542         if (fq->ifq.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
543             fq->ifq.meat == fq->ifq.len)
544                 return frag_reasm(fq, dev);
545
546         write_lock(&frag_state.lock);
547         list_move_tail(&fq->ifq.lru_list, &fq->ifq.net->lru_list);
548         write_unlock(&frag_state.lock);
549
550         return NULL;
551
552 error:
553         kfree_skb(skb);
554         return NULL;
555 }
556
557 static struct sk_buff *defrag(struct sk_buff *skb, bool frag_last)
558 {
559         struct iphdr *iph = ip_hdr(skb);
560         struct capwaphdr *cwh = capwap_hdr(skb);
561         struct frag_match match;
562         u16 frag_off;
563         struct frag_queue *fq;
564
565         if (atomic_read(&frag_netns_state.mem) > frag_netns_state.high_thresh)
566                 inet_frag_evictor(&frag_netns_state, &frag_state);
567
568         match.daddr = iph->daddr;
569         match.saddr = iph->saddr;
570         match.id = cwh->frag_id;
571         frag_off = ntohs(cwh->frag_off) & FRAG_OFF_MASK;
572
573         fq = queue_find(&match);
574         if (fq) {
575                 spin_lock(&fq->ifq.lock);
576                 skb = frag_queue(fq, skb, frag_off, frag_last);
577                 spin_unlock(&fq->ifq.lock);
578
579                 inet_frag_put(&fq->ifq, &frag_state);
580
581                 return skb;
582         }
583
584         kfree_skb(skb);
585         return NULL;
586 }
587
588 static void defrag_init(void)
589 {
590         inet_frags_init(&frag_state);
591         inet_frags_init_net(&frag_netns_state);
592 }
593
594 static void defrag_exit(void)
595 {
596         inet_frags_exit_net(&frag_netns_state, &frag_state);
597         inet_frags_fini(&frag_state);
598 }
599
600 static void capwap_frag_init(struct inet_frag_queue *ifq, void *match_)
601 {
602         struct frag_match *match = match_;
603
604         ifq_cast(ifq)->match = *match;
605 }
606
607 static unsigned int capwap_frag_hash(struct inet_frag_queue *ifq)
608 {
609         return frag_hash(&ifq_cast(ifq)->match);
610 }
611
612 static int capwap_frag_match(struct inet_frag_queue *ifq, void *a_)
613 {
614         struct frag_match *a = a_;
615         struct frag_match *b = &ifq_cast(ifq)->match;
616
617         return a->id == b->id && a->saddr == b->saddr && a->daddr == b->daddr;
618 }
619
620 /* Run when the timeout for a given queue expires. */
621 static void capwap_frag_expire(unsigned long ifq)
622 {
623         struct frag_queue *fq;
624
625         fq = ifq_cast((struct inet_frag_queue *)ifq);
626
627         spin_lock(&fq->ifq.lock);
628
629         if (!(fq->ifq.last_in & INET_FRAG_COMPLETE))
630                 inet_frag_kill(&fq->ifq, &frag_state);
631
632         spin_unlock(&fq->ifq.lock);
633         inet_frag_put(&fq->ifq, &frag_state);
634 }
635
636 struct vport_ops capwap_vport_ops = {
637         .type           = "capwap",
638         .flags          = VPORT_F_GEN_STATS,
639         .init           = capwap_init,
640         .exit           = capwap_exit,
641         .create         = capwap_create,
642         .modify         = tnl_modify,
643         .destroy        = tnl_destroy,
644         .set_mtu        = tnl_set_mtu,
645         .set_addr       = tnl_set_addr,
646         .get_name       = tnl_get_name,
647         .get_addr       = tnl_get_addr,
648         .get_dev_flags  = vport_gen_get_dev_flags,
649         .is_running     = vport_gen_is_running,
650         .get_operstate  = vport_gen_get_operstate,
651         .get_mtu        = tnl_get_mtu,
652         .send           = tnl_send,
653 };
654
655 #endif /* Linux kernel >= 2.6.26 */