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