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