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