xref: /openbmc/linux/net/core/skbuff.c (revision ffde7328)
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 
2686ffe75ebSEric Dumazet 		fclones->skb2.fclone = SKB_FCLONE_CLONE;
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);
339ffde7328SAlexander Duyck static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
340a1c7fff7SEric Dumazet 
341ffde7328SAlexander Duyck static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
342ffde7328SAlexander Duyck 				       gfp_t gfp_mask)
3436f532612SEric Dumazet {
344ffde7328SAlexander Duyck 	const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
345ffde7328SAlexander Duyck 	struct page *page = NULL;
34669b08f62SEric Dumazet 	gfp_t gfp = gfp_mask;
34769b08f62SEric Dumazet 
348ffde7328SAlexander Duyck 	if (order) {
349ffde7328SAlexander Duyck 		gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY;
350ffde7328SAlexander Duyck 		page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
351ffde7328SAlexander Duyck 		nc->frag.size = PAGE_SIZE << (page ? order : 0);
35269b08f62SEric Dumazet 	}
353ffde7328SAlexander Duyck 
354ffde7328SAlexander Duyck 	if (unlikely(!page))
355ffde7328SAlexander Duyck 		page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
356ffde7328SAlexander Duyck 
357ffde7328SAlexander Duyck 	nc->frag.page = page;
358ffde7328SAlexander Duyck 
359ffde7328SAlexander Duyck 	return page;
360ffde7328SAlexander Duyck }
361ffde7328SAlexander Duyck 
362ffde7328SAlexander Duyck static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
363ffde7328SAlexander Duyck 			       unsigned int fragsz, gfp_t gfp_mask)
364ffde7328SAlexander Duyck {
365ffde7328SAlexander Duyck 	struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
366ffde7328SAlexander Duyck 	struct page *page = nc->frag.page;
367ffde7328SAlexander Duyck 	unsigned int size;
368ffde7328SAlexander Duyck 	int offset;
369ffde7328SAlexander Duyck 
370ffde7328SAlexander Duyck 	if (unlikely(!page)) {
371ffde7328SAlexander Duyck refill:
372ffde7328SAlexander Duyck 		page = __page_frag_refill(nc, gfp_mask);
373ffde7328SAlexander Duyck 		if (!page)
374ffde7328SAlexander Duyck 			return NULL;
375ffde7328SAlexander Duyck 
376ffde7328SAlexander Duyck 		/* if size can vary use frag.size else just use PAGE_SIZE */
377ffde7328SAlexander Duyck 		size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
378ffde7328SAlexander Duyck 
3794c450583SEric Dumazet 		/* Even if we own the page, we do not use atomic_set().
3804c450583SEric Dumazet 		 * This would break get_page_unless_zero() users.
3814c450583SEric Dumazet 		 */
382ffde7328SAlexander Duyck 		atomic_add(size - 1, &page->_count);
383ffde7328SAlexander Duyck 
384ffde7328SAlexander Duyck 		/* reset page count bias and offset to start of new frag */
385ffde7328SAlexander Duyck 		nc->pagecnt_bias = size;
386ffde7328SAlexander Duyck 		nc->frag.offset = size;
3876f532612SEric Dumazet 	}
388540eb7bfSAlexander Duyck 
389ffde7328SAlexander Duyck 	offset = nc->frag.offset - fragsz;
390ffde7328SAlexander Duyck 	if (unlikely(offset < 0)) {
391ffde7328SAlexander Duyck 		if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
3926f532612SEric Dumazet 			goto refill;
393ffde7328SAlexander Duyck 
394ffde7328SAlexander Duyck 		/* if size can vary use frag.size else just use PAGE_SIZE */
395ffde7328SAlexander Duyck 		size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
396ffde7328SAlexander Duyck 
3974c450583SEric Dumazet 		/* OK, page count is 0, we can safely set it */
398ffde7328SAlexander Duyck 		atomic_set(&page->_count, size);
399ffde7328SAlexander Duyck 
400ffde7328SAlexander Duyck 		/* reset page count bias and offset to start of new frag */
401ffde7328SAlexander Duyck 		nc->pagecnt_bias = size;
402ffde7328SAlexander Duyck 		offset = size - fragsz;
4036f532612SEric Dumazet 	}
404540eb7bfSAlexander Duyck 
405540eb7bfSAlexander Duyck 	nc->pagecnt_bias--;
406ffde7328SAlexander Duyck 	nc->frag.offset = offset;
407ffde7328SAlexander Duyck 
408ffde7328SAlexander Duyck 	return page_address(page) + offset;
409ffde7328SAlexander Duyck }
410ffde7328SAlexander Duyck 
411ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
412ffde7328SAlexander Duyck {
413ffde7328SAlexander Duyck 	unsigned long flags;
414ffde7328SAlexander Duyck 	void *data;
415ffde7328SAlexander Duyck 
416ffde7328SAlexander Duyck 	local_irq_save(flags);
417ffde7328SAlexander Duyck 	data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
4186f532612SEric Dumazet 	local_irq_restore(flags);
4196f532612SEric Dumazet 	return data;
4206f532612SEric Dumazet }
421c93bdd0eSMel Gorman 
422c93bdd0eSMel Gorman /**
423c93bdd0eSMel Gorman  * netdev_alloc_frag - allocate a page fragment
424c93bdd0eSMel Gorman  * @fragsz: fragment size
425c93bdd0eSMel Gorman  *
426c93bdd0eSMel Gorman  * Allocates a frag from a page for receive buffer.
427c93bdd0eSMel Gorman  * Uses GFP_ATOMIC allocations.
428c93bdd0eSMel Gorman  */
429c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz)
430c93bdd0eSMel Gorman {
431c93bdd0eSMel Gorman 	return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
432c93bdd0eSMel Gorman }
4336f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag);
4346f532612SEric Dumazet 
435ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
436ffde7328SAlexander Duyck {
437ffde7328SAlexander Duyck 	return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
438ffde7328SAlexander Duyck }
439ffde7328SAlexander Duyck 
440ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz)
441ffde7328SAlexander Duyck {
442ffde7328SAlexander Duyck 	return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
443ffde7328SAlexander Duyck }
444ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag);
445ffde7328SAlexander Duyck 
4466f532612SEric Dumazet /**
4478af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
4488af27456SChristoph Hellwig  *	@dev: network device to receive on
4498af27456SChristoph Hellwig  *	@length: length to allocate
4508af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
4518af27456SChristoph Hellwig  *
4528af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
4538af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
4548af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
4558af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
4568af27456SChristoph Hellwig  *
4578af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
4588af27456SChristoph Hellwig  */
4598af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
4608af27456SChristoph Hellwig 				   unsigned int length, gfp_t gfp_mask)
4618af27456SChristoph Hellwig {
4626f532612SEric Dumazet 	struct sk_buff *skb = NULL;
463a1c7fff7SEric Dumazet 	unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) +
464a1c7fff7SEric Dumazet 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4658af27456SChristoph Hellwig 
466310e158cSEric Dumazet 	if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
467c93bdd0eSMel Gorman 		void *data;
468c93bdd0eSMel Gorman 
469c93bdd0eSMel Gorman 		if (sk_memalloc_socks())
470c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
471c93bdd0eSMel Gorman 
472c93bdd0eSMel Gorman 		data = __netdev_alloc_frag(fragsz, gfp_mask);
473a1c7fff7SEric Dumazet 
4746f532612SEric Dumazet 		if (likely(data)) {
4756f532612SEric Dumazet 			skb = build_skb(data, fragsz);
4766f532612SEric Dumazet 			if (unlikely(!skb))
4776f532612SEric Dumazet 				put_page(virt_to_head_page(data));
478a1c7fff7SEric Dumazet 		}
479a1c7fff7SEric Dumazet 	} else {
480c93bdd0eSMel Gorman 		skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask,
481c93bdd0eSMel Gorman 				  SKB_ALLOC_RX, NUMA_NO_NODE);
482a1c7fff7SEric Dumazet 	}
4837b2e497aSChristoph Hellwig 	if (likely(skb)) {
4848af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
4857b2e497aSChristoph Hellwig 		skb->dev = dev;
4867b2e497aSChristoph Hellwig 	}
4878af27456SChristoph Hellwig 	return skb;
4888af27456SChristoph Hellwig }
489b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
4901da177e4SLinus Torvalds 
491654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
49250269e19SEric Dumazet 		     int size, unsigned int truesize)
493654bed16SPeter Zijlstra {
494654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
495654bed16SPeter Zijlstra 	skb->len += size;
496654bed16SPeter Zijlstra 	skb->data_len += size;
49750269e19SEric Dumazet 	skb->truesize += truesize;
498654bed16SPeter Zijlstra }
499654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
500654bed16SPeter Zijlstra 
501f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
502f8e617e1SJason Wang 			  unsigned int truesize)
503f8e617e1SJason Wang {
504f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
505f8e617e1SJason Wang 
506f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
507f8e617e1SJason Wang 	skb->len += size;
508f8e617e1SJason Wang 	skb->data_len += size;
509f8e617e1SJason Wang 	skb->truesize += truesize;
510f8e617e1SJason Wang }
511f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
512f8e617e1SJason Wang 
51327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
5141da177e4SLinus Torvalds {
515bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
51627b437c8SHerbert Xu 	*listp = NULL;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
51927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
52027b437c8SHerbert Xu {
52127b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
52227b437c8SHerbert Xu }
52327b437c8SHerbert Xu 
5241da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct sk_buff *list;
5271da177e4SLinus Torvalds 
528fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
5291da177e4SLinus Torvalds 		skb_get(list);
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
532d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
533d3836f21SEric Dumazet {
534d3836f21SEric Dumazet 	if (skb->head_frag)
535d3836f21SEric Dumazet 		put_page(virt_to_head_page(skb->head));
536d3836f21SEric Dumazet 	else
537d3836f21SEric Dumazet 		kfree(skb->head);
538d3836f21SEric Dumazet }
539d3836f21SEric Dumazet 
5405bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
5411da177e4SLinus Torvalds {
542ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
5431da177e4SLinus Torvalds 	int i;
544ff04a771SEric Dumazet 
545ff04a771SEric Dumazet 	if (skb->cloned &&
546ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
547ff04a771SEric Dumazet 			      &shinfo->dataref))
548ff04a771SEric Dumazet 		return;
549ff04a771SEric Dumazet 
550ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
551ff04a771SEric Dumazet 		__skb_frag_unref(&shinfo->frags[i]);
5521da177e4SLinus Torvalds 
553a6686f2fSShirley Ma 	/*
554a6686f2fSShirley Ma 	 * If skb buf is from userspace, we need to notify the caller
555a6686f2fSShirley Ma 	 * the lower device DMA has done;
556a6686f2fSShirley Ma 	 */
557ff04a771SEric Dumazet 	if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
558a6686f2fSShirley Ma 		struct ubuf_info *uarg;
559a6686f2fSShirley Ma 
560ff04a771SEric Dumazet 		uarg = shinfo->destructor_arg;
561a6686f2fSShirley Ma 		if (uarg->callback)
562e19d6763SMichael S. Tsirkin 			uarg->callback(uarg, true);
563a6686f2fSShirley Ma 	}
564a6686f2fSShirley Ma 
565ff04a771SEric Dumazet 	if (shinfo->frag_list)
566ff04a771SEric Dumazet 		kfree_skb_list(shinfo->frag_list);
5671da177e4SLinus Torvalds 
568d3836f21SEric Dumazet 	skb_free_head(skb);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds /*
5721da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
5731da177e4SLinus Torvalds  */
5742d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
5751da177e4SLinus Torvalds {
576d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
577d179cd12SDavid S. Miller 
578d179cd12SDavid S. Miller 	switch (skb->fclone) {
579d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
5801da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
5816ffe75ebSEric Dumazet 		return;
582d179cd12SDavid S. Miller 
583d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
584d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
5856ffe75ebSEric Dumazet 
5866ffe75ebSEric Dumazet 		/* We usually free the clone (TX completion) before original skb
5876ffe75ebSEric Dumazet 		 * This test would have no chance to be true for the clone,
5886ffe75ebSEric Dumazet 		 * while here, branch prediction will be good.
5896ffe75ebSEric Dumazet 		 */
5906ffe75ebSEric Dumazet 		if (atomic_read(&fclones->fclone_ref) == 1)
5916ffe75ebSEric Dumazet 			goto fastpath;
592d179cd12SDavid S. Miller 		break;
593d179cd12SDavid S. Miller 
5946ffe75ebSEric Dumazet 	default: /* SKB_FCLONE_CLONE */
595d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
596d179cd12SDavid S. Miller 		break;
5973ff50b79SStephen Hemminger 	}
5986ffe75ebSEric Dumazet 	if (!atomic_dec_and_test(&fclones->fclone_ref))
5996ffe75ebSEric Dumazet 		return;
6006ffe75ebSEric Dumazet fastpath:
6016ffe75ebSEric Dumazet 	kmem_cache_free(skbuff_fclone_cache, fclones);
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
60404a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
6051da177e4SLinus Torvalds {
606adf30907SEric Dumazet 	skb_dst_drop(skb);
6071da177e4SLinus Torvalds #ifdef CONFIG_XFRM
6081da177e4SLinus Torvalds 	secpath_put(skb->sp);
6091da177e4SLinus Torvalds #endif
6101da177e4SLinus Torvalds 	if (skb->destructor) {
6119c2b3328SStephen Hemminger 		WARN_ON(in_irq());
6121da177e4SLinus Torvalds 		skb->destructor(skb);
6131da177e4SLinus Torvalds 	}
614a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
6155f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
6162fc72c7bSKOVACS Krisztian #endif
6171109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
6181da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
6191da177e4SLinus Torvalds #endif
6201da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
6211da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
6221da177e4SLinus Torvalds 	skb->tc_index = 0;
6231da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
6241da177e4SLinus Torvalds 	skb->tc_verd = 0;
6251da177e4SLinus Torvalds #endif
6261da177e4SLinus Torvalds #endif
62704a4bb55SLennert Buytenhek }
62804a4bb55SLennert Buytenhek 
62904a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
63004a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
63104a4bb55SLennert Buytenhek {
63204a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
6335e71d9d7SPablo Neira 	if (likely(skb->head))
6342d4baff8SHerbert Xu 		skb_release_data(skb);
6352d4baff8SHerbert Xu }
6361da177e4SLinus Torvalds 
6372d4baff8SHerbert Xu /**
6382d4baff8SHerbert Xu  *	__kfree_skb - private function
6392d4baff8SHerbert Xu  *	@skb: buffer
6402d4baff8SHerbert Xu  *
6412d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
6422d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
6432d4baff8SHerbert Xu  *	always call kfree_skb
6442d4baff8SHerbert Xu  */
6452d4baff8SHerbert Xu 
6462d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
6472d4baff8SHerbert Xu {
6482d4baff8SHerbert Xu 	skb_release_all(skb);
6491da177e4SLinus Torvalds 	kfree_skbmem(skb);
6501da177e4SLinus Torvalds }
651b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds /**
654231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
655231d06aeSJörn Engel  *	@skb: buffer to free
656231d06aeSJörn Engel  *
657231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
658231d06aeSJörn Engel  *	hit zero.
659231d06aeSJörn Engel  */
660231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
661231d06aeSJörn Engel {
662231d06aeSJörn Engel 	if (unlikely(!skb))
663231d06aeSJörn Engel 		return;
664231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
665231d06aeSJörn Engel 		smp_rmb();
666231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
667231d06aeSJörn Engel 		return;
668ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
669231d06aeSJörn Engel 	__kfree_skb(skb);
670231d06aeSJörn Engel }
671b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
672231d06aeSJörn Engel 
673bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
674bd8a7036SEric Dumazet {
675bd8a7036SEric Dumazet 	while (segs) {
676bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
677bd8a7036SEric Dumazet 
678bd8a7036SEric Dumazet 		kfree_skb(segs);
679bd8a7036SEric Dumazet 		segs = next;
680bd8a7036SEric Dumazet 	}
681bd8a7036SEric Dumazet }
682bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
683bd8a7036SEric Dumazet 
684d1a203eaSStephen Hemminger /**
68525121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
68625121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
68725121173SMichael S. Tsirkin  *
68825121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
68925121173SMichael S. Tsirkin  *	skb must be freed afterwards.
69025121173SMichael S. Tsirkin  */
69125121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
69225121173SMichael S. Tsirkin {
69325121173SMichael S. Tsirkin 	if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
69425121173SMichael S. Tsirkin 		struct ubuf_info *uarg;
69525121173SMichael S. Tsirkin 
69625121173SMichael S. Tsirkin 		uarg = skb_shinfo(skb)->destructor_arg;
69725121173SMichael S. Tsirkin 		if (uarg->callback)
69825121173SMichael S. Tsirkin 			uarg->callback(uarg, false);
69925121173SMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
70025121173SMichael S. Tsirkin 	}
70125121173SMichael S. Tsirkin }
70225121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
70325121173SMichael S. Tsirkin 
70425121173SMichael S. Tsirkin /**
705ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
706ead2ceb0SNeil Horman  *	@skb: buffer to free
707ead2ceb0SNeil Horman  *
708ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
709ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
710ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
711ead2ceb0SNeil Horman  */
712ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
713ead2ceb0SNeil Horman {
714ead2ceb0SNeil Horman 	if (unlikely(!skb))
715ead2ceb0SNeil Horman 		return;
716ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
717ead2ceb0SNeil Horman 		smp_rmb();
718ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
719ead2ceb0SNeil Horman 		return;
72007dc22e7SKoki Sanagi 	trace_consume_skb(skb);
721ead2ceb0SNeil Horman 	__kfree_skb(skb);
722ead2ceb0SNeil Horman }
723ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
724ead2ceb0SNeil Horman 
725b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */
726b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
727b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
728b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_start));	\
729b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
730b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_end));	\
731b1937227SEric Dumazet 
732dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
733dec18810SHerbert Xu {
734dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
735b1937227SEric Dumazet 	/* We do not copy old->sk */
736dec18810SHerbert Xu 	new->dev		= old->dev;
737b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
7387fee226aSEric Dumazet 	skb_dst_copy(new, old);
739def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
740dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
741dec18810SHerbert Xu #endif
742b1937227SEric Dumazet 	__nf_copy(new, old, false);
7436aa895b0SPatrick McHardy 
744b1937227SEric Dumazet 	/* Note : this field could be in headers_start/headers_end section
745b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
746b1937227SEric Dumazet 	 */
747b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
74806021292SEliezer Tamir 
749b1937227SEric Dumazet 	memcpy(&new->headers_start, &old->headers_start,
750b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_end) -
751b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_start));
752b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
753b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
754b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
755b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
756b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
757b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
758b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
759b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
760b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
761b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
762b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
763b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
764b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
765b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
766b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
767b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
768b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
769b1937227SEric Dumazet #endif
770e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
771b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
77206021292SEliezer Tamir #endif
773b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
774b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
775b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT
776b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_verd);
777b1937227SEric Dumazet #endif
778b1937227SEric Dumazet #endif
779b1937227SEric Dumazet 
780dec18810SHerbert Xu }
781dec18810SHerbert Xu 
78282c49a35SHerbert Xu /*
78382c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
78482c49a35SHerbert Xu  * __copy_skb_header above instead.
78582c49a35SHerbert Xu  */
786e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds #define C(x) n->x = skb->x
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	n->next = n->prev = NULL;
7911da177e4SLinus Torvalds 	n->sk = NULL;
792dec18810SHerbert Xu 	__copy_skb_header(n, skb);
793dec18810SHerbert Xu 
7941da177e4SLinus Torvalds 	C(len);
7951da177e4SLinus Torvalds 	C(data_len);
7963e6b3b2eSAlexey Dobriyan 	C(mac_len);
797334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
79802f1c89dSPaul Moore 	n->cloned = 1;
7991da177e4SLinus Torvalds 	n->nohdr = 0;
8001da177e4SLinus Torvalds 	n->destructor = NULL;
8011da177e4SLinus Torvalds 	C(tail);
8021da177e4SLinus Torvalds 	C(end);
80302f1c89dSPaul Moore 	C(head);
804d3836f21SEric Dumazet 	C(head_frag);
80502f1c89dSPaul Moore 	C(data);
80602f1c89dSPaul Moore 	C(truesize);
80702f1c89dSPaul Moore 	atomic_set(&n->users, 1);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
8101da177e4SLinus Torvalds 	skb->cloned = 1;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	return n;
813e0053ec0SHerbert Xu #undef C
814e0053ec0SHerbert Xu }
815e0053ec0SHerbert Xu 
816e0053ec0SHerbert Xu /**
817e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
818e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
819e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
820e0053ec0SHerbert Xu  *
821e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
822e0053ec0SHerbert Xu  *	supplied by the user.
823e0053ec0SHerbert Xu  *
824e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
825e0053ec0SHerbert Xu  */
826e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
827e0053ec0SHerbert Xu {
8282d4baff8SHerbert Xu 	skb_release_all(dst);
829e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
830e0053ec0SHerbert Xu }
831e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
832e0053ec0SHerbert Xu 
8332c53040fSBen Hutchings /**
8342c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
83548c83012SMichael S. Tsirkin  *	@skb: the skb to modify
83648c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
83748c83012SMichael S. Tsirkin  *
83848c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
83948c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
84048c83012SMichael S. Tsirkin  *	to userspace pages.
84148c83012SMichael S. Tsirkin  *
84248c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
84348c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
84448c83012SMichael S. Tsirkin  *
84548c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
84648c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
84748c83012SMichael S. Tsirkin  */
84848c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
849a6686f2fSShirley Ma {
850a6686f2fSShirley Ma 	int i;
851a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
852a6686f2fSShirley Ma 	struct page *page, *head = NULL;
853a6686f2fSShirley Ma 	struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
854a6686f2fSShirley Ma 
855a6686f2fSShirley Ma 	for (i = 0; i < num_frags; i++) {
856a6686f2fSShirley Ma 		u8 *vaddr;
857a6686f2fSShirley Ma 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
858a6686f2fSShirley Ma 
85902756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
860a6686f2fSShirley Ma 		if (!page) {
861a6686f2fSShirley Ma 			while (head) {
86240dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
863a6686f2fSShirley Ma 				put_page(head);
864a6686f2fSShirley Ma 				head = next;
865a6686f2fSShirley Ma 			}
866a6686f2fSShirley Ma 			return -ENOMEM;
867a6686f2fSShirley Ma 		}
86851c56b00SEric Dumazet 		vaddr = kmap_atomic(skb_frag_page(f));
869a6686f2fSShirley Ma 		memcpy(page_address(page),
8709e903e08SEric Dumazet 		       vaddr + f->page_offset, skb_frag_size(f));
87151c56b00SEric Dumazet 		kunmap_atomic(vaddr);
87240dadff2SSunghan Suh 		set_page_private(page, (unsigned long)head);
873a6686f2fSShirley Ma 		head = page;
874a6686f2fSShirley Ma 	}
875a6686f2fSShirley Ma 
876a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
87702756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
878a8605c60SIan Campbell 		skb_frag_unref(skb, i);
879a6686f2fSShirley Ma 
880e19d6763SMichael S. Tsirkin 	uarg->callback(uarg, false);
881a6686f2fSShirley Ma 
882a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
88302756ed4SKrishna Kumar 	for (i = num_frags - 1; i >= 0; i--) {
88402756ed4SKrishna Kumar 		__skb_fill_page_desc(skb, i, head, 0,
88502756ed4SKrishna Kumar 				     skb_shinfo(skb)->frags[i].size);
88640dadff2SSunghan Suh 		head = (struct page *)page_private(head);
887a6686f2fSShirley Ma 	}
88848c83012SMichael S. Tsirkin 
88948c83012SMichael S. Tsirkin 	skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
890a6686f2fSShirley Ma 	return 0;
891a6686f2fSShirley Ma }
892dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
893a6686f2fSShirley Ma 
894e0053ec0SHerbert Xu /**
895e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
896e0053ec0SHerbert Xu  *	@skb: buffer to clone
897e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
898e0053ec0SHerbert Xu  *
899e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
900e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
901e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
902e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
903e0053ec0SHerbert Xu  *
904e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
905e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
906e0053ec0SHerbert Xu  */
907e0053ec0SHerbert Xu 
908e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
909e0053ec0SHerbert Xu {
910d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
911d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
912d0bf4a9eSEric Dumazet 						       skb1);
9136ffe75ebSEric Dumazet 	struct sk_buff *n;
914e0053ec0SHerbert Xu 
91570008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
916a6686f2fSShirley Ma 		return NULL;
917a6686f2fSShirley Ma 
918e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
9196ffe75ebSEric Dumazet 	    atomic_read(&fclones->fclone_ref) == 1) {
9206ffe75ebSEric Dumazet 		n = &fclones->skb2;
9216ffe75ebSEric Dumazet 		atomic_set(&fclones->fclone_ref, 2);
922e0053ec0SHerbert Xu 	} else {
923c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
924c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
925c93bdd0eSMel Gorman 
926e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
927e0053ec0SHerbert Xu 		if (!n)
928e0053ec0SHerbert Xu 			return NULL;
929fe55f6d5SVegard Nossum 
930fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags1);
931e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
932e0053ec0SHerbert Xu 	}
933e0053ec0SHerbert Xu 
934e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
9351da177e4SLinus Torvalds }
936b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
9371da177e4SLinus Torvalds 
938f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off)
939f5b17294SPravin B Shelar {
940030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
941030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
942030737bcSEric Dumazet 		skb->csum_start += off;
943f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
944f5b17294SPravin B Shelar 	skb->transport_header += off;
945f5b17294SPravin B Shelar 	skb->network_header   += off;
946f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
947f5b17294SPravin B Shelar 		skb->mac_header += off;
948f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
949f5b17294SPravin B Shelar 	skb->inner_network_header += off;
950aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
951f5b17294SPravin B Shelar }
952f5b17294SPravin B Shelar 
9531da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
9541da177e4SLinus Torvalds {
955dec18810SHerbert Xu 	__copy_skb_header(new, old);
956dec18810SHerbert Xu 
9577967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
9587967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
9597967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
962c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
963c93bdd0eSMel Gorman {
964c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
965c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
966c93bdd0eSMel Gorman 	return 0;
967c93bdd0eSMel Gorman }
968c93bdd0eSMel Gorman 
9691da177e4SLinus Torvalds /**
9701da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
9711da177e4SLinus Torvalds  *	@skb: buffer to copy
9721da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
9731da177e4SLinus Torvalds  *
9741da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
9751da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
9761da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
9771da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
9781da177e4SLinus Torvalds  *
9791da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
9801da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
9811da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
9821da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
9831da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
9841da177e4SLinus Torvalds  */
9851da177e4SLinus Torvalds 
986dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
9871da177e4SLinus Torvalds {
9886602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
989ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
990c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
991c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
9926602cebbSEric Dumazet 
9931da177e4SLinus Torvalds 	if (!n)
9941da177e4SLinus Torvalds 		return NULL;
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/* Set the data pointer */
9971da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
9981da177e4SLinus Torvalds 	/* Set the tail pointer and length */
9991da177e4SLinus Torvalds 	skb_put(n, skb->len);
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
10021da177e4SLinus Torvalds 		BUG();
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	copy_skb_header(n, skb);
10051da177e4SLinus Torvalds 	return n;
10061da177e4SLinus Torvalds }
1007b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds /**
1010bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
10111da177e4SLinus Torvalds  *	@skb: buffer to copy
1012117632e6SEric Dumazet  *	@headroom: headroom of new skb
10131da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
1014bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
1015bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
1016bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
10171da177e4SLinus Torvalds  *
10181da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
10191da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
10201da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
10211da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
10221da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
10231da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
10241da177e4SLinus Torvalds  */
10251da177e4SLinus Torvalds 
1026bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1027bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
10281da177e4SLinus Torvalds {
1029117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
1030bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1031bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
10326602cebbSEric Dumazet 
10331da177e4SLinus Torvalds 	if (!n)
10341da177e4SLinus Torvalds 		goto out;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/* Set the data pointer */
1037117632e6SEric Dumazet 	skb_reserve(n, headroom);
10381da177e4SLinus Torvalds 	/* Set the tail pointer and length */
10391da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
10401da177e4SLinus Torvalds 	/* Copy the bytes */
1041d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
10421da177e4SLinus Torvalds 
104325f484a6SHerbert Xu 	n->truesize += skb->data_len;
10441da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
10451da177e4SLinus Torvalds 	n->len	     = skb->len;
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
10481da177e4SLinus Torvalds 		int i;
10491da177e4SLinus Torvalds 
105070008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask)) {
10511511022cSDan Carpenter 			kfree_skb(n);
10521511022cSDan Carpenter 			n = NULL;
1053a6686f2fSShirley Ma 			goto out;
1054a6686f2fSShirley Ma 		}
10551da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
10561da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1057ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
10581da177e4SLinus Torvalds 		}
10591da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds 
106221dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
10631da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
10641da177e4SLinus Torvalds 		skb_clone_fraglist(n);
10651da177e4SLinus Torvalds 	}
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	copy_skb_header(n, skb);
10681da177e4SLinus Torvalds out:
10691da177e4SLinus Torvalds 	return n;
10701da177e4SLinus Torvalds }
1071bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds /**
10741da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
10751da177e4SLinus Torvalds  *	@skb: buffer to reallocate
10761da177e4SLinus Torvalds  *	@nhead: room to add at head
10771da177e4SLinus Torvalds  *	@ntail: room to add at tail
10781da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
10791da177e4SLinus Torvalds  *
1080bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1081bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
10821da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
10831da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
10841da177e4SLinus Torvalds  *
10851da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
10861da177e4SLinus Torvalds  *	reloaded after call to this function.
10871da177e4SLinus Torvalds  */
10881da177e4SLinus Torvalds 
108986a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1090dd0fc66fSAl Viro 		     gfp_t gfp_mask)
10911da177e4SLinus Torvalds {
10921da177e4SLinus Torvalds 	int i;
10931da177e4SLinus Torvalds 	u8 *data;
1094ec47ea82SAlexander Duyck 	int size = nhead + skb_end_offset(skb) + ntail;
10951da177e4SLinus Torvalds 	long off;
10961da177e4SLinus Torvalds 
10974edd87adSHerbert Xu 	BUG_ON(nhead < 0);
10984edd87adSHerbert Xu 
10991da177e4SLinus Torvalds 	if (skb_shared(skb))
11001da177e4SLinus Torvalds 		BUG();
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
11031da177e4SLinus Torvalds 
1104c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1105c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1106c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1107c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
11081da177e4SLinus Torvalds 	if (!data)
11091da177e4SLinus Torvalds 		goto nodata;
111087151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
11136602cebbSEric Dumazet 	 * optimized for the cases when header is void.
11146602cebbSEric Dumazet 	 */
11156602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
11166602cebbSEric Dumazet 
11176602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
11186602cebbSEric Dumazet 	       skb_shinfo(skb),
1119fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
11201da177e4SLinus Torvalds 
11213e24591aSAlexander Duyck 	/*
11223e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
11233e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
11243e24591aSAlexander Duyck 	 * be since all we did is relocate the values
11253e24591aSAlexander Duyck 	 */
11263e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
1127a6686f2fSShirley Ma 		/* copy this zero copy skb frags */
112870008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1129a6686f2fSShirley Ma 			goto nofrags;
11301da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1131ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
11321da177e4SLinus Torvalds 
113321dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
11341da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 		skb_release_data(skb);
11373e24591aSAlexander Duyck 	} else {
11383e24591aSAlexander Duyck 		skb_free_head(skb);
11391fd63041SEric Dumazet 	}
11401da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	skb->head     = data;
1143d3836f21SEric Dumazet 	skb->head_frag = 0;
11441da177e4SLinus Torvalds 	skb->data    += off;
11454305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
11464305b541SArnaldo Carvalho de Melo 	skb->end      = size;
114756eb8882SPatrick McHardy 	off           = nhead;
11484305b541SArnaldo Carvalho de Melo #else
11494305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
115056eb8882SPatrick McHardy #endif
115127a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1152b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
11531da177e4SLinus Torvalds 	skb->cloned   = 0;
1154334a8132SPatrick McHardy 	skb->hdr_len  = 0;
11551da177e4SLinus Torvalds 	skb->nohdr    = 0;
11561da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
11571da177e4SLinus Torvalds 	return 0;
11581da177e4SLinus Torvalds 
1159a6686f2fSShirley Ma nofrags:
1160a6686f2fSShirley Ma 	kfree(data);
11611da177e4SLinus Torvalds nodata:
11621da177e4SLinus Torvalds 	return -ENOMEM;
11631da177e4SLinus Torvalds }
1164b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	struct sk_buff *skb2;
11711da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	if (delta <= 0)
11741da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
11751da177e4SLinus Torvalds 	else {
11761da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
11771da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
11781da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
11791da177e4SLinus Torvalds 			kfree_skb(skb2);
11801da177e4SLinus Torvalds 			skb2 = NULL;
11811da177e4SLinus Torvalds 		}
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 	return skb2;
11841da177e4SLinus Torvalds }
1185b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds /**
11881da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
11891da177e4SLinus Torvalds  *	@skb: buffer to copy
11901da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
11911da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
11921da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
11931da177e4SLinus Torvalds  *
11941da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
11951da177e4SLinus Torvalds  *	allocate additional space.
11961da177e4SLinus Torvalds  *
11971da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
11981da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
11991da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
12001da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
12011da177e4SLinus Torvalds  *
12021da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
12031da177e4SLinus Torvalds  *	is called from an interrupt.
12041da177e4SLinus Torvalds  */
12051da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
120686a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1207dd0fc66fSAl Viro 				gfp_t gfp_mask)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	/*
12101da177e4SLinus Torvalds 	 *	Allocate the copy buffer
12111da177e4SLinus Torvalds 	 */
1212c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1213c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1214c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1215efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
12161da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	if (!n)
12191da177e4SLinus Torvalds 		return NULL;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 	/* Set the tail pointer and length */
12241da177e4SLinus Torvalds 	skb_put(n, skb->len);
12251da177e4SLinus Torvalds 
1226efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
12271da177e4SLinus Torvalds 	head_copy_off = 0;
12281da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
12291da177e4SLinus Torvalds 		head_copy_len = newheadroom;
12301da177e4SLinus Torvalds 	else
12311da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	/* Copy the linear header and data. */
12341da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
12351da177e4SLinus Torvalds 			  skb->len + head_copy_len))
12361da177e4SLinus Torvalds 		BUG();
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 	copy_skb_header(n, skb);
12391da177e4SLinus Torvalds 
1240030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1241efd1e8d5SPatrick McHardy 
12421da177e4SLinus Torvalds 	return n;
12431da177e4SLinus Torvalds }
1244b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds /**
12471da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
12481da177e4SLinus Torvalds  *	@skb: buffer to pad
12491da177e4SLinus Torvalds  *	@pad: space to pad
12501da177e4SLinus Torvalds  *
12511da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
12521da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
12531da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
12541da177e4SLinus Torvalds  *
12555b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
12561da177e4SLinus Torvalds  */
12571da177e4SLinus Torvalds 
12585b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
12591da177e4SLinus Torvalds {
12605b057c6bSHerbert Xu 	int err;
12615b057c6bSHerbert Xu 	int ntail;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
12645b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
12651da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
12665b057c6bSHerbert Xu 		return 0;
12671da177e4SLinus Torvalds 	}
12681da177e4SLinus Torvalds 
12694305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
12705b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
12715b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
12725b057c6bSHerbert Xu 		if (unlikely(err))
12735b057c6bSHerbert Xu 			goto free_skb;
12745b057c6bSHerbert Xu 	}
12755b057c6bSHerbert Xu 
12765b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
12775b057c6bSHerbert Xu 	 * to be audited.
12785b057c6bSHerbert Xu 	 */
12795b057c6bSHerbert Xu 	err = skb_linearize(skb);
12805b057c6bSHerbert Xu 	if (unlikely(err))
12815b057c6bSHerbert Xu 		goto free_skb;
12825b057c6bSHerbert Xu 
12835b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
12845b057c6bSHerbert Xu 	return 0;
12855b057c6bSHerbert Xu 
12865b057c6bSHerbert Xu free_skb:
12871da177e4SLinus Torvalds 	kfree_skb(skb);
12885b057c6bSHerbert Xu 	return err;
12891da177e4SLinus Torvalds }
1290b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
12911da177e4SLinus Torvalds 
12920dde3e16SIlpo Järvinen /**
12930c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
12940c7ddf36SMathias Krause  *	@skb: start of the buffer to use
12950c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
12960c7ddf36SMathias Krause  *	@len: amount of data to add
12970c7ddf36SMathias Krause  *
12980c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
12990c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
13000c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
13010c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
13020c7ddf36SMathias Krause  *	returned.
13030c7ddf36SMathias Krause  */
13040c7ddf36SMathias Krause 
13050c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
13060c7ddf36SMathias Krause {
13070c7ddf36SMathias Krause 	if (tail != skb) {
13080c7ddf36SMathias Krause 		skb->data_len += len;
13090c7ddf36SMathias Krause 		skb->len += len;
13100c7ddf36SMathias Krause 	}
13110c7ddf36SMathias Krause 	return skb_put(tail, len);
13120c7ddf36SMathias Krause }
13130c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
13140c7ddf36SMathias Krause 
13150c7ddf36SMathias Krause /**
13160dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
13170dde3e16SIlpo Järvinen  *	@skb: buffer to use
13180dde3e16SIlpo Järvinen  *	@len: amount of data to add
13190dde3e16SIlpo Järvinen  *
13200dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
13210dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
13220dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
13230dde3e16SIlpo Järvinen  */
13240dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
13250dde3e16SIlpo Järvinen {
13260dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
13270dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
13280dde3e16SIlpo Järvinen 	skb->tail += len;
13290dde3e16SIlpo Järvinen 	skb->len  += len;
13300dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
13310dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
13320dde3e16SIlpo Järvinen 	return tmp;
13330dde3e16SIlpo Järvinen }
13340dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
13350dde3e16SIlpo Järvinen 
13366be8ac2fSIlpo Järvinen /**
1337c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1338c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1339c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1340c2aa270aSIlpo Järvinen  *
1341c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1342c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1343c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1344c2aa270aSIlpo Järvinen  */
1345c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1346c2aa270aSIlpo Järvinen {
1347c2aa270aSIlpo Järvinen 	skb->data -= len;
1348c2aa270aSIlpo Järvinen 	skb->len  += len;
1349c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1350c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1351c2aa270aSIlpo Järvinen 	return skb->data;
1352c2aa270aSIlpo Järvinen }
1353c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1354c2aa270aSIlpo Järvinen 
1355c2aa270aSIlpo Järvinen /**
13566be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
13576be8ac2fSIlpo Järvinen  *	@skb: buffer to use
13586be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
13596be8ac2fSIlpo Järvinen  *
13606be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
13616be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
13626be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
13636be8ac2fSIlpo Järvinen  *	the old data.
13646be8ac2fSIlpo Järvinen  */
13656be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
13666be8ac2fSIlpo Järvinen {
136747d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
13686be8ac2fSIlpo Järvinen }
13696be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
13706be8ac2fSIlpo Järvinen 
1371419ae74eSIlpo Järvinen /**
1372419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1373419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1374419ae74eSIlpo Järvinen  *	@len: new length
1375419ae74eSIlpo Järvinen  *
1376419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1377419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1378419ae74eSIlpo Järvinen  *	The skb must be linear.
1379419ae74eSIlpo Järvinen  */
1380419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1381419ae74eSIlpo Järvinen {
1382419ae74eSIlpo Järvinen 	if (skb->len > len)
1383419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1384419ae74eSIlpo Järvinen }
1385419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1386419ae74eSIlpo Järvinen 
13873cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
13881da177e4SLinus Torvalds  */
13891da177e4SLinus Torvalds 
13903cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
13911da177e4SLinus Torvalds {
139227b437c8SHerbert Xu 	struct sk_buff **fragp;
139327b437c8SHerbert Xu 	struct sk_buff *frag;
13941da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
13951da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
13961da177e4SLinus Torvalds 	int i;
139727b437c8SHerbert Xu 	int err;
139827b437c8SHerbert Xu 
139927b437c8SHerbert Xu 	if (skb_cloned(skb) &&
140027b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
140127b437c8SHerbert Xu 		return err;
14021da177e4SLinus Torvalds 
1403f4d26fb3SHerbert Xu 	i = 0;
1404f4d26fb3SHerbert Xu 	if (offset >= len)
1405f4d26fb3SHerbert Xu 		goto drop_pages;
1406f4d26fb3SHerbert Xu 
1407f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
14089e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
140927b437c8SHerbert Xu 
141027b437c8SHerbert Xu 		if (end < len) {
14111da177e4SLinus Torvalds 			offset = end;
141227b437c8SHerbert Xu 			continue;
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 
14159e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
141627b437c8SHerbert Xu 
1417f4d26fb3SHerbert Xu drop_pages:
141827b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
141927b437c8SHerbert Xu 
142027b437c8SHerbert Xu 		for (; i < nfrags; i++)
1421ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
142227b437c8SHerbert Xu 
142321dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
142427b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1425f4d26fb3SHerbert Xu 		goto done;
142627b437c8SHerbert Xu 	}
142727b437c8SHerbert Xu 
142827b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
142927b437c8SHerbert Xu 	     fragp = &frag->next) {
143027b437c8SHerbert Xu 		int end = offset + frag->len;
143127b437c8SHerbert Xu 
143227b437c8SHerbert Xu 		if (skb_shared(frag)) {
143327b437c8SHerbert Xu 			struct sk_buff *nfrag;
143427b437c8SHerbert Xu 
143527b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
143627b437c8SHerbert Xu 			if (unlikely(!nfrag))
143727b437c8SHerbert Xu 				return -ENOMEM;
143827b437c8SHerbert Xu 
143927b437c8SHerbert Xu 			nfrag->next = frag->next;
144085bb2a60SEric Dumazet 			consume_skb(frag);
144127b437c8SHerbert Xu 			frag = nfrag;
144227b437c8SHerbert Xu 			*fragp = frag;
144327b437c8SHerbert Xu 		}
144427b437c8SHerbert Xu 
144527b437c8SHerbert Xu 		if (end < len) {
144627b437c8SHerbert Xu 			offset = end;
144727b437c8SHerbert Xu 			continue;
144827b437c8SHerbert Xu 		}
144927b437c8SHerbert Xu 
145027b437c8SHerbert Xu 		if (end > len &&
145127b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
145227b437c8SHerbert Xu 			return err;
145327b437c8SHerbert Xu 
145427b437c8SHerbert Xu 		if (frag->next)
145527b437c8SHerbert Xu 			skb_drop_list(&frag->next);
145627b437c8SHerbert Xu 		break;
145727b437c8SHerbert Xu 	}
145827b437c8SHerbert Xu 
1459f4d26fb3SHerbert Xu done:
146027b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
14611da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
14621da177e4SLinus Torvalds 		skb->len       = len;
14631da177e4SLinus Torvalds 	} else {
14641da177e4SLinus Torvalds 		skb->len       = len;
14651da177e4SLinus Torvalds 		skb->data_len  = 0;
146627a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	return 0;
14701da177e4SLinus Torvalds }
1471b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds /**
14741da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
14751da177e4SLinus Torvalds  *	@skb: buffer to reallocate
14761da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
14771da177e4SLinus Torvalds  *
14781da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
14791da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
14801da177e4SLinus Torvalds  *	data from fragmented part.
14811da177e4SLinus Torvalds  *
14821da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
14831da177e4SLinus Torvalds  *
14841da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
14851da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
14861da177e4SLinus Torvalds  *
14871da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
14881da177e4SLinus Torvalds  *	reloaded after call to this function.
14891da177e4SLinus Torvalds  */
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
14921da177e4SLinus Torvalds  * when it is necessary.
14931da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
14941da177e4SLinus Torvalds  * 2. It may change skb pointers.
14951da177e4SLinus Torvalds  *
14961da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
14971da177e4SLinus Torvalds  */
14981da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
15011da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
15021da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
15031da177e4SLinus Torvalds 	 */
15044305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
15071da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
15081da177e4SLinus Torvalds 				     GFP_ATOMIC))
15091da177e4SLinus Torvalds 			return NULL;
15101da177e4SLinus Torvalds 	}
15111da177e4SLinus Torvalds 
151227a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
15131da177e4SLinus Torvalds 		BUG();
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
15161da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
15171da177e4SLinus Torvalds 	 */
151821dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
15191da177e4SLinus Torvalds 		goto pull_pages;
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
15221da177e4SLinus Torvalds 	eat = delta;
15231da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15249e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
15259e903e08SEric Dumazet 
15269e903e08SEric Dumazet 		if (size >= eat)
15271da177e4SLinus Torvalds 			goto pull_pages;
15289e903e08SEric Dumazet 		eat -= size;
15291da177e4SLinus Torvalds 	}
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
15321da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
15331da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
15341da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
15351da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
15361da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
15371da177e4SLinus Torvalds 	 */
15381da177e4SLinus Torvalds 	if (eat) {
15391da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
15401da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
15411da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 		do {
154409a62660SKris Katterjohn 			BUG_ON(!list);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 			if (list->len <= eat) {
15471da177e4SLinus Torvalds 				/* Eaten as whole. */
15481da177e4SLinus Torvalds 				eat -= list->len;
15491da177e4SLinus Torvalds 				list = list->next;
15501da177e4SLinus Torvalds 				insp = list;
15511da177e4SLinus Torvalds 			} else {
15521da177e4SLinus Torvalds 				/* Eaten partially. */
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 				if (skb_shared(list)) {
15551da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
15561da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
15571da177e4SLinus Torvalds 					if (!clone)
15581da177e4SLinus Torvalds 						return NULL;
15591da177e4SLinus Torvalds 					insp = list->next;
15601da177e4SLinus Torvalds 					list = clone;
15611da177e4SLinus Torvalds 				} else {
15621da177e4SLinus Torvalds 					/* This may be pulled without
15631da177e4SLinus Torvalds 					 * problems. */
15641da177e4SLinus Torvalds 					insp = list;
15651da177e4SLinus Torvalds 				}
15661da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
15671da177e4SLinus Torvalds 					kfree_skb(clone);
15681da177e4SLinus Torvalds 					return NULL;
15691da177e4SLinus Torvalds 				}
15701da177e4SLinus Torvalds 				break;
15711da177e4SLinus Torvalds 			}
15721da177e4SLinus Torvalds 		} while (eat);
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 		/* Free pulled out fragments. */
15751da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
15761da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
15771da177e4SLinus Torvalds 			kfree_skb(list);
15781da177e4SLinus Torvalds 		}
15791da177e4SLinus Torvalds 		/* And insert new clone at head. */
15801da177e4SLinus Torvalds 		if (clone) {
15811da177e4SLinus Torvalds 			clone->next = list;
15821da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
15831da177e4SLinus Torvalds 		}
15841da177e4SLinus Torvalds 	}
15851da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
15861da177e4SLinus Torvalds 
15871da177e4SLinus Torvalds pull_pages:
15881da177e4SLinus Torvalds 	eat = delta;
15891da177e4SLinus Torvalds 	k = 0;
15901da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15919e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
15929e903e08SEric Dumazet 
15939e903e08SEric Dumazet 		if (size <= eat) {
1594ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
15959e903e08SEric Dumazet 			eat -= size;
15961da177e4SLinus Torvalds 		} else {
15971da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
15981da177e4SLinus Torvalds 			if (eat) {
15991da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
16009e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
16011da177e4SLinus Torvalds 				eat = 0;
16021da177e4SLinus Torvalds 			}
16031da177e4SLinus Torvalds 			k++;
16041da177e4SLinus Torvalds 		}
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	skb->tail     += delta;
16091da177e4SLinus Torvalds 	skb->data_len -= delta;
16101da177e4SLinus Torvalds 
161127a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
16121da177e4SLinus Torvalds }
1613b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
16141da177e4SLinus Torvalds 
161522019b17SEric Dumazet /**
161622019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
161722019b17SEric Dumazet  *	@skb: source skb
161822019b17SEric Dumazet  *	@offset: offset in source
161922019b17SEric Dumazet  *	@to: destination buffer
162022019b17SEric Dumazet  *	@len: number of bytes to copy
162122019b17SEric Dumazet  *
162222019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
162322019b17SEric Dumazet  *	destination buffer.
162422019b17SEric Dumazet  *
162522019b17SEric Dumazet  *	CAUTION ! :
162622019b17SEric Dumazet  *		If its prototype is ever changed,
162722019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
162822019b17SEric Dumazet  *		since it is called from BPF assembly code.
162922019b17SEric Dumazet  */
16301da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
16311da177e4SLinus Torvalds {
16321a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1633fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1634fbb398a8SDavid S. Miller 	int i, copy;
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
16371da177e4SLinus Torvalds 		goto fault;
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	/* Copy header. */
16401a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
16411da177e4SLinus Torvalds 		if (copy > len)
16421da177e4SLinus Torvalds 			copy = len;
1643d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
16441da177e4SLinus Torvalds 		if ((len -= copy) == 0)
16451da177e4SLinus Torvalds 			return 0;
16461da177e4SLinus Torvalds 		offset += copy;
16471da177e4SLinus Torvalds 		to     += copy;
16481da177e4SLinus Torvalds 	}
16491da177e4SLinus Torvalds 
16501da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16511a028e50SDavid S. Miller 		int end;
165251c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
16531da177e4SLinus Torvalds 
1654547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16551a028e50SDavid S. Miller 
165651c56b00SEric Dumazet 		end = start + skb_frag_size(f);
16571da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
16581da177e4SLinus Torvalds 			u8 *vaddr;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 			if (copy > len)
16611da177e4SLinus Torvalds 				copy = len;
16621da177e4SLinus Torvalds 
166351c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(f));
16641da177e4SLinus Torvalds 			memcpy(to,
166551c56b00SEric Dumazet 			       vaddr + f->page_offset + offset - start,
166651c56b00SEric Dumazet 			       copy);
166751c56b00SEric Dumazet 			kunmap_atomic(vaddr);
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 			if ((len -= copy) == 0)
16701da177e4SLinus Torvalds 				return 0;
16711da177e4SLinus Torvalds 			offset += copy;
16721da177e4SLinus Torvalds 			to     += copy;
16731da177e4SLinus Torvalds 		}
16741a028e50SDavid S. Miller 		start = end;
16751da177e4SLinus Torvalds 	}
16761da177e4SLinus Torvalds 
1677fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
16781a028e50SDavid S. Miller 		int end;
16791da177e4SLinus Torvalds 
1680547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16811a028e50SDavid S. Miller 
1682fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
16831da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
16841da177e4SLinus Torvalds 			if (copy > len)
16851da177e4SLinus Torvalds 				copy = len;
1686fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
16871da177e4SLinus Torvalds 				goto fault;
16881da177e4SLinus Torvalds 			if ((len -= copy) == 0)
16891da177e4SLinus Torvalds 				return 0;
16901da177e4SLinus Torvalds 			offset += copy;
16911da177e4SLinus Torvalds 			to     += copy;
16921da177e4SLinus Torvalds 		}
16931a028e50SDavid S. Miller 		start = end;
16941da177e4SLinus Torvalds 	}
1695a6686f2fSShirley Ma 
16961da177e4SLinus Torvalds 	if (!len)
16971da177e4SLinus Torvalds 		return 0;
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds fault:
17001da177e4SLinus Torvalds 	return -EFAULT;
17011da177e4SLinus Torvalds }
1702b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
17031da177e4SLinus Torvalds 
17049c55e01cSJens Axboe /*
17059c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
17069c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
17079c55e01cSJens Axboe  */
17089c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
17099c55e01cSJens Axboe {
17108b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
17118b9d3728SJarek Poplawski }
17129c55e01cSJens Axboe 
1713a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
17144fb66994SJarek Poplawski 				   unsigned int *offset,
171518aafc62SEric Dumazet 				   struct sock *sk)
17168b9d3728SJarek Poplawski {
17175640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
17188b9d3728SJarek Poplawski 
17195640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
17208b9d3728SJarek Poplawski 		return NULL;
17214fb66994SJarek Poplawski 
17225640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
17234fb66994SJarek Poplawski 
17245640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
17255640f768SEric Dumazet 	       page_address(page) + *offset, *len);
17265640f768SEric Dumazet 	*offset = pfrag->offset;
17275640f768SEric Dumazet 	pfrag->offset += *len;
17284fb66994SJarek Poplawski 
17295640f768SEric Dumazet 	return pfrag->page;
17309c55e01cSJens Axboe }
17319c55e01cSJens Axboe 
173241c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
173341c73a0dSEric Dumazet 			     struct page *page,
173441c73a0dSEric Dumazet 			     unsigned int offset)
173541c73a0dSEric Dumazet {
173641c73a0dSEric Dumazet 	return	spd->nr_pages &&
173741c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
173841c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
173941c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
174041c73a0dSEric Dumazet }
174141c73a0dSEric Dumazet 
17429c55e01cSJens Axboe /*
17439c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
17449c55e01cSJens Axboe  */
1745a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
174635f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
17474fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
174818aafc62SEric Dumazet 			  bool linear,
17497a67e56fSJarek Poplawski 			  struct sock *sk)
17509c55e01cSJens Axboe {
175141c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1752a108d5f3SDavid S. Miller 		return true;
17539c55e01cSJens Axboe 
17548b9d3728SJarek Poplawski 	if (linear) {
175518aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
17568b9d3728SJarek Poplawski 		if (!page)
1757a108d5f3SDavid S. Miller 			return true;
175841c73a0dSEric Dumazet 	}
175941c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
176041c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
1761a108d5f3SDavid S. Miller 		return false;
176241c73a0dSEric Dumazet 	}
17638b9d3728SJarek Poplawski 	get_page(page);
17649c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
17654fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
17669c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
17679c55e01cSJens Axboe 	spd->nr_pages++;
17688b9d3728SJarek Poplawski 
1769a108d5f3SDavid S. Miller 	return false;
17709c55e01cSJens Axboe }
17719c55e01cSJens Axboe 
1772a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
17732870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
177418aafc62SEric Dumazet 			     unsigned int *len,
1775d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
177635f3d14dSJens Axboe 			     struct sock *sk,
177735f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
17789c55e01cSJens Axboe {
17792870c43dSOctavian Purdila 	if (!*len)
1780a108d5f3SDavid S. Miller 		return true;
17819c55e01cSJens Axboe 
17822870c43dSOctavian Purdila 	/* skip this segment if already processed */
17832870c43dSOctavian Purdila 	if (*off >= plen) {
17842870c43dSOctavian Purdila 		*off -= plen;
1785a108d5f3SDavid S. Miller 		return false;
17862870c43dSOctavian Purdila 	}
17872870c43dSOctavian Purdila 
17882870c43dSOctavian Purdila 	/* ignore any bits we already processed */
17899ca1b22dSEric Dumazet 	poff += *off;
17909ca1b22dSEric Dumazet 	plen -= *off;
17912870c43dSOctavian Purdila 	*off = 0;
17922870c43dSOctavian Purdila 
179318aafc62SEric Dumazet 	do {
179418aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
17952870c43dSOctavian Purdila 
179618aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
179718aafc62SEric Dumazet 				  linear, sk))
1798a108d5f3SDavid S. Miller 			return true;
179918aafc62SEric Dumazet 		poff += flen;
180018aafc62SEric Dumazet 		plen -= flen;
18012870c43dSOctavian Purdila 		*len -= flen;
180218aafc62SEric Dumazet 	} while (*len && plen);
18032870c43dSOctavian Purdila 
1804a108d5f3SDavid S. Miller 	return false;
1805db43a282SOctavian Purdila }
18069c55e01cSJens Axboe 
18079c55e01cSJens Axboe /*
1808a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
18092870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
18109c55e01cSJens Axboe  */
1811a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
181235f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
181335f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
18142870c43dSOctavian Purdila {
18152870c43dSOctavian Purdila 	int seg;
18169c55e01cSJens Axboe 
18171d0c0b32SEric Dumazet 	/* map the linear part :
18182996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
18192996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
18202996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
18219c55e01cSJens Axboe 	 */
18222870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
18232870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
18242870c43dSOctavian Purdila 			     skb_headlen(skb),
182518aafc62SEric Dumazet 			     offset, len, spd,
18263a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
18271d0c0b32SEric Dumazet 			     sk, pipe))
1828a108d5f3SDavid S. Miller 		return true;
18299c55e01cSJens Axboe 
18309c55e01cSJens Axboe 	/*
18319c55e01cSJens Axboe 	 * then map the fragments
18329c55e01cSJens Axboe 	 */
18339c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
18349c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
18359c55e01cSJens Axboe 
1836ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
18379e903e08SEric Dumazet 				     f->page_offset, skb_frag_size(f),
183818aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
1839a108d5f3SDavid S. Miller 			return true;
18409c55e01cSJens Axboe 	}
18419c55e01cSJens Axboe 
1842a108d5f3SDavid S. Miller 	return false;
18439c55e01cSJens Axboe }
18449c55e01cSJens Axboe 
18459c55e01cSJens Axboe /*
18469c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
18479c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
18489c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
18499c55e01cSJens Axboe  * handle that cleanly.
18509c55e01cSJens Axboe  */
18518b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
18529c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
18539c55e01cSJens Axboe 		    unsigned int flags)
18549c55e01cSJens Axboe {
185541c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
185641c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
18579c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
18589c55e01cSJens Axboe 		.pages = pages,
18599c55e01cSJens Axboe 		.partial = partial,
1860047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
18619c55e01cSJens Axboe 		.flags = flags,
186228a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
18639c55e01cSJens Axboe 		.spd_release = sock_spd_release,
18649c55e01cSJens Axboe 	};
1865fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
18667a67e56fSJarek Poplawski 	struct sock *sk = skb->sk;
186735f3d14dSJens Axboe 	int ret = 0;
186835f3d14dSJens Axboe 
18699c55e01cSJens Axboe 	/*
18709c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
18719c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
18729c55e01cSJens Axboe 	 */
187335f3d14dSJens Axboe 	if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
18749c55e01cSJens Axboe 		goto done;
18759c55e01cSJens Axboe 	else if (!tlen)
18769c55e01cSJens Axboe 		goto done;
18779c55e01cSJens Axboe 
18789c55e01cSJens Axboe 	/*
18799c55e01cSJens Axboe 	 * now see if we have a frag_list to map
18809c55e01cSJens Axboe 	 */
1881fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
1882fbb398a8SDavid S. Miller 		if (!tlen)
18839c55e01cSJens Axboe 			break;
188435f3d14dSJens Axboe 		if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1885fbb398a8SDavid S. Miller 			break;
18869c55e01cSJens Axboe 	}
18879c55e01cSJens Axboe 
18889c55e01cSJens Axboe done:
18899c55e01cSJens Axboe 	if (spd.nr_pages) {
18909c55e01cSJens Axboe 		/*
18919c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
18929c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
18939c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
18949c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
18959c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
18969c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
18979c55e01cSJens Axboe 		 * and networking will grab the socket lock.
18989c55e01cSJens Axboe 		 */
1899293ad604SOctavian Purdila 		release_sock(sk);
19009c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1901293ad604SOctavian Purdila 		lock_sock(sk);
19029c55e01cSJens Axboe 	}
19039c55e01cSJens Axboe 
190435f3d14dSJens Axboe 	return ret;
19059c55e01cSJens Axboe }
19069c55e01cSJens Axboe 
1907357b40a1SHerbert Xu /**
1908357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1909357b40a1SHerbert Xu  *	@skb: destination buffer
1910357b40a1SHerbert Xu  *	@offset: offset in destination
1911357b40a1SHerbert Xu  *	@from: source buffer
1912357b40a1SHerbert Xu  *	@len: number of bytes to copy
1913357b40a1SHerbert Xu  *
1914357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1915357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1916357b40a1SHerbert Xu  *	traversing fragment lists and such.
1917357b40a1SHerbert Xu  */
1918357b40a1SHerbert Xu 
19190c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1920357b40a1SHerbert Xu {
19211a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1922fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1923fbb398a8SDavid S. Miller 	int i, copy;
1924357b40a1SHerbert Xu 
1925357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1926357b40a1SHerbert Xu 		goto fault;
1927357b40a1SHerbert Xu 
19281a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1929357b40a1SHerbert Xu 		if (copy > len)
1930357b40a1SHerbert Xu 			copy = len;
193127d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1932357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1933357b40a1SHerbert Xu 			return 0;
1934357b40a1SHerbert Xu 		offset += copy;
1935357b40a1SHerbert Xu 		from += copy;
1936357b40a1SHerbert Xu 	}
1937357b40a1SHerbert Xu 
1938357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1939357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
19401a028e50SDavid S. Miller 		int end;
1941357b40a1SHerbert Xu 
1942547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19431a028e50SDavid S. Miller 
19449e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
1945357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1946357b40a1SHerbert Xu 			u8 *vaddr;
1947357b40a1SHerbert Xu 
1948357b40a1SHerbert Xu 			if (copy > len)
1949357b40a1SHerbert Xu 				copy = len;
1950357b40a1SHerbert Xu 
195151c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
19521a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
19531a028e50SDavid S. Miller 			       from, copy);
195451c56b00SEric Dumazet 			kunmap_atomic(vaddr);
1955357b40a1SHerbert Xu 
1956357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1957357b40a1SHerbert Xu 				return 0;
1958357b40a1SHerbert Xu 			offset += copy;
1959357b40a1SHerbert Xu 			from += copy;
1960357b40a1SHerbert Xu 		}
19611a028e50SDavid S. Miller 		start = end;
1962357b40a1SHerbert Xu 	}
1963357b40a1SHerbert Xu 
1964fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
19651a028e50SDavid S. Miller 		int end;
1966357b40a1SHerbert Xu 
1967547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19681a028e50SDavid S. Miller 
1969fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
1970357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1971357b40a1SHerbert Xu 			if (copy > len)
1972357b40a1SHerbert Xu 				copy = len;
1973fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
19741a028e50SDavid S. Miller 					   from, copy))
1975357b40a1SHerbert Xu 				goto fault;
1976357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1977357b40a1SHerbert Xu 				return 0;
1978357b40a1SHerbert Xu 			offset += copy;
1979357b40a1SHerbert Xu 			from += copy;
1980357b40a1SHerbert Xu 		}
19811a028e50SDavid S. Miller 		start = end;
1982357b40a1SHerbert Xu 	}
1983357b40a1SHerbert Xu 	if (!len)
1984357b40a1SHerbert Xu 		return 0;
1985357b40a1SHerbert Xu 
1986357b40a1SHerbert Xu fault:
1987357b40a1SHerbert Xu 	return -EFAULT;
1988357b40a1SHerbert Xu }
1989357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1990357b40a1SHerbert Xu 
19911da177e4SLinus Torvalds /* Checksum skb data. */
19922817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
19932817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
19941da177e4SLinus Torvalds {
19951a028e50SDavid S. Miller 	int start = skb_headlen(skb);
19961a028e50SDavid S. Miller 	int i, copy = start - offset;
1997fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
19981da177e4SLinus Torvalds 	int pos = 0;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	/* Checksum header. */
20011da177e4SLinus Torvalds 	if (copy > 0) {
20021da177e4SLinus Torvalds 		if (copy > len)
20031da177e4SLinus Torvalds 			copy = len;
20042817a336SDaniel Borkmann 		csum = ops->update(skb->data + offset, copy, csum);
20051da177e4SLinus Torvalds 		if ((len -= copy) == 0)
20061da177e4SLinus Torvalds 			return csum;
20071da177e4SLinus Torvalds 		offset += copy;
20081da177e4SLinus Torvalds 		pos	= copy;
20091da177e4SLinus Torvalds 	}
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20121a028e50SDavid S. Miller 		int end;
201351c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
20141da177e4SLinus Torvalds 
2015547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20161a028e50SDavid S. Miller 
201751c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
20181da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
201944bb9363SAl Viro 			__wsum csum2;
20201da177e4SLinus Torvalds 			u8 *vaddr;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 			if (copy > len)
20231da177e4SLinus Torvalds 				copy = len;
202451c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
20252817a336SDaniel Borkmann 			csum2 = ops->update(vaddr + frag->page_offset +
20261a028e50SDavid S. Miller 					    offset - start, copy, 0);
202751c56b00SEric Dumazet 			kunmap_atomic(vaddr);
20282817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
20291da177e4SLinus Torvalds 			if (!(len -= copy))
20301da177e4SLinus Torvalds 				return csum;
20311da177e4SLinus Torvalds 			offset += copy;
20321da177e4SLinus Torvalds 			pos    += copy;
20331da177e4SLinus Torvalds 		}
20341a028e50SDavid S. Miller 		start = end;
20351da177e4SLinus Torvalds 	}
20361da177e4SLinus Torvalds 
2037fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
20381a028e50SDavid S. Miller 		int end;
20391da177e4SLinus Torvalds 
2040547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20411a028e50SDavid S. Miller 
2042fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
20431da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
20445f92a738SAl Viro 			__wsum csum2;
20451da177e4SLinus Torvalds 			if (copy > len)
20461da177e4SLinus Torvalds 				copy = len;
20472817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
20482817a336SDaniel Borkmann 					       copy, 0, ops);
20492817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
20501da177e4SLinus Torvalds 			if ((len -= copy) == 0)
20511da177e4SLinus Torvalds 				return csum;
20521da177e4SLinus Torvalds 			offset += copy;
20531da177e4SLinus Torvalds 			pos    += copy;
20541da177e4SLinus Torvalds 		}
20551a028e50SDavid S. Miller 		start = end;
20561da177e4SLinus Torvalds 	}
205709a62660SKris Katterjohn 	BUG_ON(len);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	return csum;
20601da177e4SLinus Torvalds }
20612817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
20622817a336SDaniel Borkmann 
20632817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
20642817a336SDaniel Borkmann 		    int len, __wsum csum)
20652817a336SDaniel Borkmann {
20662817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2067cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
20682817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
20692817a336SDaniel Borkmann 	};
20702817a336SDaniel Borkmann 
20712817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
20722817a336SDaniel Borkmann }
2073b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds /* Both of above in one bottle. */
20761da177e4SLinus Torvalds 
207781d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
207881d77662SAl Viro 				    u8 *to, int len, __wsum csum)
20791da177e4SLinus Torvalds {
20801a028e50SDavid S. Miller 	int start = skb_headlen(skb);
20811a028e50SDavid S. Miller 	int i, copy = start - offset;
2082fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
20831da177e4SLinus Torvalds 	int pos = 0;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* Copy header. */
20861da177e4SLinus Torvalds 	if (copy > 0) {
20871da177e4SLinus Torvalds 		if (copy > len)
20881da177e4SLinus Torvalds 			copy = len;
20891da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
20901da177e4SLinus Torvalds 						 copy, csum);
20911da177e4SLinus Torvalds 		if ((len -= copy) == 0)
20921da177e4SLinus Torvalds 			return csum;
20931da177e4SLinus Torvalds 		offset += copy;
20941da177e4SLinus Torvalds 		to     += copy;
20951da177e4SLinus Torvalds 		pos	= copy;
20961da177e4SLinus Torvalds 	}
20971da177e4SLinus Torvalds 
20981da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20991a028e50SDavid S. Miller 		int end;
21001da177e4SLinus Torvalds 
2101547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21021a028e50SDavid S. Miller 
21039e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
21041da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21055084205fSAl Viro 			__wsum csum2;
21061da177e4SLinus Torvalds 			u8 *vaddr;
21071da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 			if (copy > len)
21101da177e4SLinus Torvalds 				copy = len;
211151c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
21121da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
21131a028e50SDavid S. Miller 							  frag->page_offset +
21141a028e50SDavid S. Miller 							  offset - start, to,
21151a028e50SDavid S. Miller 							  copy, 0);
211651c56b00SEric Dumazet 			kunmap_atomic(vaddr);
21171da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
21181da177e4SLinus Torvalds 			if (!(len -= copy))
21191da177e4SLinus Torvalds 				return csum;
21201da177e4SLinus Torvalds 			offset += copy;
21211da177e4SLinus Torvalds 			to     += copy;
21221da177e4SLinus Torvalds 			pos    += copy;
21231da177e4SLinus Torvalds 		}
21241a028e50SDavid S. Miller 		start = end;
21251da177e4SLinus Torvalds 	}
21261da177e4SLinus Torvalds 
2127fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
212881d77662SAl Viro 		__wsum csum2;
21291a028e50SDavid S. Miller 		int end;
21301da177e4SLinus Torvalds 
2131547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21321a028e50SDavid S. Miller 
2133fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
21341da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21351da177e4SLinus Torvalds 			if (copy > len)
21361da177e4SLinus Torvalds 				copy = len;
2137fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
21381a028e50SDavid S. Miller 						       offset - start,
21391da177e4SLinus Torvalds 						       to, copy, 0);
21401da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
21411da177e4SLinus Torvalds 			if ((len -= copy) == 0)
21421da177e4SLinus Torvalds 				return csum;
21431da177e4SLinus Torvalds 			offset += copy;
21441da177e4SLinus Torvalds 			to     += copy;
21451da177e4SLinus Torvalds 			pos    += copy;
21461da177e4SLinus Torvalds 		}
21471a028e50SDavid S. Miller 		start = end;
21481da177e4SLinus Torvalds 	}
214909a62660SKris Katterjohn 	BUG_ON(len);
21501da177e4SLinus Torvalds 	return csum;
21511da177e4SLinus Torvalds }
2152b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
21531da177e4SLinus Torvalds 
2154af2806f8SThomas Graf  /**
2155af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2156af2806f8SThomas Graf  *	@from: source buffer
2157af2806f8SThomas Graf  *
2158af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2159af2806f8SThomas Graf  *	into skb_zerocopy().
2160af2806f8SThomas Graf  */
2161af2806f8SThomas Graf unsigned int
2162af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2163af2806f8SThomas Graf {
2164af2806f8SThomas Graf 	unsigned int hlen = 0;
2165af2806f8SThomas Graf 
2166af2806f8SThomas Graf 	if (!from->head_frag ||
2167af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2168af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2169af2806f8SThomas Graf 		hlen = skb_headlen(from);
2170af2806f8SThomas Graf 
2171af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2172af2806f8SThomas Graf 		hlen = from->len;
2173af2806f8SThomas Graf 
2174af2806f8SThomas Graf 	return hlen;
2175af2806f8SThomas Graf }
2176af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2177af2806f8SThomas Graf 
2178af2806f8SThomas Graf /**
2179af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2180af2806f8SThomas Graf  *	@to: destination buffer
21817fceb4deSMasanari Iida  *	@from: source buffer
2182af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2183af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2184af2806f8SThomas Graf  *
2185af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2186af2806f8SThomas Graf  *	to the frags in the source buffer.
2187af2806f8SThomas Graf  *
2188af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2189af2806f8SThomas Graf  *	headroom in the `to` buffer.
219036d5fe6aSZoltan Kiss  *
219136d5fe6aSZoltan Kiss  *	Return value:
219236d5fe6aSZoltan Kiss  *	0: everything is OK
219336d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
219436d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2195af2806f8SThomas Graf  */
219636d5fe6aSZoltan Kiss int
219736d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2198af2806f8SThomas Graf {
2199af2806f8SThomas Graf 	int i, j = 0;
2200af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
220136d5fe6aSZoltan Kiss 	int ret;
2202af2806f8SThomas Graf 	struct page *page;
2203af2806f8SThomas Graf 	unsigned int offset;
2204af2806f8SThomas Graf 
2205af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2206af2806f8SThomas Graf 
2207af2806f8SThomas Graf 	/* dont bother with small payloads */
220836d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
220936d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2210af2806f8SThomas Graf 
2211af2806f8SThomas Graf 	if (hlen) {
221236d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
221336d5fe6aSZoltan Kiss 		if (unlikely(ret))
221436d5fe6aSZoltan Kiss 			return ret;
2215af2806f8SThomas Graf 		len -= hlen;
2216af2806f8SThomas Graf 	} else {
2217af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2218af2806f8SThomas Graf 		if (plen) {
2219af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2220af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2221af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2222af2806f8SThomas Graf 			get_page(page);
2223af2806f8SThomas Graf 			j = 1;
2224af2806f8SThomas Graf 			len -= plen;
2225af2806f8SThomas Graf 		}
2226af2806f8SThomas Graf 	}
2227af2806f8SThomas Graf 
2228af2806f8SThomas Graf 	to->truesize += len + plen;
2229af2806f8SThomas Graf 	to->len += len + plen;
2230af2806f8SThomas Graf 	to->data_len += len + plen;
2231af2806f8SThomas Graf 
223236d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
223336d5fe6aSZoltan Kiss 		skb_tx_error(from);
223436d5fe6aSZoltan Kiss 		return -ENOMEM;
223536d5fe6aSZoltan Kiss 	}
223636d5fe6aSZoltan Kiss 
2237af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2238af2806f8SThomas Graf 		if (!len)
2239af2806f8SThomas Graf 			break;
2240af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2241af2806f8SThomas Graf 		skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2242af2806f8SThomas Graf 		len -= skb_shinfo(to)->frags[j].size;
2243af2806f8SThomas Graf 		skb_frag_ref(to, j);
2244af2806f8SThomas Graf 		j++;
2245af2806f8SThomas Graf 	}
2246af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
224736d5fe6aSZoltan Kiss 
224836d5fe6aSZoltan Kiss 	return 0;
2249af2806f8SThomas Graf }
2250af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
2251af2806f8SThomas Graf 
22521da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
22531da177e4SLinus Torvalds {
2254d3bc23e7SAl Viro 	__wsum csum;
22551da177e4SLinus Torvalds 	long csstart;
22561da177e4SLinus Torvalds 
225784fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
225855508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
22591da177e4SLinus Torvalds 	else
22601da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
22611da177e4SLinus Torvalds 
226209a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
22631da177e4SLinus Torvalds 
2264d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds 	csum = 0;
22671da177e4SLinus Torvalds 	if (csstart != skb->len)
22681da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
22691da177e4SLinus Torvalds 					      skb->len - csstart, 0);
22701da177e4SLinus Torvalds 
227184fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2272ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
22731da177e4SLinus Torvalds 
2274d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
22751da177e4SLinus Torvalds 	}
22761da177e4SLinus Torvalds }
2277b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
22781da177e4SLinus Torvalds 
22791da177e4SLinus Torvalds /**
22801da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
22811da177e4SLinus Torvalds  *	@list: list to dequeue from
22821da177e4SLinus Torvalds  *
22831da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
22841da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
22851da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
22861da177e4SLinus Torvalds  */
22871da177e4SLinus Torvalds 
22881da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
22891da177e4SLinus Torvalds {
22901da177e4SLinus Torvalds 	unsigned long flags;
22911da177e4SLinus Torvalds 	struct sk_buff *result;
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
22941da177e4SLinus Torvalds 	result = __skb_dequeue(list);
22951da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
22961da177e4SLinus Torvalds 	return result;
22971da177e4SLinus Torvalds }
2298b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds /**
23011da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
23021da177e4SLinus Torvalds  *	@list: list to dequeue from
23031da177e4SLinus Torvalds  *
23041da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
23051da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
23061da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
23071da177e4SLinus Torvalds  */
23081da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
23091da177e4SLinus Torvalds {
23101da177e4SLinus Torvalds 	unsigned long flags;
23111da177e4SLinus Torvalds 	struct sk_buff *result;
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23141da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
23151da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23161da177e4SLinus Torvalds 	return result;
23171da177e4SLinus Torvalds }
2318b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
23191da177e4SLinus Torvalds 
23201da177e4SLinus Torvalds /**
23211da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
23221da177e4SLinus Torvalds  *	@list: list to empty
23231da177e4SLinus Torvalds  *
23241da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
23251da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
23261da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
23271da177e4SLinus Torvalds  */
23281da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
23291da177e4SLinus Torvalds {
23301da177e4SLinus Torvalds 	struct sk_buff *skb;
23311da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
23321da177e4SLinus Torvalds 		kfree_skb(skb);
23331da177e4SLinus Torvalds }
2334b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
23351da177e4SLinus Torvalds 
23361da177e4SLinus Torvalds /**
23371da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
23381da177e4SLinus Torvalds  *	@list: list to use
23391da177e4SLinus Torvalds  *	@newsk: buffer to queue
23401da177e4SLinus Torvalds  *
23411da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
23421da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
23431da177e4SLinus Torvalds  *	safely.
23441da177e4SLinus Torvalds  *
23451da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23461da177e4SLinus Torvalds  */
23471da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
23481da177e4SLinus Torvalds {
23491da177e4SLinus Torvalds 	unsigned long flags;
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23521da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
23531da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23541da177e4SLinus Torvalds }
2355b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
23561da177e4SLinus Torvalds 
23571da177e4SLinus Torvalds /**
23581da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
23591da177e4SLinus Torvalds  *	@list: list to use
23601da177e4SLinus Torvalds  *	@newsk: buffer to queue
23611da177e4SLinus Torvalds  *
23621da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
23631da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
23641da177e4SLinus Torvalds  *	safely.
23651da177e4SLinus Torvalds  *
23661da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
23671da177e4SLinus Torvalds  */
23681da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
23691da177e4SLinus Torvalds {
23701da177e4SLinus Torvalds 	unsigned long flags;
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23731da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
23741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23751da177e4SLinus Torvalds }
2376b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
23778728b834SDavid S. Miller 
23781da177e4SLinus Torvalds /**
23791da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
23801da177e4SLinus Torvalds  *	@skb: buffer to remove
23818728b834SDavid S. Miller  *	@list: list to use
23821da177e4SLinus Torvalds  *
23838728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
23848728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
23851da177e4SLinus Torvalds  *
23868728b834SDavid S. Miller  *	You must know what list the SKB is on.
23871da177e4SLinus Torvalds  */
23888728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
23891da177e4SLinus Torvalds {
23901da177e4SLinus Torvalds 	unsigned long flags;
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23938728b834SDavid S. Miller 	__skb_unlink(skb, list);
23941da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23951da177e4SLinus Torvalds }
2396b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds /**
23991da177e4SLinus Torvalds  *	skb_append	-	append a buffer
24001da177e4SLinus Torvalds  *	@old: buffer to insert after
24011da177e4SLinus Torvalds  *	@newsk: buffer to insert
24028728b834SDavid S. Miller  *	@list: list to use
24031da177e4SLinus Torvalds  *
24041da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
24051da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
24061da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24071da177e4SLinus Torvalds  */
24088728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
24091da177e4SLinus Torvalds {
24101da177e4SLinus Torvalds 	unsigned long flags;
24111da177e4SLinus Torvalds 
24128728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
24137de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
24148728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
24151da177e4SLinus Torvalds }
2416b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
24171da177e4SLinus Torvalds 
24181da177e4SLinus Torvalds /**
24191da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
24201da177e4SLinus Torvalds  *	@old: buffer to insert before
24211da177e4SLinus Torvalds  *	@newsk: buffer to insert
24228728b834SDavid S. Miller  *	@list: list to use
24231da177e4SLinus Torvalds  *
24248728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
24258728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
24268728b834SDavid S. Miller  *	calls.
24278728b834SDavid S. Miller  *
24281da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24291da177e4SLinus Torvalds  */
24308728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
24311da177e4SLinus Torvalds {
24321da177e4SLinus Torvalds 	unsigned long flags;
24331da177e4SLinus Torvalds 
24348728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
24358728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
24368728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
24371da177e4SLinus Torvalds }
2438b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
24411da177e4SLinus Torvalds 					   struct sk_buff* skb1,
24421da177e4SLinus Torvalds 					   const u32 len, const int pos)
24431da177e4SLinus Torvalds {
24441da177e4SLinus Torvalds 	int i;
24451da177e4SLinus Torvalds 
2446d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2447d626f62bSArnaldo Carvalho de Melo 					 pos - len);
24481da177e4SLinus Torvalds 	/* And move data appendix as is. */
24491da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
24501da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
24511da177e4SLinus Torvalds 
24521da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
24531da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
24541da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
24551da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
24561da177e4SLinus Torvalds 	skb->data_len		   = 0;
24571da177e4SLinus Torvalds 	skb->len		   = len;
245827a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
24591da177e4SLinus Torvalds }
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
24621da177e4SLinus Torvalds 				       struct sk_buff* skb1,
24631da177e4SLinus Torvalds 				       const u32 len, int pos)
24641da177e4SLinus Torvalds {
24651da177e4SLinus Torvalds 	int i, k = 0;
24661da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
24691da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
24701da177e4SLinus Torvalds 	skb->len		  = len;
24711da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
24721da177e4SLinus Torvalds 
24731da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
24749e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 		if (pos + size > len) {
24771da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
24781da177e4SLinus Torvalds 
24791da177e4SLinus Torvalds 			if (pos < len) {
24801da177e4SLinus Torvalds 				/* Split frag.
24811da177e4SLinus Torvalds 				 * We have two variants in this case:
24821da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
24831da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
24841da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
24851da177e4SLinus Torvalds 				 *    where splitting is expensive.
24861da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
24871da177e4SLinus Torvalds 				 */
2488ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
24891da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
24909e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
24919e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
24921da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
24931da177e4SLinus Torvalds 			}
24941da177e4SLinus Torvalds 			k++;
24951da177e4SLinus Torvalds 		} else
24961da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
24971da177e4SLinus Torvalds 		pos += size;
24981da177e4SLinus Torvalds 	}
24991da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
25001da177e4SLinus Torvalds }
25011da177e4SLinus Torvalds 
25021da177e4SLinus Torvalds /**
25031da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
25041da177e4SLinus Torvalds  * @skb: the buffer to split
25051da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
25061da177e4SLinus Torvalds  * @len: new length for skb
25071da177e4SLinus Torvalds  */
25081da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
25091da177e4SLinus Torvalds {
25101da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
25111da177e4SLinus Torvalds 
251268534c68SAmerigo Wang 	skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
25131da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
25141da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
25151da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
25161da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
25171da177e4SLinus Torvalds }
2518b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
25191da177e4SLinus Torvalds 
25209f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
25219f782db3SIlpo Järvinen  *
25229f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
25239f782db3SIlpo Järvinen  */
2524832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2525832d11c5SIlpo Järvinen {
25260ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2527832d11c5SIlpo Järvinen }
2528832d11c5SIlpo Järvinen 
2529832d11c5SIlpo Järvinen /**
2530832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2531832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2532832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2533832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2534832d11c5SIlpo Järvinen  *
2535832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
253620e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2537832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2538832d11c5SIlpo Järvinen  *
2539832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2540832d11c5SIlpo Järvinen  *
2541832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2542832d11c5SIlpo Järvinen  * to have non-paged data as well.
2543832d11c5SIlpo Järvinen  *
2544832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2545832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2546832d11c5SIlpo Järvinen  */
2547832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2548832d11c5SIlpo Järvinen {
2549832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2550832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2551832d11c5SIlpo Järvinen 
2552832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2553832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2554832d11c5SIlpo Järvinen 
2555832d11c5SIlpo Järvinen 	todo = shiftlen;
2556832d11c5SIlpo Järvinen 	from = 0;
2557832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2558832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2559832d11c5SIlpo Järvinen 
2560832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2561832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2562832d11c5SIlpo Järvinen 	 */
2563832d11c5SIlpo Järvinen 	if (!to ||
2564ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2565ea2ab693SIan Campbell 			      fragfrom->page_offset)) {
2566832d11c5SIlpo Järvinen 		merge = -1;
2567832d11c5SIlpo Järvinen 	} else {
2568832d11c5SIlpo Järvinen 		merge = to - 1;
2569832d11c5SIlpo Järvinen 
25709e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
2571832d11c5SIlpo Järvinen 		if (todo < 0) {
2572832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2573832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2574832d11c5SIlpo Järvinen 				return 0;
2575832d11c5SIlpo Järvinen 
25769f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
25779f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2578832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2579832d11c5SIlpo Järvinen 
25809e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
25819e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
2582832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2583832d11c5SIlpo Järvinen 
2584832d11c5SIlpo Järvinen 			goto onlymerged;
2585832d11c5SIlpo Järvinen 		}
2586832d11c5SIlpo Järvinen 
2587832d11c5SIlpo Järvinen 		from++;
2588832d11c5SIlpo Järvinen 	}
2589832d11c5SIlpo Järvinen 
2590832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2591832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2592832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2593832d11c5SIlpo Järvinen 		return 0;
2594832d11c5SIlpo Järvinen 
2595832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2596832d11c5SIlpo Järvinen 		return 0;
2597832d11c5SIlpo Järvinen 
2598832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2599832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2600832d11c5SIlpo Järvinen 			return 0;
2601832d11c5SIlpo Järvinen 
2602832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2603832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2604832d11c5SIlpo Järvinen 
26059e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
2606832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
26079e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
2608832d11c5SIlpo Järvinen 			from++;
2609832d11c5SIlpo Järvinen 			to++;
2610832d11c5SIlpo Järvinen 
2611832d11c5SIlpo Järvinen 		} else {
2612ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
2613832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2614832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
26159e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
2616832d11c5SIlpo Järvinen 
2617832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
26189e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
2619832d11c5SIlpo Järvinen 			todo = 0;
2620832d11c5SIlpo Järvinen 
2621832d11c5SIlpo Järvinen 			to++;
2622832d11c5SIlpo Järvinen 			break;
2623832d11c5SIlpo Järvinen 		}
2624832d11c5SIlpo Järvinen 	}
2625832d11c5SIlpo Järvinen 
2626832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2627832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2628832d11c5SIlpo Järvinen 
2629832d11c5SIlpo Järvinen 	if (merge >= 0) {
2630832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2631832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2632832d11c5SIlpo Järvinen 
26339e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2634ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
2635832d11c5SIlpo Järvinen 	}
2636832d11c5SIlpo Järvinen 
2637832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2638832d11c5SIlpo Järvinen 	to = 0;
2639832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2640832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2641832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2642832d11c5SIlpo Järvinen 
2643832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2644832d11c5SIlpo Järvinen 
2645832d11c5SIlpo Järvinen onlymerged:
2646832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2647832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2648832d11c5SIlpo Järvinen 	 */
2649832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2650832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2651832d11c5SIlpo Järvinen 
2652832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2653832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2654832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2655832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2656832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2657832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2658832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2659832d11c5SIlpo Järvinen 
2660832d11c5SIlpo Järvinen 	return shiftlen;
2661832d11c5SIlpo Järvinen }
2662832d11c5SIlpo Järvinen 
2663677e90edSThomas Graf /**
2664677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2665677e90edSThomas Graf  * @skb: the buffer to read
2666677e90edSThomas Graf  * @from: lower offset of data to be read
2667677e90edSThomas Graf  * @to: upper offset of data to be read
2668677e90edSThomas Graf  * @st: state variable
2669677e90edSThomas Graf  *
2670677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2671677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2672677e90edSThomas Graf  */
2673677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2674677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2675677e90edSThomas Graf {
2676677e90edSThomas Graf 	st->lower_offset = from;
2677677e90edSThomas Graf 	st->upper_offset = to;
2678677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2679677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2680677e90edSThomas Graf 	st->frag_data = NULL;
2681677e90edSThomas Graf }
2682b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2683677e90edSThomas Graf 
2684677e90edSThomas Graf /**
2685677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2686677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2687677e90edSThomas Graf  * @data: destination pointer for data to be returned
2688677e90edSThomas Graf  * @st: state variable
2689677e90edSThomas Graf  *
2690bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
2691677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2692bc32383cSMathias Krause  * the head of the data block to @data and returns the length
2693677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2694677e90edSThomas Graf  * offset has been reached.
2695677e90edSThomas Graf  *
2696677e90edSThomas Graf  * The caller is not required to consume all of the data
2697bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
2698677e90edSThomas Graf  * of bytes already consumed and the next call to
2699677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2700677e90edSThomas Graf  *
270125985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
2702e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
2703677e90edSThomas Graf  *       reads of potentially non linear data.
2704677e90edSThomas Graf  *
2705bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2706677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2707677e90edSThomas Graf  *       a stack for this purpose.
2708677e90edSThomas Graf  */
2709677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2710677e90edSThomas Graf 			  struct skb_seq_state *st)
2711677e90edSThomas Graf {
2712677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2713677e90edSThomas Graf 	skb_frag_t *frag;
2714677e90edSThomas Graf 
2715aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
2716aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
2717aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
2718aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
2719aeb193eaSWedson Almeida Filho 		}
2720677e90edSThomas Graf 		return 0;
2721aeb193eaSWedson Almeida Filho 	}
2722677e90edSThomas Graf 
2723677e90edSThomas Graf next_skb:
272495e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2725677e90edSThomas Graf 
2726995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
272795e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2728677e90edSThomas Graf 		return block_limit - abs_offset;
2729677e90edSThomas Graf 	}
2730677e90edSThomas Graf 
2731677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2732677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2733677e90edSThomas Graf 
2734677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2735677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
27369e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
2737677e90edSThomas Graf 
2738677e90edSThomas Graf 		if (abs_offset < block_limit) {
2739677e90edSThomas Graf 			if (!st->frag_data)
274051c56b00SEric Dumazet 				st->frag_data = kmap_atomic(skb_frag_page(frag));
2741677e90edSThomas Graf 
2742677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2743677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2744677e90edSThomas Graf 
2745677e90edSThomas Graf 			return block_limit - abs_offset;
2746677e90edSThomas Graf 		}
2747677e90edSThomas Graf 
2748677e90edSThomas Graf 		if (st->frag_data) {
274951c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
2750677e90edSThomas Graf 			st->frag_data = NULL;
2751677e90edSThomas Graf 		}
2752677e90edSThomas Graf 
2753677e90edSThomas Graf 		st->frag_idx++;
27549e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
2755677e90edSThomas Graf 	}
2756677e90edSThomas Graf 
27575b5a60daSOlaf Kirch 	if (st->frag_data) {
275851c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
27595b5a60daSOlaf Kirch 		st->frag_data = NULL;
27605b5a60daSOlaf Kirch 	}
27615b5a60daSOlaf Kirch 
276221dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2763677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
276495e3b24cSHerbert Xu 		st->frag_idx = 0;
2765677e90edSThomas Graf 		goto next_skb;
276671b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
276771b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
276871b3346dSShyam Iyer 		st->frag_idx = 0;
2769677e90edSThomas Graf 		goto next_skb;
2770677e90edSThomas Graf 	}
2771677e90edSThomas Graf 
2772677e90edSThomas Graf 	return 0;
2773677e90edSThomas Graf }
2774b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2775677e90edSThomas Graf 
2776677e90edSThomas Graf /**
2777677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2778677e90edSThomas Graf  * @st: state variable
2779677e90edSThomas Graf  *
2780677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2781677e90edSThomas Graf  * returned 0.
2782677e90edSThomas Graf  */
2783677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2784677e90edSThomas Graf {
2785677e90edSThomas Graf 	if (st->frag_data)
278651c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
2787677e90edSThomas Graf }
2788b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2789677e90edSThomas Graf 
27903fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
27913fc7e8a6SThomas Graf 
27923fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
27933fc7e8a6SThomas Graf 					  struct ts_config *conf,
27943fc7e8a6SThomas Graf 					  struct ts_state *state)
27953fc7e8a6SThomas Graf {
27963fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
27973fc7e8a6SThomas Graf }
27983fc7e8a6SThomas Graf 
27993fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
28003fc7e8a6SThomas Graf {
28013fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
28023fc7e8a6SThomas Graf }
28033fc7e8a6SThomas Graf 
28043fc7e8a6SThomas Graf /**
28053fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
28063fc7e8a6SThomas Graf  * @skb: the buffer to look in
28073fc7e8a6SThomas Graf  * @from: search offset
28083fc7e8a6SThomas Graf  * @to: search limit
28093fc7e8a6SThomas Graf  * @config: textsearch configuration
28103fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
28113fc7e8a6SThomas Graf  *
28123fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
28133fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
28143fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
28153fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
28163fc7e8a6SThomas Graf  */
28173fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
28183fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
28193fc7e8a6SThomas Graf 			   struct ts_state *state)
28203fc7e8a6SThomas Graf {
2821f72b948dSPhil Oester 	unsigned int ret;
2822f72b948dSPhil Oester 
28233fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
28243fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
28253fc7e8a6SThomas Graf 
28263fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
28273fc7e8a6SThomas Graf 
2828f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2829f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
28303fc7e8a6SThomas Graf }
2831b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
28323fc7e8a6SThomas Graf 
2833e89e9cf5SAnanda Raju /**
28342c53040fSBen Hutchings  * skb_append_datato_frags - append the user data to a skb
2835e89e9cf5SAnanda Raju  * @sk: sock  structure
2836e793c0f7SMasanari Iida  * @skb: skb structure to be appended with user data.
2837e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2838e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2839e89e9cf5SAnanda Raju  * @length: length of the iov message
2840e89e9cf5SAnanda Raju  *
2841e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2842e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2843e89e9cf5SAnanda Raju  */
2844e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2845dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2846e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2847e89e9cf5SAnanda Raju 			void *from, int length)
2848e89e9cf5SAnanda Raju {
2849b2111724SEric Dumazet 	int frg_cnt = skb_shinfo(skb)->nr_frags;
2850b2111724SEric Dumazet 	int copy;
2851e89e9cf5SAnanda Raju 	int offset = 0;
2852e89e9cf5SAnanda Raju 	int ret;
2853b2111724SEric Dumazet 	struct page_frag *pfrag = &current->task_frag;
2854e89e9cf5SAnanda Raju 
2855e89e9cf5SAnanda Raju 	do {
2856e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2857e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2858b2111724SEric Dumazet 			return -EMSGSIZE;
2859e89e9cf5SAnanda Raju 
2860b2111724SEric Dumazet 		if (!sk_page_frag_refill(sk, pfrag))
2861e89e9cf5SAnanda Raju 			return -ENOMEM;
2862e89e9cf5SAnanda Raju 
2863e89e9cf5SAnanda Raju 		/* copy the user data to page */
2864b2111724SEric Dumazet 		copy = min_t(int, length, pfrag->size - pfrag->offset);
2865e89e9cf5SAnanda Raju 
2866b2111724SEric Dumazet 		ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2867e89e9cf5SAnanda Raju 			      offset, copy, 0, skb);
2868e89e9cf5SAnanda Raju 		if (ret < 0)
2869e89e9cf5SAnanda Raju 			return -EFAULT;
2870e89e9cf5SAnanda Raju 
2871e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2872b2111724SEric Dumazet 		skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2873b2111724SEric Dumazet 				   copy);
2874b2111724SEric Dumazet 		frg_cnt++;
2875b2111724SEric Dumazet 		pfrag->offset += copy;
2876b2111724SEric Dumazet 		get_page(pfrag->page);
2877b2111724SEric Dumazet 
2878b2111724SEric Dumazet 		skb->truesize += copy;
2879b2111724SEric Dumazet 		atomic_add(copy, &sk->sk_wmem_alloc);
2880e89e9cf5SAnanda Raju 		skb->len += copy;
2881e89e9cf5SAnanda Raju 		skb->data_len += copy;
2882e89e9cf5SAnanda Raju 		offset += copy;
2883e89e9cf5SAnanda Raju 		length -= copy;
2884e89e9cf5SAnanda Raju 
2885e89e9cf5SAnanda Raju 	} while (length > 0);
2886e89e9cf5SAnanda Raju 
2887e89e9cf5SAnanda Raju 	return 0;
2888e89e9cf5SAnanda Raju }
2889b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
2890e89e9cf5SAnanda Raju 
2891cbb042f9SHerbert Xu /**
2892cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2893cbb042f9SHerbert Xu  *	@skb: buffer to update
2894cbb042f9SHerbert Xu  *	@len: length of data pulled
2895cbb042f9SHerbert Xu  *
2896cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2897fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
289884fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
289984fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
290084fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2901cbb042f9SHerbert Xu  */
2902cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2903cbb042f9SHerbert Xu {
2904cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2905cbb042f9SHerbert Xu 	skb->len -= len;
2906cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2907cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2908cbb042f9SHerbert Xu 	return skb->data += len;
2909cbb042f9SHerbert Xu }
2910f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2911f94691acSArnaldo Carvalho de Melo 
2912f4c50d99SHerbert Xu /**
2913f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2914df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
2915576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2916f4c50d99SHerbert Xu  *
2917f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
29184c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
29194c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2920f4c50d99SHerbert Xu  */
2921df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
2922df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
2923f4c50d99SHerbert Xu {
2924f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2925f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
29261a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2927df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2928df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
2929df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
29301fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
2931f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2932df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2933f4c50d99SHerbert Xu 	unsigned int headroom;
2934f4c50d99SHerbert Xu 	unsigned int len;
2935ec5f0615SPravin B Shelar 	__be16 proto;
2936ec5f0615SPravin B Shelar 	bool csum;
293704ed3e74SMichał Mirosław 	int sg = !!(features & NETIF_F_SG);
2938df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
2939f4c50d99SHerbert Xu 	int err = -ENOMEM;
2940f4c50d99SHerbert Xu 	int i = 0;
2941f4c50d99SHerbert Xu 	int pos;
294253d6471cSVlad Yasevich 	int dummy;
2943f4c50d99SHerbert Xu 
29445882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
294553d6471cSVlad Yasevich 	proto = skb_network_protocol(head_skb, &dummy);
2946ec5f0615SPravin B Shelar 	if (unlikely(!proto))
2947ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
2948ec5f0615SPravin B Shelar 
29497e2b10c1STom Herbert 	csum = !head_skb->encap_hdr_csum &&
29507e2b10c1STom Herbert 	    !!can_checksum_protocol(features, proto);
29517e2b10c1STom Herbert 
2952df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
2953df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
2954f4c50d99SHerbert Xu 
2955f4c50d99SHerbert Xu 	do {
2956f4c50d99SHerbert Xu 		struct sk_buff *nskb;
29578cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
2958c8884eddSHerbert Xu 		int hsize;
2959f4c50d99SHerbert Xu 		int size;
2960f4c50d99SHerbert Xu 
2961df5771ffSMichael S. Tsirkin 		len = head_skb->len - offset;
2962f4c50d99SHerbert Xu 		if (len > mss)
2963f4c50d99SHerbert Xu 			len = mss;
2964f4c50d99SHerbert Xu 
2965df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
2966f4c50d99SHerbert Xu 		if (hsize < 0)
2967f4c50d99SHerbert Xu 			hsize = 0;
2968c8884eddSHerbert Xu 		if (hsize > len || !sg)
2969c8884eddSHerbert Xu 			hsize = len;
2970f4c50d99SHerbert Xu 
29711a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
29721a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
29731a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
297489319d38SHerbert Xu 
29759d8506ccSHerbert Xu 			i = 0;
29761a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
29771a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
29781fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
29791a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
29809d8506ccSHerbert Xu 
29819d8506ccSHerbert Xu 			while (pos < offset + len) {
29829d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
29839d8506ccSHerbert Xu 
29844e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
29859d8506ccSHerbert Xu 				if (pos + size > offset + len)
29869d8506ccSHerbert Xu 					break;
29879d8506ccSHerbert Xu 
29889d8506ccSHerbert Xu 				i++;
29899d8506ccSHerbert Xu 				pos += size;
29904e1beba1SMichael S. Tsirkin 				frag++;
29919d8506ccSHerbert Xu 			}
29929d8506ccSHerbert Xu 
29931a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
29941a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
299589319d38SHerbert Xu 
2996f4c50d99SHerbert Xu 			if (unlikely(!nskb))
2997f4c50d99SHerbert Xu 				goto err;
2998f4c50d99SHerbert Xu 
29999d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
30009d8506ccSHerbert Xu 				kfree_skb(nskb);
30019d8506ccSHerbert Xu 				goto err;
30029d8506ccSHerbert Xu 			}
30039d8506ccSHerbert Xu 
3004ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
300589319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
300689319d38SHerbert Xu 				kfree_skb(nskb);
300789319d38SHerbert Xu 				goto err;
300889319d38SHerbert Xu 			}
300989319d38SHerbert Xu 
3010ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
301189319d38SHerbert Xu 			skb_release_head_state(nskb);
301289319d38SHerbert Xu 			__skb_push(nskb, doffset);
301389319d38SHerbert Xu 		} else {
3014c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
3015df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3016c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
301789319d38SHerbert Xu 
301889319d38SHerbert Xu 			if (unlikely(!nskb))
301989319d38SHerbert Xu 				goto err;
302089319d38SHerbert Xu 
302189319d38SHerbert Xu 			skb_reserve(nskb, headroom);
302289319d38SHerbert Xu 			__skb_put(nskb, doffset);
302389319d38SHerbert Xu 		}
302489319d38SHerbert Xu 
3025f4c50d99SHerbert Xu 		if (segs)
3026f4c50d99SHerbert Xu 			tail->next = nskb;
3027f4c50d99SHerbert Xu 		else
3028f4c50d99SHerbert Xu 			segs = nskb;
3029f4c50d99SHerbert Xu 		tail = nskb;
3030f4c50d99SHerbert Xu 
3031df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
3032f4c50d99SHerbert Xu 
3033030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3034fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
303568c33163SPravin B Shelar 
3036df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
303768c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
303868c33163SPravin B Shelar 						 doffset + tnl_hlen);
303989319d38SHerbert Xu 
30409d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
30411cdbcb79SSimon Horman 			goto perform_csum_check;
304289319d38SHerbert Xu 
3043e585f236STom Herbert 		if (!sg && !nskb->remcsum_offload) {
30446f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
3045df5771ffSMichael S. Tsirkin 			nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3046f4c50d99SHerbert Xu 							    skb_put(nskb, len),
3047f4c50d99SHerbert Xu 							    len, 0);
30487e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
3049de843723STom Herbert 			    skb_headroom(nskb) + doffset;
3050f4c50d99SHerbert Xu 			continue;
3051f4c50d99SHerbert Xu 		}
3052f4c50d99SHerbert Xu 
30538cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3054f4c50d99SHerbert Xu 
3055df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3056d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
3057f4c50d99SHerbert Xu 
3058df5771ffSMichael S. Tsirkin 		skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3059df5771ffSMichael S. Tsirkin 			SKBTX_SHARED_FRAG;
3060cef401deSEric Dumazet 
30619d8506ccSHerbert Xu 		while (pos < offset + len) {
30629d8506ccSHerbert Xu 			if (i >= nfrags) {
30631a4cedafSMichael S. Tsirkin 				BUG_ON(skb_headlen(list_skb));
30649d8506ccSHerbert Xu 
30659d8506ccSHerbert Xu 				i = 0;
30661a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
30671a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
30681fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
30699d8506ccSHerbert Xu 
30709d8506ccSHerbert Xu 				BUG_ON(!nfrags);
30719d8506ccSHerbert Xu 
30721a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
30739d8506ccSHerbert Xu 			}
30749d8506ccSHerbert Xu 
30759d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
30769d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
30779d8506ccSHerbert Xu 				net_warn_ratelimited(
30789d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
30799d8506ccSHerbert Xu 					pos, mss);
30809d8506ccSHerbert Xu 				goto err;
30819d8506ccSHerbert Xu 			}
30829d8506ccSHerbert Xu 
30831fd819ecSMichael S. Tsirkin 			if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
30841fd819ecSMichael S. Tsirkin 				goto err;
30851fd819ecSMichael S. Tsirkin 
30864e1beba1SMichael S. Tsirkin 			*nskb_frag = *frag;
30878cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
30888cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
3089f4c50d99SHerbert Xu 
3090f4c50d99SHerbert Xu 			if (pos < offset) {
30918cb19905SMichael S. Tsirkin 				nskb_frag->page_offset += offset - pos;
30928cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
3093f4c50d99SHerbert Xu 			}
3094f4c50d99SHerbert Xu 
309589319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
3096f4c50d99SHerbert Xu 
3097f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
3098f4c50d99SHerbert Xu 				i++;
30994e1beba1SMichael S. Tsirkin 				frag++;
3100f4c50d99SHerbert Xu 				pos += size;
3101f4c50d99SHerbert Xu 			} else {
31028cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
310389319d38SHerbert Xu 				goto skip_fraglist;
3104f4c50d99SHerbert Xu 			}
3105f4c50d99SHerbert Xu 
31068cb19905SMichael S. Tsirkin 			nskb_frag++;
3107f4c50d99SHerbert Xu 		}
3108f4c50d99SHerbert Xu 
310989319d38SHerbert Xu skip_fraglist:
3110f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
3111f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
3112f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
3113ec5f0615SPravin B Shelar 
31141cdbcb79SSimon Horman perform_csum_check:
3115e585f236STom Herbert 		if (!csum && !nskb->remcsum_offload) {
3116ec5f0615SPravin B Shelar 			nskb->csum = skb_checksum(nskb, doffset,
3117ec5f0615SPravin B Shelar 						  nskb->len - doffset, 0);
3118ec5f0615SPravin B Shelar 			nskb->ip_summed = CHECKSUM_NONE;
31197e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
31207e2b10c1STom Herbert 			    skb_headroom(nskb) + doffset;
3121ec5f0615SPravin B Shelar 		}
3122df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
3123f4c50d99SHerbert Xu 
3124bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
3125bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
3126bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
3127bec3cfdcSEric Dumazet 	 */
3128bec3cfdcSEric Dumazet 	segs->prev = tail;
3129432c856fSToshiaki Makita 
3130432c856fSToshiaki Makita 	/* Following permits correct backpressure, for protocols
3131432c856fSToshiaki Makita 	 * using skb_set_owner_w().
3132432c856fSToshiaki Makita 	 * Idea is to tranfert ownership from head_skb to last segment.
3133432c856fSToshiaki Makita 	 */
3134432c856fSToshiaki Makita 	if (head_skb->destructor == sock_wfree) {
3135432c856fSToshiaki Makita 		swap(tail->truesize, head_skb->truesize);
3136432c856fSToshiaki Makita 		swap(tail->destructor, head_skb->destructor);
3137432c856fSToshiaki Makita 		swap(tail->sk, head_skb->sk);
3138432c856fSToshiaki Makita 	}
3139f4c50d99SHerbert Xu 	return segs;
3140f4c50d99SHerbert Xu 
3141f4c50d99SHerbert Xu err:
3142289dccbeSEric Dumazet 	kfree_skb_list(segs);
3143f4c50d99SHerbert Xu 	return ERR_PTR(err);
3144f4c50d99SHerbert Xu }
3145f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
3146f4c50d99SHerbert Xu 
314771d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
314871d93b39SHerbert Xu {
31498a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
315067147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
315167147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
31528a29111cSEric Dumazet 	struct sk_buff *nskb, *lp, *p = *head;
31538a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
3154715dc1f3SEric Dumazet 	unsigned int delta_truesize;
31558a29111cSEric Dumazet 	unsigned int headroom;
315671d93b39SHerbert Xu 
31578a29111cSEric Dumazet 	if (unlikely(p->len + len >= 65536))
315871d93b39SHerbert Xu 		return -E2BIG;
315971d93b39SHerbert Xu 
316029e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
31618a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
31628a29111cSEric Dumazet 
31638a29111cSEric Dumazet 	if (headlen <= offset) {
316442da6994SHerbert Xu 		skb_frag_t *frag;
316566e92fcfSHerbert Xu 		skb_frag_t *frag2;
31669aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
31679aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
316842da6994SHerbert Xu 
316966e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
31708a29111cSEric Dumazet 			goto merge;
317181705ad1SHerbert Xu 
31728a29111cSEric Dumazet 		offset -= headlen;
31739aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
31749aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
3175f5572068SHerbert Xu 
31769aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
31779aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
317866e92fcfSHerbert Xu 		do {
317966e92fcfSHerbert Xu 			*--frag = *--frag2;
318066e92fcfSHerbert Xu 		} while (--i);
318166e92fcfSHerbert Xu 
318266e92fcfSHerbert Xu 		frag->page_offset += offset;
31839e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
318466e92fcfSHerbert Xu 
3185715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
3186ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
3187ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
3188715dc1f3SEric Dumazet 
3189f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
3190f5572068SHerbert Xu 		skb->len -= skb->data_len;
3191f5572068SHerbert Xu 		skb->data_len = 0;
3192f5572068SHerbert Xu 
3193715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
31945d38a079SHerbert Xu 		goto done;
3195d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
3196d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
3197d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
3198d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
3199d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
3200d7e8883cSEric Dumazet 		unsigned int first_offset;
3201d7e8883cSEric Dumazet 
3202d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
32038a29111cSEric Dumazet 			goto merge;
3204d7e8883cSEric Dumazet 
3205d7e8883cSEric Dumazet 		first_offset = skb->data -
3206d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
3207d7e8883cSEric Dumazet 			       offset;
3208d7e8883cSEric Dumazet 
3209d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3210d7e8883cSEric Dumazet 
3211d7e8883cSEric Dumazet 		frag->page.p	  = page;
3212d7e8883cSEric Dumazet 		frag->page_offset = first_offset;
3213d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
3214d7e8883cSEric Dumazet 
3215d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3216d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
3217d7e8883cSEric Dumazet 
3218715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3219d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3220d7e8883cSEric Dumazet 		goto done;
32218a29111cSEric Dumazet 	}
322273d3fe6dSEric Dumazet 	/* switch back to head shinfo */
322373d3fe6dSEric Dumazet 	pinfo = skb_shinfo(p);
322473d3fe6dSEric Dumazet 
32258a29111cSEric Dumazet 	if (pinfo->frag_list)
32268a29111cSEric Dumazet 		goto merge;
32278a29111cSEric Dumazet 	if (skb_gro_len(p) != pinfo->gso_size)
322869c0cab1SHerbert Xu 		return -E2BIG;
322971d93b39SHerbert Xu 
323071d93b39SHerbert Xu 	headroom = skb_headroom(p);
32313d3be433SEric Dumazet 	nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
323271d93b39SHerbert Xu 	if (unlikely(!nskb))
323371d93b39SHerbert Xu 		return -ENOMEM;
323471d93b39SHerbert Xu 
323571d93b39SHerbert Xu 	__copy_skb_header(nskb, p);
323671d93b39SHerbert Xu 	nskb->mac_len = p->mac_len;
323771d93b39SHerbert Xu 
323871d93b39SHerbert Xu 	skb_reserve(nskb, headroom);
323986911732SHerbert Xu 	__skb_put(nskb, skb_gro_offset(p));
324071d93b39SHerbert Xu 
324186911732SHerbert Xu 	skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
324271d93b39SHerbert Xu 	skb_set_network_header(nskb, skb_network_offset(p));
324371d93b39SHerbert Xu 	skb_set_transport_header(nskb, skb_transport_offset(p));
324471d93b39SHerbert Xu 
324586911732SHerbert Xu 	__skb_pull(p, skb_gro_offset(p));
324686911732SHerbert Xu 	memcpy(skb_mac_header(nskb), skb_mac_header(p),
324786911732SHerbert Xu 	       p->data - skb_mac_header(p));
324871d93b39SHerbert Xu 
324971d93b39SHerbert Xu 	skb_shinfo(nskb)->frag_list = p;
32509aaa156cSHerbert Xu 	skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3251622e0ca1SHerbert Xu 	pinfo->gso_size = 0;
3252f4a775d1SEric Dumazet 	__skb_header_release(p);
3253c3c7c254SEric Dumazet 	NAPI_GRO_CB(nskb)->last = p;
325471d93b39SHerbert Xu 
325571d93b39SHerbert Xu 	nskb->data_len += p->len;
3256de8261c2SEric Dumazet 	nskb->truesize += p->truesize;
325771d93b39SHerbert Xu 	nskb->len += p->len;
325871d93b39SHerbert Xu 
325971d93b39SHerbert Xu 	*head = nskb;
326071d93b39SHerbert Xu 	nskb->next = p->next;
326171d93b39SHerbert Xu 	p->next = NULL;
326271d93b39SHerbert Xu 
326371d93b39SHerbert Xu 	p = nskb;
326471d93b39SHerbert Xu 
326571d93b39SHerbert Xu merge:
3266715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
326767147ba9SHerbert Xu 	if (offset > headlen) {
3268d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
3269d1dc7abfSMichal Schmidt 
3270d1dc7abfSMichal Schmidt 		skbinfo->frags[0].page_offset += eat;
32719e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
3272d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
3273d1dc7abfSMichal Schmidt 		skb->len -= eat;
327467147ba9SHerbert Xu 		offset = headlen;
327556035022SHerbert Xu 	}
327656035022SHerbert Xu 
327767147ba9SHerbert Xu 	__skb_pull(skb, offset);
327856035022SHerbert Xu 
327929e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
32808a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
32818a29111cSEric Dumazet 	else
3282c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
3283c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
3284f4a775d1SEric Dumazet 	__skb_header_release(skb);
32858a29111cSEric Dumazet 	lp = p;
328671d93b39SHerbert Xu 
32875d38a079SHerbert Xu done:
32885d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
328937fe4732SHerbert Xu 	p->data_len += len;
3290715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
329137fe4732SHerbert Xu 	p->len += len;
32928a29111cSEric Dumazet 	if (lp != p) {
32938a29111cSEric Dumazet 		lp->data_len += len;
32948a29111cSEric Dumazet 		lp->truesize += delta_truesize;
32958a29111cSEric Dumazet 		lp->len += len;
32968a29111cSEric Dumazet 	}
329771d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
329871d93b39SHerbert Xu 	return 0;
329971d93b39SHerbert Xu }
330071d93b39SHerbert Xu 
33011da177e4SLinus Torvalds void __init skb_init(void)
33021da177e4SLinus Torvalds {
33031da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
33041da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
33051da177e4SLinus Torvalds 					      0,
3306e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
330720c2df83SPaul Mundt 					      NULL);
3308d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3309d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
3310d179cd12SDavid S. Miller 						0,
3311e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
331220c2df83SPaul Mundt 						NULL);
33131da177e4SLinus Torvalds }
33141da177e4SLinus Torvalds 
3315716ea3a7SDavid Howells /**
3316716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3317716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
3318716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
3319716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
3320716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
3321716ea3a7SDavid Howells  *
3322716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
3323716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
3324716ea3a7SDavid Howells  */
332551c739d1SDavid S. Miller static int
332651c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3327716ea3a7SDavid Howells {
33281a028e50SDavid S. Miller 	int start = skb_headlen(skb);
33291a028e50SDavid S. Miller 	int i, copy = start - offset;
3330fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
3331716ea3a7SDavid Howells 	int elt = 0;
3332716ea3a7SDavid Howells 
3333716ea3a7SDavid Howells 	if (copy > 0) {
3334716ea3a7SDavid Howells 		if (copy > len)
3335716ea3a7SDavid Howells 			copy = len;
3336642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
3337716ea3a7SDavid Howells 		elt++;
3338716ea3a7SDavid Howells 		if ((len -= copy) == 0)
3339716ea3a7SDavid Howells 			return elt;
3340716ea3a7SDavid Howells 		offset += copy;
3341716ea3a7SDavid Howells 	}
3342716ea3a7SDavid Howells 
3343716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
33441a028e50SDavid S. Miller 		int end;
3345716ea3a7SDavid Howells 
3346547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
33471a028e50SDavid S. Miller 
33489e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3349716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3350716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3351716ea3a7SDavid Howells 
3352716ea3a7SDavid Howells 			if (copy > len)
3353716ea3a7SDavid Howells 				copy = len;
3354ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3355642f1490SJens Axboe 					frag->page_offset+offset-start);
3356716ea3a7SDavid Howells 			elt++;
3357716ea3a7SDavid Howells 			if (!(len -= copy))
3358716ea3a7SDavid Howells 				return elt;
3359716ea3a7SDavid Howells 			offset += copy;
3360716ea3a7SDavid Howells 		}
33611a028e50SDavid S. Miller 		start = end;
3362716ea3a7SDavid Howells 	}
3363716ea3a7SDavid Howells 
3364fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
33651a028e50SDavid S. Miller 		int end;
3366716ea3a7SDavid Howells 
3367547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
33681a028e50SDavid S. Miller 
3369fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
3370716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3371716ea3a7SDavid Howells 			if (copy > len)
3372716ea3a7SDavid Howells 				copy = len;
3373fbb398a8SDavid S. Miller 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
337451c739d1SDavid S. Miller 					      copy);
3375716ea3a7SDavid Howells 			if ((len -= copy) == 0)
3376716ea3a7SDavid Howells 				return elt;
3377716ea3a7SDavid Howells 			offset += copy;
3378716ea3a7SDavid Howells 		}
33791a028e50SDavid S. Miller 		start = end;
3380716ea3a7SDavid Howells 	}
3381716ea3a7SDavid Howells 	BUG_ON(len);
3382716ea3a7SDavid Howells 	return elt;
3383716ea3a7SDavid Howells }
3384716ea3a7SDavid Howells 
338525a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
338625a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
338725a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
338825a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
338925a91d8dSFan Du  *
339025a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
339125a91d8dSFan Du  * 1. sg_init_table
339225a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
339325a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
339425a91d8dSFan Du  *
339525a91d8dSFan Du  * This is equivalent to:
339625a91d8dSFan Du  * 1. sg_init_table
339725a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
339825a91d8dSFan Du  * 3. sg_unmark_end
339925a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
340025a91d8dSFan Du  *
340125a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
340225a91d8dSFan Du  * is more preferable.
340325a91d8dSFan Du  */
340425a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
340525a91d8dSFan Du 			int offset, int len)
340625a91d8dSFan Du {
340725a91d8dSFan Du 	return __skb_to_sgvec(skb, sg, offset, len);
340825a91d8dSFan Du }
340925a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
341025a91d8dSFan Du 
341151c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
341251c739d1SDavid S. Miller {
341351c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
341451c739d1SDavid S. Miller 
3415c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
341651c739d1SDavid S. Miller 
341751c739d1SDavid S. Miller 	return nsg;
341851c739d1SDavid S. Miller }
3419b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
342051c739d1SDavid S. Miller 
3421716ea3a7SDavid Howells /**
3422716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
3423716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
3424716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
3425716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
3426716ea3a7SDavid Howells  *
3427716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
3428716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
3429716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
3430716ea3a7SDavid Howells  *
3431716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
3432716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
3433716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
3434716ea3a7SDavid Howells  *
3435716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
3436716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
3437716ea3a7SDavid Howells  */
3438716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3439716ea3a7SDavid Howells {
3440716ea3a7SDavid Howells 	int copyflag;
3441716ea3a7SDavid Howells 	int elt;
3442716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
3443716ea3a7SDavid Howells 
3444716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
3445716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
3446716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
3447716ea3a7SDavid Howells 	 */
3448716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3449716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3450716ea3a7SDavid Howells 		return -ENOMEM;
3451716ea3a7SDavid Howells 
3452716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
345321dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
3454716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
3455716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
3456716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
3457716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
3458716ea3a7SDavid Howells 
3459716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
3460716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3461716ea3a7SDavid Howells 			return -ENOMEM;
3462716ea3a7SDavid Howells 
3463716ea3a7SDavid Howells 		/* Voila! */
3464716ea3a7SDavid Howells 		*trailer = skb;
3465716ea3a7SDavid Howells 		return 1;
3466716ea3a7SDavid Howells 	}
3467716ea3a7SDavid Howells 
3468716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
3469716ea3a7SDavid Howells 
3470716ea3a7SDavid Howells 	elt = 1;
3471716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
3472716ea3a7SDavid Howells 	copyflag = 0;
3473716ea3a7SDavid Howells 
3474716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
3475716ea3a7SDavid Howells 		int ntail = 0;
3476716ea3a7SDavid Howells 
3477716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
3478716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
3479716ea3a7SDavid Howells 		 * after it. */
3480716ea3a7SDavid Howells 
3481716ea3a7SDavid Howells 		if (skb_shared(skb1))
3482716ea3a7SDavid Howells 			copyflag = 1;
3483716ea3a7SDavid Howells 
3484716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
3485716ea3a7SDavid Howells 
3486716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
3487716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
348821dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
3489716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
3490716ea3a7SDavid Howells 				ntail = tailbits + 128;
3491716ea3a7SDavid Howells 		}
3492716ea3a7SDavid Howells 
3493716ea3a7SDavid Howells 		if (copyflag ||
3494716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
3495716ea3a7SDavid Howells 		    ntail ||
3496716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
349721dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
3498716ea3a7SDavid Howells 			struct sk_buff *skb2;
3499716ea3a7SDavid Howells 
3500716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
3501716ea3a7SDavid Howells 			if (ntail == 0)
3502716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
3503716ea3a7SDavid Howells 			else
3504716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
3505716ea3a7SDavid Howells 						       skb_headroom(skb1),
3506716ea3a7SDavid Howells 						       ntail,
3507716ea3a7SDavid Howells 						       GFP_ATOMIC);
3508716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
3509716ea3a7SDavid Howells 				return -ENOMEM;
3510716ea3a7SDavid Howells 
3511716ea3a7SDavid Howells 			if (skb1->sk)
3512716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
3513716ea3a7SDavid Howells 
3514716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
3515716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
3516716ea3a7SDavid Howells 
3517716ea3a7SDavid Howells 			skb2->next = skb1->next;
3518716ea3a7SDavid Howells 			*skb_p = skb2;
3519716ea3a7SDavid Howells 			kfree_skb(skb1);
3520716ea3a7SDavid Howells 			skb1 = skb2;
3521716ea3a7SDavid Howells 		}
3522716ea3a7SDavid Howells 		elt++;
3523716ea3a7SDavid Howells 		*trailer = skb1;
3524716ea3a7SDavid Howells 		skb_p = &skb1->next;
3525716ea3a7SDavid Howells 	}
3526716ea3a7SDavid Howells 
3527716ea3a7SDavid Howells 	return elt;
3528716ea3a7SDavid Howells }
3529b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
3530716ea3a7SDavid Howells 
3531b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
3532b1faf566SEric Dumazet {
3533b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
3534b1faf566SEric Dumazet 
3535b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3536b1faf566SEric Dumazet }
3537b1faf566SEric Dumazet 
3538b1faf566SEric Dumazet /*
3539b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3540b1faf566SEric Dumazet  */
3541b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3542b1faf566SEric Dumazet {
3543b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
354495c96174SEric Dumazet 	    (unsigned int)sk->sk_rcvbuf)
3545b1faf566SEric Dumazet 		return -ENOMEM;
3546b1faf566SEric Dumazet 
3547b1faf566SEric Dumazet 	skb_orphan(skb);
3548b1faf566SEric Dumazet 	skb->sk = sk;
3549b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
3550b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3551b1faf566SEric Dumazet 
3552abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
3553abb57ea4SEric Dumazet 	skb_dst_force(skb);
3554abb57ea4SEric Dumazet 
3555b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
3556b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
3557676d2369SDavid S. Miller 		sk->sk_data_ready(sk);
3558b1faf566SEric Dumazet 	return 0;
3559b1faf566SEric Dumazet }
3560b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
3561b1faf566SEric Dumazet 
3562364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3563364a9e93SWillem de Bruijn {
3564364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
3565364a9e93SWillem de Bruijn 	struct sk_buff *skb, *skb_next;
3566364a9e93SWillem de Bruijn 	int err = 0;
3567364a9e93SWillem de Bruijn 
3568364a9e93SWillem de Bruijn 	spin_lock_bh(&q->lock);
3569364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
3570364a9e93SWillem de Bruijn 	if (skb && (skb_next = skb_peek(q)))
3571364a9e93SWillem de Bruijn 		err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3572364a9e93SWillem de Bruijn 	spin_unlock_bh(&q->lock);
3573364a9e93SWillem de Bruijn 
3574364a9e93SWillem de Bruijn 	sk->sk_err = err;
3575364a9e93SWillem de Bruijn 	if (err)
3576364a9e93SWillem de Bruijn 		sk->sk_error_report(sk);
3577364a9e93SWillem de Bruijn 
3578364a9e93SWillem de Bruijn 	return skb;
3579364a9e93SWillem de Bruijn }
3580364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
3581364a9e93SWillem de Bruijn 
3582cab41c47SAlexander Duyck /**
3583cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
3584cab41c47SAlexander Duyck  * @skb: the skb to clone
3585cab41c47SAlexander Duyck  *
3586cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
3587cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
3588cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
3589cab41c47SAlexander Duyck  *
3590cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
3591cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
3592cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
3593cab41c47SAlexander Duyck  * the sk_error_queue.
3594cab41c47SAlexander Duyck  */
359562bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
359662bccb8cSAlexander Duyck {
359762bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
359862bccb8cSAlexander Duyck 	struct sk_buff *clone;
359962bccb8cSAlexander Duyck 
360062bccb8cSAlexander Duyck 	if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
360162bccb8cSAlexander Duyck 		return NULL;
360262bccb8cSAlexander Duyck 
360362bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
360462bccb8cSAlexander Duyck 	if (!clone) {
360562bccb8cSAlexander Duyck 		sock_put(sk);
360662bccb8cSAlexander Duyck 		return NULL;
360762bccb8cSAlexander Duyck 	}
360862bccb8cSAlexander Duyck 
360962bccb8cSAlexander Duyck 	clone->sk = sk;
361062bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
361162bccb8cSAlexander Duyck 
361262bccb8cSAlexander Duyck 	return clone;
361362bccb8cSAlexander Duyck }
361462bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
361562bccb8cSAlexander Duyck 
361637846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
361737846ef0SAlexander Duyck 					struct sock *sk,
361837846ef0SAlexander Duyck 					int tstype)
3619ac45f602SPatrick Ohly {
3620ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
3621ac45f602SPatrick Ohly 	int err;
3622ac45f602SPatrick Ohly 
3623ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
3624ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
3625ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
3626ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3627e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
36284ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
362909c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
36304ed2d765SWillem de Bruijn 		if (sk->sk_protocol == IPPROTO_TCP)
36314ed2d765SWillem de Bruijn 			serr->ee.ee_data -= sk->sk_tskey;
36324ed2d765SWillem de Bruijn 	}
363329030374SEric Dumazet 
3634ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
363529030374SEric Dumazet 
3636ac45f602SPatrick Ohly 	if (err)
3637ac45f602SPatrick Ohly 		kfree_skb(skb);
3638ac45f602SPatrick Ohly }
363937846ef0SAlexander Duyck 
364037846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
364137846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
364237846ef0SAlexander Duyck {
364337846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
364437846ef0SAlexander Duyck 
364562bccb8cSAlexander Duyck 	/* take a reference to prevent skb_orphan() from freeing the socket */
364662bccb8cSAlexander Duyck 	sock_hold(sk);
364737846ef0SAlexander Duyck 
364837846ef0SAlexander Duyck 	*skb_hwtstamps(skb) = *hwtstamps;
364937846ef0SAlexander Duyck 	__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
365037846ef0SAlexander Duyck 
365137846ef0SAlexander Duyck 	sock_put(sk);
365237846ef0SAlexander Duyck }
365337846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
365437846ef0SAlexander Duyck 
365537846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
365637846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
365737846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
365837846ef0SAlexander Duyck {
365937846ef0SAlexander Duyck 	struct sk_buff *skb;
366037846ef0SAlexander Duyck 
366137846ef0SAlexander Duyck 	if (!sk)
366237846ef0SAlexander Duyck 		return;
366337846ef0SAlexander Duyck 
366437846ef0SAlexander Duyck 	if (hwtstamps)
366537846ef0SAlexander Duyck 		*skb_hwtstamps(orig_skb) = *hwtstamps;
366637846ef0SAlexander Duyck 	else
366737846ef0SAlexander Duyck 		orig_skb->tstamp = ktime_get_real();
366837846ef0SAlexander Duyck 
366937846ef0SAlexander Duyck 	skb = skb_clone(orig_skb, GFP_ATOMIC);
367037846ef0SAlexander Duyck 	if (!skb)
367137846ef0SAlexander Duyck 		return;
367237846ef0SAlexander Duyck 
367337846ef0SAlexander Duyck 	__skb_complete_tx_timestamp(skb, sk, tstype);
367437846ef0SAlexander Duyck }
3675e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3676e7fd2885SWillem de Bruijn 
3677e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
3678e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
3679e7fd2885SWillem de Bruijn {
3680e7fd2885SWillem de Bruijn 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3681e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
3682e7fd2885SWillem de Bruijn }
3683ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3684ac45f602SPatrick Ohly 
36856e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
36866e3e939fSJohannes Berg {
36876e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
36886e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
36896e3e939fSJohannes Berg 	int err;
36906e3e939fSJohannes Berg 
36916e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
36926e3e939fSJohannes Berg 	skb->wifi_acked = acked;
36936e3e939fSJohannes Berg 
36946e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
36956e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
36966e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
36976e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
36986e3e939fSJohannes Berg 
3699bf7fa551SAlexander Duyck 	/* take a reference to prevent skb_orphan() from freeing the socket */
3700bf7fa551SAlexander Duyck 	sock_hold(sk);
3701bf7fa551SAlexander Duyck 
37026e3e939fSJohannes Berg 	err = sock_queue_err_skb(sk, skb);
37036e3e939fSJohannes Berg 	if (err)
37046e3e939fSJohannes Berg 		kfree_skb(skb);
3705bf7fa551SAlexander Duyck 
3706bf7fa551SAlexander Duyck 	sock_put(sk);
37076e3e939fSJohannes Berg }
37086e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
37096e3e939fSJohannes Berg 
3710ac45f602SPatrick Ohly 
3711f35d9d8aSRusty Russell /**
3712f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3713f35d9d8aSRusty Russell  * @skb: the skb to set
3714f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3715f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3716f35d9d8aSRusty Russell  *
3717f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3718f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3719f35d9d8aSRusty Russell  *
3720f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3721f35d9d8aSRusty Russell  * returns false you should drop the packet.
3722f35d9d8aSRusty Russell  */
3723f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3724f35d9d8aSRusty Russell {
37255ff8dda3SHerbert Xu 	if (unlikely(start > skb_headlen(skb)) ||
37265ff8dda3SHerbert Xu 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3727e87cc472SJoe Perches 		net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
37285ff8dda3SHerbert Xu 				     start, off, skb_headlen(skb));
3729f35d9d8aSRusty Russell 		return false;
3730f35d9d8aSRusty Russell 	}
3731f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3732f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3733f35d9d8aSRusty Russell 	skb->csum_offset = off;
3734e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
3735f35d9d8aSRusty Russell 	return true;
3736f35d9d8aSRusty Russell }
3737b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3738f35d9d8aSRusty Russell 
3739ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3740ed1f50c3SPaul Durrant 			       unsigned int max)
3741ed1f50c3SPaul Durrant {
3742ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
3743ed1f50c3SPaul Durrant 		return 0;
3744ed1f50c3SPaul Durrant 
3745ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
3746ed1f50c3SPaul Durrant 	 * won't need to do it again.
3747ed1f50c3SPaul Durrant 	 */
3748ed1f50c3SPaul Durrant 	if (max > skb->len)
3749ed1f50c3SPaul Durrant 		max = skb->len;
3750ed1f50c3SPaul Durrant 
3751ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3752ed1f50c3SPaul Durrant 		return -ENOMEM;
3753ed1f50c3SPaul Durrant 
3754ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
3755ed1f50c3SPaul Durrant 		return -EPROTO;
3756ed1f50c3SPaul Durrant 
3757ed1f50c3SPaul Durrant 	return 0;
3758ed1f50c3SPaul Durrant }
3759ed1f50c3SPaul Durrant 
3760f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
3761f9708b43SJan Beulich 
3762f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3763f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
3764f9708b43SJan Beulich 				      unsigned int off)
3765f9708b43SJan Beulich {
3766f9708b43SJan Beulich 	switch (proto) {
3767f9708b43SJan Beulich 		int err;
3768f9708b43SJan Beulich 
3769f9708b43SJan Beulich 	case IPPROTO_TCP:
3770f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3771f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
3772f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3773f9708b43SJan Beulich 						  offsetof(struct tcphdr,
3774f9708b43SJan Beulich 							   check)))
3775f9708b43SJan Beulich 			err = -EPROTO;
3776f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3777f9708b43SJan Beulich 
3778f9708b43SJan Beulich 	case IPPROTO_UDP:
3779f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3780f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
3781f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3782f9708b43SJan Beulich 						  offsetof(struct udphdr,
3783f9708b43SJan Beulich 							   check)))
3784f9708b43SJan Beulich 			err = -EPROTO;
3785f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3786f9708b43SJan Beulich 	}
3787f9708b43SJan Beulich 
3788f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
3789f9708b43SJan Beulich }
3790f9708b43SJan Beulich 
3791ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3792ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
3793ed1f50c3SPaul Durrant  */
3794ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
3795ed1f50c3SPaul Durrant 
3796f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3797ed1f50c3SPaul Durrant {
3798ed1f50c3SPaul Durrant 	unsigned int off;
3799ed1f50c3SPaul Durrant 	bool fragment;
3800f9708b43SJan Beulich 	__sum16 *csum;
3801ed1f50c3SPaul Durrant 	int err;
3802ed1f50c3SPaul Durrant 
3803ed1f50c3SPaul Durrant 	fragment = false;
3804ed1f50c3SPaul Durrant 
3805ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
3806ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
3807ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
3808ed1f50c3SPaul Durrant 	if (err < 0)
3809ed1f50c3SPaul Durrant 		goto out;
3810ed1f50c3SPaul Durrant 
3811ed1f50c3SPaul Durrant 	if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3812ed1f50c3SPaul Durrant 		fragment = true;
3813ed1f50c3SPaul Durrant 
3814ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
3815ed1f50c3SPaul Durrant 
3816ed1f50c3SPaul Durrant 	err = -EPROTO;
3817ed1f50c3SPaul Durrant 
3818ed1f50c3SPaul Durrant 	if (fragment)
3819ed1f50c3SPaul Durrant 		goto out;
3820ed1f50c3SPaul Durrant 
3821f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3822f9708b43SJan Beulich 	if (IS_ERR(csum))
3823f9708b43SJan Beulich 		return PTR_ERR(csum);
3824ed1f50c3SPaul Durrant 
3825ed1f50c3SPaul Durrant 	if (recalculate)
3826f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3827ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
3828ed1f50c3SPaul Durrant 					   skb->len - off,
3829f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
3830ed1f50c3SPaul Durrant 	err = 0;
3831ed1f50c3SPaul Durrant 
3832ed1f50c3SPaul Durrant out:
3833ed1f50c3SPaul Durrant 	return err;
3834ed1f50c3SPaul Durrant }
3835ed1f50c3SPaul Durrant 
3836ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3837ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
3838ed1f50c3SPaul Durrant  */
3839ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
3840ed1f50c3SPaul Durrant 
3841ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
3842ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
3843ed1f50c3SPaul Durrant 
3844ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3845ed1f50c3SPaul Durrant {
3846ed1f50c3SPaul Durrant 	int err;
3847ed1f50c3SPaul Durrant 	u8 nexthdr;
3848ed1f50c3SPaul Durrant 	unsigned int off;
3849ed1f50c3SPaul Durrant 	unsigned int len;
3850ed1f50c3SPaul Durrant 	bool fragment;
3851ed1f50c3SPaul Durrant 	bool done;
3852f9708b43SJan Beulich 	__sum16 *csum;
3853ed1f50c3SPaul Durrant 
3854ed1f50c3SPaul Durrant 	fragment = false;
3855ed1f50c3SPaul Durrant 	done = false;
3856ed1f50c3SPaul Durrant 
3857ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
3858ed1f50c3SPaul Durrant 
3859ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3860ed1f50c3SPaul Durrant 	if (err < 0)
3861ed1f50c3SPaul Durrant 		goto out;
3862ed1f50c3SPaul Durrant 
3863ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
3864ed1f50c3SPaul Durrant 
3865ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3866ed1f50c3SPaul Durrant 	while (off <= len && !done) {
3867ed1f50c3SPaul Durrant 		switch (nexthdr) {
3868ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
3869ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
3870ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
3871ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
3872ed1f50c3SPaul Durrant 
3873ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3874ed1f50c3SPaul Durrant 						  off +
3875ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
3876ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3877ed1f50c3SPaul Durrant 			if (err < 0)
3878ed1f50c3SPaul Durrant 				goto out;
3879ed1f50c3SPaul Durrant 
3880ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3881ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3882ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
3883ed1f50c3SPaul Durrant 			break;
3884ed1f50c3SPaul Durrant 		}
3885ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
3886ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
3887ed1f50c3SPaul Durrant 
3888ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3889ed1f50c3SPaul Durrant 						  off +
3890ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
3891ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3892ed1f50c3SPaul Durrant 			if (err < 0)
3893ed1f50c3SPaul Durrant 				goto out;
3894ed1f50c3SPaul Durrant 
3895ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3896ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3897ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
3898ed1f50c3SPaul Durrant 			break;
3899ed1f50c3SPaul Durrant 		}
3900ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
3901ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
3902ed1f50c3SPaul Durrant 
3903ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3904ed1f50c3SPaul Durrant 						  off +
3905ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
3906ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3907ed1f50c3SPaul Durrant 			if (err < 0)
3908ed1f50c3SPaul Durrant 				goto out;
3909ed1f50c3SPaul Durrant 
3910ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
3911ed1f50c3SPaul Durrant 
3912ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3913ed1f50c3SPaul Durrant 				fragment = true;
3914ed1f50c3SPaul Durrant 
3915ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3916ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
3917ed1f50c3SPaul Durrant 			break;
3918ed1f50c3SPaul Durrant 		}
3919ed1f50c3SPaul Durrant 		default:
3920ed1f50c3SPaul Durrant 			done = true;
3921ed1f50c3SPaul Durrant 			break;
3922ed1f50c3SPaul Durrant 		}
3923ed1f50c3SPaul Durrant 	}
3924ed1f50c3SPaul Durrant 
3925ed1f50c3SPaul Durrant 	err = -EPROTO;
3926ed1f50c3SPaul Durrant 
3927ed1f50c3SPaul Durrant 	if (!done || fragment)
3928ed1f50c3SPaul Durrant 		goto out;
3929ed1f50c3SPaul Durrant 
3930f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
3931f9708b43SJan Beulich 	if (IS_ERR(csum))
3932f9708b43SJan Beulich 		return PTR_ERR(csum);
3933ed1f50c3SPaul Durrant 
3934ed1f50c3SPaul Durrant 	if (recalculate)
3935f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3936ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
3937f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
3938ed1f50c3SPaul Durrant 	err = 0;
3939ed1f50c3SPaul Durrant 
3940ed1f50c3SPaul Durrant out:
3941ed1f50c3SPaul Durrant 	return err;
3942ed1f50c3SPaul Durrant }
3943ed1f50c3SPaul Durrant 
3944ed1f50c3SPaul Durrant /**
3945ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
3946ed1f50c3SPaul Durrant  * @skb: the skb to set up
3947ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
3948ed1f50c3SPaul Durrant  */
3949ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3950ed1f50c3SPaul Durrant {
3951ed1f50c3SPaul Durrant 	int err;
3952ed1f50c3SPaul Durrant 
3953ed1f50c3SPaul Durrant 	switch (skb->protocol) {
3954ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
3955f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
3956ed1f50c3SPaul Durrant 		break;
3957ed1f50c3SPaul Durrant 
3958ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
3959ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
3960ed1f50c3SPaul Durrant 		break;
3961ed1f50c3SPaul Durrant 
3962ed1f50c3SPaul Durrant 	default:
3963ed1f50c3SPaul Durrant 		err = -EPROTO;
3964ed1f50c3SPaul Durrant 		break;
3965ed1f50c3SPaul Durrant 	}
3966ed1f50c3SPaul Durrant 
3967ed1f50c3SPaul Durrant 	return err;
3968ed1f50c3SPaul Durrant }
3969ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
3970ed1f50c3SPaul Durrant 
39714497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
39724497b076SBen Hutchings {
3973e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
3974e87cc472SJoe Perches 			     skb->dev->name);
39754497b076SBen Hutchings }
39764497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3977bad43ca8SEric Dumazet 
3978bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
3979bad43ca8SEric Dumazet {
39803d861f66SEric Dumazet 	if (head_stolen) {
39813d861f66SEric Dumazet 		skb_release_head_state(skb);
3982bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
39833d861f66SEric Dumazet 	} else {
3984bad43ca8SEric Dumazet 		__kfree_skb(skb);
3985bad43ca8SEric Dumazet 	}
39863d861f66SEric Dumazet }
3987bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
3988bad43ca8SEric Dumazet 
3989bad43ca8SEric Dumazet /**
3990bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
3991bad43ca8SEric Dumazet  * @to: prior buffer
3992bad43ca8SEric Dumazet  * @from: buffer to add
3993bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
3994c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
3995bad43ca8SEric Dumazet  */
3996bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
3997bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
3998bad43ca8SEric Dumazet {
3999bad43ca8SEric Dumazet 	int i, delta, len = from->len;
4000bad43ca8SEric Dumazet 
4001bad43ca8SEric Dumazet 	*fragstolen = false;
4002bad43ca8SEric Dumazet 
4003bad43ca8SEric Dumazet 	if (skb_cloned(to))
4004bad43ca8SEric Dumazet 		return false;
4005bad43ca8SEric Dumazet 
4006bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
4007e93a0435SEric Dumazet 		if (len)
4008bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4009bad43ca8SEric Dumazet 		*delta_truesize = 0;
4010bad43ca8SEric Dumazet 		return true;
4011bad43ca8SEric Dumazet 	}
4012bad43ca8SEric Dumazet 
4013bad43ca8SEric Dumazet 	if (skb_has_frag_list(to) || skb_has_frag_list(from))
4014bad43ca8SEric Dumazet 		return false;
4015bad43ca8SEric Dumazet 
4016bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
4017bad43ca8SEric Dumazet 		struct page *page;
4018bad43ca8SEric Dumazet 		unsigned int offset;
4019bad43ca8SEric Dumazet 
4020bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
4021bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4022bad43ca8SEric Dumazet 			return false;
4023bad43ca8SEric Dumazet 
4024bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
4025bad43ca8SEric Dumazet 			return false;
4026bad43ca8SEric Dumazet 
4027bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4028bad43ca8SEric Dumazet 
4029bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
4030bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
4031bad43ca8SEric Dumazet 
4032bad43ca8SEric Dumazet 		skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4033bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
4034bad43ca8SEric Dumazet 		*fragstolen = true;
4035bad43ca8SEric Dumazet 	} else {
4036bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
4037bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4038bad43ca8SEric Dumazet 			return false;
4039bad43ca8SEric Dumazet 
4040f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4041bad43ca8SEric Dumazet 	}
4042bad43ca8SEric Dumazet 
4043bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
4044bad43ca8SEric Dumazet 
4045bad43ca8SEric Dumazet 	memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4046bad43ca8SEric Dumazet 	       skb_shinfo(from)->frags,
4047bad43ca8SEric Dumazet 	       skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4048bad43ca8SEric Dumazet 	skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4049bad43ca8SEric Dumazet 
4050bad43ca8SEric Dumazet 	if (!skb_cloned(from))
4051bad43ca8SEric Dumazet 		skb_shinfo(from)->nr_frags = 0;
4052bad43ca8SEric Dumazet 
40538ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
40548ea853fdSLi RongQing 	 * since we set nr_frags to 0.
40558ea853fdSLi RongQing 	 */
4056bad43ca8SEric Dumazet 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4057bad43ca8SEric Dumazet 		skb_frag_ref(from, i);
4058bad43ca8SEric Dumazet 
4059bad43ca8SEric Dumazet 	to->truesize += delta;
4060bad43ca8SEric Dumazet 	to->len += len;
4061bad43ca8SEric Dumazet 	to->data_len += len;
4062bad43ca8SEric Dumazet 
4063bad43ca8SEric Dumazet 	*delta_truesize = delta;
4064bad43ca8SEric Dumazet 	return true;
4065bad43ca8SEric Dumazet }
4066bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
4067621e84d6SNicolas Dichtel 
4068621e84d6SNicolas Dichtel /**
40698b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
4070621e84d6SNicolas Dichtel  *
4071621e84d6SNicolas Dichtel  * @skb: buffer to clean
40728b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
4073621e84d6SNicolas Dichtel  *
40748b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
40758b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
40768b27f277SNicolas Dichtel  * operations.
40778b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
40788b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
40798b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
4080621e84d6SNicolas Dichtel  */
40818b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4082621e84d6SNicolas Dichtel {
40838b27f277SNicolas Dichtel 	if (xnet)
4084621e84d6SNicolas Dichtel 		skb_orphan(skb);
4085621e84d6SNicolas Dichtel 	skb->tstamp.tv64 = 0;
4086621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
4087621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
408860ff7467SWANG Cong 	skb->ignore_df = 0;
4089621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
4090621e84d6SNicolas Dichtel 	skb->mark = 0;
4091621e84d6SNicolas Dichtel 	secpath_reset(skb);
4092621e84d6SNicolas Dichtel 	nf_reset(skb);
4093621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
4094621e84d6SNicolas Dichtel }
4095621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
4096de960aa9SFlorian Westphal 
4097de960aa9SFlorian Westphal /**
4098de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4099de960aa9SFlorian Westphal  *
4100de960aa9SFlorian Westphal  * @skb: GSO skb
4101de960aa9SFlorian Westphal  *
4102de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
4103de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
4104de960aa9SFlorian Westphal  *
4105de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4106de960aa9SFlorian Westphal  */
4107de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4108de960aa9SFlorian Westphal {
4109de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
4110f993bc25SFlorian Westphal 	unsigned int thlen = 0;
4111f993bc25SFlorian Westphal 
4112f993bc25SFlorian Westphal 	if (skb->encapsulation) {
4113f993bc25SFlorian Westphal 		thlen = skb_inner_transport_header(skb) -
4114f993bc25SFlorian Westphal 			skb_transport_header(skb);
4115de960aa9SFlorian Westphal 
4116de960aa9SFlorian Westphal 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4117f993bc25SFlorian Westphal 			thlen += inner_tcp_hdrlen(skb);
4118f993bc25SFlorian Westphal 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4119f993bc25SFlorian Westphal 		thlen = tcp_hdrlen(skb);
4120f993bc25SFlorian Westphal 	}
41216d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
41226d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
41236d39d589SFlorian Westphal 	 * accounted for.
41246d39d589SFlorian Westphal 	 */
4125f993bc25SFlorian Westphal 	return thlen + shinfo->gso_size;
4126de960aa9SFlorian Westphal }
4127de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
41280d5501c1SVlad Yasevich 
41290d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
41300d5501c1SVlad Yasevich {
41310d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
41320d5501c1SVlad Yasevich 		kfree_skb(skb);
41330d5501c1SVlad Yasevich 		return NULL;
41340d5501c1SVlad Yasevich 	}
41350d5501c1SVlad Yasevich 
41360d5501c1SVlad Yasevich 	memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
41370d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
41380d5501c1SVlad Yasevich 	return skb;
41390d5501c1SVlad Yasevich }
41400d5501c1SVlad Yasevich 
41410d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
41420d5501c1SVlad Yasevich {
41430d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
41440d5501c1SVlad Yasevich 	u16 vlan_tci;
41450d5501c1SVlad Yasevich 
41460d5501c1SVlad Yasevich 	if (unlikely(vlan_tx_tag_present(skb))) {
41470d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
41480d5501c1SVlad Yasevich 		return skb;
41490d5501c1SVlad Yasevich 	}
41500d5501c1SVlad Yasevich 
41510d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
41520d5501c1SVlad Yasevich 	if (unlikely(!skb))
41530d5501c1SVlad Yasevich 		goto err_free;
41540d5501c1SVlad Yasevich 
41550d5501c1SVlad Yasevich 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
41560d5501c1SVlad Yasevich 		goto err_free;
41570d5501c1SVlad Yasevich 
41580d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
41590d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
41600d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
41610d5501c1SVlad Yasevich 
41620d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
41630d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
41640d5501c1SVlad Yasevich 
41650d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
41660d5501c1SVlad Yasevich 	if (unlikely(!skb))
41670d5501c1SVlad Yasevich 		goto err_free;
41680d5501c1SVlad Yasevich 
41690d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
41700d5501c1SVlad Yasevich 	skb_reset_transport_header(skb);
41710d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
41720d5501c1SVlad Yasevich 
41730d5501c1SVlad Yasevich 	return skb;
41740d5501c1SVlad Yasevich 
41750d5501c1SVlad Yasevich err_free:
41760d5501c1SVlad Yasevich 	kfree_skb(skb);
41770d5501c1SVlad Yasevich 	return NULL;
41780d5501c1SVlad Yasevich }
41790d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
41802e4e4410SEric Dumazet 
4181e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len)
4182e2195121SJiri Pirko {
4183e2195121SJiri Pirko 	if (!pskb_may_pull(skb, write_len))
4184e2195121SJiri Pirko 		return -ENOMEM;
4185e2195121SJiri Pirko 
4186e2195121SJiri Pirko 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4187e2195121SJiri Pirko 		return 0;
4188e2195121SJiri Pirko 
4189e2195121SJiri Pirko 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4190e2195121SJiri Pirko }
4191e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
4192e2195121SJiri Pirko 
419393515d53SJiri Pirko /* remove VLAN header from packet and update csum accordingly. */
419493515d53SJiri Pirko static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
419593515d53SJiri Pirko {
419693515d53SJiri Pirko 	struct vlan_hdr *vhdr;
419793515d53SJiri Pirko 	unsigned int offset = skb->data - skb_mac_header(skb);
419893515d53SJiri Pirko 	int err;
419993515d53SJiri Pirko 
420093515d53SJiri Pirko 	__skb_push(skb, offset);
420193515d53SJiri Pirko 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
420293515d53SJiri Pirko 	if (unlikely(err))
420393515d53SJiri Pirko 		goto pull;
420493515d53SJiri Pirko 
420593515d53SJiri Pirko 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
420693515d53SJiri Pirko 
420793515d53SJiri Pirko 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
420893515d53SJiri Pirko 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
420993515d53SJiri Pirko 
421093515d53SJiri Pirko 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
421193515d53SJiri Pirko 	__skb_pull(skb, VLAN_HLEN);
421293515d53SJiri Pirko 
421393515d53SJiri Pirko 	vlan_set_encap_proto(skb, vhdr);
421493515d53SJiri Pirko 	skb->mac_header += VLAN_HLEN;
421593515d53SJiri Pirko 
421693515d53SJiri Pirko 	if (skb_network_offset(skb) < ETH_HLEN)
421793515d53SJiri Pirko 		skb_set_network_header(skb, ETH_HLEN);
421893515d53SJiri Pirko 
421993515d53SJiri Pirko 	skb_reset_mac_len(skb);
422093515d53SJiri Pirko pull:
422193515d53SJiri Pirko 	__skb_pull(skb, offset);
422293515d53SJiri Pirko 
422393515d53SJiri Pirko 	return err;
422493515d53SJiri Pirko }
422593515d53SJiri Pirko 
422693515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
422793515d53SJiri Pirko {
422893515d53SJiri Pirko 	u16 vlan_tci;
422993515d53SJiri Pirko 	__be16 vlan_proto;
423093515d53SJiri Pirko 	int err;
423193515d53SJiri Pirko 
423293515d53SJiri Pirko 	if (likely(vlan_tx_tag_present(skb))) {
423393515d53SJiri Pirko 		skb->vlan_tci = 0;
423493515d53SJiri Pirko 	} else {
423593515d53SJiri Pirko 		if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
423693515d53SJiri Pirko 			      skb->protocol != htons(ETH_P_8021AD)) ||
423793515d53SJiri Pirko 			     skb->len < VLAN_ETH_HLEN))
423893515d53SJiri Pirko 			return 0;
423993515d53SJiri Pirko 
424093515d53SJiri Pirko 		err = __skb_vlan_pop(skb, &vlan_tci);
424193515d53SJiri Pirko 		if (err)
424293515d53SJiri Pirko 			return err;
424393515d53SJiri Pirko 	}
424493515d53SJiri Pirko 	/* move next vlan tag to hw accel tag */
424593515d53SJiri Pirko 	if (likely((skb->protocol != htons(ETH_P_8021Q) &&
424693515d53SJiri Pirko 		    skb->protocol != htons(ETH_P_8021AD)) ||
424793515d53SJiri Pirko 		   skb->len < VLAN_ETH_HLEN))
424893515d53SJiri Pirko 		return 0;
424993515d53SJiri Pirko 
425093515d53SJiri Pirko 	vlan_proto = skb->protocol;
425193515d53SJiri Pirko 	err = __skb_vlan_pop(skb, &vlan_tci);
425293515d53SJiri Pirko 	if (unlikely(err))
425393515d53SJiri Pirko 		return err;
425493515d53SJiri Pirko 
425593515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
425693515d53SJiri Pirko 	return 0;
425793515d53SJiri Pirko }
425893515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
425993515d53SJiri Pirko 
426093515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
426193515d53SJiri Pirko {
426293515d53SJiri Pirko 	if (vlan_tx_tag_present(skb)) {
426393515d53SJiri Pirko 		unsigned int offset = skb->data - skb_mac_header(skb);
426493515d53SJiri Pirko 		int err;
426593515d53SJiri Pirko 
426693515d53SJiri Pirko 		/* __vlan_insert_tag expect skb->data pointing to mac header.
426793515d53SJiri Pirko 		 * So change skb->data before calling it and change back to
426893515d53SJiri Pirko 		 * original position later
426993515d53SJiri Pirko 		 */
427093515d53SJiri Pirko 		__skb_push(skb, offset);
427193515d53SJiri Pirko 		err = __vlan_insert_tag(skb, skb->vlan_proto,
427293515d53SJiri Pirko 					vlan_tx_tag_get(skb));
427393515d53SJiri Pirko 		if (err)
427493515d53SJiri Pirko 			return err;
427593515d53SJiri Pirko 		skb->protocol = skb->vlan_proto;
427693515d53SJiri Pirko 		skb->mac_len += VLAN_HLEN;
427793515d53SJiri Pirko 		__skb_pull(skb, offset);
427893515d53SJiri Pirko 
427993515d53SJiri Pirko 		if (skb->ip_summed == CHECKSUM_COMPLETE)
428093515d53SJiri Pirko 			skb->csum = csum_add(skb->csum, csum_partial(skb->data
428193515d53SJiri Pirko 					+ (2 * ETH_ALEN), VLAN_HLEN, 0));
428293515d53SJiri Pirko 	}
428393515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
428493515d53SJiri Pirko 	return 0;
428593515d53SJiri Pirko }
428693515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
428793515d53SJiri Pirko 
42882e4e4410SEric Dumazet /**
42892e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
42902e4e4410SEric Dumazet  *
4291de3f0d0eSMasanari Iida  * @header_len: size of linear part
4292de3f0d0eSMasanari Iida  * @data_len: needed length in frags
4293de3f0d0eSMasanari Iida  * @max_page_order: max page order desired.
4294de3f0d0eSMasanari Iida  * @errcode: pointer to error code if any
4295de3f0d0eSMasanari Iida  * @gfp_mask: allocation mask
42962e4e4410SEric Dumazet  *
42972e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
42982e4e4410SEric Dumazet  */
42992e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
43002e4e4410SEric Dumazet 				     unsigned long data_len,
43012e4e4410SEric Dumazet 				     int max_page_order,
43022e4e4410SEric Dumazet 				     int *errcode,
43032e4e4410SEric Dumazet 				     gfp_t gfp_mask)
43042e4e4410SEric Dumazet {
43052e4e4410SEric Dumazet 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
43062e4e4410SEric Dumazet 	unsigned long chunk;
43072e4e4410SEric Dumazet 	struct sk_buff *skb;
43082e4e4410SEric Dumazet 	struct page *page;
43092e4e4410SEric Dumazet 	gfp_t gfp_head;
43102e4e4410SEric Dumazet 	int i;
43112e4e4410SEric Dumazet 
43122e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
43132e4e4410SEric Dumazet 	/* Note this test could be relaxed, if we succeed to allocate
43142e4e4410SEric Dumazet 	 * high order pages...
43152e4e4410SEric Dumazet 	 */
43162e4e4410SEric Dumazet 	if (npages > MAX_SKB_FRAGS)
43172e4e4410SEric Dumazet 		return NULL;
43182e4e4410SEric Dumazet 
43192e4e4410SEric Dumazet 	gfp_head = gfp_mask;
43202e4e4410SEric Dumazet 	if (gfp_head & __GFP_WAIT)
43212e4e4410SEric Dumazet 		gfp_head |= __GFP_REPEAT;
43222e4e4410SEric Dumazet 
43232e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
43242e4e4410SEric Dumazet 	skb = alloc_skb(header_len, gfp_head);
43252e4e4410SEric Dumazet 	if (!skb)
43262e4e4410SEric Dumazet 		return NULL;
43272e4e4410SEric Dumazet 
43282e4e4410SEric Dumazet 	skb->truesize += npages << PAGE_SHIFT;
43292e4e4410SEric Dumazet 
43302e4e4410SEric Dumazet 	for (i = 0; npages > 0; i++) {
43312e4e4410SEric Dumazet 		int order = max_page_order;
43322e4e4410SEric Dumazet 
43332e4e4410SEric Dumazet 		while (order) {
43342e4e4410SEric Dumazet 			if (npages >= 1 << order) {
43352e4e4410SEric Dumazet 				page = alloc_pages(gfp_mask |
43362e4e4410SEric Dumazet 						   __GFP_COMP |
43372e4e4410SEric Dumazet 						   __GFP_NOWARN |
43382e4e4410SEric Dumazet 						   __GFP_NORETRY,
43392e4e4410SEric Dumazet 						   order);
43402e4e4410SEric Dumazet 				if (page)
43412e4e4410SEric Dumazet 					goto fill_page;
43422e4e4410SEric Dumazet 				/* Do not retry other high order allocations */
43432e4e4410SEric Dumazet 				order = 1;
43442e4e4410SEric Dumazet 				max_page_order = 0;
43452e4e4410SEric Dumazet 			}
43462e4e4410SEric Dumazet 			order--;
43472e4e4410SEric Dumazet 		}
43482e4e4410SEric Dumazet 		page = alloc_page(gfp_mask);
43492e4e4410SEric Dumazet 		if (!page)
43502e4e4410SEric Dumazet 			goto failure;
43512e4e4410SEric Dumazet fill_page:
43522e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
43532e4e4410SEric Dumazet 			      PAGE_SIZE << order);
43542e4e4410SEric Dumazet 		skb_fill_page_desc(skb, i, page, 0, chunk);
43552e4e4410SEric Dumazet 		data_len -= chunk;
43562e4e4410SEric Dumazet 		npages -= 1 << order;
43572e4e4410SEric Dumazet 	}
43582e4e4410SEric Dumazet 	return skb;
43592e4e4410SEric Dumazet 
43602e4e4410SEric Dumazet failure:
43612e4e4410SEric Dumazet 	kfree_skb(skb);
43622e4e4410SEric Dumazet 	return NULL;
43632e4e4410SEric Dumazet }
43642e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
4365