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