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