xref: /linux/net/ipv4/inet_fragment.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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 	q->rb_fragments = RB_ROOT;
330 	q->fragments_tail = NULL;
331 	q->last_run_head = NULL;
332 }
333 EXPORT_SYMBOL(inet_frag_queue_flush);
334 
335 void inet_frag_destroy(struct inet_frag_queue *q)
336 {
337 	unsigned int sum, sum_truesize = 0;
338 	enum skb_drop_reason reason;
339 	struct inet_frags *f;
340 	struct fqdir *fqdir;
341 
342 	WARN_ON(!(q->flags & INET_FRAG_COMPLETE));
343 	reason = (q->flags & INET_FRAG_DROP) ?
344 			SKB_DROP_REASON_FRAG_REASM_TIMEOUT :
345 			SKB_CONSUMED;
346 	WARN_ON(timer_delete(&q->timer) != 0);
347 
348 	/* Release all fragment data. */
349 	fqdir = q->fqdir;
350 	f = fqdir->f;
351 	sum_truesize = inet_frag_rbtree_purge(&q->rb_fragments, reason);
352 	sum = sum_truesize + f->qsize;
353 
354 	call_rcu(&q->rcu, inet_frag_destroy_rcu);
355 
356 	sub_frag_mem_limit(fqdir, sum);
357 }
358 EXPORT_SYMBOL(inet_frag_destroy);
359 
360 static struct inet_frag_queue *inet_frag_alloc(struct fqdir *fqdir,
361 					       struct inet_frags *f,
362 					       void *arg)
363 {
364 	struct inet_frag_queue *q;
365 
366 	q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
367 	if (!q)
368 		return NULL;
369 
370 	q->fqdir = fqdir;
371 	f->constructor(q, arg);
372 	add_frag_mem_limit(fqdir, f->qsize);
373 
374 	timer_setup(&q->timer, f->frag_expire, 0);
375 	spin_lock_init(&q->lock);
376 	/* One reference for the timer, one for the hash table.
377 	 * We never take any extra references, only decrement this field.
378 	 */
379 	refcount_set(&q->refcnt, 2);
380 
381 	return q;
382 }
383 
384 static struct inet_frag_queue *inet_frag_create(struct fqdir *fqdir,
385 						void *arg,
386 						struct inet_frag_queue **prev)
387 {
388 	struct inet_frags *f = fqdir->f;
389 	struct inet_frag_queue *q;
390 
391 	q = inet_frag_alloc(fqdir, f, arg);
392 	if (!q) {
393 		*prev = ERR_PTR(-ENOMEM);
394 		return NULL;
395 	}
396 
397 	spin_lock_bh(&q->lock);
398 	*prev = rhashtable_lookup_get_insert_key(&fqdir->rhashtable, &q->key,
399 						 &q->node, f->rhash_params);
400 	if (*prev) {
401 		/* We could not insert in the hash table,
402 		 * we need to cancel what inet_frag_alloc()
403 		 * anticipated.
404 		 */
405 		q->flags |= INET_FRAG_COMPLETE;
406 		spin_unlock_bh(&q->lock);
407 		inet_frag_putn(q, 2);
408 		return NULL;
409 	}
410 	mod_timer(&q->timer, jiffies + fqdir->timeout);
411 	spin_unlock_bh(&q->lock);
412 	return q;
413 }
414 
415 struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key)
416 {
417 	/* This pairs with WRITE_ONCE() in fqdir_pre_exit(). */
418 	long high_thresh = READ_ONCE(fqdir->high_thresh);
419 	struct inet_frag_queue *fq = NULL, *prev;
420 
421 	if (!high_thresh || frag_mem_limit(fqdir) > high_thresh)
422 		return NULL;
423 
424 	prev = rhashtable_lookup(&fqdir->rhashtable, key, fqdir->f->rhash_params);
425 	if (!prev)
426 		fq = inet_frag_create(fqdir, key, &prev);
427 	if (!IS_ERR_OR_NULL(prev))
428 		fq = prev;
429 	return fq;
430 }
431 EXPORT_SYMBOL(inet_frag_find);
432 
433 int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
434 			   int offset, int end)
435 {
436 	struct sk_buff *last = q->fragments_tail;
437 
438 	/* An IP fragment is never a GSO packet, but an untrusted source
439 	 * (virtio_net_hdr) may have attached GSO metadata to it. Do not let
440 	 * that reach the reassembled skb, whose head keeps the first
441 	 * fragment's shinfo and whose frag_list is not GRO-shaped.
442 	 */
443 	skb_gso_reset(skb);
444 
445 	/* RFC5722, Section 4, amended by Errata ID : 3089
446 	 *                          When reassembling an IPv6 datagram, if
447 	 *   one or more its constituent fragments is determined to be an
448 	 *   overlapping fragment, the entire datagram (and any constituent
449 	 *   fragments) MUST be silently discarded.
450 	 *
451 	 * Duplicates, however, should be ignored (i.e. skb dropped, but the
452 	 * queue/fragments kept for later reassembly).
453 	 */
454 	if (!last)
455 		fragrun_create(q, skb);  /* First fragment. */
456 	else if (FRAG_CB(last)->ip_defrag_offset + last->len < end) {
457 		/* This is the common case: skb goes to the end. */
458 		/* Detect and discard overlaps. */
459 		if (offset < FRAG_CB(last)->ip_defrag_offset + last->len)
460 			return IPFRAG_OVERLAP;
461 		if (offset == FRAG_CB(last)->ip_defrag_offset + last->len)
462 			fragrun_append_to_last(q, skb);
463 		else
464 			fragrun_create(q, skb);
465 	} else {
466 		/* Binary search. Note that skb can become the first fragment,
467 		 * but not the last (covered above).
468 		 */
469 		struct rb_node **rbn, *parent;
470 
471 		rbn = &q->rb_fragments.rb_node;
472 		do {
473 			struct sk_buff *curr;
474 			int curr_run_end;
475 
476 			parent = *rbn;
477 			curr = rb_to_skb(parent);
478 			curr_run_end = FRAG_CB(curr)->ip_defrag_offset +
479 					FRAG_CB(curr)->frag_run_len;
480 			if (end <= FRAG_CB(curr)->ip_defrag_offset)
481 				rbn = &parent->rb_left;
482 			else if (offset >= curr_run_end)
483 				rbn = &parent->rb_right;
484 			else if (offset >= FRAG_CB(curr)->ip_defrag_offset &&
485 				 end <= curr_run_end)
486 				return IPFRAG_DUP;
487 			else
488 				return IPFRAG_OVERLAP;
489 		} while (*rbn);
490 		/* Here we have parent properly set, and rbn pointing to
491 		 * one of its NULL left/right children. Insert skb.
492 		 */
493 		fragcb_clear(skb);
494 		rb_link_node(&skb->rbnode, parent, rbn);
495 		rb_insert_color(&skb->rbnode, &q->rb_fragments);
496 	}
497 
498 	FRAG_CB(skb)->ip_defrag_offset = offset;
499 	if (offset)
500 		nf_reset_ct(skb);
501 
502 	return IPFRAG_OK;
503 }
504 EXPORT_SYMBOL(inet_frag_queue_insert);
505 
506 void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
507 			      struct sk_buff *parent)
508 {
509 	struct sk_buff *fp, *head = skb_rb_first(&q->rb_fragments);
510 	void (*destructor)(struct sk_buff *);
511 	unsigned int orig_truesize = 0;
512 	struct sk_buff **nextp = NULL;
513 	struct sock *sk = skb->sk;
514 	int delta;
515 
516 	if (sk && is_skb_wmem(skb)) {
517 		/* TX: skb->sk might have been passed as argument to
518 		 * dst->output and must remain valid until tx completes.
519 		 *
520 		 * Move sk to reassembled skb and fix up wmem accounting.
521 		 */
522 		orig_truesize = skb->truesize;
523 		destructor = skb->destructor;
524 	}
525 
526 	if (head != skb) {
527 		fp = skb_clone(skb, GFP_ATOMIC);
528 		if (!fp) {
529 			head = skb;
530 			goto out_restore_sk;
531 		}
532 		if (RB_EMPTY_NODE(&skb->rbnode))
533 			FRAG_CB(parent)->next_frag = fp;
534 		else
535 			rb_replace_node(&skb->rbnode, &fp->rbnode,
536 					&q->rb_fragments);
537 		if (q->fragments_tail == skb)
538 			q->fragments_tail = fp;
539 
540 		if (orig_truesize) {
541 			/* prevent skb_morph from releasing sk */
542 			skb->sk = NULL;
543 			skb->destructor = NULL;
544 		}
545 		skb_morph(skb, head);
546 		rb_replace_node(&head->rbnode, &skb->rbnode,
547 				&q->rb_fragments);
548 		consume_skb(head);
549 		head = skb;
550 	}
551 	WARN_ON(FRAG_CB(head)->ip_defrag_offset != 0);
552 
553 	delta = -head->truesize;
554 
555 	/* Head of list must not be cloned. */
556 	if (skb_unclone(head, GFP_ATOMIC))
557 		goto out_restore_sk;
558 
559 	delta += head->truesize;
560 	if (delta)
561 		add_frag_mem_limit(q->fqdir, delta);
562 
563 	/* If the first fragment is fragmented itself, we split
564 	 * it to two chunks: the first with data and paged part
565 	 * and the second, holding only fragments.
566 	 */
567 	if (skb_has_frag_list(head)) {
568 		struct sk_buff *clone;
569 		int i, plen = 0;
570 
571 		clone = alloc_skb(0, GFP_ATOMIC);
572 		if (!clone)
573 			goto out_restore_sk;
574 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
575 		skb_frag_list_init(head);
576 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
577 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
578 		clone->data_len = head->data_len - plen;
579 		clone->len = clone->data_len;
580 		head->truesize += clone->truesize;
581 		clone->csum = 0;
582 		clone->ip_summed = head->ip_summed;
583 		add_frag_mem_limit(q->fqdir, clone->truesize);
584 		skb_shinfo(head)->frag_list = clone;
585 		nextp = &clone->next;
586 	} else {
587 		nextp = &skb_shinfo(head)->frag_list;
588 	}
589 
590 out_restore_sk:
591 	if (orig_truesize) {
592 		int ts_delta = head->truesize - orig_truesize;
593 
594 		/* if this reassembled skb is fragmented later,
595 		 * fraglist skbs will get skb->sk assigned from head->sk,
596 		 * and each frag skb will be released via sock_wfree.
597 		 *
598 		 * Update sk_wmem_alloc.
599 		 */
600 		head->sk = sk;
601 		head->destructor = destructor;
602 		refcount_add(ts_delta, &sk->sk_wmem_alloc);
603 	}
604 
605 	return nextp;
606 }
607 EXPORT_SYMBOL(inet_frag_reasm_prepare);
608 
609 void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
610 			    void *reasm_data, bool try_coalesce)
611 {
612 	struct sock *sk = is_skb_wmem(head) ? head->sk : NULL;
613 	const unsigned int head_truesize = head->truesize;
614 	struct sk_buff **nextp = reasm_data;
615 	struct rb_node *rbn;
616 	struct sk_buff *fp;
617 	int sum_truesize;
618 
619 	skb_push(head, head->data - skb_network_header(head));
620 
621 	/* Traverse the tree in order, to build frag_list. */
622 	fp = FRAG_CB(head)->next_frag;
623 	rbn = rb_next(&head->rbnode);
624 	rb_erase(&head->rbnode, &q->rb_fragments);
625 
626 	sum_truesize = head->truesize;
627 	while (rbn || fp) {
628 		/* fp points to the next sk_buff in the current run;
629 		 * rbn points to the next run.
630 		 */
631 		/* Go through the current run. */
632 		while (fp) {
633 			struct sk_buff *next_frag = FRAG_CB(fp)->next_frag;
634 			bool stolen;
635 			int delta;
636 
637 			sum_truesize += fp->truesize;
638 			if (head->ip_summed != fp->ip_summed)
639 				head->ip_summed = CHECKSUM_NONE;
640 			else if (head->ip_summed == CHECKSUM_COMPLETE)
641 				head->csum = csum_add(head->csum, fp->csum);
642 
643 			if (try_coalesce && skb_try_coalesce(head, fp, &stolen,
644 							     &delta)) {
645 				kfree_skb_partial(fp, stolen);
646 			} else {
647 				fp->prev = NULL;
648 				memset(&fp->rbnode, 0, sizeof(fp->rbnode));
649 				fp->sk = NULL;
650 
651 				head->data_len += fp->len;
652 				head->len += fp->len;
653 				head->truesize += fp->truesize;
654 
655 				*nextp = fp;
656 				nextp = &fp->next;
657 			}
658 
659 			fp = next_frag;
660 		}
661 		/* Move to the next run. */
662 		if (rbn) {
663 			struct rb_node *rbnext = rb_next(rbn);
664 
665 			fp = rb_to_skb(rbn);
666 			rb_erase(rbn, &q->rb_fragments);
667 			rbn = rbnext;
668 		}
669 	}
670 	sub_frag_mem_limit(q->fqdir, sum_truesize);
671 
672 	*nextp = NULL;
673 	skb_mark_not_on_list(head);
674 	head->prev = NULL;
675 	head->tstamp = q->stamp;
676 	head->tstamp_type = q->tstamp_type;
677 
678 	if (sk)
679 		refcount_add(sum_truesize - head_truesize, &sk->sk_wmem_alloc);
680 }
681 EXPORT_SYMBOL(inet_frag_reasm_finish);
682 
683 struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q)
684 {
685 	struct sk_buff *head, *skb;
686 
687 	head = skb_rb_first(&q->rb_fragments);
688 	if (!head)
689 		return NULL;
690 	skb = FRAG_CB(head)->next_frag;
691 	if (skb)
692 		rb_replace_node(&head->rbnode, &skb->rbnode,
693 				&q->rb_fragments);
694 	else
695 		rb_erase(&head->rbnode, &q->rb_fragments);
696 	memset(&head->rbnode, 0, sizeof(head->rbnode));
697 	barrier();
698 
699 	if (head == q->fragments_tail)
700 		q->fragments_tail = NULL;
701 
702 	sub_frag_mem_limit(q->fqdir, head->truesize);
703 
704 	return head;
705 }
706 EXPORT_SYMBOL(inet_frag_pull_head);
707