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