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