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