xref: /linux/net/core/skbuff.c (revision aaa44952bbd1d4db14a4d676bf9595bb5db7e7b0)
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 static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg,
2505 			    struct kvec *vec, size_t num, size_t size)
2506 {
2507 	struct socket *sock = sk->sk_socket;
2508 
2509 	if (!sock)
2510 		return -EINVAL;
2511 	return kernel_sendmsg(sock, msg, vec, num, size);
2512 }
2513 
2514 static int sendpage_unlocked(struct sock *sk, struct page *page, int offset,
2515 			     size_t size, int flags)
2516 {
2517 	struct socket *sock = sk->sk_socket;
2518 
2519 	if (!sock)
2520 		return -EINVAL;
2521 	return kernel_sendpage(sock, page, offset, size, flags);
2522 }
2523 
2524 typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg,
2525 			    struct kvec *vec, size_t num, size_t size);
2526 typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset,
2527 			     size_t size, int flags);
2528 static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
2529 			   int len, sendmsg_func sendmsg, sendpage_func sendpage)
2530 {
2531 	unsigned int orig_len = len;
2532 	struct sk_buff *head = skb;
2533 	unsigned short fragidx;
2534 	int slen, ret;
2535 
2536 do_frag_list:
2537 
2538 	/* Deal with head data */
2539 	while (offset < skb_headlen(skb) && len) {
2540 		struct kvec kv;
2541 		struct msghdr msg;
2542 
2543 		slen = min_t(int, len, skb_headlen(skb) - offset);
2544 		kv.iov_base = skb->data + offset;
2545 		kv.iov_len = slen;
2546 		memset(&msg, 0, sizeof(msg));
2547 		msg.msg_flags = MSG_DONTWAIT;
2548 
2549 		ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked,
2550 				      sendmsg_unlocked, sk, &msg, &kv, 1, slen);
2551 		if (ret <= 0)
2552 			goto error;
2553 
2554 		offset += ret;
2555 		len -= ret;
2556 	}
2557 
2558 	/* All the data was skb head? */
2559 	if (!len)
2560 		goto out;
2561 
2562 	/* Make offset relative to start of frags */
2563 	offset -= skb_headlen(skb);
2564 
2565 	/* Find where we are in frag list */
2566 	for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2567 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
2568 
2569 		if (offset < skb_frag_size(frag))
2570 			break;
2571 
2572 		offset -= skb_frag_size(frag);
2573 	}
2574 
2575 	for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) {
2576 		skb_frag_t *frag  = &skb_shinfo(skb)->frags[fragidx];
2577 
2578 		slen = min_t(size_t, len, skb_frag_size(frag) - offset);
2579 
2580 		while (slen) {
2581 			ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked,
2582 					      sendpage_unlocked, sk,
2583 					      skb_frag_page(frag),
2584 					      skb_frag_off(frag) + offset,
2585 					      slen, MSG_DONTWAIT);
2586 			if (ret <= 0)
2587 				goto error;
2588 
2589 			len -= ret;
2590 			offset += ret;
2591 			slen -= ret;
2592 		}
2593 
2594 		offset = 0;
2595 	}
2596 
2597 	if (len) {
2598 		/* Process any frag lists */
2599 
2600 		if (skb == head) {
2601 			if (skb_has_frag_list(skb)) {
2602 				skb = skb_shinfo(skb)->frag_list;
2603 				goto do_frag_list;
2604 			}
2605 		} else if (skb->next) {
2606 			skb = skb->next;
2607 			goto do_frag_list;
2608 		}
2609 	}
2610 
2611 out:
2612 	return orig_len - len;
2613 
2614 error:
2615 	return orig_len == len ? ret : orig_len - len;
2616 }
2617 
2618 /* Send skb data on a socket. Socket must be locked. */
2619 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
2620 			 int len)
2621 {
2622 	return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked,
2623 			       kernel_sendpage_locked);
2624 }
2625 EXPORT_SYMBOL_GPL(skb_send_sock_locked);
2626 
2627 /* Send skb data on a socket. Socket must be unlocked. */
2628 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len)
2629 {
2630 	return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked,
2631 			       sendpage_unlocked);
2632 }
2633 
2634 /**
2635  *	skb_store_bits - store bits from kernel buffer to skb
2636  *	@skb: destination buffer
2637  *	@offset: offset in destination
2638  *	@from: source buffer
2639  *	@len: number of bytes to copy
2640  *
2641  *	Copy the specified number of bytes from the source buffer to the
2642  *	destination skb.  This function handles all the messy bits of
2643  *	traversing fragment lists and such.
2644  */
2645 
2646 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2647 {
2648 	int start = skb_headlen(skb);
2649 	struct sk_buff *frag_iter;
2650 	int i, copy;
2651 
2652 	if (offset > (int)skb->len - len)
2653 		goto fault;
2654 
2655 	if ((copy = start - offset) > 0) {
2656 		if (copy > len)
2657 			copy = len;
2658 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
2659 		if ((len -= copy) == 0)
2660 			return 0;
2661 		offset += copy;
2662 		from += copy;
2663 	}
2664 
2665 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2666 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2667 		int end;
2668 
2669 		WARN_ON(start > offset + len);
2670 
2671 		end = start + skb_frag_size(frag);
2672 		if ((copy = end - offset) > 0) {
2673 			u32 p_off, p_len, copied;
2674 			struct page *p;
2675 			u8 *vaddr;
2676 
2677 			if (copy > len)
2678 				copy = len;
2679 
2680 			skb_frag_foreach_page(frag,
2681 					      skb_frag_off(frag) + offset - start,
2682 					      copy, p, p_off, p_len, copied) {
2683 				vaddr = kmap_atomic(p);
2684 				memcpy(vaddr + p_off, from + copied, p_len);
2685 				kunmap_atomic(vaddr);
2686 			}
2687 
2688 			if ((len -= copy) == 0)
2689 				return 0;
2690 			offset += copy;
2691 			from += copy;
2692 		}
2693 		start = end;
2694 	}
2695 
2696 	skb_walk_frags(skb, frag_iter) {
2697 		int end;
2698 
2699 		WARN_ON(start > offset + len);
2700 
2701 		end = start + frag_iter->len;
2702 		if ((copy = end - offset) > 0) {
2703 			if (copy > len)
2704 				copy = len;
2705 			if (skb_store_bits(frag_iter, offset - start,
2706 					   from, copy))
2707 				goto fault;
2708 			if ((len -= copy) == 0)
2709 				return 0;
2710 			offset += copy;
2711 			from += copy;
2712 		}
2713 		start = end;
2714 	}
2715 	if (!len)
2716 		return 0;
2717 
2718 fault:
2719 	return -EFAULT;
2720 }
2721 EXPORT_SYMBOL(skb_store_bits);
2722 
2723 /* Checksum skb data. */
2724 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2725 		      __wsum csum, const struct skb_checksum_ops *ops)
2726 {
2727 	int start = skb_headlen(skb);
2728 	int i, copy = start - offset;
2729 	struct sk_buff *frag_iter;
2730 	int pos = 0;
2731 
2732 	/* Checksum header. */
2733 	if (copy > 0) {
2734 		if (copy > len)
2735 			copy = len;
2736 		csum = INDIRECT_CALL_1(ops->update, csum_partial_ext,
2737 				       skb->data + offset, copy, csum);
2738 		if ((len -= copy) == 0)
2739 			return csum;
2740 		offset += copy;
2741 		pos	= copy;
2742 	}
2743 
2744 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2745 		int end;
2746 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2747 
2748 		WARN_ON(start > offset + len);
2749 
2750 		end = start + skb_frag_size(frag);
2751 		if ((copy = end - offset) > 0) {
2752 			u32 p_off, p_len, copied;
2753 			struct page *p;
2754 			__wsum csum2;
2755 			u8 *vaddr;
2756 
2757 			if (copy > len)
2758 				copy = len;
2759 
2760 			skb_frag_foreach_page(frag,
2761 					      skb_frag_off(frag) + offset - start,
2762 					      copy, p, p_off, p_len, copied) {
2763 				vaddr = kmap_atomic(p);
2764 				csum2 = INDIRECT_CALL_1(ops->update,
2765 							csum_partial_ext,
2766 							vaddr + p_off, p_len, 0);
2767 				kunmap_atomic(vaddr);
2768 				csum = INDIRECT_CALL_1(ops->combine,
2769 						       csum_block_add_ext, csum,
2770 						       csum2, pos, p_len);
2771 				pos += p_len;
2772 			}
2773 
2774 			if (!(len -= copy))
2775 				return csum;
2776 			offset += copy;
2777 		}
2778 		start = end;
2779 	}
2780 
2781 	skb_walk_frags(skb, frag_iter) {
2782 		int end;
2783 
2784 		WARN_ON(start > offset + len);
2785 
2786 		end = start + frag_iter->len;
2787 		if ((copy = end - offset) > 0) {
2788 			__wsum csum2;
2789 			if (copy > len)
2790 				copy = len;
2791 			csum2 = __skb_checksum(frag_iter, offset - start,
2792 					       copy, 0, ops);
2793 			csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext,
2794 					       csum, csum2, pos, copy);
2795 			if ((len -= copy) == 0)
2796 				return csum;
2797 			offset += copy;
2798 			pos    += copy;
2799 		}
2800 		start = end;
2801 	}
2802 	BUG_ON(len);
2803 
2804 	return csum;
2805 }
2806 EXPORT_SYMBOL(__skb_checksum);
2807 
2808 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2809 		    int len, __wsum csum)
2810 {
2811 	const struct skb_checksum_ops ops = {
2812 		.update  = csum_partial_ext,
2813 		.combine = csum_block_add_ext,
2814 	};
2815 
2816 	return __skb_checksum(skb, offset, len, csum, &ops);
2817 }
2818 EXPORT_SYMBOL(skb_checksum);
2819 
2820 /* Both of above in one bottle. */
2821 
2822 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2823 				    u8 *to, int len)
2824 {
2825 	int start = skb_headlen(skb);
2826 	int i, copy = start - offset;
2827 	struct sk_buff *frag_iter;
2828 	int pos = 0;
2829 	__wsum csum = 0;
2830 
2831 	/* Copy header. */
2832 	if (copy > 0) {
2833 		if (copy > len)
2834 			copy = len;
2835 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
2836 						 copy);
2837 		if ((len -= copy) == 0)
2838 			return csum;
2839 		offset += copy;
2840 		to     += copy;
2841 		pos	= copy;
2842 	}
2843 
2844 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2845 		int end;
2846 
2847 		WARN_ON(start > offset + len);
2848 
2849 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2850 		if ((copy = end - offset) > 0) {
2851 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2852 			u32 p_off, p_len, copied;
2853 			struct page *p;
2854 			__wsum csum2;
2855 			u8 *vaddr;
2856 
2857 			if (copy > len)
2858 				copy = len;
2859 
2860 			skb_frag_foreach_page(frag,
2861 					      skb_frag_off(frag) + offset - start,
2862 					      copy, p, p_off, p_len, copied) {
2863 				vaddr = kmap_atomic(p);
2864 				csum2 = csum_partial_copy_nocheck(vaddr + p_off,
2865 								  to + copied,
2866 								  p_len);
2867 				kunmap_atomic(vaddr);
2868 				csum = csum_block_add(csum, csum2, pos);
2869 				pos += p_len;
2870 			}
2871 
2872 			if (!(len -= copy))
2873 				return csum;
2874 			offset += copy;
2875 			to     += copy;
2876 		}
2877 		start = end;
2878 	}
2879 
2880 	skb_walk_frags(skb, frag_iter) {
2881 		__wsum csum2;
2882 		int end;
2883 
2884 		WARN_ON(start > offset + len);
2885 
2886 		end = start + frag_iter->len;
2887 		if ((copy = end - offset) > 0) {
2888 			if (copy > len)
2889 				copy = len;
2890 			csum2 = skb_copy_and_csum_bits(frag_iter,
2891 						       offset - start,
2892 						       to, copy);
2893 			csum = csum_block_add(csum, csum2, pos);
2894 			if ((len -= copy) == 0)
2895 				return csum;
2896 			offset += copy;
2897 			to     += copy;
2898 			pos    += copy;
2899 		}
2900 		start = end;
2901 	}
2902 	BUG_ON(len);
2903 	return csum;
2904 }
2905 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2906 
2907 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len)
2908 {
2909 	__sum16 sum;
2910 
2911 	sum = csum_fold(skb_checksum(skb, 0, len, skb->csum));
2912 	/* See comments in __skb_checksum_complete(). */
2913 	if (likely(!sum)) {
2914 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2915 		    !skb->csum_complete_sw)
2916 			netdev_rx_csum_fault(skb->dev, skb);
2917 	}
2918 	if (!skb_shared(skb))
2919 		skb->csum_valid = !sum;
2920 	return sum;
2921 }
2922 EXPORT_SYMBOL(__skb_checksum_complete_head);
2923 
2924 /* This function assumes skb->csum already holds pseudo header's checksum,
2925  * which has been changed from the hardware checksum, for example, by
2926  * __skb_checksum_validate_complete(). And, the original skb->csum must
2927  * have been validated unsuccessfully for CHECKSUM_COMPLETE case.
2928  *
2929  * It returns non-zero if the recomputed checksum is still invalid, otherwise
2930  * zero. The new checksum is stored back into skb->csum unless the skb is
2931  * shared.
2932  */
2933 __sum16 __skb_checksum_complete(struct sk_buff *skb)
2934 {
2935 	__wsum csum;
2936 	__sum16 sum;
2937 
2938 	csum = skb_checksum(skb, 0, skb->len, 0);
2939 
2940 	sum = csum_fold(csum_add(skb->csum, csum));
2941 	/* This check is inverted, because we already knew the hardware
2942 	 * checksum is invalid before calling this function. So, if the
2943 	 * re-computed checksum is valid instead, then we have a mismatch
2944 	 * between the original skb->csum and skb_checksum(). This means either
2945 	 * the original hardware checksum is incorrect or we screw up skb->csum
2946 	 * when moving skb->data around.
2947 	 */
2948 	if (likely(!sum)) {
2949 		if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
2950 		    !skb->csum_complete_sw)
2951 			netdev_rx_csum_fault(skb->dev, skb);
2952 	}
2953 
2954 	if (!skb_shared(skb)) {
2955 		/* Save full packet checksum */
2956 		skb->csum = csum;
2957 		skb->ip_summed = CHECKSUM_COMPLETE;
2958 		skb->csum_complete_sw = 1;
2959 		skb->csum_valid = !sum;
2960 	}
2961 
2962 	return sum;
2963 }
2964 EXPORT_SYMBOL(__skb_checksum_complete);
2965 
2966 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum)
2967 {
2968 	net_warn_ratelimited(
2969 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
2970 		__func__);
2971 	return 0;
2972 }
2973 
2974 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2,
2975 				       int offset, int len)
2976 {
2977 	net_warn_ratelimited(
2978 		"%s: attempt to compute crc32c without libcrc32c.ko\n",
2979 		__func__);
2980 	return 0;
2981 }
2982 
2983 static const struct skb_checksum_ops default_crc32c_ops = {
2984 	.update  = warn_crc32c_csum_update,
2985 	.combine = warn_crc32c_csum_combine,
2986 };
2987 
2988 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly =
2989 	&default_crc32c_ops;
2990 EXPORT_SYMBOL(crc32c_csum_stub);
2991 
2992  /**
2993  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2994  *	@from: source buffer
2995  *
2996  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2997  *	into skb_zerocopy().
2998  */
2999 unsigned int
3000 skb_zerocopy_headlen(const struct sk_buff *from)
3001 {
3002 	unsigned int hlen = 0;
3003 
3004 	if (!from->head_frag ||
3005 	    skb_headlen(from) < L1_CACHE_BYTES ||
3006 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3007 		hlen = skb_headlen(from);
3008 
3009 	if (skb_has_frag_list(from))
3010 		hlen = from->len;
3011 
3012 	return hlen;
3013 }
3014 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
3015 
3016 /**
3017  *	skb_zerocopy - Zero copy skb to skb
3018  *	@to: destination buffer
3019  *	@from: source buffer
3020  *	@len: number of bytes to copy from source buffer
3021  *	@hlen: size of linear headroom in destination buffer
3022  *
3023  *	Copies up to `len` bytes from `from` to `to` by creating references
3024  *	to the frags in the source buffer.
3025  *
3026  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
3027  *	headroom in the `to` buffer.
3028  *
3029  *	Return value:
3030  *	0: everything is OK
3031  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
3032  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
3033  */
3034 int
3035 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
3036 {
3037 	int i, j = 0;
3038 	int plen = 0; /* length of skb->head fragment */
3039 	int ret;
3040 	struct page *page;
3041 	unsigned int offset;
3042 
3043 	BUG_ON(!from->head_frag && !hlen);
3044 
3045 	/* dont bother with small payloads */
3046 	if (len <= skb_tailroom(to))
3047 		return skb_copy_bits(from, 0, skb_put(to, len), len);
3048 
3049 	if (hlen) {
3050 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
3051 		if (unlikely(ret))
3052 			return ret;
3053 		len -= hlen;
3054 	} else {
3055 		plen = min_t(int, skb_headlen(from), len);
3056 		if (plen) {
3057 			page = virt_to_head_page(from->head);
3058 			offset = from->data - (unsigned char *)page_address(page);
3059 			__skb_fill_page_desc(to, 0, page, offset, plen);
3060 			get_page(page);
3061 			j = 1;
3062 			len -= plen;
3063 		}
3064 	}
3065 
3066 	to->truesize += len + plen;
3067 	to->len += len + plen;
3068 	to->data_len += len + plen;
3069 
3070 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
3071 		skb_tx_error(from);
3072 		return -ENOMEM;
3073 	}
3074 	skb_zerocopy_clone(to, from, GFP_ATOMIC);
3075 
3076 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
3077 		int size;
3078 
3079 		if (!len)
3080 			break;
3081 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
3082 		size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]),
3083 					len);
3084 		skb_frag_size_set(&skb_shinfo(to)->frags[j], size);
3085 		len -= size;
3086 		skb_frag_ref(to, j);
3087 		j++;
3088 	}
3089 	skb_shinfo(to)->nr_frags = j;
3090 
3091 	return 0;
3092 }
3093 EXPORT_SYMBOL_GPL(skb_zerocopy);
3094 
3095 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
3096 {
3097 	__wsum csum;
3098 	long csstart;
3099 
3100 	if (skb->ip_summed == CHECKSUM_PARTIAL)
3101 		csstart = skb_checksum_start_offset(skb);
3102 	else
3103 		csstart = skb_headlen(skb);
3104 
3105 	BUG_ON(csstart > skb_headlen(skb));
3106 
3107 	skb_copy_from_linear_data(skb, to, csstart);
3108 
3109 	csum = 0;
3110 	if (csstart != skb->len)
3111 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
3112 					      skb->len - csstart);
3113 
3114 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
3115 		long csstuff = csstart + skb->csum_offset;
3116 
3117 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
3118 	}
3119 }
3120 EXPORT_SYMBOL(skb_copy_and_csum_dev);
3121 
3122 /**
3123  *	skb_dequeue - remove from the head of the queue
3124  *	@list: list to dequeue from
3125  *
3126  *	Remove the head of the list. The list lock is taken so the function
3127  *	may be used safely with other locking list functions. The head item is
3128  *	returned or %NULL if the list is empty.
3129  */
3130 
3131 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
3132 {
3133 	unsigned long flags;
3134 	struct sk_buff *result;
3135 
3136 	spin_lock_irqsave(&list->lock, flags);
3137 	result = __skb_dequeue(list);
3138 	spin_unlock_irqrestore(&list->lock, flags);
3139 	return result;
3140 }
3141 EXPORT_SYMBOL(skb_dequeue);
3142 
3143 /**
3144  *	skb_dequeue_tail - remove from the tail of the queue
3145  *	@list: list to dequeue from
3146  *
3147  *	Remove the tail of the list. The list lock is taken so the function
3148  *	may be used safely with other locking list functions. The tail item is
3149  *	returned or %NULL if the list is empty.
3150  */
3151 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
3152 {
3153 	unsigned long flags;
3154 	struct sk_buff *result;
3155 
3156 	spin_lock_irqsave(&list->lock, flags);
3157 	result = __skb_dequeue_tail(list);
3158 	spin_unlock_irqrestore(&list->lock, flags);
3159 	return result;
3160 }
3161 EXPORT_SYMBOL(skb_dequeue_tail);
3162 
3163 /**
3164  *	skb_queue_purge - empty a list
3165  *	@list: list to empty
3166  *
3167  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
3168  *	the list and one reference dropped. This function takes the list
3169  *	lock and is atomic with respect to other list locking functions.
3170  */
3171 void skb_queue_purge(struct sk_buff_head *list)
3172 {
3173 	struct sk_buff *skb;
3174 	while ((skb = skb_dequeue(list)) != NULL)
3175 		kfree_skb(skb);
3176 }
3177 EXPORT_SYMBOL(skb_queue_purge);
3178 
3179 /**
3180  *	skb_rbtree_purge - empty a skb rbtree
3181  *	@root: root of the rbtree to empty
3182  *	Return value: the sum of truesizes of all purged skbs.
3183  *
3184  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
3185  *	the list and one reference dropped. This function does not take
3186  *	any lock. Synchronization should be handled by the caller (e.g., TCP
3187  *	out-of-order queue is protected by the socket lock).
3188  */
3189 unsigned int skb_rbtree_purge(struct rb_root *root)
3190 {
3191 	struct rb_node *p = rb_first(root);
3192 	unsigned int sum = 0;
3193 
3194 	while (p) {
3195 		struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode);
3196 
3197 		p = rb_next(p);
3198 		rb_erase(&skb->rbnode, root);
3199 		sum += skb->truesize;
3200 		kfree_skb(skb);
3201 	}
3202 	return sum;
3203 }
3204 
3205 /**
3206  *	skb_queue_head - queue a buffer at the list head
3207  *	@list: list to use
3208  *	@newsk: buffer to queue
3209  *
3210  *	Queue a buffer at the start of the list. This function takes the
3211  *	list lock and can be used safely with other locking &sk_buff functions
3212  *	safely.
3213  *
3214  *	A buffer cannot be placed on two lists at the same time.
3215  */
3216 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
3217 {
3218 	unsigned long flags;
3219 
3220 	spin_lock_irqsave(&list->lock, flags);
3221 	__skb_queue_head(list, newsk);
3222 	spin_unlock_irqrestore(&list->lock, flags);
3223 }
3224 EXPORT_SYMBOL(skb_queue_head);
3225 
3226 /**
3227  *	skb_queue_tail - queue a buffer at the list tail
3228  *	@list: list to use
3229  *	@newsk: buffer to queue
3230  *
3231  *	Queue a buffer at the tail of the list. This function takes the
3232  *	list lock and can be used safely with other locking &sk_buff functions
3233  *	safely.
3234  *
3235  *	A buffer cannot be placed on two lists at the same time.
3236  */
3237 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
3238 {
3239 	unsigned long flags;
3240 
3241 	spin_lock_irqsave(&list->lock, flags);
3242 	__skb_queue_tail(list, newsk);
3243 	spin_unlock_irqrestore(&list->lock, flags);
3244 }
3245 EXPORT_SYMBOL(skb_queue_tail);
3246 
3247 /**
3248  *	skb_unlink	-	remove a buffer from a list
3249  *	@skb: buffer to remove
3250  *	@list: list to use
3251  *
3252  *	Remove a packet from a list. The list locks are taken and this
3253  *	function is atomic with respect to other list locked calls
3254  *
3255  *	You must know what list the SKB is on.
3256  */
3257 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
3258 {
3259 	unsigned long flags;
3260 
3261 	spin_lock_irqsave(&list->lock, flags);
3262 	__skb_unlink(skb, list);
3263 	spin_unlock_irqrestore(&list->lock, flags);
3264 }
3265 EXPORT_SYMBOL(skb_unlink);
3266 
3267 /**
3268  *	skb_append	-	append a buffer
3269  *	@old: buffer to insert after
3270  *	@newsk: buffer to insert
3271  *	@list: list to use
3272  *
3273  *	Place a packet after a given packet in a list. The list locks are taken
3274  *	and this function is atomic with respect to other list locked calls.
3275  *	A buffer cannot be placed on two lists at the same time.
3276  */
3277 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
3278 {
3279 	unsigned long flags;
3280 
3281 	spin_lock_irqsave(&list->lock, flags);
3282 	__skb_queue_after(list, old, newsk);
3283 	spin_unlock_irqrestore(&list->lock, flags);
3284 }
3285 EXPORT_SYMBOL(skb_append);
3286 
3287 static inline void skb_split_inside_header(struct sk_buff *skb,
3288 					   struct sk_buff* skb1,
3289 					   const u32 len, const int pos)
3290 {
3291 	int i;
3292 
3293 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
3294 					 pos - len);
3295 	/* And move data appendix as is. */
3296 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3297 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
3298 
3299 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
3300 	skb_shinfo(skb)->nr_frags  = 0;
3301 	skb1->data_len		   = skb->data_len;
3302 	skb1->len		   += skb1->data_len;
3303 	skb->data_len		   = 0;
3304 	skb->len		   = len;
3305 	skb_set_tail_pointer(skb, len);
3306 }
3307 
3308 static inline void skb_split_no_header(struct sk_buff *skb,
3309 				       struct sk_buff* skb1,
3310 				       const u32 len, int pos)
3311 {
3312 	int i, k = 0;
3313 	const int nfrags = skb_shinfo(skb)->nr_frags;
3314 
3315 	skb_shinfo(skb)->nr_frags = 0;
3316 	skb1->len		  = skb1->data_len = skb->len - len;
3317 	skb->len		  = len;
3318 	skb->data_len		  = len - pos;
3319 
3320 	for (i = 0; i < nfrags; i++) {
3321 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
3322 
3323 		if (pos + size > len) {
3324 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
3325 
3326 			if (pos < len) {
3327 				/* Split frag.
3328 				 * We have two variants in this case:
3329 				 * 1. Move all the frag to the second
3330 				 *    part, if it is possible. F.e.
3331 				 *    this approach is mandatory for TUX,
3332 				 *    where splitting is expensive.
3333 				 * 2. Split is accurately. We make this.
3334 				 */
3335 				skb_frag_ref(skb, i);
3336 				skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos);
3337 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
3338 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
3339 				skb_shinfo(skb)->nr_frags++;
3340 			}
3341 			k++;
3342 		} else
3343 			skb_shinfo(skb)->nr_frags++;
3344 		pos += size;
3345 	}
3346 	skb_shinfo(skb1)->nr_frags = k;
3347 }
3348 
3349 /**
3350  * skb_split - Split fragmented skb to two parts at length len.
3351  * @skb: the buffer to split
3352  * @skb1: the buffer to receive the second part
3353  * @len: new length for skb
3354  */
3355 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
3356 {
3357 	int pos = skb_headlen(skb);
3358 
3359 	skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG;
3360 	skb_zerocopy_clone(skb1, skb, 0);
3361 	if (len < pos)	/* Split line is inside header. */
3362 		skb_split_inside_header(skb, skb1, len, pos);
3363 	else		/* Second chunk has no header, nothing to copy. */
3364 		skb_split_no_header(skb, skb1, len, pos);
3365 }
3366 EXPORT_SYMBOL(skb_split);
3367 
3368 /* Shifting from/to a cloned skb is a no-go.
3369  *
3370  * Caller cannot keep skb_shinfo related pointers past calling here!
3371  */
3372 static int skb_prepare_for_shift(struct sk_buff *skb)
3373 {
3374 	int ret = 0;
3375 
3376 	if (skb_cloned(skb)) {
3377 		/* Save and restore truesize: pskb_expand_head() may reallocate
3378 		 * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we
3379 		 * cannot change truesize at this point.
3380 		 */
3381 		unsigned int save_truesize = skb->truesize;
3382 
3383 		ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
3384 		skb->truesize = save_truesize;
3385 	}
3386 	return ret;
3387 }
3388 
3389 /**
3390  * skb_shift - Shifts paged data partially from skb to another
3391  * @tgt: buffer into which tail data gets added
3392  * @skb: buffer from which the paged data comes from
3393  * @shiftlen: shift up to this many bytes
3394  *
3395  * Attempts to shift up to shiftlen worth of bytes, which may be less than
3396  * the length of the skb, from skb to tgt. Returns number bytes shifted.
3397  * It's up to caller to free skb if everything was shifted.
3398  *
3399  * If @tgt runs out of frags, the whole operation is aborted.
3400  *
3401  * Skb cannot include anything else but paged data while tgt is allowed
3402  * to have non-paged data as well.
3403  *
3404  * TODO: full sized shift could be optimized but that would need
3405  * specialized skb free'er to handle frags without up-to-date nr_frags.
3406  */
3407 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
3408 {
3409 	int from, to, merge, todo;
3410 	skb_frag_t *fragfrom, *fragto;
3411 
3412 	BUG_ON(shiftlen > skb->len);
3413 
3414 	if (skb_headlen(skb))
3415 		return 0;
3416 	if (skb_zcopy(tgt) || skb_zcopy(skb))
3417 		return 0;
3418 
3419 	todo = shiftlen;
3420 	from = 0;
3421 	to = skb_shinfo(tgt)->nr_frags;
3422 	fragfrom = &skb_shinfo(skb)->frags[from];
3423 
3424 	/* Actual merge is delayed until the point when we know we can
3425 	 * commit all, so that we don't have to undo partial changes
3426 	 */
3427 	if (!to ||
3428 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
3429 			      skb_frag_off(fragfrom))) {
3430 		merge = -1;
3431 	} else {
3432 		merge = to - 1;
3433 
3434 		todo -= skb_frag_size(fragfrom);
3435 		if (todo < 0) {
3436 			if (skb_prepare_for_shift(skb) ||
3437 			    skb_prepare_for_shift(tgt))
3438 				return 0;
3439 
3440 			/* All previous frag pointers might be stale! */
3441 			fragfrom = &skb_shinfo(skb)->frags[from];
3442 			fragto = &skb_shinfo(tgt)->frags[merge];
3443 
3444 			skb_frag_size_add(fragto, shiftlen);
3445 			skb_frag_size_sub(fragfrom, shiftlen);
3446 			skb_frag_off_add(fragfrom, shiftlen);
3447 
3448 			goto onlymerged;
3449 		}
3450 
3451 		from++;
3452 	}
3453 
3454 	/* Skip full, not-fitting skb to avoid expensive operations */
3455 	if ((shiftlen == skb->len) &&
3456 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
3457 		return 0;
3458 
3459 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
3460 		return 0;
3461 
3462 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
3463 		if (to == MAX_SKB_FRAGS)
3464 			return 0;
3465 
3466 		fragfrom = &skb_shinfo(skb)->frags[from];
3467 		fragto = &skb_shinfo(tgt)->frags[to];
3468 
3469 		if (todo >= skb_frag_size(fragfrom)) {
3470 			*fragto = *fragfrom;
3471 			todo -= skb_frag_size(fragfrom);
3472 			from++;
3473 			to++;
3474 
3475 		} else {
3476 			__skb_frag_ref(fragfrom);
3477 			skb_frag_page_copy(fragto, fragfrom);
3478 			skb_frag_off_copy(fragto, fragfrom);
3479 			skb_frag_size_set(fragto, todo);
3480 
3481 			skb_frag_off_add(fragfrom, todo);
3482 			skb_frag_size_sub(fragfrom, todo);
3483 			todo = 0;
3484 
3485 			to++;
3486 			break;
3487 		}
3488 	}
3489 
3490 	/* Ready to "commit" this state change to tgt */
3491 	skb_shinfo(tgt)->nr_frags = to;
3492 
3493 	if (merge >= 0) {
3494 		fragfrom = &skb_shinfo(skb)->frags[0];
3495 		fragto = &skb_shinfo(tgt)->frags[merge];
3496 
3497 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3498 		__skb_frag_unref(fragfrom);
3499 	}
3500 
3501 	/* Reposition in the original skb */
3502 	to = 0;
3503 	while (from < skb_shinfo(skb)->nr_frags)
3504 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
3505 	skb_shinfo(skb)->nr_frags = to;
3506 
3507 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
3508 
3509 onlymerged:
3510 	/* Most likely the tgt won't ever need its checksum anymore, skb on
3511 	 * the other hand might need it if it needs to be resent
3512 	 */
3513 	tgt->ip_summed = CHECKSUM_PARTIAL;
3514 	skb->ip_summed = CHECKSUM_PARTIAL;
3515 
3516 	/* Yak, is it really working this way? Some helper please? */
3517 	skb->len -= shiftlen;
3518 	skb->data_len -= shiftlen;
3519 	skb->truesize -= shiftlen;
3520 	tgt->len += shiftlen;
3521 	tgt->data_len += shiftlen;
3522 	tgt->truesize += shiftlen;
3523 
3524 	return shiftlen;
3525 }
3526 
3527 /**
3528  * skb_prepare_seq_read - Prepare a sequential read of skb data
3529  * @skb: the buffer to read
3530  * @from: lower offset of data to be read
3531  * @to: upper offset of data to be read
3532  * @st: state variable
3533  *
3534  * Initializes the specified state variable. Must be called before
3535  * invoking skb_seq_read() for the first time.
3536  */
3537 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
3538 			  unsigned int to, struct skb_seq_state *st)
3539 {
3540 	st->lower_offset = from;
3541 	st->upper_offset = to;
3542 	st->root_skb = st->cur_skb = skb;
3543 	st->frag_idx = st->stepped_offset = 0;
3544 	st->frag_data = NULL;
3545 	st->frag_off = 0;
3546 }
3547 EXPORT_SYMBOL(skb_prepare_seq_read);
3548 
3549 /**
3550  * skb_seq_read - Sequentially read skb data
3551  * @consumed: number of bytes consumed by the caller so far
3552  * @data: destination pointer for data to be returned
3553  * @st: state variable
3554  *
3555  * Reads a block of skb data at @consumed relative to the
3556  * lower offset specified to skb_prepare_seq_read(). Assigns
3557  * the head of the data block to @data and returns the length
3558  * of the block or 0 if the end of the skb data or the upper
3559  * offset has been reached.
3560  *
3561  * The caller is not required to consume all of the data
3562  * returned, i.e. @consumed is typically set to the number
3563  * of bytes already consumed and the next call to
3564  * skb_seq_read() will return the remaining part of the block.
3565  *
3566  * Note 1: The size of each block of data returned can be arbitrary,
3567  *       this limitation is the cost for zerocopy sequential
3568  *       reads of potentially non linear data.
3569  *
3570  * Note 2: Fragment lists within fragments are not implemented
3571  *       at the moment, state->root_skb could be replaced with
3572  *       a stack for this purpose.
3573  */
3574 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
3575 			  struct skb_seq_state *st)
3576 {
3577 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
3578 	skb_frag_t *frag;
3579 
3580 	if (unlikely(abs_offset >= st->upper_offset)) {
3581 		if (st->frag_data) {
3582 			kunmap_atomic(st->frag_data);
3583 			st->frag_data = NULL;
3584 		}
3585 		return 0;
3586 	}
3587 
3588 next_skb:
3589 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
3590 
3591 	if (abs_offset < block_limit && !st->frag_data) {
3592 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
3593 		return block_limit - abs_offset;
3594 	}
3595 
3596 	if (st->frag_idx == 0 && !st->frag_data)
3597 		st->stepped_offset += skb_headlen(st->cur_skb);
3598 
3599 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
3600 		unsigned int pg_idx, pg_off, pg_sz;
3601 
3602 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
3603 
3604 		pg_idx = 0;
3605 		pg_off = skb_frag_off(frag);
3606 		pg_sz = skb_frag_size(frag);
3607 
3608 		if (skb_frag_must_loop(skb_frag_page(frag))) {
3609 			pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT;
3610 			pg_off = offset_in_page(pg_off + st->frag_off);
3611 			pg_sz = min_t(unsigned int, pg_sz - st->frag_off,
3612 						    PAGE_SIZE - pg_off);
3613 		}
3614 
3615 		block_limit = pg_sz + st->stepped_offset;
3616 		if (abs_offset < block_limit) {
3617 			if (!st->frag_data)
3618 				st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx);
3619 
3620 			*data = (u8 *)st->frag_data + pg_off +
3621 				(abs_offset - st->stepped_offset);
3622 
3623 			return block_limit - abs_offset;
3624 		}
3625 
3626 		if (st->frag_data) {
3627 			kunmap_atomic(st->frag_data);
3628 			st->frag_data = NULL;
3629 		}
3630 
3631 		st->stepped_offset += pg_sz;
3632 		st->frag_off += pg_sz;
3633 		if (st->frag_off == skb_frag_size(frag)) {
3634 			st->frag_off = 0;
3635 			st->frag_idx++;
3636 		}
3637 	}
3638 
3639 	if (st->frag_data) {
3640 		kunmap_atomic(st->frag_data);
3641 		st->frag_data = NULL;
3642 	}
3643 
3644 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
3645 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
3646 		st->frag_idx = 0;
3647 		goto next_skb;
3648 	} else if (st->cur_skb->next) {
3649 		st->cur_skb = st->cur_skb->next;
3650 		st->frag_idx = 0;
3651 		goto next_skb;
3652 	}
3653 
3654 	return 0;
3655 }
3656 EXPORT_SYMBOL(skb_seq_read);
3657 
3658 /**
3659  * skb_abort_seq_read - Abort a sequential read of skb data
3660  * @st: state variable
3661  *
3662  * Must be called if skb_seq_read() was not called until it
3663  * returned 0.
3664  */
3665 void skb_abort_seq_read(struct skb_seq_state *st)
3666 {
3667 	if (st->frag_data)
3668 		kunmap_atomic(st->frag_data);
3669 }
3670 EXPORT_SYMBOL(skb_abort_seq_read);
3671 
3672 #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
3673 
3674 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
3675 					  struct ts_config *conf,
3676 					  struct ts_state *state)
3677 {
3678 	return skb_seq_read(offset, text, TS_SKB_CB(state));
3679 }
3680 
3681 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
3682 {
3683 	skb_abort_seq_read(TS_SKB_CB(state));
3684 }
3685 
3686 /**
3687  * skb_find_text - Find a text pattern in skb data
3688  * @skb: the buffer to look in
3689  * @from: search offset
3690  * @to: search limit
3691  * @config: textsearch configuration
3692  *
3693  * Finds a pattern in the skb data according to the specified
3694  * textsearch configuration. Use textsearch_next() to retrieve
3695  * subsequent occurrences of the pattern. Returns the offset
3696  * to the first occurrence or UINT_MAX if no match was found.
3697  */
3698 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
3699 			   unsigned int to, struct ts_config *config)
3700 {
3701 	struct ts_state state;
3702 	unsigned int ret;
3703 
3704 	BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb));
3705 
3706 	config->get_next_block = skb_ts_get_next_block;
3707 	config->finish = skb_ts_finish;
3708 
3709 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
3710 
3711 	ret = textsearch_find(config, &state);
3712 	return (ret <= to - from ? ret : UINT_MAX);
3713 }
3714 EXPORT_SYMBOL(skb_find_text);
3715 
3716 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3717 			 int offset, size_t size)
3718 {
3719 	int i = skb_shinfo(skb)->nr_frags;
3720 
3721 	if (skb_can_coalesce(skb, i, page, offset)) {
3722 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3723 	} else if (i < MAX_SKB_FRAGS) {
3724 		get_page(page);
3725 		skb_fill_page_desc(skb, i, page, offset, size);
3726 	} else {
3727 		return -EMSGSIZE;
3728 	}
3729 
3730 	return 0;
3731 }
3732 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3733 
3734 /**
3735  *	skb_pull_rcsum - pull skb and update receive checksum
3736  *	@skb: buffer to update
3737  *	@len: length of data pulled
3738  *
3739  *	This function performs an skb_pull on the packet and updates
3740  *	the CHECKSUM_COMPLETE checksum.  It should be used on
3741  *	receive path processing instead of skb_pull unless you know
3742  *	that the checksum difference is zero (e.g., a valid IP header)
3743  *	or you are setting ip_summed to CHECKSUM_NONE.
3744  */
3745 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3746 {
3747 	unsigned char *data = skb->data;
3748 
3749 	BUG_ON(len > skb->len);
3750 	__skb_pull(skb, len);
3751 	skb_postpull_rcsum(skb, data, len);
3752 	return skb->data;
3753 }
3754 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3755 
3756 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb)
3757 {
3758 	skb_frag_t head_frag;
3759 	struct page *page;
3760 
3761 	page = virt_to_head_page(frag_skb->head);
3762 	__skb_frag_set_page(&head_frag, page);
3763 	skb_frag_off_set(&head_frag, frag_skb->data -
3764 			 (unsigned char *)page_address(page));
3765 	skb_frag_size_set(&head_frag, skb_headlen(frag_skb));
3766 	return head_frag;
3767 }
3768 
3769 struct sk_buff *skb_segment_list(struct sk_buff *skb,
3770 				 netdev_features_t features,
3771 				 unsigned int offset)
3772 {
3773 	struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
3774 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
3775 	unsigned int delta_truesize = 0;
3776 	unsigned int delta_len = 0;
3777 	struct sk_buff *tail = NULL;
3778 	struct sk_buff *nskb, *tmp;
3779 	int err;
3780 
3781 	skb_push(skb, -skb_network_offset(skb) + offset);
3782 
3783 	skb_shinfo(skb)->frag_list = NULL;
3784 
3785 	do {
3786 		nskb = list_skb;
3787 		list_skb = list_skb->next;
3788 
3789 		err = 0;
3790 		if (skb_shared(nskb)) {
3791 			tmp = skb_clone(nskb, GFP_ATOMIC);
3792 			if (tmp) {
3793 				consume_skb(nskb);
3794 				nskb = tmp;
3795 				err = skb_unclone(nskb, GFP_ATOMIC);
3796 			} else {
3797 				err = -ENOMEM;
3798 			}
3799 		}
3800 
3801 		if (!tail)
3802 			skb->next = nskb;
3803 		else
3804 			tail->next = nskb;
3805 
3806 		if (unlikely(err)) {
3807 			nskb->next = list_skb;
3808 			goto err_linearize;
3809 		}
3810 
3811 		tail = nskb;
3812 
3813 		delta_len += nskb->len;
3814 		delta_truesize += nskb->truesize;
3815 
3816 		skb_push(nskb, -skb_network_offset(nskb) + offset);
3817 
3818 		skb_release_head_state(nskb);
3819 		 __copy_skb_header(nskb, skb);
3820 
3821 		skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb));
3822 		skb_copy_from_linear_data_offset(skb, -tnl_hlen,
3823 						 nskb->data - tnl_hlen,
3824 						 offset + tnl_hlen);
3825 
3826 		if (skb_needs_linearize(nskb, features) &&
3827 		    __skb_linearize(nskb))
3828 			goto err_linearize;
3829 
3830 	} while (list_skb);
3831 
3832 	skb->truesize = skb->truesize - delta_truesize;
3833 	skb->data_len = skb->data_len - delta_len;
3834 	skb->len = skb->len - delta_len;
3835 
3836 	skb_gso_reset(skb);
3837 
3838 	skb->prev = tail;
3839 
3840 	if (skb_needs_linearize(skb, features) &&
3841 	    __skb_linearize(skb))
3842 		goto err_linearize;
3843 
3844 	skb_get(skb);
3845 
3846 	return skb;
3847 
3848 err_linearize:
3849 	kfree_skb_list(skb->next);
3850 	skb->next = NULL;
3851 	return ERR_PTR(-ENOMEM);
3852 }
3853 EXPORT_SYMBOL_GPL(skb_segment_list);
3854 
3855 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
3856 {
3857 	if (unlikely(p->len + skb->len >= 65536))
3858 		return -E2BIG;
3859 
3860 	if (NAPI_GRO_CB(p)->last == p)
3861 		skb_shinfo(p)->frag_list = skb;
3862 	else
3863 		NAPI_GRO_CB(p)->last->next = skb;
3864 
3865 	skb_pull(skb, skb_gro_offset(skb));
3866 
3867 	NAPI_GRO_CB(p)->last = skb;
3868 	NAPI_GRO_CB(p)->count++;
3869 	p->data_len += skb->len;
3870 	p->truesize += skb->truesize;
3871 	p->len += skb->len;
3872 
3873 	NAPI_GRO_CB(skb)->same_flow = 1;
3874 
3875 	return 0;
3876 }
3877 
3878 /**
3879  *	skb_segment - Perform protocol segmentation on skb.
3880  *	@head_skb: buffer to segment
3881  *	@features: features for the output path (see dev->features)
3882  *
3883  *	This function performs segmentation on the given skb.  It returns
3884  *	a pointer to the first in a list of new skbs for the segments.
3885  *	In case of error it returns ERR_PTR(err).
3886  */
3887 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3888 			    netdev_features_t features)
3889 {
3890 	struct sk_buff *segs = NULL;
3891 	struct sk_buff *tail = NULL;
3892 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3893 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3894 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
3895 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3896 	struct sk_buff *frag_skb = head_skb;
3897 	unsigned int offset = doffset;
3898 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3899 	unsigned int partial_segs = 0;
3900 	unsigned int headroom;
3901 	unsigned int len = head_skb->len;
3902 	__be16 proto;
3903 	bool csum, sg;
3904 	int nfrags = skb_shinfo(head_skb)->nr_frags;
3905 	int err = -ENOMEM;
3906 	int i = 0;
3907 	int pos;
3908 
3909 	if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) &&
3910 	    (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) {
3911 		/* gso_size is untrusted, and we have a frag_list with a linear
3912 		 * non head_frag head.
3913 		 *
3914 		 * (we assume checking the first list_skb member suffices;
3915 		 * i.e if either of the list_skb members have non head_frag
3916 		 * head, then the first one has too).
3917 		 *
3918 		 * If head_skb's headlen does not fit requested gso_size, it
3919 		 * means that the frag_list members do NOT terminate on exact
3920 		 * gso_size boundaries. Hence we cannot perform skb_frag_t page
3921 		 * sharing. Therefore we must fallback to copying the frag_list
3922 		 * skbs; we do so by disabling SG.
3923 		 */
3924 		if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb))
3925 			features &= ~NETIF_F_SG;
3926 	}
3927 
3928 	__skb_push(head_skb, doffset);
3929 	proto = skb_network_protocol(head_skb, NULL);
3930 	if (unlikely(!proto))
3931 		return ERR_PTR(-EINVAL);
3932 
3933 	sg = !!(features & NETIF_F_SG);
3934 	csum = !!can_checksum_protocol(features, proto);
3935 
3936 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
3937 		if (!(features & NETIF_F_GSO_PARTIAL)) {
3938 			struct sk_buff *iter;
3939 			unsigned int frag_len;
3940 
3941 			if (!list_skb ||
3942 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
3943 				goto normal;
3944 
3945 			/* If we get here then all the required
3946 			 * GSO features except frag_list are supported.
3947 			 * Try to split the SKB to multiple GSO SKBs
3948 			 * with no frag_list.
3949 			 * Currently we can do that only when the buffers don't
3950 			 * have a linear part and all the buffers except
3951 			 * the last are of the same length.
3952 			 */
3953 			frag_len = list_skb->len;
3954 			skb_walk_frags(head_skb, iter) {
3955 				if (frag_len != iter->len && iter->next)
3956 					goto normal;
3957 				if (skb_headlen(iter) && !iter->head_frag)
3958 					goto normal;
3959 
3960 				len -= iter->len;
3961 			}
3962 
3963 			if (len != frag_len)
3964 				goto normal;
3965 		}
3966 
3967 		/* GSO partial only requires that we trim off any excess that
3968 		 * doesn't fit into an MSS sized block, so take care of that
3969 		 * now.
3970 		 */
3971 		partial_segs = len / mss;
3972 		if (partial_segs > 1)
3973 			mss *= partial_segs;
3974 		else
3975 			partial_segs = 0;
3976 	}
3977 
3978 normal:
3979 	headroom = skb_headroom(head_skb);
3980 	pos = skb_headlen(head_skb);
3981 
3982 	do {
3983 		struct sk_buff *nskb;
3984 		skb_frag_t *nskb_frag;
3985 		int hsize;
3986 		int size;
3987 
3988 		if (unlikely(mss == GSO_BY_FRAGS)) {
3989 			len = list_skb->len;
3990 		} else {
3991 			len = head_skb->len - offset;
3992 			if (len > mss)
3993 				len = mss;
3994 		}
3995 
3996 		hsize = skb_headlen(head_skb) - offset;
3997 
3998 		if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) &&
3999 		    (skb_headlen(list_skb) == len || sg)) {
4000 			BUG_ON(skb_headlen(list_skb) > len);
4001 
4002 			i = 0;
4003 			nfrags = skb_shinfo(list_skb)->nr_frags;
4004 			frag = skb_shinfo(list_skb)->frags;
4005 			frag_skb = list_skb;
4006 			pos += skb_headlen(list_skb);
4007 
4008 			while (pos < offset + len) {
4009 				BUG_ON(i >= nfrags);
4010 
4011 				size = skb_frag_size(frag);
4012 				if (pos + size > offset + len)
4013 					break;
4014 
4015 				i++;
4016 				pos += size;
4017 				frag++;
4018 			}
4019 
4020 			nskb = skb_clone(list_skb, GFP_ATOMIC);
4021 			list_skb = list_skb->next;
4022 
4023 			if (unlikely(!nskb))
4024 				goto err;
4025 
4026 			if (unlikely(pskb_trim(nskb, len))) {
4027 				kfree_skb(nskb);
4028 				goto err;
4029 			}
4030 
4031 			hsize = skb_end_offset(nskb);
4032 			if (skb_cow_head(nskb, doffset + headroom)) {
4033 				kfree_skb(nskb);
4034 				goto err;
4035 			}
4036 
4037 			nskb->truesize += skb_end_offset(nskb) - hsize;
4038 			skb_release_head_state(nskb);
4039 			__skb_push(nskb, doffset);
4040 		} else {
4041 			if (hsize < 0)
4042 				hsize = 0;
4043 			if (hsize > len || !sg)
4044 				hsize = len;
4045 
4046 			nskb = __alloc_skb(hsize + doffset + headroom,
4047 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
4048 					   NUMA_NO_NODE);
4049 
4050 			if (unlikely(!nskb))
4051 				goto err;
4052 
4053 			skb_reserve(nskb, headroom);
4054 			__skb_put(nskb, doffset);
4055 		}
4056 
4057 		if (segs)
4058 			tail->next = nskb;
4059 		else
4060 			segs = nskb;
4061 		tail = nskb;
4062 
4063 		__copy_skb_header(nskb, head_skb);
4064 
4065 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
4066 		skb_reset_mac_len(nskb);
4067 
4068 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
4069 						 nskb->data - tnl_hlen,
4070 						 doffset + tnl_hlen);
4071 
4072 		if (nskb->len == len + doffset)
4073 			goto perform_csum_check;
4074 
4075 		if (!sg) {
4076 			if (!csum) {
4077 				if (!nskb->remcsum_offload)
4078 					nskb->ip_summed = CHECKSUM_NONE;
4079 				SKB_GSO_CB(nskb)->csum =
4080 					skb_copy_and_csum_bits(head_skb, offset,
4081 							       skb_put(nskb,
4082 								       len),
4083 							       len);
4084 				SKB_GSO_CB(nskb)->csum_start =
4085 					skb_headroom(nskb) + doffset;
4086 			} else {
4087 				skb_copy_bits(head_skb, offset,
4088 					      skb_put(nskb, len),
4089 					      len);
4090 			}
4091 			continue;
4092 		}
4093 
4094 		nskb_frag = skb_shinfo(nskb)->frags;
4095 
4096 		skb_copy_from_linear_data_offset(head_skb, offset,
4097 						 skb_put(nskb, hsize), hsize);
4098 
4099 		skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags &
4100 					   SKBFL_SHARED_FRAG;
4101 
4102 		if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4103 		    skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC))
4104 			goto err;
4105 
4106 		while (pos < offset + len) {
4107 			if (i >= nfrags) {
4108 				i = 0;
4109 				nfrags = skb_shinfo(list_skb)->nr_frags;
4110 				frag = skb_shinfo(list_skb)->frags;
4111 				frag_skb = list_skb;
4112 				if (!skb_headlen(list_skb)) {
4113 					BUG_ON(!nfrags);
4114 				} else {
4115 					BUG_ON(!list_skb->head_frag);
4116 
4117 					/* to make room for head_frag. */
4118 					i--;
4119 					frag--;
4120 				}
4121 				if (skb_orphan_frags(frag_skb, GFP_ATOMIC) ||
4122 				    skb_zerocopy_clone(nskb, frag_skb,
4123 						       GFP_ATOMIC))
4124 					goto err;
4125 
4126 				list_skb = list_skb->next;
4127 			}
4128 
4129 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
4130 				     MAX_SKB_FRAGS)) {
4131 				net_warn_ratelimited(
4132 					"skb_segment: too many frags: %u %u\n",
4133 					pos, mss);
4134 				err = -EINVAL;
4135 				goto err;
4136 			}
4137 
4138 			*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
4139 			__skb_frag_ref(nskb_frag);
4140 			size = skb_frag_size(nskb_frag);
4141 
4142 			if (pos < offset) {
4143 				skb_frag_off_add(nskb_frag, offset - pos);
4144 				skb_frag_size_sub(nskb_frag, offset - pos);
4145 			}
4146 
4147 			skb_shinfo(nskb)->nr_frags++;
4148 
4149 			if (pos + size <= offset + len) {
4150 				i++;
4151 				frag++;
4152 				pos += size;
4153 			} else {
4154 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
4155 				goto skip_fraglist;
4156 			}
4157 
4158 			nskb_frag++;
4159 		}
4160 
4161 skip_fraglist:
4162 		nskb->data_len = len - hsize;
4163 		nskb->len += nskb->data_len;
4164 		nskb->truesize += nskb->data_len;
4165 
4166 perform_csum_check:
4167 		if (!csum) {
4168 			if (skb_has_shared_frag(nskb) &&
4169 			    __skb_linearize(nskb))
4170 				goto err;
4171 
4172 			if (!nskb->remcsum_offload)
4173 				nskb->ip_summed = CHECKSUM_NONE;
4174 			SKB_GSO_CB(nskb)->csum =
4175 				skb_checksum(nskb, doffset,
4176 					     nskb->len - doffset, 0);
4177 			SKB_GSO_CB(nskb)->csum_start =
4178 				skb_headroom(nskb) + doffset;
4179 		}
4180 	} while ((offset += len) < head_skb->len);
4181 
4182 	/* Some callers want to get the end of the list.
4183 	 * Put it in segs->prev to avoid walking the list.
4184 	 * (see validate_xmit_skb_list() for example)
4185 	 */
4186 	segs->prev = tail;
4187 
4188 	if (partial_segs) {
4189 		struct sk_buff *iter;
4190 		int type = skb_shinfo(head_skb)->gso_type;
4191 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
4192 
4193 		/* Update type to add partial and then remove dodgy if set */
4194 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
4195 		type &= ~SKB_GSO_DODGY;
4196 
4197 		/* Update GSO info and prepare to start updating headers on
4198 		 * our way back down the stack of protocols.
4199 		 */
4200 		for (iter = segs; iter; iter = iter->next) {
4201 			skb_shinfo(iter)->gso_size = gso_size;
4202 			skb_shinfo(iter)->gso_segs = partial_segs;
4203 			skb_shinfo(iter)->gso_type = type;
4204 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
4205 		}
4206 
4207 		if (tail->len - doffset <= gso_size)
4208 			skb_shinfo(tail)->gso_size = 0;
4209 		else if (tail != segs)
4210 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
4211 	}
4212 
4213 	/* Following permits correct backpressure, for protocols
4214 	 * using skb_set_owner_w().
4215 	 * Idea is to tranfert ownership from head_skb to last segment.
4216 	 */
4217 	if (head_skb->destructor == sock_wfree) {
4218 		swap(tail->truesize, head_skb->truesize);
4219 		swap(tail->destructor, head_skb->destructor);
4220 		swap(tail->sk, head_skb->sk);
4221 	}
4222 	return segs;
4223 
4224 err:
4225 	kfree_skb_list(segs);
4226 	return ERR_PTR(err);
4227 }
4228 EXPORT_SYMBOL_GPL(skb_segment);
4229 
4230 int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
4231 {
4232 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
4233 	unsigned int offset = skb_gro_offset(skb);
4234 	unsigned int headlen = skb_headlen(skb);
4235 	unsigned int len = skb_gro_len(skb);
4236 	unsigned int delta_truesize;
4237 	struct sk_buff *lp;
4238 
4239 	if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush))
4240 		return -E2BIG;
4241 
4242 	lp = NAPI_GRO_CB(p)->last;
4243 	pinfo = skb_shinfo(lp);
4244 
4245 	if (headlen <= offset) {
4246 		skb_frag_t *frag;
4247 		skb_frag_t *frag2;
4248 		int i = skbinfo->nr_frags;
4249 		int nr_frags = pinfo->nr_frags + i;
4250 
4251 		if (nr_frags > MAX_SKB_FRAGS)
4252 			goto merge;
4253 
4254 		offset -= headlen;
4255 		pinfo->nr_frags = nr_frags;
4256 		skbinfo->nr_frags = 0;
4257 
4258 		frag = pinfo->frags + nr_frags;
4259 		frag2 = skbinfo->frags + i;
4260 		do {
4261 			*--frag = *--frag2;
4262 		} while (--i);
4263 
4264 		skb_frag_off_add(frag, offset);
4265 		skb_frag_size_sub(frag, offset);
4266 
4267 		/* all fragments truesize : remove (head size + sk_buff) */
4268 		delta_truesize = skb->truesize -
4269 				 SKB_TRUESIZE(skb_end_offset(skb));
4270 
4271 		skb->truesize -= skb->data_len;
4272 		skb->len -= skb->data_len;
4273 		skb->data_len = 0;
4274 
4275 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
4276 		goto done;
4277 	} else if (skb->head_frag) {
4278 		int nr_frags = pinfo->nr_frags;
4279 		skb_frag_t *frag = pinfo->frags + nr_frags;
4280 		struct page *page = virt_to_head_page(skb->head);
4281 		unsigned int first_size = headlen - offset;
4282 		unsigned int first_offset;
4283 
4284 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
4285 			goto merge;
4286 
4287 		first_offset = skb->data -
4288 			       (unsigned char *)page_address(page) +
4289 			       offset;
4290 
4291 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
4292 
4293 		__skb_frag_set_page(frag, page);
4294 		skb_frag_off_set(frag, first_offset);
4295 		skb_frag_size_set(frag, first_size);
4296 
4297 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
4298 		/* We dont need to clear skbinfo->nr_frags here */
4299 
4300 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4301 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
4302 		goto done;
4303 	}
4304 
4305 merge:
4306 	delta_truesize = skb->truesize;
4307 	if (offset > headlen) {
4308 		unsigned int eat = offset - headlen;
4309 
4310 		skb_frag_off_add(&skbinfo->frags[0], eat);
4311 		skb_frag_size_sub(&skbinfo->frags[0], eat);
4312 		skb->data_len -= eat;
4313 		skb->len -= eat;
4314 		offset = headlen;
4315 	}
4316 
4317 	__skb_pull(skb, offset);
4318 
4319 	if (NAPI_GRO_CB(p)->last == p)
4320 		skb_shinfo(p)->frag_list = skb;
4321 	else
4322 		NAPI_GRO_CB(p)->last->next = skb;
4323 	NAPI_GRO_CB(p)->last = skb;
4324 	__skb_header_release(skb);
4325 	lp = p;
4326 
4327 done:
4328 	NAPI_GRO_CB(p)->count++;
4329 	p->data_len += len;
4330 	p->truesize += delta_truesize;
4331 	p->len += len;
4332 	if (lp != p) {
4333 		lp->data_len += len;
4334 		lp->truesize += delta_truesize;
4335 		lp->len += len;
4336 	}
4337 	NAPI_GRO_CB(skb)->same_flow = 1;
4338 	return 0;
4339 }
4340 
4341 #ifdef CONFIG_SKB_EXTENSIONS
4342 #define SKB_EXT_ALIGN_VALUE	8
4343 #define SKB_EXT_CHUNKSIZEOF(x)	(ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE)
4344 
4345 static const u8 skb_ext_type_len[] = {
4346 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4347 	[SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info),
4348 #endif
4349 #ifdef CONFIG_XFRM
4350 	[SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path),
4351 #endif
4352 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4353 	[TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext),
4354 #endif
4355 #if IS_ENABLED(CONFIG_MPTCP)
4356 	[SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext),
4357 #endif
4358 };
4359 
4360 static __always_inline unsigned int skb_ext_total_length(void)
4361 {
4362 	return SKB_EXT_CHUNKSIZEOF(struct skb_ext) +
4363 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
4364 		skb_ext_type_len[SKB_EXT_BRIDGE_NF] +
4365 #endif
4366 #ifdef CONFIG_XFRM
4367 		skb_ext_type_len[SKB_EXT_SEC_PATH] +
4368 #endif
4369 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
4370 		skb_ext_type_len[TC_SKB_EXT] +
4371 #endif
4372 #if IS_ENABLED(CONFIG_MPTCP)
4373 		skb_ext_type_len[SKB_EXT_MPTCP] +
4374 #endif
4375 		0;
4376 }
4377 
4378 static void skb_extensions_init(void)
4379 {
4380 	BUILD_BUG_ON(SKB_EXT_NUM >= 8);
4381 	BUILD_BUG_ON(skb_ext_total_length() > 255);
4382 
4383 	skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache",
4384 					     SKB_EXT_ALIGN_VALUE * skb_ext_total_length(),
4385 					     0,
4386 					     SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4387 					     NULL);
4388 }
4389 #else
4390 static void skb_extensions_init(void) {}
4391 #endif
4392 
4393 void __init skb_init(void)
4394 {
4395 	skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache",
4396 					      sizeof(struct sk_buff),
4397 					      0,
4398 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4399 					      offsetof(struct sk_buff, cb),
4400 					      sizeof_field(struct sk_buff, cb),
4401 					      NULL);
4402 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
4403 						sizeof(struct sk_buff_fclones),
4404 						0,
4405 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
4406 						NULL);
4407 	skb_extensions_init();
4408 }
4409 
4410 static int
4411 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
4412 	       unsigned int recursion_level)
4413 {
4414 	int start = skb_headlen(skb);
4415 	int i, copy = start - offset;
4416 	struct sk_buff *frag_iter;
4417 	int elt = 0;
4418 
4419 	if (unlikely(recursion_level >= 24))
4420 		return -EMSGSIZE;
4421 
4422 	if (copy > 0) {
4423 		if (copy > len)
4424 			copy = len;
4425 		sg_set_buf(sg, skb->data + offset, copy);
4426 		elt++;
4427 		if ((len -= copy) == 0)
4428 			return elt;
4429 		offset += copy;
4430 	}
4431 
4432 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
4433 		int end;
4434 
4435 		WARN_ON(start > offset + len);
4436 
4437 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
4438 		if ((copy = end - offset) > 0) {
4439 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
4440 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4441 				return -EMSGSIZE;
4442 
4443 			if (copy > len)
4444 				copy = len;
4445 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
4446 				    skb_frag_off(frag) + offset - start);
4447 			elt++;
4448 			if (!(len -= copy))
4449 				return elt;
4450 			offset += copy;
4451 		}
4452 		start = end;
4453 	}
4454 
4455 	skb_walk_frags(skb, frag_iter) {
4456 		int end, ret;
4457 
4458 		WARN_ON(start > offset + len);
4459 
4460 		end = start + frag_iter->len;
4461 		if ((copy = end - offset) > 0) {
4462 			if (unlikely(elt && sg_is_last(&sg[elt - 1])))
4463 				return -EMSGSIZE;
4464 
4465 			if (copy > len)
4466 				copy = len;
4467 			ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start,
4468 					      copy, recursion_level + 1);
4469 			if (unlikely(ret < 0))
4470 				return ret;
4471 			elt += ret;
4472 			if ((len -= copy) == 0)
4473 				return elt;
4474 			offset += copy;
4475 		}
4476 		start = end;
4477 	}
4478 	BUG_ON(len);
4479 	return elt;
4480 }
4481 
4482 /**
4483  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
4484  *	@skb: Socket buffer containing the buffers to be mapped
4485  *	@sg: The scatter-gather list to map into
4486  *	@offset: The offset into the buffer's contents to start mapping
4487  *	@len: Length of buffer space to be mapped
4488  *
4489  *	Fill the specified scatter-gather list with mappings/pointers into a
4490  *	region of the buffer space attached to a socket buffer. Returns either
4491  *	the number of scatterlist items used, or -EMSGSIZE if the contents
4492  *	could not fit.
4493  */
4494 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
4495 {
4496 	int nsg = __skb_to_sgvec(skb, sg, offset, len, 0);
4497 
4498 	if (nsg <= 0)
4499 		return nsg;
4500 
4501 	sg_mark_end(&sg[nsg - 1]);
4502 
4503 	return nsg;
4504 }
4505 EXPORT_SYMBOL_GPL(skb_to_sgvec);
4506 
4507 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
4508  * sglist without mark the sg which contain last skb data as the end.
4509  * So the caller can mannipulate sg list as will when padding new data after
4510  * the first call without calling sg_unmark_end to expend sg list.
4511  *
4512  * Scenario to use skb_to_sgvec_nomark:
4513  * 1. sg_init_table
4514  * 2. skb_to_sgvec_nomark(payload1)
4515  * 3. skb_to_sgvec_nomark(payload2)
4516  *
4517  * This is equivalent to:
4518  * 1. sg_init_table
4519  * 2. skb_to_sgvec(payload1)
4520  * 3. sg_unmark_end
4521  * 4. skb_to_sgvec(payload2)
4522  *
4523  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
4524  * is more preferable.
4525  */
4526 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
4527 			int offset, int len)
4528 {
4529 	return __skb_to_sgvec(skb, sg, offset, len, 0);
4530 }
4531 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
4532 
4533 
4534 
4535 /**
4536  *	skb_cow_data - Check that a socket buffer's data buffers are writable
4537  *	@skb: The socket buffer to check.
4538  *	@tailbits: Amount of trailing space to be added
4539  *	@trailer: Returned pointer to the skb where the @tailbits space begins
4540  *
4541  *	Make sure that the data buffers attached to a socket buffer are
4542  *	writable. If they are not, private copies are made of the data buffers
4543  *	and the socket buffer is set to use these instead.
4544  *
4545  *	If @tailbits is given, make sure that there is space to write @tailbits
4546  *	bytes of data beyond current end of socket buffer.  @trailer will be
4547  *	set to point to the skb in which this space begins.
4548  *
4549  *	The number of scatterlist elements required to completely map the
4550  *	COW'd and extended socket buffer will be returned.
4551  */
4552 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
4553 {
4554 	int copyflag;
4555 	int elt;
4556 	struct sk_buff *skb1, **skb_p;
4557 
4558 	/* If skb is cloned or its head is paged, reallocate
4559 	 * head pulling out all the pages (pages are considered not writable
4560 	 * at the moment even if they are anonymous).
4561 	 */
4562 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
4563 	    !__pskb_pull_tail(skb, __skb_pagelen(skb)))
4564 		return -ENOMEM;
4565 
4566 	/* Easy case. Most of packets will go this way. */
4567 	if (!skb_has_frag_list(skb)) {
4568 		/* A little of trouble, not enough of space for trailer.
4569 		 * This should not happen, when stack is tuned to generate
4570 		 * good frames. OK, on miss we reallocate and reserve even more
4571 		 * space, 128 bytes is fair. */
4572 
4573 		if (skb_tailroom(skb) < tailbits &&
4574 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
4575 			return -ENOMEM;
4576 
4577 		/* Voila! */
4578 		*trailer = skb;
4579 		return 1;
4580 	}
4581 
4582 	/* Misery. We are in troubles, going to mincer fragments... */
4583 
4584 	elt = 1;
4585 	skb_p = &skb_shinfo(skb)->frag_list;
4586 	copyflag = 0;
4587 
4588 	while ((skb1 = *skb_p) != NULL) {
4589 		int ntail = 0;
4590 
4591 		/* The fragment is partially pulled by someone,
4592 		 * this can happen on input. Copy it and everything
4593 		 * after it. */
4594 
4595 		if (skb_shared(skb1))
4596 			copyflag = 1;
4597 
4598 		/* If the skb is the last, worry about trailer. */
4599 
4600 		if (skb1->next == NULL && tailbits) {
4601 			if (skb_shinfo(skb1)->nr_frags ||
4602 			    skb_has_frag_list(skb1) ||
4603 			    skb_tailroom(skb1) < tailbits)
4604 				ntail = tailbits + 128;
4605 		}
4606 
4607 		if (copyflag ||
4608 		    skb_cloned(skb1) ||
4609 		    ntail ||
4610 		    skb_shinfo(skb1)->nr_frags ||
4611 		    skb_has_frag_list(skb1)) {
4612 			struct sk_buff *skb2;
4613 
4614 			/* Fuck, we are miserable poor guys... */
4615 			if (ntail == 0)
4616 				skb2 = skb_copy(skb1, GFP_ATOMIC);
4617 			else
4618 				skb2 = skb_copy_expand(skb1,
4619 						       skb_headroom(skb1),
4620 						       ntail,
4621 						       GFP_ATOMIC);
4622 			if (unlikely(skb2 == NULL))
4623 				return -ENOMEM;
4624 
4625 			if (skb1->sk)
4626 				skb_set_owner_w(skb2, skb1->sk);
4627 
4628 			/* Looking around. Are we still alive?
4629 			 * OK, link new skb, drop old one */
4630 
4631 			skb2->next = skb1->next;
4632 			*skb_p = skb2;
4633 			kfree_skb(skb1);
4634 			skb1 = skb2;
4635 		}
4636 		elt++;
4637 		*trailer = skb1;
4638 		skb_p = &skb1->next;
4639 	}
4640 
4641 	return elt;
4642 }
4643 EXPORT_SYMBOL_GPL(skb_cow_data);
4644 
4645 static void sock_rmem_free(struct sk_buff *skb)
4646 {
4647 	struct sock *sk = skb->sk;
4648 
4649 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
4650 }
4651 
4652 static void skb_set_err_queue(struct sk_buff *skb)
4653 {
4654 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
4655 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
4656 	 */
4657 	skb->pkt_type = PACKET_OUTGOING;
4658 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
4659 }
4660 
4661 /*
4662  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
4663  */
4664 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
4665 {
4666 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
4667 	    (unsigned int)READ_ONCE(sk->sk_rcvbuf))
4668 		return -ENOMEM;
4669 
4670 	skb_orphan(skb);
4671 	skb->sk = sk;
4672 	skb->destructor = sock_rmem_free;
4673 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
4674 	skb_set_err_queue(skb);
4675 
4676 	/* before exiting rcu section, make sure dst is refcounted */
4677 	skb_dst_force(skb);
4678 
4679 	skb_queue_tail(&sk->sk_error_queue, skb);
4680 	if (!sock_flag(sk, SOCK_DEAD))
4681 		sk->sk_error_report(sk);
4682 	return 0;
4683 }
4684 EXPORT_SYMBOL(sock_queue_err_skb);
4685 
4686 static bool is_icmp_err_skb(const struct sk_buff *skb)
4687 {
4688 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
4689 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
4690 }
4691 
4692 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
4693 {
4694 	struct sk_buff_head *q = &sk->sk_error_queue;
4695 	struct sk_buff *skb, *skb_next = NULL;
4696 	bool icmp_next = false;
4697 	unsigned long flags;
4698 
4699 	spin_lock_irqsave(&q->lock, flags);
4700 	skb = __skb_dequeue(q);
4701 	if (skb && (skb_next = skb_peek(q))) {
4702 		icmp_next = is_icmp_err_skb(skb_next);
4703 		if (icmp_next)
4704 			sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
4705 	}
4706 	spin_unlock_irqrestore(&q->lock, flags);
4707 
4708 	if (is_icmp_err_skb(skb) && !icmp_next)
4709 		sk->sk_err = 0;
4710 
4711 	if (skb_next)
4712 		sk->sk_error_report(sk);
4713 
4714 	return skb;
4715 }
4716 EXPORT_SYMBOL(sock_dequeue_err_skb);
4717 
4718 /**
4719  * skb_clone_sk - create clone of skb, and take reference to socket
4720  * @skb: the skb to clone
4721  *
4722  * This function creates a clone of a buffer that holds a reference on
4723  * sk_refcnt.  Buffers created via this function are meant to be
4724  * returned using sock_queue_err_skb, or free via kfree_skb.
4725  *
4726  * When passing buffers allocated with this function to sock_queue_err_skb
4727  * it is necessary to wrap the call with sock_hold/sock_put in order to
4728  * prevent the socket from being released prior to being enqueued on
4729  * the sk_error_queue.
4730  */
4731 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
4732 {
4733 	struct sock *sk = skb->sk;
4734 	struct sk_buff *clone;
4735 
4736 	if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
4737 		return NULL;
4738 
4739 	clone = skb_clone(skb, GFP_ATOMIC);
4740 	if (!clone) {
4741 		sock_put(sk);
4742 		return NULL;
4743 	}
4744 
4745 	clone->sk = sk;
4746 	clone->destructor = sock_efree;
4747 
4748 	return clone;
4749 }
4750 EXPORT_SYMBOL(skb_clone_sk);
4751 
4752 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
4753 					struct sock *sk,
4754 					int tstype,
4755 					bool opt_stats)
4756 {
4757 	struct sock_exterr_skb *serr;
4758 	int err;
4759 
4760 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
4761 
4762 	serr = SKB_EXT_ERR(skb);
4763 	memset(serr, 0, sizeof(*serr));
4764 	serr->ee.ee_errno = ENOMSG;
4765 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
4766 	serr->ee.ee_info = tstype;
4767 	serr->opt_stats = opt_stats;
4768 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
4769 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
4770 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
4771 		if (sk->sk_protocol == IPPROTO_TCP &&
4772 		    sk->sk_type == SOCK_STREAM)
4773 			serr->ee.ee_data -= sk->sk_tskey;
4774 	}
4775 
4776 	err = sock_queue_err_skb(sk, skb);
4777 
4778 	if (err)
4779 		kfree_skb(skb);
4780 }
4781 
4782 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
4783 {
4784 	bool ret;
4785 
4786 	if (likely(sysctl_tstamp_allow_data || tsonly))
4787 		return true;
4788 
4789 	read_lock_bh(&sk->sk_callback_lock);
4790 	ret = sk->sk_socket && sk->sk_socket->file &&
4791 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
4792 	read_unlock_bh(&sk->sk_callback_lock);
4793 	return ret;
4794 }
4795 
4796 void skb_complete_tx_timestamp(struct sk_buff *skb,
4797 			       struct skb_shared_hwtstamps *hwtstamps)
4798 {
4799 	struct sock *sk = skb->sk;
4800 
4801 	if (!skb_may_tx_timestamp(sk, false))
4802 		goto err;
4803 
4804 	/* Take a reference to prevent skb_orphan() from freeing the socket,
4805 	 * but only if the socket refcount is not zero.
4806 	 */
4807 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4808 		*skb_hwtstamps(skb) = *hwtstamps;
4809 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
4810 		sock_put(sk);
4811 		return;
4812 	}
4813 
4814 err:
4815 	kfree_skb(skb);
4816 }
4817 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
4818 
4819 void __skb_tstamp_tx(struct sk_buff *orig_skb,
4820 		     const struct sk_buff *ack_skb,
4821 		     struct skb_shared_hwtstamps *hwtstamps,
4822 		     struct sock *sk, int tstype)
4823 {
4824 	struct sk_buff *skb;
4825 	bool tsonly, opt_stats = false;
4826 
4827 	if (!sk)
4828 		return;
4829 
4830 	if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) &&
4831 	    skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS)
4832 		return;
4833 
4834 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
4835 	if (!skb_may_tx_timestamp(sk, tsonly))
4836 		return;
4837 
4838 	if (tsonly) {
4839 #ifdef CONFIG_INET
4840 		if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
4841 		    sk->sk_protocol == IPPROTO_TCP &&
4842 		    sk->sk_type == SOCK_STREAM) {
4843 			skb = tcp_get_timestamping_opt_stats(sk, orig_skb,
4844 							     ack_skb);
4845 			opt_stats = true;
4846 		} else
4847 #endif
4848 			skb = alloc_skb(0, GFP_ATOMIC);
4849 	} else {
4850 		skb = skb_clone(orig_skb, GFP_ATOMIC);
4851 	}
4852 	if (!skb)
4853 		return;
4854 
4855 	if (tsonly) {
4856 		skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags &
4857 					     SKBTX_ANY_TSTAMP;
4858 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
4859 	}
4860 
4861 	if (hwtstamps)
4862 		*skb_hwtstamps(skb) = *hwtstamps;
4863 	else
4864 		skb->tstamp = ktime_get_real();
4865 
4866 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
4867 }
4868 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
4869 
4870 void skb_tstamp_tx(struct sk_buff *orig_skb,
4871 		   struct skb_shared_hwtstamps *hwtstamps)
4872 {
4873 	return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk,
4874 			       SCM_TSTAMP_SND);
4875 }
4876 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
4877 
4878 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
4879 {
4880 	struct sock *sk = skb->sk;
4881 	struct sock_exterr_skb *serr;
4882 	int err = 1;
4883 
4884 	skb->wifi_acked_valid = 1;
4885 	skb->wifi_acked = acked;
4886 
4887 	serr = SKB_EXT_ERR(skb);
4888 	memset(serr, 0, sizeof(*serr));
4889 	serr->ee.ee_errno = ENOMSG;
4890 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
4891 
4892 	/* Take a reference to prevent skb_orphan() from freeing the socket,
4893 	 * but only if the socket refcount is not zero.
4894 	 */
4895 	if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) {
4896 		err = sock_queue_err_skb(sk, skb);
4897 		sock_put(sk);
4898 	}
4899 	if (err)
4900 		kfree_skb(skb);
4901 }
4902 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
4903 
4904 /**
4905  * skb_partial_csum_set - set up and verify partial csum values for packet
4906  * @skb: the skb to set
4907  * @start: the number of bytes after skb->data to start checksumming.
4908  * @off: the offset from start to place the checksum.
4909  *
4910  * For untrusted partially-checksummed packets, we need to make sure the values
4911  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
4912  *
4913  * This function checks and sets those values and skb->ip_summed: if this
4914  * returns false you should drop the packet.
4915  */
4916 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
4917 {
4918 	u32 csum_end = (u32)start + (u32)off + sizeof(__sum16);
4919 	u32 csum_start = skb_headroom(skb) + (u32)start;
4920 
4921 	if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) {
4922 		net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n",
4923 				     start, off, skb_headroom(skb), skb_headlen(skb));
4924 		return false;
4925 	}
4926 	skb->ip_summed = CHECKSUM_PARTIAL;
4927 	skb->csum_start = csum_start;
4928 	skb->csum_offset = off;
4929 	skb_set_transport_header(skb, start);
4930 	return true;
4931 }
4932 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
4933 
4934 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
4935 			       unsigned int max)
4936 {
4937 	if (skb_headlen(skb) >= len)
4938 		return 0;
4939 
4940 	/* If we need to pullup then pullup to the max, so we
4941 	 * won't need to do it again.
4942 	 */
4943 	if (max > skb->len)
4944 		max = skb->len;
4945 
4946 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
4947 		return -ENOMEM;
4948 
4949 	if (skb_headlen(skb) < len)
4950 		return -EPROTO;
4951 
4952 	return 0;
4953 }
4954 
4955 #define MAX_TCP_HDR_LEN (15 * 4)
4956 
4957 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
4958 				      typeof(IPPROTO_IP) proto,
4959 				      unsigned int off)
4960 {
4961 	int err;
4962 
4963 	switch (proto) {
4964 	case IPPROTO_TCP:
4965 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
4966 					  off + MAX_TCP_HDR_LEN);
4967 		if (!err && !skb_partial_csum_set(skb, off,
4968 						  offsetof(struct tcphdr,
4969 							   check)))
4970 			err = -EPROTO;
4971 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4972 
4973 	case IPPROTO_UDP:
4974 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4975 					  off + sizeof(struct udphdr));
4976 		if (!err && !skb_partial_csum_set(skb, off,
4977 						  offsetof(struct udphdr,
4978 							   check)))
4979 			err = -EPROTO;
4980 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4981 	}
4982 
4983 	return ERR_PTR(-EPROTO);
4984 }
4985 
4986 /* This value should be large enough to cover a tagged ethernet header plus
4987  * maximally sized IP and TCP or UDP headers.
4988  */
4989 #define MAX_IP_HDR_LEN 128
4990 
4991 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4992 {
4993 	unsigned int off;
4994 	bool fragment;
4995 	__sum16 *csum;
4996 	int err;
4997 
4998 	fragment = false;
4999 
5000 	err = skb_maybe_pull_tail(skb,
5001 				  sizeof(struct iphdr),
5002 				  MAX_IP_HDR_LEN);
5003 	if (err < 0)
5004 		goto out;
5005 
5006 	if (ip_is_fragment(ip_hdr(skb)))
5007 		fragment = true;
5008 
5009 	off = ip_hdrlen(skb);
5010 
5011 	err = -EPROTO;
5012 
5013 	if (fragment)
5014 		goto out;
5015 
5016 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
5017 	if (IS_ERR(csum))
5018 		return PTR_ERR(csum);
5019 
5020 	if (recalculate)
5021 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
5022 					   ip_hdr(skb)->daddr,
5023 					   skb->len - off,
5024 					   ip_hdr(skb)->protocol, 0);
5025 	err = 0;
5026 
5027 out:
5028 	return err;
5029 }
5030 
5031 /* This value should be large enough to cover a tagged ethernet header plus
5032  * an IPv6 header, all options, and a maximal TCP or UDP header.
5033  */
5034 #define MAX_IPV6_HDR_LEN 256
5035 
5036 #define OPT_HDR(type, skb, off) \
5037 	(type *)(skb_network_header(skb) + (off))
5038 
5039 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
5040 {
5041 	int err;
5042 	u8 nexthdr;
5043 	unsigned int off;
5044 	unsigned int len;
5045 	bool fragment;
5046 	bool done;
5047 	__sum16 *csum;
5048 
5049 	fragment = false;
5050 	done = false;
5051 
5052 	off = sizeof(struct ipv6hdr);
5053 
5054 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
5055 	if (err < 0)
5056 		goto out;
5057 
5058 	nexthdr = ipv6_hdr(skb)->nexthdr;
5059 
5060 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
5061 	while (off <= len && !done) {
5062 		switch (nexthdr) {
5063 		case IPPROTO_DSTOPTS:
5064 		case IPPROTO_HOPOPTS:
5065 		case IPPROTO_ROUTING: {
5066 			struct ipv6_opt_hdr *hp;
5067 
5068 			err = skb_maybe_pull_tail(skb,
5069 						  off +
5070 						  sizeof(struct ipv6_opt_hdr),
5071 						  MAX_IPV6_HDR_LEN);
5072 			if (err < 0)
5073 				goto out;
5074 
5075 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
5076 			nexthdr = hp->nexthdr;
5077 			off += ipv6_optlen(hp);
5078 			break;
5079 		}
5080 		case IPPROTO_AH: {
5081 			struct ip_auth_hdr *hp;
5082 
5083 			err = skb_maybe_pull_tail(skb,
5084 						  off +
5085 						  sizeof(struct ip_auth_hdr),
5086 						  MAX_IPV6_HDR_LEN);
5087 			if (err < 0)
5088 				goto out;
5089 
5090 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
5091 			nexthdr = hp->nexthdr;
5092 			off += ipv6_authlen(hp);
5093 			break;
5094 		}
5095 		case IPPROTO_FRAGMENT: {
5096 			struct frag_hdr *hp;
5097 
5098 			err = skb_maybe_pull_tail(skb,
5099 						  off +
5100 						  sizeof(struct frag_hdr),
5101 						  MAX_IPV6_HDR_LEN);
5102 			if (err < 0)
5103 				goto out;
5104 
5105 			hp = OPT_HDR(struct frag_hdr, skb, off);
5106 
5107 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
5108 				fragment = true;
5109 
5110 			nexthdr = hp->nexthdr;
5111 			off += sizeof(struct frag_hdr);
5112 			break;
5113 		}
5114 		default:
5115 			done = true;
5116 			break;
5117 		}
5118 	}
5119 
5120 	err = -EPROTO;
5121 
5122 	if (!done || fragment)
5123 		goto out;
5124 
5125 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
5126 	if (IS_ERR(csum))
5127 		return PTR_ERR(csum);
5128 
5129 	if (recalculate)
5130 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5131 					 &ipv6_hdr(skb)->daddr,
5132 					 skb->len - off, nexthdr, 0);
5133 	err = 0;
5134 
5135 out:
5136 	return err;
5137 }
5138 
5139 /**
5140  * skb_checksum_setup - set up partial checksum offset
5141  * @skb: the skb to set up
5142  * @recalculate: if true the pseudo-header checksum will be recalculated
5143  */
5144 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
5145 {
5146 	int err;
5147 
5148 	switch (skb->protocol) {
5149 	case htons(ETH_P_IP):
5150 		err = skb_checksum_setup_ipv4(skb, recalculate);
5151 		break;
5152 
5153 	case htons(ETH_P_IPV6):
5154 		err = skb_checksum_setup_ipv6(skb, recalculate);
5155 		break;
5156 
5157 	default:
5158 		err = -EPROTO;
5159 		break;
5160 	}
5161 
5162 	return err;
5163 }
5164 EXPORT_SYMBOL(skb_checksum_setup);
5165 
5166 /**
5167  * skb_checksum_maybe_trim - maybe trims the given skb
5168  * @skb: the skb to check
5169  * @transport_len: the data length beyond the network header
5170  *
5171  * Checks whether the given skb has data beyond the given transport length.
5172  * If so, returns a cloned skb trimmed to this transport length.
5173  * Otherwise returns the provided skb. Returns NULL in error cases
5174  * (e.g. transport_len exceeds skb length or out-of-memory).
5175  *
5176  * Caller needs to set the skb transport header and free any returned skb if it
5177  * differs from the provided skb.
5178  */
5179 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
5180 					       unsigned int transport_len)
5181 {
5182 	struct sk_buff *skb_chk;
5183 	unsigned int len = skb_transport_offset(skb) + transport_len;
5184 	int ret;
5185 
5186 	if (skb->len < len)
5187 		return NULL;
5188 	else if (skb->len == len)
5189 		return skb;
5190 
5191 	skb_chk = skb_clone(skb, GFP_ATOMIC);
5192 	if (!skb_chk)
5193 		return NULL;
5194 
5195 	ret = pskb_trim_rcsum(skb_chk, len);
5196 	if (ret) {
5197 		kfree_skb(skb_chk);
5198 		return NULL;
5199 	}
5200 
5201 	return skb_chk;
5202 }
5203 
5204 /**
5205  * skb_checksum_trimmed - validate checksum of an skb
5206  * @skb: the skb to check
5207  * @transport_len: the data length beyond the network header
5208  * @skb_chkf: checksum function to use
5209  *
5210  * Applies the given checksum function skb_chkf to the provided skb.
5211  * Returns a checked and maybe trimmed skb. Returns NULL on error.
5212  *
5213  * If the skb has data beyond the given transport length, then a
5214  * trimmed & cloned skb is checked and returned.
5215  *
5216  * Caller needs to set the skb transport header and free any returned skb if it
5217  * differs from the provided skb.
5218  */
5219 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
5220 				     unsigned int transport_len,
5221 				     __sum16(*skb_chkf)(struct sk_buff *skb))
5222 {
5223 	struct sk_buff *skb_chk;
5224 	unsigned int offset = skb_transport_offset(skb);
5225 	__sum16 ret;
5226 
5227 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
5228 	if (!skb_chk)
5229 		goto err;
5230 
5231 	if (!pskb_may_pull(skb_chk, offset))
5232 		goto err;
5233 
5234 	skb_pull_rcsum(skb_chk, offset);
5235 	ret = skb_chkf(skb_chk);
5236 	skb_push_rcsum(skb_chk, offset);
5237 
5238 	if (ret)
5239 		goto err;
5240 
5241 	return skb_chk;
5242 
5243 err:
5244 	if (skb_chk && skb_chk != skb)
5245 		kfree_skb(skb_chk);
5246 
5247 	return NULL;
5248 
5249 }
5250 EXPORT_SYMBOL(skb_checksum_trimmed);
5251 
5252 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
5253 {
5254 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
5255 			     skb->dev->name);
5256 }
5257 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
5258 
5259 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
5260 {
5261 	if (head_stolen) {
5262 		skb_release_head_state(skb);
5263 		kmem_cache_free(skbuff_head_cache, skb);
5264 	} else {
5265 		__kfree_skb(skb);
5266 	}
5267 }
5268 EXPORT_SYMBOL(kfree_skb_partial);
5269 
5270 /**
5271  * skb_try_coalesce - try to merge skb to prior one
5272  * @to: prior buffer
5273  * @from: buffer to add
5274  * @fragstolen: pointer to boolean
5275  * @delta_truesize: how much more was allocated than was requested
5276  */
5277 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
5278 		      bool *fragstolen, int *delta_truesize)
5279 {
5280 	struct skb_shared_info *to_shinfo, *from_shinfo;
5281 	int i, delta, len = from->len;
5282 
5283 	*fragstolen = false;
5284 
5285 	if (skb_cloned(to))
5286 		return false;
5287 
5288 	if (len <= skb_tailroom(to)) {
5289 		if (len)
5290 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
5291 		*delta_truesize = 0;
5292 		return true;
5293 	}
5294 
5295 	to_shinfo = skb_shinfo(to);
5296 	from_shinfo = skb_shinfo(from);
5297 	if (to_shinfo->frag_list || from_shinfo->frag_list)
5298 		return false;
5299 	if (skb_zcopy(to) || skb_zcopy(from))
5300 		return false;
5301 
5302 	if (skb_headlen(from) != 0) {
5303 		struct page *page;
5304 		unsigned int offset;
5305 
5306 		if (to_shinfo->nr_frags +
5307 		    from_shinfo->nr_frags >= MAX_SKB_FRAGS)
5308 			return false;
5309 
5310 		if (skb_head_is_locked(from))
5311 			return false;
5312 
5313 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
5314 
5315 		page = virt_to_head_page(from->head);
5316 		offset = from->data - (unsigned char *)page_address(page);
5317 
5318 		skb_fill_page_desc(to, to_shinfo->nr_frags,
5319 				   page, offset, skb_headlen(from));
5320 		*fragstolen = true;
5321 	} else {
5322 		if (to_shinfo->nr_frags +
5323 		    from_shinfo->nr_frags > MAX_SKB_FRAGS)
5324 			return false;
5325 
5326 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
5327 	}
5328 
5329 	WARN_ON_ONCE(delta < len);
5330 
5331 	memcpy(to_shinfo->frags + to_shinfo->nr_frags,
5332 	       from_shinfo->frags,
5333 	       from_shinfo->nr_frags * sizeof(skb_frag_t));
5334 	to_shinfo->nr_frags += from_shinfo->nr_frags;
5335 
5336 	if (!skb_cloned(from))
5337 		from_shinfo->nr_frags = 0;
5338 
5339 	/* if the skb is not cloned this does nothing
5340 	 * since we set nr_frags to 0.
5341 	 */
5342 	for (i = 0; i < from_shinfo->nr_frags; i++)
5343 		__skb_frag_ref(&from_shinfo->frags[i]);
5344 
5345 	to->truesize += delta;
5346 	to->len += len;
5347 	to->data_len += len;
5348 
5349 	*delta_truesize = delta;
5350 	return true;
5351 }
5352 EXPORT_SYMBOL(skb_try_coalesce);
5353 
5354 /**
5355  * skb_scrub_packet - scrub an skb
5356  *
5357  * @skb: buffer to clean
5358  * @xnet: packet is crossing netns
5359  *
5360  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
5361  * into/from a tunnel. Some information have to be cleared during these
5362  * operations.
5363  * skb_scrub_packet can also be used to clean a skb before injecting it in
5364  * another namespace (@xnet == true). We have to clear all information in the
5365  * skb that could impact namespace isolation.
5366  */
5367 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
5368 {
5369 	skb->pkt_type = PACKET_HOST;
5370 	skb->skb_iif = 0;
5371 	skb->ignore_df = 0;
5372 	skb_dst_drop(skb);
5373 	skb_ext_reset(skb);
5374 	nf_reset_ct(skb);
5375 	nf_reset_trace(skb);
5376 
5377 #ifdef CONFIG_NET_SWITCHDEV
5378 	skb->offload_fwd_mark = 0;
5379 	skb->offload_l3_fwd_mark = 0;
5380 #endif
5381 
5382 	if (!xnet)
5383 		return;
5384 
5385 	ipvs_reset(skb);
5386 	skb->mark = 0;
5387 	skb->tstamp = 0;
5388 }
5389 EXPORT_SYMBOL_GPL(skb_scrub_packet);
5390 
5391 /**
5392  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
5393  *
5394  * @skb: GSO skb
5395  *
5396  * skb_gso_transport_seglen is used to determine the real size of the
5397  * individual segments, including Layer4 headers (TCP/UDP).
5398  *
5399  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
5400  */
5401 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
5402 {
5403 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
5404 	unsigned int thlen = 0;
5405 
5406 	if (skb->encapsulation) {
5407 		thlen = skb_inner_transport_header(skb) -
5408 			skb_transport_header(skb);
5409 
5410 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
5411 			thlen += inner_tcp_hdrlen(skb);
5412 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
5413 		thlen = tcp_hdrlen(skb);
5414 	} else if (unlikely(skb_is_gso_sctp(skb))) {
5415 		thlen = sizeof(struct sctphdr);
5416 	} else if (shinfo->gso_type & SKB_GSO_UDP_L4) {
5417 		thlen = sizeof(struct udphdr);
5418 	}
5419 	/* UFO sets gso_size to the size of the fragmentation
5420 	 * payload, i.e. the size of the L4 (UDP) header is already
5421 	 * accounted for.
5422 	 */
5423 	return thlen + shinfo->gso_size;
5424 }
5425 
5426 /**
5427  * skb_gso_network_seglen - Return length of individual segments of a gso packet
5428  *
5429  * @skb: GSO skb
5430  *
5431  * skb_gso_network_seglen is used to determine the real size of the
5432  * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
5433  *
5434  * The MAC/L2 header is not accounted for.
5435  */
5436 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
5437 {
5438 	unsigned int hdr_len = skb_transport_header(skb) -
5439 			       skb_network_header(skb);
5440 
5441 	return hdr_len + skb_gso_transport_seglen(skb);
5442 }
5443 
5444 /**
5445  * skb_gso_mac_seglen - Return length of individual segments of a gso packet
5446  *
5447  * @skb: GSO skb
5448  *
5449  * skb_gso_mac_seglen is used to determine the real size of the
5450  * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4
5451  * headers (TCP/UDP).
5452  */
5453 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb)
5454 {
5455 	unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
5456 
5457 	return hdr_len + skb_gso_transport_seglen(skb);
5458 }
5459 
5460 /**
5461  * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS
5462  *
5463  * There are a couple of instances where we have a GSO skb, and we
5464  * want to determine what size it would be after it is segmented.
5465  *
5466  * We might want to check:
5467  * -    L3+L4+payload size (e.g. IP forwarding)
5468  * - L2+L3+L4+payload size (e.g. sanity check before passing to driver)
5469  *
5470  * This is a helper to do that correctly considering GSO_BY_FRAGS.
5471  *
5472  * @skb: GSO skb
5473  *
5474  * @seg_len: The segmented length (from skb_gso_*_seglen). In the
5475  *           GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS].
5476  *
5477  * @max_len: The maximum permissible length.
5478  *
5479  * Returns true if the segmented length <= max length.
5480  */
5481 static inline bool skb_gso_size_check(const struct sk_buff *skb,
5482 				      unsigned int seg_len,
5483 				      unsigned int max_len) {
5484 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
5485 	const struct sk_buff *iter;
5486 
5487 	if (shinfo->gso_size != GSO_BY_FRAGS)
5488 		return seg_len <= max_len;
5489 
5490 	/* Undo this so we can re-use header sizes */
5491 	seg_len -= GSO_BY_FRAGS;
5492 
5493 	skb_walk_frags(skb, iter) {
5494 		if (seg_len + skb_headlen(iter) > max_len)
5495 			return false;
5496 	}
5497 
5498 	return true;
5499 }
5500 
5501 /**
5502  * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU?
5503  *
5504  * @skb: GSO skb
5505  * @mtu: MTU to validate against
5506  *
5507  * skb_gso_validate_network_len validates if a given skb will fit a
5508  * wanted MTU once split. It considers L3 headers, L4 headers, and the
5509  * payload.
5510  */
5511 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu)
5512 {
5513 	return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu);
5514 }
5515 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len);
5516 
5517 /**
5518  * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length?
5519  *
5520  * @skb: GSO skb
5521  * @len: length to validate against
5522  *
5523  * skb_gso_validate_mac_len validates if a given skb will fit a wanted
5524  * length once split, including L2, L3 and L4 headers and the payload.
5525  */
5526 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len)
5527 {
5528 	return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len);
5529 }
5530 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len);
5531 
5532 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
5533 {
5534 	int mac_len, meta_len;
5535 	void *meta;
5536 
5537 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
5538 		kfree_skb(skb);
5539 		return NULL;
5540 	}
5541 
5542 	mac_len = skb->data - skb_mac_header(skb);
5543 	if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) {
5544 		memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb),
5545 			mac_len - VLAN_HLEN - ETH_TLEN);
5546 	}
5547 
5548 	meta_len = skb_metadata_len(skb);
5549 	if (meta_len) {
5550 		meta = skb_metadata_end(skb) - meta_len;
5551 		memmove(meta + VLAN_HLEN, meta, meta_len);
5552 	}
5553 
5554 	skb->mac_header += VLAN_HLEN;
5555 	return skb;
5556 }
5557 
5558 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
5559 {
5560 	struct vlan_hdr *vhdr;
5561 	u16 vlan_tci;
5562 
5563 	if (unlikely(skb_vlan_tag_present(skb))) {
5564 		/* vlan_tci is already set-up so leave this for another time */
5565 		return skb;
5566 	}
5567 
5568 	skb = skb_share_check(skb, GFP_ATOMIC);
5569 	if (unlikely(!skb))
5570 		goto err_free;
5571 	/* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */
5572 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short))))
5573 		goto err_free;
5574 
5575 	vhdr = (struct vlan_hdr *)skb->data;
5576 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
5577 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
5578 
5579 	skb_pull_rcsum(skb, VLAN_HLEN);
5580 	vlan_set_encap_proto(skb, vhdr);
5581 
5582 	skb = skb_reorder_vlan_header(skb);
5583 	if (unlikely(!skb))
5584 		goto err_free;
5585 
5586 	skb_reset_network_header(skb);
5587 	if (!skb_transport_header_was_set(skb))
5588 		skb_reset_transport_header(skb);
5589 	skb_reset_mac_len(skb);
5590 
5591 	return skb;
5592 
5593 err_free:
5594 	kfree_skb(skb);
5595 	return NULL;
5596 }
5597 EXPORT_SYMBOL(skb_vlan_untag);
5598 
5599 int skb_ensure_writable(struct sk_buff *skb, int write_len)
5600 {
5601 	if (!pskb_may_pull(skb, write_len))
5602 		return -ENOMEM;
5603 
5604 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
5605 		return 0;
5606 
5607 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
5608 }
5609 EXPORT_SYMBOL(skb_ensure_writable);
5610 
5611 /* remove VLAN header from packet and update csum accordingly.
5612  * expects a non skb_vlan_tag_present skb with a vlan tag payload
5613  */
5614 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
5615 {
5616 	struct vlan_hdr *vhdr;
5617 	int offset = skb->data - skb_mac_header(skb);
5618 	int err;
5619 
5620 	if (WARN_ONCE(offset,
5621 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
5622 		      offset)) {
5623 		return -EINVAL;
5624 	}
5625 
5626 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
5627 	if (unlikely(err))
5628 		return err;
5629 
5630 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5631 
5632 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
5633 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
5634 
5635 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
5636 	__skb_pull(skb, VLAN_HLEN);
5637 
5638 	vlan_set_encap_proto(skb, vhdr);
5639 	skb->mac_header += VLAN_HLEN;
5640 
5641 	if (skb_network_offset(skb) < ETH_HLEN)
5642 		skb_set_network_header(skb, ETH_HLEN);
5643 
5644 	skb_reset_mac_len(skb);
5645 
5646 	return err;
5647 }
5648 EXPORT_SYMBOL(__skb_vlan_pop);
5649 
5650 /* Pop a vlan tag either from hwaccel or from payload.
5651  * Expects skb->data at mac header.
5652  */
5653 int skb_vlan_pop(struct sk_buff *skb)
5654 {
5655 	u16 vlan_tci;
5656 	__be16 vlan_proto;
5657 	int err;
5658 
5659 	if (likely(skb_vlan_tag_present(skb))) {
5660 		__vlan_hwaccel_clear_tag(skb);
5661 	} else {
5662 		if (unlikely(!eth_type_vlan(skb->protocol)))
5663 			return 0;
5664 
5665 		err = __skb_vlan_pop(skb, &vlan_tci);
5666 		if (err)
5667 			return err;
5668 	}
5669 	/* move next vlan tag to hw accel tag */
5670 	if (likely(!eth_type_vlan(skb->protocol)))
5671 		return 0;
5672 
5673 	vlan_proto = skb->protocol;
5674 	err = __skb_vlan_pop(skb, &vlan_tci);
5675 	if (unlikely(err))
5676 		return err;
5677 
5678 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5679 	return 0;
5680 }
5681 EXPORT_SYMBOL(skb_vlan_pop);
5682 
5683 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
5684  * Expects skb->data at mac header.
5685  */
5686 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
5687 {
5688 	if (skb_vlan_tag_present(skb)) {
5689 		int offset = skb->data - skb_mac_header(skb);
5690 		int err;
5691 
5692 		if (WARN_ONCE(offset,
5693 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
5694 			      offset)) {
5695 			return -EINVAL;
5696 		}
5697 
5698 		err = __vlan_insert_tag(skb, skb->vlan_proto,
5699 					skb_vlan_tag_get(skb));
5700 		if (err)
5701 			return err;
5702 
5703 		skb->protocol = skb->vlan_proto;
5704 		skb->mac_len += VLAN_HLEN;
5705 
5706 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
5707 	}
5708 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
5709 	return 0;
5710 }
5711 EXPORT_SYMBOL(skb_vlan_push);
5712 
5713 /**
5714  * skb_eth_pop() - Drop the Ethernet header at the head of a packet
5715  *
5716  * @skb: Socket buffer to modify
5717  *
5718  * Drop the Ethernet header of @skb.
5719  *
5720  * Expects that skb->data points to the mac header and that no VLAN tags are
5721  * present.
5722  *
5723  * Returns 0 on success, -errno otherwise.
5724  */
5725 int skb_eth_pop(struct sk_buff *skb)
5726 {
5727 	if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) ||
5728 	    skb_network_offset(skb) < ETH_HLEN)
5729 		return -EPROTO;
5730 
5731 	skb_pull_rcsum(skb, ETH_HLEN);
5732 	skb_reset_mac_header(skb);
5733 	skb_reset_mac_len(skb);
5734 
5735 	return 0;
5736 }
5737 EXPORT_SYMBOL(skb_eth_pop);
5738 
5739 /**
5740  * skb_eth_push() - Add a new Ethernet header at the head of a packet
5741  *
5742  * @skb: Socket buffer to modify
5743  * @dst: Destination MAC address of the new header
5744  * @src: Source MAC address of the new header
5745  *
5746  * Prepend @skb with a new Ethernet header.
5747  *
5748  * Expects that skb->data points to the mac header, which must be empty.
5749  *
5750  * Returns 0 on success, -errno otherwise.
5751  */
5752 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst,
5753 		 const unsigned char *src)
5754 {
5755 	struct ethhdr *eth;
5756 	int err;
5757 
5758 	if (skb_network_offset(skb) || skb_vlan_tag_present(skb))
5759 		return -EPROTO;
5760 
5761 	err = skb_cow_head(skb, sizeof(*eth));
5762 	if (err < 0)
5763 		return err;
5764 
5765 	skb_push(skb, sizeof(*eth));
5766 	skb_reset_mac_header(skb);
5767 	skb_reset_mac_len(skb);
5768 
5769 	eth = eth_hdr(skb);
5770 	ether_addr_copy(eth->h_dest, dst);
5771 	ether_addr_copy(eth->h_source, src);
5772 	eth->h_proto = skb->protocol;
5773 
5774 	skb_postpush_rcsum(skb, eth, sizeof(*eth));
5775 
5776 	return 0;
5777 }
5778 EXPORT_SYMBOL(skb_eth_push);
5779 
5780 /* Update the ethertype of hdr and the skb csum value if required. */
5781 static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr,
5782 			     __be16 ethertype)
5783 {
5784 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
5785 		__be16 diff[] = { ~hdr->h_proto, ethertype };
5786 
5787 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5788 	}
5789 
5790 	hdr->h_proto = ethertype;
5791 }
5792 
5793 /**
5794  * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of
5795  *                   the packet
5796  *
5797  * @skb: buffer
5798  * @mpls_lse: MPLS label stack entry to push
5799  * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848)
5800  * @mac_len: length of the MAC header
5801  * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is
5802  *            ethernet
5803  *
5804  * Expects skb->data at mac header.
5805  *
5806  * Returns 0 on success, -errno otherwise.
5807  */
5808 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto,
5809 		  int mac_len, bool ethernet)
5810 {
5811 	struct mpls_shim_hdr *lse;
5812 	int err;
5813 
5814 	if (unlikely(!eth_p_mpls(mpls_proto)))
5815 		return -EINVAL;
5816 
5817 	/* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */
5818 	if (skb->encapsulation)
5819 		return -EINVAL;
5820 
5821 	err = skb_cow_head(skb, MPLS_HLEN);
5822 	if (unlikely(err))
5823 		return err;
5824 
5825 	if (!skb->inner_protocol) {
5826 		skb_set_inner_network_header(skb, skb_network_offset(skb));
5827 		skb_set_inner_protocol(skb, skb->protocol);
5828 	}
5829 
5830 	skb_push(skb, MPLS_HLEN);
5831 	memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb),
5832 		mac_len);
5833 	skb_reset_mac_header(skb);
5834 	skb_set_network_header(skb, mac_len);
5835 	skb_reset_mac_len(skb);
5836 
5837 	lse = mpls_hdr(skb);
5838 	lse->label_stack_entry = mpls_lse;
5839 	skb_postpush_rcsum(skb, lse, MPLS_HLEN);
5840 
5841 	if (ethernet && mac_len >= ETH_HLEN)
5842 		skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto);
5843 	skb->protocol = mpls_proto;
5844 
5845 	return 0;
5846 }
5847 EXPORT_SYMBOL_GPL(skb_mpls_push);
5848 
5849 /**
5850  * skb_mpls_pop() - pop the outermost MPLS header
5851  *
5852  * @skb: buffer
5853  * @next_proto: ethertype of header after popped MPLS header
5854  * @mac_len: length of the MAC header
5855  * @ethernet: flag to indicate if the packet is ethernet
5856  *
5857  * Expects skb->data at mac header.
5858  *
5859  * Returns 0 on success, -errno otherwise.
5860  */
5861 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len,
5862 		 bool ethernet)
5863 {
5864 	int err;
5865 
5866 	if (unlikely(!eth_p_mpls(skb->protocol)))
5867 		return 0;
5868 
5869 	err = skb_ensure_writable(skb, mac_len + MPLS_HLEN);
5870 	if (unlikely(err))
5871 		return err;
5872 
5873 	skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN);
5874 	memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb),
5875 		mac_len);
5876 
5877 	__skb_pull(skb, MPLS_HLEN);
5878 	skb_reset_mac_header(skb);
5879 	skb_set_network_header(skb, mac_len);
5880 
5881 	if (ethernet && mac_len >= ETH_HLEN) {
5882 		struct ethhdr *hdr;
5883 
5884 		/* use mpls_hdr() to get ethertype to account for VLANs. */
5885 		hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN);
5886 		skb_mod_eth_type(skb, hdr, next_proto);
5887 	}
5888 	skb->protocol = next_proto;
5889 
5890 	return 0;
5891 }
5892 EXPORT_SYMBOL_GPL(skb_mpls_pop);
5893 
5894 /**
5895  * skb_mpls_update_lse() - modify outermost MPLS header and update csum
5896  *
5897  * @skb: buffer
5898  * @mpls_lse: new MPLS label stack entry to update to
5899  *
5900  * Expects skb->data at mac header.
5901  *
5902  * Returns 0 on success, -errno otherwise.
5903  */
5904 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse)
5905 {
5906 	int err;
5907 
5908 	if (unlikely(!eth_p_mpls(skb->protocol)))
5909 		return -EINVAL;
5910 
5911 	err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN);
5912 	if (unlikely(err))
5913 		return err;
5914 
5915 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
5916 		__be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse };
5917 
5918 		skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum);
5919 	}
5920 
5921 	mpls_hdr(skb)->label_stack_entry = mpls_lse;
5922 
5923 	return 0;
5924 }
5925 EXPORT_SYMBOL_GPL(skb_mpls_update_lse);
5926 
5927 /**
5928  * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header
5929  *
5930  * @skb: buffer
5931  *
5932  * Expects skb->data at mac header.
5933  *
5934  * Returns 0 on success, -errno otherwise.
5935  */
5936 int skb_mpls_dec_ttl(struct sk_buff *skb)
5937 {
5938 	u32 lse;
5939 	u8 ttl;
5940 
5941 	if (unlikely(!eth_p_mpls(skb->protocol)))
5942 		return -EINVAL;
5943 
5944 	if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN))
5945 		return -ENOMEM;
5946 
5947 	lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry);
5948 	ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
5949 	if (!--ttl)
5950 		return -EINVAL;
5951 
5952 	lse &= ~MPLS_LS_TTL_MASK;
5953 	lse |= ttl << MPLS_LS_TTL_SHIFT;
5954 
5955 	return skb_mpls_update_lse(skb, cpu_to_be32(lse));
5956 }
5957 EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl);
5958 
5959 /**
5960  * alloc_skb_with_frags - allocate skb with page frags
5961  *
5962  * @header_len: size of linear part
5963  * @data_len: needed length in frags
5964  * @max_page_order: max page order desired.
5965  * @errcode: pointer to error code if any
5966  * @gfp_mask: allocation mask
5967  *
5968  * This can be used to allocate a paged skb, given a maximal order for frags.
5969  */
5970 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
5971 				     unsigned long data_len,
5972 				     int max_page_order,
5973 				     int *errcode,
5974 				     gfp_t gfp_mask)
5975 {
5976 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
5977 	unsigned long chunk;
5978 	struct sk_buff *skb;
5979 	struct page *page;
5980 	int i;
5981 
5982 	*errcode = -EMSGSIZE;
5983 	/* Note this test could be relaxed, if we succeed to allocate
5984 	 * high order pages...
5985 	 */
5986 	if (npages > MAX_SKB_FRAGS)
5987 		return NULL;
5988 
5989 	*errcode = -ENOBUFS;
5990 	skb = alloc_skb(header_len, gfp_mask);
5991 	if (!skb)
5992 		return NULL;
5993 
5994 	skb->truesize += npages << PAGE_SHIFT;
5995 
5996 	for (i = 0; npages > 0; i++) {
5997 		int order = max_page_order;
5998 
5999 		while (order) {
6000 			if (npages >= 1 << order) {
6001 				page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
6002 						   __GFP_COMP |
6003 						   __GFP_NOWARN,
6004 						   order);
6005 				if (page)
6006 					goto fill_page;
6007 				/* Do not retry other high order allocations */
6008 				order = 1;
6009 				max_page_order = 0;
6010 			}
6011 			order--;
6012 		}
6013 		page = alloc_page(gfp_mask);
6014 		if (!page)
6015 			goto failure;
6016 fill_page:
6017 		chunk = min_t(unsigned long, data_len,
6018 			      PAGE_SIZE << order);
6019 		skb_fill_page_desc(skb, i, page, 0, chunk);
6020 		data_len -= chunk;
6021 		npages -= 1 << order;
6022 	}
6023 	return skb;
6024 
6025 failure:
6026 	kfree_skb(skb);
6027 	return NULL;
6028 }
6029 EXPORT_SYMBOL(alloc_skb_with_frags);
6030 
6031 /* carve out the first off bytes from skb when off < headlen */
6032 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
6033 				    const int headlen, gfp_t gfp_mask)
6034 {
6035 	int i;
6036 	int size = skb_end_offset(skb);
6037 	int new_hlen = headlen - off;
6038 	u8 *data;
6039 
6040 	size = SKB_DATA_ALIGN(size);
6041 
6042 	if (skb_pfmemalloc(skb))
6043 		gfp_mask |= __GFP_MEMALLOC;
6044 	data = kmalloc_reserve(size +
6045 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6046 			       gfp_mask, NUMA_NO_NODE, NULL);
6047 	if (!data)
6048 		return -ENOMEM;
6049 
6050 	size = SKB_WITH_OVERHEAD(ksize(data));
6051 
6052 	/* Copy real data, and all frags */
6053 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
6054 	skb->len -= off;
6055 
6056 	memcpy((struct skb_shared_info *)(data + size),
6057 	       skb_shinfo(skb),
6058 	       offsetof(struct skb_shared_info,
6059 			frags[skb_shinfo(skb)->nr_frags]));
6060 	if (skb_cloned(skb)) {
6061 		/* drop the old head gracefully */
6062 		if (skb_orphan_frags(skb, gfp_mask)) {
6063 			kfree(data);
6064 			return -ENOMEM;
6065 		}
6066 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6067 			skb_frag_ref(skb, i);
6068 		if (skb_has_frag_list(skb))
6069 			skb_clone_fraglist(skb);
6070 		skb_release_data(skb);
6071 	} else {
6072 		/* we can reuse existing recount- all we did was
6073 		 * relocate values
6074 		 */
6075 		skb_free_head(skb);
6076 	}
6077 
6078 	skb->head = data;
6079 	skb->data = data;
6080 	skb->head_frag = 0;
6081 #ifdef NET_SKBUFF_DATA_USES_OFFSET
6082 	skb->end = size;
6083 #else
6084 	skb->end = skb->head + size;
6085 #endif
6086 	skb_set_tail_pointer(skb, skb_headlen(skb));
6087 	skb_headers_offset_update(skb, 0);
6088 	skb->cloned = 0;
6089 	skb->hdr_len = 0;
6090 	skb->nohdr = 0;
6091 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6092 
6093 	return 0;
6094 }
6095 
6096 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
6097 
6098 /* carve out the first eat bytes from skb's frag_list. May recurse into
6099  * pskb_carve()
6100  */
6101 static int pskb_carve_frag_list(struct sk_buff *skb,
6102 				struct skb_shared_info *shinfo, int eat,
6103 				gfp_t gfp_mask)
6104 {
6105 	struct sk_buff *list = shinfo->frag_list;
6106 	struct sk_buff *clone = NULL;
6107 	struct sk_buff *insp = NULL;
6108 
6109 	do {
6110 		if (!list) {
6111 			pr_err("Not enough bytes to eat. Want %d\n", eat);
6112 			return -EFAULT;
6113 		}
6114 		if (list->len <= eat) {
6115 			/* Eaten as whole. */
6116 			eat -= list->len;
6117 			list = list->next;
6118 			insp = list;
6119 		} else {
6120 			/* Eaten partially. */
6121 			if (skb_shared(list)) {
6122 				clone = skb_clone(list, gfp_mask);
6123 				if (!clone)
6124 					return -ENOMEM;
6125 				insp = list->next;
6126 				list = clone;
6127 			} else {
6128 				/* This may be pulled without problems. */
6129 				insp = list;
6130 			}
6131 			if (pskb_carve(list, eat, gfp_mask) < 0) {
6132 				kfree_skb(clone);
6133 				return -ENOMEM;
6134 			}
6135 			break;
6136 		}
6137 	} while (eat);
6138 
6139 	/* Free pulled out fragments. */
6140 	while ((list = shinfo->frag_list) != insp) {
6141 		shinfo->frag_list = list->next;
6142 		kfree_skb(list);
6143 	}
6144 	/* And insert new clone at head. */
6145 	if (clone) {
6146 		clone->next = list;
6147 		shinfo->frag_list = clone;
6148 	}
6149 	return 0;
6150 }
6151 
6152 /* carve off first len bytes from skb. Split line (off) is in the
6153  * non-linear part of skb
6154  */
6155 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
6156 				       int pos, gfp_t gfp_mask)
6157 {
6158 	int i, k = 0;
6159 	int size = skb_end_offset(skb);
6160 	u8 *data;
6161 	const int nfrags = skb_shinfo(skb)->nr_frags;
6162 	struct skb_shared_info *shinfo;
6163 
6164 	size = SKB_DATA_ALIGN(size);
6165 
6166 	if (skb_pfmemalloc(skb))
6167 		gfp_mask |= __GFP_MEMALLOC;
6168 	data = kmalloc_reserve(size +
6169 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
6170 			       gfp_mask, NUMA_NO_NODE, NULL);
6171 	if (!data)
6172 		return -ENOMEM;
6173 
6174 	size = SKB_WITH_OVERHEAD(ksize(data));
6175 
6176 	memcpy((struct skb_shared_info *)(data + size),
6177 	       skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0]));
6178 	if (skb_orphan_frags(skb, gfp_mask)) {
6179 		kfree(data);
6180 		return -ENOMEM;
6181 	}
6182 	shinfo = (struct skb_shared_info *)(data + size);
6183 	for (i = 0; i < nfrags; i++) {
6184 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
6185 
6186 		if (pos + fsize > off) {
6187 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
6188 
6189 			if (pos < off) {
6190 				/* Split frag.
6191 				 * We have two variants in this case:
6192 				 * 1. Move all the frag to the second
6193 				 *    part, if it is possible. F.e.
6194 				 *    this approach is mandatory for TUX,
6195 				 *    where splitting is expensive.
6196 				 * 2. Split is accurately. We make this.
6197 				 */
6198 				skb_frag_off_add(&shinfo->frags[0], off - pos);
6199 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
6200 			}
6201 			skb_frag_ref(skb, i);
6202 			k++;
6203 		}
6204 		pos += fsize;
6205 	}
6206 	shinfo->nr_frags = k;
6207 	if (skb_has_frag_list(skb))
6208 		skb_clone_fraglist(skb);
6209 
6210 	/* split line is in frag list */
6211 	if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) {
6212 		/* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */
6213 		if (skb_has_frag_list(skb))
6214 			kfree_skb_list(skb_shinfo(skb)->frag_list);
6215 		kfree(data);
6216 		return -ENOMEM;
6217 	}
6218 	skb_release_data(skb);
6219 
6220 	skb->head = data;
6221 	skb->head_frag = 0;
6222 	skb->data = data;
6223 #ifdef NET_SKBUFF_DATA_USES_OFFSET
6224 	skb->end = size;
6225 #else
6226 	skb->end = skb->head + size;
6227 #endif
6228 	skb_reset_tail_pointer(skb);
6229 	skb_headers_offset_update(skb, 0);
6230 	skb->cloned   = 0;
6231 	skb->hdr_len  = 0;
6232 	skb->nohdr    = 0;
6233 	skb->len -= off;
6234 	skb->data_len = skb->len;
6235 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6236 	return 0;
6237 }
6238 
6239 /* remove len bytes from the beginning of the skb */
6240 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
6241 {
6242 	int headlen = skb_headlen(skb);
6243 
6244 	if (len < headlen)
6245 		return pskb_carve_inside_header(skb, len, headlen, gfp);
6246 	else
6247 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
6248 }
6249 
6250 /* Extract to_copy bytes starting at off from skb, and return this in
6251  * a new skb
6252  */
6253 struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
6254 			     int to_copy, gfp_t gfp)
6255 {
6256 	struct sk_buff  *clone = skb_clone(skb, gfp);
6257 
6258 	if (!clone)
6259 		return NULL;
6260 
6261 	if (pskb_carve(clone, off, gfp) < 0 ||
6262 	    pskb_trim(clone, to_copy)) {
6263 		kfree_skb(clone);
6264 		return NULL;
6265 	}
6266 	return clone;
6267 }
6268 EXPORT_SYMBOL(pskb_extract);
6269 
6270 /**
6271  * skb_condense - try to get rid of fragments/frag_list if possible
6272  * @skb: buffer
6273  *
6274  * Can be used to save memory before skb is added to a busy queue.
6275  * If packet has bytes in frags and enough tail room in skb->head,
6276  * pull all of them, so that we can free the frags right now and adjust
6277  * truesize.
6278  * Notes:
6279  *	We do not reallocate skb->head thus can not fail.
6280  *	Caller must re-evaluate skb->truesize if needed.
6281  */
6282 void skb_condense(struct sk_buff *skb)
6283 {
6284 	if (skb->data_len) {
6285 		if (skb->data_len > skb->end - skb->tail ||
6286 		    skb_cloned(skb))
6287 			return;
6288 
6289 		/* Nice, we can free page frag(s) right now */
6290 		__pskb_pull_tail(skb, skb->data_len);
6291 	}
6292 	/* At this point, skb->truesize might be over estimated,
6293 	 * because skb had a fragment, and fragments do not tell
6294 	 * their truesize.
6295 	 * When we pulled its content into skb->head, fragment
6296 	 * was freed, but __pskb_pull_tail() could not possibly
6297 	 * adjust skb->truesize, not knowing the frag truesize.
6298 	 */
6299 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
6300 }
6301 
6302 #ifdef CONFIG_SKB_EXTENSIONS
6303 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id)
6304 {
6305 	return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE);
6306 }
6307 
6308 /**
6309  * __skb_ext_alloc - allocate a new skb extensions storage
6310  *
6311  * @flags: See kmalloc().
6312  *
6313  * Returns the newly allocated pointer. The pointer can later attached to a
6314  * skb via __skb_ext_set().
6315  * Note: caller must handle the skb_ext as an opaque data.
6316  */
6317 struct skb_ext *__skb_ext_alloc(gfp_t flags)
6318 {
6319 	struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags);
6320 
6321 	if (new) {
6322 		memset(new->offset, 0, sizeof(new->offset));
6323 		refcount_set(&new->refcnt, 1);
6324 	}
6325 
6326 	return new;
6327 }
6328 
6329 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
6330 					 unsigned int old_active)
6331 {
6332 	struct skb_ext *new;
6333 
6334 	if (refcount_read(&old->refcnt) == 1)
6335 		return old;
6336 
6337 	new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC);
6338 	if (!new)
6339 		return NULL;
6340 
6341 	memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE);
6342 	refcount_set(&new->refcnt, 1);
6343 
6344 #ifdef CONFIG_XFRM
6345 	if (old_active & (1 << SKB_EXT_SEC_PATH)) {
6346 		struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH);
6347 		unsigned int i;
6348 
6349 		for (i = 0; i < sp->len; i++)
6350 			xfrm_state_hold(sp->xvec[i]);
6351 	}
6352 #endif
6353 	__skb_ext_put(old);
6354 	return new;
6355 }
6356 
6357 /**
6358  * __skb_ext_set - attach the specified extension storage to this skb
6359  * @skb: buffer
6360  * @id: extension id
6361  * @ext: extension storage previously allocated via __skb_ext_alloc()
6362  *
6363  * Existing extensions, if any, are cleared.
6364  *
6365  * Returns the pointer to the extension.
6366  */
6367 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id,
6368 		    struct skb_ext *ext)
6369 {
6370 	unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext);
6371 
6372 	skb_ext_put(skb);
6373 	newlen = newoff + skb_ext_type_len[id];
6374 	ext->chunks = newlen;
6375 	ext->offset[id] = newoff;
6376 	skb->extensions = ext;
6377 	skb->active_extensions = 1 << id;
6378 	return skb_ext_get_ptr(ext, id);
6379 }
6380 
6381 /**
6382  * skb_ext_add - allocate space for given extension, COW if needed
6383  * @skb: buffer
6384  * @id: extension to allocate space for
6385  *
6386  * Allocates enough space for the given extension.
6387  * If the extension is already present, a pointer to that extension
6388  * is returned.
6389  *
6390  * If the skb was cloned, COW applies and the returned memory can be
6391  * modified without changing the extension space of clones buffers.
6392  *
6393  * Returns pointer to the extension or NULL on allocation failure.
6394  */
6395 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id)
6396 {
6397 	struct skb_ext *new, *old = NULL;
6398 	unsigned int newlen, newoff;
6399 
6400 	if (skb->active_extensions) {
6401 		old = skb->extensions;
6402 
6403 		new = skb_ext_maybe_cow(old, skb->active_extensions);
6404 		if (!new)
6405 			return NULL;
6406 
6407 		if (__skb_ext_exist(new, id))
6408 			goto set_active;
6409 
6410 		newoff = new->chunks;
6411 	} else {
6412 		newoff = SKB_EXT_CHUNKSIZEOF(*new);
6413 
6414 		new = __skb_ext_alloc(GFP_ATOMIC);
6415 		if (!new)
6416 			return NULL;
6417 	}
6418 
6419 	newlen = newoff + skb_ext_type_len[id];
6420 	new->chunks = newlen;
6421 	new->offset[id] = newoff;
6422 set_active:
6423 	skb->extensions = new;
6424 	skb->active_extensions |= 1 << id;
6425 	return skb_ext_get_ptr(new, id);
6426 }
6427 EXPORT_SYMBOL(skb_ext_add);
6428 
6429 #ifdef CONFIG_XFRM
6430 static void skb_ext_put_sp(struct sec_path *sp)
6431 {
6432 	unsigned int i;
6433 
6434 	for (i = 0; i < sp->len; i++)
6435 		xfrm_state_put(sp->xvec[i]);
6436 }
6437 #endif
6438 
6439 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id)
6440 {
6441 	struct skb_ext *ext = skb->extensions;
6442 
6443 	skb->active_extensions &= ~(1 << id);
6444 	if (skb->active_extensions == 0) {
6445 		skb->extensions = NULL;
6446 		__skb_ext_put(ext);
6447 #ifdef CONFIG_XFRM
6448 	} else if (id == SKB_EXT_SEC_PATH &&
6449 		   refcount_read(&ext->refcnt) == 1) {
6450 		struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH);
6451 
6452 		skb_ext_put_sp(sp);
6453 		sp->len = 0;
6454 #endif
6455 	}
6456 }
6457 EXPORT_SYMBOL(__skb_ext_del);
6458 
6459 void __skb_ext_put(struct skb_ext *ext)
6460 {
6461 	/* If this is last clone, nothing can increment
6462 	 * it after check passes.  Avoids one atomic op.
6463 	 */
6464 	if (refcount_read(&ext->refcnt) == 1)
6465 		goto free_now;
6466 
6467 	if (!refcount_dec_and_test(&ext->refcnt))
6468 		return;
6469 free_now:
6470 #ifdef CONFIG_XFRM
6471 	if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH))
6472 		skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH));
6473 #endif
6474 
6475 	kmem_cache_free(skbuff_ext_cache, ext);
6476 }
6477 EXPORT_SYMBOL(__skb_ext_put);
6478 #endif /* CONFIG_SKB_EXTENSIONS */
6479