xref: /linux-6.15/net/core/skbuff.c (revision 8403bf04)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Routines having to do with the 'struct sk_buff' memory handlers.
4  *
5  *	Authors:	Alan Cox <[email protected]>
6  *			Florian La Roche <[email protected]>
7  *
8  *	Fixes:
9  *		Alan Cox	:	Fixed the worst of the load
10  *					balancer bugs.
11  *		Dave Platt	:	Interrupt stacking fix.
12  *	Richard Kooijman	:	Timestamp fixes.
13  *		Alan Cox	:	Changed buffer format.
14  *		Alan Cox	:	destructor hook for AF_UNIX etc.
15  *		Linus Torvalds	:	Better skb_clone.
16  *		Alan Cox	:	Added skb_copy.
17  *		Alan Cox	:	Added all the changed routines Linus
18  *					only put in the headers
19  *		Ray VanTassle	:	Fixed --skb->lock in free
20  *		Alan Cox	:	skb_copy copy arp field
21  *		Andi Kleen	:	slabified it.
22  *		Robert Olsson	:	Removed skb_head_pool
23  *
24  *	NOTE:
25  *		The __skb_ routines should be called with interrupts
26  *	disabled, or you better be *real* sure that the operation is atomic
27  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
28  *	or via disabling bottom half handlers, etc).
29  */
30 
31 /*
32  *	The functions in this file will not compile correctly with gcc 2.4.x
33  */
34 
35 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/in.h>
43 #include <linux/inet.h>
44 #include <linux/slab.h>
45 #include <linux/tcp.h>
46 #include <linux/udp.h>
47 #include <linux/sctp.h>
48 #include <linux/netdevice.h>
49 #ifdef CONFIG_NET_CLS_ACT
50 #include <net/pkt_sched.h>
51 #endif
52 #include <linux/string.h>
53 #include <linux/skbuff.h>
54 #include <linux/splice.h>
55 #include <linux/cache.h>
56 #include <linux/rtnetlink.h>
57 #include <linux/init.h>
58 #include <linux/scatterlist.h>
59 #include <linux/errqueue.h>
60 #include <linux/prefetch.h>
61 #include <linux/if_vlan.h>
62 #include <linux/mpls.h>
63 #include <linux/kcov.h>
64 
65 #include <net/protocol.h>
66 #include <net/dst.h>
67 #include <net/sock.h>
68 #include <net/checksum.h>
69 #include <net/ip6_checksum.h>
70 #include <net/xfrm.h>
71 #include <net/mpls.h>
72 #include <net/mptcp.h>
73 #include <net/mctp.h>
74 #include <net/page_pool.h>
75 
76 #include <linux/uaccess.h>
77 #include <trace/events/skb.h>
78 #include <linux/highmem.h>
79 #include <linux/capability.h>
80 #include <linux/user_namespace.h>
81 #include <linux/indirect_call_wrapper.h>
82 #include <linux/textsearch.h>
83 
84 #include "dev.h"
85 #include "sock_destructor.h"
86 
87 struct kmem_cache *skbuff_head_cache __ro_after_init;
88 static struct kmem_cache *skbuff_fclone_cache __ro_after_init;
89 #ifdef CONFIG_SKB_EXTENSIONS
90 static struct kmem_cache *skbuff_ext_cache __ro_after_init;
91 #endif
92 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
93 EXPORT_SYMBOL(sysctl_max_skb_frags);
94 
95 #undef FN
96 #define FN(reason) [SKB_DROP_REASON_##reason] = #reason,
97 const char * const drop_reasons[] = {
98 	[SKB_CONSUMED] = "CONSUMED",
99 	DEFINE_DROP_REASON(FN, FN)
100 };
101 EXPORT_SYMBOL(drop_reasons);
102 
103 /**
104  *	skb_panic - private function for out-of-line support
105  *	@skb:	buffer
106  *	@sz:	size
107  *	@addr:	address
108  *	@msg:	skb_over_panic or skb_under_panic
109  *
110  *	Out-of-line support for skb_put() and skb_push().
111  *	Called via the wrapper skb_over_panic() or skb_under_panic().
112  *	Keep out of line to prevent kernel bloat.
113  *	__builtin_return_address is not used because it is not always reliable.
114  */
115 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
116 		      const char msg[])
117 {
118 	pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n",
119 		 msg, addr, skb->len, sz, skb->head, skb->data,
120 		 (unsigned long)skb->tail, (unsigned long)skb->end,
121 		 skb->dev ? skb->dev->name : "<NULL>");
122 	BUG();
123 }
124 
125 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
126 {
127 	skb_panic(skb, sz, addr, __func__);
128 }
129 
130 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
131 {
132 	skb_panic(skb, sz, addr, __func__);
133 }
134 
135 #define NAPI_SKB_CACHE_SIZE	64
136 #define NAPI_SKB_CACHE_BULK	16
137 #define NAPI_SKB_CACHE_HALF	(NAPI_SKB_CACHE_SIZE / 2)
138 
139 #if PAGE_SIZE == SZ_4K
140 
141 #define NAPI_HAS_SMALL_PAGE_FRAG	1
142 #define NAPI_SMALL_PAGE_PFMEMALLOC(nc)	((nc).pfmemalloc)
143 
144 /* specialized page frag allocator using a single order 0 page
145  * and slicing it into 1K sized fragment. Constrained to systems
146  * with a very limited amount of 1K fragments fitting a single
147  * page - to avoid excessive truesize underestimation
148  */
149 
150 struct page_frag_1k {
151 	void *va;
152 	u16 offset;
153 	bool pfmemalloc;
154 };
155 
156 static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp)
157 {
158 	struct page *page;
159 	int offset;
160 
161 	offset = nc->offset - SZ_1K;
162 	if (likely(offset >= 0))
163 		goto use_frag;
164 
165 	page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
166 	if (!page)
167 		return NULL;
168 
169 	nc->va = page_address(page);
170 	nc->pfmemalloc = page_is_pfmemalloc(page);
171 	offset = PAGE_SIZE - SZ_1K;
172 	page_ref_add(page, offset / SZ_1K);
173 
174 use_frag:
175 	nc->offset = offset;
176 	return nc->va + offset;
177 }
178 #else
179 
180 /* the small page is actually unused in this build; add dummy helpers
181  * to please the compiler and avoid later preprocessor's conditionals
182  */
183 #define NAPI_HAS_SMALL_PAGE_FRAG	0
184 #define NAPI_SMALL_PAGE_PFMEMALLOC(nc)	false
185 
186 struct page_frag_1k {
187 };
188 
189 static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask)
190 {
191 	return NULL;
192 }
193 
194 #endif
195 
196 struct napi_alloc_cache {
197 	struct page_frag_cache page;
198 	struct page_frag_1k page_small;
199 	unsigned int skb_count;
200 	void *skb_cache[NAPI_SKB_CACHE_SIZE];
201 };
202 
203 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
204 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
205 
206 /* Double check that napi_get_frags() allocates skbs with
207  * skb->head being backed by slab, not a page fragment.
208  * This is to make sure bug fixed in 3226b158e67c
209  * ("net: avoid 32 x truesize under-estimation for tiny skbs")
210  * does not accidentally come back.
211  */
212 void napi_get_frags_check(struct napi_struct *napi)
213 {
214 	struct sk_buff *skb;
215 
216 	local_bh_disable();
217 	skb = napi_get_frags(napi);
218 	WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag);
219 	napi_free_frags(napi);
220 	local_bh_enable();
221 }
222 
223 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
224 {
225 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
226 
227 	fragsz = SKB_DATA_ALIGN(fragsz);
228 
229 	return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
230 }
231 EXPORT_SYMBOL(__napi_alloc_frag_align);
232 
233 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask)
234 {
235 	void *data;
236 
237 	fragsz = SKB_DATA_ALIGN(fragsz);
238 	if (in_hardirq() || irqs_disabled()) {
239 		struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache);
240 
241 		data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask);
242 	} else {
243 		struct napi_alloc_cache *nc;
244 
245 		local_bh_disable();
246 		nc = this_cpu_ptr(&napi_alloc_cache);
247 		data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask);
248 		local_bh_enable();
249 	}
250 	return data;
251 }
252 EXPORT_SYMBOL(__netdev_alloc_frag_align);
253 
254 static struct sk_buff *napi_skb_cache_get(void)
255 {
256 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
257 	struct sk_buff *skb;
258 
259 	if (unlikely(!nc->skb_count)) {
260 		nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache,
261 						      GFP_ATOMIC,
262 						      NAPI_SKB_CACHE_BULK,
263 						      nc->skb_cache);
264 		if (unlikely(!nc->skb_count))
265 			return NULL;
266 	}
267 
268 	skb = nc->skb_cache[--nc->skb_count];
269 	kasan_unpoison_object_data(skbuff_head_cache, skb);
270 
271 	return skb;
272 }
273 
274 static inline void __finalize_skb_around(struct sk_buff *skb, void *data,
275 					 unsigned int size)
276 {
277 	struct skb_shared_info *shinfo;
278 
279 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
280 
281 	/* Assumes caller memset cleared SKB */
282 	skb->truesize = SKB_TRUESIZE(size);
283 	refcount_set(&skb->users, 1);
284 	skb->head = data;
285 	skb->data = data;
286 	skb_reset_tail_pointer(skb);
287 	skb_set_end_offset(skb, size);
288 	skb->mac_header = (typeof(skb->mac_header))~0U;
289 	skb->transport_header = (typeof(skb->transport_header))~0U;
290 	skb->alloc_cpu = raw_smp_processor_id();
291 	/* make sure we initialize shinfo sequentially */
292 	shinfo = skb_shinfo(skb);
293 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
294 	atomic_set(&shinfo->dataref, 1);
295 
296 	skb_set_kcov_handle(skb, kcov_common_handle());
297 }
298 
299 static inline void *__slab_build_skb(struct sk_buff *skb, void *data,
300 				     unsigned int *size)
301 {
302 	void *resized;
303 
304 	/* Must find the allocation size (and grow it to match). */
305 	*size = ksize(data);
306 	/* krealloc() will immediately return "data" when
307 	 * "ksize(data)" is requested: it is the existing upper
308 	 * bounds. As a result, GFP_ATOMIC will be ignored. Note
309 	 * that this "new" pointer needs to be passed back to the
310 	 * caller for use so the __alloc_size hinting will be
311 	 * tracked correctly.
312 	 */
313 	resized = krealloc(data, *size, GFP_ATOMIC);
314 	WARN_ON_ONCE(resized != data);
315 	return resized;
316 }
317 
318 /* build_skb() variant which can operate on slab buffers.
319  * Note that this should be used sparingly as slab buffers
320  * cannot be combined efficiently by GRO!
321  */
322 struct sk_buff *slab_build_skb(void *data)
323 {
324 	struct sk_buff *skb;
325 	unsigned int size;
326 
327 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
328 	if (unlikely(!skb))
329 		return NULL;
330 
331 	memset(skb, 0, offsetof(struct sk_buff, tail));
332 	data = __slab_build_skb(skb, data, &size);
333 	__finalize_skb_around(skb, data, size);
334 
335 	return skb;
336 }
337 EXPORT_SYMBOL(slab_build_skb);
338 
339 /* Caller must provide SKB that is memset cleared */
340 static void __build_skb_around(struct sk_buff *skb, void *data,
341 			       unsigned int frag_size)
342 {
343 	unsigned int size = frag_size;
344 
345 	/* frag_size == 0 is considered deprecated now. Callers
346 	 * using slab buffer should use slab_build_skb() instead.
347 	 */
348 	if (WARN_ONCE(size == 0, "Use slab_build_skb() instead"))
349 		data = __slab_build_skb(skb, data, &size);
350 
351 	__finalize_skb_around(skb, data, size);
352 }
353 
354 /**
355  * __build_skb - build a network buffer
356  * @data: data buffer provided by caller
357  * @frag_size: size of data (must not be 0)
358  *
359  * Allocate a new &sk_buff. Caller provides space holding head and
360  * skb_shared_info. @data must have been allocated from the page
361  * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc()
362  * allocation is deprecated, and callers should use slab_build_skb()
363  * instead.)
364  * The return is the new skb buffer.
365  * On a failure the return is %NULL, and @data is not freed.
366  * Notes :
367  *  Before IO, driver allocates only data buffer where NIC put incoming frame
368  *  Driver should add room at head (NET_SKB_PAD) and
369  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
370  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
371  *  before giving packet to stack.
372  *  RX rings only contains data buffers, not full skbs.
373  */
374 struct sk_buff *__build_skb(void *data, unsigned int frag_size)
375 {
376 	struct sk_buff *skb;
377 
378 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
379 	if (unlikely(!skb))
380 		return NULL;
381 
382 	memset(skb, 0, offsetof(struct sk_buff, tail));
383 	__build_skb_around(skb, data, frag_size);
384 
385 	return skb;
386 }
387 
388 /* build_skb() is wrapper over __build_skb(), that specifically
389  * takes care of skb->head and skb->pfmemalloc
390  */
391 struct sk_buff *build_skb(void *data, unsigned int frag_size)
392 {
393 	struct sk_buff *skb = __build_skb(data, frag_size);
394 
395 	if (skb && frag_size) {
396 		skb->head_frag = 1;
397 		if (page_is_pfmemalloc(virt_to_head_page(data)))
398 			skb->pfmemalloc = 1;
399 	}
400 	return skb;
401 }
402 EXPORT_SYMBOL(build_skb);
403 
404 /**
405  * build_skb_around - build a network buffer around provided skb
406  * @skb: sk_buff provide by caller, must be memset cleared
407  * @data: data buffer provided by caller
408  * @frag_size: size of data
409  */
410 struct sk_buff *build_skb_around(struct sk_buff *skb,
411 				 void *data, unsigned int frag_size)
412 {
413 	if (unlikely(!skb))
414 		return NULL;
415 
416 	__build_skb_around(skb, data, frag_size);
417 
418 	if (frag_size) {
419 		skb->head_frag = 1;
420 		if (page_is_pfmemalloc(virt_to_head_page(data)))
421 			skb->pfmemalloc = 1;
422 	}
423 	return skb;
424 }
425 EXPORT_SYMBOL(build_skb_around);
426 
427 /**
428  * __napi_build_skb - build a network buffer
429  * @data: data buffer provided by caller
430  * @frag_size: size of data
431  *
432  * Version of __build_skb() that uses NAPI percpu caches to obtain
433  * skbuff_head instead of inplace allocation.
434  *
435  * Returns a new &sk_buff on success, %NULL on allocation failure.
436  */
437 static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size)
438 {
439 	struct sk_buff *skb;
440 
441 	skb = napi_skb_cache_get();
442 	if (unlikely(!skb))
443 		return NULL;
444 
445 	memset(skb, 0, offsetof(struct sk_buff, tail));
446 	__build_skb_around(skb, data, frag_size);
447 
448 	return skb;
449 }
450 
451 /**
452  * napi_build_skb - build a network buffer
453  * @data: data buffer provided by caller
454  * @frag_size: size of data
455  *
456  * Version of __napi_build_skb() that takes care of skb->head_frag
457  * and skb->pfmemalloc when the data is a page or page fragment.
458  *
459  * Returns a new &sk_buff on success, %NULL on allocation failure.
460  */
461 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size)
462 {
463 	struct sk_buff *skb = __napi_build_skb(data, frag_size);
464 
465 	if (likely(skb) && frag_size) {
466 		skb->head_frag = 1;
467 		skb_propagate_pfmemalloc(virt_to_head_page(data), skb);
468 	}
469 
470 	return skb;
471 }
472 EXPORT_SYMBOL(napi_build_skb);
473 
474 /*
475  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
476  * the caller if emergency pfmemalloc reserves are being used. If it is and
477  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
478  * may be used. Otherwise, the packet data may be discarded until enough
479  * memory is free
480  */
481 static void *kmalloc_reserve(size_t size, gfp_t flags, int node,
482 			     bool *pfmemalloc)
483 {
484 	void *obj;
485 	bool ret_pfmemalloc = false;
486 
487 	/*
488 	 * Try a regular allocation, when that fails and we're not entitled
489 	 * to the reserves, fail.
490 	 */
491 	obj = kmalloc_node_track_caller(size,
492 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
493 					node);
494 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
495 		goto out;
496 
497 	/* Try again but now we are using pfmemalloc reserves */
498 	ret_pfmemalloc = true;
499 	obj = kmalloc_node_track_caller(size, flags, node);
500 
501 out:
502 	if (pfmemalloc)
503 		*pfmemalloc = ret_pfmemalloc;
504 
505 	return obj;
506 }
507 
508 /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
509  *	'private' fields and also do memory statistics to find all the
510  *	[BEEP] leaks.
511  *
512  */
513 
514 /**
515  *	__alloc_skb	-	allocate a network buffer
516  *	@size: size to allocate
517  *	@gfp_mask: allocation mask
518  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
519  *		instead of head cache and allocate a cloned (child) skb.
520  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
521  *		allocations in case the data is required for writeback
522  *	@node: numa node to allocate memory on
523  *
524  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
525  *	tail room of at least size bytes. The object has a reference count
526  *	of one. The return is the buffer. On a failure the return is %NULL.
527  *
528  *	Buffers may only be allocated from interrupts using a @gfp_mask of
529  *	%GFP_ATOMIC.
530  */
531 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
532 			    int flags, int node)
533 {
534 	struct kmem_cache *cache;
535 	struct sk_buff *skb;
536 	unsigned int osize;
537 	bool pfmemalloc;
538 	u8 *data;
539 
540 	cache = (flags & SKB_ALLOC_FCLONE)
541 		? skbuff_fclone_cache : skbuff_head_cache;
542 
543 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
544 		gfp_mask |= __GFP_MEMALLOC;
545 
546 	/* Get the HEAD */
547 	if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI &&
548 	    likely(node == NUMA_NO_NODE || node == numa_mem_id()))
549 		skb = napi_skb_cache_get();
550 	else
551 		skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node);
552 	if (unlikely(!skb))
553 		return NULL;
554 	prefetchw(skb);
555 
556 	/* We do our best to align skb_shared_info on a separate cache
557 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
558 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
559 	 * Both skb->head and skb_shared_info are cache line aligned.
560 	 */
561 	size = SKB_DATA_ALIGN(size);
562 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
563 	osize = kmalloc_size_roundup(size);
564 	data = kmalloc_reserve(osize, gfp_mask, node, &pfmemalloc);
565 	if (unlikely(!data))
566 		goto nodata;
567 	/* kmalloc_size_roundup() might give us more room than requested.
568 	 * Put skb_shared_info exactly at the end of allocated zone,
569 	 * to allow max possible filling before reallocation.
570 	 */
571 	size = SKB_WITH_OVERHEAD(osize);
572 	prefetchw(data + size);
573 
574 	/*
575 	 * Only clear those fields we need to clear, not those that we will
576 	 * actually initialise below. Hence, don't put any more fields after
577 	 * the tail pointer in struct sk_buff!
578 	 */
579 	memset(skb, 0, offsetof(struct sk_buff, tail));
580 	__build_skb_around(skb, data, osize);
581 	skb->pfmemalloc = pfmemalloc;
582 
583 	if (flags & SKB_ALLOC_FCLONE) {
584 		struct sk_buff_fclones *fclones;
585 
586 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
587 
588 		skb->fclone = SKB_FCLONE_ORIG;
589 		refcount_set(&fclones->fclone_ref, 1);
590 	}
591 
592 	return skb;
593 
594 nodata:
595 	kmem_cache_free(cache, skb);
596 	return NULL;
597 }
598 EXPORT_SYMBOL(__alloc_skb);
599 
600 /**
601  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
602  *	@dev: network device to receive on
603  *	@len: length to allocate
604  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
605  *
606  *	Allocate a new &sk_buff and assign it a usage count of one. The
607  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
608  *	the headroom they think they need without accounting for the
609  *	built in space. The built in space is used for optimisations.
610  *
611  *	%NULL is returned if there is no free memory.
612  */
613 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
614 				   gfp_t gfp_mask)
615 {
616 	struct page_frag_cache *nc;
617 	struct sk_buff *skb;
618 	bool pfmemalloc;
619 	void *data;
620 
621 	len += NET_SKB_PAD;
622 
623 	/* If requested length is either too small or too big,
624 	 * we use kmalloc() for skb->head allocation.
625 	 */
626 	if (len <= SKB_WITH_OVERHEAD(1024) ||
627 	    len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
628 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
629 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
630 		if (!skb)
631 			goto skb_fail;
632 		goto skb_success;
633 	}
634 
635 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
636 	len = SKB_DATA_ALIGN(len);
637 
638 	if (sk_memalloc_socks())
639 		gfp_mask |= __GFP_MEMALLOC;
640 
641 	if (in_hardirq() || irqs_disabled()) {
642 		nc = this_cpu_ptr(&netdev_alloc_cache);
643 		data = page_frag_alloc(nc, len, gfp_mask);
644 		pfmemalloc = nc->pfmemalloc;
645 	} else {
646 		local_bh_disable();
647 		nc = this_cpu_ptr(&napi_alloc_cache.page);
648 		data = page_frag_alloc(nc, len, gfp_mask);
649 		pfmemalloc = nc->pfmemalloc;
650 		local_bh_enable();
651 	}
652 
653 	if (unlikely(!data))
654 		return NULL;
655 
656 	skb = __build_skb(data, len);
657 	if (unlikely(!skb)) {
658 		skb_free_frag(data);
659 		return NULL;
660 	}
661 
662 	if (pfmemalloc)
663 		skb->pfmemalloc = 1;
664 	skb->head_frag = 1;
665 
666 skb_success:
667 	skb_reserve(skb, NET_SKB_PAD);
668 	skb->dev = dev;
669 
670 skb_fail:
671 	return skb;
672 }
673 EXPORT_SYMBOL(__netdev_alloc_skb);
674 
675 /**
676  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
677  *	@napi: napi instance this buffer was allocated for
678  *	@len: length to allocate
679  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
680  *
681  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
682  *	attempt to allocate the head from a special reserved region used
683  *	only for NAPI Rx allocation.  By doing this we can save several
684  *	CPU cycles by avoiding having to disable and re-enable IRQs.
685  *
686  *	%NULL is returned if there is no free memory.
687  */
688 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
689 				 gfp_t gfp_mask)
690 {
691 	struct napi_alloc_cache *nc;
692 	struct sk_buff *skb;
693 	bool pfmemalloc;
694 	void *data;
695 
696 	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
697 	len += NET_SKB_PAD + NET_IP_ALIGN;
698 
699 	/* If requested length is either too small or too big,
700 	 * we use kmalloc() for skb->head allocation.
701 	 * When the small frag allocator is available, prefer it over kmalloc
702 	 * for small fragments
703 	 */
704 	if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) ||
705 	    len > SKB_WITH_OVERHEAD(PAGE_SIZE) ||
706 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
707 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI,
708 				  NUMA_NO_NODE);
709 		if (!skb)
710 			goto skb_fail;
711 		goto skb_success;
712 	}
713 
714 	nc = this_cpu_ptr(&napi_alloc_cache);
715 
716 	if (sk_memalloc_socks())
717 		gfp_mask |= __GFP_MEMALLOC;
718 
719 	if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) {
720 		/* we are artificially inflating the allocation size, but
721 		 * that is not as bad as it may look like, as:
722 		 * - 'len' less than GRO_MAX_HEAD makes little sense
723 		 * - On most systems, larger 'len' values lead to fragment
724 		 *   size above 512 bytes
725 		 * - kmalloc would use the kmalloc-1k slab for such values
726 		 * - Builds with smaller GRO_MAX_HEAD will very likely do
727 		 *   little networking, as that implies no WiFi and no
728 		 *   tunnels support, and 32 bits arches.
729 		 */
730 		len = SZ_1K;
731 
732 		data = page_frag_alloc_1k(&nc->page_small, gfp_mask);
733 		pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small);
734 	} else {
735 		len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
736 		len = SKB_DATA_ALIGN(len);
737 
738 		data = page_frag_alloc(&nc->page, len, gfp_mask);
739 		pfmemalloc = nc->page.pfmemalloc;
740 	}
741 
742 	if (unlikely(!data))
743 		return NULL;
744 
745 	skb = __napi_build_skb(data, len);
746 	if (unlikely(!skb)) {
747 		skb_free_frag(data);
748 		return NULL;
749 	}
750 
751 	if (pfmemalloc)
752 		skb->pfmemalloc = 1;
753 	skb->head_frag = 1;
754 
755 skb_success:
756 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
757 	skb->dev = napi->dev;
758 
759 skb_fail:
760 	return skb;
761 }
762 EXPORT_SYMBOL(__napi_alloc_skb);
763 
764 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
765 		     int size, unsigned int truesize)
766 {
767 	skb_fill_page_desc(skb, i, page, off, size);
768 	skb->len += size;
769 	skb->data_len += size;
770 	skb->truesize += truesize;
771 }
772 EXPORT_SYMBOL(skb_add_rx_frag);
773 
774 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
775 			  unsigned int truesize)
776 {
777 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
778 
779 	skb_frag_size_add(frag, size);
780 	skb->len += size;
781 	skb->data_len += size;
782 	skb->truesize += truesize;
783 }
784 EXPORT_SYMBOL(skb_coalesce_rx_frag);
785 
786 static void skb_drop_list(struct sk_buff **listp)
787 {
788 	kfree_skb_list(*listp);
789 	*listp = NULL;
790 }
791 
792 static inline void skb_drop_fraglist(struct sk_buff *skb)
793 {
794 	skb_drop_list(&skb_shinfo(skb)->frag_list);
795 }
796 
797 static void skb_clone_fraglist(struct sk_buff *skb)
798 {
799 	struct sk_buff *list;
800 
801 	skb_walk_frags(skb, list)
802 		skb_get(list);
803 }
804 
805 static bool skb_pp_recycle(struct sk_buff *skb, void *data)
806 {
807 	if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
808 		return false;
809 	return page_pool_return_skb_page(virt_to_page(data));
810 }
811 
812 static void skb_free_head(struct sk_buff *skb)
813 {
814 	unsigned char *head = skb->head;
815 
816 	if (skb->head_frag) {
817 		if (skb_pp_recycle(skb, head))
818 			return;
819 		skb_free_frag(head);
820 	} else {
821 		kfree(head);
822 	}
823 }
824 
825 static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason)
826 {
827 	struct skb_shared_info *shinfo = skb_shinfo(skb);
828 	int i;
829 
830 	if (skb->cloned &&
831 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
832 			      &shinfo->dataref))
833 		goto exit;
834 
835 	if (skb_zcopy(skb)) {
836 		bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS;
837 
838 		skb_zcopy_clear(skb, true);
839 		if (skip_unref)
840 			goto free_head;
841 	}
842 
843 	for (i = 0; i < shinfo->nr_frags; i++)
844 		__skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
845 
846 free_head:
847 	if (shinfo->frag_list)
848 		kfree_skb_list_reason(shinfo->frag_list, reason);
849 
850 	skb_free_head(skb);
851 exit:
852 	/* When we clone an SKB we copy the reycling bit. The pp_recycle
853 	 * bit is only set on the head though, so in order to avoid races
854 	 * while trying to recycle fragments on __skb_frag_unref() we need
855 	 * to make one SKB responsible for triggering the recycle path.
856 	 * So disable the recycling bit if an SKB is cloned and we have
857 	 * additional references to the fragmented part of the SKB.
858 	 * Eventually the last SKB will have the recycling bit set and it's
859 	 * dataref set to 0, which will trigger the recycling
860 	 */
861 	skb->pp_recycle = 0;
862 }
863 
864 /*
865  *	Free an skbuff by memory without cleaning the state.
866  */
867 static void kfree_skbmem(struct sk_buff *skb)
868 {
869 	struct sk_buff_fclones *fclones;
870 
871 	switch (skb->fclone) {
872 	case SKB_FCLONE_UNAVAILABLE:
873 		kmem_cache_free(skbuff_head_cache, skb);
874 		return;
875 
876 	case SKB_FCLONE_ORIG:
877 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
878 
879 		/* We usually free the clone (TX completion) before original skb
880 		 * This test would have no chance to be true for the clone,
881 		 * while here, branch prediction will be good.
882 		 */
883 		if (refcount_read(&fclones->fclone_ref) == 1)
884 			goto fastpath;
885 		break;
886 
887 	default: /* SKB_FCLONE_CLONE */
888 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
889 		break;
890 	}
891 	if (!refcount_dec_and_test(&fclones->fclone_ref))
892 		return;
893 fastpath:
894 	kmem_cache_free(skbuff_fclone_cache, fclones);
895 }
896 
897 void skb_release_head_state(struct sk_buff *skb)
898 {
899 	skb_dst_drop(skb);
900 	if (skb->destructor) {
901 		DEBUG_NET_WARN_ON_ONCE(in_hardirq());
902 		skb->destructor(skb);
903 	}
904 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
905 	nf_conntrack_put(skb_nfct(skb));
906 #endif
907 	skb_ext_put(skb);
908 }
909 
910 /* Free everything but the sk_buff shell. */
911 static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason)
912 {
913 	skb_release_head_state(skb);
914 	if (likely(skb->head))
915 		skb_release_data(skb, reason);
916 }
917 
918 /**
919  *	__kfree_skb - private function
920  *	@skb: buffer
921  *
922  *	Free an sk_buff. Release anything attached to the buffer.
923  *	Clean the state. This is an internal helper function. Users should
924  *	always call kfree_skb
925  */
926 
927 void __kfree_skb(struct sk_buff *skb)
928 {
929 	skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED);
930 	kfree_skbmem(skb);
931 }
932 EXPORT_SYMBOL(__kfree_skb);
933 
934 static __always_inline
935 bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
936 {
937 	if (unlikely(!skb_unref(skb)))
938 		return false;
939 
940 	DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX);
941 
942 	if (reason == SKB_CONSUMED)
943 		trace_consume_skb(skb);
944 	else
945 		trace_kfree_skb(skb, __builtin_return_address(0), reason);
946 	return true;
947 }
948 
949 /**
950  *	kfree_skb_reason - free an sk_buff with special reason
951  *	@skb: buffer to free
952  *	@reason: reason why this skb is dropped
953  *
954  *	Drop a reference to the buffer and free it if the usage count has
955  *	hit zero. Meanwhile, pass the drop reason to 'kfree_skb'
956  *	tracepoint.
957  */
958 void __fix_address
959 kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
960 {
961 	if (__kfree_skb_reason(skb, reason))
962 		__kfree_skb(skb);
963 }
964 EXPORT_SYMBOL(kfree_skb_reason);
965 
966 #define KFREE_SKB_BULK_SIZE	16
967 
968 struct skb_free_array {
969 	unsigned int skb_count;
970 	void *skb_array[KFREE_SKB_BULK_SIZE];
971 };
972 
973 static void kfree_skb_add_bulk(struct sk_buff *skb,
974 			       struct skb_free_array *sa,
975 			       enum skb_drop_reason reason)
976 {
977 	/* if SKB is a clone, don't handle this case */
978 	if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) {
979 		__kfree_skb(skb);
980 		return;
981 	}
982 
983 	skb_release_all(skb, reason);
984 	sa->skb_array[sa->skb_count++] = skb;
985 
986 	if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) {
987 		kmem_cache_free_bulk(skbuff_head_cache, KFREE_SKB_BULK_SIZE,
988 				     sa->skb_array);
989 		sa->skb_count = 0;
990 	}
991 }
992 
993 void __fix_address
994 kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason)
995 {
996 	struct skb_free_array sa;
997 
998 	sa.skb_count = 0;
999 
1000 	while (segs) {
1001 		struct sk_buff *next = segs->next;
1002 
1003 		if (__kfree_skb_reason(segs, reason))
1004 			kfree_skb_add_bulk(segs, &sa, reason);
1005 
1006 		segs = next;
1007 	}
1008 
1009 	if (sa.skb_count)
1010 		kmem_cache_free_bulk(skbuff_head_cache, sa.skb_count,
1011 				     sa.skb_array);
1012 }
1013 EXPORT_SYMBOL(kfree_skb_list_reason);
1014 
1015 /* Dump skb information and contents.
1016  *
1017  * Must only be called from net_ratelimit()-ed paths.
1018  *
1019  * Dumps whole packets if full_pkt, only headers otherwise.
1020  */
1021 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt)
1022 {
1023 	struct skb_shared_info *sh = skb_shinfo(skb);
1024 	struct net_device *dev = skb->dev;
1025 	struct sock *sk = skb->sk;
1026 	struct sk_buff *list_skb;
1027 	bool has_mac, has_trans;
1028 	int headroom, tailroom;
1029 	int i, len, seg_len;
1030 
1031 	if (full_pkt)
1032 		len = skb->len;
1033 	else
1034 		len = min_t(int, skb->len, MAX_HEADER + 128);
1035 
1036 	headroom = skb_headroom(skb);
1037 	tailroom = skb_tailroom(skb);
1038 
1039 	has_mac = skb_mac_header_was_set(skb);
1040 	has_trans = skb_transport_header_was_set(skb);
1041 
1042 	printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n"
1043 	       "mac=(%d,%d) net=(%d,%d) trans=%d\n"
1044 	       "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n"
1045 	       "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n"
1046 	       "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n",
1047 	       level, skb->len, headroom, skb_headlen(skb), tailroom,
1048 	       has_mac ? skb->mac_header : -1,
1049 	       has_mac ? skb_mac_header_len(skb) : -1,
1050 	       skb->network_header,
1051 	       has_trans ? skb_network_header_len(skb) : -1,
1052 	       has_trans ? skb->transport_header : -1,
1053 	       sh->tx_flags, sh->nr_frags,
1054 	       sh->gso_size, sh->gso_type, sh->gso_segs,
1055 	       skb->csum, skb->ip_summed, skb->csum_complete_sw,
1056 	       skb->csum_valid, skb->csum_level,
1057 	       skb->hash, skb->sw_hash, skb->l4_hash,
1058 	       ntohs(skb->protocol), skb->pkt_type, skb->skb_iif);
1059 
1060 	if (dev)
1061 		printk("%sdev name=%s feat=%pNF\n",
1062 		       level, dev->name, &dev->features);
1063 	if (sk)
1064 		printk("%ssk family=%hu type=%u proto=%u\n",
1065 		       level, sk->sk_family, sk->sk_type, sk->sk_protocol);
1066 
1067 	if (full_pkt && headroom)
1068 		print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET,
1069 			       16, 1, skb->head, headroom, false);
1070 
1071 	seg_len = min_t(int, skb_headlen(skb), len);
1072 	if (seg_len)
1073 		print_hex_dump(level, "skb linear:   ", DUMP_PREFIX_OFFSET,
1074 			       16, 1, skb->data, seg_len, false);
1075 	len -= seg_len;
1076 
1077 	if (full_pkt && tailroom)
1078 		print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET,
1079 			       16, 1, skb_tail_pointer(skb), tailroom, false);
1080 
1081 	for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) {
1082 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1083 		u32 p_off, p_len, copied;
1084 		struct page *p;
1085 		u8 *vaddr;
1086 
1087 		skb_frag_foreach_page(frag, skb_frag_off(frag),
1088 				      skb_frag_size(frag), p, p_off, p_len,
1089 				      copied) {
1090 			seg_len = min_t(int, p_len, len);
1091 			vaddr = kmap_atomic(p);
1092 			print_hex_dump(level, "skb frag:     ",
1093 				       DUMP_PREFIX_OFFSET,
1094 				       16, 1, vaddr + p_off, seg_len, false);
1095 			kunmap_atomic(vaddr);
1096 			len -= seg_len;
1097 			if (!len)
1098 				break;
1099 		}
1100 	}
1101 
1102 	if (full_pkt && skb_has_frag_list(skb)) {
1103 		printk("skb fraglist:\n");
1104 		skb_walk_frags(skb, list_skb)
1105 			skb_dump(level, list_skb, true);
1106 	}
1107 }
1108 EXPORT_SYMBOL(skb_dump);
1109 
1110 /**
1111  *	skb_tx_error - report an sk_buff xmit error
1112  *	@skb: buffer that triggered an error
1113  *
1114  *	Report xmit error if a device callback is tracking this skb.
1115  *	skb must be freed afterwards.
1116  */
1117 void skb_tx_error(struct sk_buff *skb)
1118 {
1119 	if (skb) {
1120 		skb_zcopy_downgrade_managed(skb);
1121 		skb_zcopy_clear(skb, true);
1122 	}
1123 }
1124 EXPORT_SYMBOL(skb_tx_error);
1125 
1126 #ifdef CONFIG_TRACEPOINTS
1127 /**
1128  *	consume_skb - free an skbuff
1129  *	@skb: buffer to free
1130  *
1131  *	Drop a ref to the buffer and free it if the usage count has hit zero
1132  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
1133  *	is being dropped after a failure and notes that
1134  */
1135 void consume_skb(struct sk_buff *skb)
1136 {
1137 	if (!skb_unref(skb))
1138 		return;
1139 
1140 	trace_consume_skb(skb);
1141 	__kfree_skb(skb);
1142 }
1143 EXPORT_SYMBOL(consume_skb);
1144 #endif
1145 
1146 /**
1147  *	__consume_stateless_skb - free an skbuff, assuming it is stateless
1148  *	@skb: buffer to free
1149  *
1150  *	Alike consume_skb(), but this variant assumes that this is the last
1151  *	skb reference and all the head states have been already dropped
1152  */
1153 void __consume_stateless_skb(struct sk_buff *skb)
1154 {
1155 	trace_consume_skb(skb);
1156 	skb_release_data(skb, SKB_CONSUMED);
1157 	kfree_skbmem(skb);
1158 }
1159 
1160 static void napi_skb_cache_put(struct sk_buff *skb)
1161 {
1162 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
1163 	u32 i;
1164 
1165 	kasan_poison_object_data(skbuff_head_cache, skb);
1166 	nc->skb_cache[nc->skb_count++] = skb;
1167 
1168 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
1169 		for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++)
1170 			kasan_unpoison_object_data(skbuff_head_cache,
1171 						   nc->skb_cache[i]);
1172 
1173 		kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF,
1174 				     nc->skb_cache + NAPI_SKB_CACHE_HALF);
1175 		nc->skb_count = NAPI_SKB_CACHE_HALF;
1176 	}
1177 }
1178 
1179 void __kfree_skb_defer(struct sk_buff *skb)
1180 {
1181 	skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED);
1182 	napi_skb_cache_put(skb);
1183 }
1184 
1185 void napi_skb_free_stolen_head(struct sk_buff *skb)
1186 {
1187 	if (unlikely(skb->slow_gro)) {
1188 		nf_reset_ct(skb);
1189 		skb_dst_drop(skb);
1190 		skb_ext_put(skb);
1191 		skb_orphan(skb);
1192 		skb->slow_gro = 0;
1193 	}
1194 	napi_skb_cache_put(skb);
1195 }
1196 
1197 void napi_consume_skb(struct sk_buff *skb, int budget)
1198 {
1199 	/* Zero budget indicate non-NAPI context called us, like netpoll */
1200 	if (unlikely(!budget)) {
1201 		dev_consume_skb_any(skb);
1202 		return;
1203 	}
1204 
1205 	DEBUG_NET_WARN_ON_ONCE(!in_softirq());
1206 
1207 	if (!skb_unref(skb))
1208 		return;
1209 
1210 	/* if reaching here SKB is ready to free */
1211 	trace_consume_skb(skb);
1212 
1213 	/* if SKB is a clone, don't handle this case */
1214 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
1215 		__kfree_skb(skb);
1216 		return;
1217 	}
1218 
1219 	skb_release_all(skb, SKB_CONSUMED);
1220 	napi_skb_cache_put(skb);
1221 }
1222 EXPORT_SYMBOL(napi_consume_skb);
1223 
1224 /* Make sure a field is contained by headers group */
1225 #define CHECK_SKB_FIELD(field) \
1226 	BUILD_BUG_ON(offsetof(struct sk_buff, field) !=		\
1227 		     offsetof(struct sk_buff, headers.field));	\
1228 
1229 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1230 {
1231 	new->tstamp		= old->tstamp;
1232 	/* We do not copy old->sk */
1233 	new->dev		= old->dev;
1234 	memcpy(new->cb, old->cb, sizeof(old->cb));
1235 	skb_dst_copy(new, old);
1236 	__skb_ext_copy(new, old);
1237 	__nf_copy(new, old, false);
1238 
1239 	/* Note : this field could be in the headers group.
1240 	 * It is not yet because we do not want to have a 16 bit hole
1241 	 */
1242 	new->queue_mapping = old->queue_mapping;
1243 
1244 	memcpy(&new->headers, &old->headers, sizeof(new->headers));
1245 	CHECK_SKB_FIELD(protocol);
1246 	CHECK_SKB_FIELD(csum);
1247 	CHECK_SKB_FIELD(hash);
1248 	CHECK_SKB_FIELD(priority);
1249 	CHECK_SKB_FIELD(skb_iif);
1250 	CHECK_SKB_FIELD(vlan_proto);
1251 	CHECK_SKB_FIELD(vlan_tci);
1252 	CHECK_SKB_FIELD(transport_header);
1253 	CHECK_SKB_FIELD(network_header);
1254 	CHECK_SKB_FIELD(mac_header);
1255 	CHECK_SKB_FIELD(inner_protocol);
1256 	CHECK_SKB_FIELD(inner_transport_header);
1257 	CHECK_SKB_FIELD(inner_network_header);
1258 	CHECK_SKB_FIELD(inner_mac_header);
1259 	CHECK_SKB_FIELD(mark);
1260 #ifdef CONFIG_NETWORK_SECMARK
1261 	CHECK_SKB_FIELD(secmark);
1262 #endif
1263 #ifdef CONFIG_NET_RX_BUSY_POLL
1264 	CHECK_SKB_FIELD(napi_id);
1265 #endif
1266 	CHECK_SKB_FIELD(alloc_cpu);
1267 #ifdef CONFIG_XPS
1268 	CHECK_SKB_FIELD(sender_cpu);
1269 #endif
1270 #ifdef CONFIG_NET_SCHED
1271 	CHECK_SKB_FIELD(tc_index);
1272 #endif
1273 
1274 }
1275 
1276 /*
1277  * You should not add any new code to this function.  Add it to
1278  * __copy_skb_header above instead.
1279  */
1280 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
1281 {
1282 #define C(x) n->x = skb->x
1283 
1284 	n->next = n->prev = NULL;
1285 	n->sk = NULL;
1286 	__copy_skb_header(n, skb);
1287 
1288 	C(len);
1289 	C(data_len);
1290 	C(mac_len);
1291 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
1292 	n->cloned = 1;
1293 	n->nohdr = 0;
1294 	n->peeked = 0;
1295 	C(pfmemalloc);
1296 	C(pp_recycle);
1297 	n->destructor = NULL;
1298 	C(tail);
1299 	C(end);
1300 	C(head);
1301 	C(head_frag);
1302 	C(data);
1303 	C(truesize);
1304 	refcount_set(&n->users, 1);
1305 
1306 	atomic_inc(&(skb_shinfo(skb)->dataref));
1307 	skb->cloned = 1;
1308 
1309 	return n;
1310 #undef C
1311 }
1312 
1313 /**
1314  * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg
1315  * @first: first sk_buff of the msg
1316  */
1317 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first)
1318 {
1319 	struct sk_buff *n;
1320 
1321 	n = alloc_skb(0, GFP_ATOMIC);
1322 	if (!n)
1323 		return NULL;
1324 
1325 	n->len = first->len;
1326 	n->data_len = first->len;
1327 	n->truesize = first->truesize;
1328 
1329 	skb_shinfo(n)->frag_list = first;
1330 
1331 	__copy_skb_header(n, first);
1332 	n->destructor = NULL;
1333 
1334 	return n;
1335 }
1336 EXPORT_SYMBOL_GPL(alloc_skb_for_msg);
1337 
1338 /**
1339  *	skb_morph	-	morph one skb into another
1340  *	@dst: the skb to receive the contents
1341  *	@src: the skb to supply the contents
1342  *
1343  *	This is identical to skb_clone except that the target skb is
1344  *	supplied by the user.
1345  *
1346  *	The target skb is returned upon exit.
1347  */
1348 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
1349 {
1350 	skb_release_all(dst, SKB_CONSUMED);
1351 	return __skb_clone(dst, src);
1352 }
1353 EXPORT_SYMBOL_GPL(skb_morph);
1354 
1355 int mm_account_pinned_pages(struct mmpin *mmp, size_t size)
1356 {
1357 	unsigned long max_pg, num_pg, new_pg, old_pg;
1358 	struct user_struct *user;
1359 
1360 	if (capable(CAP_IPC_LOCK) || !size)
1361 		return 0;
1362 
1363 	num_pg = (size >> PAGE_SHIFT) + 2;	/* worst case */
1364 	max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1365 	user = mmp->user ? : current_user();
1366 
1367 	old_pg = atomic_long_read(&user->locked_vm);
1368 	do {
1369 		new_pg = old_pg + num_pg;
1370 		if (new_pg > max_pg)
1371 			return -ENOBUFS;
1372 	} while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg));
1373 
1374 	if (!mmp->user) {
1375 		mmp->user = get_uid(user);
1376 		mmp->num_pg = num_pg;
1377 	} else {
1378 		mmp->num_pg += num_pg;
1379 	}
1380 
1381 	return 0;
1382 }
1383 EXPORT_SYMBOL_GPL(mm_account_pinned_pages);
1384 
1385 void mm_unaccount_pinned_pages(struct mmpin *mmp)
1386 {
1387 	if (mmp->user) {
1388 		atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm);
1389 		free_uid(mmp->user);
1390 	}
1391 }
1392 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages);
1393 
1394 static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size)
1395 {
1396 	struct ubuf_info_msgzc *uarg;
1397 	struct sk_buff *skb;
1398 
1399 	WARN_ON_ONCE(!in_task());
1400 
1401 	skb = sock_omalloc(sk, 0, GFP_KERNEL);
1402 	if (!skb)
1403 		return NULL;
1404 
1405 	BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb));
1406 	uarg = (void *)skb->cb;
1407 	uarg->mmp.user = NULL;
1408 
1409 	if (mm_account_pinned_pages(&uarg->mmp, size)) {
1410 		kfree_skb(skb);
1411 		return NULL;
1412 	}
1413 
1414 	uarg->ubuf.callback = msg_zerocopy_callback;
1415 	uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1;
1416 	uarg->len = 1;
1417 	uarg->bytelen = size;
1418 	uarg->zerocopy = 1;
1419 	uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN;
1420 	refcount_set(&uarg->ubuf.refcnt, 1);
1421 	sock_hold(sk);
1422 
1423 	return &uarg->ubuf;
1424 }
1425 
1426 static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg)
1427 {
1428 	return container_of((void *)uarg, struct sk_buff, cb);
1429 }
1430 
1431 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size,
1432 				       struct ubuf_info *uarg)
1433 {
1434 	if (uarg) {
1435 		struct ubuf_info_msgzc *uarg_zc;
1436 		const u32 byte_limit = 1 << 19;		/* limit to a few TSO */
1437 		u32 bytelen, next;
1438 
1439 		/* there might be non MSG_ZEROCOPY users */
1440 		if (uarg->callback != msg_zerocopy_callback)
1441 			return NULL;
1442 
1443 		/* realloc only when socket is locked (TCP, UDP cork),
1444 		 * so uarg->len and sk_zckey access is serialized
1445 		 */
1446 		if (!sock_owned_by_user(sk)) {
1447 			WARN_ON_ONCE(1);
1448 			return NULL;
1449 		}
1450 
1451 		uarg_zc = uarg_to_msgzc(uarg);
1452 		bytelen = uarg_zc->bytelen + size;
1453 		if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) {
1454 			/* TCP can create new skb to attach new uarg */
1455 			if (sk->sk_type == SOCK_STREAM)
1456 				goto new_alloc;
1457 			return NULL;
1458 		}
1459 
1460 		next = (u32)atomic_read(&sk->sk_zckey);
1461 		if ((u32)(uarg_zc->id + uarg_zc->len) == next) {
1462 			if (mm_account_pinned_pages(&uarg_zc->mmp, size))
1463 				return NULL;
1464 			uarg_zc->len++;
1465 			uarg_zc->bytelen = bytelen;
1466 			atomic_set(&sk->sk_zckey, ++next);
1467 
1468 			/* no extra ref when appending to datagram (MSG_MORE) */
1469 			if (sk->sk_type == SOCK_STREAM)
1470 				net_zcopy_get(uarg);
1471 
1472 			return uarg;
1473 		}
1474 	}
1475 
1476 new_alloc:
1477 	return msg_zerocopy_alloc(sk, size);
1478 }
1479 EXPORT_SYMBOL_GPL(msg_zerocopy_realloc);
1480 
1481 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len)
1482 {
1483 	struct sock_exterr_skb *serr = SKB_EXT_ERR(skb);
1484 	u32 old_lo, old_hi;
1485 	u64 sum_len;
1486 
1487 	old_lo = serr->ee.ee_info;
1488 	old_hi = serr->ee.ee_data;
1489 	sum_len = old_hi - old_lo + 1ULL + len;
1490 
1491 	if (sum_len >= (1ULL << 32))
1492 		return false;
1493 
1494 	if (lo != old_hi + 1)
1495 		return false;
1496 
1497 	serr->ee.ee_data += len;
1498 	return true;
1499 }
1500 
1501 static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg)
1502 {
1503 	struct sk_buff *tail, *skb = skb_from_uarg(uarg);
1504 	struct sock_exterr_skb *serr;
1505 	struct sock *sk = skb->sk;
1506 	struct sk_buff_head *q;
1507 	unsigned long flags;
1508 	bool is_zerocopy;
1509 	u32 lo, hi;
1510 	u16 len;
1511 
1512 	mm_unaccount_pinned_pages(&uarg->mmp);
1513 
1514 	/* if !len, there was only 1 call, and it was aborted
1515 	 * so do not queue a completion notification
1516 	 */
1517 	if (!uarg->len || sock_flag(sk, SOCK_DEAD))
1518 		goto release;
1519 
1520 	len = uarg->len;
1521 	lo = uarg->id;
1522 	hi = uarg->id + len - 1;
1523 	is_zerocopy = uarg->zerocopy;
1524 
1525 	serr = SKB_EXT_ERR(skb);
1526 	memset(serr, 0, sizeof(*serr));
1527 	serr->ee.ee_errno = 0;
1528 	serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY;
1529 	serr->ee.ee_data = hi;
1530 	serr->ee.ee_info = lo;
1531 	if (!is_zerocopy)
1532 		serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED;
1533 
1534 	q = &sk->sk_error_queue;
1535 	spin_lock_irqsave(&q->lock, flags);
1536 	tail = skb_peek_tail(q);
1537 	if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY ||
1538 	    !skb_zerocopy_notify_extend(tail, lo, len)) {
1539 		__skb_queue_tail(q, skb);
1540 		skb = NULL;
1541 	}
1542 	spin_unlock_irqrestore(&q->lock, flags);
1543 
1544 	sk_error_report(sk);
1545 
1546 release:
1547 	consume_skb(skb);
1548 	sock_put(sk);
1549 }
1550 
1551 void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg,
1552 			   bool success)
1553 {
1554 	struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg);
1555 
1556 	uarg_zc->zerocopy = uarg_zc->zerocopy & success;
1557 
1558 	if (refcount_dec_and_test(&uarg->refcnt))
1559 		__msg_zerocopy_callback(uarg_zc);
1560 }
1561 EXPORT_SYMBOL_GPL(msg_zerocopy_callback);
1562 
1563 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref)
1564 {
1565 	struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk;
1566 
1567 	atomic_dec(&sk->sk_zckey);
1568 	uarg_to_msgzc(uarg)->len--;
1569 
1570 	if (have_uref)
1571 		msg_zerocopy_callback(NULL, uarg, true);
1572 }
1573 EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort);
1574 
1575 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
1576 			     struct msghdr *msg, int len,
1577 			     struct ubuf_info *uarg)
1578 {
1579 	struct ubuf_info *orig_uarg = skb_zcopy(skb);
1580 	int err, orig_len = skb->len;
1581 
1582 	/* An skb can only point to one uarg. This edge case happens when
1583 	 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc.
1584 	 */
1585 	if (orig_uarg && uarg != orig_uarg)
1586 		return -EEXIST;
1587 
1588 	err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len);
1589 	if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) {
1590 		struct sock *save_sk = skb->sk;
1591 
1592 		/* Streams do not free skb on error. Reset to prev state. */
1593 		iov_iter_revert(&msg->msg_iter, skb->len - orig_len);
1594 		skb->sk = sk;
1595 		___pskb_trim(skb, orig_len);
1596 		skb->sk = save_sk;
1597 		return err;
1598 	}
1599 
1600 	skb_zcopy_set(skb, uarg, NULL);
1601 	return skb->len - orig_len;
1602 }
1603 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream);
1604 
1605 void __skb_zcopy_downgrade_managed(struct sk_buff *skb)
1606 {
1607 	int i;
1608 
1609 	skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS;
1610 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1611 		skb_frag_ref(skb, i);
1612 }
1613 EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed);
1614 
1615 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig,
1616 			      gfp_t gfp_mask)
1617 {
1618 	if (skb_zcopy(orig)) {
1619 		if (skb_zcopy(nskb)) {
1620 			/* !gfp_mask callers are verified to !skb_zcopy(nskb) */
1621 			if (!gfp_mask) {
1622 				WARN_ON_ONCE(1);
1623 				return -ENOMEM;
1624 			}
1625 			if (skb_uarg(nskb) == skb_uarg(orig))
1626 				return 0;
1627 			if (skb_copy_ubufs(nskb, GFP_ATOMIC))
1628 				return -EIO;
1629 		}
1630 		skb_zcopy_set(nskb, skb_uarg(orig), NULL);
1631 	}
1632 	return 0;
1633 }
1634 
1635 /**
1636  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
1637  *	@skb: the skb to modify
1638  *	@gfp_mask: allocation priority
1639  *
1640  *	This must be called on skb with SKBFL_ZEROCOPY_ENABLE.
1641  *	It will copy all frags into kernel and drop the reference
1642  *	to userspace pages.
1643  *
1644  *	If this function is called from an interrupt gfp_mask() must be
1645  *	%GFP_ATOMIC.
1646  *
1647  *	Returns 0 on success or a negative error code on failure
1648  *	to allocate kernel memory to copy to.
1649  */
1650 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
1651 {
1652 	int num_frags = skb_shinfo(skb)->nr_frags;
1653 	struct page *page, *head = NULL;
1654 	int i, new_frags;
1655 	u32 d_off;
1656 
1657 	if (skb_shared(skb) || skb_unclone(skb, gfp_mask))
1658 		return -EINVAL;
1659 
1660 	if (!num_frags)
1661 		goto release;
1662 
1663 	new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT;
1664 	for (i = 0; i < new_frags; i++) {
1665 		page = alloc_page(gfp_mask);
1666 		if (!page) {
1667 			while (head) {
1668 				struct page *next = (struct page *)page_private(head);
1669 				put_page(head);
1670 				head = next;
1671 			}
1672 			return -ENOMEM;
1673 		}
1674 		set_page_private(page, (unsigned long)head);
1675 		head = page;
1676 	}
1677 
1678 	page = head;
1679 	d_off = 0;
1680 	for (i = 0; i < num_frags; i++) {
1681 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1682 		u32 p_off, p_len, copied;
1683 		struct page *p;
1684 		u8 *vaddr;
1685 
1686 		skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f),
1687 				      p, p_off, p_len, copied) {
1688 			u32 copy, done = 0;
1689 			vaddr = kmap_atomic(p);
1690 
1691 			while (done < p_len) {
1692 				if (d_off == PAGE_SIZE) {
1693 					d_off = 0;
1694 					page = (struct page *)page_private(page);
1695 				}
1696 				copy = min_t(u32, PAGE_SIZE - d_off, p_len - done);
1697 				memcpy(page_address(page) + d_off,
1698 				       vaddr + p_off + done, copy);
1699 				done += copy;
1700 				d_off += copy;
1701 			}
1702 			kunmap_atomic(vaddr);
1703 		}
1704 	}
1705 
1706 	/* skb frags release userspace buffers */
1707 	for (i = 0; i < num_frags; i++)
1708 		skb_frag_unref(skb, i);
1709 
1710 	/* skb frags point to kernel buffers */
1711 	for (i = 0; i < new_frags - 1; i++) {
1712 		__skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE);
1713 		head = (struct page *)page_private(head);
1714 	}
1715 	__skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off);
1716 	skb_shinfo(skb)->nr_frags = new_frags;
1717 
1718 release:
1719 	skb_zcopy_clear(skb, false);
1720 	return 0;
1721 }
1722 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
1723 
1724 /**
1725  *	skb_clone	-	duplicate an sk_buff
1726  *	@skb: buffer to clone
1727  *	@gfp_mask: allocation priority
1728  *
1729  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
1730  *	copies share the same packet data but not structure. The new
1731  *	buffer has a reference count of 1. If the allocation fails the
1732  *	function returns %NULL otherwise the new buffer is returned.
1733  *
1734  *	If this function is called from an interrupt gfp_mask() must be
1735  *	%GFP_ATOMIC.
1736  */
1737 
1738 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1739 {
1740 	struct sk_buff_fclones *fclones = container_of(skb,
1741 						       struct sk_buff_fclones,
1742 						       skb1);
1743 	struct sk_buff *n;
1744 
1745 	if (skb_orphan_frags(skb, gfp_mask))
1746 		return NULL;
1747 
1748 	if (skb->fclone == SKB_FCLONE_ORIG &&
1749 	    refcount_read(&fclones->fclone_ref) == 1) {
1750 		n = &fclones->skb2;
1751 		refcount_set(&fclones->fclone_ref, 2);
1752 		n->fclone = SKB_FCLONE_CLONE;
1753 	} else {
1754 		if (skb_pfmemalloc(skb))
1755 			gfp_mask |= __GFP_MEMALLOC;
1756 
1757 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1758 		if (!n)
1759 			return NULL;
1760 
1761 		n->fclone = SKB_FCLONE_UNAVAILABLE;
1762 	}
1763 
1764 	return __skb_clone(n, skb);
1765 }
1766 EXPORT_SYMBOL(skb_clone);
1767 
1768 void skb_headers_offset_update(struct sk_buff *skb, int off)
1769 {
1770 	/* Only adjust this if it actually is csum_start rather than csum */
1771 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1772 		skb->csum_start += off;
1773 	/* {transport,network,mac}_header and tail are relative to skb->head */
1774 	skb->transport_header += off;
1775 	skb->network_header   += off;
1776 	if (skb_mac_header_was_set(skb))
1777 		skb->mac_header += off;
1778 	skb->inner_transport_header += off;
1779 	skb->inner_network_header += off;
1780 	skb->inner_mac_header += off;
1781 }
1782 EXPORT_SYMBOL(skb_headers_offset_update);
1783 
1784 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old)
1785 {
1786 	__copy_skb_header(new, old);
1787 
1788 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1789 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1790 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1791 }
1792 EXPORT_SYMBOL(skb_copy_header);
1793 
1794 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1795 {
1796 	if (skb_pfmemalloc(skb))
1797 		return SKB_ALLOC_RX;
1798 	return 0;
1799 }
1800 
1801 /**
1802  *	skb_copy	-	create private copy of an sk_buff
1803  *	@skb: buffer to copy
1804  *	@gfp_mask: allocation priority
1805  *
1806  *	Make a copy of both an &sk_buff and its data. This is used when the
1807  *	caller wishes to modify the data and needs a private copy of the
1808  *	data to alter. Returns %NULL on failure or the pointer to the buffer
1809  *	on success. The returned buffer has a reference count of 1.
1810  *
1811  *	As by-product this function converts non-linear &sk_buff to linear
1812  *	one, so that &sk_buff becomes completely private and caller is allowed
1813  *	to modify all the data of returned buffer. This means that this
1814  *	function is not recommended for use in circumstances when only
1815  *	header is going to be modified. Use pskb_copy() instead.
1816  */
1817 
1818 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1819 {
1820 	int headerlen = skb_headroom(skb);
1821 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1822 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1823 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1824 
1825 	if (!n)
1826 		return NULL;
1827 
1828 	/* Set the data pointer */
1829 	skb_reserve(n, headerlen);
1830 	/* Set the tail pointer and length */
1831 	skb_put(n, skb->len);
1832 
1833 	BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len));
1834 
1835 	skb_copy_header(n, skb);
1836 	return n;
1837 }
1838 EXPORT_SYMBOL(skb_copy);
1839 
1840 /**
1841  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
1842  *	@skb: buffer to copy
1843  *	@headroom: headroom of new skb
1844  *	@gfp_mask: allocation priority
1845  *	@fclone: if true allocate the copy of the skb from the fclone
1846  *	cache instead of the head cache; it is recommended to set this
1847  *	to true for the cases where the copy will likely be cloned
1848  *
1849  *	Make a copy of both an &sk_buff and part of its data, located
1850  *	in header. Fragmented data remain shared. This is used when
1851  *	the caller wishes to modify only header of &sk_buff and needs
1852  *	private copy of the header to alter. Returns %NULL on failure
1853  *	or the pointer to the buffer on success.
1854  *	The returned buffer has a reference count of 1.
1855  */
1856 
1857 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1858 				   gfp_t gfp_mask, bool fclone)
1859 {
1860 	unsigned int size = skb_headlen(skb) + headroom;
1861 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1862 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1863 
1864 	if (!n)
1865 		goto out;
1866 
1867 	/* Set the data pointer */
1868 	skb_reserve(n, headroom);
1869 	/* Set the tail pointer and length */
1870 	skb_put(n, skb_headlen(skb));
1871 	/* Copy the bytes */
1872 	skb_copy_from_linear_data(skb, n->data, n->len);
1873 
1874 	n->truesize += skb->data_len;
1875 	n->data_len  = skb->data_len;
1876 	n->len	     = skb->len;
1877 
1878 	if (skb_shinfo(skb)->nr_frags) {
1879 		int i;
1880 
1881 		if (skb_orphan_frags(skb, gfp_mask) ||
1882 		    skb_zerocopy_clone(n, skb, gfp_mask)) {
1883 			kfree_skb(n);
1884 			n = NULL;
1885 			goto out;
1886 		}
1887 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1888 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1889 			skb_frag_ref(skb, i);
1890 		}
1891 		skb_shinfo(n)->nr_frags = i;
1892 	}
1893 
1894 	if (skb_has_frag_list(skb)) {
1895 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1896 		skb_clone_fraglist(n);
1897 	}
1898 
1899 	skb_copy_header(n, skb);
1900 out:
1901 	return n;
1902 }
1903 EXPORT_SYMBOL(__pskb_copy_fclone);
1904 
1905 /**
1906  *	pskb_expand_head - reallocate header of &sk_buff
1907  *	@skb: buffer to reallocate
1908  *	@nhead: room to add at head
1909  *	@ntail: room to add at tail
1910  *	@gfp_mask: allocation priority
1911  *
1912  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1913  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1914  *	reference count of 1. Returns zero in the case of success or error,
1915  *	if expansion failed. In the last case, &sk_buff is not changed.
1916  *
1917  *	All the pointers pointing into skb header may change and must be
1918  *	reloaded after call to this function.
1919  */
1920 
1921 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1922 		     gfp_t gfp_mask)
1923 {
1924 	unsigned int osize = skb_end_offset(skb);
1925 	unsigned int size = osize + nhead + ntail;
1926 	long off;
1927 	u8 *data;
1928 	int i;
1929 
1930 	BUG_ON(nhead < 0);
1931 
1932 	BUG_ON(skb_shared(skb));
1933 
1934 	skb_zcopy_downgrade_managed(skb);
1935 
1936 	if (skb_pfmemalloc(skb))
1937 		gfp_mask |= __GFP_MEMALLOC;
1938 
1939 	size = SKB_DATA_ALIGN(size);
1940 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1941 	size = kmalloc_size_roundup(size);
1942 	data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL);
1943 	if (!data)
1944 		goto nodata;
1945 	size = SKB_WITH_OVERHEAD(size);
1946 
1947 	/* Copy only real data... and, alas, header. This should be
1948 	 * optimized for the cases when header is void.
1949 	 */
1950 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1951 
1952 	memcpy((struct skb_shared_info *)(data + size),
1953 	       skb_shinfo(skb),
1954 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1955 
1956 	/*
1957 	 * if shinfo is shared we must drop the old head gracefully, but if it
1958 	 * is not we can just drop the old head and let the existing refcount
1959 	 * be since all we did is relocate the values
1960 	 */
1961 	if (skb_cloned(skb)) {
1962 		if (skb_orphan_frags(skb, gfp_mask))
1963 			goto nofrags;
1964 		if (skb_zcopy(skb))
1965 			refcount_inc(&skb_uarg(skb)->refcnt);
1966 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1967 			skb_frag_ref(skb, i);
1968 
1969 		if (skb_has_frag_list(skb))
1970 			skb_clone_fraglist(skb);
1971 
1972 		skb_release_data(skb, SKB_CONSUMED);
1973 	} else {
1974 		skb_free_head(skb);
1975 	}
1976 	off = (data + nhead) - skb->head;
1977 
1978 	skb->head     = data;
1979 	skb->head_frag = 0;
1980 	skb->data    += off;
1981 
1982 	skb_set_end_offset(skb, size);
1983 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1984 	off           = nhead;
1985 #endif
1986 	skb->tail	      += off;
1987 	skb_headers_offset_update(skb, nhead);
1988 	skb->cloned   = 0;
1989 	skb->hdr_len  = 0;
1990 	skb->nohdr    = 0;
1991 	atomic_set(&skb_shinfo(skb)->dataref, 1);
1992 
1993 	skb_metadata_clear(skb);
1994 
1995 	/* It is not generally safe to change skb->truesize.
1996 	 * For the moment, we really care of rx path, or
1997 	 * when skb is orphaned (not attached to a socket).
1998 	 */
1999 	if (!skb->sk || skb->destructor == sock_edemux)
2000 		skb->truesize += size - osize;
2001 
2002 	return 0;
2003 
2004 nofrags:
2005 	kfree(data);
2006 nodata:
2007 	return -ENOMEM;
2008 }
2009 EXPORT_SYMBOL(pskb_expand_head);
2010 
2011 /* Make private copy of skb with writable head and some headroom */
2012 
2013 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
2014 {
2015 	struct sk_buff *skb2;
2016 	int delta = headroom - skb_headroom(skb);
2017 
2018 	if (delta <= 0)
2019 		skb2 = pskb_copy(skb, GFP_ATOMIC);
2020 	else {
2021 		skb2 = skb_clone(skb, GFP_ATOMIC);
2022 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
2023 					     GFP_ATOMIC)) {
2024 			kfree_skb(skb2);
2025 			skb2 = NULL;
2026 		}
2027 	}
2028 	return skb2;
2029 }
2030 EXPORT_SYMBOL(skb_realloc_headroom);
2031 
2032 int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri)
2033 {
2034 	unsigned int saved_end_offset, saved_truesize;
2035 	struct skb_shared_info *shinfo;
2036 	int res;
2037 
2038 	saved_end_offset = skb_end_offset(skb);
2039 	saved_truesize = skb->truesize;
2040 
2041 	res = pskb_expand_head(skb, 0, 0, pri);
2042 	if (res)
2043 		return res;
2044 
2045 	skb->truesize = saved_truesize;
2046 
2047 	if (likely(skb_end_offset(skb) == saved_end_offset))
2048 		return 0;
2049 
2050 	shinfo = skb_shinfo(skb);
2051 
2052 	/* We are about to change back skb->end,
2053 	 * we need to move skb_shinfo() to its new location.
2054 	 */
2055 	memmove(skb->head + saved_end_offset,
2056 		shinfo,
2057 		offsetof(struct skb_shared_info, frags[shinfo->nr_frags]));
2058 
2059 	skb_set_end_offset(skb, saved_end_offset);
2060 
2061 	return 0;
2062 }
2063 
2064 /**
2065  *	skb_expand_head - reallocate header of &sk_buff
2066  *	@skb: buffer to reallocate
2067  *	@headroom: needed headroom
2068  *
2069  *	Unlike skb_realloc_headroom, this one does not allocate a new skb
2070  *	if possible; copies skb->sk to new skb as needed
2071  *	and frees original skb in case of failures.
2072  *
2073  *	It expect increased headroom and generates warning otherwise.
2074  */
2075 
2076 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom)
2077 {
2078 	int delta = headroom - skb_headroom(skb);
2079 	int osize = skb_end_offset(skb);
2080 	struct sock *sk = skb->sk;
2081 
2082 	if (WARN_ONCE(delta <= 0,
2083 		      "%s is expecting an increase in the headroom", __func__))
2084 		return skb;
2085 
2086 	delta = SKB_DATA_ALIGN(delta);
2087 	/* pskb_expand_head() might crash, if skb is shared. */
2088 	if (skb_shared(skb) || !is_skb_wmem(skb)) {
2089 		struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
2090 
2091 		if (unlikely(!nskb))
2092 			goto fail;
2093 
2094 		if (sk)
2095 			skb_set_owner_w(nskb, sk);
2096 		consume_skb(skb);
2097 		skb = nskb;
2098 	}
2099 	if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC))
2100 		goto fail;
2101 
2102 	if (sk && is_skb_wmem(skb)) {
2103 		delta = skb_end_offset(skb) - osize;
2104 		refcount_add(delta, &sk->sk_wmem_alloc);
2105 		skb->truesize += delta;
2106 	}
2107 	return skb;
2108 
2109 fail:
2110 	kfree_skb(skb);
2111 	return NULL;
2112 }
2113 EXPORT_SYMBOL(skb_expand_head);
2114 
2115 /**
2116  *	skb_copy_expand	-	copy and expand sk_buff
2117  *	@skb: buffer to copy
2118  *	@newheadroom: new free bytes at head
2119  *	@newtailroom: new free bytes at tail
2120  *	@gfp_mask: allocation priority
2121  *
2122  *	Make a copy of both an &sk_buff and its data and while doing so
2123  *	allocate additional space.
2124  *
2125  *	This is used when the caller wishes to modify the data and needs a
2126  *	private copy of the data to alter as well as more space for new fields.
2127  *	Returns %NULL on failure or the pointer to the buffer
2128  *	on success. The returned buffer has a reference count of 1.
2129  *
2130  *	You must pass %GFP_ATOMIC as the allocation priority if this function
2131  *	is called from an interrupt.
2132  */
2133 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
2134 				int newheadroom, int newtailroom,
2135 				gfp_t gfp_mask)
2136 {
2137 	/*
2138 	 *	Allocate the copy buffer
2139 	 */
2140 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
2141 					gfp_mask, skb_alloc_rx_flag(skb),
2142 					NUMA_NO_NODE);
2143 	int oldheadroom = skb_headroom(skb);
2144 	int head_copy_len, head_copy_off;
2145 
2146 	if (!n)
2147 		return NULL;
2148 
2149 	skb_reserve(n, newheadroom);
2150 
2151 	/* Set the tail pointer and length */
2152 	skb_put(n, skb->len);
2153 
2154 	head_copy_len = oldheadroom;
2155 	head_copy_off = 0;
2156 	if (newheadroom <= head_copy_len)
2157 		head_copy_len = newheadroom;
2158 	else
2159 		head_copy_off = newheadroom - head_copy_len;
2160 
2161 	/* Copy the linear header and data. */
2162 	BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
2163 			     skb->len + head_copy_len));
2164 
2165 	skb_copy_header(n, skb);
2166 
2167 	skb_headers_offset_update(n, newheadroom - oldheadroom);
2168 
2169 	return n;
2170 }
2171 EXPORT_SYMBOL(skb_copy_expand);
2172 
2173 /**
2174  *	__skb_pad		-	zero pad the tail of an skb
2175  *	@skb: buffer to pad
2176  *	@pad: space to pad
2177  *	@free_on_error: free buffer on error
2178  *
2179  *	Ensure that a buffer is followed by a padding area that is zero
2180  *	filled. Used by network drivers which may DMA or transfer data
2181  *	beyond the buffer end onto the wire.
2182  *
2183  *	May return error in out of memory cases. The skb is freed on error
2184  *	if @free_on_error is true.
2185  */
2186 
2187 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error)
2188 {
2189 	int err;
2190 	int ntail;
2191 
2192 	/* If the skbuff is non linear tailroom is always zero.. */
2193 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
2194 		memset(skb->data+skb->len, 0, pad);
2195 		return 0;
2196 	}
2197 
2198 	ntail = skb->data_len + pad - (skb->end - skb->tail);
2199 	if (likely(skb_cloned(skb) || ntail > 0)) {
2200 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
2201 		if (unlikely(err))
2202 			goto free_skb;
2203 	}
2204 
2205 	/* FIXME: The use of this function with non-linear skb's really needs
2206 	 * to be audited.
2207 	 */
2208 	err = skb_linearize(skb);
2209 	if (unlikely(err))
2210 		goto free_skb;
2211 
2212 	memset(skb->data + skb->len, 0, pad);
2213 	return 0;
2214 
2215 free_skb:
2216 	if (free_on_error)
2217 		kfree_skb(skb);
2218 	return err;
2219 }
2220 EXPORT_SYMBOL(__skb_pad);
2221 
2222 /**
2223  *	pskb_put - add data to the tail of a potentially fragmented buffer
2224  *	@skb: start of the buffer to use
2225  *	@tail: tail fragment of the buffer to use
2226  *	@len: amount of data to add
2227  *
2228  *	This function extends the used data area of the potentially
2229  *	fragmented buffer. @tail must be the last fragment of @skb -- or
2230  *	@skb itself. If this would exceed the total buffer size the kernel
2231  *	will panic. A pointer to the first byte of the extra data is
2232  *	returned.
2233  */
2234 
2235 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
2236 {
2237 	if (tail != skb) {
2238 		skb->data_len += len;
2239 		skb->len += len;
2240 	}
2241 	return skb_put(tail, len);
2242 }
2243 EXPORT_SYMBOL_GPL(pskb_put);
2244 
2245 /**
2246  *	skb_put - add data to a buffer
2247  *	@skb: buffer to use
2248  *	@len: amount of data to add
2249  *
2250  *	This function extends the used data area of the buffer. If this would
2251  *	exceed the total buffer size the kernel will panic. A pointer to the
2252  *	first byte of the extra data is returned.
2253  */
2254 void *skb_put(struct sk_buff *skb, unsigned int len)
2255 {
2256 	void *tmp = skb_tail_pointer(skb);
2257 	SKB_LINEAR_ASSERT(skb);
2258 	skb->tail += len;
2259 	skb->len  += len;
2260 	if (unlikely(skb->tail > skb->end))
2261 		skb_over_panic(skb, len, __builtin_return_address(0));
2262 	return tmp;
2263 }
2264 EXPORT_SYMBOL(skb_put);
2265 
2266 /**
2267  *	skb_push - add data to the start of a buffer
2268  *	@skb: buffer to use
2269  *	@len: amount of data to add
2270  *
2271  *	This function extends the used data area of the buffer at the buffer
2272  *	start. If this would exceed the total buffer headroom the kernel will
2273  *	panic. A pointer to the first byte of the extra data is returned.
2274  */
2275 void *skb_push(struct sk_buff *skb, unsigned int len)
2276 {
2277 	skb->data -= len;
2278 	skb->len  += len;
2279 	if (unlikely(skb->data < skb->head))
2280 		skb_under_panic(skb, len, __builtin_return_address(0));
2281 	return skb->data;
2282 }
2283 EXPORT_SYMBOL(skb_push);
2284 
2285 /**
2286  *	skb_pull - remove data from the start of a buffer
2287  *	@skb: buffer to use
2288  *	@len: amount of data to remove
2289  *
2290  *	This function removes data from the start of a buffer, returning
2291  *	the memory to the headroom. A pointer to the next data in the buffer
2292  *	is returned. Once the data has been pulled future pushes will overwrite
2293  *	the old data.
2294  */
2295 void *skb_pull(struct sk_buff *skb, unsigned int len)
2296 {
2297 	return skb_pull_inline(skb, len);
2298 }
2299 EXPORT_SYMBOL(skb_pull);
2300 
2301 /**
2302  *	skb_pull_data - remove data from the start of a buffer returning its
2303  *	original position.
2304  *	@skb: buffer to use
2305  *	@len: amount of data to remove
2306  *
2307  *	This function removes data from the start of a buffer, returning
2308  *	the memory to the headroom. A pointer to the original data in the buffer
2309  *	is returned after checking if there is enough data to pull. Once the
2310  *	data has been pulled future pushes will overwrite the old data.
2311  */
2312 void *skb_pull_data(struct sk_buff *skb, size_t len)
2313 {
2314 	void *data = skb->data;
2315 
2316 	if (skb->len < len)
2317 		return NULL;
2318 
2319 	skb_pull(skb, len);
2320 
2321 	return data;
2322 }
2323 EXPORT_SYMBOL(skb_pull_data);
2324 
2325 /**
2326  *	skb_trim - remove end from a buffer
2327  *	@skb: buffer to alter
2328  *	@len: new length
2329  *
2330  *	Cut the length of a buffer down by removing data from the tail. If
2331  *	the buffer is already under the length specified it is not modified.
2332  *	The skb must be linear.
2333  */
2334 void skb_trim(struct sk_buff *skb, unsigned int len)
2335 {
2336 	if (skb->len > len)
2337 		__skb_trim(skb, len);
2338 }
2339 EXPORT_SYMBOL(skb_trim);
2340 
2341 /* Trims skb to length len. It can change skb pointers.
2342  */
2343 
2344 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
2345 {
2346 	struct sk_buff **fragp;
2347 	struct sk_buff *frag;
2348 	int offset = skb_headlen(skb);
2349 	int nfrags = skb_shinfo(skb)->nr_frags;
2350 	int i;
2351 	int err;
2352 
2353 	if (skb_cloned(skb) &&
2354 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
2355 		return err;
2356 
2357 	i = 0;
2358 	if (offset >= len)
2359 		goto drop_pages;
2360 
2361 	for (; i < nfrags; i++) {
2362 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2363 
2364 		if (end < len) {
2365 			offset = end;
2366 			continue;
2367 		}
2368 
2369 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
2370 
2371 drop_pages:
2372 		skb_shinfo(skb)->nr_frags = i;
2373 
2374 		for (; i < nfrags; i++)
2375 			skb_frag_unref(skb, i);
2376 
2377 		if (skb_has_frag_list(skb))
2378 			skb_drop_fraglist(skb);
2379 		goto done;
2380 	}
2381 
2382 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
2383 	     fragp = &frag->next) {
2384 		int end = offset + frag->len;
2385 
2386 		if (skb_shared(frag)) {
2387 			struct sk_buff *nfrag;
2388 
2389 			nfrag = skb_clone(frag, GFP_ATOMIC);
2390 			if (unlikely(!nfrag))
2391 				return -ENOMEM;
2392 
2393 			nfrag->next = frag->next;
2394 			consume_skb(frag);
2395 			frag = nfrag;
2396 			*fragp = frag;
2397 		}
2398 
2399 		if (end < len) {
2400 			offset = end;
2401 			continue;
2402 		}
2403 
2404 		if (end > len &&
2405 		    unlikely((err = pskb_trim(frag, len - offset))))
2406 			return err;
2407 
2408 		if (frag->next)
2409 			skb_drop_list(&frag->next);
2410 		break;
2411 	}
2412 
2413 done:
2414 	if (len > skb_headlen(skb)) {
2415 		skb->data_len -= skb->len - len;
2416 		skb->len       = len;
2417 	} else {
2418 		skb->len       = len;
2419 		skb->data_len  = 0;
2420 		skb_set_tail_pointer(skb, len);
2421 	}
2422 
2423 	if (!skb->sk || skb->destructor == sock_edemux)
2424 		skb_condense(skb);
2425 	return 0;
2426 }
2427 EXPORT_SYMBOL(___pskb_trim);
2428 
2429 /* Note : use pskb_trim_rcsum() instead of calling this directly
2430  */
2431 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len)
2432 {
2433 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
2434 		int delta = skb->len - len;
2435 
2436 		skb->csum = csum_block_sub(skb->csum,
2437 					   skb_checksum(skb, len, delta, 0),
2438 					   len);
2439 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
2440 		int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len;
2441 		int offset = skb_checksum_start_offset(skb) + skb->csum_offset;
2442 
2443 		if (offset + sizeof(__sum16) > hdlen)
2444 			return -EINVAL;
2445 	}
2446 	return __pskb_trim(skb, len);
2447 }
2448 EXPORT_SYMBOL(pskb_trim_rcsum_slow);
2449 
2450 /**
2451  *	__pskb_pull_tail - advance tail of skb header
2452  *	@skb: buffer to reallocate
2453  *	@delta: number of bytes to advance tail
2454  *
2455  *	The function makes a sense only on a fragmented &sk_buff,
2456  *	it expands header moving its tail forward and copying necessary
2457  *	data from fragmented part.
2458  *
2459  *	&sk_buff MUST have reference count of 1.
2460  *
2461  *	Returns %NULL (and &sk_buff does not change) if pull failed
2462  *	or value of new tail of skb in the case of success.
2463  *
2464  *	All the pointers pointing into skb header may change and must be
2465  *	reloaded after call to this function.
2466  */
2467 
2468 /* Moves tail of skb head forward, copying data from fragmented part,
2469  * when it is necessary.
2470  * 1. It may fail due to malloc failure.
2471  * 2. It may change skb pointers.
2472  *
2473  * It is pretty complicated. Luckily, it is called only in exceptional cases.
2474  */
2475 void *__pskb_pull_tail(struct sk_buff *skb, int delta)
2476 {
2477 	/* If skb has not enough free space at tail, get new one
2478 	 * plus 128 bytes for future expansions. If we have enough
2479 	 * room at tail, reallocate without expansion only if skb is cloned.
2480 	 */
2481 	int i, k, eat = (skb->tail + delta) - skb->end;
2482 
2483 	if (eat > 0 || skb_cloned(skb)) {
2484 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
2485 				     GFP_ATOMIC))
2486 			return NULL;
2487 	}
2488 
2489 	BUG_ON(skb_copy_bits(skb, skb_headlen(skb),
2490 			     skb_tail_pointer(skb), delta));
2491 
2492 	/* Optimization: no fragments, no reasons to preestimate
2493 	 * size of pulled pages. Superb.
2494 	 */
2495 	if (!skb_has_frag_list(skb))
2496 		goto pull_pages;
2497 
2498 	/* Estimate size of pulled pages. */
2499 	eat = delta;
2500 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2501 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2502 
2503 		if (size >= eat)
2504 			goto pull_pages;
2505 		eat -= size;
2506 	}
2507 
2508 	/* If we need update frag list, we are in troubles.
2509 	 * Certainly, it is possible to add an offset to skb data,
2510 	 * but taking into account that pulling is expected to
2511 	 * be very rare operation, it is worth to fight against
2512 	 * further bloating skb head and crucify ourselves here instead.
2513 	 * Pure masohism, indeed. 8)8)
2514 	 */
2515 	if (eat) {
2516 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
2517 		struct sk_buff *clone = NULL;
2518 		struct sk_buff *insp = NULL;
2519 
2520 		do {
2521 			if (list->len <= eat) {
2522 				/* Eaten as whole. */
2523 				eat -= list->len;
2524 				list = list->next;
2525 				insp = list;
2526 			} else {
2527 				/* Eaten partially. */
2528 				if (skb_is_gso(skb) && !list->head_frag &&
2529 				    skb_headlen(list))
2530 					skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
2531 
2532 				if (skb_shared(list)) {
2533 					/* Sucks! We need to fork list. :-( */
2534 					clone = skb_clone(list, GFP_ATOMIC);
2535 					if (!clone)
2536 						return NULL;
2537 					insp = list->next;
2538 					list = clone;
2539 				} else {
2540 					/* This may be pulled without
2541 					 * problems. */
2542 					insp = list;
2543 				}
2544 				if (!pskb_pull(list, eat)) {
2545 					kfree_skb(clone);
2546 					return NULL;
2547 				}
2548 				break;
2549 			}
2550 		} while (eat);
2551 
2552 		/* Free pulled out fragments. */
2553 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
2554 			skb_shinfo(skb)->frag_list = list->next;
2555 			consume_skb(list);
2556 		}
2557 		/* And insert new clone at head. */
2558 		if (clone) {
2559 			clone->next = list;
2560 			skb_shinfo(skb)->frag_list = clone;
2561 		}
2562 	}
2563 	/* Success! Now we may commit changes to skb data. */
2564 
2565 pull_pages:
2566 	eat = delta;
2567 	k = 0;
2568 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2569 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2570 
2571 		if (size <= eat) {
2572 			skb_frag_unref(skb, i);
2573 			eat -= size;
2574 		} else {
2575 			skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
2576 
2577 			*frag = skb_shinfo(skb)->frags[i];
2578 			if (eat) {
2579 				skb_frag_off_add(frag, eat);
2580 				skb_frag_size_sub(frag, eat);
2581 				if (!i)
2582 					goto end;
2583 				eat = 0;
2584 			}
2585 			k++;
2586 		}
2587 	}
2588 	skb_shinfo(skb)->nr_frags = k;
2589 
2590 end:
2591 	skb->tail     += delta;
2592 	skb->data_len -= delta;
2593 
2594 	if (!skb->data_len)
2595 		skb_zcopy_clear(skb, false);
2596 
2597 	return skb_tail_pointer(skb);
2598 }
2599 EXPORT_SYMBOL(__pskb_pull_tail);
2600 
2601 /**
2602  *	skb_copy_bits - copy bits from skb to kernel buffer
2603  *	@skb: source skb
2604  *	@offset: offset in source
2605  *	@to: destination buffer
2606  *	@len: number of bytes to copy
2607  *
2608  *	Copy the specified number of bytes from the source skb to the
2609  *	destination buffer.
2610  *
2611  *	CAUTION ! :
2612  *		If its prototype is ever changed,
2613  *		check arch/{*}/net/{*}.S files,
2614  *		since it is called from BPF assembly code.
2615  */
2616 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
2617 {
2618 	int start = skb_headlen(skb);
2619 	struct sk_buff *frag_iter;
2620 	int i, copy;
2621 
2622 	if (offset > (int)skb->len - len)
2623 		goto fault;
2624 
2625 	/* Copy header. */
2626 	if ((copy = start - offset) > 0) {
2627 		if (copy > len)
2628 			copy = len;
2629 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
2630 		if ((len -= copy) == 0)
2631 			return 0;
2632 		offset += copy;
2633 		to     += copy;
2634 	}
2635 
2636 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2637 		int end;
2638 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
2639 
2640 		WARN_ON(start > offset + len);
2641 
2642 		end = start + skb_frag_size(f);
2643 		if ((copy = end - offset) > 0) {
2644 			u32 p_off, p_len, copied;
2645 			struct page *p;
2646 			u8 *vaddr;
2647 
2648 			if (copy > len)
2649 				copy = len;
2650 
2651 			skb_frag_foreach_page(f,
2652 					      skb_frag_off(f) + offset - start,
2653 					      copy, p, p_off, p_len, copied) {
2654 				vaddr = kmap_atomic(p);
2655 				memcpy(to + copied, vaddr + p_off, p_len);
2656 				kunmap_atomic(vaddr);
2657 			}
2658 
2659 			if ((len -= copy) == 0)
2660 				return 0;
2661 			offset += copy;
2662 			to     += copy;
2663 		}
2664 		start = end;
2665 	}
2666 
2667 	skb_walk_frags(skb, frag_iter) {
2668 		int end;
2669 
2670 		WARN_ON(start > offset + len);
2671 
2672 		end = start + frag_iter->len;
2673 		if ((copy = end - offset) > 0) {
2674 			if (copy > len)
2675 				copy = len;
2676 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
2677 				goto fault;
2678 			if ((len -= copy) == 0)
2679 				return 0;
2680 			offset += copy;
2681 			to     += copy;
2682 		}
2683 		start = end;
2684 	}
2685 
2686 	if (!len)
2687 		return 0;
2688 
2689 fault:
2690 	return -EFAULT;
2691 }
2692 EXPORT_SYMBOL(skb_copy_bits);
2693 
2694 /*
2695  * Callback from splice_to_pipe(), if we need to release some pages
2696  * at the end of the spd in case we error'ed out in filling the pipe.
2697  */
2698 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
2699 {
2700 	put_page(spd->pages[i]);
2701 }
2702 
2703 static struct page *linear_to_page(struct page *page, unsigned int *len,
2704 				   unsigned int *offset,
2705 				   struct sock *sk)
2706 {
2707 	struct page_frag *pfrag = sk_page_frag(sk);
2708 
2709 	if (!sk_page_frag_refill(sk, pfrag))
2710 		return NULL;
2711 
2712 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
2713 
2714 	memcpy(page_address(pfrag->page) + pfrag->offset,
2715 	       page_address(page) + *offset, *len);
2716 	*offset = pfrag->offset;
2717 	pfrag->offset += *len;
2718 
2719 	return pfrag->page;
2720 }
2721 
2722 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
2723 			     struct page *page,
2724 			     unsigned int offset)
2725 {
2726 	return	spd->nr_pages &&
2727 		spd->pages[spd->nr_pages - 1] == page &&
2728 		(spd->partial[spd->nr_pages - 1].offset +
2729 		 spd->partial[spd->nr_pages - 1].len == offset);
2730 }
2731 
2732 /*
2733  * Fill page/offset/length into spd, if it can hold more pages.
2734  */
2735 static bool spd_fill_page(struct splice_pipe_desc *spd,
2736 			  struct pipe_inode_info *pipe, struct page *page,
2737 			  unsigned int *len, unsigned int offset,
2738 			  bool linear,
2739 			  struct sock *sk)
2740 {
2741 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
2742 		return true;
2743 
2744 	if (linear) {
2745 		page = linear_to_page(page, len, &offset, sk);
2746 		if (!page)
2747 			return true;
2748 	}
2749 	if (spd_can_coalesce(spd, page, offset)) {
2750 		spd->partial[spd->nr_pages - 1].len += *len;
2751 		return false;
2752 	}
2753 	get_page(page);
2754 	spd->pages[spd->nr_pages] = page;
2755 	spd->partial[spd->nr_pages].len = *len;
2756 	spd->partial[spd->nr_pages].offset = offset;
2757 	spd->nr_pages++;
2758 
2759 	return false;
2760 }
2761 
2762 static bool __splice_segment(struct page *page, unsigned int poff,
2763 			     unsigned int plen, unsigned int *off,
2764 			     unsigned int *len,
2765 			     struct splice_pipe_desc *spd, bool linear,
2766 			     struct sock *sk,
2767 			     struct pipe_inode_info *pipe)
2768 {
2769 	if (!*len)
2770 		return true;
2771 
2772 	/* skip this segment if already processed */
2773 	if (*off >= plen) {
2774 		*off -= plen;
2775 		return false;
2776 	}
2777 
2778 	/* ignore any bits we already processed */
2779 	poff += *off;
2780 	plen -= *off;
2781 	*off = 0;
2782 
2783 	do {
2784 		unsigned int flen = min(*len, plen);
2785 
2786 		if (spd_fill_page(spd, pipe, page, &flen, poff,
2787 				  linear, sk))
2788 			return true;
2789 		poff += flen;
2790 		plen -= flen;
2791 		*len -= flen;
2792 	} while (*len && plen);
2793 
2794 	return false;
2795 }
2796 
2797 /*
2798  * Map linear and fragment data from the skb to spd. It reports true if the
2799  * pipe is full or if we already spliced the requested length.
2800  */
2801 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
2802 			      unsigned int *offset, unsigned int *len,
2803 			      struct splice_pipe_desc *spd, struct sock *sk)
2804 {
2805 	int seg;
2806 	struct sk_buff *iter;
2807 
2808 	/* map the linear part :
2809 	 * If skb->head_frag is set, this 'linear' part is backed by a
2810 	 * fragment, and if the head is not shared with any clones then
2811 	 * we can avoid a copy since we own the head portion of this page.
2812 	 */
2813 	if (__splice_segment(virt_to_page(skb->data),
2814 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
2815 			     skb_headlen(skb),
2816 			     offset, len, spd,
2817 			     skb_head_is_locked(skb),
2818 			     sk, pipe))
2819 		return true;
2820 
2821 	/*
2822 	 * then map the fragments
2823 	 */
2824 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
2825 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
2826 
2827 		if (__splice_segment(skb_frag_page(f),
2828 				     skb_frag_off(f), skb_frag_size(f),
2829 				     offset, len, spd, false, sk, pipe))
2830 			return true;
2831 	}
2832 
2833 	skb_walk_frags(skb, iter) {
2834 		if (*offset >= iter->len) {
2835 			*offset -= iter->len;
2836 			continue;
2837 		}
2838 		/* __skb_splice_bits() only fails if the output has no room
2839 		 * left, so no point in going over the frag_list for the error
2840 		 * case.
2841 		 */
2842 		if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
2843 			return true;
2844 	}
2845 
2846 	return false;
2847 }
2848 
2849 /*
2850  * Map data from the skb to a pipe. Should handle both the linear part,
2851  * the fragments, and the frag list.
2852  */
2853 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
2854 		    struct pipe_inode_info *pipe, unsigned int tlen,
2855 		    unsigned int flags)
2856 {
2857 	struct partial_page partial[MAX_SKB_FRAGS];
2858 	struct page *pages[MAX_SKB_FRAGS];
2859 	struct splice_pipe_desc spd = {
2860 		.pages = pages,
2861 		.partial = partial,
2862 		.nr_pages_max = MAX_SKB_FRAGS,
2863 		.ops = &nosteal_pipe_buf_ops,
2864 		.spd_release = sock_spd_release,
2865 	};
2866 	int ret = 0;
2867 
2868 	__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
2869 
2870 	if (spd.nr_pages)
2871 		ret = splice_to_pipe(pipe, &spd);
2872 
2873 	return ret;
2874 }
2875 EXPORT_SYMBOL_GPL(skb_splice_bits);
2876 
2877 static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg,
2878 			    struct kvec *vec, size_t num, size_t size)
2879 {
2880 	struct socket *sock = sk->sk_socket;
2881 
2882 	if (!sock)
2883 		return -EINVAL;
2884 	return kernel_sendmsg(sock, msg, vec, num, size);
2885 }
2886 
2887 static int sendpage_unlocked(struct sock *sk, struct page *page, int offset,
2888 			     size_t size, int flags)
2889 {
2890 	struct socket *sock = sk->sk_socket;
2891 
2892 	if (!sock)
2893 		return -EINVAL;
2894 	return kernel_sendpage(sock, page, offset, size, flags);
2895 }
2896 
2897 typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg,
2898 			    struct kvec *vec, size_t num, size_t size);
2899 typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset,
2900 			     size_t size, int flags);
2901 static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
2902 			   int len, sendmsg_func sendmsg, sendpage_func sendpage)
2903 {
2904 	unsigned int orig_len = len;
2905 	struct sk_buff *head = skb;
2906 	unsigned short fragidx;
2907 	int slen, ret;
2908 
2909 do_frag_list:
2910 
2911 	/* Deal with head data */
2912 	while (offset < skb_headlen(skb) && len) {
2913 		struct kvec kv;
2914 		struct msghdr msg;
2915 
2916 		slen = min_t(int, len, skb_headlen(skb) - offset);
2917 		kv.iov_base = skb->data + offset;
2918 		kv.iov_len = slen;
2919 		memset(&msg, 0, sizeof(msg));
2920 		msg.msg_flags = MSG_DONTWAIT;
2921 
2922 		ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked,
2923 				      sendmsg_unlocked, sk, &msg, &kv, 1, slen);
2924 		if (ret <= 0)
2925 			goto error;
2926 
2927 		offset += ret;
2928 		len -= ret;
2929 	}
2930 
2931 	/* All the data was skb head? */
2932 	if (!len)
2933 		goto out;
2934 
2935 	/* Make offset relative to start of frags */
2936 	offset -= skb_headlen(skb);
2937 
2938 	/* Find where we are in frag list */
2939 	for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2940 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
2941 
2942 		if (offset < skb_frag_size(frag))
2943 			break;
2944 
2945 		offset -= skb_frag_size(frag);
2946 	}
2947 
2948 	for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2949 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
2950 
2951 		slen = min_t(size_t, len, skb_frag_size(frag) - offset);
2952 
2953 		while (slen) {
2954 			ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked,
2955 					      sendpage_unlocked, sk,
2956 					      skb_frag_page(frag),
2957 					      skb_frag_off(frag) + offset,
2958 					      slen, MSG_DONTWAIT);
2959 			if (ret <= 0)
2960 				goto error;
2961 
2962 			len -= ret;
2963 			offset += ret;
2964 			slen -= ret;
2965 		}
2966 
2967 		offset = 0;
2968 	}
2969 
2970 	if (len) {
2971 		/* Process any frag lists */
2972 
2973 		if (skb == head) {
2974 			if (skb_has_frag_list(skb)) {
2975 				skb = skb_shinfo(skb)->frag_list;
2976 				goto do_frag_list;
2977 			}
2978 		} else if (skb->next) {
2979 			skb = skb->next;
2980 			goto do_frag_list;
2981 		}
2982 	}
2983 
2984 out:
2985 	return orig_len - len;
2986 
2987 error:
2988 	return orig_len == len ? ret : orig_len - len;
2989 }
2990 
2991 /* Send skb data on a socket. Socket must be locked. */
2992 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2993 			 int len)
2994 {
2995 	return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked,
2996 			       kernel_sendpage_locked);
2997 }
2998 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2999 
3000 /* Send skb data on a socket. Socket must be unlocked. */
3001 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
3002 {
3003 	return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked,
3004 			       sendpage_unlocked);
3005 }
3006 
3007 /**
3008  *	skb_store_bits - store bits from kernel buffer to skb
3009  *	@skb: destination buffer
3010  *	@offset: offset in destination
3011  *	@from: source buffer
3012  *	@len: number of bytes to copy
3013  *
3014  *	Copy the specified number of bytes from the source buffer to the
3015  *	destination skb.  This function handles all the messy bits of
3016  *	traversing fragment lists and such.
3017  */
3018 
3019 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
3020 {
3021 	int start = skb_headlen(skb);
3022 	struct sk_buff *frag_iter;
3023 	int i, copy;
3024 
3025 	if (offset > (int)skb->len - len)
3026 		goto fault;
3027 
3028 	if ((copy = start - offset) > 0) {
3029 		if (copy > len)
3030 			copy = len;
3031 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
3032 		if ((len -= copy) == 0)
3033 			return 0;
3034 		offset += copy;
3035 		from += copy;
3036 	}
3037 
3038 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3039 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3040 		int end;
3041 
3042 		WARN_ON(start > offset + len);
3043 
3044 		end = start + skb_frag_size(frag);
3045 		if ((copy = end - offset) > 0) {
3046 			u32 p_off, p_len, copied;
3047 			struct page *p;
3048 			u8 *vaddr;
3049 
3050 			if (copy > len)
3051 				copy = len;
3052 
3053 			skb_frag_foreach_page(frag,
3054 					      skb_frag_off(frag) + offset - start,
3055 					      copy, p, p_off, p_len, copied) {
3056 				vaddr = kmap_atomic(p);
3057 				memcpy(vaddr + p_off, from + copied, p_len);
3058 				kunmap_atomic(vaddr);
3059 			}
3060 
3061 			if ((len -= copy) == 0)
3062 				return 0;
3063 			offset += copy;
3064 			from += copy;
3065 		}
3066 		start = end;
3067 	}
3068 
3069 	skb_walk_frags(skb, frag_iter) {
3070 		int end;
3071 
3072 		WARN_ON(start > offset + len);
3073 
3074 		end = start + frag_iter->len;
3075 		if ((copy = end - offset) > 0) {
3076 			if (copy > len)
3077 				copy = len;
3078 			if (skb_store_bits(frag_iter, offset - start,
3079 					   from, copy))
3080 				goto fault;
3081 			if ((len -= copy) == 0)
3082 				return 0;
3083 			offset += copy;
3084 			from += copy;
3085 		}
3086 		start = end;
3087 	}
3088 	if (!len)
3089 		return 0;
3090 
3091 fault:
3092 	return -EFAULT;
3093 }
3094 EXPORT_SYMBOL(skb_store_bits);
3095 
3096 /* Checksum skb data. */
3097 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
3098 		      __wsum csum, const struct skb_checksum_ops *ops)
3099 {
3100 	int start = skb_headlen(skb);
3101 	int i, copy = start - offset;
3102 	struct sk_buff *frag_iter;
3103 	int pos = 0;
3104 
3105 	/* Checksum header. */
3106 	if (copy > 0) {
3107 		if (copy > len)
3108 			copy = len;
3109 		csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
3110 				       skb->data + offset, copy, csum);
3111 		if ((len -= copy) == 0)
3112 			return csum;
3113 		offset += copy;
3114 		pos	= copy;
3115 	}
3116 
3117 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3118 		int end;
3119 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3120 
3121 		WARN_ON(start > offset + len);
3122 
3123 		end = start + skb_frag_size(frag);
3124 		if ((copy = end - offset) > 0) {
3125 			u32 p_off, p_len, copied;
3126 			struct page *p;
3127 			__wsum csum2;
3128 			u8 *vaddr;
3129 
3130 			if (copy > len)
3131 				copy = len;
3132 
3133 			skb_frag_foreach_page(frag,
3134 					      skb_frag_off(frag) + offset - start,
3135 					      copy, p, p_off, p_len, copied) {
3136 				vaddr = kmap_atomic(p);
3137 				csum2 = INDIRECT_CALL_1(ops->update,
3138 							csum_partial_ext,
3139 							vaddr + p_off, p_len, 0);
3140 				kunmap_atomic(vaddr);
3141 				csum = INDIRECT_CALL_1(ops->combine,
3142 						       csum_block_add_ext, csum,
3143 						       csum2, pos, p_len);
3144 				pos += p_len;
3145 			}
3146 
3147 			if (!(len -= copy))
3148 				return csum;
3149 			offset += copy;
3150 		}
3151 		start = end;
3152 	}
3153 
3154 	skb_walk_frags(skb, frag_iter) {
3155 		int end;
3156 
3157 		WARN_ON(start > offset + len);
3158 
3159 		end = start + frag_iter->len;
3160 		if ((copy = end - offset) > 0) {
3161 			__wsum csum2;
3162 			if (copy > len)
3163 				copy = len;
3164 			csum2 = __skb_checksum(frag_iter, offset - start,
3165 					       copy, 0, ops);
3166 			csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
3167 					       csum, csum2, pos, copy);
3168 			if ((len -= copy) == 0)
3169 				return csum;
3170 			offset += copy;
3171 			pos    += copy;
3172 		}
3173 		start = end;
3174 	}
3175 	BUG_ON(len);
3176 
3177 	return csum;
3178 }
3179 EXPORT_SYMBOL(__skb_checksum);
3180 
3181 __wsum skb_checksum(const struct sk_buff *skb, int offset,
3182 		    int len, __wsum csum)
3183 {
3184 	const struct skb_checksum_ops ops = {
3185 		.update  = csum_partial_ext,
3186 		.combine = csum_block_add_ext,
3187 	};
3188 
3189 	return __skb_checksum(skb, offset, len, csum, &ops);
3190 }
3191 EXPORT_SYMBOL(skb_checksum);
3192 
3193 /* Both of above in one bottle. */
3194 
3195 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
3196 				    u8 *to, int len)
3197 {
3198 	int start = skb_headlen(skb);
3199 	int i, copy = start - offset;
3200 	struct sk_buff *frag_iter;
3201 	int pos = 0;
3202 	__wsum csum = 0;
3203 
3204 	/* Copy header. */
3205 	if (copy > 0) {
3206 		if (copy > len)
3207 			copy = len;
3208 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
3209 						 copy);
3210 		if ((len -= copy) == 0)
3211 			return csum;
3212 		offset += copy;
3213 		to     += copy;
3214 		pos	= copy;
3215 	}
3216 
3217 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3218 		int end;
3219 
3220 		WARN_ON(start > offset + len);
3221 
3222 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3223 		if ((copy = end - offset) > 0) {
3224 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3225 			u32 p_off, p_len, copied;
3226 			struct page *p;
3227 			__wsum csum2;
3228 			u8 *vaddr;
3229 
3230 			if (copy > len)
3231 				copy = len;
3232 
3233 			skb_frag_foreach_page(frag,
3234 					      skb_frag_off(frag) + offset - start,
3235 					      copy, p, p_off, p_len, copied) {
3236 				vaddr = kmap_atomic(p);
3237 				csum2 = csum_partial_copy_nocheck(vaddr + p_off,
3238 								  to + copied,
3239 								  p_len);
3240 				kunmap_atomic(vaddr);
3241 				csum = csum_block_add(csum, csum2, pos);
3242 				pos += p_len;
3243 			}
3244 
3245 			if (!(len -= copy))
3246 				return csum;
3247 			offset += copy;
3248 			to     += copy;
3249 		}
3250 		start = end;
3251 	}
3252 
3253 	skb_walk_frags(skb, frag_iter) {
3254 		__wsum csum2;
3255 		int end;
3256 
3257 		WARN_ON(start > offset + len);
3258 
3259 		end = start + frag_iter->len;
3260 		if ((copy = end - offset) > 0) {
3261 			if (copy > len)
3262 				copy = len;
3263 			csum2 = skb_copy_and_csum_bits(frag_iter,
3264 						       offset - start,
3265 						       to, copy);
3266 			csum = csum_block_add(csum, csum2, pos);
3267 			if ((len -= copy) == 0)
3268 				return csum;
3269 			offset += copy;
3270 			to     += copy;
3271 			pos    += copy;
3272 		}
3273 		start = end;
3274 	}
3275 	BUG_ON(len);
3276 	return csum;
3277 }
3278 EXPORT_SYMBOL(skb_copy_and_csum_bits);
3279 
3280 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
3281 {
3282 	__sum16 sum;
3283 
3284 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
3285 	/* See comments in __skb_checksum_complete(). */
3286 	if (likely(!sum)) {
3287 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3288 		    !skb->csum_complete_sw)
3289 			netdev_rx_csum_fault(skb->dev, skb);
3290 	}
3291 	if (!skb_shared(skb))
3292 		skb->csum_valid = !sum;
3293 	return sum;
3294 }
3295 EXPORT_SYMBOL(__skb_checksum_complete_head);
3296 
3297 /* This function assumes skb->csum already holds pseudo header's checksum,
3298  * which has been changed from the hardware checksum, for example, by
3299  * __skb_checksum_validate_complete(). And, the original skb->csum must
3300  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
3301  *
3302  * It returns non-zero if the recomputed checksum is still invalid, otherwise
3303  * zero. The new checksum is stored back into skb->csum unless the skb is
3304  * shared.
3305  */
3306 __sum16 __skb_checksum_complete(struct sk_buff *skb)
3307 {
3308 	__wsum csum;
3309 	__sum16 sum;
3310 
3311 	csum = skb_checksum(skb, 0, skb->len, 0);
3312 
3313 	sum = csum_fold(csum_add(skb->csum, csum));
3314 	/* This check is inverted, because we already knew the hardware
3315 	 * checksum is invalid before calling this function. So, if the
3316 	 * re-computed checksum is valid instead, then we have a mismatch
3317 	 * between the original skb->csum and skb_checksum(). This means either
3318 	 * the original hardware checksum is incorrect or we screw up skb->csum
3319 	 * when moving skb->data around.
3320 	 */
3321 	if (likely(!sum)) {
3322 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
3323 		    !skb->csum_complete_sw)
3324 			netdev_rx_csum_fault(skb->dev, skb);
3325 	}
3326 
3327 	if (!skb_shared(skb)) {
3328 		/* Save full packet checksum */
3329 		skb->csum = csum;
3330 		skb->ip_summed = CHECKSUM_COMPLETE;
3331 		skb->csum_complete_sw = 1;
3332 		skb->csum_valid = !sum;
3333 	}
3334 
3335 	return sum;
3336 }
3337 EXPORT_SYMBOL(__skb_checksum_complete);
3338 
3339 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
3340 {
3341 	net_warn_ratelimited(
3342 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
3343 		__func__);
3344 	return 0;
3345 }
3346 
3347 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
3348 				       int offset, int len)
3349 {
3350 	net_warn_ratelimited(
3351 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
3352 		__func__);
3353 	return 0;
3354 }
3355 
3356 static const struct skb_checksum_ops default_crc32c_ops = {
3357 	.update  = warn_crc32c_csum_update,
3358 	.combine = warn_crc32c_csum_combine,
3359 };
3360 
3361 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
3362 	&default_crc32c_ops;
3363 EXPORT_SYMBOL(crc32c_csum_stub);
3364 
3365  /**
3366  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
3367  *	@from: source buffer
3368  *
3369  *	Calculates the amount of linear headroom needed in the 'to' skb passed
3370  *	into skb_zerocopy().
3371  */
3372 unsigned int
3373 skb_zerocopy_headlen(const struct sk_buff *from)
3374 {
3375 	unsigned int hlen = 0;
3376 
3377 	if (!from->head_frag ||
3378 	    skb_headlen(from) < L1_CACHE_BYTES ||
3379 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) {
3380 		hlen = skb_headlen(from);
3381 		if (!hlen)
3382 			hlen = from->len;
3383 	}
3384 
3385 	if (skb_has_frag_list(from))
3386 		hlen = from->len;
3387 
3388 	return hlen;
3389 }
3390 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3391 
3392 /**
3393  *	skb_zerocopy - Zero copy skb to skb
3394  *	@to: destination buffer
3395  *	@from: source buffer
3396  *	@len: number of bytes to copy from source buffer
3397  *	@hlen: size of linear headroom in destination buffer
3398  *
3399  *	Copies up to `len` bytes from `from` to `to` by creating references
3400  *	to the frags in the source buffer.
3401  *
3402  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3403  *	headroom in the `to` buffer.
3404  *
3405  *	Return value:
3406  *	0: everything is OK
3407  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
3408  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
3409  */
3410 int
3411 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
3412 {
3413 	int i, j = 0;
3414 	int plen = 0; /* length of skb->head fragment */
3415 	int ret;
3416 	struct page *page;
3417 	unsigned int offset;
3418 
3419 	BUG_ON(!from->head_frag && !hlen);
3420 
3421 	/* dont bother with small payloads */
3422 	if (len <= skb_tailroom(to))
3423 		return skb_copy_bits(from, 0, skb_put(to, len), len);
3424 
3425 	if (hlen) {
3426 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
3427 		if (unlikely(ret))
3428 			return ret;
3429 		len -= hlen;
3430 	} else {
3431 		plen = min_t(int, skb_headlen(from), len);
3432 		if (plen) {
3433 			page = virt_to_head_page(from->head);
3434 			offset = from->data - (unsigned char *)page_address(page);
3435 			__skb_fill_page_desc(to, 0, page, offset, plen);
3436 			get_page(page);
3437 			j = 1;
3438 			len -= plen;
3439 		}
3440 	}
3441 
3442 	skb_len_add(to, len + plen);
3443 
3444 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
3445 		skb_tx_error(from);
3446 		return -ENOMEM;
3447 	}
3448 	skb_zerocopy_clone(to, from, GFP_ATOMIC);
3449 
3450 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
3451 		int size;
3452 
3453 		if (!len)
3454 			break;
3455 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
3456 		size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3457 					len);
3458 		skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3459 		len -= size;
3460 		skb_frag_ref(to, j);
3461 		j++;
3462 	}
3463 	skb_shinfo(to)->nr_frags = j;
3464 
3465 	return 0;
3466 }
3467 EXPORT_SYMBOL_GPL(skb_zerocopy);
3468 
3469 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3470 {
3471 	__wsum csum;
3472 	long csstart;
3473 
3474 	if (skb->ip_summed == CHECKSUM_PARTIAL)
3475 		csstart = skb_checksum_start_offset(skb);
3476 	else
3477 		csstart = skb_headlen(skb);
3478 
3479 	BUG_ON(csstart > skb_headlen(skb));
3480 
3481 	skb_copy_from_linear_data(skb, to, csstart);
3482 
3483 	csum = 0;
3484 	if (csstart != skb->len)
3485 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
3486 					      skb->len - csstart);
3487 
3488 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3489 		long csstuff = csstart + skb->csum_offset;
3490 
3491 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
3492 	}
3493 }
3494 EXPORT_SYMBOL(skb_copy_and_csum_dev);
3495 
3496 /**
3497  *	skb_dequeue - remove from the head of the queue
3498  *	@list: list to dequeue from
3499  *
3500  *	Remove the head of the list. The list lock is taken so the function
3501  *	may be used safely with other locking list functions. The head item is
3502  *	returned or %NULL if the list is empty.
3503  */
3504 
3505 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3506 {
3507 	unsigned long flags;
3508 	struct sk_buff *result;
3509 
3510 	spin_lock_irqsave(&list->lock, flags);
3511 	result = __skb_dequeue(list);
3512 	spin_unlock_irqrestore(&list->lock, flags);
3513 	return result;
3514 }
3515 EXPORT_SYMBOL(skb_dequeue);
3516 
3517 /**
3518  *	skb_dequeue_tail - remove from the tail of the queue
3519  *	@list: list to dequeue from
3520  *
3521  *	Remove the tail of the list. The list lock is taken so the function
3522  *	may be used safely with other locking list functions. The tail item is
3523  *	returned or %NULL if the list is empty.
3524  */
3525 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3526 {
3527 	unsigned long flags;
3528 	struct sk_buff *result;
3529 
3530 	spin_lock_irqsave(&list->lock, flags);
3531 	result = __skb_dequeue_tail(list);
3532 	spin_unlock_irqrestore(&list->lock, flags);
3533 	return result;
3534 }
3535 EXPORT_SYMBOL(skb_dequeue_tail);
3536 
3537 /**
3538  *	skb_queue_purge - empty a list
3539  *	@list: list to empty
3540  *
3541  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
3542  *	the list and one reference dropped. This function takes the list
3543  *	lock and is atomic with respect to other list locking functions.
3544  */
3545 void skb_queue_purge(struct sk_buff_head *list)
3546 {
3547 	struct sk_buff *skb;
3548 	while ((skb = skb_dequeue(list)) != NULL)
3549 		kfree_skb(skb);
3550 }
3551 EXPORT_SYMBOL(skb_queue_purge);
3552 
3553 /**
3554  *	skb_rbtree_purge - empty a skb rbtree
3555  *	@root: root of the rbtree to empty
3556  *	Return value: the sum of truesizes of all purged skbs.
3557  *
3558  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3559  *	the list and one reference dropped. This function does not take
3560  *	any lock. Synchronization should be handled by the caller (e.g., TCP
3561  *	out-of-order queue is protected by the socket lock).
3562  */
3563 unsigned int skb_rbtree_purge(struct rb_root *root)
3564 {
3565 	struct rb_node *p = rb_first(root);
3566 	unsigned int sum = 0;
3567 
3568 	while (p) {
3569 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3570 
3571 		p = rb_next(p);
3572 		rb_erase(&skb->rbnode, root);
3573 		sum += skb->truesize;
3574 		kfree_skb(skb);
3575 	}
3576 	return sum;
3577 }
3578 
3579 /**
3580  *	skb_queue_head - queue a buffer at the list head
3581  *	@list: list to use
3582  *	@newsk: buffer to queue
3583  *
3584  *	Queue a buffer at the start of the list. This function takes the
3585  *	list lock and can be used safely with other locking &sk_buff functions
3586  *	safely.
3587  *
3588  *	A buffer cannot be placed on two lists at the same time.
3589  */
3590 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3591 {
3592 	unsigned long flags;
3593 
3594 	spin_lock_irqsave(&list->lock, flags);
3595 	__skb_queue_head(list, newsk);
3596 	spin_unlock_irqrestore(&list->lock, flags);
3597 }
3598 EXPORT_SYMBOL(skb_queue_head);
3599 
3600 /**
3601  *	skb_queue_tail - queue a buffer at the list tail
3602  *	@list: list to use
3603  *	@newsk: buffer to queue
3604  *
3605  *	Queue a buffer at the tail of the list. This function takes the
3606  *	list lock and can be used safely with other locking &sk_buff functions
3607  *	safely.
3608  *
3609  *	A buffer cannot be placed on two lists at the same time.
3610  */
3611 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3612 {
3613 	unsigned long flags;
3614 
3615 	spin_lock_irqsave(&list->lock, flags);
3616 	__skb_queue_tail(list, newsk);
3617 	spin_unlock_irqrestore(&list->lock, flags);
3618 }
3619 EXPORT_SYMBOL(skb_queue_tail);
3620 
3621 /**
3622  *	skb_unlink	-	remove a buffer from a list
3623  *	@skb: buffer to remove
3624  *	@list: list to use
3625  *
3626  *	Remove a packet from a list. The list locks are taken and this
3627  *	function is atomic with respect to other list locked calls
3628  *
3629  *	You must know what list the SKB is on.
3630  */
3631 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
3632 {
3633 	unsigned long flags;
3634 
3635 	spin_lock_irqsave(&list->lock, flags);
3636 	__skb_unlink(skb, list);
3637 	spin_unlock_irqrestore(&list->lock, flags);
3638 }
3639 EXPORT_SYMBOL(skb_unlink);
3640 
3641 /**
3642  *	skb_append	-	append a buffer
3643  *	@old: buffer to insert after
3644  *	@newsk: buffer to insert
3645  *	@list: list to use
3646  *
3647  *	Place a packet after a given packet in a list. The list locks are taken
3648  *	and this function is atomic with respect to other list locked calls.
3649  *	A buffer cannot be placed on two lists at the same time.
3650  */
3651 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3652 {
3653 	unsigned long flags;
3654 
3655 	spin_lock_irqsave(&list->lock, flags);
3656 	__skb_queue_after(list, old, newsk);
3657 	spin_unlock_irqrestore(&list->lock, flags);
3658 }
3659 EXPORT_SYMBOL(skb_append);
3660 
3661 static inline void skb_split_inside_header(struct sk_buff *skb,
3662 					   struct sk_buff* skb1,
3663 					   const u32 len, const int pos)
3664 {
3665 	int i;
3666 
3667 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3668 					 pos - len);
3669 	/* And move data appendix as is. */
3670 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3671 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3672 
3673 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3674 	skb_shinfo(skb)->nr_frags  = 0;
3675 	skb1->data_len		   = skb->data_len;
3676 	skb1->len		   += skb1->data_len;
3677 	skb->data_len		   = 0;
3678 	skb->len		   = len;
3679 	skb_set_tail_pointer(skb, len);
3680 }
3681 
3682 static inline void skb_split_no_header(struct sk_buff *skb,
3683 				       struct sk_buff* skb1,
3684 				       const u32 len, int pos)
3685 {
3686 	int i, k = 0;
3687 	const int nfrags = skb_shinfo(skb)->nr_frags;
3688 
3689 	skb_shinfo(skb)->nr_frags = 0;
3690 	skb1->len		  = skb1->data_len = skb->len - len;
3691 	skb->len		  = len;
3692 	skb->data_len		  = len - pos;
3693 
3694 	for (i = 0; i < nfrags; i++) {
3695 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3696 
3697 		if (pos + size > len) {
3698 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3699 
3700 			if (pos < len) {
3701 				/* Split frag.
3702 				 * We have two variants in this case:
3703 				 * 1. Move all the frag to the second
3704 				 *    part, if it is possible. F.e.
3705 				 *    this approach is mandatory for TUX,
3706 				 *    where splitting is expensive.
3707 				 * 2. Split is accurately. We make this.
3708 				 */
3709 				skb_frag_ref(skb, i);
3710 				skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
3711 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3712 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3713 				skb_shinfo(skb)->nr_frags++;
3714 			}
3715 			k++;
3716 		} else
3717 			skb_shinfo(skb)->nr_frags++;
3718 		pos += size;
3719 	}
3720 	skb_shinfo(skb1)->nr_frags = k;
3721 }
3722 
3723 /**
3724  * skb_split - Split fragmented skb to two parts at length len.
3725  * @skb: the buffer to split
3726  * @skb1: the buffer to receive the second part
3727  * @len: new length for skb
3728  */
3729 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3730 {
3731 	int pos = skb_headlen(skb);
3732 	const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY;
3733 
3734 	skb_zcopy_downgrade_managed(skb);
3735 
3736 	skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags;
3737 	skb_zerocopy_clone(skb1, skb, 0);
3738 	if (len < pos)	/* Split line is inside header. */
3739 		skb_split_inside_header(skb, skb1, len, pos);
3740 	else		/* Second chunk has no header, nothing to copy. */
3741 		skb_split_no_header(skb, skb1, len, pos);
3742 }
3743 EXPORT_SYMBOL(skb_split);
3744 
3745 /* Shifting from/to a cloned skb is a no-go.
3746  *
3747  * Caller cannot keep skb_shinfo related pointers past calling here!
3748  */
3749 static int skb_prepare_for_shift(struct sk_buff *skb)
3750 {
3751 	return skb_unclone_keeptruesize(skb, GFP_ATOMIC);
3752 }
3753 
3754 /**
3755  * skb_shift - Shifts paged data partially from skb to another
3756  * @tgt: buffer into which tail data gets added
3757  * @skb: buffer from which the paged data comes from
3758  * @shiftlen: shift up to this many bytes
3759  *
3760  * Attempts to shift up to shiftlen worth of bytes, which may be less than
3761  * the length of the skb, from skb to tgt. Returns number bytes shifted.
3762  * It's up to caller to free skb if everything was shifted.
3763  *
3764  * If @tgt runs out of frags, the whole operation is aborted.
3765  *
3766  * Skb cannot include anything else but paged data while tgt is allowed
3767  * to have non-paged data as well.
3768  *
3769  * TODO: full sized shift could be optimized but that would need
3770  * specialized skb free'er to handle frags without up-to-date nr_frags.
3771  */
3772 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3773 {
3774 	int from, to, merge, todo;
3775 	skb_frag_t *fragfrom, *fragto;
3776 
3777 	BUG_ON(shiftlen > skb->len);
3778 
3779 	if (skb_headlen(skb))
3780 		return 0;
3781 	if (skb_zcopy(tgt) || skb_zcopy(skb))
3782 		return 0;
3783 
3784 	todo = shiftlen;
3785 	from = 0;
3786 	to = skb_shinfo(tgt)->nr_frags;
3787 	fragfrom = &skb_shinfo(skb)->frags[from];
3788 
3789 	/* Actual merge is delayed until the point when we know we can
3790 	 * commit all, so that we don't have to undo partial changes
3791 	 */
3792 	if (!to ||
3793 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3794 			      skb_frag_off(fragfrom))) {
3795 		merge = -1;
3796 	} else {
3797 		merge = to - 1;
3798 
3799 		todo -= skb_frag_size(fragfrom);
3800 		if (todo < 0) {
3801 			if (skb_prepare_for_shift(skb) ||
3802 			    skb_prepare_for_shift(tgt))
3803 				return 0;
3804 
3805 			/* All previous frag pointers might be stale! */
3806 			fragfrom = &skb_shinfo(skb)->frags[from];
3807 			fragto = &skb_shinfo(tgt)->frags[merge];
3808 
3809 			skb_frag_size_add(fragto, shiftlen);
3810 			skb_frag_size_sub(fragfrom, shiftlen);
3811 			skb_frag_off_add(fragfrom, shiftlen);
3812 
3813 			goto onlymerged;
3814 		}
3815 
3816 		from++;
3817 	}
3818 
3819 	/* Skip full, not-fitting skb to avoid expensive operations */
3820 	if ((shiftlen == skb->len) &&
3821 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3822 		return 0;
3823 
3824 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3825 		return 0;
3826 
3827 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3828 		if (to == MAX_SKB_FRAGS)
3829 			return 0;
3830 
3831 		fragfrom = &skb_shinfo(skb)->frags[from];
3832 		fragto = &skb_shinfo(tgt)->frags[to];
3833 
3834 		if (todo >= skb_frag_size(fragfrom)) {
3835 			*fragto = *fragfrom;
3836 			todo -= skb_frag_size(fragfrom);
3837 			from++;
3838 			to++;
3839 
3840 		} else {
3841 			__skb_frag_ref(fragfrom);
3842 			skb_frag_page_copy(fragto, fragfrom);
3843 			skb_frag_off_copy(fragto, fragfrom);
3844 			skb_frag_size_set(fragto, todo);
3845 
3846 			skb_frag_off_add(fragfrom, todo);
3847 			skb_frag_size_sub(fragfrom, todo);
3848 			todo = 0;
3849 
3850 			to++;
3851 			break;
3852 		}
3853 	}
3854 
3855 	/* Ready to "commit" this state change to tgt */
3856 	skb_shinfo(tgt)->nr_frags = to;
3857 
3858 	if (merge >= 0) {
3859 		fragfrom = &skb_shinfo(skb)->frags[0];
3860 		fragto = &skb_shinfo(tgt)->frags[merge];
3861 
3862 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3863 		__skb_frag_unref(fragfrom, skb->pp_recycle);
3864 	}
3865 
3866 	/* Reposition in the original skb */
3867 	to = 0;
3868 	while (from < skb_shinfo(skb)->nr_frags)
3869 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3870 	skb_shinfo(skb)->nr_frags = to;
3871 
3872 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3873 
3874 onlymerged:
3875 	/* Most likely the tgt won't ever need its checksum anymore, skb on
3876 	 * the other hand might need it if it needs to be resent
3877 	 */
3878 	tgt->ip_summed = CHECKSUM_PARTIAL;
3879 	skb->ip_summed = CHECKSUM_PARTIAL;
3880 
3881 	skb_len_add(skb, -shiftlen);
3882 	skb_len_add(tgt, shiftlen);
3883 
3884 	return shiftlen;
3885 }
3886 
3887 /**
3888  * skb_prepare_seq_read - Prepare a sequential read of skb data
3889  * @skb: the buffer to read
3890  * @from: lower offset of data to be read
3891  * @to: upper offset of data to be read
3892  * @st: state variable
3893  *
3894  * Initializes the specified state variable. Must be called before
3895  * invoking skb_seq_read() for the first time.
3896  */
3897 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3898 			  unsigned int to, struct skb_seq_state *st)
3899 {
3900 	st->lower_offset = from;
3901 	st->upper_offset = to;
3902 	st->root_skb = st->cur_skb = skb;
3903 	st->frag_idx = st->stepped_offset = 0;
3904 	st->frag_data = NULL;
3905 	st->frag_off = 0;
3906 }
3907 EXPORT_SYMBOL(skb_prepare_seq_read);
3908 
3909 /**
3910  * skb_seq_read - Sequentially read skb data
3911  * @consumed: number of bytes consumed by the caller so far
3912  * @data: destination pointer for data to be returned
3913  * @st: state variable
3914  *
3915  * Reads a block of skb data at @consumed relative to the
3916  * lower offset specified to skb_prepare_seq_read(). Assigns
3917  * the head of the data block to @data and returns the length
3918  * of the block or 0 if the end of the skb data or the upper
3919  * offset has been reached.
3920  *
3921  * The caller is not required to consume all of the data
3922  * returned, i.e. @consumed is typically set to the number
3923  * of bytes already consumed and the next call to
3924  * skb_seq_read() will return the remaining part of the block.
3925  *
3926  * Note 1: The size of each block of data returned can be arbitrary,
3927  *       this limitation is the cost for zerocopy sequential
3928  *       reads of potentially non linear data.
3929  *
3930  * Note 2: Fragment lists within fragments are not implemented
3931  *       at the moment, state->root_skb could be replaced with
3932  *       a stack for this purpose.
3933  */
3934 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3935 			  struct skb_seq_state *st)
3936 {
3937 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3938 	skb_frag_t *frag;
3939 
3940 	if (unlikely(abs_offset >= st->upper_offset)) {
3941 		if (st->frag_data) {
3942 			kunmap_atomic(st->frag_data);
3943 			st->frag_data = NULL;
3944 		}
3945 		return 0;
3946 	}
3947 
3948 next_skb:
3949 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3950 
3951 	if (abs_offset < block_limit && !st->frag_data) {
3952 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3953 		return block_limit - abs_offset;
3954 	}
3955 
3956 	if (st->frag_idx == 0 && !st->frag_data)
3957 		st->stepped_offset += skb_headlen(st->cur_skb);
3958 
3959 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3960 		unsigned int pg_idx, pg_off, pg_sz;
3961 
3962 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3963 
3964 		pg_idx = 0;
3965 		pg_off = skb_frag_off(frag);
3966 		pg_sz = skb_frag_size(frag);
3967 
3968 		if (skb_frag_must_loop(skb_frag_page(frag))) {
3969 			pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
3970 			pg_off = offset_in_page(pg_off + st->frag_off);
3971 			pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
3972 						    PAGE_SIZE - pg_off);
3973 		}
3974 
3975 		block_limit = pg_sz + st->stepped_offset;
3976 		if (abs_offset < block_limit) {
3977 			if (!st->frag_data)
3978 				st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
3979 
3980 			*data = (u8 *)st->frag_data + pg_off +
3981 				(abs_offset - st->stepped_offset);
3982 
3983 			return block_limit - abs_offset;
3984 		}
3985 
3986 		if (st->frag_data) {
3987 			kunmap_atomic(st->frag_data);
3988 			st->frag_data = NULL;
3989 		}
3990 
3991 		st->stepped_offset += pg_sz;
3992 		st->frag_off += pg_sz;
3993 		if (st->frag_off == skb_frag_size(frag)) {
3994 			st->frag_off = 0;
3995 			st->frag_idx++;
3996 		}
3997 	}
3998 
3999 	if (st->frag_data) {
4000 		kunmap_atomic(st->frag_data);
4001 		st->frag_data = NULL;
4002 	}
4003 
4004 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
4005 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
4006 		st->frag_idx = 0;
4007 		goto next_skb;
4008 	} else if (st->cur_skb->next) {
4009 		st->cur_skb = st->cur_skb->next;
4010 		st->frag_idx = 0;
4011 		goto next_skb;
4012 	}
4013 
4014 	return 0;
4015 }
4016 EXPORT_SYMBOL(skb_seq_read);
4017 
4018 /**
4019  * skb_abort_seq_read - Abort a sequential read of skb data
4020  * @st: state variable
4021  *
4022  * Must be called if skb_seq_read() was not called until it
4023  * returned 0.
4024  */
4025 void skb_abort_seq_read(struct skb_seq_state *st)
4026 {
4027 	if (st->frag_data)
4028 		kunmap_atomic(st->frag_data);
4029 }
4030 EXPORT_SYMBOL(skb_abort_seq_read);
4031 
4032 #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
4033 
4034 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
4035 					  struct ts_config *conf,
4036 					  struct ts_state *state)
4037 {
4038 	return skb_seq_read(offset, text, TS_SKB_CB(state));
4039 }
4040 
4041 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
4042 {
4043 	skb_abort_seq_read(TS_SKB_CB(state));
4044 }
4045 
4046 /**
4047  * skb_find_text - Find a text pattern in skb data
4048  * @skb: the buffer to look in
4049  * @from: search offset
4050  * @to: search limit
4051  * @config: textsearch configuration
4052  *
4053  * Finds a pattern in the skb data according to the specified
4054  * textsearch configuration. Use textsearch_next() to retrieve
4055  * subsequent occurrences of the pattern. Returns the offset
4056  * to the first occurrence or UINT_MAX if no match was found.
4057  */
4058 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
4059 			   unsigned int to, struct ts_config *config)
4060 {
4061 	struct ts_state state;
4062 	unsigned int ret;
4063 
4064 	BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
4065 
4066 	config->get_next_block = skb_ts_get_next_block;
4067 	config->finish = skb_ts_finish;
4068 
4069 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
4070 
4071 	ret = textsearch_find(config, &state);
4072 	return (ret <= to - from ? ret : UINT_MAX);
4073 }
4074 EXPORT_SYMBOL(skb_find_text);
4075 
4076 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
4077 			 int offset, size_t size)
4078 {
4079 	int i = skb_shinfo(skb)->nr_frags;
4080 
4081 	if (skb_can_coalesce(skb, i, page, offset)) {
4082 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
4083 	} else if (i < MAX_SKB_FRAGS) {
4084 		skb_zcopy_downgrade_managed(skb);
4085 		get_page(page);
4086 		skb_fill_page_desc_noacc(skb, i, page, offset, size);
4087 	} else {
4088 		return -EMSGSIZE;
4089 	}
4090 
4091 	return 0;
4092 }
4093 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
4094 
4095 /**
4096  *	skb_pull_rcsum - pull skb and update receive checksum
4097  *	@skb: buffer to update
4098  *	@len: length of data pulled
4099  *
4100  *	This function performs an skb_pull on the packet and updates
4101  *	the CHECKSUM_COMPLETE checksum.  It should be used on
4102  *	receive path processing instead of skb_pull unless you know
4103  *	that the checksum difference is zero (e.g., a valid IP header)
4104  *	or you are setting ip_summed to CHECKSUM_NONE.
4105  */
4106 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
4107 {
4108 	unsigned char *data = skb->data;
4109 
4110 	BUG_ON(len > skb->len);
4111 	__skb_pull(skb, len);
4112 	skb_postpull_rcsum(skb, data, len);
4113 	return skb->data;
4114 }
4115 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
4116 
4117 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
4118 {
4119 	skb_frag_t head_frag;
4120 	struct page *page;
4121 
4122 	page = virt_to_head_page(frag_skb->head);
4123 	__skb_frag_set_page(&head_frag, page);
4124 	skb_frag_off_set(&head_frag, frag_skb->data -
4125 			 (unsigned char *)page_address(page));
4126 	skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
4127 	return head_frag;
4128 }
4129 
4130 struct sk_buff *skb_segment_list(struct sk_buff *skb,
4131 				 netdev_features_t features,
4132 				 unsigned int offset)
4133 {
4134 	struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
4135 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
4136 	unsigned int delta_truesize = 0;
4137 	unsigned int delta_len = 0;
4138 	struct sk_buff *tail = NULL;
4139 	struct sk_buff *nskb, *tmp;
4140 	int len_diff, err;
4141 
4142 	skb_push(skb, -skb_network_offset(skb) + offset);
4143 
4144 	skb_shinfo(skb)->frag_list = NULL;
4145 
4146 	do {
4147 		nskb = list_skb;
4148 		list_skb = list_skb->next;
4149 
4150 		err = 0;
4151 		delta_truesize += nskb->truesize;
4152 		if (skb_shared(nskb)) {
4153 			tmp = skb_clone(nskb, GFP_ATOMIC);
4154 			if (tmp) {
4155 				consume_skb(nskb);
4156 				nskb = tmp;
4157 				err = skb_unclone(nskb, GFP_ATOMIC);
4158 			} else {
4159 				err = -ENOMEM;
4160 			}
4161 		}
4162 
4163 		if (!tail)
4164 			skb->next = nskb;
4165 		else
4166 			tail->next = nskb;
4167 
4168 		if (unlikely(err)) {
4169 			nskb->next = list_skb;
4170 			goto err_linearize;
4171 		}
4172 
4173 		tail = nskb;
4174 
4175 		delta_len += nskb->len;
4176 
4177 		skb_push(nskb, -skb_network_offset(nskb) + offset);
4178 
4179 		skb_release_head_state(nskb);
4180 		len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb);
4181 		__copy_skb_header(nskb, skb);
4182 
4183 		skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
4184 		nskb->transport_header += len_diff;
4185 		skb_copy_from_linear_data_offset(skb, -tnl_hlen,
4186 						 nskb->data - tnl_hlen,
4187 						 offset + tnl_hlen);
4188 
4189 		if (skb_needs_linearize(nskb, features) &&
4190 		    __skb_linearize(nskb))
4191 			goto err_linearize;
4192 
4193 	} while (list_skb);
4194 
4195 	skb->truesize = skb->truesize - delta_truesize;
4196 	skb->data_len = skb->data_len - delta_len;
4197 	skb->len = skb->len - delta_len;
4198 
4199 	skb_gso_reset(skb);
4200 
4201 	skb->prev = tail;
4202 
4203 	if (skb_needs_linearize(skb, features) &&
4204 	    __skb_linearize(skb))
4205 		goto err_linearize;
4206 
4207 	skb_get(skb);
4208 
4209 	return skb;
4210 
4211 err_linearize:
4212 	kfree_skb_list(skb->next);
4213 	skb->next = NULL;
4214 	return ERR_PTR(-ENOMEM);
4215 }
4216 EXPORT_SYMBOL_GPL(skb_segment_list);
4217 
4218 /**
4219  *	skb_segment - Perform protocol segmentation on skb.
4220  *	@head_skb: buffer to segment
4221  *	@features: features for the output path (see dev->features)
4222  *
4223  *	This function performs segmentation on the given skb.  It returns
4224  *	a pointer to the first in a list of new skbs for the segments.
4225  *	In case of error it returns ERR_PTR(err).
4226  */
4227 struct sk_buff *skb_segment(struct sk_buff *head_skb,
4228 			    netdev_features_t features)
4229 {
4230 	struct sk_buff *segs = NULL;
4231 	struct sk_buff *tail = NULL;
4232 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
4233 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
4234 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
4235 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
4236 	struct sk_buff *frag_skb = head_skb;
4237 	unsigned int offset = doffset;
4238 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
4239 	unsigned int partial_segs = 0;
4240 	unsigned int headroom;
4241 	unsigned int len = head_skb->len;
4242 	__be16 proto;
4243 	bool csum, sg;
4244 	int nfrags = skb_shinfo(head_skb)->nr_frags;
4245 	int err = -ENOMEM;
4246 	int i = 0;
4247 	int pos;
4248 
4249 	if ((skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY) &&
4250 	    mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
4251 		struct sk_buff *check_skb;
4252 
4253 		for (check_skb = list_skb; check_skb; check_skb = check_skb->next) {
4254 			if (skb_headlen(check_skb) && !check_skb->head_frag) {
4255 				/* gso_size is untrusted, and we have a frag_list with
4256 				 * a linear non head_frag item.
4257 				 *
4258 				 * If head_skb's headlen does not fit requested gso_size,
4259 				 * it means that the frag_list members do NOT terminate
4260 				 * on exact gso_size boundaries. Hence we cannot perform
4261 				 * skb_frag_t page sharing. Therefore we must fallback to
4262 				 * copying the frag_list skbs; we do so by disabling SG.
4263 				 */
4264 				features &= ~NETIF_F_SG;
4265 				break;
4266 			}
4267 		}
4268 	}
4269 
4270 	__skb_push(head_skb, doffset);
4271 	proto = skb_network_protocol(head_skb, NULL);
4272 	if (unlikely(!proto))
4273 		return ERR_PTR(-EINVAL);
4274 
4275 	sg = !!(features & NETIF_F_SG);
4276 	csum = !!can_checksum_protocol(features, proto);
4277 
4278 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
4279 		if (!(features & NETIF_F_GSO_PARTIAL)) {
4280 			struct sk_buff *iter;
4281 			unsigned int frag_len;
4282 
4283 			if (!list_skb ||
4284 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
4285 				goto normal;
4286 
4287 			/* If we get here then all the required
4288 			 * GSO features except frag_list are supported.
4289 			 * Try to split the SKB to multiple GSO SKBs
4290 			 * with no frag_list.
4291 			 * Currently we can do that only when the buffers don't
4292 			 * have a linear part and all the buffers except
4293 			 * the last are of the same length.
4294 			 */
4295 			frag_len = list_skb->len;
4296 			skb_walk_frags(head_skb, iter) {
4297 				if (frag_len != iter->len && iter->next)
4298 					goto normal;
4299 				if (skb_headlen(iter) && !iter->head_frag)
4300 					goto normal;
4301 
4302 				len -= iter->len;
4303 			}
4304 
4305 			if (len != frag_len)
4306 				goto normal;
4307 		}
4308 
4309 		/* GSO partial only requires that we trim off any excess that
4310 		 * doesn't fit into an MSS sized block, so take care of that
4311 		 * now.
4312 		 */
4313 		partial_segs = len / mss;
4314 		if (partial_segs > 1)
4315 			mss *= partial_segs;
4316 		else
4317 			partial_segs = 0;
4318 	}
4319 
4320 normal:
4321 	headroom = skb_headroom(head_skb);
4322 	pos = skb_headlen(head_skb);
4323 
4324 	do {
4325 		struct sk_buff *nskb;
4326 		skb_frag_t *nskb_frag;
4327 		int hsize;
4328 		int size;
4329 
4330 		if (unlikely(mss == GSO_BY_FRAGS)) {
4331 			len = list_skb->len;
4332 		} else {
4333 			len = head_skb->len - offset;
4334 			if (len > mss)
4335 				len = mss;
4336 		}
4337 
4338 		hsize = skb_headlen(head_skb) - offset;
4339 
4340 		if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
4341 		    (skb_headlen(list_skb) == len || sg)) {
4342 			BUG_ON(skb_headlen(list_skb) > len);
4343 
4344 			i = 0;
4345 			nfrags = skb_shinfo(list_skb)->nr_frags;
4346 			frag = skb_shinfo(list_skb)->frags;
4347 			frag_skb = list_skb;
4348 			pos += skb_headlen(list_skb);
4349 
4350 			while (pos < offset + len) {
4351 				BUG_ON(i >= nfrags);
4352 
4353 				size = skb_frag_size(frag);
4354 				if (pos + size > offset + len)
4355 					break;
4356 
4357 				i++;
4358 				pos += size;
4359 				frag++;
4360 			}
4361 
4362 			nskb = skb_clone(list_skb, GFP_ATOMIC);
4363 			list_skb = list_skb->next;
4364 
4365 			if (unlikely(!nskb))
4366 				goto err;
4367 
4368 			if (unlikely(pskb_trim(nskb, len))) {
4369 				kfree_skb(nskb);
4370 				goto err;
4371 			}
4372 
4373 			hsize = skb_end_offset(nskb);
4374 			if (skb_cow_head(nskb, doffset + headroom)) {
4375 				kfree_skb(nskb);
4376 				goto err;
4377 			}
4378 
4379 			nskb->truesize += skb_end_offset(nskb) - hsize;
4380 			skb_release_head_state(nskb);
4381 			__skb_push(nskb, doffset);
4382 		} else {
4383 			if (hsize < 0)
4384 				hsize = 0;
4385 			if (hsize > len || !sg)
4386 				hsize = len;
4387 
4388 			nskb = __alloc_skb(hsize + doffset + headroom,
4389 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
4390 					   NUMA_NO_NODE);
4391 
4392 			if (unlikely(!nskb))
4393 				goto err;
4394 
4395 			skb_reserve(nskb, headroom);
4396 			__skb_put(nskb, doffset);
4397 		}
4398 
4399 		if (segs)
4400 			tail->next = nskb;
4401 		else
4402 			segs = nskb;
4403 		tail = nskb;
4404 
4405 		__copy_skb_header(nskb, head_skb);
4406 
4407 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
4408 		skb_reset_mac_len(nskb);
4409 
4410 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
4411 						 nskb->data - tnl_hlen,
4412 						 doffset + tnl_hlen);
4413 
4414 		if (nskb->len == len + doffset)
4415 			goto perform_csum_check;
4416 
4417 		if (!sg) {
4418 			if (!csum) {
4419 				if (!nskb->remcsum_offload)
4420 					nskb->ip_summed = CHECKSUM_NONE;
4421 				SKB_GSO_CB(nskb)->csum =
4422 					skb_copy_and_csum_bits(head_skb, offset,
4423 							       skb_put(nskb,
4424 								       len),
4425 							       len);
4426 				SKB_GSO_CB(nskb)->csum_start =
4427 					skb_headroom(nskb) + doffset;
4428 			} else {
4429 				if (skb_copy_bits(head_skb, offset, skb_put(nskb, len), len))
4430 					goto err;
4431 			}
4432 			continue;
4433 		}
4434 
4435 		nskb_frag = skb_shinfo(nskb)->frags;
4436 
4437 		skb_copy_from_linear_data_offset(head_skb, offset,
4438 						 skb_put(nskb, hsize), hsize);
4439 
4440 		skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4441 					   SKBFL_SHARED_FRAG;
4442 
4443 		if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4444 		    skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4445 			goto err;
4446 
4447 		while (pos < offset + len) {
4448 			if (i >= nfrags) {
4449 				i = 0;
4450 				nfrags = skb_shinfo(list_skb)->nr_frags;
4451 				frag = skb_shinfo(list_skb)->frags;
4452 				frag_skb = list_skb;
4453 				if (!skb_headlen(list_skb)) {
4454 					BUG_ON(!nfrags);
4455 				} else {
4456 					BUG_ON(!list_skb->head_frag);
4457 
4458 					/* to make room for head_frag. */
4459 					i--;
4460 					frag--;
4461 				}
4462 				if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4463 				    skb_zerocopy_clone(nskb, frag_skb,
4464 						       GFP_ATOMIC))
4465 					goto err;
4466 
4467 				list_skb = list_skb->next;
4468 			}
4469 
4470 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
4471 				     MAX_SKB_FRAGS)) {
4472 				net_warn_ratelimited(
4473 					"skb_segment: too many frags: %u %u\n",
4474 					pos, mss);
4475 				err = -EINVAL;
4476 				goto err;
4477 			}
4478 
4479 			*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
4480 			__skb_frag_ref(nskb_frag);
4481 			size = skb_frag_size(nskb_frag);
4482 
4483 			if (pos < offset) {
4484 				skb_frag_off_add(nskb_frag, offset - pos);
4485 				skb_frag_size_sub(nskb_frag, offset - pos);
4486 			}
4487 
4488 			skb_shinfo(nskb)->nr_frags++;
4489 
4490 			if (pos + size <= offset + len) {
4491 				i++;
4492 				frag++;
4493 				pos += size;
4494 			} else {
4495 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
4496 				goto skip_fraglist;
4497 			}
4498 
4499 			nskb_frag++;
4500 		}
4501 
4502 skip_fraglist:
4503 		nskb->data_len = len - hsize;
4504 		nskb->len += nskb->data_len;
4505 		nskb->truesize += nskb->data_len;
4506 
4507 perform_csum_check:
4508 		if (!csum) {
4509 			if (skb_has_shared_frag(nskb) &&
4510 			    __skb_linearize(nskb))
4511 				goto err;
4512 
4513 			if (!nskb->remcsum_offload)
4514 				nskb->ip_summed = CHECKSUM_NONE;
4515 			SKB_GSO_CB(nskb)->csum =
4516 				skb_checksum(nskb, doffset,
4517 					     nskb->len - doffset, 0);
4518 			SKB_GSO_CB(nskb)->csum_start =
4519 				skb_headroom(nskb) + doffset;
4520 		}
4521 	} while ((offset += len) < head_skb->len);
4522 
4523 	/* Some callers want to get the end of the list.
4524 	 * Put it in segs->prev to avoid walking the list.
4525 	 * (see validate_xmit_skb_list() for example)
4526 	 */
4527 	segs->prev = tail;
4528 
4529 	if (partial_segs) {
4530 		struct sk_buff *iter;
4531 		int type = skb_shinfo(head_skb)->gso_type;
4532 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4533 
4534 		/* Update type to add partial and then remove dodgy if set */
4535 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4536 		type &= ~SKB_GSO_DODGY;
4537 
4538 		/* Update GSO info and prepare to start updating headers on
4539 		 * our way back down the stack of protocols.
4540 		 */
4541 		for (iter = segs; iter; iter = iter->next) {
4542 			skb_shinfo(iter)->gso_size = gso_size;
4543 			skb_shinfo(iter)->gso_segs = partial_segs;
4544 			skb_shinfo(iter)->gso_type = type;
4545 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4546 		}
4547 
4548 		if (tail->len - doffset <= gso_size)
4549 			skb_shinfo(tail)->gso_size = 0;
4550 		else if (tail != segs)
4551 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4552 	}
4553 
4554 	/* Following permits correct backpressure, for protocols
4555 	 * using skb_set_owner_w().
4556 	 * Idea is to tranfert ownership from head_skb to last segment.
4557 	 */
4558 	if (head_skb->destructor == sock_wfree) {
4559 		swap(tail->truesize, head_skb->truesize);
4560 		swap(tail->destructor, head_skb->destructor);
4561 		swap(tail->sk, head_skb->sk);
4562 	}
4563 	return segs;
4564 
4565 err:
4566 	kfree_skb_list(segs);
4567 	return ERR_PTR(err);
4568 }
4569 EXPORT_SYMBOL_GPL(skb_segment);
4570 
4571 #ifdef CONFIG_SKB_EXTENSIONS
4572 #define SKB_EXT_ALIGN_VALUE	8
4573 #define SKB_EXT_CHUNKSIZEOF(x)	(ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4574 
4575 static const u8 skb_ext_type_len[] = {
4576 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4577 	[SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4578 #endif
4579 #ifdef CONFIG_XFRM
4580 	[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4581 #endif
4582 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4583 	[TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4584 #endif
4585 #if IS_ENABLED(CONFIG_MPTCP)
4586 	[SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4587 #endif
4588 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
4589 	[SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow),
4590 #endif
4591 };
4592 
4593 static __always_inline unsigned int skb_ext_total_length(void)
4594 {
4595 	return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4596 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4597 		skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4598 #endif
4599 #ifdef CONFIG_XFRM
4600 		skb_ext_type_len[SKB_EXT_SEC_PATH] +
4601 #endif
4602 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4603 		skb_ext_type_len[TC_SKB_EXT] +
4604 #endif
4605 #if IS_ENABLED(CONFIG_MPTCP)
4606 		skb_ext_type_len[SKB_EXT_MPTCP] +
4607 #endif
4608 #if IS_ENABLED(CONFIG_MCTP_FLOWS)
4609 		skb_ext_type_len[SKB_EXT_MCTP] +
4610 #endif
4611 		0;
4612 }
4613 
4614 static void skb_extensions_init(void)
4615 {
4616 	BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4617 	BUILD_BUG_ON(skb_ext_total_length() > 255);
4618 
4619 	skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4620 					     SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4621 					     0,
4622 					     SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4623 					     NULL);
4624 }
4625 #else
4626 static void skb_extensions_init(void) {}
4627 #endif
4628 
4629 void __init skb_init(void)
4630 {
4631 	skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
4632 					      sizeof(struct sk_buff),
4633 					      0,
4634 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4635 					      offsetof(struct sk_buff, cb),
4636 					      sizeof_field(struct sk_buff, cb),
4637 					      NULL);
4638 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4639 						sizeof(struct sk_buff_fclones),
4640 						0,
4641 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4642 						NULL);
4643 	skb_extensions_init();
4644 }
4645 
4646 static int
4647 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4648 	       unsigned int recursion_level)
4649 {
4650 	int start = skb_headlen(skb);
4651 	int i, copy = start - offset;
4652 	struct sk_buff *frag_iter;
4653 	int elt = 0;
4654 
4655 	if (unlikely(recursion_level >= 24))
4656 		return -EMSGSIZE;
4657 
4658 	if (copy > 0) {
4659 		if (copy > len)
4660 			copy = len;
4661 		sg_set_buf(sg, skb->data + offset, copy);
4662 		elt++;
4663 		if ((len -= copy) == 0)
4664 			return elt;
4665 		offset += copy;
4666 	}
4667 
4668 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4669 		int end;
4670 
4671 		WARN_ON(start > offset + len);
4672 
4673 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4674 		if ((copy = end - offset) > 0) {
4675 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4676 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4677 				return -EMSGSIZE;
4678 
4679 			if (copy > len)
4680 				copy = len;
4681 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4682 				    skb_frag_off(frag) + offset - start);
4683 			elt++;
4684 			if (!(len -= copy))
4685 				return elt;
4686 			offset += copy;
4687 		}
4688 		start = end;
4689 	}
4690 
4691 	skb_walk_frags(skb, frag_iter) {
4692 		int end, ret;
4693 
4694 		WARN_ON(start > offset + len);
4695 
4696 		end = start + frag_iter->len;
4697 		if ((copy = end - offset) > 0) {
4698 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4699 				return -EMSGSIZE;
4700 
4701 			if (copy > len)
4702 				copy = len;
4703 			ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4704 					      copy, recursion_level + 1);
4705 			if (unlikely(ret < 0))
4706 				return ret;
4707 			elt += ret;
4708 			if ((len -= copy) == 0)
4709 				return elt;
4710 			offset += copy;
4711 		}
4712 		start = end;
4713 	}
4714 	BUG_ON(len);
4715 	return elt;
4716 }
4717 
4718 /**
4719  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4720  *	@skb: Socket buffer containing the buffers to be mapped
4721  *	@sg: The scatter-gather list to map into
4722  *	@offset: The offset into the buffer's contents to start mapping
4723  *	@len: Length of buffer space to be mapped
4724  *
4725  *	Fill the specified scatter-gather list with mappings/pointers into a
4726  *	region of the buffer space attached to a socket buffer. Returns either
4727  *	the number of scatterlist items used, or -EMSGSIZE if the contents
4728  *	could not fit.
4729  */
4730 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4731 {
4732 	int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4733 
4734 	if (nsg <= 0)
4735 		return nsg;
4736 
4737 	sg_mark_end(&sg[nsg - 1]);
4738 
4739 	return nsg;
4740 }
4741 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4742 
4743 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4744  * sglist without mark the sg which contain last skb data as the end.
4745  * So the caller can mannipulate sg list as will when padding new data after
4746  * the first call without calling sg_unmark_end to expend sg list.
4747  *
4748  * Scenario to use skb_to_sgvec_nomark:
4749  * 1. sg_init_table
4750  * 2. skb_to_sgvec_nomark(payload1)
4751  * 3. skb_to_sgvec_nomark(payload2)
4752  *
4753  * This is equivalent to:
4754  * 1. sg_init_table
4755  * 2. skb_to_sgvec(payload1)
4756  * 3. sg_unmark_end
4757  * 4. skb_to_sgvec(payload2)
4758  *
4759  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4760  * is more preferable.
4761  */
4762 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4763 			int offset, int len)
4764 {
4765 	return __skb_to_sgvec(skb, sg, offset, len, 0);
4766 }
4767 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4768 
4769 
4770 
4771 /**
4772  *	skb_cow_data - Check that a socket buffer's data buffers are writable
4773  *	@skb: The socket buffer to check.
4774  *	@tailbits: Amount of trailing space to be added
4775  *	@trailer: Returned pointer to the skb where the @tailbits space begins
4776  *
4777  *	Make sure that the data buffers attached to a socket buffer are
4778  *	writable. If they are not, private copies are made of the data buffers
4779  *	and the socket buffer is set to use these instead.
4780  *
4781  *	If @tailbits is given, make sure that there is space to write @tailbits
4782  *	bytes of data beyond current end of socket buffer.  @trailer will be
4783  *	set to point to the skb in which this space begins.
4784  *
4785  *	The number of scatterlist elements required to completely map the
4786  *	COW'd and extended socket buffer will be returned.
4787  */
4788 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4789 {
4790 	int copyflag;
4791 	int elt;
4792 	struct sk_buff *skb1, **skb_p;
4793 
4794 	/* If skb is cloned or its head is paged, reallocate
4795 	 * head pulling out all the pages (pages are considered not writable
4796 	 * at the moment even if they are anonymous).
4797 	 */
4798 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4799 	    !__pskb_pull_tail(skb, __skb_pagelen(skb)))
4800 		return -ENOMEM;
4801 
4802 	/* Easy case. Most of packets will go this way. */
4803 	if (!skb_has_frag_list(skb)) {
4804 		/* A little of trouble, not enough of space for trailer.
4805 		 * This should not happen, when stack is tuned to generate
4806 		 * good frames. OK, on miss we reallocate and reserve even more
4807 		 * space, 128 bytes is fair. */
4808 
4809 		if (skb_tailroom(skb) < tailbits &&
4810 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4811 			return -ENOMEM;
4812 
4813 		/* Voila! */
4814 		*trailer = skb;
4815 		return 1;
4816 	}
4817 
4818 	/* Misery. We are in troubles, going to mincer fragments... */
4819 
4820 	elt = 1;
4821 	skb_p = &skb_shinfo(skb)->frag_list;
4822 	copyflag = 0;
4823 
4824 	while ((skb1 = *skb_p) != NULL) {
4825 		int ntail = 0;
4826 
4827 		/* The fragment is partially pulled by someone,
4828 		 * this can happen on input. Copy it and everything
4829 		 * after it. */
4830 
4831 		if (skb_shared(skb1))
4832 			copyflag = 1;
4833 
4834 		/* If the skb is the last, worry about trailer. */
4835 
4836 		if (skb1->next == NULL && tailbits) {
4837 			if (skb_shinfo(skb1)->nr_frags ||
4838 			    skb_has_frag_list(skb1) ||
4839 			    skb_tailroom(skb1) < tailbits)
4840 				ntail = tailbits + 128;
4841 		}
4842 
4843 		if (copyflag ||
4844 		    skb_cloned(skb1) ||
4845 		    ntail ||
4846 		    skb_shinfo(skb1)->nr_frags ||
4847 		    skb_has_frag_list(skb1)) {
4848 			struct sk_buff *skb2;
4849 
4850 			/* Fuck, we are miserable poor guys... */
4851 			if (ntail == 0)
4852 				skb2 = skb_copy(skb1, GFP_ATOMIC);
4853 			else
4854 				skb2 = skb_copy_expand(skb1,
4855 						       skb_headroom(skb1),
4856 						       ntail,
4857 						       GFP_ATOMIC);
4858 			if (unlikely(skb2 == NULL))
4859 				return -ENOMEM;
4860 
4861 			if (skb1->sk)
4862 				skb_set_owner_w(skb2, skb1->sk);
4863 
4864 			/* Looking around. Are we still alive?
4865 			 * OK, link new skb, drop old one */
4866 
4867 			skb2->next = skb1->next;
4868 			*skb_p = skb2;
4869 			kfree_skb(skb1);
4870 			skb1 = skb2;
4871 		}
4872 		elt++;
4873 		*trailer = skb1;
4874 		skb_p = &skb1->next;
4875 	}
4876 
4877 	return elt;
4878 }
4879 EXPORT_SYMBOL_GPL(skb_cow_data);
4880 
4881 static void sock_rmem_free(struct sk_buff *skb)
4882 {
4883 	struct sock *sk = skb->sk;
4884 
4885 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4886 }
4887 
4888 static void skb_set_err_queue(struct sk_buff *skb)
4889 {
4890 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4891 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
4892 	 */
4893 	skb->pkt_type = PACKET_OUTGOING;
4894 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
4895 }
4896 
4897 /*
4898  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4899  */
4900 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4901 {
4902 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4903 	    (unsigned int)READ_ONCE(sk->sk_rcvbuf))
4904 		return -ENOMEM;
4905 
4906 	skb_orphan(skb);
4907 	skb->sk = sk;
4908 	skb->destructor = sock_rmem_free;
4909 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4910 	skb_set_err_queue(skb);
4911 
4912 	/* before exiting rcu section, make sure dst is refcounted */
4913 	skb_dst_force(skb);
4914 
4915 	skb_queue_tail(&sk->sk_error_queue, skb);
4916 	if (!sock_flag(sk, SOCK_DEAD))
4917 		sk_error_report(sk);
4918 	return 0;
4919 }
4920 EXPORT_SYMBOL(sock_queue_err_skb);
4921 
4922 static bool is_icmp_err_skb(const struct sk_buff *skb)
4923 {
4924 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4925 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4926 }
4927 
4928 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4929 {
4930 	struct sk_buff_head *q = &sk->sk_error_queue;
4931 	struct sk_buff *skb, *skb_next = NULL;
4932 	bool icmp_next = false;
4933 	unsigned long flags;
4934 
4935 	spin_lock_irqsave(&q->lock, flags);
4936 	skb = __skb_dequeue(q);
4937 	if (skb && (skb_next = skb_peek(q))) {
4938 		icmp_next = is_icmp_err_skb(skb_next);
4939 		if (icmp_next)
4940 			sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
4941 	}
4942 	spin_unlock_irqrestore(&q->lock, flags);
4943 
4944 	if (is_icmp_err_skb(skb) && !icmp_next)
4945 		sk->sk_err = 0;
4946 
4947 	if (skb_next)
4948 		sk_error_report(sk);
4949 
4950 	return skb;
4951 }
4952 EXPORT_SYMBOL(sock_dequeue_err_skb);
4953 
4954 /**
4955  * skb_clone_sk - create clone of skb, and take reference to socket
4956  * @skb: the skb to clone
4957  *
4958  * This function creates a clone of a buffer that holds a reference on
4959  * sk_refcnt.  Buffers created via this function are meant to be
4960  * returned using sock_queue_err_skb, or free via kfree_skb.
4961  *
4962  * When passing buffers allocated with this function to sock_queue_err_skb
4963  * it is necessary to wrap the call with sock_hold/sock_put in order to
4964  * prevent the socket from being released prior to being enqueued on
4965  * the sk_error_queue.
4966  */
4967 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4968 {
4969 	struct sock *sk = skb->sk;
4970 	struct sk_buff *clone;
4971 
4972 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4973 		return NULL;
4974 
4975 	clone = skb_clone(skb, GFP_ATOMIC);
4976 	if (!clone) {
4977 		sock_put(sk);
4978 		return NULL;
4979 	}
4980 
4981 	clone->sk = sk;
4982 	clone->destructor = sock_efree;
4983 
4984 	return clone;
4985 }
4986 EXPORT_SYMBOL(skb_clone_sk);
4987 
4988 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4989 					struct sock *sk,
4990 					int tstype,
4991 					bool opt_stats)
4992 {
4993 	struct sock_exterr_skb *serr;
4994 	int err;
4995 
4996 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4997 
4998 	serr = SKB_EXT_ERR(skb);
4999 	memset(serr, 0, sizeof(*serr));
5000 	serr->ee.ee_errno = ENOMSG;
5001 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
5002 	serr->ee.ee_info = tstype;
5003 	serr->opt_stats = opt_stats;
5004 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
5005 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
5006 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
5007 		if (sk_is_tcp(sk))
5008 			serr->ee.ee_data -= atomic_read(&sk->sk_tskey);
5009 	}
5010 
5011 	err = sock_queue_err_skb(sk, skb);
5012 
5013 	if (err)
5014 		kfree_skb(skb);
5015 }
5016 
5017 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
5018 {
5019 	bool ret;
5020 
5021 	if (likely(READ_ONCE(sysctl_tstamp_allow_data) || tsonly))
5022 		return true;
5023 
5024 	read_lock_bh(&sk->sk_callback_lock);
5025 	ret = sk->sk_socket && sk->sk_socket->file &&
5026 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
5027 	read_unlock_bh(&sk->sk_callback_lock);
5028 	return ret;
5029 }
5030 
5031 void skb_complete_tx_timestamp(struct sk_buff *skb,
5032 			       struct skb_shared_hwtstamps *hwtstamps)
5033 {
5034 	struct sock *sk = skb->sk;
5035 
5036 	if (!skb_may_tx_timestamp(sk, false))
5037 		goto err;
5038 
5039 	/* Take a reference to prevent skb_orphan() from freeing the socket,
5040 	 * but only if the socket refcount is not zero.
5041 	 */
5042 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
5043 		*skb_hwtstamps(skb) = *hwtstamps;
5044 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
5045 		sock_put(sk);
5046 		return;
5047 	}
5048 
5049 err:
5050 	kfree_skb(skb);
5051 }
5052 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
5053 
5054 void __skb_tstamp_tx(struct sk_buff *orig_skb,
5055 		     const struct sk_buff *ack_skb,
5056 		     struct skb_shared_hwtstamps *hwtstamps,
5057 		     struct sock *sk, int tstype)
5058 {
5059 	struct sk_buff *skb;
5060 	bool tsonly, opt_stats = false;
5061 
5062 	if (!sk)
5063 		return;
5064 
5065 	if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
5066 	    skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
5067 		return;
5068 
5069 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
5070 	if (!skb_may_tx_timestamp(sk, tsonly))
5071 		return;
5072 
5073 	if (tsonly) {
5074 #ifdef CONFIG_INET
5075 		if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
5076 		    sk_is_tcp(sk)) {
5077 			skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
5078 							     ack_skb);
5079 			opt_stats = true;
5080 		} else
5081 #endif
5082 			skb = alloc_skb(0, GFP_ATOMIC);
5083 	} else {
5084 		skb = skb_clone(orig_skb, GFP_ATOMIC);
5085 	}
5086 	if (!skb)
5087 		return;
5088 
5089 	if (tsonly) {
5090 		skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
5091 					     SKBTX_ANY_TSTAMP;
5092 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
5093 	}
5094 
5095 	if (hwtstamps)
5096 		*skb_hwtstamps(skb) = *hwtstamps;
5097 	else
5098 		__net_timestamp(skb);
5099 
5100 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
5101 }
5102 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
5103 
5104 void skb_tstamp_tx(struct sk_buff *orig_skb,
5105 		   struct skb_shared_hwtstamps *hwtstamps)
5106 {
5107 	return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
5108 			       SCM_TSTAMP_SND);
5109 }
5110 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
5111 
5112 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
5113 {
5114 	struct sock *sk = skb->sk;
5115 	struct sock_exterr_skb *serr;
5116 	int err = 1;
5117 
5118 	skb->wifi_acked_valid = 1;
5119 	skb->wifi_acked = acked;
5120 
5121 	serr = SKB_EXT_ERR(skb);
5122 	memset(serr, 0, sizeof(*serr));
5123 	serr->ee.ee_errno = ENOMSG;
5124 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
5125 
5126 	/* Take a reference to prevent skb_orphan() from freeing the socket,
5127 	 * but only if the socket refcount is not zero.
5128 	 */
5129 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
5130 		err = sock_queue_err_skb(sk, skb);
5131 		sock_put(sk);
5132 	}
5133 	if (err)
5134 		kfree_skb(skb);
5135 }
5136 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
5137 
5138 /**
5139  * skb_partial_csum_set - set up and verify partial csum values for packet
5140  * @skb: the skb to set
5141  * @start: the number of bytes after skb->data to start checksumming.
5142  * @off: the offset from start to place the checksum.
5143  *
5144  * For untrusted partially-checksummed packets, we need to make sure the values
5145  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
5146  *
5147  * This function checks and sets those values and skb->ip_summed: if this
5148  * returns false you should drop the packet.
5149  */
5150 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
5151 {
5152 	u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
5153 	u32 csum_start = skb_headroom(skb) + (u32)start;
5154 
5155 	if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
5156 		net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
5157 				     start, off, skb_headroom(skb), skb_headlen(skb));
5158 		return false;
5159 	}
5160 	skb->ip_summed = CHECKSUM_PARTIAL;
5161 	skb->csum_start = csum_start;
5162 	skb->csum_offset = off;
5163 	skb_set_transport_header(skb, start);
5164 	return true;
5165 }
5166 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
5167 
5168 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
5169 			       unsigned int max)
5170 {
5171 	if (skb_headlen(skb) >= len)
5172 		return 0;
5173 
5174 	/* If we need to pullup then pullup to the max, so we
5175 	 * won't need to do it again.
5176 	 */
5177 	if (max > skb->len)
5178 		max = skb->len;
5179 
5180 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
5181 		return -ENOMEM;
5182 
5183 	if (skb_headlen(skb) < len)
5184 		return -EPROTO;
5185 
5186 	return 0;
5187 }
5188 
5189 #define MAX_TCP_HDR_LEN (15 * 4)
5190 
5191 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
5192 				      typeof(IPPROTO_IP) proto,
5193 				      unsigned int off)
5194 {
5195 	int err;
5196 
5197 	switch (proto) {
5198 	case IPPROTO_TCP:
5199 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
5200 					  off + MAX_TCP_HDR_LEN);
5201 		if (!err && !skb_partial_csum_set(skb, off,
5202 						  offsetof(struct tcphdr,
5203 							   check)))
5204 			err = -EPROTO;
5205 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
5206 
5207 	case IPPROTO_UDP:
5208 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
5209 					  off + sizeof(struct udphdr));
5210 		if (!err && !skb_partial_csum_set(skb, off,
5211 						  offsetof(struct udphdr,
5212 							   check)))
5213 			err = -EPROTO;
5214 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
5215 	}
5216 
5217 	return ERR_PTR(-EPROTO);
5218 }
5219 
5220 /* This value should be large enough to cover a tagged ethernet header plus
5221  * maximally sized IP and TCP or UDP headers.
5222  */
5223 #define MAX_IP_HDR_LEN 128
5224 
5225 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
5226 {
5227 	unsigned int off;
5228 	bool fragment;
5229 	__sum16 *csum;
5230 	int err;
5231 
5232 	fragment = false;
5233 
5234 	err = skb_maybe_pull_tail(skb,
5235 				  sizeof(struct iphdr),
5236 				  MAX_IP_HDR_LEN);
5237 	if (err < 0)
5238 		goto out;
5239 
5240 	if (ip_is_fragment(ip_hdr(skb)))
5241 		fragment = true;
5242 
5243 	off = ip_hdrlen(skb);
5244 
5245 	err = -EPROTO;
5246 
5247 	if (fragment)
5248 		goto out;
5249 
5250 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5251 	if (IS_ERR(csum))
5252 		return PTR_ERR(csum);
5253 
5254 	if (recalculate)
5255 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5256 					   ip_hdr(skb)->daddr,
5257 					   skb->len - off,
5258 					   ip_hdr(skb)->protocol, 0);
5259 	err = 0;
5260 
5261 out:
5262 	return err;
5263 }
5264 
5265 /* This value should be large enough to cover a tagged ethernet header plus
5266  * an IPv6 header, all options, and a maximal TCP or UDP header.
5267  */
5268 #define MAX_IPV6_HDR_LEN 256
5269 
5270 #define OPT_HDR(type, skb, off) \
5271 	(type *)(skb_network_header(skb) + (off))
5272 
5273 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5274 {
5275 	int err;
5276 	u8 nexthdr;
5277 	unsigned int off;
5278 	unsigned int len;
5279 	bool fragment;
5280 	bool done;
5281 	__sum16 *csum;
5282 
5283 	fragment = false;
5284 	done = false;
5285 
5286 	off = sizeof(struct ipv6hdr);
5287 
5288 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5289 	if (err < 0)
5290 		goto out;
5291 
5292 	nexthdr = ipv6_hdr(skb)->nexthdr;
5293 
5294 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5295 	while (off <= len && !done) {
5296 		switch (nexthdr) {
5297 		case IPPROTO_DSTOPTS:
5298 		case IPPROTO_HOPOPTS:
5299 		case IPPROTO_ROUTING: {
5300 			struct ipv6_opt_hdr *hp;
5301 
5302 			err = skb_maybe_pull_tail(skb,
5303 						  off +
5304 						  sizeof(struct ipv6_opt_hdr),
5305 						  MAX_IPV6_HDR_LEN);
5306 			if (err < 0)
5307 				goto out;
5308 
5309 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5310 			nexthdr = hp->nexthdr;
5311 			off += ipv6_optlen(hp);
5312 			break;
5313 		}
5314 		case IPPROTO_AH: {
5315 			struct ip_auth_hdr *hp;
5316 
5317 			err = skb_maybe_pull_tail(skb,
5318 						  off +
5319 						  sizeof(struct ip_auth_hdr),
5320 						  MAX_IPV6_HDR_LEN);
5321 			if (err < 0)
5322 				goto out;
5323 
5324 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5325 			nexthdr = hp->nexthdr;
5326 			off += ipv6_authlen(hp);
5327 			break;
5328 		}
5329 		case IPPROTO_FRAGMENT: {
5330 			struct frag_hdr *hp;
5331 
5332 			err = skb_maybe_pull_tail(skb,
5333 						  off +
5334 						  sizeof(struct frag_hdr),
5335 						  MAX_IPV6_HDR_LEN);
5336 			if (err < 0)
5337 				goto out;
5338 
5339 			hp = OPT_HDR(struct frag_hdr, skb, off);
5340 
5341 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5342 				fragment = true;
5343 
5344 			nexthdr = hp->nexthdr;
5345 			off += sizeof(struct frag_hdr);
5346 			break;
5347 		}
5348 		default:
5349 			done = true;
5350 			break;
5351 		}
5352 	}
5353 
5354 	err = -EPROTO;
5355 
5356 	if (!done || fragment)
5357 		goto out;
5358 
5359 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
5360 	if (IS_ERR(csum))
5361 		return PTR_ERR(csum);
5362 
5363 	if (recalculate)
5364 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5365 					 &ipv6_hdr(skb)->daddr,
5366 					 skb->len - off, nexthdr, 0);
5367 	err = 0;
5368 
5369 out:
5370 	return err;
5371 }
5372 
5373 /**
5374  * skb_checksum_setup - set up partial checksum offset
5375  * @skb: the skb to set up
5376  * @recalculate: if true the pseudo-header checksum will be recalculated
5377  */
5378 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5379 {
5380 	int err;
5381 
5382 	switch (skb->protocol) {
5383 	case htons(ETH_P_IP):
5384 		err = skb_checksum_setup_ipv4(skb, recalculate);
5385 		break;
5386 
5387 	case htons(ETH_P_IPV6):
5388 		err = skb_checksum_setup_ipv6(skb, recalculate);
5389 		break;
5390 
5391 	default:
5392 		err = -EPROTO;
5393 		break;
5394 	}
5395 
5396 	return err;
5397 }
5398 EXPORT_SYMBOL(skb_checksum_setup);
5399 
5400 /**
5401  * skb_checksum_maybe_trim - maybe trims the given skb
5402  * @skb: the skb to check
5403  * @transport_len: the data length beyond the network header
5404  *
5405  * Checks whether the given skb has data beyond the given transport length.
5406  * If so, returns a cloned skb trimmed to this transport length.
5407  * Otherwise returns the provided skb. Returns NULL in error cases
5408  * (e.g. transport_len exceeds skb length or out-of-memory).
5409  *
5410  * Caller needs to set the skb transport header and free any returned skb if it
5411  * differs from the provided skb.
5412  */
5413 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5414 					       unsigned int transport_len)
5415 {
5416 	struct sk_buff *skb_chk;
5417 	unsigned int len = skb_transport_offset(skb) + transport_len;
5418 	int ret;
5419 
5420 	if (skb->len < len)
5421 		return NULL;
5422 	else if (skb->len == len)
5423 		return skb;
5424 
5425 	skb_chk = skb_clone(skb, GFP_ATOMIC);
5426 	if (!skb_chk)
5427 		return NULL;
5428 
5429 	ret = pskb_trim_rcsum(skb_chk, len);
5430 	if (ret) {
5431 		kfree_skb(skb_chk);
5432 		return NULL;
5433 	}
5434 
5435 	return skb_chk;
5436 }
5437 
5438 /**
5439  * skb_checksum_trimmed - validate checksum of an skb
5440  * @skb: the skb to check
5441  * @transport_len: the data length beyond the network header
5442  * @skb_chkf: checksum function to use
5443  *
5444  * Applies the given checksum function skb_chkf to the provided skb.
5445  * Returns a checked and maybe trimmed skb. Returns NULL on error.
5446  *
5447  * If the skb has data beyond the given transport length, then a
5448  * trimmed & cloned skb is checked and returned.
5449  *
5450  * Caller needs to set the skb transport header and free any returned skb if it
5451  * differs from the provided skb.
5452  */
5453 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5454 				     unsigned int transport_len,
5455 				     __sum16(*skb_chkf)(struct sk_buff *skb))
5456 {
5457 	struct sk_buff *skb_chk;
5458 	unsigned int offset = skb_transport_offset(skb);
5459 	__sum16 ret;
5460 
5461 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5462 	if (!skb_chk)
5463 		goto err;
5464 
5465 	if (!pskb_may_pull(skb_chk, offset))
5466 		goto err;
5467 
5468 	skb_pull_rcsum(skb_chk, offset);
5469 	ret = skb_chkf(skb_chk);
5470 	skb_push_rcsum(skb_chk, offset);
5471 
5472 	if (ret)
5473 		goto err;
5474 
5475 	return skb_chk;
5476 
5477 err:
5478 	if (skb_chk && skb_chk != skb)
5479 		kfree_skb(skb_chk);
5480 
5481 	return NULL;
5482 
5483 }
5484 EXPORT_SYMBOL(skb_checksum_trimmed);
5485 
5486 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5487 {
5488 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5489 			     skb->dev->name);
5490 }
5491 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5492 
5493 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5494 {
5495 	if (head_stolen) {
5496 		skb_release_head_state(skb);
5497 		kmem_cache_free(skbuff_head_cache, skb);
5498 	} else {
5499 		__kfree_skb(skb);
5500 	}
5501 }
5502 EXPORT_SYMBOL(kfree_skb_partial);
5503 
5504 /**
5505  * skb_try_coalesce - try to merge skb to prior one
5506  * @to: prior buffer
5507  * @from: buffer to add
5508  * @fragstolen: pointer to boolean
5509  * @delta_truesize: how much more was allocated than was requested
5510  */
5511 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5512 		      bool *fragstolen, int *delta_truesize)
5513 {
5514 	struct skb_shared_info *to_shinfo, *from_shinfo;
5515 	int i, delta, len = from->len;
5516 
5517 	*fragstolen = false;
5518 
5519 	if (skb_cloned(to))
5520 		return false;
5521 
5522 	/* In general, avoid mixing slab allocated and page_pool allocated
5523 	 * pages within the same SKB. However when @to is not pp_recycle and
5524 	 * @from is cloned, we can transition frag pages from page_pool to
5525 	 * reference counted.
5526 	 *
5527 	 * On the other hand, don't allow coalescing two pp_recycle SKBs if
5528 	 * @from is cloned, in case the SKB is using page_pool fragment
5529 	 * references (PP_FLAG_PAGE_FRAG). Since we only take full page
5530 	 * references for cloned SKBs at the moment that would result in
5531 	 * inconsistent reference counts.
5532 	 */
5533 	if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from)))
5534 		return false;
5535 
5536 	if (len <= skb_tailroom(to)) {
5537 		if (len)
5538 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5539 		*delta_truesize = 0;
5540 		return true;
5541 	}
5542 
5543 	to_shinfo = skb_shinfo(to);
5544 	from_shinfo = skb_shinfo(from);
5545 	if (to_shinfo->frag_list || from_shinfo->frag_list)
5546 		return false;
5547 	if (skb_zcopy(to) || skb_zcopy(from))
5548 		return false;
5549 
5550 	if (skb_headlen(from) != 0) {
5551 		struct page *page;
5552 		unsigned int offset;
5553 
5554 		if (to_shinfo->nr_frags +
5555 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5556 			return false;
5557 
5558 		if (skb_head_is_locked(from))
5559 			return false;
5560 
5561 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5562 
5563 		page = virt_to_head_page(from->head);
5564 		offset = from->data - (unsigned char *)page_address(page);
5565 
5566 		skb_fill_page_desc(to, to_shinfo->nr_frags,
5567 				   page, offset, skb_headlen(from));
5568 		*fragstolen = true;
5569 	} else {
5570 		if (to_shinfo->nr_frags +
5571 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
5572 			return false;
5573 
5574 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5575 	}
5576 
5577 	WARN_ON_ONCE(delta < len);
5578 
5579 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5580 	       from_shinfo->frags,
5581 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
5582 	to_shinfo->nr_frags += from_shinfo->nr_frags;
5583 
5584 	if (!skb_cloned(from))
5585 		from_shinfo->nr_frags = 0;
5586 
5587 	/* if the skb is not cloned this does nothing
5588 	 * since we set nr_frags to 0.
5589 	 */
5590 	for (i = 0; i < from_shinfo->nr_frags; i++)
5591 		__skb_frag_ref(&from_shinfo->frags[i]);
5592 
5593 	to->truesize += delta;
5594 	to->len += len;
5595 	to->data_len += len;
5596 
5597 	*delta_truesize = delta;
5598 	return true;
5599 }
5600 EXPORT_SYMBOL(skb_try_coalesce);
5601 
5602 /**
5603  * skb_scrub_packet - scrub an skb
5604  *
5605  * @skb: buffer to clean
5606  * @xnet: packet is crossing netns
5607  *
5608  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5609  * into/from a tunnel. Some information have to be cleared during these
5610  * operations.
5611  * skb_scrub_packet can also be used to clean a skb before injecting it in
5612  * another namespace (@xnet == true). We have to clear all information in the
5613  * skb that could impact namespace isolation.
5614  */
5615 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5616 {
5617 	skb->pkt_type = PACKET_HOST;
5618 	skb->skb_iif = 0;
5619 	skb->ignore_df = 0;
5620 	skb_dst_drop(skb);
5621 	skb_ext_reset(skb);
5622 	nf_reset_ct(skb);
5623 	nf_reset_trace(skb);
5624 
5625 #ifdef CONFIG_NET_SWITCHDEV
5626 	skb->offload_fwd_mark = 0;
5627 	skb->offload_l3_fwd_mark = 0;
5628 #endif
5629 
5630 	if (!xnet)
5631 		return;
5632 
5633 	ipvs_reset(skb);
5634 	skb->mark = 0;
5635 	skb_clear_tstamp(skb);
5636 }
5637 EXPORT_SYMBOL_GPL(skb_scrub_packet);
5638 
5639 /**
5640  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5641  *
5642  * @skb: GSO skb
5643  *
5644  * skb_gso_transport_seglen is used to determine the real size of the
5645  * individual segments, including Layer4 headers (TCP/UDP).
5646  *
5647  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5648  */
5649 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
5650 {
5651 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
5652 	unsigned int thlen = 0;
5653 
5654 	if (skb->encapsulation) {
5655 		thlen = skb_inner_transport_header(skb) -
5656 			skb_transport_header(skb);
5657 
5658 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5659 			thlen += inner_tcp_hdrlen(skb);
5660 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5661 		thlen = tcp_hdrlen(skb);
5662 	} else if (unlikely(skb_is_gso_sctp(skb))) {
5663 		thlen = sizeof(struct sctphdr);
5664 	} else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5665 		thlen = sizeof(struct udphdr);
5666 	}
5667 	/* UFO sets gso_size to the size of the fragmentation
5668 	 * payload, i.e. the size of the L4 (UDP) header is already
5669 	 * accounted for.
5670 	 */
5671 	return thlen + shinfo->gso_size;
5672 }
5673 
5674 /**
5675  * skb_gso_network_seglen - Return length of individual segments of a gso packet
5676  *
5677  * @skb: GSO skb
5678  *
5679  * skb_gso_network_seglen is used to determine the real size of the
5680  * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5681  *
5682  * The MAC/L2 header is not accounted for.
5683  */
5684 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5685 {
5686 	unsigned int hdr_len = skb_transport_header(skb) -
5687 			       skb_network_header(skb);
5688 
5689 	return hdr_len + skb_gso_transport_seglen(skb);
5690 }
5691 
5692 /**
5693  * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5694  *
5695  * @skb: GSO skb
5696  *
5697  * skb_gso_mac_seglen is used to determine the real size of the
5698  * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5699  * headers (TCP/UDP).
5700  */
5701 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5702 {
5703 	unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5704 
5705 	return hdr_len + skb_gso_transport_seglen(skb);
5706 }
5707 
5708 /**
5709  * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5710  *
5711  * There are a couple of instances where we have a GSO skb, and we
5712  * want to determine what size it would be after it is segmented.
5713  *
5714  * We might want to check:
5715  * -    L3+L4+payload size (e.g. IP forwarding)
5716  * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5717  *
5718  * This is a helper to do that correctly considering GSO_BY_FRAGS.
5719  *
5720  * @skb: GSO skb
5721  *
5722  * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5723  *           GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5724  *
5725  * @max_len: The maximum permissible length.
5726  *
5727  * Returns true if the segmented length <= max length.
5728  */
5729 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5730 				      unsigned int seg_len,
5731 				      unsigned int max_len) {
5732 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
5733 	const struct sk_buff *iter;
5734 
5735 	if (shinfo->gso_size != GSO_BY_FRAGS)
5736 		return seg_len <= max_len;
5737 
5738 	/* Undo this so we can re-use header sizes */
5739 	seg_len -= GSO_BY_FRAGS;
5740 
5741 	skb_walk_frags(skb, iter) {
5742 		if (seg_len + skb_headlen(iter) > max_len)
5743 			return false;
5744 	}
5745 
5746 	return true;
5747 }
5748 
5749 /**
5750  * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5751  *
5752  * @skb: GSO skb
5753  * @mtu: MTU to validate against
5754  *
5755  * skb_gso_validate_network_len validates if a given skb will fit a
5756  * wanted MTU once split. It considers L3 headers, L4 headers, and the
5757  * payload.
5758  */
5759 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5760 {
5761 	return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5762 }
5763 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5764 
5765 /**
5766  * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5767  *
5768  * @skb: GSO skb
5769  * @len: length to validate against
5770  *
5771  * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5772  * length once split, including L2, L3 and L4 headers and the payload.
5773  */
5774 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5775 {
5776 	return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5777 }
5778 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5779 
5780 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5781 {
5782 	int mac_len, meta_len;
5783 	void *meta;
5784 
5785 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
5786 		kfree_skb(skb);
5787 		return NULL;
5788 	}
5789 
5790 	mac_len = skb->data - skb_mac_header(skb);
5791 	if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5792 		memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5793 			mac_len - VLAN_HLEN - ETH_TLEN);
5794 	}
5795 
5796 	meta_len = skb_metadata_len(skb);
5797 	if (meta_len) {
5798 		meta = skb_metadata_end(skb) - meta_len;
5799 		memmove(meta + VLAN_HLEN, meta, meta_len);
5800 	}
5801 
5802 	skb->mac_header += VLAN_HLEN;
5803 	return skb;
5804 }
5805 
5806 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5807 {
5808 	struct vlan_hdr *vhdr;
5809 	u16 vlan_tci;
5810 
5811 	if (unlikely(skb_vlan_tag_present(skb))) {
5812 		/* vlan_tci is already set-up so leave this for another time */
5813 		return skb;
5814 	}
5815 
5816 	skb = skb_share_check(skb, GFP_ATOMIC);
5817 	if (unlikely(!skb))
5818 		goto err_free;
5819 	/* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5820 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
5821 		goto err_free;
5822 
5823 	vhdr = (struct vlan_hdr *)skb->data;
5824 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
5825 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5826 
5827 	skb_pull_rcsum(skb, VLAN_HLEN);
5828 	vlan_set_encap_proto(skb, vhdr);
5829 
5830 	skb = skb_reorder_vlan_header(skb);
5831 	if (unlikely(!skb))
5832 		goto err_free;
5833 
5834 	skb_reset_network_header(skb);
5835 	if (!skb_transport_header_was_set(skb))
5836 		skb_reset_transport_header(skb);
5837 	skb_reset_mac_len(skb);
5838 
5839 	return skb;
5840 
5841 err_free:
5842 	kfree_skb(skb);
5843 	return NULL;
5844 }
5845 EXPORT_SYMBOL(skb_vlan_untag);
5846 
5847 int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len)
5848 {
5849 	if (!pskb_may_pull(skb, write_len))
5850 		return -ENOMEM;
5851 
5852 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5853 		return 0;
5854 
5855 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5856 }
5857 EXPORT_SYMBOL(skb_ensure_writable);
5858 
5859 /* remove VLAN header from packet and update csum accordingly.
5860  * expects a non skb_vlan_tag_present skb with a vlan tag payload
5861  */
5862 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5863 {
5864 	struct vlan_hdr *vhdr;
5865 	int offset = skb->data - skb_mac_header(skb);
5866 	int err;
5867 
5868 	if (WARN_ONCE(offset,
5869 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5870 		      offset)) {
5871 		return -EINVAL;
5872 	}
5873 
5874 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5875 	if (unlikely(err))
5876 		return err;
5877 
5878 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5879 
5880 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5881 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
5882 
5883 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5884 	__skb_pull(skb, VLAN_HLEN);
5885 
5886 	vlan_set_encap_proto(skb, vhdr);
5887 	skb->mac_header += VLAN_HLEN;
5888 
5889 	if (skb_network_offset(skb) < ETH_HLEN)
5890 		skb_set_network_header(skb, ETH_HLEN);
5891 
5892 	skb_reset_mac_len(skb);
5893 
5894 	return err;
5895 }
5896 EXPORT_SYMBOL(__skb_vlan_pop);
5897 
5898 /* Pop a vlan tag either from hwaccel or from payload.
5899  * Expects skb->data at mac header.
5900  */
5901 int skb_vlan_pop(struct sk_buff *skb)
5902 {
5903 	u16 vlan_tci;
5904 	__be16 vlan_proto;
5905 	int err;
5906 
5907 	if (likely(skb_vlan_tag_present(skb))) {
5908 		__vlan_hwaccel_clear_tag(skb);
5909 	} else {
5910 		if (unlikely(!eth_type_vlan(skb->protocol)))
5911 			return 0;
5912 
5913 		err = __skb_vlan_pop(skb, &vlan_tci);
5914 		if (err)
5915 			return err;
5916 	}
5917 	/* move next vlan tag to hw accel tag */
5918 	if (likely(!eth_type_vlan(skb->protocol)))
5919 		return 0;
5920 
5921 	vlan_proto = skb->protocol;
5922 	err = __skb_vlan_pop(skb, &vlan_tci);
5923 	if (unlikely(err))
5924 		return err;
5925 
5926 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5927 	return 0;
5928 }
5929 EXPORT_SYMBOL(skb_vlan_pop);
5930 
5931 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5932  * Expects skb->data at mac header.
5933  */
5934 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5935 {
5936 	if (skb_vlan_tag_present(skb)) {
5937 		int offset = skb->data - skb_mac_header(skb);
5938 		int err;
5939 
5940 		if (WARN_ONCE(offset,
5941 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5942 			      offset)) {
5943 			return -EINVAL;
5944 		}
5945 
5946 		err = __vlan_insert_tag(skb, skb->vlan_proto,
5947 					skb_vlan_tag_get(skb));
5948 		if (err)
5949 			return err;
5950 
5951 		skb->protocol = skb->vlan_proto;
5952 		skb->mac_len += VLAN_HLEN;
5953 
5954 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5955 	}
5956 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5957 	return 0;
5958 }
5959 EXPORT_SYMBOL(skb_vlan_push);
5960 
5961 /**
5962  * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5963  *
5964  * @skb: Socket buffer to modify
5965  *
5966  * Drop the Ethernet header of @skb.
5967  *
5968  * Expects that skb->data points to the mac header and that no VLAN tags are
5969  * present.
5970  *
5971  * Returns 0 on success, -errno otherwise.
5972  */
5973 int skb_eth_pop(struct sk_buff *skb)
5974 {
5975 	if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5976 	    skb_network_offset(skb) < ETH_HLEN)
5977 		return -EPROTO;
5978 
5979 	skb_pull_rcsum(skb, ETH_HLEN);
5980 	skb_reset_mac_header(skb);
5981 	skb_reset_mac_len(skb);
5982 
5983 	return 0;
5984 }
5985 EXPORT_SYMBOL(skb_eth_pop);
5986 
5987 /**
5988  * skb_eth_push() - Add a new Ethernet header at the head of a packet
5989  *
5990  * @skb: Socket buffer to modify
5991  * @dst: Destination MAC address of the new header
5992  * @src: Source MAC address of the new header
5993  *
5994  * Prepend @skb with a new Ethernet header.
5995  *
5996  * Expects that skb->data points to the mac header, which must be empty.
5997  *
5998  * Returns 0 on success, -errno otherwise.
5999  */
6000 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
6001 		 const unsigned char *src)
6002 {
6003 	struct ethhdr *eth;
6004 	int err;
6005 
6006 	if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
6007 		return -EPROTO;
6008 
6009 	err = skb_cow_head(skb, sizeof(*eth));
6010 	if (err < 0)
6011 		return err;
6012 
6013 	skb_push(skb, sizeof(*eth));
6014 	skb_reset_mac_header(skb);
6015 	skb_reset_mac_len(skb);
6016 
6017 	eth = eth_hdr(skb);
6018 	ether_addr_copy(eth->h_dest, dst);
6019 	ether_addr_copy(eth->h_source, src);
6020 	eth->h_proto = skb->protocol;
6021 
6022 	skb_postpush_rcsum(skb, eth, sizeof(*eth));
6023 
6024 	return 0;
6025 }
6026 EXPORT_SYMBOL(skb_eth_push);
6027 
6028 /* Update the ethertype of hdr and the skb csum value if required. */
6029 static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
6030 			     __be16 ethertype)
6031 {
6032 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
6033 		__be16 diff[] = { ~hdr->h_proto, ethertype };
6034 
6035 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6036 	}
6037 
6038 	hdr->h_proto = ethertype;
6039 }
6040 
6041 /**
6042  * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
6043  *                   the packet
6044  *
6045  * @skb: buffer
6046  * @mpls_lse: MPLS label stack entry to push
6047  * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
6048  * @mac_len: length of the MAC header
6049  * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
6050  *            ethernet
6051  *
6052  * Expects skb->data at mac header.
6053  *
6054  * Returns 0 on success, -errno otherwise.
6055  */
6056 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
6057 		  int mac_len, bool ethernet)
6058 {
6059 	struct mpls_shim_hdr *lse;
6060 	int err;
6061 
6062 	if (unlikely(!eth_p_mpls(mpls_proto)))
6063 		return -EINVAL;
6064 
6065 	/* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
6066 	if (skb->encapsulation)
6067 		return -EINVAL;
6068 
6069 	err = skb_cow_head(skb, MPLS_HLEN);
6070 	if (unlikely(err))
6071 		return err;
6072 
6073 	if (!skb->inner_protocol) {
6074 		skb_set_inner_network_header(skb, skb_network_offset(skb));
6075 		skb_set_inner_protocol(skb, skb->protocol);
6076 	}
6077 
6078 	skb_push(skb, MPLS_HLEN);
6079 	memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
6080 		mac_len);
6081 	skb_reset_mac_header(skb);
6082 	skb_set_network_header(skb, mac_len);
6083 	skb_reset_mac_len(skb);
6084 
6085 	lse = mpls_hdr(skb);
6086 	lse->label_stack_entry = mpls_lse;
6087 	skb_postpush_rcsum(skb, lse, MPLS_HLEN);
6088 
6089 	if (ethernet && mac_len >= ETH_HLEN)
6090 		skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
6091 	skb->protocol = mpls_proto;
6092 
6093 	return 0;
6094 }
6095 EXPORT_SYMBOL_GPL(skb_mpls_push);
6096 
6097 /**
6098  * skb_mpls_pop() - pop the outermost MPLS header
6099  *
6100  * @skb: buffer
6101  * @next_proto: ethertype of header after popped MPLS header
6102  * @mac_len: length of the MAC header
6103  * @ethernet: flag to indicate if the packet is ethernet
6104  *
6105  * Expects skb->data at mac header.
6106  *
6107  * Returns 0 on success, -errno otherwise.
6108  */
6109 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
6110 		 bool ethernet)
6111 {
6112 	int err;
6113 
6114 	if (unlikely(!eth_p_mpls(skb->protocol)))
6115 		return 0;
6116 
6117 	err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
6118 	if (unlikely(err))
6119 		return err;
6120 
6121 	skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
6122 	memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
6123 		mac_len);
6124 
6125 	__skb_pull(skb, MPLS_HLEN);
6126 	skb_reset_mac_header(skb);
6127 	skb_set_network_header(skb, mac_len);
6128 
6129 	if (ethernet && mac_len >= ETH_HLEN) {
6130 		struct ethhdr *hdr;
6131 
6132 		/* use mpls_hdr() to get ethertype to account for VLANs. */
6133 		hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
6134 		skb_mod_eth_type(skb, hdr, next_proto);
6135 	}
6136 	skb->protocol = next_proto;
6137 
6138 	return 0;
6139 }
6140 EXPORT_SYMBOL_GPL(skb_mpls_pop);
6141 
6142 /**
6143  * skb_mpls_update_lse() - modify outermost MPLS header and update csum
6144  *
6145  * @skb: buffer
6146  * @mpls_lse: new MPLS label stack entry to update to
6147  *
6148  * Expects skb->data at mac header.
6149  *
6150  * Returns 0 on success, -errno otherwise.
6151  */
6152 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
6153 {
6154 	int err;
6155 
6156 	if (unlikely(!eth_p_mpls(skb->protocol)))
6157 		return -EINVAL;
6158 
6159 	err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
6160 	if (unlikely(err))
6161 		return err;
6162 
6163 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
6164 		__be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
6165 
6166 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
6167 	}
6168 
6169 	mpls_hdr(skb)->label_stack_entry = mpls_lse;
6170 
6171 	return 0;
6172 }
6173 EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
6174 
6175 /**
6176  * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
6177  *
6178  * @skb: buffer
6179  *
6180  * Expects skb->data at mac header.
6181  *
6182  * Returns 0 on success, -errno otherwise.
6183  */
6184 int skb_mpls_dec_ttl(struct sk_buff *skb)
6185 {
6186 	u32 lse;
6187 	u8 ttl;
6188 
6189 	if (unlikely(!eth_p_mpls(skb->protocol)))
6190 		return -EINVAL;
6191 
6192 	if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
6193 		return -ENOMEM;
6194 
6195 	lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
6196 	ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
6197 	if (!--ttl)
6198 		return -EINVAL;
6199 
6200 	lse &= ~MPLS_LS_TTL_MASK;
6201 	lse |= ttl << MPLS_LS_TTL_SHIFT;
6202 
6203 	return skb_mpls_update_lse(skb, cpu_to_be32(lse));
6204 }
6205 EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
6206 
6207 /**
6208  * alloc_skb_with_frags - allocate skb with page frags
6209  *
6210  * @header_len: size of linear part
6211  * @data_len: needed length in frags
6212  * @max_page_order: max page order desired.
6213  * @errcode: pointer to error code if any
6214  * @gfp_mask: allocation mask
6215  *
6216  * This can be used to allocate a paged skb, given a maximal order for frags.
6217  */
6218 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
6219 				     unsigned long data_len,
6220 				     int max_page_order,
6221 				     int *errcode,
6222 				     gfp_t gfp_mask)
6223 {
6224 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
6225 	unsigned long chunk;
6226 	struct sk_buff *skb;
6227 	struct page *page;
6228 	int i;
6229 
6230 	*errcode = -EMSGSIZE;
6231 	/* Note this test could be relaxed, if we succeed to allocate
6232 	 * high order pages...
6233 	 */
6234 	if (npages > MAX_SKB_FRAGS)
6235 		return NULL;
6236 
6237 	*errcode = -ENOBUFS;
6238 	skb = alloc_skb(header_len, gfp_mask);
6239 	if (!skb)
6240 		return NULL;
6241 
6242 	skb->truesize += npages << PAGE_SHIFT;
6243 
6244 	for (i = 0; npages > 0; i++) {
6245 		int order = max_page_order;
6246 
6247 		while (order) {
6248 			if (npages >= 1 << order) {
6249 				page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
6250 						   __GFP_COMP |
6251 						   __GFP_NOWARN,
6252 						   order);
6253 				if (page)
6254 					goto fill_page;
6255 				/* Do not retry other high order allocations */
6256 				order = 1;
6257 				max_page_order = 0;
6258 			}
6259 			order--;
6260 		}
6261 		page = alloc_page(gfp_mask);
6262 		if (!page)
6263 			goto failure;
6264 fill_page:
6265 		chunk = min_t(unsigned long, data_len,
6266 			      PAGE_SIZE << order);
6267 		skb_fill_page_desc(skb, i, page, 0, chunk);
6268 		data_len -= chunk;
6269 		npages -= 1 << order;
6270 	}
6271 	return skb;
6272 
6273 failure:
6274 	kfree_skb(skb);
6275 	return NULL;
6276 }
6277 EXPORT_SYMBOL(alloc_skb_with_frags);
6278 
6279 /* carve out the first off bytes from skb when off < headlen */
6280 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
6281 				    const int headlen, gfp_t gfp_mask)
6282 {
6283 	int i;
6284 	unsigned int size = skb_end_offset(skb);
6285 	int new_hlen = headlen - off;
6286 	u8 *data;
6287 
6288 	if (skb_pfmemalloc(skb))
6289 		gfp_mask |= __GFP_MEMALLOC;
6290 
6291 	size = SKB_DATA_ALIGN(size);
6292 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6293 	size = kmalloc_size_roundup(size);
6294 	data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL);
6295 	if (!data)
6296 		return -ENOMEM;
6297 	size = SKB_WITH_OVERHEAD(size);
6298 
6299 	/* Copy real data, and all frags */
6300 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
6301 	skb->len -= off;
6302 
6303 	memcpy((struct skb_shared_info *)(data + size),
6304 	       skb_shinfo(skb),
6305 	       offsetof(struct skb_shared_info,
6306 			frags[skb_shinfo(skb)->nr_frags]));
6307 	if (skb_cloned(skb)) {
6308 		/* drop the old head gracefully */
6309 		if (skb_orphan_frags(skb, gfp_mask)) {
6310 			kfree(data);
6311 			return -ENOMEM;
6312 		}
6313 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6314 			skb_frag_ref(skb, i);
6315 		if (skb_has_frag_list(skb))
6316 			skb_clone_fraglist(skb);
6317 		skb_release_data(skb, SKB_CONSUMED);
6318 	} else {
6319 		/* we can reuse existing recount- all we did was
6320 		 * relocate values
6321 		 */
6322 		skb_free_head(skb);
6323 	}
6324 
6325 	skb->head = data;
6326 	skb->data = data;
6327 	skb->head_frag = 0;
6328 	skb_set_end_offset(skb, size);
6329 	skb_set_tail_pointer(skb, skb_headlen(skb));
6330 	skb_headers_offset_update(skb, 0);
6331 	skb->cloned = 0;
6332 	skb->hdr_len = 0;
6333 	skb->nohdr = 0;
6334 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6335 
6336 	return 0;
6337 }
6338 
6339 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6340 
6341 /* carve out the first eat bytes from skb's frag_list. May recurse into
6342  * pskb_carve()
6343  */
6344 static int pskb_carve_frag_list(struct sk_buff *skb,
6345 				struct skb_shared_info *shinfo, int eat,
6346 				gfp_t gfp_mask)
6347 {
6348 	struct sk_buff *list = shinfo->frag_list;
6349 	struct sk_buff *clone = NULL;
6350 	struct sk_buff *insp = NULL;
6351 
6352 	do {
6353 		if (!list) {
6354 			pr_err("Not enough bytes to eat. Want %d\n", eat);
6355 			return -EFAULT;
6356 		}
6357 		if (list->len <= eat) {
6358 			/* Eaten as whole. */
6359 			eat -= list->len;
6360 			list = list->next;
6361 			insp = list;
6362 		} else {
6363 			/* Eaten partially. */
6364 			if (skb_shared(list)) {
6365 				clone = skb_clone(list, gfp_mask);
6366 				if (!clone)
6367 					return -ENOMEM;
6368 				insp = list->next;
6369 				list = clone;
6370 			} else {
6371 				/* This may be pulled without problems. */
6372 				insp = list;
6373 			}
6374 			if (pskb_carve(list, eat, gfp_mask) < 0) {
6375 				kfree_skb(clone);
6376 				return -ENOMEM;
6377 			}
6378 			break;
6379 		}
6380 	} while (eat);
6381 
6382 	/* Free pulled out fragments. */
6383 	while ((list = shinfo->frag_list) != insp) {
6384 		shinfo->frag_list = list->next;
6385 		consume_skb(list);
6386 	}
6387 	/* And insert new clone at head. */
6388 	if (clone) {
6389 		clone->next = list;
6390 		shinfo->frag_list = clone;
6391 	}
6392 	return 0;
6393 }
6394 
6395 /* carve off first len bytes from skb. Split line (off) is in the
6396  * non-linear part of skb
6397  */
6398 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6399 				       int pos, gfp_t gfp_mask)
6400 {
6401 	int i, k = 0;
6402 	unsigned int size = skb_end_offset(skb);
6403 	u8 *data;
6404 	const int nfrags = skb_shinfo(skb)->nr_frags;
6405 	struct skb_shared_info *shinfo;
6406 
6407 	if (skb_pfmemalloc(skb))
6408 		gfp_mask |= __GFP_MEMALLOC;
6409 
6410 	size = SKB_DATA_ALIGN(size);
6411 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
6412 	size = kmalloc_size_roundup(size);
6413 	data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL);
6414 	if (!data)
6415 		return -ENOMEM;
6416 	size = SKB_WITH_OVERHEAD(size);
6417 
6418 	memcpy((struct skb_shared_info *)(data + size),
6419 	       skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
6420 	if (skb_orphan_frags(skb, gfp_mask)) {
6421 		kfree(data);
6422 		return -ENOMEM;
6423 	}
6424 	shinfo = (struct skb_shared_info *)(data + size);
6425 	for (i = 0; i < nfrags; i++) {
6426 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6427 
6428 		if (pos + fsize > off) {
6429 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6430 
6431 			if (pos < off) {
6432 				/* Split frag.
6433 				 * We have two variants in this case:
6434 				 * 1. Move all the frag to the second
6435 				 *    part, if it is possible. F.e.
6436 				 *    this approach is mandatory for TUX,
6437 				 *    where splitting is expensive.
6438 				 * 2. Split is accurately. We make this.
6439 				 */
6440 				skb_frag_off_add(&shinfo->frags[0], off - pos);
6441 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
6442 			}
6443 			skb_frag_ref(skb, i);
6444 			k++;
6445 		}
6446 		pos += fsize;
6447 	}
6448 	shinfo->nr_frags = k;
6449 	if (skb_has_frag_list(skb))
6450 		skb_clone_fraglist(skb);
6451 
6452 	/* split line is in frag list */
6453 	if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6454 		/* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6455 		if (skb_has_frag_list(skb))
6456 			kfree_skb_list(skb_shinfo(skb)->frag_list);
6457 		kfree(data);
6458 		return -ENOMEM;
6459 	}
6460 	skb_release_data(skb, SKB_CONSUMED);
6461 
6462 	skb->head = data;
6463 	skb->head_frag = 0;
6464 	skb->data = data;
6465 	skb_set_end_offset(skb, size);
6466 	skb_reset_tail_pointer(skb);
6467 	skb_headers_offset_update(skb, 0);
6468 	skb->cloned   = 0;
6469 	skb->hdr_len  = 0;
6470 	skb->nohdr    = 0;
6471 	skb->len -= off;
6472 	skb->data_len = skb->len;
6473 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6474 	return 0;
6475 }
6476 
6477 /* remove len bytes from the beginning of the skb */
6478 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6479 {
6480 	int headlen = skb_headlen(skb);
6481 
6482 	if (len < headlen)
6483 		return pskb_carve_inside_header(skb, len, headlen, gfp);
6484 	else
6485 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6486 }
6487 
6488 /* Extract to_copy bytes starting at off from skb, and return this in
6489  * a new skb
6490  */
6491 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6492 			     int to_copy, gfp_t gfp)
6493 {
6494 	struct sk_buff  *clone = skb_clone(skb, gfp);
6495 
6496 	if (!clone)
6497 		return NULL;
6498 
6499 	if (pskb_carve(clone, off, gfp) < 0 ||
6500 	    pskb_trim(clone, to_copy)) {
6501 		kfree_skb(clone);
6502 		return NULL;
6503 	}
6504 	return clone;
6505 }
6506 EXPORT_SYMBOL(pskb_extract);
6507 
6508 /**
6509  * skb_condense - try to get rid of fragments/frag_list if possible
6510  * @skb: buffer
6511  *
6512  * Can be used to save memory before skb is added to a busy queue.
6513  * If packet has bytes in frags and enough tail room in skb->head,
6514  * pull all of them, so that we can free the frags right now and adjust
6515  * truesize.
6516  * Notes:
6517  *	We do not reallocate skb->head thus can not fail.
6518  *	Caller must re-evaluate skb->truesize if needed.
6519  */
6520 void skb_condense(struct sk_buff *skb)
6521 {
6522 	if (skb->data_len) {
6523 		if (skb->data_len > skb->end - skb->tail ||
6524 		    skb_cloned(skb))
6525 			return;
6526 
6527 		/* Nice, we can free page frag(s) right now */
6528 		__pskb_pull_tail(skb, skb->data_len);
6529 	}
6530 	/* At this point, skb->truesize might be over estimated,
6531 	 * because skb had a fragment, and fragments do not tell
6532 	 * their truesize.
6533 	 * When we pulled its content into skb->head, fragment
6534 	 * was freed, but __pskb_pull_tail() could not possibly
6535 	 * adjust skb->truesize, not knowing the frag truesize.
6536 	 */
6537 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6538 }
6539 EXPORT_SYMBOL(skb_condense);
6540 
6541 #ifdef CONFIG_SKB_EXTENSIONS
6542 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6543 {
6544 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6545 }
6546 
6547 /**
6548  * __skb_ext_alloc - allocate a new skb extensions storage
6549  *
6550  * @flags: See kmalloc().
6551  *
6552  * Returns the newly allocated pointer. The pointer can later attached to a
6553  * skb via __skb_ext_set().
6554  * Note: caller must handle the skb_ext as an opaque data.
6555  */
6556 struct skb_ext *__skb_ext_alloc(gfp_t flags)
6557 {
6558 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6559 
6560 	if (new) {
6561 		memset(new->offset, 0, sizeof(new->offset));
6562 		refcount_set(&new->refcnt, 1);
6563 	}
6564 
6565 	return new;
6566 }
6567 
6568 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6569 					 unsigned int old_active)
6570 {
6571 	struct skb_ext *new;
6572 
6573 	if (refcount_read(&old->refcnt) == 1)
6574 		return old;
6575 
6576 	new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6577 	if (!new)
6578 		return NULL;
6579 
6580 	memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6581 	refcount_set(&new->refcnt, 1);
6582 
6583 #ifdef CONFIG_XFRM
6584 	if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6585 		struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6586 		unsigned int i;
6587 
6588 		for (i = 0; i < sp->len; i++)
6589 			xfrm_state_hold(sp->xvec[i]);
6590 	}
6591 #endif
6592 	__skb_ext_put(old);
6593 	return new;
6594 }
6595 
6596 /**
6597  * __skb_ext_set - attach the specified extension storage to this skb
6598  * @skb: buffer
6599  * @id: extension id
6600  * @ext: extension storage previously allocated via __skb_ext_alloc()
6601  *
6602  * Existing extensions, if any, are cleared.
6603  *
6604  * Returns the pointer to the extension.
6605  */
6606 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6607 		    struct skb_ext *ext)
6608 {
6609 	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6610 
6611 	skb_ext_put(skb);
6612 	newlen = newoff + skb_ext_type_len[id];
6613 	ext->chunks = newlen;
6614 	ext->offset[id] = newoff;
6615 	skb->extensions = ext;
6616 	skb->active_extensions = 1 << id;
6617 	return skb_ext_get_ptr(ext, id);
6618 }
6619 
6620 /**
6621  * skb_ext_add - allocate space for given extension, COW if needed
6622  * @skb: buffer
6623  * @id: extension to allocate space for
6624  *
6625  * Allocates enough space for the given extension.
6626  * If the extension is already present, a pointer to that extension
6627  * is returned.
6628  *
6629  * If the skb was cloned, COW applies and the returned memory can be
6630  * modified without changing the extension space of clones buffers.
6631  *
6632  * Returns pointer to the extension or NULL on allocation failure.
6633  */
6634 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6635 {
6636 	struct skb_ext *new, *old = NULL;
6637 	unsigned int newlen, newoff;
6638 
6639 	if (skb->active_extensions) {
6640 		old = skb->extensions;
6641 
6642 		new = skb_ext_maybe_cow(old, skb->active_extensions);
6643 		if (!new)
6644 			return NULL;
6645 
6646 		if (__skb_ext_exist(new, id))
6647 			goto set_active;
6648 
6649 		newoff = new->chunks;
6650 	} else {
6651 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
6652 
6653 		new = __skb_ext_alloc(GFP_ATOMIC);
6654 		if (!new)
6655 			return NULL;
6656 	}
6657 
6658 	newlen = newoff + skb_ext_type_len[id];
6659 	new->chunks = newlen;
6660 	new->offset[id] = newoff;
6661 set_active:
6662 	skb->slow_gro = 1;
6663 	skb->extensions = new;
6664 	skb->active_extensions |= 1 << id;
6665 	return skb_ext_get_ptr(new, id);
6666 }
6667 EXPORT_SYMBOL(skb_ext_add);
6668 
6669 #ifdef CONFIG_XFRM
6670 static void skb_ext_put_sp(struct sec_path *sp)
6671 {
6672 	unsigned int i;
6673 
6674 	for (i = 0; i < sp->len; i++)
6675 		xfrm_state_put(sp->xvec[i]);
6676 }
6677 #endif
6678 
6679 #ifdef CONFIG_MCTP_FLOWS
6680 static void skb_ext_put_mctp(struct mctp_flow *flow)
6681 {
6682 	if (flow->key)
6683 		mctp_key_unref(flow->key);
6684 }
6685 #endif
6686 
6687 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6688 {
6689 	struct skb_ext *ext = skb->extensions;
6690 
6691 	skb->active_extensions &= ~(1 << id);
6692 	if (skb->active_extensions == 0) {
6693 		skb->extensions = NULL;
6694 		__skb_ext_put(ext);
6695 #ifdef CONFIG_XFRM
6696 	} else if (id == SKB_EXT_SEC_PATH &&
6697 		   refcount_read(&ext->refcnt) == 1) {
6698 		struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6699 
6700 		skb_ext_put_sp(sp);
6701 		sp->len = 0;
6702 #endif
6703 	}
6704 }
6705 EXPORT_SYMBOL(__skb_ext_del);
6706 
6707 void __skb_ext_put(struct skb_ext *ext)
6708 {
6709 	/* If this is last clone, nothing can increment
6710 	 * it after check passes.  Avoids one atomic op.
6711 	 */
6712 	if (refcount_read(&ext->refcnt) == 1)
6713 		goto free_now;
6714 
6715 	if (!refcount_dec_and_test(&ext->refcnt))
6716 		return;
6717 free_now:
6718 #ifdef CONFIG_XFRM
6719 	if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6720 		skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6721 #endif
6722 #ifdef CONFIG_MCTP_FLOWS
6723 	if (__skb_ext_exist(ext, SKB_EXT_MCTP))
6724 		skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP));
6725 #endif
6726 
6727 	kmem_cache_free(skbuff_ext_cache, ext);
6728 }
6729 EXPORT_SYMBOL(__skb_ext_put);
6730 #endif /* CONFIG_SKB_EXTENSIONS */
6731 
6732 /**
6733  * skb_attempt_defer_free - queue skb for remote freeing
6734  * @skb: buffer
6735  *
6736  * Put @skb in a per-cpu list, using the cpu which
6737  * allocated the skb/pages to reduce false sharing
6738  * and memory zone spinlock contention.
6739  */
6740 void skb_attempt_defer_free(struct sk_buff *skb)
6741 {
6742 	int cpu = skb->alloc_cpu;
6743 	struct softnet_data *sd;
6744 	unsigned long flags;
6745 	unsigned int defer_max;
6746 	bool kick;
6747 
6748 	if (WARN_ON_ONCE(cpu >= nr_cpu_ids) ||
6749 	    !cpu_online(cpu) ||
6750 	    cpu == raw_smp_processor_id()) {
6751 nodefer:	__kfree_skb(skb);
6752 		return;
6753 	}
6754 
6755 	sd = &per_cpu(softnet_data, cpu);
6756 	defer_max = READ_ONCE(sysctl_skb_defer_max);
6757 	if (READ_ONCE(sd->defer_count) >= defer_max)
6758 		goto nodefer;
6759 
6760 	spin_lock_irqsave(&sd->defer_lock, flags);
6761 	/* Send an IPI every time queue reaches half capacity. */
6762 	kick = sd->defer_count == (defer_max >> 1);
6763 	/* Paired with the READ_ONCE() few lines above */
6764 	WRITE_ONCE(sd->defer_count, sd->defer_count + 1);
6765 
6766 	skb->next = sd->defer_list;
6767 	/* Paired with READ_ONCE() in skb_defer_free_flush() */
6768 	WRITE_ONCE(sd->defer_list, skb);
6769 	spin_unlock_irqrestore(&sd->defer_lock, flags);
6770 
6771 	/* Make sure to trigger NET_RX_SOFTIRQ on the remote CPU
6772 	 * if we are unlucky enough (this seems very unlikely).
6773 	 */
6774 	if (unlikely(kick) && !cmpxchg(&sd->defer_ipi_scheduled, 0, 1))
6775 		smp_call_function_single_async(cpu, &sd->defer_csd);
6776 }
6777