xref: /openbmc/linux/net/core/skbuff.c (revision bec3cfdc)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
31da177e4SLinus Torvalds  *
4113aa838SAlan Cox  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
51da177e4SLinus Torvalds  *			Florian La Roche <rzsfl@rz.uni-sb.de>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	Fixes:
81da177e4SLinus Torvalds  *		Alan Cox	:	Fixed the worst of the load
91da177e4SLinus Torvalds  *					balancer bugs.
101da177e4SLinus Torvalds  *		Dave Platt	:	Interrupt stacking fix.
111da177e4SLinus Torvalds  *	Richard Kooijman	:	Timestamp fixes.
121da177e4SLinus Torvalds  *		Alan Cox	:	Changed buffer format.
131da177e4SLinus Torvalds  *		Alan Cox	:	destructor hook for AF_UNIX etc.
141da177e4SLinus Torvalds  *		Linus Torvalds	:	Better skb_clone.
151da177e4SLinus Torvalds  *		Alan Cox	:	Added skb_copy.
161da177e4SLinus Torvalds  *		Alan Cox	:	Added all the changed routines Linus
171da177e4SLinus Torvalds  *					only put in the headers
181da177e4SLinus Torvalds  *		Ray VanTassle	:	Fixed --skb->lock in free
191da177e4SLinus Torvalds  *		Alan Cox	:	skb_copy copy arp field
201da177e4SLinus Torvalds  *		Andi Kleen	:	slabified it.
211da177e4SLinus Torvalds  *		Robert Olsson	:	Removed skb_head_pool
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *	NOTE:
241da177e4SLinus Torvalds  *		The __skb_ routines should be called with interrupts
251da177e4SLinus Torvalds  *	disabled, or you better be *real* sure that the operation is atomic
261da177e4SLinus Torvalds  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
271da177e4SLinus Torvalds  *	or via disabling bottom half handlers, etc).
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
301da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
311da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
321da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  *	The functions in this file will not compile correctly with gcc 2.4.x
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
39e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40e005d193SJoe Perches 
411da177e4SLinus Torvalds #include <linux/module.h>
421da177e4SLinus Torvalds #include <linux/types.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
44fe55f6d5SVegard Nossum #include <linux/kmemcheck.h>
451da177e4SLinus Torvalds #include <linux/mm.h>
461da177e4SLinus Torvalds #include <linux/interrupt.h>
471da177e4SLinus Torvalds #include <linux/in.h>
481da177e4SLinus Torvalds #include <linux/inet.h>
491da177e4SLinus Torvalds #include <linux/slab.h>
50de960aa9SFlorian Westphal #include <linux/tcp.h>
51de960aa9SFlorian Westphal #include <linux/udp.h>
521da177e4SLinus Torvalds #include <linux/netdevice.h>
531da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
541da177e4SLinus Torvalds #include <net/pkt_sched.h>
551da177e4SLinus Torvalds #endif
561da177e4SLinus Torvalds #include <linux/string.h>
571da177e4SLinus Torvalds #include <linux/skbuff.h>
589c55e01cSJens Axboe #include <linux/splice.h>
591da177e4SLinus Torvalds #include <linux/cache.h>
601da177e4SLinus Torvalds #include <linux/rtnetlink.h>
611da177e4SLinus Torvalds #include <linux/init.h>
62716ea3a7SDavid Howells #include <linux/scatterlist.h>
63ac45f602SPatrick Ohly #include <linux/errqueue.h>
64268bb0ceSLinus Torvalds #include <linux/prefetch.h>
650d5501c1SVlad Yasevich #include <linux/if_vlan.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <net/protocol.h>
681da177e4SLinus Torvalds #include <net/dst.h>
691da177e4SLinus Torvalds #include <net/sock.h>
701da177e4SLinus Torvalds #include <net/checksum.h>
71ed1f50c3SPaul Durrant #include <net/ip6_checksum.h>
721da177e4SLinus Torvalds #include <net/xfrm.h>
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #include <asm/uaccess.h>
75ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
7651c56b00SEric Dumazet #include <linux/highmem.h>
77a1f8e7f7SAl Viro 
78d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly;
79e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /**
82f05de73bSJean Sacren  *	skb_panic - private function for out-of-line support
831da177e4SLinus Torvalds  *	@skb:	buffer
841da177e4SLinus Torvalds  *	@sz:	size
85f05de73bSJean Sacren  *	@addr:	address
8699d5851eSJames Hogan  *	@msg:	skb_over_panic or skb_under_panic
871da177e4SLinus Torvalds  *
88f05de73bSJean Sacren  *	Out-of-line support for skb_put() and skb_push().
89f05de73bSJean Sacren  *	Called via the wrapper skb_over_panic() or skb_under_panic().
90f05de73bSJean Sacren  *	Keep out of line to prevent kernel bloat.
91f05de73bSJean Sacren  *	__builtin_return_address is not used because it is not always reliable.
921da177e4SLinus Torvalds  */
93f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
9499d5851eSJames Hogan 		      const char msg[])
951da177e4SLinus Torvalds {
96e005d193SJoe Perches 	pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
9799d5851eSJames Hogan 		 msg, addr, skb->len, sz, skb->head, skb->data,
984305b541SArnaldo Carvalho de Melo 		 (unsigned long)skb->tail, (unsigned long)skb->end,
9926095455SPatrick McHardy 		 skb->dev ? skb->dev->name : "<NULL>");
1001da177e4SLinus Torvalds 	BUG();
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds 
103f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1041da177e4SLinus Torvalds {
105f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
109f05de73bSJean Sacren {
110f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
111f05de73bSJean Sacren }
112c93bdd0eSMel Gorman 
113c93bdd0eSMel Gorman /*
114c93bdd0eSMel Gorman  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
115c93bdd0eSMel Gorman  * the caller if emergency pfmemalloc reserves are being used. If it is and
116c93bdd0eSMel Gorman  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
117c93bdd0eSMel Gorman  * may be used. Otherwise, the packet data may be discarded until enough
118c93bdd0eSMel Gorman  * memory is free
119c93bdd0eSMel Gorman  */
120c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
121c93bdd0eSMel Gorman 	 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
12261c5e88aSstephen hemminger 
12361c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
12461c5e88aSstephen hemminger 			       unsigned long ip, bool *pfmemalloc)
125c93bdd0eSMel Gorman {
126c93bdd0eSMel Gorman 	void *obj;
127c93bdd0eSMel Gorman 	bool ret_pfmemalloc = false;
128c93bdd0eSMel Gorman 
129c93bdd0eSMel Gorman 	/*
130c93bdd0eSMel Gorman 	 * Try a regular allocation, when that fails and we're not entitled
131c93bdd0eSMel Gorman 	 * to the reserves, fail.
132c93bdd0eSMel Gorman 	 */
133c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size,
134c93bdd0eSMel Gorman 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
135c93bdd0eSMel Gorman 					node);
136c93bdd0eSMel Gorman 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
137c93bdd0eSMel Gorman 		goto out;
138c93bdd0eSMel Gorman 
139c93bdd0eSMel Gorman 	/* Try again but now we are using pfmemalloc reserves */
140c93bdd0eSMel Gorman 	ret_pfmemalloc = true;
141c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size, flags, node);
142c93bdd0eSMel Gorman 
143c93bdd0eSMel Gorman out:
144c93bdd0eSMel Gorman 	if (pfmemalloc)
145c93bdd0eSMel Gorman 		*pfmemalloc = ret_pfmemalloc;
146c93bdd0eSMel Gorman 
147c93bdd0eSMel Gorman 	return obj;
148c93bdd0eSMel Gorman }
149c93bdd0eSMel Gorman 
1501da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1511da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1521da177e4SLinus Torvalds  *	[BEEP] leaks.
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  */
1551da177e4SLinus Torvalds 
1560ebd0ac5SPatrick McHardy struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
1570ebd0ac5SPatrick McHardy {
1580ebd0ac5SPatrick McHardy 	struct sk_buff *skb;
1590ebd0ac5SPatrick McHardy 
1600ebd0ac5SPatrick McHardy 	/* Get the HEAD */
1610ebd0ac5SPatrick McHardy 	skb = kmem_cache_alloc_node(skbuff_head_cache,
1620ebd0ac5SPatrick McHardy 				    gfp_mask & ~__GFP_DMA, node);
1630ebd0ac5SPatrick McHardy 	if (!skb)
1640ebd0ac5SPatrick McHardy 		goto out;
1650ebd0ac5SPatrick McHardy 
1660ebd0ac5SPatrick McHardy 	/*
1670ebd0ac5SPatrick McHardy 	 * Only clear those fields we need to clear, not those that we will
1680ebd0ac5SPatrick McHardy 	 * actually initialise below. Hence, don't put any more fields after
1690ebd0ac5SPatrick McHardy 	 * the tail pointer in struct sk_buff!
1700ebd0ac5SPatrick McHardy 	 */
1710ebd0ac5SPatrick McHardy 	memset(skb, 0, offsetof(struct sk_buff, tail));
1725e71d9d7SPablo Neira 	skb->head = NULL;
1730ebd0ac5SPatrick McHardy 	skb->truesize = sizeof(struct sk_buff);
1740ebd0ac5SPatrick McHardy 	atomic_set(&skb->users, 1);
1750ebd0ac5SPatrick McHardy 
17635d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
1770ebd0ac5SPatrick McHardy out:
1780ebd0ac5SPatrick McHardy 	return skb;
1790ebd0ac5SPatrick McHardy }
1800ebd0ac5SPatrick McHardy 
1811da177e4SLinus Torvalds /**
182d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1831da177e4SLinus Torvalds  *	@size: size to allocate
1841da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
185c93bdd0eSMel Gorman  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
186c93bdd0eSMel Gorman  *		instead of head cache and allocate a cloned (child) skb.
187c93bdd0eSMel Gorman  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
188c93bdd0eSMel Gorman  *		allocations in case the data is required for writeback
189b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1901da177e4SLinus Torvalds  *
1911da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
19294b6042cSBen Hutchings  *	tail room of at least size bytes. The object has a reference count
19394b6042cSBen Hutchings  *	of one. The return is the buffer. On a failure the return is %NULL.
1941da177e4SLinus Torvalds  *
1951da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1961da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1971da177e4SLinus Torvalds  */
198dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
199c93bdd0eSMel Gorman 			    int flags, int node)
2001da177e4SLinus Torvalds {
201e18b890bSChristoph Lameter 	struct kmem_cache *cache;
2024947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
2031da177e4SLinus Torvalds 	struct sk_buff *skb;
2041da177e4SLinus Torvalds 	u8 *data;
205c93bdd0eSMel Gorman 	bool pfmemalloc;
2061da177e4SLinus Torvalds 
207c93bdd0eSMel Gorman 	cache = (flags & SKB_ALLOC_FCLONE)
208c93bdd0eSMel Gorman 		? skbuff_fclone_cache : skbuff_head_cache;
209c93bdd0eSMel Gorman 
210c93bdd0eSMel Gorman 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
211c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
2128798b3fbSHerbert Xu 
2131da177e4SLinus Torvalds 	/* Get the HEAD */
214b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
2151da177e4SLinus Torvalds 	if (!skb)
2161da177e4SLinus Torvalds 		goto out;
217ec7d2f2cSEric Dumazet 	prefetchw(skb);
2181da177e4SLinus Torvalds 
21987fb4b7bSEric Dumazet 	/* We do our best to align skb_shared_info on a separate cache
22087fb4b7bSEric Dumazet 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
22187fb4b7bSEric Dumazet 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
22287fb4b7bSEric Dumazet 	 * Both skb->head and skb_shared_info are cache line aligned.
22387fb4b7bSEric Dumazet 	 */
224bc417e30STony Lindgren 	size = SKB_DATA_ALIGN(size);
22587fb4b7bSEric Dumazet 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
226c93bdd0eSMel Gorman 	data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
2271da177e4SLinus Torvalds 	if (!data)
2281da177e4SLinus Torvalds 		goto nodata;
22987fb4b7bSEric Dumazet 	/* kmalloc(size) might give us more room than requested.
23087fb4b7bSEric Dumazet 	 * Put skb_shared_info exactly at the end of allocated zone,
23187fb4b7bSEric Dumazet 	 * to allow max possible filling before reallocation.
23287fb4b7bSEric Dumazet 	 */
23387fb4b7bSEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
234ec7d2f2cSEric Dumazet 	prefetchw(data + size);
2351da177e4SLinus Torvalds 
236ca0605a7SArnaldo Carvalho de Melo 	/*
237c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
238c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
239c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
240ca0605a7SArnaldo Carvalho de Melo 	 */
241ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
24287fb4b7bSEric Dumazet 	/* Account for allocated memory : skb + skb->head */
24387fb4b7bSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
244c93bdd0eSMel Gorman 	skb->pfmemalloc = pfmemalloc;
2451da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2461da177e4SLinus Torvalds 	skb->head = data;
2471da177e4SLinus Torvalds 	skb->data = data;
24827a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2494305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
25035d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
25135d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
25219633e12SStephen Hemminger 
2534947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2544947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
255ec7d2f2cSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
2564947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
257c2aa3665SEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
2584947d3efSBenjamin LaHaise 
259c93bdd0eSMel Gorman 	if (flags & SKB_ALLOC_FCLONE) {
260d0bf4a9eSEric Dumazet 		struct sk_buff_fclones *fclones;
2611da177e4SLinus Torvalds 
262d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
263d0bf4a9eSEric Dumazet 
264d0bf4a9eSEric Dumazet 		kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
265d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
266d0bf4a9eSEric Dumazet 		atomic_set(&fclones->fclone_ref, 1);
267d179cd12SDavid S. Miller 
268c8753d55SVijay Subramanian 		fclones->skb2.fclone = SKB_FCLONE_FREE;
269d0bf4a9eSEric Dumazet 		fclones->skb2.pfmemalloc = pfmemalloc;
270d179cd12SDavid S. Miller 	}
2711da177e4SLinus Torvalds out:
2721da177e4SLinus Torvalds 	return skb;
2731da177e4SLinus Torvalds nodata:
2748798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2751da177e4SLinus Torvalds 	skb = NULL;
2761da177e4SLinus Torvalds 	goto out;
2771da177e4SLinus Torvalds }
278b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds /**
281b2b5ce9dSEric Dumazet  * build_skb - build a network buffer
282b2b5ce9dSEric Dumazet  * @data: data buffer provided by caller
283d3836f21SEric Dumazet  * @frag_size: size of fragment, or 0 if head was kmalloced
284b2b5ce9dSEric Dumazet  *
285b2b5ce9dSEric Dumazet  * Allocate a new &sk_buff. Caller provides space holding head and
286deceb4c0SFlorian Fainelli  * skb_shared_info. @data must have been allocated by kmalloc() only if
287deceb4c0SFlorian Fainelli  * @frag_size is 0, otherwise data should come from the page allocator.
288b2b5ce9dSEric Dumazet  * The return is the new skb buffer.
289b2b5ce9dSEric Dumazet  * On a failure the return is %NULL, and @data is not freed.
290b2b5ce9dSEric Dumazet  * Notes :
291b2b5ce9dSEric Dumazet  *  Before IO, driver allocates only data buffer where NIC put incoming frame
292b2b5ce9dSEric Dumazet  *  Driver should add room at head (NET_SKB_PAD) and
293b2b5ce9dSEric Dumazet  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
294b2b5ce9dSEric Dumazet  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
295b2b5ce9dSEric Dumazet  *  before giving packet to stack.
296b2b5ce9dSEric Dumazet  *  RX rings only contains data buffers, not full skbs.
297b2b5ce9dSEric Dumazet  */
298d3836f21SEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
299b2b5ce9dSEric Dumazet {
300b2b5ce9dSEric Dumazet 	struct skb_shared_info *shinfo;
301b2b5ce9dSEric Dumazet 	struct sk_buff *skb;
302d3836f21SEric Dumazet 	unsigned int size = frag_size ? : ksize(data);
303b2b5ce9dSEric Dumazet 
304b2b5ce9dSEric Dumazet 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
305b2b5ce9dSEric Dumazet 	if (!skb)
306b2b5ce9dSEric Dumazet 		return NULL;
307b2b5ce9dSEric Dumazet 
308d3836f21SEric Dumazet 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
309b2b5ce9dSEric Dumazet 
310b2b5ce9dSEric Dumazet 	memset(skb, 0, offsetof(struct sk_buff, tail));
311b2b5ce9dSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
312d3836f21SEric Dumazet 	skb->head_frag = frag_size != 0;
313b2b5ce9dSEric Dumazet 	atomic_set(&skb->users, 1);
314b2b5ce9dSEric Dumazet 	skb->head = data;
315b2b5ce9dSEric Dumazet 	skb->data = data;
316b2b5ce9dSEric Dumazet 	skb_reset_tail_pointer(skb);
317b2b5ce9dSEric Dumazet 	skb->end = skb->tail + size;
31835d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
31935d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
320b2b5ce9dSEric Dumazet 
321b2b5ce9dSEric Dumazet 	/* make sure we initialize shinfo sequentially */
322b2b5ce9dSEric Dumazet 	shinfo = skb_shinfo(skb);
323b2b5ce9dSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
324b2b5ce9dSEric Dumazet 	atomic_set(&shinfo->dataref, 1);
325b2b5ce9dSEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
326b2b5ce9dSEric Dumazet 
327b2b5ce9dSEric Dumazet 	return skb;
328b2b5ce9dSEric Dumazet }
329b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
330b2b5ce9dSEric Dumazet 
331a1c7fff7SEric Dumazet struct netdev_alloc_cache {
33269b08f62SEric Dumazet 	struct page_frag	frag;
33369b08f62SEric Dumazet 	/* we maintain a pagecount bias, so that we dont dirty cache line
33469b08f62SEric Dumazet 	 * containing page->_count every time we allocate a fragment.
33569b08f62SEric Dumazet 	 */
336540eb7bfSAlexander Duyck 	unsigned int		pagecnt_bias;
337a1c7fff7SEric Dumazet };
338a1c7fff7SEric Dumazet static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
339a1c7fff7SEric Dumazet 
340c93bdd0eSMel Gorman static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
3416f532612SEric Dumazet {
3426f532612SEric Dumazet 	struct netdev_alloc_cache *nc;
3436f532612SEric Dumazet 	void *data = NULL;
34469b08f62SEric Dumazet 	int order;
3456f532612SEric Dumazet 	unsigned long flags;
3466f532612SEric Dumazet 
3476f532612SEric Dumazet 	local_irq_save(flags);
3486f532612SEric Dumazet 	nc = &__get_cpu_var(netdev_alloc_cache);
34969b08f62SEric Dumazet 	if (unlikely(!nc->frag.page)) {
3506f532612SEric Dumazet refill:
35169b08f62SEric Dumazet 		for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) {
35269b08f62SEric Dumazet 			gfp_t gfp = gfp_mask;
35369b08f62SEric Dumazet 
35469b08f62SEric Dumazet 			if (order)
35569b08f62SEric Dumazet 				gfp |= __GFP_COMP | __GFP_NOWARN;
35669b08f62SEric Dumazet 			nc->frag.page = alloc_pages(gfp, order);
35769b08f62SEric Dumazet 			if (likely(nc->frag.page))
35869b08f62SEric Dumazet 				break;
35969b08f62SEric Dumazet 			if (--order < 0)
360540eb7bfSAlexander Duyck 				goto end;
36169b08f62SEric Dumazet 		}
36269b08f62SEric Dumazet 		nc->frag.size = PAGE_SIZE << order;
363540eb7bfSAlexander Duyck recycle:
36469b08f62SEric Dumazet 		atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS);
36569b08f62SEric Dumazet 		nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS;
36669b08f62SEric Dumazet 		nc->frag.offset = 0;
3676f532612SEric Dumazet 	}
368540eb7bfSAlexander Duyck 
36969b08f62SEric Dumazet 	if (nc->frag.offset + fragsz > nc->frag.size) {
370540eb7bfSAlexander Duyck 		/* avoid unnecessary locked operations if possible */
37169b08f62SEric Dumazet 		if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) ||
37269b08f62SEric Dumazet 		    atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count))
373540eb7bfSAlexander Duyck 			goto recycle;
3746f532612SEric Dumazet 		goto refill;
3756f532612SEric Dumazet 	}
376540eb7bfSAlexander Duyck 
37769b08f62SEric Dumazet 	data = page_address(nc->frag.page) + nc->frag.offset;
37869b08f62SEric Dumazet 	nc->frag.offset += fragsz;
379540eb7bfSAlexander Duyck 	nc->pagecnt_bias--;
380540eb7bfSAlexander Duyck end:
3816f532612SEric Dumazet 	local_irq_restore(flags);
3826f532612SEric Dumazet 	return data;
3836f532612SEric Dumazet }
384c93bdd0eSMel Gorman 
385c93bdd0eSMel Gorman /**
386c93bdd0eSMel Gorman  * netdev_alloc_frag - allocate a page fragment
387c93bdd0eSMel Gorman  * @fragsz: fragment size
388c93bdd0eSMel Gorman  *
389c93bdd0eSMel Gorman  * Allocates a frag from a page for receive buffer.
390c93bdd0eSMel Gorman  * Uses GFP_ATOMIC allocations.
391c93bdd0eSMel Gorman  */
392c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz)
393c93bdd0eSMel Gorman {
394c93bdd0eSMel Gorman 	return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
395c93bdd0eSMel Gorman }
3966f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag);
3976f532612SEric Dumazet 
3986f532612SEric Dumazet /**
3998af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
4008af27456SChristoph Hellwig  *	@dev: network device to receive on
4018af27456SChristoph Hellwig  *	@length: length to allocate
4028af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
4038af27456SChristoph Hellwig  *
4048af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
4058af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
4068af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
4078af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
4088af27456SChristoph Hellwig  *
4098af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
4108af27456SChristoph Hellwig  */
4118af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
4128af27456SChristoph Hellwig 				   unsigned int length, gfp_t gfp_mask)
4138af27456SChristoph Hellwig {
4146f532612SEric Dumazet 	struct sk_buff *skb = NULL;
415a1c7fff7SEric Dumazet 	unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
416a1c7fff7SEric Dumazet 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4178af27456SChristoph Hellwig 
418310e158cSEric Dumazet 	if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
419c93bdd0eSMel Gorman 		void *data;
420c93bdd0eSMel Gorman 
421c93bdd0eSMel Gorman 		if (sk_memalloc_socks())
422c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
423c93bdd0eSMel Gorman 
424c93bdd0eSMel Gorman 		data = __netdev_alloc_frag(fragsz, gfp_mask);
425a1c7fff7SEric Dumazet 
4266f532612SEric Dumazet 		if (likely(data)) {
4276f532612SEric Dumazet 			skb = build_skb(data, fragsz);
4286f532612SEric Dumazet 			if (unlikely(!skb))
4296f532612SEric Dumazet 				put_page(virt_to_head_page(data));
430a1c7fff7SEric Dumazet 		}
431a1c7fff7SEric Dumazet 	} else {
432c93bdd0eSMel Gorman 		skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
433c93bdd0eSMel Gorman 				  SKB_ALLOC_RX, NUMA_NO_NODE);
434a1c7fff7SEric Dumazet 	}
4357b2e497aSChristoph Hellwig 	if (likely(skb)) {
4368af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
4377b2e497aSChristoph Hellwig 		skb->dev = dev;
4387b2e497aSChristoph Hellwig 	}
4398af27456SChristoph Hellwig 	return skb;
4408af27456SChristoph Hellwig }
441b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
4421da177e4SLinus Torvalds 
443654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
44450269e19SEric Dumazet 		     int size, unsigned int truesize)
445654bed16SPeter Zijlstra {
446654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
447654bed16SPeter Zijlstra 	skb->len += size;
448654bed16SPeter Zijlstra 	skb->data_len += size;
44950269e19SEric Dumazet 	skb->truesize += truesize;
450654bed16SPeter Zijlstra }
451654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
452654bed16SPeter Zijlstra 
453f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
454f8e617e1SJason Wang 			  unsigned int truesize)
455f8e617e1SJason Wang {
456f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
457f8e617e1SJason Wang 
458f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
459f8e617e1SJason Wang 	skb->len += size;
460f8e617e1SJason Wang 	skb->data_len += size;
461f8e617e1SJason Wang 	skb->truesize += truesize;
462f8e617e1SJason Wang }
463f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
464f8e617e1SJason Wang 
46527b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
4661da177e4SLinus Torvalds {
467bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
46827b437c8SHerbert Xu 	*listp = NULL;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
47127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
47227b437c8SHerbert Xu {
47327b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
47427b437c8SHerbert Xu }
47527b437c8SHerbert Xu 
4761da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct sk_buff *list;
4791da177e4SLinus Torvalds 
480fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
4811da177e4SLinus Torvalds 		skb_get(list);
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
485d3836f21SEric Dumazet {
486d3836f21SEric Dumazet 	if (skb->head_frag)
487d3836f21SEric Dumazet 		put_page(virt_to_head_page(skb->head));
488d3836f21SEric Dumazet 	else
489d3836f21SEric Dumazet 		kfree(skb->head);
490d3836f21SEric Dumazet }
491d3836f21SEric Dumazet 
4925bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
4931da177e4SLinus Torvalds {
494ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
4951da177e4SLinus Torvalds 	int i;
496ff04a771SEric Dumazet 
497ff04a771SEric Dumazet 	if (skb->cloned &&
498ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
499ff04a771SEric Dumazet 			      &shinfo->dataref))
500ff04a771SEric Dumazet 		return;
501ff04a771SEric Dumazet 
502ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
503ff04a771SEric Dumazet 		__skb_frag_unref(&shinfo->frags[i]);
5041da177e4SLinus Torvalds 
505a6686f2fSShirley Ma 	/*
506a6686f2fSShirley Ma 	 * If skb buf is from userspace, we need to notify the caller
507a6686f2fSShirley Ma 	 * the lower device DMA has done;
508a6686f2fSShirley Ma 	 */
509ff04a771SEric Dumazet 	if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
510a6686f2fSShirley Ma 		struct ubuf_info *uarg;
511a6686f2fSShirley Ma 
512ff04a771SEric Dumazet 		uarg = shinfo->destructor_arg;
513a6686f2fSShirley Ma 		if (uarg->callback)
514e19d6763SMichael S. Tsirkin 			uarg->callback(uarg, true);
515a6686f2fSShirley Ma 	}
516a6686f2fSShirley Ma 
517ff04a771SEric Dumazet 	if (shinfo->frag_list)
518ff04a771SEric Dumazet 		kfree_skb_list(shinfo->frag_list);
5191da177e4SLinus Torvalds 
520d3836f21SEric Dumazet 	skb_free_head(skb);
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /*
5241da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
5251da177e4SLinus Torvalds  */
5262d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
5271da177e4SLinus Torvalds {
528d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
529d179cd12SDavid S. Miller 
530d179cd12SDavid S. Miller 	switch (skb->fclone) {
531d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
5321da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
533d179cd12SDavid S. Miller 		break;
534d179cd12SDavid S. Miller 
535d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
536d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
537d0bf4a9eSEric Dumazet 		if (atomic_dec_and_test(&fclones->fclone_ref))
538d0bf4a9eSEric Dumazet 			kmem_cache_free(skbuff_fclone_cache, fclones);
539d179cd12SDavid S. Miller 		break;
540d179cd12SDavid S. Miller 
541d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
542d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
543d179cd12SDavid S. Miller 
544ce1a4ea3SEric Dumazet 		/* Warning : We must perform the atomic_dec_and_test() before
545c8753d55SVijay Subramanian 		 * setting skb->fclone back to SKB_FCLONE_FREE, otherwise
546ce1a4ea3SEric Dumazet 		 * skb_clone() could set clone_ref to 2 before our decrement.
547ce1a4ea3SEric Dumazet 		 * Anyway, if we are going to free the structure, no need to
548ce1a4ea3SEric Dumazet 		 * rewrite skb->fclone.
549ce1a4ea3SEric Dumazet 		 */
550ce1a4ea3SEric Dumazet 		if (atomic_dec_and_test(&fclones->fclone_ref)) {
551ce1a4ea3SEric Dumazet 			kmem_cache_free(skbuff_fclone_cache, fclones);
552ce1a4ea3SEric Dumazet 		} else {
553d179cd12SDavid S. Miller 			/* The clone portion is available for
554d179cd12SDavid S. Miller 			 * fast-cloning again.
555d179cd12SDavid S. Miller 			 */
556c8753d55SVijay Subramanian 			skb->fclone = SKB_FCLONE_FREE;
557ce1a4ea3SEric Dumazet 		}
558d179cd12SDavid S. Miller 		break;
5593ff50b79SStephen Hemminger 	}
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
56204a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
5631da177e4SLinus Torvalds {
564adf30907SEric Dumazet 	skb_dst_drop(skb);
5651da177e4SLinus Torvalds #ifdef CONFIG_XFRM
5661da177e4SLinus Torvalds 	secpath_put(skb->sp);
5671da177e4SLinus Torvalds #endif
5681da177e4SLinus Torvalds 	if (skb->destructor) {
5699c2b3328SStephen Hemminger 		WARN_ON(in_irq());
5701da177e4SLinus Torvalds 		skb->destructor(skb);
5711da177e4SLinus Torvalds 	}
572a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
5735f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
5742fc72c7bSKOVACS Krisztian #endif
5751109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
5761da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
5771da177e4SLinus Torvalds #endif
5781da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
5791da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
5801da177e4SLinus Torvalds 	skb->tc_index = 0;
5811da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
5821da177e4SLinus Torvalds 	skb->tc_verd = 0;
5831da177e4SLinus Torvalds #endif
5841da177e4SLinus Torvalds #endif
58504a4bb55SLennert Buytenhek }
58604a4bb55SLennert Buytenhek 
58704a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
58804a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
58904a4bb55SLennert Buytenhek {
59004a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
5915e71d9d7SPablo Neira 	if (likely(skb->head))
5922d4baff8SHerbert Xu 		skb_release_data(skb);
5932d4baff8SHerbert Xu }
5941da177e4SLinus Torvalds 
5952d4baff8SHerbert Xu /**
5962d4baff8SHerbert Xu  *	__kfree_skb - private function
5972d4baff8SHerbert Xu  *	@skb: buffer
5982d4baff8SHerbert Xu  *
5992d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
6002d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
6012d4baff8SHerbert Xu  *	always call kfree_skb
6022d4baff8SHerbert Xu  */
6032d4baff8SHerbert Xu 
6042d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
6052d4baff8SHerbert Xu {
6062d4baff8SHerbert Xu 	skb_release_all(skb);
6071da177e4SLinus Torvalds 	kfree_skbmem(skb);
6081da177e4SLinus Torvalds }
609b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds /**
612231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
613231d06aeSJörn Engel  *	@skb: buffer to free
614231d06aeSJörn Engel  *
615231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
616231d06aeSJörn Engel  *	hit zero.
617231d06aeSJörn Engel  */
618231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
619231d06aeSJörn Engel {
620231d06aeSJörn Engel 	if (unlikely(!skb))
621231d06aeSJörn Engel 		return;
622231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
623231d06aeSJörn Engel 		smp_rmb();
624231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
625231d06aeSJörn Engel 		return;
626ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
627231d06aeSJörn Engel 	__kfree_skb(skb);
628231d06aeSJörn Engel }
629b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
630231d06aeSJörn Engel 
631bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
632bd8a7036SEric Dumazet {
633bd8a7036SEric Dumazet 	while (segs) {
634bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
635bd8a7036SEric Dumazet 
636bd8a7036SEric Dumazet 		kfree_skb(segs);
637bd8a7036SEric Dumazet 		segs = next;
638bd8a7036SEric Dumazet 	}
639bd8a7036SEric Dumazet }
640bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
641bd8a7036SEric Dumazet 
642d1a203eaSStephen Hemminger /**
64325121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
64425121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
64525121173SMichael S. Tsirkin  *
64625121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
64725121173SMichael S. Tsirkin  *	skb must be freed afterwards.
64825121173SMichael S. Tsirkin  */
64925121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
65025121173SMichael S. Tsirkin {
65125121173SMichael S. Tsirkin 	if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
65225121173SMichael S. Tsirkin 		struct ubuf_info *uarg;
65325121173SMichael S. Tsirkin 
65425121173SMichael S. Tsirkin 		uarg = skb_shinfo(skb)->destructor_arg;
65525121173SMichael S. Tsirkin 		if (uarg->callback)
65625121173SMichael S. Tsirkin 			uarg->callback(uarg, false);
65725121173SMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
65825121173SMichael S. Tsirkin 	}
65925121173SMichael S. Tsirkin }
66025121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
66125121173SMichael S. Tsirkin 
66225121173SMichael S. Tsirkin /**
663ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
664ead2ceb0SNeil Horman  *	@skb: buffer to free
665ead2ceb0SNeil Horman  *
666ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
667ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
668ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
669ead2ceb0SNeil Horman  */
670ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
671ead2ceb0SNeil Horman {
672ead2ceb0SNeil Horman 	if (unlikely(!skb))
673ead2ceb0SNeil Horman 		return;
674ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
675ead2ceb0SNeil Horman 		smp_rmb();
676ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
677ead2ceb0SNeil Horman 		return;
67807dc22e7SKoki Sanagi 	trace_consume_skb(skb);
679ead2ceb0SNeil Horman 	__kfree_skb(skb);
680ead2ceb0SNeil Horman }
681ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
682ead2ceb0SNeil Horman 
683b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */
684b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
685b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
686b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_start));	\
687b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
688b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_end));	\
689b1937227SEric Dumazet 
690dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
691dec18810SHerbert Xu {
692dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
693b1937227SEric Dumazet 	/* We do not copy old->sk */
694dec18810SHerbert Xu 	new->dev		= old->dev;
695b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
6967fee226aSEric Dumazet 	skb_dst_copy(new, old);
697def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
698dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
699dec18810SHerbert Xu #endif
700b1937227SEric Dumazet 	__nf_copy(new, old, false);
7016aa895b0SPatrick McHardy 
702b1937227SEric Dumazet 	/* Note : this field could be in headers_start/headers_end section
703b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
704b1937227SEric Dumazet 	 */
705b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
70606021292SEliezer Tamir 
707b1937227SEric Dumazet 	memcpy(&new->headers_start, &old->headers_start,
708b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_end) -
709b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_start));
710b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
711b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
712b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
713b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
714b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
715b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
716b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
717b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
718b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
719b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
720b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
721b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
722b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
723b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
724b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
725b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
726b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
727b1937227SEric Dumazet #endif
728e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
729b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
73006021292SEliezer Tamir #endif
731b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
732b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
733b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT
734b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_verd);
735b1937227SEric Dumazet #endif
736b1937227SEric Dumazet #endif
737b1937227SEric Dumazet 
738dec18810SHerbert Xu }
739dec18810SHerbert Xu 
74082c49a35SHerbert Xu /*
74182c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
74282c49a35SHerbert Xu  * __copy_skb_header above instead.
74382c49a35SHerbert Xu  */
744e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds #define C(x) n->x = skb->x
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	n->next = n->prev = NULL;
7491da177e4SLinus Torvalds 	n->sk = NULL;
750dec18810SHerbert Xu 	__copy_skb_header(n, skb);
751dec18810SHerbert Xu 
7521da177e4SLinus Torvalds 	C(len);
7531da177e4SLinus Torvalds 	C(data_len);
7543e6b3b2eSAlexey Dobriyan 	C(mac_len);
755334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
75602f1c89dSPaul Moore 	n->cloned = 1;
7571da177e4SLinus Torvalds 	n->nohdr = 0;
7581da177e4SLinus Torvalds 	n->destructor = NULL;
7591da177e4SLinus Torvalds 	C(tail);
7601da177e4SLinus Torvalds 	C(end);
76102f1c89dSPaul Moore 	C(head);
762d3836f21SEric Dumazet 	C(head_frag);
76302f1c89dSPaul Moore 	C(data);
76402f1c89dSPaul Moore 	C(truesize);
76502f1c89dSPaul Moore 	atomic_set(&n->users, 1);
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
7681da177e4SLinus Torvalds 	skb->cloned = 1;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	return n;
771e0053ec0SHerbert Xu #undef C
772e0053ec0SHerbert Xu }
773e0053ec0SHerbert Xu 
774e0053ec0SHerbert Xu /**
775e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
776e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
777e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
778e0053ec0SHerbert Xu  *
779e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
780e0053ec0SHerbert Xu  *	supplied by the user.
781e0053ec0SHerbert Xu  *
782e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
783e0053ec0SHerbert Xu  */
784e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
785e0053ec0SHerbert Xu {
7862d4baff8SHerbert Xu 	skb_release_all(dst);
787e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
788e0053ec0SHerbert Xu }
789e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
790e0053ec0SHerbert Xu 
7912c53040fSBen Hutchings /**
7922c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
79348c83012SMichael S. Tsirkin  *	@skb: the skb to modify
79448c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
79548c83012SMichael S. Tsirkin  *
79648c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
79748c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
79848c83012SMichael S. Tsirkin  *	to userspace pages.
79948c83012SMichael S. Tsirkin  *
80048c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
80148c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
80248c83012SMichael S. Tsirkin  *
80348c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
80448c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
80548c83012SMichael S. Tsirkin  */
80648c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
807a6686f2fSShirley Ma {
808a6686f2fSShirley Ma 	int i;
809a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
810a6686f2fSShirley Ma 	struct page *page, *head = NULL;
811a6686f2fSShirley Ma 	struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
812a6686f2fSShirley Ma 
813a6686f2fSShirley Ma 	for (i = 0; i < num_frags; i++) {
814a6686f2fSShirley Ma 		u8 *vaddr;
815a6686f2fSShirley Ma 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
816a6686f2fSShirley Ma 
81702756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
818a6686f2fSShirley Ma 		if (!page) {
819a6686f2fSShirley Ma 			while (head) {
82040dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
821a6686f2fSShirley Ma 				put_page(head);
822a6686f2fSShirley Ma 				head = next;
823a6686f2fSShirley Ma 			}
824a6686f2fSShirley Ma 			return -ENOMEM;
825a6686f2fSShirley Ma 		}
82651c56b00SEric Dumazet 		vaddr = kmap_atomic(skb_frag_page(f));
827a6686f2fSShirley Ma 		memcpy(page_address(page),
8289e903e08SEric Dumazet 		       vaddr + f->page_offset, skb_frag_size(f));
82951c56b00SEric Dumazet 		kunmap_atomic(vaddr);
83040dadff2SSunghan Suh 		set_page_private(page, (unsigned long)head);
831a6686f2fSShirley Ma 		head = page;
832a6686f2fSShirley Ma 	}
833a6686f2fSShirley Ma 
834a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
83502756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
836a8605c60SIan Campbell 		skb_frag_unref(skb, i);
837a6686f2fSShirley Ma 
838e19d6763SMichael S. Tsirkin 	uarg->callback(uarg, false);
839a6686f2fSShirley Ma 
840a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
84102756ed4SKrishna Kumar 	for (i = num_frags - 1; i >= 0; i--) {
84202756ed4SKrishna Kumar 		__skb_fill_page_desc(skb, i, head, 0,
84302756ed4SKrishna Kumar 				     skb_shinfo(skb)->frags[i].size);
84440dadff2SSunghan Suh 		head = (struct page *)page_private(head);
845a6686f2fSShirley Ma 	}
84648c83012SMichael S. Tsirkin 
84748c83012SMichael S. Tsirkin 	skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
848a6686f2fSShirley Ma 	return 0;
849a6686f2fSShirley Ma }
850dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
851a6686f2fSShirley Ma 
852e0053ec0SHerbert Xu /**
853e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
854e0053ec0SHerbert Xu  *	@skb: buffer to clone
855e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
856e0053ec0SHerbert Xu  *
857e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
858e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
859e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
860e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
861e0053ec0SHerbert Xu  *
862e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
863e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
864e0053ec0SHerbert Xu  */
865e0053ec0SHerbert Xu 
866e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
867e0053ec0SHerbert Xu {
868d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
869d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
870d0bf4a9eSEric Dumazet 						       skb1);
871d0bf4a9eSEric Dumazet 	struct sk_buff *n = &fclones->skb2;
872e0053ec0SHerbert Xu 
87370008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
874a6686f2fSShirley Ma 		return NULL;
875a6686f2fSShirley Ma 
876e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
877c8753d55SVijay Subramanian 	    n->fclone == SKB_FCLONE_FREE) {
878e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
879ce1a4ea3SEric Dumazet 		/* As our fastclone was free, clone_ref must be 1 at this point.
880ce1a4ea3SEric Dumazet 		 * We could use atomic_inc() here, but it is faster
881ce1a4ea3SEric Dumazet 		 * to set the final value.
882ce1a4ea3SEric Dumazet 		 */
883ce1a4ea3SEric Dumazet 		atomic_set(&fclones->fclone_ref, 2);
884e0053ec0SHerbert Xu 	} else {
885c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
886c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
887c93bdd0eSMel Gorman 
888e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
889e0053ec0SHerbert Xu 		if (!n)
890e0053ec0SHerbert Xu 			return NULL;
891fe55f6d5SVegard Nossum 
892fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags1);
893e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
894e0053ec0SHerbert Xu 	}
895e0053ec0SHerbert Xu 
896e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
8971da177e4SLinus Torvalds }
898b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
8991da177e4SLinus Torvalds 
900f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off)
901f5b17294SPravin B Shelar {
902030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
903030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
904030737bcSEric Dumazet 		skb->csum_start += off;
905f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
906f5b17294SPravin B Shelar 	skb->transport_header += off;
907f5b17294SPravin B Shelar 	skb->network_header   += off;
908f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
909f5b17294SPravin B Shelar 		skb->mac_header += off;
910f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
911f5b17294SPravin B Shelar 	skb->inner_network_header += off;
912aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
913f5b17294SPravin B Shelar }
914f5b17294SPravin B Shelar 
9151da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
9161da177e4SLinus Torvalds {
917dec18810SHerbert Xu 	__copy_skb_header(new, old);
918dec18810SHerbert Xu 
9197967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
9207967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
9217967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
924c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
925c93bdd0eSMel Gorman {
926c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
927c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
928c93bdd0eSMel Gorman 	return 0;
929c93bdd0eSMel Gorman }
930c93bdd0eSMel Gorman 
9311da177e4SLinus Torvalds /**
9321da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
9331da177e4SLinus Torvalds  *	@skb: buffer to copy
9341da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
9351da177e4SLinus Torvalds  *
9361da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
9371da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
9381da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
9391da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
9401da177e4SLinus Torvalds  *
9411da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
9421da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
9431da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
9441da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
9451da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
9461da177e4SLinus Torvalds  */
9471da177e4SLinus Torvalds 
948dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
9491da177e4SLinus Torvalds {
9506602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
951ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
952c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
953c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
9546602cebbSEric Dumazet 
9551da177e4SLinus Torvalds 	if (!n)
9561da177e4SLinus Torvalds 		return NULL;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	/* Set the data pointer */
9591da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
9601da177e4SLinus Torvalds 	/* Set the tail pointer and length */
9611da177e4SLinus Torvalds 	skb_put(n, skb->len);
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
9641da177e4SLinus Torvalds 		BUG();
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	copy_skb_header(n, skb);
9671da177e4SLinus Torvalds 	return n;
9681da177e4SLinus Torvalds }
969b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds /**
972bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
9731da177e4SLinus Torvalds  *	@skb: buffer to copy
974117632e6SEric Dumazet  *	@headroom: headroom of new skb
9751da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
976bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
977bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
978bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
9791da177e4SLinus Torvalds  *
9801da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
9811da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
9821da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
9831da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
9841da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
9851da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
9861da177e4SLinus Torvalds  */
9871da177e4SLinus Torvalds 
988bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
989bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
9901da177e4SLinus Torvalds {
991117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
992bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
993bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
9946602cebbSEric Dumazet 
9951da177e4SLinus Torvalds 	if (!n)
9961da177e4SLinus Torvalds 		goto out;
9971da177e4SLinus Torvalds 
9981da177e4SLinus Torvalds 	/* Set the data pointer */
999117632e6SEric Dumazet 	skb_reserve(n, headroom);
10001da177e4SLinus Torvalds 	/* Set the tail pointer and length */
10011da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
10021da177e4SLinus Torvalds 	/* Copy the bytes */
1003d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
10041da177e4SLinus Torvalds 
100525f484a6SHerbert Xu 	n->truesize += skb->data_len;
10061da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
10071da177e4SLinus Torvalds 	n->len	     = skb->len;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
10101da177e4SLinus Torvalds 		int i;
10111da177e4SLinus Torvalds 
101270008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask)) {
10131511022cSDan Carpenter 			kfree_skb(n);
10141511022cSDan Carpenter 			n = NULL;
1015a6686f2fSShirley Ma 			goto out;
1016a6686f2fSShirley Ma 		}
10171da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
10181da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1019ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
10201da177e4SLinus Torvalds 		}
10211da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds 
102421dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
10251da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
10261da177e4SLinus Torvalds 		skb_clone_fraglist(n);
10271da177e4SLinus Torvalds 	}
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	copy_skb_header(n, skb);
10301da177e4SLinus Torvalds out:
10311da177e4SLinus Torvalds 	return n;
10321da177e4SLinus Torvalds }
1033bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds /**
10361da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
10371da177e4SLinus Torvalds  *	@skb: buffer to reallocate
10381da177e4SLinus Torvalds  *	@nhead: room to add at head
10391da177e4SLinus Torvalds  *	@ntail: room to add at tail
10401da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
10411da177e4SLinus Torvalds  *
1042bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1043bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
10441da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
10451da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
10461da177e4SLinus Torvalds  *
10471da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
10481da177e4SLinus Torvalds  *	reloaded after call to this function.
10491da177e4SLinus Torvalds  */
10501da177e4SLinus Torvalds 
105186a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1052dd0fc66fSAl Viro 		     gfp_t gfp_mask)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	int i;
10551da177e4SLinus Torvalds 	u8 *data;
1056ec47ea82SAlexander Duyck 	int size = nhead + skb_end_offset(skb) + ntail;
10571da177e4SLinus Torvalds 	long off;
10581da177e4SLinus Torvalds 
10594edd87adSHerbert Xu 	BUG_ON(nhead < 0);
10604edd87adSHerbert Xu 
10611da177e4SLinus Torvalds 	if (skb_shared(skb))
10621da177e4SLinus Torvalds 		BUG();
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
10651da177e4SLinus Torvalds 
1066c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1067c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1068c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1069c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
10701da177e4SLinus Torvalds 	if (!data)
10711da177e4SLinus Torvalds 		goto nodata;
107287151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
10756602cebbSEric Dumazet 	 * optimized for the cases when header is void.
10766602cebbSEric Dumazet 	 */
10776602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
10786602cebbSEric Dumazet 
10796602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
10806602cebbSEric Dumazet 	       skb_shinfo(skb),
1081fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
10821da177e4SLinus Torvalds 
10833e24591aSAlexander Duyck 	/*
10843e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
10853e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
10863e24591aSAlexander Duyck 	 * be since all we did is relocate the values
10873e24591aSAlexander Duyck 	 */
10883e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
1089a6686f2fSShirley Ma 		/* copy this zero copy skb frags */
109070008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1091a6686f2fSShirley Ma 			goto nofrags;
10921da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1093ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
10941da177e4SLinus Torvalds 
109521dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
10961da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 		skb_release_data(skb);
10993e24591aSAlexander Duyck 	} else {
11003e24591aSAlexander Duyck 		skb_free_head(skb);
11011fd63041SEric Dumazet 	}
11021da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	skb->head     = data;
1105d3836f21SEric Dumazet 	skb->head_frag = 0;
11061da177e4SLinus Torvalds 	skb->data    += off;
11074305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
11084305b541SArnaldo Carvalho de Melo 	skb->end      = size;
110956eb8882SPatrick McHardy 	off           = nhead;
11104305b541SArnaldo Carvalho de Melo #else
11114305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
111256eb8882SPatrick McHardy #endif
111327a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1114b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
11151da177e4SLinus Torvalds 	skb->cloned   = 0;
1116334a8132SPatrick McHardy 	skb->hdr_len  = 0;
11171da177e4SLinus Torvalds 	skb->nohdr    = 0;
11181da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
11191da177e4SLinus Torvalds 	return 0;
11201da177e4SLinus Torvalds 
1121a6686f2fSShirley Ma nofrags:
1122a6686f2fSShirley Ma 	kfree(data);
11231da177e4SLinus Torvalds nodata:
11241da177e4SLinus Torvalds 	return -ENOMEM;
11251da177e4SLinus Torvalds }
1126b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
11311da177e4SLinus Torvalds {
11321da177e4SLinus Torvalds 	struct sk_buff *skb2;
11331da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds 	if (delta <= 0)
11361da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
11371da177e4SLinus Torvalds 	else {
11381da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
11391da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
11401da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
11411da177e4SLinus Torvalds 			kfree_skb(skb2);
11421da177e4SLinus Torvalds 			skb2 = NULL;
11431da177e4SLinus Torvalds 		}
11441da177e4SLinus Torvalds 	}
11451da177e4SLinus Torvalds 	return skb2;
11461da177e4SLinus Torvalds }
1147b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds /**
11501da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
11511da177e4SLinus Torvalds  *	@skb: buffer to copy
11521da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
11531da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
11541da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
11551da177e4SLinus Torvalds  *
11561da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
11571da177e4SLinus Torvalds  *	allocate additional space.
11581da177e4SLinus Torvalds  *
11591da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
11601da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
11611da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
11621da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
11631da177e4SLinus Torvalds  *
11641da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
11651da177e4SLinus Torvalds  *	is called from an interrupt.
11661da177e4SLinus Torvalds  */
11671da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
116886a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1169dd0fc66fSAl Viro 				gfp_t gfp_mask)
11701da177e4SLinus Torvalds {
11711da177e4SLinus Torvalds 	/*
11721da177e4SLinus Torvalds 	 *	Allocate the copy buffer
11731da177e4SLinus Torvalds 	 */
1174c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1175c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1176c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1177efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
11781da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	if (!n)
11811da177e4SLinus Torvalds 		return NULL;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	/* Set the tail pointer and length */
11861da177e4SLinus Torvalds 	skb_put(n, skb->len);
11871da177e4SLinus Torvalds 
1188efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
11891da177e4SLinus Torvalds 	head_copy_off = 0;
11901da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
11911da177e4SLinus Torvalds 		head_copy_len = newheadroom;
11921da177e4SLinus Torvalds 	else
11931da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	/* Copy the linear header and data. */
11961da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
11971da177e4SLinus Torvalds 			  skb->len + head_copy_len))
11981da177e4SLinus Torvalds 		BUG();
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	copy_skb_header(n, skb);
12011da177e4SLinus Torvalds 
1202030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1203efd1e8d5SPatrick McHardy 
12041da177e4SLinus Torvalds 	return n;
12051da177e4SLinus Torvalds }
1206b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds /**
12091da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
12101da177e4SLinus Torvalds  *	@skb: buffer to pad
12111da177e4SLinus Torvalds  *	@pad: space to pad
12121da177e4SLinus Torvalds  *
12131da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
12141da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
12151da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
12161da177e4SLinus Torvalds  *
12175b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
12181da177e4SLinus Torvalds  */
12191da177e4SLinus Torvalds 
12205b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
12211da177e4SLinus Torvalds {
12225b057c6bSHerbert Xu 	int err;
12235b057c6bSHerbert Xu 	int ntail;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
12265b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
12271da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
12285b057c6bSHerbert Xu 		return 0;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 
12314305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
12325b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
12335b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
12345b057c6bSHerbert Xu 		if (unlikely(err))
12355b057c6bSHerbert Xu 			goto free_skb;
12365b057c6bSHerbert Xu 	}
12375b057c6bSHerbert Xu 
12385b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
12395b057c6bSHerbert Xu 	 * to be audited.
12405b057c6bSHerbert Xu 	 */
12415b057c6bSHerbert Xu 	err = skb_linearize(skb);
12425b057c6bSHerbert Xu 	if (unlikely(err))
12435b057c6bSHerbert Xu 		goto free_skb;
12445b057c6bSHerbert Xu 
12455b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
12465b057c6bSHerbert Xu 	return 0;
12475b057c6bSHerbert Xu 
12485b057c6bSHerbert Xu free_skb:
12491da177e4SLinus Torvalds 	kfree_skb(skb);
12505b057c6bSHerbert Xu 	return err;
12511da177e4SLinus Torvalds }
1252b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
12531da177e4SLinus Torvalds 
12540dde3e16SIlpo Järvinen /**
12550c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
12560c7ddf36SMathias Krause  *	@skb: start of the buffer to use
12570c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
12580c7ddf36SMathias Krause  *	@len: amount of data to add
12590c7ddf36SMathias Krause  *
12600c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
12610c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
12620c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
12630c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
12640c7ddf36SMathias Krause  *	returned.
12650c7ddf36SMathias Krause  */
12660c7ddf36SMathias Krause 
12670c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
12680c7ddf36SMathias Krause {
12690c7ddf36SMathias Krause 	if (tail != skb) {
12700c7ddf36SMathias Krause 		skb->data_len += len;
12710c7ddf36SMathias Krause 		skb->len += len;
12720c7ddf36SMathias Krause 	}
12730c7ddf36SMathias Krause 	return skb_put(tail, len);
12740c7ddf36SMathias Krause }
12750c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
12760c7ddf36SMathias Krause 
12770c7ddf36SMathias Krause /**
12780dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
12790dde3e16SIlpo Järvinen  *	@skb: buffer to use
12800dde3e16SIlpo Järvinen  *	@len: amount of data to add
12810dde3e16SIlpo Järvinen  *
12820dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
12830dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
12840dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
12850dde3e16SIlpo Järvinen  */
12860dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
12870dde3e16SIlpo Järvinen {
12880dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
12890dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
12900dde3e16SIlpo Järvinen 	skb->tail += len;
12910dde3e16SIlpo Järvinen 	skb->len  += len;
12920dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
12930dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
12940dde3e16SIlpo Järvinen 	return tmp;
12950dde3e16SIlpo Järvinen }
12960dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
12970dde3e16SIlpo Järvinen 
12986be8ac2fSIlpo Järvinen /**
1299c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1300c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1301c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1302c2aa270aSIlpo Järvinen  *
1303c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1304c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1305c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1306c2aa270aSIlpo Järvinen  */
1307c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1308c2aa270aSIlpo Järvinen {
1309c2aa270aSIlpo Järvinen 	skb->data -= len;
1310c2aa270aSIlpo Järvinen 	skb->len  += len;
1311c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1312c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1313c2aa270aSIlpo Järvinen 	return skb->data;
1314c2aa270aSIlpo Järvinen }
1315c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1316c2aa270aSIlpo Järvinen 
1317c2aa270aSIlpo Järvinen /**
13186be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
13196be8ac2fSIlpo Järvinen  *	@skb: buffer to use
13206be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
13216be8ac2fSIlpo Järvinen  *
13226be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
13236be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
13246be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
13256be8ac2fSIlpo Järvinen  *	the old data.
13266be8ac2fSIlpo Järvinen  */
13276be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
13286be8ac2fSIlpo Järvinen {
132947d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
13306be8ac2fSIlpo Järvinen }
13316be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
13326be8ac2fSIlpo Järvinen 
1333419ae74eSIlpo Järvinen /**
1334419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1335419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1336419ae74eSIlpo Järvinen  *	@len: new length
1337419ae74eSIlpo Järvinen  *
1338419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1339419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1340419ae74eSIlpo Järvinen  *	The skb must be linear.
1341419ae74eSIlpo Järvinen  */
1342419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1343419ae74eSIlpo Järvinen {
1344419ae74eSIlpo Järvinen 	if (skb->len > len)
1345419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1346419ae74eSIlpo Järvinen }
1347419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1348419ae74eSIlpo Järvinen 
13493cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
13501da177e4SLinus Torvalds  */
13511da177e4SLinus Torvalds 
13523cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
13531da177e4SLinus Torvalds {
135427b437c8SHerbert Xu 	struct sk_buff **fragp;
135527b437c8SHerbert Xu 	struct sk_buff *frag;
13561da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
13571da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
13581da177e4SLinus Torvalds 	int i;
135927b437c8SHerbert Xu 	int err;
136027b437c8SHerbert Xu 
136127b437c8SHerbert Xu 	if (skb_cloned(skb) &&
136227b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
136327b437c8SHerbert Xu 		return err;
13641da177e4SLinus Torvalds 
1365f4d26fb3SHerbert Xu 	i = 0;
1366f4d26fb3SHerbert Xu 	if (offset >= len)
1367f4d26fb3SHerbert Xu 		goto drop_pages;
1368f4d26fb3SHerbert Xu 
1369f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
13709e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
137127b437c8SHerbert Xu 
137227b437c8SHerbert Xu 		if (end < len) {
13731da177e4SLinus Torvalds 			offset = end;
137427b437c8SHerbert Xu 			continue;
13751da177e4SLinus Torvalds 		}
13761da177e4SLinus Torvalds 
13779e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
137827b437c8SHerbert Xu 
1379f4d26fb3SHerbert Xu drop_pages:
138027b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
138127b437c8SHerbert Xu 
138227b437c8SHerbert Xu 		for (; i < nfrags; i++)
1383ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
138427b437c8SHerbert Xu 
138521dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
138627b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1387f4d26fb3SHerbert Xu 		goto done;
138827b437c8SHerbert Xu 	}
138927b437c8SHerbert Xu 
139027b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
139127b437c8SHerbert Xu 	     fragp = &frag->next) {
139227b437c8SHerbert Xu 		int end = offset + frag->len;
139327b437c8SHerbert Xu 
139427b437c8SHerbert Xu 		if (skb_shared(frag)) {
139527b437c8SHerbert Xu 			struct sk_buff *nfrag;
139627b437c8SHerbert Xu 
139727b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
139827b437c8SHerbert Xu 			if (unlikely(!nfrag))
139927b437c8SHerbert Xu 				return -ENOMEM;
140027b437c8SHerbert Xu 
140127b437c8SHerbert Xu 			nfrag->next = frag->next;
140285bb2a60SEric Dumazet 			consume_skb(frag);
140327b437c8SHerbert Xu 			frag = nfrag;
140427b437c8SHerbert Xu 			*fragp = frag;
140527b437c8SHerbert Xu 		}
140627b437c8SHerbert Xu 
140727b437c8SHerbert Xu 		if (end < len) {
140827b437c8SHerbert Xu 			offset = end;
140927b437c8SHerbert Xu 			continue;
141027b437c8SHerbert Xu 		}
141127b437c8SHerbert Xu 
141227b437c8SHerbert Xu 		if (end > len &&
141327b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
141427b437c8SHerbert Xu 			return err;
141527b437c8SHerbert Xu 
141627b437c8SHerbert Xu 		if (frag->next)
141727b437c8SHerbert Xu 			skb_drop_list(&frag->next);
141827b437c8SHerbert Xu 		break;
141927b437c8SHerbert Xu 	}
142027b437c8SHerbert Xu 
1421f4d26fb3SHerbert Xu done:
142227b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
14231da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
14241da177e4SLinus Torvalds 		skb->len       = len;
14251da177e4SLinus Torvalds 	} else {
14261da177e4SLinus Torvalds 		skb->len       = len;
14271da177e4SLinus Torvalds 		skb->data_len  = 0;
142827a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	return 0;
14321da177e4SLinus Torvalds }
1433b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds /**
14361da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
14371da177e4SLinus Torvalds  *	@skb: buffer to reallocate
14381da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
14391da177e4SLinus Torvalds  *
14401da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
14411da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
14421da177e4SLinus Torvalds  *	data from fragmented part.
14431da177e4SLinus Torvalds  *
14441da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
14451da177e4SLinus Torvalds  *
14461da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
14471da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
14481da177e4SLinus Torvalds  *
14491da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
14501da177e4SLinus Torvalds  *	reloaded after call to this function.
14511da177e4SLinus Torvalds  */
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
14541da177e4SLinus Torvalds  * when it is necessary.
14551da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
14561da177e4SLinus Torvalds  * 2. It may change skb pointers.
14571da177e4SLinus Torvalds  *
14581da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
14591da177e4SLinus Torvalds  */
14601da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
14611da177e4SLinus Torvalds {
14621da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
14631da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
14641da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
14651da177e4SLinus Torvalds 	 */
14664305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
14691da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
14701da177e4SLinus Torvalds 				     GFP_ATOMIC))
14711da177e4SLinus Torvalds 			return NULL;
14721da177e4SLinus Torvalds 	}
14731da177e4SLinus Torvalds 
147427a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
14751da177e4SLinus Torvalds 		BUG();
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
14781da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
14791da177e4SLinus Torvalds 	 */
148021dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
14811da177e4SLinus Torvalds 		goto pull_pages;
14821da177e4SLinus Torvalds 
14831da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
14841da177e4SLinus Torvalds 	eat = delta;
14851da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
14869e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
14879e903e08SEric Dumazet 
14889e903e08SEric Dumazet 		if (size >= eat)
14891da177e4SLinus Torvalds 			goto pull_pages;
14909e903e08SEric Dumazet 		eat -= size;
14911da177e4SLinus Torvalds 	}
14921da177e4SLinus Torvalds 
14931da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
14941da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
14951da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
14961da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
14971da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
14981da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
14991da177e4SLinus Torvalds 	 */
15001da177e4SLinus Torvalds 	if (eat) {
15011da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
15021da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
15031da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds 		do {
150609a62660SKris Katterjohn 			BUG_ON(!list);
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 			if (list->len <= eat) {
15091da177e4SLinus Torvalds 				/* Eaten as whole. */
15101da177e4SLinus Torvalds 				eat -= list->len;
15111da177e4SLinus Torvalds 				list = list->next;
15121da177e4SLinus Torvalds 				insp = list;
15131da177e4SLinus Torvalds 			} else {
15141da177e4SLinus Torvalds 				/* Eaten partially. */
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 				if (skb_shared(list)) {
15171da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
15181da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
15191da177e4SLinus Torvalds 					if (!clone)
15201da177e4SLinus Torvalds 						return NULL;
15211da177e4SLinus Torvalds 					insp = list->next;
15221da177e4SLinus Torvalds 					list = clone;
15231da177e4SLinus Torvalds 				} else {
15241da177e4SLinus Torvalds 					/* This may be pulled without
15251da177e4SLinus Torvalds 					 * problems. */
15261da177e4SLinus Torvalds 					insp = list;
15271da177e4SLinus Torvalds 				}
15281da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
15291da177e4SLinus Torvalds 					kfree_skb(clone);
15301da177e4SLinus Torvalds 					return NULL;
15311da177e4SLinus Torvalds 				}
15321da177e4SLinus Torvalds 				break;
15331da177e4SLinus Torvalds 			}
15341da177e4SLinus Torvalds 		} while (eat);
15351da177e4SLinus Torvalds 
15361da177e4SLinus Torvalds 		/* Free pulled out fragments. */
15371da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
15381da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
15391da177e4SLinus Torvalds 			kfree_skb(list);
15401da177e4SLinus Torvalds 		}
15411da177e4SLinus Torvalds 		/* And insert new clone at head. */
15421da177e4SLinus Torvalds 		if (clone) {
15431da177e4SLinus Torvalds 			clone->next = list;
15441da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
15451da177e4SLinus Torvalds 		}
15461da177e4SLinus Torvalds 	}
15471da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds pull_pages:
15501da177e4SLinus Torvalds 	eat = delta;
15511da177e4SLinus Torvalds 	k = 0;
15521da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15539e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
15549e903e08SEric Dumazet 
15559e903e08SEric Dumazet 		if (size <= eat) {
1556ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
15579e903e08SEric Dumazet 			eat -= size;
15581da177e4SLinus Torvalds 		} else {
15591da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
15601da177e4SLinus Torvalds 			if (eat) {
15611da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
15629e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
15631da177e4SLinus Torvalds 				eat = 0;
15641da177e4SLinus Torvalds 			}
15651da177e4SLinus Torvalds 			k++;
15661da177e4SLinus Torvalds 		}
15671da177e4SLinus Torvalds 	}
15681da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	skb->tail     += delta;
15711da177e4SLinus Torvalds 	skb->data_len -= delta;
15721da177e4SLinus Torvalds 
157327a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
15741da177e4SLinus Torvalds }
1575b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
15761da177e4SLinus Torvalds 
157722019b17SEric Dumazet /**
157822019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
157922019b17SEric Dumazet  *	@skb: source skb
158022019b17SEric Dumazet  *	@offset: offset in source
158122019b17SEric Dumazet  *	@to: destination buffer
158222019b17SEric Dumazet  *	@len: number of bytes to copy
158322019b17SEric Dumazet  *
158422019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
158522019b17SEric Dumazet  *	destination buffer.
158622019b17SEric Dumazet  *
158722019b17SEric Dumazet  *	CAUTION ! :
158822019b17SEric Dumazet  *		If its prototype is ever changed,
158922019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
159022019b17SEric Dumazet  *		since it is called from BPF assembly code.
159122019b17SEric Dumazet  */
15921da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
15931da177e4SLinus Torvalds {
15941a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1595fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1596fbb398a8SDavid S. Miller 	int i, copy;
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
15991da177e4SLinus Torvalds 		goto fault;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	/* Copy header. */
16021a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
16031da177e4SLinus Torvalds 		if (copy > len)
16041da177e4SLinus Torvalds 			copy = len;
1605d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
16061da177e4SLinus Torvalds 		if ((len -= copy) == 0)
16071da177e4SLinus Torvalds 			return 0;
16081da177e4SLinus Torvalds 		offset += copy;
16091da177e4SLinus Torvalds 		to     += copy;
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16131a028e50SDavid S. Miller 		int end;
161451c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
16151da177e4SLinus Torvalds 
1616547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16171a028e50SDavid S. Miller 
161851c56b00SEric Dumazet 		end = start + skb_frag_size(f);
16191da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
16201da177e4SLinus Torvalds 			u8 *vaddr;
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 			if (copy > len)
16231da177e4SLinus Torvalds 				copy = len;
16241da177e4SLinus Torvalds 
162551c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(f));
16261da177e4SLinus Torvalds 			memcpy(to,
162751c56b00SEric Dumazet 			       vaddr + f->page_offset + offset - start,
162851c56b00SEric Dumazet 			       copy);
162951c56b00SEric Dumazet 			kunmap_atomic(vaddr);
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds 			if ((len -= copy) == 0)
16321da177e4SLinus Torvalds 				return 0;
16331da177e4SLinus Torvalds 			offset += copy;
16341da177e4SLinus Torvalds 			to     += copy;
16351da177e4SLinus Torvalds 		}
16361a028e50SDavid S. Miller 		start = end;
16371da177e4SLinus Torvalds 	}
16381da177e4SLinus Torvalds 
1639fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
16401a028e50SDavid S. Miller 		int end;
16411da177e4SLinus Torvalds 
1642547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16431a028e50SDavid S. Miller 
1644fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
16451da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
16461da177e4SLinus Torvalds 			if (copy > len)
16471da177e4SLinus Torvalds 				copy = len;
1648fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
16491da177e4SLinus Torvalds 				goto fault;
16501da177e4SLinus Torvalds 			if ((len -= copy) == 0)
16511da177e4SLinus Torvalds 				return 0;
16521da177e4SLinus Torvalds 			offset += copy;
16531da177e4SLinus Torvalds 			to     += copy;
16541da177e4SLinus Torvalds 		}
16551a028e50SDavid S. Miller 		start = end;
16561da177e4SLinus Torvalds 	}
1657a6686f2fSShirley Ma 
16581da177e4SLinus Torvalds 	if (!len)
16591da177e4SLinus Torvalds 		return 0;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds fault:
16621da177e4SLinus Torvalds 	return -EFAULT;
16631da177e4SLinus Torvalds }
1664b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
16651da177e4SLinus Torvalds 
16669c55e01cSJens Axboe /*
16679c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
16689c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
16699c55e01cSJens Axboe  */
16709c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
16719c55e01cSJens Axboe {
16728b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
16738b9d3728SJarek Poplawski }
16749c55e01cSJens Axboe 
1675a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
16764fb66994SJarek Poplawski 				   unsigned int *offset,
167718aafc62SEric Dumazet 				   struct sock *sk)
16788b9d3728SJarek Poplawski {
16795640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
16808b9d3728SJarek Poplawski 
16815640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
16828b9d3728SJarek Poplawski 		return NULL;
16834fb66994SJarek Poplawski 
16845640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
16854fb66994SJarek Poplawski 
16865640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
16875640f768SEric Dumazet 	       page_address(page) + *offset, *len);
16885640f768SEric Dumazet 	*offset = pfrag->offset;
16895640f768SEric Dumazet 	pfrag->offset += *len;
16904fb66994SJarek Poplawski 
16915640f768SEric Dumazet 	return pfrag->page;
16929c55e01cSJens Axboe }
16939c55e01cSJens Axboe 
169441c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
169541c73a0dSEric Dumazet 			     struct page *page,
169641c73a0dSEric Dumazet 			     unsigned int offset)
169741c73a0dSEric Dumazet {
169841c73a0dSEric Dumazet 	return	spd->nr_pages &&
169941c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
170041c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
170141c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
170241c73a0dSEric Dumazet }
170341c73a0dSEric Dumazet 
17049c55e01cSJens Axboe /*
17059c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
17069c55e01cSJens Axboe  */
1707a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
170835f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
17094fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
171018aafc62SEric Dumazet 			  bool linear,
17117a67e56fSJarek Poplawski 			  struct sock *sk)
17129c55e01cSJens Axboe {
171341c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1714a108d5f3SDavid S. Miller 		return true;
17159c55e01cSJens Axboe 
17168b9d3728SJarek Poplawski 	if (linear) {
171718aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
17188b9d3728SJarek Poplawski 		if (!page)
1719a108d5f3SDavid S. Miller 			return true;
172041c73a0dSEric Dumazet 	}
172141c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
172241c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
1723a108d5f3SDavid S. Miller 		return false;
172441c73a0dSEric Dumazet 	}
17258b9d3728SJarek Poplawski 	get_page(page);
17269c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
17274fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
17289c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
17299c55e01cSJens Axboe 	spd->nr_pages++;
17308b9d3728SJarek Poplawski 
1731a108d5f3SDavid S. Miller 	return false;
17329c55e01cSJens Axboe }
17339c55e01cSJens Axboe 
1734a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
17352870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
173618aafc62SEric Dumazet 			     unsigned int *len,
1737d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
173835f3d14dSJens Axboe 			     struct sock *sk,
173935f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
17409c55e01cSJens Axboe {
17412870c43dSOctavian Purdila 	if (!*len)
1742a108d5f3SDavid S. Miller 		return true;
17439c55e01cSJens Axboe 
17442870c43dSOctavian Purdila 	/* skip this segment if already processed */
17452870c43dSOctavian Purdila 	if (*off >= plen) {
17462870c43dSOctavian Purdila 		*off -= plen;
1747a108d5f3SDavid S. Miller 		return false;
17482870c43dSOctavian Purdila 	}
17492870c43dSOctavian Purdila 
17502870c43dSOctavian Purdila 	/* ignore any bits we already processed */
17519ca1b22dSEric Dumazet 	poff += *off;
17529ca1b22dSEric Dumazet 	plen -= *off;
17532870c43dSOctavian Purdila 	*off = 0;
17542870c43dSOctavian Purdila 
175518aafc62SEric Dumazet 	do {
175618aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
17572870c43dSOctavian Purdila 
175818aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
175918aafc62SEric Dumazet 				  linear, sk))
1760a108d5f3SDavid S. Miller 			return true;
176118aafc62SEric Dumazet 		poff += flen;
176218aafc62SEric Dumazet 		plen -= flen;
17632870c43dSOctavian Purdila 		*len -= flen;
176418aafc62SEric Dumazet 	} while (*len && plen);
17652870c43dSOctavian Purdila 
1766a108d5f3SDavid S. Miller 	return false;
1767db43a282SOctavian Purdila }
17689c55e01cSJens Axboe 
17699c55e01cSJens Axboe /*
1770a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
17712870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
17729c55e01cSJens Axboe  */
1773a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
177435f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
177535f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
17762870c43dSOctavian Purdila {
17772870c43dSOctavian Purdila 	int seg;
17789c55e01cSJens Axboe 
17791d0c0b32SEric Dumazet 	/* map the linear part :
17802996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
17812996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
17822996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
17839c55e01cSJens Axboe 	 */
17842870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
17852870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
17862870c43dSOctavian Purdila 			     skb_headlen(skb),
178718aafc62SEric Dumazet 			     offset, len, spd,
17883a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
17891d0c0b32SEric Dumazet 			     sk, pipe))
1790a108d5f3SDavid S. Miller 		return true;
17919c55e01cSJens Axboe 
17929c55e01cSJens Axboe 	/*
17939c55e01cSJens Axboe 	 * then map the fragments
17949c55e01cSJens Axboe 	 */
17959c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
17969c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
17979c55e01cSJens Axboe 
1798ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
17999e903e08SEric Dumazet 				     f->page_offset, skb_frag_size(f),
180018aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
1801a108d5f3SDavid S. Miller 			return true;
18029c55e01cSJens Axboe 	}
18039c55e01cSJens Axboe 
1804a108d5f3SDavid S. Miller 	return false;
18059c55e01cSJens Axboe }
18069c55e01cSJens Axboe 
18079c55e01cSJens Axboe /*
18089c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
18099c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
18109c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
18119c55e01cSJens Axboe  * handle that cleanly.
18129c55e01cSJens Axboe  */
18138b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
18149c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
18159c55e01cSJens Axboe 		    unsigned int flags)
18169c55e01cSJens Axboe {
181741c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
181841c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
18199c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
18209c55e01cSJens Axboe 		.pages = pages,
18219c55e01cSJens Axboe 		.partial = partial,
1822047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
18239c55e01cSJens Axboe 		.flags = flags,
182428a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
18259c55e01cSJens Axboe 		.spd_release = sock_spd_release,
18269c55e01cSJens Axboe 	};
1827fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
18287a67e56fSJarek Poplawski 	struct sock *sk = skb->sk;
182935f3d14dSJens Axboe 	int ret = 0;
183035f3d14dSJens Axboe 
18319c55e01cSJens Axboe 	/*
18329c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
18339c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
18349c55e01cSJens Axboe 	 */
183535f3d14dSJens Axboe 	if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
18369c55e01cSJens Axboe 		goto done;
18379c55e01cSJens Axboe 	else if (!tlen)
18389c55e01cSJens Axboe 		goto done;
18399c55e01cSJens Axboe 
18409c55e01cSJens Axboe 	/*
18419c55e01cSJens Axboe 	 * now see if we have a frag_list to map
18429c55e01cSJens Axboe 	 */
1843fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
1844fbb398a8SDavid S. Miller 		if (!tlen)
18459c55e01cSJens Axboe 			break;
184635f3d14dSJens Axboe 		if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1847fbb398a8SDavid S. Miller 			break;
18489c55e01cSJens Axboe 	}
18499c55e01cSJens Axboe 
18509c55e01cSJens Axboe done:
18519c55e01cSJens Axboe 	if (spd.nr_pages) {
18529c55e01cSJens Axboe 		/*
18539c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
18549c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
18559c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
18569c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
18579c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
18589c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
18599c55e01cSJens Axboe 		 * and networking will grab the socket lock.
18609c55e01cSJens Axboe 		 */
1861293ad604SOctavian Purdila 		release_sock(sk);
18629c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1863293ad604SOctavian Purdila 		lock_sock(sk);
18649c55e01cSJens Axboe 	}
18659c55e01cSJens Axboe 
186635f3d14dSJens Axboe 	return ret;
18679c55e01cSJens Axboe }
18689c55e01cSJens Axboe 
1869357b40a1SHerbert Xu /**
1870357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1871357b40a1SHerbert Xu  *	@skb: destination buffer
1872357b40a1SHerbert Xu  *	@offset: offset in destination
1873357b40a1SHerbert Xu  *	@from: source buffer
1874357b40a1SHerbert Xu  *	@len: number of bytes to copy
1875357b40a1SHerbert Xu  *
1876357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1877357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1878357b40a1SHerbert Xu  *	traversing fragment lists and such.
1879357b40a1SHerbert Xu  */
1880357b40a1SHerbert Xu 
18810c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1882357b40a1SHerbert Xu {
18831a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1884fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1885fbb398a8SDavid S. Miller 	int i, copy;
1886357b40a1SHerbert Xu 
1887357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1888357b40a1SHerbert Xu 		goto fault;
1889357b40a1SHerbert Xu 
18901a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1891357b40a1SHerbert Xu 		if (copy > len)
1892357b40a1SHerbert Xu 			copy = len;
189327d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1894357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1895357b40a1SHerbert Xu 			return 0;
1896357b40a1SHerbert Xu 		offset += copy;
1897357b40a1SHerbert Xu 		from += copy;
1898357b40a1SHerbert Xu 	}
1899357b40a1SHerbert Xu 
1900357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1901357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
19021a028e50SDavid S. Miller 		int end;
1903357b40a1SHerbert Xu 
1904547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19051a028e50SDavid S. Miller 
19069e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
1907357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1908357b40a1SHerbert Xu 			u8 *vaddr;
1909357b40a1SHerbert Xu 
1910357b40a1SHerbert Xu 			if (copy > len)
1911357b40a1SHerbert Xu 				copy = len;
1912357b40a1SHerbert Xu 
191351c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
19141a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
19151a028e50SDavid S. Miller 			       from, copy);
191651c56b00SEric Dumazet 			kunmap_atomic(vaddr);
1917357b40a1SHerbert Xu 
1918357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1919357b40a1SHerbert Xu 				return 0;
1920357b40a1SHerbert Xu 			offset += copy;
1921357b40a1SHerbert Xu 			from += copy;
1922357b40a1SHerbert Xu 		}
19231a028e50SDavid S. Miller 		start = end;
1924357b40a1SHerbert Xu 	}
1925357b40a1SHerbert Xu 
1926fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
19271a028e50SDavid S. Miller 		int end;
1928357b40a1SHerbert Xu 
1929547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19301a028e50SDavid S. Miller 
1931fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
1932357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1933357b40a1SHerbert Xu 			if (copy > len)
1934357b40a1SHerbert Xu 				copy = len;
1935fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
19361a028e50SDavid S. Miller 					   from, copy))
1937357b40a1SHerbert Xu 				goto fault;
1938357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1939357b40a1SHerbert Xu 				return 0;
1940357b40a1SHerbert Xu 			offset += copy;
1941357b40a1SHerbert Xu 			from += copy;
1942357b40a1SHerbert Xu 		}
19431a028e50SDavid S. Miller 		start = end;
1944357b40a1SHerbert Xu 	}
1945357b40a1SHerbert Xu 	if (!len)
1946357b40a1SHerbert Xu 		return 0;
1947357b40a1SHerbert Xu 
1948357b40a1SHerbert Xu fault:
1949357b40a1SHerbert Xu 	return -EFAULT;
1950357b40a1SHerbert Xu }
1951357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1952357b40a1SHerbert Xu 
19531da177e4SLinus Torvalds /* Checksum skb data. */
19542817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
19552817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
19561da177e4SLinus Torvalds {
19571a028e50SDavid S. Miller 	int start = skb_headlen(skb);
19581a028e50SDavid S. Miller 	int i, copy = start - offset;
1959fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
19601da177e4SLinus Torvalds 	int pos = 0;
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds 	/* Checksum header. */
19631da177e4SLinus Torvalds 	if (copy > 0) {
19641da177e4SLinus Torvalds 		if (copy > len)
19651da177e4SLinus Torvalds 			copy = len;
19662817a336SDaniel Borkmann 		csum = ops->update(skb->data + offset, copy, csum);
19671da177e4SLinus Torvalds 		if ((len -= copy) == 0)
19681da177e4SLinus Torvalds 			return csum;
19691da177e4SLinus Torvalds 		offset += copy;
19701da177e4SLinus Torvalds 		pos	= copy;
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
19741a028e50SDavid S. Miller 		int end;
197551c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
19761da177e4SLinus Torvalds 
1977547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19781a028e50SDavid S. Miller 
197951c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
19801da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
198144bb9363SAl Viro 			__wsum csum2;
19821da177e4SLinus Torvalds 			u8 *vaddr;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 			if (copy > len)
19851da177e4SLinus Torvalds 				copy = len;
198651c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
19872817a336SDaniel Borkmann 			csum2 = ops->update(vaddr + frag->page_offset +
19881a028e50SDavid S. Miller 					    offset - start, copy, 0);
198951c56b00SEric Dumazet 			kunmap_atomic(vaddr);
19902817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
19911da177e4SLinus Torvalds 			if (!(len -= copy))
19921da177e4SLinus Torvalds 				return csum;
19931da177e4SLinus Torvalds 			offset += copy;
19941da177e4SLinus Torvalds 			pos    += copy;
19951da177e4SLinus Torvalds 		}
19961a028e50SDavid S. Miller 		start = end;
19971da177e4SLinus Torvalds 	}
19981da177e4SLinus Torvalds 
1999fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
20001a028e50SDavid S. Miller 		int end;
20011da177e4SLinus Torvalds 
2002547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20031a028e50SDavid S. Miller 
2004fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
20051da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
20065f92a738SAl Viro 			__wsum csum2;
20071da177e4SLinus Torvalds 			if (copy > len)
20081da177e4SLinus Torvalds 				copy = len;
20092817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
20102817a336SDaniel Borkmann 					       copy, 0, ops);
20112817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
20121da177e4SLinus Torvalds 			if ((len -= copy) == 0)
20131da177e4SLinus Torvalds 				return csum;
20141da177e4SLinus Torvalds 			offset += copy;
20151da177e4SLinus Torvalds 			pos    += copy;
20161da177e4SLinus Torvalds 		}
20171a028e50SDavid S. Miller 		start = end;
20181da177e4SLinus Torvalds 	}
201909a62660SKris Katterjohn 	BUG_ON(len);
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	return csum;
20221da177e4SLinus Torvalds }
20232817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
20242817a336SDaniel Borkmann 
20252817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
20262817a336SDaniel Borkmann 		    int len, __wsum csum)
20272817a336SDaniel Borkmann {
20282817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2029cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
20302817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
20312817a336SDaniel Borkmann 	};
20322817a336SDaniel Borkmann 
20332817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
20342817a336SDaniel Borkmann }
2035b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds /* Both of above in one bottle. */
20381da177e4SLinus Torvalds 
203981d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
204081d77662SAl Viro 				    u8 *to, int len, __wsum csum)
20411da177e4SLinus Torvalds {
20421a028e50SDavid S. Miller 	int start = skb_headlen(skb);
20431a028e50SDavid S. Miller 	int i, copy = start - offset;
2044fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
20451da177e4SLinus Torvalds 	int pos = 0;
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds 	/* Copy header. */
20481da177e4SLinus Torvalds 	if (copy > 0) {
20491da177e4SLinus Torvalds 		if (copy > len)
20501da177e4SLinus Torvalds 			copy = len;
20511da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
20521da177e4SLinus Torvalds 						 copy, csum);
20531da177e4SLinus Torvalds 		if ((len -= copy) == 0)
20541da177e4SLinus Torvalds 			return csum;
20551da177e4SLinus Torvalds 		offset += copy;
20561da177e4SLinus Torvalds 		to     += copy;
20571da177e4SLinus Torvalds 		pos	= copy;
20581da177e4SLinus Torvalds 	}
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20611a028e50SDavid S. Miller 		int end;
20621da177e4SLinus Torvalds 
2063547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20641a028e50SDavid S. Miller 
20659e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
20661da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
20675084205fSAl Viro 			__wsum csum2;
20681da177e4SLinus Torvalds 			u8 *vaddr;
20691da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 			if (copy > len)
20721da177e4SLinus Torvalds 				copy = len;
207351c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
20741da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
20751a028e50SDavid S. Miller 							  frag->page_offset +
20761a028e50SDavid S. Miller 							  offset - start, to,
20771a028e50SDavid S. Miller 							  copy, 0);
207851c56b00SEric Dumazet 			kunmap_atomic(vaddr);
20791da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
20801da177e4SLinus Torvalds 			if (!(len -= copy))
20811da177e4SLinus Torvalds 				return csum;
20821da177e4SLinus Torvalds 			offset += copy;
20831da177e4SLinus Torvalds 			to     += copy;
20841da177e4SLinus Torvalds 			pos    += copy;
20851da177e4SLinus Torvalds 		}
20861a028e50SDavid S. Miller 		start = end;
20871da177e4SLinus Torvalds 	}
20881da177e4SLinus Torvalds 
2089fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
209081d77662SAl Viro 		__wsum csum2;
20911a028e50SDavid S. Miller 		int end;
20921da177e4SLinus Torvalds 
2093547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20941a028e50SDavid S. Miller 
2095fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
20961da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
20971da177e4SLinus Torvalds 			if (copy > len)
20981da177e4SLinus Torvalds 				copy = len;
2099fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
21001a028e50SDavid S. Miller 						       offset - start,
21011da177e4SLinus Torvalds 						       to, copy, 0);
21021da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
21031da177e4SLinus Torvalds 			if ((len -= copy) == 0)
21041da177e4SLinus Torvalds 				return csum;
21051da177e4SLinus Torvalds 			offset += copy;
21061da177e4SLinus Torvalds 			to     += copy;
21071da177e4SLinus Torvalds 			pos    += copy;
21081da177e4SLinus Torvalds 		}
21091a028e50SDavid S. Miller 		start = end;
21101da177e4SLinus Torvalds 	}
211109a62660SKris Katterjohn 	BUG_ON(len);
21121da177e4SLinus Torvalds 	return csum;
21131da177e4SLinus Torvalds }
2114b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
21151da177e4SLinus Torvalds 
2116af2806f8SThomas Graf  /**
2117af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2118af2806f8SThomas Graf  *	@from: source buffer
2119af2806f8SThomas Graf  *
2120af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2121af2806f8SThomas Graf  *	into skb_zerocopy().
2122af2806f8SThomas Graf  */
2123af2806f8SThomas Graf unsigned int
2124af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2125af2806f8SThomas Graf {
2126af2806f8SThomas Graf 	unsigned int hlen = 0;
2127af2806f8SThomas Graf 
2128af2806f8SThomas Graf 	if (!from->head_frag ||
2129af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2130af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2131af2806f8SThomas Graf 		hlen = skb_headlen(from);
2132af2806f8SThomas Graf 
2133af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2134af2806f8SThomas Graf 		hlen = from->len;
2135af2806f8SThomas Graf 
2136af2806f8SThomas Graf 	return hlen;
2137af2806f8SThomas Graf }
2138af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2139af2806f8SThomas Graf 
2140af2806f8SThomas Graf /**
2141af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2142af2806f8SThomas Graf  *	@to: destination buffer
21437fceb4deSMasanari Iida  *	@from: source buffer
2144af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2145af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2146af2806f8SThomas Graf  *
2147af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2148af2806f8SThomas Graf  *	to the frags in the source buffer.
2149af2806f8SThomas Graf  *
2150af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2151af2806f8SThomas Graf  *	headroom in the `to` buffer.
215236d5fe6aSZoltan Kiss  *
215336d5fe6aSZoltan Kiss  *	Return value:
215436d5fe6aSZoltan Kiss  *	0: everything is OK
215536d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
215636d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2157af2806f8SThomas Graf  */
215836d5fe6aSZoltan Kiss int
215936d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2160af2806f8SThomas Graf {
2161af2806f8SThomas Graf 	int i, j = 0;
2162af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
216336d5fe6aSZoltan Kiss 	int ret;
2164af2806f8SThomas Graf 	struct page *page;
2165af2806f8SThomas Graf 	unsigned int offset;
2166af2806f8SThomas Graf 
2167af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2168af2806f8SThomas Graf 
2169af2806f8SThomas Graf 	/* dont bother with small payloads */
217036d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
217136d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2172af2806f8SThomas Graf 
2173af2806f8SThomas Graf 	if (hlen) {
217436d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
217536d5fe6aSZoltan Kiss 		if (unlikely(ret))
217636d5fe6aSZoltan Kiss 			return ret;
2177af2806f8SThomas Graf 		len -= hlen;
2178af2806f8SThomas Graf 	} else {
2179af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2180af2806f8SThomas Graf 		if (plen) {
2181af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2182af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2183af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2184af2806f8SThomas Graf 			get_page(page);
2185af2806f8SThomas Graf 			j = 1;
2186af2806f8SThomas Graf 			len -= plen;
2187af2806f8SThomas Graf 		}
2188af2806f8SThomas Graf 	}
2189af2806f8SThomas Graf 
2190af2806f8SThomas Graf 	to->truesize += len + plen;
2191af2806f8SThomas Graf 	to->len += len + plen;
2192af2806f8SThomas Graf 	to->data_len += len + plen;
2193af2806f8SThomas Graf 
219436d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
219536d5fe6aSZoltan Kiss 		skb_tx_error(from);
219636d5fe6aSZoltan Kiss 		return -ENOMEM;
219736d5fe6aSZoltan Kiss 	}
219836d5fe6aSZoltan Kiss 
2199af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2200af2806f8SThomas Graf 		if (!len)
2201af2806f8SThomas Graf 			break;
2202af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2203af2806f8SThomas Graf 		skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2204af2806f8SThomas Graf 		len -= skb_shinfo(to)->frags[j].size;
2205af2806f8SThomas Graf 		skb_frag_ref(to, j);
2206af2806f8SThomas Graf 		j++;
2207af2806f8SThomas Graf 	}
2208af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
220936d5fe6aSZoltan Kiss 
221036d5fe6aSZoltan Kiss 	return 0;
2211af2806f8SThomas Graf }
2212af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
2213af2806f8SThomas Graf 
22141da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
22151da177e4SLinus Torvalds {
2216d3bc23e7SAl Viro 	__wsum csum;
22171da177e4SLinus Torvalds 	long csstart;
22181da177e4SLinus Torvalds 
221984fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
222055508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
22211da177e4SLinus Torvalds 	else
22221da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
22231da177e4SLinus Torvalds 
222409a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
22251da177e4SLinus Torvalds 
2226d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds 	csum = 0;
22291da177e4SLinus Torvalds 	if (csstart != skb->len)
22301da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
22311da177e4SLinus Torvalds 					      skb->len - csstart, 0);
22321da177e4SLinus Torvalds 
223384fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2234ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
22351da177e4SLinus Torvalds 
2236d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
22371da177e4SLinus Torvalds 	}
22381da177e4SLinus Torvalds }
2239b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds /**
22421da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
22431da177e4SLinus Torvalds  *	@list: list to dequeue from
22441da177e4SLinus Torvalds  *
22451da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
22461da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
22471da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
22481da177e4SLinus Torvalds  */
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
22511da177e4SLinus Torvalds {
22521da177e4SLinus Torvalds 	unsigned long flags;
22531da177e4SLinus Torvalds 	struct sk_buff *result;
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
22561da177e4SLinus Torvalds 	result = __skb_dequeue(list);
22571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
22581da177e4SLinus Torvalds 	return result;
22591da177e4SLinus Torvalds }
2260b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
22611da177e4SLinus Torvalds 
22621da177e4SLinus Torvalds /**
22631da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
22641da177e4SLinus Torvalds  *	@list: list to dequeue from
22651da177e4SLinus Torvalds  *
22661da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
22671da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
22681da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
22691da177e4SLinus Torvalds  */
22701da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
22711da177e4SLinus Torvalds {
22721da177e4SLinus Torvalds 	unsigned long flags;
22731da177e4SLinus Torvalds 	struct sk_buff *result;
22741da177e4SLinus Torvalds 
22751da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
22761da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
22771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
22781da177e4SLinus Torvalds 	return result;
22791da177e4SLinus Torvalds }
2280b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
22811da177e4SLinus Torvalds 
22821da177e4SLinus Torvalds /**
22831da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
22841da177e4SLinus Torvalds  *	@list: list to empty
22851da177e4SLinus Torvalds  *
22861da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
22871da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
22881da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
22891da177e4SLinus Torvalds  */
22901da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
22911da177e4SLinus Torvalds {
22921da177e4SLinus Torvalds 	struct sk_buff *skb;
22931da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
22941da177e4SLinus Torvalds 		kfree_skb(skb);
22951da177e4SLinus Torvalds }
2296b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
22971da177e4SLinus Torvalds 
22981da177e4SLinus Torvalds /**
22991da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
23001da177e4SLinus Torvalds  *	@list: list to use
23011da177e4SLinus Torvalds  *	@newsk: buffer to queue
23021da177e4SLinus Torvalds  *
23031da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
23041da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
23051da177e4SLinus Torvalds  *	safely.
23061da177e4SLinus Torvalds  *
23071da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23081da177e4SLinus Torvalds  */
23091da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
23101da177e4SLinus Torvalds {
23111da177e4SLinus Torvalds 	unsigned long flags;
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23141da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
23151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23161da177e4SLinus Torvalds }
2317b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
23181da177e4SLinus Torvalds 
23191da177e4SLinus Torvalds /**
23201da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
23211da177e4SLinus Torvalds  *	@list: list to use
23221da177e4SLinus Torvalds  *	@newsk: buffer to queue
23231da177e4SLinus Torvalds  *
23241da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
23251da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
23261da177e4SLinus Torvalds  *	safely.
23271da177e4SLinus Torvalds  *
23281da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23291da177e4SLinus Torvalds  */
23301da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
23311da177e4SLinus Torvalds {
23321da177e4SLinus Torvalds 	unsigned long flags;
23331da177e4SLinus Torvalds 
23341da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23351da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
23361da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23371da177e4SLinus Torvalds }
2338b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
23398728b834SDavid S. Miller 
23401da177e4SLinus Torvalds /**
23411da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
23421da177e4SLinus Torvalds  *	@skb: buffer to remove
23438728b834SDavid S. Miller  *	@list: list to use
23441da177e4SLinus Torvalds  *
23458728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
23468728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
23471da177e4SLinus Torvalds  *
23488728b834SDavid S. Miller  *	You must know what list the SKB is on.
23491da177e4SLinus Torvalds  */
23508728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
23511da177e4SLinus Torvalds {
23521da177e4SLinus Torvalds 	unsigned long flags;
23531da177e4SLinus Torvalds 
23541da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23558728b834SDavid S. Miller 	__skb_unlink(skb, list);
23561da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23571da177e4SLinus Torvalds }
2358b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds /**
23611da177e4SLinus Torvalds  *	skb_append	-	append a buffer
23621da177e4SLinus Torvalds  *	@old: buffer to insert after
23631da177e4SLinus Torvalds  *	@newsk: buffer to insert
23648728b834SDavid S. Miller  *	@list: list to use
23651da177e4SLinus Torvalds  *
23661da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
23671da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
23681da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23691da177e4SLinus Torvalds  */
23708728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
23711da177e4SLinus Torvalds {
23721da177e4SLinus Torvalds 	unsigned long flags;
23731da177e4SLinus Torvalds 
23748728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
23757de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
23768728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
23771da177e4SLinus Torvalds }
2378b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
23791da177e4SLinus Torvalds 
23801da177e4SLinus Torvalds /**
23811da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
23821da177e4SLinus Torvalds  *	@old: buffer to insert before
23831da177e4SLinus Torvalds  *	@newsk: buffer to insert
23848728b834SDavid S. Miller  *	@list: list to use
23851da177e4SLinus Torvalds  *
23868728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
23878728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
23888728b834SDavid S. Miller  *	calls.
23898728b834SDavid S. Miller  *
23901da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23911da177e4SLinus Torvalds  */
23928728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
23931da177e4SLinus Torvalds {
23941da177e4SLinus Torvalds 	unsigned long flags;
23951da177e4SLinus Torvalds 
23968728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
23978728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
23988728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
23991da177e4SLinus Torvalds }
2400b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
24011da177e4SLinus Torvalds 
24021da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
24031da177e4SLinus Torvalds 					   struct sk_buff* skb1,
24041da177e4SLinus Torvalds 					   const u32 len, const int pos)
24051da177e4SLinus Torvalds {
24061da177e4SLinus Torvalds 	int i;
24071da177e4SLinus Torvalds 
2408d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2409d626f62bSArnaldo Carvalho de Melo 					 pos - len);
24101da177e4SLinus Torvalds 	/* And move data appendix as is. */
24111da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
24121da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
24151da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
24161da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
24171da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
24181da177e4SLinus Torvalds 	skb->data_len		   = 0;
24191da177e4SLinus Torvalds 	skb->len		   = len;
242027a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
24211da177e4SLinus Torvalds }
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
24241da177e4SLinus Torvalds 				       struct sk_buff* skb1,
24251da177e4SLinus Torvalds 				       const u32 len, int pos)
24261da177e4SLinus Torvalds {
24271da177e4SLinus Torvalds 	int i, k = 0;
24281da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
24311da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
24321da177e4SLinus Torvalds 	skb->len		  = len;
24331da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
24369e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
24371da177e4SLinus Torvalds 
24381da177e4SLinus Torvalds 		if (pos + size > len) {
24391da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 			if (pos < len) {
24421da177e4SLinus Torvalds 				/* Split frag.
24431da177e4SLinus Torvalds 				 * We have two variants in this case:
24441da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
24451da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
24461da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
24471da177e4SLinus Torvalds 				 *    where splitting is expensive.
24481da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
24491da177e4SLinus Torvalds 				 */
2450ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
24511da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
24529e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
24539e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
24541da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
24551da177e4SLinus Torvalds 			}
24561da177e4SLinus Torvalds 			k++;
24571da177e4SLinus Torvalds 		} else
24581da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
24591da177e4SLinus Torvalds 		pos += size;
24601da177e4SLinus Torvalds 	}
24611da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds /**
24651da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
24661da177e4SLinus Torvalds  * @skb: the buffer to split
24671da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
24681da177e4SLinus Torvalds  * @len: new length for skb
24691da177e4SLinus Torvalds  */
24701da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
24711da177e4SLinus Torvalds {
24721da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
24731da177e4SLinus Torvalds 
247468534c68SAmerigo Wang 	skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
24751da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
24761da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
24771da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
24781da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
24791da177e4SLinus Torvalds }
2480b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
24811da177e4SLinus Torvalds 
24829f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
24839f782db3SIlpo Järvinen  *
24849f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
24859f782db3SIlpo Järvinen  */
2486832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2487832d11c5SIlpo Järvinen {
24880ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2489832d11c5SIlpo Järvinen }
2490832d11c5SIlpo Järvinen 
2491832d11c5SIlpo Järvinen /**
2492832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2493832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2494832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2495832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2496832d11c5SIlpo Järvinen  *
2497832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
249820e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2499832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2500832d11c5SIlpo Järvinen  *
2501832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2502832d11c5SIlpo Järvinen  *
2503832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2504832d11c5SIlpo Järvinen  * to have non-paged data as well.
2505832d11c5SIlpo Järvinen  *
2506832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2507832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2508832d11c5SIlpo Järvinen  */
2509832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2510832d11c5SIlpo Järvinen {
2511832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2512832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2513832d11c5SIlpo Järvinen 
2514832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2515832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2516832d11c5SIlpo Järvinen 
2517832d11c5SIlpo Järvinen 	todo = shiftlen;
2518832d11c5SIlpo Järvinen 	from = 0;
2519832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2520832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2521832d11c5SIlpo Järvinen 
2522832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2523832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2524832d11c5SIlpo Järvinen 	 */
2525832d11c5SIlpo Järvinen 	if (!to ||
2526ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2527ea2ab693SIan Campbell 			      fragfrom->page_offset)) {
2528832d11c5SIlpo Järvinen 		merge = -1;
2529832d11c5SIlpo Järvinen 	} else {
2530832d11c5SIlpo Järvinen 		merge = to - 1;
2531832d11c5SIlpo Järvinen 
25329e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
2533832d11c5SIlpo Järvinen 		if (todo < 0) {
2534832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2535832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2536832d11c5SIlpo Järvinen 				return 0;
2537832d11c5SIlpo Järvinen 
25389f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
25399f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2540832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2541832d11c5SIlpo Järvinen 
25429e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
25439e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
2544832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2545832d11c5SIlpo Järvinen 
2546832d11c5SIlpo Järvinen 			goto onlymerged;
2547832d11c5SIlpo Järvinen 		}
2548832d11c5SIlpo Järvinen 
2549832d11c5SIlpo Järvinen 		from++;
2550832d11c5SIlpo Järvinen 	}
2551832d11c5SIlpo Järvinen 
2552832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2553832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2554832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2555832d11c5SIlpo Järvinen 		return 0;
2556832d11c5SIlpo Järvinen 
2557832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2558832d11c5SIlpo Järvinen 		return 0;
2559832d11c5SIlpo Järvinen 
2560832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2561832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2562832d11c5SIlpo Järvinen 			return 0;
2563832d11c5SIlpo Järvinen 
2564832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2565832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2566832d11c5SIlpo Järvinen 
25679e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
2568832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
25699e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
2570832d11c5SIlpo Järvinen 			from++;
2571832d11c5SIlpo Järvinen 			to++;
2572832d11c5SIlpo Järvinen 
2573832d11c5SIlpo Järvinen 		} else {
2574ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
2575832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2576832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
25779e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
2578832d11c5SIlpo Järvinen 
2579832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
25809e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
2581832d11c5SIlpo Järvinen 			todo = 0;
2582832d11c5SIlpo Järvinen 
2583832d11c5SIlpo Järvinen 			to++;
2584832d11c5SIlpo Järvinen 			break;
2585832d11c5SIlpo Järvinen 		}
2586832d11c5SIlpo Järvinen 	}
2587832d11c5SIlpo Järvinen 
2588832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2589832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2590832d11c5SIlpo Järvinen 
2591832d11c5SIlpo Järvinen 	if (merge >= 0) {
2592832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2593832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2594832d11c5SIlpo Järvinen 
25959e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2596ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
2597832d11c5SIlpo Järvinen 	}
2598832d11c5SIlpo Järvinen 
2599832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2600832d11c5SIlpo Järvinen 	to = 0;
2601832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2602832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2603832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2604832d11c5SIlpo Järvinen 
2605832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2606832d11c5SIlpo Järvinen 
2607832d11c5SIlpo Järvinen onlymerged:
2608832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2609832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2610832d11c5SIlpo Järvinen 	 */
2611832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2612832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2613832d11c5SIlpo Järvinen 
2614832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2615832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2616832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2617832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2618832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2619832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2620832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2621832d11c5SIlpo Järvinen 
2622832d11c5SIlpo Järvinen 	return shiftlen;
2623832d11c5SIlpo Järvinen }
2624832d11c5SIlpo Järvinen 
2625677e90edSThomas Graf /**
2626677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2627677e90edSThomas Graf  * @skb: the buffer to read
2628677e90edSThomas Graf  * @from: lower offset of data to be read
2629677e90edSThomas Graf  * @to: upper offset of data to be read
2630677e90edSThomas Graf  * @st: state variable
2631677e90edSThomas Graf  *
2632677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2633677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2634677e90edSThomas Graf  */
2635677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2636677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2637677e90edSThomas Graf {
2638677e90edSThomas Graf 	st->lower_offset = from;
2639677e90edSThomas Graf 	st->upper_offset = to;
2640677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2641677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2642677e90edSThomas Graf 	st->frag_data = NULL;
2643677e90edSThomas Graf }
2644b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2645677e90edSThomas Graf 
2646677e90edSThomas Graf /**
2647677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2648677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2649677e90edSThomas Graf  * @data: destination pointer for data to be returned
2650677e90edSThomas Graf  * @st: state variable
2651677e90edSThomas Graf  *
2652bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
2653677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2654bc32383cSMathias Krause  * the head of the data block to @data and returns the length
2655677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2656677e90edSThomas Graf  * offset has been reached.
2657677e90edSThomas Graf  *
2658677e90edSThomas Graf  * The caller is not required to consume all of the data
2659bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
2660677e90edSThomas Graf  * of bytes already consumed and the next call to
2661677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2662677e90edSThomas Graf  *
266325985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
2664e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
2665677e90edSThomas Graf  *       reads of potentially non linear data.
2666677e90edSThomas Graf  *
2667bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2668677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2669677e90edSThomas Graf  *       a stack for this purpose.
2670677e90edSThomas Graf  */
2671677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2672677e90edSThomas Graf 			  struct skb_seq_state *st)
2673677e90edSThomas Graf {
2674677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2675677e90edSThomas Graf 	skb_frag_t *frag;
2676677e90edSThomas Graf 
2677aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
2678aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
2679aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
2680aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
2681aeb193eaSWedson Almeida Filho 		}
2682677e90edSThomas Graf 		return 0;
2683aeb193eaSWedson Almeida Filho 	}
2684677e90edSThomas Graf 
2685677e90edSThomas Graf next_skb:
268695e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2687677e90edSThomas Graf 
2688995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
268995e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2690677e90edSThomas Graf 		return block_limit - abs_offset;
2691677e90edSThomas Graf 	}
2692677e90edSThomas Graf 
2693677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2694677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2695677e90edSThomas Graf 
2696677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2697677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
26989e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
2699677e90edSThomas Graf 
2700677e90edSThomas Graf 		if (abs_offset < block_limit) {
2701677e90edSThomas Graf 			if (!st->frag_data)
270251c56b00SEric Dumazet 				st->frag_data = kmap_atomic(skb_frag_page(frag));
2703677e90edSThomas Graf 
2704677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2705677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2706677e90edSThomas Graf 
2707677e90edSThomas Graf 			return block_limit - abs_offset;
2708677e90edSThomas Graf 		}
2709677e90edSThomas Graf 
2710677e90edSThomas Graf 		if (st->frag_data) {
271151c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
2712677e90edSThomas Graf 			st->frag_data = NULL;
2713677e90edSThomas Graf 		}
2714677e90edSThomas Graf 
2715677e90edSThomas Graf 		st->frag_idx++;
27169e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
2717677e90edSThomas Graf 	}
2718677e90edSThomas Graf 
27195b5a60daSOlaf Kirch 	if (st->frag_data) {
272051c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
27215b5a60daSOlaf Kirch 		st->frag_data = NULL;
27225b5a60daSOlaf Kirch 	}
27235b5a60daSOlaf Kirch 
272421dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2725677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
272695e3b24cSHerbert Xu 		st->frag_idx = 0;
2727677e90edSThomas Graf 		goto next_skb;
272871b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
272971b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
273071b3346dSShyam Iyer 		st->frag_idx = 0;
2731677e90edSThomas Graf 		goto next_skb;
2732677e90edSThomas Graf 	}
2733677e90edSThomas Graf 
2734677e90edSThomas Graf 	return 0;
2735677e90edSThomas Graf }
2736b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2737677e90edSThomas Graf 
2738677e90edSThomas Graf /**
2739677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2740677e90edSThomas Graf  * @st: state variable
2741677e90edSThomas Graf  *
2742677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2743677e90edSThomas Graf  * returned 0.
2744677e90edSThomas Graf  */
2745677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2746677e90edSThomas Graf {
2747677e90edSThomas Graf 	if (st->frag_data)
274851c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
2749677e90edSThomas Graf }
2750b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2751677e90edSThomas Graf 
27523fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
27533fc7e8a6SThomas Graf 
27543fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
27553fc7e8a6SThomas Graf 					  struct ts_config *conf,
27563fc7e8a6SThomas Graf 					  struct ts_state *state)
27573fc7e8a6SThomas Graf {
27583fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
27593fc7e8a6SThomas Graf }
27603fc7e8a6SThomas Graf 
27613fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
27623fc7e8a6SThomas Graf {
27633fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
27643fc7e8a6SThomas Graf }
27653fc7e8a6SThomas Graf 
27663fc7e8a6SThomas Graf /**
27673fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
27683fc7e8a6SThomas Graf  * @skb: the buffer to look in
27693fc7e8a6SThomas Graf  * @from: search offset
27703fc7e8a6SThomas Graf  * @to: search limit
27713fc7e8a6SThomas Graf  * @config: textsearch configuration
27723fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
27733fc7e8a6SThomas Graf  *
27743fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
27753fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
27763fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
27773fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
27783fc7e8a6SThomas Graf  */
27793fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
27803fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
27813fc7e8a6SThomas Graf 			   struct ts_state *state)
27823fc7e8a6SThomas Graf {
2783f72b948dSPhil Oester 	unsigned int ret;
2784f72b948dSPhil Oester 
27853fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
27863fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
27873fc7e8a6SThomas Graf 
27883fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
27893fc7e8a6SThomas Graf 
2790f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2791f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
27923fc7e8a6SThomas Graf }
2793b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
27943fc7e8a6SThomas Graf 
2795e89e9cf5SAnanda Raju /**
27962c53040fSBen Hutchings  * skb_append_datato_frags - append the user data to a skb
2797e89e9cf5SAnanda Raju  * @sk: sock  structure
2798e793c0f7SMasanari Iida  * @skb: skb structure to be appended with user data.
2799e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2800e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2801e89e9cf5SAnanda Raju  * @length: length of the iov message
2802e89e9cf5SAnanda Raju  *
2803e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2804e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2805e89e9cf5SAnanda Raju  */
2806e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2807dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2808e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2809e89e9cf5SAnanda Raju 			void *from, int length)
2810e89e9cf5SAnanda Raju {
2811b2111724SEric Dumazet 	int frg_cnt = skb_shinfo(skb)->nr_frags;
2812b2111724SEric Dumazet 	int copy;
2813e89e9cf5SAnanda Raju 	int offset = 0;
2814e89e9cf5SAnanda Raju 	int ret;
2815b2111724SEric Dumazet 	struct page_frag *pfrag = &current->task_frag;
2816e89e9cf5SAnanda Raju 
2817e89e9cf5SAnanda Raju 	do {
2818e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2819e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2820b2111724SEric Dumazet 			return -EMSGSIZE;
2821e89e9cf5SAnanda Raju 
2822b2111724SEric Dumazet 		if (!sk_page_frag_refill(sk, pfrag))
2823e89e9cf5SAnanda Raju 			return -ENOMEM;
2824e89e9cf5SAnanda Raju 
2825e89e9cf5SAnanda Raju 		/* copy the user data to page */
2826b2111724SEric Dumazet 		copy = min_t(int, length, pfrag->size - pfrag->offset);
2827e89e9cf5SAnanda Raju 
2828b2111724SEric Dumazet 		ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2829e89e9cf5SAnanda Raju 			      offset, copy, 0, skb);
2830e89e9cf5SAnanda Raju 		if (ret < 0)
2831e89e9cf5SAnanda Raju 			return -EFAULT;
2832e89e9cf5SAnanda Raju 
2833e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2834b2111724SEric Dumazet 		skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2835b2111724SEric Dumazet 				   copy);
2836b2111724SEric Dumazet 		frg_cnt++;
2837b2111724SEric Dumazet 		pfrag->offset += copy;
2838b2111724SEric Dumazet 		get_page(pfrag->page);
2839b2111724SEric Dumazet 
2840b2111724SEric Dumazet 		skb->truesize += copy;
2841b2111724SEric Dumazet 		atomic_add(copy, &sk->sk_wmem_alloc);
2842e89e9cf5SAnanda Raju 		skb->len += copy;
2843e89e9cf5SAnanda Raju 		skb->data_len += copy;
2844e89e9cf5SAnanda Raju 		offset += copy;
2845e89e9cf5SAnanda Raju 		length -= copy;
2846e89e9cf5SAnanda Raju 
2847e89e9cf5SAnanda Raju 	} while (length > 0);
2848e89e9cf5SAnanda Raju 
2849e89e9cf5SAnanda Raju 	return 0;
2850e89e9cf5SAnanda Raju }
2851b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
2852e89e9cf5SAnanda Raju 
2853cbb042f9SHerbert Xu /**
2854cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2855cbb042f9SHerbert Xu  *	@skb: buffer to update
2856cbb042f9SHerbert Xu  *	@len: length of data pulled
2857cbb042f9SHerbert Xu  *
2858cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2859fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
286084fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
286184fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
286284fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2863cbb042f9SHerbert Xu  */
2864cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2865cbb042f9SHerbert Xu {
2866cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2867cbb042f9SHerbert Xu 	skb->len -= len;
2868cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2869cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2870cbb042f9SHerbert Xu 	return skb->data += len;
2871cbb042f9SHerbert Xu }
2872f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2873f94691acSArnaldo Carvalho de Melo 
2874f4c50d99SHerbert Xu /**
2875f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2876df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
2877576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2878f4c50d99SHerbert Xu  *
2879f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
28804c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
28814c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2882f4c50d99SHerbert Xu  */
2883df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
2884df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
2885f4c50d99SHerbert Xu {
2886f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2887f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
28881a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2889df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2890df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
2891df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
28921fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
2893f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2894df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2895f4c50d99SHerbert Xu 	unsigned int headroom;
2896f4c50d99SHerbert Xu 	unsigned int len;
2897ec5f0615SPravin B Shelar 	__be16 proto;
2898ec5f0615SPravin B Shelar 	bool csum;
289904ed3e74SMichał Mirosław 	int sg = !!(features & NETIF_F_SG);
2900df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
2901f4c50d99SHerbert Xu 	int err = -ENOMEM;
2902f4c50d99SHerbert Xu 	int i = 0;
2903f4c50d99SHerbert Xu 	int pos;
290453d6471cSVlad Yasevich 	int dummy;
2905f4c50d99SHerbert Xu 
29065882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
290753d6471cSVlad Yasevich 	proto = skb_network_protocol(head_skb, &dummy);
2908ec5f0615SPravin B Shelar 	if (unlikely(!proto))
2909ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
2910ec5f0615SPravin B Shelar 
29117e2b10c1STom Herbert 	csum = !head_skb->encap_hdr_csum &&
29127e2b10c1STom Herbert 	    !!can_checksum_protocol(features, proto);
29137e2b10c1STom Herbert 
2914df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
2915df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
2916f4c50d99SHerbert Xu 
2917f4c50d99SHerbert Xu 	do {
2918f4c50d99SHerbert Xu 		struct sk_buff *nskb;
29198cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
2920c8884eddSHerbert Xu 		int hsize;
2921f4c50d99SHerbert Xu 		int size;
2922f4c50d99SHerbert Xu 
2923df5771ffSMichael S. Tsirkin 		len = head_skb->len - offset;
2924f4c50d99SHerbert Xu 		if (len > mss)
2925f4c50d99SHerbert Xu 			len = mss;
2926f4c50d99SHerbert Xu 
2927df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
2928f4c50d99SHerbert Xu 		if (hsize < 0)
2929f4c50d99SHerbert Xu 			hsize = 0;
2930c8884eddSHerbert Xu 		if (hsize > len || !sg)
2931c8884eddSHerbert Xu 			hsize = len;
2932f4c50d99SHerbert Xu 
29331a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
29341a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
29351a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
293689319d38SHerbert Xu 
29379d8506ccSHerbert Xu 			i = 0;
29381a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
29391a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
29401fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
29411a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
29429d8506ccSHerbert Xu 
29439d8506ccSHerbert Xu 			while (pos < offset + len) {
29449d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
29459d8506ccSHerbert Xu 
29464e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
29479d8506ccSHerbert Xu 				if (pos + size > offset + len)
29489d8506ccSHerbert Xu 					break;
29499d8506ccSHerbert Xu 
29509d8506ccSHerbert Xu 				i++;
29519d8506ccSHerbert Xu 				pos += size;
29524e1beba1SMichael S. Tsirkin 				frag++;
29539d8506ccSHerbert Xu 			}
29549d8506ccSHerbert Xu 
29551a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
29561a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
295789319d38SHerbert Xu 
2958f4c50d99SHerbert Xu 			if (unlikely(!nskb))
2959f4c50d99SHerbert Xu 				goto err;
2960f4c50d99SHerbert Xu 
29619d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
29629d8506ccSHerbert Xu 				kfree_skb(nskb);
29639d8506ccSHerbert Xu 				goto err;
29649d8506ccSHerbert Xu 			}
29659d8506ccSHerbert Xu 
2966ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
296789319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
296889319d38SHerbert Xu 				kfree_skb(nskb);
296989319d38SHerbert Xu 				goto err;
297089319d38SHerbert Xu 			}
297189319d38SHerbert Xu 
2972ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
297389319d38SHerbert Xu 			skb_release_head_state(nskb);
297489319d38SHerbert Xu 			__skb_push(nskb, doffset);
297589319d38SHerbert Xu 		} else {
2976c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
2977df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
2978c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
297989319d38SHerbert Xu 
298089319d38SHerbert Xu 			if (unlikely(!nskb))
298189319d38SHerbert Xu 				goto err;
298289319d38SHerbert Xu 
298389319d38SHerbert Xu 			skb_reserve(nskb, headroom);
298489319d38SHerbert Xu 			__skb_put(nskb, doffset);
298589319d38SHerbert Xu 		}
298689319d38SHerbert Xu 
2987f4c50d99SHerbert Xu 		if (segs)
2988f4c50d99SHerbert Xu 			tail->next = nskb;
2989f4c50d99SHerbert Xu 		else
2990f4c50d99SHerbert Xu 			segs = nskb;
2991f4c50d99SHerbert Xu 		tail = nskb;
2992f4c50d99SHerbert Xu 
2993df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
2994f4c50d99SHerbert Xu 
2995030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
2996fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
299768c33163SPravin B Shelar 
2998df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
299968c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
300068c33163SPravin B Shelar 						 doffset + tnl_hlen);
300189319d38SHerbert Xu 
30029d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
30031cdbcb79SSimon Horman 			goto perform_csum_check;
300489319d38SHerbert Xu 
3005f4c50d99SHerbert Xu 		if (!sg) {
30066f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
3007df5771ffSMichael S. Tsirkin 			nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3008f4c50d99SHerbert Xu 							    skb_put(nskb, len),
3009f4c50d99SHerbert Xu 							    len, 0);
30107e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
3011de843723STom Herbert 			    skb_headroom(nskb) + doffset;
3012f4c50d99SHerbert Xu 			continue;
3013f4c50d99SHerbert Xu 		}
3014f4c50d99SHerbert Xu 
30158cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3016f4c50d99SHerbert Xu 
3017df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3018d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
3019f4c50d99SHerbert Xu 
3020df5771ffSMichael S. Tsirkin 		skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3021df5771ffSMichael S. Tsirkin 			SKBTX_SHARED_FRAG;
3022cef401deSEric Dumazet 
30239d8506ccSHerbert Xu 		while (pos < offset + len) {
30249d8506ccSHerbert Xu 			if (i >= nfrags) {
30251a4cedafSMichael S. Tsirkin 				BUG_ON(skb_headlen(list_skb));
30269d8506ccSHerbert Xu 
30279d8506ccSHerbert Xu 				i = 0;
30281a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
30291a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
30301fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
30319d8506ccSHerbert Xu 
30329d8506ccSHerbert Xu 				BUG_ON(!nfrags);
30339d8506ccSHerbert Xu 
30341a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
30359d8506ccSHerbert Xu 			}
30369d8506ccSHerbert Xu 
30379d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
30389d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
30399d8506ccSHerbert Xu 				net_warn_ratelimited(
30409d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
30419d8506ccSHerbert Xu 					pos, mss);
30429d8506ccSHerbert Xu 				goto err;
30439d8506ccSHerbert Xu 			}
30449d8506ccSHerbert Xu 
30451fd819ecSMichael S. Tsirkin 			if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
30461fd819ecSMichael S. Tsirkin 				goto err;
30471fd819ecSMichael S. Tsirkin 
30484e1beba1SMichael S. Tsirkin 			*nskb_frag = *frag;
30498cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
30508cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
3051f4c50d99SHerbert Xu 
3052f4c50d99SHerbert Xu 			if (pos < offset) {
30538cb19905SMichael S. Tsirkin 				nskb_frag->page_offset += offset - pos;
30548cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
3055f4c50d99SHerbert Xu 			}
3056f4c50d99SHerbert Xu 
305789319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
3058f4c50d99SHerbert Xu 
3059f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
3060f4c50d99SHerbert Xu 				i++;
30614e1beba1SMichael S. Tsirkin 				frag++;
3062f4c50d99SHerbert Xu 				pos += size;
3063f4c50d99SHerbert Xu 			} else {
30648cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
306589319d38SHerbert Xu 				goto skip_fraglist;
3066f4c50d99SHerbert Xu 			}
3067f4c50d99SHerbert Xu 
30688cb19905SMichael S. Tsirkin 			nskb_frag++;
3069f4c50d99SHerbert Xu 		}
3070f4c50d99SHerbert Xu 
307189319d38SHerbert Xu skip_fraglist:
3072f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
3073f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
3074f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
3075ec5f0615SPravin B Shelar 
30761cdbcb79SSimon Horman perform_csum_check:
3077ec5f0615SPravin B Shelar 		if (!csum) {
3078ec5f0615SPravin B Shelar 			nskb->csum = skb_checksum(nskb, doffset,
3079ec5f0615SPravin B Shelar 						  nskb->len - doffset, 0);
3080ec5f0615SPravin B Shelar 			nskb->ip_summed = CHECKSUM_NONE;
30817e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
30827e2b10c1STom Herbert 			    skb_headroom(nskb) + doffset;
3083ec5f0615SPravin B Shelar 		}
3084df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
3085f4c50d99SHerbert Xu 
3086bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
3087bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
3088bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
3089bec3cfdcSEric Dumazet 	 */
3090bec3cfdcSEric Dumazet 	segs->prev = tail;
3091f4c50d99SHerbert Xu 	return segs;
3092f4c50d99SHerbert Xu 
3093f4c50d99SHerbert Xu err:
3094289dccbeSEric Dumazet 	kfree_skb_list(segs);
3095f4c50d99SHerbert Xu 	return ERR_PTR(err);
3096f4c50d99SHerbert Xu }
3097f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
3098f4c50d99SHerbert Xu 
309971d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
310071d93b39SHerbert Xu {
31018a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
310267147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
310367147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
31048a29111cSEric Dumazet 	struct sk_buff *nskb, *lp, *p = *head;
31058a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
3106715dc1f3SEric Dumazet 	unsigned int delta_truesize;
31078a29111cSEric Dumazet 	unsigned int headroom;
310871d93b39SHerbert Xu 
31098a29111cSEric Dumazet 	if (unlikely(p->len + len >= 65536))
311071d93b39SHerbert Xu 		return -E2BIG;
311171d93b39SHerbert Xu 
311229e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
31138a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
31148a29111cSEric Dumazet 
31158a29111cSEric Dumazet 	if (headlen <= offset) {
311642da6994SHerbert Xu 		skb_frag_t *frag;
311766e92fcfSHerbert Xu 		skb_frag_t *frag2;
31189aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
31199aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
312042da6994SHerbert Xu 
312166e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
31228a29111cSEric Dumazet 			goto merge;
312381705ad1SHerbert Xu 
31248a29111cSEric Dumazet 		offset -= headlen;
31259aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
31269aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
3127f5572068SHerbert Xu 
31289aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
31299aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
313066e92fcfSHerbert Xu 		do {
313166e92fcfSHerbert Xu 			*--frag = *--frag2;
313266e92fcfSHerbert Xu 		} while (--i);
313366e92fcfSHerbert Xu 
313466e92fcfSHerbert Xu 		frag->page_offset += offset;
31359e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
313666e92fcfSHerbert Xu 
3137715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
3138ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
3139ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
3140715dc1f3SEric Dumazet 
3141f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
3142f5572068SHerbert Xu 		skb->len -= skb->data_len;
3143f5572068SHerbert Xu 		skb->data_len = 0;
3144f5572068SHerbert Xu 
3145715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
31465d38a079SHerbert Xu 		goto done;
3147d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
3148d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
3149d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
3150d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
3151d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
3152d7e8883cSEric Dumazet 		unsigned int first_offset;
3153d7e8883cSEric Dumazet 
3154d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
31558a29111cSEric Dumazet 			goto merge;
3156d7e8883cSEric Dumazet 
3157d7e8883cSEric Dumazet 		first_offset = skb->data -
3158d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
3159d7e8883cSEric Dumazet 			       offset;
3160d7e8883cSEric Dumazet 
3161d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3162d7e8883cSEric Dumazet 
3163d7e8883cSEric Dumazet 		frag->page.p	  = page;
3164d7e8883cSEric Dumazet 		frag->page_offset = first_offset;
3165d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
3166d7e8883cSEric Dumazet 
3167d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3168d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
3169d7e8883cSEric Dumazet 
3170715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3171d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3172d7e8883cSEric Dumazet 		goto done;
31738a29111cSEric Dumazet 	}
317473d3fe6dSEric Dumazet 	/* switch back to head shinfo */
317573d3fe6dSEric Dumazet 	pinfo = skb_shinfo(p);
317673d3fe6dSEric Dumazet 
31778a29111cSEric Dumazet 	if (pinfo->frag_list)
31788a29111cSEric Dumazet 		goto merge;
31798a29111cSEric Dumazet 	if (skb_gro_len(p) != pinfo->gso_size)
318069c0cab1SHerbert Xu 		return -E2BIG;
318171d93b39SHerbert Xu 
318271d93b39SHerbert Xu 	headroom = skb_headroom(p);
31833d3be433SEric Dumazet 	nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
318471d93b39SHerbert Xu 	if (unlikely(!nskb))
318571d93b39SHerbert Xu 		return -ENOMEM;
318671d93b39SHerbert Xu 
318771d93b39SHerbert Xu 	__copy_skb_header(nskb, p);
318871d93b39SHerbert Xu 	nskb->mac_len = p->mac_len;
318971d93b39SHerbert Xu 
319071d93b39SHerbert Xu 	skb_reserve(nskb, headroom);
319186911732SHerbert Xu 	__skb_put(nskb, skb_gro_offset(p));
319271d93b39SHerbert Xu 
319386911732SHerbert Xu 	skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
319471d93b39SHerbert Xu 	skb_set_network_header(nskb, skb_network_offset(p));
319571d93b39SHerbert Xu 	skb_set_transport_header(nskb, skb_transport_offset(p));
319671d93b39SHerbert Xu 
319786911732SHerbert Xu 	__skb_pull(p, skb_gro_offset(p));
319886911732SHerbert Xu 	memcpy(skb_mac_header(nskb), skb_mac_header(p),
319986911732SHerbert Xu 	       p->data - skb_mac_header(p));
320071d93b39SHerbert Xu 
320171d93b39SHerbert Xu 	skb_shinfo(nskb)->frag_list = p;
32029aaa156cSHerbert Xu 	skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3203622e0ca1SHerbert Xu 	pinfo->gso_size = 0;
3204f4a775d1SEric Dumazet 	__skb_header_release(p);
3205c3c7c254SEric Dumazet 	NAPI_GRO_CB(nskb)->last = p;
320671d93b39SHerbert Xu 
320771d93b39SHerbert Xu 	nskb->data_len += p->len;
3208de8261c2SEric Dumazet 	nskb->truesize += p->truesize;
320971d93b39SHerbert Xu 	nskb->len += p->len;
321071d93b39SHerbert Xu 
321171d93b39SHerbert Xu 	*head = nskb;
321271d93b39SHerbert Xu 	nskb->next = p->next;
321371d93b39SHerbert Xu 	p->next = NULL;
321471d93b39SHerbert Xu 
321571d93b39SHerbert Xu 	p = nskb;
321671d93b39SHerbert Xu 
321771d93b39SHerbert Xu merge:
3218715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
321967147ba9SHerbert Xu 	if (offset > headlen) {
3220d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
3221d1dc7abfSMichal Schmidt 
3222d1dc7abfSMichal Schmidt 		skbinfo->frags[0].page_offset += eat;
32239e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
3224d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
3225d1dc7abfSMichal Schmidt 		skb->len -= eat;
322667147ba9SHerbert Xu 		offset = headlen;
322756035022SHerbert Xu 	}
322856035022SHerbert Xu 
322967147ba9SHerbert Xu 	__skb_pull(skb, offset);
323056035022SHerbert Xu 
323129e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
32328a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
32338a29111cSEric Dumazet 	else
3234c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
3235c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
3236f4a775d1SEric Dumazet 	__skb_header_release(skb);
32378a29111cSEric Dumazet 	lp = p;
323871d93b39SHerbert Xu 
32395d38a079SHerbert Xu done:
32405d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
324137fe4732SHerbert Xu 	p->data_len += len;
3242715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
324337fe4732SHerbert Xu 	p->len += len;
32448a29111cSEric Dumazet 	if (lp != p) {
32458a29111cSEric Dumazet 		lp->data_len += len;
32468a29111cSEric Dumazet 		lp->truesize += delta_truesize;
32478a29111cSEric Dumazet 		lp->len += len;
32488a29111cSEric Dumazet 	}
324971d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
325071d93b39SHerbert Xu 	return 0;
325171d93b39SHerbert Xu }
325271d93b39SHerbert Xu 
32531da177e4SLinus Torvalds void __init skb_init(void)
32541da177e4SLinus Torvalds {
32551da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
32561da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
32571da177e4SLinus Torvalds 					      0,
3258e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
325920c2df83SPaul Mundt 					      NULL);
3260d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3261d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
3262d179cd12SDavid S. Miller 						0,
3263e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
326420c2df83SPaul Mundt 						NULL);
32651da177e4SLinus Torvalds }
32661da177e4SLinus Torvalds 
3267716ea3a7SDavid Howells /**
3268716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3269716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
3270716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
3271716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
3272716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
3273716ea3a7SDavid Howells  *
3274716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
3275716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
3276716ea3a7SDavid Howells  */
327751c739d1SDavid S. Miller static int
327851c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3279716ea3a7SDavid Howells {
32801a028e50SDavid S. Miller 	int start = skb_headlen(skb);
32811a028e50SDavid S. Miller 	int i, copy = start - offset;
3282fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
3283716ea3a7SDavid Howells 	int elt = 0;
3284716ea3a7SDavid Howells 
3285716ea3a7SDavid Howells 	if (copy > 0) {
3286716ea3a7SDavid Howells 		if (copy > len)
3287716ea3a7SDavid Howells 			copy = len;
3288642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
3289716ea3a7SDavid Howells 		elt++;
3290716ea3a7SDavid Howells 		if ((len -= copy) == 0)
3291716ea3a7SDavid Howells 			return elt;
3292716ea3a7SDavid Howells 		offset += copy;
3293716ea3a7SDavid Howells 	}
3294716ea3a7SDavid Howells 
3295716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
32961a028e50SDavid S. Miller 		int end;
3297716ea3a7SDavid Howells 
3298547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
32991a028e50SDavid S. Miller 
33009e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3301716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3302716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3303716ea3a7SDavid Howells 
3304716ea3a7SDavid Howells 			if (copy > len)
3305716ea3a7SDavid Howells 				copy = len;
3306ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3307642f1490SJens Axboe 					frag->page_offset+offset-start);
3308716ea3a7SDavid Howells 			elt++;
3309716ea3a7SDavid Howells 			if (!(len -= copy))
3310716ea3a7SDavid Howells 				return elt;
3311716ea3a7SDavid Howells 			offset += copy;
3312716ea3a7SDavid Howells 		}
33131a028e50SDavid S. Miller 		start = end;
3314716ea3a7SDavid Howells 	}
3315716ea3a7SDavid Howells 
3316fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
33171a028e50SDavid S. Miller 		int end;
3318716ea3a7SDavid Howells 
3319547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
33201a028e50SDavid S. Miller 
3321fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
3322716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3323716ea3a7SDavid Howells 			if (copy > len)
3324716ea3a7SDavid Howells 				copy = len;
3325fbb398a8SDavid S. Miller 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
332651c739d1SDavid S. Miller 					      copy);
3327716ea3a7SDavid Howells 			if ((len -= copy) == 0)
3328716ea3a7SDavid Howells 				return elt;
3329716ea3a7SDavid Howells 			offset += copy;
3330716ea3a7SDavid Howells 		}
33311a028e50SDavid S. Miller 		start = end;
3332716ea3a7SDavid Howells 	}
3333716ea3a7SDavid Howells 	BUG_ON(len);
3334716ea3a7SDavid Howells 	return elt;
3335716ea3a7SDavid Howells }
3336716ea3a7SDavid Howells 
333725a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
333825a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
333925a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
334025a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
334125a91d8dSFan Du  *
334225a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
334325a91d8dSFan Du  * 1. sg_init_table
334425a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
334525a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
334625a91d8dSFan Du  *
334725a91d8dSFan Du  * This is equivalent to:
334825a91d8dSFan Du  * 1. sg_init_table
334925a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
335025a91d8dSFan Du  * 3. sg_unmark_end
335125a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
335225a91d8dSFan Du  *
335325a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
335425a91d8dSFan Du  * is more preferable.
335525a91d8dSFan Du  */
335625a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
335725a91d8dSFan Du 			int offset, int len)
335825a91d8dSFan Du {
335925a91d8dSFan Du 	return __skb_to_sgvec(skb, sg, offset, len);
336025a91d8dSFan Du }
336125a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
336225a91d8dSFan Du 
336351c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
336451c739d1SDavid S. Miller {
336551c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
336651c739d1SDavid S. Miller 
3367c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
336851c739d1SDavid S. Miller 
336951c739d1SDavid S. Miller 	return nsg;
337051c739d1SDavid S. Miller }
3371b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
337251c739d1SDavid S. Miller 
3373716ea3a7SDavid Howells /**
3374716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
3375716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
3376716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
3377716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
3378716ea3a7SDavid Howells  *
3379716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
3380716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
3381716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
3382716ea3a7SDavid Howells  *
3383716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
3384716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
3385716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
3386716ea3a7SDavid Howells  *
3387716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
3388716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
3389716ea3a7SDavid Howells  */
3390716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3391716ea3a7SDavid Howells {
3392716ea3a7SDavid Howells 	int copyflag;
3393716ea3a7SDavid Howells 	int elt;
3394716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
3395716ea3a7SDavid Howells 
3396716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
3397716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
3398716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
3399716ea3a7SDavid Howells 	 */
3400716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3401716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3402716ea3a7SDavid Howells 		return -ENOMEM;
3403716ea3a7SDavid Howells 
3404716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
340521dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
3406716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
3407716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
3408716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
3409716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
3410716ea3a7SDavid Howells 
3411716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
3412716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3413716ea3a7SDavid Howells 			return -ENOMEM;
3414716ea3a7SDavid Howells 
3415716ea3a7SDavid Howells 		/* Voila! */
3416716ea3a7SDavid Howells 		*trailer = skb;
3417716ea3a7SDavid Howells 		return 1;
3418716ea3a7SDavid Howells 	}
3419716ea3a7SDavid Howells 
3420716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
3421716ea3a7SDavid Howells 
3422716ea3a7SDavid Howells 	elt = 1;
3423716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
3424716ea3a7SDavid Howells 	copyflag = 0;
3425716ea3a7SDavid Howells 
3426716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
3427716ea3a7SDavid Howells 		int ntail = 0;
3428716ea3a7SDavid Howells 
3429716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
3430716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
3431716ea3a7SDavid Howells 		 * after it. */
3432716ea3a7SDavid Howells 
3433716ea3a7SDavid Howells 		if (skb_shared(skb1))
3434716ea3a7SDavid Howells 			copyflag = 1;
3435716ea3a7SDavid Howells 
3436716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
3437716ea3a7SDavid Howells 
3438716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
3439716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
344021dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
3441716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
3442716ea3a7SDavid Howells 				ntail = tailbits + 128;
3443716ea3a7SDavid Howells 		}
3444716ea3a7SDavid Howells 
3445716ea3a7SDavid Howells 		if (copyflag ||
3446716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
3447716ea3a7SDavid Howells 		    ntail ||
3448716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
344921dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
3450716ea3a7SDavid Howells 			struct sk_buff *skb2;
3451716ea3a7SDavid Howells 
3452716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
3453716ea3a7SDavid Howells 			if (ntail == 0)
3454716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
3455716ea3a7SDavid Howells 			else
3456716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
3457716ea3a7SDavid Howells 						       skb_headroom(skb1),
3458716ea3a7SDavid Howells 						       ntail,
3459716ea3a7SDavid Howells 						       GFP_ATOMIC);
3460716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
3461716ea3a7SDavid Howells 				return -ENOMEM;
3462716ea3a7SDavid Howells 
3463716ea3a7SDavid Howells 			if (skb1->sk)
3464716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
3465716ea3a7SDavid Howells 
3466716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
3467716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
3468716ea3a7SDavid Howells 
3469716ea3a7SDavid Howells 			skb2->next = skb1->next;
3470716ea3a7SDavid Howells 			*skb_p = skb2;
3471716ea3a7SDavid Howells 			kfree_skb(skb1);
3472716ea3a7SDavid Howells 			skb1 = skb2;
3473716ea3a7SDavid Howells 		}
3474716ea3a7SDavid Howells 		elt++;
3475716ea3a7SDavid Howells 		*trailer = skb1;
3476716ea3a7SDavid Howells 		skb_p = &skb1->next;
3477716ea3a7SDavid Howells 	}
3478716ea3a7SDavid Howells 
3479716ea3a7SDavid Howells 	return elt;
3480716ea3a7SDavid Howells }
3481b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
3482716ea3a7SDavid Howells 
3483b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
3484b1faf566SEric Dumazet {
3485b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
3486b1faf566SEric Dumazet 
3487b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3488b1faf566SEric Dumazet }
3489b1faf566SEric Dumazet 
3490b1faf566SEric Dumazet /*
3491b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3492b1faf566SEric Dumazet  */
3493b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3494b1faf566SEric Dumazet {
3495b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
349695c96174SEric Dumazet 	    (unsigned int)sk->sk_rcvbuf)
3497b1faf566SEric Dumazet 		return -ENOMEM;
3498b1faf566SEric Dumazet 
3499b1faf566SEric Dumazet 	skb_orphan(skb);
3500b1faf566SEric Dumazet 	skb->sk = sk;
3501b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
3502b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3503b1faf566SEric Dumazet 
3504abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
3505abb57ea4SEric Dumazet 	skb_dst_force(skb);
3506abb57ea4SEric Dumazet 
3507b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
3508b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
3509676d2369SDavid S. Miller 		sk->sk_data_ready(sk);
3510b1faf566SEric Dumazet 	return 0;
3511b1faf566SEric Dumazet }
3512b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
3513b1faf566SEric Dumazet 
3514364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3515364a9e93SWillem de Bruijn {
3516364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
3517364a9e93SWillem de Bruijn 	struct sk_buff *skb, *skb_next;
3518364a9e93SWillem de Bruijn 	int err = 0;
3519364a9e93SWillem de Bruijn 
3520364a9e93SWillem de Bruijn 	spin_lock_bh(&q->lock);
3521364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
3522364a9e93SWillem de Bruijn 	if (skb && (skb_next = skb_peek(q)))
3523364a9e93SWillem de Bruijn 		err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3524364a9e93SWillem de Bruijn 	spin_unlock_bh(&q->lock);
3525364a9e93SWillem de Bruijn 
3526364a9e93SWillem de Bruijn 	sk->sk_err = err;
3527364a9e93SWillem de Bruijn 	if (err)
3528364a9e93SWillem de Bruijn 		sk->sk_error_report(sk);
3529364a9e93SWillem de Bruijn 
3530364a9e93SWillem de Bruijn 	return skb;
3531364a9e93SWillem de Bruijn }
3532364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
3533364a9e93SWillem de Bruijn 
3534cab41c47SAlexander Duyck /**
3535cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
3536cab41c47SAlexander Duyck  * @skb: the skb to clone
3537cab41c47SAlexander Duyck  *
3538cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
3539cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
3540cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
3541cab41c47SAlexander Duyck  *
3542cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
3543cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
3544cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
3545cab41c47SAlexander Duyck  * the sk_error_queue.
3546cab41c47SAlexander Duyck  */
354762bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
354862bccb8cSAlexander Duyck {
354962bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
355062bccb8cSAlexander Duyck 	struct sk_buff *clone;
355162bccb8cSAlexander Duyck 
355262bccb8cSAlexander Duyck 	if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
355362bccb8cSAlexander Duyck 		return NULL;
355462bccb8cSAlexander Duyck 
355562bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
355662bccb8cSAlexander Duyck 	if (!clone) {
355762bccb8cSAlexander Duyck 		sock_put(sk);
355862bccb8cSAlexander Duyck 		return NULL;
355962bccb8cSAlexander Duyck 	}
356062bccb8cSAlexander Duyck 
356162bccb8cSAlexander Duyck 	clone->sk = sk;
356262bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
356362bccb8cSAlexander Duyck 
356462bccb8cSAlexander Duyck 	return clone;
356562bccb8cSAlexander Duyck }
356662bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
356762bccb8cSAlexander Duyck 
356837846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
356937846ef0SAlexander Duyck 					struct sock *sk,
357037846ef0SAlexander Duyck 					int tstype)
3571ac45f602SPatrick Ohly {
3572ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
3573ac45f602SPatrick Ohly 	int err;
3574ac45f602SPatrick Ohly 
3575ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
3576ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
3577ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
3578ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3579e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
35804ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
358109c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
35824ed2d765SWillem de Bruijn 		if (sk->sk_protocol == IPPROTO_TCP)
35834ed2d765SWillem de Bruijn 			serr->ee.ee_data -= sk->sk_tskey;
35844ed2d765SWillem de Bruijn 	}
358529030374SEric Dumazet 
3586ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
358729030374SEric Dumazet 
3588ac45f602SPatrick Ohly 	if (err)
3589ac45f602SPatrick Ohly 		kfree_skb(skb);
3590ac45f602SPatrick Ohly }
359137846ef0SAlexander Duyck 
359237846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
359337846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
359437846ef0SAlexander Duyck {
359537846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
359637846ef0SAlexander Duyck 
359762bccb8cSAlexander Duyck 	/* take a reference to prevent skb_orphan() from freeing the socket */
359862bccb8cSAlexander Duyck 	sock_hold(sk);
359937846ef0SAlexander Duyck 
360037846ef0SAlexander Duyck 	*skb_hwtstamps(skb) = *hwtstamps;
360137846ef0SAlexander Duyck 	__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
360237846ef0SAlexander Duyck 
360337846ef0SAlexander Duyck 	sock_put(sk);
360437846ef0SAlexander Duyck }
360537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
360637846ef0SAlexander Duyck 
360737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
360837846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
360937846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
361037846ef0SAlexander Duyck {
361137846ef0SAlexander Duyck 	struct sk_buff *skb;
361237846ef0SAlexander Duyck 
361337846ef0SAlexander Duyck 	if (!sk)
361437846ef0SAlexander Duyck 		return;
361537846ef0SAlexander Duyck 
361637846ef0SAlexander Duyck 	if (hwtstamps)
361737846ef0SAlexander Duyck 		*skb_hwtstamps(orig_skb) = *hwtstamps;
361837846ef0SAlexander Duyck 	else
361937846ef0SAlexander Duyck 		orig_skb->tstamp = ktime_get_real();
362037846ef0SAlexander Duyck 
362137846ef0SAlexander Duyck 	skb = skb_clone(orig_skb, GFP_ATOMIC);
362237846ef0SAlexander Duyck 	if (!skb)
362337846ef0SAlexander Duyck 		return;
362437846ef0SAlexander Duyck 
362537846ef0SAlexander Duyck 	__skb_complete_tx_timestamp(skb, sk, tstype);
362637846ef0SAlexander Duyck }
3627e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3628e7fd2885SWillem de Bruijn 
3629e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
3630e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
3631e7fd2885SWillem de Bruijn {
3632e7fd2885SWillem de Bruijn 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3633e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
3634e7fd2885SWillem de Bruijn }
3635ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3636ac45f602SPatrick Ohly 
36376e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
36386e3e939fSJohannes Berg {
36396e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
36406e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
36416e3e939fSJohannes Berg 	int err;
36426e3e939fSJohannes Berg 
36436e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
36446e3e939fSJohannes Berg 	skb->wifi_acked = acked;
36456e3e939fSJohannes Berg 
36466e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
36476e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
36486e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
36496e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
36506e3e939fSJohannes Berg 
3651bf7fa551SAlexander Duyck 	/* take a reference to prevent skb_orphan() from freeing the socket */
3652bf7fa551SAlexander Duyck 	sock_hold(sk);
3653bf7fa551SAlexander Duyck 
36546e3e939fSJohannes Berg 	err = sock_queue_err_skb(sk, skb);
36556e3e939fSJohannes Berg 	if (err)
36566e3e939fSJohannes Berg 		kfree_skb(skb);
3657bf7fa551SAlexander Duyck 
3658bf7fa551SAlexander Duyck 	sock_put(sk);
36596e3e939fSJohannes Berg }
36606e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
36616e3e939fSJohannes Berg 
3662ac45f602SPatrick Ohly 
3663f35d9d8aSRusty Russell /**
3664f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3665f35d9d8aSRusty Russell  * @skb: the skb to set
3666f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3667f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3668f35d9d8aSRusty Russell  *
3669f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3670f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3671f35d9d8aSRusty Russell  *
3672f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3673f35d9d8aSRusty Russell  * returns false you should drop the packet.
3674f35d9d8aSRusty Russell  */
3675f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3676f35d9d8aSRusty Russell {
36775ff8dda3SHerbert Xu 	if (unlikely(start > skb_headlen(skb)) ||
36785ff8dda3SHerbert Xu 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3679e87cc472SJoe Perches 		net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
36805ff8dda3SHerbert Xu 				     start, off, skb_headlen(skb));
3681f35d9d8aSRusty Russell 		return false;
3682f35d9d8aSRusty Russell 	}
3683f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3684f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3685f35d9d8aSRusty Russell 	skb->csum_offset = off;
3686e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
3687f35d9d8aSRusty Russell 	return true;
3688f35d9d8aSRusty Russell }
3689b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3690f35d9d8aSRusty Russell 
3691ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3692ed1f50c3SPaul Durrant 			       unsigned int max)
3693ed1f50c3SPaul Durrant {
3694ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
3695ed1f50c3SPaul Durrant 		return 0;
3696ed1f50c3SPaul Durrant 
3697ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
3698ed1f50c3SPaul Durrant 	 * won't need to do it again.
3699ed1f50c3SPaul Durrant 	 */
3700ed1f50c3SPaul Durrant 	if (max > skb->len)
3701ed1f50c3SPaul Durrant 		max = skb->len;
3702ed1f50c3SPaul Durrant 
3703ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3704ed1f50c3SPaul Durrant 		return -ENOMEM;
3705ed1f50c3SPaul Durrant 
3706ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
3707ed1f50c3SPaul Durrant 		return -EPROTO;
3708ed1f50c3SPaul Durrant 
3709ed1f50c3SPaul Durrant 	return 0;
3710ed1f50c3SPaul Durrant }
3711ed1f50c3SPaul Durrant 
3712f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
3713f9708b43SJan Beulich 
3714f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3715f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
3716f9708b43SJan Beulich 				      unsigned int off)
3717f9708b43SJan Beulich {
3718f9708b43SJan Beulich 	switch (proto) {
3719f9708b43SJan Beulich 		int err;
3720f9708b43SJan Beulich 
3721f9708b43SJan Beulich 	case IPPROTO_TCP:
3722f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3723f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
3724f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3725f9708b43SJan Beulich 						  offsetof(struct tcphdr,
3726f9708b43SJan Beulich 							   check)))
3727f9708b43SJan Beulich 			err = -EPROTO;
3728f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3729f9708b43SJan Beulich 
3730f9708b43SJan Beulich 	case IPPROTO_UDP:
3731f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3732f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
3733f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3734f9708b43SJan Beulich 						  offsetof(struct udphdr,
3735f9708b43SJan Beulich 							   check)))
3736f9708b43SJan Beulich 			err = -EPROTO;
3737f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3738f9708b43SJan Beulich 	}
3739f9708b43SJan Beulich 
3740f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
3741f9708b43SJan Beulich }
3742f9708b43SJan Beulich 
3743ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3744ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
3745ed1f50c3SPaul Durrant  */
3746ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
3747ed1f50c3SPaul Durrant 
3748f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3749ed1f50c3SPaul Durrant {
3750ed1f50c3SPaul Durrant 	unsigned int off;
3751ed1f50c3SPaul Durrant 	bool fragment;
3752f9708b43SJan Beulich 	__sum16 *csum;
3753ed1f50c3SPaul Durrant 	int err;
3754ed1f50c3SPaul Durrant 
3755ed1f50c3SPaul Durrant 	fragment = false;
3756ed1f50c3SPaul Durrant 
3757ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
3758ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
3759ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
3760ed1f50c3SPaul Durrant 	if (err < 0)
3761ed1f50c3SPaul Durrant 		goto out;
3762ed1f50c3SPaul Durrant 
3763ed1f50c3SPaul Durrant 	if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3764ed1f50c3SPaul Durrant 		fragment = true;
3765ed1f50c3SPaul Durrant 
3766ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
3767ed1f50c3SPaul Durrant 
3768ed1f50c3SPaul Durrant 	err = -EPROTO;
3769ed1f50c3SPaul Durrant 
3770ed1f50c3SPaul Durrant 	if (fragment)
3771ed1f50c3SPaul Durrant 		goto out;
3772ed1f50c3SPaul Durrant 
3773f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3774f9708b43SJan Beulich 	if (IS_ERR(csum))
3775f9708b43SJan Beulich 		return PTR_ERR(csum);
3776ed1f50c3SPaul Durrant 
3777ed1f50c3SPaul Durrant 	if (recalculate)
3778f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3779ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
3780ed1f50c3SPaul Durrant 					   skb->len - off,
3781f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
3782ed1f50c3SPaul Durrant 	err = 0;
3783ed1f50c3SPaul Durrant 
3784ed1f50c3SPaul Durrant out:
3785ed1f50c3SPaul Durrant 	return err;
3786ed1f50c3SPaul Durrant }
3787ed1f50c3SPaul Durrant 
3788ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3789ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
3790ed1f50c3SPaul Durrant  */
3791ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
3792ed1f50c3SPaul Durrant 
3793ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
3794ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
3795ed1f50c3SPaul Durrant 
3796ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3797ed1f50c3SPaul Durrant {
3798ed1f50c3SPaul Durrant 	int err;
3799ed1f50c3SPaul Durrant 	u8 nexthdr;
3800ed1f50c3SPaul Durrant 	unsigned int off;
3801ed1f50c3SPaul Durrant 	unsigned int len;
3802ed1f50c3SPaul Durrant 	bool fragment;
3803ed1f50c3SPaul Durrant 	bool done;
3804f9708b43SJan Beulich 	__sum16 *csum;
3805ed1f50c3SPaul Durrant 
3806ed1f50c3SPaul Durrant 	fragment = false;
3807ed1f50c3SPaul Durrant 	done = false;
3808ed1f50c3SPaul Durrant 
3809ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
3810ed1f50c3SPaul Durrant 
3811ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3812ed1f50c3SPaul Durrant 	if (err < 0)
3813ed1f50c3SPaul Durrant 		goto out;
3814ed1f50c3SPaul Durrant 
3815ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
3816ed1f50c3SPaul Durrant 
3817ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3818ed1f50c3SPaul Durrant 	while (off <= len && !done) {
3819ed1f50c3SPaul Durrant 		switch (nexthdr) {
3820ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
3821ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
3822ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
3823ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
3824ed1f50c3SPaul Durrant 
3825ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3826ed1f50c3SPaul Durrant 						  off +
3827ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
3828ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3829ed1f50c3SPaul Durrant 			if (err < 0)
3830ed1f50c3SPaul Durrant 				goto out;
3831ed1f50c3SPaul Durrant 
3832ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3833ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3834ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
3835ed1f50c3SPaul Durrant 			break;
3836ed1f50c3SPaul Durrant 		}
3837ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
3838ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
3839ed1f50c3SPaul Durrant 
3840ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3841ed1f50c3SPaul Durrant 						  off +
3842ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
3843ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3844ed1f50c3SPaul Durrant 			if (err < 0)
3845ed1f50c3SPaul Durrant 				goto out;
3846ed1f50c3SPaul Durrant 
3847ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3848ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3849ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
3850ed1f50c3SPaul Durrant 			break;
3851ed1f50c3SPaul Durrant 		}
3852ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
3853ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
3854ed1f50c3SPaul Durrant 
3855ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3856ed1f50c3SPaul Durrant 						  off +
3857ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
3858ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3859ed1f50c3SPaul Durrant 			if (err < 0)
3860ed1f50c3SPaul Durrant 				goto out;
3861ed1f50c3SPaul Durrant 
3862ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
3863ed1f50c3SPaul Durrant 
3864ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3865ed1f50c3SPaul Durrant 				fragment = true;
3866ed1f50c3SPaul Durrant 
3867ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3868ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
3869ed1f50c3SPaul Durrant 			break;
3870ed1f50c3SPaul Durrant 		}
3871ed1f50c3SPaul Durrant 		default:
3872ed1f50c3SPaul Durrant 			done = true;
3873ed1f50c3SPaul Durrant 			break;
3874ed1f50c3SPaul Durrant 		}
3875ed1f50c3SPaul Durrant 	}
3876ed1f50c3SPaul Durrant 
3877ed1f50c3SPaul Durrant 	err = -EPROTO;
3878ed1f50c3SPaul Durrant 
3879ed1f50c3SPaul Durrant 	if (!done || fragment)
3880ed1f50c3SPaul Durrant 		goto out;
3881ed1f50c3SPaul Durrant 
3882f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
3883f9708b43SJan Beulich 	if (IS_ERR(csum))
3884f9708b43SJan Beulich 		return PTR_ERR(csum);
3885ed1f50c3SPaul Durrant 
3886ed1f50c3SPaul Durrant 	if (recalculate)
3887f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3888ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
3889f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
3890ed1f50c3SPaul Durrant 	err = 0;
3891ed1f50c3SPaul Durrant 
3892ed1f50c3SPaul Durrant out:
3893ed1f50c3SPaul Durrant 	return err;
3894ed1f50c3SPaul Durrant }
3895ed1f50c3SPaul Durrant 
3896ed1f50c3SPaul Durrant /**
3897ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
3898ed1f50c3SPaul Durrant  * @skb: the skb to set up
3899ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
3900ed1f50c3SPaul Durrant  */
3901ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3902ed1f50c3SPaul Durrant {
3903ed1f50c3SPaul Durrant 	int err;
3904ed1f50c3SPaul Durrant 
3905ed1f50c3SPaul Durrant 	switch (skb->protocol) {
3906ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
3907f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
3908ed1f50c3SPaul Durrant 		break;
3909ed1f50c3SPaul Durrant 
3910ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
3911ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
3912ed1f50c3SPaul Durrant 		break;
3913ed1f50c3SPaul Durrant 
3914ed1f50c3SPaul Durrant 	default:
3915ed1f50c3SPaul Durrant 		err = -EPROTO;
3916ed1f50c3SPaul Durrant 		break;
3917ed1f50c3SPaul Durrant 	}
3918ed1f50c3SPaul Durrant 
3919ed1f50c3SPaul Durrant 	return err;
3920ed1f50c3SPaul Durrant }
3921ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
3922ed1f50c3SPaul Durrant 
39234497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
39244497b076SBen Hutchings {
3925e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3926e87cc472SJoe Perches 			     skb->dev->name);
39274497b076SBen Hutchings }
39284497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3929bad43ca8SEric Dumazet 
3930bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3931bad43ca8SEric Dumazet {
39323d861f66SEric Dumazet 	if (head_stolen) {
39333d861f66SEric Dumazet 		skb_release_head_state(skb);
3934bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
39353d861f66SEric Dumazet 	} else {
3936bad43ca8SEric Dumazet 		__kfree_skb(skb);
3937bad43ca8SEric Dumazet 	}
39383d861f66SEric Dumazet }
3939bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
3940bad43ca8SEric Dumazet 
3941bad43ca8SEric Dumazet /**
3942bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
3943bad43ca8SEric Dumazet  * @to: prior buffer
3944bad43ca8SEric Dumazet  * @from: buffer to add
3945bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
3946c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
3947bad43ca8SEric Dumazet  */
3948bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3949bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
3950bad43ca8SEric Dumazet {
3951bad43ca8SEric Dumazet 	int i, delta, len = from->len;
3952bad43ca8SEric Dumazet 
3953bad43ca8SEric Dumazet 	*fragstolen = false;
3954bad43ca8SEric Dumazet 
3955bad43ca8SEric Dumazet 	if (skb_cloned(to))
3956bad43ca8SEric Dumazet 		return false;
3957bad43ca8SEric Dumazet 
3958bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
3959e93a0435SEric Dumazet 		if (len)
3960bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
3961bad43ca8SEric Dumazet 		*delta_truesize = 0;
3962bad43ca8SEric Dumazet 		return true;
3963bad43ca8SEric Dumazet 	}
3964bad43ca8SEric Dumazet 
3965bad43ca8SEric Dumazet 	if (skb_has_frag_list(to) || skb_has_frag_list(from))
3966bad43ca8SEric Dumazet 		return false;
3967bad43ca8SEric Dumazet 
3968bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
3969bad43ca8SEric Dumazet 		struct page *page;
3970bad43ca8SEric Dumazet 		unsigned int offset;
3971bad43ca8SEric Dumazet 
3972bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
3973bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
3974bad43ca8SEric Dumazet 			return false;
3975bad43ca8SEric Dumazet 
3976bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
3977bad43ca8SEric Dumazet 			return false;
3978bad43ca8SEric Dumazet 
3979bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3980bad43ca8SEric Dumazet 
3981bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
3982bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
3983bad43ca8SEric Dumazet 
3984bad43ca8SEric Dumazet 		skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
3985bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
3986bad43ca8SEric Dumazet 		*fragstolen = true;
3987bad43ca8SEric Dumazet 	} else {
3988bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
3989bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
3990bad43ca8SEric Dumazet 			return false;
3991bad43ca8SEric Dumazet 
3992f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
3993bad43ca8SEric Dumazet 	}
3994bad43ca8SEric Dumazet 
3995bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
3996bad43ca8SEric Dumazet 
3997bad43ca8SEric Dumazet 	memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
3998bad43ca8SEric Dumazet 	       skb_shinfo(from)->frags,
3999bad43ca8SEric Dumazet 	       skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4000bad43ca8SEric Dumazet 	skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4001bad43ca8SEric Dumazet 
4002bad43ca8SEric Dumazet 	if (!skb_cloned(from))
4003bad43ca8SEric Dumazet 		skb_shinfo(from)->nr_frags = 0;
4004bad43ca8SEric Dumazet 
40058ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
40068ea853fdSLi RongQing 	 * since we set nr_frags to 0.
40078ea853fdSLi RongQing 	 */
4008bad43ca8SEric Dumazet 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4009bad43ca8SEric Dumazet 		skb_frag_ref(from, i);
4010bad43ca8SEric Dumazet 
4011bad43ca8SEric Dumazet 	to->truesize += delta;
4012bad43ca8SEric Dumazet 	to->len += len;
4013bad43ca8SEric Dumazet 	to->data_len += len;
4014bad43ca8SEric Dumazet 
4015bad43ca8SEric Dumazet 	*delta_truesize = delta;
4016bad43ca8SEric Dumazet 	return true;
4017bad43ca8SEric Dumazet }
4018bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
4019621e84d6SNicolas Dichtel 
4020621e84d6SNicolas Dichtel /**
40218b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
4022621e84d6SNicolas Dichtel  *
4023621e84d6SNicolas Dichtel  * @skb: buffer to clean
40248b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
4025621e84d6SNicolas Dichtel  *
40268b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
40278b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
40288b27f277SNicolas Dichtel  * operations.
40298b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
40308b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
40318b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
4032621e84d6SNicolas Dichtel  */
40338b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4034621e84d6SNicolas Dichtel {
40358b27f277SNicolas Dichtel 	if (xnet)
4036621e84d6SNicolas Dichtel 		skb_orphan(skb);
4037621e84d6SNicolas Dichtel 	skb->tstamp.tv64 = 0;
4038621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
4039621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
404060ff7467SWANG Cong 	skb->ignore_df = 0;
4041621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
4042621e84d6SNicolas Dichtel 	skb->mark = 0;
4043621e84d6SNicolas Dichtel 	secpath_reset(skb);
4044621e84d6SNicolas Dichtel 	nf_reset(skb);
4045621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
4046621e84d6SNicolas Dichtel }
4047621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
4048de960aa9SFlorian Westphal 
4049de960aa9SFlorian Westphal /**
4050de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4051de960aa9SFlorian Westphal  *
4052de960aa9SFlorian Westphal  * @skb: GSO skb
4053de960aa9SFlorian Westphal  *
4054de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
4055de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
4056de960aa9SFlorian Westphal  *
4057de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4058de960aa9SFlorian Westphal  */
4059de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4060de960aa9SFlorian Westphal {
4061de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
4062de960aa9SFlorian Westphal 
4063de960aa9SFlorian Westphal 	if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
40646d39d589SFlorian Westphal 		return tcp_hdrlen(skb) + shinfo->gso_size;
40656d39d589SFlorian Westphal 
40666d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
40676d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
40686d39d589SFlorian Westphal 	 * accounted for.
40696d39d589SFlorian Westphal 	 */
40706d39d589SFlorian Westphal 	return shinfo->gso_size;
4071de960aa9SFlorian Westphal }
4072de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
40730d5501c1SVlad Yasevich 
40740d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
40750d5501c1SVlad Yasevich {
40760d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
40770d5501c1SVlad Yasevich 		kfree_skb(skb);
40780d5501c1SVlad Yasevich 		return NULL;
40790d5501c1SVlad Yasevich 	}
40800d5501c1SVlad Yasevich 
40810d5501c1SVlad Yasevich 	memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
40820d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
40830d5501c1SVlad Yasevich 	return skb;
40840d5501c1SVlad Yasevich }
40850d5501c1SVlad Yasevich 
40860d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
40870d5501c1SVlad Yasevich {
40880d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
40890d5501c1SVlad Yasevich 	u16 vlan_tci;
40900d5501c1SVlad Yasevich 
40910d5501c1SVlad Yasevich 	if (unlikely(vlan_tx_tag_present(skb))) {
40920d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
40930d5501c1SVlad Yasevich 		return skb;
40940d5501c1SVlad Yasevich 	}
40950d5501c1SVlad Yasevich 
40960d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
40970d5501c1SVlad Yasevich 	if (unlikely(!skb))
40980d5501c1SVlad Yasevich 		goto err_free;
40990d5501c1SVlad Yasevich 
41000d5501c1SVlad Yasevich 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
41010d5501c1SVlad Yasevich 		goto err_free;
41020d5501c1SVlad Yasevich 
41030d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
41040d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
41050d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
41060d5501c1SVlad Yasevich 
41070d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
41080d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
41090d5501c1SVlad Yasevich 
41100d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
41110d5501c1SVlad Yasevich 	if (unlikely(!skb))
41120d5501c1SVlad Yasevich 		goto err_free;
41130d5501c1SVlad Yasevich 
41140d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
41150d5501c1SVlad Yasevich 	skb_reset_transport_header(skb);
41160d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
41170d5501c1SVlad Yasevich 
41180d5501c1SVlad Yasevich 	return skb;
41190d5501c1SVlad Yasevich 
41200d5501c1SVlad Yasevich err_free:
41210d5501c1SVlad Yasevich 	kfree_skb(skb);
41220d5501c1SVlad Yasevich 	return NULL;
41230d5501c1SVlad Yasevich }
41240d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
41252e4e4410SEric Dumazet 
41262e4e4410SEric Dumazet /**
41272e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
41282e4e4410SEric Dumazet  *
41292e4e4410SEric Dumazet  * header_len: size of linear part
41302e4e4410SEric Dumazet  * data_len: needed length in frags
41312e4e4410SEric Dumazet  * max_page_order: max page order desired.
41322e4e4410SEric Dumazet  * errcode: pointer to error code if any
41332e4e4410SEric Dumazet  * gfp_mask: allocation mask
41342e4e4410SEric Dumazet  *
41352e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
41362e4e4410SEric Dumazet  */
41372e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
41382e4e4410SEric Dumazet 				     unsigned long data_len,
41392e4e4410SEric Dumazet 				     int max_page_order,
41402e4e4410SEric Dumazet 				     int *errcode,
41412e4e4410SEric Dumazet 				     gfp_t gfp_mask)
41422e4e4410SEric Dumazet {
41432e4e4410SEric Dumazet 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
41442e4e4410SEric Dumazet 	unsigned long chunk;
41452e4e4410SEric Dumazet 	struct sk_buff *skb;
41462e4e4410SEric Dumazet 	struct page *page;
41472e4e4410SEric Dumazet 	gfp_t gfp_head;
41482e4e4410SEric Dumazet 	int i;
41492e4e4410SEric Dumazet 
41502e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
41512e4e4410SEric Dumazet 	/* Note this test could be relaxed, if we succeed to allocate
41522e4e4410SEric Dumazet 	 * high order pages...
41532e4e4410SEric Dumazet 	 */
41542e4e4410SEric Dumazet 	if (npages > MAX_SKB_FRAGS)
41552e4e4410SEric Dumazet 		return NULL;
41562e4e4410SEric Dumazet 
41572e4e4410SEric Dumazet 	gfp_head = gfp_mask;
41582e4e4410SEric Dumazet 	if (gfp_head & __GFP_WAIT)
41592e4e4410SEric Dumazet 		gfp_head |= __GFP_REPEAT;
41602e4e4410SEric Dumazet 
41612e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
41622e4e4410SEric Dumazet 	skb = alloc_skb(header_len, gfp_head);
41632e4e4410SEric Dumazet 	if (!skb)
41642e4e4410SEric Dumazet 		return NULL;
41652e4e4410SEric Dumazet 
41662e4e4410SEric Dumazet 	skb->truesize += npages << PAGE_SHIFT;
41672e4e4410SEric Dumazet 
41682e4e4410SEric Dumazet 	for (i = 0; npages > 0; i++) {
41692e4e4410SEric Dumazet 		int order = max_page_order;
41702e4e4410SEric Dumazet 
41712e4e4410SEric Dumazet 		while (order) {
41722e4e4410SEric Dumazet 			if (npages >= 1 << order) {
41732e4e4410SEric Dumazet 				page = alloc_pages(gfp_mask |
41742e4e4410SEric Dumazet 						   __GFP_COMP |
41752e4e4410SEric Dumazet 						   __GFP_NOWARN |
41762e4e4410SEric Dumazet 						   __GFP_NORETRY,
41772e4e4410SEric Dumazet 						   order);
41782e4e4410SEric Dumazet 				if (page)
41792e4e4410SEric Dumazet 					goto fill_page;
41802e4e4410SEric Dumazet 				/* Do not retry other high order allocations */
41812e4e4410SEric Dumazet 				order = 1;
41822e4e4410SEric Dumazet 				max_page_order = 0;
41832e4e4410SEric Dumazet 			}
41842e4e4410SEric Dumazet 			order--;
41852e4e4410SEric Dumazet 		}
41862e4e4410SEric Dumazet 		page = alloc_page(gfp_mask);
41872e4e4410SEric Dumazet 		if (!page)
41882e4e4410SEric Dumazet 			goto failure;
41892e4e4410SEric Dumazet fill_page:
41902e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
41912e4e4410SEric Dumazet 			      PAGE_SIZE << order);
41922e4e4410SEric Dumazet 		skb_fill_page_desc(skb, i, page, 0, chunk);
41932e4e4410SEric Dumazet 		data_len -= chunk;
41942e4e4410SEric Dumazet 		npages -= 1 << order;
41952e4e4410SEric Dumazet 	}
41962e4e4410SEric Dumazet 	return skb;
41972e4e4410SEric Dumazet 
41982e4e4410SEric Dumazet failure:
41992e4e4410SEric Dumazet 	kfree_skb(skb);
42002e4e4410SEric Dumazet 	return NULL;
42012e4e4410SEric Dumazet }
42022e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
4203