xref: /openbmc/linux/net/core/skbuff.c (revision 1862d620)
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>
5290017accSMarcelo Ricardo Leitner #include <linux/sctp.h>
531da177e4SLinus Torvalds #include <linux/netdevice.h>
541da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
551da177e4SLinus Torvalds #include <net/pkt_sched.h>
561da177e4SLinus Torvalds #endif
571da177e4SLinus Torvalds #include <linux/string.h>
581da177e4SLinus Torvalds #include <linux/skbuff.h>
599c55e01cSJens Axboe #include <linux/splice.h>
601da177e4SLinus Torvalds #include <linux/cache.h>
611da177e4SLinus Torvalds #include <linux/rtnetlink.h>
621da177e4SLinus Torvalds #include <linux/init.h>
63716ea3a7SDavid Howells #include <linux/scatterlist.h>
64ac45f602SPatrick Ohly #include <linux/errqueue.h>
65268bb0ceSLinus Torvalds #include <linux/prefetch.h>
660d5501c1SVlad Yasevich #include <linux/if_vlan.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <net/protocol.h>
691da177e4SLinus Torvalds #include <net/dst.h>
701da177e4SLinus Torvalds #include <net/sock.h>
711da177e4SLinus Torvalds #include <net/checksum.h>
72ed1f50c3SPaul Durrant #include <net/ip6_checksum.h>
731da177e4SLinus Torvalds #include <net/xfrm.h>
741da177e4SLinus Torvalds 
757c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
76ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
7751c56b00SEric Dumazet #include <linux/highmem.h>
78b245be1fSWillem de Bruijn #include <linux/capability.h>
79b245be1fSWillem de Bruijn #include <linux/user_namespace.h>
80a1f8e7f7SAl Viro 
81d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly;
82e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
835f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS;
845f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /**
87f05de73bSJean Sacren  *	skb_panic - private function for out-of-line support
881da177e4SLinus Torvalds  *	@skb:	buffer
891da177e4SLinus Torvalds  *	@sz:	size
90f05de73bSJean Sacren  *	@addr:	address
9199d5851eSJames Hogan  *	@msg:	skb_over_panic or skb_under_panic
921da177e4SLinus Torvalds  *
93f05de73bSJean Sacren  *	Out-of-line support for skb_put() and skb_push().
94f05de73bSJean Sacren  *	Called via the wrapper skb_over_panic() or skb_under_panic().
95f05de73bSJean Sacren  *	Keep out of line to prevent kernel bloat.
96f05de73bSJean Sacren  *	__builtin_return_address is not used because it is not always reliable.
971da177e4SLinus Torvalds  */
98f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
9999d5851eSJames Hogan 		      const char msg[])
1001da177e4SLinus Torvalds {
101e005d193SJoe Perches 	pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
10299d5851eSJames Hogan 		 msg, addr, skb->len, sz, skb->head, skb->data,
1034305b541SArnaldo Carvalho de Melo 		 (unsigned long)skb->tail, (unsigned long)skb->end,
10426095455SPatrick McHardy 		 skb->dev ? skb->dev->name : "<NULL>");
1051da177e4SLinus Torvalds 	BUG();
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1091da177e4SLinus Torvalds {
110f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
113f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
114f05de73bSJean Sacren {
115f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
116f05de73bSJean Sacren }
117c93bdd0eSMel Gorman 
118c93bdd0eSMel Gorman /*
119c93bdd0eSMel Gorman  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
120c93bdd0eSMel Gorman  * the caller if emergency pfmemalloc reserves are being used. If it is and
121c93bdd0eSMel Gorman  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
122c93bdd0eSMel Gorman  * may be used. Otherwise, the packet data may be discarded until enough
123c93bdd0eSMel Gorman  * memory is free
124c93bdd0eSMel Gorman  */
125c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
126c93bdd0eSMel Gorman 	 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
12761c5e88aSstephen hemminger 
12861c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
12961c5e88aSstephen hemminger 			       unsigned long ip, bool *pfmemalloc)
130c93bdd0eSMel Gorman {
131c93bdd0eSMel Gorman 	void *obj;
132c93bdd0eSMel Gorman 	bool ret_pfmemalloc = false;
133c93bdd0eSMel Gorman 
134c93bdd0eSMel Gorman 	/*
135c93bdd0eSMel Gorman 	 * Try a regular allocation, when that fails and we're not entitled
136c93bdd0eSMel Gorman 	 * to the reserves, fail.
137c93bdd0eSMel Gorman 	 */
138c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size,
139c93bdd0eSMel Gorman 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
140c93bdd0eSMel Gorman 					node);
141c93bdd0eSMel Gorman 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
142c93bdd0eSMel Gorman 		goto out;
143c93bdd0eSMel Gorman 
144c93bdd0eSMel Gorman 	/* Try again but now we are using pfmemalloc reserves */
145c93bdd0eSMel Gorman 	ret_pfmemalloc = true;
146c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size, flags, node);
147c93bdd0eSMel Gorman 
148c93bdd0eSMel Gorman out:
149c93bdd0eSMel Gorman 	if (pfmemalloc)
150c93bdd0eSMel Gorman 		*pfmemalloc = ret_pfmemalloc;
151c93bdd0eSMel Gorman 
152c93bdd0eSMel Gorman 	return obj;
153c93bdd0eSMel Gorman }
154c93bdd0eSMel Gorman 
1551da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1561da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1571da177e4SLinus Torvalds  *	[BEEP] leaks.
1581da177e4SLinus Torvalds  *
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds 
1610ebd0ac5SPatrick McHardy struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
1620ebd0ac5SPatrick McHardy {
1630ebd0ac5SPatrick McHardy 	struct sk_buff *skb;
1640ebd0ac5SPatrick McHardy 
1650ebd0ac5SPatrick McHardy 	/* Get the HEAD */
1660ebd0ac5SPatrick McHardy 	skb = kmem_cache_alloc_node(skbuff_head_cache,
1670ebd0ac5SPatrick McHardy 				    gfp_mask & ~__GFP_DMA, node);
1680ebd0ac5SPatrick McHardy 	if (!skb)
1690ebd0ac5SPatrick McHardy 		goto out;
1700ebd0ac5SPatrick McHardy 
1710ebd0ac5SPatrick McHardy 	/*
1720ebd0ac5SPatrick McHardy 	 * Only clear those fields we need to clear, not those that we will
1730ebd0ac5SPatrick McHardy 	 * actually initialise below. Hence, don't put any more fields after
1740ebd0ac5SPatrick McHardy 	 * the tail pointer in struct sk_buff!
1750ebd0ac5SPatrick McHardy 	 */
1760ebd0ac5SPatrick McHardy 	memset(skb, 0, offsetof(struct sk_buff, tail));
1775e71d9d7SPablo Neira 	skb->head = NULL;
1780ebd0ac5SPatrick McHardy 	skb->truesize = sizeof(struct sk_buff);
1790ebd0ac5SPatrick McHardy 	atomic_set(&skb->users, 1);
1800ebd0ac5SPatrick McHardy 
18135d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
1820ebd0ac5SPatrick McHardy out:
1830ebd0ac5SPatrick McHardy 	return skb;
1840ebd0ac5SPatrick McHardy }
1850ebd0ac5SPatrick McHardy 
1861da177e4SLinus Torvalds /**
187d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1881da177e4SLinus Torvalds  *	@size: size to allocate
1891da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
190c93bdd0eSMel Gorman  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
191c93bdd0eSMel Gorman  *		instead of head cache and allocate a cloned (child) skb.
192c93bdd0eSMel Gorman  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
193c93bdd0eSMel Gorman  *		allocations in case the data is required for writeback
194b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
19794b6042cSBen Hutchings  *	tail room of at least size bytes. The object has a reference count
19894b6042cSBen Hutchings  *	of one. The return is the buffer. On a failure the return is %NULL.
1991da177e4SLinus Torvalds  *
2001da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
2011da177e4SLinus Torvalds  *	%GFP_ATOMIC.
2021da177e4SLinus Torvalds  */
203dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
204c93bdd0eSMel Gorman 			    int flags, int node)
2051da177e4SLinus Torvalds {
206e18b890bSChristoph Lameter 	struct kmem_cache *cache;
2074947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
2081da177e4SLinus Torvalds 	struct sk_buff *skb;
2091da177e4SLinus Torvalds 	u8 *data;
210c93bdd0eSMel Gorman 	bool pfmemalloc;
2111da177e4SLinus Torvalds 
212c93bdd0eSMel Gorman 	cache = (flags & SKB_ALLOC_FCLONE)
213c93bdd0eSMel Gorman 		? skbuff_fclone_cache : skbuff_head_cache;
214c93bdd0eSMel Gorman 
215c93bdd0eSMel Gorman 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
216c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
2178798b3fbSHerbert Xu 
2181da177e4SLinus Torvalds 	/* Get the HEAD */
219b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
2201da177e4SLinus Torvalds 	if (!skb)
2211da177e4SLinus Torvalds 		goto out;
222ec7d2f2cSEric Dumazet 	prefetchw(skb);
2231da177e4SLinus Torvalds 
22487fb4b7bSEric Dumazet 	/* We do our best to align skb_shared_info on a separate cache
22587fb4b7bSEric Dumazet 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
22687fb4b7bSEric Dumazet 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
22787fb4b7bSEric Dumazet 	 * Both skb->head and skb_shared_info are cache line aligned.
22887fb4b7bSEric Dumazet 	 */
229bc417e30STony Lindgren 	size = SKB_DATA_ALIGN(size);
23087fb4b7bSEric Dumazet 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
231c93bdd0eSMel Gorman 	data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
2321da177e4SLinus Torvalds 	if (!data)
2331da177e4SLinus Torvalds 		goto nodata;
23487fb4b7bSEric Dumazet 	/* kmalloc(size) might give us more room than requested.
23587fb4b7bSEric Dumazet 	 * Put skb_shared_info exactly at the end of allocated zone,
23687fb4b7bSEric Dumazet 	 * to allow max possible filling before reallocation.
23787fb4b7bSEric Dumazet 	 */
23887fb4b7bSEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
239ec7d2f2cSEric Dumazet 	prefetchw(data + size);
2401da177e4SLinus Torvalds 
241ca0605a7SArnaldo Carvalho de Melo 	/*
242c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
243c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
244c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
245ca0605a7SArnaldo Carvalho de Melo 	 */
246ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
24787fb4b7bSEric Dumazet 	/* Account for allocated memory : skb + skb->head */
24887fb4b7bSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
249c93bdd0eSMel Gorman 	skb->pfmemalloc = pfmemalloc;
2501da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2511da177e4SLinus Torvalds 	skb->head = data;
2521da177e4SLinus Torvalds 	skb->data = data;
25327a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2544305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
25535d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
25635d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
25719633e12SStephen Hemminger 
2584947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2594947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
260ec7d2f2cSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
2614947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
262c2aa3665SEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
2634947d3efSBenjamin LaHaise 
264c93bdd0eSMel Gorman 	if (flags & SKB_ALLOC_FCLONE) {
265d0bf4a9eSEric Dumazet 		struct sk_buff_fclones *fclones;
2661da177e4SLinus Torvalds 
267d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
268d0bf4a9eSEric Dumazet 
269d0bf4a9eSEric Dumazet 		kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
270d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
271d0bf4a9eSEric Dumazet 		atomic_set(&fclones->fclone_ref, 1);
272d179cd12SDavid S. Miller 
2736ffe75ebSEric Dumazet 		fclones->skb2.fclone = SKB_FCLONE_CLONE;
274d179cd12SDavid S. Miller 	}
2751da177e4SLinus Torvalds out:
2761da177e4SLinus Torvalds 	return skb;
2771da177e4SLinus Torvalds nodata:
2788798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2791da177e4SLinus Torvalds 	skb = NULL;
2801da177e4SLinus Torvalds 	goto out;
2811da177e4SLinus Torvalds }
282b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /**
2852ea2f62cSEric Dumazet  * __build_skb - build a network buffer
286b2b5ce9dSEric Dumazet  * @data: data buffer provided by caller
2872ea2f62cSEric Dumazet  * @frag_size: size of data, or 0 if head was kmalloced
288b2b5ce9dSEric Dumazet  *
289b2b5ce9dSEric Dumazet  * Allocate a new &sk_buff. Caller provides space holding head and
290deceb4c0SFlorian Fainelli  * skb_shared_info. @data must have been allocated by kmalloc() only if
2912ea2f62cSEric Dumazet  * @frag_size is 0, otherwise data should come from the page allocator
2922ea2f62cSEric Dumazet  *  or vmalloc()
293b2b5ce9dSEric Dumazet  * The return is the new skb buffer.
294b2b5ce9dSEric Dumazet  * On a failure the return is %NULL, and @data is not freed.
295b2b5ce9dSEric Dumazet  * Notes :
296b2b5ce9dSEric Dumazet  *  Before IO, driver allocates only data buffer where NIC put incoming frame
297b2b5ce9dSEric Dumazet  *  Driver should add room at head (NET_SKB_PAD) and
298b2b5ce9dSEric Dumazet  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
299b2b5ce9dSEric Dumazet  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
300b2b5ce9dSEric Dumazet  *  before giving packet to stack.
301b2b5ce9dSEric Dumazet  *  RX rings only contains data buffers, not full skbs.
302b2b5ce9dSEric Dumazet  */
3032ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size)
304b2b5ce9dSEric Dumazet {
305b2b5ce9dSEric Dumazet 	struct skb_shared_info *shinfo;
306b2b5ce9dSEric Dumazet 	struct sk_buff *skb;
307d3836f21SEric Dumazet 	unsigned int size = frag_size ? : ksize(data);
308b2b5ce9dSEric Dumazet 
309b2b5ce9dSEric Dumazet 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
310b2b5ce9dSEric Dumazet 	if (!skb)
311b2b5ce9dSEric Dumazet 		return NULL;
312b2b5ce9dSEric Dumazet 
313d3836f21SEric Dumazet 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
314b2b5ce9dSEric Dumazet 
315b2b5ce9dSEric Dumazet 	memset(skb, 0, offsetof(struct sk_buff, tail));
316b2b5ce9dSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
317b2b5ce9dSEric Dumazet 	atomic_set(&skb->users, 1);
318b2b5ce9dSEric Dumazet 	skb->head = data;
319b2b5ce9dSEric Dumazet 	skb->data = data;
320b2b5ce9dSEric Dumazet 	skb_reset_tail_pointer(skb);
321b2b5ce9dSEric Dumazet 	skb->end = skb->tail + size;
32235d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
32335d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
324b2b5ce9dSEric Dumazet 
325b2b5ce9dSEric Dumazet 	/* make sure we initialize shinfo sequentially */
326b2b5ce9dSEric Dumazet 	shinfo = skb_shinfo(skb);
327b2b5ce9dSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
328b2b5ce9dSEric Dumazet 	atomic_set(&shinfo->dataref, 1);
329b2b5ce9dSEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
330b2b5ce9dSEric Dumazet 
331b2b5ce9dSEric Dumazet 	return skb;
332b2b5ce9dSEric Dumazet }
3332ea2f62cSEric Dumazet 
3342ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically
3352ea2f62cSEric Dumazet  * takes care of skb->head and skb->pfmemalloc
3362ea2f62cSEric Dumazet  * This means that if @frag_size is not zero, then @data must be backed
3372ea2f62cSEric Dumazet  * by a page fragment, not kmalloc() or vmalloc()
3382ea2f62cSEric Dumazet  */
3392ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
3402ea2f62cSEric Dumazet {
3412ea2f62cSEric Dumazet 	struct sk_buff *skb = __build_skb(data, frag_size);
3422ea2f62cSEric Dumazet 
3432ea2f62cSEric Dumazet 	if (skb && frag_size) {
3442ea2f62cSEric Dumazet 		skb->head_frag = 1;
3452f064f34SMichal Hocko 		if (page_is_pfmemalloc(virt_to_head_page(data)))
3462ea2f62cSEric Dumazet 			skb->pfmemalloc = 1;
3472ea2f62cSEric Dumazet 	}
3482ea2f62cSEric Dumazet 	return skb;
3492ea2f62cSEric Dumazet }
350b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
351b2b5ce9dSEric Dumazet 
352795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE	64
353795bb1c0SJesper Dangaard Brouer 
354795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache {
355795bb1c0SJesper Dangaard Brouer 	struct page_frag_cache page;
356e0d7924aSAlexey Dobriyan 	unsigned int skb_count;
357795bb1c0SJesper Dangaard Brouer 	void *skb_cache[NAPI_SKB_CACHE_SIZE];
358795bb1c0SJesper Dangaard Brouer };
359795bb1c0SJesper Dangaard Brouer 
360b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache);
361795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache);
362ffde7328SAlexander Duyck 
363ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
364ffde7328SAlexander Duyck {
365b63ae8caSAlexander Duyck 	struct page_frag_cache *nc;
366ffde7328SAlexander Duyck 	unsigned long flags;
367ffde7328SAlexander Duyck 	void *data;
368ffde7328SAlexander Duyck 
369ffde7328SAlexander Duyck 	local_irq_save(flags);
3709451980aSAlexander Duyck 	nc = this_cpu_ptr(&netdev_alloc_cache);
3718c2dd3e4SAlexander Duyck 	data = page_frag_alloc(nc, fragsz, gfp_mask);
3726f532612SEric Dumazet 	local_irq_restore(flags);
3736f532612SEric Dumazet 	return data;
3746f532612SEric Dumazet }
375c93bdd0eSMel Gorman 
376c93bdd0eSMel Gorman /**
377c93bdd0eSMel Gorman  * netdev_alloc_frag - allocate a page fragment
378c93bdd0eSMel Gorman  * @fragsz: fragment size
379c93bdd0eSMel Gorman  *
380c93bdd0eSMel Gorman  * Allocates a frag from a page for receive buffer.
381c93bdd0eSMel Gorman  * Uses GFP_ATOMIC allocations.
382c93bdd0eSMel Gorman  */
383c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz)
384c93bdd0eSMel Gorman {
385c93bdd0eSMel Gorman 	return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
386c93bdd0eSMel Gorman }
3876f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag);
3886f532612SEric Dumazet 
389ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
390ffde7328SAlexander Duyck {
391795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
3929451980aSAlexander Duyck 
3938c2dd3e4SAlexander Duyck 	return page_frag_alloc(&nc->page, fragsz, gfp_mask);
394ffde7328SAlexander Duyck }
395ffde7328SAlexander Duyck 
396ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz)
397ffde7328SAlexander Duyck {
398ffde7328SAlexander Duyck 	return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
399ffde7328SAlexander Duyck }
400ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag);
401ffde7328SAlexander Duyck 
4026f532612SEric Dumazet /**
403fd11a83dSAlexander Duyck  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
404fd11a83dSAlexander Duyck  *	@dev: network device to receive on
405d7499160SMasanari Iida  *	@len: length to allocate
406fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
407fd11a83dSAlexander Duyck  *
408fd11a83dSAlexander Duyck  *	Allocate a new &sk_buff and assign it a usage count of one. The
409fd11a83dSAlexander Duyck  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
410fd11a83dSAlexander Duyck  *	the headroom they think they need without accounting for the
411fd11a83dSAlexander Duyck  *	built in space. The built in space is used for optimisations.
412fd11a83dSAlexander Duyck  *
413fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
414fd11a83dSAlexander Duyck  */
4159451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len,
4169451980aSAlexander Duyck 				   gfp_t gfp_mask)
417fd11a83dSAlexander Duyck {
418b63ae8caSAlexander Duyck 	struct page_frag_cache *nc;
4199451980aSAlexander Duyck 	unsigned long flags;
420fd11a83dSAlexander Duyck 	struct sk_buff *skb;
4219451980aSAlexander Duyck 	bool pfmemalloc;
4229451980aSAlexander Duyck 	void *data;
423fd11a83dSAlexander Duyck 
4249451980aSAlexander Duyck 	len += NET_SKB_PAD;
425fd11a83dSAlexander Duyck 
4269451980aSAlexander Duyck 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
427d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
428a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
429a080e7bdSAlexander Duyck 		if (!skb)
430a080e7bdSAlexander Duyck 			goto skb_fail;
431a080e7bdSAlexander Duyck 		goto skb_success;
432a080e7bdSAlexander Duyck 	}
4339451980aSAlexander Duyck 
4349451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4359451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
4369451980aSAlexander Duyck 
4379451980aSAlexander Duyck 	if (sk_memalloc_socks())
4389451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
4399451980aSAlexander Duyck 
4409451980aSAlexander Duyck 	local_irq_save(flags);
4419451980aSAlexander Duyck 
4429451980aSAlexander Duyck 	nc = this_cpu_ptr(&netdev_alloc_cache);
4438c2dd3e4SAlexander Duyck 	data = page_frag_alloc(nc, len, gfp_mask);
4449451980aSAlexander Duyck 	pfmemalloc = nc->pfmemalloc;
4459451980aSAlexander Duyck 
4469451980aSAlexander Duyck 	local_irq_restore(flags);
4479451980aSAlexander Duyck 
4489451980aSAlexander Duyck 	if (unlikely(!data))
4499451980aSAlexander Duyck 		return NULL;
4509451980aSAlexander Duyck 
4519451980aSAlexander Duyck 	skb = __build_skb(data, len);
4529451980aSAlexander Duyck 	if (unlikely(!skb)) {
453181edb2bSAlexander Duyck 		skb_free_frag(data);
4549451980aSAlexander Duyck 		return NULL;
4559451980aSAlexander Duyck 	}
4569451980aSAlexander Duyck 
4579451980aSAlexander Duyck 	/* use OR instead of assignment to avoid clearing of bits in mask */
4589451980aSAlexander Duyck 	if (pfmemalloc)
4599451980aSAlexander Duyck 		skb->pfmemalloc = 1;
4609451980aSAlexander Duyck 	skb->head_frag = 1;
4619451980aSAlexander Duyck 
462a080e7bdSAlexander Duyck skb_success:
4638af27456SChristoph Hellwig 	skb_reserve(skb, NET_SKB_PAD);
4647b2e497aSChristoph Hellwig 	skb->dev = dev;
465fd11a83dSAlexander Duyck 
466a080e7bdSAlexander Duyck skb_fail:
4678af27456SChristoph Hellwig 	return skb;
4688af27456SChristoph Hellwig }
469b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
4701da177e4SLinus Torvalds 
471fd11a83dSAlexander Duyck /**
472fd11a83dSAlexander Duyck  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
473fd11a83dSAlexander Duyck  *	@napi: napi instance this buffer was allocated for
474d7499160SMasanari Iida  *	@len: length to allocate
475fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
476fd11a83dSAlexander Duyck  *
477fd11a83dSAlexander Duyck  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
478fd11a83dSAlexander Duyck  *	attempt to allocate the head from a special reserved region used
479fd11a83dSAlexander Duyck  *	only for NAPI Rx allocation.  By doing this we can save several
480fd11a83dSAlexander Duyck  *	CPU cycles by avoiding having to disable and re-enable IRQs.
481fd11a83dSAlexander Duyck  *
482fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
483fd11a83dSAlexander Duyck  */
4849451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len,
4859451980aSAlexander Duyck 				 gfp_t gfp_mask)
486fd11a83dSAlexander Duyck {
487795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
488fd11a83dSAlexander Duyck 	struct sk_buff *skb;
4899451980aSAlexander Duyck 	void *data;
490fd11a83dSAlexander Duyck 
4919451980aSAlexander Duyck 	len += NET_SKB_PAD + NET_IP_ALIGN;
492fd11a83dSAlexander Duyck 
4939451980aSAlexander Duyck 	if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) ||
494d0164adcSMel Gorman 	    (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) {
495a080e7bdSAlexander Duyck 		skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE);
496a080e7bdSAlexander Duyck 		if (!skb)
497a080e7bdSAlexander Duyck 			goto skb_fail;
498a080e7bdSAlexander Duyck 		goto skb_success;
499a080e7bdSAlexander Duyck 	}
5009451980aSAlexander Duyck 
5019451980aSAlexander Duyck 	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
5029451980aSAlexander Duyck 	len = SKB_DATA_ALIGN(len);
5039451980aSAlexander Duyck 
5049451980aSAlexander Duyck 	if (sk_memalloc_socks())
5059451980aSAlexander Duyck 		gfp_mask |= __GFP_MEMALLOC;
5069451980aSAlexander Duyck 
5078c2dd3e4SAlexander Duyck 	data = page_frag_alloc(&nc->page, len, gfp_mask);
5089451980aSAlexander Duyck 	if (unlikely(!data))
5099451980aSAlexander Duyck 		return NULL;
5109451980aSAlexander Duyck 
5119451980aSAlexander Duyck 	skb = __build_skb(data, len);
5129451980aSAlexander Duyck 	if (unlikely(!skb)) {
513181edb2bSAlexander Duyck 		skb_free_frag(data);
5149451980aSAlexander Duyck 		return NULL;
5159451980aSAlexander Duyck 	}
5169451980aSAlexander Duyck 
5179451980aSAlexander Duyck 	/* use OR instead of assignment to avoid clearing of bits in mask */
518795bb1c0SJesper Dangaard Brouer 	if (nc->page.pfmemalloc)
5199451980aSAlexander Duyck 		skb->pfmemalloc = 1;
5209451980aSAlexander Duyck 	skb->head_frag = 1;
5219451980aSAlexander Duyck 
522a080e7bdSAlexander Duyck skb_success:
523fd11a83dSAlexander Duyck 	skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
524fd11a83dSAlexander Duyck 	skb->dev = napi->dev;
525fd11a83dSAlexander Duyck 
526a080e7bdSAlexander Duyck skb_fail:
527fd11a83dSAlexander Duyck 	return skb;
528fd11a83dSAlexander Duyck }
529fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb);
530fd11a83dSAlexander Duyck 
531654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
53250269e19SEric Dumazet 		     int size, unsigned int truesize)
533654bed16SPeter Zijlstra {
534654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
535654bed16SPeter Zijlstra 	skb->len += size;
536654bed16SPeter Zijlstra 	skb->data_len += size;
53750269e19SEric Dumazet 	skb->truesize += truesize;
538654bed16SPeter Zijlstra }
539654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
540654bed16SPeter Zijlstra 
541f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
542f8e617e1SJason Wang 			  unsigned int truesize)
543f8e617e1SJason Wang {
544f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
545f8e617e1SJason Wang 
546f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
547f8e617e1SJason Wang 	skb->len += size;
548f8e617e1SJason Wang 	skb->data_len += size;
549f8e617e1SJason Wang 	skb->truesize += truesize;
550f8e617e1SJason Wang }
551f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
552f8e617e1SJason Wang 
55327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
5541da177e4SLinus Torvalds {
555bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
55627b437c8SHerbert Xu 	*listp = NULL;
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
55927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
56027b437c8SHerbert Xu {
56127b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
56227b437c8SHerbert Xu }
56327b437c8SHerbert Xu 
5641da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
5651da177e4SLinus Torvalds {
5661da177e4SLinus Torvalds 	struct sk_buff *list;
5671da177e4SLinus Torvalds 
568fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
5691da177e4SLinus Torvalds 		skb_get(list);
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
572d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
573d3836f21SEric Dumazet {
574181edb2bSAlexander Duyck 	unsigned char *head = skb->head;
575181edb2bSAlexander Duyck 
576d3836f21SEric Dumazet 	if (skb->head_frag)
577181edb2bSAlexander Duyck 		skb_free_frag(head);
578d3836f21SEric Dumazet 	else
579181edb2bSAlexander Duyck 		kfree(head);
580d3836f21SEric Dumazet }
581d3836f21SEric Dumazet 
5825bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
5831da177e4SLinus Torvalds {
584ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
5851da177e4SLinus Torvalds 	int i;
586ff04a771SEric Dumazet 
587ff04a771SEric Dumazet 	if (skb->cloned &&
588ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
589ff04a771SEric Dumazet 			      &shinfo->dataref))
590ff04a771SEric Dumazet 		return;
591ff04a771SEric Dumazet 
592ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
593ff04a771SEric Dumazet 		__skb_frag_unref(&shinfo->frags[i]);
5941da177e4SLinus Torvalds 
595a6686f2fSShirley Ma 	/*
596a6686f2fSShirley Ma 	 * If skb buf is from userspace, we need to notify the caller
597a6686f2fSShirley Ma 	 * the lower device DMA has done;
598a6686f2fSShirley Ma 	 */
599ff04a771SEric Dumazet 	if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
600a6686f2fSShirley Ma 		struct ubuf_info *uarg;
601a6686f2fSShirley Ma 
602ff04a771SEric Dumazet 		uarg = shinfo->destructor_arg;
603a6686f2fSShirley Ma 		if (uarg->callback)
604e19d6763SMichael S. Tsirkin 			uarg->callback(uarg, true);
605a6686f2fSShirley Ma 	}
606a6686f2fSShirley Ma 
607ff04a771SEric Dumazet 	if (shinfo->frag_list)
608ff04a771SEric Dumazet 		kfree_skb_list(shinfo->frag_list);
6091da177e4SLinus Torvalds 
610d3836f21SEric Dumazet 	skb_free_head(skb);
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds /*
6141da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
6151da177e4SLinus Torvalds  */
6162d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
6171da177e4SLinus Torvalds {
618d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
619d179cd12SDavid S. Miller 
620d179cd12SDavid S. Miller 	switch (skb->fclone) {
621d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
6221da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
6236ffe75ebSEric Dumazet 		return;
624d179cd12SDavid S. Miller 
625d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
626d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
6276ffe75ebSEric Dumazet 
6286ffe75ebSEric Dumazet 		/* We usually free the clone (TX completion) before original skb
6296ffe75ebSEric Dumazet 		 * This test would have no chance to be true for the clone,
6306ffe75ebSEric Dumazet 		 * while here, branch prediction will be good.
6316ffe75ebSEric Dumazet 		 */
6326ffe75ebSEric Dumazet 		if (atomic_read(&fclones->fclone_ref) == 1)
6336ffe75ebSEric Dumazet 			goto fastpath;
634d179cd12SDavid S. Miller 		break;
635d179cd12SDavid S. Miller 
6366ffe75ebSEric Dumazet 	default: /* SKB_FCLONE_CLONE */
637d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
638d179cd12SDavid S. Miller 		break;
6393ff50b79SStephen Hemminger 	}
6406ffe75ebSEric Dumazet 	if (!atomic_dec_and_test(&fclones->fclone_ref))
6416ffe75ebSEric Dumazet 		return;
6426ffe75ebSEric Dumazet fastpath:
6436ffe75ebSEric Dumazet 	kmem_cache_free(skbuff_fclone_cache, fclones);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
64604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
6471da177e4SLinus Torvalds {
648adf30907SEric Dumazet 	skb_dst_drop(skb);
6491da177e4SLinus Torvalds #ifdef CONFIG_XFRM
6501da177e4SLinus Torvalds 	secpath_put(skb->sp);
6511da177e4SLinus Torvalds #endif
6521da177e4SLinus Torvalds 	if (skb->destructor) {
6539c2b3328SStephen Hemminger 		WARN_ON(in_irq());
6541da177e4SLinus Torvalds 		skb->destructor(skb);
6551da177e4SLinus Torvalds 	}
656a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
657cb9c6836SFlorian Westphal 	nf_conntrack_put(skb_nfct(skb));
6582fc72c7bSKOVACS Krisztian #endif
6591109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
6601da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
6611da177e4SLinus Torvalds #endif
66204a4bb55SLennert Buytenhek }
66304a4bb55SLennert Buytenhek 
66404a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
66504a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
66604a4bb55SLennert Buytenhek {
66704a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
6685e71d9d7SPablo Neira 	if (likely(skb->head))
6692d4baff8SHerbert Xu 		skb_release_data(skb);
6702d4baff8SHerbert Xu }
6711da177e4SLinus Torvalds 
6722d4baff8SHerbert Xu /**
6732d4baff8SHerbert Xu  *	__kfree_skb - private function
6742d4baff8SHerbert Xu  *	@skb: buffer
6752d4baff8SHerbert Xu  *
6762d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
6772d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
6782d4baff8SHerbert Xu  *	always call kfree_skb
6792d4baff8SHerbert Xu  */
6802d4baff8SHerbert Xu 
6812d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
6822d4baff8SHerbert Xu {
6832d4baff8SHerbert Xu 	skb_release_all(skb);
6841da177e4SLinus Torvalds 	kfree_skbmem(skb);
6851da177e4SLinus Torvalds }
686b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds /**
689231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
690231d06aeSJörn Engel  *	@skb: buffer to free
691231d06aeSJörn Engel  *
692231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
693231d06aeSJörn Engel  *	hit zero.
694231d06aeSJörn Engel  */
695231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
696231d06aeSJörn Engel {
697231d06aeSJörn Engel 	if (unlikely(!skb))
698231d06aeSJörn Engel 		return;
699231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
700231d06aeSJörn Engel 		smp_rmb();
701231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
702231d06aeSJörn Engel 		return;
703ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
704231d06aeSJörn Engel 	__kfree_skb(skb);
705231d06aeSJörn Engel }
706b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
707231d06aeSJörn Engel 
708bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
709bd8a7036SEric Dumazet {
710bd8a7036SEric Dumazet 	while (segs) {
711bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
712bd8a7036SEric Dumazet 
713bd8a7036SEric Dumazet 		kfree_skb(segs);
714bd8a7036SEric Dumazet 		segs = next;
715bd8a7036SEric Dumazet 	}
716bd8a7036SEric Dumazet }
717bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
718bd8a7036SEric Dumazet 
719d1a203eaSStephen Hemminger /**
72025121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
72125121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
72225121173SMichael S. Tsirkin  *
72325121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
72425121173SMichael S. Tsirkin  *	skb must be freed afterwards.
72525121173SMichael S. Tsirkin  */
72625121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
72725121173SMichael S. Tsirkin {
72825121173SMichael S. Tsirkin 	if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
72925121173SMichael S. Tsirkin 		struct ubuf_info *uarg;
73025121173SMichael S. Tsirkin 
73125121173SMichael S. Tsirkin 		uarg = skb_shinfo(skb)->destructor_arg;
73225121173SMichael S. Tsirkin 		if (uarg->callback)
73325121173SMichael S. Tsirkin 			uarg->callback(uarg, false);
73425121173SMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
73525121173SMichael S. Tsirkin 	}
73625121173SMichael S. Tsirkin }
73725121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
73825121173SMichael S. Tsirkin 
73925121173SMichael S. Tsirkin /**
740ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
741ead2ceb0SNeil Horman  *	@skb: buffer to free
742ead2ceb0SNeil Horman  *
743ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
744ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
745ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
746ead2ceb0SNeil Horman  */
747ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
748ead2ceb0SNeil Horman {
749ead2ceb0SNeil Horman 	if (unlikely(!skb))
750ead2ceb0SNeil Horman 		return;
751ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
752ead2ceb0SNeil Horman 		smp_rmb();
753ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
754ead2ceb0SNeil Horman 		return;
75507dc22e7SKoki Sanagi 	trace_consume_skb(skb);
756ead2ceb0SNeil Horman 	__kfree_skb(skb);
757ead2ceb0SNeil Horman }
758ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
759ead2ceb0SNeil Horman 
760795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void)
761795bb1c0SJesper Dangaard Brouer {
762795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
763795bb1c0SJesper Dangaard Brouer 
764795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if containing objects */
765795bb1c0SJesper Dangaard Brouer 	if (nc->skb_count) {
766795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count,
767795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
768795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
769795bb1c0SJesper Dangaard Brouer 	}
770795bb1c0SJesper Dangaard Brouer }
771795bb1c0SJesper Dangaard Brouer 
77215fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb)
773795bb1c0SJesper Dangaard Brouer {
774795bb1c0SJesper Dangaard Brouer 	struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache);
775795bb1c0SJesper Dangaard Brouer 
776795bb1c0SJesper Dangaard Brouer 	/* drop skb->head and call any destructors for packet */
777795bb1c0SJesper Dangaard Brouer 	skb_release_all(skb);
778795bb1c0SJesper Dangaard Brouer 
779795bb1c0SJesper Dangaard Brouer 	/* record skb to CPU local list */
780795bb1c0SJesper Dangaard Brouer 	nc->skb_cache[nc->skb_count++] = skb;
781795bb1c0SJesper Dangaard Brouer 
782795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB
783795bb1c0SJesper Dangaard Brouer 	/* SLUB writes into objects when freeing */
784795bb1c0SJesper Dangaard Brouer 	prefetchw(skb);
785795bb1c0SJesper Dangaard Brouer #endif
786795bb1c0SJesper Dangaard Brouer 
787795bb1c0SJesper Dangaard Brouer 	/* flush skb_cache if it is filled */
788795bb1c0SJesper Dangaard Brouer 	if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) {
789795bb1c0SJesper Dangaard Brouer 		kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE,
790795bb1c0SJesper Dangaard Brouer 				     nc->skb_cache);
791795bb1c0SJesper Dangaard Brouer 		nc->skb_count = 0;
792795bb1c0SJesper Dangaard Brouer 	}
793795bb1c0SJesper Dangaard Brouer }
79415fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb)
79515fad714SJesper Dangaard Brouer {
79615fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
79715fad714SJesper Dangaard Brouer }
798795bb1c0SJesper Dangaard Brouer 
799795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget)
800795bb1c0SJesper Dangaard Brouer {
801795bb1c0SJesper Dangaard Brouer 	if (unlikely(!skb))
802795bb1c0SJesper Dangaard Brouer 		return;
803795bb1c0SJesper Dangaard Brouer 
804885eb0a5SJesper Dangaard Brouer 	/* Zero budget indicate non-NAPI context called us, like netpoll */
805795bb1c0SJesper Dangaard Brouer 	if (unlikely(!budget)) {
806885eb0a5SJesper Dangaard Brouer 		dev_consume_skb_any(skb);
807795bb1c0SJesper Dangaard Brouer 		return;
808795bb1c0SJesper Dangaard Brouer 	}
809795bb1c0SJesper Dangaard Brouer 
810795bb1c0SJesper Dangaard Brouer 	if (likely(atomic_read(&skb->users) == 1))
811795bb1c0SJesper Dangaard Brouer 		smp_rmb();
812795bb1c0SJesper Dangaard Brouer 	else if (likely(!atomic_dec_and_test(&skb->users)))
813795bb1c0SJesper Dangaard Brouer 		return;
814795bb1c0SJesper Dangaard Brouer 	/* if reaching here SKB is ready to free */
815795bb1c0SJesper Dangaard Brouer 	trace_consume_skb(skb);
816795bb1c0SJesper Dangaard Brouer 
817795bb1c0SJesper Dangaard Brouer 	/* if SKB is a clone, don't handle this case */
818abbdb5a7SEric Dumazet 	if (skb->fclone != SKB_FCLONE_UNAVAILABLE) {
819795bb1c0SJesper Dangaard Brouer 		__kfree_skb(skb);
820795bb1c0SJesper Dangaard Brouer 		return;
821795bb1c0SJesper Dangaard Brouer 	}
822795bb1c0SJesper Dangaard Brouer 
82315fad714SJesper Dangaard Brouer 	_kfree_skb_defer(skb);
824795bb1c0SJesper Dangaard Brouer }
825795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb);
826795bb1c0SJesper Dangaard Brouer 
827b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */
828b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
829b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
830b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_start));	\
831b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
832b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_end));	\
833b1937227SEric Dumazet 
834dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
835dec18810SHerbert Xu {
836dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
837b1937227SEric Dumazet 	/* We do not copy old->sk */
838dec18810SHerbert Xu 	new->dev		= old->dev;
839b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
8407fee226aSEric Dumazet 	skb_dst_copy(new, old);
841def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
842dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
843dec18810SHerbert Xu #endif
844b1937227SEric Dumazet 	__nf_copy(new, old, false);
8456aa895b0SPatrick McHardy 
846b1937227SEric Dumazet 	/* Note : this field could be in headers_start/headers_end section
847b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
848b1937227SEric Dumazet 	 */
849b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
85006021292SEliezer Tamir 
851b1937227SEric Dumazet 	memcpy(&new->headers_start, &old->headers_start,
852b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_end) -
853b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_start));
854b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
855b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
856b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
857b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
858b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
859b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
860b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
861b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
862b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
863b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
864b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
865b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
866b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
867b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
868b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
869b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
870b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
871b1937227SEric Dumazet #endif
872e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
873b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
87406021292SEliezer Tamir #endif
8752bd82484SEric Dumazet #ifdef CONFIG_XPS
8762bd82484SEric Dumazet 	CHECK_SKB_FIELD(sender_cpu);
8772bd82484SEric Dumazet #endif
878b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
879b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
880b1937227SEric Dumazet #endif
881b1937227SEric Dumazet 
882dec18810SHerbert Xu }
883dec18810SHerbert Xu 
88482c49a35SHerbert Xu /*
88582c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
88682c49a35SHerbert Xu  * __copy_skb_header above instead.
88782c49a35SHerbert Xu  */
888e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
8891da177e4SLinus Torvalds {
8901da177e4SLinus Torvalds #define C(x) n->x = skb->x
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 	n->next = n->prev = NULL;
8931da177e4SLinus Torvalds 	n->sk = NULL;
894dec18810SHerbert Xu 	__copy_skb_header(n, skb);
895dec18810SHerbert Xu 
8961da177e4SLinus Torvalds 	C(len);
8971da177e4SLinus Torvalds 	C(data_len);
8983e6b3b2eSAlexey Dobriyan 	C(mac_len);
899334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
90002f1c89dSPaul Moore 	n->cloned = 1;
9011da177e4SLinus Torvalds 	n->nohdr = 0;
9021da177e4SLinus Torvalds 	n->destructor = NULL;
9031da177e4SLinus Torvalds 	C(tail);
9041da177e4SLinus Torvalds 	C(end);
90502f1c89dSPaul Moore 	C(head);
906d3836f21SEric Dumazet 	C(head_frag);
90702f1c89dSPaul Moore 	C(data);
90802f1c89dSPaul Moore 	C(truesize);
90902f1c89dSPaul Moore 	atomic_set(&n->users, 1);
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
9121da177e4SLinus Torvalds 	skb->cloned = 1;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	return n;
915e0053ec0SHerbert Xu #undef C
916e0053ec0SHerbert Xu }
917e0053ec0SHerbert Xu 
918e0053ec0SHerbert Xu /**
919e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
920e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
921e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
922e0053ec0SHerbert Xu  *
923e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
924e0053ec0SHerbert Xu  *	supplied by the user.
925e0053ec0SHerbert Xu  *
926e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
927e0053ec0SHerbert Xu  */
928e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
929e0053ec0SHerbert Xu {
9302d4baff8SHerbert Xu 	skb_release_all(dst);
931e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
932e0053ec0SHerbert Xu }
933e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
934e0053ec0SHerbert Xu 
9352c53040fSBen Hutchings /**
9362c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
93748c83012SMichael S. Tsirkin  *	@skb: the skb to modify
93848c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
93948c83012SMichael S. Tsirkin  *
94048c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
94148c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
94248c83012SMichael S. Tsirkin  *	to userspace pages.
94348c83012SMichael S. Tsirkin  *
94448c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
94548c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
94648c83012SMichael S. Tsirkin  *
94748c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
94848c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
94948c83012SMichael S. Tsirkin  */
95048c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
951a6686f2fSShirley Ma {
952a6686f2fSShirley Ma 	int i;
953a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
954a6686f2fSShirley Ma 	struct page *page, *head = NULL;
955a6686f2fSShirley Ma 	struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
956a6686f2fSShirley Ma 
957a6686f2fSShirley Ma 	for (i = 0; i < num_frags; i++) {
958a6686f2fSShirley Ma 		u8 *vaddr;
959a6686f2fSShirley Ma 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
960a6686f2fSShirley Ma 
96102756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
962a6686f2fSShirley Ma 		if (!page) {
963a6686f2fSShirley Ma 			while (head) {
96440dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
965a6686f2fSShirley Ma 				put_page(head);
966a6686f2fSShirley Ma 				head = next;
967a6686f2fSShirley Ma 			}
968a6686f2fSShirley Ma 			return -ENOMEM;
969a6686f2fSShirley Ma 		}
97051c56b00SEric Dumazet 		vaddr = kmap_atomic(skb_frag_page(f));
971a6686f2fSShirley Ma 		memcpy(page_address(page),
9729e903e08SEric Dumazet 		       vaddr + f->page_offset, skb_frag_size(f));
97351c56b00SEric Dumazet 		kunmap_atomic(vaddr);
97440dadff2SSunghan Suh 		set_page_private(page, (unsigned long)head);
975a6686f2fSShirley Ma 		head = page;
976a6686f2fSShirley Ma 	}
977a6686f2fSShirley Ma 
978a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
97902756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
980a8605c60SIan Campbell 		skb_frag_unref(skb, i);
981a6686f2fSShirley Ma 
982e19d6763SMichael S. Tsirkin 	uarg->callback(uarg, false);
983a6686f2fSShirley Ma 
984a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
98502756ed4SKrishna Kumar 	for (i = num_frags - 1; i >= 0; i--) {
98602756ed4SKrishna Kumar 		__skb_fill_page_desc(skb, i, head, 0,
98702756ed4SKrishna Kumar 				     skb_shinfo(skb)->frags[i].size);
98840dadff2SSunghan Suh 		head = (struct page *)page_private(head);
989a6686f2fSShirley Ma 	}
99048c83012SMichael S. Tsirkin 
99148c83012SMichael S. Tsirkin 	skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
992a6686f2fSShirley Ma 	return 0;
993a6686f2fSShirley Ma }
994dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
995a6686f2fSShirley Ma 
996e0053ec0SHerbert Xu /**
997e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
998e0053ec0SHerbert Xu  *	@skb: buffer to clone
999e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
1000e0053ec0SHerbert Xu  *
1001e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
1002e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
1003e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
1004e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
1005e0053ec0SHerbert Xu  *
1006e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
1007e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
1008e0053ec0SHerbert Xu  */
1009e0053ec0SHerbert Xu 
1010e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
1011e0053ec0SHerbert Xu {
1012d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
1013d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
1014d0bf4a9eSEric Dumazet 						       skb1);
10156ffe75ebSEric Dumazet 	struct sk_buff *n;
1016e0053ec0SHerbert Xu 
101770008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
1018a6686f2fSShirley Ma 		return NULL;
1019a6686f2fSShirley Ma 
1020e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
10216ffe75ebSEric Dumazet 	    atomic_read(&fclones->fclone_ref) == 1) {
10226ffe75ebSEric Dumazet 		n = &fclones->skb2;
10236ffe75ebSEric Dumazet 		atomic_set(&fclones->fclone_ref, 2);
1024e0053ec0SHerbert Xu 	} else {
1025c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
1026c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
1027c93bdd0eSMel Gorman 
1028e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
1029e0053ec0SHerbert Xu 		if (!n)
1030e0053ec0SHerbert Xu 			return NULL;
1031fe55f6d5SVegard Nossum 
1032fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags1);
1033e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
1034e0053ec0SHerbert Xu 	}
1035e0053ec0SHerbert Xu 
1036e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
10371da177e4SLinus Torvalds }
1038b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
10391da177e4SLinus Torvalds 
1040f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off)
1041f5b17294SPravin B Shelar {
1042030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
1043030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1044030737bcSEric Dumazet 		skb->csum_start += off;
1045f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
1046f5b17294SPravin B Shelar 	skb->transport_header += off;
1047f5b17294SPravin B Shelar 	skb->network_header   += off;
1048f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
1049f5b17294SPravin B Shelar 		skb->mac_header += off;
1050f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
1051f5b17294SPravin B Shelar 	skb->inner_network_header += off;
1052aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
1053f5b17294SPravin B Shelar }
1054f5b17294SPravin B Shelar 
10551da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
10561da177e4SLinus Torvalds {
1057dec18810SHerbert Xu 	__copy_skb_header(new, old);
1058dec18810SHerbert Xu 
10597967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
10607967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
10617967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
1064c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1065c93bdd0eSMel Gorman {
1066c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1067c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
1068c93bdd0eSMel Gorman 	return 0;
1069c93bdd0eSMel Gorman }
1070c93bdd0eSMel Gorman 
10711da177e4SLinus Torvalds /**
10721da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
10731da177e4SLinus Torvalds  *	@skb: buffer to copy
10741da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
10751da177e4SLinus Torvalds  *
10761da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
10771da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
10781da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
10791da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
10801da177e4SLinus Torvalds  *
10811da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
10821da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
10831da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
10841da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
10851da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
10861da177e4SLinus Torvalds  */
10871da177e4SLinus Torvalds 
1088dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
10891da177e4SLinus Torvalds {
10906602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
1091ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1092c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1093c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
10946602cebbSEric Dumazet 
10951da177e4SLinus Torvalds 	if (!n)
10961da177e4SLinus Torvalds 		return NULL;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	/* Set the data pointer */
10991da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
11001da177e4SLinus Torvalds 	/* Set the tail pointer and length */
11011da177e4SLinus Torvalds 	skb_put(n, skb->len);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
11041da177e4SLinus Torvalds 		BUG();
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	copy_skb_header(n, skb);
11071da177e4SLinus Torvalds 	return n;
11081da177e4SLinus Torvalds }
1109b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds /**
1112bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
11131da177e4SLinus Torvalds  *	@skb: buffer to copy
1114117632e6SEric Dumazet  *	@headroom: headroom of new skb
11151da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
1116bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
1117bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
1118bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
11191da177e4SLinus Torvalds  *
11201da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
11211da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
11221da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
11231da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
11241da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
11251da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
11261da177e4SLinus Torvalds  */
11271da177e4SLinus Torvalds 
1128bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1129bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
11301da177e4SLinus Torvalds {
1131117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
1132bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1133bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
11346602cebbSEric Dumazet 
11351da177e4SLinus Torvalds 	if (!n)
11361da177e4SLinus Torvalds 		goto out;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	/* Set the data pointer */
1139117632e6SEric Dumazet 	skb_reserve(n, headroom);
11401da177e4SLinus Torvalds 	/* Set the tail pointer and length */
11411da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
11421da177e4SLinus Torvalds 	/* Copy the bytes */
1143d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
11441da177e4SLinus Torvalds 
114525f484a6SHerbert Xu 	n->truesize += skb->data_len;
11461da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
11471da177e4SLinus Torvalds 	n->len	     = skb->len;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
11501da177e4SLinus Torvalds 		int i;
11511da177e4SLinus Torvalds 
115270008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask)) {
11531511022cSDan Carpenter 			kfree_skb(n);
11541511022cSDan Carpenter 			n = NULL;
1155a6686f2fSShirley Ma 			goto out;
1156a6686f2fSShirley Ma 		}
11571da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
11581da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1159ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
11601da177e4SLinus Torvalds 		}
11611da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
11621da177e4SLinus Torvalds 	}
11631da177e4SLinus Torvalds 
116421dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
11651da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
11661da177e4SLinus Torvalds 		skb_clone_fraglist(n);
11671da177e4SLinus Torvalds 	}
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	copy_skb_header(n, skb);
11701da177e4SLinus Torvalds out:
11711da177e4SLinus Torvalds 	return n;
11721da177e4SLinus Torvalds }
1173bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds /**
11761da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
11771da177e4SLinus Torvalds  *	@skb: buffer to reallocate
11781da177e4SLinus Torvalds  *	@nhead: room to add at head
11791da177e4SLinus Torvalds  *	@ntail: room to add at tail
11801da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
11811da177e4SLinus Torvalds  *
1182bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1183bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
11841da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
11851da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
11861da177e4SLinus Torvalds  *
11871da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
11881da177e4SLinus Torvalds  *	reloaded after call to this function.
11891da177e4SLinus Torvalds  */
11901da177e4SLinus Torvalds 
119186a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1192dd0fc66fSAl Viro 		     gfp_t gfp_mask)
11931da177e4SLinus Torvalds {
1194158f323bSEric Dumazet 	int i, osize = skb_end_offset(skb);
1195158f323bSEric Dumazet 	int size = osize + nhead + ntail;
11961da177e4SLinus Torvalds 	long off;
1197158f323bSEric Dumazet 	u8 *data;
11981da177e4SLinus Torvalds 
11994edd87adSHerbert Xu 	BUG_ON(nhead < 0);
12004edd87adSHerbert Xu 
12011da177e4SLinus Torvalds 	if (skb_shared(skb))
12021da177e4SLinus Torvalds 		BUG();
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
12051da177e4SLinus Torvalds 
1206c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1207c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1208c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1209c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
12101da177e4SLinus Torvalds 	if (!data)
12111da177e4SLinus Torvalds 		goto nodata;
121287151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
12156602cebbSEric Dumazet 	 * optimized for the cases when header is void.
12166602cebbSEric Dumazet 	 */
12176602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
12186602cebbSEric Dumazet 
12196602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
12206602cebbSEric Dumazet 	       skb_shinfo(skb),
1221fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
12221da177e4SLinus Torvalds 
12233e24591aSAlexander Duyck 	/*
12243e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
12253e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
12263e24591aSAlexander Duyck 	 * be since all we did is relocate the values
12273e24591aSAlexander Duyck 	 */
12283e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
1229a6686f2fSShirley Ma 		/* copy this zero copy skb frags */
123070008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1231a6686f2fSShirley Ma 			goto nofrags;
12321da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1233ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
12341da177e4SLinus Torvalds 
123521dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
12361da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 		skb_release_data(skb);
12393e24591aSAlexander Duyck 	} else {
12403e24591aSAlexander Duyck 		skb_free_head(skb);
12411fd63041SEric Dumazet 	}
12421da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	skb->head     = data;
1245d3836f21SEric Dumazet 	skb->head_frag = 0;
12461da177e4SLinus Torvalds 	skb->data    += off;
12474305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
12484305b541SArnaldo Carvalho de Melo 	skb->end      = size;
124956eb8882SPatrick McHardy 	off           = nhead;
12504305b541SArnaldo Carvalho de Melo #else
12514305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
125256eb8882SPatrick McHardy #endif
125327a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1254b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
12551da177e4SLinus Torvalds 	skb->cloned   = 0;
1256334a8132SPatrick McHardy 	skb->hdr_len  = 0;
12571da177e4SLinus Torvalds 	skb->nohdr    = 0;
12581da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
1259158f323bSEric Dumazet 
1260158f323bSEric Dumazet 	/* It is not generally safe to change skb->truesize.
1261158f323bSEric Dumazet 	 * For the moment, we really care of rx path, or
1262158f323bSEric Dumazet 	 * when skb is orphaned (not attached to a socket).
1263158f323bSEric Dumazet 	 */
1264158f323bSEric Dumazet 	if (!skb->sk || skb->destructor == sock_edemux)
1265158f323bSEric Dumazet 		skb->truesize += size - osize;
1266158f323bSEric Dumazet 
12671da177e4SLinus Torvalds 	return 0;
12681da177e4SLinus Torvalds 
1269a6686f2fSShirley Ma nofrags:
1270a6686f2fSShirley Ma 	kfree(data);
12711da177e4SLinus Torvalds nodata:
12721da177e4SLinus Torvalds 	return -ENOMEM;
12731da177e4SLinus Torvalds }
1274b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
12791da177e4SLinus Torvalds {
12801da177e4SLinus Torvalds 	struct sk_buff *skb2;
12811da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if (delta <= 0)
12841da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
12851da177e4SLinus Torvalds 	else {
12861da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
12871da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
12881da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
12891da177e4SLinus Torvalds 			kfree_skb(skb2);
12901da177e4SLinus Torvalds 			skb2 = NULL;
12911da177e4SLinus Torvalds 		}
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds 	return skb2;
12941da177e4SLinus Torvalds }
1295b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds /**
12981da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
12991da177e4SLinus Torvalds  *	@skb: buffer to copy
13001da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
13011da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
13021da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
13031da177e4SLinus Torvalds  *
13041da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
13051da177e4SLinus Torvalds  *	allocate additional space.
13061da177e4SLinus Torvalds  *
13071da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
13081da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
13091da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
13101da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
13111da177e4SLinus Torvalds  *
13121da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
13131da177e4SLinus Torvalds  *	is called from an interrupt.
13141da177e4SLinus Torvalds  */
13151da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
131686a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1317dd0fc66fSAl Viro 				gfp_t gfp_mask)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	/*
13201da177e4SLinus Torvalds 	 *	Allocate the copy buffer
13211da177e4SLinus Torvalds 	 */
1322c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1323c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1324c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1325efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
13261da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	if (!n)
13291da177e4SLinus Torvalds 		return NULL;
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	/* Set the tail pointer and length */
13341da177e4SLinus Torvalds 	skb_put(n, skb->len);
13351da177e4SLinus Torvalds 
1336efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
13371da177e4SLinus Torvalds 	head_copy_off = 0;
13381da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
13391da177e4SLinus Torvalds 		head_copy_len = newheadroom;
13401da177e4SLinus Torvalds 	else
13411da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	/* Copy the linear header and data. */
13441da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
13451da177e4SLinus Torvalds 			  skb->len + head_copy_len))
13461da177e4SLinus Torvalds 		BUG();
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	copy_skb_header(n, skb);
13491da177e4SLinus Torvalds 
1350030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1351efd1e8d5SPatrick McHardy 
13521da177e4SLinus Torvalds 	return n;
13531da177e4SLinus Torvalds }
1354b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds /**
13571da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
13581da177e4SLinus Torvalds  *	@skb: buffer to pad
13591da177e4SLinus Torvalds  *	@pad: space to pad
13601da177e4SLinus Torvalds  *
13611da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
13621da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
13631da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
13641da177e4SLinus Torvalds  *
13655b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
13661da177e4SLinus Torvalds  */
13671da177e4SLinus Torvalds 
13685b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
13691da177e4SLinus Torvalds {
13705b057c6bSHerbert Xu 	int err;
13715b057c6bSHerbert Xu 	int ntail;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
13745b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
13751da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
13765b057c6bSHerbert Xu 		return 0;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13794305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
13805b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
13815b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
13825b057c6bSHerbert Xu 		if (unlikely(err))
13835b057c6bSHerbert Xu 			goto free_skb;
13845b057c6bSHerbert Xu 	}
13855b057c6bSHerbert Xu 
13865b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
13875b057c6bSHerbert Xu 	 * to be audited.
13885b057c6bSHerbert Xu 	 */
13895b057c6bSHerbert Xu 	err = skb_linearize(skb);
13905b057c6bSHerbert Xu 	if (unlikely(err))
13915b057c6bSHerbert Xu 		goto free_skb;
13925b057c6bSHerbert Xu 
13935b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
13945b057c6bSHerbert Xu 	return 0;
13955b057c6bSHerbert Xu 
13965b057c6bSHerbert Xu free_skb:
13971da177e4SLinus Torvalds 	kfree_skb(skb);
13985b057c6bSHerbert Xu 	return err;
13991da177e4SLinus Torvalds }
1400b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
14011da177e4SLinus Torvalds 
14020dde3e16SIlpo Järvinen /**
14030c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
14040c7ddf36SMathias Krause  *	@skb: start of the buffer to use
14050c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
14060c7ddf36SMathias Krause  *	@len: amount of data to add
14070c7ddf36SMathias Krause  *
14080c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
14090c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
14100c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
14110c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
14120c7ddf36SMathias Krause  *	returned.
14130c7ddf36SMathias Krause  */
14140c7ddf36SMathias Krause 
14150c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
14160c7ddf36SMathias Krause {
14170c7ddf36SMathias Krause 	if (tail != skb) {
14180c7ddf36SMathias Krause 		skb->data_len += len;
14190c7ddf36SMathias Krause 		skb->len += len;
14200c7ddf36SMathias Krause 	}
14210c7ddf36SMathias Krause 	return skb_put(tail, len);
14220c7ddf36SMathias Krause }
14230c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
14240c7ddf36SMathias Krause 
14250c7ddf36SMathias Krause /**
14260dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
14270dde3e16SIlpo Järvinen  *	@skb: buffer to use
14280dde3e16SIlpo Järvinen  *	@len: amount of data to add
14290dde3e16SIlpo Järvinen  *
14300dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
14310dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
14320dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
14330dde3e16SIlpo Järvinen  */
14340dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
14350dde3e16SIlpo Järvinen {
14360dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
14370dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
14380dde3e16SIlpo Järvinen 	skb->tail += len;
14390dde3e16SIlpo Järvinen 	skb->len  += len;
14400dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
14410dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
14420dde3e16SIlpo Järvinen 	return tmp;
14430dde3e16SIlpo Järvinen }
14440dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
14450dde3e16SIlpo Järvinen 
14466be8ac2fSIlpo Järvinen /**
1447c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1448c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1449c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1450c2aa270aSIlpo Järvinen  *
1451c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1452c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1453c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1454c2aa270aSIlpo Järvinen  */
1455c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1456c2aa270aSIlpo Järvinen {
1457c2aa270aSIlpo Järvinen 	skb->data -= len;
1458c2aa270aSIlpo Järvinen 	skb->len  += len;
1459c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1460c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1461c2aa270aSIlpo Järvinen 	return skb->data;
1462c2aa270aSIlpo Järvinen }
1463c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1464c2aa270aSIlpo Järvinen 
1465c2aa270aSIlpo Järvinen /**
14666be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
14676be8ac2fSIlpo Järvinen  *	@skb: buffer to use
14686be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
14696be8ac2fSIlpo Järvinen  *
14706be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
14716be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
14726be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
14736be8ac2fSIlpo Järvinen  *	the old data.
14746be8ac2fSIlpo Järvinen  */
14756be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
14766be8ac2fSIlpo Järvinen {
147747d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
14786be8ac2fSIlpo Järvinen }
14796be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
14806be8ac2fSIlpo Järvinen 
1481419ae74eSIlpo Järvinen /**
1482419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1483419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1484419ae74eSIlpo Järvinen  *	@len: new length
1485419ae74eSIlpo Järvinen  *
1486419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1487419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1488419ae74eSIlpo Järvinen  *	The skb must be linear.
1489419ae74eSIlpo Järvinen  */
1490419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1491419ae74eSIlpo Järvinen {
1492419ae74eSIlpo Järvinen 	if (skb->len > len)
1493419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1494419ae74eSIlpo Järvinen }
1495419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1496419ae74eSIlpo Järvinen 
14973cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
14981da177e4SLinus Torvalds  */
14991da177e4SLinus Torvalds 
15003cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
15011da177e4SLinus Torvalds {
150227b437c8SHerbert Xu 	struct sk_buff **fragp;
150327b437c8SHerbert Xu 	struct sk_buff *frag;
15041da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
15051da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
15061da177e4SLinus Torvalds 	int i;
150727b437c8SHerbert Xu 	int err;
150827b437c8SHerbert Xu 
150927b437c8SHerbert Xu 	if (skb_cloned(skb) &&
151027b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
151127b437c8SHerbert Xu 		return err;
15121da177e4SLinus Torvalds 
1513f4d26fb3SHerbert Xu 	i = 0;
1514f4d26fb3SHerbert Xu 	if (offset >= len)
1515f4d26fb3SHerbert Xu 		goto drop_pages;
1516f4d26fb3SHerbert Xu 
1517f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
15189e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
151927b437c8SHerbert Xu 
152027b437c8SHerbert Xu 		if (end < len) {
15211da177e4SLinus Torvalds 			offset = end;
152227b437c8SHerbert Xu 			continue;
15231da177e4SLinus Torvalds 		}
15241da177e4SLinus Torvalds 
15259e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
152627b437c8SHerbert Xu 
1527f4d26fb3SHerbert Xu drop_pages:
152827b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
152927b437c8SHerbert Xu 
153027b437c8SHerbert Xu 		for (; i < nfrags; i++)
1531ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
153227b437c8SHerbert Xu 
153321dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
153427b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1535f4d26fb3SHerbert Xu 		goto done;
153627b437c8SHerbert Xu 	}
153727b437c8SHerbert Xu 
153827b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
153927b437c8SHerbert Xu 	     fragp = &frag->next) {
154027b437c8SHerbert Xu 		int end = offset + frag->len;
154127b437c8SHerbert Xu 
154227b437c8SHerbert Xu 		if (skb_shared(frag)) {
154327b437c8SHerbert Xu 			struct sk_buff *nfrag;
154427b437c8SHerbert Xu 
154527b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
154627b437c8SHerbert Xu 			if (unlikely(!nfrag))
154727b437c8SHerbert Xu 				return -ENOMEM;
154827b437c8SHerbert Xu 
154927b437c8SHerbert Xu 			nfrag->next = frag->next;
155085bb2a60SEric Dumazet 			consume_skb(frag);
155127b437c8SHerbert Xu 			frag = nfrag;
155227b437c8SHerbert Xu 			*fragp = frag;
155327b437c8SHerbert Xu 		}
155427b437c8SHerbert Xu 
155527b437c8SHerbert Xu 		if (end < len) {
155627b437c8SHerbert Xu 			offset = end;
155727b437c8SHerbert Xu 			continue;
155827b437c8SHerbert Xu 		}
155927b437c8SHerbert Xu 
156027b437c8SHerbert Xu 		if (end > len &&
156127b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
156227b437c8SHerbert Xu 			return err;
156327b437c8SHerbert Xu 
156427b437c8SHerbert Xu 		if (frag->next)
156527b437c8SHerbert Xu 			skb_drop_list(&frag->next);
156627b437c8SHerbert Xu 		break;
156727b437c8SHerbert Xu 	}
156827b437c8SHerbert Xu 
1569f4d26fb3SHerbert Xu done:
157027b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
15711da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
15721da177e4SLinus Torvalds 		skb->len       = len;
15731da177e4SLinus Torvalds 	} else {
15741da177e4SLinus Torvalds 		skb->len       = len;
15751da177e4SLinus Torvalds 		skb->data_len  = 0;
157627a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
15771da177e4SLinus Torvalds 	}
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	return 0;
15801da177e4SLinus Torvalds }
1581b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds /**
15841da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
15851da177e4SLinus Torvalds  *	@skb: buffer to reallocate
15861da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
15871da177e4SLinus Torvalds  *
15881da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
15891da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
15901da177e4SLinus Torvalds  *	data from fragmented part.
15911da177e4SLinus Torvalds  *
15921da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
15931da177e4SLinus Torvalds  *
15941da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
15951da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
15961da177e4SLinus Torvalds  *
15971da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
15981da177e4SLinus Torvalds  *	reloaded after call to this function.
15991da177e4SLinus Torvalds  */
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
16021da177e4SLinus Torvalds  * when it is necessary.
16031da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
16041da177e4SLinus Torvalds  * 2. It may change skb pointers.
16051da177e4SLinus Torvalds  *
16061da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
16071da177e4SLinus Torvalds  */
16081da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
16091da177e4SLinus Torvalds {
16101da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
16111da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
16121da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
16131da177e4SLinus Torvalds 	 */
16144305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
16171da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
16181da177e4SLinus Torvalds 				     GFP_ATOMIC))
16191da177e4SLinus Torvalds 			return NULL;
16201da177e4SLinus Torvalds 	}
16211da177e4SLinus Torvalds 
162227a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
16231da177e4SLinus Torvalds 		BUG();
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
16261da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
16271da177e4SLinus Torvalds 	 */
162821dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
16291da177e4SLinus Torvalds 		goto pull_pages;
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
16321da177e4SLinus Torvalds 	eat = delta;
16331da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16349e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
16359e903e08SEric Dumazet 
16369e903e08SEric Dumazet 		if (size >= eat)
16371da177e4SLinus Torvalds 			goto pull_pages;
16389e903e08SEric Dumazet 		eat -= size;
16391da177e4SLinus Torvalds 	}
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
16421da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
16431da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
16441da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
16451da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
16461da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
16471da177e4SLinus Torvalds 	 */
16481da177e4SLinus Torvalds 	if (eat) {
16491da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
16501da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
16511da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 		do {
165409a62660SKris Katterjohn 			BUG_ON(!list);
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 			if (list->len <= eat) {
16571da177e4SLinus Torvalds 				/* Eaten as whole. */
16581da177e4SLinus Torvalds 				eat -= list->len;
16591da177e4SLinus Torvalds 				list = list->next;
16601da177e4SLinus Torvalds 				insp = list;
16611da177e4SLinus Torvalds 			} else {
16621da177e4SLinus Torvalds 				/* Eaten partially. */
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds 				if (skb_shared(list)) {
16651da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
16661da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
16671da177e4SLinus Torvalds 					if (!clone)
16681da177e4SLinus Torvalds 						return NULL;
16691da177e4SLinus Torvalds 					insp = list->next;
16701da177e4SLinus Torvalds 					list = clone;
16711da177e4SLinus Torvalds 				} else {
16721da177e4SLinus Torvalds 					/* This may be pulled without
16731da177e4SLinus Torvalds 					 * problems. */
16741da177e4SLinus Torvalds 					insp = list;
16751da177e4SLinus Torvalds 				}
16761da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
16771da177e4SLinus Torvalds 					kfree_skb(clone);
16781da177e4SLinus Torvalds 					return NULL;
16791da177e4SLinus Torvalds 				}
16801da177e4SLinus Torvalds 				break;
16811da177e4SLinus Torvalds 			}
16821da177e4SLinus Torvalds 		} while (eat);
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds 		/* Free pulled out fragments. */
16851da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
16861da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
16871da177e4SLinus Torvalds 			kfree_skb(list);
16881da177e4SLinus Torvalds 		}
16891da177e4SLinus Torvalds 		/* And insert new clone at head. */
16901da177e4SLinus Torvalds 		if (clone) {
16911da177e4SLinus Torvalds 			clone->next = list;
16921da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
16931da177e4SLinus Torvalds 		}
16941da177e4SLinus Torvalds 	}
16951da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds pull_pages:
16981da177e4SLinus Torvalds 	eat = delta;
16991da177e4SLinus Torvalds 	k = 0;
17001da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
17019e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
17029e903e08SEric Dumazet 
17039e903e08SEric Dumazet 		if (size <= eat) {
1704ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
17059e903e08SEric Dumazet 			eat -= size;
17061da177e4SLinus Torvalds 		} else {
17071da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
17081da177e4SLinus Torvalds 			if (eat) {
17091da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
17109e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
17111da177e4SLinus Torvalds 				eat = 0;
17121da177e4SLinus Torvalds 			}
17131da177e4SLinus Torvalds 			k++;
17141da177e4SLinus Torvalds 		}
17151da177e4SLinus Torvalds 	}
17161da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	skb->tail     += delta;
17191da177e4SLinus Torvalds 	skb->data_len -= delta;
17201da177e4SLinus Torvalds 
172127a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
17221da177e4SLinus Torvalds }
1723b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
17241da177e4SLinus Torvalds 
172522019b17SEric Dumazet /**
172622019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
172722019b17SEric Dumazet  *	@skb: source skb
172822019b17SEric Dumazet  *	@offset: offset in source
172922019b17SEric Dumazet  *	@to: destination buffer
173022019b17SEric Dumazet  *	@len: number of bytes to copy
173122019b17SEric Dumazet  *
173222019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
173322019b17SEric Dumazet  *	destination buffer.
173422019b17SEric Dumazet  *
173522019b17SEric Dumazet  *	CAUTION ! :
173622019b17SEric Dumazet  *		If its prototype is ever changed,
173722019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
173822019b17SEric Dumazet  *		since it is called from BPF assembly code.
173922019b17SEric Dumazet  */
17401da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
17411da177e4SLinus Torvalds {
17421a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1743fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1744fbb398a8SDavid S. Miller 	int i, copy;
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
17471da177e4SLinus Torvalds 		goto fault;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	/* Copy header. */
17501a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
17511da177e4SLinus Torvalds 		if (copy > len)
17521da177e4SLinus Torvalds 			copy = len;
1753d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
17541da177e4SLinus Torvalds 		if ((len -= copy) == 0)
17551da177e4SLinus Torvalds 			return 0;
17561da177e4SLinus Torvalds 		offset += copy;
17571da177e4SLinus Torvalds 		to     += copy;
17581da177e4SLinus Torvalds 	}
17591da177e4SLinus Torvalds 
17601da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
17611a028e50SDavid S. Miller 		int end;
176251c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
17631da177e4SLinus Torvalds 
1764547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17651a028e50SDavid S. Miller 
176651c56b00SEric Dumazet 		end = start + skb_frag_size(f);
17671da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17681da177e4SLinus Torvalds 			u8 *vaddr;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 			if (copy > len)
17711da177e4SLinus Torvalds 				copy = len;
17721da177e4SLinus Torvalds 
177351c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(f));
17741da177e4SLinus Torvalds 			memcpy(to,
177551c56b00SEric Dumazet 			       vaddr + f->page_offset + offset - start,
177651c56b00SEric Dumazet 			       copy);
177751c56b00SEric Dumazet 			kunmap_atomic(vaddr);
17781da177e4SLinus Torvalds 
17791da177e4SLinus Torvalds 			if ((len -= copy) == 0)
17801da177e4SLinus Torvalds 				return 0;
17811da177e4SLinus Torvalds 			offset += copy;
17821da177e4SLinus Torvalds 			to     += copy;
17831da177e4SLinus Torvalds 		}
17841a028e50SDavid S. Miller 		start = end;
17851da177e4SLinus Torvalds 	}
17861da177e4SLinus Torvalds 
1787fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
17881a028e50SDavid S. Miller 		int end;
17891da177e4SLinus Torvalds 
1790547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17911a028e50SDavid S. Miller 
1792fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
17931da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17941da177e4SLinus Torvalds 			if (copy > len)
17951da177e4SLinus Torvalds 				copy = len;
1796fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
17971da177e4SLinus Torvalds 				goto fault;
17981da177e4SLinus Torvalds 			if ((len -= copy) == 0)
17991da177e4SLinus Torvalds 				return 0;
18001da177e4SLinus Torvalds 			offset += copy;
18011da177e4SLinus Torvalds 			to     += copy;
18021da177e4SLinus Torvalds 		}
18031a028e50SDavid S. Miller 		start = end;
18041da177e4SLinus Torvalds 	}
1805a6686f2fSShirley Ma 
18061da177e4SLinus Torvalds 	if (!len)
18071da177e4SLinus Torvalds 		return 0;
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds fault:
18101da177e4SLinus Torvalds 	return -EFAULT;
18111da177e4SLinus Torvalds }
1812b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
18131da177e4SLinus Torvalds 
18149c55e01cSJens Axboe /*
18159c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
18169c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
18179c55e01cSJens Axboe  */
18189c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
18199c55e01cSJens Axboe {
18208b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
18218b9d3728SJarek Poplawski }
18229c55e01cSJens Axboe 
1823a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
18244fb66994SJarek Poplawski 				   unsigned int *offset,
182518aafc62SEric Dumazet 				   struct sock *sk)
18268b9d3728SJarek Poplawski {
18275640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
18288b9d3728SJarek Poplawski 
18295640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
18308b9d3728SJarek Poplawski 		return NULL;
18314fb66994SJarek Poplawski 
18325640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
18334fb66994SJarek Poplawski 
18345640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
18355640f768SEric Dumazet 	       page_address(page) + *offset, *len);
18365640f768SEric Dumazet 	*offset = pfrag->offset;
18375640f768SEric Dumazet 	pfrag->offset += *len;
18384fb66994SJarek Poplawski 
18395640f768SEric Dumazet 	return pfrag->page;
18409c55e01cSJens Axboe }
18419c55e01cSJens Axboe 
184241c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
184341c73a0dSEric Dumazet 			     struct page *page,
184441c73a0dSEric Dumazet 			     unsigned int offset)
184541c73a0dSEric Dumazet {
184641c73a0dSEric Dumazet 	return	spd->nr_pages &&
184741c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
184841c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
184941c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
185041c73a0dSEric Dumazet }
185141c73a0dSEric Dumazet 
18529c55e01cSJens Axboe /*
18539c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
18549c55e01cSJens Axboe  */
1855a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
185635f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
18574fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
185818aafc62SEric Dumazet 			  bool linear,
18597a67e56fSJarek Poplawski 			  struct sock *sk)
18609c55e01cSJens Axboe {
186141c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1862a108d5f3SDavid S. Miller 		return true;
18639c55e01cSJens Axboe 
18648b9d3728SJarek Poplawski 	if (linear) {
186518aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
18668b9d3728SJarek Poplawski 		if (!page)
1867a108d5f3SDavid S. Miller 			return true;
186841c73a0dSEric Dumazet 	}
186941c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
187041c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
1871a108d5f3SDavid S. Miller 		return false;
187241c73a0dSEric Dumazet 	}
18738b9d3728SJarek Poplawski 	get_page(page);
18749c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
18754fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
18769c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
18779c55e01cSJens Axboe 	spd->nr_pages++;
18788b9d3728SJarek Poplawski 
1879a108d5f3SDavid S. Miller 	return false;
18809c55e01cSJens Axboe }
18819c55e01cSJens Axboe 
1882a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
18832870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
188418aafc62SEric Dumazet 			     unsigned int *len,
1885d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
188635f3d14dSJens Axboe 			     struct sock *sk,
188735f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
18889c55e01cSJens Axboe {
18892870c43dSOctavian Purdila 	if (!*len)
1890a108d5f3SDavid S. Miller 		return true;
18919c55e01cSJens Axboe 
18922870c43dSOctavian Purdila 	/* skip this segment if already processed */
18932870c43dSOctavian Purdila 	if (*off >= plen) {
18942870c43dSOctavian Purdila 		*off -= plen;
1895a108d5f3SDavid S. Miller 		return false;
18962870c43dSOctavian Purdila 	}
18972870c43dSOctavian Purdila 
18982870c43dSOctavian Purdila 	/* ignore any bits we already processed */
18999ca1b22dSEric Dumazet 	poff += *off;
19009ca1b22dSEric Dumazet 	plen -= *off;
19012870c43dSOctavian Purdila 	*off = 0;
19022870c43dSOctavian Purdila 
190318aafc62SEric Dumazet 	do {
190418aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
19052870c43dSOctavian Purdila 
190618aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
190718aafc62SEric Dumazet 				  linear, sk))
1908a108d5f3SDavid S. Miller 			return true;
190918aafc62SEric Dumazet 		poff += flen;
191018aafc62SEric Dumazet 		plen -= flen;
19112870c43dSOctavian Purdila 		*len -= flen;
191218aafc62SEric Dumazet 	} while (*len && plen);
19132870c43dSOctavian Purdila 
1914a108d5f3SDavid S. Miller 	return false;
1915db43a282SOctavian Purdila }
19169c55e01cSJens Axboe 
19179c55e01cSJens Axboe /*
1918a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
19192870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
19209c55e01cSJens Axboe  */
1921a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
192235f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
192335f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
19242870c43dSOctavian Purdila {
19252870c43dSOctavian Purdila 	int seg;
1926fa9835e5STom Herbert 	struct sk_buff *iter;
19279c55e01cSJens Axboe 
19281d0c0b32SEric Dumazet 	/* map the linear part :
19292996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
19302996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
19312996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
19329c55e01cSJens Axboe 	 */
19332870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
19342870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
19352870c43dSOctavian Purdila 			     skb_headlen(skb),
193618aafc62SEric Dumazet 			     offset, len, spd,
19373a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
19381d0c0b32SEric Dumazet 			     sk, pipe))
1939a108d5f3SDavid S. Miller 		return true;
19409c55e01cSJens Axboe 
19419c55e01cSJens Axboe 	/*
19429c55e01cSJens Axboe 	 * then map the fragments
19439c55e01cSJens Axboe 	 */
19449c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
19459c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
19469c55e01cSJens Axboe 
1947ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
19489e903e08SEric Dumazet 				     f->page_offset, skb_frag_size(f),
194918aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
1950a108d5f3SDavid S. Miller 			return true;
19519c55e01cSJens Axboe 	}
19529c55e01cSJens Axboe 
1953fa9835e5STom Herbert 	skb_walk_frags(skb, iter) {
1954fa9835e5STom Herbert 		if (*offset >= iter->len) {
1955fa9835e5STom Herbert 			*offset -= iter->len;
1956fa9835e5STom Herbert 			continue;
1957fa9835e5STom Herbert 		}
1958fa9835e5STom Herbert 		/* __skb_splice_bits() only fails if the output has no room
1959fa9835e5STom Herbert 		 * left, so no point in going over the frag_list for the error
1960fa9835e5STom Herbert 		 * case.
1961fa9835e5STom Herbert 		 */
1962fa9835e5STom Herbert 		if (__skb_splice_bits(iter, pipe, offset, len, spd, sk))
1963fa9835e5STom Herbert 			return true;
1964fa9835e5STom Herbert 	}
1965fa9835e5STom Herbert 
1966a108d5f3SDavid S. Miller 	return false;
19679c55e01cSJens Axboe }
19689c55e01cSJens Axboe 
19699c55e01cSJens Axboe /*
19709c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
1971fa9835e5STom Herbert  * the fragments, and the frag list.
19729c55e01cSJens Axboe  */
1973a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
19749c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
197525869262SAl Viro 		    unsigned int flags)
19769c55e01cSJens Axboe {
197741c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
197841c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
19799c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
19809c55e01cSJens Axboe 		.pages = pages,
19819c55e01cSJens Axboe 		.partial = partial,
1982047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
19839c55e01cSJens Axboe 		.flags = flags,
198428a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
19859c55e01cSJens Axboe 		.spd_release = sock_spd_release,
19869c55e01cSJens Axboe 	};
198735f3d14dSJens Axboe 	int ret = 0;
198835f3d14dSJens Axboe 
1989fa9835e5STom Herbert 	__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk);
19909c55e01cSJens Axboe 
1991a60e3cc7SHannes Frederic Sowa 	if (spd.nr_pages)
199225869262SAl Viro 		ret = splice_to_pipe(pipe, &spd);
19939c55e01cSJens Axboe 
199435f3d14dSJens Axboe 	return ret;
19959c55e01cSJens Axboe }
19962b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits);
19979c55e01cSJens Axboe 
1998357b40a1SHerbert Xu /**
1999357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
2000357b40a1SHerbert Xu  *	@skb: destination buffer
2001357b40a1SHerbert Xu  *	@offset: offset in destination
2002357b40a1SHerbert Xu  *	@from: source buffer
2003357b40a1SHerbert Xu  *	@len: number of bytes to copy
2004357b40a1SHerbert Xu  *
2005357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
2006357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
2007357b40a1SHerbert Xu  *	traversing fragment lists and such.
2008357b40a1SHerbert Xu  */
2009357b40a1SHerbert Xu 
20100c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
2011357b40a1SHerbert Xu {
20121a028e50SDavid S. Miller 	int start = skb_headlen(skb);
2013fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2014fbb398a8SDavid S. Miller 	int i, copy;
2015357b40a1SHerbert Xu 
2016357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
2017357b40a1SHerbert Xu 		goto fault;
2018357b40a1SHerbert Xu 
20191a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
2020357b40a1SHerbert Xu 		if (copy > len)
2021357b40a1SHerbert Xu 			copy = len;
202227d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
2023357b40a1SHerbert Xu 		if ((len -= copy) == 0)
2024357b40a1SHerbert Xu 			return 0;
2025357b40a1SHerbert Xu 		offset += copy;
2026357b40a1SHerbert Xu 		from += copy;
2027357b40a1SHerbert Xu 	}
2028357b40a1SHerbert Xu 
2029357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2030357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
20311a028e50SDavid S. Miller 		int end;
2032357b40a1SHerbert Xu 
2033547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20341a028e50SDavid S. Miller 
20359e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
2036357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2037357b40a1SHerbert Xu 			u8 *vaddr;
2038357b40a1SHerbert Xu 
2039357b40a1SHerbert Xu 			if (copy > len)
2040357b40a1SHerbert Xu 				copy = len;
2041357b40a1SHerbert Xu 
204251c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
20431a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
20441a028e50SDavid S. Miller 			       from, copy);
204551c56b00SEric Dumazet 			kunmap_atomic(vaddr);
2046357b40a1SHerbert Xu 
2047357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2048357b40a1SHerbert Xu 				return 0;
2049357b40a1SHerbert Xu 			offset += copy;
2050357b40a1SHerbert Xu 			from += copy;
2051357b40a1SHerbert Xu 		}
20521a028e50SDavid S. Miller 		start = end;
2053357b40a1SHerbert Xu 	}
2054357b40a1SHerbert Xu 
2055fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
20561a028e50SDavid S. Miller 		int end;
2057357b40a1SHerbert Xu 
2058547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20591a028e50SDavid S. Miller 
2060fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
2061357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2062357b40a1SHerbert Xu 			if (copy > len)
2063357b40a1SHerbert Xu 				copy = len;
2064fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
20651a028e50SDavid S. Miller 					   from, copy))
2066357b40a1SHerbert Xu 				goto fault;
2067357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2068357b40a1SHerbert Xu 				return 0;
2069357b40a1SHerbert Xu 			offset += copy;
2070357b40a1SHerbert Xu 			from += copy;
2071357b40a1SHerbert Xu 		}
20721a028e50SDavid S. Miller 		start = end;
2073357b40a1SHerbert Xu 	}
2074357b40a1SHerbert Xu 	if (!len)
2075357b40a1SHerbert Xu 		return 0;
2076357b40a1SHerbert Xu 
2077357b40a1SHerbert Xu fault:
2078357b40a1SHerbert Xu 	return -EFAULT;
2079357b40a1SHerbert Xu }
2080357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
2081357b40a1SHerbert Xu 
20821da177e4SLinus Torvalds /* Checksum skb data. */
20832817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
20842817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
20851da177e4SLinus Torvalds {
20861a028e50SDavid S. Miller 	int start = skb_headlen(skb);
20871a028e50SDavid S. Miller 	int i, copy = start - offset;
2088fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
20891da177e4SLinus Torvalds 	int pos = 0;
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds 	/* Checksum header. */
20921da177e4SLinus Torvalds 	if (copy > 0) {
20931da177e4SLinus Torvalds 		if (copy > len)
20941da177e4SLinus Torvalds 			copy = len;
20952817a336SDaniel Borkmann 		csum = ops->update(skb->data + offset, copy, csum);
20961da177e4SLinus Torvalds 		if ((len -= copy) == 0)
20971da177e4SLinus Torvalds 			return csum;
20981da177e4SLinus Torvalds 		offset += copy;
20991da177e4SLinus Torvalds 		pos	= copy;
21001da177e4SLinus Torvalds 	}
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
21031a028e50SDavid S. Miller 		int end;
210451c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
21051da177e4SLinus Torvalds 
2106547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21071a028e50SDavid S. Miller 
210851c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
21091da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
211044bb9363SAl Viro 			__wsum csum2;
21111da177e4SLinus Torvalds 			u8 *vaddr;
21121da177e4SLinus Torvalds 
21131da177e4SLinus Torvalds 			if (copy > len)
21141da177e4SLinus Torvalds 				copy = len;
211551c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
21162817a336SDaniel Borkmann 			csum2 = ops->update(vaddr + frag->page_offset +
21171a028e50SDavid S. Miller 					    offset - start, copy, 0);
211851c56b00SEric Dumazet 			kunmap_atomic(vaddr);
21192817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
21201da177e4SLinus Torvalds 			if (!(len -= copy))
21211da177e4SLinus Torvalds 				return csum;
21221da177e4SLinus Torvalds 			offset += copy;
21231da177e4SLinus Torvalds 			pos    += copy;
21241da177e4SLinus Torvalds 		}
21251a028e50SDavid S. Miller 		start = end;
21261da177e4SLinus Torvalds 	}
21271da177e4SLinus Torvalds 
2128fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
21291a028e50SDavid S. Miller 		int end;
21301da177e4SLinus Torvalds 
2131547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21321a028e50SDavid S. Miller 
2133fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
21341da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21355f92a738SAl Viro 			__wsum csum2;
21361da177e4SLinus Torvalds 			if (copy > len)
21371da177e4SLinus Torvalds 				copy = len;
21382817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
21392817a336SDaniel Borkmann 					       copy, 0, ops);
21402817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
21411da177e4SLinus Torvalds 			if ((len -= copy) == 0)
21421da177e4SLinus Torvalds 				return csum;
21431da177e4SLinus Torvalds 			offset += copy;
21441da177e4SLinus Torvalds 			pos    += copy;
21451da177e4SLinus Torvalds 		}
21461a028e50SDavid S. Miller 		start = end;
21471da177e4SLinus Torvalds 	}
214809a62660SKris Katterjohn 	BUG_ON(len);
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	return csum;
21511da177e4SLinus Torvalds }
21522817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
21532817a336SDaniel Borkmann 
21542817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
21552817a336SDaniel Borkmann 		    int len, __wsum csum)
21562817a336SDaniel Borkmann {
21572817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2158cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
21592817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
21602817a336SDaniel Borkmann 	};
21612817a336SDaniel Borkmann 
21622817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
21632817a336SDaniel Borkmann }
2164b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds /* Both of above in one bottle. */
21671da177e4SLinus Torvalds 
216881d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
216981d77662SAl Viro 				    u8 *to, int len, __wsum csum)
21701da177e4SLinus Torvalds {
21711a028e50SDavid S. Miller 	int start = skb_headlen(skb);
21721a028e50SDavid S. Miller 	int i, copy = start - offset;
2173fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
21741da177e4SLinus Torvalds 	int pos = 0;
21751da177e4SLinus Torvalds 
21761da177e4SLinus Torvalds 	/* Copy header. */
21771da177e4SLinus Torvalds 	if (copy > 0) {
21781da177e4SLinus Torvalds 		if (copy > len)
21791da177e4SLinus Torvalds 			copy = len;
21801da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
21811da177e4SLinus Torvalds 						 copy, csum);
21821da177e4SLinus Torvalds 		if ((len -= copy) == 0)
21831da177e4SLinus Torvalds 			return csum;
21841da177e4SLinus Torvalds 		offset += copy;
21851da177e4SLinus Torvalds 		to     += copy;
21861da177e4SLinus Torvalds 		pos	= copy;
21871da177e4SLinus Torvalds 	}
21881da177e4SLinus Torvalds 
21891da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
21901a028e50SDavid S. Miller 		int end;
21911da177e4SLinus Torvalds 
2192547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21931a028e50SDavid S. Miller 
21949e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
21951da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21965084205fSAl Viro 			__wsum csum2;
21971da177e4SLinus Torvalds 			u8 *vaddr;
21981da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds 			if (copy > len)
22011da177e4SLinus Torvalds 				copy = len;
220251c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
22031da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
22041a028e50SDavid S. Miller 							  frag->page_offset +
22051a028e50SDavid S. Miller 							  offset - start, to,
22061a028e50SDavid S. Miller 							  copy, 0);
220751c56b00SEric Dumazet 			kunmap_atomic(vaddr);
22081da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
22091da177e4SLinus Torvalds 			if (!(len -= copy))
22101da177e4SLinus Torvalds 				return csum;
22111da177e4SLinus Torvalds 			offset += copy;
22121da177e4SLinus Torvalds 			to     += copy;
22131da177e4SLinus Torvalds 			pos    += copy;
22141da177e4SLinus Torvalds 		}
22151a028e50SDavid S. Miller 		start = end;
22161da177e4SLinus Torvalds 	}
22171da177e4SLinus Torvalds 
2218fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
221981d77662SAl Viro 		__wsum csum2;
22201a028e50SDavid S. Miller 		int end;
22211da177e4SLinus Torvalds 
2222547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
22231a028e50SDavid S. Miller 
2224fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
22251da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
22261da177e4SLinus Torvalds 			if (copy > len)
22271da177e4SLinus Torvalds 				copy = len;
2228fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
22291a028e50SDavid S. Miller 						       offset - start,
22301da177e4SLinus Torvalds 						       to, copy, 0);
22311da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
22321da177e4SLinus Torvalds 			if ((len -= copy) == 0)
22331da177e4SLinus Torvalds 				return csum;
22341da177e4SLinus Torvalds 			offset += copy;
22351da177e4SLinus Torvalds 			to     += copy;
22361da177e4SLinus Torvalds 			pos    += copy;
22371da177e4SLinus Torvalds 		}
22381a028e50SDavid S. Miller 		start = end;
22391da177e4SLinus Torvalds 	}
224009a62660SKris Katterjohn 	BUG_ON(len);
22411da177e4SLinus Torvalds 	return csum;
22421da177e4SLinus Torvalds }
2243b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
22441da177e4SLinus Torvalds 
2245af2806f8SThomas Graf  /**
2246af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2247af2806f8SThomas Graf  *	@from: source buffer
2248af2806f8SThomas Graf  *
2249af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2250af2806f8SThomas Graf  *	into skb_zerocopy().
2251af2806f8SThomas Graf  */
2252af2806f8SThomas Graf unsigned int
2253af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2254af2806f8SThomas Graf {
2255af2806f8SThomas Graf 	unsigned int hlen = 0;
2256af2806f8SThomas Graf 
2257af2806f8SThomas Graf 	if (!from->head_frag ||
2258af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2259af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2260af2806f8SThomas Graf 		hlen = skb_headlen(from);
2261af2806f8SThomas Graf 
2262af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2263af2806f8SThomas Graf 		hlen = from->len;
2264af2806f8SThomas Graf 
2265af2806f8SThomas Graf 	return hlen;
2266af2806f8SThomas Graf }
2267af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2268af2806f8SThomas Graf 
2269af2806f8SThomas Graf /**
2270af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2271af2806f8SThomas Graf  *	@to: destination buffer
22727fceb4deSMasanari Iida  *	@from: source buffer
2273af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2274af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2275af2806f8SThomas Graf  *
2276af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2277af2806f8SThomas Graf  *	to the frags in the source buffer.
2278af2806f8SThomas Graf  *
2279af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2280af2806f8SThomas Graf  *	headroom in the `to` buffer.
228136d5fe6aSZoltan Kiss  *
228236d5fe6aSZoltan Kiss  *	Return value:
228336d5fe6aSZoltan Kiss  *	0: everything is OK
228436d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
228536d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2286af2806f8SThomas Graf  */
228736d5fe6aSZoltan Kiss int
228836d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2289af2806f8SThomas Graf {
2290af2806f8SThomas Graf 	int i, j = 0;
2291af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
229236d5fe6aSZoltan Kiss 	int ret;
2293af2806f8SThomas Graf 	struct page *page;
2294af2806f8SThomas Graf 	unsigned int offset;
2295af2806f8SThomas Graf 
2296af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2297af2806f8SThomas Graf 
2298af2806f8SThomas Graf 	/* dont bother with small payloads */
229936d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
230036d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2301af2806f8SThomas Graf 
2302af2806f8SThomas Graf 	if (hlen) {
230336d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
230436d5fe6aSZoltan Kiss 		if (unlikely(ret))
230536d5fe6aSZoltan Kiss 			return ret;
2306af2806f8SThomas Graf 		len -= hlen;
2307af2806f8SThomas Graf 	} else {
2308af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2309af2806f8SThomas Graf 		if (plen) {
2310af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2311af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2312af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2313af2806f8SThomas Graf 			get_page(page);
2314af2806f8SThomas Graf 			j = 1;
2315af2806f8SThomas Graf 			len -= plen;
2316af2806f8SThomas Graf 		}
2317af2806f8SThomas Graf 	}
2318af2806f8SThomas Graf 
2319af2806f8SThomas Graf 	to->truesize += len + plen;
2320af2806f8SThomas Graf 	to->len += len + plen;
2321af2806f8SThomas Graf 	to->data_len += len + plen;
2322af2806f8SThomas Graf 
232336d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
232436d5fe6aSZoltan Kiss 		skb_tx_error(from);
232536d5fe6aSZoltan Kiss 		return -ENOMEM;
232636d5fe6aSZoltan Kiss 	}
232736d5fe6aSZoltan Kiss 
2328af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2329af2806f8SThomas Graf 		if (!len)
2330af2806f8SThomas Graf 			break;
2331af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2332af2806f8SThomas Graf 		skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2333af2806f8SThomas Graf 		len -= skb_shinfo(to)->frags[j].size;
2334af2806f8SThomas Graf 		skb_frag_ref(to, j);
2335af2806f8SThomas Graf 		j++;
2336af2806f8SThomas Graf 	}
2337af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
233836d5fe6aSZoltan Kiss 
233936d5fe6aSZoltan Kiss 	return 0;
2340af2806f8SThomas Graf }
2341af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
2342af2806f8SThomas Graf 
23431da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
23441da177e4SLinus Torvalds {
2345d3bc23e7SAl Viro 	__wsum csum;
23461da177e4SLinus Torvalds 	long csstart;
23471da177e4SLinus Torvalds 
234884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
234955508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
23501da177e4SLinus Torvalds 	else
23511da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
23521da177e4SLinus Torvalds 
235309a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
23541da177e4SLinus Torvalds 
2355d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds 	csum = 0;
23581da177e4SLinus Torvalds 	if (csstart != skb->len)
23591da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
23601da177e4SLinus Torvalds 					      skb->len - csstart, 0);
23611da177e4SLinus Torvalds 
236284fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2363ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
23641da177e4SLinus Torvalds 
2365d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
23661da177e4SLinus Torvalds 	}
23671da177e4SLinus Torvalds }
2368b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds /**
23711da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
23721da177e4SLinus Torvalds  *	@list: list to dequeue from
23731da177e4SLinus Torvalds  *
23741da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
23751da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
23761da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
23771da177e4SLinus Torvalds  */
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
23801da177e4SLinus Torvalds {
23811da177e4SLinus Torvalds 	unsigned long flags;
23821da177e4SLinus Torvalds 	struct sk_buff *result;
23831da177e4SLinus Torvalds 
23841da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23851da177e4SLinus Torvalds 	result = __skb_dequeue(list);
23861da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23871da177e4SLinus Torvalds 	return result;
23881da177e4SLinus Torvalds }
2389b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds /**
23921da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
23931da177e4SLinus Torvalds  *	@list: list to dequeue from
23941da177e4SLinus Torvalds  *
23951da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
23961da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
23971da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
23981da177e4SLinus Torvalds  */
23991da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
24001da177e4SLinus Torvalds {
24011da177e4SLinus Torvalds 	unsigned long flags;
24021da177e4SLinus Torvalds 	struct sk_buff *result;
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
24051da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
24061da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
24071da177e4SLinus Torvalds 	return result;
24081da177e4SLinus Torvalds }
2409b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds /**
24121da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
24131da177e4SLinus Torvalds  *	@list: list to empty
24141da177e4SLinus Torvalds  *
24151da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
24161da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
24171da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
24181da177e4SLinus Torvalds  */
24191da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
24201da177e4SLinus Torvalds {
24211da177e4SLinus Torvalds 	struct sk_buff *skb;
24221da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
24231da177e4SLinus Torvalds 		kfree_skb(skb);
24241da177e4SLinus Torvalds }
2425b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds /**
24289f5afeaeSYaogong Wang  *	skb_rbtree_purge - empty a skb rbtree
24299f5afeaeSYaogong Wang  *	@root: root of the rbtree to empty
24309f5afeaeSYaogong Wang  *
24319f5afeaeSYaogong Wang  *	Delete all buffers on an &sk_buff rbtree. Each buffer is removed from
24329f5afeaeSYaogong Wang  *	the list and one reference dropped. This function does not take
24339f5afeaeSYaogong Wang  *	any lock. Synchronization should be handled by the caller (e.g., TCP
24349f5afeaeSYaogong Wang  *	out-of-order queue is protected by the socket lock).
24359f5afeaeSYaogong Wang  */
24369f5afeaeSYaogong Wang void skb_rbtree_purge(struct rb_root *root)
24379f5afeaeSYaogong Wang {
24389f5afeaeSYaogong Wang 	struct sk_buff *skb, *next;
24399f5afeaeSYaogong Wang 
24409f5afeaeSYaogong Wang 	rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode)
24419f5afeaeSYaogong Wang 		kfree_skb(skb);
24429f5afeaeSYaogong Wang 
24439f5afeaeSYaogong Wang 	*root = RB_ROOT;
24449f5afeaeSYaogong Wang }
24459f5afeaeSYaogong Wang 
24469f5afeaeSYaogong Wang /**
24471da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
24481da177e4SLinus Torvalds  *	@list: list to use
24491da177e4SLinus Torvalds  *	@newsk: buffer to queue
24501da177e4SLinus Torvalds  *
24511da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
24521da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
24531da177e4SLinus Torvalds  *	safely.
24541da177e4SLinus Torvalds  *
24551da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24561da177e4SLinus Torvalds  */
24571da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
24581da177e4SLinus Torvalds {
24591da177e4SLinus Torvalds 	unsigned long flags;
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
24621da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
24631da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
24641da177e4SLinus Torvalds }
2465b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds /**
24681da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
24691da177e4SLinus Torvalds  *	@list: list to use
24701da177e4SLinus Torvalds  *	@newsk: buffer to queue
24711da177e4SLinus Torvalds  *
24721da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
24731da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
24741da177e4SLinus Torvalds  *	safely.
24751da177e4SLinus Torvalds  *
24761da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24771da177e4SLinus Torvalds  */
24781da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
24791da177e4SLinus Torvalds {
24801da177e4SLinus Torvalds 	unsigned long flags;
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
24831da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
24841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
24851da177e4SLinus Torvalds }
2486b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
24878728b834SDavid S. Miller 
24881da177e4SLinus Torvalds /**
24891da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
24901da177e4SLinus Torvalds  *	@skb: buffer to remove
24918728b834SDavid S. Miller  *	@list: list to use
24921da177e4SLinus Torvalds  *
24938728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
24948728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
24951da177e4SLinus Torvalds  *
24968728b834SDavid S. Miller  *	You must know what list the SKB is on.
24971da177e4SLinus Torvalds  */
24988728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
24991da177e4SLinus Torvalds {
25001da177e4SLinus Torvalds 	unsigned long flags;
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
25038728b834SDavid S. Miller 	__skb_unlink(skb, list);
25041da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
25051da177e4SLinus Torvalds }
2506b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds /**
25091da177e4SLinus Torvalds  *	skb_append	-	append a buffer
25101da177e4SLinus Torvalds  *	@old: buffer to insert after
25111da177e4SLinus Torvalds  *	@newsk: buffer to insert
25128728b834SDavid S. Miller  *	@list: list to use
25131da177e4SLinus Torvalds  *
25141da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
25151da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
25161da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
25171da177e4SLinus Torvalds  */
25188728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
25191da177e4SLinus Torvalds {
25201da177e4SLinus Torvalds 	unsigned long flags;
25211da177e4SLinus Torvalds 
25228728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
25237de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
25248728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
25251da177e4SLinus Torvalds }
2526b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds /**
25291da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
25301da177e4SLinus Torvalds  *	@old: buffer to insert before
25311da177e4SLinus Torvalds  *	@newsk: buffer to insert
25328728b834SDavid S. Miller  *	@list: list to use
25331da177e4SLinus Torvalds  *
25348728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
25358728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
25368728b834SDavid S. Miller  *	calls.
25378728b834SDavid S. Miller  *
25381da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
25391da177e4SLinus Torvalds  */
25408728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
25411da177e4SLinus Torvalds {
25421da177e4SLinus Torvalds 	unsigned long flags;
25431da177e4SLinus Torvalds 
25448728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
25458728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
25468728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
25471da177e4SLinus Torvalds }
2548b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
25511da177e4SLinus Torvalds 					   struct sk_buff* skb1,
25521da177e4SLinus Torvalds 					   const u32 len, const int pos)
25531da177e4SLinus Torvalds {
25541da177e4SLinus Torvalds 	int i;
25551da177e4SLinus Torvalds 
2556d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2557d626f62bSArnaldo Carvalho de Melo 					 pos - len);
25581da177e4SLinus Torvalds 	/* And move data appendix as is. */
25591da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
25601da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
25631da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
25641da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
25651da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
25661da177e4SLinus Torvalds 	skb->data_len		   = 0;
25671da177e4SLinus Torvalds 	skb->len		   = len;
256827a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
25691da177e4SLinus Torvalds }
25701da177e4SLinus Torvalds 
25711da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
25721da177e4SLinus Torvalds 				       struct sk_buff* skb1,
25731da177e4SLinus Torvalds 				       const u32 len, int pos)
25741da177e4SLinus Torvalds {
25751da177e4SLinus Torvalds 	int i, k = 0;
25761da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
25771da177e4SLinus Torvalds 
25781da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
25791da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
25801da177e4SLinus Torvalds 	skb->len		  = len;
25811da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
25849e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds 		if (pos + size > len) {
25871da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
25881da177e4SLinus Torvalds 
25891da177e4SLinus Torvalds 			if (pos < len) {
25901da177e4SLinus Torvalds 				/* Split frag.
25911da177e4SLinus Torvalds 				 * We have two variants in this case:
25921da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
25931da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
25941da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
25951da177e4SLinus Torvalds 				 *    where splitting is expensive.
25961da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
25971da177e4SLinus Torvalds 				 */
2598ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
25991da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
26009e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
26019e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
26021da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
26031da177e4SLinus Torvalds 			}
26041da177e4SLinus Torvalds 			k++;
26051da177e4SLinus Torvalds 		} else
26061da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
26071da177e4SLinus Torvalds 		pos += size;
26081da177e4SLinus Torvalds 	}
26091da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
26101da177e4SLinus Torvalds }
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds /**
26131da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
26141da177e4SLinus Torvalds  * @skb: the buffer to split
26151da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
26161da177e4SLinus Torvalds  * @len: new length for skb
26171da177e4SLinus Torvalds  */
26181da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
26191da177e4SLinus Torvalds {
26201da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
26211da177e4SLinus Torvalds 
262268534c68SAmerigo Wang 	skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
26231da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
26241da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
26251da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
26261da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
26271da177e4SLinus Torvalds }
2628b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
26291da177e4SLinus Torvalds 
26309f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
26319f782db3SIlpo Järvinen  *
26329f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
26339f782db3SIlpo Järvinen  */
2634832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2635832d11c5SIlpo Järvinen {
26360ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2637832d11c5SIlpo Järvinen }
2638832d11c5SIlpo Järvinen 
2639832d11c5SIlpo Järvinen /**
2640832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2641832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2642832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2643832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2644832d11c5SIlpo Järvinen  *
2645832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
264620e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2647832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2648832d11c5SIlpo Järvinen  *
2649832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2650832d11c5SIlpo Järvinen  *
2651832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2652832d11c5SIlpo Järvinen  * to have non-paged data as well.
2653832d11c5SIlpo Järvinen  *
2654832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2655832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2656832d11c5SIlpo Järvinen  */
2657832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2658832d11c5SIlpo Järvinen {
2659832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2660832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2661832d11c5SIlpo Järvinen 
2662832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2663f8071cdeSEric Dumazet 
2664f8071cdeSEric Dumazet 	if (skb_headlen(skb))
2665f8071cdeSEric Dumazet 		return 0;
2666832d11c5SIlpo Järvinen 
2667832d11c5SIlpo Järvinen 	todo = shiftlen;
2668832d11c5SIlpo Järvinen 	from = 0;
2669832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2670832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2671832d11c5SIlpo Järvinen 
2672832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2673832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2674832d11c5SIlpo Järvinen 	 */
2675832d11c5SIlpo Järvinen 	if (!to ||
2676ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2677ea2ab693SIan Campbell 			      fragfrom->page_offset)) {
2678832d11c5SIlpo Järvinen 		merge = -1;
2679832d11c5SIlpo Järvinen 	} else {
2680832d11c5SIlpo Järvinen 		merge = to - 1;
2681832d11c5SIlpo Järvinen 
26829e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
2683832d11c5SIlpo Järvinen 		if (todo < 0) {
2684832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2685832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2686832d11c5SIlpo Järvinen 				return 0;
2687832d11c5SIlpo Järvinen 
26889f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
26899f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2690832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2691832d11c5SIlpo Järvinen 
26929e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
26939e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
2694832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2695832d11c5SIlpo Järvinen 
2696832d11c5SIlpo Järvinen 			goto onlymerged;
2697832d11c5SIlpo Järvinen 		}
2698832d11c5SIlpo Järvinen 
2699832d11c5SIlpo Järvinen 		from++;
2700832d11c5SIlpo Järvinen 	}
2701832d11c5SIlpo Järvinen 
2702832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2703832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2704832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2705832d11c5SIlpo Järvinen 		return 0;
2706832d11c5SIlpo Järvinen 
2707832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2708832d11c5SIlpo Järvinen 		return 0;
2709832d11c5SIlpo Järvinen 
2710832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2711832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2712832d11c5SIlpo Järvinen 			return 0;
2713832d11c5SIlpo Järvinen 
2714832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2715832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2716832d11c5SIlpo Järvinen 
27179e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
2718832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
27199e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
2720832d11c5SIlpo Järvinen 			from++;
2721832d11c5SIlpo Järvinen 			to++;
2722832d11c5SIlpo Järvinen 
2723832d11c5SIlpo Järvinen 		} else {
2724ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
2725832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2726832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
27279e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
2728832d11c5SIlpo Järvinen 
2729832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
27309e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
2731832d11c5SIlpo Järvinen 			todo = 0;
2732832d11c5SIlpo Järvinen 
2733832d11c5SIlpo Järvinen 			to++;
2734832d11c5SIlpo Järvinen 			break;
2735832d11c5SIlpo Järvinen 		}
2736832d11c5SIlpo Järvinen 	}
2737832d11c5SIlpo Järvinen 
2738832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2739832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2740832d11c5SIlpo Järvinen 
2741832d11c5SIlpo Järvinen 	if (merge >= 0) {
2742832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2743832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2744832d11c5SIlpo Järvinen 
27459e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2746ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
2747832d11c5SIlpo Järvinen 	}
2748832d11c5SIlpo Järvinen 
2749832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2750832d11c5SIlpo Järvinen 	to = 0;
2751832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2752832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2753832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2754832d11c5SIlpo Järvinen 
2755832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2756832d11c5SIlpo Järvinen 
2757832d11c5SIlpo Järvinen onlymerged:
2758832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2759832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2760832d11c5SIlpo Järvinen 	 */
2761832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2762832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2763832d11c5SIlpo Järvinen 
2764832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2765832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2766832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2767832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2768832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2769832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2770832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2771832d11c5SIlpo Järvinen 
2772832d11c5SIlpo Järvinen 	return shiftlen;
2773832d11c5SIlpo Järvinen }
2774832d11c5SIlpo Järvinen 
2775677e90edSThomas Graf /**
2776677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2777677e90edSThomas Graf  * @skb: the buffer to read
2778677e90edSThomas Graf  * @from: lower offset of data to be read
2779677e90edSThomas Graf  * @to: upper offset of data to be read
2780677e90edSThomas Graf  * @st: state variable
2781677e90edSThomas Graf  *
2782677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2783677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2784677e90edSThomas Graf  */
2785677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2786677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2787677e90edSThomas Graf {
2788677e90edSThomas Graf 	st->lower_offset = from;
2789677e90edSThomas Graf 	st->upper_offset = to;
2790677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2791677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2792677e90edSThomas Graf 	st->frag_data = NULL;
2793677e90edSThomas Graf }
2794b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2795677e90edSThomas Graf 
2796677e90edSThomas Graf /**
2797677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2798677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2799677e90edSThomas Graf  * @data: destination pointer for data to be returned
2800677e90edSThomas Graf  * @st: state variable
2801677e90edSThomas Graf  *
2802bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
2803677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2804bc32383cSMathias Krause  * the head of the data block to @data and returns the length
2805677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2806677e90edSThomas Graf  * offset has been reached.
2807677e90edSThomas Graf  *
2808677e90edSThomas Graf  * The caller is not required to consume all of the data
2809bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
2810677e90edSThomas Graf  * of bytes already consumed and the next call to
2811677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2812677e90edSThomas Graf  *
281325985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
2814e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
2815677e90edSThomas Graf  *       reads of potentially non linear data.
2816677e90edSThomas Graf  *
2817bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2818677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2819677e90edSThomas Graf  *       a stack for this purpose.
2820677e90edSThomas Graf  */
2821677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2822677e90edSThomas Graf 			  struct skb_seq_state *st)
2823677e90edSThomas Graf {
2824677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2825677e90edSThomas Graf 	skb_frag_t *frag;
2826677e90edSThomas Graf 
2827aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
2828aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
2829aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
2830aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
2831aeb193eaSWedson Almeida Filho 		}
2832677e90edSThomas Graf 		return 0;
2833aeb193eaSWedson Almeida Filho 	}
2834677e90edSThomas Graf 
2835677e90edSThomas Graf next_skb:
283695e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2837677e90edSThomas Graf 
2838995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
283995e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2840677e90edSThomas Graf 		return block_limit - abs_offset;
2841677e90edSThomas Graf 	}
2842677e90edSThomas Graf 
2843677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2844677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2845677e90edSThomas Graf 
2846677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2847677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
28489e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
2849677e90edSThomas Graf 
2850677e90edSThomas Graf 		if (abs_offset < block_limit) {
2851677e90edSThomas Graf 			if (!st->frag_data)
285251c56b00SEric Dumazet 				st->frag_data = kmap_atomic(skb_frag_page(frag));
2853677e90edSThomas Graf 
2854677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2855677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2856677e90edSThomas Graf 
2857677e90edSThomas Graf 			return block_limit - abs_offset;
2858677e90edSThomas Graf 		}
2859677e90edSThomas Graf 
2860677e90edSThomas Graf 		if (st->frag_data) {
286151c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
2862677e90edSThomas Graf 			st->frag_data = NULL;
2863677e90edSThomas Graf 		}
2864677e90edSThomas Graf 
2865677e90edSThomas Graf 		st->frag_idx++;
28669e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
2867677e90edSThomas Graf 	}
2868677e90edSThomas Graf 
28695b5a60daSOlaf Kirch 	if (st->frag_data) {
287051c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
28715b5a60daSOlaf Kirch 		st->frag_data = NULL;
28725b5a60daSOlaf Kirch 	}
28735b5a60daSOlaf Kirch 
287421dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2875677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
287695e3b24cSHerbert Xu 		st->frag_idx = 0;
2877677e90edSThomas Graf 		goto next_skb;
287871b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
287971b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
288071b3346dSShyam Iyer 		st->frag_idx = 0;
2881677e90edSThomas Graf 		goto next_skb;
2882677e90edSThomas Graf 	}
2883677e90edSThomas Graf 
2884677e90edSThomas Graf 	return 0;
2885677e90edSThomas Graf }
2886b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2887677e90edSThomas Graf 
2888677e90edSThomas Graf /**
2889677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2890677e90edSThomas Graf  * @st: state variable
2891677e90edSThomas Graf  *
2892677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2893677e90edSThomas Graf  * returned 0.
2894677e90edSThomas Graf  */
2895677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2896677e90edSThomas Graf {
2897677e90edSThomas Graf 	if (st->frag_data)
289851c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
2899677e90edSThomas Graf }
2900b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2901677e90edSThomas Graf 
29023fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
29033fc7e8a6SThomas Graf 
29043fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
29053fc7e8a6SThomas Graf 					  struct ts_config *conf,
29063fc7e8a6SThomas Graf 					  struct ts_state *state)
29073fc7e8a6SThomas Graf {
29083fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
29093fc7e8a6SThomas Graf }
29103fc7e8a6SThomas Graf 
29113fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
29123fc7e8a6SThomas Graf {
29133fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
29143fc7e8a6SThomas Graf }
29153fc7e8a6SThomas Graf 
29163fc7e8a6SThomas Graf /**
29173fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
29183fc7e8a6SThomas Graf  * @skb: the buffer to look in
29193fc7e8a6SThomas Graf  * @from: search offset
29203fc7e8a6SThomas Graf  * @to: search limit
29213fc7e8a6SThomas Graf  * @config: textsearch configuration
29223fc7e8a6SThomas Graf  *
29233fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
29243fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
29253fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
29263fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
29273fc7e8a6SThomas Graf  */
29283fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2929059a2440SBojan Prtvar 			   unsigned int to, struct ts_config *config)
29303fc7e8a6SThomas Graf {
2931059a2440SBojan Prtvar 	struct ts_state state;
2932f72b948dSPhil Oester 	unsigned int ret;
2933f72b948dSPhil Oester 
29343fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
29353fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
29363fc7e8a6SThomas Graf 
2937059a2440SBojan Prtvar 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
29383fc7e8a6SThomas Graf 
2939059a2440SBojan Prtvar 	ret = textsearch_find(config, &state);
2940f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
29413fc7e8a6SThomas Graf }
2942b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
29433fc7e8a6SThomas Graf 
2944e89e9cf5SAnanda Raju /**
29452c53040fSBen Hutchings  * skb_append_datato_frags - append the user data to a skb
2946e89e9cf5SAnanda Raju  * @sk: sock  structure
2947e793c0f7SMasanari Iida  * @skb: skb structure to be appended with user data.
2948e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2949e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2950e89e9cf5SAnanda Raju  * @length: length of the iov message
2951e89e9cf5SAnanda Raju  *
2952e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2953e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2954e89e9cf5SAnanda Raju  */
2955e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2956dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2957e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2958e89e9cf5SAnanda Raju 			void *from, int length)
2959e89e9cf5SAnanda Raju {
2960b2111724SEric Dumazet 	int frg_cnt = skb_shinfo(skb)->nr_frags;
2961b2111724SEric Dumazet 	int copy;
2962e89e9cf5SAnanda Raju 	int offset = 0;
2963e89e9cf5SAnanda Raju 	int ret;
2964b2111724SEric Dumazet 	struct page_frag *pfrag = &current->task_frag;
2965e89e9cf5SAnanda Raju 
2966e89e9cf5SAnanda Raju 	do {
2967e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2968e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2969b2111724SEric Dumazet 			return -EMSGSIZE;
2970e89e9cf5SAnanda Raju 
2971b2111724SEric Dumazet 		if (!sk_page_frag_refill(sk, pfrag))
2972e89e9cf5SAnanda Raju 			return -ENOMEM;
2973e89e9cf5SAnanda Raju 
2974e89e9cf5SAnanda Raju 		/* copy the user data to page */
2975b2111724SEric Dumazet 		copy = min_t(int, length, pfrag->size - pfrag->offset);
2976e89e9cf5SAnanda Raju 
2977b2111724SEric Dumazet 		ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2978e89e9cf5SAnanda Raju 			      offset, copy, 0, skb);
2979e89e9cf5SAnanda Raju 		if (ret < 0)
2980e89e9cf5SAnanda Raju 			return -EFAULT;
2981e89e9cf5SAnanda Raju 
2982e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2983b2111724SEric Dumazet 		skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2984b2111724SEric Dumazet 				   copy);
2985b2111724SEric Dumazet 		frg_cnt++;
2986b2111724SEric Dumazet 		pfrag->offset += copy;
2987b2111724SEric Dumazet 		get_page(pfrag->page);
2988b2111724SEric Dumazet 
2989b2111724SEric Dumazet 		skb->truesize += copy;
2990b2111724SEric Dumazet 		atomic_add(copy, &sk->sk_wmem_alloc);
2991e89e9cf5SAnanda Raju 		skb->len += copy;
2992e89e9cf5SAnanda Raju 		skb->data_len += copy;
2993e89e9cf5SAnanda Raju 		offset += copy;
2994e89e9cf5SAnanda Raju 		length -= copy;
2995e89e9cf5SAnanda Raju 
2996e89e9cf5SAnanda Raju 	} while (length > 0);
2997e89e9cf5SAnanda Raju 
2998e89e9cf5SAnanda Raju 	return 0;
2999e89e9cf5SAnanda Raju }
3000b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
3001e89e9cf5SAnanda Raju 
3002be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
3003be12a1feSHannes Frederic Sowa 			 int offset, size_t size)
3004be12a1feSHannes Frederic Sowa {
3005be12a1feSHannes Frederic Sowa 	int i = skb_shinfo(skb)->nr_frags;
3006be12a1feSHannes Frederic Sowa 
3007be12a1feSHannes Frederic Sowa 	if (skb_can_coalesce(skb, i, page, offset)) {
3008be12a1feSHannes Frederic Sowa 		skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size);
3009be12a1feSHannes Frederic Sowa 	} else if (i < MAX_SKB_FRAGS) {
3010be12a1feSHannes Frederic Sowa 		get_page(page);
3011be12a1feSHannes Frederic Sowa 		skb_fill_page_desc(skb, i, page, offset, size);
3012be12a1feSHannes Frederic Sowa 	} else {
3013be12a1feSHannes Frederic Sowa 		return -EMSGSIZE;
3014be12a1feSHannes Frederic Sowa 	}
3015be12a1feSHannes Frederic Sowa 
3016be12a1feSHannes Frederic Sowa 	return 0;
3017be12a1feSHannes Frederic Sowa }
3018be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags);
3019be12a1feSHannes Frederic Sowa 
3020cbb042f9SHerbert Xu /**
3021cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
3022cbb042f9SHerbert Xu  *	@skb: buffer to update
3023cbb042f9SHerbert Xu  *	@len: length of data pulled
3024cbb042f9SHerbert Xu  *
3025cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
3026fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
302784fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
302884fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
302984fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
3030cbb042f9SHerbert Xu  */
3031cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
3032cbb042f9SHerbert Xu {
303331b33dfbSPravin B Shelar 	unsigned char *data = skb->data;
303431b33dfbSPravin B Shelar 
3035cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
303631b33dfbSPravin B Shelar 	__skb_pull(skb, len);
303731b33dfbSPravin B Shelar 	skb_postpull_rcsum(skb, data, len);
303831b33dfbSPravin B Shelar 	return skb->data;
3039cbb042f9SHerbert Xu }
3040f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
3041f94691acSArnaldo Carvalho de Melo 
3042f4c50d99SHerbert Xu /**
3043f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
3044df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
3045576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
3046f4c50d99SHerbert Xu  *
3047f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
30484c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
30494c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
3050f4c50d99SHerbert Xu  */
3051df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
3052df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
3053f4c50d99SHerbert Xu {
3054f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
3055f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
30561a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
3057df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
3058df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
3059df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
30601fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
3061f4c50d99SHerbert Xu 	unsigned int offset = doffset;
3062df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
3063802ab55aSAlexander Duyck 	unsigned int partial_segs = 0;
3064f4c50d99SHerbert Xu 	unsigned int headroom;
3065802ab55aSAlexander Duyck 	unsigned int len = head_skb->len;
3066ec5f0615SPravin B Shelar 	__be16 proto;
306736c98382SAlexander Duyck 	bool csum, sg;
3068df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
3069f4c50d99SHerbert Xu 	int err = -ENOMEM;
3070f4c50d99SHerbert Xu 	int i = 0;
3071f4c50d99SHerbert Xu 	int pos;
307253d6471cSVlad Yasevich 	int dummy;
3073f4c50d99SHerbert Xu 
30745882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
307553d6471cSVlad Yasevich 	proto = skb_network_protocol(head_skb, &dummy);
3076ec5f0615SPravin B Shelar 	if (unlikely(!proto))
3077ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
3078ec5f0615SPravin B Shelar 
307936c98382SAlexander Duyck 	sg = !!(features & NETIF_F_SG);
3080f245d079SAlexander Duyck 	csum = !!can_checksum_protocol(features, proto);
30817e2b10c1STom Herbert 
308207b26c94SSteffen Klassert 	if (sg && csum && (mss != GSO_BY_FRAGS))  {
308307b26c94SSteffen Klassert 		if (!(features & NETIF_F_GSO_PARTIAL)) {
308407b26c94SSteffen Klassert 			struct sk_buff *iter;
308507b26c94SSteffen Klassert 
308607b26c94SSteffen Klassert 			if (!list_skb ||
308707b26c94SSteffen Klassert 			    !net_gso_ok(features, skb_shinfo(head_skb)->gso_type))
308807b26c94SSteffen Klassert 				goto normal;
308907b26c94SSteffen Klassert 
309007b26c94SSteffen Klassert 			/* Split the buffer at the frag_list pointer.
309107b26c94SSteffen Klassert 			 * This is based on the assumption that all
309207b26c94SSteffen Klassert 			 * buffers in the chain excluding the last
309307b26c94SSteffen Klassert 			 * containing the same amount of data.
309407b26c94SSteffen Klassert 			 */
309507b26c94SSteffen Klassert 			skb_walk_frags(head_skb, iter) {
309607b26c94SSteffen Klassert 				if (skb_headlen(iter))
309707b26c94SSteffen Klassert 					goto normal;
309807b26c94SSteffen Klassert 
309907b26c94SSteffen Klassert 				len -= iter->len;
310007b26c94SSteffen Klassert 			}
310107b26c94SSteffen Klassert 		}
310207b26c94SSteffen Klassert 
3103802ab55aSAlexander Duyck 		/* GSO partial only requires that we trim off any excess that
3104802ab55aSAlexander Duyck 		 * doesn't fit into an MSS sized block, so take care of that
3105802ab55aSAlexander Duyck 		 * now.
3106802ab55aSAlexander Duyck 		 */
3107802ab55aSAlexander Duyck 		partial_segs = len / mss;
3108d7fb5a80SAlexander Duyck 		if (partial_segs > 1)
3109802ab55aSAlexander Duyck 			mss *= partial_segs;
3110d7fb5a80SAlexander Duyck 		else
3111d7fb5a80SAlexander Duyck 			partial_segs = 0;
3112802ab55aSAlexander Duyck 	}
3113802ab55aSAlexander Duyck 
311407b26c94SSteffen Klassert normal:
3115df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
3116df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
3117f4c50d99SHerbert Xu 
3118f4c50d99SHerbert Xu 	do {
3119f4c50d99SHerbert Xu 		struct sk_buff *nskb;
31208cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
3121c8884eddSHerbert Xu 		int hsize;
3122f4c50d99SHerbert Xu 		int size;
3123f4c50d99SHerbert Xu 
31243953c46cSMarcelo Ricardo Leitner 		if (unlikely(mss == GSO_BY_FRAGS)) {
31253953c46cSMarcelo Ricardo Leitner 			len = list_skb->len;
31263953c46cSMarcelo Ricardo Leitner 		} else {
3127df5771ffSMichael S. Tsirkin 			len = head_skb->len - offset;
3128f4c50d99SHerbert Xu 			if (len > mss)
3129f4c50d99SHerbert Xu 				len = mss;
31303953c46cSMarcelo Ricardo Leitner 		}
3131f4c50d99SHerbert Xu 
3132df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
3133f4c50d99SHerbert Xu 		if (hsize < 0)
3134f4c50d99SHerbert Xu 			hsize = 0;
3135c8884eddSHerbert Xu 		if (hsize > len || !sg)
3136c8884eddSHerbert Xu 			hsize = len;
3137f4c50d99SHerbert Xu 
31381a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
31391a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
31401a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
314189319d38SHerbert Xu 
31429d8506ccSHerbert Xu 			i = 0;
31431a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
31441a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
31451fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
31461a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
31479d8506ccSHerbert Xu 
31489d8506ccSHerbert Xu 			while (pos < offset + len) {
31499d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
31509d8506ccSHerbert Xu 
31514e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
31529d8506ccSHerbert Xu 				if (pos + size > offset + len)
31539d8506ccSHerbert Xu 					break;
31549d8506ccSHerbert Xu 
31559d8506ccSHerbert Xu 				i++;
31569d8506ccSHerbert Xu 				pos += size;
31574e1beba1SMichael S. Tsirkin 				frag++;
31589d8506ccSHerbert Xu 			}
31599d8506ccSHerbert Xu 
31601a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
31611a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
316289319d38SHerbert Xu 
3163f4c50d99SHerbert Xu 			if (unlikely(!nskb))
3164f4c50d99SHerbert Xu 				goto err;
3165f4c50d99SHerbert Xu 
31669d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
31679d8506ccSHerbert Xu 				kfree_skb(nskb);
31689d8506ccSHerbert Xu 				goto err;
31699d8506ccSHerbert Xu 			}
31709d8506ccSHerbert Xu 
3171ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
317289319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
317389319d38SHerbert Xu 				kfree_skb(nskb);
317489319d38SHerbert Xu 				goto err;
317589319d38SHerbert Xu 			}
317689319d38SHerbert Xu 
3177ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
317889319d38SHerbert Xu 			skb_release_head_state(nskb);
317989319d38SHerbert Xu 			__skb_push(nskb, doffset);
318089319d38SHerbert Xu 		} else {
3181c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
3182df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3183c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
318489319d38SHerbert Xu 
318589319d38SHerbert Xu 			if (unlikely(!nskb))
318689319d38SHerbert Xu 				goto err;
318789319d38SHerbert Xu 
318889319d38SHerbert Xu 			skb_reserve(nskb, headroom);
318989319d38SHerbert Xu 			__skb_put(nskb, doffset);
319089319d38SHerbert Xu 		}
319189319d38SHerbert Xu 
3192f4c50d99SHerbert Xu 		if (segs)
3193f4c50d99SHerbert Xu 			tail->next = nskb;
3194f4c50d99SHerbert Xu 		else
3195f4c50d99SHerbert Xu 			segs = nskb;
3196f4c50d99SHerbert Xu 		tail = nskb;
3197f4c50d99SHerbert Xu 
3198df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
3199f4c50d99SHerbert Xu 
3200030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3201fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
320268c33163SPravin B Shelar 
3203df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
320468c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
320568c33163SPravin B Shelar 						 doffset + tnl_hlen);
320689319d38SHerbert Xu 
32079d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
32081cdbcb79SSimon Horman 			goto perform_csum_check;
320989319d38SHerbert Xu 
32107fbeffedSAlexander Duyck 		if (!sg) {
32117fbeffedSAlexander Duyck 			if (!nskb->remcsum_offload)
32126f85a124SHerbert Xu 				nskb->ip_summed = CHECKSUM_NONE;
321376443456SAlexander Duyck 			SKB_GSO_CB(nskb)->csum =
321476443456SAlexander Duyck 				skb_copy_and_csum_bits(head_skb, offset,
3215f4c50d99SHerbert Xu 						       skb_put(nskb, len),
3216f4c50d99SHerbert Xu 						       len, 0);
32177e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
3218de843723STom Herbert 				skb_headroom(nskb) + doffset;
3219f4c50d99SHerbert Xu 			continue;
3220f4c50d99SHerbert Xu 		}
3221f4c50d99SHerbert Xu 
32228cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3223f4c50d99SHerbert Xu 
3224df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3225d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
3226f4c50d99SHerbert Xu 
3227df5771ffSMichael S. Tsirkin 		skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3228df5771ffSMichael S. Tsirkin 			SKBTX_SHARED_FRAG;
3229cef401deSEric Dumazet 
32309d8506ccSHerbert Xu 		while (pos < offset + len) {
32319d8506ccSHerbert Xu 			if (i >= nfrags) {
32321a4cedafSMichael S. Tsirkin 				BUG_ON(skb_headlen(list_skb));
32339d8506ccSHerbert Xu 
32349d8506ccSHerbert Xu 				i = 0;
32351a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
32361a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
32371fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
32389d8506ccSHerbert Xu 
32399d8506ccSHerbert Xu 				BUG_ON(!nfrags);
32409d8506ccSHerbert Xu 
32411a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
32429d8506ccSHerbert Xu 			}
32439d8506ccSHerbert Xu 
32449d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
32459d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
32469d8506ccSHerbert Xu 				net_warn_ratelimited(
32479d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
32489d8506ccSHerbert Xu 					pos, mss);
32499d8506ccSHerbert Xu 				goto err;
32509d8506ccSHerbert Xu 			}
32519d8506ccSHerbert Xu 
32521fd819ecSMichael S. Tsirkin 			if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
32531fd819ecSMichael S. Tsirkin 				goto err;
32541fd819ecSMichael S. Tsirkin 
32554e1beba1SMichael S. Tsirkin 			*nskb_frag = *frag;
32568cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
32578cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
3258f4c50d99SHerbert Xu 
3259f4c50d99SHerbert Xu 			if (pos < offset) {
32608cb19905SMichael S. Tsirkin 				nskb_frag->page_offset += offset - pos;
32618cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
3262f4c50d99SHerbert Xu 			}
3263f4c50d99SHerbert Xu 
326489319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
3265f4c50d99SHerbert Xu 
3266f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
3267f4c50d99SHerbert Xu 				i++;
32684e1beba1SMichael S. Tsirkin 				frag++;
3269f4c50d99SHerbert Xu 				pos += size;
3270f4c50d99SHerbert Xu 			} else {
32718cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
327289319d38SHerbert Xu 				goto skip_fraglist;
3273f4c50d99SHerbert Xu 			}
3274f4c50d99SHerbert Xu 
32758cb19905SMichael S. Tsirkin 			nskb_frag++;
3276f4c50d99SHerbert Xu 		}
3277f4c50d99SHerbert Xu 
327889319d38SHerbert Xu skip_fraglist:
3279f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
3280f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
3281f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
3282ec5f0615SPravin B Shelar 
32831cdbcb79SSimon Horman perform_csum_check:
32847fbeffedSAlexander Duyck 		if (!csum) {
3285ddff00d4SAlexander Duyck 			if (skb_has_shared_frag(nskb)) {
3286ddff00d4SAlexander Duyck 				err = __skb_linearize(nskb);
3287ddff00d4SAlexander Duyck 				if (err)
3288ddff00d4SAlexander Duyck 					goto err;
3289ddff00d4SAlexander Duyck 			}
32907fbeffedSAlexander Duyck 			if (!nskb->remcsum_offload)
3291ec5f0615SPravin B Shelar 				nskb->ip_summed = CHECKSUM_NONE;
329276443456SAlexander Duyck 			SKB_GSO_CB(nskb)->csum =
329376443456SAlexander Duyck 				skb_checksum(nskb, doffset,
329476443456SAlexander Duyck 					     nskb->len - doffset, 0);
32957e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
32967e2b10c1STom Herbert 				skb_headroom(nskb) + doffset;
3297ec5f0615SPravin B Shelar 		}
3298df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
3299f4c50d99SHerbert Xu 
3300bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
3301bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
3302bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
3303bec3cfdcSEric Dumazet 	 */
3304bec3cfdcSEric Dumazet 	segs->prev = tail;
3305432c856fSToshiaki Makita 
3306802ab55aSAlexander Duyck 	if (partial_segs) {
330707b26c94SSteffen Klassert 		struct sk_buff *iter;
3308802ab55aSAlexander Duyck 		int type = skb_shinfo(head_skb)->gso_type;
330907b26c94SSteffen Klassert 		unsigned short gso_size = skb_shinfo(head_skb)->gso_size;
3310802ab55aSAlexander Duyck 
3311802ab55aSAlexander Duyck 		/* Update type to add partial and then remove dodgy if set */
331207b26c94SSteffen Klassert 		type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL;
3313802ab55aSAlexander Duyck 		type &= ~SKB_GSO_DODGY;
3314802ab55aSAlexander Duyck 
3315802ab55aSAlexander Duyck 		/* Update GSO info and prepare to start updating headers on
3316802ab55aSAlexander Duyck 		 * our way back down the stack of protocols.
3317802ab55aSAlexander Duyck 		 */
331807b26c94SSteffen Klassert 		for (iter = segs; iter; iter = iter->next) {
331907b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_size = gso_size;
332007b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_segs = partial_segs;
332107b26c94SSteffen Klassert 			skb_shinfo(iter)->gso_type = type;
332207b26c94SSteffen Klassert 			SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset;
332307b26c94SSteffen Klassert 		}
332407b26c94SSteffen Klassert 
332507b26c94SSteffen Klassert 		if (tail->len - doffset <= gso_size)
332607b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_size = 0;
332707b26c94SSteffen Klassert 		else if (tail != segs)
332807b26c94SSteffen Klassert 			skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size);
3329802ab55aSAlexander Duyck 	}
3330802ab55aSAlexander Duyck 
3331432c856fSToshiaki Makita 	/* Following permits correct backpressure, for protocols
3332432c856fSToshiaki Makita 	 * using skb_set_owner_w().
3333432c856fSToshiaki Makita 	 * Idea is to tranfert ownership from head_skb to last segment.
3334432c856fSToshiaki Makita 	 */
3335432c856fSToshiaki Makita 	if (head_skb->destructor == sock_wfree) {
3336432c856fSToshiaki Makita 		swap(tail->truesize, head_skb->truesize);
3337432c856fSToshiaki Makita 		swap(tail->destructor, head_skb->destructor);
3338432c856fSToshiaki Makita 		swap(tail->sk, head_skb->sk);
3339432c856fSToshiaki Makita 	}
3340f4c50d99SHerbert Xu 	return segs;
3341f4c50d99SHerbert Xu 
3342f4c50d99SHerbert Xu err:
3343289dccbeSEric Dumazet 	kfree_skb_list(segs);
3344f4c50d99SHerbert Xu 	return ERR_PTR(err);
3345f4c50d99SHerbert Xu }
3346f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
3347f4c50d99SHerbert Xu 
334871d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
334971d93b39SHerbert Xu {
33508a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
335167147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
335267147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
33538a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
335458025e46SEric Dumazet 	struct sk_buff *lp, *p = *head;
3355715dc1f3SEric Dumazet 	unsigned int delta_truesize;
335671d93b39SHerbert Xu 
33578a29111cSEric Dumazet 	if (unlikely(p->len + len >= 65536))
335871d93b39SHerbert Xu 		return -E2BIG;
335971d93b39SHerbert Xu 
336029e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
33618a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
33628a29111cSEric Dumazet 
33638a29111cSEric Dumazet 	if (headlen <= offset) {
336442da6994SHerbert Xu 		skb_frag_t *frag;
336566e92fcfSHerbert Xu 		skb_frag_t *frag2;
33669aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
33679aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
336842da6994SHerbert Xu 
336966e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
33708a29111cSEric Dumazet 			goto merge;
337181705ad1SHerbert Xu 
33728a29111cSEric Dumazet 		offset -= headlen;
33739aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
33749aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
3375f5572068SHerbert Xu 
33769aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
33779aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
337866e92fcfSHerbert Xu 		do {
337966e92fcfSHerbert Xu 			*--frag = *--frag2;
338066e92fcfSHerbert Xu 		} while (--i);
338166e92fcfSHerbert Xu 
338266e92fcfSHerbert Xu 		frag->page_offset += offset;
33839e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
338466e92fcfSHerbert Xu 
3385715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
3386ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
3387ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
3388715dc1f3SEric Dumazet 
3389f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
3390f5572068SHerbert Xu 		skb->len -= skb->data_len;
3391f5572068SHerbert Xu 		skb->data_len = 0;
3392f5572068SHerbert Xu 
3393715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
33945d38a079SHerbert Xu 		goto done;
3395d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
3396d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
3397d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
3398d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
3399d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
3400d7e8883cSEric Dumazet 		unsigned int first_offset;
3401d7e8883cSEric Dumazet 
3402d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
34038a29111cSEric Dumazet 			goto merge;
3404d7e8883cSEric Dumazet 
3405d7e8883cSEric Dumazet 		first_offset = skb->data -
3406d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
3407d7e8883cSEric Dumazet 			       offset;
3408d7e8883cSEric Dumazet 
3409d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3410d7e8883cSEric Dumazet 
3411d7e8883cSEric Dumazet 		frag->page.p	  = page;
3412d7e8883cSEric Dumazet 		frag->page_offset = first_offset;
3413d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
3414d7e8883cSEric Dumazet 
3415d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3416d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
3417d7e8883cSEric Dumazet 
3418715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3419d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3420d7e8883cSEric Dumazet 		goto done;
34218a29111cSEric Dumazet 	}
342271d93b39SHerbert Xu 
342371d93b39SHerbert Xu merge:
3424715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
342567147ba9SHerbert Xu 	if (offset > headlen) {
3426d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
3427d1dc7abfSMichal Schmidt 
3428d1dc7abfSMichal Schmidt 		skbinfo->frags[0].page_offset += eat;
34299e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
3430d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
3431d1dc7abfSMichal Schmidt 		skb->len -= eat;
343267147ba9SHerbert Xu 		offset = headlen;
343356035022SHerbert Xu 	}
343456035022SHerbert Xu 
343567147ba9SHerbert Xu 	__skb_pull(skb, offset);
343656035022SHerbert Xu 
343729e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
34388a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
34398a29111cSEric Dumazet 	else
3440c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
3441c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
3442f4a775d1SEric Dumazet 	__skb_header_release(skb);
34438a29111cSEric Dumazet 	lp = p;
344471d93b39SHerbert Xu 
34455d38a079SHerbert Xu done:
34465d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
344737fe4732SHerbert Xu 	p->data_len += len;
3448715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
344937fe4732SHerbert Xu 	p->len += len;
34508a29111cSEric Dumazet 	if (lp != p) {
34518a29111cSEric Dumazet 		lp->data_len += len;
34528a29111cSEric Dumazet 		lp->truesize += delta_truesize;
34538a29111cSEric Dumazet 		lp->len += len;
34548a29111cSEric Dumazet 	}
345571d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
345671d93b39SHerbert Xu 	return 0;
345771d93b39SHerbert Xu }
345857c05650SMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gro_receive);
345971d93b39SHerbert Xu 
34601da177e4SLinus Torvalds void __init skb_init(void)
34611da177e4SLinus Torvalds {
34621da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
34631da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
34641da177e4SLinus Torvalds 					      0,
3465e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
346620c2df83SPaul Mundt 					      NULL);
3467d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3468d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
3469d179cd12SDavid S. Miller 						0,
3470e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
347120c2df83SPaul Mundt 						NULL);
34721da177e4SLinus Torvalds }
34731da177e4SLinus Torvalds 
3474716ea3a7SDavid Howells /**
3475716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3476716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
3477716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
3478716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
3479716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
3480716ea3a7SDavid Howells  *
3481716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
3482716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
3483716ea3a7SDavid Howells  */
348451c739d1SDavid S. Miller static int
348551c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3486716ea3a7SDavid Howells {
34871a028e50SDavid S. Miller 	int start = skb_headlen(skb);
34881a028e50SDavid S. Miller 	int i, copy = start - offset;
3489fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
3490716ea3a7SDavid Howells 	int elt = 0;
3491716ea3a7SDavid Howells 
3492716ea3a7SDavid Howells 	if (copy > 0) {
3493716ea3a7SDavid Howells 		if (copy > len)
3494716ea3a7SDavid Howells 			copy = len;
3495642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
3496716ea3a7SDavid Howells 		elt++;
3497716ea3a7SDavid Howells 		if ((len -= copy) == 0)
3498716ea3a7SDavid Howells 			return elt;
3499716ea3a7SDavid Howells 		offset += copy;
3500716ea3a7SDavid Howells 	}
3501716ea3a7SDavid Howells 
3502716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
35031a028e50SDavid S. Miller 		int end;
3504716ea3a7SDavid Howells 
3505547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
35061a028e50SDavid S. Miller 
35079e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3508716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3509716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3510716ea3a7SDavid Howells 
3511716ea3a7SDavid Howells 			if (copy > len)
3512716ea3a7SDavid Howells 				copy = len;
3513ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3514642f1490SJens Axboe 					frag->page_offset+offset-start);
3515716ea3a7SDavid Howells 			elt++;
3516716ea3a7SDavid Howells 			if (!(len -= copy))
3517716ea3a7SDavid Howells 				return elt;
3518716ea3a7SDavid Howells 			offset += copy;
3519716ea3a7SDavid Howells 		}
35201a028e50SDavid S. Miller 		start = end;
3521716ea3a7SDavid Howells 	}
3522716ea3a7SDavid Howells 
3523fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
35241a028e50SDavid S. Miller 		int end;
3525716ea3a7SDavid Howells 
3526547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
35271a028e50SDavid S. Miller 
3528fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
3529716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3530716ea3a7SDavid Howells 			if (copy > len)
3531716ea3a7SDavid Howells 				copy = len;
3532fbb398a8SDavid S. Miller 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
353351c739d1SDavid S. Miller 					      copy);
3534716ea3a7SDavid Howells 			if ((len -= copy) == 0)
3535716ea3a7SDavid Howells 				return elt;
3536716ea3a7SDavid Howells 			offset += copy;
3537716ea3a7SDavid Howells 		}
35381a028e50SDavid S. Miller 		start = end;
3539716ea3a7SDavid Howells 	}
3540716ea3a7SDavid Howells 	BUG_ON(len);
3541716ea3a7SDavid Howells 	return elt;
3542716ea3a7SDavid Howells }
3543716ea3a7SDavid Howells 
354425a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
354525a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
354625a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
354725a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
354825a91d8dSFan Du  *
354925a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
355025a91d8dSFan Du  * 1. sg_init_table
355125a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
355225a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
355325a91d8dSFan Du  *
355425a91d8dSFan Du  * This is equivalent to:
355525a91d8dSFan Du  * 1. sg_init_table
355625a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
355725a91d8dSFan Du  * 3. sg_unmark_end
355825a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
355925a91d8dSFan Du  *
356025a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
356125a91d8dSFan Du  * is more preferable.
356225a91d8dSFan Du  */
356325a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
356425a91d8dSFan Du 			int offset, int len)
356525a91d8dSFan Du {
356625a91d8dSFan Du 	return __skb_to_sgvec(skb, sg, offset, len);
356725a91d8dSFan Du }
356825a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
356925a91d8dSFan Du 
357051c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
357151c739d1SDavid S. Miller {
357251c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
357351c739d1SDavid S. Miller 
3574c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
357551c739d1SDavid S. Miller 
357651c739d1SDavid S. Miller 	return nsg;
357751c739d1SDavid S. Miller }
3578b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
357951c739d1SDavid S. Miller 
3580716ea3a7SDavid Howells /**
3581716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
3582716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
3583716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
3584716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
3585716ea3a7SDavid Howells  *
3586716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
3587716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
3588716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
3589716ea3a7SDavid Howells  *
3590716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
3591716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
3592716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
3593716ea3a7SDavid Howells  *
3594716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
3595716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
3596716ea3a7SDavid Howells  */
3597716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3598716ea3a7SDavid Howells {
3599716ea3a7SDavid Howells 	int copyflag;
3600716ea3a7SDavid Howells 	int elt;
3601716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
3602716ea3a7SDavid Howells 
3603716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
3604716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
3605716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
3606716ea3a7SDavid Howells 	 */
3607716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3608716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3609716ea3a7SDavid Howells 		return -ENOMEM;
3610716ea3a7SDavid Howells 
3611716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
361221dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
3613716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
3614716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
3615716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
3616716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
3617716ea3a7SDavid Howells 
3618716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
3619716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3620716ea3a7SDavid Howells 			return -ENOMEM;
3621716ea3a7SDavid Howells 
3622716ea3a7SDavid Howells 		/* Voila! */
3623716ea3a7SDavid Howells 		*trailer = skb;
3624716ea3a7SDavid Howells 		return 1;
3625716ea3a7SDavid Howells 	}
3626716ea3a7SDavid Howells 
3627716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
3628716ea3a7SDavid Howells 
3629716ea3a7SDavid Howells 	elt = 1;
3630716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
3631716ea3a7SDavid Howells 	copyflag = 0;
3632716ea3a7SDavid Howells 
3633716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
3634716ea3a7SDavid Howells 		int ntail = 0;
3635716ea3a7SDavid Howells 
3636716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
3637716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
3638716ea3a7SDavid Howells 		 * after it. */
3639716ea3a7SDavid Howells 
3640716ea3a7SDavid Howells 		if (skb_shared(skb1))
3641716ea3a7SDavid Howells 			copyflag = 1;
3642716ea3a7SDavid Howells 
3643716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
3644716ea3a7SDavid Howells 
3645716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
3646716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
364721dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
3648716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
3649716ea3a7SDavid Howells 				ntail = tailbits + 128;
3650716ea3a7SDavid Howells 		}
3651716ea3a7SDavid Howells 
3652716ea3a7SDavid Howells 		if (copyflag ||
3653716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
3654716ea3a7SDavid Howells 		    ntail ||
3655716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
365621dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
3657716ea3a7SDavid Howells 			struct sk_buff *skb2;
3658716ea3a7SDavid Howells 
3659716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
3660716ea3a7SDavid Howells 			if (ntail == 0)
3661716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
3662716ea3a7SDavid Howells 			else
3663716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
3664716ea3a7SDavid Howells 						       skb_headroom(skb1),
3665716ea3a7SDavid Howells 						       ntail,
3666716ea3a7SDavid Howells 						       GFP_ATOMIC);
3667716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
3668716ea3a7SDavid Howells 				return -ENOMEM;
3669716ea3a7SDavid Howells 
3670716ea3a7SDavid Howells 			if (skb1->sk)
3671716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
3672716ea3a7SDavid Howells 
3673716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
3674716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
3675716ea3a7SDavid Howells 
3676716ea3a7SDavid Howells 			skb2->next = skb1->next;
3677716ea3a7SDavid Howells 			*skb_p = skb2;
3678716ea3a7SDavid Howells 			kfree_skb(skb1);
3679716ea3a7SDavid Howells 			skb1 = skb2;
3680716ea3a7SDavid Howells 		}
3681716ea3a7SDavid Howells 		elt++;
3682716ea3a7SDavid Howells 		*trailer = skb1;
3683716ea3a7SDavid Howells 		skb_p = &skb1->next;
3684716ea3a7SDavid Howells 	}
3685716ea3a7SDavid Howells 
3686716ea3a7SDavid Howells 	return elt;
3687716ea3a7SDavid Howells }
3688b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
3689716ea3a7SDavid Howells 
3690b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
3691b1faf566SEric Dumazet {
3692b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
3693b1faf566SEric Dumazet 
3694b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3695b1faf566SEric Dumazet }
3696b1faf566SEric Dumazet 
36978605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb)
36988605330aSSoheil Hassas Yeganeh {
36998605330aSSoheil Hassas Yeganeh 	/* pkt_type of skbs received on local sockets is never PACKET_OUTGOING.
37008605330aSSoheil Hassas Yeganeh 	 * So, it is safe to (mis)use it to mark skbs on the error queue.
37018605330aSSoheil Hassas Yeganeh 	 */
37028605330aSSoheil Hassas Yeganeh 	skb->pkt_type = PACKET_OUTGOING;
37038605330aSSoheil Hassas Yeganeh 	BUILD_BUG_ON(PACKET_OUTGOING == 0);
37048605330aSSoheil Hassas Yeganeh }
37058605330aSSoheil Hassas Yeganeh 
3706b1faf566SEric Dumazet /*
3707b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3708b1faf566SEric Dumazet  */
3709b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3710b1faf566SEric Dumazet {
3711b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
371295c96174SEric Dumazet 	    (unsigned int)sk->sk_rcvbuf)
3713b1faf566SEric Dumazet 		return -ENOMEM;
3714b1faf566SEric Dumazet 
3715b1faf566SEric Dumazet 	skb_orphan(skb);
3716b1faf566SEric Dumazet 	skb->sk = sk;
3717b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
3718b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
37198605330aSSoheil Hassas Yeganeh 	skb_set_err_queue(skb);
3720b1faf566SEric Dumazet 
3721abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
3722abb57ea4SEric Dumazet 	skb_dst_force(skb);
3723abb57ea4SEric Dumazet 
3724b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
3725b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
3726676d2369SDavid S. Miller 		sk->sk_data_ready(sk);
3727b1faf566SEric Dumazet 	return 0;
3728b1faf566SEric Dumazet }
3729b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
3730b1faf566SEric Dumazet 
373183a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb)
373283a1a1a7SSoheil Hassas Yeganeh {
373383a1a1a7SSoheil Hassas Yeganeh 	return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
373483a1a1a7SSoheil Hassas Yeganeh 		       SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6);
373583a1a1a7SSoheil Hassas Yeganeh }
373683a1a1a7SSoheil Hassas Yeganeh 
3737364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3738364a9e93SWillem de Bruijn {
3739364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
374083a1a1a7SSoheil Hassas Yeganeh 	struct sk_buff *skb, *skb_next = NULL;
374183a1a1a7SSoheil Hassas Yeganeh 	bool icmp_next = false;
3742997d5c3fSEric Dumazet 	unsigned long flags;
3743364a9e93SWillem de Bruijn 
3744997d5c3fSEric Dumazet 	spin_lock_irqsave(&q->lock, flags);
3745364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
3746364a9e93SWillem de Bruijn 	if (skb && (skb_next = skb_peek(q)))
374783a1a1a7SSoheil Hassas Yeganeh 		icmp_next = is_icmp_err_skb(skb_next);
3748997d5c3fSEric Dumazet 	spin_unlock_irqrestore(&q->lock, flags);
3749364a9e93SWillem de Bruijn 
375083a1a1a7SSoheil Hassas Yeganeh 	if (is_icmp_err_skb(skb) && !icmp_next)
375183a1a1a7SSoheil Hassas Yeganeh 		sk->sk_err = 0;
375283a1a1a7SSoheil Hassas Yeganeh 
375383a1a1a7SSoheil Hassas Yeganeh 	if (skb_next)
3754364a9e93SWillem de Bruijn 		sk->sk_error_report(sk);
3755364a9e93SWillem de Bruijn 
3756364a9e93SWillem de Bruijn 	return skb;
3757364a9e93SWillem de Bruijn }
3758364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
3759364a9e93SWillem de Bruijn 
3760cab41c47SAlexander Duyck /**
3761cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
3762cab41c47SAlexander Duyck  * @skb: the skb to clone
3763cab41c47SAlexander Duyck  *
3764cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
3765cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
3766cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
3767cab41c47SAlexander Duyck  *
3768cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
3769cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
3770cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
3771cab41c47SAlexander Duyck  * the sk_error_queue.
3772cab41c47SAlexander Duyck  */
377362bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
377462bccb8cSAlexander Duyck {
377562bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
377662bccb8cSAlexander Duyck 	struct sk_buff *clone;
377762bccb8cSAlexander Duyck 
377862bccb8cSAlexander Duyck 	if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
377962bccb8cSAlexander Duyck 		return NULL;
378062bccb8cSAlexander Duyck 
378162bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
378262bccb8cSAlexander Duyck 	if (!clone) {
378362bccb8cSAlexander Duyck 		sock_put(sk);
378462bccb8cSAlexander Duyck 		return NULL;
378562bccb8cSAlexander Duyck 	}
378662bccb8cSAlexander Duyck 
378762bccb8cSAlexander Duyck 	clone->sk = sk;
378862bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
378962bccb8cSAlexander Duyck 
379062bccb8cSAlexander Duyck 	return clone;
379162bccb8cSAlexander Duyck }
379262bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
379362bccb8cSAlexander Duyck 
379437846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
379537846ef0SAlexander Duyck 					struct sock *sk,
37964ef1b286SSoheil Hassas Yeganeh 					int tstype,
37974ef1b286SSoheil Hassas Yeganeh 					bool opt_stats)
3798ac45f602SPatrick Ohly {
3799ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
3800ac45f602SPatrick Ohly 	int err;
3801ac45f602SPatrick Ohly 
38024ef1b286SSoheil Hassas Yeganeh 	BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb));
38034ef1b286SSoheil Hassas Yeganeh 
3804ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
3805ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
3806ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
3807ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3808e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
38094ef1b286SSoheil Hassas Yeganeh 	serr->opt_stats = opt_stats;
38101862d620SWillem de Bruijn 	serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0;
38114ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
381209c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
3813ac5cc977SWANG Cong 		if (sk->sk_protocol == IPPROTO_TCP &&
3814ac5cc977SWANG Cong 		    sk->sk_type == SOCK_STREAM)
38154ed2d765SWillem de Bruijn 			serr->ee.ee_data -= sk->sk_tskey;
38164ed2d765SWillem de Bruijn 	}
381729030374SEric Dumazet 
3818ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
381929030374SEric Dumazet 
3820ac45f602SPatrick Ohly 	if (err)
3821ac45f602SPatrick Ohly 		kfree_skb(skb);
3822ac45f602SPatrick Ohly }
382337846ef0SAlexander Duyck 
3824b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3825b245be1fSWillem de Bruijn {
3826b245be1fSWillem de Bruijn 	bool ret;
3827b245be1fSWillem de Bruijn 
3828b245be1fSWillem de Bruijn 	if (likely(sysctl_tstamp_allow_data || tsonly))
3829b245be1fSWillem de Bruijn 		return true;
3830b245be1fSWillem de Bruijn 
3831b245be1fSWillem de Bruijn 	read_lock_bh(&sk->sk_callback_lock);
3832b245be1fSWillem de Bruijn 	ret = sk->sk_socket && sk->sk_socket->file &&
3833b245be1fSWillem de Bruijn 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3834b245be1fSWillem de Bruijn 	read_unlock_bh(&sk->sk_callback_lock);
3835b245be1fSWillem de Bruijn 	return ret;
3836b245be1fSWillem de Bruijn }
3837b245be1fSWillem de Bruijn 
383837846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
383937846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
384037846ef0SAlexander Duyck {
384137846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
384237846ef0SAlexander Duyck 
3843b245be1fSWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, false))
3844b245be1fSWillem de Bruijn 		return;
3845b245be1fSWillem de Bruijn 
38469ac25fc0SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
38479ac25fc0SEric Dumazet 	 * but only if the socket refcount is not zero.
38489ac25fc0SEric Dumazet 	 */
38499ac25fc0SEric Dumazet 	if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
385037846ef0SAlexander Duyck 		*skb_hwtstamps(skb) = *hwtstamps;
38514ef1b286SSoheil Hassas Yeganeh 		__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false);
385237846ef0SAlexander Duyck 		sock_put(sk);
385337846ef0SAlexander Duyck 	}
38549ac25fc0SEric Dumazet }
385537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
385637846ef0SAlexander Duyck 
385737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
385837846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
385937846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
386037846ef0SAlexander Duyck {
386137846ef0SAlexander Duyck 	struct sk_buff *skb;
38624ef1b286SSoheil Hassas Yeganeh 	bool tsonly, opt_stats = false;
386337846ef0SAlexander Duyck 
38643a8dd971SWillem de Bruijn 	if (!sk)
38653a8dd971SWillem de Bruijn 		return;
38663a8dd971SWillem de Bruijn 
38673a8dd971SWillem de Bruijn 	tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
38683a8dd971SWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, tsonly))
386937846ef0SAlexander Duyck 		return;
387037846ef0SAlexander Duyck 
38711c885808SFrancis Yan 	if (tsonly) {
38721c885808SFrancis Yan #ifdef CONFIG_INET
38731c885808SFrancis Yan 		if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) &&
38741c885808SFrancis Yan 		    sk->sk_protocol == IPPROTO_TCP &&
38754ef1b286SSoheil Hassas Yeganeh 		    sk->sk_type == SOCK_STREAM) {
38761c885808SFrancis Yan 			skb = tcp_get_timestamping_opt_stats(sk);
38774ef1b286SSoheil Hassas Yeganeh 			opt_stats = true;
38784ef1b286SSoheil Hassas Yeganeh 		} else
38791c885808SFrancis Yan #endif
38801c885808SFrancis Yan 			skb = alloc_skb(0, GFP_ATOMIC);
38811c885808SFrancis Yan 	} else {
388237846ef0SAlexander Duyck 		skb = skb_clone(orig_skb, GFP_ATOMIC);
38831c885808SFrancis Yan 	}
388437846ef0SAlexander Duyck 	if (!skb)
388537846ef0SAlexander Duyck 		return;
388637846ef0SAlexander Duyck 
388749ca0d8bSWillem de Bruijn 	if (tsonly) {
388849ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
388949ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
389049ca0d8bSWillem de Bruijn 	}
389149ca0d8bSWillem de Bruijn 
389249ca0d8bSWillem de Bruijn 	if (hwtstamps)
389349ca0d8bSWillem de Bruijn 		*skb_hwtstamps(skb) = *hwtstamps;
389449ca0d8bSWillem de Bruijn 	else
389549ca0d8bSWillem de Bruijn 		skb->tstamp = ktime_get_real();
389649ca0d8bSWillem de Bruijn 
38974ef1b286SSoheil Hassas Yeganeh 	__skb_complete_tx_timestamp(skb, sk, tstype, opt_stats);
389837846ef0SAlexander Duyck }
3899e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3900e7fd2885SWillem de Bruijn 
3901e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
3902e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
3903e7fd2885SWillem de Bruijn {
3904e7fd2885SWillem de Bruijn 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3905e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
3906e7fd2885SWillem de Bruijn }
3907ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3908ac45f602SPatrick Ohly 
39096e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
39106e3e939fSJohannes Berg {
39116e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
39126e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
3913dd4f1072SEric Dumazet 	int err = 1;
39146e3e939fSJohannes Berg 
39156e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
39166e3e939fSJohannes Berg 	skb->wifi_acked = acked;
39176e3e939fSJohannes Berg 
39186e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
39196e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
39206e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
39216e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
39226e3e939fSJohannes Berg 
3923dd4f1072SEric Dumazet 	/* Take a reference to prevent skb_orphan() from freeing the socket,
3924dd4f1072SEric Dumazet 	 * but only if the socket refcount is not zero.
3925dd4f1072SEric Dumazet 	 */
3926dd4f1072SEric Dumazet 	if (likely(atomic_inc_not_zero(&sk->sk_refcnt))) {
39276e3e939fSJohannes Berg 		err = sock_queue_err_skb(sk, skb);
3928dd4f1072SEric Dumazet 		sock_put(sk);
3929dd4f1072SEric Dumazet 	}
39306e3e939fSJohannes Berg 	if (err)
39316e3e939fSJohannes Berg 		kfree_skb(skb);
39326e3e939fSJohannes Berg }
39336e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
39346e3e939fSJohannes Berg 
3935f35d9d8aSRusty Russell /**
3936f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3937f35d9d8aSRusty Russell  * @skb: the skb to set
3938f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3939f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3940f35d9d8aSRusty Russell  *
3941f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3942f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3943f35d9d8aSRusty Russell  *
3944f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3945f35d9d8aSRusty Russell  * returns false you should drop the packet.
3946f35d9d8aSRusty Russell  */
3947f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3948f35d9d8aSRusty Russell {
39495ff8dda3SHerbert Xu 	if (unlikely(start > skb_headlen(skb)) ||
39505ff8dda3SHerbert Xu 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3951e87cc472SJoe Perches 		net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
39525ff8dda3SHerbert Xu 				     start, off, skb_headlen(skb));
3953f35d9d8aSRusty Russell 		return false;
3954f35d9d8aSRusty Russell 	}
3955f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3956f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3957f35d9d8aSRusty Russell 	skb->csum_offset = off;
3958e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
3959f35d9d8aSRusty Russell 	return true;
3960f35d9d8aSRusty Russell }
3961b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3962f35d9d8aSRusty Russell 
3963ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3964ed1f50c3SPaul Durrant 			       unsigned int max)
3965ed1f50c3SPaul Durrant {
3966ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
3967ed1f50c3SPaul Durrant 		return 0;
3968ed1f50c3SPaul Durrant 
3969ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
3970ed1f50c3SPaul Durrant 	 * won't need to do it again.
3971ed1f50c3SPaul Durrant 	 */
3972ed1f50c3SPaul Durrant 	if (max > skb->len)
3973ed1f50c3SPaul Durrant 		max = skb->len;
3974ed1f50c3SPaul Durrant 
3975ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3976ed1f50c3SPaul Durrant 		return -ENOMEM;
3977ed1f50c3SPaul Durrant 
3978ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
3979ed1f50c3SPaul Durrant 		return -EPROTO;
3980ed1f50c3SPaul Durrant 
3981ed1f50c3SPaul Durrant 	return 0;
3982ed1f50c3SPaul Durrant }
3983ed1f50c3SPaul Durrant 
3984f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
3985f9708b43SJan Beulich 
3986f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3987f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
3988f9708b43SJan Beulich 				      unsigned int off)
3989f9708b43SJan Beulich {
3990f9708b43SJan Beulich 	switch (proto) {
3991f9708b43SJan Beulich 		int err;
3992f9708b43SJan Beulich 
3993f9708b43SJan Beulich 	case IPPROTO_TCP:
3994f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3995f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
3996f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3997f9708b43SJan Beulich 						  offsetof(struct tcphdr,
3998f9708b43SJan Beulich 							   check)))
3999f9708b43SJan Beulich 			err = -EPROTO;
4000f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
4001f9708b43SJan Beulich 
4002f9708b43SJan Beulich 	case IPPROTO_UDP:
4003f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
4004f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
4005f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
4006f9708b43SJan Beulich 						  offsetof(struct udphdr,
4007f9708b43SJan Beulich 							   check)))
4008f9708b43SJan Beulich 			err = -EPROTO;
4009f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
4010f9708b43SJan Beulich 	}
4011f9708b43SJan Beulich 
4012f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
4013f9708b43SJan Beulich }
4014f9708b43SJan Beulich 
4015ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4016ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
4017ed1f50c3SPaul Durrant  */
4018ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
4019ed1f50c3SPaul Durrant 
4020f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
4021ed1f50c3SPaul Durrant {
4022ed1f50c3SPaul Durrant 	unsigned int off;
4023ed1f50c3SPaul Durrant 	bool fragment;
4024f9708b43SJan Beulich 	__sum16 *csum;
4025ed1f50c3SPaul Durrant 	int err;
4026ed1f50c3SPaul Durrant 
4027ed1f50c3SPaul Durrant 	fragment = false;
4028ed1f50c3SPaul Durrant 
4029ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
4030ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
4031ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
4032ed1f50c3SPaul Durrant 	if (err < 0)
4033ed1f50c3SPaul Durrant 		goto out;
4034ed1f50c3SPaul Durrant 
4035ed1f50c3SPaul Durrant 	if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
4036ed1f50c3SPaul Durrant 		fragment = true;
4037ed1f50c3SPaul Durrant 
4038ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
4039ed1f50c3SPaul Durrant 
4040ed1f50c3SPaul Durrant 	err = -EPROTO;
4041ed1f50c3SPaul Durrant 
4042ed1f50c3SPaul Durrant 	if (fragment)
4043ed1f50c3SPaul Durrant 		goto out;
4044ed1f50c3SPaul Durrant 
4045f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
4046f9708b43SJan Beulich 	if (IS_ERR(csum))
4047f9708b43SJan Beulich 		return PTR_ERR(csum);
4048ed1f50c3SPaul Durrant 
4049ed1f50c3SPaul Durrant 	if (recalculate)
4050f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
4051ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
4052ed1f50c3SPaul Durrant 					   skb->len - off,
4053f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
4054ed1f50c3SPaul Durrant 	err = 0;
4055ed1f50c3SPaul Durrant 
4056ed1f50c3SPaul Durrant out:
4057ed1f50c3SPaul Durrant 	return err;
4058ed1f50c3SPaul Durrant }
4059ed1f50c3SPaul Durrant 
4060ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
4061ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
4062ed1f50c3SPaul Durrant  */
4063ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
4064ed1f50c3SPaul Durrant 
4065ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
4066ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
4067ed1f50c3SPaul Durrant 
4068ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
4069ed1f50c3SPaul Durrant {
4070ed1f50c3SPaul Durrant 	int err;
4071ed1f50c3SPaul Durrant 	u8 nexthdr;
4072ed1f50c3SPaul Durrant 	unsigned int off;
4073ed1f50c3SPaul Durrant 	unsigned int len;
4074ed1f50c3SPaul Durrant 	bool fragment;
4075ed1f50c3SPaul Durrant 	bool done;
4076f9708b43SJan Beulich 	__sum16 *csum;
4077ed1f50c3SPaul Durrant 
4078ed1f50c3SPaul Durrant 	fragment = false;
4079ed1f50c3SPaul Durrant 	done = false;
4080ed1f50c3SPaul Durrant 
4081ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
4082ed1f50c3SPaul Durrant 
4083ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
4084ed1f50c3SPaul Durrant 	if (err < 0)
4085ed1f50c3SPaul Durrant 		goto out;
4086ed1f50c3SPaul Durrant 
4087ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
4088ed1f50c3SPaul Durrant 
4089ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
4090ed1f50c3SPaul Durrant 	while (off <= len && !done) {
4091ed1f50c3SPaul Durrant 		switch (nexthdr) {
4092ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
4093ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
4094ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
4095ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
4096ed1f50c3SPaul Durrant 
4097ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4098ed1f50c3SPaul Durrant 						  off +
4099ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
4100ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4101ed1f50c3SPaul Durrant 			if (err < 0)
4102ed1f50c3SPaul Durrant 				goto out;
4103ed1f50c3SPaul Durrant 
4104ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
4105ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4106ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
4107ed1f50c3SPaul Durrant 			break;
4108ed1f50c3SPaul Durrant 		}
4109ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
4110ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
4111ed1f50c3SPaul Durrant 
4112ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4113ed1f50c3SPaul Durrant 						  off +
4114ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
4115ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4116ed1f50c3SPaul Durrant 			if (err < 0)
4117ed1f50c3SPaul Durrant 				goto out;
4118ed1f50c3SPaul Durrant 
4119ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
4120ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4121ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
4122ed1f50c3SPaul Durrant 			break;
4123ed1f50c3SPaul Durrant 		}
4124ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
4125ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
4126ed1f50c3SPaul Durrant 
4127ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
4128ed1f50c3SPaul Durrant 						  off +
4129ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
4130ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
4131ed1f50c3SPaul Durrant 			if (err < 0)
4132ed1f50c3SPaul Durrant 				goto out;
4133ed1f50c3SPaul Durrant 
4134ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
4135ed1f50c3SPaul Durrant 
4136ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
4137ed1f50c3SPaul Durrant 				fragment = true;
4138ed1f50c3SPaul Durrant 
4139ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
4140ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
4141ed1f50c3SPaul Durrant 			break;
4142ed1f50c3SPaul Durrant 		}
4143ed1f50c3SPaul Durrant 		default:
4144ed1f50c3SPaul Durrant 			done = true;
4145ed1f50c3SPaul Durrant 			break;
4146ed1f50c3SPaul Durrant 		}
4147ed1f50c3SPaul Durrant 	}
4148ed1f50c3SPaul Durrant 
4149ed1f50c3SPaul Durrant 	err = -EPROTO;
4150ed1f50c3SPaul Durrant 
4151ed1f50c3SPaul Durrant 	if (!done || fragment)
4152ed1f50c3SPaul Durrant 		goto out;
4153ed1f50c3SPaul Durrant 
4154f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
4155f9708b43SJan Beulich 	if (IS_ERR(csum))
4156f9708b43SJan Beulich 		return PTR_ERR(csum);
4157ed1f50c3SPaul Durrant 
4158ed1f50c3SPaul Durrant 	if (recalculate)
4159f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
4160ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
4161f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
4162ed1f50c3SPaul Durrant 	err = 0;
4163ed1f50c3SPaul Durrant 
4164ed1f50c3SPaul Durrant out:
4165ed1f50c3SPaul Durrant 	return err;
4166ed1f50c3SPaul Durrant }
4167ed1f50c3SPaul Durrant 
4168ed1f50c3SPaul Durrant /**
4169ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
4170ed1f50c3SPaul Durrant  * @skb: the skb to set up
4171ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
4172ed1f50c3SPaul Durrant  */
4173ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
4174ed1f50c3SPaul Durrant {
4175ed1f50c3SPaul Durrant 	int err;
4176ed1f50c3SPaul Durrant 
4177ed1f50c3SPaul Durrant 	switch (skb->protocol) {
4178ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
4179f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
4180ed1f50c3SPaul Durrant 		break;
4181ed1f50c3SPaul Durrant 
4182ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
4183ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
4184ed1f50c3SPaul Durrant 		break;
4185ed1f50c3SPaul Durrant 
4186ed1f50c3SPaul Durrant 	default:
4187ed1f50c3SPaul Durrant 		err = -EPROTO;
4188ed1f50c3SPaul Durrant 		break;
4189ed1f50c3SPaul Durrant 	}
4190ed1f50c3SPaul Durrant 
4191ed1f50c3SPaul Durrant 	return err;
4192ed1f50c3SPaul Durrant }
4193ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
4194ed1f50c3SPaul Durrant 
41959afd85c9SLinus Lüssing /**
41969afd85c9SLinus Lüssing  * skb_checksum_maybe_trim - maybe trims the given skb
41979afd85c9SLinus Lüssing  * @skb: the skb to check
41989afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
41999afd85c9SLinus Lüssing  *
42009afd85c9SLinus Lüssing  * Checks whether the given skb has data beyond the given transport length.
42019afd85c9SLinus Lüssing  * If so, returns a cloned skb trimmed to this transport length.
42029afd85c9SLinus Lüssing  * Otherwise returns the provided skb. Returns NULL in error cases
42039afd85c9SLinus Lüssing  * (e.g. transport_len exceeds skb length or out-of-memory).
42049afd85c9SLinus Lüssing  *
4205a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
4206a516993fSLinus Lüssing  * differs from the provided skb.
42079afd85c9SLinus Lüssing  */
42089afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb,
42099afd85c9SLinus Lüssing 					       unsigned int transport_len)
42109afd85c9SLinus Lüssing {
42119afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
42129afd85c9SLinus Lüssing 	unsigned int len = skb_transport_offset(skb) + transport_len;
42139afd85c9SLinus Lüssing 	int ret;
42149afd85c9SLinus Lüssing 
4215a516993fSLinus Lüssing 	if (skb->len < len)
42169afd85c9SLinus Lüssing 		return NULL;
4217a516993fSLinus Lüssing 	else if (skb->len == len)
42189afd85c9SLinus Lüssing 		return skb;
42199afd85c9SLinus Lüssing 
42209afd85c9SLinus Lüssing 	skb_chk = skb_clone(skb, GFP_ATOMIC);
42219afd85c9SLinus Lüssing 	if (!skb_chk)
42229afd85c9SLinus Lüssing 		return NULL;
42239afd85c9SLinus Lüssing 
42249afd85c9SLinus Lüssing 	ret = pskb_trim_rcsum(skb_chk, len);
42259afd85c9SLinus Lüssing 	if (ret) {
42269afd85c9SLinus Lüssing 		kfree_skb(skb_chk);
42279afd85c9SLinus Lüssing 		return NULL;
42289afd85c9SLinus Lüssing 	}
42299afd85c9SLinus Lüssing 
42309afd85c9SLinus Lüssing 	return skb_chk;
42319afd85c9SLinus Lüssing }
42329afd85c9SLinus Lüssing 
42339afd85c9SLinus Lüssing /**
42349afd85c9SLinus Lüssing  * skb_checksum_trimmed - validate checksum of an skb
42359afd85c9SLinus Lüssing  * @skb: the skb to check
42369afd85c9SLinus Lüssing  * @transport_len: the data length beyond the network header
42379afd85c9SLinus Lüssing  * @skb_chkf: checksum function to use
42389afd85c9SLinus Lüssing  *
42399afd85c9SLinus Lüssing  * Applies the given checksum function skb_chkf to the provided skb.
42409afd85c9SLinus Lüssing  * Returns a checked and maybe trimmed skb. Returns NULL on error.
42419afd85c9SLinus Lüssing  *
42429afd85c9SLinus Lüssing  * If the skb has data beyond the given transport length, then a
42439afd85c9SLinus Lüssing  * trimmed & cloned skb is checked and returned.
42449afd85c9SLinus Lüssing  *
4245a516993fSLinus Lüssing  * Caller needs to set the skb transport header and free any returned skb if it
4246a516993fSLinus Lüssing  * differs from the provided skb.
42479afd85c9SLinus Lüssing  */
42489afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
42499afd85c9SLinus Lüssing 				     unsigned int transport_len,
42509afd85c9SLinus Lüssing 				     __sum16(*skb_chkf)(struct sk_buff *skb))
42519afd85c9SLinus Lüssing {
42529afd85c9SLinus Lüssing 	struct sk_buff *skb_chk;
42539afd85c9SLinus Lüssing 	unsigned int offset = skb_transport_offset(skb);
4254fcba67c9SLinus Lüssing 	__sum16 ret;
42559afd85c9SLinus Lüssing 
42569afd85c9SLinus Lüssing 	skb_chk = skb_checksum_maybe_trim(skb, transport_len);
42579afd85c9SLinus Lüssing 	if (!skb_chk)
4258a516993fSLinus Lüssing 		goto err;
42599afd85c9SLinus Lüssing 
4260a516993fSLinus Lüssing 	if (!pskb_may_pull(skb_chk, offset))
4261a516993fSLinus Lüssing 		goto err;
42629afd85c9SLinus Lüssing 
42639b368814SLinus Lüssing 	skb_pull_rcsum(skb_chk, offset);
42649afd85c9SLinus Lüssing 	ret = skb_chkf(skb_chk);
42659b368814SLinus Lüssing 	skb_push_rcsum(skb_chk, offset);
42669afd85c9SLinus Lüssing 
4267a516993fSLinus Lüssing 	if (ret)
4268a516993fSLinus Lüssing 		goto err;
42699afd85c9SLinus Lüssing 
42709afd85c9SLinus Lüssing 	return skb_chk;
4271a516993fSLinus Lüssing 
4272a516993fSLinus Lüssing err:
4273a516993fSLinus Lüssing 	if (skb_chk && skb_chk != skb)
4274a516993fSLinus Lüssing 		kfree_skb(skb_chk);
4275a516993fSLinus Lüssing 
4276a516993fSLinus Lüssing 	return NULL;
4277a516993fSLinus Lüssing 
42789afd85c9SLinus Lüssing }
42799afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed);
42809afd85c9SLinus Lüssing 
42814497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
42824497b076SBen Hutchings {
4283e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4284e87cc472SJoe Perches 			     skb->dev->name);
42854497b076SBen Hutchings }
42864497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4287bad43ca8SEric Dumazet 
4288bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4289bad43ca8SEric Dumazet {
42903d861f66SEric Dumazet 	if (head_stolen) {
42913d861f66SEric Dumazet 		skb_release_head_state(skb);
4292bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
42933d861f66SEric Dumazet 	} else {
4294bad43ca8SEric Dumazet 		__kfree_skb(skb);
4295bad43ca8SEric Dumazet 	}
42963d861f66SEric Dumazet }
4297bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
4298bad43ca8SEric Dumazet 
4299bad43ca8SEric Dumazet /**
4300bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
4301bad43ca8SEric Dumazet  * @to: prior buffer
4302bad43ca8SEric Dumazet  * @from: buffer to add
4303bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
4304c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
4305bad43ca8SEric Dumazet  */
4306bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4307bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
4308bad43ca8SEric Dumazet {
4309bad43ca8SEric Dumazet 	int i, delta, len = from->len;
4310bad43ca8SEric Dumazet 
4311bad43ca8SEric Dumazet 	*fragstolen = false;
4312bad43ca8SEric Dumazet 
4313bad43ca8SEric Dumazet 	if (skb_cloned(to))
4314bad43ca8SEric Dumazet 		return false;
4315bad43ca8SEric Dumazet 
4316bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
4317e93a0435SEric Dumazet 		if (len)
4318bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4319bad43ca8SEric Dumazet 		*delta_truesize = 0;
4320bad43ca8SEric Dumazet 		return true;
4321bad43ca8SEric Dumazet 	}
4322bad43ca8SEric Dumazet 
4323bad43ca8SEric Dumazet 	if (skb_has_frag_list(to) || skb_has_frag_list(from))
4324bad43ca8SEric Dumazet 		return false;
4325bad43ca8SEric Dumazet 
4326bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
4327bad43ca8SEric Dumazet 		struct page *page;
4328bad43ca8SEric Dumazet 		unsigned int offset;
4329bad43ca8SEric Dumazet 
4330bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
4331bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4332bad43ca8SEric Dumazet 			return false;
4333bad43ca8SEric Dumazet 
4334bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
4335bad43ca8SEric Dumazet 			return false;
4336bad43ca8SEric Dumazet 
4337bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4338bad43ca8SEric Dumazet 
4339bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
4340bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
4341bad43ca8SEric Dumazet 
4342bad43ca8SEric Dumazet 		skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4343bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
4344bad43ca8SEric Dumazet 		*fragstolen = true;
4345bad43ca8SEric Dumazet 	} else {
4346bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
4347bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4348bad43ca8SEric Dumazet 			return false;
4349bad43ca8SEric Dumazet 
4350f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4351bad43ca8SEric Dumazet 	}
4352bad43ca8SEric Dumazet 
4353bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
4354bad43ca8SEric Dumazet 
4355bad43ca8SEric Dumazet 	memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4356bad43ca8SEric Dumazet 	       skb_shinfo(from)->frags,
4357bad43ca8SEric Dumazet 	       skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4358bad43ca8SEric Dumazet 	skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4359bad43ca8SEric Dumazet 
4360bad43ca8SEric Dumazet 	if (!skb_cloned(from))
4361bad43ca8SEric Dumazet 		skb_shinfo(from)->nr_frags = 0;
4362bad43ca8SEric Dumazet 
43638ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
43648ea853fdSLi RongQing 	 * since we set nr_frags to 0.
43658ea853fdSLi RongQing 	 */
4366bad43ca8SEric Dumazet 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4367bad43ca8SEric Dumazet 		skb_frag_ref(from, i);
4368bad43ca8SEric Dumazet 
4369bad43ca8SEric Dumazet 	to->truesize += delta;
4370bad43ca8SEric Dumazet 	to->len += len;
4371bad43ca8SEric Dumazet 	to->data_len += len;
4372bad43ca8SEric Dumazet 
4373bad43ca8SEric Dumazet 	*delta_truesize = delta;
4374bad43ca8SEric Dumazet 	return true;
4375bad43ca8SEric Dumazet }
4376bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
4377621e84d6SNicolas Dichtel 
4378621e84d6SNicolas Dichtel /**
43798b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
4380621e84d6SNicolas Dichtel  *
4381621e84d6SNicolas Dichtel  * @skb: buffer to clean
43828b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
4383621e84d6SNicolas Dichtel  *
43848b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
43858b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
43868b27f277SNicolas Dichtel  * operations.
43878b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
43888b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
43898b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
4390621e84d6SNicolas Dichtel  */
43918b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4392621e84d6SNicolas Dichtel {
43932456e855SThomas Gleixner 	skb->tstamp = 0;
4394621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
4395621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
439660ff7467SWANG Cong 	skb->ignore_df = 0;
4397621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
4398621e84d6SNicolas Dichtel 	secpath_reset(skb);
4399621e84d6SNicolas Dichtel 	nf_reset(skb);
4400621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
4401213dd74aSHerbert Xu 
4402213dd74aSHerbert Xu 	if (!xnet)
4403213dd74aSHerbert Xu 		return;
4404213dd74aSHerbert Xu 
4405213dd74aSHerbert Xu 	skb_orphan(skb);
4406213dd74aSHerbert Xu 	skb->mark = 0;
4407621e84d6SNicolas Dichtel }
4408621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
4409de960aa9SFlorian Westphal 
4410de960aa9SFlorian Westphal /**
4411de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4412de960aa9SFlorian Westphal  *
4413de960aa9SFlorian Westphal  * @skb: GSO skb
4414de960aa9SFlorian Westphal  *
4415de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
4416de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
4417de960aa9SFlorian Westphal  *
4418de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4419de960aa9SFlorian Westphal  */
4420de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4421de960aa9SFlorian Westphal {
4422de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
4423f993bc25SFlorian Westphal 	unsigned int thlen = 0;
4424f993bc25SFlorian Westphal 
4425f993bc25SFlorian Westphal 	if (skb->encapsulation) {
4426f993bc25SFlorian Westphal 		thlen = skb_inner_transport_header(skb) -
4427f993bc25SFlorian Westphal 			skb_transport_header(skb);
4428de960aa9SFlorian Westphal 
4429de960aa9SFlorian Westphal 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4430f993bc25SFlorian Westphal 			thlen += inner_tcp_hdrlen(skb);
4431f993bc25SFlorian Westphal 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4432f993bc25SFlorian Westphal 		thlen = tcp_hdrlen(skb);
443390017accSMarcelo Ricardo Leitner 	} else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) {
443490017accSMarcelo Ricardo Leitner 		thlen = sizeof(struct sctphdr);
4435f993bc25SFlorian Westphal 	}
44366d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
44376d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
44386d39d589SFlorian Westphal 	 * accounted for.
44396d39d589SFlorian Westphal 	 */
4440f993bc25SFlorian Westphal 	return thlen + shinfo->gso_size;
4441de960aa9SFlorian Westphal }
4442de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
44430d5501c1SVlad Yasevich 
4444ae7ef81eSMarcelo Ricardo Leitner /**
4445ae7ef81eSMarcelo Ricardo Leitner  * skb_gso_validate_mtu - Return in case such skb fits a given MTU
4446ae7ef81eSMarcelo Ricardo Leitner  *
4447ae7ef81eSMarcelo Ricardo Leitner  * @skb: GSO skb
444876f21b99SDavid S. Miller  * @mtu: MTU to validate against
4449ae7ef81eSMarcelo Ricardo Leitner  *
4450ae7ef81eSMarcelo Ricardo Leitner  * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU
4451ae7ef81eSMarcelo Ricardo Leitner  * once split.
4452ae7ef81eSMarcelo Ricardo Leitner  */
4453ae7ef81eSMarcelo Ricardo Leitner bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu)
4454ae7ef81eSMarcelo Ricardo Leitner {
4455ae7ef81eSMarcelo Ricardo Leitner 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
4456ae7ef81eSMarcelo Ricardo Leitner 	const struct sk_buff *iter;
4457ae7ef81eSMarcelo Ricardo Leitner 	unsigned int hlen;
4458ae7ef81eSMarcelo Ricardo Leitner 
4459ae7ef81eSMarcelo Ricardo Leitner 	hlen = skb_gso_network_seglen(skb);
4460ae7ef81eSMarcelo Ricardo Leitner 
4461ae7ef81eSMarcelo Ricardo Leitner 	if (shinfo->gso_size != GSO_BY_FRAGS)
4462ae7ef81eSMarcelo Ricardo Leitner 		return hlen <= mtu;
4463ae7ef81eSMarcelo Ricardo Leitner 
4464ae7ef81eSMarcelo Ricardo Leitner 	/* Undo this so we can re-use header sizes */
4465ae7ef81eSMarcelo Ricardo Leitner 	hlen -= GSO_BY_FRAGS;
4466ae7ef81eSMarcelo Ricardo Leitner 
4467ae7ef81eSMarcelo Ricardo Leitner 	skb_walk_frags(skb, iter) {
4468ae7ef81eSMarcelo Ricardo Leitner 		if (hlen + skb_headlen(iter) > mtu)
4469ae7ef81eSMarcelo Ricardo Leitner 			return false;
4470ae7ef81eSMarcelo Ricardo Leitner 	}
4471ae7ef81eSMarcelo Ricardo Leitner 
4472ae7ef81eSMarcelo Ricardo Leitner 	return true;
4473ae7ef81eSMarcelo Ricardo Leitner }
4474ae7ef81eSMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gso_validate_mtu);
4475ae7ef81eSMarcelo Ricardo Leitner 
44760d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
44770d5501c1SVlad Yasevich {
44780d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
44790d5501c1SVlad Yasevich 		kfree_skb(skb);
44800d5501c1SVlad Yasevich 		return NULL;
44810d5501c1SVlad Yasevich 	}
44820d5501c1SVlad Yasevich 
4483f6548615SVlad Yasevich 	memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN,
4484a6e18ff1SVlad Yasevich 		2 * ETH_ALEN);
44850d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
44860d5501c1SVlad Yasevich 	return skb;
44870d5501c1SVlad Yasevich }
44880d5501c1SVlad Yasevich 
44890d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
44900d5501c1SVlad Yasevich {
44910d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
44920d5501c1SVlad Yasevich 	u16 vlan_tci;
44930d5501c1SVlad Yasevich 
4494df8a39deSJiri Pirko 	if (unlikely(skb_vlan_tag_present(skb))) {
44950d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
44960d5501c1SVlad Yasevich 		return skb;
44970d5501c1SVlad Yasevich 	}
44980d5501c1SVlad Yasevich 
44990d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
45000d5501c1SVlad Yasevich 	if (unlikely(!skb))
45010d5501c1SVlad Yasevich 		goto err_free;
45020d5501c1SVlad Yasevich 
45030d5501c1SVlad Yasevich 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
45040d5501c1SVlad Yasevich 		goto err_free;
45050d5501c1SVlad Yasevich 
45060d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
45070d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
45080d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
45090d5501c1SVlad Yasevich 
45100d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
45110d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
45120d5501c1SVlad Yasevich 
45130d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
45140d5501c1SVlad Yasevich 	if (unlikely(!skb))
45150d5501c1SVlad Yasevich 		goto err_free;
45160d5501c1SVlad Yasevich 
45170d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
45180d5501c1SVlad Yasevich 	skb_reset_transport_header(skb);
45190d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
45200d5501c1SVlad Yasevich 
45210d5501c1SVlad Yasevich 	return skb;
45220d5501c1SVlad Yasevich 
45230d5501c1SVlad Yasevich err_free:
45240d5501c1SVlad Yasevich 	kfree_skb(skb);
45250d5501c1SVlad Yasevich 	return NULL;
45260d5501c1SVlad Yasevich }
45270d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
45282e4e4410SEric Dumazet 
4529e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len)
4530e2195121SJiri Pirko {
4531e2195121SJiri Pirko 	if (!pskb_may_pull(skb, write_len))
4532e2195121SJiri Pirko 		return -ENOMEM;
4533e2195121SJiri Pirko 
4534e2195121SJiri Pirko 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4535e2195121SJiri Pirko 		return 0;
4536e2195121SJiri Pirko 
4537e2195121SJiri Pirko 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4538e2195121SJiri Pirko }
4539e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
4540e2195121SJiri Pirko 
4541bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly.
4542bfca4c52SShmulik Ladkani  * expects a non skb_vlan_tag_present skb with a vlan tag payload
4543bfca4c52SShmulik Ladkani  */
4544bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
454593515d53SJiri Pirko {
454693515d53SJiri Pirko 	struct vlan_hdr *vhdr;
4547b6a79208SShmulik Ladkani 	int offset = skb->data - skb_mac_header(skb);
454893515d53SJiri Pirko 	int err;
454993515d53SJiri Pirko 
4550b6a79208SShmulik Ladkani 	if (WARN_ONCE(offset,
4551b6a79208SShmulik Ladkani 		      "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n",
4552b6a79208SShmulik Ladkani 		      offset)) {
4553b6a79208SShmulik Ladkani 		return -EINVAL;
4554b6a79208SShmulik Ladkani 	}
4555b6a79208SShmulik Ladkani 
455693515d53SJiri Pirko 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
455793515d53SJiri Pirko 	if (unlikely(err))
4558b6a79208SShmulik Ladkani 		return err;
455993515d53SJiri Pirko 
456093515d53SJiri Pirko 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
456193515d53SJiri Pirko 
456293515d53SJiri Pirko 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
456393515d53SJiri Pirko 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
456493515d53SJiri Pirko 
456593515d53SJiri Pirko 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
456693515d53SJiri Pirko 	__skb_pull(skb, VLAN_HLEN);
456793515d53SJiri Pirko 
456893515d53SJiri Pirko 	vlan_set_encap_proto(skb, vhdr);
456993515d53SJiri Pirko 	skb->mac_header += VLAN_HLEN;
457093515d53SJiri Pirko 
457193515d53SJiri Pirko 	if (skb_network_offset(skb) < ETH_HLEN)
457293515d53SJiri Pirko 		skb_set_network_header(skb, ETH_HLEN);
457393515d53SJiri Pirko 
457493515d53SJiri Pirko 	skb_reset_mac_len(skb);
457593515d53SJiri Pirko 
457693515d53SJiri Pirko 	return err;
457793515d53SJiri Pirko }
4578bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop);
457993515d53SJiri Pirko 
4580b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload.
4581b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
4582b6a79208SShmulik Ladkani  */
458393515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
458493515d53SJiri Pirko {
458593515d53SJiri Pirko 	u16 vlan_tci;
458693515d53SJiri Pirko 	__be16 vlan_proto;
458793515d53SJiri Pirko 	int err;
458893515d53SJiri Pirko 
4589df8a39deSJiri Pirko 	if (likely(skb_vlan_tag_present(skb))) {
459093515d53SJiri Pirko 		skb->vlan_tci = 0;
459193515d53SJiri Pirko 	} else {
4592ecf4ee41SShmulik Ladkani 		if (unlikely(!eth_type_vlan(skb->protocol)))
459393515d53SJiri Pirko 			return 0;
459493515d53SJiri Pirko 
459593515d53SJiri Pirko 		err = __skb_vlan_pop(skb, &vlan_tci);
459693515d53SJiri Pirko 		if (err)
459793515d53SJiri Pirko 			return err;
459893515d53SJiri Pirko 	}
459993515d53SJiri Pirko 	/* move next vlan tag to hw accel tag */
4600ecf4ee41SShmulik Ladkani 	if (likely(!eth_type_vlan(skb->protocol)))
460193515d53SJiri Pirko 		return 0;
460293515d53SJiri Pirko 
460393515d53SJiri Pirko 	vlan_proto = skb->protocol;
460493515d53SJiri Pirko 	err = __skb_vlan_pop(skb, &vlan_tci);
460593515d53SJiri Pirko 	if (unlikely(err))
460693515d53SJiri Pirko 		return err;
460793515d53SJiri Pirko 
460893515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
460993515d53SJiri Pirko 	return 0;
461093515d53SJiri Pirko }
461193515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
461293515d53SJiri Pirko 
4613b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present).
4614b6a79208SShmulik Ladkani  * Expects skb->data at mac header.
4615b6a79208SShmulik Ladkani  */
461693515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
461793515d53SJiri Pirko {
4618df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
4619b6a79208SShmulik Ladkani 		int offset = skb->data - skb_mac_header(skb);
462093515d53SJiri Pirko 		int err;
462193515d53SJiri Pirko 
4622b6a79208SShmulik Ladkani 		if (WARN_ONCE(offset,
4623b6a79208SShmulik Ladkani 			      "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n",
4624b6a79208SShmulik Ladkani 			      offset)) {
4625b6a79208SShmulik Ladkani 			return -EINVAL;
4626b6a79208SShmulik Ladkani 		}
4627b6a79208SShmulik Ladkani 
462893515d53SJiri Pirko 		err = __vlan_insert_tag(skb, skb->vlan_proto,
4629df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
4630b6a79208SShmulik Ladkani 		if (err)
463193515d53SJiri Pirko 			return err;
46329241e2dfSDaniel Borkmann 
463393515d53SJiri Pirko 		skb->protocol = skb->vlan_proto;
463493515d53SJiri Pirko 		skb->mac_len += VLAN_HLEN;
463593515d53SJiri Pirko 
46366b83d28aSDaniel Borkmann 		skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
463793515d53SJiri Pirko 	}
463893515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
463993515d53SJiri Pirko 	return 0;
464093515d53SJiri Pirko }
464193515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
464293515d53SJiri Pirko 
46432e4e4410SEric Dumazet /**
46442e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
46452e4e4410SEric Dumazet  *
4646de3f0d0eSMasanari Iida  * @header_len: size of linear part
4647de3f0d0eSMasanari Iida  * @data_len: needed length in frags
4648de3f0d0eSMasanari Iida  * @max_page_order: max page order desired.
4649de3f0d0eSMasanari Iida  * @errcode: pointer to error code if any
4650de3f0d0eSMasanari Iida  * @gfp_mask: allocation mask
46512e4e4410SEric Dumazet  *
46522e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
46532e4e4410SEric Dumazet  */
46542e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
46552e4e4410SEric Dumazet 				     unsigned long data_len,
46562e4e4410SEric Dumazet 				     int max_page_order,
46572e4e4410SEric Dumazet 				     int *errcode,
46582e4e4410SEric Dumazet 				     gfp_t gfp_mask)
46592e4e4410SEric Dumazet {
46602e4e4410SEric Dumazet 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
46612e4e4410SEric Dumazet 	unsigned long chunk;
46622e4e4410SEric Dumazet 	struct sk_buff *skb;
46632e4e4410SEric Dumazet 	struct page *page;
46642e4e4410SEric Dumazet 	gfp_t gfp_head;
46652e4e4410SEric Dumazet 	int i;
46662e4e4410SEric Dumazet 
46672e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
46682e4e4410SEric Dumazet 	/* Note this test could be relaxed, if we succeed to allocate
46692e4e4410SEric Dumazet 	 * high order pages...
46702e4e4410SEric Dumazet 	 */
46712e4e4410SEric Dumazet 	if (npages > MAX_SKB_FRAGS)
46722e4e4410SEric Dumazet 		return NULL;
46732e4e4410SEric Dumazet 
46742e4e4410SEric Dumazet 	gfp_head = gfp_mask;
4675d0164adcSMel Gorman 	if (gfp_head & __GFP_DIRECT_RECLAIM)
46762e4e4410SEric Dumazet 		gfp_head |= __GFP_REPEAT;
46772e4e4410SEric Dumazet 
46782e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
46792e4e4410SEric Dumazet 	skb = alloc_skb(header_len, gfp_head);
46802e4e4410SEric Dumazet 	if (!skb)
46812e4e4410SEric Dumazet 		return NULL;
46822e4e4410SEric Dumazet 
46832e4e4410SEric Dumazet 	skb->truesize += npages << PAGE_SHIFT;
46842e4e4410SEric Dumazet 
46852e4e4410SEric Dumazet 	for (i = 0; npages > 0; i++) {
46862e4e4410SEric Dumazet 		int order = max_page_order;
46872e4e4410SEric Dumazet 
46882e4e4410SEric Dumazet 		while (order) {
46892e4e4410SEric Dumazet 			if (npages >= 1 << order) {
4690d0164adcSMel Gorman 				page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) |
46912e4e4410SEric Dumazet 						   __GFP_COMP |
46922e4e4410SEric Dumazet 						   __GFP_NOWARN |
46932e4e4410SEric Dumazet 						   __GFP_NORETRY,
46942e4e4410SEric Dumazet 						   order);
46952e4e4410SEric Dumazet 				if (page)
46962e4e4410SEric Dumazet 					goto fill_page;
46972e4e4410SEric Dumazet 				/* Do not retry other high order allocations */
46982e4e4410SEric Dumazet 				order = 1;
46992e4e4410SEric Dumazet 				max_page_order = 0;
47002e4e4410SEric Dumazet 			}
47012e4e4410SEric Dumazet 			order--;
47022e4e4410SEric Dumazet 		}
47032e4e4410SEric Dumazet 		page = alloc_page(gfp_mask);
47042e4e4410SEric Dumazet 		if (!page)
47052e4e4410SEric Dumazet 			goto failure;
47062e4e4410SEric Dumazet fill_page:
47072e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
47082e4e4410SEric Dumazet 			      PAGE_SIZE << order);
47092e4e4410SEric Dumazet 		skb_fill_page_desc(skb, i, page, 0, chunk);
47102e4e4410SEric Dumazet 		data_len -= chunk;
47112e4e4410SEric Dumazet 		npages -= 1 << order;
47122e4e4410SEric Dumazet 	}
47132e4e4410SEric Dumazet 	return skb;
47142e4e4410SEric Dumazet 
47152e4e4410SEric Dumazet failure:
47162e4e4410SEric Dumazet 	kfree_skb(skb);
47172e4e4410SEric Dumazet 	return NULL;
47182e4e4410SEric Dumazet }
47192e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
47206fa01ccdSSowmini Varadhan 
47216fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */
47226fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off,
47236fa01ccdSSowmini Varadhan 				    const int headlen, gfp_t gfp_mask)
47246fa01ccdSSowmini Varadhan {
47256fa01ccdSSowmini Varadhan 	int i;
47266fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
47276fa01ccdSSowmini Varadhan 	int new_hlen = headlen - off;
47286fa01ccdSSowmini Varadhan 	u8 *data;
47296fa01ccdSSowmini Varadhan 
47306fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
47316fa01ccdSSowmini Varadhan 
47326fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
47336fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
47346fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
47356fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
47366fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
47376fa01ccdSSowmini Varadhan 	if (!data)
47386fa01ccdSSowmini Varadhan 		return -ENOMEM;
47396fa01ccdSSowmini Varadhan 
47406fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
47416fa01ccdSSowmini Varadhan 
47426fa01ccdSSowmini Varadhan 	/* Copy real data, and all frags */
47436fa01ccdSSowmini Varadhan 	skb_copy_from_linear_data_offset(skb, off, data, new_hlen);
47446fa01ccdSSowmini Varadhan 	skb->len -= off;
47456fa01ccdSSowmini Varadhan 
47466fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
47476fa01ccdSSowmini Varadhan 	       skb_shinfo(skb),
47486fa01ccdSSowmini Varadhan 	       offsetof(struct skb_shared_info,
47496fa01ccdSSowmini Varadhan 			frags[skb_shinfo(skb)->nr_frags]));
47506fa01ccdSSowmini Varadhan 	if (skb_cloned(skb)) {
47516fa01ccdSSowmini Varadhan 		/* drop the old head gracefully */
47526fa01ccdSSowmini Varadhan 		if (skb_orphan_frags(skb, gfp_mask)) {
47536fa01ccdSSowmini Varadhan 			kfree(data);
47546fa01ccdSSowmini Varadhan 			return -ENOMEM;
47556fa01ccdSSowmini Varadhan 		}
47566fa01ccdSSowmini Varadhan 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
47576fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
47586fa01ccdSSowmini Varadhan 		if (skb_has_frag_list(skb))
47596fa01ccdSSowmini Varadhan 			skb_clone_fraglist(skb);
47606fa01ccdSSowmini Varadhan 		skb_release_data(skb);
47616fa01ccdSSowmini Varadhan 	} else {
47626fa01ccdSSowmini Varadhan 		/* we can reuse existing recount- all we did was
47636fa01ccdSSowmini Varadhan 		 * relocate values
47646fa01ccdSSowmini Varadhan 		 */
47656fa01ccdSSowmini Varadhan 		skb_free_head(skb);
47666fa01ccdSSowmini Varadhan 	}
47676fa01ccdSSowmini Varadhan 
47686fa01ccdSSowmini Varadhan 	skb->head = data;
47696fa01ccdSSowmini Varadhan 	skb->data = data;
47706fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
47716fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
47726fa01ccdSSowmini Varadhan 	skb->end = size;
47736fa01ccdSSowmini Varadhan #else
47746fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
47756fa01ccdSSowmini Varadhan #endif
47766fa01ccdSSowmini Varadhan 	skb_set_tail_pointer(skb, skb_headlen(skb));
47776fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
47786fa01ccdSSowmini Varadhan 	skb->cloned = 0;
47796fa01ccdSSowmini Varadhan 	skb->hdr_len = 0;
47806fa01ccdSSowmini Varadhan 	skb->nohdr = 0;
47816fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
47826fa01ccdSSowmini Varadhan 
47836fa01ccdSSowmini Varadhan 	return 0;
47846fa01ccdSSowmini Varadhan }
47856fa01ccdSSowmini Varadhan 
47866fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp);
47876fa01ccdSSowmini Varadhan 
47886fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into
47896fa01ccdSSowmini Varadhan  * pskb_carve()
47906fa01ccdSSowmini Varadhan  */
47916fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb,
47926fa01ccdSSowmini Varadhan 				struct skb_shared_info *shinfo, int eat,
47936fa01ccdSSowmini Varadhan 				gfp_t gfp_mask)
47946fa01ccdSSowmini Varadhan {
47956fa01ccdSSowmini Varadhan 	struct sk_buff *list = shinfo->frag_list;
47966fa01ccdSSowmini Varadhan 	struct sk_buff *clone = NULL;
47976fa01ccdSSowmini Varadhan 	struct sk_buff *insp = NULL;
47986fa01ccdSSowmini Varadhan 
47996fa01ccdSSowmini Varadhan 	do {
48006fa01ccdSSowmini Varadhan 		if (!list) {
48016fa01ccdSSowmini Varadhan 			pr_err("Not enough bytes to eat. Want %d\n", eat);
48026fa01ccdSSowmini Varadhan 			return -EFAULT;
48036fa01ccdSSowmini Varadhan 		}
48046fa01ccdSSowmini Varadhan 		if (list->len <= eat) {
48056fa01ccdSSowmini Varadhan 			/* Eaten as whole. */
48066fa01ccdSSowmini Varadhan 			eat -= list->len;
48076fa01ccdSSowmini Varadhan 			list = list->next;
48086fa01ccdSSowmini Varadhan 			insp = list;
48096fa01ccdSSowmini Varadhan 		} else {
48106fa01ccdSSowmini Varadhan 			/* Eaten partially. */
48116fa01ccdSSowmini Varadhan 			if (skb_shared(list)) {
48126fa01ccdSSowmini Varadhan 				clone = skb_clone(list, gfp_mask);
48136fa01ccdSSowmini Varadhan 				if (!clone)
48146fa01ccdSSowmini Varadhan 					return -ENOMEM;
48156fa01ccdSSowmini Varadhan 				insp = list->next;
48166fa01ccdSSowmini Varadhan 				list = clone;
48176fa01ccdSSowmini Varadhan 			} else {
48186fa01ccdSSowmini Varadhan 				/* This may be pulled without problems. */
48196fa01ccdSSowmini Varadhan 				insp = list;
48206fa01ccdSSowmini Varadhan 			}
48216fa01ccdSSowmini Varadhan 			if (pskb_carve(list, eat, gfp_mask) < 0) {
48226fa01ccdSSowmini Varadhan 				kfree_skb(clone);
48236fa01ccdSSowmini Varadhan 				return -ENOMEM;
48246fa01ccdSSowmini Varadhan 			}
48256fa01ccdSSowmini Varadhan 			break;
48266fa01ccdSSowmini Varadhan 		}
48276fa01ccdSSowmini Varadhan 	} while (eat);
48286fa01ccdSSowmini Varadhan 
48296fa01ccdSSowmini Varadhan 	/* Free pulled out fragments. */
48306fa01ccdSSowmini Varadhan 	while ((list = shinfo->frag_list) != insp) {
48316fa01ccdSSowmini Varadhan 		shinfo->frag_list = list->next;
48326fa01ccdSSowmini Varadhan 		kfree_skb(list);
48336fa01ccdSSowmini Varadhan 	}
48346fa01ccdSSowmini Varadhan 	/* And insert new clone at head. */
48356fa01ccdSSowmini Varadhan 	if (clone) {
48366fa01ccdSSowmini Varadhan 		clone->next = list;
48376fa01ccdSSowmini Varadhan 		shinfo->frag_list = clone;
48386fa01ccdSSowmini Varadhan 	}
48396fa01ccdSSowmini Varadhan 	return 0;
48406fa01ccdSSowmini Varadhan }
48416fa01ccdSSowmini Varadhan 
48426fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the
48436fa01ccdSSowmini Varadhan  * non-linear part of skb
48446fa01ccdSSowmini Varadhan  */
48456fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off,
48466fa01ccdSSowmini Varadhan 				       int pos, gfp_t gfp_mask)
48476fa01ccdSSowmini Varadhan {
48486fa01ccdSSowmini Varadhan 	int i, k = 0;
48496fa01ccdSSowmini Varadhan 	int size = skb_end_offset(skb);
48506fa01ccdSSowmini Varadhan 	u8 *data;
48516fa01ccdSSowmini Varadhan 	const int nfrags = skb_shinfo(skb)->nr_frags;
48526fa01ccdSSowmini Varadhan 	struct skb_shared_info *shinfo;
48536fa01ccdSSowmini Varadhan 
48546fa01ccdSSowmini Varadhan 	size = SKB_DATA_ALIGN(size);
48556fa01ccdSSowmini Varadhan 
48566fa01ccdSSowmini Varadhan 	if (skb_pfmemalloc(skb))
48576fa01ccdSSowmini Varadhan 		gfp_mask |= __GFP_MEMALLOC;
48586fa01ccdSSowmini Varadhan 	data = kmalloc_reserve(size +
48596fa01ccdSSowmini Varadhan 			       SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
48606fa01ccdSSowmini Varadhan 			       gfp_mask, NUMA_NO_NODE, NULL);
48616fa01ccdSSowmini Varadhan 	if (!data)
48626fa01ccdSSowmini Varadhan 		return -ENOMEM;
48636fa01ccdSSowmini Varadhan 
48646fa01ccdSSowmini Varadhan 	size = SKB_WITH_OVERHEAD(ksize(data));
48656fa01ccdSSowmini Varadhan 
48666fa01ccdSSowmini Varadhan 	memcpy((struct skb_shared_info *)(data + size),
48676fa01ccdSSowmini Varadhan 	       skb_shinfo(skb), offsetof(struct skb_shared_info,
48686fa01ccdSSowmini Varadhan 					 frags[skb_shinfo(skb)->nr_frags]));
48696fa01ccdSSowmini Varadhan 	if (skb_orphan_frags(skb, gfp_mask)) {
48706fa01ccdSSowmini Varadhan 		kfree(data);
48716fa01ccdSSowmini Varadhan 		return -ENOMEM;
48726fa01ccdSSowmini Varadhan 	}
48736fa01ccdSSowmini Varadhan 	shinfo = (struct skb_shared_info *)(data + size);
48746fa01ccdSSowmini Varadhan 	for (i = 0; i < nfrags; i++) {
48756fa01ccdSSowmini Varadhan 		int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]);
48766fa01ccdSSowmini Varadhan 
48776fa01ccdSSowmini Varadhan 		if (pos + fsize > off) {
48786fa01ccdSSowmini Varadhan 			shinfo->frags[k] = skb_shinfo(skb)->frags[i];
48796fa01ccdSSowmini Varadhan 
48806fa01ccdSSowmini Varadhan 			if (pos < off) {
48816fa01ccdSSowmini Varadhan 				/* Split frag.
48826fa01ccdSSowmini Varadhan 				 * We have two variants in this case:
48836fa01ccdSSowmini Varadhan 				 * 1. Move all the frag to the second
48846fa01ccdSSowmini Varadhan 				 *    part, if it is possible. F.e.
48856fa01ccdSSowmini Varadhan 				 *    this approach is mandatory for TUX,
48866fa01ccdSSowmini Varadhan 				 *    where splitting is expensive.
48876fa01ccdSSowmini Varadhan 				 * 2. Split is accurately. We make this.
48886fa01ccdSSowmini Varadhan 				 */
48896fa01ccdSSowmini Varadhan 				shinfo->frags[0].page_offset += off - pos;
48906fa01ccdSSowmini Varadhan 				skb_frag_size_sub(&shinfo->frags[0], off - pos);
48916fa01ccdSSowmini Varadhan 			}
48926fa01ccdSSowmini Varadhan 			skb_frag_ref(skb, i);
48936fa01ccdSSowmini Varadhan 			k++;
48946fa01ccdSSowmini Varadhan 		}
48956fa01ccdSSowmini Varadhan 		pos += fsize;
48966fa01ccdSSowmini Varadhan 	}
48976fa01ccdSSowmini Varadhan 	shinfo->nr_frags = k;
48986fa01ccdSSowmini Varadhan 	if (skb_has_frag_list(skb))
48996fa01ccdSSowmini Varadhan 		skb_clone_fraglist(skb);
49006fa01ccdSSowmini Varadhan 
49016fa01ccdSSowmini Varadhan 	if (k == 0) {
49026fa01ccdSSowmini Varadhan 		/* split line is in frag list */
49036fa01ccdSSowmini Varadhan 		pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask);
49046fa01ccdSSowmini Varadhan 	}
49056fa01ccdSSowmini Varadhan 	skb_release_data(skb);
49066fa01ccdSSowmini Varadhan 
49076fa01ccdSSowmini Varadhan 	skb->head = data;
49086fa01ccdSSowmini Varadhan 	skb->head_frag = 0;
49096fa01ccdSSowmini Varadhan 	skb->data = data;
49106fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET
49116fa01ccdSSowmini Varadhan 	skb->end = size;
49126fa01ccdSSowmini Varadhan #else
49136fa01ccdSSowmini Varadhan 	skb->end = skb->head + size;
49146fa01ccdSSowmini Varadhan #endif
49156fa01ccdSSowmini Varadhan 	skb_reset_tail_pointer(skb);
49166fa01ccdSSowmini Varadhan 	skb_headers_offset_update(skb, 0);
49176fa01ccdSSowmini Varadhan 	skb->cloned   = 0;
49186fa01ccdSSowmini Varadhan 	skb->hdr_len  = 0;
49196fa01ccdSSowmini Varadhan 	skb->nohdr    = 0;
49206fa01ccdSSowmini Varadhan 	skb->len -= off;
49216fa01ccdSSowmini Varadhan 	skb->data_len = skb->len;
49226fa01ccdSSowmini Varadhan 	atomic_set(&skb_shinfo(skb)->dataref, 1);
49236fa01ccdSSowmini Varadhan 	return 0;
49246fa01ccdSSowmini Varadhan }
49256fa01ccdSSowmini Varadhan 
49266fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */
49276fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp)
49286fa01ccdSSowmini Varadhan {
49296fa01ccdSSowmini Varadhan 	int headlen = skb_headlen(skb);
49306fa01ccdSSowmini Varadhan 
49316fa01ccdSSowmini Varadhan 	if (len < headlen)
49326fa01ccdSSowmini Varadhan 		return pskb_carve_inside_header(skb, len, headlen, gfp);
49336fa01ccdSSowmini Varadhan 	else
49346fa01ccdSSowmini Varadhan 		return pskb_carve_inside_nonlinear(skb, len, headlen, gfp);
49356fa01ccdSSowmini Varadhan }
49366fa01ccdSSowmini Varadhan 
49376fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in
49386fa01ccdSSowmini Varadhan  * a new skb
49396fa01ccdSSowmini Varadhan  */
49406fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off,
49416fa01ccdSSowmini Varadhan 			     int to_copy, gfp_t gfp)
49426fa01ccdSSowmini Varadhan {
49436fa01ccdSSowmini Varadhan 	struct sk_buff  *clone = skb_clone(skb, gfp);
49446fa01ccdSSowmini Varadhan 
49456fa01ccdSSowmini Varadhan 	if (!clone)
49466fa01ccdSSowmini Varadhan 		return NULL;
49476fa01ccdSSowmini Varadhan 
49486fa01ccdSSowmini Varadhan 	if (pskb_carve(clone, off, gfp) < 0 ||
49496fa01ccdSSowmini Varadhan 	    pskb_trim(clone, to_copy)) {
49506fa01ccdSSowmini Varadhan 		kfree_skb(clone);
49516fa01ccdSSowmini Varadhan 		return NULL;
49526fa01ccdSSowmini Varadhan 	}
49536fa01ccdSSowmini Varadhan 	return clone;
49546fa01ccdSSowmini Varadhan }
49556fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract);
4956c8c8b127SEric Dumazet 
4957c8c8b127SEric Dumazet /**
4958c8c8b127SEric Dumazet  * skb_condense - try to get rid of fragments/frag_list if possible
4959c8c8b127SEric Dumazet  * @skb: buffer
4960c8c8b127SEric Dumazet  *
4961c8c8b127SEric Dumazet  * Can be used to save memory before skb is added to a busy queue.
4962c8c8b127SEric Dumazet  * If packet has bytes in frags and enough tail room in skb->head,
4963c8c8b127SEric Dumazet  * pull all of them, so that we can free the frags right now and adjust
4964c8c8b127SEric Dumazet  * truesize.
4965c8c8b127SEric Dumazet  * Notes:
4966c8c8b127SEric Dumazet  *	We do not reallocate skb->head thus can not fail.
4967c8c8b127SEric Dumazet  *	Caller must re-evaluate skb->truesize if needed.
4968c8c8b127SEric Dumazet  */
4969c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb)
4970c8c8b127SEric Dumazet {
49713174fed9SEric Dumazet 	if (skb->data_len) {
49723174fed9SEric Dumazet 		if (skb->data_len > skb->end - skb->tail ||
4973c8c8b127SEric Dumazet 		    skb_cloned(skb))
4974c8c8b127SEric Dumazet 			return;
4975c8c8b127SEric Dumazet 
4976c8c8b127SEric Dumazet 		/* Nice, we can free page frag(s) right now */
4977c8c8b127SEric Dumazet 		__pskb_pull_tail(skb, skb->data_len);
49783174fed9SEric Dumazet 	}
49793174fed9SEric Dumazet 	/* At this point, skb->truesize might be over estimated,
49803174fed9SEric Dumazet 	 * because skb had a fragment, and fragments do not tell
49813174fed9SEric Dumazet 	 * their truesize.
49823174fed9SEric Dumazet 	 * When we pulled its content into skb->head, fragment
49833174fed9SEric Dumazet 	 * was freed, but __pskb_pull_tail() could not possibly
49843174fed9SEric Dumazet 	 * adjust skb->truesize, not knowing the frag truesize.
4985c8c8b127SEric Dumazet 	 */
4986c8c8b127SEric Dumazet 	skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
4987c8c8b127SEric Dumazet }
4988