reassembly.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /* 6LoWPAN fragment reassembly
  2. *
  3. *
  4. * Authors:
  5. * Alexander Aring <aar@pengutronix.de>
  6. *
  7. * Based on: net/ipv6/reassembly.c
  8. *
  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.
  13. */
  14. #define pr_fmt(fmt) "6LoWPAN: " fmt
  15. #include <linux/net.h>
  16. #include <linux/list.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/random.h>
  19. #include <linux/jhash.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/slab.h>
  22. #include <linux/export.h>
  23. #include <net/ieee802154_netdev.h>
  24. #include <net/6lowpan.h>
  25. #include <net/ipv6.h>
  26. #include <net/inet_frag.h>
  27. #include "6lowpan_i.h"
  28. static const char lowpan_frags_cache_name[] = "lowpan-frags";
  29. struct lowpan_frag_info {
  30. u16 d_tag;
  31. u16 d_size;
  32. u8 d_offset;
  33. };
  34. static struct lowpan_frag_info *lowpan_cb(struct sk_buff *skb)
  35. {
  36. return (struct lowpan_frag_info *)skb->cb;
  37. }
  38. static struct inet_frags lowpan_frags;
  39. static int lowpan_frag_reasm(struct lowpan_frag_queue *fq,
  40. struct sk_buff *prev, struct net_device *dev);
  41. static unsigned int lowpan_hash_frag(u16 tag, u16 d_size,
  42. const struct ieee802154_addr *saddr,
  43. const struct ieee802154_addr *daddr)
  44. {
  45. net_get_random_once(&lowpan_frags.rnd, sizeof(lowpan_frags.rnd));
  46. return jhash_3words(ieee802154_addr_hash(saddr),
  47. ieee802154_addr_hash(daddr),
  48. (__force u32)(tag + (d_size << 16)),
  49. lowpan_frags.rnd);
  50. }
  51. static unsigned int lowpan_hashfn(const struct inet_frag_queue *q)
  52. {
  53. const struct lowpan_frag_queue *fq;
  54. fq = container_of(q, struct lowpan_frag_queue, q);
  55. return lowpan_hash_frag(fq->tag, fq->d_size, &fq->saddr, &fq->daddr);
  56. }
  57. static bool lowpan_frag_match(const struct inet_frag_queue *q, const void *a)
  58. {
  59. const struct lowpan_frag_queue *fq;
  60. const struct lowpan_create_arg *arg = a;
  61. fq = container_of(q, struct lowpan_frag_queue, q);
  62. return fq->tag == arg->tag && fq->d_size == arg->d_size &&
  63. ieee802154_addr_equal(&fq->saddr, arg->src) &&
  64. ieee802154_addr_equal(&fq->daddr, arg->dst);
  65. }
  66. static void lowpan_frag_init(struct inet_frag_queue *q, const void *a)
  67. {
  68. const struct lowpan_create_arg *arg = a;
  69. struct lowpan_frag_queue *fq;
  70. fq = container_of(q, struct lowpan_frag_queue, q);
  71. fq->tag = arg->tag;
  72. fq->d_size = arg->d_size;
  73. fq->saddr = *arg->src;
  74. fq->daddr = *arg->dst;
  75. }
  76. static void lowpan_frag_expire(unsigned long data)
  77. {
  78. struct frag_queue *fq;
  79. struct net *net;
  80. fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
  81. net = container_of(fq->q.net, struct net, ieee802154_lowpan.frags);
  82. spin_lock(&fq->q.lock);
  83. if (fq->q.flags & INET_FRAG_COMPLETE)
  84. goto out;
  85. inet_frag_kill(&fq->q, &lowpan_frags);
  86. out:
  87. spin_unlock(&fq->q.lock);
  88. inet_frag_put(&fq->q, &lowpan_frags);
  89. }
  90. static inline struct lowpan_frag_queue *
  91. fq_find(struct net *net, const struct lowpan_frag_info *frag_info,
  92. const struct ieee802154_addr *src,
  93. const struct ieee802154_addr *dst)
  94. {
  95. struct inet_frag_queue *q;
  96. struct lowpan_create_arg arg;
  97. unsigned int hash;
  98. struct netns_ieee802154_lowpan *ieee802154_lowpan =
  99. net_ieee802154_lowpan(net);
  100. arg.tag = frag_info->d_tag;
  101. arg.d_size = frag_info->d_size;
  102. arg.src = src;
  103. arg.dst = dst;
  104. hash = lowpan_hash_frag(frag_info->d_tag, frag_info->d_size, src, dst);
  105. q = inet_frag_find(&ieee802154_lowpan->frags,
  106. &lowpan_frags, &arg, hash);
  107. if (IS_ERR_OR_NULL(q)) {
  108. inet_frag_maybe_warn_overflow(q, pr_fmt());
  109. return NULL;
  110. }
  111. return container_of(q, struct lowpan_frag_queue, q);
  112. }
  113. static int lowpan_frag_queue(struct lowpan_frag_queue *fq,
  114. struct sk_buff *skb, const u8 frag_type)
  115. {
  116. struct sk_buff *prev, *next;
  117. struct net_device *dev;
  118. int end, offset;
  119. if (fq->q.flags & INET_FRAG_COMPLETE)
  120. goto err;
  121. offset = lowpan_cb(skb)->d_offset << 3;
  122. end = lowpan_cb(skb)->d_size;
  123. /* Is this the final fragment? */
  124. if (offset + skb->len == end) {
  125. /* If we already have some bits beyond end
  126. * or have different end, the segment is corrupted.
  127. */
  128. if (end < fq->q.len ||
  129. ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
  130. goto err;
  131. fq->q.flags |= INET_FRAG_LAST_IN;
  132. fq->q.len = end;
  133. } else {
  134. if (end > fq->q.len) {
  135. /* Some bits beyond end -> corruption. */
  136. if (fq->q.flags & INET_FRAG_LAST_IN)
  137. goto err;
  138. fq->q.len = end;
  139. }
  140. }
  141. /* Find out which fragments are in front and at the back of us
  142. * in the chain of fragments so far. We must know where to put
  143. * this fragment, right?
  144. */
  145. prev = fq->q.fragments_tail;
  146. if (!prev || lowpan_cb(prev)->d_offset < lowpan_cb(skb)->d_offset) {
  147. next = NULL;
  148. goto found;
  149. }
  150. prev = NULL;
  151. for (next = fq->q.fragments; next != NULL; next = next->next) {
  152. if (lowpan_cb(next)->d_offset >= lowpan_cb(skb)->d_offset)
  153. break; /* bingo! */
  154. prev = next;
  155. }
  156. found:
  157. /* Insert this fragment in the chain of fragments. */
  158. skb->next = next;
  159. if (!next)
  160. fq->q.fragments_tail = skb;
  161. if (prev)
  162. prev->next = skb;
  163. else
  164. fq->q.fragments = skb;
  165. dev = skb->dev;
  166. if (dev)
  167. skb->dev = NULL;
  168. fq->q.stamp = skb->tstamp;
  169. if (frag_type == LOWPAN_DISPATCH_FRAG1) {
  170. /* Calculate uncomp. 6lowpan header to estimate full size */
  171. fq->q.meat += lowpan_uncompress_size(skb, NULL);
  172. fq->q.flags |= INET_FRAG_FIRST_IN;
  173. } else {
  174. fq->q.meat += skb->len;
  175. }
  176. add_frag_mem_limit(&fq->q, skb->truesize);
  177. if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
  178. fq->q.meat == fq->q.len) {
  179. int res;
  180. unsigned long orefdst = skb->_skb_refdst;
  181. skb->_skb_refdst = 0UL;
  182. res = lowpan_frag_reasm(fq, prev, dev);
  183. skb->_skb_refdst = orefdst;
  184. return res;
  185. }
  186. return -1;
  187. err:
  188. kfree_skb(skb);
  189. return -1;
  190. }
  191. /* Check if this packet is complete.
  192. * Returns NULL on failure by any reason, and pointer
  193. * to current nexthdr field in reassembled frame.
  194. *
  195. * It is called with locked fq, and caller must check that
  196. * queue is eligible for reassembly i.e. it is not COMPLETE,
  197. * the last and the first frames arrived and all the bits are here.
  198. */
  199. static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *prev,
  200. struct net_device *dev)
  201. {
  202. struct sk_buff *fp, *head = fq->q.fragments;
  203. int sum_truesize;
  204. inet_frag_kill(&fq->q, &lowpan_frags);
  205. /* Make the one we just received the head. */
  206. if (prev) {
  207. head = prev->next;
  208. fp = skb_clone(head, GFP_ATOMIC);
  209. if (!fp)
  210. goto out_oom;
  211. fp->next = head->next;
  212. if (!fp->next)
  213. fq->q.fragments_tail = fp;
  214. prev->next = fp;
  215. skb_morph(head, fq->q.fragments);
  216. head->next = fq->q.fragments->next;
  217. consume_skb(fq->q.fragments);
  218. fq->q.fragments = head;
  219. }
  220. /* Head of list must not be cloned. */
  221. if (skb_unclone(head, GFP_ATOMIC))
  222. goto out_oom;
  223. /* If the first fragment is fragmented itself, we split
  224. * it to two chunks: the first with data and paged part
  225. * and the second, holding only fragments.
  226. */
  227. if (skb_has_frag_list(head)) {
  228. struct sk_buff *clone;
  229. int i, plen = 0;
  230. clone = alloc_skb(0, GFP_ATOMIC);
  231. if (!clone)
  232. goto out_oom;
  233. clone->next = head->next;
  234. head->next = clone;
  235. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  236. skb_frag_list_init(head);
  237. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  238. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  239. clone->len = head->data_len - plen;
  240. clone->data_len = clone->len;
  241. head->data_len -= clone->len;
  242. head->len -= clone->len;
  243. add_frag_mem_limit(&fq->q, clone->truesize);
  244. }
  245. WARN_ON(head == NULL);
  246. sum_truesize = head->truesize;
  247. for (fp = head->next; fp;) {
  248. bool headstolen;
  249. int delta;
  250. struct sk_buff *next = fp->next;
  251. sum_truesize += fp->truesize;
  252. if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
  253. kfree_skb_partial(fp, headstolen);
  254. } else {
  255. if (!skb_shinfo(head)->frag_list)
  256. skb_shinfo(head)->frag_list = fp;
  257. head->data_len += fp->len;
  258. head->len += fp->len;
  259. head->truesize += fp->truesize;
  260. }
  261. fp = next;
  262. }
  263. sub_frag_mem_limit(&fq->q, sum_truesize);
  264. head->next = NULL;
  265. head->dev = dev;
  266. head->tstamp = fq->q.stamp;
  267. fq->q.fragments = NULL;
  268. fq->q.fragments_tail = NULL;
  269. return 1;
  270. out_oom:
  271. net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n");
  272. return -1;
  273. }
  274. static int lowpan_get_frag_info(struct sk_buff *skb, const u8 frag_type,
  275. struct lowpan_frag_info *frag_info)
  276. {
  277. bool fail;
  278. u8 pattern = 0, low = 0;
  279. __be16 d_tag = 0;
  280. fail = lowpan_fetch_skb(skb, &pattern, 1);
  281. fail |= lowpan_fetch_skb(skb, &low, 1);
  282. frag_info->d_size = (pattern & 7) << 8 | low;
  283. fail |= lowpan_fetch_skb(skb, &d_tag, 2);
  284. frag_info->d_tag = ntohs(d_tag);
  285. if (frag_type == LOWPAN_DISPATCH_FRAGN) {
  286. fail |= lowpan_fetch_skb(skb, &frag_info->d_offset, 1);
  287. } else {
  288. skb_reset_network_header(skb);
  289. frag_info->d_offset = 0;
  290. }
  291. if (unlikely(fail))
  292. return -EIO;
  293. return 0;
  294. }
  295. int lowpan_frag_rcv(struct sk_buff *skb, const u8 frag_type)
  296. {
  297. struct lowpan_frag_queue *fq;
  298. struct net *net = dev_net(skb->dev);
  299. struct lowpan_frag_info *frag_info = lowpan_cb(skb);
  300. struct ieee802154_addr source, dest;
  301. int err;
  302. source = mac_cb(skb)->source;
  303. dest = mac_cb(skb)->dest;
  304. err = lowpan_get_frag_info(skb, frag_type, frag_info);
  305. if (err < 0)
  306. goto err;
  307. if (frag_info->d_size > IPV6_MIN_MTU) {
  308. net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n");
  309. goto err;
  310. }
  311. fq = fq_find(net, frag_info, &source, &dest);
  312. if (fq != NULL) {
  313. int ret;
  314. spin_lock(&fq->q.lock);
  315. ret = lowpan_frag_queue(fq, skb, frag_type);
  316. spin_unlock(&fq->q.lock);
  317. inet_frag_put(&fq->q, &lowpan_frags);
  318. return ret;
  319. }
  320. err:
  321. kfree_skb(skb);
  322. return -1;
  323. }
  324. EXPORT_SYMBOL(lowpan_frag_rcv);
  325. #ifdef CONFIG_SYSCTL
  326. static int zero;
  327. static struct ctl_table lowpan_frags_ns_ctl_table[] = {
  328. {
  329. .procname = "6lowpanfrag_high_thresh",
  330. .data = &init_net.ieee802154_lowpan.frags.high_thresh,
  331. .maxlen = sizeof(int),
  332. .mode = 0644,
  333. .proc_handler = proc_dointvec_minmax,
  334. .extra1 = &init_net.ieee802154_lowpan.frags.low_thresh
  335. },
  336. {
  337. .procname = "6lowpanfrag_low_thresh",
  338. .data = &init_net.ieee802154_lowpan.frags.low_thresh,
  339. .maxlen = sizeof(int),
  340. .mode = 0644,
  341. .proc_handler = proc_dointvec_minmax,
  342. .extra1 = &zero,
  343. .extra2 = &init_net.ieee802154_lowpan.frags.high_thresh
  344. },
  345. {
  346. .procname = "6lowpanfrag_time",
  347. .data = &init_net.ieee802154_lowpan.frags.timeout,
  348. .maxlen = sizeof(int),
  349. .mode = 0644,
  350. .proc_handler = proc_dointvec_jiffies,
  351. },
  352. { }
  353. };
  354. /* secret interval has been deprecated */
  355. static int lowpan_frags_secret_interval_unused;
  356. static struct ctl_table lowpan_frags_ctl_table[] = {
  357. {
  358. .procname = "6lowpanfrag_secret_interval",
  359. .data = &lowpan_frags_secret_interval_unused,
  360. .maxlen = sizeof(int),
  361. .mode = 0644,
  362. .proc_handler = proc_dointvec_jiffies,
  363. },
  364. { }
  365. };
  366. static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
  367. {
  368. struct ctl_table *table;
  369. struct ctl_table_header *hdr;
  370. struct netns_ieee802154_lowpan *ieee802154_lowpan =
  371. net_ieee802154_lowpan(net);
  372. table = lowpan_frags_ns_ctl_table;
  373. if (!net_eq(net, &init_net)) {
  374. table = kmemdup(table, sizeof(lowpan_frags_ns_ctl_table),
  375. GFP_KERNEL);
  376. if (table == NULL)
  377. goto err_alloc;
  378. table[0].data = &ieee802154_lowpan->frags.high_thresh;
  379. table[0].extra1 = &ieee802154_lowpan->frags.low_thresh;
  380. table[0].extra2 = &init_net.ieee802154_lowpan.frags.high_thresh;
  381. table[1].data = &ieee802154_lowpan->frags.low_thresh;
  382. table[1].extra2 = &ieee802154_lowpan->frags.high_thresh;
  383. table[2].data = &ieee802154_lowpan->frags.timeout;
  384. /* Don't export sysctls to unprivileged users */
  385. if (net->user_ns != &init_user_ns)
  386. table[0].procname = NULL;
  387. }
  388. hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table);
  389. if (hdr == NULL)
  390. goto err_reg;
  391. ieee802154_lowpan->sysctl.frags_hdr = hdr;
  392. return 0;
  393. err_reg:
  394. if (!net_eq(net, &init_net))
  395. kfree(table);
  396. err_alloc:
  397. return -ENOMEM;
  398. }
  399. static void __net_exit lowpan_frags_ns_sysctl_unregister(struct net *net)
  400. {
  401. struct ctl_table *table;
  402. struct netns_ieee802154_lowpan *ieee802154_lowpan =
  403. net_ieee802154_lowpan(net);
  404. table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg;
  405. unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr);
  406. if (!net_eq(net, &init_net))
  407. kfree(table);
  408. }
  409. static struct ctl_table_header *lowpan_ctl_header;
  410. static int __init lowpan_frags_sysctl_register(void)
  411. {
  412. lowpan_ctl_header = register_net_sysctl(&init_net,
  413. "net/ieee802154/6lowpan",
  414. lowpan_frags_ctl_table);
  415. return lowpan_ctl_header == NULL ? -ENOMEM : 0;
  416. }
  417. static void lowpan_frags_sysctl_unregister(void)
  418. {
  419. unregister_net_sysctl_table(lowpan_ctl_header);
  420. }
  421. #else
  422. static inline int lowpan_frags_ns_sysctl_register(struct net *net)
  423. {
  424. return 0;
  425. }
  426. static inline void lowpan_frags_ns_sysctl_unregister(struct net *net)
  427. {
  428. }
  429. static inline int __init lowpan_frags_sysctl_register(void)
  430. {
  431. return 0;
  432. }
  433. static inline void lowpan_frags_sysctl_unregister(void)
  434. {
  435. }
  436. #endif
  437. static int __net_init lowpan_frags_init_net(struct net *net)
  438. {
  439. struct netns_ieee802154_lowpan *ieee802154_lowpan =
  440. net_ieee802154_lowpan(net);
  441. ieee802154_lowpan->frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
  442. ieee802154_lowpan->frags.low_thresh = IPV6_FRAG_LOW_THRESH;
  443. ieee802154_lowpan->frags.timeout = IPV6_FRAG_TIMEOUT;
  444. inet_frags_init_net(&ieee802154_lowpan->frags);
  445. return lowpan_frags_ns_sysctl_register(net);
  446. }
  447. static void __net_exit lowpan_frags_exit_net(struct net *net)
  448. {
  449. struct netns_ieee802154_lowpan *ieee802154_lowpan =
  450. net_ieee802154_lowpan(net);
  451. lowpan_frags_ns_sysctl_unregister(net);
  452. inet_frags_exit_net(&ieee802154_lowpan->frags, &lowpan_frags);
  453. }
  454. static struct pernet_operations lowpan_frags_ops = {
  455. .init = lowpan_frags_init_net,
  456. .exit = lowpan_frags_exit_net,
  457. };
  458. int __init lowpan_net_frag_init(void)
  459. {
  460. int ret;
  461. ret = lowpan_frags_sysctl_register();
  462. if (ret)
  463. return ret;
  464. ret = register_pernet_subsys(&lowpan_frags_ops);
  465. if (ret)
  466. goto err_pernet;
  467. lowpan_frags.hashfn = lowpan_hashfn;
  468. lowpan_frags.constructor = lowpan_frag_init;
  469. lowpan_frags.destructor = NULL;
  470. lowpan_frags.skb_free = NULL;
  471. lowpan_frags.qsize = sizeof(struct frag_queue);
  472. lowpan_frags.match = lowpan_frag_match;
  473. lowpan_frags.frag_expire = lowpan_frag_expire;
  474. lowpan_frags.frags_cache_name = lowpan_frags_cache_name;
  475. ret = inet_frags_init(&lowpan_frags);
  476. if (ret)
  477. goto err_pernet;
  478. return ret;
  479. err_pernet:
  480. lowpan_frags_sysctl_unregister();
  481. return ret;
  482. }
  483. void lowpan_net_frag_exit(void)
  484. {
  485. inet_frags_fini(&lowpan_frags);
  486. lowpan_frags_sysctl_unregister();
  487. unregister_pernet_subsys(&lowpan_frags_ops);
  488. }