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>
28 #include "vport-generic.h"
30 #define CAPWAP_SRC_PORT 58881
31 #define CAPWAP_DST_PORT 58882
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)
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
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)
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)
60 static inline struct capwaphdr *capwap_hdr(const struct sk_buff *skb)
62 return (struct capwaphdr *)(udp_hdr(skb) + 1);
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.
71 #define FRAG_OFF_MASK (~0x7U)
73 #define CAPWAP_HLEN (sizeof(struct udphdr) + sizeof(struct capwaphdr))
82 struct inet_frag_queue ifq;
83 struct frag_match match;
89 #define FRAG_CB(skb) ((struct frag_skb_cb *)(skb)->cb)
91 static struct sk_buff *fragment(struct sk_buff *, const struct vport *,
93 static void defrag_init(void);
94 static void defrag_exit(void);
95 static struct sk_buff *defrag(struct sk_buff *, bool frag_last);
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);
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,
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,
116 static struct socket *capwap_rcv_socket;
118 static int capwap_hdr_len(const struct tnl_port_config *port_config)
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))
125 if (port_config->in_key != 0 || port_config->out_key != 0)
131 static void capwap_build_header(const struct vport *vport,
132 const struct tnl_mutable_config *mutable,
135 struct udphdr *udph = header;
136 struct capwaphdr *cwh = (struct capwaphdr *)(udph + 1);
138 udph->source = htons(CAPWAP_SRC_PORT);
139 udph->dest = htons(CAPWAP_DST_PORT);
142 cwh->begin = NO_FRAG_HDR;
147 static struct sk_buff *capwap_update_header(const struct vport *vport,
148 const struct tnl_mutable_config *mutable,
149 struct dst_entry *dst,
152 struct udphdr *udph = udp_hdr(skb);
154 udph->len = htons(skb->len - skb_transport_offset(skb));
156 if (unlikely(skb->len - skb_network_offset(skb) > dst_mtu(dst)))
157 skb = fragment(skb, vport, dst);
162 static inline struct sk_buff *process_capwap_proto(struct sk_buff *skb)
164 struct capwaphdr *cwh = capwap_hdr(skb);
166 if (likely(cwh->begin == NO_FRAG_HDR))
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);
174 pr_warn("unparsable packet receive on capwap socket\n");
181 /* Called with rcu_read_lock and BH disabled. */
182 static int capwap_rcv(struct sock *sk, struct sk_buff *skb)
185 const struct tnl_mutable_config *mutable;
188 if (unlikely(!pskb_may_pull(skb, CAPWAP_HLEN + ETH_HLEN)))
191 __skb_pull(skb, CAPWAP_HLEN);
192 skb_postpull_rcsum(skb, skb_transport_header(skb), CAPWAP_HLEN + ETH_HLEN);
194 skb = process_capwap_proto(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);
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,
223 static struct vport *capwap_create(const struct vport_parms *parms)
225 return tnl_create(parms, &capwap_vport_ops, &capwap_tnl_ops);
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)
233 struct sockaddr_in sin;
235 err = sock_create(AF_INET, SOCK_DGRAM, 0, &capwap_rcv_socket);
239 sin.sin_family = AF_INET;
240 sin.sin_addr.s_addr = htonl(INADDR_ANY);
241 sin.sin_port = htons(CAPWAP_DST_PORT);
243 err = kernel_bind(capwap_rcv_socket, (struct sockaddr *)&sin,
244 sizeof(struct sockaddr_in));
248 udp_sk(capwap_rcv_socket->sk)->encap_type = UDP_ENCAP_CAPWAP;
249 udp_sk(capwap_rcv_socket->sk)->encap_rcv = capwap_rcv;
256 sock_release(capwap_rcv_socket);
258 pr_warn("cannot register capwap protocol handler\n");
262 static void capwap_exit(void)
265 sock_release(capwap_rcv_socket);
268 static void copy_skb_metadata(struct sk_buff *from, struct sk_buff *to)
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)));
275 to->mark = from->mark;
278 skb_set_owner_w(to, from->sk);
280 #ifdef CONFIG_NET_SCHED
281 to->tc_index = from->tc_index;
283 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
284 to->ipvs_property = from->ipvs_property;
286 skb_copy_secmark(to, from);
289 static struct sk_buff *fragment(struct sk_buff *skb, const struct vport *vport,
290 struct dst_entry *dst)
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;
301 if (hlen + ~FRAG_OFF_MASK + 1 > max_frame_len) {
303 pr_warn("capwap link mtu (%d) is less than minimum packet (%d)\n",
305 hlen - skb_network_offset(skb) + ~FRAG_OFF_MASK + 1);
309 remaining = skb->len - hlen;
311 frag_id = htons(atomic_inc_return(&tnl_vport->frag_id));
313 headroom = dst->header_len + 16;
314 if (!skb_network_offset(skb))
315 headroom += LL_RESERVED_SPACE(dst->dev);
318 struct sk_buff *skb2;
321 struct capwaphdr *cwh;
323 frag_size = min(remaining, max_frame_len - hlen);
324 if (remaining > frag_size)
325 frag_size &= FRAG_OFF_MASK;
327 skb2 = alloc_skb(headroom + hlen + frag_size, GFP_ATOMIC);
331 skb_reserve(skb2, headroom);
332 __skb_put(skb2, hlen + frag_size);
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));
339 /* Copy (Ethernet)/IP/UDP/CAPWAP header. */
340 copy_skb_metadata(skb, skb2);
341 skb_copy_from_linear_data(skb, skb2->data, hlen);
343 /* Copy this data chunk. */
344 if (skb_copy_bits(skb, hlen + offset, skb2->data + hlen, frag_size))
347 udph = udp_hdr(skb2);
348 udph->len = htons(skb2->len - skb_transport_offset(skb2));
350 cwh = capwap_hdr(skb2);
351 if (remaining > frag_size)
352 cwh->begin = FRAG_HDR;
354 cwh->begin = FRAG_LAST_HDR;
355 cwh->frag_id = frag_id;
356 cwh->frag_off = htons(offset);
359 list_cur->next = skb2;
362 result = list_cur = skb2;
365 remaining -= frag_size;
371 tnl_free_linked_skbs(result);
377 /* All of the following functions relate to fragmentation reassembly. */
379 static inline struct frag_queue *ifq_cast(struct inet_frag_queue *ifq)
381 return container_of(ifq, struct frag_queue, ifq);
384 static u32 frag_hash(struct frag_match *match)
386 return jhash_3words((__force u16)match->id, (__force u32)match->saddr,
387 (__force u32)match->daddr,
388 frag_state.rnd) & (INETFRAGS_HASHSZ - 1);
391 static struct frag_queue *queue_find(struct frag_match *match)
393 struct inet_frag_queue *ifq;
395 read_lock(&frag_state.lock);
397 ifq = inet_frag_find(&frag_netns_state, &frag_state, match, frag_hash(match));
401 /* Unlock happens inside inet_frag_find(). */
403 return ifq_cast(ifq);
406 static struct sk_buff *frag_reasm(struct frag_queue *fq, struct net_device *dev)
408 struct sk_buff *head = fq->ifq.fragments;
409 struct sk_buff *frag;
411 /* Succeed or fail, we're done with this queue. */
412 inet_frag_kill(&fq->ifq, &frag_state);
414 if (fq->ifq.len > 65535)
417 /* Can't have the head be a clone. */
418 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
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.
425 if (skb_shinfo(head)->frag_list) {
429 frag = alloc_skb(0, GFP_ATOMIC);
433 frag->next = head->next;
435 skb_shinfo(frag)->frag_list = skb_shinfo(head)->frag_list;
436 skb_shinfo(head)->frag_list = NULL;
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;
444 frag->ip_summed = head->ip_summed;
445 atomic_add(frag->truesize, &fq->ifq.net->mem);
448 skb_shinfo(head)->frag_list = head->next;
449 atomic_sub(head->truesize, &fq->ifq.net->mem);
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;
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);
461 head->truesize += frag->truesize;
462 atomic_sub(frag->truesize, &fq->ifq.net->mem);
467 head->tstamp = fq->ifq.stamp;
468 fq->ifq.fragments = NULL;
473 static struct sk_buff *frag_queue(struct frag_queue *fq, struct sk_buff *skb,
474 u16 offset, bool frag_last)
476 struct sk_buff *prev, *next;
477 struct net_device *dev;
480 if (fq->ifq.last_in & INET_FRAG_COMPLETE)
486 end = offset + skb->len;
490 * Last fragment, shouldn't already have data past our end or
491 * have another last fragment.
493 if (end < fq->ifq.len || fq->ifq.last_in & INET_FRAG_LAST_IN)
496 fq->ifq.last_in |= INET_FRAG_LAST_IN;
499 /* Fragments should align to 8 byte chunks. */
500 if (end & ~FRAG_OFF_MASK)
503 if (end > fq->ifq.len) {
505 * Shouldn't have data past the end, if we already
508 if (fq->ifq.last_in & INET_FRAG_LAST_IN)
515 /* Find where we fit in. */
517 for (next = fq->ifq.fragments; next != NULL; next = next->next) {
518 if (FRAG_CB(next)->offset >= offset)
524 * Overlapping fragments aren't allowed. We shouldn't start before
525 * the end of the previous fragment.
527 if (prev && FRAG_CB(prev)->offset + prev->len > offset)
530 /* We also shouldn't end after the beginning of the next fragment. */
531 if (next && end > FRAG_CB(next)->offset)
534 FRAG_CB(skb)->offset = offset;
536 /* Link into list. */
541 fq->ifq.fragments = skb;
546 fq->ifq.stamp = skb->tstamp;
547 fq->ifq.meat += skb->len;
548 atomic_add(skb->truesize, &fq->ifq.net->mem);
550 fq->ifq.last_in |= INET_FRAG_FIRST_IN;
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);
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);
568 static struct sk_buff *defrag(struct sk_buff *skb, bool frag_last)
570 struct iphdr *iph = ip_hdr(skb);
571 struct capwaphdr *cwh = capwap_hdr(skb);
572 struct frag_match match;
574 struct frag_queue *fq;
576 if (atomic_read(&frag_netns_state.mem) > frag_netns_state.high_thresh)
577 inet_frag_evictor(&frag_netns_state, &frag_state);
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;
584 fq = queue_find(&match);
586 spin_lock(&fq->ifq.lock);
587 skb = frag_queue(fq, skb, frag_off, frag_last);
588 spin_unlock(&fq->ifq.lock);
590 inet_frag_put(&fq->ifq, &frag_state);
599 static void defrag_init(void)
601 inet_frags_init(&frag_state);
602 inet_frags_init_net(&frag_netns_state);
605 static void defrag_exit(void)
607 inet_frags_exit_net(&frag_netns_state, &frag_state);
608 inet_frags_fini(&frag_state);
611 static void capwap_frag_init(struct inet_frag_queue *ifq, void *match_)
613 struct frag_match *match = match_;
615 ifq_cast(ifq)->match = *match;
618 static unsigned int capwap_frag_hash(struct inet_frag_queue *ifq)
620 return frag_hash(&ifq_cast(ifq)->match);
623 static int capwap_frag_match(struct inet_frag_queue *ifq, void *a_)
625 struct frag_match *a = a_;
626 struct frag_match *b = &ifq_cast(ifq)->match;
628 return a->id == b->id && a->saddr == b->saddr && a->daddr == b->daddr;
631 /* Run when the timeout for a given queue expires. */
632 static void capwap_frag_expire(unsigned long ifq)
634 struct frag_queue *fq;
636 fq = ifq_cast((struct inet_frag_queue *)ifq);
638 spin_lock(&fq->ifq.lock);
640 if (!(fq->ifq.last_in & INET_FRAG_COMPLETE))
641 inet_frag_kill(&fq->ifq, &frag_state);
643 spin_unlock(&fq->ifq.lock);
644 inet_frag_put(&fq->ifq, &frag_state);
647 const struct vport_ops capwap_vport_ops = {
649 .flags = VPORT_F_GEN_STATS,
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,
667 #endif /* Linux kernel >= 2.6.26 */