xref: /openbmc/linux/net/core/skbuff.c (revision de843723)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
31da177e4SLinus Torvalds  *
4113aa838SAlan Cox  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
51da177e4SLinus Torvalds  *			Florian La Roche <rzsfl@rz.uni-sb.de>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	Fixes:
81da177e4SLinus Torvalds  *		Alan Cox	:	Fixed the worst of the load
91da177e4SLinus Torvalds  *					balancer bugs.
101da177e4SLinus Torvalds  *		Dave Platt	:	Interrupt stacking fix.
111da177e4SLinus Torvalds  *	Richard Kooijman	:	Timestamp fixes.
121da177e4SLinus Torvalds  *		Alan Cox	:	Changed buffer format.
131da177e4SLinus Torvalds  *		Alan Cox	:	destructor hook for AF_UNIX etc.
141da177e4SLinus Torvalds  *		Linus Torvalds	:	Better skb_clone.
151da177e4SLinus Torvalds  *		Alan Cox	:	Added skb_copy.
161da177e4SLinus Torvalds  *		Alan Cox	:	Added all the changed routines Linus
171da177e4SLinus Torvalds  *					only put in the headers
181da177e4SLinus Torvalds  *		Ray VanTassle	:	Fixed --skb->lock in free
191da177e4SLinus Torvalds  *		Alan Cox	:	skb_copy copy arp field
201da177e4SLinus Torvalds  *		Andi Kleen	:	slabified it.
211da177e4SLinus Torvalds  *		Robert Olsson	:	Removed skb_head_pool
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *	NOTE:
241da177e4SLinus Torvalds  *		The __skb_ routines should be called with interrupts
251da177e4SLinus Torvalds  *	disabled, or you better be *real* sure that the operation is atomic
261da177e4SLinus Torvalds  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
271da177e4SLinus Torvalds  *	or via disabling bottom half handlers, etc).
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
301da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
311da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
321da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  *	The functions in this file will not compile correctly with gcc 2.4.x
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
39e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40e005d193SJoe Perches 
411da177e4SLinus Torvalds #include <linux/module.h>
421da177e4SLinus Torvalds #include <linux/types.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
44fe55f6d5SVegard Nossum #include <linux/kmemcheck.h>
451da177e4SLinus Torvalds #include <linux/mm.h>
461da177e4SLinus Torvalds #include <linux/interrupt.h>
471da177e4SLinus Torvalds #include <linux/in.h>
481da177e4SLinus Torvalds #include <linux/inet.h>
491da177e4SLinus Torvalds #include <linux/slab.h>
50de960aa9SFlorian Westphal #include <linux/tcp.h>
51de960aa9SFlorian Westphal #include <linux/udp.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
541da177e4SLinus Torvalds #include <net/pkt_sched.h>
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds #include <linux/string.h>
571da177e4SLinus Torvalds #include <linux/skbuff.h>
589c55e01cSJens Axboe #include <linux/splice.h>
591da177e4SLinus Torvalds #include <linux/cache.h>
601da177e4SLinus Torvalds #include <linux/rtnetlink.h>
611da177e4SLinus Torvalds #include <linux/init.h>
62716ea3a7SDavid Howells #include <linux/scatterlist.h>
63ac45f602SPatrick Ohly #include <linux/errqueue.h>
64268bb0ceSLinus Torvalds #include <linux/prefetch.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <net/protocol.h>
671da177e4SLinus Torvalds #include <net/dst.h>
681da177e4SLinus Torvalds #include <net/sock.h>
691da177e4SLinus Torvalds #include <net/checksum.h>
70ed1f50c3SPaul Durrant #include <net/ip6_checksum.h>
711da177e4SLinus Torvalds #include <net/xfrm.h>
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds #include <asm/uaccess.h>
74ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
7551c56b00SEric Dumazet #include <linux/highmem.h>
76a1f8e7f7SAl Viro 
77d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly;
78e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /**
81f05de73bSJean Sacren  *	skb_panic - private function for out-of-line support
821da177e4SLinus Torvalds  *	@skb:	buffer
831da177e4SLinus Torvalds  *	@sz:	size
84f05de73bSJean Sacren  *	@addr:	address
8599d5851eSJames Hogan  *	@msg:	skb_over_panic or skb_under_panic
861da177e4SLinus Torvalds  *
87f05de73bSJean Sacren  *	Out-of-line support for skb_put() and skb_push().
88f05de73bSJean Sacren  *	Called via the wrapper skb_over_panic() or skb_under_panic().
89f05de73bSJean Sacren  *	Keep out of line to prevent kernel bloat.
90f05de73bSJean Sacren  *	__builtin_return_address is not used because it is not always reliable.
911da177e4SLinus Torvalds  */
92f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
9399d5851eSJames Hogan 		      const char msg[])
941da177e4SLinus Torvalds {
95e005d193SJoe Perches 	pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
9699d5851eSJames Hogan 		 msg, addr, skb->len, sz, skb->head, skb->data,
974305b541SArnaldo Carvalho de Melo 		 (unsigned long)skb->tail, (unsigned long)skb->end,
9826095455SPatrick McHardy 		 skb->dev ? skb->dev->name : "<NULL>");
991da177e4SLinus Torvalds 	BUG();
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
102f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1031da177e4SLinus Torvalds {
104f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
107f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
108f05de73bSJean Sacren {
109f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
110f05de73bSJean Sacren }
111c93bdd0eSMel Gorman 
112c93bdd0eSMel Gorman /*
113c93bdd0eSMel Gorman  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
114c93bdd0eSMel Gorman  * the caller if emergency pfmemalloc reserves are being used. If it is and
115c93bdd0eSMel Gorman  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
116c93bdd0eSMel Gorman  * may be used. Otherwise, the packet data may be discarded until enough
117c93bdd0eSMel Gorman  * memory is free
118c93bdd0eSMel Gorman  */
119c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
120c93bdd0eSMel Gorman 	 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
12161c5e88aSstephen hemminger 
12261c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
12361c5e88aSstephen hemminger 			       unsigned long ip, bool *pfmemalloc)
124c93bdd0eSMel Gorman {
125c93bdd0eSMel Gorman 	void *obj;
126c93bdd0eSMel Gorman 	bool ret_pfmemalloc = false;
127c93bdd0eSMel Gorman 
128c93bdd0eSMel Gorman 	/*
129c93bdd0eSMel Gorman 	 * Try a regular allocation, when that fails and we're not entitled
130c93bdd0eSMel Gorman 	 * to the reserves, fail.
131c93bdd0eSMel Gorman 	 */
132c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size,
133c93bdd0eSMel Gorman 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
134c93bdd0eSMel Gorman 					node);
135c93bdd0eSMel Gorman 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
136c93bdd0eSMel Gorman 		goto out;
137c93bdd0eSMel Gorman 
138c93bdd0eSMel Gorman 	/* Try again but now we are using pfmemalloc reserves */
139c93bdd0eSMel Gorman 	ret_pfmemalloc = true;
140c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size, flags, node);
141c93bdd0eSMel Gorman 
142c93bdd0eSMel Gorman out:
143c93bdd0eSMel Gorman 	if (pfmemalloc)
144c93bdd0eSMel Gorman 		*pfmemalloc = ret_pfmemalloc;
145c93bdd0eSMel Gorman 
146c93bdd0eSMel Gorman 	return obj;
147c93bdd0eSMel Gorman }
148c93bdd0eSMel Gorman 
1491da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1501da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1511da177e4SLinus Torvalds  *	[BEEP] leaks.
1521da177e4SLinus Torvalds  *
1531da177e4SLinus Torvalds  */
1541da177e4SLinus Torvalds 
1550ebd0ac5SPatrick McHardy struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
1560ebd0ac5SPatrick McHardy {
1570ebd0ac5SPatrick McHardy 	struct sk_buff *skb;
1580ebd0ac5SPatrick McHardy 
1590ebd0ac5SPatrick McHardy 	/* Get the HEAD */
1600ebd0ac5SPatrick McHardy 	skb = kmem_cache_alloc_node(skbuff_head_cache,
1610ebd0ac5SPatrick McHardy 				    gfp_mask & ~__GFP_DMA, node);
1620ebd0ac5SPatrick McHardy 	if (!skb)
1630ebd0ac5SPatrick McHardy 		goto out;
1640ebd0ac5SPatrick McHardy 
1650ebd0ac5SPatrick McHardy 	/*
1660ebd0ac5SPatrick McHardy 	 * Only clear those fields we need to clear, not those that we will
1670ebd0ac5SPatrick McHardy 	 * actually initialise below. Hence, don't put any more fields after
1680ebd0ac5SPatrick McHardy 	 * the tail pointer in struct sk_buff!
1690ebd0ac5SPatrick McHardy 	 */
1700ebd0ac5SPatrick McHardy 	memset(skb, 0, offsetof(struct sk_buff, tail));
1715e71d9d7SPablo Neira 	skb->head = NULL;
1720ebd0ac5SPatrick McHardy 	skb->truesize = sizeof(struct sk_buff);
1730ebd0ac5SPatrick McHardy 	atomic_set(&skb->users, 1);
1740ebd0ac5SPatrick McHardy 
17535d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
1760ebd0ac5SPatrick McHardy out:
1770ebd0ac5SPatrick McHardy 	return skb;
1780ebd0ac5SPatrick McHardy }
1790ebd0ac5SPatrick McHardy 
1801da177e4SLinus Torvalds /**
181d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1821da177e4SLinus Torvalds  *	@size: size to allocate
1831da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
184c93bdd0eSMel Gorman  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
185c93bdd0eSMel Gorman  *		instead of head cache and allocate a cloned (child) skb.
186c93bdd0eSMel Gorman  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
187c93bdd0eSMel Gorman  *		allocations in case the data is required for writeback
188b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1891da177e4SLinus Torvalds  *
1901da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
19194b6042cSBen Hutchings  *	tail room of at least size bytes. The object has a reference count
19294b6042cSBen Hutchings  *	of one. The return is the buffer. On a failure the return is %NULL.
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1951da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1961da177e4SLinus Torvalds  */
197dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
198c93bdd0eSMel Gorman 			    int flags, int node)
1991da177e4SLinus Torvalds {
200e18b890bSChristoph Lameter 	struct kmem_cache *cache;
2014947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
2021da177e4SLinus Torvalds 	struct sk_buff *skb;
2031da177e4SLinus Torvalds 	u8 *data;
204c93bdd0eSMel Gorman 	bool pfmemalloc;
2051da177e4SLinus Torvalds 
206c93bdd0eSMel Gorman 	cache = (flags & SKB_ALLOC_FCLONE)
207c93bdd0eSMel Gorman 		? skbuff_fclone_cache : skbuff_head_cache;
208c93bdd0eSMel Gorman 
209c93bdd0eSMel Gorman 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
210c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
2118798b3fbSHerbert Xu 
2121da177e4SLinus Torvalds 	/* Get the HEAD */
213b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
2141da177e4SLinus Torvalds 	if (!skb)
2151da177e4SLinus Torvalds 		goto out;
216ec7d2f2cSEric Dumazet 	prefetchw(skb);
2171da177e4SLinus Torvalds 
21887fb4b7bSEric Dumazet 	/* We do our best to align skb_shared_info on a separate cache
21987fb4b7bSEric Dumazet 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
22087fb4b7bSEric Dumazet 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
22187fb4b7bSEric Dumazet 	 * Both skb->head and skb_shared_info are cache line aligned.
22287fb4b7bSEric Dumazet 	 */
223bc417e30STony Lindgren 	size = SKB_DATA_ALIGN(size);
22487fb4b7bSEric Dumazet 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
225c93bdd0eSMel Gorman 	data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
2261da177e4SLinus Torvalds 	if (!data)
2271da177e4SLinus Torvalds 		goto nodata;
22887fb4b7bSEric Dumazet 	/* kmalloc(size) might give us more room than requested.
22987fb4b7bSEric Dumazet 	 * Put skb_shared_info exactly at the end of allocated zone,
23087fb4b7bSEric Dumazet 	 * to allow max possible filling before reallocation.
23187fb4b7bSEric Dumazet 	 */
23287fb4b7bSEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
233ec7d2f2cSEric Dumazet 	prefetchw(data + size);
2341da177e4SLinus Torvalds 
235ca0605a7SArnaldo Carvalho de Melo 	/*
236c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
237c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
238c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
239ca0605a7SArnaldo Carvalho de Melo 	 */
240ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
24187fb4b7bSEric Dumazet 	/* Account for allocated memory : skb + skb->head */
24287fb4b7bSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
243c93bdd0eSMel Gorman 	skb->pfmemalloc = pfmemalloc;
2441da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2451da177e4SLinus Torvalds 	skb->head = data;
2461da177e4SLinus Torvalds 	skb->data = data;
24727a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2484305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
24935d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
25035d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
25119633e12SStephen Hemminger 
2524947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2534947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
254ec7d2f2cSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
2554947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
256c2aa3665SEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
2574947d3efSBenjamin LaHaise 
258c93bdd0eSMel Gorman 	if (flags & SKB_ALLOC_FCLONE) {
259d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
260d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
2611da177e4SLinus Torvalds 
262fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(child, flags1);
263fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(child, flags2);
264d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
265d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
266d179cd12SDavid S. Miller 
267d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
268c93bdd0eSMel Gorman 		child->pfmemalloc = pfmemalloc;
269d179cd12SDavid S. Miller 	}
2701da177e4SLinus Torvalds out:
2711da177e4SLinus Torvalds 	return skb;
2721da177e4SLinus Torvalds nodata:
2738798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2741da177e4SLinus Torvalds 	skb = NULL;
2751da177e4SLinus Torvalds 	goto out;
2761da177e4SLinus Torvalds }
277b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds /**
280b2b5ce9dSEric Dumazet  * build_skb - build a network buffer
281b2b5ce9dSEric Dumazet  * @data: data buffer provided by caller
282d3836f21SEric Dumazet  * @frag_size: size of fragment, or 0 if head was kmalloced
283b2b5ce9dSEric Dumazet  *
284b2b5ce9dSEric Dumazet  * Allocate a new &sk_buff. Caller provides space holding head and
285deceb4c0SFlorian Fainelli  * skb_shared_info. @data must have been allocated by kmalloc() only if
286deceb4c0SFlorian Fainelli  * @frag_size is 0, otherwise data should come from the page allocator.
287b2b5ce9dSEric Dumazet  * The return is the new skb buffer.
288b2b5ce9dSEric Dumazet  * On a failure the return is %NULL, and @data is not freed.
289b2b5ce9dSEric Dumazet  * Notes :
290b2b5ce9dSEric Dumazet  *  Before IO, driver allocates only data buffer where NIC put incoming frame
291b2b5ce9dSEric Dumazet  *  Driver should add room at head (NET_SKB_PAD) and
292b2b5ce9dSEric Dumazet  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
293b2b5ce9dSEric Dumazet  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
294b2b5ce9dSEric Dumazet  *  before giving packet to stack.
295b2b5ce9dSEric Dumazet  *  RX rings only contains data buffers, not full skbs.
296b2b5ce9dSEric Dumazet  */
297d3836f21SEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
298b2b5ce9dSEric Dumazet {
299b2b5ce9dSEric Dumazet 	struct skb_shared_info *shinfo;
300b2b5ce9dSEric Dumazet 	struct sk_buff *skb;
301d3836f21SEric Dumazet 	unsigned int size = frag_size ? : ksize(data);
302b2b5ce9dSEric Dumazet 
303b2b5ce9dSEric Dumazet 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
304b2b5ce9dSEric Dumazet 	if (!skb)
305b2b5ce9dSEric Dumazet 		return NULL;
306b2b5ce9dSEric Dumazet 
307d3836f21SEric Dumazet 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
308b2b5ce9dSEric Dumazet 
309b2b5ce9dSEric Dumazet 	memset(skb, 0, offsetof(struct sk_buff, tail));
310b2b5ce9dSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
311d3836f21SEric Dumazet 	skb->head_frag = frag_size != 0;
312b2b5ce9dSEric Dumazet 	atomic_set(&skb->users, 1);
313b2b5ce9dSEric Dumazet 	skb->head = data;
314b2b5ce9dSEric Dumazet 	skb->data = data;
315b2b5ce9dSEric Dumazet 	skb_reset_tail_pointer(skb);
316b2b5ce9dSEric Dumazet 	skb->end = skb->tail + size;
31735d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
31835d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
319b2b5ce9dSEric Dumazet 
320b2b5ce9dSEric Dumazet 	/* make sure we initialize shinfo sequentially */
321b2b5ce9dSEric Dumazet 	shinfo = skb_shinfo(skb);
322b2b5ce9dSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
323b2b5ce9dSEric Dumazet 	atomic_set(&shinfo->dataref, 1);
324b2b5ce9dSEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
325b2b5ce9dSEric Dumazet 
326b2b5ce9dSEric Dumazet 	return skb;
327b2b5ce9dSEric Dumazet }
328b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
329b2b5ce9dSEric Dumazet 
330a1c7fff7SEric Dumazet struct netdev_alloc_cache {
33169b08f62SEric Dumazet 	struct page_frag	frag;
33269b08f62SEric Dumazet 	/* we maintain a pagecount bias, so that we dont dirty cache line
33369b08f62SEric Dumazet 	 * containing page->_count every time we allocate a fragment.
33469b08f62SEric Dumazet 	 */
335540eb7bfSAlexander Duyck 	unsigned int		pagecnt_bias;
336a1c7fff7SEric Dumazet };
337a1c7fff7SEric Dumazet static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
338a1c7fff7SEric Dumazet 
339c93bdd0eSMel Gorman static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
3406f532612SEric Dumazet {
3416f532612SEric Dumazet 	struct netdev_alloc_cache *nc;
3426f532612SEric Dumazet 	void *data = NULL;
34369b08f62SEric Dumazet 	int order;
3446f532612SEric Dumazet 	unsigned long flags;
3456f532612SEric Dumazet 
3466f532612SEric Dumazet 	local_irq_save(flags);
3476f532612SEric Dumazet 	nc = &__get_cpu_var(netdev_alloc_cache);
34869b08f62SEric Dumazet 	if (unlikely(!nc->frag.page)) {
3496f532612SEric Dumazet refill:
35069b08f62SEric Dumazet 		for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
35169b08f62SEric Dumazet 			gfp_t gfp = gfp_mask;
35269b08f62SEric Dumazet 
35369b08f62SEric Dumazet 			if (order)
35469b08f62SEric Dumazet 				gfp |= __GFP_COMP | __GFP_NOWARN;
35569b08f62SEric Dumazet 			nc->frag.page = alloc_pages(gfp, order);
35669b08f62SEric Dumazet 			if (likely(nc->frag.page))
35769b08f62SEric Dumazet 				break;
35869b08f62SEric Dumazet 			if (--order < 0)
359540eb7bfSAlexander Duyck 				goto end;
36069b08f62SEric Dumazet 		}
36169b08f62SEric Dumazet 		nc->frag.size = PAGE_SIZE << order;
362540eb7bfSAlexander Duyck recycle:
36369b08f62SEric Dumazet 		atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
36469b08f62SEric Dumazet 		nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
36569b08f62SEric Dumazet 		nc->frag.offset = 0;
3666f532612SEric Dumazet 	}
367540eb7bfSAlexander Duyck 
36869b08f62SEric Dumazet 	if (nc->frag.offset + fragsz > nc->frag.size) {
369540eb7bfSAlexander Duyck 		/* avoid unnecessary locked operations if possible */
37069b08f62SEric Dumazet 		if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
37169b08f62SEric Dumazet 		    atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
372540eb7bfSAlexander Duyck 			goto recycle;
3736f532612SEric Dumazet 		goto refill;
3746f532612SEric Dumazet 	}
375540eb7bfSAlexander Duyck 
37669b08f62SEric Dumazet 	data = page_address(nc->frag.page) + nc->frag.offset;
37769b08f62SEric Dumazet 	nc->frag.offset += fragsz;
378540eb7bfSAlexander Duyck 	nc->pagecnt_bias--;
379540eb7bfSAlexander Duyck end:
3806f532612SEric Dumazet 	local_irq_restore(flags);
3816f532612SEric Dumazet 	return data;
3826f532612SEric Dumazet }
383c93bdd0eSMel Gorman 
384c93bdd0eSMel Gorman /**
385c93bdd0eSMel Gorman  * netdev_alloc_frag - allocate a page fragment
386c93bdd0eSMel Gorman  * @fragsz: fragment size
387c93bdd0eSMel Gorman  *
388c93bdd0eSMel Gorman  * Allocates a frag from a page for receive buffer.
389c93bdd0eSMel Gorman  * Uses GFP_ATOMIC allocations.
390c93bdd0eSMel Gorman  */
391c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz)
392c93bdd0eSMel Gorman {
393c93bdd0eSMel Gorman 	return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
394c93bdd0eSMel Gorman }
3956f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag);
3966f532612SEric Dumazet 
3976f532612SEric Dumazet /**
3988af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
3998af27456SChristoph Hellwig  *	@dev: network device to receive on
4008af27456SChristoph Hellwig  *	@length: length to allocate
4018af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
4028af27456SChristoph Hellwig  *
4038af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
4048af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
4058af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
4068af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
4078af27456SChristoph Hellwig  *
4088af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
4098af27456SChristoph Hellwig  */
4108af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
4118af27456SChristoph Hellwig 				   unsigned int length, gfp_t gfp_mask)
4128af27456SChristoph Hellwig {
4136f532612SEric Dumazet 	struct sk_buff *skb = NULL;
414a1c7fff7SEric Dumazet 	unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
415a1c7fff7SEric Dumazet 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4168af27456SChristoph Hellwig 
417310e158cSEric Dumazet 	if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
418c93bdd0eSMel Gorman 		void *data;
419c93bdd0eSMel Gorman 
420c93bdd0eSMel Gorman 		if (sk_memalloc_socks())
421c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
422c93bdd0eSMel Gorman 
423c93bdd0eSMel Gorman 		data = __netdev_alloc_frag(fragsz, gfp_mask);
424a1c7fff7SEric Dumazet 
4256f532612SEric Dumazet 		if (likely(data)) {
4266f532612SEric Dumazet 			skb = build_skb(data, fragsz);
4276f532612SEric Dumazet 			if (unlikely(!skb))
4286f532612SEric Dumazet 				put_page(virt_to_head_page(data));
429a1c7fff7SEric Dumazet 		}
430a1c7fff7SEric Dumazet 	} else {
431c93bdd0eSMel Gorman 		skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
432c93bdd0eSMel Gorman 				  SKB_ALLOC_RX, NUMA_NO_NODE);
433a1c7fff7SEric Dumazet 	}
4347b2e497aSChristoph Hellwig 	if (likely(skb)) {
4358af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
4367b2e497aSChristoph Hellwig 		skb->dev = dev;
4377b2e497aSChristoph Hellwig 	}
4388af27456SChristoph Hellwig 	return skb;
4398af27456SChristoph Hellwig }
440b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
4411da177e4SLinus Torvalds 
442654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
44350269e19SEric Dumazet 		     int size, unsigned int truesize)
444654bed16SPeter Zijlstra {
445654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
446654bed16SPeter Zijlstra 	skb->len += size;
447654bed16SPeter Zijlstra 	skb->data_len += size;
44850269e19SEric Dumazet 	skb->truesize += truesize;
449654bed16SPeter Zijlstra }
450654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
451654bed16SPeter Zijlstra 
452f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
453f8e617e1SJason Wang 			  unsigned int truesize)
454f8e617e1SJason Wang {
455f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
456f8e617e1SJason Wang 
457f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
458f8e617e1SJason Wang 	skb->len += size;
459f8e617e1SJason Wang 	skb->data_len += size;
460f8e617e1SJason Wang 	skb->truesize += truesize;
461f8e617e1SJason Wang }
462f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
463f8e617e1SJason Wang 
46427b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
4651da177e4SLinus Torvalds {
466bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
46727b437c8SHerbert Xu 	*listp = NULL;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
47027b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
47127b437c8SHerbert Xu {
47227b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
47327b437c8SHerbert Xu }
47427b437c8SHerbert Xu 
4751da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	struct sk_buff *list;
4781da177e4SLinus Torvalds 
479fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
4801da177e4SLinus Torvalds 		skb_get(list);
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
483d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
484d3836f21SEric Dumazet {
485d3836f21SEric Dumazet 	if (skb->head_frag)
486d3836f21SEric Dumazet 		put_page(virt_to_head_page(skb->head));
487d3836f21SEric Dumazet 	else
488d3836f21SEric Dumazet 		kfree(skb->head);
489d3836f21SEric Dumazet }
490d3836f21SEric Dumazet 
4915bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	if (!skb->cloned ||
4941da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
4951da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
4961da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
4971da177e4SLinus Torvalds 			int i;
4981da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
499ea2ab693SIan Campbell 				skb_frag_unref(skb, i);
5001da177e4SLinus Torvalds 		}
5011da177e4SLinus Torvalds 
502a6686f2fSShirley Ma 		/*
503a6686f2fSShirley Ma 		 * If skb buf is from userspace, we need to notify the caller
504a6686f2fSShirley Ma 		 * the lower device DMA has done;
505a6686f2fSShirley Ma 		 */
506a6686f2fSShirley Ma 		if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
507a6686f2fSShirley Ma 			struct ubuf_info *uarg;
508a6686f2fSShirley Ma 
509a6686f2fSShirley Ma 			uarg = skb_shinfo(skb)->destructor_arg;
510a6686f2fSShirley Ma 			if (uarg->callback)
511e19d6763SMichael S. Tsirkin 				uarg->callback(uarg, true);
512a6686f2fSShirley Ma 		}
513a6686f2fSShirley Ma 
51421dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
5151da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
5161da177e4SLinus Torvalds 
517d3836f21SEric Dumazet 		skb_free_head(skb);
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /*
5221da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
5231da177e4SLinus Torvalds  */
5242d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
5251da177e4SLinus Torvalds {
526d179cd12SDavid S. Miller 	struct sk_buff *other;
527d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
528d179cd12SDavid S. Miller 
529d179cd12SDavid S. Miller 	switch (skb->fclone) {
530d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
5311da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
532d179cd12SDavid S. Miller 		break;
533d179cd12SDavid S. Miller 
534d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
535d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
536d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
537d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
538d179cd12SDavid S. Miller 		break;
539d179cd12SDavid S. Miller 
540d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
541d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
542d179cd12SDavid S. Miller 		other = skb - 1;
543d179cd12SDavid S. Miller 
544d179cd12SDavid S. Miller 		/* The clone portion is available for
545d179cd12SDavid S. Miller 		 * fast-cloning again.
546d179cd12SDavid S. Miller 		 */
547d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
548d179cd12SDavid S. Miller 
549d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
550d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
551d179cd12SDavid S. Miller 		break;
5523ff50b79SStephen Hemminger 	}
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
55504a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
5561da177e4SLinus Torvalds {
557adf30907SEric Dumazet 	skb_dst_drop(skb);
5581da177e4SLinus Torvalds #ifdef CONFIG_XFRM
5591da177e4SLinus Torvalds 	secpath_put(skb->sp);
5601da177e4SLinus Torvalds #endif
5611da177e4SLinus Torvalds 	if (skb->destructor) {
5629c2b3328SStephen Hemminger 		WARN_ON(in_irq());
5631da177e4SLinus Torvalds 		skb->destructor(skb);
5641da177e4SLinus Torvalds 	}
565a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
5665f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
5672fc72c7bSKOVACS Krisztian #endif
5681da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
5691da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
5701da177e4SLinus Torvalds #endif
5711da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
5721da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
5731da177e4SLinus Torvalds 	skb->tc_index = 0;
5741da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
5751da177e4SLinus Torvalds 	skb->tc_verd = 0;
5761da177e4SLinus Torvalds #endif
5771da177e4SLinus Torvalds #endif
57804a4bb55SLennert Buytenhek }
57904a4bb55SLennert Buytenhek 
58004a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
58104a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
58204a4bb55SLennert Buytenhek {
58304a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
5845e71d9d7SPablo Neira 	if (likely(skb->head))
5852d4baff8SHerbert Xu 		skb_release_data(skb);
5862d4baff8SHerbert Xu }
5871da177e4SLinus Torvalds 
5882d4baff8SHerbert Xu /**
5892d4baff8SHerbert Xu  *	__kfree_skb - private function
5902d4baff8SHerbert Xu  *	@skb: buffer
5912d4baff8SHerbert Xu  *
5922d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
5932d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
5942d4baff8SHerbert Xu  *	always call kfree_skb
5952d4baff8SHerbert Xu  */
5962d4baff8SHerbert Xu 
5972d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
5982d4baff8SHerbert Xu {
5992d4baff8SHerbert Xu 	skb_release_all(skb);
6001da177e4SLinus Torvalds 	kfree_skbmem(skb);
6011da177e4SLinus Torvalds }
602b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds /**
605231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
606231d06aeSJörn Engel  *	@skb: buffer to free
607231d06aeSJörn Engel  *
608231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
609231d06aeSJörn Engel  *	hit zero.
610231d06aeSJörn Engel  */
611231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
612231d06aeSJörn Engel {
613231d06aeSJörn Engel 	if (unlikely(!skb))
614231d06aeSJörn Engel 		return;
615231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
616231d06aeSJörn Engel 		smp_rmb();
617231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
618231d06aeSJörn Engel 		return;
619ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
620231d06aeSJörn Engel 	__kfree_skb(skb);
621231d06aeSJörn Engel }
622b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
623231d06aeSJörn Engel 
624bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
625bd8a7036SEric Dumazet {
626bd8a7036SEric Dumazet 	while (segs) {
627bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
628bd8a7036SEric Dumazet 
629bd8a7036SEric Dumazet 		kfree_skb(segs);
630bd8a7036SEric Dumazet 		segs = next;
631bd8a7036SEric Dumazet 	}
632bd8a7036SEric Dumazet }
633bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
634bd8a7036SEric Dumazet 
635d1a203eaSStephen Hemminger /**
63625121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
63725121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
63825121173SMichael S. Tsirkin  *
63925121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
64025121173SMichael S. Tsirkin  *	skb must be freed afterwards.
64125121173SMichael S. Tsirkin  */
64225121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
64325121173SMichael S. Tsirkin {
64425121173SMichael S. Tsirkin 	if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
64525121173SMichael S. Tsirkin 		struct ubuf_info *uarg;
64625121173SMichael S. Tsirkin 
64725121173SMichael S. Tsirkin 		uarg = skb_shinfo(skb)->destructor_arg;
64825121173SMichael S. Tsirkin 		if (uarg->callback)
64925121173SMichael S. Tsirkin 			uarg->callback(uarg, false);
65025121173SMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
65125121173SMichael S. Tsirkin 	}
65225121173SMichael S. Tsirkin }
65325121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
65425121173SMichael S. Tsirkin 
65525121173SMichael S. Tsirkin /**
656ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
657ead2ceb0SNeil Horman  *	@skb: buffer to free
658ead2ceb0SNeil Horman  *
659ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
660ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
661ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
662ead2ceb0SNeil Horman  */
663ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
664ead2ceb0SNeil Horman {
665ead2ceb0SNeil Horman 	if (unlikely(!skb))
666ead2ceb0SNeil Horman 		return;
667ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
668ead2ceb0SNeil Horman 		smp_rmb();
669ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
670ead2ceb0SNeil Horman 		return;
67107dc22e7SKoki Sanagi 	trace_consume_skb(skb);
672ead2ceb0SNeil Horman 	__kfree_skb(skb);
673ead2ceb0SNeil Horman }
674ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
675ead2ceb0SNeil Horman 
676dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
677dec18810SHerbert Xu {
678dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
679dec18810SHerbert Xu 	new->dev		= old->dev;
680dec18810SHerbert Xu 	new->transport_header	= old->transport_header;
681dec18810SHerbert Xu 	new->network_header	= old->network_header;
682dec18810SHerbert Xu 	new->mac_header		= old->mac_header;
6834bc41b84SJoe Stringer 	new->inner_protocol	= old->inner_protocol;
6846a674e9cSJoseph Gasparakis 	new->inner_transport_header = old->inner_transport_header;
68592df9b21SPravin B Shelar 	new->inner_network_header = old->inner_network_header;
686aefbd2b3SPravin B Shelar 	new->inner_mac_header = old->inner_mac_header;
6877fee226aSEric Dumazet 	skb_dst_copy(new, old);
6883df7a74eSTom Herbert 	skb_copy_hash(new, old);
6896461be3aSChangli Gao 	new->ooo_okay		= old->ooo_okay;
6903bdc0ebaSBen Greear 	new->no_fcs		= old->no_fcs;
6916a674e9cSJoseph Gasparakis 	new->encapsulation	= old->encapsulation;
69246fb51ebSTom Herbert 	new->encap_hdr_csum	= old->encap_hdr_csum;
69346fb51ebSTom Herbert 	new->csum_valid		= old->csum_valid;
69446fb51ebSTom Herbert 	new->csum_complete_sw	= old->csum_complete_sw;
695def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
696dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
697dec18810SHerbert Xu #endif
698dec18810SHerbert Xu 	memcpy(new->cb, old->cb, sizeof(old->cb));
6999bcb97caSHerbert Xu 	new->csum		= old->csum;
70060ff7467SWANG Cong 	new->ignore_df		= old->ignore_df;
701dec18810SHerbert Xu 	new->pkt_type		= old->pkt_type;
702dec18810SHerbert Xu 	new->ip_summed		= old->ip_summed;
703dec18810SHerbert Xu 	skb_copy_queue_mapping(new, old);
704dec18810SHerbert Xu 	new->priority		= old->priority;
705a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_IP_VS)
706dec18810SHerbert Xu 	new->ipvs_property	= old->ipvs_property;
707dec18810SHerbert Xu #endif
708c93bdd0eSMel Gorman 	new->pfmemalloc		= old->pfmemalloc;
709dec18810SHerbert Xu 	new->protocol		= old->protocol;
710dec18810SHerbert Xu 	new->mark		= old->mark;
7118964be4aSEric Dumazet 	new->skb_iif		= old->skb_iif;
712dec18810SHerbert Xu 	__nf_copy(new, old);
713dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED
714dec18810SHerbert Xu 	new->tc_index		= old->tc_index;
715dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT
716dec18810SHerbert Xu 	new->tc_verd		= old->tc_verd;
717dec18810SHerbert Xu #endif
718dec18810SHerbert Xu #endif
71986a9bad3SPatrick McHardy 	new->vlan_proto		= old->vlan_proto;
7206aa895b0SPatrick McHardy 	new->vlan_tci		= old->vlan_tci;
7216aa895b0SPatrick McHardy 
722dec18810SHerbert Xu 	skb_copy_secmark(new, old);
72306021292SEliezer Tamir 
724e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
72506021292SEliezer Tamir 	new->napi_id	= old->napi_id;
72606021292SEliezer Tamir #endif
727dec18810SHerbert Xu }
728dec18810SHerbert Xu 
72982c49a35SHerbert Xu /*
73082c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
73182c49a35SHerbert Xu  * __copy_skb_header above instead.
73282c49a35SHerbert Xu  */
733e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds #define C(x) n->x = skb->x
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	n->next = n->prev = NULL;
7381da177e4SLinus Torvalds 	n->sk = NULL;
739dec18810SHerbert Xu 	__copy_skb_header(n, skb);
740dec18810SHerbert Xu 
7411da177e4SLinus Torvalds 	C(len);
7421da177e4SLinus Torvalds 	C(data_len);
7433e6b3b2eSAlexey Dobriyan 	C(mac_len);
744334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
74502f1c89dSPaul Moore 	n->cloned = 1;
7461da177e4SLinus Torvalds 	n->nohdr = 0;
7471da177e4SLinus Torvalds 	n->destructor = NULL;
7481da177e4SLinus Torvalds 	C(tail);
7491da177e4SLinus Torvalds 	C(end);
75002f1c89dSPaul Moore 	C(head);
751d3836f21SEric Dumazet 	C(head_frag);
75202f1c89dSPaul Moore 	C(data);
75302f1c89dSPaul Moore 	C(truesize);
75402f1c89dSPaul Moore 	atomic_set(&n->users, 1);
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
7571da177e4SLinus Torvalds 	skb->cloned = 1;
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	return n;
760e0053ec0SHerbert Xu #undef C
761e0053ec0SHerbert Xu }
762e0053ec0SHerbert Xu 
763e0053ec0SHerbert Xu /**
764e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
765e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
766e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
767e0053ec0SHerbert Xu  *
768e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
769e0053ec0SHerbert Xu  *	supplied by the user.
770e0053ec0SHerbert Xu  *
771e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
772e0053ec0SHerbert Xu  */
773e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
774e0053ec0SHerbert Xu {
7752d4baff8SHerbert Xu 	skb_release_all(dst);
776e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
777e0053ec0SHerbert Xu }
778e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
779e0053ec0SHerbert Xu 
7802c53040fSBen Hutchings /**
7812c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
78248c83012SMichael S. Tsirkin  *	@skb: the skb to modify
78348c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
78448c83012SMichael S. Tsirkin  *
78548c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
78648c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
78748c83012SMichael S. Tsirkin  *	to userspace pages.
78848c83012SMichael S. Tsirkin  *
78948c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
79048c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
79148c83012SMichael S. Tsirkin  *
79248c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
79348c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
79448c83012SMichael S. Tsirkin  */
79548c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
796a6686f2fSShirley Ma {
797a6686f2fSShirley Ma 	int i;
798a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
799a6686f2fSShirley Ma 	struct page *page, *head = NULL;
800a6686f2fSShirley Ma 	struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
801a6686f2fSShirley Ma 
802a6686f2fSShirley Ma 	for (i = 0; i < num_frags; i++) {
803a6686f2fSShirley Ma 		u8 *vaddr;
804a6686f2fSShirley Ma 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
805a6686f2fSShirley Ma 
80602756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
807a6686f2fSShirley Ma 		if (!page) {
808a6686f2fSShirley Ma 			while (head) {
80940dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
810a6686f2fSShirley Ma 				put_page(head);
811a6686f2fSShirley Ma 				head = next;
812a6686f2fSShirley Ma 			}
813a6686f2fSShirley Ma 			return -ENOMEM;
814a6686f2fSShirley Ma 		}
81551c56b00SEric Dumazet 		vaddr = kmap_atomic(skb_frag_page(f));
816a6686f2fSShirley Ma 		memcpy(page_address(page),
8179e903e08SEric Dumazet 		       vaddr + f->page_offset, skb_frag_size(f));
81851c56b00SEric Dumazet 		kunmap_atomic(vaddr);
81940dadff2SSunghan Suh 		set_page_private(page, (unsigned long)head);
820a6686f2fSShirley Ma 		head = page;
821a6686f2fSShirley Ma 	}
822a6686f2fSShirley Ma 
823a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
82402756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
825a8605c60SIan Campbell 		skb_frag_unref(skb, i);
826a6686f2fSShirley Ma 
827e19d6763SMichael S. Tsirkin 	uarg->callback(uarg, false);
828a6686f2fSShirley Ma 
829a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
83002756ed4SKrishna Kumar 	for (i = num_frags - 1; i >= 0; i--) {
83102756ed4SKrishna Kumar 		__skb_fill_page_desc(skb, i, head, 0,
83202756ed4SKrishna Kumar 				     skb_shinfo(skb)->frags[i].size);
83340dadff2SSunghan Suh 		head = (struct page *)page_private(head);
834a6686f2fSShirley Ma 	}
83548c83012SMichael S. Tsirkin 
83648c83012SMichael S. Tsirkin 	skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
837a6686f2fSShirley Ma 	return 0;
838a6686f2fSShirley Ma }
839dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
840a6686f2fSShirley Ma 
841e0053ec0SHerbert Xu /**
842e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
843e0053ec0SHerbert Xu  *	@skb: buffer to clone
844e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
845e0053ec0SHerbert Xu  *
846e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
847e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
848e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
849e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
850e0053ec0SHerbert Xu  *
851e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
852e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
853e0053ec0SHerbert Xu  */
854e0053ec0SHerbert Xu 
855e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
856e0053ec0SHerbert Xu {
857e0053ec0SHerbert Xu 	struct sk_buff *n;
858e0053ec0SHerbert Xu 
85970008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
860a6686f2fSShirley Ma 		return NULL;
861a6686f2fSShirley Ma 
862e0053ec0SHerbert Xu 	n = skb + 1;
863e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
864e0053ec0SHerbert Xu 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
865e0053ec0SHerbert Xu 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
866e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
867e0053ec0SHerbert Xu 		atomic_inc(fclone_ref);
868e0053ec0SHerbert Xu 	} else {
869c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
870c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
871c93bdd0eSMel Gorman 
872e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
873e0053ec0SHerbert Xu 		if (!n)
874e0053ec0SHerbert Xu 			return NULL;
875fe55f6d5SVegard Nossum 
876fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags1);
877fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags2);
878e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
879e0053ec0SHerbert Xu 	}
880e0053ec0SHerbert Xu 
881e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
8821da177e4SLinus Torvalds }
883b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
8841da177e4SLinus Torvalds 
885f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off)
886f5b17294SPravin B Shelar {
887030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
888030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
889030737bcSEric Dumazet 		skb->csum_start += off;
890f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
891f5b17294SPravin B Shelar 	skb->transport_header += off;
892f5b17294SPravin B Shelar 	skb->network_header   += off;
893f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
894f5b17294SPravin B Shelar 		skb->mac_header += off;
895f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
896f5b17294SPravin B Shelar 	skb->inner_network_header += off;
897aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
898f5b17294SPravin B Shelar }
899f5b17294SPravin B Shelar 
9001da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
9011da177e4SLinus Torvalds {
902dec18810SHerbert Xu 	__copy_skb_header(new, old);
903dec18810SHerbert Xu 
9047967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
9057967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
9067967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
909c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
910c93bdd0eSMel Gorman {
911c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
912c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
913c93bdd0eSMel Gorman 	return 0;
914c93bdd0eSMel Gorman }
915c93bdd0eSMel Gorman 
9161da177e4SLinus Torvalds /**
9171da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
9181da177e4SLinus Torvalds  *	@skb: buffer to copy
9191da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
9201da177e4SLinus Torvalds  *
9211da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
9221da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
9231da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
9241da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
9251da177e4SLinus Torvalds  *
9261da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
9271da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
9281da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
9291da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
9301da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
9311da177e4SLinus Torvalds  */
9321da177e4SLinus Torvalds 
933dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
9341da177e4SLinus Torvalds {
9356602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
936ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
937c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
938c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
9396602cebbSEric Dumazet 
9401da177e4SLinus Torvalds 	if (!n)
9411da177e4SLinus Torvalds 		return NULL;
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds 	/* Set the data pointer */
9441da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
9451da177e4SLinus Torvalds 	/* Set the tail pointer and length */
9461da177e4SLinus Torvalds 	skb_put(n, skb->len);
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
9491da177e4SLinus Torvalds 		BUG();
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	copy_skb_header(n, skb);
9521da177e4SLinus Torvalds 	return n;
9531da177e4SLinus Torvalds }
954b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds /**
957bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
9581da177e4SLinus Torvalds  *	@skb: buffer to copy
959117632e6SEric Dumazet  *	@headroom: headroom of new skb
9601da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
961bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
962bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
963bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
9641da177e4SLinus Torvalds  *
9651da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
9661da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
9671da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
9681da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
9691da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
9701da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
9711da177e4SLinus Torvalds  */
9721da177e4SLinus Torvalds 
973bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
974bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
9751da177e4SLinus Torvalds {
976117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
977bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
978bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
9796602cebbSEric Dumazet 
9801da177e4SLinus Torvalds 	if (!n)
9811da177e4SLinus Torvalds 		goto out;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	/* Set the data pointer */
984117632e6SEric Dumazet 	skb_reserve(n, headroom);
9851da177e4SLinus Torvalds 	/* Set the tail pointer and length */
9861da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
9871da177e4SLinus Torvalds 	/* Copy the bytes */
988d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
9891da177e4SLinus Torvalds 
99025f484a6SHerbert Xu 	n->truesize += skb->data_len;
9911da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
9921da177e4SLinus Torvalds 	n->len	     = skb->len;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
9951da177e4SLinus Torvalds 		int i;
9961da177e4SLinus Torvalds 
99770008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask)) {
9981511022cSDan Carpenter 			kfree_skb(n);
9991511022cSDan Carpenter 			n = NULL;
1000a6686f2fSShirley Ma 			goto out;
1001a6686f2fSShirley Ma 		}
10021da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
10031da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1004ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
10051da177e4SLinus Torvalds 		}
10061da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 
100921dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
10101da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
10111da177e4SLinus Torvalds 		skb_clone_fraglist(n);
10121da177e4SLinus Torvalds 	}
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	copy_skb_header(n, skb);
10151da177e4SLinus Torvalds out:
10161da177e4SLinus Torvalds 	return n;
10171da177e4SLinus Torvalds }
1018bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds /**
10211da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
10221da177e4SLinus Torvalds  *	@skb: buffer to reallocate
10231da177e4SLinus Torvalds  *	@nhead: room to add at head
10241da177e4SLinus Torvalds  *	@ntail: room to add at tail
10251da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
10261da177e4SLinus Torvalds  *
1027bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1028bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
10291da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
10301da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
10311da177e4SLinus Torvalds  *
10321da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
10331da177e4SLinus Torvalds  *	reloaded after call to this function.
10341da177e4SLinus Torvalds  */
10351da177e4SLinus Torvalds 
103686a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1037dd0fc66fSAl Viro 		     gfp_t gfp_mask)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	int i;
10401da177e4SLinus Torvalds 	u8 *data;
1041ec47ea82SAlexander Duyck 	int size = nhead + skb_end_offset(skb) + ntail;
10421da177e4SLinus Torvalds 	long off;
10431da177e4SLinus Torvalds 
10444edd87adSHerbert Xu 	BUG_ON(nhead < 0);
10454edd87adSHerbert Xu 
10461da177e4SLinus Torvalds 	if (skb_shared(skb))
10471da177e4SLinus Torvalds 		BUG();
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
10501da177e4SLinus Torvalds 
1051c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1052c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1053c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1054c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
10551da177e4SLinus Torvalds 	if (!data)
10561da177e4SLinus Torvalds 		goto nodata;
105787151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
10606602cebbSEric Dumazet 	 * optimized for the cases when header is void.
10616602cebbSEric Dumazet 	 */
10626602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
10636602cebbSEric Dumazet 
10646602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
10656602cebbSEric Dumazet 	       skb_shinfo(skb),
1066fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
10671da177e4SLinus Torvalds 
10683e24591aSAlexander Duyck 	/*
10693e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
10703e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
10713e24591aSAlexander Duyck 	 * be since all we did is relocate the values
10723e24591aSAlexander Duyck 	 */
10733e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
1074a6686f2fSShirley Ma 		/* copy this zero copy skb frags */
107570008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1076a6686f2fSShirley Ma 			goto nofrags;
10771da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1078ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
10791da177e4SLinus Torvalds 
108021dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
10811da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 		skb_release_data(skb);
10843e24591aSAlexander Duyck 	} else {
10853e24591aSAlexander Duyck 		skb_free_head(skb);
10861fd63041SEric Dumazet 	}
10871da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	skb->head     = data;
1090d3836f21SEric Dumazet 	skb->head_frag = 0;
10911da177e4SLinus Torvalds 	skb->data    += off;
10924305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
10934305b541SArnaldo Carvalho de Melo 	skb->end      = size;
109456eb8882SPatrick McHardy 	off           = nhead;
10954305b541SArnaldo Carvalho de Melo #else
10964305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
109756eb8882SPatrick McHardy #endif
109827a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1099b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
11001da177e4SLinus Torvalds 	skb->cloned   = 0;
1101334a8132SPatrick McHardy 	skb->hdr_len  = 0;
11021da177e4SLinus Torvalds 	skb->nohdr    = 0;
11031da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
11041da177e4SLinus Torvalds 	return 0;
11051da177e4SLinus Torvalds 
1106a6686f2fSShirley Ma nofrags:
1107a6686f2fSShirley Ma 	kfree(data);
11081da177e4SLinus Torvalds nodata:
11091da177e4SLinus Torvalds 	return -ENOMEM;
11101da177e4SLinus Torvalds }
1111b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	struct sk_buff *skb2;
11181da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 	if (delta <= 0)
11211da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
11221da177e4SLinus Torvalds 	else {
11231da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
11241da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
11251da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
11261da177e4SLinus Torvalds 			kfree_skb(skb2);
11271da177e4SLinus Torvalds 			skb2 = NULL;
11281da177e4SLinus Torvalds 		}
11291da177e4SLinus Torvalds 	}
11301da177e4SLinus Torvalds 	return skb2;
11311da177e4SLinus Torvalds }
1132b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds /**
11351da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
11361da177e4SLinus Torvalds  *	@skb: buffer to copy
11371da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
11381da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
11391da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
11401da177e4SLinus Torvalds  *
11411da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
11421da177e4SLinus Torvalds  *	allocate additional space.
11431da177e4SLinus Torvalds  *
11441da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
11451da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
11461da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
11471da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
11481da177e4SLinus Torvalds  *
11491da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
11501da177e4SLinus Torvalds  *	is called from an interrupt.
11511da177e4SLinus Torvalds  */
11521da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
115386a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1154dd0fc66fSAl Viro 				gfp_t gfp_mask)
11551da177e4SLinus Torvalds {
11561da177e4SLinus Torvalds 	/*
11571da177e4SLinus Torvalds 	 *	Allocate the copy buffer
11581da177e4SLinus Torvalds 	 */
1159c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1160c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1161c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1162efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
11631da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if (!n)
11661da177e4SLinus Torvalds 		return NULL;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	/* Set the tail pointer and length */
11711da177e4SLinus Torvalds 	skb_put(n, skb->len);
11721da177e4SLinus Torvalds 
1173efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
11741da177e4SLinus Torvalds 	head_copy_off = 0;
11751da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
11761da177e4SLinus Torvalds 		head_copy_len = newheadroom;
11771da177e4SLinus Torvalds 	else
11781da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	/* Copy the linear header and data. */
11811da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
11821da177e4SLinus Torvalds 			  skb->len + head_copy_len))
11831da177e4SLinus Torvalds 		BUG();
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	copy_skb_header(n, skb);
11861da177e4SLinus Torvalds 
1187030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1188efd1e8d5SPatrick McHardy 
11891da177e4SLinus Torvalds 	return n;
11901da177e4SLinus Torvalds }
1191b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds /**
11941da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
11951da177e4SLinus Torvalds  *	@skb: buffer to pad
11961da177e4SLinus Torvalds  *	@pad: space to pad
11971da177e4SLinus Torvalds  *
11981da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
11991da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
12001da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
12011da177e4SLinus Torvalds  *
12025b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
12031da177e4SLinus Torvalds  */
12041da177e4SLinus Torvalds 
12055b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
12061da177e4SLinus Torvalds {
12075b057c6bSHerbert Xu 	int err;
12085b057c6bSHerbert Xu 	int ntail;
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
12115b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
12121da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
12135b057c6bSHerbert Xu 		return 0;
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 
12164305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
12175b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
12185b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
12195b057c6bSHerbert Xu 		if (unlikely(err))
12205b057c6bSHerbert Xu 			goto free_skb;
12215b057c6bSHerbert Xu 	}
12225b057c6bSHerbert Xu 
12235b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
12245b057c6bSHerbert Xu 	 * to be audited.
12255b057c6bSHerbert Xu 	 */
12265b057c6bSHerbert Xu 	err = skb_linearize(skb);
12275b057c6bSHerbert Xu 	if (unlikely(err))
12285b057c6bSHerbert Xu 		goto free_skb;
12295b057c6bSHerbert Xu 
12305b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
12315b057c6bSHerbert Xu 	return 0;
12325b057c6bSHerbert Xu 
12335b057c6bSHerbert Xu free_skb:
12341da177e4SLinus Torvalds 	kfree_skb(skb);
12355b057c6bSHerbert Xu 	return err;
12361da177e4SLinus Torvalds }
1237b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
12381da177e4SLinus Torvalds 
12390dde3e16SIlpo Järvinen /**
12400c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
12410c7ddf36SMathias Krause  *	@skb: start of the buffer to use
12420c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
12430c7ddf36SMathias Krause  *	@len: amount of data to add
12440c7ddf36SMathias Krause  *
12450c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
12460c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
12470c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
12480c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
12490c7ddf36SMathias Krause  *	returned.
12500c7ddf36SMathias Krause  */
12510c7ddf36SMathias Krause 
12520c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
12530c7ddf36SMathias Krause {
12540c7ddf36SMathias Krause 	if (tail != skb) {
12550c7ddf36SMathias Krause 		skb->data_len += len;
12560c7ddf36SMathias Krause 		skb->len += len;
12570c7ddf36SMathias Krause 	}
12580c7ddf36SMathias Krause 	return skb_put(tail, len);
12590c7ddf36SMathias Krause }
12600c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
12610c7ddf36SMathias Krause 
12620c7ddf36SMathias Krause /**
12630dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
12640dde3e16SIlpo Järvinen  *	@skb: buffer to use
12650dde3e16SIlpo Järvinen  *	@len: amount of data to add
12660dde3e16SIlpo Järvinen  *
12670dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
12680dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
12690dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
12700dde3e16SIlpo Järvinen  */
12710dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
12720dde3e16SIlpo Järvinen {
12730dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
12740dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
12750dde3e16SIlpo Järvinen 	skb->tail += len;
12760dde3e16SIlpo Järvinen 	skb->len  += len;
12770dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
12780dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
12790dde3e16SIlpo Järvinen 	return tmp;
12800dde3e16SIlpo Järvinen }
12810dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
12820dde3e16SIlpo Järvinen 
12836be8ac2fSIlpo Järvinen /**
1284c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1285c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1286c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1287c2aa270aSIlpo Järvinen  *
1288c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1289c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1290c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1291c2aa270aSIlpo Järvinen  */
1292c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1293c2aa270aSIlpo Järvinen {
1294c2aa270aSIlpo Järvinen 	skb->data -= len;
1295c2aa270aSIlpo Järvinen 	skb->len  += len;
1296c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1297c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1298c2aa270aSIlpo Järvinen 	return skb->data;
1299c2aa270aSIlpo Järvinen }
1300c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1301c2aa270aSIlpo Järvinen 
1302c2aa270aSIlpo Järvinen /**
13036be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
13046be8ac2fSIlpo Järvinen  *	@skb: buffer to use
13056be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
13066be8ac2fSIlpo Järvinen  *
13076be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
13086be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
13096be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
13106be8ac2fSIlpo Järvinen  *	the old data.
13116be8ac2fSIlpo Järvinen  */
13126be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
13136be8ac2fSIlpo Järvinen {
131447d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
13156be8ac2fSIlpo Järvinen }
13166be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
13176be8ac2fSIlpo Järvinen 
1318419ae74eSIlpo Järvinen /**
1319419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1320419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1321419ae74eSIlpo Järvinen  *	@len: new length
1322419ae74eSIlpo Järvinen  *
1323419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1324419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1325419ae74eSIlpo Järvinen  *	The skb must be linear.
1326419ae74eSIlpo Järvinen  */
1327419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1328419ae74eSIlpo Järvinen {
1329419ae74eSIlpo Järvinen 	if (skb->len > len)
1330419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1331419ae74eSIlpo Järvinen }
1332419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1333419ae74eSIlpo Järvinen 
13343cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
13351da177e4SLinus Torvalds  */
13361da177e4SLinus Torvalds 
13373cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
13381da177e4SLinus Torvalds {
133927b437c8SHerbert Xu 	struct sk_buff **fragp;
134027b437c8SHerbert Xu 	struct sk_buff *frag;
13411da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
13421da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
13431da177e4SLinus Torvalds 	int i;
134427b437c8SHerbert Xu 	int err;
134527b437c8SHerbert Xu 
134627b437c8SHerbert Xu 	if (skb_cloned(skb) &&
134727b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
134827b437c8SHerbert Xu 		return err;
13491da177e4SLinus Torvalds 
1350f4d26fb3SHerbert Xu 	i = 0;
1351f4d26fb3SHerbert Xu 	if (offset >= len)
1352f4d26fb3SHerbert Xu 		goto drop_pages;
1353f4d26fb3SHerbert Xu 
1354f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
13559e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
135627b437c8SHerbert Xu 
135727b437c8SHerbert Xu 		if (end < len) {
13581da177e4SLinus Torvalds 			offset = end;
135927b437c8SHerbert Xu 			continue;
13601da177e4SLinus Torvalds 		}
13611da177e4SLinus Torvalds 
13629e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
136327b437c8SHerbert Xu 
1364f4d26fb3SHerbert Xu drop_pages:
136527b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
136627b437c8SHerbert Xu 
136727b437c8SHerbert Xu 		for (; i < nfrags; i++)
1368ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
136927b437c8SHerbert Xu 
137021dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
137127b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1372f4d26fb3SHerbert Xu 		goto done;
137327b437c8SHerbert Xu 	}
137427b437c8SHerbert Xu 
137527b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
137627b437c8SHerbert Xu 	     fragp = &frag->next) {
137727b437c8SHerbert Xu 		int end = offset + frag->len;
137827b437c8SHerbert Xu 
137927b437c8SHerbert Xu 		if (skb_shared(frag)) {
138027b437c8SHerbert Xu 			struct sk_buff *nfrag;
138127b437c8SHerbert Xu 
138227b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
138327b437c8SHerbert Xu 			if (unlikely(!nfrag))
138427b437c8SHerbert Xu 				return -ENOMEM;
138527b437c8SHerbert Xu 
138627b437c8SHerbert Xu 			nfrag->next = frag->next;
138785bb2a60SEric Dumazet 			consume_skb(frag);
138827b437c8SHerbert Xu 			frag = nfrag;
138927b437c8SHerbert Xu 			*fragp = frag;
139027b437c8SHerbert Xu 		}
139127b437c8SHerbert Xu 
139227b437c8SHerbert Xu 		if (end < len) {
139327b437c8SHerbert Xu 			offset = end;
139427b437c8SHerbert Xu 			continue;
139527b437c8SHerbert Xu 		}
139627b437c8SHerbert Xu 
139727b437c8SHerbert Xu 		if (end > len &&
139827b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
139927b437c8SHerbert Xu 			return err;
140027b437c8SHerbert Xu 
140127b437c8SHerbert Xu 		if (frag->next)
140227b437c8SHerbert Xu 			skb_drop_list(&frag->next);
140327b437c8SHerbert Xu 		break;
140427b437c8SHerbert Xu 	}
140527b437c8SHerbert Xu 
1406f4d26fb3SHerbert Xu done:
140727b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
14081da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
14091da177e4SLinus Torvalds 		skb->len       = len;
14101da177e4SLinus Torvalds 	} else {
14111da177e4SLinus Torvalds 		skb->len       = len;
14121da177e4SLinus Torvalds 		skb->data_len  = 0;
141327a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
14141da177e4SLinus Torvalds 	}
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	return 0;
14171da177e4SLinus Torvalds }
1418b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds /**
14211da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
14221da177e4SLinus Torvalds  *	@skb: buffer to reallocate
14231da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
14241da177e4SLinus Torvalds  *
14251da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
14261da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
14271da177e4SLinus Torvalds  *	data from fragmented part.
14281da177e4SLinus Torvalds  *
14291da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
14301da177e4SLinus Torvalds  *
14311da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
14321da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
14331da177e4SLinus Torvalds  *
14341da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
14351da177e4SLinus Torvalds  *	reloaded after call to this function.
14361da177e4SLinus Torvalds  */
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
14391da177e4SLinus Torvalds  * when it is necessary.
14401da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
14411da177e4SLinus Torvalds  * 2. It may change skb pointers.
14421da177e4SLinus Torvalds  *
14431da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
14441da177e4SLinus Torvalds  */
14451da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
14461da177e4SLinus Torvalds {
14471da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
14481da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
14491da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
14501da177e4SLinus Torvalds 	 */
14514305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
14541da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
14551da177e4SLinus Torvalds 				     GFP_ATOMIC))
14561da177e4SLinus Torvalds 			return NULL;
14571da177e4SLinus Torvalds 	}
14581da177e4SLinus Torvalds 
145927a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
14601da177e4SLinus Torvalds 		BUG();
14611da177e4SLinus Torvalds 
14621da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
14631da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
14641da177e4SLinus Torvalds 	 */
146521dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
14661da177e4SLinus Torvalds 		goto pull_pages;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
14691da177e4SLinus Torvalds 	eat = delta;
14701da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
14719e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
14729e903e08SEric Dumazet 
14739e903e08SEric Dumazet 		if (size >= eat)
14741da177e4SLinus Torvalds 			goto pull_pages;
14759e903e08SEric Dumazet 		eat -= size;
14761da177e4SLinus Torvalds 	}
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
14791da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
14801da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
14811da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
14821da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
14831da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
14841da177e4SLinus Torvalds 	 */
14851da177e4SLinus Torvalds 	if (eat) {
14861da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
14871da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
14881da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 		do {
149109a62660SKris Katterjohn 			BUG_ON(!list);
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 			if (list->len <= eat) {
14941da177e4SLinus Torvalds 				/* Eaten as whole. */
14951da177e4SLinus Torvalds 				eat -= list->len;
14961da177e4SLinus Torvalds 				list = list->next;
14971da177e4SLinus Torvalds 				insp = list;
14981da177e4SLinus Torvalds 			} else {
14991da177e4SLinus Torvalds 				/* Eaten partially. */
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 				if (skb_shared(list)) {
15021da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
15031da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
15041da177e4SLinus Torvalds 					if (!clone)
15051da177e4SLinus Torvalds 						return NULL;
15061da177e4SLinus Torvalds 					insp = list->next;
15071da177e4SLinus Torvalds 					list = clone;
15081da177e4SLinus Torvalds 				} else {
15091da177e4SLinus Torvalds 					/* This may be pulled without
15101da177e4SLinus Torvalds 					 * problems. */
15111da177e4SLinus Torvalds 					insp = list;
15121da177e4SLinus Torvalds 				}
15131da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
15141da177e4SLinus Torvalds 					kfree_skb(clone);
15151da177e4SLinus Torvalds 					return NULL;
15161da177e4SLinus Torvalds 				}
15171da177e4SLinus Torvalds 				break;
15181da177e4SLinus Torvalds 			}
15191da177e4SLinus Torvalds 		} while (eat);
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 		/* Free pulled out fragments. */
15221da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
15231da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
15241da177e4SLinus Torvalds 			kfree_skb(list);
15251da177e4SLinus Torvalds 		}
15261da177e4SLinus Torvalds 		/* And insert new clone at head. */
15271da177e4SLinus Torvalds 		if (clone) {
15281da177e4SLinus Torvalds 			clone->next = list;
15291da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
15301da177e4SLinus Torvalds 		}
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds pull_pages:
15351da177e4SLinus Torvalds 	eat = delta;
15361da177e4SLinus Torvalds 	k = 0;
15371da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15389e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
15399e903e08SEric Dumazet 
15409e903e08SEric Dumazet 		if (size <= eat) {
1541ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
15429e903e08SEric Dumazet 			eat -= size;
15431da177e4SLinus Torvalds 		} else {
15441da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
15451da177e4SLinus Torvalds 			if (eat) {
15461da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
15479e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
15481da177e4SLinus Torvalds 				eat = 0;
15491da177e4SLinus Torvalds 			}
15501da177e4SLinus Torvalds 			k++;
15511da177e4SLinus Torvalds 		}
15521da177e4SLinus Torvalds 	}
15531da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	skb->tail     += delta;
15561da177e4SLinus Torvalds 	skb->data_len -= delta;
15571da177e4SLinus Torvalds 
155827a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
15591da177e4SLinus Torvalds }
1560b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
15611da177e4SLinus Torvalds 
156222019b17SEric Dumazet /**
156322019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
156422019b17SEric Dumazet  *	@skb: source skb
156522019b17SEric Dumazet  *	@offset: offset in source
156622019b17SEric Dumazet  *	@to: destination buffer
156722019b17SEric Dumazet  *	@len: number of bytes to copy
156822019b17SEric Dumazet  *
156922019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
157022019b17SEric Dumazet  *	destination buffer.
157122019b17SEric Dumazet  *
157222019b17SEric Dumazet  *	CAUTION ! :
157322019b17SEric Dumazet  *		If its prototype is ever changed,
157422019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
157522019b17SEric Dumazet  *		since it is called from BPF assembly code.
157622019b17SEric Dumazet  */
15771da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
15781da177e4SLinus Torvalds {
15791a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1580fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1581fbb398a8SDavid S. Miller 	int i, copy;
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
15841da177e4SLinus Torvalds 		goto fault;
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	/* Copy header. */
15871a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
15881da177e4SLinus Torvalds 		if (copy > len)
15891da177e4SLinus Torvalds 			copy = len;
1590d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
15911da177e4SLinus Torvalds 		if ((len -= copy) == 0)
15921da177e4SLinus Torvalds 			return 0;
15931da177e4SLinus Torvalds 		offset += copy;
15941da177e4SLinus Torvalds 		to     += copy;
15951da177e4SLinus Torvalds 	}
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15981a028e50SDavid S. Miller 		int end;
159951c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
16001da177e4SLinus Torvalds 
1601547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16021a028e50SDavid S. Miller 
160351c56b00SEric Dumazet 		end = start + skb_frag_size(f);
16041da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
16051da177e4SLinus Torvalds 			u8 *vaddr;
16061da177e4SLinus Torvalds 
16071da177e4SLinus Torvalds 			if (copy > len)
16081da177e4SLinus Torvalds 				copy = len;
16091da177e4SLinus Torvalds 
161051c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(f));
16111da177e4SLinus Torvalds 			memcpy(to,
161251c56b00SEric Dumazet 			       vaddr + f->page_offset + offset - start,
161351c56b00SEric Dumazet 			       copy);
161451c56b00SEric Dumazet 			kunmap_atomic(vaddr);
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 			if ((len -= copy) == 0)
16171da177e4SLinus Torvalds 				return 0;
16181da177e4SLinus Torvalds 			offset += copy;
16191da177e4SLinus Torvalds 			to     += copy;
16201da177e4SLinus Torvalds 		}
16211a028e50SDavid S. Miller 		start = end;
16221da177e4SLinus Torvalds 	}
16231da177e4SLinus Torvalds 
1624fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
16251a028e50SDavid S. Miller 		int end;
16261da177e4SLinus Torvalds 
1627547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16281a028e50SDavid S. Miller 
1629fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
16301da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
16311da177e4SLinus Torvalds 			if (copy > len)
16321da177e4SLinus Torvalds 				copy = len;
1633fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
16341da177e4SLinus Torvalds 				goto fault;
16351da177e4SLinus Torvalds 			if ((len -= copy) == 0)
16361da177e4SLinus Torvalds 				return 0;
16371da177e4SLinus Torvalds 			offset += copy;
16381da177e4SLinus Torvalds 			to     += copy;
16391da177e4SLinus Torvalds 		}
16401a028e50SDavid S. Miller 		start = end;
16411da177e4SLinus Torvalds 	}
1642a6686f2fSShirley Ma 
16431da177e4SLinus Torvalds 	if (!len)
16441da177e4SLinus Torvalds 		return 0;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds fault:
16471da177e4SLinus Torvalds 	return -EFAULT;
16481da177e4SLinus Torvalds }
1649b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
16501da177e4SLinus Torvalds 
16519c55e01cSJens Axboe /*
16529c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
16539c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
16549c55e01cSJens Axboe  */
16559c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
16569c55e01cSJens Axboe {
16578b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
16588b9d3728SJarek Poplawski }
16599c55e01cSJens Axboe 
1660a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
16614fb66994SJarek Poplawski 				   unsigned int *offset,
166218aafc62SEric Dumazet 				   struct sock *sk)
16638b9d3728SJarek Poplawski {
16645640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
16658b9d3728SJarek Poplawski 
16665640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
16678b9d3728SJarek Poplawski 		return NULL;
16684fb66994SJarek Poplawski 
16695640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
16704fb66994SJarek Poplawski 
16715640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
16725640f768SEric Dumazet 	       page_address(page) + *offset, *len);
16735640f768SEric Dumazet 	*offset = pfrag->offset;
16745640f768SEric Dumazet 	pfrag->offset += *len;
16754fb66994SJarek Poplawski 
16765640f768SEric Dumazet 	return pfrag->page;
16779c55e01cSJens Axboe }
16789c55e01cSJens Axboe 
167941c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
168041c73a0dSEric Dumazet 			     struct page *page,
168141c73a0dSEric Dumazet 			     unsigned int offset)
168241c73a0dSEric Dumazet {
168341c73a0dSEric Dumazet 	return	spd->nr_pages &&
168441c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
168541c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
168641c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
168741c73a0dSEric Dumazet }
168841c73a0dSEric Dumazet 
16899c55e01cSJens Axboe /*
16909c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
16919c55e01cSJens Axboe  */
1692a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
169335f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
16944fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
169518aafc62SEric Dumazet 			  bool linear,
16967a67e56fSJarek Poplawski 			  struct sock *sk)
16979c55e01cSJens Axboe {
169841c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1699a108d5f3SDavid S. Miller 		return true;
17009c55e01cSJens Axboe 
17018b9d3728SJarek Poplawski 	if (linear) {
170218aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
17038b9d3728SJarek Poplawski 		if (!page)
1704a108d5f3SDavid S. Miller 			return true;
170541c73a0dSEric Dumazet 	}
170641c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
170741c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
1708a108d5f3SDavid S. Miller 		return false;
170941c73a0dSEric Dumazet 	}
17108b9d3728SJarek Poplawski 	get_page(page);
17119c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
17124fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
17139c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
17149c55e01cSJens Axboe 	spd->nr_pages++;
17158b9d3728SJarek Poplawski 
1716a108d5f3SDavid S. Miller 	return false;
17179c55e01cSJens Axboe }
17189c55e01cSJens Axboe 
1719a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
17202870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
172118aafc62SEric Dumazet 			     unsigned int *len,
1722d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
172335f3d14dSJens Axboe 			     struct sock *sk,
172435f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
17259c55e01cSJens Axboe {
17262870c43dSOctavian Purdila 	if (!*len)
1727a108d5f3SDavid S. Miller 		return true;
17289c55e01cSJens Axboe 
17292870c43dSOctavian Purdila 	/* skip this segment if already processed */
17302870c43dSOctavian Purdila 	if (*off >= plen) {
17312870c43dSOctavian Purdila 		*off -= plen;
1732a108d5f3SDavid S. Miller 		return false;
17332870c43dSOctavian Purdila 	}
17342870c43dSOctavian Purdila 
17352870c43dSOctavian Purdila 	/* ignore any bits we already processed */
17369ca1b22dSEric Dumazet 	poff += *off;
17379ca1b22dSEric Dumazet 	plen -= *off;
17382870c43dSOctavian Purdila 	*off = 0;
17392870c43dSOctavian Purdila 
174018aafc62SEric Dumazet 	do {
174118aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
17422870c43dSOctavian Purdila 
174318aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
174418aafc62SEric Dumazet 				  linear, sk))
1745a108d5f3SDavid S. Miller 			return true;
174618aafc62SEric Dumazet 		poff += flen;
174718aafc62SEric Dumazet 		plen -= flen;
17482870c43dSOctavian Purdila 		*len -= flen;
174918aafc62SEric Dumazet 	} while (*len && plen);
17502870c43dSOctavian Purdila 
1751a108d5f3SDavid S. Miller 	return false;
1752db43a282SOctavian Purdila }
17539c55e01cSJens Axboe 
17549c55e01cSJens Axboe /*
1755a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
17562870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
17579c55e01cSJens Axboe  */
1758a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
175935f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
176035f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
17612870c43dSOctavian Purdila {
17622870c43dSOctavian Purdila 	int seg;
17639c55e01cSJens Axboe 
17641d0c0b32SEric Dumazet 	/* map the linear part :
17652996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
17662996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
17672996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
17689c55e01cSJens Axboe 	 */
17692870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
17702870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
17712870c43dSOctavian Purdila 			     skb_headlen(skb),
177218aafc62SEric Dumazet 			     offset, len, spd,
17733a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
17741d0c0b32SEric Dumazet 			     sk, pipe))
1775a108d5f3SDavid S. Miller 		return true;
17769c55e01cSJens Axboe 
17779c55e01cSJens Axboe 	/*
17789c55e01cSJens Axboe 	 * then map the fragments
17799c55e01cSJens Axboe 	 */
17809c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
17819c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
17829c55e01cSJens Axboe 
1783ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
17849e903e08SEric Dumazet 				     f->page_offset, skb_frag_size(f),
178518aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
1786a108d5f3SDavid S. Miller 			return true;
17879c55e01cSJens Axboe 	}
17889c55e01cSJens Axboe 
1789a108d5f3SDavid S. Miller 	return false;
17909c55e01cSJens Axboe }
17919c55e01cSJens Axboe 
17929c55e01cSJens Axboe /*
17939c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
17949c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
17959c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
17969c55e01cSJens Axboe  * handle that cleanly.
17979c55e01cSJens Axboe  */
17988b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
17999c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
18009c55e01cSJens Axboe 		    unsigned int flags)
18019c55e01cSJens Axboe {
180241c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
180341c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
18049c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
18059c55e01cSJens Axboe 		.pages = pages,
18069c55e01cSJens Axboe 		.partial = partial,
1807047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
18089c55e01cSJens Axboe 		.flags = flags,
180928a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
18109c55e01cSJens Axboe 		.spd_release = sock_spd_release,
18119c55e01cSJens Axboe 	};
1812fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
18137a67e56fSJarek Poplawski 	struct sock *sk = skb->sk;
181435f3d14dSJens Axboe 	int ret = 0;
181535f3d14dSJens Axboe 
18169c55e01cSJens Axboe 	/*
18179c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
18189c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
18199c55e01cSJens Axboe 	 */
182035f3d14dSJens Axboe 	if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
18219c55e01cSJens Axboe 		goto done;
18229c55e01cSJens Axboe 	else if (!tlen)
18239c55e01cSJens Axboe 		goto done;
18249c55e01cSJens Axboe 
18259c55e01cSJens Axboe 	/*
18269c55e01cSJens Axboe 	 * now see if we have a frag_list to map
18279c55e01cSJens Axboe 	 */
1828fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
1829fbb398a8SDavid S. Miller 		if (!tlen)
18309c55e01cSJens Axboe 			break;
183135f3d14dSJens Axboe 		if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1832fbb398a8SDavid S. Miller 			break;
18339c55e01cSJens Axboe 	}
18349c55e01cSJens Axboe 
18359c55e01cSJens Axboe done:
18369c55e01cSJens Axboe 	if (spd.nr_pages) {
18379c55e01cSJens Axboe 		/*
18389c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
18399c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
18409c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
18419c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
18429c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
18439c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
18449c55e01cSJens Axboe 		 * and networking will grab the socket lock.
18459c55e01cSJens Axboe 		 */
1846293ad604SOctavian Purdila 		release_sock(sk);
18479c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1848293ad604SOctavian Purdila 		lock_sock(sk);
18499c55e01cSJens Axboe 	}
18509c55e01cSJens Axboe 
185135f3d14dSJens Axboe 	return ret;
18529c55e01cSJens Axboe }
18539c55e01cSJens Axboe 
1854357b40a1SHerbert Xu /**
1855357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1856357b40a1SHerbert Xu  *	@skb: destination buffer
1857357b40a1SHerbert Xu  *	@offset: offset in destination
1858357b40a1SHerbert Xu  *	@from: source buffer
1859357b40a1SHerbert Xu  *	@len: number of bytes to copy
1860357b40a1SHerbert Xu  *
1861357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1862357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1863357b40a1SHerbert Xu  *	traversing fragment lists and such.
1864357b40a1SHerbert Xu  */
1865357b40a1SHerbert Xu 
18660c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1867357b40a1SHerbert Xu {
18681a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1869fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1870fbb398a8SDavid S. Miller 	int i, copy;
1871357b40a1SHerbert Xu 
1872357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1873357b40a1SHerbert Xu 		goto fault;
1874357b40a1SHerbert Xu 
18751a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1876357b40a1SHerbert Xu 		if (copy > len)
1877357b40a1SHerbert Xu 			copy = len;
187827d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1879357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1880357b40a1SHerbert Xu 			return 0;
1881357b40a1SHerbert Xu 		offset += copy;
1882357b40a1SHerbert Xu 		from += copy;
1883357b40a1SHerbert Xu 	}
1884357b40a1SHerbert Xu 
1885357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1886357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
18871a028e50SDavid S. Miller 		int end;
1888357b40a1SHerbert Xu 
1889547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
18901a028e50SDavid S. Miller 
18919e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
1892357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1893357b40a1SHerbert Xu 			u8 *vaddr;
1894357b40a1SHerbert Xu 
1895357b40a1SHerbert Xu 			if (copy > len)
1896357b40a1SHerbert Xu 				copy = len;
1897357b40a1SHerbert Xu 
189851c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
18991a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
19001a028e50SDavid S. Miller 			       from, copy);
190151c56b00SEric Dumazet 			kunmap_atomic(vaddr);
1902357b40a1SHerbert Xu 
1903357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1904357b40a1SHerbert Xu 				return 0;
1905357b40a1SHerbert Xu 			offset += copy;
1906357b40a1SHerbert Xu 			from += copy;
1907357b40a1SHerbert Xu 		}
19081a028e50SDavid S. Miller 		start = end;
1909357b40a1SHerbert Xu 	}
1910357b40a1SHerbert Xu 
1911fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
19121a028e50SDavid S. Miller 		int end;
1913357b40a1SHerbert Xu 
1914547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19151a028e50SDavid S. Miller 
1916fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
1917357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1918357b40a1SHerbert Xu 			if (copy > len)
1919357b40a1SHerbert Xu 				copy = len;
1920fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
19211a028e50SDavid S. Miller 					   from, copy))
1922357b40a1SHerbert Xu 				goto fault;
1923357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1924357b40a1SHerbert Xu 				return 0;
1925357b40a1SHerbert Xu 			offset += copy;
1926357b40a1SHerbert Xu 			from += copy;
1927357b40a1SHerbert Xu 		}
19281a028e50SDavid S. Miller 		start = end;
1929357b40a1SHerbert Xu 	}
1930357b40a1SHerbert Xu 	if (!len)
1931357b40a1SHerbert Xu 		return 0;
1932357b40a1SHerbert Xu 
1933357b40a1SHerbert Xu fault:
1934357b40a1SHerbert Xu 	return -EFAULT;
1935357b40a1SHerbert Xu }
1936357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1937357b40a1SHerbert Xu 
19381da177e4SLinus Torvalds /* Checksum skb data. */
19392817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
19402817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
19411da177e4SLinus Torvalds {
19421a028e50SDavid S. Miller 	int start = skb_headlen(skb);
19431a028e50SDavid S. Miller 	int i, copy = start - offset;
1944fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
19451da177e4SLinus Torvalds 	int pos = 0;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	/* Checksum header. */
19481da177e4SLinus Torvalds 	if (copy > 0) {
19491da177e4SLinus Torvalds 		if (copy > len)
19501da177e4SLinus Torvalds 			copy = len;
19512817a336SDaniel Borkmann 		csum = ops->update(skb->data + offset, copy, csum);
19521da177e4SLinus Torvalds 		if ((len -= copy) == 0)
19531da177e4SLinus Torvalds 			return csum;
19541da177e4SLinus Torvalds 		offset += copy;
19551da177e4SLinus Torvalds 		pos	= copy;
19561da177e4SLinus Torvalds 	}
19571da177e4SLinus Torvalds 
19581da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
19591a028e50SDavid S. Miller 		int end;
196051c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
19611da177e4SLinus Torvalds 
1962547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19631a028e50SDavid S. Miller 
196451c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
19651da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
196644bb9363SAl Viro 			__wsum csum2;
19671da177e4SLinus Torvalds 			u8 *vaddr;
19681da177e4SLinus Torvalds 
19691da177e4SLinus Torvalds 			if (copy > len)
19701da177e4SLinus Torvalds 				copy = len;
197151c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
19722817a336SDaniel Borkmann 			csum2 = ops->update(vaddr + frag->page_offset +
19731a028e50SDavid S. Miller 					    offset - start, copy, 0);
197451c56b00SEric Dumazet 			kunmap_atomic(vaddr);
19752817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
19761da177e4SLinus Torvalds 			if (!(len -= copy))
19771da177e4SLinus Torvalds 				return csum;
19781da177e4SLinus Torvalds 			offset += copy;
19791da177e4SLinus Torvalds 			pos    += copy;
19801da177e4SLinus Torvalds 		}
19811a028e50SDavid S. Miller 		start = end;
19821da177e4SLinus Torvalds 	}
19831da177e4SLinus Torvalds 
1984fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
19851a028e50SDavid S. Miller 		int end;
19861da177e4SLinus Torvalds 
1987547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19881a028e50SDavid S. Miller 
1989fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
19901da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
19915f92a738SAl Viro 			__wsum csum2;
19921da177e4SLinus Torvalds 			if (copy > len)
19931da177e4SLinus Torvalds 				copy = len;
19942817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
19952817a336SDaniel Borkmann 					       copy, 0, ops);
19962817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
19971da177e4SLinus Torvalds 			if ((len -= copy) == 0)
19981da177e4SLinus Torvalds 				return csum;
19991da177e4SLinus Torvalds 			offset += copy;
20001da177e4SLinus Torvalds 			pos    += copy;
20011da177e4SLinus Torvalds 		}
20021a028e50SDavid S. Miller 		start = end;
20031da177e4SLinus Torvalds 	}
200409a62660SKris Katterjohn 	BUG_ON(len);
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	return csum;
20071da177e4SLinus Torvalds }
20082817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
20092817a336SDaniel Borkmann 
20102817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
20112817a336SDaniel Borkmann 		    int len, __wsum csum)
20122817a336SDaniel Borkmann {
20132817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2014cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
20152817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
20162817a336SDaniel Borkmann 	};
20172817a336SDaniel Borkmann 
20182817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
20192817a336SDaniel Borkmann }
2020b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds /* Both of above in one bottle. */
20231da177e4SLinus Torvalds 
202481d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
202581d77662SAl Viro 				    u8 *to, int len, __wsum csum)
20261da177e4SLinus Torvalds {
20271a028e50SDavid S. Miller 	int start = skb_headlen(skb);
20281a028e50SDavid S. Miller 	int i, copy = start - offset;
2029fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
20301da177e4SLinus Torvalds 	int pos = 0;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	/* Copy header. */
20331da177e4SLinus Torvalds 	if (copy > 0) {
20341da177e4SLinus Torvalds 		if (copy > len)
20351da177e4SLinus Torvalds 			copy = len;
20361da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
20371da177e4SLinus Torvalds 						 copy, csum);
20381da177e4SLinus Torvalds 		if ((len -= copy) == 0)
20391da177e4SLinus Torvalds 			return csum;
20401da177e4SLinus Torvalds 		offset += copy;
20411da177e4SLinus Torvalds 		to     += copy;
20421da177e4SLinus Torvalds 		pos	= copy;
20431da177e4SLinus Torvalds 	}
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20461a028e50SDavid S. Miller 		int end;
20471da177e4SLinus Torvalds 
2048547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20491a028e50SDavid S. Miller 
20509e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
20511da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
20525084205fSAl Viro 			__wsum csum2;
20531da177e4SLinus Torvalds 			u8 *vaddr;
20541da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 			if (copy > len)
20571da177e4SLinus Torvalds 				copy = len;
205851c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
20591da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
20601a028e50SDavid S. Miller 							  frag->page_offset +
20611a028e50SDavid S. Miller 							  offset - start, to,
20621a028e50SDavid S. Miller 							  copy, 0);
206351c56b00SEric Dumazet 			kunmap_atomic(vaddr);
20641da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
20651da177e4SLinus Torvalds 			if (!(len -= copy))
20661da177e4SLinus Torvalds 				return csum;
20671da177e4SLinus Torvalds 			offset += copy;
20681da177e4SLinus Torvalds 			to     += copy;
20691da177e4SLinus Torvalds 			pos    += copy;
20701da177e4SLinus Torvalds 		}
20711a028e50SDavid S. Miller 		start = end;
20721da177e4SLinus Torvalds 	}
20731da177e4SLinus Torvalds 
2074fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
207581d77662SAl Viro 		__wsum csum2;
20761a028e50SDavid S. Miller 		int end;
20771da177e4SLinus Torvalds 
2078547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20791a028e50SDavid S. Miller 
2080fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
20811da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
20821da177e4SLinus Torvalds 			if (copy > len)
20831da177e4SLinus Torvalds 				copy = len;
2084fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
20851a028e50SDavid S. Miller 						       offset - start,
20861da177e4SLinus Torvalds 						       to, copy, 0);
20871da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
20881da177e4SLinus Torvalds 			if ((len -= copy) == 0)
20891da177e4SLinus Torvalds 				return csum;
20901da177e4SLinus Torvalds 			offset += copy;
20911da177e4SLinus Torvalds 			to     += copy;
20921da177e4SLinus Torvalds 			pos    += copy;
20931da177e4SLinus Torvalds 		}
20941a028e50SDavid S. Miller 		start = end;
20951da177e4SLinus Torvalds 	}
209609a62660SKris Katterjohn 	BUG_ON(len);
20971da177e4SLinus Torvalds 	return csum;
20981da177e4SLinus Torvalds }
2099b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
21001da177e4SLinus Torvalds 
2101af2806f8SThomas Graf  /**
2102af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2103af2806f8SThomas Graf  *	@from: source buffer
2104af2806f8SThomas Graf  *
2105af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2106af2806f8SThomas Graf  *	into skb_zerocopy().
2107af2806f8SThomas Graf  */
2108af2806f8SThomas Graf unsigned int
2109af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2110af2806f8SThomas Graf {
2111af2806f8SThomas Graf 	unsigned int hlen = 0;
2112af2806f8SThomas Graf 
2113af2806f8SThomas Graf 	if (!from->head_frag ||
2114af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2115af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2116af2806f8SThomas Graf 		hlen = skb_headlen(from);
2117af2806f8SThomas Graf 
2118af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2119af2806f8SThomas Graf 		hlen = from->len;
2120af2806f8SThomas Graf 
2121af2806f8SThomas Graf 	return hlen;
2122af2806f8SThomas Graf }
2123af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2124af2806f8SThomas Graf 
2125af2806f8SThomas Graf /**
2126af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2127af2806f8SThomas Graf  *	@to: destination buffer
21287fceb4deSMasanari Iida  *	@from: source buffer
2129af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2130af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2131af2806f8SThomas Graf  *
2132af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2133af2806f8SThomas Graf  *	to the frags in the source buffer.
2134af2806f8SThomas Graf  *
2135af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2136af2806f8SThomas Graf  *	headroom in the `to` buffer.
213736d5fe6aSZoltan Kiss  *
213836d5fe6aSZoltan Kiss  *	Return value:
213936d5fe6aSZoltan Kiss  *	0: everything is OK
214036d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
214136d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2142af2806f8SThomas Graf  */
214336d5fe6aSZoltan Kiss int
214436d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2145af2806f8SThomas Graf {
2146af2806f8SThomas Graf 	int i, j = 0;
2147af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
214836d5fe6aSZoltan Kiss 	int ret;
2149af2806f8SThomas Graf 	struct page *page;
2150af2806f8SThomas Graf 	unsigned int offset;
2151af2806f8SThomas Graf 
2152af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2153af2806f8SThomas Graf 
2154af2806f8SThomas Graf 	/* dont bother with small payloads */
215536d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
215636d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2157af2806f8SThomas Graf 
2158af2806f8SThomas Graf 	if (hlen) {
215936d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
216036d5fe6aSZoltan Kiss 		if (unlikely(ret))
216136d5fe6aSZoltan Kiss 			return ret;
2162af2806f8SThomas Graf 		len -= hlen;
2163af2806f8SThomas Graf 	} else {
2164af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2165af2806f8SThomas Graf 		if (plen) {
2166af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2167af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2168af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2169af2806f8SThomas Graf 			get_page(page);
2170af2806f8SThomas Graf 			j = 1;
2171af2806f8SThomas Graf 			len -= plen;
2172af2806f8SThomas Graf 		}
2173af2806f8SThomas Graf 	}
2174af2806f8SThomas Graf 
2175af2806f8SThomas Graf 	to->truesize += len + plen;
2176af2806f8SThomas Graf 	to->len += len + plen;
2177af2806f8SThomas Graf 	to->data_len += len + plen;
2178af2806f8SThomas Graf 
217936d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
218036d5fe6aSZoltan Kiss 		skb_tx_error(from);
218136d5fe6aSZoltan Kiss 		return -ENOMEM;
218236d5fe6aSZoltan Kiss 	}
218336d5fe6aSZoltan Kiss 
2184af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2185af2806f8SThomas Graf 		if (!len)
2186af2806f8SThomas Graf 			break;
2187af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2188af2806f8SThomas Graf 		skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2189af2806f8SThomas Graf 		len -= skb_shinfo(to)->frags[j].size;
2190af2806f8SThomas Graf 		skb_frag_ref(to, j);
2191af2806f8SThomas Graf 		j++;
2192af2806f8SThomas Graf 	}
2193af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
219436d5fe6aSZoltan Kiss 
219536d5fe6aSZoltan Kiss 	return 0;
2196af2806f8SThomas Graf }
2197af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
2198af2806f8SThomas Graf 
21991da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
22001da177e4SLinus Torvalds {
2201d3bc23e7SAl Viro 	__wsum csum;
22021da177e4SLinus Torvalds 	long csstart;
22031da177e4SLinus Torvalds 
220484fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
220555508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
22061da177e4SLinus Torvalds 	else
22071da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
22081da177e4SLinus Torvalds 
220909a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
22101da177e4SLinus Torvalds 
2211d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
22121da177e4SLinus Torvalds 
22131da177e4SLinus Torvalds 	csum = 0;
22141da177e4SLinus Torvalds 	if (csstart != skb->len)
22151da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
22161da177e4SLinus Torvalds 					      skb->len - csstart, 0);
22171da177e4SLinus Torvalds 
221884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2219ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
22201da177e4SLinus Torvalds 
2221d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
22221da177e4SLinus Torvalds 	}
22231da177e4SLinus Torvalds }
2224b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds /**
22271da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
22281da177e4SLinus Torvalds  *	@list: list to dequeue from
22291da177e4SLinus Torvalds  *
22301da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
22311da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
22321da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
22331da177e4SLinus Torvalds  */
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
22361da177e4SLinus Torvalds {
22371da177e4SLinus Torvalds 	unsigned long flags;
22381da177e4SLinus Torvalds 	struct sk_buff *result;
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
22411da177e4SLinus Torvalds 	result = __skb_dequeue(list);
22421da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
22431da177e4SLinus Torvalds 	return result;
22441da177e4SLinus Torvalds }
2245b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
22461da177e4SLinus Torvalds 
22471da177e4SLinus Torvalds /**
22481da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
22491da177e4SLinus Torvalds  *	@list: list to dequeue from
22501da177e4SLinus Torvalds  *
22511da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
22521da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
22531da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
22541da177e4SLinus Torvalds  */
22551da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
22561da177e4SLinus Torvalds {
22571da177e4SLinus Torvalds 	unsigned long flags;
22581da177e4SLinus Torvalds 	struct sk_buff *result;
22591da177e4SLinus Torvalds 
22601da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
22611da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
22621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
22631da177e4SLinus Torvalds 	return result;
22641da177e4SLinus Torvalds }
2265b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
22661da177e4SLinus Torvalds 
22671da177e4SLinus Torvalds /**
22681da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
22691da177e4SLinus Torvalds  *	@list: list to empty
22701da177e4SLinus Torvalds  *
22711da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
22721da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
22731da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
22741da177e4SLinus Torvalds  */
22751da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
22761da177e4SLinus Torvalds {
22771da177e4SLinus Torvalds 	struct sk_buff *skb;
22781da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
22791da177e4SLinus Torvalds 		kfree_skb(skb);
22801da177e4SLinus Torvalds }
2281b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds /**
22841da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
22851da177e4SLinus Torvalds  *	@list: list to use
22861da177e4SLinus Torvalds  *	@newsk: buffer to queue
22871da177e4SLinus Torvalds  *
22881da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
22891da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
22901da177e4SLinus Torvalds  *	safely.
22911da177e4SLinus Torvalds  *
22921da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
22931da177e4SLinus Torvalds  */
22941da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
22951da177e4SLinus Torvalds {
22961da177e4SLinus Torvalds 	unsigned long flags;
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
22991da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
23001da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23011da177e4SLinus Torvalds }
2302b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
23031da177e4SLinus Torvalds 
23041da177e4SLinus Torvalds /**
23051da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
23061da177e4SLinus Torvalds  *	@list: list to use
23071da177e4SLinus Torvalds  *	@newsk: buffer to queue
23081da177e4SLinus Torvalds  *
23091da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
23101da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
23111da177e4SLinus Torvalds  *	safely.
23121da177e4SLinus Torvalds  *
23131da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23141da177e4SLinus Torvalds  */
23151da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
23161da177e4SLinus Torvalds {
23171da177e4SLinus Torvalds 	unsigned long flags;
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23201da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
23211da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23221da177e4SLinus Torvalds }
2323b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
23248728b834SDavid S. Miller 
23251da177e4SLinus Torvalds /**
23261da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
23271da177e4SLinus Torvalds  *	@skb: buffer to remove
23288728b834SDavid S. Miller  *	@list: list to use
23291da177e4SLinus Torvalds  *
23308728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
23318728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
23321da177e4SLinus Torvalds  *
23338728b834SDavid S. Miller  *	You must know what list the SKB is on.
23341da177e4SLinus Torvalds  */
23358728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
23361da177e4SLinus Torvalds {
23371da177e4SLinus Torvalds 	unsigned long flags;
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23408728b834SDavid S. Miller 	__skb_unlink(skb, list);
23411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23421da177e4SLinus Torvalds }
2343b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds /**
23461da177e4SLinus Torvalds  *	skb_append	-	append a buffer
23471da177e4SLinus Torvalds  *	@old: buffer to insert after
23481da177e4SLinus Torvalds  *	@newsk: buffer to insert
23498728b834SDavid S. Miller  *	@list: list to use
23501da177e4SLinus Torvalds  *
23511da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
23521da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
23531da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23541da177e4SLinus Torvalds  */
23558728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
23561da177e4SLinus Torvalds {
23571da177e4SLinus Torvalds 	unsigned long flags;
23581da177e4SLinus Torvalds 
23598728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
23607de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
23618728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
23621da177e4SLinus Torvalds }
2363b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds /**
23661da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
23671da177e4SLinus Torvalds  *	@old: buffer to insert before
23681da177e4SLinus Torvalds  *	@newsk: buffer to insert
23698728b834SDavid S. Miller  *	@list: list to use
23701da177e4SLinus Torvalds  *
23718728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
23728728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
23738728b834SDavid S. Miller  *	calls.
23748728b834SDavid S. Miller  *
23751da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23761da177e4SLinus Torvalds  */
23778728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
23781da177e4SLinus Torvalds {
23791da177e4SLinus Torvalds 	unsigned long flags;
23801da177e4SLinus Torvalds 
23818728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
23828728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
23838728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
23841da177e4SLinus Torvalds }
2385b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
23861da177e4SLinus Torvalds 
23871da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
23881da177e4SLinus Torvalds 					   struct sk_buff* skb1,
23891da177e4SLinus Torvalds 					   const u32 len, const int pos)
23901da177e4SLinus Torvalds {
23911da177e4SLinus Torvalds 	int i;
23921da177e4SLinus Torvalds 
2393d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2394d626f62bSArnaldo Carvalho de Melo 					 pos - len);
23951da177e4SLinus Torvalds 	/* And move data appendix as is. */
23961da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
23971da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
24001da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
24011da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
24021da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
24031da177e4SLinus Torvalds 	skb->data_len		   = 0;
24041da177e4SLinus Torvalds 	skb->len		   = len;
240527a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
24091da177e4SLinus Torvalds 				       struct sk_buff* skb1,
24101da177e4SLinus Torvalds 				       const u32 len, int pos)
24111da177e4SLinus Torvalds {
24121da177e4SLinus Torvalds 	int i, k = 0;
24131da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
24161da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
24171da177e4SLinus Torvalds 	skb->len		  = len;
24181da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
24219e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 		if (pos + size > len) {
24241da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 			if (pos < len) {
24271da177e4SLinus Torvalds 				/* Split frag.
24281da177e4SLinus Torvalds 				 * We have two variants in this case:
24291da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
24301da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
24311da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
24321da177e4SLinus Torvalds 				 *    where splitting is expensive.
24331da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
24341da177e4SLinus Torvalds 				 */
2435ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
24361da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
24379e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
24389e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
24391da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
24401da177e4SLinus Torvalds 			}
24411da177e4SLinus Torvalds 			k++;
24421da177e4SLinus Torvalds 		} else
24431da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
24441da177e4SLinus Torvalds 		pos += size;
24451da177e4SLinus Torvalds 	}
24461da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
24471da177e4SLinus Torvalds }
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds /**
24501da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
24511da177e4SLinus Torvalds  * @skb: the buffer to split
24521da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
24531da177e4SLinus Torvalds  * @len: new length for skb
24541da177e4SLinus Torvalds  */
24551da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
24561da177e4SLinus Torvalds {
24571da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
24581da177e4SLinus Torvalds 
245968534c68SAmerigo Wang 	skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
24601da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
24611da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
24621da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
24631da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
24641da177e4SLinus Torvalds }
2465b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
24661da177e4SLinus Torvalds 
24679f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
24689f782db3SIlpo Järvinen  *
24699f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
24709f782db3SIlpo Järvinen  */
2471832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2472832d11c5SIlpo Järvinen {
24730ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2474832d11c5SIlpo Järvinen }
2475832d11c5SIlpo Järvinen 
2476832d11c5SIlpo Järvinen /**
2477832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2478832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2479832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2480832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2481832d11c5SIlpo Järvinen  *
2482832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
248320e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2484832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2485832d11c5SIlpo Järvinen  *
2486832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2487832d11c5SIlpo Järvinen  *
2488832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2489832d11c5SIlpo Järvinen  * to have non-paged data as well.
2490832d11c5SIlpo Järvinen  *
2491832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2492832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2493832d11c5SIlpo Järvinen  */
2494832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2495832d11c5SIlpo Järvinen {
2496832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2497832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2498832d11c5SIlpo Järvinen 
2499832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2500832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2501832d11c5SIlpo Järvinen 
2502832d11c5SIlpo Järvinen 	todo = shiftlen;
2503832d11c5SIlpo Järvinen 	from = 0;
2504832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2505832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2506832d11c5SIlpo Järvinen 
2507832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2508832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2509832d11c5SIlpo Järvinen 	 */
2510832d11c5SIlpo Järvinen 	if (!to ||
2511ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2512ea2ab693SIan Campbell 			      fragfrom->page_offset)) {
2513832d11c5SIlpo Järvinen 		merge = -1;
2514832d11c5SIlpo Järvinen 	} else {
2515832d11c5SIlpo Järvinen 		merge = to - 1;
2516832d11c5SIlpo Järvinen 
25179e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
2518832d11c5SIlpo Järvinen 		if (todo < 0) {
2519832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2520832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2521832d11c5SIlpo Järvinen 				return 0;
2522832d11c5SIlpo Järvinen 
25239f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
25249f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2525832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2526832d11c5SIlpo Järvinen 
25279e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
25289e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
2529832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2530832d11c5SIlpo Järvinen 
2531832d11c5SIlpo Järvinen 			goto onlymerged;
2532832d11c5SIlpo Järvinen 		}
2533832d11c5SIlpo Järvinen 
2534832d11c5SIlpo Järvinen 		from++;
2535832d11c5SIlpo Järvinen 	}
2536832d11c5SIlpo Järvinen 
2537832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2538832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2539832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2540832d11c5SIlpo Järvinen 		return 0;
2541832d11c5SIlpo Järvinen 
2542832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2543832d11c5SIlpo Järvinen 		return 0;
2544832d11c5SIlpo Järvinen 
2545832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2546832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2547832d11c5SIlpo Järvinen 			return 0;
2548832d11c5SIlpo Järvinen 
2549832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2550832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2551832d11c5SIlpo Järvinen 
25529e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
2553832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
25549e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
2555832d11c5SIlpo Järvinen 			from++;
2556832d11c5SIlpo Järvinen 			to++;
2557832d11c5SIlpo Järvinen 
2558832d11c5SIlpo Järvinen 		} else {
2559ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
2560832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2561832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
25629e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
2563832d11c5SIlpo Järvinen 
2564832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
25659e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
2566832d11c5SIlpo Järvinen 			todo = 0;
2567832d11c5SIlpo Järvinen 
2568832d11c5SIlpo Järvinen 			to++;
2569832d11c5SIlpo Järvinen 			break;
2570832d11c5SIlpo Järvinen 		}
2571832d11c5SIlpo Järvinen 	}
2572832d11c5SIlpo Järvinen 
2573832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2574832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2575832d11c5SIlpo Järvinen 
2576832d11c5SIlpo Järvinen 	if (merge >= 0) {
2577832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2578832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2579832d11c5SIlpo Järvinen 
25809e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2581ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
2582832d11c5SIlpo Järvinen 	}
2583832d11c5SIlpo Järvinen 
2584832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2585832d11c5SIlpo Järvinen 	to = 0;
2586832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2587832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2588832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2589832d11c5SIlpo Järvinen 
2590832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2591832d11c5SIlpo Järvinen 
2592832d11c5SIlpo Järvinen onlymerged:
2593832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2594832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2595832d11c5SIlpo Järvinen 	 */
2596832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2597832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2598832d11c5SIlpo Järvinen 
2599832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2600832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2601832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2602832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2603832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2604832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2605832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2606832d11c5SIlpo Järvinen 
2607832d11c5SIlpo Järvinen 	return shiftlen;
2608832d11c5SIlpo Järvinen }
2609832d11c5SIlpo Järvinen 
2610677e90edSThomas Graf /**
2611677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2612677e90edSThomas Graf  * @skb: the buffer to read
2613677e90edSThomas Graf  * @from: lower offset of data to be read
2614677e90edSThomas Graf  * @to: upper offset of data to be read
2615677e90edSThomas Graf  * @st: state variable
2616677e90edSThomas Graf  *
2617677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2618677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2619677e90edSThomas Graf  */
2620677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2621677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2622677e90edSThomas Graf {
2623677e90edSThomas Graf 	st->lower_offset = from;
2624677e90edSThomas Graf 	st->upper_offset = to;
2625677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2626677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2627677e90edSThomas Graf 	st->frag_data = NULL;
2628677e90edSThomas Graf }
2629b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2630677e90edSThomas Graf 
2631677e90edSThomas Graf /**
2632677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2633677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2634677e90edSThomas Graf  * @data: destination pointer for data to be returned
2635677e90edSThomas Graf  * @st: state variable
2636677e90edSThomas Graf  *
2637bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
2638677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2639bc32383cSMathias Krause  * the head of the data block to @data and returns the length
2640677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2641677e90edSThomas Graf  * offset has been reached.
2642677e90edSThomas Graf  *
2643677e90edSThomas Graf  * The caller is not required to consume all of the data
2644bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
2645677e90edSThomas Graf  * of bytes already consumed and the next call to
2646677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2647677e90edSThomas Graf  *
264825985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
2649677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
2650677e90edSThomas Graf  *       reads of potentially non linear data.
2651677e90edSThomas Graf  *
2652bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2653677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2654677e90edSThomas Graf  *       a stack for this purpose.
2655677e90edSThomas Graf  */
2656677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2657677e90edSThomas Graf 			  struct skb_seq_state *st)
2658677e90edSThomas Graf {
2659677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2660677e90edSThomas Graf 	skb_frag_t *frag;
2661677e90edSThomas Graf 
2662aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
2663aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
2664aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
2665aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
2666aeb193eaSWedson Almeida Filho 		}
2667677e90edSThomas Graf 		return 0;
2668aeb193eaSWedson Almeida Filho 	}
2669677e90edSThomas Graf 
2670677e90edSThomas Graf next_skb:
267195e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2672677e90edSThomas Graf 
2673995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
267495e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2675677e90edSThomas Graf 		return block_limit - abs_offset;
2676677e90edSThomas Graf 	}
2677677e90edSThomas Graf 
2678677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2679677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2680677e90edSThomas Graf 
2681677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2682677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
26839e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
2684677e90edSThomas Graf 
2685677e90edSThomas Graf 		if (abs_offset < block_limit) {
2686677e90edSThomas Graf 			if (!st->frag_data)
268751c56b00SEric Dumazet 				st->frag_data = kmap_atomic(skb_frag_page(frag));
2688677e90edSThomas Graf 
2689677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2690677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2691677e90edSThomas Graf 
2692677e90edSThomas Graf 			return block_limit - abs_offset;
2693677e90edSThomas Graf 		}
2694677e90edSThomas Graf 
2695677e90edSThomas Graf 		if (st->frag_data) {
269651c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
2697677e90edSThomas Graf 			st->frag_data = NULL;
2698677e90edSThomas Graf 		}
2699677e90edSThomas Graf 
2700677e90edSThomas Graf 		st->frag_idx++;
27019e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
2702677e90edSThomas Graf 	}
2703677e90edSThomas Graf 
27045b5a60daSOlaf Kirch 	if (st->frag_data) {
270551c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
27065b5a60daSOlaf Kirch 		st->frag_data = NULL;
27075b5a60daSOlaf Kirch 	}
27085b5a60daSOlaf Kirch 
270921dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2710677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
271195e3b24cSHerbert Xu 		st->frag_idx = 0;
2712677e90edSThomas Graf 		goto next_skb;
271371b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
271471b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
271571b3346dSShyam Iyer 		st->frag_idx = 0;
2716677e90edSThomas Graf 		goto next_skb;
2717677e90edSThomas Graf 	}
2718677e90edSThomas Graf 
2719677e90edSThomas Graf 	return 0;
2720677e90edSThomas Graf }
2721b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2722677e90edSThomas Graf 
2723677e90edSThomas Graf /**
2724677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2725677e90edSThomas Graf  * @st: state variable
2726677e90edSThomas Graf  *
2727677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2728677e90edSThomas Graf  * returned 0.
2729677e90edSThomas Graf  */
2730677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2731677e90edSThomas Graf {
2732677e90edSThomas Graf 	if (st->frag_data)
273351c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
2734677e90edSThomas Graf }
2735b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2736677e90edSThomas Graf 
27373fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
27383fc7e8a6SThomas Graf 
27393fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
27403fc7e8a6SThomas Graf 					  struct ts_config *conf,
27413fc7e8a6SThomas Graf 					  struct ts_state *state)
27423fc7e8a6SThomas Graf {
27433fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
27443fc7e8a6SThomas Graf }
27453fc7e8a6SThomas Graf 
27463fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
27473fc7e8a6SThomas Graf {
27483fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
27493fc7e8a6SThomas Graf }
27503fc7e8a6SThomas Graf 
27513fc7e8a6SThomas Graf /**
27523fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
27533fc7e8a6SThomas Graf  * @skb: the buffer to look in
27543fc7e8a6SThomas Graf  * @from: search offset
27553fc7e8a6SThomas Graf  * @to: search limit
27563fc7e8a6SThomas Graf  * @config: textsearch configuration
27573fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
27583fc7e8a6SThomas Graf  *
27593fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
27603fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
27613fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
27623fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
27633fc7e8a6SThomas Graf  */
27643fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
27653fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
27663fc7e8a6SThomas Graf 			   struct ts_state *state)
27673fc7e8a6SThomas Graf {
2768f72b948dSPhil Oester 	unsigned int ret;
2769f72b948dSPhil Oester 
27703fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
27713fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
27723fc7e8a6SThomas Graf 
27733fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
27743fc7e8a6SThomas Graf 
2775f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2776f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
27773fc7e8a6SThomas Graf }
2778b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
27793fc7e8a6SThomas Graf 
2780e89e9cf5SAnanda Raju /**
27812c53040fSBen Hutchings  * skb_append_datato_frags - append the user data to a skb
2782e89e9cf5SAnanda Raju  * @sk: sock  structure
2783e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
2784e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2785e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2786e89e9cf5SAnanda Raju  * @length: length of the iov message
2787e89e9cf5SAnanda Raju  *
2788e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2789e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2790e89e9cf5SAnanda Raju  */
2791e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2792dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2793e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2794e89e9cf5SAnanda Raju 			void *from, int length)
2795e89e9cf5SAnanda Raju {
2796b2111724SEric Dumazet 	int frg_cnt = skb_shinfo(skb)->nr_frags;
2797b2111724SEric Dumazet 	int copy;
2798e89e9cf5SAnanda Raju 	int offset = 0;
2799e89e9cf5SAnanda Raju 	int ret;
2800b2111724SEric Dumazet 	struct page_frag *pfrag = &current->task_frag;
2801e89e9cf5SAnanda Raju 
2802e89e9cf5SAnanda Raju 	do {
2803e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2804e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2805b2111724SEric Dumazet 			return -EMSGSIZE;
2806e89e9cf5SAnanda Raju 
2807b2111724SEric Dumazet 		if (!sk_page_frag_refill(sk, pfrag))
2808e89e9cf5SAnanda Raju 			return -ENOMEM;
2809e89e9cf5SAnanda Raju 
2810e89e9cf5SAnanda Raju 		/* copy the user data to page */
2811b2111724SEric Dumazet 		copy = min_t(int, length, pfrag->size - pfrag->offset);
2812e89e9cf5SAnanda Raju 
2813b2111724SEric Dumazet 		ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2814e89e9cf5SAnanda Raju 			      offset, copy, 0, skb);
2815e89e9cf5SAnanda Raju 		if (ret < 0)
2816e89e9cf5SAnanda Raju 			return -EFAULT;
2817e89e9cf5SAnanda Raju 
2818e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2819b2111724SEric Dumazet 		skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2820b2111724SEric Dumazet 				   copy);
2821b2111724SEric Dumazet 		frg_cnt++;
2822b2111724SEric Dumazet 		pfrag->offset += copy;
2823b2111724SEric Dumazet 		get_page(pfrag->page);
2824b2111724SEric Dumazet 
2825b2111724SEric Dumazet 		skb->truesize += copy;
2826b2111724SEric Dumazet 		atomic_add(copy, &sk->sk_wmem_alloc);
2827e89e9cf5SAnanda Raju 		skb->len += copy;
2828e89e9cf5SAnanda Raju 		skb->data_len += copy;
2829e89e9cf5SAnanda Raju 		offset += copy;
2830e89e9cf5SAnanda Raju 		length -= copy;
2831e89e9cf5SAnanda Raju 
2832e89e9cf5SAnanda Raju 	} while (length > 0);
2833e89e9cf5SAnanda Raju 
2834e89e9cf5SAnanda Raju 	return 0;
2835e89e9cf5SAnanda Raju }
2836b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
2837e89e9cf5SAnanda Raju 
2838cbb042f9SHerbert Xu /**
2839cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2840cbb042f9SHerbert Xu  *	@skb: buffer to update
2841cbb042f9SHerbert Xu  *	@len: length of data pulled
2842cbb042f9SHerbert Xu  *
2843cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2844fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
284584fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
284684fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
284784fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2848cbb042f9SHerbert Xu  */
2849cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2850cbb042f9SHerbert Xu {
2851cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2852cbb042f9SHerbert Xu 	skb->len -= len;
2853cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2854cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2855cbb042f9SHerbert Xu 	return skb->data += len;
2856cbb042f9SHerbert Xu }
2857f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2858f94691acSArnaldo Carvalho de Melo 
2859f4c50d99SHerbert Xu /**
2860f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2861df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
2862576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2863f4c50d99SHerbert Xu  *
2864f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
28654c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
28664c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2867f4c50d99SHerbert Xu  */
2868df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
2869df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
2870f4c50d99SHerbert Xu {
2871f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2872f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
28731a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2874df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2875df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
2876df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
28771fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
2878f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2879df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2880f4c50d99SHerbert Xu 	unsigned int headroom;
2881f4c50d99SHerbert Xu 	unsigned int len;
2882ec5f0615SPravin B Shelar 	__be16 proto;
2883ec5f0615SPravin B Shelar 	bool csum;
288404ed3e74SMichał Mirosław 	int sg = !!(features & NETIF_F_SG);
2885df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
2886f4c50d99SHerbert Xu 	int err = -ENOMEM;
2887f4c50d99SHerbert Xu 	int i = 0;
2888f4c50d99SHerbert Xu 	int pos;
288953d6471cSVlad Yasevich 	int dummy;
2890f4c50d99SHerbert Xu 
28915882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
289253d6471cSVlad Yasevich 	proto = skb_network_protocol(head_skb, &dummy);
2893ec5f0615SPravin B Shelar 	if (unlikely(!proto))
2894ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
2895ec5f0615SPravin B Shelar 
28967e2b10c1STom Herbert 	csum = !head_skb->encap_hdr_csum &&
28977e2b10c1STom Herbert 	    !!can_checksum_protocol(features, proto);
28987e2b10c1STom Herbert 
2899df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
2900df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
2901f4c50d99SHerbert Xu 
2902f4c50d99SHerbert Xu 	do {
2903f4c50d99SHerbert Xu 		struct sk_buff *nskb;
29048cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
2905c8884eddSHerbert Xu 		int hsize;
2906f4c50d99SHerbert Xu 		int size;
2907f4c50d99SHerbert Xu 
2908df5771ffSMichael S. Tsirkin 		len = head_skb->len - offset;
2909f4c50d99SHerbert Xu 		if (len > mss)
2910f4c50d99SHerbert Xu 			len = mss;
2911f4c50d99SHerbert Xu 
2912df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
2913f4c50d99SHerbert Xu 		if (hsize < 0)
2914f4c50d99SHerbert Xu 			hsize = 0;
2915c8884eddSHerbert Xu 		if (hsize > len || !sg)
2916c8884eddSHerbert Xu 			hsize = len;
2917f4c50d99SHerbert Xu 
29181a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
29191a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
29201a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
292189319d38SHerbert Xu 
29229d8506ccSHerbert Xu 			i = 0;
29231a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
29241a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
29251fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
29261a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
29279d8506ccSHerbert Xu 
29289d8506ccSHerbert Xu 			while (pos < offset + len) {
29299d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
29309d8506ccSHerbert Xu 
29314e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
29329d8506ccSHerbert Xu 				if (pos + size > offset + len)
29339d8506ccSHerbert Xu 					break;
29349d8506ccSHerbert Xu 
29359d8506ccSHerbert Xu 				i++;
29369d8506ccSHerbert Xu 				pos += size;
29374e1beba1SMichael S. Tsirkin 				frag++;
29389d8506ccSHerbert Xu 			}
29399d8506ccSHerbert Xu 
29401a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
29411a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
294289319d38SHerbert Xu 
2943f4c50d99SHerbert Xu 			if (unlikely(!nskb))
2944f4c50d99SHerbert Xu 				goto err;
2945f4c50d99SHerbert Xu 
29469d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
29479d8506ccSHerbert Xu 				kfree_skb(nskb);
29489d8506ccSHerbert Xu 				goto err;
29499d8506ccSHerbert Xu 			}
29509d8506ccSHerbert Xu 
2951ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
295289319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
295389319d38SHerbert Xu 				kfree_skb(nskb);
295489319d38SHerbert Xu 				goto err;
295589319d38SHerbert Xu 			}
295689319d38SHerbert Xu 
2957ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
295889319d38SHerbert Xu 			skb_release_head_state(nskb);
295989319d38SHerbert Xu 			__skb_push(nskb, doffset);
296089319d38SHerbert Xu 		} else {
2961c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
2962df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
2963c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
296489319d38SHerbert Xu 
296589319d38SHerbert Xu 			if (unlikely(!nskb))
296689319d38SHerbert Xu 				goto err;
296789319d38SHerbert Xu 
296889319d38SHerbert Xu 			skb_reserve(nskb, headroom);
296989319d38SHerbert Xu 			__skb_put(nskb, doffset);
297089319d38SHerbert Xu 		}
297189319d38SHerbert Xu 
2972f4c50d99SHerbert Xu 		if (segs)
2973f4c50d99SHerbert Xu 			tail->next = nskb;
2974f4c50d99SHerbert Xu 		else
2975f4c50d99SHerbert Xu 			segs = nskb;
2976f4c50d99SHerbert Xu 		tail = nskb;
2977f4c50d99SHerbert Xu 
2978df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
2979df5771ffSMichael S. Tsirkin 		nskb->mac_len = head_skb->mac_len;
2980f4c50d99SHerbert Xu 
2981030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
298268c33163SPravin B Shelar 
2983df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
298468c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
298568c33163SPravin B Shelar 						 doffset + tnl_hlen);
298689319d38SHerbert Xu 
29879d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
29881cdbcb79SSimon Horman 			goto perform_csum_check;
298989319d38SHerbert Xu 
2990f4c50d99SHerbert Xu 		if (!sg) {
29916f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
2992df5771ffSMichael S. Tsirkin 			nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
2993f4c50d99SHerbert Xu 							    skb_put(nskb, len),
2994f4c50d99SHerbert Xu 							    len, 0);
29957e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
2996de843723STom Herbert 			    skb_headroom(nskb) + doffset;
2997f4c50d99SHerbert Xu 			continue;
2998f4c50d99SHerbert Xu 		}
2999f4c50d99SHerbert Xu 
30008cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3001f4c50d99SHerbert Xu 
3002df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3003d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
3004f4c50d99SHerbert Xu 
3005df5771ffSMichael S. Tsirkin 		skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3006df5771ffSMichael S. Tsirkin 			SKBTX_SHARED_FRAG;
3007cef401deSEric Dumazet 
30089d8506ccSHerbert Xu 		while (pos < offset + len) {
30099d8506ccSHerbert Xu 			if (i >= nfrags) {
30101a4cedafSMichael S. Tsirkin 				BUG_ON(skb_headlen(list_skb));
30119d8506ccSHerbert Xu 
30129d8506ccSHerbert Xu 				i = 0;
30131a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
30141a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
30151fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
30169d8506ccSHerbert Xu 
30179d8506ccSHerbert Xu 				BUG_ON(!nfrags);
30189d8506ccSHerbert Xu 
30191a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
30209d8506ccSHerbert Xu 			}
30219d8506ccSHerbert Xu 
30229d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
30239d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
30249d8506ccSHerbert Xu 				net_warn_ratelimited(
30259d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
30269d8506ccSHerbert Xu 					pos, mss);
30279d8506ccSHerbert Xu 				goto err;
30289d8506ccSHerbert Xu 			}
30299d8506ccSHerbert Xu 
30301fd819ecSMichael S. Tsirkin 			if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
30311fd819ecSMichael S. Tsirkin 				goto err;
30321fd819ecSMichael S. Tsirkin 
30334e1beba1SMichael S. Tsirkin 			*nskb_frag = *frag;
30348cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
30358cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
3036f4c50d99SHerbert Xu 
3037f4c50d99SHerbert Xu 			if (pos < offset) {
30388cb19905SMichael S. Tsirkin 				nskb_frag->page_offset += offset - pos;
30398cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
3040f4c50d99SHerbert Xu 			}
3041f4c50d99SHerbert Xu 
304289319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
3043f4c50d99SHerbert Xu 
3044f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
3045f4c50d99SHerbert Xu 				i++;
30464e1beba1SMichael S. Tsirkin 				frag++;
3047f4c50d99SHerbert Xu 				pos += size;
3048f4c50d99SHerbert Xu 			} else {
30498cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
305089319d38SHerbert Xu 				goto skip_fraglist;
3051f4c50d99SHerbert Xu 			}
3052f4c50d99SHerbert Xu 
30538cb19905SMichael S. Tsirkin 			nskb_frag++;
3054f4c50d99SHerbert Xu 		}
3055f4c50d99SHerbert Xu 
305689319d38SHerbert Xu skip_fraglist:
3057f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
3058f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
3059f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
3060ec5f0615SPravin B Shelar 
30611cdbcb79SSimon Horman perform_csum_check:
3062ec5f0615SPravin B Shelar 		if (!csum) {
3063ec5f0615SPravin B Shelar 			nskb->csum = skb_checksum(nskb, doffset,
3064ec5f0615SPravin B Shelar 						  nskb->len - doffset, 0);
3065ec5f0615SPravin B Shelar 			nskb->ip_summed = CHECKSUM_NONE;
30667e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
30677e2b10c1STom Herbert 			    skb_headroom(nskb) + doffset;
3068ec5f0615SPravin B Shelar 		}
3069df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
3070f4c50d99SHerbert Xu 
3071f4c50d99SHerbert Xu 	return segs;
3072f4c50d99SHerbert Xu 
3073f4c50d99SHerbert Xu err:
3074289dccbeSEric Dumazet 	kfree_skb_list(segs);
3075f4c50d99SHerbert Xu 	return ERR_PTR(err);
3076f4c50d99SHerbert Xu }
3077f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
3078f4c50d99SHerbert Xu 
307971d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
308071d93b39SHerbert Xu {
30818a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
308267147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
308367147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
30848a29111cSEric Dumazet 	struct sk_buff *nskb, *lp, *p = *head;
30858a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
3086715dc1f3SEric Dumazet 	unsigned int delta_truesize;
30878a29111cSEric Dumazet 	unsigned int headroom;
308871d93b39SHerbert Xu 
30898a29111cSEric Dumazet 	if (unlikely(p->len + len >= 65536))
309071d93b39SHerbert Xu 		return -E2BIG;
309171d93b39SHerbert Xu 
309229e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
30938a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
30948a29111cSEric Dumazet 
30958a29111cSEric Dumazet 	if (headlen <= offset) {
309642da6994SHerbert Xu 		skb_frag_t *frag;
309766e92fcfSHerbert Xu 		skb_frag_t *frag2;
30989aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
30999aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
310042da6994SHerbert Xu 
310166e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
31028a29111cSEric Dumazet 			goto merge;
310381705ad1SHerbert Xu 
31048a29111cSEric Dumazet 		offset -= headlen;
31059aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
31069aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
3107f5572068SHerbert Xu 
31089aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
31099aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
311066e92fcfSHerbert Xu 		do {
311166e92fcfSHerbert Xu 			*--frag = *--frag2;
311266e92fcfSHerbert Xu 		} while (--i);
311366e92fcfSHerbert Xu 
311466e92fcfSHerbert Xu 		frag->page_offset += offset;
31159e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
311666e92fcfSHerbert Xu 
3117715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
3118ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
3119ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
3120715dc1f3SEric Dumazet 
3121f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
3122f5572068SHerbert Xu 		skb->len -= skb->data_len;
3123f5572068SHerbert Xu 		skb->data_len = 0;
3124f5572068SHerbert Xu 
3125715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
31265d38a079SHerbert Xu 		goto done;
3127d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
3128d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
3129d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
3130d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
3131d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
3132d7e8883cSEric Dumazet 		unsigned int first_offset;
3133d7e8883cSEric Dumazet 
3134d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
31358a29111cSEric Dumazet 			goto merge;
3136d7e8883cSEric Dumazet 
3137d7e8883cSEric Dumazet 		first_offset = skb->data -
3138d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
3139d7e8883cSEric Dumazet 			       offset;
3140d7e8883cSEric Dumazet 
3141d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3142d7e8883cSEric Dumazet 
3143d7e8883cSEric Dumazet 		frag->page.p	  = page;
3144d7e8883cSEric Dumazet 		frag->page_offset = first_offset;
3145d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
3146d7e8883cSEric Dumazet 
3147d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3148d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
3149d7e8883cSEric Dumazet 
3150715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3151d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3152d7e8883cSEric Dumazet 		goto done;
31538a29111cSEric Dumazet 	}
31548a29111cSEric Dumazet 	if (pinfo->frag_list)
31558a29111cSEric Dumazet 		goto merge;
31568a29111cSEric Dumazet 	if (skb_gro_len(p) != pinfo->gso_size)
315769c0cab1SHerbert Xu 		return -E2BIG;
315871d93b39SHerbert Xu 
315971d93b39SHerbert Xu 	headroom = skb_headroom(p);
31603d3be433SEric Dumazet 	nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
316171d93b39SHerbert Xu 	if (unlikely(!nskb))
316271d93b39SHerbert Xu 		return -ENOMEM;
316371d93b39SHerbert Xu 
316471d93b39SHerbert Xu 	__copy_skb_header(nskb, p);
316571d93b39SHerbert Xu 	nskb->mac_len = p->mac_len;
316671d93b39SHerbert Xu 
316771d93b39SHerbert Xu 	skb_reserve(nskb, headroom);
316886911732SHerbert Xu 	__skb_put(nskb, skb_gro_offset(p));
316971d93b39SHerbert Xu 
317086911732SHerbert Xu 	skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
317171d93b39SHerbert Xu 	skb_set_network_header(nskb, skb_network_offset(p));
317271d93b39SHerbert Xu 	skb_set_transport_header(nskb, skb_transport_offset(p));
317371d93b39SHerbert Xu 
317486911732SHerbert Xu 	__skb_pull(p, skb_gro_offset(p));
317586911732SHerbert Xu 	memcpy(skb_mac_header(nskb), skb_mac_header(p),
317686911732SHerbert Xu 	       p->data - skb_mac_header(p));
317771d93b39SHerbert Xu 
317871d93b39SHerbert Xu 	skb_shinfo(nskb)->frag_list = p;
31799aaa156cSHerbert Xu 	skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3180622e0ca1SHerbert Xu 	pinfo->gso_size = 0;
318171d93b39SHerbert Xu 	skb_header_release(p);
3182c3c7c254SEric Dumazet 	NAPI_GRO_CB(nskb)->last = p;
318371d93b39SHerbert Xu 
318471d93b39SHerbert Xu 	nskb->data_len += p->len;
3185de8261c2SEric Dumazet 	nskb->truesize += p->truesize;
318671d93b39SHerbert Xu 	nskb->len += p->len;
318771d93b39SHerbert Xu 
318871d93b39SHerbert Xu 	*head = nskb;
318971d93b39SHerbert Xu 	nskb->next = p->next;
319071d93b39SHerbert Xu 	p->next = NULL;
319171d93b39SHerbert Xu 
319271d93b39SHerbert Xu 	p = nskb;
319371d93b39SHerbert Xu 
319471d93b39SHerbert Xu merge:
3195715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
319667147ba9SHerbert Xu 	if (offset > headlen) {
3197d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
3198d1dc7abfSMichal Schmidt 
3199d1dc7abfSMichal Schmidt 		skbinfo->frags[0].page_offset += eat;
32009e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
3201d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
3202d1dc7abfSMichal Schmidt 		skb->len -= eat;
320367147ba9SHerbert Xu 		offset = headlen;
320456035022SHerbert Xu 	}
320556035022SHerbert Xu 
320667147ba9SHerbert Xu 	__skb_pull(skb, offset);
320756035022SHerbert Xu 
320829e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
32098a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
32108a29111cSEric Dumazet 	else
3211c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
3212c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
321371d93b39SHerbert Xu 	skb_header_release(skb);
32148a29111cSEric Dumazet 	lp = p;
321571d93b39SHerbert Xu 
32165d38a079SHerbert Xu done:
32175d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
321837fe4732SHerbert Xu 	p->data_len += len;
3219715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
322037fe4732SHerbert Xu 	p->len += len;
32218a29111cSEric Dumazet 	if (lp != p) {
32228a29111cSEric Dumazet 		lp->data_len += len;
32238a29111cSEric Dumazet 		lp->truesize += delta_truesize;
32248a29111cSEric Dumazet 		lp->len += len;
32258a29111cSEric Dumazet 	}
322671d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
322771d93b39SHerbert Xu 	return 0;
322871d93b39SHerbert Xu }
322971d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive);
323071d93b39SHerbert Xu 
32311da177e4SLinus Torvalds void __init skb_init(void)
32321da177e4SLinus Torvalds {
32331da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
32341da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
32351da177e4SLinus Torvalds 					      0,
3236e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
323720c2df83SPaul Mundt 					      NULL);
3238d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3239d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
3240d179cd12SDavid S. Miller 						sizeof(atomic_t),
3241d179cd12SDavid S. Miller 						0,
3242e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
324320c2df83SPaul Mundt 						NULL);
32441da177e4SLinus Torvalds }
32451da177e4SLinus Torvalds 
3246716ea3a7SDavid Howells /**
3247716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3248716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
3249716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
3250716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
3251716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
3252716ea3a7SDavid Howells  *
3253716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
3254716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
3255716ea3a7SDavid Howells  */
325651c739d1SDavid S. Miller static int
325751c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3258716ea3a7SDavid Howells {
32591a028e50SDavid S. Miller 	int start = skb_headlen(skb);
32601a028e50SDavid S. Miller 	int i, copy = start - offset;
3261fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
3262716ea3a7SDavid Howells 	int elt = 0;
3263716ea3a7SDavid Howells 
3264716ea3a7SDavid Howells 	if (copy > 0) {
3265716ea3a7SDavid Howells 		if (copy > len)
3266716ea3a7SDavid Howells 			copy = len;
3267642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
3268716ea3a7SDavid Howells 		elt++;
3269716ea3a7SDavid Howells 		if ((len -= copy) == 0)
3270716ea3a7SDavid Howells 			return elt;
3271716ea3a7SDavid Howells 		offset += copy;
3272716ea3a7SDavid Howells 	}
3273716ea3a7SDavid Howells 
3274716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
32751a028e50SDavid S. Miller 		int end;
3276716ea3a7SDavid Howells 
3277547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
32781a028e50SDavid S. Miller 
32799e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3280716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3281716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3282716ea3a7SDavid Howells 
3283716ea3a7SDavid Howells 			if (copy > len)
3284716ea3a7SDavid Howells 				copy = len;
3285ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3286642f1490SJens Axboe 					frag->page_offset+offset-start);
3287716ea3a7SDavid Howells 			elt++;
3288716ea3a7SDavid Howells 			if (!(len -= copy))
3289716ea3a7SDavid Howells 				return elt;
3290716ea3a7SDavid Howells 			offset += copy;
3291716ea3a7SDavid Howells 		}
32921a028e50SDavid S. Miller 		start = end;
3293716ea3a7SDavid Howells 	}
3294716ea3a7SDavid Howells 
3295fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
32961a028e50SDavid S. Miller 		int end;
3297716ea3a7SDavid Howells 
3298547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
32991a028e50SDavid S. Miller 
3300fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
3301716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3302716ea3a7SDavid Howells 			if (copy > len)
3303716ea3a7SDavid Howells 				copy = len;
3304fbb398a8SDavid S. Miller 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
330551c739d1SDavid S. Miller 					      copy);
3306716ea3a7SDavid Howells 			if ((len -= copy) == 0)
3307716ea3a7SDavid Howells 				return elt;
3308716ea3a7SDavid Howells 			offset += copy;
3309716ea3a7SDavid Howells 		}
33101a028e50SDavid S. Miller 		start = end;
3311716ea3a7SDavid Howells 	}
3312716ea3a7SDavid Howells 	BUG_ON(len);
3313716ea3a7SDavid Howells 	return elt;
3314716ea3a7SDavid Howells }
3315716ea3a7SDavid Howells 
331625a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
331725a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
331825a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
331925a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
332025a91d8dSFan Du  *
332125a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
332225a91d8dSFan Du  * 1. sg_init_table
332325a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
332425a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
332525a91d8dSFan Du  *
332625a91d8dSFan Du  * This is equivalent to:
332725a91d8dSFan Du  * 1. sg_init_table
332825a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
332925a91d8dSFan Du  * 3. sg_unmark_end
333025a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
333125a91d8dSFan Du  *
333225a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
333325a91d8dSFan Du  * is more preferable.
333425a91d8dSFan Du  */
333525a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
333625a91d8dSFan Du 			int offset, int len)
333725a91d8dSFan Du {
333825a91d8dSFan Du 	return __skb_to_sgvec(skb, sg, offset, len);
333925a91d8dSFan Du }
334025a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
334125a91d8dSFan Du 
334251c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
334351c739d1SDavid S. Miller {
334451c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
334551c739d1SDavid S. Miller 
3346c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
334751c739d1SDavid S. Miller 
334851c739d1SDavid S. Miller 	return nsg;
334951c739d1SDavid S. Miller }
3350b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
335151c739d1SDavid S. Miller 
3352716ea3a7SDavid Howells /**
3353716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
3354716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
3355716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
3356716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
3357716ea3a7SDavid Howells  *
3358716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
3359716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
3360716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
3361716ea3a7SDavid Howells  *
3362716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
3363716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
3364716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
3365716ea3a7SDavid Howells  *
3366716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
3367716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
3368716ea3a7SDavid Howells  */
3369716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3370716ea3a7SDavid Howells {
3371716ea3a7SDavid Howells 	int copyflag;
3372716ea3a7SDavid Howells 	int elt;
3373716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
3374716ea3a7SDavid Howells 
3375716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
3376716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
3377716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
3378716ea3a7SDavid Howells 	 */
3379716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3380716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3381716ea3a7SDavid Howells 		return -ENOMEM;
3382716ea3a7SDavid Howells 
3383716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
338421dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
3385716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
3386716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
3387716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
3388716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
3389716ea3a7SDavid Howells 
3390716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
3391716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3392716ea3a7SDavid Howells 			return -ENOMEM;
3393716ea3a7SDavid Howells 
3394716ea3a7SDavid Howells 		/* Voila! */
3395716ea3a7SDavid Howells 		*trailer = skb;
3396716ea3a7SDavid Howells 		return 1;
3397716ea3a7SDavid Howells 	}
3398716ea3a7SDavid Howells 
3399716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
3400716ea3a7SDavid Howells 
3401716ea3a7SDavid Howells 	elt = 1;
3402716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
3403716ea3a7SDavid Howells 	copyflag = 0;
3404716ea3a7SDavid Howells 
3405716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
3406716ea3a7SDavid Howells 		int ntail = 0;
3407716ea3a7SDavid Howells 
3408716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
3409716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
3410716ea3a7SDavid Howells 		 * after it. */
3411716ea3a7SDavid Howells 
3412716ea3a7SDavid Howells 		if (skb_shared(skb1))
3413716ea3a7SDavid Howells 			copyflag = 1;
3414716ea3a7SDavid Howells 
3415716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
3416716ea3a7SDavid Howells 
3417716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
3418716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
341921dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
3420716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
3421716ea3a7SDavid Howells 				ntail = tailbits + 128;
3422716ea3a7SDavid Howells 		}
3423716ea3a7SDavid Howells 
3424716ea3a7SDavid Howells 		if (copyflag ||
3425716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
3426716ea3a7SDavid Howells 		    ntail ||
3427716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
342821dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
3429716ea3a7SDavid Howells 			struct sk_buff *skb2;
3430716ea3a7SDavid Howells 
3431716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
3432716ea3a7SDavid Howells 			if (ntail == 0)
3433716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
3434716ea3a7SDavid Howells 			else
3435716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
3436716ea3a7SDavid Howells 						       skb_headroom(skb1),
3437716ea3a7SDavid Howells 						       ntail,
3438716ea3a7SDavid Howells 						       GFP_ATOMIC);
3439716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
3440716ea3a7SDavid Howells 				return -ENOMEM;
3441716ea3a7SDavid Howells 
3442716ea3a7SDavid Howells 			if (skb1->sk)
3443716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
3444716ea3a7SDavid Howells 
3445716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
3446716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
3447716ea3a7SDavid Howells 
3448716ea3a7SDavid Howells 			skb2->next = skb1->next;
3449716ea3a7SDavid Howells 			*skb_p = skb2;
3450716ea3a7SDavid Howells 			kfree_skb(skb1);
3451716ea3a7SDavid Howells 			skb1 = skb2;
3452716ea3a7SDavid Howells 		}
3453716ea3a7SDavid Howells 		elt++;
3454716ea3a7SDavid Howells 		*trailer = skb1;
3455716ea3a7SDavid Howells 		skb_p = &skb1->next;
3456716ea3a7SDavid Howells 	}
3457716ea3a7SDavid Howells 
3458716ea3a7SDavid Howells 	return elt;
3459716ea3a7SDavid Howells }
3460b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
3461716ea3a7SDavid Howells 
3462b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
3463b1faf566SEric Dumazet {
3464b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
3465b1faf566SEric Dumazet 
3466b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3467b1faf566SEric Dumazet }
3468b1faf566SEric Dumazet 
3469b1faf566SEric Dumazet /*
3470b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3471b1faf566SEric Dumazet  */
3472b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3473b1faf566SEric Dumazet {
3474b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
347595c96174SEric Dumazet 	    (unsigned int)sk->sk_rcvbuf)
3476b1faf566SEric Dumazet 		return -ENOMEM;
3477b1faf566SEric Dumazet 
3478b1faf566SEric Dumazet 	skb_orphan(skb);
3479b1faf566SEric Dumazet 	skb->sk = sk;
3480b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
3481b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3482b1faf566SEric Dumazet 
3483abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
3484abb57ea4SEric Dumazet 	skb_dst_force(skb);
3485abb57ea4SEric Dumazet 
3486b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
3487b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
3488676d2369SDavid S. Miller 		sk->sk_data_ready(sk);
3489b1faf566SEric Dumazet 	return 0;
3490b1faf566SEric Dumazet }
3491b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
3492b1faf566SEric Dumazet 
3493ac45f602SPatrick Ohly void skb_tstamp_tx(struct sk_buff *orig_skb,
3494ac45f602SPatrick Ohly 		struct skb_shared_hwtstamps *hwtstamps)
3495ac45f602SPatrick Ohly {
3496ac45f602SPatrick Ohly 	struct sock *sk = orig_skb->sk;
3497ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
3498ac45f602SPatrick Ohly 	struct sk_buff *skb;
3499ac45f602SPatrick Ohly 	int err;
3500ac45f602SPatrick Ohly 
3501ac45f602SPatrick Ohly 	if (!sk)
3502ac45f602SPatrick Ohly 		return;
3503ac45f602SPatrick Ohly 
3504ac45f602SPatrick Ohly 	if (hwtstamps) {
35052e31396fSWillem de Bruijn 		*skb_hwtstamps(orig_skb) =
3506ac45f602SPatrick Ohly 			*hwtstamps;
3507ac45f602SPatrick Ohly 	} else {
3508ac45f602SPatrick Ohly 		/*
3509ac45f602SPatrick Ohly 		 * no hardware time stamps available,
35102244d07bSOliver Hartkopp 		 * so keep the shared tx_flags and only
3511ac45f602SPatrick Ohly 		 * store software time stamp
3512ac45f602SPatrick Ohly 		 */
35132e31396fSWillem de Bruijn 		orig_skb->tstamp = ktime_get_real();
3514ac45f602SPatrick Ohly 	}
3515ac45f602SPatrick Ohly 
35162e31396fSWillem de Bruijn 	skb = skb_clone(orig_skb, GFP_ATOMIC);
35172e31396fSWillem de Bruijn 	if (!skb)
35182e31396fSWillem de Bruijn 		return;
35192e31396fSWillem de Bruijn 
3520ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
3521ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
3522ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
3523ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
352429030374SEric Dumazet 
3525ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
352629030374SEric Dumazet 
3527ac45f602SPatrick Ohly 	if (err)
3528ac45f602SPatrick Ohly 		kfree_skb(skb);
3529ac45f602SPatrick Ohly }
3530ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3531ac45f602SPatrick Ohly 
35326e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
35336e3e939fSJohannes Berg {
35346e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
35356e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
35366e3e939fSJohannes Berg 	int err;
35376e3e939fSJohannes Berg 
35386e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
35396e3e939fSJohannes Berg 	skb->wifi_acked = acked;
35406e3e939fSJohannes Berg 
35416e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
35426e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
35436e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
35446e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
35456e3e939fSJohannes Berg 
35466e3e939fSJohannes Berg 	err = sock_queue_err_skb(sk, skb);
35476e3e939fSJohannes Berg 	if (err)
35486e3e939fSJohannes Berg 		kfree_skb(skb);
35496e3e939fSJohannes Berg }
35506e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
35516e3e939fSJohannes Berg 
3552ac45f602SPatrick Ohly 
3553f35d9d8aSRusty Russell /**
3554f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3555f35d9d8aSRusty Russell  * @skb: the skb to set
3556f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3557f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3558f35d9d8aSRusty Russell  *
3559f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3560f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3561f35d9d8aSRusty Russell  *
3562f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3563f35d9d8aSRusty Russell  * returns false you should drop the packet.
3564f35d9d8aSRusty Russell  */
3565f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3566f35d9d8aSRusty Russell {
35675ff8dda3SHerbert Xu 	if (unlikely(start > skb_headlen(skb)) ||
35685ff8dda3SHerbert Xu 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3569e87cc472SJoe Perches 		net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
35705ff8dda3SHerbert Xu 				     start, off, skb_headlen(skb));
3571f35d9d8aSRusty Russell 		return false;
3572f35d9d8aSRusty Russell 	}
3573f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3574f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3575f35d9d8aSRusty Russell 	skb->csum_offset = off;
3576e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
3577f35d9d8aSRusty Russell 	return true;
3578f35d9d8aSRusty Russell }
3579b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3580f35d9d8aSRusty Russell 
3581ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3582ed1f50c3SPaul Durrant 			       unsigned int max)
3583ed1f50c3SPaul Durrant {
3584ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
3585ed1f50c3SPaul Durrant 		return 0;
3586ed1f50c3SPaul Durrant 
3587ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
3588ed1f50c3SPaul Durrant 	 * won't need to do it again.
3589ed1f50c3SPaul Durrant 	 */
3590ed1f50c3SPaul Durrant 	if (max > skb->len)
3591ed1f50c3SPaul Durrant 		max = skb->len;
3592ed1f50c3SPaul Durrant 
3593ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3594ed1f50c3SPaul Durrant 		return -ENOMEM;
3595ed1f50c3SPaul Durrant 
3596ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
3597ed1f50c3SPaul Durrant 		return -EPROTO;
3598ed1f50c3SPaul Durrant 
3599ed1f50c3SPaul Durrant 	return 0;
3600ed1f50c3SPaul Durrant }
3601ed1f50c3SPaul Durrant 
3602f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
3603f9708b43SJan Beulich 
3604f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3605f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
3606f9708b43SJan Beulich 				      unsigned int off)
3607f9708b43SJan Beulich {
3608f9708b43SJan Beulich 	switch (proto) {
3609f9708b43SJan Beulich 		int err;
3610f9708b43SJan Beulich 
3611f9708b43SJan Beulich 	case IPPROTO_TCP:
3612f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3613f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
3614f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3615f9708b43SJan Beulich 						  offsetof(struct tcphdr,
3616f9708b43SJan Beulich 							   check)))
3617f9708b43SJan Beulich 			err = -EPROTO;
3618f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3619f9708b43SJan Beulich 
3620f9708b43SJan Beulich 	case IPPROTO_UDP:
3621f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3622f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
3623f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3624f9708b43SJan Beulich 						  offsetof(struct udphdr,
3625f9708b43SJan Beulich 							   check)))
3626f9708b43SJan Beulich 			err = -EPROTO;
3627f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3628f9708b43SJan Beulich 	}
3629f9708b43SJan Beulich 
3630f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
3631f9708b43SJan Beulich }
3632f9708b43SJan Beulich 
3633ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3634ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
3635ed1f50c3SPaul Durrant  */
3636ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
3637ed1f50c3SPaul Durrant 
3638f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3639ed1f50c3SPaul Durrant {
3640ed1f50c3SPaul Durrant 	unsigned int off;
3641ed1f50c3SPaul Durrant 	bool fragment;
3642f9708b43SJan Beulich 	__sum16 *csum;
3643ed1f50c3SPaul Durrant 	int err;
3644ed1f50c3SPaul Durrant 
3645ed1f50c3SPaul Durrant 	fragment = false;
3646ed1f50c3SPaul Durrant 
3647ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
3648ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
3649ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
3650ed1f50c3SPaul Durrant 	if (err < 0)
3651ed1f50c3SPaul Durrant 		goto out;
3652ed1f50c3SPaul Durrant 
3653ed1f50c3SPaul Durrant 	if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3654ed1f50c3SPaul Durrant 		fragment = true;
3655ed1f50c3SPaul Durrant 
3656ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
3657ed1f50c3SPaul Durrant 
3658ed1f50c3SPaul Durrant 	err = -EPROTO;
3659ed1f50c3SPaul Durrant 
3660ed1f50c3SPaul Durrant 	if (fragment)
3661ed1f50c3SPaul Durrant 		goto out;
3662ed1f50c3SPaul Durrant 
3663f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3664f9708b43SJan Beulich 	if (IS_ERR(csum))
3665f9708b43SJan Beulich 		return PTR_ERR(csum);
3666ed1f50c3SPaul Durrant 
3667ed1f50c3SPaul Durrant 	if (recalculate)
3668f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3669ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
3670ed1f50c3SPaul Durrant 					   skb->len - off,
3671f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
3672ed1f50c3SPaul Durrant 	err = 0;
3673ed1f50c3SPaul Durrant 
3674ed1f50c3SPaul Durrant out:
3675ed1f50c3SPaul Durrant 	return err;
3676ed1f50c3SPaul Durrant }
3677ed1f50c3SPaul Durrant 
3678ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3679ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
3680ed1f50c3SPaul Durrant  */
3681ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
3682ed1f50c3SPaul Durrant 
3683ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
3684ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
3685ed1f50c3SPaul Durrant 
3686ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3687ed1f50c3SPaul Durrant {
3688ed1f50c3SPaul Durrant 	int err;
3689ed1f50c3SPaul Durrant 	u8 nexthdr;
3690ed1f50c3SPaul Durrant 	unsigned int off;
3691ed1f50c3SPaul Durrant 	unsigned int len;
3692ed1f50c3SPaul Durrant 	bool fragment;
3693ed1f50c3SPaul Durrant 	bool done;
3694f9708b43SJan Beulich 	__sum16 *csum;
3695ed1f50c3SPaul Durrant 
3696ed1f50c3SPaul Durrant 	fragment = false;
3697ed1f50c3SPaul Durrant 	done = false;
3698ed1f50c3SPaul Durrant 
3699ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
3700ed1f50c3SPaul Durrant 
3701ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3702ed1f50c3SPaul Durrant 	if (err < 0)
3703ed1f50c3SPaul Durrant 		goto out;
3704ed1f50c3SPaul Durrant 
3705ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
3706ed1f50c3SPaul Durrant 
3707ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3708ed1f50c3SPaul Durrant 	while (off <= len && !done) {
3709ed1f50c3SPaul Durrant 		switch (nexthdr) {
3710ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
3711ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
3712ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
3713ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
3714ed1f50c3SPaul Durrant 
3715ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3716ed1f50c3SPaul Durrant 						  off +
3717ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
3718ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3719ed1f50c3SPaul Durrant 			if (err < 0)
3720ed1f50c3SPaul Durrant 				goto out;
3721ed1f50c3SPaul Durrant 
3722ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3723ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3724ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
3725ed1f50c3SPaul Durrant 			break;
3726ed1f50c3SPaul Durrant 		}
3727ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
3728ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
3729ed1f50c3SPaul Durrant 
3730ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3731ed1f50c3SPaul Durrant 						  off +
3732ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
3733ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3734ed1f50c3SPaul Durrant 			if (err < 0)
3735ed1f50c3SPaul Durrant 				goto out;
3736ed1f50c3SPaul Durrant 
3737ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3738ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3739ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
3740ed1f50c3SPaul Durrant 			break;
3741ed1f50c3SPaul Durrant 		}
3742ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
3743ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
3744ed1f50c3SPaul Durrant 
3745ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3746ed1f50c3SPaul Durrant 						  off +
3747ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
3748ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3749ed1f50c3SPaul Durrant 			if (err < 0)
3750ed1f50c3SPaul Durrant 				goto out;
3751ed1f50c3SPaul Durrant 
3752ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
3753ed1f50c3SPaul Durrant 
3754ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3755ed1f50c3SPaul Durrant 				fragment = true;
3756ed1f50c3SPaul Durrant 
3757ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3758ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
3759ed1f50c3SPaul Durrant 			break;
3760ed1f50c3SPaul Durrant 		}
3761ed1f50c3SPaul Durrant 		default:
3762ed1f50c3SPaul Durrant 			done = true;
3763ed1f50c3SPaul Durrant 			break;
3764ed1f50c3SPaul Durrant 		}
3765ed1f50c3SPaul Durrant 	}
3766ed1f50c3SPaul Durrant 
3767ed1f50c3SPaul Durrant 	err = -EPROTO;
3768ed1f50c3SPaul Durrant 
3769ed1f50c3SPaul Durrant 	if (!done || fragment)
3770ed1f50c3SPaul Durrant 		goto out;
3771ed1f50c3SPaul Durrant 
3772f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
3773f9708b43SJan Beulich 	if (IS_ERR(csum))
3774f9708b43SJan Beulich 		return PTR_ERR(csum);
3775ed1f50c3SPaul Durrant 
3776ed1f50c3SPaul Durrant 	if (recalculate)
3777f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3778ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
3779f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
3780ed1f50c3SPaul Durrant 	err = 0;
3781ed1f50c3SPaul Durrant 
3782ed1f50c3SPaul Durrant out:
3783ed1f50c3SPaul Durrant 	return err;
3784ed1f50c3SPaul Durrant }
3785ed1f50c3SPaul Durrant 
3786ed1f50c3SPaul Durrant /**
3787ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
3788ed1f50c3SPaul Durrant  * @skb: the skb to set up
3789ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
3790ed1f50c3SPaul Durrant  */
3791ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3792ed1f50c3SPaul Durrant {
3793ed1f50c3SPaul Durrant 	int err;
3794ed1f50c3SPaul Durrant 
3795ed1f50c3SPaul Durrant 	switch (skb->protocol) {
3796ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
3797f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
3798ed1f50c3SPaul Durrant 		break;
3799ed1f50c3SPaul Durrant 
3800ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
3801ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
3802ed1f50c3SPaul Durrant 		break;
3803ed1f50c3SPaul Durrant 
3804ed1f50c3SPaul Durrant 	default:
3805ed1f50c3SPaul Durrant 		err = -EPROTO;
3806ed1f50c3SPaul Durrant 		break;
3807ed1f50c3SPaul Durrant 	}
3808ed1f50c3SPaul Durrant 
3809ed1f50c3SPaul Durrant 	return err;
3810ed1f50c3SPaul Durrant }
3811ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
3812ed1f50c3SPaul Durrant 
38134497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
38144497b076SBen Hutchings {
3815e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3816e87cc472SJoe Perches 			     skb->dev->name);
38174497b076SBen Hutchings }
38184497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3819bad43ca8SEric Dumazet 
3820bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3821bad43ca8SEric Dumazet {
38223d861f66SEric Dumazet 	if (head_stolen) {
38233d861f66SEric Dumazet 		skb_release_head_state(skb);
3824bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
38253d861f66SEric Dumazet 	} else {
3826bad43ca8SEric Dumazet 		__kfree_skb(skb);
3827bad43ca8SEric Dumazet 	}
38283d861f66SEric Dumazet }
3829bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
3830bad43ca8SEric Dumazet 
3831bad43ca8SEric Dumazet /**
3832bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
3833bad43ca8SEric Dumazet  * @to: prior buffer
3834bad43ca8SEric Dumazet  * @from: buffer to add
3835bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
3836c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
3837bad43ca8SEric Dumazet  */
3838bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3839bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
3840bad43ca8SEric Dumazet {
3841bad43ca8SEric Dumazet 	int i, delta, len = from->len;
3842bad43ca8SEric Dumazet 
3843bad43ca8SEric Dumazet 	*fragstolen = false;
3844bad43ca8SEric Dumazet 
3845bad43ca8SEric Dumazet 	if (skb_cloned(to))
3846bad43ca8SEric Dumazet 		return false;
3847bad43ca8SEric Dumazet 
3848bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
3849bad43ca8SEric Dumazet 		BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3850bad43ca8SEric Dumazet 		*delta_truesize = 0;
3851bad43ca8SEric Dumazet 		return true;
3852bad43ca8SEric Dumazet 	}
3853bad43ca8SEric Dumazet 
3854bad43ca8SEric Dumazet 	if (skb_has_frag_list(to) || skb_has_frag_list(from))
3855bad43ca8SEric Dumazet 		return false;
3856bad43ca8SEric Dumazet 
3857bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
3858bad43ca8SEric Dumazet 		struct page *page;
3859bad43ca8SEric Dumazet 		unsigned int offset;
3860bad43ca8SEric Dumazet 
3861bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
3862bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3863bad43ca8SEric Dumazet 			return false;
3864bad43ca8SEric Dumazet 
3865bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
3866bad43ca8SEric Dumazet 			return false;
3867bad43ca8SEric Dumazet 
3868bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3869bad43ca8SEric Dumazet 
3870bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
3871bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
3872bad43ca8SEric Dumazet 
3873bad43ca8SEric Dumazet 		skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3874bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
3875bad43ca8SEric Dumazet 		*fragstolen = true;
3876bad43ca8SEric Dumazet 	} else {
3877bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
3878bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3879bad43ca8SEric Dumazet 			return false;
3880bad43ca8SEric Dumazet 
3881f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
3882bad43ca8SEric Dumazet 	}
3883bad43ca8SEric Dumazet 
3884bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
3885bad43ca8SEric Dumazet 
3886bad43ca8SEric Dumazet 	memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3887bad43ca8SEric Dumazet 	       skb_shinfo(from)->frags,
3888bad43ca8SEric Dumazet 	       skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
3889bad43ca8SEric Dumazet 	skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
3890bad43ca8SEric Dumazet 
3891bad43ca8SEric Dumazet 	if (!skb_cloned(from))
3892bad43ca8SEric Dumazet 		skb_shinfo(from)->nr_frags = 0;
3893bad43ca8SEric Dumazet 
38948ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
38958ea853fdSLi RongQing 	 * since we set nr_frags to 0.
38968ea853fdSLi RongQing 	 */
3897bad43ca8SEric Dumazet 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
3898bad43ca8SEric Dumazet 		skb_frag_ref(from, i);
3899bad43ca8SEric Dumazet 
3900bad43ca8SEric Dumazet 	to->truesize += delta;
3901bad43ca8SEric Dumazet 	to->len += len;
3902bad43ca8SEric Dumazet 	to->data_len += len;
3903bad43ca8SEric Dumazet 
3904bad43ca8SEric Dumazet 	*delta_truesize = delta;
3905bad43ca8SEric Dumazet 	return true;
3906bad43ca8SEric Dumazet }
3907bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
3908621e84d6SNicolas Dichtel 
3909621e84d6SNicolas Dichtel /**
39108b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
3911621e84d6SNicolas Dichtel  *
3912621e84d6SNicolas Dichtel  * @skb: buffer to clean
39138b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
3914621e84d6SNicolas Dichtel  *
39158b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
39168b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
39178b27f277SNicolas Dichtel  * operations.
39188b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
39198b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
39208b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
3921621e84d6SNicolas Dichtel  */
39228b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
3923621e84d6SNicolas Dichtel {
39248b27f277SNicolas Dichtel 	if (xnet)
3925621e84d6SNicolas Dichtel 		skb_orphan(skb);
3926621e84d6SNicolas Dichtel 	skb->tstamp.tv64 = 0;
3927621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
3928621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
392960ff7467SWANG Cong 	skb->ignore_df = 0;
3930621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
3931621e84d6SNicolas Dichtel 	skb->mark = 0;
3932621e84d6SNicolas Dichtel 	secpath_reset(skb);
3933621e84d6SNicolas Dichtel 	nf_reset(skb);
3934621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
3935621e84d6SNicolas Dichtel }
3936621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
3937de960aa9SFlorian Westphal 
3938de960aa9SFlorian Westphal /**
3939de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
3940de960aa9SFlorian Westphal  *
3941de960aa9SFlorian Westphal  * @skb: GSO skb
3942de960aa9SFlorian Westphal  *
3943de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
3944de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
3945de960aa9SFlorian Westphal  *
3946de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
3947de960aa9SFlorian Westphal  */
3948de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
3949de960aa9SFlorian Westphal {
3950de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
3951de960aa9SFlorian Westphal 
3952de960aa9SFlorian Westphal 	if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
39536d39d589SFlorian Westphal 		return tcp_hdrlen(skb) + shinfo->gso_size;
39546d39d589SFlorian Westphal 
39556d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
39566d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
39576d39d589SFlorian Westphal 	 * accounted for.
39586d39d589SFlorian Westphal 	 */
39596d39d589SFlorian Westphal 	return shinfo->gso_size;
3960de960aa9SFlorian Westphal }
3961de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
3962