1 /* 6LoWPAN fragment reassembly
5 * Alexander Aring <aar@pengutronix.de>
7 * Based on: net/ipv6/reassembly.c
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
15 #define pr_fmt(fmt) "6LoWPAN: " fmt
17 #include <linux/net.h>
18 #include <linux/list.h>
19 #include <linux/netdevice.h>
20 #include <linux/random.h>
21 #include <linux/jhash.h>
22 #include <linux/skbuff.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
26 #include <net/ieee802154_netdev.h>
27 #include <net/6lowpan.h>
29 #include <net/inet_frag.h>
31 #include "6lowpan_i.h"
33 static const char lowpan_frags_cache_name[] = "lowpan-frags";
35 static struct inet_frags lowpan_frags;
37 static int lowpan_frag_reasm(struct lowpan_frag_queue *fq,
38 struct sk_buff *prev, struct net_device *ldev);
40 static void lowpan_frag_init(struct inet_frag_queue *q, const void *a)
42 const struct frag_lowpan_compare_key *key = a;
43 struct lowpan_frag_queue *fq;
45 fq = container_of(q, struct lowpan_frag_queue, q);
47 BUILD_BUG_ON(sizeof(*key) > sizeof(q->key));
48 memcpy(&q->key, key, sizeof(*key));
51 static void lowpan_frag_expire(unsigned long data)
53 struct frag_queue *fq;
56 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
57 net = container_of(fq->q.net, struct net, ieee802154_lowpan.frags);
59 spin_lock(&fq->q.lock);
61 if (fq->q.flags & INET_FRAG_COMPLETE)
64 inet_frag_kill(&fq->q);
66 spin_unlock(&fq->q.lock);
67 inet_frag_put(&fq->q);
70 static inline struct lowpan_frag_queue *
71 fq_find(struct net *net, const struct lowpan_802154_cb *cb,
72 const struct ieee802154_addr *src,
73 const struct ieee802154_addr *dst)
75 struct netns_ieee802154_lowpan *ieee802154_lowpan =
76 net_ieee802154_lowpan(net);
77 struct frag_lowpan_compare_key key = {
83 struct inet_frag_queue *q;
85 q = inet_frag_find(&ieee802154_lowpan->frags, &key);
89 return container_of(q, struct lowpan_frag_queue, q);
92 static int lowpan_frag_queue(struct lowpan_frag_queue *fq,
93 struct sk_buff *skb, u8 frag_type)
95 struct sk_buff *prev, *next;
96 struct net_device *ldev;
99 if (fq->q.flags & INET_FRAG_COMPLETE)
102 offset = lowpan_802154_cb(skb)->d_offset << 3;
103 end = lowpan_802154_cb(skb)->d_size;
105 /* Is this the final fragment? */
106 if (offset + skb->len == end) {
107 /* If we already have some bits beyond end
108 * or have different end, the segment is corrupted.
110 if (end < fq->q.len ||
111 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
113 fq->q.flags |= INET_FRAG_LAST_IN;
116 if (end > fq->q.len) {
117 /* Some bits beyond end -> corruption. */
118 if (fq->q.flags & INET_FRAG_LAST_IN)
124 /* Find out which fragments are in front and at the back of us
125 * in the chain of fragments so far. We must know where to put
126 * this fragment, right?
128 prev = fq->q.fragments_tail;
130 lowpan_802154_cb(prev)->d_offset <
131 lowpan_802154_cb(skb)->d_offset) {
136 for (next = fq->q.fragments; next != NULL; next = next->next) {
137 if (lowpan_802154_cb(next)->d_offset >=
138 lowpan_802154_cb(skb)->d_offset)
144 /* Insert this fragment in the chain of fragments. */
147 fq->q.fragments_tail = skb;
151 fq->q.fragments = skb;
157 fq->q.stamp = skb->tstamp;
158 if (frag_type == LOWPAN_DISPATCH_FRAG1)
159 fq->q.flags |= INET_FRAG_FIRST_IN;
161 fq->q.meat += skb->len;
162 add_frag_mem_limit(fq->q.net, skb->truesize);
164 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
165 fq->q.meat == fq->q.len) {
167 unsigned long orefdst = skb->_skb_refdst;
169 skb->_skb_refdst = 0UL;
170 res = lowpan_frag_reasm(fq, prev, ldev);
171 skb->_skb_refdst = orefdst;
181 /* Check if this packet is complete.
182 * Returns NULL on failure by any reason, and pointer
183 * to current nexthdr field in reassembled frame.
185 * It is called with locked fq, and caller must check that
186 * queue is eligible for reassembly i.e. it is not COMPLETE,
187 * the last and the first frames arrived and all the bits are here.
189 static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *prev,
190 struct net_device *ldev)
192 struct sk_buff *fp, *head = fq->q.fragments;
195 inet_frag_kill(&fq->q);
197 /* Make the one we just received the head. */
200 fp = skb_clone(head, GFP_ATOMIC);
205 fp->next = head->next;
207 fq->q.fragments_tail = fp;
210 skb_morph(head, fq->q.fragments);
211 head->next = fq->q.fragments->next;
213 consume_skb(fq->q.fragments);
214 fq->q.fragments = head;
217 /* Head of list must not be cloned. */
218 if (skb_unclone(head, GFP_ATOMIC))
221 /* If the first fragment is fragmented itself, we split
222 * it to two chunks: the first with data and paged part
223 * and the second, holding only fragments.
225 if (skb_has_frag_list(head)) {
226 struct sk_buff *clone;
229 clone = alloc_skb(0, GFP_ATOMIC);
232 clone->next = head->next;
234 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
235 skb_frag_list_init(head);
236 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
237 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
238 clone->len = head->data_len - plen;
239 clone->data_len = clone->len;
240 head->data_len -= clone->len;
241 head->len -= clone->len;
242 add_frag_mem_limit(fq->q.net, clone->truesize);
245 WARN_ON(head == NULL);
247 sum_truesize = head->truesize;
248 for (fp = head->next; fp;) {
251 struct sk_buff *next = fp->next;
253 sum_truesize += fp->truesize;
254 if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
255 kfree_skb_partial(fp, headstolen);
257 if (!skb_shinfo(head)->frag_list)
258 skb_shinfo(head)->frag_list = fp;
259 head->data_len += fp->len;
260 head->len += fp->len;
261 head->truesize += fp->truesize;
265 sub_frag_mem_limit(fq->q.net, sum_truesize);
269 head->tstamp = fq->q.stamp;
271 fq->q.fragments = NULL;
272 fq->q.fragments_tail = NULL;
276 net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n");
280 static int lowpan_frag_rx_handlers_result(struct sk_buff *skb,
281 lowpan_rx_result res)
285 return NET_RX_SUCCESS;
287 /* nobody cared about this packet */
288 net_warn_ratelimited("%s: received unknown dispatch\n",
293 /* all others failure */
298 static lowpan_rx_result lowpan_frag_rx_h_iphc(struct sk_buff *skb)
302 if (!lowpan_is_iphc(*skb_network_header(skb)))
305 ret = lowpan_iphc_decompress(skb);
312 static int lowpan_invoke_frag_rx_handlers(struct sk_buff *skb)
314 lowpan_rx_result res;
316 #define CALL_RXH(rxh) \
319 if (res != RX_CONTINUE) \
323 /* likely at first */
324 CALL_RXH(lowpan_frag_rx_h_iphc);
325 CALL_RXH(lowpan_rx_h_ipv6);
328 return lowpan_frag_rx_handlers_result(skb, res);
332 #define LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK 0x07
333 #define LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT 8
335 static int lowpan_get_cb(struct sk_buff *skb, u8 frag_type,
336 struct lowpan_802154_cb *cb)
339 u8 high = 0, low = 0;
342 fail = lowpan_fetch_skb(skb, &high, 1);
343 fail |= lowpan_fetch_skb(skb, &low, 1);
344 /* remove the dispatch value and use first three bits as high value
345 * for the datagram size
347 cb->d_size = (high & LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK) <<
348 LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT | low;
349 fail |= lowpan_fetch_skb(skb, &d_tag, 2);
350 cb->d_tag = ntohs(d_tag);
352 if (frag_type == LOWPAN_DISPATCH_FRAGN) {
353 fail |= lowpan_fetch_skb(skb, &cb->d_offset, 1);
355 skb_reset_network_header(skb);
357 /* check if datagram_size has ipv6hdr on FRAG1 */
358 fail |= cb->d_size < sizeof(struct ipv6hdr);
359 /* check if we can dereference the dispatch value */
369 int lowpan_frag_rcv(struct sk_buff *skb, u8 frag_type)
371 struct lowpan_frag_queue *fq;
372 struct net *net = dev_net(skb->dev);
373 struct lowpan_802154_cb *cb = lowpan_802154_cb(skb);
374 struct ieee802154_hdr hdr;
377 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
380 err = lowpan_get_cb(skb, frag_type, cb);
384 if (frag_type == LOWPAN_DISPATCH_FRAG1) {
385 err = lowpan_invoke_frag_rx_handlers(skb);
386 if (err == NET_RX_DROP)
390 if (cb->d_size > IPV6_MIN_MTU) {
391 net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n");
395 fq = fq_find(net, cb, &hdr.source, &hdr.dest);
399 spin_lock(&fq->q.lock);
400 ret = lowpan_frag_queue(fq, skb, frag_type);
401 spin_unlock(&fq->q.lock);
403 inet_frag_put(&fq->q);
415 static struct ctl_table lowpan_frags_ns_ctl_table[] = {
417 .procname = "6lowpanfrag_high_thresh",
418 .data = &init_net.ieee802154_lowpan.frags.high_thresh,
419 .maxlen = sizeof(unsigned long),
421 .proc_handler = proc_doulongvec_minmax,
422 .extra1 = &init_net.ieee802154_lowpan.frags.low_thresh
425 .procname = "6lowpanfrag_low_thresh",
426 .data = &init_net.ieee802154_lowpan.frags.low_thresh,
427 .maxlen = sizeof(unsigned long),
429 .proc_handler = proc_doulongvec_minmax,
431 .extra2 = &init_net.ieee802154_lowpan.frags.high_thresh
434 .procname = "6lowpanfrag_time",
435 .data = &init_net.ieee802154_lowpan.frags.timeout,
436 .maxlen = sizeof(int),
438 .proc_handler = proc_dointvec_jiffies,
443 /* secret interval has been deprecated */
444 static int lowpan_frags_secret_interval_unused;
445 static struct ctl_table lowpan_frags_ctl_table[] = {
447 .procname = "6lowpanfrag_secret_interval",
448 .data = &lowpan_frags_secret_interval_unused,
449 .maxlen = sizeof(int),
451 .proc_handler = proc_dointvec_jiffies,
456 static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
458 struct ctl_table *table;
459 struct ctl_table_header *hdr;
460 struct netns_ieee802154_lowpan *ieee802154_lowpan =
461 net_ieee802154_lowpan(net);
463 table = lowpan_frags_ns_ctl_table;
464 if (!net_eq(net, &init_net)) {
465 table = kmemdup(table, sizeof(lowpan_frags_ns_ctl_table),
470 table[0].data = &ieee802154_lowpan->frags.high_thresh;
471 table[0].extra1 = &ieee802154_lowpan->frags.low_thresh;
472 table[0].extra2 = &init_net.ieee802154_lowpan.frags.high_thresh;
473 table[1].data = &ieee802154_lowpan->frags.low_thresh;
474 table[1].extra2 = &ieee802154_lowpan->frags.high_thresh;
475 table[2].data = &ieee802154_lowpan->frags.timeout;
477 /* Don't export sysctls to unprivileged users */
478 if (net->user_ns != &init_user_ns)
479 table[0].procname = NULL;
482 hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table);
486 ieee802154_lowpan->sysctl.frags_hdr = hdr;
490 if (!net_eq(net, &init_net))
496 static void __net_exit lowpan_frags_ns_sysctl_unregister(struct net *net)
498 struct ctl_table *table;
499 struct netns_ieee802154_lowpan *ieee802154_lowpan =
500 net_ieee802154_lowpan(net);
502 table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg;
503 unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr);
504 if (!net_eq(net, &init_net))
508 static struct ctl_table_header *lowpan_ctl_header;
510 static int __init lowpan_frags_sysctl_register(void)
512 lowpan_ctl_header = register_net_sysctl(&init_net,
513 "net/ieee802154/6lowpan",
514 lowpan_frags_ctl_table);
515 return lowpan_ctl_header == NULL ? -ENOMEM : 0;
518 static void lowpan_frags_sysctl_unregister(void)
520 unregister_net_sysctl_table(lowpan_ctl_header);
523 static inline int lowpan_frags_ns_sysctl_register(struct net *net)
528 static inline void lowpan_frags_ns_sysctl_unregister(struct net *net)
532 static inline int __init lowpan_frags_sysctl_register(void)
537 static inline void lowpan_frags_sysctl_unregister(void)
542 static int __net_init lowpan_frags_init_net(struct net *net)
544 struct netns_ieee802154_lowpan *ieee802154_lowpan =
545 net_ieee802154_lowpan(net);
548 ieee802154_lowpan->frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
549 ieee802154_lowpan->frags.low_thresh = IPV6_FRAG_LOW_THRESH;
550 ieee802154_lowpan->frags.timeout = IPV6_FRAG_TIMEOUT;
551 ieee802154_lowpan->frags.f = &lowpan_frags;
553 res = inet_frags_init_net(&ieee802154_lowpan->frags);
556 res = lowpan_frags_ns_sysctl_register(net);
558 inet_frags_exit_net(&ieee802154_lowpan->frags);
562 static void __net_exit lowpan_frags_exit_net(struct net *net)
564 struct netns_ieee802154_lowpan *ieee802154_lowpan =
565 net_ieee802154_lowpan(net);
567 lowpan_frags_ns_sysctl_unregister(net);
568 inet_frags_exit_net(&ieee802154_lowpan->frags);
571 static struct pernet_operations lowpan_frags_ops = {
572 .init = lowpan_frags_init_net,
573 .exit = lowpan_frags_exit_net,
576 static u32 lowpan_key_hashfn(const void *data, u32 len, u32 seed)
579 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
582 static u32 lowpan_obj_hashfn(const void *data, u32 len, u32 seed)
584 const struct inet_frag_queue *fq = data;
586 return jhash2((const u32 *)&fq->key,
587 sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
590 static int lowpan_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
592 const struct frag_lowpan_compare_key *key = arg->key;
593 const struct inet_frag_queue *fq = ptr;
595 return !!memcmp(&fq->key, key, sizeof(*key));
598 static const struct rhashtable_params lowpan_rhash_params = {
599 .head_offset = offsetof(struct inet_frag_queue, node),
600 .hashfn = lowpan_key_hashfn,
601 .obj_hashfn = lowpan_obj_hashfn,
602 .obj_cmpfn = lowpan_obj_cmpfn,
603 .automatic_shrinking = true,
606 int __init lowpan_net_frag_init(void)
610 lowpan_frags.constructor = lowpan_frag_init;
611 lowpan_frags.destructor = NULL;
612 lowpan_frags.qsize = sizeof(struct frag_queue);
613 lowpan_frags.frag_expire = lowpan_frag_expire;
614 lowpan_frags.frags_cache_name = lowpan_frags_cache_name;
615 lowpan_frags.rhash_params = lowpan_rhash_params;
616 ret = inet_frags_init(&lowpan_frags);
620 ret = lowpan_frags_sysctl_register();
624 ret = register_pernet_subsys(&lowpan_frags_ops);
630 lowpan_frags_sysctl_unregister();
632 inet_frags_fini(&lowpan_frags);
636 void lowpan_net_frag_exit(void)
638 inet_frags_fini(&lowpan_frags);
639 lowpan_frags_sysctl_unregister();
640 unregister_pernet_subsys(&lowpan_frags_ops);