xref: /linux/net/core/skbuff.c (revision 3f2fb9a834cb1fcddbae22deca7fde136944dc89)
1 /*
2  *	Routines having to do with the 'struct sk_buff' memory handlers.
3  *
4  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
5  *			Florian La Roche <rzsfl@rz.uni-sb.de>
6  *
7  *	Fixes:
8  *		Alan Cox	:	Fixed the worst of the load
9  *					balancer bugs.
10  *		Dave Platt	:	Interrupt stacking fix.
11  *	Richard Kooijman	:	Timestamp fixes.
12  *		Alan Cox	:	Changed buffer format.
13  *		Alan Cox	:	destructor hook for AF_UNIX etc.
14  *		Linus Torvalds	:	Better skb_clone.
15  *		Alan Cox	:	Added skb_copy.
16  *		Alan Cox	:	Added all the changed routines Linus
17  *					only put in the headers
18  *		Ray VanTassle	:	Fixed --skb->lock in free
19  *		Alan Cox	:	skb_copy copy arp field
20  *		Andi Kleen	:	slabified it.
21  *		Robert Olsson	:	Removed skb_head_pool
22  *
23  *	NOTE:
24  *		The __skb_ routines should be called with interrupts
25  *	disabled, or you better be *real* sure that the operation is atomic
26  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
27  *	or via disabling bottom half handlers, etc).
28  *
29  *	This program is free software; you can redistribute it and/or
30  *	modify it under the terms of the GNU General Public License
31  *	as published by the Free Software Foundation; either version
32  *	2 of the License, or (at your option) any later version.
33  */
34 
35 /*
36  *	The functions in this file will not compile correctly with gcc 2.4.x
37  */
38 
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40 
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/kmemcheck.h>
45 #include <linux/mm.h>
46 #include <linux/interrupt.h>
47 #include <linux/in.h>
48 #include <linux/inet.h>
49 #include <linux/slab.h>
50 #include <linux/tcp.h>
51 #include <linux/udp.h>
52 #include <linux/netdevice.h>
53 #ifdef CONFIG_NET_CLS_ACT
54 #include <net/pkt_sched.h>
55 #endif
56 #include <linux/string.h>
57 #include <linux/skbuff.h>
58 #include <linux/splice.h>
59 #include <linux/cache.h>
60 #include <linux/rtnetlink.h>
61 #include <linux/init.h>
62 #include <linux/scatterlist.h>
63 #include <linux/errqueue.h>
64 #include <linux/prefetch.h>
65 #include <linux/if_vlan.h>
66 
67 #include <net/protocol.h>
68 #include <net/dst.h>
69 #include <net/sock.h>
70 #include <net/checksum.h>
71 #include <net/ip6_checksum.h>
72 #include <net/xfrm.h>
73 
74 #include <asm/uaccess.h>
75 #include <trace/events/skb.h>
76 #include <linux/highmem.h>
77 #include <linux/capability.h>
78 #include <linux/user_namespace.h>
79 
80 struct kmem_cache *skbuff_head_cache __read_mostly;
81 static struct kmem_cache *skbuff_fclone_cache __read_mostly;
82 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
83 EXPORT_SYMBOL(sysctl_max_skb_frags);
84 
85 /**
86  *	skb_panic - private function for out-of-line support
87  *	@skb:	buffer
88  *	@sz:	size
89  *	@addr:	address
90  *	@msg:	skb_over_panic or skb_under_panic
91  *
92  *	Out-of-line support for skb_put() and skb_push().
93  *	Called via the wrapper skb_over_panic() or skb_under_panic().
94  *	Keep out of line to prevent kernel bloat.
95  *	__builtin_return_address is not used because it is not always reliable.
96  */
97 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
98 		      const char msg[])
99 {
100 	pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
101 		 msg, addr, skb->len, sz, skb->head, skb->data,
102 		 (unsigned long)skb->tail, (unsigned long)skb->end,
103 		 skb->dev ? skb->dev->name : "<NULL>");
104 	BUG();
105 }
106 
107 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
108 {
109 	skb_panic(skb, sz, addr, __func__);
110 }
111 
112 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
113 {
114 	skb_panic(skb, sz, addr, __func__);
115 }
116 
117 /*
118  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
119  * the caller if emergency pfmemalloc reserves are being used. If it is and
120  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
121  * may be used. Otherwise, the packet data may be discarded until enough
122  * memory is free
123  */
124 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
125 	 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
126 
127 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
128 			       unsigned long ip, bool *pfmemalloc)
129 {
130 	void *obj;
131 	bool ret_pfmemalloc = false;
132 
133 	/*
134 	 * Try a regular allocation, when that fails and we're not entitled
135 	 * to the reserves, fail.
136 	 */
137 	obj = kmalloc_node_track_caller(size,
138 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
139 					node);
140 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
141 		goto out;
142 
143 	/* Try again but now we are using pfmemalloc reserves */
144 	ret_pfmemalloc = true;
145 	obj = kmalloc_node_track_caller(size, flags, node);
146 
147 out:
148 	if (pfmemalloc)
149 		*pfmemalloc = ret_pfmemalloc;
150 
151 	return obj;
152 }
153 
154 /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
155  *	'private' fields and also do memory statistics to find all the
156  *	[BEEP] leaks.
157  *
158  */
159 
160 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
161 {
162 	struct sk_buff *skb;
163 
164 	/* Get the HEAD */
165 	skb = kmem_cache_alloc_node(skbuff_head_cache,
166 				    gfp_mask & ~__GFP_DMA, node);
167 	if (!skb)
168 		goto out;
169 
170 	/*
171 	 * Only clear those fields we need to clear, not those that we will
172 	 * actually initialise below. Hence, don't put any more fields after
173 	 * the tail pointer in struct sk_buff!
174 	 */
175 	memset(skb, 0, offsetof(struct sk_buff, tail));
176 	skb->head = NULL;
177 	skb->truesize = sizeof(struct sk_buff);
178 	atomic_set(&skb->users, 1);
179 
180 	skb->mac_header = (typeof(skb->mac_header))~0U;
181 out:
182 	return skb;
183 }
184 
185 /**
186  *	__alloc_skb	-	allocate a network buffer
187  *	@size: size to allocate
188  *	@gfp_mask: allocation mask
189  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
190  *		instead of head cache and allocate a cloned (child) skb.
191  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
192  *		allocations in case the data is required for writeback
193  *	@node: numa node to allocate memory on
194  *
195  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
196  *	tail room of at least size bytes. The object has a reference count
197  *	of one. The return is the buffer. On a failure the return is %NULL.
198  *
199  *	Buffers may only be allocated from interrupts using a @gfp_mask of
200  *	%GFP_ATOMIC.
201  */
202 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
203 			    int flags, int node)
204 {
205 	struct kmem_cache *cache;
206 	struct skb_shared_info *shinfo;
207 	struct sk_buff *skb;
208 	u8 *data;
209 	bool pfmemalloc;
210 
211 	cache = (flags & SKB_ALLOC_FCLONE)
212 		? skbuff_fclone_cache : skbuff_head_cache;
213 
214 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
215 		gfp_mask |= __GFP_MEMALLOC;
216 
217 	/* Get the HEAD */
218 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
219 	if (!skb)
220 		goto out;
221 	prefetchw(skb);
222 
223 	/* We do our best to align skb_shared_info on a separate cache
224 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
225 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
226 	 * Both skb->head and skb_shared_info are cache line aligned.
227 	 */
228 	size = SKB_DATA_ALIGN(size);
229 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
230 	data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
231 	if (!data)
232 		goto nodata;
233 	/* kmalloc(size) might give us more room than requested.
234 	 * Put skb_shared_info exactly at the end of allocated zone,
235 	 * to allow max possible filling before reallocation.
236 	 */
237 	size = SKB_WITH_OVERHEAD(ksize(data));
238 	prefetchw(data + size);
239 
240 	/*
241 	 * Only clear those fields we need to clear, not those that we will
242 	 * actually initialise below. Hence, don't put any more fields after
243 	 * the tail pointer in struct sk_buff!
244 	 */
245 	memset(skb, 0, offsetof(struct sk_buff, tail));
246 	/* Account for allocated memory : skb + skb->head */
247 	skb->truesize = SKB_TRUESIZE(size);
248 	skb->pfmemalloc = pfmemalloc;
249 	atomic_set(&skb->users, 1);
250 	skb->head = data;
251 	skb->data = data;
252 	skb_reset_tail_pointer(skb);
253 	skb->end = skb->tail + size;
254 	skb->mac_header = (typeof(skb->mac_header))~0U;
255 	skb->transport_header = (typeof(skb->transport_header))~0U;
256 
257 	/* make sure we initialize shinfo sequentially */
258 	shinfo = skb_shinfo(skb);
259 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
260 	atomic_set(&shinfo->dataref, 1);
261 	kmemcheck_annotate_variable(shinfo->destructor_arg);
262 
263 	if (flags & SKB_ALLOC_FCLONE) {
264 		struct sk_buff_fclones *fclones;
265 
266 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
267 
268 		kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
269 		skb->fclone = SKB_FCLONE_ORIG;
270 		atomic_set(&fclones->fclone_ref, 1);
271 
272 		fclones->skb2.fclone = SKB_FCLONE_CLONE;
273 		fclones->skb2.pfmemalloc = pfmemalloc;
274 	}
275 out:
276 	return skb;
277 nodata:
278 	kmem_cache_free(cache, skb);
279 	skb = NULL;
280 	goto out;
281 }
282 EXPORT_SYMBOL(__alloc_skb);
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 skb_shared_info *shinfo;
306 	struct sk_buff *skb;
307 	unsigned int size = frag_size ? : ksize(data);
308 
309 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
310 	if (!skb)
311 		return NULL;
312 
313 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
314 
315 	memset(skb, 0, offsetof(struct sk_buff, tail));
316 	skb->truesize = SKB_TRUESIZE(size);
317 	atomic_set(&skb->users, 1);
318 	skb->head = data;
319 	skb->data = data;
320 	skb_reset_tail_pointer(skb);
321 	skb->end = skb->tail + size;
322 	skb->mac_header = (typeof(skb->mac_header))~0U;
323 	skb->transport_header = (typeof(skb->transport_header))~0U;
324 
325 	/* make sure we initialize shinfo sequentially */
326 	shinfo = skb_shinfo(skb);
327 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328 	atomic_set(&shinfo->dataref, 1);
329 	kmemcheck_annotate_variable(shinfo->destructor_arg);
330 
331 	return skb;
332 }
333 
334 /* build_skb() is wrapper over __build_skb(), that specifically
335  * takes care of skb->head and skb->pfmemalloc
336  * This means that if @frag_size is not zero, then @data must be backed
337  * by a page fragment, not kmalloc() or vmalloc()
338  */
339 struct sk_buff *build_skb(void *data, unsigned int frag_size)
340 {
341 	struct sk_buff *skb = __build_skb(data, frag_size);
342 
343 	if (skb && frag_size) {
344 		skb->head_frag = 1;
345 		if (page_is_pfmemalloc(virt_to_head_page(data)))
346 			skb->pfmemalloc = 1;
347 	}
348 	return skb;
349 }
350 EXPORT_SYMBOL(build_skb);
351 
352 #define NAPI_SKB_CACHE_SIZE	64
353 
354 struct napi_alloc_cache {
355 	struct page_frag_cache page;
356 	size_t skb_count;
357 	void *skb_cache[NAPI_SKB_CACHE_SIZE];
358 };
359 
360 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
361 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
362 
363 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
364 {
365 	struct page_frag_cache *nc;
366 	unsigned long flags;
367 	void *data;
368 
369 	local_irq_save(flags);
370 	nc = this_cpu_ptr(&netdev_alloc_cache);
371 	data = __alloc_page_frag(nc, fragsz, gfp_mask);
372 	local_irq_restore(flags);
373 	return data;
374 }
375 
376 /**
377  * netdev_alloc_frag - allocate a page fragment
378  * @fragsz: fragment size
379  *
380  * Allocates a frag from a page for receive buffer.
381  * Uses GFP_ATOMIC allocations.
382  */
383 void *netdev_alloc_frag(unsigned int fragsz)
384 {
385 	return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
386 }
387 EXPORT_SYMBOL(netdev_alloc_frag);
388 
389 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
390 {
391 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
392 
393 	return __alloc_page_frag(&nc->page, fragsz, gfp_mask);
394 }
395 
396 void *napi_alloc_frag(unsigned int fragsz)
397 {
398 	return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
399 }
400 EXPORT_SYMBOL(napi_alloc_frag);
401 
402 /**
403  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
404  *	@dev: network device to receive on
405  *	@len: length to allocate
406  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
407  *
408  *	Allocate a new &sk_buff and assign it a usage count of one. The
409  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
410  *	the headroom they think they need without accounting for the
411  *	built in space. The built in space is used for optimisations.
412  *
413  *	%NULL is returned if there is no free memory.
414  */
415 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
416 				   gfp_t gfp_mask)
417 {
418 	struct page_frag_cache *nc;
419 	unsigned long flags;
420 	struct sk_buff *skb;
421 	bool pfmemalloc;
422 	void *data;
423 
424 	len += NET_SKB_PAD;
425 
426 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
427 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
428 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
429 		if (!skb)
430 			goto skb_fail;
431 		goto skb_success;
432 	}
433 
434 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
435 	len = SKB_DATA_ALIGN(len);
436 
437 	if (sk_memalloc_socks())
438 		gfp_mask |= __GFP_MEMALLOC;
439 
440 	local_irq_save(flags);
441 
442 	nc = this_cpu_ptr(&netdev_alloc_cache);
443 	data = __alloc_page_frag(nc, len, gfp_mask);
444 	pfmemalloc = nc->pfmemalloc;
445 
446 	local_irq_restore(flags);
447 
448 	if (unlikely(!data))
449 		return NULL;
450 
451 	skb = __build_skb(data, len);
452 	if (unlikely(!skb)) {
453 		skb_free_frag(data);
454 		return NULL;
455 	}
456 
457 	/* use OR instead of assignment to avoid clearing of bits in mask */
458 	if (pfmemalloc)
459 		skb->pfmemalloc = 1;
460 	skb->head_frag = 1;
461 
462 skb_success:
463 	skb_reserve(skb, NET_SKB_PAD);
464 	skb->dev = dev;
465 
466 skb_fail:
467 	return skb;
468 }
469 EXPORT_SYMBOL(__netdev_alloc_skb);
470 
471 /**
472  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
473  *	@napi: napi instance this buffer was allocated for
474  *	@len: length to allocate
475  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
476  *
477  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
478  *	attempt to allocate the head from a special reserved region used
479  *	only for NAPI Rx allocation.  By doing this we can save several
480  *	CPU cycles by avoiding having to disable and re-enable IRQs.
481  *
482  *	%NULL is returned if there is no free memory.
483  */
484 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
485 				 gfp_t gfp_mask)
486 {
487 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
488 	struct sk_buff *skb;
489 	void *data;
490 
491 	len += NET_SKB_PAD + NET_IP_ALIGN;
492 
493 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
494 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
495 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
496 		if (!skb)
497 			goto skb_fail;
498 		goto skb_success;
499 	}
500 
501 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
502 	len = SKB_DATA_ALIGN(len);
503 
504 	if (sk_memalloc_socks())
505 		gfp_mask |= __GFP_MEMALLOC;
506 
507 	data = __alloc_page_frag(&nc->page, len, gfp_mask);
508 	if (unlikely(!data))
509 		return NULL;
510 
511 	skb = __build_skb(data, len);
512 	if (unlikely(!skb)) {
513 		skb_free_frag(data);
514 		return NULL;
515 	}
516 
517 	/* use OR instead of assignment to avoid clearing of bits in mask */
518 	if (nc->page.pfmemalloc)
519 		skb->pfmemalloc = 1;
520 	skb->head_frag = 1;
521 
522 skb_success:
523 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
524 	skb->dev = napi->dev;
525 
526 skb_fail:
527 	return skb;
528 }
529 EXPORT_SYMBOL(__napi_alloc_skb);
530 
531 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
532 		     int size, unsigned int truesize)
533 {
534 	skb_fill_page_desc(skb, i, page, off, size);
535 	skb->len += size;
536 	skb->data_len += size;
537 	skb->truesize += truesize;
538 }
539 EXPORT_SYMBOL(skb_add_rx_frag);
540 
541 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
542 			  unsigned int truesize)
543 {
544 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
545 
546 	skb_frag_size_add(frag, size);
547 	skb->len += size;
548 	skb->data_len += size;
549 	skb->truesize += truesize;
550 }
551 EXPORT_SYMBOL(skb_coalesce_rx_frag);
552 
553 static void skb_drop_list(struct sk_buff **listp)
554 {
555 	kfree_skb_list(*listp);
556 	*listp = NULL;
557 }
558 
559 static inline void skb_drop_fraglist(struct sk_buff *skb)
560 {
561 	skb_drop_list(&skb_shinfo(skb)->frag_list);
562 }
563 
564 static void skb_clone_fraglist(struct sk_buff *skb)
565 {
566 	struct sk_buff *list;
567 
568 	skb_walk_frags(skb, list)
569 		skb_get(list);
570 }
571 
572 static void skb_free_head(struct sk_buff *skb)
573 {
574 	unsigned char *head = skb->head;
575 
576 	if (skb->head_frag)
577 		skb_free_frag(head);
578 	else
579 		kfree(head);
580 }
581 
582 static void skb_release_data(struct sk_buff *skb)
583 {
584 	struct skb_shared_info *shinfo = skb_shinfo(skb);
585 	int i;
586 
587 	if (skb->cloned &&
588 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
589 			      &shinfo->dataref))
590 		return;
591 
592 	for (i = 0; i < shinfo->nr_frags; i++)
593 		__skb_frag_unref(&shinfo->frags[i]);
594 
595 	/*
596 	 * If skb buf is from userspace, we need to notify the caller
597 	 * the lower device DMA has done;
598 	 */
599 	if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
600 		struct ubuf_info *uarg;
601 
602 		uarg = shinfo->destructor_arg;
603 		if (uarg->callback)
604 			uarg->callback(uarg, true);
605 	}
606 
607 	if (shinfo->frag_list)
608 		kfree_skb_list(shinfo->frag_list);
609 
610 	skb_free_head(skb);
611 }
612 
613 /*
614  *	Free an skbuff by memory without cleaning the state.
615  */
616 static void kfree_skbmem(struct sk_buff *skb)
617 {
618 	struct sk_buff_fclones *fclones;
619 
620 	switch (skb->fclone) {
621 	case SKB_FCLONE_UNAVAILABLE:
622 		kmem_cache_free(skbuff_head_cache, skb);
623 		return;
624 
625 	case SKB_FCLONE_ORIG:
626 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
627 
628 		/* We usually free the clone (TX completion) before original skb
629 		 * This test would have no chance to be true for the clone,
630 		 * while here, branch prediction will be good.
631 		 */
632 		if (atomic_read(&fclones->fclone_ref) == 1)
633 			goto fastpath;
634 		break;
635 
636 	default: /* SKB_FCLONE_CLONE */
637 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
638 		break;
639 	}
640 	if (!atomic_dec_and_test(&fclones->fclone_ref))
641 		return;
642 fastpath:
643 	kmem_cache_free(skbuff_fclone_cache, fclones);
644 }
645 
646 static void skb_release_head_state(struct sk_buff *skb)
647 {
648 	skb_dst_drop(skb);
649 #ifdef CONFIG_XFRM
650 	secpath_put(skb->sp);
651 #endif
652 	if (skb->destructor) {
653 		WARN_ON(in_irq());
654 		skb->destructor(skb);
655 	}
656 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
657 	nf_conntrack_put(skb->nfct);
658 #endif
659 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
660 	nf_bridge_put(skb->nf_bridge);
661 #endif
662 }
663 
664 /* Free everything but the sk_buff shell. */
665 static void skb_release_all(struct sk_buff *skb)
666 {
667 	skb_release_head_state(skb);
668 	if (likely(skb->head))
669 		skb_release_data(skb);
670 }
671 
672 /**
673  *	__kfree_skb - private function
674  *	@skb: buffer
675  *
676  *	Free an sk_buff. Release anything attached to the buffer.
677  *	Clean the state. This is an internal helper function. Users should
678  *	always call kfree_skb
679  */
680 
681 void __kfree_skb(struct sk_buff *skb)
682 {
683 	skb_release_all(skb);
684 	kfree_skbmem(skb);
685 }
686 EXPORT_SYMBOL(__kfree_skb);
687 
688 /**
689  *	kfree_skb - free an sk_buff
690  *	@skb: buffer to free
691  *
692  *	Drop a reference to the buffer and free it if the usage count has
693  *	hit zero.
694  */
695 void kfree_skb(struct sk_buff *skb)
696 {
697 	if (unlikely(!skb))
698 		return;
699 	if (likely(atomic_read(&skb->users) == 1))
700 		smp_rmb();
701 	else if (likely(!atomic_dec_and_test(&skb->users)))
702 		return;
703 	trace_kfree_skb(skb, __builtin_return_address(0));
704 	__kfree_skb(skb);
705 }
706 EXPORT_SYMBOL(kfree_skb);
707 
708 void kfree_skb_list(struct sk_buff *segs)
709 {
710 	while (segs) {
711 		struct sk_buff *next = segs->next;
712 
713 		kfree_skb(segs);
714 		segs = next;
715 	}
716 }
717 EXPORT_SYMBOL(kfree_skb_list);
718 
719 /**
720  *	skb_tx_error - report an sk_buff xmit error
721  *	@skb: buffer that triggered an error
722  *
723  *	Report xmit error if a device callback is tracking this skb.
724  *	skb must be freed afterwards.
725  */
726 void skb_tx_error(struct sk_buff *skb)
727 {
728 	if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
729 		struct ubuf_info *uarg;
730 
731 		uarg = skb_shinfo(skb)->destructor_arg;
732 		if (uarg->callback)
733 			uarg->callback(uarg, false);
734 		skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
735 	}
736 }
737 EXPORT_SYMBOL(skb_tx_error);
738 
739 /**
740  *	consume_skb - free an skbuff
741  *	@skb: buffer to free
742  *
743  *	Drop a ref to the buffer and free it if the usage count has hit zero
744  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
745  *	is being dropped after a failure and notes that
746  */
747 void consume_skb(struct sk_buff *skb)
748 {
749 	if (unlikely(!skb))
750 		return;
751 	if (likely(atomic_read(&skb->users) == 1))
752 		smp_rmb();
753 	else if (likely(!atomic_dec_and_test(&skb->users)))
754 		return;
755 	trace_consume_skb(skb);
756 	__kfree_skb(skb);
757 }
758 EXPORT_SYMBOL(consume_skb);
759 
760 void __kfree_skb_flush(void)
761 {
762 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
763 
764 	/* flush skb_cache if containing objects */
765 	if (nc->skb_count) {
766 		kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
767 				     nc->skb_cache);
768 		nc->skb_count = 0;
769 	}
770 }
771 
772 static inline void _kfree_skb_defer(struct sk_buff *skb)
773 {
774 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
775 
776 	/* drop skb->head and call any destructors for packet */
777 	skb_release_all(skb);
778 
779 	/* record skb to CPU local list */
780 	nc->skb_cache[nc->skb_count++] = skb;
781 
782 #ifdef CONFIG_SLUB
783 	/* SLUB writes into objects when freeing */
784 	prefetchw(skb);
785 #endif
786 
787 	/* flush skb_cache if it is filled */
788 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
789 		kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
790 				     nc->skb_cache);
791 		nc->skb_count = 0;
792 	}
793 }
794 void __kfree_skb_defer(struct sk_buff *skb)
795 {
796 	_kfree_skb_defer(skb);
797 }
798 
799 void napi_consume_skb(struct sk_buff *skb, int budget)
800 {
801 	if (unlikely(!skb))
802 		return;
803 
804 	/* if budget is 0 assume netpoll w/ IRQs disabled */
805 	if (unlikely(!budget)) {
806 		dev_consume_skb_irq(skb);
807 		return;
808 	}
809 
810 	if (likely(atomic_read(&skb->users) == 1))
811 		smp_rmb();
812 	else if (likely(!atomic_dec_and_test(&skb->users)))
813 		return;
814 	/* if reaching here SKB is ready to free */
815 	trace_consume_skb(skb);
816 
817 	/* if SKB is a clone, don't handle this case */
818 	if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) {
819 		__kfree_skb(skb);
820 		return;
821 	}
822 
823 	_kfree_skb_defer(skb);
824 }
825 EXPORT_SYMBOL(napi_consume_skb);
826 
827 /* Make sure a field is enclosed inside headers_start/headers_end section */
828 #define CHECK_SKB_FIELD(field) \
829 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
830 		     offsetof(struct sk_buff, headers_start));	\
831 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
832 		     offsetof(struct sk_buff, headers_end));	\
833 
834 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
835 {
836 	new->tstamp		= old->tstamp;
837 	/* We do not copy old->sk */
838 	new->dev		= old->dev;
839 	memcpy(new->cb, old->cb, sizeof(old->cb));
840 	skb_dst_copy(new, old);
841 #ifdef CONFIG_XFRM
842 	new->sp			= secpath_get(old->sp);
843 #endif
844 	__nf_copy(new, old, false);
845 
846 	/* Note : this field could be in headers_start/headers_end section
847 	 * It is not yet because we do not want to have a 16 bit hole
848 	 */
849 	new->queue_mapping = old->queue_mapping;
850 
851 	memcpy(&new->headers_start, &old->headers_start,
852 	       offsetof(struct sk_buff, headers_end) -
853 	       offsetof(struct sk_buff, headers_start));
854 	CHECK_SKB_FIELD(protocol);
855 	CHECK_SKB_FIELD(csum);
856 	CHECK_SKB_FIELD(hash);
857 	CHECK_SKB_FIELD(priority);
858 	CHECK_SKB_FIELD(skb_iif);
859 	CHECK_SKB_FIELD(vlan_proto);
860 	CHECK_SKB_FIELD(vlan_tci);
861 	CHECK_SKB_FIELD(transport_header);
862 	CHECK_SKB_FIELD(network_header);
863 	CHECK_SKB_FIELD(mac_header);
864 	CHECK_SKB_FIELD(inner_protocol);
865 	CHECK_SKB_FIELD(inner_transport_header);
866 	CHECK_SKB_FIELD(inner_network_header);
867 	CHECK_SKB_FIELD(inner_mac_header);
868 	CHECK_SKB_FIELD(mark);
869 #ifdef CONFIG_NETWORK_SECMARK
870 	CHECK_SKB_FIELD(secmark);
871 #endif
872 #ifdef CONFIG_NET_RX_BUSY_POLL
873 	CHECK_SKB_FIELD(napi_id);
874 #endif
875 #ifdef CONFIG_XPS
876 	CHECK_SKB_FIELD(sender_cpu);
877 #endif
878 #ifdef CONFIG_NET_SCHED
879 	CHECK_SKB_FIELD(tc_index);
880 #ifdef CONFIG_NET_CLS_ACT
881 	CHECK_SKB_FIELD(tc_verd);
882 #endif
883 #endif
884 
885 }
886 
887 /*
888  * You should not add any new code to this function.  Add it to
889  * __copy_skb_header above instead.
890  */
891 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
892 {
893 #define C(x) n->x = skb->x
894 
895 	n->next = n->prev = NULL;
896 	n->sk = NULL;
897 	__copy_skb_header(n, skb);
898 
899 	C(len);
900 	C(data_len);
901 	C(mac_len);
902 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
903 	n->cloned = 1;
904 	n->nohdr = 0;
905 	n->destructor = NULL;
906 	C(tail);
907 	C(end);
908 	C(head);
909 	C(head_frag);
910 	C(data);
911 	C(truesize);
912 	atomic_set(&n->users, 1);
913 
914 	atomic_inc(&(skb_shinfo(skb)->dataref));
915 	skb->cloned = 1;
916 
917 	return n;
918 #undef C
919 }
920 
921 /**
922  *	skb_morph	-	morph one skb into another
923  *	@dst: the skb to receive the contents
924  *	@src: the skb to supply the contents
925  *
926  *	This is identical to skb_clone except that the target skb is
927  *	supplied by the user.
928  *
929  *	The target skb is returned upon exit.
930  */
931 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
932 {
933 	skb_release_all(dst);
934 	return __skb_clone(dst, src);
935 }
936 EXPORT_SYMBOL_GPL(skb_morph);
937 
938 /**
939  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
940  *	@skb: the skb to modify
941  *	@gfp_mask: allocation priority
942  *
943  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
944  *	It will copy all frags into kernel and drop the reference
945  *	to userspace pages.
946  *
947  *	If this function is called from an interrupt gfp_mask() must be
948  *	%GFP_ATOMIC.
949  *
950  *	Returns 0 on success or a negative error code on failure
951  *	to allocate kernel memory to copy to.
952  */
953 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
954 {
955 	int i;
956 	int num_frags = skb_shinfo(skb)->nr_frags;
957 	struct page *page, *head = NULL;
958 	struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
959 
960 	for (i = 0; i < num_frags; i++) {
961 		u8 *vaddr;
962 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
963 
964 		page = alloc_page(gfp_mask);
965 		if (!page) {
966 			while (head) {
967 				struct page *next = (struct page *)page_private(head);
968 				put_page(head);
969 				head = next;
970 			}
971 			return -ENOMEM;
972 		}
973 		vaddr = kmap_atomic(skb_frag_page(f));
974 		memcpy(page_address(page),
975 		       vaddr + f->page_offset, skb_frag_size(f));
976 		kunmap_atomic(vaddr);
977 		set_page_private(page, (unsigned long)head);
978 		head = page;
979 	}
980 
981 	/* skb frags release userspace buffers */
982 	for (i = 0; i < num_frags; i++)
983 		skb_frag_unref(skb, i);
984 
985 	uarg->callback(uarg, false);
986 
987 	/* skb frags point to kernel buffers */
988 	for (i = num_frags - 1; i >= 0; i--) {
989 		__skb_fill_page_desc(skb, i, head, 0,
990 				     skb_shinfo(skb)->frags[i].size);
991 		head = (struct page *)page_private(head);
992 	}
993 
994 	skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
995 	return 0;
996 }
997 EXPORT_SYMBOL_GPL(skb_copy_ubufs);
998 
999 /**
1000  *	skb_clone	-	duplicate an sk_buff
1001  *	@skb: buffer to clone
1002  *	@gfp_mask: allocation priority
1003  *
1004  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
1005  *	copies share the same packet data but not structure. The new
1006  *	buffer has a reference count of 1. If the allocation fails the
1007  *	function returns %NULL otherwise the new buffer is returned.
1008  *
1009  *	If this function is called from an interrupt gfp_mask() must be
1010  *	%GFP_ATOMIC.
1011  */
1012 
1013 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1014 {
1015 	struct sk_buff_fclones *fclones = container_of(skb,
1016 						       struct sk_buff_fclones,
1017 						       skb1);
1018 	struct sk_buff *n;
1019 
1020 	if (skb_orphan_frags(skb, gfp_mask))
1021 		return NULL;
1022 
1023 	if (skb->fclone == SKB_FCLONE_ORIG &&
1024 	    atomic_read(&fclones->fclone_ref) == 1) {
1025 		n = &fclones->skb2;
1026 		atomic_set(&fclones->fclone_ref, 2);
1027 	} else {
1028 		if (skb_pfmemalloc(skb))
1029 			gfp_mask |= __GFP_MEMALLOC;
1030 
1031 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1032 		if (!n)
1033 			return NULL;
1034 
1035 		kmemcheck_annotate_bitfield(n, flags1);
1036 		n->fclone = SKB_FCLONE_UNAVAILABLE;
1037 	}
1038 
1039 	return __skb_clone(n, skb);
1040 }
1041 EXPORT_SYMBOL(skb_clone);
1042 
1043 static void skb_headers_offset_update(struct sk_buff *skb, int off)
1044 {
1045 	/* Only adjust this if it actually is csum_start rather than csum */
1046 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1047 		skb->csum_start += off;
1048 	/* {transport,network,mac}_header and tail are relative to skb->head */
1049 	skb->transport_header += off;
1050 	skb->network_header   += off;
1051 	if (skb_mac_header_was_set(skb))
1052 		skb->mac_header += off;
1053 	skb->inner_transport_header += off;
1054 	skb->inner_network_header += off;
1055 	skb->inner_mac_header += off;
1056 }
1057 
1058 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
1059 {
1060 	__copy_skb_header(new, old);
1061 
1062 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
1063 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
1064 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
1065 }
1066 
1067 static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1068 {
1069 	if (skb_pfmemalloc(skb))
1070 		return SKB_ALLOC_RX;
1071 	return 0;
1072 }
1073 
1074 /**
1075  *	skb_copy	-	create private copy of an sk_buff
1076  *	@skb: buffer to copy
1077  *	@gfp_mask: allocation priority
1078  *
1079  *	Make a copy of both an &sk_buff and its data. This is used when the
1080  *	caller wishes to modify the data and needs a private copy of the
1081  *	data to alter. Returns %NULL on failure or the pointer to the buffer
1082  *	on success. The returned buffer has a reference count of 1.
1083  *
1084  *	As by-product this function converts non-linear &sk_buff to linear
1085  *	one, so that &sk_buff becomes completely private and caller is allowed
1086  *	to modify all the data of returned buffer. This means that this
1087  *	function is not recommended for use in circumstances when only
1088  *	header is going to be modified. Use pskb_copy() instead.
1089  */
1090 
1091 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
1092 {
1093 	int headerlen = skb_headroom(skb);
1094 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1095 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1096 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
1097 
1098 	if (!n)
1099 		return NULL;
1100 
1101 	/* Set the data pointer */
1102 	skb_reserve(n, headerlen);
1103 	/* Set the tail pointer and length */
1104 	skb_put(n, skb->len);
1105 
1106 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
1107 		BUG();
1108 
1109 	copy_skb_header(n, skb);
1110 	return n;
1111 }
1112 EXPORT_SYMBOL(skb_copy);
1113 
1114 /**
1115  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
1116  *	@skb: buffer to copy
1117  *	@headroom: headroom of new skb
1118  *	@gfp_mask: allocation priority
1119  *	@fclone: if true allocate the copy of the skb from the fclone
1120  *	cache instead of the head cache; it is recommended to set this
1121  *	to true for the cases where the copy will likely be cloned
1122  *
1123  *	Make a copy of both an &sk_buff and part of its data, located
1124  *	in header. Fragmented data remain shared. This is used when
1125  *	the caller wishes to modify only header of &sk_buff and needs
1126  *	private copy of the header to alter. Returns %NULL on failure
1127  *	or the pointer to the buffer on success.
1128  *	The returned buffer has a reference count of 1.
1129  */
1130 
1131 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1132 				   gfp_t gfp_mask, bool fclone)
1133 {
1134 	unsigned int size = skb_headlen(skb) + headroom;
1135 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1136 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
1137 
1138 	if (!n)
1139 		goto out;
1140 
1141 	/* Set the data pointer */
1142 	skb_reserve(n, headroom);
1143 	/* Set the tail pointer and length */
1144 	skb_put(n, skb_headlen(skb));
1145 	/* Copy the bytes */
1146 	skb_copy_from_linear_data(skb, n->data, n->len);
1147 
1148 	n->truesize += skb->data_len;
1149 	n->data_len  = skb->data_len;
1150 	n->len	     = skb->len;
1151 
1152 	if (skb_shinfo(skb)->nr_frags) {
1153 		int i;
1154 
1155 		if (skb_orphan_frags(skb, gfp_mask)) {
1156 			kfree_skb(n);
1157 			n = NULL;
1158 			goto out;
1159 		}
1160 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1161 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1162 			skb_frag_ref(skb, i);
1163 		}
1164 		skb_shinfo(n)->nr_frags = i;
1165 	}
1166 
1167 	if (skb_has_frag_list(skb)) {
1168 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
1169 		skb_clone_fraglist(n);
1170 	}
1171 
1172 	copy_skb_header(n, skb);
1173 out:
1174 	return n;
1175 }
1176 EXPORT_SYMBOL(__pskb_copy_fclone);
1177 
1178 /**
1179  *	pskb_expand_head - reallocate header of &sk_buff
1180  *	@skb: buffer to reallocate
1181  *	@nhead: room to add at head
1182  *	@ntail: room to add at tail
1183  *	@gfp_mask: allocation priority
1184  *
1185  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1186  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1187  *	reference count of 1. Returns zero in the case of success or error,
1188  *	if expansion failed. In the last case, &sk_buff is not changed.
1189  *
1190  *	All the pointers pointing into skb header may change and must be
1191  *	reloaded after call to this function.
1192  */
1193 
1194 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1195 		     gfp_t gfp_mask)
1196 {
1197 	int i;
1198 	u8 *data;
1199 	int size = nhead + skb_end_offset(skb) + ntail;
1200 	long off;
1201 
1202 	BUG_ON(nhead < 0);
1203 
1204 	if (skb_shared(skb))
1205 		BUG();
1206 
1207 	size = SKB_DATA_ALIGN(size);
1208 
1209 	if (skb_pfmemalloc(skb))
1210 		gfp_mask |= __GFP_MEMALLOC;
1211 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1212 			       gfp_mask, NUMA_NO_NODE, NULL);
1213 	if (!data)
1214 		goto nodata;
1215 	size = SKB_WITH_OVERHEAD(ksize(data));
1216 
1217 	/* Copy only real data... and, alas, header. This should be
1218 	 * optimized for the cases when header is void.
1219 	 */
1220 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
1221 
1222 	memcpy((struct skb_shared_info *)(data + size),
1223 	       skb_shinfo(skb),
1224 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
1225 
1226 	/*
1227 	 * if shinfo is shared we must drop the old head gracefully, but if it
1228 	 * is not we can just drop the old head and let the existing refcount
1229 	 * be since all we did is relocate the values
1230 	 */
1231 	if (skb_cloned(skb)) {
1232 		/* copy this zero copy skb frags */
1233 		if (skb_orphan_frags(skb, gfp_mask))
1234 			goto nofrags;
1235 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1236 			skb_frag_ref(skb, i);
1237 
1238 		if (skb_has_frag_list(skb))
1239 			skb_clone_fraglist(skb);
1240 
1241 		skb_release_data(skb);
1242 	} else {
1243 		skb_free_head(skb);
1244 	}
1245 	off = (data + nhead) - skb->head;
1246 
1247 	skb->head     = data;
1248 	skb->head_frag = 0;
1249 	skb->data    += off;
1250 #ifdef NET_SKBUFF_DATA_USES_OFFSET
1251 	skb->end      = size;
1252 	off           = nhead;
1253 #else
1254 	skb->end      = skb->head + size;
1255 #endif
1256 	skb->tail	      += off;
1257 	skb_headers_offset_update(skb, nhead);
1258 	skb->cloned   = 0;
1259 	skb->hdr_len  = 0;
1260 	skb->nohdr    = 0;
1261 	atomic_set(&skb_shinfo(skb)->dataref, 1);
1262 	return 0;
1263 
1264 nofrags:
1265 	kfree(data);
1266 nodata:
1267 	return -ENOMEM;
1268 }
1269 EXPORT_SYMBOL(pskb_expand_head);
1270 
1271 /* Make private copy of skb with writable head and some headroom */
1272 
1273 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
1274 {
1275 	struct sk_buff *skb2;
1276 	int delta = headroom - skb_headroom(skb);
1277 
1278 	if (delta <= 0)
1279 		skb2 = pskb_copy(skb, GFP_ATOMIC);
1280 	else {
1281 		skb2 = skb_clone(skb, GFP_ATOMIC);
1282 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
1283 					     GFP_ATOMIC)) {
1284 			kfree_skb(skb2);
1285 			skb2 = NULL;
1286 		}
1287 	}
1288 	return skb2;
1289 }
1290 EXPORT_SYMBOL(skb_realloc_headroom);
1291 
1292 /**
1293  *	skb_copy_expand	-	copy and expand sk_buff
1294  *	@skb: buffer to copy
1295  *	@newheadroom: new free bytes at head
1296  *	@newtailroom: new free bytes at tail
1297  *	@gfp_mask: allocation priority
1298  *
1299  *	Make a copy of both an &sk_buff and its data and while doing so
1300  *	allocate additional space.
1301  *
1302  *	This is used when the caller wishes to modify the data and needs a
1303  *	private copy of the data to alter as well as more space for new fields.
1304  *	Returns %NULL on failure or the pointer to the buffer
1305  *	on success. The returned buffer has a reference count of 1.
1306  *
1307  *	You must pass %GFP_ATOMIC as the allocation priority if this function
1308  *	is called from an interrupt.
1309  */
1310 struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1311 				int newheadroom, int newtailroom,
1312 				gfp_t gfp_mask)
1313 {
1314 	/*
1315 	 *	Allocate the copy buffer
1316 	 */
1317 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1318 					gfp_mask, skb_alloc_rx_flag(skb),
1319 					NUMA_NO_NODE);
1320 	int oldheadroom = skb_headroom(skb);
1321 	int head_copy_len, head_copy_off;
1322 
1323 	if (!n)
1324 		return NULL;
1325 
1326 	skb_reserve(n, newheadroom);
1327 
1328 	/* Set the tail pointer and length */
1329 	skb_put(n, skb->len);
1330 
1331 	head_copy_len = oldheadroom;
1332 	head_copy_off = 0;
1333 	if (newheadroom <= head_copy_len)
1334 		head_copy_len = newheadroom;
1335 	else
1336 		head_copy_off = newheadroom - head_copy_len;
1337 
1338 	/* Copy the linear header and data. */
1339 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
1340 			  skb->len + head_copy_len))
1341 		BUG();
1342 
1343 	copy_skb_header(n, skb);
1344 
1345 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1346 
1347 	return n;
1348 }
1349 EXPORT_SYMBOL(skb_copy_expand);
1350 
1351 /**
1352  *	skb_pad			-	zero pad the tail of an skb
1353  *	@skb: buffer to pad
1354  *	@pad: space to pad
1355  *
1356  *	Ensure that a buffer is followed by a padding area that is zero
1357  *	filled. Used by network drivers which may DMA or transfer data
1358  *	beyond the buffer end onto the wire.
1359  *
1360  *	May return error in out of memory cases. The skb is freed on error.
1361  */
1362 
1363 int skb_pad(struct sk_buff *skb, int pad)
1364 {
1365 	int err;
1366 	int ntail;
1367 
1368 	/* If the skbuff is non linear tailroom is always zero.. */
1369 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
1370 		memset(skb->data+skb->len, 0, pad);
1371 		return 0;
1372 	}
1373 
1374 	ntail = skb->data_len + pad - (skb->end - skb->tail);
1375 	if (likely(skb_cloned(skb) || ntail > 0)) {
1376 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
1377 		if (unlikely(err))
1378 			goto free_skb;
1379 	}
1380 
1381 	/* FIXME: The use of this function with non-linear skb's really needs
1382 	 * to be audited.
1383 	 */
1384 	err = skb_linearize(skb);
1385 	if (unlikely(err))
1386 		goto free_skb;
1387 
1388 	memset(skb->data + skb->len, 0, pad);
1389 	return 0;
1390 
1391 free_skb:
1392 	kfree_skb(skb);
1393 	return err;
1394 }
1395 EXPORT_SYMBOL(skb_pad);
1396 
1397 /**
1398  *	pskb_put - add data to the tail of a potentially fragmented buffer
1399  *	@skb: start of the buffer to use
1400  *	@tail: tail fragment of the buffer to use
1401  *	@len: amount of data to add
1402  *
1403  *	This function extends the used data area of the potentially
1404  *	fragmented buffer. @tail must be the last fragment of @skb -- or
1405  *	@skb itself. If this would exceed the total buffer size the kernel
1406  *	will panic. A pointer to the first byte of the extra data is
1407  *	returned.
1408  */
1409 
1410 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1411 {
1412 	if (tail != skb) {
1413 		skb->data_len += len;
1414 		skb->len += len;
1415 	}
1416 	return skb_put(tail, len);
1417 }
1418 EXPORT_SYMBOL_GPL(pskb_put);
1419 
1420 /**
1421  *	skb_put - add data to a buffer
1422  *	@skb: buffer to use
1423  *	@len: amount of data to add
1424  *
1425  *	This function extends the used data area of the buffer. If this would
1426  *	exceed the total buffer size the kernel will panic. A pointer to the
1427  *	first byte of the extra data is returned.
1428  */
1429 unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
1430 {
1431 	unsigned char *tmp = skb_tail_pointer(skb);
1432 	SKB_LINEAR_ASSERT(skb);
1433 	skb->tail += len;
1434 	skb->len  += len;
1435 	if (unlikely(skb->tail > skb->end))
1436 		skb_over_panic(skb, len, __builtin_return_address(0));
1437 	return tmp;
1438 }
1439 EXPORT_SYMBOL(skb_put);
1440 
1441 /**
1442  *	skb_push - add data to the start of a buffer
1443  *	@skb: buffer to use
1444  *	@len: amount of data to add
1445  *
1446  *	This function extends the used data area of the buffer at the buffer
1447  *	start. If this would exceed the total buffer headroom the kernel will
1448  *	panic. A pointer to the first byte of the extra data is returned.
1449  */
1450 unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1451 {
1452 	skb->data -= len;
1453 	skb->len  += len;
1454 	if (unlikely(skb->data<skb->head))
1455 		skb_under_panic(skb, len, __builtin_return_address(0));
1456 	return skb->data;
1457 }
1458 EXPORT_SYMBOL(skb_push);
1459 
1460 /**
1461  *	skb_pull - remove data from the start of a buffer
1462  *	@skb: buffer to use
1463  *	@len: amount of data to remove
1464  *
1465  *	This function removes data from the start of a buffer, returning
1466  *	the memory to the headroom. A pointer to the next data in the buffer
1467  *	is returned. Once the data has been pulled future pushes will overwrite
1468  *	the old data.
1469  */
1470 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
1471 {
1472 	return skb_pull_inline(skb, len);
1473 }
1474 EXPORT_SYMBOL(skb_pull);
1475 
1476 /**
1477  *	skb_trim - remove end from a buffer
1478  *	@skb: buffer to alter
1479  *	@len: new length
1480  *
1481  *	Cut the length of a buffer down by removing data from the tail. If
1482  *	the buffer is already under the length specified it is not modified.
1483  *	The skb must be linear.
1484  */
1485 void skb_trim(struct sk_buff *skb, unsigned int len)
1486 {
1487 	if (skb->len > len)
1488 		__skb_trim(skb, len);
1489 }
1490 EXPORT_SYMBOL(skb_trim);
1491 
1492 /* Trims skb to length len. It can change skb pointers.
1493  */
1494 
1495 int ___pskb_trim(struct sk_buff *skb, unsigned int len)
1496 {
1497 	struct sk_buff **fragp;
1498 	struct sk_buff *frag;
1499 	int offset = skb_headlen(skb);
1500 	int nfrags = skb_shinfo(skb)->nr_frags;
1501 	int i;
1502 	int err;
1503 
1504 	if (skb_cloned(skb) &&
1505 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
1506 		return err;
1507 
1508 	i = 0;
1509 	if (offset >= len)
1510 		goto drop_pages;
1511 
1512 	for (; i < nfrags; i++) {
1513 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
1514 
1515 		if (end < len) {
1516 			offset = end;
1517 			continue;
1518 		}
1519 
1520 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
1521 
1522 drop_pages:
1523 		skb_shinfo(skb)->nr_frags = i;
1524 
1525 		for (; i < nfrags; i++)
1526 			skb_frag_unref(skb, i);
1527 
1528 		if (skb_has_frag_list(skb))
1529 			skb_drop_fraglist(skb);
1530 		goto done;
1531 	}
1532 
1533 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
1534 	     fragp = &frag->next) {
1535 		int end = offset + frag->len;
1536 
1537 		if (skb_shared(frag)) {
1538 			struct sk_buff *nfrag;
1539 
1540 			nfrag = skb_clone(frag, GFP_ATOMIC);
1541 			if (unlikely(!nfrag))
1542 				return -ENOMEM;
1543 
1544 			nfrag->next = frag->next;
1545 			consume_skb(frag);
1546 			frag = nfrag;
1547 			*fragp = frag;
1548 		}
1549 
1550 		if (end < len) {
1551 			offset = end;
1552 			continue;
1553 		}
1554 
1555 		if (end > len &&
1556 		    unlikely((err = pskb_trim(frag, len - offset))))
1557 			return err;
1558 
1559 		if (frag->next)
1560 			skb_drop_list(&frag->next);
1561 		break;
1562 	}
1563 
1564 done:
1565 	if (len > skb_headlen(skb)) {
1566 		skb->data_len -= skb->len - len;
1567 		skb->len       = len;
1568 	} else {
1569 		skb->len       = len;
1570 		skb->data_len  = 0;
1571 		skb_set_tail_pointer(skb, len);
1572 	}
1573 
1574 	return 0;
1575 }
1576 EXPORT_SYMBOL(___pskb_trim);
1577 
1578 /**
1579  *	__pskb_pull_tail - advance tail of skb header
1580  *	@skb: buffer to reallocate
1581  *	@delta: number of bytes to advance tail
1582  *
1583  *	The function makes a sense only on a fragmented &sk_buff,
1584  *	it expands header moving its tail forward and copying necessary
1585  *	data from fragmented part.
1586  *
1587  *	&sk_buff MUST have reference count of 1.
1588  *
1589  *	Returns %NULL (and &sk_buff does not change) if pull failed
1590  *	or value of new tail of skb in the case of success.
1591  *
1592  *	All the pointers pointing into skb header may change and must be
1593  *	reloaded after call to this function.
1594  */
1595 
1596 /* Moves tail of skb head forward, copying data from fragmented part,
1597  * when it is necessary.
1598  * 1. It may fail due to malloc failure.
1599  * 2. It may change skb pointers.
1600  *
1601  * It is pretty complicated. Luckily, it is called only in exceptional cases.
1602  */
1603 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
1604 {
1605 	/* If skb has not enough free space at tail, get new one
1606 	 * plus 128 bytes for future expansions. If we have enough
1607 	 * room at tail, reallocate without expansion only if skb is cloned.
1608 	 */
1609 	int i, k, eat = (skb->tail + delta) - skb->end;
1610 
1611 	if (eat > 0 || skb_cloned(skb)) {
1612 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
1613 				     GFP_ATOMIC))
1614 			return NULL;
1615 	}
1616 
1617 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
1618 		BUG();
1619 
1620 	/* Optimization: no fragments, no reasons to preestimate
1621 	 * size of pulled pages. Superb.
1622 	 */
1623 	if (!skb_has_frag_list(skb))
1624 		goto pull_pages;
1625 
1626 	/* Estimate size of pulled pages. */
1627 	eat = delta;
1628 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1629 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1630 
1631 		if (size >= eat)
1632 			goto pull_pages;
1633 		eat -= size;
1634 	}
1635 
1636 	/* If we need update frag list, we are in troubles.
1637 	 * Certainly, it possible to add an offset to skb data,
1638 	 * but taking into account that pulling is expected to
1639 	 * be very rare operation, it is worth to fight against
1640 	 * further bloating skb head and crucify ourselves here instead.
1641 	 * Pure masohism, indeed. 8)8)
1642 	 */
1643 	if (eat) {
1644 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1645 		struct sk_buff *clone = NULL;
1646 		struct sk_buff *insp = NULL;
1647 
1648 		do {
1649 			BUG_ON(!list);
1650 
1651 			if (list->len <= eat) {
1652 				/* Eaten as whole. */
1653 				eat -= list->len;
1654 				list = list->next;
1655 				insp = list;
1656 			} else {
1657 				/* Eaten partially. */
1658 
1659 				if (skb_shared(list)) {
1660 					/* Sucks! We need to fork list. :-( */
1661 					clone = skb_clone(list, GFP_ATOMIC);
1662 					if (!clone)
1663 						return NULL;
1664 					insp = list->next;
1665 					list = clone;
1666 				} else {
1667 					/* This may be pulled without
1668 					 * problems. */
1669 					insp = list;
1670 				}
1671 				if (!pskb_pull(list, eat)) {
1672 					kfree_skb(clone);
1673 					return NULL;
1674 				}
1675 				break;
1676 			}
1677 		} while (eat);
1678 
1679 		/* Free pulled out fragments. */
1680 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
1681 			skb_shinfo(skb)->frag_list = list->next;
1682 			kfree_skb(list);
1683 		}
1684 		/* And insert new clone at head. */
1685 		if (clone) {
1686 			clone->next = list;
1687 			skb_shinfo(skb)->frag_list = clone;
1688 		}
1689 	}
1690 	/* Success! Now we may commit changes to skb data. */
1691 
1692 pull_pages:
1693 	eat = delta;
1694 	k = 0;
1695 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1696 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
1697 
1698 		if (size <= eat) {
1699 			skb_frag_unref(skb, i);
1700 			eat -= size;
1701 		} else {
1702 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
1703 			if (eat) {
1704 				skb_shinfo(skb)->frags[k].page_offset += eat;
1705 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
1706 				eat = 0;
1707 			}
1708 			k++;
1709 		}
1710 	}
1711 	skb_shinfo(skb)->nr_frags = k;
1712 
1713 	skb->tail     += delta;
1714 	skb->data_len -= delta;
1715 
1716 	return skb_tail_pointer(skb);
1717 }
1718 EXPORT_SYMBOL(__pskb_pull_tail);
1719 
1720 /**
1721  *	skb_copy_bits - copy bits from skb to kernel buffer
1722  *	@skb: source skb
1723  *	@offset: offset in source
1724  *	@to: destination buffer
1725  *	@len: number of bytes to copy
1726  *
1727  *	Copy the specified number of bytes from the source skb to the
1728  *	destination buffer.
1729  *
1730  *	CAUTION ! :
1731  *		If its prototype is ever changed,
1732  *		check arch/{*}/net/{*}.S files,
1733  *		since it is called from BPF assembly code.
1734  */
1735 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
1736 {
1737 	int start = skb_headlen(skb);
1738 	struct sk_buff *frag_iter;
1739 	int i, copy;
1740 
1741 	if (offset > (int)skb->len - len)
1742 		goto fault;
1743 
1744 	/* Copy header. */
1745 	if ((copy = start - offset) > 0) {
1746 		if (copy > len)
1747 			copy = len;
1748 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
1749 		if ((len -= copy) == 0)
1750 			return 0;
1751 		offset += copy;
1752 		to     += copy;
1753 	}
1754 
1755 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1756 		int end;
1757 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
1758 
1759 		WARN_ON(start > offset + len);
1760 
1761 		end = start + skb_frag_size(f);
1762 		if ((copy = end - offset) > 0) {
1763 			u8 *vaddr;
1764 
1765 			if (copy > len)
1766 				copy = len;
1767 
1768 			vaddr = kmap_atomic(skb_frag_page(f));
1769 			memcpy(to,
1770 			       vaddr + f->page_offset + offset - start,
1771 			       copy);
1772 			kunmap_atomic(vaddr);
1773 
1774 			if ((len -= copy) == 0)
1775 				return 0;
1776 			offset += copy;
1777 			to     += copy;
1778 		}
1779 		start = end;
1780 	}
1781 
1782 	skb_walk_frags(skb, frag_iter) {
1783 		int end;
1784 
1785 		WARN_ON(start > offset + len);
1786 
1787 		end = start + frag_iter->len;
1788 		if ((copy = end - offset) > 0) {
1789 			if (copy > len)
1790 				copy = len;
1791 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
1792 				goto fault;
1793 			if ((len -= copy) == 0)
1794 				return 0;
1795 			offset += copy;
1796 			to     += copy;
1797 		}
1798 		start = end;
1799 	}
1800 
1801 	if (!len)
1802 		return 0;
1803 
1804 fault:
1805 	return -EFAULT;
1806 }
1807 EXPORT_SYMBOL(skb_copy_bits);
1808 
1809 /*
1810  * Callback from splice_to_pipe(), if we need to release some pages
1811  * at the end of the spd in case we error'ed out in filling the pipe.
1812  */
1813 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
1814 {
1815 	put_page(spd->pages[i]);
1816 }
1817 
1818 static struct page *linear_to_page(struct page *page, unsigned int *len,
1819 				   unsigned int *offset,
1820 				   struct sock *sk)
1821 {
1822 	struct page_frag *pfrag = sk_page_frag(sk);
1823 
1824 	if (!sk_page_frag_refill(sk, pfrag))
1825 		return NULL;
1826 
1827 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
1828 
1829 	memcpy(page_address(pfrag->page) + pfrag->offset,
1830 	       page_address(page) + *offset, *len);
1831 	*offset = pfrag->offset;
1832 	pfrag->offset += *len;
1833 
1834 	return pfrag->page;
1835 }
1836 
1837 static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
1838 			     struct page *page,
1839 			     unsigned int offset)
1840 {
1841 	return	spd->nr_pages &&
1842 		spd->pages[spd->nr_pages - 1] == page &&
1843 		(spd->partial[spd->nr_pages - 1].offset +
1844 		 spd->partial[spd->nr_pages - 1].len == offset);
1845 }
1846 
1847 /*
1848  * Fill page/offset/length into spd, if it can hold more pages.
1849  */
1850 static bool spd_fill_page(struct splice_pipe_desc *spd,
1851 			  struct pipe_inode_info *pipe, struct page *page,
1852 			  unsigned int *len, unsigned int offset,
1853 			  bool linear,
1854 			  struct sock *sk)
1855 {
1856 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1857 		return true;
1858 
1859 	if (linear) {
1860 		page = linear_to_page(page, len, &offset, sk);
1861 		if (!page)
1862 			return true;
1863 	}
1864 	if (spd_can_coalesce(spd, page, offset)) {
1865 		spd->partial[spd->nr_pages - 1].len += *len;
1866 		return false;
1867 	}
1868 	get_page(page);
1869 	spd->pages[spd->nr_pages] = page;
1870 	spd->partial[spd->nr_pages].len = *len;
1871 	spd->partial[spd->nr_pages].offset = offset;
1872 	spd->nr_pages++;
1873 
1874 	return false;
1875 }
1876 
1877 static bool __splice_segment(struct page *page, unsigned int poff,
1878 			     unsigned int plen, unsigned int *off,
1879 			     unsigned int *len,
1880 			     struct splice_pipe_desc *spd, bool linear,
1881 			     struct sock *sk,
1882 			     struct pipe_inode_info *pipe)
1883 {
1884 	if (!*len)
1885 		return true;
1886 
1887 	/* skip this segment if already processed */
1888 	if (*off >= plen) {
1889 		*off -= plen;
1890 		return false;
1891 	}
1892 
1893 	/* ignore any bits we already processed */
1894 	poff += *off;
1895 	plen -= *off;
1896 	*off = 0;
1897 
1898 	do {
1899 		unsigned int flen = min(*len, plen);
1900 
1901 		if (spd_fill_page(spd, pipe, page, &flen, poff,
1902 				  linear, sk))
1903 			return true;
1904 		poff += flen;
1905 		plen -= flen;
1906 		*len -= flen;
1907 	} while (*len && plen);
1908 
1909 	return false;
1910 }
1911 
1912 /*
1913  * Map linear and fragment data from the skb to spd. It reports true if the
1914  * pipe is full or if we already spliced the requested length.
1915  */
1916 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
1917 			      unsigned int *offset, unsigned int *len,
1918 			      struct splice_pipe_desc *spd, struct sock *sk)
1919 {
1920 	int seg;
1921 
1922 	/* map the linear part :
1923 	 * If skb->head_frag is set, this 'linear' part is backed by a
1924 	 * fragment, and if the head is not shared with any clones then
1925 	 * we can avoid a copy since we own the head portion of this page.
1926 	 */
1927 	if (__splice_segment(virt_to_page(skb->data),
1928 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
1929 			     skb_headlen(skb),
1930 			     offset, len, spd,
1931 			     skb_head_is_locked(skb),
1932 			     sk, pipe))
1933 		return true;
1934 
1935 	/*
1936 	 * then map the fragments
1937 	 */
1938 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
1939 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
1940 
1941 		if (__splice_segment(skb_frag_page(f),
1942 				     f->page_offset, skb_frag_size(f),
1943 				     offset, len, spd, false, sk, pipe))
1944 			return true;
1945 	}
1946 
1947 	return false;
1948 }
1949 
1950 ssize_t skb_socket_splice(struct sock *sk,
1951 			  struct pipe_inode_info *pipe,
1952 			  struct splice_pipe_desc *spd)
1953 {
1954 	int ret;
1955 
1956 	/* Drop the socket lock, otherwise we have reverse
1957 	 * locking dependencies between sk_lock and i_mutex
1958 	 * here as compared to sendfile(). We enter here
1959 	 * with the socket lock held, and splice_to_pipe() will
1960 	 * grab the pipe inode lock. For sendfile() emulation,
1961 	 * we call into ->sendpage() with the i_mutex lock held
1962 	 * and networking will grab the socket lock.
1963 	 */
1964 	release_sock(sk);
1965 	ret = splice_to_pipe(pipe, spd);
1966 	lock_sock(sk);
1967 
1968 	return ret;
1969 }
1970 
1971 /*
1972  * Map data from the skb to a pipe. Should handle both the linear part,
1973  * the fragments, and the frag list. It does NOT handle frag lists within
1974  * the frag list, if such a thing exists. We'd probably need to recurse to
1975  * handle that cleanly.
1976  */
1977 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
1978 		    struct pipe_inode_info *pipe, unsigned int tlen,
1979 		    unsigned int flags,
1980 		    ssize_t (*splice_cb)(struct sock *,
1981 					 struct pipe_inode_info *,
1982 					 struct splice_pipe_desc *))
1983 {
1984 	struct partial_page partial[MAX_SKB_FRAGS];
1985 	struct page *pages[MAX_SKB_FRAGS];
1986 	struct splice_pipe_desc spd = {
1987 		.pages = pages,
1988 		.partial = partial,
1989 		.nr_pages_max = MAX_SKB_FRAGS,
1990 		.flags = flags,
1991 		.ops = &nosteal_pipe_buf_ops,
1992 		.spd_release = sock_spd_release,
1993 	};
1994 	struct sk_buff *frag_iter;
1995 	int ret = 0;
1996 
1997 	/*
1998 	 * __skb_splice_bits() only fails if the output has no room left,
1999 	 * so no point in going over the frag_list for the error case.
2000 	 */
2001 	if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
2002 		goto done;
2003 	else if (!tlen)
2004 		goto done;
2005 
2006 	/*
2007 	 * now see if we have a frag_list to map
2008 	 */
2009 	skb_walk_frags(skb, frag_iter) {
2010 		if (!tlen)
2011 			break;
2012 		if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
2013 			break;
2014 	}
2015 
2016 done:
2017 	if (spd.nr_pages)
2018 		ret = splice_cb(sk, pipe, &spd);
2019 
2020 	return ret;
2021 }
2022 EXPORT_SYMBOL_GPL(skb_splice_bits);
2023 
2024 /**
2025  *	skb_store_bits - store bits from kernel buffer to skb
2026  *	@skb: destination buffer
2027  *	@offset: offset in destination
2028  *	@from: source buffer
2029  *	@len: number of bytes to copy
2030  *
2031  *	Copy the specified number of bytes from the source buffer to the
2032  *	destination skb.  This function handles all the messy bits of
2033  *	traversing fragment lists and such.
2034  */
2035 
2036 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2037 {
2038 	int start = skb_headlen(skb);
2039 	struct sk_buff *frag_iter;
2040 	int i, copy;
2041 
2042 	if (offset > (int)skb->len - len)
2043 		goto fault;
2044 
2045 	if ((copy = start - offset) > 0) {
2046 		if (copy > len)
2047 			copy = len;
2048 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
2049 		if ((len -= copy) == 0)
2050 			return 0;
2051 		offset += copy;
2052 		from += copy;
2053 	}
2054 
2055 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2056 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2057 		int end;
2058 
2059 		WARN_ON(start > offset + len);
2060 
2061 		end = start + skb_frag_size(frag);
2062 		if ((copy = end - offset) > 0) {
2063 			u8 *vaddr;
2064 
2065 			if (copy > len)
2066 				copy = len;
2067 
2068 			vaddr = kmap_atomic(skb_frag_page(frag));
2069 			memcpy(vaddr + frag->page_offset + offset - start,
2070 			       from, copy);
2071 			kunmap_atomic(vaddr);
2072 
2073 			if ((len -= copy) == 0)
2074 				return 0;
2075 			offset += copy;
2076 			from += copy;
2077 		}
2078 		start = end;
2079 	}
2080 
2081 	skb_walk_frags(skb, frag_iter) {
2082 		int end;
2083 
2084 		WARN_ON(start > offset + len);
2085 
2086 		end = start + frag_iter->len;
2087 		if ((copy = end - offset) > 0) {
2088 			if (copy > len)
2089 				copy = len;
2090 			if (skb_store_bits(frag_iter, offset - start,
2091 					   from, copy))
2092 				goto fault;
2093 			if ((len -= copy) == 0)
2094 				return 0;
2095 			offset += copy;
2096 			from += copy;
2097 		}
2098 		start = end;
2099 	}
2100 	if (!len)
2101 		return 0;
2102 
2103 fault:
2104 	return -EFAULT;
2105 }
2106 EXPORT_SYMBOL(skb_store_bits);
2107 
2108 /* Checksum skb data. */
2109 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
2110 		      __wsum csum, const struct skb_checksum_ops *ops)
2111 {
2112 	int start = skb_headlen(skb);
2113 	int i, copy = start - offset;
2114 	struct sk_buff *frag_iter;
2115 	int pos = 0;
2116 
2117 	/* Checksum header. */
2118 	if (copy > 0) {
2119 		if (copy > len)
2120 			copy = len;
2121 		csum = ops->update(skb->data + offset, copy, csum);
2122 		if ((len -= copy) == 0)
2123 			return csum;
2124 		offset += copy;
2125 		pos	= copy;
2126 	}
2127 
2128 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2129 		int end;
2130 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2131 
2132 		WARN_ON(start > offset + len);
2133 
2134 		end = start + skb_frag_size(frag);
2135 		if ((copy = end - offset) > 0) {
2136 			__wsum csum2;
2137 			u8 *vaddr;
2138 
2139 			if (copy > len)
2140 				copy = len;
2141 			vaddr = kmap_atomic(skb_frag_page(frag));
2142 			csum2 = ops->update(vaddr + frag->page_offset +
2143 					    offset - start, copy, 0);
2144 			kunmap_atomic(vaddr);
2145 			csum = ops->combine(csum, csum2, pos, copy);
2146 			if (!(len -= copy))
2147 				return csum;
2148 			offset += copy;
2149 			pos    += copy;
2150 		}
2151 		start = end;
2152 	}
2153 
2154 	skb_walk_frags(skb, frag_iter) {
2155 		int end;
2156 
2157 		WARN_ON(start > offset + len);
2158 
2159 		end = start + frag_iter->len;
2160 		if ((copy = end - offset) > 0) {
2161 			__wsum csum2;
2162 			if (copy > len)
2163 				copy = len;
2164 			csum2 = __skb_checksum(frag_iter, offset - start,
2165 					       copy, 0, ops);
2166 			csum = ops->combine(csum, csum2, pos, copy);
2167 			if ((len -= copy) == 0)
2168 				return csum;
2169 			offset += copy;
2170 			pos    += copy;
2171 		}
2172 		start = end;
2173 	}
2174 	BUG_ON(len);
2175 
2176 	return csum;
2177 }
2178 EXPORT_SYMBOL(__skb_checksum);
2179 
2180 __wsum skb_checksum(const struct sk_buff *skb, int offset,
2181 		    int len, __wsum csum)
2182 {
2183 	const struct skb_checksum_ops ops = {
2184 		.update  = csum_partial_ext,
2185 		.combine = csum_block_add_ext,
2186 	};
2187 
2188 	return __skb_checksum(skb, offset, len, csum, &ops);
2189 }
2190 EXPORT_SYMBOL(skb_checksum);
2191 
2192 /* Both of above in one bottle. */
2193 
2194 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
2195 				    u8 *to, int len, __wsum csum)
2196 {
2197 	int start = skb_headlen(skb);
2198 	int i, copy = start - offset;
2199 	struct sk_buff *frag_iter;
2200 	int pos = 0;
2201 
2202 	/* Copy header. */
2203 	if (copy > 0) {
2204 		if (copy > len)
2205 			copy = len;
2206 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
2207 						 copy, csum);
2208 		if ((len -= copy) == 0)
2209 			return csum;
2210 		offset += copy;
2211 		to     += copy;
2212 		pos	= copy;
2213 	}
2214 
2215 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2216 		int end;
2217 
2218 		WARN_ON(start > offset + len);
2219 
2220 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2221 		if ((copy = end - offset) > 0) {
2222 			__wsum csum2;
2223 			u8 *vaddr;
2224 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2225 
2226 			if (copy > len)
2227 				copy = len;
2228 			vaddr = kmap_atomic(skb_frag_page(frag));
2229 			csum2 = csum_partial_copy_nocheck(vaddr +
2230 							  frag->page_offset +
2231 							  offset - start, to,
2232 							  copy, 0);
2233 			kunmap_atomic(vaddr);
2234 			csum = csum_block_add(csum, csum2, pos);
2235 			if (!(len -= copy))
2236 				return csum;
2237 			offset += copy;
2238 			to     += copy;
2239 			pos    += copy;
2240 		}
2241 		start = end;
2242 	}
2243 
2244 	skb_walk_frags(skb, frag_iter) {
2245 		__wsum csum2;
2246 		int end;
2247 
2248 		WARN_ON(start > offset + len);
2249 
2250 		end = start + frag_iter->len;
2251 		if ((copy = end - offset) > 0) {
2252 			if (copy > len)
2253 				copy = len;
2254 			csum2 = skb_copy_and_csum_bits(frag_iter,
2255 						       offset - start,
2256 						       to, copy, 0);
2257 			csum = csum_block_add(csum, csum2, pos);
2258 			if ((len -= copy) == 0)
2259 				return csum;
2260 			offset += copy;
2261 			to     += copy;
2262 			pos    += copy;
2263 		}
2264 		start = end;
2265 	}
2266 	BUG_ON(len);
2267 	return csum;
2268 }
2269 EXPORT_SYMBOL(skb_copy_and_csum_bits);
2270 
2271  /**
2272  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2273  *	@from: source buffer
2274  *
2275  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2276  *	into skb_zerocopy().
2277  */
2278 unsigned int
2279 skb_zerocopy_headlen(const struct sk_buff *from)
2280 {
2281 	unsigned int hlen = 0;
2282 
2283 	if (!from->head_frag ||
2284 	    skb_headlen(from) < L1_CACHE_BYTES ||
2285 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2286 		hlen = skb_headlen(from);
2287 
2288 	if (skb_has_frag_list(from))
2289 		hlen = from->len;
2290 
2291 	return hlen;
2292 }
2293 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2294 
2295 /**
2296  *	skb_zerocopy - Zero copy skb to skb
2297  *	@to: destination buffer
2298  *	@from: source buffer
2299  *	@len: number of bytes to copy from source buffer
2300  *	@hlen: size of linear headroom in destination buffer
2301  *
2302  *	Copies up to `len` bytes from `from` to `to` by creating references
2303  *	to the frags in the source buffer.
2304  *
2305  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2306  *	headroom in the `to` buffer.
2307  *
2308  *	Return value:
2309  *	0: everything is OK
2310  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
2311  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2312  */
2313 int
2314 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2315 {
2316 	int i, j = 0;
2317 	int plen = 0; /* length of skb->head fragment */
2318 	int ret;
2319 	struct page *page;
2320 	unsigned int offset;
2321 
2322 	BUG_ON(!from->head_frag && !hlen);
2323 
2324 	/* dont bother with small payloads */
2325 	if (len <= skb_tailroom(to))
2326 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2327 
2328 	if (hlen) {
2329 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
2330 		if (unlikely(ret))
2331 			return ret;
2332 		len -= hlen;
2333 	} else {
2334 		plen = min_t(int, skb_headlen(from), len);
2335 		if (plen) {
2336 			page = virt_to_head_page(from->head);
2337 			offset = from->data - (unsigned char *)page_address(page);
2338 			__skb_fill_page_desc(to, 0, page, offset, plen);
2339 			get_page(page);
2340 			j = 1;
2341 			len -= plen;
2342 		}
2343 	}
2344 
2345 	to->truesize += len + plen;
2346 	to->len += len + plen;
2347 	to->data_len += len + plen;
2348 
2349 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
2350 		skb_tx_error(from);
2351 		return -ENOMEM;
2352 	}
2353 
2354 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2355 		if (!len)
2356 			break;
2357 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2358 		skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2359 		len -= skb_shinfo(to)->frags[j].size;
2360 		skb_frag_ref(to, j);
2361 		j++;
2362 	}
2363 	skb_shinfo(to)->nr_frags = j;
2364 
2365 	return 0;
2366 }
2367 EXPORT_SYMBOL_GPL(skb_zerocopy);
2368 
2369 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
2370 {
2371 	__wsum csum;
2372 	long csstart;
2373 
2374 	if (skb->ip_summed == CHECKSUM_PARTIAL)
2375 		csstart = skb_checksum_start_offset(skb);
2376 	else
2377 		csstart = skb_headlen(skb);
2378 
2379 	BUG_ON(csstart > skb_headlen(skb));
2380 
2381 	skb_copy_from_linear_data(skb, to, csstart);
2382 
2383 	csum = 0;
2384 	if (csstart != skb->len)
2385 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
2386 					      skb->len - csstart, 0);
2387 
2388 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2389 		long csstuff = csstart + skb->csum_offset;
2390 
2391 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
2392 	}
2393 }
2394 EXPORT_SYMBOL(skb_copy_and_csum_dev);
2395 
2396 /**
2397  *	skb_dequeue - remove from the head of the queue
2398  *	@list: list to dequeue from
2399  *
2400  *	Remove the head of the list. The list lock is taken so the function
2401  *	may be used safely with other locking list functions. The head item is
2402  *	returned or %NULL if the list is empty.
2403  */
2404 
2405 struct sk_buff *skb_dequeue(struct sk_buff_head *list)
2406 {
2407 	unsigned long flags;
2408 	struct sk_buff *result;
2409 
2410 	spin_lock_irqsave(&list->lock, flags);
2411 	result = __skb_dequeue(list);
2412 	spin_unlock_irqrestore(&list->lock, flags);
2413 	return result;
2414 }
2415 EXPORT_SYMBOL(skb_dequeue);
2416 
2417 /**
2418  *	skb_dequeue_tail - remove from the tail of the queue
2419  *	@list: list to dequeue from
2420  *
2421  *	Remove the tail of the list. The list lock is taken so the function
2422  *	may be used safely with other locking list functions. The tail item is
2423  *	returned or %NULL if the list is empty.
2424  */
2425 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
2426 {
2427 	unsigned long flags;
2428 	struct sk_buff *result;
2429 
2430 	spin_lock_irqsave(&list->lock, flags);
2431 	result = __skb_dequeue_tail(list);
2432 	spin_unlock_irqrestore(&list->lock, flags);
2433 	return result;
2434 }
2435 EXPORT_SYMBOL(skb_dequeue_tail);
2436 
2437 /**
2438  *	skb_queue_purge - empty a list
2439  *	@list: list to empty
2440  *
2441  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
2442  *	the list and one reference dropped. This function takes the list
2443  *	lock and is atomic with respect to other list locking functions.
2444  */
2445 void skb_queue_purge(struct sk_buff_head *list)
2446 {
2447 	struct sk_buff *skb;
2448 	while ((skb = skb_dequeue(list)) != NULL)
2449 		kfree_skb(skb);
2450 }
2451 EXPORT_SYMBOL(skb_queue_purge);
2452 
2453 /**
2454  *	skb_queue_head - queue a buffer at the list head
2455  *	@list: list to use
2456  *	@newsk: buffer to queue
2457  *
2458  *	Queue a buffer at the start of the list. This function takes the
2459  *	list lock and can be used safely with other locking &sk_buff functions
2460  *	safely.
2461  *
2462  *	A buffer cannot be placed on two lists at the same time.
2463  */
2464 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
2465 {
2466 	unsigned long flags;
2467 
2468 	spin_lock_irqsave(&list->lock, flags);
2469 	__skb_queue_head(list, newsk);
2470 	spin_unlock_irqrestore(&list->lock, flags);
2471 }
2472 EXPORT_SYMBOL(skb_queue_head);
2473 
2474 /**
2475  *	skb_queue_tail - queue a buffer at the list tail
2476  *	@list: list to use
2477  *	@newsk: buffer to queue
2478  *
2479  *	Queue a buffer at the tail of the list. This function takes the
2480  *	list lock and can be used safely with other locking &sk_buff functions
2481  *	safely.
2482  *
2483  *	A buffer cannot be placed on two lists at the same time.
2484  */
2485 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
2486 {
2487 	unsigned long flags;
2488 
2489 	spin_lock_irqsave(&list->lock, flags);
2490 	__skb_queue_tail(list, newsk);
2491 	spin_unlock_irqrestore(&list->lock, flags);
2492 }
2493 EXPORT_SYMBOL(skb_queue_tail);
2494 
2495 /**
2496  *	skb_unlink	-	remove a buffer from a list
2497  *	@skb: buffer to remove
2498  *	@list: list to use
2499  *
2500  *	Remove a packet from a list. The list locks are taken and this
2501  *	function is atomic with respect to other list locked calls
2502  *
2503  *	You must know what list the SKB is on.
2504  */
2505 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
2506 {
2507 	unsigned long flags;
2508 
2509 	spin_lock_irqsave(&list->lock, flags);
2510 	__skb_unlink(skb, list);
2511 	spin_unlock_irqrestore(&list->lock, flags);
2512 }
2513 EXPORT_SYMBOL(skb_unlink);
2514 
2515 /**
2516  *	skb_append	-	append a buffer
2517  *	@old: buffer to insert after
2518  *	@newsk: buffer to insert
2519  *	@list: list to use
2520  *
2521  *	Place a packet after a given packet in a list. The list locks are taken
2522  *	and this function is atomic with respect to other list locked calls.
2523  *	A buffer cannot be placed on two lists at the same time.
2524  */
2525 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2526 {
2527 	unsigned long flags;
2528 
2529 	spin_lock_irqsave(&list->lock, flags);
2530 	__skb_queue_after(list, old, newsk);
2531 	spin_unlock_irqrestore(&list->lock, flags);
2532 }
2533 EXPORT_SYMBOL(skb_append);
2534 
2535 /**
2536  *	skb_insert	-	insert a buffer
2537  *	@old: buffer to insert before
2538  *	@newsk: buffer to insert
2539  *	@list: list to use
2540  *
2541  *	Place a packet before a given packet in a list. The list locks are
2542  * 	taken and this function is atomic with respect to other list locked
2543  *	calls.
2544  *
2545  *	A buffer cannot be placed on two lists at the same time.
2546  */
2547 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
2548 {
2549 	unsigned long flags;
2550 
2551 	spin_lock_irqsave(&list->lock, flags);
2552 	__skb_insert(newsk, old->prev, old, list);
2553 	spin_unlock_irqrestore(&list->lock, flags);
2554 }
2555 EXPORT_SYMBOL(skb_insert);
2556 
2557 static inline void skb_split_inside_header(struct sk_buff *skb,
2558 					   struct sk_buff* skb1,
2559 					   const u32 len, const int pos)
2560 {
2561 	int i;
2562 
2563 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2564 					 pos - len);
2565 	/* And move data appendix as is. */
2566 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2567 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
2568 
2569 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
2570 	skb_shinfo(skb)->nr_frags  = 0;
2571 	skb1->data_len		   = skb->data_len;
2572 	skb1->len		   += skb1->data_len;
2573 	skb->data_len		   = 0;
2574 	skb->len		   = len;
2575 	skb_set_tail_pointer(skb, len);
2576 }
2577 
2578 static inline void skb_split_no_header(struct sk_buff *skb,
2579 				       struct sk_buff* skb1,
2580 				       const u32 len, int pos)
2581 {
2582 	int i, k = 0;
2583 	const int nfrags = skb_shinfo(skb)->nr_frags;
2584 
2585 	skb_shinfo(skb)->nr_frags = 0;
2586 	skb1->len		  = skb1->data_len = skb->len - len;
2587 	skb->len		  = len;
2588 	skb->data_len		  = len - pos;
2589 
2590 	for (i = 0; i < nfrags; i++) {
2591 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
2592 
2593 		if (pos + size > len) {
2594 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
2595 
2596 			if (pos < len) {
2597 				/* Split frag.
2598 				 * We have two variants in this case:
2599 				 * 1. Move all the frag to the second
2600 				 *    part, if it is possible. F.e.
2601 				 *    this approach is mandatory for TUX,
2602 				 *    where splitting is expensive.
2603 				 * 2. Split is accurately. We make this.
2604 				 */
2605 				skb_frag_ref(skb, i);
2606 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
2607 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
2608 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
2609 				skb_shinfo(skb)->nr_frags++;
2610 			}
2611 			k++;
2612 		} else
2613 			skb_shinfo(skb)->nr_frags++;
2614 		pos += size;
2615 	}
2616 	skb_shinfo(skb1)->nr_frags = k;
2617 }
2618 
2619 /**
2620  * skb_split - Split fragmented skb to two parts at length len.
2621  * @skb: the buffer to split
2622  * @skb1: the buffer to receive the second part
2623  * @len: new length for skb
2624  */
2625 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
2626 {
2627 	int pos = skb_headlen(skb);
2628 
2629 	skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2630 	if (len < pos)	/* Split line is inside header. */
2631 		skb_split_inside_header(skb, skb1, len, pos);
2632 	else		/* Second chunk has no header, nothing to copy. */
2633 		skb_split_no_header(skb, skb1, len, pos);
2634 }
2635 EXPORT_SYMBOL(skb_split);
2636 
2637 /* Shifting from/to a cloned skb is a no-go.
2638  *
2639  * Caller cannot keep skb_shinfo related pointers past calling here!
2640  */
2641 static int skb_prepare_for_shift(struct sk_buff *skb)
2642 {
2643 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2644 }
2645 
2646 /**
2647  * skb_shift - Shifts paged data partially from skb to another
2648  * @tgt: buffer into which tail data gets added
2649  * @skb: buffer from which the paged data comes from
2650  * @shiftlen: shift up to this many bytes
2651  *
2652  * Attempts to shift up to shiftlen worth of bytes, which may be less than
2653  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2654  * It's up to caller to free skb if everything was shifted.
2655  *
2656  * If @tgt runs out of frags, the whole operation is aborted.
2657  *
2658  * Skb cannot include anything else but paged data while tgt is allowed
2659  * to have non-paged data as well.
2660  *
2661  * TODO: full sized shift could be optimized but that would need
2662  * specialized skb free'er to handle frags without up-to-date nr_frags.
2663  */
2664 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2665 {
2666 	int from, to, merge, todo;
2667 	struct skb_frag_struct *fragfrom, *fragto;
2668 
2669 	BUG_ON(shiftlen > skb->len);
2670 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2671 
2672 	todo = shiftlen;
2673 	from = 0;
2674 	to = skb_shinfo(tgt)->nr_frags;
2675 	fragfrom = &skb_shinfo(skb)->frags[from];
2676 
2677 	/* Actual merge is delayed until the point when we know we can
2678 	 * commit all, so that we don't have to undo partial changes
2679 	 */
2680 	if (!to ||
2681 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2682 			      fragfrom->page_offset)) {
2683 		merge = -1;
2684 	} else {
2685 		merge = to - 1;
2686 
2687 		todo -= skb_frag_size(fragfrom);
2688 		if (todo < 0) {
2689 			if (skb_prepare_for_shift(skb) ||
2690 			    skb_prepare_for_shift(tgt))
2691 				return 0;
2692 
2693 			/* All previous frag pointers might be stale! */
2694 			fragfrom = &skb_shinfo(skb)->frags[from];
2695 			fragto = &skb_shinfo(tgt)->frags[merge];
2696 
2697 			skb_frag_size_add(fragto, shiftlen);
2698 			skb_frag_size_sub(fragfrom, shiftlen);
2699 			fragfrom->page_offset += shiftlen;
2700 
2701 			goto onlymerged;
2702 		}
2703 
2704 		from++;
2705 	}
2706 
2707 	/* Skip full, not-fitting skb to avoid expensive operations */
2708 	if ((shiftlen == skb->len) &&
2709 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2710 		return 0;
2711 
2712 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2713 		return 0;
2714 
2715 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2716 		if (to == MAX_SKB_FRAGS)
2717 			return 0;
2718 
2719 		fragfrom = &skb_shinfo(skb)->frags[from];
2720 		fragto = &skb_shinfo(tgt)->frags[to];
2721 
2722 		if (todo >= skb_frag_size(fragfrom)) {
2723 			*fragto = *fragfrom;
2724 			todo -= skb_frag_size(fragfrom);
2725 			from++;
2726 			to++;
2727 
2728 		} else {
2729 			__skb_frag_ref(fragfrom);
2730 			fragto->page = fragfrom->page;
2731 			fragto->page_offset = fragfrom->page_offset;
2732 			skb_frag_size_set(fragto, todo);
2733 
2734 			fragfrom->page_offset += todo;
2735 			skb_frag_size_sub(fragfrom, todo);
2736 			todo = 0;
2737 
2738 			to++;
2739 			break;
2740 		}
2741 	}
2742 
2743 	/* Ready to "commit" this state change to tgt */
2744 	skb_shinfo(tgt)->nr_frags = to;
2745 
2746 	if (merge >= 0) {
2747 		fragfrom = &skb_shinfo(skb)->frags[0];
2748 		fragto = &skb_shinfo(tgt)->frags[merge];
2749 
2750 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2751 		__skb_frag_unref(fragfrom);
2752 	}
2753 
2754 	/* Reposition in the original skb */
2755 	to = 0;
2756 	while (from < skb_shinfo(skb)->nr_frags)
2757 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2758 	skb_shinfo(skb)->nr_frags = to;
2759 
2760 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2761 
2762 onlymerged:
2763 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2764 	 * the other hand might need it if it needs to be resent
2765 	 */
2766 	tgt->ip_summed = CHECKSUM_PARTIAL;
2767 	skb->ip_summed = CHECKSUM_PARTIAL;
2768 
2769 	/* Yak, is it really working this way? Some helper please? */
2770 	skb->len -= shiftlen;
2771 	skb->data_len -= shiftlen;
2772 	skb->truesize -= shiftlen;
2773 	tgt->len += shiftlen;
2774 	tgt->data_len += shiftlen;
2775 	tgt->truesize += shiftlen;
2776 
2777 	return shiftlen;
2778 }
2779 
2780 /**
2781  * skb_prepare_seq_read - Prepare a sequential read of skb data
2782  * @skb: the buffer to read
2783  * @from: lower offset of data to be read
2784  * @to: upper offset of data to be read
2785  * @st: state variable
2786  *
2787  * Initializes the specified state variable. Must be called before
2788  * invoking skb_seq_read() for the first time.
2789  */
2790 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2791 			  unsigned int to, struct skb_seq_state *st)
2792 {
2793 	st->lower_offset = from;
2794 	st->upper_offset = to;
2795 	st->root_skb = st->cur_skb = skb;
2796 	st->frag_idx = st->stepped_offset = 0;
2797 	st->frag_data = NULL;
2798 }
2799 EXPORT_SYMBOL(skb_prepare_seq_read);
2800 
2801 /**
2802  * skb_seq_read - Sequentially read skb data
2803  * @consumed: number of bytes consumed by the caller so far
2804  * @data: destination pointer for data to be returned
2805  * @st: state variable
2806  *
2807  * Reads a block of skb data at @consumed relative to the
2808  * lower offset specified to skb_prepare_seq_read(). Assigns
2809  * the head of the data block to @data and returns the length
2810  * of the block or 0 if the end of the skb data or the upper
2811  * offset has been reached.
2812  *
2813  * The caller is not required to consume all of the data
2814  * returned, i.e. @consumed is typically set to the number
2815  * of bytes already consumed and the next call to
2816  * skb_seq_read() will return the remaining part of the block.
2817  *
2818  * Note 1: The size of each block of data returned can be arbitrary,
2819  *       this limitation is the cost for zerocopy sequential
2820  *       reads of potentially non linear data.
2821  *
2822  * Note 2: Fragment lists within fragments are not implemented
2823  *       at the moment, state->root_skb could be replaced with
2824  *       a stack for this purpose.
2825  */
2826 unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2827 			  struct skb_seq_state *st)
2828 {
2829 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2830 	skb_frag_t *frag;
2831 
2832 	if (unlikely(abs_offset >= st->upper_offset)) {
2833 		if (st->frag_data) {
2834 			kunmap_atomic(st->frag_data);
2835 			st->frag_data = NULL;
2836 		}
2837 		return 0;
2838 	}
2839 
2840 next_skb:
2841 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2842 
2843 	if (abs_offset < block_limit && !st->frag_data) {
2844 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2845 		return block_limit - abs_offset;
2846 	}
2847 
2848 	if (st->frag_idx == 0 && !st->frag_data)
2849 		st->stepped_offset += skb_headlen(st->cur_skb);
2850 
2851 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2852 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2853 		block_limit = skb_frag_size(frag) + st->stepped_offset;
2854 
2855 		if (abs_offset < block_limit) {
2856 			if (!st->frag_data)
2857 				st->frag_data = kmap_atomic(skb_frag_page(frag));
2858 
2859 			*data = (u8 *) st->frag_data + frag->page_offset +
2860 				(abs_offset - st->stepped_offset);
2861 
2862 			return block_limit - abs_offset;
2863 		}
2864 
2865 		if (st->frag_data) {
2866 			kunmap_atomic(st->frag_data);
2867 			st->frag_data = NULL;
2868 		}
2869 
2870 		st->frag_idx++;
2871 		st->stepped_offset += skb_frag_size(frag);
2872 	}
2873 
2874 	if (st->frag_data) {
2875 		kunmap_atomic(st->frag_data);
2876 		st->frag_data = NULL;
2877 	}
2878 
2879 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2880 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2881 		st->frag_idx = 0;
2882 		goto next_skb;
2883 	} else if (st->cur_skb->next) {
2884 		st->cur_skb = st->cur_skb->next;
2885 		st->frag_idx = 0;
2886 		goto next_skb;
2887 	}
2888 
2889 	return 0;
2890 }
2891 EXPORT_SYMBOL(skb_seq_read);
2892 
2893 /**
2894  * skb_abort_seq_read - Abort a sequential read of skb data
2895  * @st: state variable
2896  *
2897  * Must be called if skb_seq_read() was not called until it
2898  * returned 0.
2899  */
2900 void skb_abort_seq_read(struct skb_seq_state *st)
2901 {
2902 	if (st->frag_data)
2903 		kunmap_atomic(st->frag_data);
2904 }
2905 EXPORT_SYMBOL(skb_abort_seq_read);
2906 
2907 #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
2908 
2909 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
2910 					  struct ts_config *conf,
2911 					  struct ts_state *state)
2912 {
2913 	return skb_seq_read(offset, text, TS_SKB_CB(state));
2914 }
2915 
2916 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2917 {
2918 	skb_abort_seq_read(TS_SKB_CB(state));
2919 }
2920 
2921 /**
2922  * skb_find_text - Find a text pattern in skb data
2923  * @skb: the buffer to look in
2924  * @from: search offset
2925  * @to: search limit
2926  * @config: textsearch configuration
2927  *
2928  * Finds a pattern in the skb data according to the specified
2929  * textsearch configuration. Use textsearch_next() to retrieve
2930  * subsequent occurrences of the pattern. Returns the offset
2931  * to the first occurrence or UINT_MAX if no match was found.
2932  */
2933 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2934 			   unsigned int to, struct ts_config *config)
2935 {
2936 	struct ts_state state;
2937 	unsigned int ret;
2938 
2939 	config->get_next_block = skb_ts_get_next_block;
2940 	config->finish = skb_ts_finish;
2941 
2942 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2943 
2944 	ret = textsearch_find(config, &state);
2945 	return (ret <= to - from ? ret : UINT_MAX);
2946 }
2947 EXPORT_SYMBOL(skb_find_text);
2948 
2949 /**
2950  * skb_append_datato_frags - append the user data to a skb
2951  * @sk: sock  structure
2952  * @skb: skb structure to be appended with user data.
2953  * @getfrag: call back function to be used for getting the user data
2954  * @from: pointer to user message iov
2955  * @length: length of the iov message
2956  *
2957  * Description: This procedure append the user data in the fragment part
2958  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2959  */
2960 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2961 			int (*getfrag)(void *from, char *to, int offset,
2962 					int len, int odd, struct sk_buff *skb),
2963 			void *from, int length)
2964 {
2965 	int frg_cnt = skb_shinfo(skb)->nr_frags;
2966 	int copy;
2967 	int offset = 0;
2968 	int ret;
2969 	struct page_frag *pfrag = &current->task_frag;
2970 
2971 	do {
2972 		/* Return error if we don't have space for new frag */
2973 		if (frg_cnt >= MAX_SKB_FRAGS)
2974 			return -EMSGSIZE;
2975 
2976 		if (!sk_page_frag_refill(sk, pfrag))
2977 			return -ENOMEM;
2978 
2979 		/* copy the user data to page */
2980 		copy = min_t(int, length, pfrag->size - pfrag->offset);
2981 
2982 		ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2983 			      offset, copy, 0, skb);
2984 		if (ret < 0)
2985 			return -EFAULT;
2986 
2987 		/* copy was successful so update the size parameters */
2988 		skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2989 				   copy);
2990 		frg_cnt++;
2991 		pfrag->offset += copy;
2992 		get_page(pfrag->page);
2993 
2994 		skb->truesize += copy;
2995 		atomic_add(copy, &sk->sk_wmem_alloc);
2996 		skb->len += copy;
2997 		skb->data_len += copy;
2998 		offset += copy;
2999 		length -= copy;
3000 
3001 	} while (length > 0);
3002 
3003 	return 0;
3004 }
3005 EXPORT_SYMBOL(skb_append_datato_frags);
3006 
3007 int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3008 			 int offset, size_t size)
3009 {
3010 	int i = skb_shinfo(skb)->nr_frags;
3011 
3012 	if (skb_can_coalesce(skb, i, page, offset)) {
3013 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3014 	} else if (i < MAX_SKB_FRAGS) {
3015 		get_page(page);
3016 		skb_fill_page_desc(skb, i, page, offset, size);
3017 	} else {
3018 		return -EMSGSIZE;
3019 	}
3020 
3021 	return 0;
3022 }
3023 EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3024 
3025 /**
3026  *	skb_pull_rcsum - pull skb and update receive checksum
3027  *	@skb: buffer to update
3028  *	@len: length of data pulled
3029  *
3030  *	This function performs an skb_pull on the packet and updates
3031  *	the CHECKSUM_COMPLETE checksum.  It should be used on
3032  *	receive path processing instead of skb_pull unless you know
3033  *	that the checksum difference is zero (e.g., a valid IP header)
3034  *	or you are setting ip_summed to CHECKSUM_NONE.
3035  */
3036 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3037 {
3038 	unsigned char *data = skb->data;
3039 
3040 	BUG_ON(len > skb->len);
3041 	__skb_pull(skb, len);
3042 	skb_postpull_rcsum(skb, data, len);
3043 	return skb->data;
3044 }
3045 EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3046 
3047 /**
3048  *	skb_segment - Perform protocol segmentation on skb.
3049  *	@head_skb: buffer to segment
3050  *	@features: features for the output path (see dev->features)
3051  *
3052  *	This function performs segmentation on the given skb.  It returns
3053  *	a pointer to the first in a list of new skbs for the segments.
3054  *	In case of error it returns ERR_PTR(err).
3055  */
3056 struct sk_buff *skb_segment(struct sk_buff *head_skb,
3057 			    netdev_features_t features)
3058 {
3059 	struct sk_buff *segs = NULL;
3060 	struct sk_buff *tail = NULL;
3061 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3062 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3063 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
3064 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
3065 	struct sk_buff *frag_skb = head_skb;
3066 	unsigned int offset = doffset;
3067 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3068 	unsigned int headroom;
3069 	unsigned int len;
3070 	__be16 proto;
3071 	bool csum;
3072 	int sg = !!(features & NETIF_F_SG);
3073 	int nfrags = skb_shinfo(head_skb)->nr_frags;
3074 	int err = -ENOMEM;
3075 	int i = 0;
3076 	int pos;
3077 	int dummy;
3078 
3079 	__skb_push(head_skb, doffset);
3080 	proto = skb_network_protocol(head_skb, &dummy);
3081 	if (unlikely(!proto))
3082 		return ERR_PTR(-EINVAL);
3083 
3084 	csum = !!can_checksum_protocol(features, proto);
3085 
3086 	headroom = skb_headroom(head_skb);
3087 	pos = skb_headlen(head_skb);
3088 
3089 	do {
3090 		struct sk_buff *nskb;
3091 		skb_frag_t *nskb_frag;
3092 		int hsize;
3093 		int size;
3094 
3095 		len = head_skb->len - offset;
3096 		if (len > mss)
3097 			len = mss;
3098 
3099 		hsize = skb_headlen(head_skb) - offset;
3100 		if (hsize < 0)
3101 			hsize = 0;
3102 		if (hsize > len || !sg)
3103 			hsize = len;
3104 
3105 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
3106 		    (skb_headlen(list_skb) == len || sg)) {
3107 			BUG_ON(skb_headlen(list_skb) > len);
3108 
3109 			i = 0;
3110 			nfrags = skb_shinfo(list_skb)->nr_frags;
3111 			frag = skb_shinfo(list_skb)->frags;
3112 			frag_skb = list_skb;
3113 			pos += skb_headlen(list_skb);
3114 
3115 			while (pos < offset + len) {
3116 				BUG_ON(i >= nfrags);
3117 
3118 				size = skb_frag_size(frag);
3119 				if (pos + size > offset + len)
3120 					break;
3121 
3122 				i++;
3123 				pos += size;
3124 				frag++;
3125 			}
3126 
3127 			nskb = skb_clone(list_skb, GFP_ATOMIC);
3128 			list_skb = list_skb->next;
3129 
3130 			if (unlikely(!nskb))
3131 				goto err;
3132 
3133 			if (unlikely(pskb_trim(nskb, len))) {
3134 				kfree_skb(nskb);
3135 				goto err;
3136 			}
3137 
3138 			hsize = skb_end_offset(nskb);
3139 			if (skb_cow_head(nskb, doffset + headroom)) {
3140 				kfree_skb(nskb);
3141 				goto err;
3142 			}
3143 
3144 			nskb->truesize += skb_end_offset(nskb) - hsize;
3145 			skb_release_head_state(nskb);
3146 			__skb_push(nskb, doffset);
3147 		} else {
3148 			nskb = __alloc_skb(hsize + doffset + headroom,
3149 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3150 					   NUMA_NO_NODE);
3151 
3152 			if (unlikely(!nskb))
3153 				goto err;
3154 
3155 			skb_reserve(nskb, headroom);
3156 			__skb_put(nskb, doffset);
3157 		}
3158 
3159 		if (segs)
3160 			tail->next = nskb;
3161 		else
3162 			segs = nskb;
3163 		tail = nskb;
3164 
3165 		__copy_skb_header(nskb, head_skb);
3166 
3167 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3168 		skb_reset_mac_len(nskb);
3169 
3170 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
3171 						 nskb->data - tnl_hlen,
3172 						 doffset + tnl_hlen);
3173 
3174 		if (nskb->len == len + doffset)
3175 			goto perform_csum_check;
3176 
3177 		if (!sg) {
3178 			if (!nskb->remcsum_offload)
3179 				nskb->ip_summed = CHECKSUM_NONE;
3180 			SKB_GSO_CB(nskb)->csum =
3181 				skb_copy_and_csum_bits(head_skb, offset,
3182 						       skb_put(nskb, len),
3183 						       len, 0);
3184 			SKB_GSO_CB(nskb)->csum_start =
3185 				skb_headroom(nskb) + doffset;
3186 			continue;
3187 		}
3188 
3189 		nskb_frag = skb_shinfo(nskb)->frags;
3190 
3191 		skb_copy_from_linear_data_offset(head_skb, offset,
3192 						 skb_put(nskb, hsize), hsize);
3193 
3194 		skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3195 			SKBTX_SHARED_FRAG;
3196 
3197 		while (pos < offset + len) {
3198 			if (i >= nfrags) {
3199 				BUG_ON(skb_headlen(list_skb));
3200 
3201 				i = 0;
3202 				nfrags = skb_shinfo(list_skb)->nr_frags;
3203 				frag = skb_shinfo(list_skb)->frags;
3204 				frag_skb = list_skb;
3205 
3206 				BUG_ON(!nfrags);
3207 
3208 				list_skb = list_skb->next;
3209 			}
3210 
3211 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
3212 				     MAX_SKB_FRAGS)) {
3213 				net_warn_ratelimited(
3214 					"skb_segment: too many frags: %u %u\n",
3215 					pos, mss);
3216 				goto err;
3217 			}
3218 
3219 			if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
3220 				goto err;
3221 
3222 			*nskb_frag = *frag;
3223 			__skb_frag_ref(nskb_frag);
3224 			size = skb_frag_size(nskb_frag);
3225 
3226 			if (pos < offset) {
3227 				nskb_frag->page_offset += offset - pos;
3228 				skb_frag_size_sub(nskb_frag, offset - pos);
3229 			}
3230 
3231 			skb_shinfo(nskb)->nr_frags++;
3232 
3233 			if (pos + size <= offset + len) {
3234 				i++;
3235 				frag++;
3236 				pos += size;
3237 			} else {
3238 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
3239 				goto skip_fraglist;
3240 			}
3241 
3242 			nskb_frag++;
3243 		}
3244 
3245 skip_fraglist:
3246 		nskb->data_len = len - hsize;
3247 		nskb->len += nskb->data_len;
3248 		nskb->truesize += nskb->data_len;
3249 
3250 perform_csum_check:
3251 		if (!csum) {
3252 			if (skb_has_shared_frag(nskb)) {
3253 				err = __skb_linearize(nskb);
3254 				if (err)
3255 					goto err;
3256 			}
3257 			if (!nskb->remcsum_offload)
3258 				nskb->ip_summed = CHECKSUM_NONE;
3259 			SKB_GSO_CB(nskb)->csum =
3260 				skb_checksum(nskb, doffset,
3261 					     nskb->len - doffset, 0);
3262 			SKB_GSO_CB(nskb)->csum_start =
3263 				skb_headroom(nskb) + doffset;
3264 		}
3265 	} while ((offset += len) < head_skb->len);
3266 
3267 	/* Some callers want to get the end of the list.
3268 	 * Put it in segs->prev to avoid walking the list.
3269 	 * (see validate_xmit_skb_list() for example)
3270 	 */
3271 	segs->prev = tail;
3272 
3273 	/* Following permits correct backpressure, for protocols
3274 	 * using skb_set_owner_w().
3275 	 * Idea is to tranfert ownership from head_skb to last segment.
3276 	 */
3277 	if (head_skb->destructor == sock_wfree) {
3278 		swap(tail->truesize, head_skb->truesize);
3279 		swap(tail->destructor, head_skb->destructor);
3280 		swap(tail->sk, head_skb->sk);
3281 	}
3282 	return segs;
3283 
3284 err:
3285 	kfree_skb_list(segs);
3286 	return ERR_PTR(err);
3287 }
3288 EXPORT_SYMBOL_GPL(skb_segment);
3289 
3290 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3291 {
3292 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3293 	unsigned int offset = skb_gro_offset(skb);
3294 	unsigned int headlen = skb_headlen(skb);
3295 	unsigned int len = skb_gro_len(skb);
3296 	struct sk_buff *lp, *p = *head;
3297 	unsigned int delta_truesize;
3298 
3299 	if (unlikely(p->len + len >= 65536))
3300 		return -E2BIG;
3301 
3302 	lp = NAPI_GRO_CB(p)->last;
3303 	pinfo = skb_shinfo(lp);
3304 
3305 	if (headlen <= offset) {
3306 		skb_frag_t *frag;
3307 		skb_frag_t *frag2;
3308 		int i = skbinfo->nr_frags;
3309 		int nr_frags = pinfo->nr_frags + i;
3310 
3311 		if (nr_frags > MAX_SKB_FRAGS)
3312 			goto merge;
3313 
3314 		offset -= headlen;
3315 		pinfo->nr_frags = nr_frags;
3316 		skbinfo->nr_frags = 0;
3317 
3318 		frag = pinfo->frags + nr_frags;
3319 		frag2 = skbinfo->frags + i;
3320 		do {
3321 			*--frag = *--frag2;
3322 		} while (--i);
3323 
3324 		frag->page_offset += offset;
3325 		skb_frag_size_sub(frag, offset);
3326 
3327 		/* all fragments truesize : remove (head size + sk_buff) */
3328 		delta_truesize = skb->truesize -
3329 				 SKB_TRUESIZE(skb_end_offset(skb));
3330 
3331 		skb->truesize -= skb->data_len;
3332 		skb->len -= skb->data_len;
3333 		skb->data_len = 0;
3334 
3335 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
3336 		goto done;
3337 	} else if (skb->head_frag) {
3338 		int nr_frags = pinfo->nr_frags;
3339 		skb_frag_t *frag = pinfo->frags + nr_frags;
3340 		struct page *page = virt_to_head_page(skb->head);
3341 		unsigned int first_size = headlen - offset;
3342 		unsigned int first_offset;
3343 
3344 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
3345 			goto merge;
3346 
3347 		first_offset = skb->data -
3348 			       (unsigned char *)page_address(page) +
3349 			       offset;
3350 
3351 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3352 
3353 		frag->page.p	  = page;
3354 		frag->page_offset = first_offset;
3355 		skb_frag_size_set(frag, first_size);
3356 
3357 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3358 		/* We dont need to clear skbinfo->nr_frags here */
3359 
3360 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3361 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3362 		goto done;
3363 	}
3364 
3365 merge:
3366 	delta_truesize = skb->truesize;
3367 	if (offset > headlen) {
3368 		unsigned int eat = offset - headlen;
3369 
3370 		skbinfo->frags[0].page_offset += eat;
3371 		skb_frag_size_sub(&skbinfo->frags[0], eat);
3372 		skb->data_len -= eat;
3373 		skb->len -= eat;
3374 		offset = headlen;
3375 	}
3376 
3377 	__skb_pull(skb, offset);
3378 
3379 	if (NAPI_GRO_CB(p)->last == p)
3380 		skb_shinfo(p)->frag_list = skb;
3381 	else
3382 		NAPI_GRO_CB(p)->last->next = skb;
3383 	NAPI_GRO_CB(p)->last = skb;
3384 	__skb_header_release(skb);
3385 	lp = p;
3386 
3387 done:
3388 	NAPI_GRO_CB(p)->count++;
3389 	p->data_len += len;
3390 	p->truesize += delta_truesize;
3391 	p->len += len;
3392 	if (lp != p) {
3393 		lp->data_len += len;
3394 		lp->truesize += delta_truesize;
3395 		lp->len += len;
3396 	}
3397 	NAPI_GRO_CB(skb)->same_flow = 1;
3398 	return 0;
3399 }
3400 
3401 void __init skb_init(void)
3402 {
3403 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
3404 					      sizeof(struct sk_buff),
3405 					      0,
3406 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3407 					      NULL);
3408 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3409 						sizeof(struct sk_buff_fclones),
3410 						0,
3411 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
3412 						NULL);
3413 }
3414 
3415 /**
3416  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3417  *	@skb: Socket buffer containing the buffers to be mapped
3418  *	@sg: The scatter-gather list to map into
3419  *	@offset: The offset into the buffer's contents to start mapping
3420  *	@len: Length of buffer space to be mapped
3421  *
3422  *	Fill the specified scatter-gather list with mappings/pointers into a
3423  *	region of the buffer space attached to a socket buffer.
3424  */
3425 static int
3426 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3427 {
3428 	int start = skb_headlen(skb);
3429 	int i, copy = start - offset;
3430 	struct sk_buff *frag_iter;
3431 	int elt = 0;
3432 
3433 	if (copy > 0) {
3434 		if (copy > len)
3435 			copy = len;
3436 		sg_set_buf(sg, skb->data + offset, copy);
3437 		elt++;
3438 		if ((len -= copy) == 0)
3439 			return elt;
3440 		offset += copy;
3441 	}
3442 
3443 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3444 		int end;
3445 
3446 		WARN_ON(start > offset + len);
3447 
3448 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3449 		if ((copy = end - offset) > 0) {
3450 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3451 
3452 			if (copy > len)
3453 				copy = len;
3454 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3455 					frag->page_offset+offset-start);
3456 			elt++;
3457 			if (!(len -= copy))
3458 				return elt;
3459 			offset += copy;
3460 		}
3461 		start = end;
3462 	}
3463 
3464 	skb_walk_frags(skb, frag_iter) {
3465 		int end;
3466 
3467 		WARN_ON(start > offset + len);
3468 
3469 		end = start + frag_iter->len;
3470 		if ((copy = end - offset) > 0) {
3471 			if (copy > len)
3472 				copy = len;
3473 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
3474 					      copy);
3475 			if ((len -= copy) == 0)
3476 				return elt;
3477 			offset += copy;
3478 		}
3479 		start = end;
3480 	}
3481 	BUG_ON(len);
3482 	return elt;
3483 }
3484 
3485 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
3486  * sglist without mark the sg which contain last skb data as the end.
3487  * So the caller can mannipulate sg list as will when padding new data after
3488  * the first call without calling sg_unmark_end to expend sg list.
3489  *
3490  * Scenario to use skb_to_sgvec_nomark:
3491  * 1. sg_init_table
3492  * 2. skb_to_sgvec_nomark(payload1)
3493  * 3. skb_to_sgvec_nomark(payload2)
3494  *
3495  * This is equivalent to:
3496  * 1. sg_init_table
3497  * 2. skb_to_sgvec(payload1)
3498  * 3. sg_unmark_end
3499  * 4. skb_to_sgvec(payload2)
3500  *
3501  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
3502  * is more preferable.
3503  */
3504 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
3505 			int offset, int len)
3506 {
3507 	return __skb_to_sgvec(skb, sg, offset, len);
3508 }
3509 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
3510 
3511 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3512 {
3513 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
3514 
3515 	sg_mark_end(&sg[nsg - 1]);
3516 
3517 	return nsg;
3518 }
3519 EXPORT_SYMBOL_GPL(skb_to_sgvec);
3520 
3521 /**
3522  *	skb_cow_data - Check that a socket buffer's data buffers are writable
3523  *	@skb: The socket buffer to check.
3524  *	@tailbits: Amount of trailing space to be added
3525  *	@trailer: Returned pointer to the skb where the @tailbits space begins
3526  *
3527  *	Make sure that the data buffers attached to a socket buffer are
3528  *	writable. If they are not, private copies are made of the data buffers
3529  *	and the socket buffer is set to use these instead.
3530  *
3531  *	If @tailbits is given, make sure that there is space to write @tailbits
3532  *	bytes of data beyond current end of socket buffer.  @trailer will be
3533  *	set to point to the skb in which this space begins.
3534  *
3535  *	The number of scatterlist elements required to completely map the
3536  *	COW'd and extended socket buffer will be returned.
3537  */
3538 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3539 {
3540 	int copyflag;
3541 	int elt;
3542 	struct sk_buff *skb1, **skb_p;
3543 
3544 	/* If skb is cloned or its head is paged, reallocate
3545 	 * head pulling out all the pages (pages are considered not writable
3546 	 * at the moment even if they are anonymous).
3547 	 */
3548 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3549 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3550 		return -ENOMEM;
3551 
3552 	/* Easy case. Most of packets will go this way. */
3553 	if (!skb_has_frag_list(skb)) {
3554 		/* A little of trouble, not enough of space for trailer.
3555 		 * This should not happen, when stack is tuned to generate
3556 		 * good frames. OK, on miss we reallocate and reserve even more
3557 		 * space, 128 bytes is fair. */
3558 
3559 		if (skb_tailroom(skb) < tailbits &&
3560 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3561 			return -ENOMEM;
3562 
3563 		/* Voila! */
3564 		*trailer = skb;
3565 		return 1;
3566 	}
3567 
3568 	/* Misery. We are in troubles, going to mincer fragments... */
3569 
3570 	elt = 1;
3571 	skb_p = &skb_shinfo(skb)->frag_list;
3572 	copyflag = 0;
3573 
3574 	while ((skb1 = *skb_p) != NULL) {
3575 		int ntail = 0;
3576 
3577 		/* The fragment is partially pulled by someone,
3578 		 * this can happen on input. Copy it and everything
3579 		 * after it. */
3580 
3581 		if (skb_shared(skb1))
3582 			copyflag = 1;
3583 
3584 		/* If the skb is the last, worry about trailer. */
3585 
3586 		if (skb1->next == NULL && tailbits) {
3587 			if (skb_shinfo(skb1)->nr_frags ||
3588 			    skb_has_frag_list(skb1) ||
3589 			    skb_tailroom(skb1) < tailbits)
3590 				ntail = tailbits + 128;
3591 		}
3592 
3593 		if (copyflag ||
3594 		    skb_cloned(skb1) ||
3595 		    ntail ||
3596 		    skb_shinfo(skb1)->nr_frags ||
3597 		    skb_has_frag_list(skb1)) {
3598 			struct sk_buff *skb2;
3599 
3600 			/* Fuck, we are miserable poor guys... */
3601 			if (ntail == 0)
3602 				skb2 = skb_copy(skb1, GFP_ATOMIC);
3603 			else
3604 				skb2 = skb_copy_expand(skb1,
3605 						       skb_headroom(skb1),
3606 						       ntail,
3607 						       GFP_ATOMIC);
3608 			if (unlikely(skb2 == NULL))
3609 				return -ENOMEM;
3610 
3611 			if (skb1->sk)
3612 				skb_set_owner_w(skb2, skb1->sk);
3613 
3614 			/* Looking around. Are we still alive?
3615 			 * OK, link new skb, drop old one */
3616 
3617 			skb2->next = skb1->next;
3618 			*skb_p = skb2;
3619 			kfree_skb(skb1);
3620 			skb1 = skb2;
3621 		}
3622 		elt++;
3623 		*trailer = skb1;
3624 		skb_p = &skb1->next;
3625 	}
3626 
3627 	return elt;
3628 }
3629 EXPORT_SYMBOL_GPL(skb_cow_data);
3630 
3631 static void sock_rmem_free(struct sk_buff *skb)
3632 {
3633 	struct sock *sk = skb->sk;
3634 
3635 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3636 }
3637 
3638 /*
3639  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3640  */
3641 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3642 {
3643 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3644 	    (unsigned int)sk->sk_rcvbuf)
3645 		return -ENOMEM;
3646 
3647 	skb_orphan(skb);
3648 	skb->sk = sk;
3649 	skb->destructor = sock_rmem_free;
3650 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3651 
3652 	/* before exiting rcu section, make sure dst is refcounted */
3653 	skb_dst_force(skb);
3654 
3655 	skb_queue_tail(&sk->sk_error_queue, skb);
3656 	if (!sock_flag(sk, SOCK_DEAD))
3657 		sk->sk_data_ready(sk);
3658 	return 0;
3659 }
3660 EXPORT_SYMBOL(sock_queue_err_skb);
3661 
3662 struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3663 {
3664 	struct sk_buff_head *q = &sk->sk_error_queue;
3665 	struct sk_buff *skb, *skb_next;
3666 	unsigned long flags;
3667 	int err = 0;
3668 
3669 	spin_lock_irqsave(&q->lock, flags);
3670 	skb = __skb_dequeue(q);
3671 	if (skb && (skb_next = skb_peek(q)))
3672 		err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3673 	spin_unlock_irqrestore(&q->lock, flags);
3674 
3675 	sk->sk_err = err;
3676 	if (err)
3677 		sk->sk_error_report(sk);
3678 
3679 	return skb;
3680 }
3681 EXPORT_SYMBOL(sock_dequeue_err_skb);
3682 
3683 /**
3684  * skb_clone_sk - create clone of skb, and take reference to socket
3685  * @skb: the skb to clone
3686  *
3687  * This function creates a clone of a buffer that holds a reference on
3688  * sk_refcnt.  Buffers created via this function are meant to be
3689  * returned using sock_queue_err_skb, or free via kfree_skb.
3690  *
3691  * When passing buffers allocated with this function to sock_queue_err_skb
3692  * it is necessary to wrap the call with sock_hold/sock_put in order to
3693  * prevent the socket from being released prior to being enqueued on
3694  * the sk_error_queue.
3695  */
3696 struct sk_buff *skb_clone_sk(struct sk_buff *skb)
3697 {
3698 	struct sock *sk = skb->sk;
3699 	struct sk_buff *clone;
3700 
3701 	if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
3702 		return NULL;
3703 
3704 	clone = skb_clone(skb, GFP_ATOMIC);
3705 	if (!clone) {
3706 		sock_put(sk);
3707 		return NULL;
3708 	}
3709 
3710 	clone->sk = sk;
3711 	clone->destructor = sock_efree;
3712 
3713 	return clone;
3714 }
3715 EXPORT_SYMBOL(skb_clone_sk);
3716 
3717 static void __skb_complete_tx_timestamp(struct sk_buff *skb,
3718 					struct sock *sk,
3719 					int tstype)
3720 {
3721 	struct sock_exterr_skb *serr;
3722 	int err;
3723 
3724 	serr = SKB_EXT_ERR(skb);
3725 	memset(serr, 0, sizeof(*serr));
3726 	serr->ee.ee_errno = ENOMSG;
3727 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3728 	serr->ee.ee_info = tstype;
3729 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
3730 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
3731 		if (sk->sk_protocol == IPPROTO_TCP &&
3732 		    sk->sk_type == SOCK_STREAM)
3733 			serr->ee.ee_data -= sk->sk_tskey;
3734 	}
3735 
3736 	err = sock_queue_err_skb(sk, skb);
3737 
3738 	if (err)
3739 		kfree_skb(skb);
3740 }
3741 
3742 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3743 {
3744 	bool ret;
3745 
3746 	if (likely(sysctl_tstamp_allow_data || tsonly))
3747 		return true;
3748 
3749 	read_lock_bh(&sk->sk_callback_lock);
3750 	ret = sk->sk_socket && sk->sk_socket->file &&
3751 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3752 	read_unlock_bh(&sk->sk_callback_lock);
3753 	return ret;
3754 }
3755 
3756 void skb_complete_tx_timestamp(struct sk_buff *skb,
3757 			       struct skb_shared_hwtstamps *hwtstamps)
3758 {
3759 	struct sock *sk = skb->sk;
3760 
3761 	if (!skb_may_tx_timestamp(sk, false))
3762 		return;
3763 
3764 	/* take a reference to prevent skb_orphan() from freeing the socket */
3765 	sock_hold(sk);
3766 
3767 	*skb_hwtstamps(skb) = *hwtstamps;
3768 	__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
3769 
3770 	sock_put(sk);
3771 }
3772 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
3773 
3774 void __skb_tstamp_tx(struct sk_buff *orig_skb,
3775 		     struct skb_shared_hwtstamps *hwtstamps,
3776 		     struct sock *sk, int tstype)
3777 {
3778 	struct sk_buff *skb;
3779 	bool tsonly;
3780 
3781 	if (!sk)
3782 		return;
3783 
3784 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
3785 	if (!skb_may_tx_timestamp(sk, tsonly))
3786 		return;
3787 
3788 	if (tsonly)
3789 		skb = alloc_skb(0, GFP_ATOMIC);
3790 	else
3791 		skb = skb_clone(orig_skb, GFP_ATOMIC);
3792 	if (!skb)
3793 		return;
3794 
3795 	if (tsonly) {
3796 		skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
3797 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
3798 	}
3799 
3800 	if (hwtstamps)
3801 		*skb_hwtstamps(skb) = *hwtstamps;
3802 	else
3803 		skb->tstamp = ktime_get_real();
3804 
3805 	__skb_complete_tx_timestamp(skb, sk, tstype);
3806 }
3807 EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3808 
3809 void skb_tstamp_tx(struct sk_buff *orig_skb,
3810 		   struct skb_shared_hwtstamps *hwtstamps)
3811 {
3812 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3813 			       SCM_TSTAMP_SND);
3814 }
3815 EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3816 
3817 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3818 {
3819 	struct sock *sk = skb->sk;
3820 	struct sock_exterr_skb *serr;
3821 	int err;
3822 
3823 	skb->wifi_acked_valid = 1;
3824 	skb->wifi_acked = acked;
3825 
3826 	serr = SKB_EXT_ERR(skb);
3827 	memset(serr, 0, sizeof(*serr));
3828 	serr->ee.ee_errno = ENOMSG;
3829 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
3830 
3831 	/* take a reference to prevent skb_orphan() from freeing the socket */
3832 	sock_hold(sk);
3833 
3834 	err = sock_queue_err_skb(sk, skb);
3835 	if (err)
3836 		kfree_skb(skb);
3837 
3838 	sock_put(sk);
3839 }
3840 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3841 
3842 /**
3843  * skb_partial_csum_set - set up and verify partial csum values for packet
3844  * @skb: the skb to set
3845  * @start: the number of bytes after skb->data to start checksumming.
3846  * @off: the offset from start to place the checksum.
3847  *
3848  * For untrusted partially-checksummed packets, we need to make sure the values
3849  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3850  *
3851  * This function checks and sets those values and skb->ip_summed: if this
3852  * returns false you should drop the packet.
3853  */
3854 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3855 {
3856 	if (unlikely(start > skb_headlen(skb)) ||
3857 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3858 		net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
3859 				     start, off, skb_headlen(skb));
3860 		return false;
3861 	}
3862 	skb->ip_summed = CHECKSUM_PARTIAL;
3863 	skb->csum_start = skb_headroom(skb) + start;
3864 	skb->csum_offset = off;
3865 	skb_set_transport_header(skb, start);
3866 	return true;
3867 }
3868 EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3869 
3870 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3871 			       unsigned int max)
3872 {
3873 	if (skb_headlen(skb) >= len)
3874 		return 0;
3875 
3876 	/* If we need to pullup then pullup to the max, so we
3877 	 * won't need to do it again.
3878 	 */
3879 	if (max > skb->len)
3880 		max = skb->len;
3881 
3882 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3883 		return -ENOMEM;
3884 
3885 	if (skb_headlen(skb) < len)
3886 		return -EPROTO;
3887 
3888 	return 0;
3889 }
3890 
3891 #define MAX_TCP_HDR_LEN (15 * 4)
3892 
3893 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3894 				      typeof(IPPROTO_IP) proto,
3895 				      unsigned int off)
3896 {
3897 	switch (proto) {
3898 		int err;
3899 
3900 	case IPPROTO_TCP:
3901 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3902 					  off + MAX_TCP_HDR_LEN);
3903 		if (!err && !skb_partial_csum_set(skb, off,
3904 						  offsetof(struct tcphdr,
3905 							   check)))
3906 			err = -EPROTO;
3907 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3908 
3909 	case IPPROTO_UDP:
3910 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3911 					  off + sizeof(struct udphdr));
3912 		if (!err && !skb_partial_csum_set(skb, off,
3913 						  offsetof(struct udphdr,
3914 							   check)))
3915 			err = -EPROTO;
3916 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3917 	}
3918 
3919 	return ERR_PTR(-EPROTO);
3920 }
3921 
3922 /* This value should be large enough to cover a tagged ethernet header plus
3923  * maximally sized IP and TCP or UDP headers.
3924  */
3925 #define MAX_IP_HDR_LEN 128
3926 
3927 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3928 {
3929 	unsigned int off;
3930 	bool fragment;
3931 	__sum16 *csum;
3932 	int err;
3933 
3934 	fragment = false;
3935 
3936 	err = skb_maybe_pull_tail(skb,
3937 				  sizeof(struct iphdr),
3938 				  MAX_IP_HDR_LEN);
3939 	if (err < 0)
3940 		goto out;
3941 
3942 	if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3943 		fragment = true;
3944 
3945 	off = ip_hdrlen(skb);
3946 
3947 	err = -EPROTO;
3948 
3949 	if (fragment)
3950 		goto out;
3951 
3952 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3953 	if (IS_ERR(csum))
3954 		return PTR_ERR(csum);
3955 
3956 	if (recalculate)
3957 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3958 					   ip_hdr(skb)->daddr,
3959 					   skb->len - off,
3960 					   ip_hdr(skb)->protocol, 0);
3961 	err = 0;
3962 
3963 out:
3964 	return err;
3965 }
3966 
3967 /* This value should be large enough to cover a tagged ethernet header plus
3968  * an IPv6 header, all options, and a maximal TCP or UDP header.
3969  */
3970 #define MAX_IPV6_HDR_LEN 256
3971 
3972 #define OPT_HDR(type, skb, off) \
3973 	(type *)(skb_network_header(skb) + (off))
3974 
3975 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3976 {
3977 	int err;
3978 	u8 nexthdr;
3979 	unsigned int off;
3980 	unsigned int len;
3981 	bool fragment;
3982 	bool done;
3983 	__sum16 *csum;
3984 
3985 	fragment = false;
3986 	done = false;
3987 
3988 	off = sizeof(struct ipv6hdr);
3989 
3990 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3991 	if (err < 0)
3992 		goto out;
3993 
3994 	nexthdr = ipv6_hdr(skb)->nexthdr;
3995 
3996 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3997 	while (off <= len && !done) {
3998 		switch (nexthdr) {
3999 		case IPPROTO_DSTOPTS:
4000 		case IPPROTO_HOPOPTS:
4001 		case IPPROTO_ROUTING: {
4002 			struct ipv6_opt_hdr *hp;
4003 
4004 			err = skb_maybe_pull_tail(skb,
4005 						  off +
4006 						  sizeof(struct ipv6_opt_hdr),
4007 						  MAX_IPV6_HDR_LEN);
4008 			if (err < 0)
4009 				goto out;
4010 
4011 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4012 			nexthdr = hp->nexthdr;
4013 			off += ipv6_optlen(hp);
4014 			break;
4015 		}
4016 		case IPPROTO_AH: {
4017 			struct ip_auth_hdr *hp;
4018 
4019 			err = skb_maybe_pull_tail(skb,
4020 						  off +
4021 						  sizeof(struct ip_auth_hdr),
4022 						  MAX_IPV6_HDR_LEN);
4023 			if (err < 0)
4024 				goto out;
4025 
4026 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4027 			nexthdr = hp->nexthdr;
4028 			off += ipv6_authlen(hp);
4029 			break;
4030 		}
4031 		case IPPROTO_FRAGMENT: {
4032 			struct frag_hdr *hp;
4033 
4034 			err = skb_maybe_pull_tail(skb,
4035 						  off +
4036 						  sizeof(struct frag_hdr),
4037 						  MAX_IPV6_HDR_LEN);
4038 			if (err < 0)
4039 				goto out;
4040 
4041 			hp = OPT_HDR(struct frag_hdr, skb, off);
4042 
4043 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4044 				fragment = true;
4045 
4046 			nexthdr = hp->nexthdr;
4047 			off += sizeof(struct frag_hdr);
4048 			break;
4049 		}
4050 		default:
4051 			done = true;
4052 			break;
4053 		}
4054 	}
4055 
4056 	err = -EPROTO;
4057 
4058 	if (!done || fragment)
4059 		goto out;
4060 
4061 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
4062 	if (IS_ERR(csum))
4063 		return PTR_ERR(csum);
4064 
4065 	if (recalculate)
4066 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4067 					 &ipv6_hdr(skb)->daddr,
4068 					 skb->len - off, nexthdr, 0);
4069 	err = 0;
4070 
4071 out:
4072 	return err;
4073 }
4074 
4075 /**
4076  * skb_checksum_setup - set up partial checksum offset
4077  * @skb: the skb to set up
4078  * @recalculate: if true the pseudo-header checksum will be recalculated
4079  */
4080 int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4081 {
4082 	int err;
4083 
4084 	switch (skb->protocol) {
4085 	case htons(ETH_P_IP):
4086 		err = skb_checksum_setup_ipv4(skb, recalculate);
4087 		break;
4088 
4089 	case htons(ETH_P_IPV6):
4090 		err = skb_checksum_setup_ipv6(skb, recalculate);
4091 		break;
4092 
4093 	default:
4094 		err = -EPROTO;
4095 		break;
4096 	}
4097 
4098 	return err;
4099 }
4100 EXPORT_SYMBOL(skb_checksum_setup);
4101 
4102 /**
4103  * skb_checksum_maybe_trim - maybe trims the given skb
4104  * @skb: the skb to check
4105  * @transport_len: the data length beyond the network header
4106  *
4107  * Checks whether the given skb has data beyond the given transport length.
4108  * If so, returns a cloned skb trimmed to this transport length.
4109  * Otherwise returns the provided skb. Returns NULL in error cases
4110  * (e.g. transport_len exceeds skb length or out-of-memory).
4111  *
4112  * Caller needs to set the skb transport header and free any returned skb if it
4113  * differs from the provided skb.
4114  */
4115 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
4116 					       unsigned int transport_len)
4117 {
4118 	struct sk_buff *skb_chk;
4119 	unsigned int len = skb_transport_offset(skb) + transport_len;
4120 	int ret;
4121 
4122 	if (skb->len < len)
4123 		return NULL;
4124 	else if (skb->len == len)
4125 		return skb;
4126 
4127 	skb_chk = skb_clone(skb, GFP_ATOMIC);
4128 	if (!skb_chk)
4129 		return NULL;
4130 
4131 	ret = pskb_trim_rcsum(skb_chk, len);
4132 	if (ret) {
4133 		kfree_skb(skb_chk);
4134 		return NULL;
4135 	}
4136 
4137 	return skb_chk;
4138 }
4139 
4140 /**
4141  * skb_checksum_trimmed - validate checksum of an skb
4142  * @skb: the skb to check
4143  * @transport_len: the data length beyond the network header
4144  * @skb_chkf: checksum function to use
4145  *
4146  * Applies the given checksum function skb_chkf to the provided skb.
4147  * Returns a checked and maybe trimmed skb. Returns NULL on error.
4148  *
4149  * If the skb has data beyond the given transport length, then a
4150  * trimmed & cloned skb is checked and returned.
4151  *
4152  * Caller needs to set the skb transport header and free any returned skb if it
4153  * differs from the provided skb.
4154  */
4155 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
4156 				     unsigned int transport_len,
4157 				     __sum16(*skb_chkf)(struct sk_buff *skb))
4158 {
4159 	struct sk_buff *skb_chk;
4160 	unsigned int offset = skb_transport_offset(skb);
4161 	__sum16 ret;
4162 
4163 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
4164 	if (!skb_chk)
4165 		goto err;
4166 
4167 	if (!pskb_may_pull(skb_chk, offset))
4168 		goto err;
4169 
4170 	__skb_pull(skb_chk, offset);
4171 	ret = skb_chkf(skb_chk);
4172 	__skb_push(skb_chk, offset);
4173 
4174 	if (ret)
4175 		goto err;
4176 
4177 	return skb_chk;
4178 
4179 err:
4180 	if (skb_chk && skb_chk != skb)
4181 		kfree_skb(skb_chk);
4182 
4183 	return NULL;
4184 
4185 }
4186 EXPORT_SYMBOL(skb_checksum_trimmed);
4187 
4188 void __skb_warn_lro_forwarding(const struct sk_buff *skb)
4189 {
4190 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4191 			     skb->dev->name);
4192 }
4193 EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4194 
4195 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4196 {
4197 	if (head_stolen) {
4198 		skb_release_head_state(skb);
4199 		kmem_cache_free(skbuff_head_cache, skb);
4200 	} else {
4201 		__kfree_skb(skb);
4202 	}
4203 }
4204 EXPORT_SYMBOL(kfree_skb_partial);
4205 
4206 /**
4207  * skb_try_coalesce - try to merge skb to prior one
4208  * @to: prior buffer
4209  * @from: buffer to add
4210  * @fragstolen: pointer to boolean
4211  * @delta_truesize: how much more was allocated than was requested
4212  */
4213 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4214 		      bool *fragstolen, int *delta_truesize)
4215 {
4216 	int i, delta, len = from->len;
4217 
4218 	*fragstolen = false;
4219 
4220 	if (skb_cloned(to))
4221 		return false;
4222 
4223 	if (len <= skb_tailroom(to)) {
4224 		if (len)
4225 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4226 		*delta_truesize = 0;
4227 		return true;
4228 	}
4229 
4230 	if (skb_has_frag_list(to) || skb_has_frag_list(from))
4231 		return false;
4232 
4233 	if (skb_headlen(from) != 0) {
4234 		struct page *page;
4235 		unsigned int offset;
4236 
4237 		if (skb_shinfo(to)->nr_frags +
4238 		    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4239 			return false;
4240 
4241 		if (skb_head_is_locked(from))
4242 			return false;
4243 
4244 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4245 
4246 		page = virt_to_head_page(from->head);
4247 		offset = from->data - (unsigned char *)page_address(page);
4248 
4249 		skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4250 				   page, offset, skb_headlen(from));
4251 		*fragstolen = true;
4252 	} else {
4253 		if (skb_shinfo(to)->nr_frags +
4254 		    skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4255 			return false;
4256 
4257 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4258 	}
4259 
4260 	WARN_ON_ONCE(delta < len);
4261 
4262 	memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4263 	       skb_shinfo(from)->frags,
4264 	       skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4265 	skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4266 
4267 	if (!skb_cloned(from))
4268 		skb_shinfo(from)->nr_frags = 0;
4269 
4270 	/* if the skb is not cloned this does nothing
4271 	 * since we set nr_frags to 0.
4272 	 */
4273 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4274 		skb_frag_ref(from, i);
4275 
4276 	to->truesize += delta;
4277 	to->len += len;
4278 	to->data_len += len;
4279 
4280 	*delta_truesize = delta;
4281 	return true;
4282 }
4283 EXPORT_SYMBOL(skb_try_coalesce);
4284 
4285 /**
4286  * skb_scrub_packet - scrub an skb
4287  *
4288  * @skb: buffer to clean
4289  * @xnet: packet is crossing netns
4290  *
4291  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
4292  * into/from a tunnel. Some information have to be cleared during these
4293  * operations.
4294  * skb_scrub_packet can also be used to clean a skb before injecting it in
4295  * another namespace (@xnet == true). We have to clear all information in the
4296  * skb that could impact namespace isolation.
4297  */
4298 void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4299 {
4300 	skb->tstamp.tv64 = 0;
4301 	skb->pkt_type = PACKET_HOST;
4302 	skb->skb_iif = 0;
4303 	skb->ignore_df = 0;
4304 	skb_dst_drop(skb);
4305 	skb_sender_cpu_clear(skb);
4306 	secpath_reset(skb);
4307 	nf_reset(skb);
4308 	nf_reset_trace(skb);
4309 
4310 	if (!xnet)
4311 		return;
4312 
4313 	skb_orphan(skb);
4314 	skb->mark = 0;
4315 }
4316 EXPORT_SYMBOL_GPL(skb_scrub_packet);
4317 
4318 /**
4319  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4320  *
4321  * @skb: GSO skb
4322  *
4323  * skb_gso_transport_seglen is used to determine the real size of the
4324  * individual segments, including Layer4 headers (TCP/UDP).
4325  *
4326  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4327  */
4328 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4329 {
4330 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
4331 	unsigned int thlen = 0;
4332 
4333 	if (skb->encapsulation) {
4334 		thlen = skb_inner_transport_header(skb) -
4335 			skb_transport_header(skb);
4336 
4337 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4338 			thlen += inner_tcp_hdrlen(skb);
4339 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4340 		thlen = tcp_hdrlen(skb);
4341 	}
4342 	/* UFO sets gso_size to the size of the fragmentation
4343 	 * payload, i.e. the size of the L4 (UDP) header is already
4344 	 * accounted for.
4345 	 */
4346 	return thlen + shinfo->gso_size;
4347 }
4348 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
4349 
4350 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
4351 {
4352 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
4353 		kfree_skb(skb);
4354 		return NULL;
4355 	}
4356 
4357 	memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
4358 		2 * ETH_ALEN);
4359 	skb->mac_header += VLAN_HLEN;
4360 	return skb;
4361 }
4362 
4363 struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
4364 {
4365 	struct vlan_hdr *vhdr;
4366 	u16 vlan_tci;
4367 
4368 	if (unlikely(skb_vlan_tag_present(skb))) {
4369 		/* vlan_tci is already set-up so leave this for another time */
4370 		return skb;
4371 	}
4372 
4373 	skb = skb_share_check(skb, GFP_ATOMIC);
4374 	if (unlikely(!skb))
4375 		goto err_free;
4376 
4377 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
4378 		goto err_free;
4379 
4380 	vhdr = (struct vlan_hdr *)skb->data;
4381 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
4382 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
4383 
4384 	skb_pull_rcsum(skb, VLAN_HLEN);
4385 	vlan_set_encap_proto(skb, vhdr);
4386 
4387 	skb = skb_reorder_vlan_header(skb);
4388 	if (unlikely(!skb))
4389 		goto err_free;
4390 
4391 	skb_reset_network_header(skb);
4392 	skb_reset_transport_header(skb);
4393 	skb_reset_mac_len(skb);
4394 
4395 	return skb;
4396 
4397 err_free:
4398 	kfree_skb(skb);
4399 	return NULL;
4400 }
4401 EXPORT_SYMBOL(skb_vlan_untag);
4402 
4403 int skb_ensure_writable(struct sk_buff *skb, int write_len)
4404 {
4405 	if (!pskb_may_pull(skb, write_len))
4406 		return -ENOMEM;
4407 
4408 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4409 		return 0;
4410 
4411 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4412 }
4413 EXPORT_SYMBOL(skb_ensure_writable);
4414 
4415 /* remove VLAN header from packet and update csum accordingly. */
4416 static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
4417 {
4418 	struct vlan_hdr *vhdr;
4419 	unsigned int offset = skb->data - skb_mac_header(skb);
4420 	int err;
4421 
4422 	__skb_push(skb, offset);
4423 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
4424 	if (unlikely(err))
4425 		goto pull;
4426 
4427 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4428 
4429 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
4430 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
4431 
4432 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
4433 	__skb_pull(skb, VLAN_HLEN);
4434 
4435 	vlan_set_encap_proto(skb, vhdr);
4436 	skb->mac_header += VLAN_HLEN;
4437 
4438 	if (skb_network_offset(skb) < ETH_HLEN)
4439 		skb_set_network_header(skb, ETH_HLEN);
4440 
4441 	skb_reset_mac_len(skb);
4442 pull:
4443 	__skb_pull(skb, offset);
4444 
4445 	return err;
4446 }
4447 
4448 int skb_vlan_pop(struct sk_buff *skb)
4449 {
4450 	u16 vlan_tci;
4451 	__be16 vlan_proto;
4452 	int err;
4453 
4454 	if (likely(skb_vlan_tag_present(skb))) {
4455 		skb->vlan_tci = 0;
4456 	} else {
4457 		if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
4458 			      skb->protocol != htons(ETH_P_8021AD)) ||
4459 			     skb->len < VLAN_ETH_HLEN))
4460 			return 0;
4461 
4462 		err = __skb_vlan_pop(skb, &vlan_tci);
4463 		if (err)
4464 			return err;
4465 	}
4466 	/* move next vlan tag to hw accel tag */
4467 	if (likely((skb->protocol != htons(ETH_P_8021Q) &&
4468 		    skb->protocol != htons(ETH_P_8021AD)) ||
4469 		   skb->len < VLAN_ETH_HLEN))
4470 		return 0;
4471 
4472 	vlan_proto = skb->protocol;
4473 	err = __skb_vlan_pop(skb, &vlan_tci);
4474 	if (unlikely(err))
4475 		return err;
4476 
4477 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4478 	return 0;
4479 }
4480 EXPORT_SYMBOL(skb_vlan_pop);
4481 
4482 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
4483 {
4484 	if (skb_vlan_tag_present(skb)) {
4485 		unsigned int offset = skb->data - skb_mac_header(skb);
4486 		int err;
4487 
4488 		/* __vlan_insert_tag expect skb->data pointing to mac header.
4489 		 * So change skb->data before calling it and change back to
4490 		 * original position later
4491 		 */
4492 		__skb_push(skb, offset);
4493 		err = __vlan_insert_tag(skb, skb->vlan_proto,
4494 					skb_vlan_tag_get(skb));
4495 		if (err)
4496 			return err;
4497 		skb->protocol = skb->vlan_proto;
4498 		skb->mac_len += VLAN_HLEN;
4499 		__skb_pull(skb, offset);
4500 
4501 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
4502 	}
4503 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
4504 	return 0;
4505 }
4506 EXPORT_SYMBOL(skb_vlan_push);
4507 
4508 /**
4509  * alloc_skb_with_frags - allocate skb with page frags
4510  *
4511  * @header_len: size of linear part
4512  * @data_len: needed length in frags
4513  * @max_page_order: max page order desired.
4514  * @errcode: pointer to error code if any
4515  * @gfp_mask: allocation mask
4516  *
4517  * This can be used to allocate a paged skb, given a maximal order for frags.
4518  */
4519 struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
4520 				     unsigned long data_len,
4521 				     int max_page_order,
4522 				     int *errcode,
4523 				     gfp_t gfp_mask)
4524 {
4525 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
4526 	unsigned long chunk;
4527 	struct sk_buff *skb;
4528 	struct page *page;
4529 	gfp_t gfp_head;
4530 	int i;
4531 
4532 	*errcode = -EMSGSIZE;
4533 	/* Note this test could be relaxed, if we succeed to allocate
4534 	 * high order pages...
4535 	 */
4536 	if (npages > MAX_SKB_FRAGS)
4537 		return NULL;
4538 
4539 	gfp_head = gfp_mask;
4540 	if (gfp_head & __GFP_DIRECT_RECLAIM)
4541 		gfp_head |= __GFP_REPEAT;
4542 
4543 	*errcode = -ENOBUFS;
4544 	skb = alloc_skb(header_len, gfp_head);
4545 	if (!skb)
4546 		return NULL;
4547 
4548 	skb->truesize += npages << PAGE_SHIFT;
4549 
4550 	for (i = 0; npages > 0; i++) {
4551 		int order = max_page_order;
4552 
4553 		while (order) {
4554 			if (npages >= 1 << order) {
4555 				page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
4556 						   __GFP_COMP |
4557 						   __GFP_NOWARN |
4558 						   __GFP_NORETRY,
4559 						   order);
4560 				if (page)
4561 					goto fill_page;
4562 				/* Do not retry other high order allocations */
4563 				order = 1;
4564 				max_page_order = 0;
4565 			}
4566 			order--;
4567 		}
4568 		page = alloc_page(gfp_mask);
4569 		if (!page)
4570 			goto failure;
4571 fill_page:
4572 		chunk = min_t(unsigned long, data_len,
4573 			      PAGE_SIZE << order);
4574 		skb_fill_page_desc(skb, i, page, 0, chunk);
4575 		data_len -= chunk;
4576 		npages -= 1 << order;
4577 	}
4578 	return skb;
4579 
4580 failure:
4581 	kfree_skb(skb);
4582 	return NULL;
4583 }
4584 EXPORT_SYMBOL(alloc_skb_with_frags);
4585