reassembly.c 15 KB

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