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 #include <linux/version.h>
10 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
15 #include <linux/list.h>
16 #include <linux/net.h>
19 #include <net/inet_frag.h>
21 #include <net/protocol.h>
25 #include "vport-generic.h"
27 #define CAPWAP_SRC_PORT 58881
28 #define CAPWAP_DST_PORT 58882
30 #define CAPWAP_FRAG_TIMEOUT (30 * HZ)
31 #define CAPWAP_FRAG_MAX_MEM (256 * 1024)
32 #define CAPWAP_FRAG_PRUNE_MEM (192 *1024)
33 #define CAPWAP_FRAG_SECRET_INTERVAL (10 * 60 * HZ)
36 * The CAPWAP header is a mess, with all kinds of odd size bit fields that
37 * cross byte boundaries, which are difficult to represent correctly in
38 * various byte orderings. Luckily we only care about a few permutations, so
39 * statically create them and we can do very fast parsing by checking all 12
42 #define CAPWAP_BEGIN_HLEN __cpu_to_be32(0x00100000)
43 #define CAPWAP_BEGIN_WBID __cpu_to_be32(0x00000200)
44 #define CAPWAP_BEGIN_FRAG __cpu_to_be32(0x00000080)
45 #define CAPWAP_BEGIN_LAST __cpu_to_be32(0x00000040)
47 #define NO_FRAG_HDR (CAPWAP_BEGIN_HLEN | CAPWAP_BEGIN_WBID)
48 #define FRAG_HDR (NO_FRAG_HDR | CAPWAP_BEGIN_FRAG)
49 #define FRAG_LAST_HDR (FRAG_HDR | CAPWAP_BEGIN_LAST)
57 static inline struct capwaphdr *capwap_hdr(const struct sk_buff *skb)
59 return (struct capwaphdr *)(udp_hdr(skb) + 1);
63 * The fragment offset is actually the high 13 bits of the last 16 bit field,
64 * so we would normally need to right shift 3 places. However, it stores the
65 * offset in 8 byte chunks, which would involve a 3 place left shift. So we
66 * just mask off the last 3 bits and be done with it.
68 #define FRAG_OFF_MASK (~0x7U)
70 #define CAPWAP_HLEN (sizeof(struct udphdr) + sizeof(struct capwaphdr))
79 struct inet_frag_queue ifq;
80 struct frag_match match;
86 #define FRAG_CB(skb) ((struct frag_skb_cb *)(skb)->cb)
88 static struct sk_buff *fragment(struct sk_buff *, const struct vport *,
90 static void defrag_init(void);
91 static void defrag_exit(void);
92 static struct sk_buff *defrag(struct sk_buff *, bool frag_last);
94 static void capwap_frag_init(struct inet_frag_queue *, void *match);
95 static unsigned int capwap_frag_hash(struct inet_frag_queue *);
96 static int capwap_frag_match(struct inet_frag_queue *, void *match);
97 static void capwap_frag_expire(unsigned long ifq);
99 static struct inet_frags frag_state = {
100 .constructor = capwap_frag_init,
101 .qsize = sizeof(struct frag_queue),
102 .hashfn = capwap_frag_hash,
103 .match = capwap_frag_match,
104 .frag_expire = capwap_frag_expire,
105 .secret_interval = CAPWAP_FRAG_SECRET_INTERVAL,
107 static struct netns_frags frag_netns_state = {
108 .timeout = CAPWAP_FRAG_TIMEOUT,
109 .high_thresh = CAPWAP_FRAG_MAX_MEM,
110 .low_thresh = CAPWAP_FRAG_PRUNE_MEM,
113 static struct socket *capwap_rcv_socket;
115 static int capwap_hdr_len(const struct tnl_port_config *port_config)
117 /* CAPWAP has neither checksums nor keys, so reject ports with those. */
118 if (port_config->flags & (TNL_F_CSUM | TNL_F_IN_KEY_MATCH |
119 TNL_F_OUT_KEY_ACTION))
122 if (port_config->in_key != 0 || port_config->out_key != 0)
128 static struct sk_buff *capwap_build_header(struct sk_buff *skb,
129 const struct vport *vport,
130 const struct tnl_mutable_config *mutable,
131 struct dst_entry *dst)
133 struct udphdr *udph = udp_hdr(skb);
134 struct capwaphdr *cwh = capwap_hdr(skb);
136 udph->source = htons(CAPWAP_SRC_PORT);
137 udph->dest = htons(CAPWAP_DST_PORT);
138 udph->len = htons(skb->len - sizeof(struct iphdr));
141 cwh->begin = NO_FRAG_HDR;
145 if (unlikely(skb->len > dst_mtu(dst)))
146 skb = fragment(skb, vport, dst);
151 static inline struct sk_buff *process_capwap_proto(struct sk_buff *skb)
153 struct capwaphdr *cwh = capwap_hdr(skb);
155 if (likely(cwh->begin == NO_FRAG_HDR))
157 else if (cwh->begin == FRAG_HDR)
158 return defrag(skb, false);
159 else if (cwh->begin == FRAG_LAST_HDR)
160 return defrag(skb, true);
163 printk(KERN_WARNING "openvswitch: unparsable packet receive on capwap socket\n");
170 /* Called with rcu_read_lock and BH disabled. */
171 static int capwap_rcv(struct sock *sk, struct sk_buff *skb)
174 const struct tnl_mutable_config *mutable;
177 if (unlikely(!pskb_may_pull(skb, CAPWAP_HLEN + ETH_HLEN)))
180 __skb_pull(skb, CAPWAP_HLEN);
181 skb_postpull_rcsum(skb, skb_transport_header(skb), CAPWAP_HLEN + ETH_HLEN);
183 skb = process_capwap_proto(skb);
188 vport = tnl_find_port(iph->daddr, iph->saddr, 0,
189 TNL_T_PROTO_CAPWAP | TNL_T_KEY_EXACT, &mutable);
190 if (unlikely(!vport)) {
191 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
204 struct tnl_ops capwap_tnl_ops = {
205 .tunnel_type = TNL_T_PROTO_CAPWAP,
206 .ipproto = IPPROTO_UDP,
207 .hdr_len = capwap_hdr_len,
208 .build_header = capwap_build_header,
211 static struct vport *capwap_create(const char *name, const void __user *config)
213 return tnl_create(name, config, &capwap_vport_ops, &capwap_tnl_ops);
216 /* Random value. Irrelevant as long as it's not 0 since we set the handler. */
217 #define UDP_ENCAP_CAPWAP 10
218 static int capwap_init(void)
221 struct sockaddr_in sin;
223 err = sock_create(AF_INET, SOCK_DGRAM, 0, &capwap_rcv_socket);
227 sin.sin_family = AF_INET;
228 sin.sin_addr.s_addr = INADDR_ANY;
229 sin.sin_port = htons(CAPWAP_DST_PORT);
231 err = kernel_bind(capwap_rcv_socket, (struct sockaddr *)&sin,
232 sizeof(struct sockaddr_in));
236 udp_sk(capwap_rcv_socket->sk)->encap_type = UDP_ENCAP_CAPWAP;
237 udp_sk(capwap_rcv_socket->sk)->encap_rcv = capwap_rcv;
244 sock_release(capwap_rcv_socket);
246 printk(KERN_WARNING "openvswitch: cannot register capwap protocol handler\n");
250 static void capwap_exit(void)
254 sock_release(capwap_rcv_socket);
257 static void copy_skb_metadata(struct sk_buff *from, struct sk_buff *to)
259 to->pkt_type = from->pkt_type;
260 to->priority = from->priority;
261 to->protocol = from->protocol;
262 skb_dst_set(to, dst_clone(skb_dst(from)));
264 to->mark = from->mark;
267 skb_set_owner_w(to, from->sk);
269 #ifdef CONFIG_NET_SCHED
270 to->tc_index = from->tc_index;
272 #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
273 to->ipvs_property = from->ipvs_property;
275 skb_copy_secmark(to, from);
278 static struct sk_buff *fragment(struct sk_buff *skb, const struct vport *vport,
279 struct dst_entry *dst)
281 struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
282 unsigned int hlen = sizeof(struct iphdr) + CAPWAP_HLEN;
283 unsigned int headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len;
284 struct sk_buff *result = NULL, *list_cur = NULL;
285 unsigned int remaining;
289 if (hlen + ~FRAG_OFF_MASK + 1 > dst_mtu(dst)) {
291 printk(KERN_WARNING "openvswitch: capwap link mtu (%d) is less than minimum packet (%d)\n",
292 dst_mtu(dst), hlen + ~FRAG_OFF_MASK + 1);
296 remaining = skb->len - hlen;
298 frag_id = htons(atomic_inc_return(&tnl_vport->frag_id));
301 struct sk_buff *skb2;
305 struct capwaphdr *cwh;
307 frag_size = min(remaining, dst_mtu(dst) - hlen);
308 if (remaining > frag_size)
309 frag_size &= FRAG_OFF_MASK;
311 skb2 = alloc_skb(headroom + hlen + frag_size, GFP_ATOMIC);
315 skb_reserve(skb2, headroom);
316 __skb_put(skb2, hlen + frag_size);
317 skb_reset_network_header(skb2);
318 skb_set_transport_header(skb2, sizeof(struct iphdr));
320 /* Copy IP/UDP/CAPWAP header. */
321 copy_skb_metadata(skb, skb2);
322 skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
324 /* Copy this data chunk. */
325 if (skb_copy_bits(skb, hlen + offset, skb2->data + hlen, frag_size))
329 iph->tot_len = hlen + frag_size;
332 udph = udp_hdr(skb2);
333 udph->len = htons(skb2->len - sizeof(struct iphdr));
335 cwh = capwap_hdr(skb2);
336 if (remaining > frag_size)
337 cwh->begin = FRAG_HDR;
339 cwh->begin = FRAG_LAST_HDR;
340 cwh->frag_id = frag_id;
341 cwh->frag_off = htons(offset);
344 list_cur->next = skb2;
347 result = list_cur = skb2;
350 remaining -= frag_size;
357 list_cur = result->next;
366 /* All of the following functions relate to fragmentation reassembly. */
368 static inline struct frag_queue *ifq_cast(struct inet_frag_queue *ifq)
370 return container_of(ifq, struct frag_queue, ifq);
373 static u32 frag_hash(struct frag_match *match)
375 return jhash_3words((__force u16)match->id, (__force u32)match->saddr,
376 (__force u32)match->daddr,
377 frag_state.rnd) & (INETFRAGS_HASHSZ - 1);
380 static struct frag_queue *queue_find(struct frag_match *match)
382 struct inet_frag_queue *ifq;
384 read_lock(&frag_state.lock);
386 ifq = inet_frag_find(&frag_netns_state, &frag_state, match, frag_hash(match));
390 /* Unlock happens inside inet_frag_find(). */
392 return ifq_cast(ifq);
395 static struct sk_buff *frag_reasm(struct frag_queue *fq, struct net_device *dev)
397 struct sk_buff *head = fq->ifq.fragments;
398 struct sk_buff *frag;
400 /* Succeed or fail, we're done with this queue. */
401 inet_frag_kill(&fq->ifq, &frag_state);
403 if (fq->ifq.len > 65535)
406 /* Can't have the head be a clone. */
407 if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
411 * We're about to build frag list for this SKB. If it already has a
412 * frag list, alloc a new SKB and put the existing frag list there.
414 if (skb_shinfo(head)->frag_list) {
418 frag = alloc_skb(0, GFP_ATOMIC);
422 frag->next = head->next;
424 skb_shinfo(frag)->frag_list = skb_shinfo(head)->frag_list;
425 skb_shinfo(head)->frag_list = NULL;
427 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
428 paged_len += skb_shinfo(head)->frags[i].size;
429 frag->len = frag->data_len = head->data_len - paged_len;
430 head->data_len -= frag->len;
431 head->len -= frag->len;
433 frag->ip_summed = head->ip_summed;
434 atomic_add(frag->truesize, &fq->ifq.net->mem);
437 skb_shinfo(head)->frag_list = head->next;
438 atomic_sub(head->truesize, &fq->ifq.net->mem);
440 /* Properly account for data in various packets. */
441 for (frag = head->next; frag; frag = frag->next) {
442 head->data_len += frag->len;
443 head->len += frag->len;
445 if (head->ip_summed != frag->ip_summed)
446 head->ip_summed = CHECKSUM_NONE;
447 else if (head->ip_summed == CHECKSUM_COMPLETE)
448 head->csum = csum_add(head->csum, frag->csum);
450 head->truesize += frag->truesize;
451 atomic_sub(frag->truesize, &fq->ifq.net->mem);
456 head->tstamp = fq->ifq.stamp;
457 fq->ifq.fragments = NULL;
462 static struct sk_buff *frag_queue(struct frag_queue *fq, struct sk_buff *skb,
463 u16 offset, bool frag_last)
465 struct sk_buff *prev, *next;
466 struct net_device *dev;
469 if (fq->ifq.last_in & INET_FRAG_COMPLETE)
475 end = offset + skb->len;
479 * Last fragment, shouldn't already have data past our end or
480 * have another last fragment.
482 if (end < fq->ifq.len || fq->ifq.last_in & INET_FRAG_LAST_IN)
485 fq->ifq.last_in |= INET_FRAG_LAST_IN;
488 /* Fragments should align to 8 byte chunks. */
489 if (end & ~FRAG_OFF_MASK)
492 if (end > fq->ifq.len) {
494 * Shouldn't have data past the end, if we already
497 if (fq->ifq.last_in & INET_FRAG_LAST_IN)
504 /* Find where we fit in. */
506 for (next = fq->ifq.fragments; next != NULL; next = next->next) {
507 if (FRAG_CB(next)->offset >= offset)
513 * Overlapping fragments aren't allowed. We shouldn't start before
514 * the end of the previous fragment.
516 if (prev && FRAG_CB(prev)->offset + prev->len > offset)
519 /* We also shouldn't end after the beginning of the next fragment. */
520 if (next && end > FRAG_CB(next)->offset)
523 FRAG_CB(skb)->offset = offset;
525 /* Link into list. */
530 fq->ifq.fragments = skb;
535 fq->ifq.stamp = skb->tstamp;
536 fq->ifq.meat += skb->len;
537 atomic_add(skb->truesize, &fq->ifq.net->mem);
539 fq->ifq.last_in |= INET_FRAG_FIRST_IN;
541 /* If we have all fragments do reassembly. */
542 if (fq->ifq.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
543 fq->ifq.meat == fq->ifq.len)
544 return frag_reasm(fq, dev);
546 write_lock(&frag_state.lock);
547 list_move_tail(&fq->ifq.lru_list, &fq->ifq.net->lru_list);
548 write_unlock(&frag_state.lock);
557 static struct sk_buff *defrag(struct sk_buff *skb, bool frag_last)
559 struct iphdr *iph = ip_hdr(skb);
560 struct capwaphdr *cwh = capwap_hdr(skb);
561 struct frag_match match;
563 struct frag_queue *fq;
565 if (atomic_read(&frag_netns_state.mem) > frag_netns_state.high_thresh)
566 inet_frag_evictor(&frag_netns_state, &frag_state);
568 match.daddr = iph->daddr;
569 match.saddr = iph->saddr;
570 match.id = cwh->frag_id;
571 frag_off = ntohs(cwh->frag_off) & FRAG_OFF_MASK;
573 fq = queue_find(&match);
575 spin_lock(&fq->ifq.lock);
576 skb = frag_queue(fq, skb, frag_off, frag_last);
577 spin_unlock(&fq->ifq.lock);
579 inet_frag_put(&fq->ifq, &frag_state);
588 static void defrag_init(void)
590 inet_frags_init(&frag_state);
591 inet_frags_init_net(&frag_netns_state);
594 static void defrag_exit(void)
596 inet_frags_exit_net(&frag_netns_state, &frag_state);
597 inet_frags_fini(&frag_state);
600 static void capwap_frag_init(struct inet_frag_queue *ifq, void *match_)
602 struct frag_match *match = match_;
604 ifq_cast(ifq)->match = *match;
607 static unsigned int capwap_frag_hash(struct inet_frag_queue *ifq)
609 return frag_hash(&ifq_cast(ifq)->match);
612 static int capwap_frag_match(struct inet_frag_queue *ifq, void *a_)
614 struct frag_match *a = a_;
615 struct frag_match *b = &ifq_cast(ifq)->match;
617 return a->id == b->id && a->saddr == b->saddr && a->daddr == b->daddr;
620 /* Run when the timeout for a given queue expires. */
621 static void capwap_frag_expire(unsigned long ifq)
623 struct frag_queue *fq;
625 fq = ifq_cast((struct inet_frag_queue *)ifq);
627 spin_lock(&fq->ifq.lock);
629 if (!(fq->ifq.last_in & INET_FRAG_COMPLETE))
630 inet_frag_kill(&fq->ifq, &frag_state);
632 spin_unlock(&fq->ifq.lock);
633 inet_frag_put(&fq->ifq, &frag_state);
636 struct vport_ops capwap_vport_ops = {
638 .flags = VPORT_F_GEN_STATS,
641 .create = capwap_create,
642 .modify = tnl_modify,
643 .destroy = tnl_destroy,
644 .set_mtu = tnl_set_mtu,
645 .set_addr = tnl_set_addr,
646 .get_name = tnl_get_name,
647 .get_addr = tnl_get_addr,
648 .get_dev_flags = vport_gen_get_dev_flags,
649 .is_running = vport_gen_is_running,
650 .get_operstate = vport_gen_get_operstate,
651 .get_mtu = tnl_get_mtu,
655 #endif /* Linux kernel >= 2.6.26 */