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