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