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