inet_fragment.c 17 KB

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