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