reassembly.c 15 KB

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