reassembly.c 15 KB

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