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