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