2 * Copyright (c) 2010 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/version.h>
12 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
17 #include <linux/list.h>
18 #include <linux/net.h>
21 #include <net/inet_frag.h>
23 #include <net/protocol.h>
27 #include "vport-generic.h"
29 #define CAPWAP_SRC_PORT 58881
30 #define CAPWAP_DST_PORT 58882
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)
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
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)
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)
59 static inline struct capwaphdr *capwap_hdr(const struct sk_buff *skb)
61 return (struct capwaphdr *)(udp_hdr(skb) + 1);
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.
70 #define FRAG_OFF_MASK (~0x7U)
72 #define CAPWAP_HLEN (sizeof(struct udphdr) + sizeof(struct capwaphdr))
81 struct inet_frag_queue ifq;
82 struct frag_match match;
88 #define FRAG_CB(skb) ((struct frag_skb_cb *)(skb)->cb)
90 static struct sk_buff *fragment(struct sk_buff *, const struct vport *,
92 static void defrag_init(void);
93 static void defrag_exit(void);
94 static struct sk_buff *defrag(struct sk_buff *, bool frag_last);
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);
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,
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,
115 static struct socket *capwap_rcv_socket;
117 static int capwap_hdr_len(const struct tnl_port_config *port_config)
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))
124 if (port_config->in_key != 0 || port_config->out_key != 0)
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)
135 struct udphdr *udph = udp_hdr(skb);
136 struct capwaphdr *cwh = capwap_hdr(skb);
138 udph->source = htons(CAPWAP_SRC_PORT);
139 udph->dest = htons(CAPWAP_DST_PORT);
140 udph->len = htons(skb->len - sizeof(struct iphdr));
143 cwh->begin = NO_FRAG_HDR;
147 if (unlikely(skb->len > dst_mtu(dst)))
148 skb = fragment(skb, vport, dst);
153 static inline struct sk_buff *process_capwap_proto(struct sk_buff *skb)
155 struct capwaphdr *cwh = capwap_hdr(skb);
157 if (likely(cwh->begin == NO_FRAG_HDR))
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);
165 pr_warn("unparsable packet receive on capwap socket\n");
172 /* Called with rcu_read_lock and BH disabled. */
173 static int capwap_rcv(struct sock *sk, struct sk_buff *skb)
176 const struct tnl_mutable_config *mutable;
179 if (unlikely(!pskb_may_pull(skb, CAPWAP_HLEN + ETH_HLEN)))
182 __skb_pull(skb, CAPWAP_HLEN);
183 skb_postpull_rcsum(skb, skb_transport_header(skb), CAPWAP_HLEN + ETH_HLEN);
185 skb = process_capwap_proto(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);
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,
213 static struct vport *capwap_create(const char *name, const void __user *config)
215 return tnl_create(name, config, &capwap_vport_ops, &capwap_tnl_ops);
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)
223 struct sockaddr_in sin;
225 err = sock_create(AF_INET, SOCK_DGRAM, 0, &capwap_rcv_socket);
229 sin.sin_family = AF_INET;
230 sin.sin_addr.s_addr = INADDR_ANY;
231 sin.sin_port = htons(CAPWAP_DST_PORT);
233 err = kernel_bind(capwap_rcv_socket, (struct sockaddr *)&sin,
234 sizeof(struct sockaddr_in));
238 udp_sk(capwap_rcv_socket->sk)->encap_type = UDP_ENCAP_CAPWAP;
239 udp_sk(capwap_rcv_socket->sk)->encap_rcv = capwap_rcv;
246 sock_release(capwap_rcv_socket);
248 pr_warn("cannot register capwap protocol handler\n");
252 static void capwap_exit(void)
256 sock_release(capwap_rcv_socket);
259 static void copy_skb_metadata(struct sk_buff *from, struct sk_buff *to)
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)));
266 to->mark = from->mark;
269 skb_set_owner_w(to, from->sk);
271 #ifdef CONFIG_NET_SCHED
272 to->tc_index = from->tc_index;
274 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
275 to->ipvs_property = from->ipvs_property;
277 skb_copy_secmark(to, from);
280 static struct sk_buff *fragment(struct sk_buff *skb, const struct vport *vport,
281 struct dst_entry *dst)
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;
291 if (hlen + ~FRAG_OFF_MASK + 1 > dst_mtu(dst)) {
293 pr_warn("capwap link mtu (%d) is less than minimum packet (%d)\n",
294 dst_mtu(dst), hlen + ~FRAG_OFF_MASK + 1);
298 remaining = skb->len - hlen;
300 frag_id = htons(atomic_inc_return(&tnl_vport->frag_id));
303 struct sk_buff *skb2;
307 struct capwaphdr *cwh;
309 frag_size = min(remaining, dst_mtu(dst) - hlen);
310 if (remaining > frag_size)
311 frag_size &= FRAG_OFF_MASK;
313 skb2 = alloc_skb(headroom + hlen + frag_size, GFP_ATOMIC);
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));
322 /* Copy IP/UDP/CAPWAP header. */
323 copy_skb_metadata(skb, skb2);
324 skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
326 /* Copy this data chunk. */
327 if (skb_copy_bits(skb, hlen + offset, skb2->data + hlen, frag_size))
331 iph->tot_len = hlen + frag_size;
334 udph = udp_hdr(skb2);
335 udph->len = htons(skb2->len - sizeof(struct iphdr));
337 cwh = capwap_hdr(skb2);
338 if (remaining > frag_size)
339 cwh->begin = FRAG_HDR;
341 cwh->begin = FRAG_LAST_HDR;
342 cwh->frag_id = frag_id;
343 cwh->frag_off = htons(offset);
346 list_cur->next = skb2;
349 result = list_cur = skb2;
352 remaining -= frag_size;
359 list_cur = result->next;
368 /* All of the following functions relate to fragmentation reassembly. */
370 static inline struct frag_queue *ifq_cast(struct inet_frag_queue *ifq)
372 return container_of(ifq, struct frag_queue, ifq);
375 static u32 frag_hash(struct frag_match *match)
377 return jhash_3words((__force u16)match->id, (__force u32)match->saddr,
378 (__force u32)match->daddr,
379 frag_state.rnd) & (INETFRAGS_HASHSZ - 1);
382 static struct frag_queue *queue_find(struct frag_match *match)
384 struct inet_frag_queue *ifq;
386 read_lock(&frag_state.lock);
388 ifq = inet_frag_find(&frag_netns_state, &frag_state, match, frag_hash(match));
392 /* Unlock happens inside inet_frag_find(). */
394 return ifq_cast(ifq);
397 static struct sk_buff *frag_reasm(struct frag_queue *fq, struct net_device *dev)
399 struct sk_buff *head = fq->ifq.fragments;
400 struct sk_buff *frag;
402 /* Succeed or fail, we're done with this queue. */
403 inet_frag_kill(&fq->ifq, &frag_state);
405 if (fq->ifq.len > 65535)
408 /* Can't have the head be a clone. */
409 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
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.
416 if (skb_shinfo(head)->frag_list) {
420 frag = alloc_skb(0, GFP_ATOMIC);
424 frag->next = head->next;
426 skb_shinfo(frag)->frag_list = skb_shinfo(head)->frag_list;
427 skb_shinfo(head)->frag_list = NULL;
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;
435 frag->ip_summed = head->ip_summed;
436 atomic_add(frag->truesize, &fq->ifq.net->mem);
439 skb_shinfo(head)->frag_list = head->next;
440 atomic_sub(head->truesize, &fq->ifq.net->mem);
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;
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);
452 head->truesize += frag->truesize;
453 atomic_sub(frag->truesize, &fq->ifq.net->mem);
458 head->tstamp = fq->ifq.stamp;
459 fq->ifq.fragments = NULL;
464 static struct sk_buff *frag_queue(struct frag_queue *fq, struct sk_buff *skb,
465 u16 offset, bool frag_last)
467 struct sk_buff *prev, *next;
468 struct net_device *dev;
471 if (fq->ifq.last_in & INET_FRAG_COMPLETE)
477 end = offset + skb->len;
481 * Last fragment, shouldn't already have data past our end or
482 * have another last fragment.
484 if (end < fq->ifq.len || fq->ifq.last_in & INET_FRAG_LAST_IN)
487 fq->ifq.last_in |= INET_FRAG_LAST_IN;
490 /* Fragments should align to 8 byte chunks. */
491 if (end & ~FRAG_OFF_MASK)
494 if (end > fq->ifq.len) {
496 * Shouldn't have data past the end, if we already
499 if (fq->ifq.last_in & INET_FRAG_LAST_IN)
506 /* Find where we fit in. */
508 for (next = fq->ifq.fragments; next != NULL; next = next->next) {
509 if (FRAG_CB(next)->offset >= offset)
515 * Overlapping fragments aren't allowed. We shouldn't start before
516 * the end of the previous fragment.
518 if (prev && FRAG_CB(prev)->offset + prev->len > offset)
521 /* We also shouldn't end after the beginning of the next fragment. */
522 if (next && end > FRAG_CB(next)->offset)
525 FRAG_CB(skb)->offset = offset;
527 /* Link into list. */
532 fq->ifq.fragments = skb;
537 fq->ifq.stamp = skb->tstamp;
538 fq->ifq.meat += skb->len;
539 atomic_add(skb->truesize, &fq->ifq.net->mem);
541 fq->ifq.last_in |= INET_FRAG_FIRST_IN;
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);
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);
559 static struct sk_buff *defrag(struct sk_buff *skb, bool frag_last)
561 struct iphdr *iph = ip_hdr(skb);
562 struct capwaphdr *cwh = capwap_hdr(skb);
563 struct frag_match match;
565 struct frag_queue *fq;
567 if (atomic_read(&frag_netns_state.mem) > frag_netns_state.high_thresh)
568 inet_frag_evictor(&frag_netns_state, &frag_state);
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;
575 fq = queue_find(&match);
577 spin_lock(&fq->ifq.lock);
578 skb = frag_queue(fq, skb, frag_off, frag_last);
579 spin_unlock(&fq->ifq.lock);
581 inet_frag_put(&fq->ifq, &frag_state);
590 static void defrag_init(void)
592 inet_frags_init(&frag_state);
593 inet_frags_init_net(&frag_netns_state);
596 static void defrag_exit(void)
598 inet_frags_exit_net(&frag_netns_state, &frag_state);
599 inet_frags_fini(&frag_state);
602 static void capwap_frag_init(struct inet_frag_queue *ifq, void *match_)
604 struct frag_match *match = match_;
606 ifq_cast(ifq)->match = *match;
609 static unsigned int capwap_frag_hash(struct inet_frag_queue *ifq)
611 return frag_hash(&ifq_cast(ifq)->match);
614 static int capwap_frag_match(struct inet_frag_queue *ifq, void *a_)
616 struct frag_match *a = a_;
617 struct frag_match *b = &ifq_cast(ifq)->match;
619 return a->id == b->id && a->saddr == b->saddr && a->daddr == b->daddr;
622 /* Run when the timeout for a given queue expires. */
623 static void capwap_frag_expire(unsigned long ifq)
625 struct frag_queue *fq;
627 fq = ifq_cast((struct inet_frag_queue *)ifq);
629 spin_lock(&fq->ifq.lock);
631 if (!(fq->ifq.last_in & INET_FRAG_COMPLETE))
632 inet_frag_kill(&fq->ifq, &frag_state);
634 spin_unlock(&fq->ifq.lock);
635 inet_frag_put(&fq->ifq, &frag_state);
638 struct vport_ops capwap_vport_ops = {
640 .flags = VPORT_F_GEN_STATS,
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,
657 #endif /* Linux kernel >= 2.6.26 */