inet_fragment.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * inet fragments management
  4. *
  5. * Authors: Pavel Emelyanov <[email protected]>
  6. * Started as consolidation of ipv4/ip_fragment.c,
  7. * ipv6/reassembly. and ipv6 nf conntrack reassembly
  8. */
  9. #include <linux/list.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/module.h>
  12. #include <linux/timer.h>
  13. #include <linux/mm.h>
  14. #include <linux/random.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/slab.h>
  18. #include <linux/rhashtable.h>
  19. #include <net/sock.h>
  20. #include <net/inet_frag.h>
  21. #include <net/inet_ecn.h>
  22. #include <net/ip.h>
  23. #include <net/ipv6.h>
  24. /* Use skb->cb to track consecutive/adjacent fragments coming at
  25. * the end of the queue. Nodes in the rb-tree queue will
  26. * contain "runs" of one or more adjacent fragments.
  27. *
  28. * Invariants:
  29. * - next_frag is NULL at the tail of a "run";
  30. * - the head of a "run" has the sum of all fragment lengths in frag_run_len.
  31. */
  32. struct ipfrag_skb_cb {
  33. union {
  34. struct inet_skb_parm h4;
  35. struct inet6_skb_parm h6;
  36. };
  37. struct sk_buff *next_frag;
  38. int frag_run_len;
  39. };
  40. #define FRAG_CB(skb) ((struct ipfrag_skb_cb *)((skb)->cb))
  41. static void fragcb_clear(struct sk_buff *skb)
  42. {
  43. RB_CLEAR_NODE(&skb->rbnode);
  44. FRAG_CB(skb)->next_frag = NULL;
  45. FRAG_CB(skb)->frag_run_len = skb->len;
  46. }
  47. /* Append skb to the last "run". */
  48. static void fragrun_append_to_last(struct inet_frag_queue *q,
  49. struct sk_buff *skb)
  50. {
  51. fragcb_clear(skb);
  52. FRAG_CB(q->last_run_head)->frag_run_len += skb->len;
  53. FRAG_CB(q->fragments_tail)->next_frag = skb;
  54. q->fragments_tail = skb;
  55. }
  56. /* Create a new "run" with the skb. */
  57. static void fragrun_create(struct inet_frag_queue *q, struct sk_buff *skb)
  58. {
  59. BUILD_BUG_ON(sizeof(struct ipfrag_skb_cb) > sizeof(skb->cb));
  60. fragcb_clear(skb);
  61. if (q->last_run_head)
  62. rb_link_node(&skb->rbnode, &q->last_run_head->rbnode,
  63. &q->last_run_head->rbnode.rb_right);
  64. else
  65. rb_link_node(&skb->rbnode, NULL, &q->rb_fragments.rb_node);
  66. rb_insert_color(&skb->rbnode, &q->rb_fragments);
  67. q->fragments_tail = skb;
  68. q->last_run_head = skb;
  69. }
  70. /* Given the OR values of all fragments, apply RFC 3168 5.3 requirements
  71. * Value : 0xff if frame should be dropped.
  72. * 0 or INET_ECN_CE value, to be ORed in to final iph->tos field
  73. */
  74. const u8 ip_frag_ecn_table[16] = {
  75. /* at least one fragment had CE, and others ECT_0 or ECT_1 */
  76. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = INET_ECN_CE,
  77. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  78. [IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = INET_ECN_CE,
  79. /* invalid combinations : drop frame */
  80. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE] = 0xff,
  81. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0] = 0xff,
  82. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_1] = 0xff,
  83. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  84. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0] = 0xff,
  85. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_1] = 0xff,
  86. [IPFRAG_ECN_NOT_ECT | IPFRAG_ECN_CE | IPFRAG_ECN_ECT_0 | IPFRAG_ECN_ECT_1] = 0xff,
  87. };
  88. EXPORT_SYMBOL(ip_frag_ecn_table);
  89. int inet_frags_init(struct inet_frags *f)
  90. {
  91. f->frags_cachep = kmem_cache_create(f->frags_cache_name, f->qsize, 0, 0,
  92. NULL);
  93. if (!f->frags_cachep)
  94. return -ENOMEM;
  95. refcount_set(&f->refcnt, 1);
  96. init_completion(&f->completion);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL(inet_frags_init);
  100. void inet_frags_fini(struct inet_frags *f)
  101. {
  102. if (refcount_dec_and_test(&f->refcnt))
  103. complete(&f->completion);
  104. wait_for_completion(&f->completion);
  105. kmem_cache_destroy(f->frags_cachep);
  106. f->frags_cachep = NULL;
  107. }
  108. EXPORT_SYMBOL(inet_frags_fini);
  109. /* called from rhashtable_free_and_destroy() at netns_frags dismantle */
  110. static void inet_frags_free_cb(void *ptr, void *arg)
  111. {
  112. struct inet_frag_queue *fq = ptr;
  113. int count;
  114. count = del_timer_sync(&fq->timer) ? 1 : 0;
  115. spin_lock_bh(&fq->lock);
  116. if (!(fq->flags & INET_FRAG_COMPLETE)) {
  117. fq->flags |= INET_FRAG_COMPLETE;
  118. count++;
  119. } else if (fq->flags & INET_FRAG_HASH_DEAD) {
  120. count++;
  121. }
  122. spin_unlock_bh(&fq->lock);
  123. if (refcount_sub_and_test(count, &fq->refcnt))
  124. inet_frag_destroy(fq);
  125. }
  126. static LLIST_HEAD(fqdir_free_list);
  127. static void fqdir_free_fn(struct work_struct *work)
  128. {
  129. struct llist_node *kill_list;
  130. struct fqdir *fqdir, *tmp;
  131. struct inet_frags *f;
  132. /* Atomically snapshot the list of fqdirs to free */
  133. kill_list = llist_del_all(&fqdir_free_list);
  134. /* We need to make sure all ongoing call_rcu(..., inet_frag_destroy_rcu)
  135. * have completed, since they need to dereference fqdir.
  136. * Would it not be nice to have kfree_rcu_barrier() ? :)
  137. */
  138. rcu_barrier();
  139. llist_for_each_entry_safe(fqdir, tmp, kill_list, free_list) {
  140. f = fqdir->f;
  141. if (refcount_dec_and_test(&f->refcnt))
  142. complete(&f->completion);
  143. kfree(fqdir);
  144. }
  145. }
  146. static DECLARE_WORK(fqdir_free_work, fqdir_free_fn);
  147. static void fqdir_work_fn(struct work_struct *work)
  148. {
  149. struct fqdir *fqdir = container_of(work, struct fqdir, destroy_work);
  150. rhashtable_free_and_destroy(&fqdir->rhashtable, inet_frags_free_cb, NULL);
  151. if (llist_add(&fqdir->free_list, &fqdir_free_list))
  152. queue_work(system_wq, &fqdir_free_work);
  153. }
  154. int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net)
  155. {
  156. struct fqdir *fqdir = kzalloc(sizeof(*fqdir), GFP_KERNEL);
  157. int res;
  158. if (!fqdir)
  159. return -ENOMEM;
  160. fqdir->f = f;
  161. fqdir->net = net;
  162. res = rhashtable_init(&fqdir->rhashtable, &fqdir->f->rhash_params);
  163. if (res < 0) {
  164. kfree(fqdir);
  165. return res;
  166. }
  167. refcount_inc(&f->refcnt);
  168. *fqdirp = fqdir;
  169. return 0;
  170. }
  171. EXPORT_SYMBOL(fqdir_init);
  172. static struct workqueue_struct *inet_frag_wq;
  173. static int __init inet_frag_wq_init(void)
  174. {
  175. inet_frag_wq = create_workqueue("inet_frag_wq");
  176. if (!inet_frag_wq)
  177. panic("Could not create inet frag workq");
  178. return 0;
  179. }
  180. pure_initcall(inet_frag_wq_init);
  181. void fqdir_exit(struct fqdir *fqdir)
  182. {
  183. INIT_WORK(&fqdir->destroy_work, fqdir_work_fn);
  184. queue_work(inet_frag_wq, &fqdir->destroy_work);
  185. }
  186. EXPORT_SYMBOL(fqdir_exit);
  187. void inet_frag_kill(struct inet_frag_queue *fq)
  188. {
  189. if (del_timer(&fq->timer))
  190. refcount_dec(&fq->refcnt);
  191. if (!(fq->flags & INET_FRAG_COMPLETE)) {
  192. struct fqdir *fqdir = fq->fqdir;
  193. fq->flags |= INET_FRAG_COMPLETE;
  194. rcu_read_lock();
  195. /* The RCU read lock provides a memory barrier
  196. * guaranteeing that if fqdir->dead is false then
  197. * the hash table destruction will not start until
  198. * after we unlock. Paired with fqdir_pre_exit().
  199. */
  200. if (!READ_ONCE(fqdir->dead)) {
  201. rhashtable_remove_fast(&fqdir->rhashtable, &fq->node,
  202. fqdir->f->rhash_params);
  203. refcount_dec(&fq->refcnt);
  204. } else {
  205. fq->flags |= INET_FRAG_HASH_DEAD;
  206. }
  207. rcu_read_unlock();
  208. }
  209. }
  210. EXPORT_SYMBOL(inet_frag_kill);
  211. static void inet_frag_destroy_rcu(struct rcu_head *head)
  212. {
  213. struct inet_frag_queue *q = container_of(head, struct inet_frag_queue,
  214. rcu);
  215. struct inet_frags *f = q->fqdir->f;
  216. if (f->destructor)
  217. f->destructor(q);
  218. kmem_cache_free(f->frags_cachep, q);
  219. }
  220. unsigned int inet_frag_rbtree_purge(struct rb_root *root)
  221. {
  222. struct rb_node *p = rb_first(root);
  223. unsigned int sum = 0;
  224. while (p) {
  225. struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
  226. p = rb_next(p);
  227. rb_erase(&skb->rbnode, root);
  228. while (skb) {
  229. struct sk_buff *next = FRAG_CB(skb)->next_frag;
  230. sum += skb->truesize;
  231. kfree_skb(skb);
  232. skb = next;
  233. }
  234. }
  235. return sum;
  236. }
  237. EXPORT_SYMBOL(inet_frag_rbtree_purge);
  238. void inet_frag_destroy(struct inet_frag_queue *q)
  239. {
  240. struct fqdir *fqdir;
  241. unsigned int sum, sum_truesize = 0;
  242. struct inet_frags *f;
  243. WARN_ON(!(q->flags & INET_FRAG_COMPLETE));
  244. WARN_ON(del_timer(&q->timer) != 0);
  245. /* Release all fragment data. */
  246. fqdir = q->fqdir;
  247. f = fqdir->f;
  248. sum_truesize = inet_frag_rbtree_purge(&q->rb_fragments);
  249. sum = sum_truesize + f->qsize;
  250. call_rcu(&q->rcu, inet_frag_destroy_rcu);
  251. sub_frag_mem_limit(fqdir, sum);
  252. }
  253. EXPORT_SYMBOL(inet_frag_destroy);
  254. static struct inet_frag_queue *inet_frag_alloc(struct fqdir *fqdir,
  255. struct inet_frags *f,
  256. void *arg)
  257. {
  258. struct inet_frag_queue *q;
  259. q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
  260. if (!q)
  261. return NULL;
  262. q->fqdir = fqdir;
  263. f->constructor(q, arg);
  264. add_frag_mem_limit(fqdir, f->qsize);
  265. timer_setup(&q->timer, f->frag_expire, 0);
  266. spin_lock_init(&q->lock);
  267. refcount_set(&q->refcnt, 3);
  268. return q;
  269. }
  270. static struct inet_frag_queue *inet_frag_create(struct fqdir *fqdir,
  271. void *arg,
  272. struct inet_frag_queue **prev)
  273. {
  274. struct inet_frags *f = fqdir->f;
  275. struct inet_frag_queue *q;
  276. q = inet_frag_alloc(fqdir, f, arg);
  277. if (!q) {
  278. *prev = ERR_PTR(-ENOMEM);
  279. return NULL;
  280. }
  281. mod_timer(&q->timer, jiffies + fqdir->timeout);
  282. *prev = rhashtable_lookup_get_insert_key(&fqdir->rhashtable, &q->key,
  283. &q->node, f->rhash_params);
  284. if (*prev) {
  285. q->flags |= INET_FRAG_COMPLETE;
  286. inet_frag_kill(q);
  287. inet_frag_destroy(q);
  288. return NULL;
  289. }
  290. return q;
  291. }
  292. /* TODO : call from rcu_read_lock() and no longer use refcount_inc_not_zero() */
  293. struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key)
  294. {
  295. /* This pairs with WRITE_ONCE() in fqdir_pre_exit(). */
  296. long high_thresh = READ_ONCE(fqdir->high_thresh);
  297. struct inet_frag_queue *fq = NULL, *prev;
  298. if (!high_thresh || frag_mem_limit(fqdir) > high_thresh)
  299. return NULL;
  300. rcu_read_lock();
  301. prev = rhashtable_lookup(&fqdir->rhashtable, key, fqdir->f->rhash_params);
  302. if (!prev)
  303. fq = inet_frag_create(fqdir, key, &prev);
  304. if (!IS_ERR_OR_NULL(prev)) {
  305. fq = prev;
  306. if (!refcount_inc_not_zero(&fq->refcnt))
  307. fq = NULL;
  308. }
  309. rcu_read_unlock();
  310. return fq;
  311. }
  312. EXPORT_SYMBOL(inet_frag_find);
  313. int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
  314. int offset, int end)
  315. {
  316. struct sk_buff *last = q->fragments_tail;
  317. /* RFC5722, Section 4, amended by Errata ID : 3089
  318. * When reassembling an IPv6 datagram, if
  319. * one or more its constituent fragments is determined to be an
  320. * overlapping fragment, the entire datagram (and any constituent
  321. * fragments) MUST be silently discarded.
  322. *
  323. * Duplicates, however, should be ignored (i.e. skb dropped, but the
  324. * queue/fragments kept for later reassembly).
  325. */
  326. if (!last)
  327. fragrun_create(q, skb); /* First fragment. */
  328. else if (last->ip_defrag_offset + last->len < end) {
  329. /* This is the common case: skb goes to the end. */
  330. /* Detect and discard overlaps. */
  331. if (offset < last->ip_defrag_offset + last->len)
  332. return IPFRAG_OVERLAP;
  333. if (offset == last->ip_defrag_offset + last->len)
  334. fragrun_append_to_last(q, skb);
  335. else
  336. fragrun_create(q, skb);
  337. } else {
  338. /* Binary search. Note that skb can become the first fragment,
  339. * but not the last (covered above).
  340. */
  341. struct rb_node **rbn, *parent;
  342. rbn = &q->rb_fragments.rb_node;
  343. do {
  344. struct sk_buff *curr;
  345. int curr_run_end;
  346. parent = *rbn;
  347. curr = rb_to_skb(parent);
  348. curr_run_end = curr->ip_defrag_offset +
  349. FRAG_CB(curr)->frag_run_len;
  350. if (end <= curr->ip_defrag_offset)
  351. rbn = &parent->rb_left;
  352. else if (offset >= curr_run_end)
  353. rbn = &parent->rb_right;
  354. else if (offset >= curr->ip_defrag_offset &&
  355. end <= curr_run_end)
  356. return IPFRAG_DUP;
  357. else
  358. return IPFRAG_OVERLAP;
  359. } while (*rbn);
  360. /* Here we have parent properly set, and rbn pointing to
  361. * one of its NULL left/right children. Insert skb.
  362. */
  363. fragcb_clear(skb);
  364. rb_link_node(&skb->rbnode, parent, rbn);
  365. rb_insert_color(&skb->rbnode, &q->rb_fragments);
  366. }
  367. skb->ip_defrag_offset = offset;
  368. return IPFRAG_OK;
  369. }
  370. EXPORT_SYMBOL(inet_frag_queue_insert);
  371. void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
  372. struct sk_buff *parent)
  373. {
  374. struct sk_buff *fp, *head = skb_rb_first(&q->rb_fragments);
  375. struct sk_buff **nextp;
  376. int delta;
  377. if (head != skb) {
  378. fp = skb_clone(skb, GFP_ATOMIC);
  379. if (!fp)
  380. return NULL;
  381. FRAG_CB(fp)->next_frag = FRAG_CB(skb)->next_frag;
  382. if (RB_EMPTY_NODE(&skb->rbnode))
  383. FRAG_CB(parent)->next_frag = fp;
  384. else
  385. rb_replace_node(&skb->rbnode, &fp->rbnode,
  386. &q->rb_fragments);
  387. if (q->fragments_tail == skb)
  388. q->fragments_tail = fp;
  389. skb_morph(skb, head);
  390. FRAG_CB(skb)->next_frag = FRAG_CB(head)->next_frag;
  391. rb_replace_node(&head->rbnode, &skb->rbnode,
  392. &q->rb_fragments);
  393. consume_skb(head);
  394. head = skb;
  395. }
  396. WARN_ON(head->ip_defrag_offset != 0);
  397. delta = -head->truesize;
  398. /* Head of list must not be cloned. */
  399. if (skb_unclone(head, GFP_ATOMIC))
  400. return NULL;
  401. delta += head->truesize;
  402. if (delta)
  403. add_frag_mem_limit(q->fqdir, delta);
  404. /* If the first fragment is fragmented itself, we split
  405. * it to two chunks: the first with data and paged part
  406. * and the second, holding only fragments.
  407. */
  408. if (skb_has_frag_list(head)) {
  409. struct sk_buff *clone;
  410. int i, plen = 0;
  411. clone = alloc_skb(0, GFP_ATOMIC);
  412. if (!clone)
  413. return NULL;
  414. skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
  415. skb_frag_list_init(head);
  416. for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
  417. plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
  418. clone->data_len = head->data_len - plen;
  419. clone->len = clone->data_len;
  420. head->truesize += clone->truesize;
  421. clone->csum = 0;
  422. clone->ip_summed = head->ip_summed;
  423. add_frag_mem_limit(q->fqdir, clone->truesize);
  424. skb_shinfo(head)->frag_list = clone;
  425. nextp = &clone->next;
  426. } else {
  427. nextp = &skb_shinfo(head)->frag_list;
  428. }
  429. return nextp;
  430. }
  431. EXPORT_SYMBOL(inet_frag_reasm_prepare);
  432. void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
  433. void *reasm_data, bool try_coalesce)
  434. {
  435. struct sk_buff **nextp = reasm_data;
  436. struct rb_node *rbn;
  437. struct sk_buff *fp;
  438. int sum_truesize;
  439. skb_push(head, head->data - skb_network_header(head));
  440. /* Traverse the tree in order, to build frag_list. */
  441. fp = FRAG_CB(head)->next_frag;
  442. rbn = rb_next(&head->rbnode);
  443. rb_erase(&head->rbnode, &q->rb_fragments);
  444. sum_truesize = head->truesize;
  445. while (rbn || fp) {
  446. /* fp points to the next sk_buff in the current run;
  447. * rbn points to the next run.
  448. */
  449. /* Go through the current run. */
  450. while (fp) {
  451. struct sk_buff *next_frag = FRAG_CB(fp)->next_frag;
  452. bool stolen;
  453. int delta;
  454. sum_truesize += fp->truesize;
  455. if (head->ip_summed != fp->ip_summed)
  456. head->ip_summed = CHECKSUM_NONE;
  457. else if (head->ip_summed == CHECKSUM_COMPLETE)
  458. head->csum = csum_add(head->csum, fp->csum);
  459. if (try_coalesce && skb_try_coalesce(head, fp, &stolen,
  460. &delta)) {
  461. kfree_skb_partial(fp, stolen);
  462. } else {
  463. fp->prev = NULL;
  464. memset(&fp->rbnode, 0, sizeof(fp->rbnode));
  465. fp->sk = NULL;
  466. head->data_len += fp->len;
  467. head->len += fp->len;
  468. head->truesize += fp->truesize;
  469. *nextp = fp;
  470. nextp = &fp->next;
  471. }
  472. fp = next_frag;
  473. }
  474. /* Move to the next run. */
  475. if (rbn) {
  476. struct rb_node *rbnext = rb_next(rbn);
  477. fp = rb_to_skb(rbn);
  478. rb_erase(rbn, &q->rb_fragments);
  479. rbn = rbnext;
  480. }
  481. }
  482. sub_frag_mem_limit(q->fqdir, sum_truesize);
  483. *nextp = NULL;
  484. skb_mark_not_on_list(head);
  485. head->prev = NULL;
  486. head->tstamp = q->stamp;
  487. head->mono_delivery_time = q->mono_delivery_time;
  488. }
  489. EXPORT_SYMBOL(inet_frag_reasm_finish);
  490. struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q)
  491. {
  492. struct sk_buff *head, *skb;
  493. head = skb_rb_first(&q->rb_fragments);
  494. if (!head)
  495. return NULL;
  496. skb = FRAG_CB(head)->next_frag;
  497. if (skb)
  498. rb_replace_node(&head->rbnode, &skb->rbnode,
  499. &q->rb_fragments);
  500. else
  501. rb_erase(&head->rbnode, &q->rb_fragments);
  502. memset(&head->rbnode, 0, sizeof(head->rbnode));
  503. barrier();
  504. if (head == q->fragments_tail)
  505. q->fragments_tail = NULL;
  506. sub_frag_mem_limit(q->fqdir, head->truesize);
  507. return head;
  508. }
  509. EXPORT_SYMBOL(inet_frag_pull_head);