xref: /openbmc/linux/net/core/skbuff.c (revision 58025e46)
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>
77b245be1fSWillem de Bruijn #include <linux/capability.h>
78b245be1fSWillem de Bruijn #include <linux/user_namespace.h>
79a1f8e7f7SAl Viro 
80d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly;
81e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /**
84f05de73bSJean Sacren  *	skb_panic - private function for out-of-line support
851da177e4SLinus Torvalds  *	@skb:	buffer
861da177e4SLinus Torvalds  *	@sz:	size
87f05de73bSJean Sacren  *	@addr:	address
8899d5851eSJames Hogan  *	@msg:	skb_over_panic or skb_under_panic
891da177e4SLinus Torvalds  *
90f05de73bSJean Sacren  *	Out-of-line support for skb_put() and skb_push().
91f05de73bSJean Sacren  *	Called via the wrapper skb_over_panic() or skb_under_panic().
92f05de73bSJean Sacren  *	Keep out of line to prevent kernel bloat.
93f05de73bSJean Sacren  *	__builtin_return_address is not used because it is not always reliable.
941da177e4SLinus Torvalds  */
95f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr,
9699d5851eSJames Hogan 		      const char msg[])
971da177e4SLinus Torvalds {
98e005d193SJoe Perches 	pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n",
9999d5851eSJames Hogan 		 msg, addr, skb->len, sz, skb->head, skb->data,
1004305b541SArnaldo Carvalho de Melo 		 (unsigned long)skb->tail, (unsigned long)skb->end,
10126095455SPatrick McHardy 		 skb->dev ? skb->dev->name : "<NULL>");
1021da177e4SLinus Torvalds 	BUG();
1031da177e4SLinus Torvalds }
1041da177e4SLinus Torvalds 
105f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr)
1061da177e4SLinus Torvalds {
107f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
110f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr)
111f05de73bSJean Sacren {
112f05de73bSJean Sacren 	skb_panic(skb, sz, addr, __func__);
113f05de73bSJean Sacren }
114c93bdd0eSMel Gorman 
115c93bdd0eSMel Gorman /*
116c93bdd0eSMel Gorman  * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells
117c93bdd0eSMel Gorman  * the caller if emergency pfmemalloc reserves are being used. If it is and
118c93bdd0eSMel Gorman  * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves
119c93bdd0eSMel Gorman  * may be used. Otherwise, the packet data may be discarded until enough
120c93bdd0eSMel Gorman  * memory is free
121c93bdd0eSMel Gorman  */
122c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \
123c93bdd0eSMel Gorman 	 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc)
12461c5e88aSstephen hemminger 
12561c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node,
12661c5e88aSstephen hemminger 			       unsigned long ip, bool *pfmemalloc)
127c93bdd0eSMel Gorman {
128c93bdd0eSMel Gorman 	void *obj;
129c93bdd0eSMel Gorman 	bool ret_pfmemalloc = false;
130c93bdd0eSMel Gorman 
131c93bdd0eSMel Gorman 	/*
132c93bdd0eSMel Gorman 	 * Try a regular allocation, when that fails and we're not entitled
133c93bdd0eSMel Gorman 	 * to the reserves, fail.
134c93bdd0eSMel Gorman 	 */
135c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size,
136c93bdd0eSMel Gorman 					flags | __GFP_NOMEMALLOC | __GFP_NOWARN,
137c93bdd0eSMel Gorman 					node);
138c93bdd0eSMel Gorman 	if (obj || !(gfp_pfmemalloc_allowed(flags)))
139c93bdd0eSMel Gorman 		goto out;
140c93bdd0eSMel Gorman 
141c93bdd0eSMel Gorman 	/* Try again but now we are using pfmemalloc reserves */
142c93bdd0eSMel Gorman 	ret_pfmemalloc = true;
143c93bdd0eSMel Gorman 	obj = kmalloc_node_track_caller(size, flags, node);
144c93bdd0eSMel Gorman 
145c93bdd0eSMel Gorman out:
146c93bdd0eSMel Gorman 	if (pfmemalloc)
147c93bdd0eSMel Gorman 		*pfmemalloc = ret_pfmemalloc;
148c93bdd0eSMel Gorman 
149c93bdd0eSMel Gorman 	return obj;
150c93bdd0eSMel Gorman }
151c93bdd0eSMel Gorman 
1521da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1531da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1541da177e4SLinus Torvalds  *	[BEEP] leaks.
1551da177e4SLinus Torvalds  *
1561da177e4SLinus Torvalds  */
1571da177e4SLinus Torvalds 
1580ebd0ac5SPatrick McHardy struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node)
1590ebd0ac5SPatrick McHardy {
1600ebd0ac5SPatrick McHardy 	struct sk_buff *skb;
1610ebd0ac5SPatrick McHardy 
1620ebd0ac5SPatrick McHardy 	/* Get the HEAD */
1630ebd0ac5SPatrick McHardy 	skb = kmem_cache_alloc_node(skbuff_head_cache,
1640ebd0ac5SPatrick McHardy 				    gfp_mask & ~__GFP_DMA, node);
1650ebd0ac5SPatrick McHardy 	if (!skb)
1660ebd0ac5SPatrick McHardy 		goto out;
1670ebd0ac5SPatrick McHardy 
1680ebd0ac5SPatrick McHardy 	/*
1690ebd0ac5SPatrick McHardy 	 * Only clear those fields we need to clear, not those that we will
1700ebd0ac5SPatrick McHardy 	 * actually initialise below. Hence, don't put any more fields after
1710ebd0ac5SPatrick McHardy 	 * the tail pointer in struct sk_buff!
1720ebd0ac5SPatrick McHardy 	 */
1730ebd0ac5SPatrick McHardy 	memset(skb, 0, offsetof(struct sk_buff, tail));
1745e71d9d7SPablo Neira 	skb->head = NULL;
1750ebd0ac5SPatrick McHardy 	skb->truesize = sizeof(struct sk_buff);
1760ebd0ac5SPatrick McHardy 	atomic_set(&skb->users, 1);
1770ebd0ac5SPatrick McHardy 
17835d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
1790ebd0ac5SPatrick McHardy out:
1800ebd0ac5SPatrick McHardy 	return skb;
1810ebd0ac5SPatrick McHardy }
1820ebd0ac5SPatrick McHardy 
1831da177e4SLinus Torvalds /**
184d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1851da177e4SLinus Torvalds  *	@size: size to allocate
1861da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
187c93bdd0eSMel Gorman  *	@flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache
188c93bdd0eSMel Gorman  *		instead of head cache and allocate a cloned (child) skb.
189c93bdd0eSMel Gorman  *		If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
190c93bdd0eSMel Gorman  *		allocations in case the data is required for writeback
191b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1921da177e4SLinus Torvalds  *
1931da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
19494b6042cSBen Hutchings  *	tail room of at least size bytes. The object has a reference count
19594b6042cSBen Hutchings  *	of one. The return is the buffer. On a failure the return is %NULL.
1961da177e4SLinus Torvalds  *
1971da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1981da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1991da177e4SLinus Torvalds  */
200dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
201c93bdd0eSMel Gorman 			    int flags, int node)
2021da177e4SLinus Torvalds {
203e18b890bSChristoph Lameter 	struct kmem_cache *cache;
2044947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
2051da177e4SLinus Torvalds 	struct sk_buff *skb;
2061da177e4SLinus Torvalds 	u8 *data;
207c93bdd0eSMel Gorman 	bool pfmemalloc;
2081da177e4SLinus Torvalds 
209c93bdd0eSMel Gorman 	cache = (flags & SKB_ALLOC_FCLONE)
210c93bdd0eSMel Gorman 		? skbuff_fclone_cache : skbuff_head_cache;
211c93bdd0eSMel Gorman 
212c93bdd0eSMel Gorman 	if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX))
213c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
2148798b3fbSHerbert Xu 
2151da177e4SLinus Torvalds 	/* Get the HEAD */
216b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
2171da177e4SLinus Torvalds 	if (!skb)
2181da177e4SLinus Torvalds 		goto out;
219ec7d2f2cSEric Dumazet 	prefetchw(skb);
2201da177e4SLinus Torvalds 
22187fb4b7bSEric Dumazet 	/* We do our best to align skb_shared_info on a separate cache
22287fb4b7bSEric Dumazet 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
22387fb4b7bSEric Dumazet 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
22487fb4b7bSEric Dumazet 	 * Both skb->head and skb_shared_info are cache line aligned.
22587fb4b7bSEric Dumazet 	 */
226bc417e30STony Lindgren 	size = SKB_DATA_ALIGN(size);
22787fb4b7bSEric Dumazet 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
228c93bdd0eSMel Gorman 	data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc);
2291da177e4SLinus Torvalds 	if (!data)
2301da177e4SLinus Torvalds 		goto nodata;
23187fb4b7bSEric Dumazet 	/* kmalloc(size) might give us more room than requested.
23287fb4b7bSEric Dumazet 	 * Put skb_shared_info exactly at the end of allocated zone,
23387fb4b7bSEric Dumazet 	 * to allow max possible filling before reallocation.
23487fb4b7bSEric Dumazet 	 */
23587fb4b7bSEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
236ec7d2f2cSEric Dumazet 	prefetchw(data + size);
2371da177e4SLinus Torvalds 
238ca0605a7SArnaldo Carvalho de Melo 	/*
239c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
240c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
241c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
242ca0605a7SArnaldo Carvalho de Melo 	 */
243ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
24487fb4b7bSEric Dumazet 	/* Account for allocated memory : skb + skb->head */
24587fb4b7bSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
246c93bdd0eSMel Gorman 	skb->pfmemalloc = pfmemalloc;
2471da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2481da177e4SLinus Torvalds 	skb->head = data;
2491da177e4SLinus Torvalds 	skb->data = data;
25027a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2514305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
25235d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
25335d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
25419633e12SStephen Hemminger 
2554947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2564947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
257ec7d2f2cSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
2584947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
259c2aa3665SEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
2604947d3efSBenjamin LaHaise 
261c93bdd0eSMel Gorman 	if (flags & SKB_ALLOC_FCLONE) {
262d0bf4a9eSEric Dumazet 		struct sk_buff_fclones *fclones;
2631da177e4SLinus Torvalds 
264d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
265d0bf4a9eSEric Dumazet 
266d0bf4a9eSEric Dumazet 		kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
267d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
268d0bf4a9eSEric Dumazet 		atomic_set(&fclones->fclone_ref, 1);
269d179cd12SDavid S. Miller 
2706ffe75ebSEric Dumazet 		fclones->skb2.fclone = SKB_FCLONE_CLONE;
271d0bf4a9eSEric Dumazet 		fclones->skb2.pfmemalloc = pfmemalloc;
272d179cd12SDavid S. Miller 	}
2731da177e4SLinus Torvalds out:
2741da177e4SLinus Torvalds 	return skb;
2751da177e4SLinus Torvalds nodata:
2768798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2771da177e4SLinus Torvalds 	skb = NULL;
2781da177e4SLinus Torvalds 	goto out;
2791da177e4SLinus Torvalds }
280b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /**
283b2b5ce9dSEric Dumazet  * build_skb - build a network buffer
284b2b5ce9dSEric Dumazet  * @data: data buffer provided by caller
285d3836f21SEric Dumazet  * @frag_size: size of fragment, or 0 if head was kmalloced
286b2b5ce9dSEric Dumazet  *
287b2b5ce9dSEric Dumazet  * Allocate a new &sk_buff. Caller provides space holding head and
288deceb4c0SFlorian Fainelli  * skb_shared_info. @data must have been allocated by kmalloc() only if
289deceb4c0SFlorian Fainelli  * @frag_size is 0, otherwise data should come from the page allocator.
290b2b5ce9dSEric Dumazet  * The return is the new skb buffer.
291b2b5ce9dSEric Dumazet  * On a failure the return is %NULL, and @data is not freed.
292b2b5ce9dSEric Dumazet  * Notes :
293b2b5ce9dSEric Dumazet  *  Before IO, driver allocates only data buffer where NIC put incoming frame
294b2b5ce9dSEric Dumazet  *  Driver should add room at head (NET_SKB_PAD) and
295b2b5ce9dSEric Dumazet  *  MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info))
296b2b5ce9dSEric Dumazet  *  After IO, driver calls build_skb(), to allocate sk_buff and populate it
297b2b5ce9dSEric Dumazet  *  before giving packet to stack.
298b2b5ce9dSEric Dumazet  *  RX rings only contains data buffers, not full skbs.
299b2b5ce9dSEric Dumazet  */
300d3836f21SEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size)
301b2b5ce9dSEric Dumazet {
302b2b5ce9dSEric Dumazet 	struct skb_shared_info *shinfo;
303b2b5ce9dSEric Dumazet 	struct sk_buff *skb;
304d3836f21SEric Dumazet 	unsigned int size = frag_size ? : ksize(data);
305b2b5ce9dSEric Dumazet 
306b2b5ce9dSEric Dumazet 	skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC);
307b2b5ce9dSEric Dumazet 	if (!skb)
308b2b5ce9dSEric Dumazet 		return NULL;
309b2b5ce9dSEric Dumazet 
310d3836f21SEric Dumazet 	size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
311b2b5ce9dSEric Dumazet 
312b2b5ce9dSEric Dumazet 	memset(skb, 0, offsetof(struct sk_buff, tail));
313b2b5ce9dSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
314d3836f21SEric Dumazet 	skb->head_frag = frag_size != 0;
315b2b5ce9dSEric Dumazet 	atomic_set(&skb->users, 1);
316b2b5ce9dSEric Dumazet 	skb->head = data;
317b2b5ce9dSEric Dumazet 	skb->data = data;
318b2b5ce9dSEric Dumazet 	skb_reset_tail_pointer(skb);
319b2b5ce9dSEric Dumazet 	skb->end = skb->tail + size;
32035d04610SCong Wang 	skb->mac_header = (typeof(skb->mac_header))~0U;
32135d04610SCong Wang 	skb->transport_header = (typeof(skb->transport_header))~0U;
322b2b5ce9dSEric Dumazet 
323b2b5ce9dSEric Dumazet 	/* make sure we initialize shinfo sequentially */
324b2b5ce9dSEric Dumazet 	shinfo = skb_shinfo(skb);
325b2b5ce9dSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
326b2b5ce9dSEric Dumazet 	atomic_set(&shinfo->dataref, 1);
327b2b5ce9dSEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
328b2b5ce9dSEric Dumazet 
329b2b5ce9dSEric Dumazet 	return skb;
330b2b5ce9dSEric Dumazet }
331b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb);
332b2b5ce9dSEric Dumazet 
333a1c7fff7SEric Dumazet struct netdev_alloc_cache {
33469b08f62SEric Dumazet 	struct page_frag	frag;
33569b08f62SEric Dumazet 	/* we maintain a pagecount bias, so that we dont dirty cache line
33669b08f62SEric Dumazet 	 * containing page->_count every time we allocate a fragment.
33769b08f62SEric Dumazet 	 */
338540eb7bfSAlexander Duyck 	unsigned int		pagecnt_bias;
339a1c7fff7SEric Dumazet };
340a1c7fff7SEric Dumazet static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache);
341ffde7328SAlexander Duyck static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache);
342a1c7fff7SEric Dumazet 
343ffde7328SAlexander Duyck static struct page *__page_frag_refill(struct netdev_alloc_cache *nc,
344ffde7328SAlexander Duyck 				       gfp_t gfp_mask)
3456f532612SEric Dumazet {
346ffde7328SAlexander Duyck 	const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER;
347ffde7328SAlexander Duyck 	struct page *page = NULL;
34869b08f62SEric Dumazet 	gfp_t gfp = gfp_mask;
34969b08f62SEric Dumazet 
350ffde7328SAlexander Duyck 	if (order) {
351ffde7328SAlexander Duyck 		gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY;
352ffde7328SAlexander Duyck 		page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
353ffde7328SAlexander Duyck 		nc->frag.size = PAGE_SIZE << (page ? order : 0);
35469b08f62SEric Dumazet 	}
355ffde7328SAlexander Duyck 
356ffde7328SAlexander Duyck 	if (unlikely(!page))
357ffde7328SAlexander Duyck 		page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
358ffde7328SAlexander Duyck 
359ffde7328SAlexander Duyck 	nc->frag.page = page;
360ffde7328SAlexander Duyck 
361ffde7328SAlexander Duyck 	return page;
362ffde7328SAlexander Duyck }
363ffde7328SAlexander Duyck 
364ffde7328SAlexander Duyck static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache,
365ffde7328SAlexander Duyck 			       unsigned int fragsz, gfp_t gfp_mask)
366ffde7328SAlexander Duyck {
367ffde7328SAlexander Duyck 	struct netdev_alloc_cache *nc = this_cpu_ptr(cache);
368ffde7328SAlexander Duyck 	struct page *page = nc->frag.page;
369ffde7328SAlexander Duyck 	unsigned int size;
370ffde7328SAlexander Duyck 	int offset;
371ffde7328SAlexander Duyck 
372ffde7328SAlexander Duyck 	if (unlikely(!page)) {
373ffde7328SAlexander Duyck refill:
374ffde7328SAlexander Duyck 		page = __page_frag_refill(nc, gfp_mask);
375ffde7328SAlexander Duyck 		if (!page)
376ffde7328SAlexander Duyck 			return NULL;
377ffde7328SAlexander Duyck 
378ffde7328SAlexander Duyck 		/* if size can vary use frag.size else just use PAGE_SIZE */
379ffde7328SAlexander Duyck 		size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
380ffde7328SAlexander Duyck 
3814c450583SEric Dumazet 		/* Even if we own the page, we do not use atomic_set().
3824c450583SEric Dumazet 		 * This would break get_page_unless_zero() users.
3834c450583SEric Dumazet 		 */
384ffde7328SAlexander Duyck 		atomic_add(size - 1, &page->_count);
385ffde7328SAlexander Duyck 
386ffde7328SAlexander Duyck 		/* reset page count bias and offset to start of new frag */
387ffde7328SAlexander Duyck 		nc->pagecnt_bias = size;
388ffde7328SAlexander Duyck 		nc->frag.offset = size;
3896f532612SEric Dumazet 	}
390540eb7bfSAlexander Duyck 
391ffde7328SAlexander Duyck 	offset = nc->frag.offset - fragsz;
392ffde7328SAlexander Duyck 	if (unlikely(offset < 0)) {
393ffde7328SAlexander Duyck 		if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count))
3946f532612SEric Dumazet 			goto refill;
395ffde7328SAlexander Duyck 
396ffde7328SAlexander Duyck 		/* if size can vary use frag.size else just use PAGE_SIZE */
397ffde7328SAlexander Duyck 		size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE;
398ffde7328SAlexander Duyck 
3994c450583SEric Dumazet 		/* OK, page count is 0, we can safely set it */
400ffde7328SAlexander Duyck 		atomic_set(&page->_count, size);
401ffde7328SAlexander Duyck 
402ffde7328SAlexander Duyck 		/* reset page count bias and offset to start of new frag */
403ffde7328SAlexander Duyck 		nc->pagecnt_bias = size;
404ffde7328SAlexander Duyck 		offset = size - fragsz;
4056f532612SEric Dumazet 	}
406540eb7bfSAlexander Duyck 
407540eb7bfSAlexander Duyck 	nc->pagecnt_bias--;
408ffde7328SAlexander Duyck 	nc->frag.offset = offset;
409ffde7328SAlexander Duyck 
410ffde7328SAlexander Duyck 	return page_address(page) + offset;
411ffde7328SAlexander Duyck }
412ffde7328SAlexander Duyck 
413ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
414ffde7328SAlexander Duyck {
415ffde7328SAlexander Duyck 	unsigned long flags;
416ffde7328SAlexander Duyck 	void *data;
417ffde7328SAlexander Duyck 
418ffde7328SAlexander Duyck 	local_irq_save(flags);
419ffde7328SAlexander Duyck 	data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask);
4206f532612SEric Dumazet 	local_irq_restore(flags);
4216f532612SEric Dumazet 	return data;
4226f532612SEric Dumazet }
423c93bdd0eSMel Gorman 
424c93bdd0eSMel Gorman /**
425c93bdd0eSMel Gorman  * netdev_alloc_frag - allocate a page fragment
426c93bdd0eSMel Gorman  * @fragsz: fragment size
427c93bdd0eSMel Gorman  *
428c93bdd0eSMel Gorman  * Allocates a frag from a page for receive buffer.
429c93bdd0eSMel Gorman  * Uses GFP_ATOMIC allocations.
430c93bdd0eSMel Gorman  */
431c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz)
432c93bdd0eSMel Gorman {
433c93bdd0eSMel Gorman 	return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
434c93bdd0eSMel Gorman }
4356f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag);
4366f532612SEric Dumazet 
437ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask)
438ffde7328SAlexander Duyck {
439ffde7328SAlexander Duyck 	return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask);
440ffde7328SAlexander Duyck }
441ffde7328SAlexander Duyck 
442ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz)
443ffde7328SAlexander Duyck {
444ffde7328SAlexander Duyck 	return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD);
445ffde7328SAlexander Duyck }
446ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag);
447ffde7328SAlexander Duyck 
4486f532612SEric Dumazet /**
449fd11a83dSAlexander Duyck  *	__alloc_rx_skb - allocate an skbuff for rx
4508af27456SChristoph Hellwig  *	@length: length to allocate
4518af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
452fd11a83dSAlexander Duyck  *	@flags:	If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for
453fd11a83dSAlexander Duyck  *		allocations in case we have to fallback to __alloc_skb()
454fd11a83dSAlexander Duyck  *		If SKB_ALLOC_NAPI is set, page fragment will be allocated
455fd11a83dSAlexander Duyck  *		from napi_cache instead of netdev_cache.
4568af27456SChristoph Hellwig  *
4578af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
4588af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
4598af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
4608af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
4618af27456SChristoph Hellwig  *
4628af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
4638af27456SChristoph Hellwig  */
464fd11a83dSAlexander Duyck static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask,
465fd11a83dSAlexander Duyck 				      int flags)
4668af27456SChristoph Hellwig {
4676f532612SEric Dumazet 	struct sk_buff *skb = NULL;
468fd11a83dSAlexander Duyck 	unsigned int fragsz = SKB_DATA_ALIGN(length) +
469a1c7fff7SEric Dumazet 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4708af27456SChristoph Hellwig 
471310e158cSEric Dumazet 	if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) {
472c93bdd0eSMel Gorman 		void *data;
473c93bdd0eSMel Gorman 
474c93bdd0eSMel Gorman 		if (sk_memalloc_socks())
475c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
476c93bdd0eSMel Gorman 
477fd11a83dSAlexander Duyck 		data = (flags & SKB_ALLOC_NAPI) ?
478fd11a83dSAlexander Duyck 			__napi_alloc_frag(fragsz, gfp_mask) :
479fd11a83dSAlexander Duyck 			__netdev_alloc_frag(fragsz, gfp_mask);
480a1c7fff7SEric Dumazet 
4816f532612SEric Dumazet 		if (likely(data)) {
4826f532612SEric Dumazet 			skb = build_skb(data, fragsz);
4836f532612SEric Dumazet 			if (unlikely(!skb))
4846f532612SEric Dumazet 				put_page(virt_to_head_page(data));
485a1c7fff7SEric Dumazet 		}
486a1c7fff7SEric Dumazet 	} else {
487fd11a83dSAlexander Duyck 		skb = __alloc_skb(length, gfp_mask,
488c93bdd0eSMel Gorman 				  SKB_ALLOC_RX, NUMA_NO_NODE);
489a1c7fff7SEric Dumazet 	}
490fd11a83dSAlexander Duyck 	return skb;
491fd11a83dSAlexander Duyck }
492fd11a83dSAlexander Duyck 
493fd11a83dSAlexander Duyck /**
494fd11a83dSAlexander Duyck  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
495fd11a83dSAlexander Duyck  *	@dev: network device to receive on
496fd11a83dSAlexander Duyck  *	@length: length to allocate
497fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
498fd11a83dSAlexander Duyck  *
499fd11a83dSAlexander Duyck  *	Allocate a new &sk_buff and assign it a usage count of one. The
500fd11a83dSAlexander Duyck  *	buffer has NET_SKB_PAD headroom built in. Users should allocate
501fd11a83dSAlexander Duyck  *	the headroom they think they need without accounting for the
502fd11a83dSAlexander Duyck  *	built in space. The built in space is used for optimisations.
503fd11a83dSAlexander Duyck  *
504fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
505fd11a83dSAlexander Duyck  */
506fd11a83dSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
507fd11a83dSAlexander Duyck 				   unsigned int length, gfp_t gfp_mask)
508fd11a83dSAlexander Duyck {
509fd11a83dSAlexander Duyck 	struct sk_buff *skb;
510fd11a83dSAlexander Duyck 
511fd11a83dSAlexander Duyck 	length += NET_SKB_PAD;
512fd11a83dSAlexander Duyck 	skb = __alloc_rx_skb(length, gfp_mask, 0);
513fd11a83dSAlexander Duyck 
5147b2e497aSChristoph Hellwig 	if (likely(skb)) {
5158af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
5167b2e497aSChristoph Hellwig 		skb->dev = dev;
5177b2e497aSChristoph Hellwig 	}
518fd11a83dSAlexander Duyck 
5198af27456SChristoph Hellwig 	return skb;
5208af27456SChristoph Hellwig }
521b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
5221da177e4SLinus Torvalds 
523fd11a83dSAlexander Duyck /**
524fd11a83dSAlexander Duyck  *	__napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance
525fd11a83dSAlexander Duyck  *	@napi: napi instance this buffer was allocated for
526fd11a83dSAlexander Duyck  *	@length: length to allocate
527fd11a83dSAlexander Duyck  *	@gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages
528fd11a83dSAlexander Duyck  *
529fd11a83dSAlexander Duyck  *	Allocate a new sk_buff for use in NAPI receive.  This buffer will
530fd11a83dSAlexander Duyck  *	attempt to allocate the head from a special reserved region used
531fd11a83dSAlexander Duyck  *	only for NAPI Rx allocation.  By doing this we can save several
532fd11a83dSAlexander Duyck  *	CPU cycles by avoiding having to disable and re-enable IRQs.
533fd11a83dSAlexander Duyck  *
534fd11a83dSAlexander Duyck  *	%NULL is returned if there is no free memory.
535fd11a83dSAlexander Duyck  */
536fd11a83dSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
537fd11a83dSAlexander Duyck 				 unsigned int length, gfp_t gfp_mask)
538fd11a83dSAlexander Duyck {
539fd11a83dSAlexander Duyck 	struct sk_buff *skb;
540fd11a83dSAlexander Duyck 
541fd11a83dSAlexander Duyck 	length += NET_SKB_PAD + NET_IP_ALIGN;
542fd11a83dSAlexander Duyck 	skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI);
543fd11a83dSAlexander Duyck 
544fd11a83dSAlexander Duyck 	if (likely(skb)) {
545fd11a83dSAlexander Duyck 		skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
546fd11a83dSAlexander Duyck 		skb->dev = napi->dev;
547fd11a83dSAlexander Duyck 	}
548fd11a83dSAlexander Duyck 
549fd11a83dSAlexander Duyck 	return skb;
550fd11a83dSAlexander Duyck }
551fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb);
552fd11a83dSAlexander Duyck 
553654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
55450269e19SEric Dumazet 		     int size, unsigned int truesize)
555654bed16SPeter Zijlstra {
556654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
557654bed16SPeter Zijlstra 	skb->len += size;
558654bed16SPeter Zijlstra 	skb->data_len += size;
55950269e19SEric Dumazet 	skb->truesize += truesize;
560654bed16SPeter Zijlstra }
561654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
562654bed16SPeter Zijlstra 
563f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
564f8e617e1SJason Wang 			  unsigned int truesize)
565f8e617e1SJason Wang {
566f8e617e1SJason Wang 	skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
567f8e617e1SJason Wang 
568f8e617e1SJason Wang 	skb_frag_size_add(frag, size);
569f8e617e1SJason Wang 	skb->len += size;
570f8e617e1SJason Wang 	skb->data_len += size;
571f8e617e1SJason Wang 	skb->truesize += truesize;
572f8e617e1SJason Wang }
573f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag);
574f8e617e1SJason Wang 
57527b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
5761da177e4SLinus Torvalds {
577bd8a7036SEric Dumazet 	kfree_skb_list(*listp);
57827b437c8SHerbert Xu 	*listp = NULL;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
58127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
58227b437c8SHerbert Xu {
58327b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
58427b437c8SHerbert Xu }
58527b437c8SHerbert Xu 
5861da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct sk_buff *list;
5891da177e4SLinus Torvalds 
590fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
5911da177e4SLinus Torvalds 		skb_get(list);
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
594d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb)
595d3836f21SEric Dumazet {
596d3836f21SEric Dumazet 	if (skb->head_frag)
597d3836f21SEric Dumazet 		put_page(virt_to_head_page(skb->head));
598d3836f21SEric Dumazet 	else
599d3836f21SEric Dumazet 		kfree(skb->head);
600d3836f21SEric Dumazet }
601d3836f21SEric Dumazet 
6025bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
6031da177e4SLinus Torvalds {
604ff04a771SEric Dumazet 	struct skb_shared_info *shinfo = skb_shinfo(skb);
6051da177e4SLinus Torvalds 	int i;
606ff04a771SEric Dumazet 
607ff04a771SEric Dumazet 	if (skb->cloned &&
608ff04a771SEric Dumazet 	    atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
609ff04a771SEric Dumazet 			      &shinfo->dataref))
610ff04a771SEric Dumazet 		return;
611ff04a771SEric Dumazet 
612ff04a771SEric Dumazet 	for (i = 0; i < shinfo->nr_frags; i++)
613ff04a771SEric Dumazet 		__skb_frag_unref(&shinfo->frags[i]);
6141da177e4SLinus Torvalds 
615a6686f2fSShirley Ma 	/*
616a6686f2fSShirley Ma 	 * If skb buf is from userspace, we need to notify the caller
617a6686f2fSShirley Ma 	 * the lower device DMA has done;
618a6686f2fSShirley Ma 	 */
619ff04a771SEric Dumazet 	if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) {
620a6686f2fSShirley Ma 		struct ubuf_info *uarg;
621a6686f2fSShirley Ma 
622ff04a771SEric Dumazet 		uarg = shinfo->destructor_arg;
623a6686f2fSShirley Ma 		if (uarg->callback)
624e19d6763SMichael S. Tsirkin 			uarg->callback(uarg, true);
625a6686f2fSShirley Ma 	}
626a6686f2fSShirley Ma 
627ff04a771SEric Dumazet 	if (shinfo->frag_list)
628ff04a771SEric Dumazet 		kfree_skb_list(shinfo->frag_list);
6291da177e4SLinus Torvalds 
630d3836f21SEric Dumazet 	skb_free_head(skb);
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /*
6341da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
6351da177e4SLinus Torvalds  */
6362d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
6371da177e4SLinus Torvalds {
638d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones;
639d179cd12SDavid S. Miller 
640d179cd12SDavid S. Miller 	switch (skb->fclone) {
641d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
6421da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
6436ffe75ebSEric Dumazet 		return;
644d179cd12SDavid S. Miller 
645d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
646d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb1);
6476ffe75ebSEric Dumazet 
6486ffe75ebSEric Dumazet 		/* We usually free the clone (TX completion) before original skb
6496ffe75ebSEric Dumazet 		 * This test would have no chance to be true for the clone,
6506ffe75ebSEric Dumazet 		 * while here, branch prediction will be good.
6516ffe75ebSEric Dumazet 		 */
6526ffe75ebSEric Dumazet 		if (atomic_read(&fclones->fclone_ref) == 1)
6536ffe75ebSEric Dumazet 			goto fastpath;
654d179cd12SDavid S. Miller 		break;
655d179cd12SDavid S. Miller 
6566ffe75ebSEric Dumazet 	default: /* SKB_FCLONE_CLONE */
657d0bf4a9eSEric Dumazet 		fclones = container_of(skb, struct sk_buff_fclones, skb2);
658d179cd12SDavid S. Miller 		break;
6593ff50b79SStephen Hemminger 	}
6606ffe75ebSEric Dumazet 	if (!atomic_dec_and_test(&fclones->fclone_ref))
6616ffe75ebSEric Dumazet 		return;
6626ffe75ebSEric Dumazet fastpath:
6636ffe75ebSEric Dumazet 	kmem_cache_free(skbuff_fclone_cache, fclones);
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
66604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
6671da177e4SLinus Torvalds {
668adf30907SEric Dumazet 	skb_dst_drop(skb);
6691da177e4SLinus Torvalds #ifdef CONFIG_XFRM
6701da177e4SLinus Torvalds 	secpath_put(skb->sp);
6711da177e4SLinus Torvalds #endif
6721da177e4SLinus Torvalds 	if (skb->destructor) {
6739c2b3328SStephen Hemminger 		WARN_ON(in_irq());
6741da177e4SLinus Torvalds 		skb->destructor(skb);
6751da177e4SLinus Torvalds 	}
676a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK)
6775f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
6782fc72c7bSKOVACS Krisztian #endif
6791109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
6801da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
6811da177e4SLinus Torvalds #endif
68204a4bb55SLennert Buytenhek }
68304a4bb55SLennert Buytenhek 
68404a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
68504a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
68604a4bb55SLennert Buytenhek {
68704a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
6885e71d9d7SPablo Neira 	if (likely(skb->head))
6892d4baff8SHerbert Xu 		skb_release_data(skb);
6902d4baff8SHerbert Xu }
6911da177e4SLinus Torvalds 
6922d4baff8SHerbert Xu /**
6932d4baff8SHerbert Xu  *	__kfree_skb - private function
6942d4baff8SHerbert Xu  *	@skb: buffer
6952d4baff8SHerbert Xu  *
6962d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
6972d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
6982d4baff8SHerbert Xu  *	always call kfree_skb
6992d4baff8SHerbert Xu  */
7002d4baff8SHerbert Xu 
7012d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
7022d4baff8SHerbert Xu {
7032d4baff8SHerbert Xu 	skb_release_all(skb);
7041da177e4SLinus Torvalds 	kfree_skbmem(skb);
7051da177e4SLinus Torvalds }
706b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /**
709231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
710231d06aeSJörn Engel  *	@skb: buffer to free
711231d06aeSJörn Engel  *
712231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
713231d06aeSJörn Engel  *	hit zero.
714231d06aeSJörn Engel  */
715231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
716231d06aeSJörn Engel {
717231d06aeSJörn Engel 	if (unlikely(!skb))
718231d06aeSJörn Engel 		return;
719231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
720231d06aeSJörn Engel 		smp_rmb();
721231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
722231d06aeSJörn Engel 		return;
723ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
724231d06aeSJörn Engel 	__kfree_skb(skb);
725231d06aeSJörn Engel }
726b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
727231d06aeSJörn Engel 
728bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs)
729bd8a7036SEric Dumazet {
730bd8a7036SEric Dumazet 	while (segs) {
731bd8a7036SEric Dumazet 		struct sk_buff *next = segs->next;
732bd8a7036SEric Dumazet 
733bd8a7036SEric Dumazet 		kfree_skb(segs);
734bd8a7036SEric Dumazet 		segs = next;
735bd8a7036SEric Dumazet 	}
736bd8a7036SEric Dumazet }
737bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list);
738bd8a7036SEric Dumazet 
739d1a203eaSStephen Hemminger /**
74025121173SMichael S. Tsirkin  *	skb_tx_error - report an sk_buff xmit error
74125121173SMichael S. Tsirkin  *	@skb: buffer that triggered an error
74225121173SMichael S. Tsirkin  *
74325121173SMichael S. Tsirkin  *	Report xmit error if a device callback is tracking this skb.
74425121173SMichael S. Tsirkin  *	skb must be freed afterwards.
74525121173SMichael S. Tsirkin  */
74625121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb)
74725121173SMichael S. Tsirkin {
74825121173SMichael S. Tsirkin 	if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
74925121173SMichael S. Tsirkin 		struct ubuf_info *uarg;
75025121173SMichael S. Tsirkin 
75125121173SMichael S. Tsirkin 		uarg = skb_shinfo(skb)->destructor_arg;
75225121173SMichael S. Tsirkin 		if (uarg->callback)
75325121173SMichael S. Tsirkin 			uarg->callback(uarg, false);
75425121173SMichael S. Tsirkin 		skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
75525121173SMichael S. Tsirkin 	}
75625121173SMichael S. Tsirkin }
75725121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error);
75825121173SMichael S. Tsirkin 
75925121173SMichael S. Tsirkin /**
760ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
761ead2ceb0SNeil Horman  *	@skb: buffer to free
762ead2ceb0SNeil Horman  *
763ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
764ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
765ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
766ead2ceb0SNeil Horman  */
767ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
768ead2ceb0SNeil Horman {
769ead2ceb0SNeil Horman 	if (unlikely(!skb))
770ead2ceb0SNeil Horman 		return;
771ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
772ead2ceb0SNeil Horman 		smp_rmb();
773ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
774ead2ceb0SNeil Horman 		return;
77507dc22e7SKoki Sanagi 	trace_consume_skb(skb);
776ead2ceb0SNeil Horman 	__kfree_skb(skb);
777ead2ceb0SNeil Horman }
778ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
779ead2ceb0SNeil Horman 
780b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */
781b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \
782b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) <		\
783b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_start));	\
784b1937227SEric Dumazet 	BUILD_BUG_ON(offsetof(struct sk_buff, field) >		\
785b1937227SEric Dumazet 		     offsetof(struct sk_buff, headers_end));	\
786b1937227SEric Dumazet 
787dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
788dec18810SHerbert Xu {
789dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
790b1937227SEric Dumazet 	/* We do not copy old->sk */
791dec18810SHerbert Xu 	new->dev		= old->dev;
792b1937227SEric Dumazet 	memcpy(new->cb, old->cb, sizeof(old->cb));
7937fee226aSEric Dumazet 	skb_dst_copy(new, old);
794def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
795dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
796dec18810SHerbert Xu #endif
797b1937227SEric Dumazet 	__nf_copy(new, old, false);
7986aa895b0SPatrick McHardy 
799b1937227SEric Dumazet 	/* Note : this field could be in headers_start/headers_end section
800b1937227SEric Dumazet 	 * It is not yet because we do not want to have a 16 bit hole
801b1937227SEric Dumazet 	 */
802b1937227SEric Dumazet 	new->queue_mapping = old->queue_mapping;
80306021292SEliezer Tamir 
804b1937227SEric Dumazet 	memcpy(&new->headers_start, &old->headers_start,
805b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_end) -
806b1937227SEric Dumazet 	       offsetof(struct sk_buff, headers_start));
807b1937227SEric Dumazet 	CHECK_SKB_FIELD(protocol);
808b1937227SEric Dumazet 	CHECK_SKB_FIELD(csum);
809b1937227SEric Dumazet 	CHECK_SKB_FIELD(hash);
810b1937227SEric Dumazet 	CHECK_SKB_FIELD(priority);
811b1937227SEric Dumazet 	CHECK_SKB_FIELD(skb_iif);
812b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_proto);
813b1937227SEric Dumazet 	CHECK_SKB_FIELD(vlan_tci);
814b1937227SEric Dumazet 	CHECK_SKB_FIELD(transport_header);
815b1937227SEric Dumazet 	CHECK_SKB_FIELD(network_header);
816b1937227SEric Dumazet 	CHECK_SKB_FIELD(mac_header);
817b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_protocol);
818b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_transport_header);
819b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_network_header);
820b1937227SEric Dumazet 	CHECK_SKB_FIELD(inner_mac_header);
821b1937227SEric Dumazet 	CHECK_SKB_FIELD(mark);
822b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK
823b1937227SEric Dumazet 	CHECK_SKB_FIELD(secmark);
824b1937227SEric Dumazet #endif
825e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL
826b1937227SEric Dumazet 	CHECK_SKB_FIELD(napi_id);
82706021292SEliezer Tamir #endif
8282bd82484SEric Dumazet #ifdef CONFIG_XPS
8292bd82484SEric Dumazet 	CHECK_SKB_FIELD(sender_cpu);
8302bd82484SEric Dumazet #endif
831b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED
832b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_index);
833b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT
834b1937227SEric Dumazet 	CHECK_SKB_FIELD(tc_verd);
835b1937227SEric Dumazet #endif
836b1937227SEric Dumazet #endif
837b1937227SEric Dumazet 
838dec18810SHerbert Xu }
839dec18810SHerbert Xu 
84082c49a35SHerbert Xu /*
84182c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
84282c49a35SHerbert Xu  * __copy_skb_header above instead.
84382c49a35SHerbert Xu  */
844e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds #define C(x) n->x = skb->x
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	n->next = n->prev = NULL;
8491da177e4SLinus Torvalds 	n->sk = NULL;
850dec18810SHerbert Xu 	__copy_skb_header(n, skb);
851dec18810SHerbert Xu 
8521da177e4SLinus Torvalds 	C(len);
8531da177e4SLinus Torvalds 	C(data_len);
8543e6b3b2eSAlexey Dobriyan 	C(mac_len);
855334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
85602f1c89dSPaul Moore 	n->cloned = 1;
8571da177e4SLinus Torvalds 	n->nohdr = 0;
8581da177e4SLinus Torvalds 	n->destructor = NULL;
8591da177e4SLinus Torvalds 	C(tail);
8601da177e4SLinus Torvalds 	C(end);
86102f1c89dSPaul Moore 	C(head);
862d3836f21SEric Dumazet 	C(head_frag);
86302f1c89dSPaul Moore 	C(data);
86402f1c89dSPaul Moore 	C(truesize);
86502f1c89dSPaul Moore 	atomic_set(&n->users, 1);
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
8681da177e4SLinus Torvalds 	skb->cloned = 1;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	return n;
871e0053ec0SHerbert Xu #undef C
872e0053ec0SHerbert Xu }
873e0053ec0SHerbert Xu 
874e0053ec0SHerbert Xu /**
875e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
876e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
877e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
878e0053ec0SHerbert Xu  *
879e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
880e0053ec0SHerbert Xu  *	supplied by the user.
881e0053ec0SHerbert Xu  *
882e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
883e0053ec0SHerbert Xu  */
884e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
885e0053ec0SHerbert Xu {
8862d4baff8SHerbert Xu 	skb_release_all(dst);
887e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
888e0053ec0SHerbert Xu }
889e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
890e0053ec0SHerbert Xu 
8912c53040fSBen Hutchings /**
8922c53040fSBen Hutchings  *	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
89348c83012SMichael S. Tsirkin  *	@skb: the skb to modify
89448c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
89548c83012SMichael S. Tsirkin  *
89648c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
89748c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
89848c83012SMichael S. Tsirkin  *	to userspace pages.
89948c83012SMichael S. Tsirkin  *
90048c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
90148c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
90248c83012SMichael S. Tsirkin  *
90348c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
90448c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
90548c83012SMichael S. Tsirkin  */
90648c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
907a6686f2fSShirley Ma {
908a6686f2fSShirley Ma 	int i;
909a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
910a6686f2fSShirley Ma 	struct page *page, *head = NULL;
911a6686f2fSShirley Ma 	struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
912a6686f2fSShirley Ma 
913a6686f2fSShirley Ma 	for (i = 0; i < num_frags; i++) {
914a6686f2fSShirley Ma 		u8 *vaddr;
915a6686f2fSShirley Ma 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
916a6686f2fSShirley Ma 
91702756ed4SKrishna Kumar 		page = alloc_page(gfp_mask);
918a6686f2fSShirley Ma 		if (!page) {
919a6686f2fSShirley Ma 			while (head) {
92040dadff2SSunghan Suh 				struct page *next = (struct page *)page_private(head);
921a6686f2fSShirley Ma 				put_page(head);
922a6686f2fSShirley Ma 				head = next;
923a6686f2fSShirley Ma 			}
924a6686f2fSShirley Ma 			return -ENOMEM;
925a6686f2fSShirley Ma 		}
92651c56b00SEric Dumazet 		vaddr = kmap_atomic(skb_frag_page(f));
927a6686f2fSShirley Ma 		memcpy(page_address(page),
9289e903e08SEric Dumazet 		       vaddr + f->page_offset, skb_frag_size(f));
92951c56b00SEric Dumazet 		kunmap_atomic(vaddr);
93040dadff2SSunghan Suh 		set_page_private(page, (unsigned long)head);
931a6686f2fSShirley Ma 		head = page;
932a6686f2fSShirley Ma 	}
933a6686f2fSShirley Ma 
934a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
93502756ed4SKrishna Kumar 	for (i = 0; i < num_frags; i++)
936a8605c60SIan Campbell 		skb_frag_unref(skb, i);
937a6686f2fSShirley Ma 
938e19d6763SMichael S. Tsirkin 	uarg->callback(uarg, false);
939a6686f2fSShirley Ma 
940a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
94102756ed4SKrishna Kumar 	for (i = num_frags - 1; i >= 0; i--) {
94202756ed4SKrishna Kumar 		__skb_fill_page_desc(skb, i, head, 0,
94302756ed4SKrishna Kumar 				     skb_shinfo(skb)->frags[i].size);
94440dadff2SSunghan Suh 		head = (struct page *)page_private(head);
945a6686f2fSShirley Ma 	}
94648c83012SMichael S. Tsirkin 
94748c83012SMichael S. Tsirkin 	skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
948a6686f2fSShirley Ma 	return 0;
949a6686f2fSShirley Ma }
950dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs);
951a6686f2fSShirley Ma 
952e0053ec0SHerbert Xu /**
953e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
954e0053ec0SHerbert Xu  *	@skb: buffer to clone
955e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
956e0053ec0SHerbert Xu  *
957e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
958e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
959e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
960e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
961e0053ec0SHerbert Xu  *
962e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
963e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
964e0053ec0SHerbert Xu  */
965e0053ec0SHerbert Xu 
966e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
967e0053ec0SHerbert Xu {
968d0bf4a9eSEric Dumazet 	struct sk_buff_fclones *fclones = container_of(skb,
969d0bf4a9eSEric Dumazet 						       struct sk_buff_fclones,
970d0bf4a9eSEric Dumazet 						       skb1);
9716ffe75ebSEric Dumazet 	struct sk_buff *n;
972e0053ec0SHerbert Xu 
97370008aa5SMichael S. Tsirkin 	if (skb_orphan_frags(skb, gfp_mask))
974a6686f2fSShirley Ma 		return NULL;
975a6686f2fSShirley Ma 
976e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
9776ffe75ebSEric Dumazet 	    atomic_read(&fclones->fclone_ref) == 1) {
9786ffe75ebSEric Dumazet 		n = &fclones->skb2;
9796ffe75ebSEric Dumazet 		atomic_set(&fclones->fclone_ref, 2);
980e0053ec0SHerbert Xu 	} else {
981c93bdd0eSMel Gorman 		if (skb_pfmemalloc(skb))
982c93bdd0eSMel Gorman 			gfp_mask |= __GFP_MEMALLOC;
983c93bdd0eSMel Gorman 
984e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
985e0053ec0SHerbert Xu 		if (!n)
986e0053ec0SHerbert Xu 			return NULL;
987fe55f6d5SVegard Nossum 
988fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags1);
989e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
990e0053ec0SHerbert Xu 	}
991e0053ec0SHerbert Xu 
992e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
9931da177e4SLinus Torvalds }
994b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
9951da177e4SLinus Torvalds 
996f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off)
997f5b17294SPravin B Shelar {
998030737bcSEric Dumazet 	/* Only adjust this if it actually is csum_start rather than csum */
999030737bcSEric Dumazet 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1000030737bcSEric Dumazet 		skb->csum_start += off;
1001f5b17294SPravin B Shelar 	/* {transport,network,mac}_header and tail are relative to skb->head */
1002f5b17294SPravin B Shelar 	skb->transport_header += off;
1003f5b17294SPravin B Shelar 	skb->network_header   += off;
1004f5b17294SPravin B Shelar 	if (skb_mac_header_was_set(skb))
1005f5b17294SPravin B Shelar 		skb->mac_header += off;
1006f5b17294SPravin B Shelar 	skb->inner_transport_header += off;
1007f5b17294SPravin B Shelar 	skb->inner_network_header += off;
1008aefbd2b3SPravin B Shelar 	skb->inner_mac_header += off;
1009f5b17294SPravin B Shelar }
1010f5b17294SPravin B Shelar 
10111da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
10121da177e4SLinus Torvalds {
1013dec18810SHerbert Xu 	__copy_skb_header(new, old);
1014dec18810SHerbert Xu 
10157967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
10167967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
10177967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
1020c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb)
1021c93bdd0eSMel Gorman {
1022c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1023c93bdd0eSMel Gorman 		return SKB_ALLOC_RX;
1024c93bdd0eSMel Gorman 	return 0;
1025c93bdd0eSMel Gorman }
1026c93bdd0eSMel Gorman 
10271da177e4SLinus Torvalds /**
10281da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
10291da177e4SLinus Torvalds  *	@skb: buffer to copy
10301da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
10311da177e4SLinus Torvalds  *
10321da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
10331da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
10341da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
10351da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
10361da177e4SLinus Torvalds  *
10371da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
10381da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
10391da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
10401da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
10411da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
10421da177e4SLinus Torvalds  */
10431da177e4SLinus Torvalds 
1044dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
10451da177e4SLinus Torvalds {
10466602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
1047ec47ea82SAlexander Duyck 	unsigned int size = skb_end_offset(skb) + skb->data_len;
1048c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(size, gfp_mask,
1049c93bdd0eSMel Gorman 					skb_alloc_rx_flag(skb), NUMA_NO_NODE);
10506602cebbSEric Dumazet 
10511da177e4SLinus Torvalds 	if (!n)
10521da177e4SLinus Torvalds 		return NULL;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	/* Set the data pointer */
10551da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
10561da177e4SLinus Torvalds 	/* Set the tail pointer and length */
10571da177e4SLinus Torvalds 	skb_put(n, skb->len);
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
10601da177e4SLinus Torvalds 		BUG();
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	copy_skb_header(n, skb);
10631da177e4SLinus Torvalds 	return n;
10641da177e4SLinus Torvalds }
1065b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds /**
1068bad93e9dSOctavian Purdila  *	__pskb_copy_fclone	-  create copy of an sk_buff with private head.
10691da177e4SLinus Torvalds  *	@skb: buffer to copy
1070117632e6SEric Dumazet  *	@headroom: headroom of new skb
10711da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
1072bad93e9dSOctavian Purdila  *	@fclone: if true allocate the copy of the skb from the fclone
1073bad93e9dSOctavian Purdila  *	cache instead of the head cache; it is recommended to set this
1074bad93e9dSOctavian Purdila  *	to true for the cases where the copy will likely be cloned
10751da177e4SLinus Torvalds  *
10761da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
10771da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
10781da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
10791da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
10801da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
10811da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
10821da177e4SLinus Torvalds  */
10831da177e4SLinus Torvalds 
1084bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
1085bad93e9dSOctavian Purdila 				   gfp_t gfp_mask, bool fclone)
10861da177e4SLinus Torvalds {
1087117632e6SEric Dumazet 	unsigned int size = skb_headlen(skb) + headroom;
1088bad93e9dSOctavian Purdila 	int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0);
1089bad93e9dSOctavian Purdila 	struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE);
10906602cebbSEric Dumazet 
10911da177e4SLinus Torvalds 	if (!n)
10921da177e4SLinus Torvalds 		goto out;
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds 	/* Set the data pointer */
1095117632e6SEric Dumazet 	skb_reserve(n, headroom);
10961da177e4SLinus Torvalds 	/* Set the tail pointer and length */
10971da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
10981da177e4SLinus Torvalds 	/* Copy the bytes */
1099d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
11001da177e4SLinus Torvalds 
110125f484a6SHerbert Xu 	n->truesize += skb->data_len;
11021da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
11031da177e4SLinus Torvalds 	n->len	     = skb->len;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
11061da177e4SLinus Torvalds 		int i;
11071da177e4SLinus Torvalds 
110870008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask)) {
11091511022cSDan Carpenter 			kfree_skb(n);
11101511022cSDan Carpenter 			n = NULL;
1111a6686f2fSShirley Ma 			goto out;
1112a6686f2fSShirley Ma 		}
11131da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
11141da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
1115ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
11161da177e4SLinus Torvalds 		}
11171da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
11181da177e4SLinus Torvalds 	}
11191da177e4SLinus Torvalds 
112021dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
11211da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
11221da177e4SLinus Torvalds 		skb_clone_fraglist(n);
11231da177e4SLinus Torvalds 	}
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	copy_skb_header(n, skb);
11261da177e4SLinus Torvalds out:
11271da177e4SLinus Torvalds 	return n;
11281da177e4SLinus Torvalds }
1129bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds /**
11321da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
11331da177e4SLinus Torvalds  *	@skb: buffer to reallocate
11341da177e4SLinus Torvalds  *	@nhead: room to add at head
11351da177e4SLinus Torvalds  *	@ntail: room to add at tail
11361da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
11371da177e4SLinus Torvalds  *
1138bc32383cSMathias Krause  *	Expands (or creates identical copy, if @nhead and @ntail are zero)
1139bc32383cSMathias Krause  *	header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
11401da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
11411da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
11421da177e4SLinus Torvalds  *
11431da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
11441da177e4SLinus Torvalds  *	reloaded after call to this function.
11451da177e4SLinus Torvalds  */
11461da177e4SLinus Torvalds 
114786a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1148dd0fc66fSAl Viro 		     gfp_t gfp_mask)
11491da177e4SLinus Torvalds {
11501da177e4SLinus Torvalds 	int i;
11511da177e4SLinus Torvalds 	u8 *data;
1152ec47ea82SAlexander Duyck 	int size = nhead + skb_end_offset(skb) + ntail;
11531da177e4SLinus Torvalds 	long off;
11541da177e4SLinus Torvalds 
11554edd87adSHerbert Xu 	BUG_ON(nhead < 0);
11564edd87adSHerbert Xu 
11571da177e4SLinus Torvalds 	if (skb_shared(skb))
11581da177e4SLinus Torvalds 		BUG();
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
11611da177e4SLinus Torvalds 
1162c93bdd0eSMel Gorman 	if (skb_pfmemalloc(skb))
1163c93bdd0eSMel Gorman 		gfp_mask |= __GFP_MEMALLOC;
1164c93bdd0eSMel Gorman 	data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)),
1165c93bdd0eSMel Gorman 			       gfp_mask, NUMA_NO_NODE, NULL);
11661da177e4SLinus Torvalds 	if (!data)
11671da177e4SLinus Torvalds 		goto nodata;
116887151b86SEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
11716602cebbSEric Dumazet 	 * optimized for the cases when header is void.
11726602cebbSEric Dumazet 	 */
11736602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
11746602cebbSEric Dumazet 
11756602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
11766602cebbSEric Dumazet 	       skb_shinfo(skb),
1177fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
11781da177e4SLinus Torvalds 
11793e24591aSAlexander Duyck 	/*
11803e24591aSAlexander Duyck 	 * if shinfo is shared we must drop the old head gracefully, but if it
11813e24591aSAlexander Duyck 	 * is not we can just drop the old head and let the existing refcount
11823e24591aSAlexander Duyck 	 * be since all we did is relocate the values
11833e24591aSAlexander Duyck 	 */
11843e24591aSAlexander Duyck 	if (skb_cloned(skb)) {
1185a6686f2fSShirley Ma 		/* copy this zero copy skb frags */
118670008aa5SMichael S. Tsirkin 		if (skb_orphan_frags(skb, gfp_mask))
1187a6686f2fSShirley Ma 			goto nofrags;
11881da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1189ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
11901da177e4SLinus Torvalds 
119121dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
11921da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 		skb_release_data(skb);
11953e24591aSAlexander Duyck 	} else {
11963e24591aSAlexander Duyck 		skb_free_head(skb);
11971fd63041SEric Dumazet 	}
11981da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	skb->head     = data;
1201d3836f21SEric Dumazet 	skb->head_frag = 0;
12021da177e4SLinus Torvalds 	skb->data    += off;
12034305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
12044305b541SArnaldo Carvalho de Melo 	skb->end      = size;
120556eb8882SPatrick McHardy 	off           = nhead;
12064305b541SArnaldo Carvalho de Melo #else
12074305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
120856eb8882SPatrick McHardy #endif
120927a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
1210b41abb42SPeter Pan(潘卫平) 	skb_headers_offset_update(skb, nhead);
12111da177e4SLinus Torvalds 	skb->cloned   = 0;
1212334a8132SPatrick McHardy 	skb->hdr_len  = 0;
12131da177e4SLinus Torvalds 	skb->nohdr    = 0;
12141da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
12151da177e4SLinus Torvalds 	return 0;
12161da177e4SLinus Torvalds 
1217a6686f2fSShirley Ma nofrags:
1218a6686f2fSShirley Ma 	kfree(data);
12191da177e4SLinus Torvalds nodata:
12201da177e4SLinus Torvalds 	return -ENOMEM;
12211da177e4SLinus Torvalds }
1222b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
12271da177e4SLinus Torvalds {
12281da177e4SLinus Torvalds 	struct sk_buff *skb2;
12291da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	if (delta <= 0)
12321da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
12331da177e4SLinus Torvalds 	else {
12341da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
12351da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
12361da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
12371da177e4SLinus Torvalds 			kfree_skb(skb2);
12381da177e4SLinus Torvalds 			skb2 = NULL;
12391da177e4SLinus Torvalds 		}
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 	return skb2;
12421da177e4SLinus Torvalds }
1243b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds /**
12461da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
12471da177e4SLinus Torvalds  *	@skb: buffer to copy
12481da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
12491da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
12501da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
12511da177e4SLinus Torvalds  *
12521da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
12531da177e4SLinus Torvalds  *	allocate additional space.
12541da177e4SLinus Torvalds  *
12551da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
12561da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
12571da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
12581da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
12591da177e4SLinus Torvalds  *
12601da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
12611da177e4SLinus Torvalds  *	is called from an interrupt.
12621da177e4SLinus Torvalds  */
12631da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
126486a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1265dd0fc66fSAl Viro 				gfp_t gfp_mask)
12661da177e4SLinus Torvalds {
12671da177e4SLinus Torvalds 	/*
12681da177e4SLinus Torvalds 	 *	Allocate the copy buffer
12691da177e4SLinus Torvalds 	 */
1270c93bdd0eSMel Gorman 	struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom,
1271c93bdd0eSMel Gorman 					gfp_mask, skb_alloc_rx_flag(skb),
1272c93bdd0eSMel Gorman 					NUMA_NO_NODE);
1273efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
12741da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	if (!n)
12771da177e4SLinus Torvalds 		return NULL;
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	/* Set the tail pointer and length */
12821da177e4SLinus Torvalds 	skb_put(n, skb->len);
12831da177e4SLinus Torvalds 
1284efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
12851da177e4SLinus Torvalds 	head_copy_off = 0;
12861da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
12871da177e4SLinus Torvalds 		head_copy_len = newheadroom;
12881da177e4SLinus Torvalds 	else
12891da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	/* Copy the linear header and data. */
12921da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
12931da177e4SLinus Torvalds 			  skb->len + head_copy_len))
12941da177e4SLinus Torvalds 		BUG();
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	copy_skb_header(n, skb);
12971da177e4SLinus Torvalds 
1298030737bcSEric Dumazet 	skb_headers_offset_update(n, newheadroom - oldheadroom);
1299efd1e8d5SPatrick McHardy 
13001da177e4SLinus Torvalds 	return n;
13011da177e4SLinus Torvalds }
1302b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds /**
13051da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
13061da177e4SLinus Torvalds  *	@skb: buffer to pad
13071da177e4SLinus Torvalds  *	@pad: space to pad
13081da177e4SLinus Torvalds  *
13091da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
13101da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
13111da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
13121da177e4SLinus Torvalds  *
13135b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
13141da177e4SLinus Torvalds  */
13151da177e4SLinus Torvalds 
13165b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
13171da177e4SLinus Torvalds {
13185b057c6bSHerbert Xu 	int err;
13195b057c6bSHerbert Xu 	int ntail;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
13225b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
13231da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
13245b057c6bSHerbert Xu 		return 0;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13274305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
13285b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
13295b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
13305b057c6bSHerbert Xu 		if (unlikely(err))
13315b057c6bSHerbert Xu 			goto free_skb;
13325b057c6bSHerbert Xu 	}
13335b057c6bSHerbert Xu 
13345b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
13355b057c6bSHerbert Xu 	 * to be audited.
13365b057c6bSHerbert Xu 	 */
13375b057c6bSHerbert Xu 	err = skb_linearize(skb);
13385b057c6bSHerbert Xu 	if (unlikely(err))
13395b057c6bSHerbert Xu 		goto free_skb;
13405b057c6bSHerbert Xu 
13415b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
13425b057c6bSHerbert Xu 	return 0;
13435b057c6bSHerbert Xu 
13445b057c6bSHerbert Xu free_skb:
13451da177e4SLinus Torvalds 	kfree_skb(skb);
13465b057c6bSHerbert Xu 	return err;
13471da177e4SLinus Torvalds }
1348b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
13491da177e4SLinus Torvalds 
13500dde3e16SIlpo Järvinen /**
13510c7ddf36SMathias Krause  *	pskb_put - add data to the tail of a potentially fragmented buffer
13520c7ddf36SMathias Krause  *	@skb: start of the buffer to use
13530c7ddf36SMathias Krause  *	@tail: tail fragment of the buffer to use
13540c7ddf36SMathias Krause  *	@len: amount of data to add
13550c7ddf36SMathias Krause  *
13560c7ddf36SMathias Krause  *	This function extends the used data area of the potentially
13570c7ddf36SMathias Krause  *	fragmented buffer. @tail must be the last fragment of @skb -- or
13580c7ddf36SMathias Krause  *	@skb itself. If this would exceed the total buffer size the kernel
13590c7ddf36SMathias Krause  *	will panic. A pointer to the first byte of the extra data is
13600c7ddf36SMathias Krause  *	returned.
13610c7ddf36SMathias Krause  */
13620c7ddf36SMathias Krause 
13630c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
13640c7ddf36SMathias Krause {
13650c7ddf36SMathias Krause 	if (tail != skb) {
13660c7ddf36SMathias Krause 		skb->data_len += len;
13670c7ddf36SMathias Krause 		skb->len += len;
13680c7ddf36SMathias Krause 	}
13690c7ddf36SMathias Krause 	return skb_put(tail, len);
13700c7ddf36SMathias Krause }
13710c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put);
13720c7ddf36SMathias Krause 
13730c7ddf36SMathias Krause /**
13740dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
13750dde3e16SIlpo Järvinen  *	@skb: buffer to use
13760dde3e16SIlpo Järvinen  *	@len: amount of data to add
13770dde3e16SIlpo Järvinen  *
13780dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
13790dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
13800dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
13810dde3e16SIlpo Järvinen  */
13820dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
13830dde3e16SIlpo Järvinen {
13840dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
13850dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
13860dde3e16SIlpo Järvinen 	skb->tail += len;
13870dde3e16SIlpo Järvinen 	skb->len  += len;
13880dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
13890dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
13900dde3e16SIlpo Järvinen 	return tmp;
13910dde3e16SIlpo Järvinen }
13920dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
13930dde3e16SIlpo Järvinen 
13946be8ac2fSIlpo Järvinen /**
1395c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1396c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1397c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1398c2aa270aSIlpo Järvinen  *
1399c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1400c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1401c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1402c2aa270aSIlpo Järvinen  */
1403c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1404c2aa270aSIlpo Järvinen {
1405c2aa270aSIlpo Järvinen 	skb->data -= len;
1406c2aa270aSIlpo Järvinen 	skb->len  += len;
1407c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1408c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1409c2aa270aSIlpo Järvinen 	return skb->data;
1410c2aa270aSIlpo Järvinen }
1411c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1412c2aa270aSIlpo Järvinen 
1413c2aa270aSIlpo Järvinen /**
14146be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
14156be8ac2fSIlpo Järvinen  *	@skb: buffer to use
14166be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
14176be8ac2fSIlpo Järvinen  *
14186be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
14196be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
14206be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
14216be8ac2fSIlpo Järvinen  *	the old data.
14226be8ac2fSIlpo Järvinen  */
14236be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
14246be8ac2fSIlpo Järvinen {
142547d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
14266be8ac2fSIlpo Järvinen }
14276be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
14286be8ac2fSIlpo Järvinen 
1429419ae74eSIlpo Järvinen /**
1430419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1431419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1432419ae74eSIlpo Järvinen  *	@len: new length
1433419ae74eSIlpo Järvinen  *
1434419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1435419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1436419ae74eSIlpo Järvinen  *	The skb must be linear.
1437419ae74eSIlpo Järvinen  */
1438419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1439419ae74eSIlpo Järvinen {
1440419ae74eSIlpo Järvinen 	if (skb->len > len)
1441419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1442419ae74eSIlpo Järvinen }
1443419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1444419ae74eSIlpo Järvinen 
14453cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
14461da177e4SLinus Torvalds  */
14471da177e4SLinus Torvalds 
14483cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
14491da177e4SLinus Torvalds {
145027b437c8SHerbert Xu 	struct sk_buff **fragp;
145127b437c8SHerbert Xu 	struct sk_buff *frag;
14521da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
14531da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
14541da177e4SLinus Torvalds 	int i;
145527b437c8SHerbert Xu 	int err;
145627b437c8SHerbert Xu 
145727b437c8SHerbert Xu 	if (skb_cloned(skb) &&
145827b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
145927b437c8SHerbert Xu 		return err;
14601da177e4SLinus Torvalds 
1461f4d26fb3SHerbert Xu 	i = 0;
1462f4d26fb3SHerbert Xu 	if (offset >= len)
1463f4d26fb3SHerbert Xu 		goto drop_pages;
1464f4d26fb3SHerbert Xu 
1465f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
14669e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
146727b437c8SHerbert Xu 
146827b437c8SHerbert Xu 		if (end < len) {
14691da177e4SLinus Torvalds 			offset = end;
147027b437c8SHerbert Xu 			continue;
14711da177e4SLinus Torvalds 		}
14721da177e4SLinus Torvalds 
14739e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
147427b437c8SHerbert Xu 
1475f4d26fb3SHerbert Xu drop_pages:
147627b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
147727b437c8SHerbert Xu 
147827b437c8SHerbert Xu 		for (; i < nfrags; i++)
1479ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
148027b437c8SHerbert Xu 
148121dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
148227b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1483f4d26fb3SHerbert Xu 		goto done;
148427b437c8SHerbert Xu 	}
148527b437c8SHerbert Xu 
148627b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
148727b437c8SHerbert Xu 	     fragp = &frag->next) {
148827b437c8SHerbert Xu 		int end = offset + frag->len;
148927b437c8SHerbert Xu 
149027b437c8SHerbert Xu 		if (skb_shared(frag)) {
149127b437c8SHerbert Xu 			struct sk_buff *nfrag;
149227b437c8SHerbert Xu 
149327b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
149427b437c8SHerbert Xu 			if (unlikely(!nfrag))
149527b437c8SHerbert Xu 				return -ENOMEM;
149627b437c8SHerbert Xu 
149727b437c8SHerbert Xu 			nfrag->next = frag->next;
149885bb2a60SEric Dumazet 			consume_skb(frag);
149927b437c8SHerbert Xu 			frag = nfrag;
150027b437c8SHerbert Xu 			*fragp = frag;
150127b437c8SHerbert Xu 		}
150227b437c8SHerbert Xu 
150327b437c8SHerbert Xu 		if (end < len) {
150427b437c8SHerbert Xu 			offset = end;
150527b437c8SHerbert Xu 			continue;
150627b437c8SHerbert Xu 		}
150727b437c8SHerbert Xu 
150827b437c8SHerbert Xu 		if (end > len &&
150927b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
151027b437c8SHerbert Xu 			return err;
151127b437c8SHerbert Xu 
151227b437c8SHerbert Xu 		if (frag->next)
151327b437c8SHerbert Xu 			skb_drop_list(&frag->next);
151427b437c8SHerbert Xu 		break;
151527b437c8SHerbert Xu 	}
151627b437c8SHerbert Xu 
1517f4d26fb3SHerbert Xu done:
151827b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
15191da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
15201da177e4SLinus Torvalds 		skb->len       = len;
15211da177e4SLinus Torvalds 	} else {
15221da177e4SLinus Torvalds 		skb->len       = len;
15231da177e4SLinus Torvalds 		skb->data_len  = 0;
152427a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 	return 0;
15281da177e4SLinus Torvalds }
1529b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds /**
15321da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
15331da177e4SLinus Torvalds  *	@skb: buffer to reallocate
15341da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
15351da177e4SLinus Torvalds  *
15361da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
15371da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
15381da177e4SLinus Torvalds  *	data from fragmented part.
15391da177e4SLinus Torvalds  *
15401da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
15411da177e4SLinus Torvalds  *
15421da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
15431da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
15441da177e4SLinus Torvalds  *
15451da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
15461da177e4SLinus Torvalds  *	reloaded after call to this function.
15471da177e4SLinus Torvalds  */
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
15501da177e4SLinus Torvalds  * when it is necessary.
15511da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
15521da177e4SLinus Torvalds  * 2. It may change skb pointers.
15531da177e4SLinus Torvalds  *
15541da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
15551da177e4SLinus Torvalds  */
15561da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
15571da177e4SLinus Torvalds {
15581da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
15591da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
15601da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
15611da177e4SLinus Torvalds 	 */
15624305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
15651da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
15661da177e4SLinus Torvalds 				     GFP_ATOMIC))
15671da177e4SLinus Torvalds 			return NULL;
15681da177e4SLinus Torvalds 	}
15691da177e4SLinus Torvalds 
157027a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
15711da177e4SLinus Torvalds 		BUG();
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
15741da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
15751da177e4SLinus Torvalds 	 */
157621dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
15771da177e4SLinus Torvalds 		goto pull_pages;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
15801da177e4SLinus Torvalds 	eat = delta;
15811da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15829e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
15839e903e08SEric Dumazet 
15849e903e08SEric Dumazet 		if (size >= eat)
15851da177e4SLinus Torvalds 			goto pull_pages;
15869e903e08SEric Dumazet 		eat -= size;
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
15901da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
15911da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
15921da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
15931da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
15941da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
15951da177e4SLinus Torvalds 	 */
15961da177e4SLinus Torvalds 	if (eat) {
15971da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
15981da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
15991da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 		do {
160209a62660SKris Katterjohn 			BUG_ON(!list);
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 			if (list->len <= eat) {
16051da177e4SLinus Torvalds 				/* Eaten as whole. */
16061da177e4SLinus Torvalds 				eat -= list->len;
16071da177e4SLinus Torvalds 				list = list->next;
16081da177e4SLinus Torvalds 				insp = list;
16091da177e4SLinus Torvalds 			} else {
16101da177e4SLinus Torvalds 				/* Eaten partially. */
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 				if (skb_shared(list)) {
16131da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
16141da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
16151da177e4SLinus Torvalds 					if (!clone)
16161da177e4SLinus Torvalds 						return NULL;
16171da177e4SLinus Torvalds 					insp = list->next;
16181da177e4SLinus Torvalds 					list = clone;
16191da177e4SLinus Torvalds 				} else {
16201da177e4SLinus Torvalds 					/* This may be pulled without
16211da177e4SLinus Torvalds 					 * problems. */
16221da177e4SLinus Torvalds 					insp = list;
16231da177e4SLinus Torvalds 				}
16241da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
16251da177e4SLinus Torvalds 					kfree_skb(clone);
16261da177e4SLinus Torvalds 					return NULL;
16271da177e4SLinus Torvalds 				}
16281da177e4SLinus Torvalds 				break;
16291da177e4SLinus Torvalds 			}
16301da177e4SLinus Torvalds 		} while (eat);
16311da177e4SLinus Torvalds 
16321da177e4SLinus Torvalds 		/* Free pulled out fragments. */
16331da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
16341da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
16351da177e4SLinus Torvalds 			kfree_skb(list);
16361da177e4SLinus Torvalds 		}
16371da177e4SLinus Torvalds 		/* And insert new clone at head. */
16381da177e4SLinus Torvalds 		if (clone) {
16391da177e4SLinus Torvalds 			clone->next = list;
16401da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
16411da177e4SLinus Torvalds 		}
16421da177e4SLinus Torvalds 	}
16431da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds pull_pages:
16461da177e4SLinus Torvalds 	eat = delta;
16471da177e4SLinus Torvalds 	k = 0;
16481da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16499e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
16509e903e08SEric Dumazet 
16519e903e08SEric Dumazet 		if (size <= eat) {
1652ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
16539e903e08SEric Dumazet 			eat -= size;
16541da177e4SLinus Torvalds 		} else {
16551da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
16561da177e4SLinus Torvalds 			if (eat) {
16571da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
16589e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
16591da177e4SLinus Torvalds 				eat = 0;
16601da177e4SLinus Torvalds 			}
16611da177e4SLinus Torvalds 			k++;
16621da177e4SLinus Torvalds 		}
16631da177e4SLinus Torvalds 	}
16641da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	skb->tail     += delta;
16671da177e4SLinus Torvalds 	skb->data_len -= delta;
16681da177e4SLinus Torvalds 
166927a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
16701da177e4SLinus Torvalds }
1671b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
16721da177e4SLinus Torvalds 
167322019b17SEric Dumazet /**
167422019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
167522019b17SEric Dumazet  *	@skb: source skb
167622019b17SEric Dumazet  *	@offset: offset in source
167722019b17SEric Dumazet  *	@to: destination buffer
167822019b17SEric Dumazet  *	@len: number of bytes to copy
167922019b17SEric Dumazet  *
168022019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
168122019b17SEric Dumazet  *	destination buffer.
168222019b17SEric Dumazet  *
168322019b17SEric Dumazet  *	CAUTION ! :
168422019b17SEric Dumazet  *		If its prototype is ever changed,
168522019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
168622019b17SEric Dumazet  *		since it is called from BPF assembly code.
168722019b17SEric Dumazet  */
16881da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
16891da177e4SLinus Torvalds {
16901a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1691fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1692fbb398a8SDavid S. Miller 	int i, copy;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
16951da177e4SLinus Torvalds 		goto fault;
16961da177e4SLinus Torvalds 
16971da177e4SLinus Torvalds 	/* Copy header. */
16981a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
16991da177e4SLinus Torvalds 		if (copy > len)
17001da177e4SLinus Torvalds 			copy = len;
1701d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
17021da177e4SLinus Torvalds 		if ((len -= copy) == 0)
17031da177e4SLinus Torvalds 			return 0;
17041da177e4SLinus Torvalds 		offset += copy;
17051da177e4SLinus Torvalds 		to     += copy;
17061da177e4SLinus Torvalds 	}
17071da177e4SLinus Torvalds 
17081da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
17091a028e50SDavid S. Miller 		int end;
171051c56b00SEric Dumazet 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
17111da177e4SLinus Torvalds 
1712547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17131a028e50SDavid S. Miller 
171451c56b00SEric Dumazet 		end = start + skb_frag_size(f);
17151da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17161da177e4SLinus Torvalds 			u8 *vaddr;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 			if (copy > len)
17191da177e4SLinus Torvalds 				copy = len;
17201da177e4SLinus Torvalds 
172151c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(f));
17221da177e4SLinus Torvalds 			memcpy(to,
172351c56b00SEric Dumazet 			       vaddr + f->page_offset + offset - start,
172451c56b00SEric Dumazet 			       copy);
172551c56b00SEric Dumazet 			kunmap_atomic(vaddr);
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 			if ((len -= copy) == 0)
17281da177e4SLinus Torvalds 				return 0;
17291da177e4SLinus Torvalds 			offset += copy;
17301da177e4SLinus Torvalds 			to     += copy;
17311da177e4SLinus Torvalds 		}
17321a028e50SDavid S. Miller 		start = end;
17331da177e4SLinus Torvalds 	}
17341da177e4SLinus Torvalds 
1735fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
17361a028e50SDavid S. Miller 		int end;
17371da177e4SLinus Torvalds 
1738547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17391a028e50SDavid S. Miller 
1740fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
17411da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17421da177e4SLinus Torvalds 			if (copy > len)
17431da177e4SLinus Torvalds 				copy = len;
1744fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
17451da177e4SLinus Torvalds 				goto fault;
17461da177e4SLinus Torvalds 			if ((len -= copy) == 0)
17471da177e4SLinus Torvalds 				return 0;
17481da177e4SLinus Torvalds 			offset += copy;
17491da177e4SLinus Torvalds 			to     += copy;
17501da177e4SLinus Torvalds 		}
17511a028e50SDavid S. Miller 		start = end;
17521da177e4SLinus Torvalds 	}
1753a6686f2fSShirley Ma 
17541da177e4SLinus Torvalds 	if (!len)
17551da177e4SLinus Torvalds 		return 0;
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds fault:
17581da177e4SLinus Torvalds 	return -EFAULT;
17591da177e4SLinus Torvalds }
1760b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
17611da177e4SLinus Torvalds 
17629c55e01cSJens Axboe /*
17639c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
17649c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
17659c55e01cSJens Axboe  */
17669c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
17679c55e01cSJens Axboe {
17688b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
17698b9d3728SJarek Poplawski }
17709c55e01cSJens Axboe 
1771a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len,
17724fb66994SJarek Poplawski 				   unsigned int *offset,
177318aafc62SEric Dumazet 				   struct sock *sk)
17748b9d3728SJarek Poplawski {
17755640f768SEric Dumazet 	struct page_frag *pfrag = sk_page_frag(sk);
17768b9d3728SJarek Poplawski 
17775640f768SEric Dumazet 	if (!sk_page_frag_refill(sk, pfrag))
17788b9d3728SJarek Poplawski 		return NULL;
17794fb66994SJarek Poplawski 
17805640f768SEric Dumazet 	*len = min_t(unsigned int, *len, pfrag->size - pfrag->offset);
17814fb66994SJarek Poplawski 
17825640f768SEric Dumazet 	memcpy(page_address(pfrag->page) + pfrag->offset,
17835640f768SEric Dumazet 	       page_address(page) + *offset, *len);
17845640f768SEric Dumazet 	*offset = pfrag->offset;
17855640f768SEric Dumazet 	pfrag->offset += *len;
17864fb66994SJarek Poplawski 
17875640f768SEric Dumazet 	return pfrag->page;
17889c55e01cSJens Axboe }
17899c55e01cSJens Axboe 
179041c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd,
179141c73a0dSEric Dumazet 			     struct page *page,
179241c73a0dSEric Dumazet 			     unsigned int offset)
179341c73a0dSEric Dumazet {
179441c73a0dSEric Dumazet 	return	spd->nr_pages &&
179541c73a0dSEric Dumazet 		spd->pages[spd->nr_pages - 1] == page &&
179641c73a0dSEric Dumazet 		(spd->partial[spd->nr_pages - 1].offset +
179741c73a0dSEric Dumazet 		 spd->partial[spd->nr_pages - 1].len == offset);
179841c73a0dSEric Dumazet }
179941c73a0dSEric Dumazet 
18009c55e01cSJens Axboe /*
18019c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
18029c55e01cSJens Axboe  */
1803a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd,
180435f3d14dSJens Axboe 			  struct pipe_inode_info *pipe, struct page *page,
18054fb66994SJarek Poplawski 			  unsigned int *len, unsigned int offset,
180618aafc62SEric Dumazet 			  bool linear,
18077a67e56fSJarek Poplawski 			  struct sock *sk)
18089c55e01cSJens Axboe {
180941c73a0dSEric Dumazet 	if (unlikely(spd->nr_pages == MAX_SKB_FRAGS))
1810a108d5f3SDavid S. Miller 		return true;
18119c55e01cSJens Axboe 
18128b9d3728SJarek Poplawski 	if (linear) {
181318aafc62SEric Dumazet 		page = linear_to_page(page, len, &offset, sk);
18148b9d3728SJarek Poplawski 		if (!page)
1815a108d5f3SDavid S. Miller 			return true;
181641c73a0dSEric Dumazet 	}
181741c73a0dSEric Dumazet 	if (spd_can_coalesce(spd, page, offset)) {
181841c73a0dSEric Dumazet 		spd->partial[spd->nr_pages - 1].len += *len;
1819a108d5f3SDavid S. Miller 		return false;
182041c73a0dSEric Dumazet 	}
18218b9d3728SJarek Poplawski 	get_page(page);
18229c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
18234fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
18249c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
18259c55e01cSJens Axboe 	spd->nr_pages++;
18268b9d3728SJarek Poplawski 
1827a108d5f3SDavid S. Miller 	return false;
18289c55e01cSJens Axboe }
18299c55e01cSJens Axboe 
1830a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff,
18312870c43dSOctavian Purdila 			     unsigned int plen, unsigned int *off,
183218aafc62SEric Dumazet 			     unsigned int *len,
1833d7ccf7c0SEric Dumazet 			     struct splice_pipe_desc *spd, bool linear,
183435f3d14dSJens Axboe 			     struct sock *sk,
183535f3d14dSJens Axboe 			     struct pipe_inode_info *pipe)
18369c55e01cSJens Axboe {
18372870c43dSOctavian Purdila 	if (!*len)
1838a108d5f3SDavid S. Miller 		return true;
18399c55e01cSJens Axboe 
18402870c43dSOctavian Purdila 	/* skip this segment if already processed */
18412870c43dSOctavian Purdila 	if (*off >= plen) {
18422870c43dSOctavian Purdila 		*off -= plen;
1843a108d5f3SDavid S. Miller 		return false;
18442870c43dSOctavian Purdila 	}
18452870c43dSOctavian Purdila 
18462870c43dSOctavian Purdila 	/* ignore any bits we already processed */
18479ca1b22dSEric Dumazet 	poff += *off;
18489ca1b22dSEric Dumazet 	plen -= *off;
18492870c43dSOctavian Purdila 	*off = 0;
18502870c43dSOctavian Purdila 
185118aafc62SEric Dumazet 	do {
185218aafc62SEric Dumazet 		unsigned int flen = min(*len, plen);
18532870c43dSOctavian Purdila 
185418aafc62SEric Dumazet 		if (spd_fill_page(spd, pipe, page, &flen, poff,
185518aafc62SEric Dumazet 				  linear, sk))
1856a108d5f3SDavid S. Miller 			return true;
185718aafc62SEric Dumazet 		poff += flen;
185818aafc62SEric Dumazet 		plen -= flen;
18592870c43dSOctavian Purdila 		*len -= flen;
186018aafc62SEric Dumazet 	} while (*len && plen);
18612870c43dSOctavian Purdila 
1862a108d5f3SDavid S. Miller 	return false;
1863db43a282SOctavian Purdila }
18649c55e01cSJens Axboe 
18659c55e01cSJens Axboe /*
1866a108d5f3SDavid S. Miller  * Map linear and fragment data from the skb to spd. It reports true if the
18672870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
18689c55e01cSJens Axboe  */
1869a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
187035f3d14dSJens Axboe 			      unsigned int *offset, unsigned int *len,
187135f3d14dSJens Axboe 			      struct splice_pipe_desc *spd, struct sock *sk)
18722870c43dSOctavian Purdila {
18732870c43dSOctavian Purdila 	int seg;
18749c55e01cSJens Axboe 
18751d0c0b32SEric Dumazet 	/* map the linear part :
18762996d31fSAlexander Duyck 	 * If skb->head_frag is set, this 'linear' part is backed by a
18772996d31fSAlexander Duyck 	 * fragment, and if the head is not shared with any clones then
18782996d31fSAlexander Duyck 	 * we can avoid a copy since we own the head portion of this page.
18799c55e01cSJens Axboe 	 */
18802870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
18812870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
18822870c43dSOctavian Purdila 			     skb_headlen(skb),
188318aafc62SEric Dumazet 			     offset, len, spd,
18843a7c1ee4SAlexander Duyck 			     skb_head_is_locked(skb),
18851d0c0b32SEric Dumazet 			     sk, pipe))
1886a108d5f3SDavid S. Miller 		return true;
18879c55e01cSJens Axboe 
18889c55e01cSJens Axboe 	/*
18899c55e01cSJens Axboe 	 * then map the fragments
18909c55e01cSJens Axboe 	 */
18919c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
18929c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
18939c55e01cSJens Axboe 
1894ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
18959e903e08SEric Dumazet 				     f->page_offset, skb_frag_size(f),
189618aafc62SEric Dumazet 				     offset, len, spd, false, sk, pipe))
1897a108d5f3SDavid S. Miller 			return true;
18989c55e01cSJens Axboe 	}
18999c55e01cSJens Axboe 
1900a108d5f3SDavid S. Miller 	return false;
19019c55e01cSJens Axboe }
19029c55e01cSJens Axboe 
19039c55e01cSJens Axboe /*
19049c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
19059c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
19069c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
19079c55e01cSJens Axboe  * handle that cleanly.
19089c55e01cSJens Axboe  */
19098b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
19109c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
19119c55e01cSJens Axboe 		    unsigned int flags)
19129c55e01cSJens Axboe {
191341c73a0dSEric Dumazet 	struct partial_page partial[MAX_SKB_FRAGS];
191441c73a0dSEric Dumazet 	struct page *pages[MAX_SKB_FRAGS];
19159c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
19169c55e01cSJens Axboe 		.pages = pages,
19179c55e01cSJens Axboe 		.partial = partial,
1918047fe360SEric Dumazet 		.nr_pages_max = MAX_SKB_FRAGS,
19199c55e01cSJens Axboe 		.flags = flags,
192028a625cbSMiklos Szeredi 		.ops = &nosteal_pipe_buf_ops,
19219c55e01cSJens Axboe 		.spd_release = sock_spd_release,
19229c55e01cSJens Axboe 	};
1923fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
19247a67e56fSJarek Poplawski 	struct sock *sk = skb->sk;
192535f3d14dSJens Axboe 	int ret = 0;
192635f3d14dSJens Axboe 
19279c55e01cSJens Axboe 	/*
19289c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
19299c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
19309c55e01cSJens Axboe 	 */
193135f3d14dSJens Axboe 	if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
19329c55e01cSJens Axboe 		goto done;
19339c55e01cSJens Axboe 	else if (!tlen)
19349c55e01cSJens Axboe 		goto done;
19359c55e01cSJens Axboe 
19369c55e01cSJens Axboe 	/*
19379c55e01cSJens Axboe 	 * now see if we have a frag_list to map
19389c55e01cSJens Axboe 	 */
1939fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
1940fbb398a8SDavid S. Miller 		if (!tlen)
19419c55e01cSJens Axboe 			break;
194235f3d14dSJens Axboe 		if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1943fbb398a8SDavid S. Miller 			break;
19449c55e01cSJens Axboe 	}
19459c55e01cSJens Axboe 
19469c55e01cSJens Axboe done:
19479c55e01cSJens Axboe 	if (spd.nr_pages) {
19489c55e01cSJens Axboe 		/*
19499c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
19509c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
19519c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
19529c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
19539c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
19549c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
19559c55e01cSJens Axboe 		 * and networking will grab the socket lock.
19569c55e01cSJens Axboe 		 */
1957293ad604SOctavian Purdila 		release_sock(sk);
19589c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1959293ad604SOctavian Purdila 		lock_sock(sk);
19609c55e01cSJens Axboe 	}
19619c55e01cSJens Axboe 
196235f3d14dSJens Axboe 	return ret;
19639c55e01cSJens Axboe }
19649c55e01cSJens Axboe 
1965357b40a1SHerbert Xu /**
1966357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1967357b40a1SHerbert Xu  *	@skb: destination buffer
1968357b40a1SHerbert Xu  *	@offset: offset in destination
1969357b40a1SHerbert Xu  *	@from: source buffer
1970357b40a1SHerbert Xu  *	@len: number of bytes to copy
1971357b40a1SHerbert Xu  *
1972357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1973357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1974357b40a1SHerbert Xu  *	traversing fragment lists and such.
1975357b40a1SHerbert Xu  */
1976357b40a1SHerbert Xu 
19770c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1978357b40a1SHerbert Xu {
19791a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1980fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1981fbb398a8SDavid S. Miller 	int i, copy;
1982357b40a1SHerbert Xu 
1983357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1984357b40a1SHerbert Xu 		goto fault;
1985357b40a1SHerbert Xu 
19861a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1987357b40a1SHerbert Xu 		if (copy > len)
1988357b40a1SHerbert Xu 			copy = len;
198927d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1990357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1991357b40a1SHerbert Xu 			return 0;
1992357b40a1SHerbert Xu 		offset += copy;
1993357b40a1SHerbert Xu 		from += copy;
1994357b40a1SHerbert Xu 	}
1995357b40a1SHerbert Xu 
1996357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1997357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
19981a028e50SDavid S. Miller 		int end;
1999357b40a1SHerbert Xu 
2000547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20011a028e50SDavid S. Miller 
20029e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
2003357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2004357b40a1SHerbert Xu 			u8 *vaddr;
2005357b40a1SHerbert Xu 
2006357b40a1SHerbert Xu 			if (copy > len)
2007357b40a1SHerbert Xu 				copy = len;
2008357b40a1SHerbert Xu 
200951c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
20101a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
20111a028e50SDavid S. Miller 			       from, copy);
201251c56b00SEric Dumazet 			kunmap_atomic(vaddr);
2013357b40a1SHerbert Xu 
2014357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2015357b40a1SHerbert Xu 				return 0;
2016357b40a1SHerbert Xu 			offset += copy;
2017357b40a1SHerbert Xu 			from += copy;
2018357b40a1SHerbert Xu 		}
20191a028e50SDavid S. Miller 		start = end;
2020357b40a1SHerbert Xu 	}
2021357b40a1SHerbert Xu 
2022fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
20231a028e50SDavid S. Miller 		int end;
2024357b40a1SHerbert Xu 
2025547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20261a028e50SDavid S. Miller 
2027fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
2028357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
2029357b40a1SHerbert Xu 			if (copy > len)
2030357b40a1SHerbert Xu 				copy = len;
2031fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
20321a028e50SDavid S. Miller 					   from, copy))
2033357b40a1SHerbert Xu 				goto fault;
2034357b40a1SHerbert Xu 			if ((len -= copy) == 0)
2035357b40a1SHerbert Xu 				return 0;
2036357b40a1SHerbert Xu 			offset += copy;
2037357b40a1SHerbert Xu 			from += copy;
2038357b40a1SHerbert Xu 		}
20391a028e50SDavid S. Miller 		start = end;
2040357b40a1SHerbert Xu 	}
2041357b40a1SHerbert Xu 	if (!len)
2042357b40a1SHerbert Xu 		return 0;
2043357b40a1SHerbert Xu 
2044357b40a1SHerbert Xu fault:
2045357b40a1SHerbert Xu 	return -EFAULT;
2046357b40a1SHerbert Xu }
2047357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
2048357b40a1SHerbert Xu 
20491da177e4SLinus Torvalds /* Checksum skb data. */
20502817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
20512817a336SDaniel Borkmann 		      __wsum csum, const struct skb_checksum_ops *ops)
20521da177e4SLinus Torvalds {
20531a028e50SDavid S. Miller 	int start = skb_headlen(skb);
20541a028e50SDavid S. Miller 	int i, copy = start - offset;
2055fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
20561da177e4SLinus Torvalds 	int pos = 0;
20571da177e4SLinus Torvalds 
20581da177e4SLinus Torvalds 	/* Checksum header. */
20591da177e4SLinus Torvalds 	if (copy > 0) {
20601da177e4SLinus Torvalds 		if (copy > len)
20611da177e4SLinus Torvalds 			copy = len;
20622817a336SDaniel Borkmann 		csum = ops->update(skb->data + offset, copy, csum);
20631da177e4SLinus Torvalds 		if ((len -= copy) == 0)
20641da177e4SLinus Torvalds 			return csum;
20651da177e4SLinus Torvalds 		offset += copy;
20661da177e4SLinus Torvalds 		pos	= copy;
20671da177e4SLinus Torvalds 	}
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20701a028e50SDavid S. Miller 		int end;
207151c56b00SEric Dumazet 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
20721da177e4SLinus Torvalds 
2073547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20741a028e50SDavid S. Miller 
207551c56b00SEric Dumazet 		end = start + skb_frag_size(frag);
20761da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
207744bb9363SAl Viro 			__wsum csum2;
20781da177e4SLinus Torvalds 			u8 *vaddr;
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 			if (copy > len)
20811da177e4SLinus Torvalds 				copy = len;
208251c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
20832817a336SDaniel Borkmann 			csum2 = ops->update(vaddr + frag->page_offset +
20841a028e50SDavid S. Miller 					    offset - start, copy, 0);
208551c56b00SEric Dumazet 			kunmap_atomic(vaddr);
20862817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
20871da177e4SLinus Torvalds 			if (!(len -= copy))
20881da177e4SLinus Torvalds 				return csum;
20891da177e4SLinus Torvalds 			offset += copy;
20901da177e4SLinus Torvalds 			pos    += copy;
20911da177e4SLinus Torvalds 		}
20921a028e50SDavid S. Miller 		start = end;
20931da177e4SLinus Torvalds 	}
20941da177e4SLinus Torvalds 
2095fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
20961a028e50SDavid S. Miller 		int end;
20971da177e4SLinus Torvalds 
2098547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
20991a028e50SDavid S. Miller 
2100fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
21011da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21025f92a738SAl Viro 			__wsum csum2;
21031da177e4SLinus Torvalds 			if (copy > len)
21041da177e4SLinus Torvalds 				copy = len;
21052817a336SDaniel Borkmann 			csum2 = __skb_checksum(frag_iter, offset - start,
21062817a336SDaniel Borkmann 					       copy, 0, ops);
21072817a336SDaniel Borkmann 			csum = ops->combine(csum, csum2, pos, copy);
21081da177e4SLinus Torvalds 			if ((len -= copy) == 0)
21091da177e4SLinus Torvalds 				return csum;
21101da177e4SLinus Torvalds 			offset += copy;
21111da177e4SLinus Torvalds 			pos    += copy;
21121da177e4SLinus Torvalds 		}
21131a028e50SDavid S. Miller 		start = end;
21141da177e4SLinus Torvalds 	}
211509a62660SKris Katterjohn 	BUG_ON(len);
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds 	return csum;
21181da177e4SLinus Torvalds }
21192817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum);
21202817a336SDaniel Borkmann 
21212817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset,
21222817a336SDaniel Borkmann 		    int len, __wsum csum)
21232817a336SDaniel Borkmann {
21242817a336SDaniel Borkmann 	const struct skb_checksum_ops ops = {
2125cea80ea8SDaniel Borkmann 		.update  = csum_partial_ext,
21262817a336SDaniel Borkmann 		.combine = csum_block_add_ext,
21272817a336SDaniel Borkmann 	};
21282817a336SDaniel Borkmann 
21292817a336SDaniel Borkmann 	return __skb_checksum(skb, offset, len, csum, &ops);
21302817a336SDaniel Borkmann }
2131b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
21321da177e4SLinus Torvalds 
21331da177e4SLinus Torvalds /* Both of above in one bottle. */
21341da177e4SLinus Torvalds 
213581d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
213681d77662SAl Viro 				    u8 *to, int len, __wsum csum)
21371da177e4SLinus Torvalds {
21381a028e50SDavid S. Miller 	int start = skb_headlen(skb);
21391a028e50SDavid S. Miller 	int i, copy = start - offset;
2140fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
21411da177e4SLinus Torvalds 	int pos = 0;
21421da177e4SLinus Torvalds 
21431da177e4SLinus Torvalds 	/* Copy header. */
21441da177e4SLinus Torvalds 	if (copy > 0) {
21451da177e4SLinus Torvalds 		if (copy > len)
21461da177e4SLinus Torvalds 			copy = len;
21471da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
21481da177e4SLinus Torvalds 						 copy, csum);
21491da177e4SLinus Torvalds 		if ((len -= copy) == 0)
21501da177e4SLinus Torvalds 			return csum;
21511da177e4SLinus Torvalds 		offset += copy;
21521da177e4SLinus Torvalds 		to     += copy;
21531da177e4SLinus Torvalds 		pos	= copy;
21541da177e4SLinus Torvalds 	}
21551da177e4SLinus Torvalds 
21561da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
21571a028e50SDavid S. Miller 		int end;
21581da177e4SLinus Torvalds 
2159547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21601a028e50SDavid S. Miller 
21619e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
21621da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21635084205fSAl Viro 			__wsum csum2;
21641da177e4SLinus Torvalds 			u8 *vaddr;
21651da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
21661da177e4SLinus Torvalds 
21671da177e4SLinus Torvalds 			if (copy > len)
21681da177e4SLinus Torvalds 				copy = len;
216951c56b00SEric Dumazet 			vaddr = kmap_atomic(skb_frag_page(frag));
21701da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
21711a028e50SDavid S. Miller 							  frag->page_offset +
21721a028e50SDavid S. Miller 							  offset - start, to,
21731a028e50SDavid S. Miller 							  copy, 0);
217451c56b00SEric Dumazet 			kunmap_atomic(vaddr);
21751da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
21761da177e4SLinus Torvalds 			if (!(len -= copy))
21771da177e4SLinus Torvalds 				return csum;
21781da177e4SLinus Torvalds 			offset += copy;
21791da177e4SLinus Torvalds 			to     += copy;
21801da177e4SLinus Torvalds 			pos    += copy;
21811da177e4SLinus Torvalds 		}
21821a028e50SDavid S. Miller 		start = end;
21831da177e4SLinus Torvalds 	}
21841da177e4SLinus Torvalds 
2185fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
218681d77662SAl Viro 		__wsum csum2;
21871a028e50SDavid S. Miller 		int end;
21881da177e4SLinus Torvalds 
2189547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
21901a028e50SDavid S. Miller 
2191fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
21921da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
21931da177e4SLinus Torvalds 			if (copy > len)
21941da177e4SLinus Torvalds 				copy = len;
2195fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
21961a028e50SDavid S. Miller 						       offset - start,
21971da177e4SLinus Torvalds 						       to, copy, 0);
21981da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
21991da177e4SLinus Torvalds 			if ((len -= copy) == 0)
22001da177e4SLinus Torvalds 				return csum;
22011da177e4SLinus Torvalds 			offset += copy;
22021da177e4SLinus Torvalds 			to     += copy;
22031da177e4SLinus Torvalds 			pos    += copy;
22041da177e4SLinus Torvalds 		}
22051a028e50SDavid S. Miller 		start = end;
22061da177e4SLinus Torvalds 	}
220709a62660SKris Katterjohn 	BUG_ON(len);
22081da177e4SLinus Torvalds 	return csum;
22091da177e4SLinus Torvalds }
2210b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
22111da177e4SLinus Torvalds 
2212af2806f8SThomas Graf  /**
2213af2806f8SThomas Graf  *	skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy()
2214af2806f8SThomas Graf  *	@from: source buffer
2215af2806f8SThomas Graf  *
2216af2806f8SThomas Graf  *	Calculates the amount of linear headroom needed in the 'to' skb passed
2217af2806f8SThomas Graf  *	into skb_zerocopy().
2218af2806f8SThomas Graf  */
2219af2806f8SThomas Graf unsigned int
2220af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from)
2221af2806f8SThomas Graf {
2222af2806f8SThomas Graf 	unsigned int hlen = 0;
2223af2806f8SThomas Graf 
2224af2806f8SThomas Graf 	if (!from->head_frag ||
2225af2806f8SThomas Graf 	    skb_headlen(from) < L1_CACHE_BYTES ||
2226af2806f8SThomas Graf 	    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
2227af2806f8SThomas Graf 		hlen = skb_headlen(from);
2228af2806f8SThomas Graf 
2229af2806f8SThomas Graf 	if (skb_has_frag_list(from))
2230af2806f8SThomas Graf 		hlen = from->len;
2231af2806f8SThomas Graf 
2232af2806f8SThomas Graf 	return hlen;
2233af2806f8SThomas Graf }
2234af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen);
2235af2806f8SThomas Graf 
2236af2806f8SThomas Graf /**
2237af2806f8SThomas Graf  *	skb_zerocopy - Zero copy skb to skb
2238af2806f8SThomas Graf  *	@to: destination buffer
22397fceb4deSMasanari Iida  *	@from: source buffer
2240af2806f8SThomas Graf  *	@len: number of bytes to copy from source buffer
2241af2806f8SThomas Graf  *	@hlen: size of linear headroom in destination buffer
2242af2806f8SThomas Graf  *
2243af2806f8SThomas Graf  *	Copies up to `len` bytes from `from` to `to` by creating references
2244af2806f8SThomas Graf  *	to the frags in the source buffer.
2245af2806f8SThomas Graf  *
2246af2806f8SThomas Graf  *	The `hlen` as calculated by skb_zerocopy_headlen() specifies the
2247af2806f8SThomas Graf  *	headroom in the `to` buffer.
224836d5fe6aSZoltan Kiss  *
224936d5fe6aSZoltan Kiss  *	Return value:
225036d5fe6aSZoltan Kiss  *	0: everything is OK
225136d5fe6aSZoltan Kiss  *	-ENOMEM: couldn't orphan frags of @from due to lack of memory
225236d5fe6aSZoltan Kiss  *	-EFAULT: skb_copy_bits() found some problem with skb geometry
2253af2806f8SThomas Graf  */
225436d5fe6aSZoltan Kiss int
225536d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen)
2256af2806f8SThomas Graf {
2257af2806f8SThomas Graf 	int i, j = 0;
2258af2806f8SThomas Graf 	int plen = 0; /* length of skb->head fragment */
225936d5fe6aSZoltan Kiss 	int ret;
2260af2806f8SThomas Graf 	struct page *page;
2261af2806f8SThomas Graf 	unsigned int offset;
2262af2806f8SThomas Graf 
2263af2806f8SThomas Graf 	BUG_ON(!from->head_frag && !hlen);
2264af2806f8SThomas Graf 
2265af2806f8SThomas Graf 	/* dont bother with small payloads */
226636d5fe6aSZoltan Kiss 	if (len <= skb_tailroom(to))
226736d5fe6aSZoltan Kiss 		return skb_copy_bits(from, 0, skb_put(to, len), len);
2268af2806f8SThomas Graf 
2269af2806f8SThomas Graf 	if (hlen) {
227036d5fe6aSZoltan Kiss 		ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
227136d5fe6aSZoltan Kiss 		if (unlikely(ret))
227236d5fe6aSZoltan Kiss 			return ret;
2273af2806f8SThomas Graf 		len -= hlen;
2274af2806f8SThomas Graf 	} else {
2275af2806f8SThomas Graf 		plen = min_t(int, skb_headlen(from), len);
2276af2806f8SThomas Graf 		if (plen) {
2277af2806f8SThomas Graf 			page = virt_to_head_page(from->head);
2278af2806f8SThomas Graf 			offset = from->data - (unsigned char *)page_address(page);
2279af2806f8SThomas Graf 			__skb_fill_page_desc(to, 0, page, offset, plen);
2280af2806f8SThomas Graf 			get_page(page);
2281af2806f8SThomas Graf 			j = 1;
2282af2806f8SThomas Graf 			len -= plen;
2283af2806f8SThomas Graf 		}
2284af2806f8SThomas Graf 	}
2285af2806f8SThomas Graf 
2286af2806f8SThomas Graf 	to->truesize += len + plen;
2287af2806f8SThomas Graf 	to->len += len + plen;
2288af2806f8SThomas Graf 	to->data_len += len + plen;
2289af2806f8SThomas Graf 
229036d5fe6aSZoltan Kiss 	if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) {
229136d5fe6aSZoltan Kiss 		skb_tx_error(from);
229236d5fe6aSZoltan Kiss 		return -ENOMEM;
229336d5fe6aSZoltan Kiss 	}
229436d5fe6aSZoltan Kiss 
2295af2806f8SThomas Graf 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
2296af2806f8SThomas Graf 		if (!len)
2297af2806f8SThomas Graf 			break;
2298af2806f8SThomas Graf 		skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
2299af2806f8SThomas Graf 		skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
2300af2806f8SThomas Graf 		len -= skb_shinfo(to)->frags[j].size;
2301af2806f8SThomas Graf 		skb_frag_ref(to, j);
2302af2806f8SThomas Graf 		j++;
2303af2806f8SThomas Graf 	}
2304af2806f8SThomas Graf 	skb_shinfo(to)->nr_frags = j;
230536d5fe6aSZoltan Kiss 
230636d5fe6aSZoltan Kiss 	return 0;
2307af2806f8SThomas Graf }
2308af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy);
2309af2806f8SThomas Graf 
23101da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
23111da177e4SLinus Torvalds {
2312d3bc23e7SAl Viro 	__wsum csum;
23131da177e4SLinus Torvalds 	long csstart;
23141da177e4SLinus Torvalds 
231584fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
231655508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
23171da177e4SLinus Torvalds 	else
23181da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
23191da177e4SLinus Torvalds 
232009a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
23211da177e4SLinus Torvalds 
2322d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
23231da177e4SLinus Torvalds 
23241da177e4SLinus Torvalds 	csum = 0;
23251da177e4SLinus Torvalds 	if (csstart != skb->len)
23261da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
23271da177e4SLinus Torvalds 					      skb->len - csstart, 0);
23281da177e4SLinus Torvalds 
232984fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
2330ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
23311da177e4SLinus Torvalds 
2332d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
23331da177e4SLinus Torvalds 	}
23341da177e4SLinus Torvalds }
2335b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds /**
23381da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
23391da177e4SLinus Torvalds  *	@list: list to dequeue from
23401da177e4SLinus Torvalds  *
23411da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
23421da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
23431da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
23441da177e4SLinus Torvalds  */
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
23471da177e4SLinus Torvalds {
23481da177e4SLinus Torvalds 	unsigned long flags;
23491da177e4SLinus Torvalds 	struct sk_buff *result;
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23521da177e4SLinus Torvalds 	result = __skb_dequeue(list);
23531da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23541da177e4SLinus Torvalds 	return result;
23551da177e4SLinus Torvalds }
2356b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
23571da177e4SLinus Torvalds 
23581da177e4SLinus Torvalds /**
23591da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
23601da177e4SLinus Torvalds  *	@list: list to dequeue from
23611da177e4SLinus Torvalds  *
23621da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
23631da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
23641da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
23651da177e4SLinus Torvalds  */
23661da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
23671da177e4SLinus Torvalds {
23681da177e4SLinus Torvalds 	unsigned long flags;
23691da177e4SLinus Torvalds 	struct sk_buff *result;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
23721da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
23731da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
23741da177e4SLinus Torvalds 	return result;
23751da177e4SLinus Torvalds }
2376b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds /**
23791da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
23801da177e4SLinus Torvalds  *	@list: list to empty
23811da177e4SLinus Torvalds  *
23821da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
23831da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
23841da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
23851da177e4SLinus Torvalds  */
23861da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
23871da177e4SLinus Torvalds {
23881da177e4SLinus Torvalds 	struct sk_buff *skb;
23891da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
23901da177e4SLinus Torvalds 		kfree_skb(skb);
23911da177e4SLinus Torvalds }
2392b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds /**
23951da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
23961da177e4SLinus Torvalds  *	@list: list to use
23971da177e4SLinus Torvalds  *	@newsk: buffer to queue
23981da177e4SLinus Torvalds  *
23991da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
24001da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
24011da177e4SLinus Torvalds  *	safely.
24021da177e4SLinus Torvalds  *
24031da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24041da177e4SLinus Torvalds  */
24051da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
24061da177e4SLinus Torvalds {
24071da177e4SLinus Torvalds 	unsigned long flags;
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
24101da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
24111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
24121da177e4SLinus Torvalds }
2413b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
24141da177e4SLinus Torvalds 
24151da177e4SLinus Torvalds /**
24161da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
24171da177e4SLinus Torvalds  *	@list: list to use
24181da177e4SLinus Torvalds  *	@newsk: buffer to queue
24191da177e4SLinus Torvalds  *
24201da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
24211da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
24221da177e4SLinus Torvalds  *	safely.
24231da177e4SLinus Torvalds  *
24241da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24251da177e4SLinus Torvalds  */
24261da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
24271da177e4SLinus Torvalds {
24281da177e4SLinus Torvalds 	unsigned long flags;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
24311da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
24321da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
24331da177e4SLinus Torvalds }
2434b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
24358728b834SDavid S. Miller 
24361da177e4SLinus Torvalds /**
24371da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
24381da177e4SLinus Torvalds  *	@skb: buffer to remove
24398728b834SDavid S. Miller  *	@list: list to use
24401da177e4SLinus Torvalds  *
24418728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
24428728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
24431da177e4SLinus Torvalds  *
24448728b834SDavid S. Miller  *	You must know what list the SKB is on.
24451da177e4SLinus Torvalds  */
24468728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
24471da177e4SLinus Torvalds {
24481da177e4SLinus Torvalds 	unsigned long flags;
24491da177e4SLinus Torvalds 
24501da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
24518728b834SDavid S. Miller 	__skb_unlink(skb, list);
24521da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
24531da177e4SLinus Torvalds }
2454b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds /**
24571da177e4SLinus Torvalds  *	skb_append	-	append a buffer
24581da177e4SLinus Torvalds  *	@old: buffer to insert after
24591da177e4SLinus Torvalds  *	@newsk: buffer to insert
24608728b834SDavid S. Miller  *	@list: list to use
24611da177e4SLinus Torvalds  *
24621da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
24631da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
24641da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24651da177e4SLinus Torvalds  */
24668728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
24671da177e4SLinus Torvalds {
24681da177e4SLinus Torvalds 	unsigned long flags;
24691da177e4SLinus Torvalds 
24708728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
24717de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
24728728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
24731da177e4SLinus Torvalds }
2474b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds /**
24771da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
24781da177e4SLinus Torvalds  *	@old: buffer to insert before
24791da177e4SLinus Torvalds  *	@newsk: buffer to insert
24808728b834SDavid S. Miller  *	@list: list to use
24811da177e4SLinus Torvalds  *
24828728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
24838728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
24848728b834SDavid S. Miller  *	calls.
24858728b834SDavid S. Miller  *
24861da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
24871da177e4SLinus Torvalds  */
24888728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
24891da177e4SLinus Torvalds {
24901da177e4SLinus Torvalds 	unsigned long flags;
24911da177e4SLinus Torvalds 
24928728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
24938728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
24948728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
24951da177e4SLinus Torvalds }
2496b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
24991da177e4SLinus Torvalds 					   struct sk_buff* skb1,
25001da177e4SLinus Torvalds 					   const u32 len, const int pos)
25011da177e4SLinus Torvalds {
25021da177e4SLinus Torvalds 	int i;
25031da177e4SLinus Torvalds 
2504d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2505d626f62bSArnaldo Carvalho de Melo 					 pos - len);
25061da177e4SLinus Torvalds 	/* And move data appendix as is. */
25071da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
25081da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
25091da177e4SLinus Torvalds 
25101da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
25111da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
25121da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
25131da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
25141da177e4SLinus Torvalds 	skb->data_len		   = 0;
25151da177e4SLinus Torvalds 	skb->len		   = len;
251627a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
25171da177e4SLinus Torvalds }
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
25201da177e4SLinus Torvalds 				       struct sk_buff* skb1,
25211da177e4SLinus Torvalds 				       const u32 len, int pos)
25221da177e4SLinus Torvalds {
25231da177e4SLinus Torvalds 	int i, k = 0;
25241da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
25271da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
25281da177e4SLinus Torvalds 	skb->len		  = len;
25291da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
25301da177e4SLinus Torvalds 
25311da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
25329e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
25331da177e4SLinus Torvalds 
25341da177e4SLinus Torvalds 		if (pos + size > len) {
25351da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 			if (pos < len) {
25381da177e4SLinus Torvalds 				/* Split frag.
25391da177e4SLinus Torvalds 				 * We have two variants in this case:
25401da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
25411da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
25421da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
25431da177e4SLinus Torvalds 				 *    where splitting is expensive.
25441da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
25451da177e4SLinus Torvalds 				 */
2546ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
25471da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
25489e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
25499e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
25501da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
25511da177e4SLinus Torvalds 			}
25521da177e4SLinus Torvalds 			k++;
25531da177e4SLinus Torvalds 		} else
25541da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
25551da177e4SLinus Torvalds 		pos += size;
25561da177e4SLinus Torvalds 	}
25571da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds /**
25611da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
25621da177e4SLinus Torvalds  * @skb: the buffer to split
25631da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
25641da177e4SLinus Torvalds  * @len: new length for skb
25651da177e4SLinus Torvalds  */
25661da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
25671da177e4SLinus Torvalds {
25681da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
25691da177e4SLinus Torvalds 
257068534c68SAmerigo Wang 	skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
25711da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
25721da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
25731da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
25741da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
25751da177e4SLinus Torvalds }
2576b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
25771da177e4SLinus Torvalds 
25789f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
25799f782db3SIlpo Järvinen  *
25809f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
25819f782db3SIlpo Järvinen  */
2582832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2583832d11c5SIlpo Järvinen {
25840ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2585832d11c5SIlpo Järvinen }
2586832d11c5SIlpo Järvinen 
2587832d11c5SIlpo Järvinen /**
2588832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2589832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2590832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2591832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2592832d11c5SIlpo Järvinen  *
2593832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
259420e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2595832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2596832d11c5SIlpo Järvinen  *
2597832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2598832d11c5SIlpo Järvinen  *
2599832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2600832d11c5SIlpo Järvinen  * to have non-paged data as well.
2601832d11c5SIlpo Järvinen  *
2602832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2603832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2604832d11c5SIlpo Järvinen  */
2605832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2606832d11c5SIlpo Järvinen {
2607832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2608832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2609832d11c5SIlpo Järvinen 
2610832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2611832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2612832d11c5SIlpo Järvinen 
2613832d11c5SIlpo Järvinen 	todo = shiftlen;
2614832d11c5SIlpo Järvinen 	from = 0;
2615832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2616832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2617832d11c5SIlpo Järvinen 
2618832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2619832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2620832d11c5SIlpo Järvinen 	 */
2621832d11c5SIlpo Järvinen 	if (!to ||
2622ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2623ea2ab693SIan Campbell 			      fragfrom->page_offset)) {
2624832d11c5SIlpo Järvinen 		merge = -1;
2625832d11c5SIlpo Järvinen 	} else {
2626832d11c5SIlpo Järvinen 		merge = to - 1;
2627832d11c5SIlpo Järvinen 
26289e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
2629832d11c5SIlpo Järvinen 		if (todo < 0) {
2630832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2631832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2632832d11c5SIlpo Järvinen 				return 0;
2633832d11c5SIlpo Järvinen 
26349f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
26359f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2636832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2637832d11c5SIlpo Järvinen 
26389e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
26399e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
2640832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2641832d11c5SIlpo Järvinen 
2642832d11c5SIlpo Järvinen 			goto onlymerged;
2643832d11c5SIlpo Järvinen 		}
2644832d11c5SIlpo Järvinen 
2645832d11c5SIlpo Järvinen 		from++;
2646832d11c5SIlpo Järvinen 	}
2647832d11c5SIlpo Järvinen 
2648832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2649832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2650832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2651832d11c5SIlpo Järvinen 		return 0;
2652832d11c5SIlpo Järvinen 
2653832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2654832d11c5SIlpo Järvinen 		return 0;
2655832d11c5SIlpo Järvinen 
2656832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2657832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2658832d11c5SIlpo Järvinen 			return 0;
2659832d11c5SIlpo Järvinen 
2660832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2661832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2662832d11c5SIlpo Järvinen 
26639e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
2664832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
26659e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
2666832d11c5SIlpo Järvinen 			from++;
2667832d11c5SIlpo Järvinen 			to++;
2668832d11c5SIlpo Järvinen 
2669832d11c5SIlpo Järvinen 		} else {
2670ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
2671832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2672832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
26739e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
2674832d11c5SIlpo Järvinen 
2675832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
26769e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
2677832d11c5SIlpo Järvinen 			todo = 0;
2678832d11c5SIlpo Järvinen 
2679832d11c5SIlpo Järvinen 			to++;
2680832d11c5SIlpo Järvinen 			break;
2681832d11c5SIlpo Järvinen 		}
2682832d11c5SIlpo Järvinen 	}
2683832d11c5SIlpo Järvinen 
2684832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2685832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2686832d11c5SIlpo Järvinen 
2687832d11c5SIlpo Järvinen 	if (merge >= 0) {
2688832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2689832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2690832d11c5SIlpo Järvinen 
26919e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2692ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
2693832d11c5SIlpo Järvinen 	}
2694832d11c5SIlpo Järvinen 
2695832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2696832d11c5SIlpo Järvinen 	to = 0;
2697832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2698832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2699832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2700832d11c5SIlpo Järvinen 
2701832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2702832d11c5SIlpo Järvinen 
2703832d11c5SIlpo Järvinen onlymerged:
2704832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2705832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2706832d11c5SIlpo Järvinen 	 */
2707832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2708832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2709832d11c5SIlpo Järvinen 
2710832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2711832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2712832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2713832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2714832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2715832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2716832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2717832d11c5SIlpo Järvinen 
2718832d11c5SIlpo Järvinen 	return shiftlen;
2719832d11c5SIlpo Järvinen }
2720832d11c5SIlpo Järvinen 
2721677e90edSThomas Graf /**
2722677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2723677e90edSThomas Graf  * @skb: the buffer to read
2724677e90edSThomas Graf  * @from: lower offset of data to be read
2725677e90edSThomas Graf  * @to: upper offset of data to be read
2726677e90edSThomas Graf  * @st: state variable
2727677e90edSThomas Graf  *
2728677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2729677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2730677e90edSThomas Graf  */
2731677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2732677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2733677e90edSThomas Graf {
2734677e90edSThomas Graf 	st->lower_offset = from;
2735677e90edSThomas Graf 	st->upper_offset = to;
2736677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2737677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2738677e90edSThomas Graf 	st->frag_data = NULL;
2739677e90edSThomas Graf }
2740b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2741677e90edSThomas Graf 
2742677e90edSThomas Graf /**
2743677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2744677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2745677e90edSThomas Graf  * @data: destination pointer for data to be returned
2746677e90edSThomas Graf  * @st: state variable
2747677e90edSThomas Graf  *
2748bc32383cSMathias Krause  * Reads a block of skb data at @consumed relative to the
2749677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2750bc32383cSMathias Krause  * the head of the data block to @data and returns the length
2751677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2752677e90edSThomas Graf  * offset has been reached.
2753677e90edSThomas Graf  *
2754677e90edSThomas Graf  * The caller is not required to consume all of the data
2755bc32383cSMathias Krause  * returned, i.e. @consumed is typically set to the number
2756677e90edSThomas Graf  * of bytes already consumed and the next call to
2757677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2758677e90edSThomas Graf  *
275925985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
2760e793c0f7SMasanari Iida  *       this limitation is the cost for zerocopy sequential
2761677e90edSThomas Graf  *       reads of potentially non linear data.
2762677e90edSThomas Graf  *
2763bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2764677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2765677e90edSThomas Graf  *       a stack for this purpose.
2766677e90edSThomas Graf  */
2767677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2768677e90edSThomas Graf 			  struct skb_seq_state *st)
2769677e90edSThomas Graf {
2770677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2771677e90edSThomas Graf 	skb_frag_t *frag;
2772677e90edSThomas Graf 
2773aeb193eaSWedson Almeida Filho 	if (unlikely(abs_offset >= st->upper_offset)) {
2774aeb193eaSWedson Almeida Filho 		if (st->frag_data) {
2775aeb193eaSWedson Almeida Filho 			kunmap_atomic(st->frag_data);
2776aeb193eaSWedson Almeida Filho 			st->frag_data = NULL;
2777aeb193eaSWedson Almeida Filho 		}
2778677e90edSThomas Graf 		return 0;
2779aeb193eaSWedson Almeida Filho 	}
2780677e90edSThomas Graf 
2781677e90edSThomas Graf next_skb:
278295e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2783677e90edSThomas Graf 
2784995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
278595e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2786677e90edSThomas Graf 		return block_limit - abs_offset;
2787677e90edSThomas Graf 	}
2788677e90edSThomas Graf 
2789677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2790677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2791677e90edSThomas Graf 
2792677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2793677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
27949e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
2795677e90edSThomas Graf 
2796677e90edSThomas Graf 		if (abs_offset < block_limit) {
2797677e90edSThomas Graf 			if (!st->frag_data)
279851c56b00SEric Dumazet 				st->frag_data = kmap_atomic(skb_frag_page(frag));
2799677e90edSThomas Graf 
2800677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2801677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2802677e90edSThomas Graf 
2803677e90edSThomas Graf 			return block_limit - abs_offset;
2804677e90edSThomas Graf 		}
2805677e90edSThomas Graf 
2806677e90edSThomas Graf 		if (st->frag_data) {
280751c56b00SEric Dumazet 			kunmap_atomic(st->frag_data);
2808677e90edSThomas Graf 			st->frag_data = NULL;
2809677e90edSThomas Graf 		}
2810677e90edSThomas Graf 
2811677e90edSThomas Graf 		st->frag_idx++;
28129e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
2813677e90edSThomas Graf 	}
2814677e90edSThomas Graf 
28155b5a60daSOlaf Kirch 	if (st->frag_data) {
281651c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
28175b5a60daSOlaf Kirch 		st->frag_data = NULL;
28185b5a60daSOlaf Kirch 	}
28195b5a60daSOlaf Kirch 
282021dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2821677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
282295e3b24cSHerbert Xu 		st->frag_idx = 0;
2823677e90edSThomas Graf 		goto next_skb;
282471b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
282571b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
282671b3346dSShyam Iyer 		st->frag_idx = 0;
2827677e90edSThomas Graf 		goto next_skb;
2828677e90edSThomas Graf 	}
2829677e90edSThomas Graf 
2830677e90edSThomas Graf 	return 0;
2831677e90edSThomas Graf }
2832b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2833677e90edSThomas Graf 
2834677e90edSThomas Graf /**
2835677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2836677e90edSThomas Graf  * @st: state variable
2837677e90edSThomas Graf  *
2838677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2839677e90edSThomas Graf  * returned 0.
2840677e90edSThomas Graf  */
2841677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2842677e90edSThomas Graf {
2843677e90edSThomas Graf 	if (st->frag_data)
284451c56b00SEric Dumazet 		kunmap_atomic(st->frag_data);
2845677e90edSThomas Graf }
2846b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2847677e90edSThomas Graf 
28483fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
28493fc7e8a6SThomas Graf 
28503fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
28513fc7e8a6SThomas Graf 					  struct ts_config *conf,
28523fc7e8a6SThomas Graf 					  struct ts_state *state)
28533fc7e8a6SThomas Graf {
28543fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
28553fc7e8a6SThomas Graf }
28563fc7e8a6SThomas Graf 
28573fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
28583fc7e8a6SThomas Graf {
28593fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
28603fc7e8a6SThomas Graf }
28613fc7e8a6SThomas Graf 
28623fc7e8a6SThomas Graf /**
28633fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
28643fc7e8a6SThomas Graf  * @skb: the buffer to look in
28653fc7e8a6SThomas Graf  * @from: search offset
28663fc7e8a6SThomas Graf  * @to: search limit
28673fc7e8a6SThomas Graf  * @config: textsearch configuration
28683fc7e8a6SThomas Graf  *
28693fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
28703fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
28713fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
28723fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
28733fc7e8a6SThomas Graf  */
28743fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2875059a2440SBojan Prtvar 			   unsigned int to, struct ts_config *config)
28763fc7e8a6SThomas Graf {
2877059a2440SBojan Prtvar 	struct ts_state state;
2878f72b948dSPhil Oester 	unsigned int ret;
2879f72b948dSPhil Oester 
28803fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
28813fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
28823fc7e8a6SThomas Graf 
2883059a2440SBojan Prtvar 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
28843fc7e8a6SThomas Graf 
2885059a2440SBojan Prtvar 	ret = textsearch_find(config, &state);
2886f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
28873fc7e8a6SThomas Graf }
2888b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
28893fc7e8a6SThomas Graf 
2890e89e9cf5SAnanda Raju /**
28912c53040fSBen Hutchings  * skb_append_datato_frags - append the user data to a skb
2892e89e9cf5SAnanda Raju  * @sk: sock  structure
2893e793c0f7SMasanari Iida  * @skb: skb structure to be appended with user data.
2894e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2895e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2896e89e9cf5SAnanda Raju  * @length: length of the iov message
2897e89e9cf5SAnanda Raju  *
2898e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2899e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2900e89e9cf5SAnanda Raju  */
2901e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2902dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2903e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2904e89e9cf5SAnanda Raju 			void *from, int length)
2905e89e9cf5SAnanda Raju {
2906b2111724SEric Dumazet 	int frg_cnt = skb_shinfo(skb)->nr_frags;
2907b2111724SEric Dumazet 	int copy;
2908e89e9cf5SAnanda Raju 	int offset = 0;
2909e89e9cf5SAnanda Raju 	int ret;
2910b2111724SEric Dumazet 	struct page_frag *pfrag = &current->task_frag;
2911e89e9cf5SAnanda Raju 
2912e89e9cf5SAnanda Raju 	do {
2913e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2914e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2915b2111724SEric Dumazet 			return -EMSGSIZE;
2916e89e9cf5SAnanda Raju 
2917b2111724SEric Dumazet 		if (!sk_page_frag_refill(sk, pfrag))
2918e89e9cf5SAnanda Raju 			return -ENOMEM;
2919e89e9cf5SAnanda Raju 
2920e89e9cf5SAnanda Raju 		/* copy the user data to page */
2921b2111724SEric Dumazet 		copy = min_t(int, length, pfrag->size - pfrag->offset);
2922e89e9cf5SAnanda Raju 
2923b2111724SEric Dumazet 		ret = getfrag(from, page_address(pfrag->page) + pfrag->offset,
2924e89e9cf5SAnanda Raju 			      offset, copy, 0, skb);
2925e89e9cf5SAnanda Raju 		if (ret < 0)
2926e89e9cf5SAnanda Raju 			return -EFAULT;
2927e89e9cf5SAnanda Raju 
2928e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2929b2111724SEric Dumazet 		skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset,
2930b2111724SEric Dumazet 				   copy);
2931b2111724SEric Dumazet 		frg_cnt++;
2932b2111724SEric Dumazet 		pfrag->offset += copy;
2933b2111724SEric Dumazet 		get_page(pfrag->page);
2934b2111724SEric Dumazet 
2935b2111724SEric Dumazet 		skb->truesize += copy;
2936b2111724SEric Dumazet 		atomic_add(copy, &sk->sk_wmem_alloc);
2937e89e9cf5SAnanda Raju 		skb->len += copy;
2938e89e9cf5SAnanda Raju 		skb->data_len += copy;
2939e89e9cf5SAnanda Raju 		offset += copy;
2940e89e9cf5SAnanda Raju 		length -= copy;
2941e89e9cf5SAnanda Raju 
2942e89e9cf5SAnanda Raju 	} while (length > 0);
2943e89e9cf5SAnanda Raju 
2944e89e9cf5SAnanda Raju 	return 0;
2945e89e9cf5SAnanda Raju }
2946b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
2947e89e9cf5SAnanda Raju 
2948cbb042f9SHerbert Xu /**
2949cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2950cbb042f9SHerbert Xu  *	@skb: buffer to update
2951cbb042f9SHerbert Xu  *	@len: length of data pulled
2952cbb042f9SHerbert Xu  *
2953cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2954fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
295584fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
295684fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
295784fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2958cbb042f9SHerbert Xu  */
2959cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2960cbb042f9SHerbert Xu {
2961cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2962cbb042f9SHerbert Xu 	skb->len -= len;
2963cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2964cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2965cbb042f9SHerbert Xu 	return skb->data += len;
2966cbb042f9SHerbert Xu }
2967f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2968f94691acSArnaldo Carvalho de Melo 
2969f4c50d99SHerbert Xu /**
2970f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2971df5771ffSMichael S. Tsirkin  *	@head_skb: buffer to segment
2972576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2973f4c50d99SHerbert Xu  *
2974f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
29754c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
29764c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2977f4c50d99SHerbert Xu  */
2978df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb,
2979df5771ffSMichael S. Tsirkin 			    netdev_features_t features)
2980f4c50d99SHerbert Xu {
2981f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2982f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
29831a4cedafSMichael S. Tsirkin 	struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list;
2984df5771ffSMichael S. Tsirkin 	skb_frag_t *frag = skb_shinfo(head_skb)->frags;
2985df5771ffSMichael S. Tsirkin 	unsigned int mss = skb_shinfo(head_skb)->gso_size;
2986df5771ffSMichael S. Tsirkin 	unsigned int doffset = head_skb->data - skb_mac_header(head_skb);
29871fd819ecSMichael S. Tsirkin 	struct sk_buff *frag_skb = head_skb;
2988f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2989df5771ffSMichael S. Tsirkin 	unsigned int tnl_hlen = skb_tnl_header_len(head_skb);
2990f4c50d99SHerbert Xu 	unsigned int headroom;
2991f4c50d99SHerbert Xu 	unsigned int len;
2992ec5f0615SPravin B Shelar 	__be16 proto;
2993ec5f0615SPravin B Shelar 	bool csum;
299404ed3e74SMichał Mirosław 	int sg = !!(features & NETIF_F_SG);
2995df5771ffSMichael S. Tsirkin 	int nfrags = skb_shinfo(head_skb)->nr_frags;
2996f4c50d99SHerbert Xu 	int err = -ENOMEM;
2997f4c50d99SHerbert Xu 	int i = 0;
2998f4c50d99SHerbert Xu 	int pos;
299953d6471cSVlad Yasevich 	int dummy;
3000f4c50d99SHerbert Xu 
30015882a07cSWei-Chun Chao 	__skb_push(head_skb, doffset);
300253d6471cSVlad Yasevich 	proto = skb_network_protocol(head_skb, &dummy);
3003ec5f0615SPravin B Shelar 	if (unlikely(!proto))
3004ec5f0615SPravin B Shelar 		return ERR_PTR(-EINVAL);
3005ec5f0615SPravin B Shelar 
30067e2b10c1STom Herbert 	csum = !head_skb->encap_hdr_csum &&
30077e2b10c1STom Herbert 	    !!can_checksum_protocol(features, proto);
30087e2b10c1STom Herbert 
3009df5771ffSMichael S. Tsirkin 	headroom = skb_headroom(head_skb);
3010df5771ffSMichael S. Tsirkin 	pos = skb_headlen(head_skb);
3011f4c50d99SHerbert Xu 
3012f4c50d99SHerbert Xu 	do {
3013f4c50d99SHerbert Xu 		struct sk_buff *nskb;
30148cb19905SMichael S. Tsirkin 		skb_frag_t *nskb_frag;
3015c8884eddSHerbert Xu 		int hsize;
3016f4c50d99SHerbert Xu 		int size;
3017f4c50d99SHerbert Xu 
3018df5771ffSMichael S. Tsirkin 		len = head_skb->len - offset;
3019f4c50d99SHerbert Xu 		if (len > mss)
3020f4c50d99SHerbert Xu 			len = mss;
3021f4c50d99SHerbert Xu 
3022df5771ffSMichael S. Tsirkin 		hsize = skb_headlen(head_skb) - offset;
3023f4c50d99SHerbert Xu 		if (hsize < 0)
3024f4c50d99SHerbert Xu 			hsize = 0;
3025c8884eddSHerbert Xu 		if (hsize > len || !sg)
3026c8884eddSHerbert Xu 			hsize = len;
3027f4c50d99SHerbert Xu 
30281a4cedafSMichael S. Tsirkin 		if (!hsize && i >= nfrags && skb_headlen(list_skb) &&
30291a4cedafSMichael S. Tsirkin 		    (skb_headlen(list_skb) == len || sg)) {
30301a4cedafSMichael S. Tsirkin 			BUG_ON(skb_headlen(list_skb) > len);
303189319d38SHerbert Xu 
30329d8506ccSHerbert Xu 			i = 0;
30331a4cedafSMichael S. Tsirkin 			nfrags = skb_shinfo(list_skb)->nr_frags;
30341a4cedafSMichael S. Tsirkin 			frag = skb_shinfo(list_skb)->frags;
30351fd819ecSMichael S. Tsirkin 			frag_skb = list_skb;
30361a4cedafSMichael S. Tsirkin 			pos += skb_headlen(list_skb);
30379d8506ccSHerbert Xu 
30389d8506ccSHerbert Xu 			while (pos < offset + len) {
30399d8506ccSHerbert Xu 				BUG_ON(i >= nfrags);
30409d8506ccSHerbert Xu 
30414e1beba1SMichael S. Tsirkin 				size = skb_frag_size(frag);
30429d8506ccSHerbert Xu 				if (pos + size > offset + len)
30439d8506ccSHerbert Xu 					break;
30449d8506ccSHerbert Xu 
30459d8506ccSHerbert Xu 				i++;
30469d8506ccSHerbert Xu 				pos += size;
30474e1beba1SMichael S. Tsirkin 				frag++;
30489d8506ccSHerbert Xu 			}
30499d8506ccSHerbert Xu 
30501a4cedafSMichael S. Tsirkin 			nskb = skb_clone(list_skb, GFP_ATOMIC);
30511a4cedafSMichael S. Tsirkin 			list_skb = list_skb->next;
305289319d38SHerbert Xu 
3053f4c50d99SHerbert Xu 			if (unlikely(!nskb))
3054f4c50d99SHerbert Xu 				goto err;
3055f4c50d99SHerbert Xu 
30569d8506ccSHerbert Xu 			if (unlikely(pskb_trim(nskb, len))) {
30579d8506ccSHerbert Xu 				kfree_skb(nskb);
30589d8506ccSHerbert Xu 				goto err;
30599d8506ccSHerbert Xu 			}
30609d8506ccSHerbert Xu 
3061ec47ea82SAlexander Duyck 			hsize = skb_end_offset(nskb);
306289319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
306389319d38SHerbert Xu 				kfree_skb(nskb);
306489319d38SHerbert Xu 				goto err;
306589319d38SHerbert Xu 			}
306689319d38SHerbert Xu 
3067ec47ea82SAlexander Duyck 			nskb->truesize += skb_end_offset(nskb) - hsize;
306889319d38SHerbert Xu 			skb_release_head_state(nskb);
306989319d38SHerbert Xu 			__skb_push(nskb, doffset);
307089319d38SHerbert Xu 		} else {
3071c93bdd0eSMel Gorman 			nskb = __alloc_skb(hsize + doffset + headroom,
3072df5771ffSMichael S. Tsirkin 					   GFP_ATOMIC, skb_alloc_rx_flag(head_skb),
3073c93bdd0eSMel Gorman 					   NUMA_NO_NODE);
307489319d38SHerbert Xu 
307589319d38SHerbert Xu 			if (unlikely(!nskb))
307689319d38SHerbert Xu 				goto err;
307789319d38SHerbert Xu 
307889319d38SHerbert Xu 			skb_reserve(nskb, headroom);
307989319d38SHerbert Xu 			__skb_put(nskb, doffset);
308089319d38SHerbert Xu 		}
308189319d38SHerbert Xu 
3082f4c50d99SHerbert Xu 		if (segs)
3083f4c50d99SHerbert Xu 			tail->next = nskb;
3084f4c50d99SHerbert Xu 		else
3085f4c50d99SHerbert Xu 			segs = nskb;
3086f4c50d99SHerbert Xu 		tail = nskb;
3087f4c50d99SHerbert Xu 
3088df5771ffSMichael S. Tsirkin 		__copy_skb_header(nskb, head_skb);
3089f4c50d99SHerbert Xu 
3090030737bcSEric Dumazet 		skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
3091fcdfe3a7SVlad Yasevich 		skb_reset_mac_len(nskb);
309268c33163SPravin B Shelar 
3093df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, -tnl_hlen,
309468c33163SPravin B Shelar 						 nskb->data - tnl_hlen,
309568c33163SPravin B Shelar 						 doffset + tnl_hlen);
309689319d38SHerbert Xu 
30979d8506ccSHerbert Xu 		if (nskb->len == len + doffset)
30981cdbcb79SSimon Horman 			goto perform_csum_check;
309989319d38SHerbert Xu 
3100e585f236STom Herbert 		if (!sg && !nskb->remcsum_offload) {
31016f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
3102df5771ffSMichael S. Tsirkin 			nskb->csum = skb_copy_and_csum_bits(head_skb, offset,
3103f4c50d99SHerbert Xu 							    skb_put(nskb, len),
3104f4c50d99SHerbert Xu 							    len, 0);
31057e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
3106de843723STom Herbert 			    skb_headroom(nskb) + doffset;
3107f4c50d99SHerbert Xu 			continue;
3108f4c50d99SHerbert Xu 		}
3109f4c50d99SHerbert Xu 
31108cb19905SMichael S. Tsirkin 		nskb_frag = skb_shinfo(nskb)->frags;
3111f4c50d99SHerbert Xu 
3112df5771ffSMichael S. Tsirkin 		skb_copy_from_linear_data_offset(head_skb, offset,
3113d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
3114f4c50d99SHerbert Xu 
3115df5771ffSMichael S. Tsirkin 		skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags &
3116df5771ffSMichael S. Tsirkin 			SKBTX_SHARED_FRAG;
3117cef401deSEric Dumazet 
31189d8506ccSHerbert Xu 		while (pos < offset + len) {
31199d8506ccSHerbert Xu 			if (i >= nfrags) {
31201a4cedafSMichael S. Tsirkin 				BUG_ON(skb_headlen(list_skb));
31219d8506ccSHerbert Xu 
31229d8506ccSHerbert Xu 				i = 0;
31231a4cedafSMichael S. Tsirkin 				nfrags = skb_shinfo(list_skb)->nr_frags;
31241a4cedafSMichael S. Tsirkin 				frag = skb_shinfo(list_skb)->frags;
31251fd819ecSMichael S. Tsirkin 				frag_skb = list_skb;
31269d8506ccSHerbert Xu 
31279d8506ccSHerbert Xu 				BUG_ON(!nfrags);
31289d8506ccSHerbert Xu 
31291a4cedafSMichael S. Tsirkin 				list_skb = list_skb->next;
31309d8506ccSHerbert Xu 			}
31319d8506ccSHerbert Xu 
31329d8506ccSHerbert Xu 			if (unlikely(skb_shinfo(nskb)->nr_frags >=
31339d8506ccSHerbert Xu 				     MAX_SKB_FRAGS)) {
31349d8506ccSHerbert Xu 				net_warn_ratelimited(
31359d8506ccSHerbert Xu 					"skb_segment: too many frags: %u %u\n",
31369d8506ccSHerbert Xu 					pos, mss);
31379d8506ccSHerbert Xu 				goto err;
31389d8506ccSHerbert Xu 			}
31399d8506ccSHerbert Xu 
31401fd819ecSMichael S. Tsirkin 			if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC)))
31411fd819ecSMichael S. Tsirkin 				goto err;
31421fd819ecSMichael S. Tsirkin 
31434e1beba1SMichael S. Tsirkin 			*nskb_frag = *frag;
31448cb19905SMichael S. Tsirkin 			__skb_frag_ref(nskb_frag);
31458cb19905SMichael S. Tsirkin 			size = skb_frag_size(nskb_frag);
3146f4c50d99SHerbert Xu 
3147f4c50d99SHerbert Xu 			if (pos < offset) {
31488cb19905SMichael S. Tsirkin 				nskb_frag->page_offset += offset - pos;
31498cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, offset - pos);
3150f4c50d99SHerbert Xu 			}
3151f4c50d99SHerbert Xu 
315289319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
3153f4c50d99SHerbert Xu 
3154f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
3155f4c50d99SHerbert Xu 				i++;
31564e1beba1SMichael S. Tsirkin 				frag++;
3157f4c50d99SHerbert Xu 				pos += size;
3158f4c50d99SHerbert Xu 			} else {
31598cb19905SMichael S. Tsirkin 				skb_frag_size_sub(nskb_frag, pos + size - (offset + len));
316089319d38SHerbert Xu 				goto skip_fraglist;
3161f4c50d99SHerbert Xu 			}
3162f4c50d99SHerbert Xu 
31638cb19905SMichael S. Tsirkin 			nskb_frag++;
3164f4c50d99SHerbert Xu 		}
3165f4c50d99SHerbert Xu 
316689319d38SHerbert Xu skip_fraglist:
3167f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
3168f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
3169f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
3170ec5f0615SPravin B Shelar 
31711cdbcb79SSimon Horman perform_csum_check:
3172e585f236STom Herbert 		if (!csum && !nskb->remcsum_offload) {
3173ec5f0615SPravin B Shelar 			nskb->csum = skb_checksum(nskb, doffset,
3174ec5f0615SPravin B Shelar 						  nskb->len - doffset, 0);
3175ec5f0615SPravin B Shelar 			nskb->ip_summed = CHECKSUM_NONE;
31767e2b10c1STom Herbert 			SKB_GSO_CB(nskb)->csum_start =
31777e2b10c1STom Herbert 			    skb_headroom(nskb) + doffset;
3178ec5f0615SPravin B Shelar 		}
3179df5771ffSMichael S. Tsirkin 	} while ((offset += len) < head_skb->len);
3180f4c50d99SHerbert Xu 
3181bec3cfdcSEric Dumazet 	/* Some callers want to get the end of the list.
3182bec3cfdcSEric Dumazet 	 * Put it in segs->prev to avoid walking the list.
3183bec3cfdcSEric Dumazet 	 * (see validate_xmit_skb_list() for example)
3184bec3cfdcSEric Dumazet 	 */
3185bec3cfdcSEric Dumazet 	segs->prev = tail;
3186432c856fSToshiaki Makita 
3187432c856fSToshiaki Makita 	/* Following permits correct backpressure, for protocols
3188432c856fSToshiaki Makita 	 * using skb_set_owner_w().
3189432c856fSToshiaki Makita 	 * Idea is to tranfert ownership from head_skb to last segment.
3190432c856fSToshiaki Makita 	 */
3191432c856fSToshiaki Makita 	if (head_skb->destructor == sock_wfree) {
3192432c856fSToshiaki Makita 		swap(tail->truesize, head_skb->truesize);
3193432c856fSToshiaki Makita 		swap(tail->destructor, head_skb->destructor);
3194432c856fSToshiaki Makita 		swap(tail->sk, head_skb->sk);
3195432c856fSToshiaki Makita 	}
3196f4c50d99SHerbert Xu 	return segs;
3197f4c50d99SHerbert Xu 
3198f4c50d99SHerbert Xu err:
3199289dccbeSEric Dumazet 	kfree_skb_list(segs);
3200f4c50d99SHerbert Xu 	return ERR_PTR(err);
3201f4c50d99SHerbert Xu }
3202f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
3203f4c50d99SHerbert Xu 
320471d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
320571d93b39SHerbert Xu {
32068a29111cSEric Dumazet 	struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
320767147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
320867147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
32098a29111cSEric Dumazet 	unsigned int len = skb_gro_len(skb);
321058025e46SEric Dumazet 	struct sk_buff *lp, *p = *head;
3211715dc1f3SEric Dumazet 	unsigned int delta_truesize;
321271d93b39SHerbert Xu 
32138a29111cSEric Dumazet 	if (unlikely(p->len + len >= 65536))
321471d93b39SHerbert Xu 		return -E2BIG;
321571d93b39SHerbert Xu 
321629e98242SEric Dumazet 	lp = NAPI_GRO_CB(p)->last;
32178a29111cSEric Dumazet 	pinfo = skb_shinfo(lp);
32188a29111cSEric Dumazet 
32198a29111cSEric Dumazet 	if (headlen <= offset) {
322042da6994SHerbert Xu 		skb_frag_t *frag;
322166e92fcfSHerbert Xu 		skb_frag_t *frag2;
32229aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
32239aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
322442da6994SHerbert Xu 
322566e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
32268a29111cSEric Dumazet 			goto merge;
322781705ad1SHerbert Xu 
32288a29111cSEric Dumazet 		offset -= headlen;
32299aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
32309aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
3231f5572068SHerbert Xu 
32329aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
32339aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
323466e92fcfSHerbert Xu 		do {
323566e92fcfSHerbert Xu 			*--frag = *--frag2;
323666e92fcfSHerbert Xu 		} while (--i);
323766e92fcfSHerbert Xu 
323866e92fcfSHerbert Xu 		frag->page_offset += offset;
32399e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
324066e92fcfSHerbert Xu 
3241715dc1f3SEric Dumazet 		/* all fragments truesize : remove (head size + sk_buff) */
3242ec47ea82SAlexander Duyck 		delta_truesize = skb->truesize -
3243ec47ea82SAlexander Duyck 				 SKB_TRUESIZE(skb_end_offset(skb));
3244715dc1f3SEric Dumazet 
3245f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
3246f5572068SHerbert Xu 		skb->len -= skb->data_len;
3247f5572068SHerbert Xu 		skb->data_len = 0;
3248f5572068SHerbert Xu 
3249715dc1f3SEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE;
32505d38a079SHerbert Xu 		goto done;
3251d7e8883cSEric Dumazet 	} else if (skb->head_frag) {
3252d7e8883cSEric Dumazet 		int nr_frags = pinfo->nr_frags;
3253d7e8883cSEric Dumazet 		skb_frag_t *frag = pinfo->frags + nr_frags;
3254d7e8883cSEric Dumazet 		struct page *page = virt_to_head_page(skb->head);
3255d7e8883cSEric Dumazet 		unsigned int first_size = headlen - offset;
3256d7e8883cSEric Dumazet 		unsigned int first_offset;
3257d7e8883cSEric Dumazet 
3258d7e8883cSEric Dumazet 		if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
32598a29111cSEric Dumazet 			goto merge;
3260d7e8883cSEric Dumazet 
3261d7e8883cSEric Dumazet 		first_offset = skb->data -
3262d7e8883cSEric Dumazet 			       (unsigned char *)page_address(page) +
3263d7e8883cSEric Dumazet 			       offset;
3264d7e8883cSEric Dumazet 
3265d7e8883cSEric Dumazet 		pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags;
3266d7e8883cSEric Dumazet 
3267d7e8883cSEric Dumazet 		frag->page.p	  = page;
3268d7e8883cSEric Dumazet 		frag->page_offset = first_offset;
3269d7e8883cSEric Dumazet 		skb_frag_size_set(frag, first_size);
3270d7e8883cSEric Dumazet 
3271d7e8883cSEric Dumazet 		memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags);
3272d7e8883cSEric Dumazet 		/* We dont need to clear skbinfo->nr_frags here */
3273d7e8883cSEric Dumazet 
3274715dc1f3SEric Dumazet 		delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3275d7e8883cSEric Dumazet 		NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3276d7e8883cSEric Dumazet 		goto done;
32778a29111cSEric Dumazet 	}
327871d93b39SHerbert Xu 
327971d93b39SHerbert Xu merge:
3280715dc1f3SEric Dumazet 	delta_truesize = skb->truesize;
328167147ba9SHerbert Xu 	if (offset > headlen) {
3282d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
3283d1dc7abfSMichal Schmidt 
3284d1dc7abfSMichal Schmidt 		skbinfo->frags[0].page_offset += eat;
32859e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
3286d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
3287d1dc7abfSMichal Schmidt 		skb->len -= eat;
328867147ba9SHerbert Xu 		offset = headlen;
328956035022SHerbert Xu 	}
329056035022SHerbert Xu 
329167147ba9SHerbert Xu 	__skb_pull(skb, offset);
329256035022SHerbert Xu 
329329e98242SEric Dumazet 	if (NAPI_GRO_CB(p)->last == p)
32948a29111cSEric Dumazet 		skb_shinfo(p)->frag_list = skb;
32958a29111cSEric Dumazet 	else
3296c3c7c254SEric Dumazet 		NAPI_GRO_CB(p)->last->next = skb;
3297c3c7c254SEric Dumazet 	NAPI_GRO_CB(p)->last = skb;
3298f4a775d1SEric Dumazet 	__skb_header_release(skb);
32998a29111cSEric Dumazet 	lp = p;
330071d93b39SHerbert Xu 
33015d38a079SHerbert Xu done:
33025d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
330337fe4732SHerbert Xu 	p->data_len += len;
3304715dc1f3SEric Dumazet 	p->truesize += delta_truesize;
330537fe4732SHerbert Xu 	p->len += len;
33068a29111cSEric Dumazet 	if (lp != p) {
33078a29111cSEric Dumazet 		lp->data_len += len;
33088a29111cSEric Dumazet 		lp->truesize += delta_truesize;
33098a29111cSEric Dumazet 		lp->len += len;
33108a29111cSEric Dumazet 	}
331171d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
331271d93b39SHerbert Xu 	return 0;
331371d93b39SHerbert Xu }
331471d93b39SHerbert Xu 
33151da177e4SLinus Torvalds void __init skb_init(void)
33161da177e4SLinus Torvalds {
33171da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
33181da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
33191da177e4SLinus Torvalds 					      0,
3320e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
332120c2df83SPaul Mundt 					      NULL);
3322d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3323d0bf4a9eSEric Dumazet 						sizeof(struct sk_buff_fclones),
3324d179cd12SDavid S. Miller 						0,
3325e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
332620c2df83SPaul Mundt 						NULL);
33271da177e4SLinus Torvalds }
33281da177e4SLinus Torvalds 
3329716ea3a7SDavid Howells /**
3330716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
3331716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
3332716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
3333716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
3334716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
3335716ea3a7SDavid Howells  *
3336716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
3337716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
3338716ea3a7SDavid Howells  */
333951c739d1SDavid S. Miller static int
334051c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
3341716ea3a7SDavid Howells {
33421a028e50SDavid S. Miller 	int start = skb_headlen(skb);
33431a028e50SDavid S. Miller 	int i, copy = start - offset;
3344fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
3345716ea3a7SDavid Howells 	int elt = 0;
3346716ea3a7SDavid Howells 
3347716ea3a7SDavid Howells 	if (copy > 0) {
3348716ea3a7SDavid Howells 		if (copy > len)
3349716ea3a7SDavid Howells 			copy = len;
3350642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
3351716ea3a7SDavid Howells 		elt++;
3352716ea3a7SDavid Howells 		if ((len -= copy) == 0)
3353716ea3a7SDavid Howells 			return elt;
3354716ea3a7SDavid Howells 		offset += copy;
3355716ea3a7SDavid Howells 	}
3356716ea3a7SDavid Howells 
3357716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
33581a028e50SDavid S. Miller 		int end;
3359716ea3a7SDavid Howells 
3360547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
33611a028e50SDavid S. Miller 
33629e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
3363716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3364716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3365716ea3a7SDavid Howells 
3366716ea3a7SDavid Howells 			if (copy > len)
3367716ea3a7SDavid Howells 				copy = len;
3368ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
3369642f1490SJens Axboe 					frag->page_offset+offset-start);
3370716ea3a7SDavid Howells 			elt++;
3371716ea3a7SDavid Howells 			if (!(len -= copy))
3372716ea3a7SDavid Howells 				return elt;
3373716ea3a7SDavid Howells 			offset += copy;
3374716ea3a7SDavid Howells 		}
33751a028e50SDavid S. Miller 		start = end;
3376716ea3a7SDavid Howells 	}
3377716ea3a7SDavid Howells 
3378fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
33791a028e50SDavid S. Miller 		int end;
3380716ea3a7SDavid Howells 
3381547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
33821a028e50SDavid S. Miller 
3383fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
3384716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
3385716ea3a7SDavid Howells 			if (copy > len)
3386716ea3a7SDavid Howells 				copy = len;
3387fbb398a8SDavid S. Miller 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
338851c739d1SDavid S. Miller 					      copy);
3389716ea3a7SDavid Howells 			if ((len -= copy) == 0)
3390716ea3a7SDavid Howells 				return elt;
3391716ea3a7SDavid Howells 			offset += copy;
3392716ea3a7SDavid Howells 		}
33931a028e50SDavid S. Miller 		start = end;
3394716ea3a7SDavid Howells 	}
3395716ea3a7SDavid Howells 	BUG_ON(len);
3396716ea3a7SDavid Howells 	return elt;
3397716ea3a7SDavid Howells }
3398716ea3a7SDavid Howells 
339925a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given
340025a91d8dSFan Du  * sglist without mark the sg which contain last skb data as the end.
340125a91d8dSFan Du  * So the caller can mannipulate sg list as will when padding new data after
340225a91d8dSFan Du  * the first call without calling sg_unmark_end to expend sg list.
340325a91d8dSFan Du  *
340425a91d8dSFan Du  * Scenario to use skb_to_sgvec_nomark:
340525a91d8dSFan Du  * 1. sg_init_table
340625a91d8dSFan Du  * 2. skb_to_sgvec_nomark(payload1)
340725a91d8dSFan Du  * 3. skb_to_sgvec_nomark(payload2)
340825a91d8dSFan Du  *
340925a91d8dSFan Du  * This is equivalent to:
341025a91d8dSFan Du  * 1. sg_init_table
341125a91d8dSFan Du  * 2. skb_to_sgvec(payload1)
341225a91d8dSFan Du  * 3. sg_unmark_end
341325a91d8dSFan Du  * 4. skb_to_sgvec(payload2)
341425a91d8dSFan Du  *
341525a91d8dSFan Du  * When mapping mutilple payload conditionally, skb_to_sgvec_nomark
341625a91d8dSFan Du  * is more preferable.
341725a91d8dSFan Du  */
341825a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
341925a91d8dSFan Du 			int offset, int len)
342025a91d8dSFan Du {
342125a91d8dSFan Du 	return __skb_to_sgvec(skb, sg, offset, len);
342225a91d8dSFan Du }
342325a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark);
342425a91d8dSFan Du 
342551c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
342651c739d1SDavid S. Miller {
342751c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
342851c739d1SDavid S. Miller 
3429c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
343051c739d1SDavid S. Miller 
343151c739d1SDavid S. Miller 	return nsg;
343251c739d1SDavid S. Miller }
3433b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
343451c739d1SDavid S. Miller 
3435716ea3a7SDavid Howells /**
3436716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
3437716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
3438716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
3439716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
3440716ea3a7SDavid Howells  *
3441716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
3442716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
3443716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
3444716ea3a7SDavid Howells  *
3445716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
3446716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
3447716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
3448716ea3a7SDavid Howells  *
3449716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
3450716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
3451716ea3a7SDavid Howells  */
3452716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3453716ea3a7SDavid Howells {
3454716ea3a7SDavid Howells 	int copyflag;
3455716ea3a7SDavid Howells 	int elt;
3456716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
3457716ea3a7SDavid Howells 
3458716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
3459716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
3460716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
3461716ea3a7SDavid Howells 	 */
3462716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3463716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3464716ea3a7SDavid Howells 		return -ENOMEM;
3465716ea3a7SDavid Howells 
3466716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
346721dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
3468716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
3469716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
3470716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
3471716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
3472716ea3a7SDavid Howells 
3473716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
3474716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3475716ea3a7SDavid Howells 			return -ENOMEM;
3476716ea3a7SDavid Howells 
3477716ea3a7SDavid Howells 		/* Voila! */
3478716ea3a7SDavid Howells 		*trailer = skb;
3479716ea3a7SDavid Howells 		return 1;
3480716ea3a7SDavid Howells 	}
3481716ea3a7SDavid Howells 
3482716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
3483716ea3a7SDavid Howells 
3484716ea3a7SDavid Howells 	elt = 1;
3485716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
3486716ea3a7SDavid Howells 	copyflag = 0;
3487716ea3a7SDavid Howells 
3488716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
3489716ea3a7SDavid Howells 		int ntail = 0;
3490716ea3a7SDavid Howells 
3491716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
3492716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
3493716ea3a7SDavid Howells 		 * after it. */
3494716ea3a7SDavid Howells 
3495716ea3a7SDavid Howells 		if (skb_shared(skb1))
3496716ea3a7SDavid Howells 			copyflag = 1;
3497716ea3a7SDavid Howells 
3498716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
3499716ea3a7SDavid Howells 
3500716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
3501716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
350221dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
3503716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
3504716ea3a7SDavid Howells 				ntail = tailbits + 128;
3505716ea3a7SDavid Howells 		}
3506716ea3a7SDavid Howells 
3507716ea3a7SDavid Howells 		if (copyflag ||
3508716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
3509716ea3a7SDavid Howells 		    ntail ||
3510716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
351121dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
3512716ea3a7SDavid Howells 			struct sk_buff *skb2;
3513716ea3a7SDavid Howells 
3514716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
3515716ea3a7SDavid Howells 			if (ntail == 0)
3516716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
3517716ea3a7SDavid Howells 			else
3518716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
3519716ea3a7SDavid Howells 						       skb_headroom(skb1),
3520716ea3a7SDavid Howells 						       ntail,
3521716ea3a7SDavid Howells 						       GFP_ATOMIC);
3522716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
3523716ea3a7SDavid Howells 				return -ENOMEM;
3524716ea3a7SDavid Howells 
3525716ea3a7SDavid Howells 			if (skb1->sk)
3526716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
3527716ea3a7SDavid Howells 
3528716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
3529716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
3530716ea3a7SDavid Howells 
3531716ea3a7SDavid Howells 			skb2->next = skb1->next;
3532716ea3a7SDavid Howells 			*skb_p = skb2;
3533716ea3a7SDavid Howells 			kfree_skb(skb1);
3534716ea3a7SDavid Howells 			skb1 = skb2;
3535716ea3a7SDavid Howells 		}
3536716ea3a7SDavid Howells 		elt++;
3537716ea3a7SDavid Howells 		*trailer = skb1;
3538716ea3a7SDavid Howells 		skb_p = &skb1->next;
3539716ea3a7SDavid Howells 	}
3540716ea3a7SDavid Howells 
3541716ea3a7SDavid Howells 	return elt;
3542716ea3a7SDavid Howells }
3543b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
3544716ea3a7SDavid Howells 
3545b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
3546b1faf566SEric Dumazet {
3547b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
3548b1faf566SEric Dumazet 
3549b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3550b1faf566SEric Dumazet }
3551b1faf566SEric Dumazet 
3552b1faf566SEric Dumazet /*
3553b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3554b1faf566SEric Dumazet  */
3555b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3556b1faf566SEric Dumazet {
3557b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
355895c96174SEric Dumazet 	    (unsigned int)sk->sk_rcvbuf)
3559b1faf566SEric Dumazet 		return -ENOMEM;
3560b1faf566SEric Dumazet 
3561b1faf566SEric Dumazet 	skb_orphan(skb);
3562b1faf566SEric Dumazet 	skb->sk = sk;
3563b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
3564b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3565b1faf566SEric Dumazet 
3566abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
3567abb57ea4SEric Dumazet 	skb_dst_force(skb);
3568abb57ea4SEric Dumazet 
3569b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
3570b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
3571676d2369SDavid S. Miller 		sk->sk_data_ready(sk);
3572b1faf566SEric Dumazet 	return 0;
3573b1faf566SEric Dumazet }
3574b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
3575b1faf566SEric Dumazet 
3576364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk)
3577364a9e93SWillem de Bruijn {
3578364a9e93SWillem de Bruijn 	struct sk_buff_head *q = &sk->sk_error_queue;
3579364a9e93SWillem de Bruijn 	struct sk_buff *skb, *skb_next;
3580997d5c3fSEric Dumazet 	unsigned long flags;
3581364a9e93SWillem de Bruijn 	int err = 0;
3582364a9e93SWillem de Bruijn 
3583997d5c3fSEric Dumazet 	spin_lock_irqsave(&q->lock, flags);
3584364a9e93SWillem de Bruijn 	skb = __skb_dequeue(q);
3585364a9e93SWillem de Bruijn 	if (skb && (skb_next = skb_peek(q)))
3586364a9e93SWillem de Bruijn 		err = SKB_EXT_ERR(skb_next)->ee.ee_errno;
3587997d5c3fSEric Dumazet 	spin_unlock_irqrestore(&q->lock, flags);
3588364a9e93SWillem de Bruijn 
3589364a9e93SWillem de Bruijn 	sk->sk_err = err;
3590364a9e93SWillem de Bruijn 	if (err)
3591364a9e93SWillem de Bruijn 		sk->sk_error_report(sk);
3592364a9e93SWillem de Bruijn 
3593364a9e93SWillem de Bruijn 	return skb;
3594364a9e93SWillem de Bruijn }
3595364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb);
3596364a9e93SWillem de Bruijn 
3597cab41c47SAlexander Duyck /**
3598cab41c47SAlexander Duyck  * skb_clone_sk - create clone of skb, and take reference to socket
3599cab41c47SAlexander Duyck  * @skb: the skb to clone
3600cab41c47SAlexander Duyck  *
3601cab41c47SAlexander Duyck  * This function creates a clone of a buffer that holds a reference on
3602cab41c47SAlexander Duyck  * sk_refcnt.  Buffers created via this function are meant to be
3603cab41c47SAlexander Duyck  * returned using sock_queue_err_skb, or free via kfree_skb.
3604cab41c47SAlexander Duyck  *
3605cab41c47SAlexander Duyck  * When passing buffers allocated with this function to sock_queue_err_skb
3606cab41c47SAlexander Duyck  * it is necessary to wrap the call with sock_hold/sock_put in order to
3607cab41c47SAlexander Duyck  * prevent the socket from being released prior to being enqueued on
3608cab41c47SAlexander Duyck  * the sk_error_queue.
3609cab41c47SAlexander Duyck  */
361062bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb)
361162bccb8cSAlexander Duyck {
361262bccb8cSAlexander Duyck 	struct sock *sk = skb->sk;
361362bccb8cSAlexander Duyck 	struct sk_buff *clone;
361462bccb8cSAlexander Duyck 
361562bccb8cSAlexander Duyck 	if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt))
361662bccb8cSAlexander Duyck 		return NULL;
361762bccb8cSAlexander Duyck 
361862bccb8cSAlexander Duyck 	clone = skb_clone(skb, GFP_ATOMIC);
361962bccb8cSAlexander Duyck 	if (!clone) {
362062bccb8cSAlexander Duyck 		sock_put(sk);
362162bccb8cSAlexander Duyck 		return NULL;
362262bccb8cSAlexander Duyck 	}
362362bccb8cSAlexander Duyck 
362462bccb8cSAlexander Duyck 	clone->sk = sk;
362562bccb8cSAlexander Duyck 	clone->destructor = sock_efree;
362662bccb8cSAlexander Duyck 
362762bccb8cSAlexander Duyck 	return clone;
362862bccb8cSAlexander Duyck }
362962bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk);
363062bccb8cSAlexander Duyck 
363137846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb,
363237846ef0SAlexander Duyck 					struct sock *sk,
363337846ef0SAlexander Duyck 					int tstype)
3634ac45f602SPatrick Ohly {
3635ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
3636ac45f602SPatrick Ohly 	int err;
3637ac45f602SPatrick Ohly 
3638ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
3639ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
3640ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
3641ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3642e7fd2885SWillem de Bruijn 	serr->ee.ee_info = tstype;
36434ed2d765SWillem de Bruijn 	if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) {
364409c2d251SWillem de Bruijn 		serr->ee.ee_data = skb_shinfo(skb)->tskey;
36454ed2d765SWillem de Bruijn 		if (sk->sk_protocol == IPPROTO_TCP)
36464ed2d765SWillem de Bruijn 			serr->ee.ee_data -= sk->sk_tskey;
36474ed2d765SWillem de Bruijn 	}
364829030374SEric Dumazet 
3649ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
365029030374SEric Dumazet 
3651ac45f602SPatrick Ohly 	if (err)
3652ac45f602SPatrick Ohly 		kfree_skb(skb);
3653ac45f602SPatrick Ohly }
365437846ef0SAlexander Duyck 
3655b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly)
3656b245be1fSWillem de Bruijn {
3657b245be1fSWillem de Bruijn 	bool ret;
3658b245be1fSWillem de Bruijn 
3659b245be1fSWillem de Bruijn 	if (likely(sysctl_tstamp_allow_data || tsonly))
3660b245be1fSWillem de Bruijn 		return true;
3661b245be1fSWillem de Bruijn 
3662b245be1fSWillem de Bruijn 	read_lock_bh(&sk->sk_callback_lock);
3663b245be1fSWillem de Bruijn 	ret = sk->sk_socket && sk->sk_socket->file &&
3664b245be1fSWillem de Bruijn 	      file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW);
3665b245be1fSWillem de Bruijn 	read_unlock_bh(&sk->sk_callback_lock);
3666b245be1fSWillem de Bruijn 	return ret;
3667b245be1fSWillem de Bruijn }
3668b245be1fSWillem de Bruijn 
366937846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb,
367037846ef0SAlexander Duyck 			       struct skb_shared_hwtstamps *hwtstamps)
367137846ef0SAlexander Duyck {
367237846ef0SAlexander Duyck 	struct sock *sk = skb->sk;
367337846ef0SAlexander Duyck 
3674b245be1fSWillem de Bruijn 	if (!skb_may_tx_timestamp(sk, false))
3675b245be1fSWillem de Bruijn 		return;
3676b245be1fSWillem de Bruijn 
367762bccb8cSAlexander Duyck 	/* take a reference to prevent skb_orphan() from freeing the socket */
367862bccb8cSAlexander Duyck 	sock_hold(sk);
367937846ef0SAlexander Duyck 
368037846ef0SAlexander Duyck 	*skb_hwtstamps(skb) = *hwtstamps;
368137846ef0SAlexander Duyck 	__skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND);
368237846ef0SAlexander Duyck 
368337846ef0SAlexander Duyck 	sock_put(sk);
368437846ef0SAlexander Duyck }
368537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp);
368637846ef0SAlexander Duyck 
368737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb,
368837846ef0SAlexander Duyck 		     struct skb_shared_hwtstamps *hwtstamps,
368937846ef0SAlexander Duyck 		     struct sock *sk, int tstype)
369037846ef0SAlexander Duyck {
369137846ef0SAlexander Duyck 	struct sk_buff *skb;
369249ca0d8bSWillem de Bruijn 	bool tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY;
369337846ef0SAlexander Duyck 
3694b245be1fSWillem de Bruijn 	if (!sk || !skb_may_tx_timestamp(sk, tsonly))
369537846ef0SAlexander Duyck 		return;
369637846ef0SAlexander Duyck 
369749ca0d8bSWillem de Bruijn 	if (tsonly)
369849ca0d8bSWillem de Bruijn 		skb = alloc_skb(0, GFP_ATOMIC);
369937846ef0SAlexander Duyck 	else
370037846ef0SAlexander Duyck 		skb = skb_clone(orig_skb, GFP_ATOMIC);
370137846ef0SAlexander Duyck 	if (!skb)
370237846ef0SAlexander Duyck 		return;
370337846ef0SAlexander Duyck 
370449ca0d8bSWillem de Bruijn 	if (tsonly) {
370549ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags;
370649ca0d8bSWillem de Bruijn 		skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey;
370749ca0d8bSWillem de Bruijn 	}
370849ca0d8bSWillem de Bruijn 
370949ca0d8bSWillem de Bruijn 	if (hwtstamps)
371049ca0d8bSWillem de Bruijn 		*skb_hwtstamps(skb) = *hwtstamps;
371149ca0d8bSWillem de Bruijn 	else
371249ca0d8bSWillem de Bruijn 		skb->tstamp = ktime_get_real();
371349ca0d8bSWillem de Bruijn 
371437846ef0SAlexander Duyck 	__skb_complete_tx_timestamp(skb, sk, tstype);
371537846ef0SAlexander Duyck }
3716e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx);
3717e7fd2885SWillem de Bruijn 
3718e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb,
3719e7fd2885SWillem de Bruijn 		   struct skb_shared_hwtstamps *hwtstamps)
3720e7fd2885SWillem de Bruijn {
3721e7fd2885SWillem de Bruijn 	return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk,
3722e7fd2885SWillem de Bruijn 			       SCM_TSTAMP_SND);
3723e7fd2885SWillem de Bruijn }
3724ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3725ac45f602SPatrick Ohly 
37266e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
37276e3e939fSJohannes Berg {
37286e3e939fSJohannes Berg 	struct sock *sk = skb->sk;
37296e3e939fSJohannes Berg 	struct sock_exterr_skb *serr;
37306e3e939fSJohannes Berg 	int err;
37316e3e939fSJohannes Berg 
37326e3e939fSJohannes Berg 	skb->wifi_acked_valid = 1;
37336e3e939fSJohannes Berg 	skb->wifi_acked = acked;
37346e3e939fSJohannes Berg 
37356e3e939fSJohannes Berg 	serr = SKB_EXT_ERR(skb);
37366e3e939fSJohannes Berg 	memset(serr, 0, sizeof(*serr));
37376e3e939fSJohannes Berg 	serr->ee.ee_errno = ENOMSG;
37386e3e939fSJohannes Berg 	serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS;
37396e3e939fSJohannes Berg 
3740bf7fa551SAlexander Duyck 	/* take a reference to prevent skb_orphan() from freeing the socket */
3741bf7fa551SAlexander Duyck 	sock_hold(sk);
3742bf7fa551SAlexander Duyck 
37436e3e939fSJohannes Berg 	err = sock_queue_err_skb(sk, skb);
37446e3e939fSJohannes Berg 	if (err)
37456e3e939fSJohannes Berg 		kfree_skb(skb);
3746bf7fa551SAlexander Duyck 
3747bf7fa551SAlexander Duyck 	sock_put(sk);
37486e3e939fSJohannes Berg }
37496e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
37506e3e939fSJohannes Berg 
3751ac45f602SPatrick Ohly 
3752f35d9d8aSRusty Russell /**
3753f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3754f35d9d8aSRusty Russell  * @skb: the skb to set
3755f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3756f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3757f35d9d8aSRusty Russell  *
3758f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3759f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3760f35d9d8aSRusty Russell  *
3761f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3762f35d9d8aSRusty Russell  * returns false you should drop the packet.
3763f35d9d8aSRusty Russell  */
3764f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3765f35d9d8aSRusty Russell {
37665ff8dda3SHerbert Xu 	if (unlikely(start > skb_headlen(skb)) ||
37675ff8dda3SHerbert Xu 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3768e87cc472SJoe Perches 		net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n",
37695ff8dda3SHerbert Xu 				     start, off, skb_headlen(skb));
3770f35d9d8aSRusty Russell 		return false;
3771f35d9d8aSRusty Russell 	}
3772f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3773f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3774f35d9d8aSRusty Russell 	skb->csum_offset = off;
3775e5d5decaSJason Wang 	skb_set_transport_header(skb, start);
3776f35d9d8aSRusty Russell 	return true;
3777f35d9d8aSRusty Russell }
3778b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3779f35d9d8aSRusty Russell 
3780ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len,
3781ed1f50c3SPaul Durrant 			       unsigned int max)
3782ed1f50c3SPaul Durrant {
3783ed1f50c3SPaul Durrant 	if (skb_headlen(skb) >= len)
3784ed1f50c3SPaul Durrant 		return 0;
3785ed1f50c3SPaul Durrant 
3786ed1f50c3SPaul Durrant 	/* If we need to pullup then pullup to the max, so we
3787ed1f50c3SPaul Durrant 	 * won't need to do it again.
3788ed1f50c3SPaul Durrant 	 */
3789ed1f50c3SPaul Durrant 	if (max > skb->len)
3790ed1f50c3SPaul Durrant 		max = skb->len;
3791ed1f50c3SPaul Durrant 
3792ed1f50c3SPaul Durrant 	if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL)
3793ed1f50c3SPaul Durrant 		return -ENOMEM;
3794ed1f50c3SPaul Durrant 
3795ed1f50c3SPaul Durrant 	if (skb_headlen(skb) < len)
3796ed1f50c3SPaul Durrant 		return -EPROTO;
3797ed1f50c3SPaul Durrant 
3798ed1f50c3SPaul Durrant 	return 0;
3799ed1f50c3SPaul Durrant }
3800ed1f50c3SPaul Durrant 
3801f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4)
3802f9708b43SJan Beulich 
3803f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb,
3804f9708b43SJan Beulich 				      typeof(IPPROTO_IP) proto,
3805f9708b43SJan Beulich 				      unsigned int off)
3806f9708b43SJan Beulich {
3807f9708b43SJan Beulich 	switch (proto) {
3808f9708b43SJan Beulich 		int err;
3809f9708b43SJan Beulich 
3810f9708b43SJan Beulich 	case IPPROTO_TCP:
3811f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr),
3812f9708b43SJan Beulich 					  off + MAX_TCP_HDR_LEN);
3813f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3814f9708b43SJan Beulich 						  offsetof(struct tcphdr,
3815f9708b43SJan Beulich 							   check)))
3816f9708b43SJan Beulich 			err = -EPROTO;
3817f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &tcp_hdr(skb)->check;
3818f9708b43SJan Beulich 
3819f9708b43SJan Beulich 	case IPPROTO_UDP:
3820f9708b43SJan Beulich 		err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr),
3821f9708b43SJan Beulich 					  off + sizeof(struct udphdr));
3822f9708b43SJan Beulich 		if (!err && !skb_partial_csum_set(skb, off,
3823f9708b43SJan Beulich 						  offsetof(struct udphdr,
3824f9708b43SJan Beulich 							   check)))
3825f9708b43SJan Beulich 			err = -EPROTO;
3826f9708b43SJan Beulich 		return err ? ERR_PTR(err) : &udp_hdr(skb)->check;
3827f9708b43SJan Beulich 	}
3828f9708b43SJan Beulich 
3829f9708b43SJan Beulich 	return ERR_PTR(-EPROTO);
3830f9708b43SJan Beulich }
3831f9708b43SJan Beulich 
3832ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3833ed1f50c3SPaul Durrant  * maximally sized IP and TCP or UDP headers.
3834ed1f50c3SPaul Durrant  */
3835ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128
3836ed1f50c3SPaul Durrant 
3837f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate)
3838ed1f50c3SPaul Durrant {
3839ed1f50c3SPaul Durrant 	unsigned int off;
3840ed1f50c3SPaul Durrant 	bool fragment;
3841f9708b43SJan Beulich 	__sum16 *csum;
3842ed1f50c3SPaul Durrant 	int err;
3843ed1f50c3SPaul Durrant 
3844ed1f50c3SPaul Durrant 	fragment = false;
3845ed1f50c3SPaul Durrant 
3846ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb,
3847ed1f50c3SPaul Durrant 				  sizeof(struct iphdr),
3848ed1f50c3SPaul Durrant 				  MAX_IP_HDR_LEN);
3849ed1f50c3SPaul Durrant 	if (err < 0)
3850ed1f50c3SPaul Durrant 		goto out;
3851ed1f50c3SPaul Durrant 
3852ed1f50c3SPaul Durrant 	if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF))
3853ed1f50c3SPaul Durrant 		fragment = true;
3854ed1f50c3SPaul Durrant 
3855ed1f50c3SPaul Durrant 	off = ip_hdrlen(skb);
3856ed1f50c3SPaul Durrant 
3857ed1f50c3SPaul Durrant 	err = -EPROTO;
3858ed1f50c3SPaul Durrant 
3859ed1f50c3SPaul Durrant 	if (fragment)
3860ed1f50c3SPaul Durrant 		goto out;
3861ed1f50c3SPaul Durrant 
3862f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off);
3863f9708b43SJan Beulich 	if (IS_ERR(csum))
3864f9708b43SJan Beulich 		return PTR_ERR(csum);
3865ed1f50c3SPaul Durrant 
3866ed1f50c3SPaul Durrant 	if (recalculate)
3867f9708b43SJan Beulich 		*csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr,
3868ed1f50c3SPaul Durrant 					   ip_hdr(skb)->daddr,
3869ed1f50c3SPaul Durrant 					   skb->len - off,
3870f9708b43SJan Beulich 					   ip_hdr(skb)->protocol, 0);
3871ed1f50c3SPaul Durrant 	err = 0;
3872ed1f50c3SPaul Durrant 
3873ed1f50c3SPaul Durrant out:
3874ed1f50c3SPaul Durrant 	return err;
3875ed1f50c3SPaul Durrant }
3876ed1f50c3SPaul Durrant 
3877ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus
3878ed1f50c3SPaul Durrant  * an IPv6 header, all options, and a maximal TCP or UDP header.
3879ed1f50c3SPaul Durrant  */
3880ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256
3881ed1f50c3SPaul Durrant 
3882ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \
3883ed1f50c3SPaul Durrant 	(type *)(skb_network_header(skb) + (off))
3884ed1f50c3SPaul Durrant 
3885ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate)
3886ed1f50c3SPaul Durrant {
3887ed1f50c3SPaul Durrant 	int err;
3888ed1f50c3SPaul Durrant 	u8 nexthdr;
3889ed1f50c3SPaul Durrant 	unsigned int off;
3890ed1f50c3SPaul Durrant 	unsigned int len;
3891ed1f50c3SPaul Durrant 	bool fragment;
3892ed1f50c3SPaul Durrant 	bool done;
3893f9708b43SJan Beulich 	__sum16 *csum;
3894ed1f50c3SPaul Durrant 
3895ed1f50c3SPaul Durrant 	fragment = false;
3896ed1f50c3SPaul Durrant 	done = false;
3897ed1f50c3SPaul Durrant 
3898ed1f50c3SPaul Durrant 	off = sizeof(struct ipv6hdr);
3899ed1f50c3SPaul Durrant 
3900ed1f50c3SPaul Durrant 	err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN);
3901ed1f50c3SPaul Durrant 	if (err < 0)
3902ed1f50c3SPaul Durrant 		goto out;
3903ed1f50c3SPaul Durrant 
3904ed1f50c3SPaul Durrant 	nexthdr = ipv6_hdr(skb)->nexthdr;
3905ed1f50c3SPaul Durrant 
3906ed1f50c3SPaul Durrant 	len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len);
3907ed1f50c3SPaul Durrant 	while (off <= len && !done) {
3908ed1f50c3SPaul Durrant 		switch (nexthdr) {
3909ed1f50c3SPaul Durrant 		case IPPROTO_DSTOPTS:
3910ed1f50c3SPaul Durrant 		case IPPROTO_HOPOPTS:
3911ed1f50c3SPaul Durrant 		case IPPROTO_ROUTING: {
3912ed1f50c3SPaul Durrant 			struct ipv6_opt_hdr *hp;
3913ed1f50c3SPaul Durrant 
3914ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3915ed1f50c3SPaul Durrant 						  off +
3916ed1f50c3SPaul Durrant 						  sizeof(struct ipv6_opt_hdr),
3917ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3918ed1f50c3SPaul Durrant 			if (err < 0)
3919ed1f50c3SPaul Durrant 				goto out;
3920ed1f50c3SPaul Durrant 
3921ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ipv6_opt_hdr, skb, off);
3922ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3923ed1f50c3SPaul Durrant 			off += ipv6_optlen(hp);
3924ed1f50c3SPaul Durrant 			break;
3925ed1f50c3SPaul Durrant 		}
3926ed1f50c3SPaul Durrant 		case IPPROTO_AH: {
3927ed1f50c3SPaul Durrant 			struct ip_auth_hdr *hp;
3928ed1f50c3SPaul Durrant 
3929ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3930ed1f50c3SPaul Durrant 						  off +
3931ed1f50c3SPaul Durrant 						  sizeof(struct ip_auth_hdr),
3932ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3933ed1f50c3SPaul Durrant 			if (err < 0)
3934ed1f50c3SPaul Durrant 				goto out;
3935ed1f50c3SPaul Durrant 
3936ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct ip_auth_hdr, skb, off);
3937ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3938ed1f50c3SPaul Durrant 			off += ipv6_authlen(hp);
3939ed1f50c3SPaul Durrant 			break;
3940ed1f50c3SPaul Durrant 		}
3941ed1f50c3SPaul Durrant 		case IPPROTO_FRAGMENT: {
3942ed1f50c3SPaul Durrant 			struct frag_hdr *hp;
3943ed1f50c3SPaul Durrant 
3944ed1f50c3SPaul Durrant 			err = skb_maybe_pull_tail(skb,
3945ed1f50c3SPaul Durrant 						  off +
3946ed1f50c3SPaul Durrant 						  sizeof(struct frag_hdr),
3947ed1f50c3SPaul Durrant 						  MAX_IPV6_HDR_LEN);
3948ed1f50c3SPaul Durrant 			if (err < 0)
3949ed1f50c3SPaul Durrant 				goto out;
3950ed1f50c3SPaul Durrant 
3951ed1f50c3SPaul Durrant 			hp = OPT_HDR(struct frag_hdr, skb, off);
3952ed1f50c3SPaul Durrant 
3953ed1f50c3SPaul Durrant 			if (hp->frag_off & htons(IP6_OFFSET | IP6_MF))
3954ed1f50c3SPaul Durrant 				fragment = true;
3955ed1f50c3SPaul Durrant 
3956ed1f50c3SPaul Durrant 			nexthdr = hp->nexthdr;
3957ed1f50c3SPaul Durrant 			off += sizeof(struct frag_hdr);
3958ed1f50c3SPaul Durrant 			break;
3959ed1f50c3SPaul Durrant 		}
3960ed1f50c3SPaul Durrant 		default:
3961ed1f50c3SPaul Durrant 			done = true;
3962ed1f50c3SPaul Durrant 			break;
3963ed1f50c3SPaul Durrant 		}
3964ed1f50c3SPaul Durrant 	}
3965ed1f50c3SPaul Durrant 
3966ed1f50c3SPaul Durrant 	err = -EPROTO;
3967ed1f50c3SPaul Durrant 
3968ed1f50c3SPaul Durrant 	if (!done || fragment)
3969ed1f50c3SPaul Durrant 		goto out;
3970ed1f50c3SPaul Durrant 
3971f9708b43SJan Beulich 	csum = skb_checksum_setup_ip(skb, nexthdr, off);
3972f9708b43SJan Beulich 	if (IS_ERR(csum))
3973f9708b43SJan Beulich 		return PTR_ERR(csum);
3974ed1f50c3SPaul Durrant 
3975ed1f50c3SPaul Durrant 	if (recalculate)
3976f9708b43SJan Beulich 		*csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
3977ed1f50c3SPaul Durrant 					 &ipv6_hdr(skb)->daddr,
3978f9708b43SJan Beulich 					 skb->len - off, nexthdr, 0);
3979ed1f50c3SPaul Durrant 	err = 0;
3980ed1f50c3SPaul Durrant 
3981ed1f50c3SPaul Durrant out:
3982ed1f50c3SPaul Durrant 	return err;
3983ed1f50c3SPaul Durrant }
3984ed1f50c3SPaul Durrant 
3985ed1f50c3SPaul Durrant /**
3986ed1f50c3SPaul Durrant  * skb_checksum_setup - set up partial checksum offset
3987ed1f50c3SPaul Durrant  * @skb: the skb to set up
3988ed1f50c3SPaul Durrant  * @recalculate: if true the pseudo-header checksum will be recalculated
3989ed1f50c3SPaul Durrant  */
3990ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate)
3991ed1f50c3SPaul Durrant {
3992ed1f50c3SPaul Durrant 	int err;
3993ed1f50c3SPaul Durrant 
3994ed1f50c3SPaul Durrant 	switch (skb->protocol) {
3995ed1f50c3SPaul Durrant 	case htons(ETH_P_IP):
3996f9708b43SJan Beulich 		err = skb_checksum_setup_ipv4(skb, recalculate);
3997ed1f50c3SPaul Durrant 		break;
3998ed1f50c3SPaul Durrant 
3999ed1f50c3SPaul Durrant 	case htons(ETH_P_IPV6):
4000ed1f50c3SPaul Durrant 		err = skb_checksum_setup_ipv6(skb, recalculate);
4001ed1f50c3SPaul Durrant 		break;
4002ed1f50c3SPaul Durrant 
4003ed1f50c3SPaul Durrant 	default:
4004ed1f50c3SPaul Durrant 		err = -EPROTO;
4005ed1f50c3SPaul Durrant 		break;
4006ed1f50c3SPaul Durrant 	}
4007ed1f50c3SPaul Durrant 
4008ed1f50c3SPaul Durrant 	return err;
4009ed1f50c3SPaul Durrant }
4010ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup);
4011ed1f50c3SPaul Durrant 
40124497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
40134497b076SBen Hutchings {
4014e87cc472SJoe Perches 	net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n",
4015e87cc472SJoe Perches 			     skb->dev->name);
40164497b076SBen Hutchings }
40174497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
4018bad43ca8SEric Dumazet 
4019bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
4020bad43ca8SEric Dumazet {
40213d861f66SEric Dumazet 	if (head_stolen) {
40223d861f66SEric Dumazet 		skb_release_head_state(skb);
4023bad43ca8SEric Dumazet 		kmem_cache_free(skbuff_head_cache, skb);
40243d861f66SEric Dumazet 	} else {
4025bad43ca8SEric Dumazet 		__kfree_skb(skb);
4026bad43ca8SEric Dumazet 	}
40273d861f66SEric Dumazet }
4028bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial);
4029bad43ca8SEric Dumazet 
4030bad43ca8SEric Dumazet /**
4031bad43ca8SEric Dumazet  * skb_try_coalesce - try to merge skb to prior one
4032bad43ca8SEric Dumazet  * @to: prior buffer
4033bad43ca8SEric Dumazet  * @from: buffer to add
4034bad43ca8SEric Dumazet  * @fragstolen: pointer to boolean
4035c6c4b97cSRandy Dunlap  * @delta_truesize: how much more was allocated than was requested
4036bad43ca8SEric Dumazet  */
4037bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
4038bad43ca8SEric Dumazet 		      bool *fragstolen, int *delta_truesize)
4039bad43ca8SEric Dumazet {
4040bad43ca8SEric Dumazet 	int i, delta, len = from->len;
4041bad43ca8SEric Dumazet 
4042bad43ca8SEric Dumazet 	*fragstolen = false;
4043bad43ca8SEric Dumazet 
4044bad43ca8SEric Dumazet 	if (skb_cloned(to))
4045bad43ca8SEric Dumazet 		return false;
4046bad43ca8SEric Dumazet 
4047bad43ca8SEric Dumazet 	if (len <= skb_tailroom(to)) {
4048e93a0435SEric Dumazet 		if (len)
4049bad43ca8SEric Dumazet 			BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));
4050bad43ca8SEric Dumazet 		*delta_truesize = 0;
4051bad43ca8SEric Dumazet 		return true;
4052bad43ca8SEric Dumazet 	}
4053bad43ca8SEric Dumazet 
4054bad43ca8SEric Dumazet 	if (skb_has_frag_list(to) || skb_has_frag_list(from))
4055bad43ca8SEric Dumazet 		return false;
4056bad43ca8SEric Dumazet 
4057bad43ca8SEric Dumazet 	if (skb_headlen(from) != 0) {
4058bad43ca8SEric Dumazet 		struct page *page;
4059bad43ca8SEric Dumazet 		unsigned int offset;
4060bad43ca8SEric Dumazet 
4061bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
4062bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS)
4063bad43ca8SEric Dumazet 			return false;
4064bad43ca8SEric Dumazet 
4065bad43ca8SEric Dumazet 		if (skb_head_is_locked(from))
4066bad43ca8SEric Dumazet 			return false;
4067bad43ca8SEric Dumazet 
4068bad43ca8SEric Dumazet 		delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
4069bad43ca8SEric Dumazet 
4070bad43ca8SEric Dumazet 		page = virt_to_head_page(from->head);
4071bad43ca8SEric Dumazet 		offset = from->data - (unsigned char *)page_address(page);
4072bad43ca8SEric Dumazet 
4073bad43ca8SEric Dumazet 		skb_fill_page_desc(to, skb_shinfo(to)->nr_frags,
4074bad43ca8SEric Dumazet 				   page, offset, skb_headlen(from));
4075bad43ca8SEric Dumazet 		*fragstolen = true;
4076bad43ca8SEric Dumazet 	} else {
4077bad43ca8SEric Dumazet 		if (skb_shinfo(to)->nr_frags +
4078bad43ca8SEric Dumazet 		    skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS)
4079bad43ca8SEric Dumazet 			return false;
4080bad43ca8SEric Dumazet 
4081f4b549a5SWeiping Pan 		delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from));
4082bad43ca8SEric Dumazet 	}
4083bad43ca8SEric Dumazet 
4084bad43ca8SEric Dumazet 	WARN_ON_ONCE(delta < len);
4085bad43ca8SEric Dumazet 
4086bad43ca8SEric Dumazet 	memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags,
4087bad43ca8SEric Dumazet 	       skb_shinfo(from)->frags,
4088bad43ca8SEric Dumazet 	       skb_shinfo(from)->nr_frags * sizeof(skb_frag_t));
4089bad43ca8SEric Dumazet 	skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags;
4090bad43ca8SEric Dumazet 
4091bad43ca8SEric Dumazet 	if (!skb_cloned(from))
4092bad43ca8SEric Dumazet 		skb_shinfo(from)->nr_frags = 0;
4093bad43ca8SEric Dumazet 
40948ea853fdSLi RongQing 	/* if the skb is not cloned this does nothing
40958ea853fdSLi RongQing 	 * since we set nr_frags to 0.
40968ea853fdSLi RongQing 	 */
4097bad43ca8SEric Dumazet 	for (i = 0; i < skb_shinfo(from)->nr_frags; i++)
4098bad43ca8SEric Dumazet 		skb_frag_ref(from, i);
4099bad43ca8SEric Dumazet 
4100bad43ca8SEric Dumazet 	to->truesize += delta;
4101bad43ca8SEric Dumazet 	to->len += len;
4102bad43ca8SEric Dumazet 	to->data_len += len;
4103bad43ca8SEric Dumazet 
4104bad43ca8SEric Dumazet 	*delta_truesize = delta;
4105bad43ca8SEric Dumazet 	return true;
4106bad43ca8SEric Dumazet }
4107bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce);
4108621e84d6SNicolas Dichtel 
4109621e84d6SNicolas Dichtel /**
41108b27f277SNicolas Dichtel  * skb_scrub_packet - scrub an skb
4111621e84d6SNicolas Dichtel  *
4112621e84d6SNicolas Dichtel  * @skb: buffer to clean
41138b27f277SNicolas Dichtel  * @xnet: packet is crossing netns
4114621e84d6SNicolas Dichtel  *
41158b27f277SNicolas Dichtel  * skb_scrub_packet can be used after encapsulating or decapsulting a packet
41168b27f277SNicolas Dichtel  * into/from a tunnel. Some information have to be cleared during these
41178b27f277SNicolas Dichtel  * operations.
41188b27f277SNicolas Dichtel  * skb_scrub_packet can also be used to clean a skb before injecting it in
41198b27f277SNicolas Dichtel  * another namespace (@xnet == true). We have to clear all information in the
41208b27f277SNicolas Dichtel  * skb that could impact namespace isolation.
4121621e84d6SNicolas Dichtel  */
41228b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4123621e84d6SNicolas Dichtel {
41248b27f277SNicolas Dichtel 	if (xnet)
4125621e84d6SNicolas Dichtel 		skb_orphan(skb);
4126621e84d6SNicolas Dichtel 	skb->tstamp.tv64 = 0;
4127621e84d6SNicolas Dichtel 	skb->pkt_type = PACKET_HOST;
4128621e84d6SNicolas Dichtel 	skb->skb_iif = 0;
412960ff7467SWANG Cong 	skb->ignore_df = 0;
4130621e84d6SNicolas Dichtel 	skb_dst_drop(skb);
4131621e84d6SNicolas Dichtel 	skb->mark = 0;
41322bd82484SEric Dumazet 	skb->sender_cpu = 0;
4133b8fb4e06SThomas Graf 	skb_init_secmark(skb);
4134621e84d6SNicolas Dichtel 	secpath_reset(skb);
4135621e84d6SNicolas Dichtel 	nf_reset(skb);
4136621e84d6SNicolas Dichtel 	nf_reset_trace(skb);
4137621e84d6SNicolas Dichtel }
4138621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet);
4139de960aa9SFlorian Westphal 
4140de960aa9SFlorian Westphal /**
4141de960aa9SFlorian Westphal  * skb_gso_transport_seglen - Return length of individual segments of a gso packet
4142de960aa9SFlorian Westphal  *
4143de960aa9SFlorian Westphal  * @skb: GSO skb
4144de960aa9SFlorian Westphal  *
4145de960aa9SFlorian Westphal  * skb_gso_transport_seglen is used to determine the real size of the
4146de960aa9SFlorian Westphal  * individual segments, including Layer4 headers (TCP/UDP).
4147de960aa9SFlorian Westphal  *
4148de960aa9SFlorian Westphal  * The MAC/L2 or network (IP, IPv6) headers are not accounted for.
4149de960aa9SFlorian Westphal  */
4150de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb)
4151de960aa9SFlorian Westphal {
4152de960aa9SFlorian Westphal 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
4153f993bc25SFlorian Westphal 	unsigned int thlen = 0;
4154f993bc25SFlorian Westphal 
4155f993bc25SFlorian Westphal 	if (skb->encapsulation) {
4156f993bc25SFlorian Westphal 		thlen = skb_inner_transport_header(skb) -
4157f993bc25SFlorian Westphal 			skb_transport_header(skb);
4158de960aa9SFlorian Westphal 
4159de960aa9SFlorian Westphal 		if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
4160f993bc25SFlorian Westphal 			thlen += inner_tcp_hdrlen(skb);
4161f993bc25SFlorian Westphal 	} else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
4162f993bc25SFlorian Westphal 		thlen = tcp_hdrlen(skb);
4163f993bc25SFlorian Westphal 	}
41646d39d589SFlorian Westphal 	/* UFO sets gso_size to the size of the fragmentation
41656d39d589SFlorian Westphal 	 * payload, i.e. the size of the L4 (UDP) header is already
41666d39d589SFlorian Westphal 	 * accounted for.
41676d39d589SFlorian Westphal 	 */
4168f993bc25SFlorian Westphal 	return thlen + shinfo->gso_size;
4169de960aa9SFlorian Westphal }
4170de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen);
41710d5501c1SVlad Yasevich 
41720d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb)
41730d5501c1SVlad Yasevich {
41740d5501c1SVlad Yasevich 	if (skb_cow(skb, skb_headroom(skb)) < 0) {
41750d5501c1SVlad Yasevich 		kfree_skb(skb);
41760d5501c1SVlad Yasevich 		return NULL;
41770d5501c1SVlad Yasevich 	}
41780d5501c1SVlad Yasevich 
41790d5501c1SVlad Yasevich 	memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN);
41800d5501c1SVlad Yasevich 	skb->mac_header += VLAN_HLEN;
41810d5501c1SVlad Yasevich 	return skb;
41820d5501c1SVlad Yasevich }
41830d5501c1SVlad Yasevich 
41840d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb)
41850d5501c1SVlad Yasevich {
41860d5501c1SVlad Yasevich 	struct vlan_hdr *vhdr;
41870d5501c1SVlad Yasevich 	u16 vlan_tci;
41880d5501c1SVlad Yasevich 
4189df8a39deSJiri Pirko 	if (unlikely(skb_vlan_tag_present(skb))) {
41900d5501c1SVlad Yasevich 		/* vlan_tci is already set-up so leave this for another time */
41910d5501c1SVlad Yasevich 		return skb;
41920d5501c1SVlad Yasevich 	}
41930d5501c1SVlad Yasevich 
41940d5501c1SVlad Yasevich 	skb = skb_share_check(skb, GFP_ATOMIC);
41950d5501c1SVlad Yasevich 	if (unlikely(!skb))
41960d5501c1SVlad Yasevich 		goto err_free;
41970d5501c1SVlad Yasevich 
41980d5501c1SVlad Yasevich 	if (unlikely(!pskb_may_pull(skb, VLAN_HLEN)))
41990d5501c1SVlad Yasevich 		goto err_free;
42000d5501c1SVlad Yasevich 
42010d5501c1SVlad Yasevich 	vhdr = (struct vlan_hdr *)skb->data;
42020d5501c1SVlad Yasevich 	vlan_tci = ntohs(vhdr->h_vlan_TCI);
42030d5501c1SVlad Yasevich 	__vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci);
42040d5501c1SVlad Yasevich 
42050d5501c1SVlad Yasevich 	skb_pull_rcsum(skb, VLAN_HLEN);
42060d5501c1SVlad Yasevich 	vlan_set_encap_proto(skb, vhdr);
42070d5501c1SVlad Yasevich 
42080d5501c1SVlad Yasevich 	skb = skb_reorder_vlan_header(skb);
42090d5501c1SVlad Yasevich 	if (unlikely(!skb))
42100d5501c1SVlad Yasevich 		goto err_free;
42110d5501c1SVlad Yasevich 
42120d5501c1SVlad Yasevich 	skb_reset_network_header(skb);
42130d5501c1SVlad Yasevich 	skb_reset_transport_header(skb);
42140d5501c1SVlad Yasevich 	skb_reset_mac_len(skb);
42150d5501c1SVlad Yasevich 
42160d5501c1SVlad Yasevich 	return skb;
42170d5501c1SVlad Yasevich 
42180d5501c1SVlad Yasevich err_free:
42190d5501c1SVlad Yasevich 	kfree_skb(skb);
42200d5501c1SVlad Yasevich 	return NULL;
42210d5501c1SVlad Yasevich }
42220d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag);
42232e4e4410SEric Dumazet 
4224e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len)
4225e2195121SJiri Pirko {
4226e2195121SJiri Pirko 	if (!pskb_may_pull(skb, write_len))
4227e2195121SJiri Pirko 		return -ENOMEM;
4228e2195121SJiri Pirko 
4229e2195121SJiri Pirko 	if (!skb_cloned(skb) || skb_clone_writable(skb, write_len))
4230e2195121SJiri Pirko 		return 0;
4231e2195121SJiri Pirko 
4232e2195121SJiri Pirko 	return pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
4233e2195121SJiri Pirko }
4234e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable);
4235e2195121SJiri Pirko 
423693515d53SJiri Pirko /* remove VLAN header from packet and update csum accordingly. */
423793515d53SJiri Pirko static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci)
423893515d53SJiri Pirko {
423993515d53SJiri Pirko 	struct vlan_hdr *vhdr;
424093515d53SJiri Pirko 	unsigned int offset = skb->data - skb_mac_header(skb);
424193515d53SJiri Pirko 	int err;
424293515d53SJiri Pirko 
424393515d53SJiri Pirko 	__skb_push(skb, offset);
424493515d53SJiri Pirko 	err = skb_ensure_writable(skb, VLAN_ETH_HLEN);
424593515d53SJiri Pirko 	if (unlikely(err))
424693515d53SJiri Pirko 		goto pull;
424793515d53SJiri Pirko 
424893515d53SJiri Pirko 	skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN);
424993515d53SJiri Pirko 
425093515d53SJiri Pirko 	vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN);
425193515d53SJiri Pirko 	*vlan_tci = ntohs(vhdr->h_vlan_TCI);
425293515d53SJiri Pirko 
425393515d53SJiri Pirko 	memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
425493515d53SJiri Pirko 	__skb_pull(skb, VLAN_HLEN);
425593515d53SJiri Pirko 
425693515d53SJiri Pirko 	vlan_set_encap_proto(skb, vhdr);
425793515d53SJiri Pirko 	skb->mac_header += VLAN_HLEN;
425893515d53SJiri Pirko 
425993515d53SJiri Pirko 	if (skb_network_offset(skb) < ETH_HLEN)
426093515d53SJiri Pirko 		skb_set_network_header(skb, ETH_HLEN);
426193515d53SJiri Pirko 
426293515d53SJiri Pirko 	skb_reset_mac_len(skb);
426393515d53SJiri Pirko pull:
426493515d53SJiri Pirko 	__skb_pull(skb, offset);
426593515d53SJiri Pirko 
426693515d53SJiri Pirko 	return err;
426793515d53SJiri Pirko }
426893515d53SJiri Pirko 
426993515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb)
427093515d53SJiri Pirko {
427193515d53SJiri Pirko 	u16 vlan_tci;
427293515d53SJiri Pirko 	__be16 vlan_proto;
427393515d53SJiri Pirko 	int err;
427493515d53SJiri Pirko 
4275df8a39deSJiri Pirko 	if (likely(skb_vlan_tag_present(skb))) {
427693515d53SJiri Pirko 		skb->vlan_tci = 0;
427793515d53SJiri Pirko 	} else {
427893515d53SJiri Pirko 		if (unlikely((skb->protocol != htons(ETH_P_8021Q) &&
427993515d53SJiri Pirko 			      skb->protocol != htons(ETH_P_8021AD)) ||
428093515d53SJiri Pirko 			     skb->len < VLAN_ETH_HLEN))
428193515d53SJiri Pirko 			return 0;
428293515d53SJiri Pirko 
428393515d53SJiri Pirko 		err = __skb_vlan_pop(skb, &vlan_tci);
428493515d53SJiri Pirko 		if (err)
428593515d53SJiri Pirko 			return err;
428693515d53SJiri Pirko 	}
428793515d53SJiri Pirko 	/* move next vlan tag to hw accel tag */
428893515d53SJiri Pirko 	if (likely((skb->protocol != htons(ETH_P_8021Q) &&
428993515d53SJiri Pirko 		    skb->protocol != htons(ETH_P_8021AD)) ||
429093515d53SJiri Pirko 		   skb->len < VLAN_ETH_HLEN))
429193515d53SJiri Pirko 		return 0;
429293515d53SJiri Pirko 
429393515d53SJiri Pirko 	vlan_proto = skb->protocol;
429493515d53SJiri Pirko 	err = __skb_vlan_pop(skb, &vlan_tci);
429593515d53SJiri Pirko 	if (unlikely(err))
429693515d53SJiri Pirko 		return err;
429793515d53SJiri Pirko 
429893515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
429993515d53SJiri Pirko 	return 0;
430093515d53SJiri Pirko }
430193515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop);
430293515d53SJiri Pirko 
430393515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci)
430493515d53SJiri Pirko {
4305df8a39deSJiri Pirko 	if (skb_vlan_tag_present(skb)) {
430693515d53SJiri Pirko 		unsigned int offset = skb->data - skb_mac_header(skb);
430793515d53SJiri Pirko 		int err;
430893515d53SJiri Pirko 
430993515d53SJiri Pirko 		/* __vlan_insert_tag expect skb->data pointing to mac header.
431093515d53SJiri Pirko 		 * So change skb->data before calling it and change back to
431193515d53SJiri Pirko 		 * original position later
431293515d53SJiri Pirko 		 */
431393515d53SJiri Pirko 		__skb_push(skb, offset);
431493515d53SJiri Pirko 		err = __vlan_insert_tag(skb, skb->vlan_proto,
4315df8a39deSJiri Pirko 					skb_vlan_tag_get(skb));
431693515d53SJiri Pirko 		if (err)
431793515d53SJiri Pirko 			return err;
431893515d53SJiri Pirko 		skb->protocol = skb->vlan_proto;
431993515d53SJiri Pirko 		skb->mac_len += VLAN_HLEN;
432093515d53SJiri Pirko 		__skb_pull(skb, offset);
432193515d53SJiri Pirko 
432293515d53SJiri Pirko 		if (skb->ip_summed == CHECKSUM_COMPLETE)
432393515d53SJiri Pirko 			skb->csum = csum_add(skb->csum, csum_partial(skb->data
432493515d53SJiri Pirko 					+ (2 * ETH_ALEN), VLAN_HLEN, 0));
432593515d53SJiri Pirko 	}
432693515d53SJiri Pirko 	__vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci);
432793515d53SJiri Pirko 	return 0;
432893515d53SJiri Pirko }
432993515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push);
433093515d53SJiri Pirko 
43312e4e4410SEric Dumazet /**
43322e4e4410SEric Dumazet  * alloc_skb_with_frags - allocate skb with page frags
43332e4e4410SEric Dumazet  *
4334de3f0d0eSMasanari Iida  * @header_len: size of linear part
4335de3f0d0eSMasanari Iida  * @data_len: needed length in frags
4336de3f0d0eSMasanari Iida  * @max_page_order: max page order desired.
4337de3f0d0eSMasanari Iida  * @errcode: pointer to error code if any
4338de3f0d0eSMasanari Iida  * @gfp_mask: allocation mask
43392e4e4410SEric Dumazet  *
43402e4e4410SEric Dumazet  * This can be used to allocate a paged skb, given a maximal order for frags.
43412e4e4410SEric Dumazet  */
43422e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
43432e4e4410SEric Dumazet 				     unsigned long data_len,
43442e4e4410SEric Dumazet 				     int max_page_order,
43452e4e4410SEric Dumazet 				     int *errcode,
43462e4e4410SEric Dumazet 				     gfp_t gfp_mask)
43472e4e4410SEric Dumazet {
43482e4e4410SEric Dumazet 	int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
43492e4e4410SEric Dumazet 	unsigned long chunk;
43502e4e4410SEric Dumazet 	struct sk_buff *skb;
43512e4e4410SEric Dumazet 	struct page *page;
43522e4e4410SEric Dumazet 	gfp_t gfp_head;
43532e4e4410SEric Dumazet 	int i;
43542e4e4410SEric Dumazet 
43552e4e4410SEric Dumazet 	*errcode = -EMSGSIZE;
43562e4e4410SEric Dumazet 	/* Note this test could be relaxed, if we succeed to allocate
43572e4e4410SEric Dumazet 	 * high order pages...
43582e4e4410SEric Dumazet 	 */
43592e4e4410SEric Dumazet 	if (npages > MAX_SKB_FRAGS)
43602e4e4410SEric Dumazet 		return NULL;
43612e4e4410SEric Dumazet 
43622e4e4410SEric Dumazet 	gfp_head = gfp_mask;
43632e4e4410SEric Dumazet 	if (gfp_head & __GFP_WAIT)
43642e4e4410SEric Dumazet 		gfp_head |= __GFP_REPEAT;
43652e4e4410SEric Dumazet 
43662e4e4410SEric Dumazet 	*errcode = -ENOBUFS;
43672e4e4410SEric Dumazet 	skb = alloc_skb(header_len, gfp_head);
43682e4e4410SEric Dumazet 	if (!skb)
43692e4e4410SEric Dumazet 		return NULL;
43702e4e4410SEric Dumazet 
43712e4e4410SEric Dumazet 	skb->truesize += npages << PAGE_SHIFT;
43722e4e4410SEric Dumazet 
43732e4e4410SEric Dumazet 	for (i = 0; npages > 0; i++) {
43742e4e4410SEric Dumazet 		int order = max_page_order;
43752e4e4410SEric Dumazet 
43762e4e4410SEric Dumazet 		while (order) {
43772e4e4410SEric Dumazet 			if (npages >= 1 << order) {
43782e4e4410SEric Dumazet 				page = alloc_pages(gfp_mask |
43792e4e4410SEric Dumazet 						   __GFP_COMP |
43802e4e4410SEric Dumazet 						   __GFP_NOWARN |
43812e4e4410SEric Dumazet 						   __GFP_NORETRY,
43822e4e4410SEric Dumazet 						   order);
43832e4e4410SEric Dumazet 				if (page)
43842e4e4410SEric Dumazet 					goto fill_page;
43852e4e4410SEric Dumazet 				/* Do not retry other high order allocations */
43862e4e4410SEric Dumazet 				order = 1;
43872e4e4410SEric Dumazet 				max_page_order = 0;
43882e4e4410SEric Dumazet 			}
43892e4e4410SEric Dumazet 			order--;
43902e4e4410SEric Dumazet 		}
43912e4e4410SEric Dumazet 		page = alloc_page(gfp_mask);
43922e4e4410SEric Dumazet 		if (!page)
43932e4e4410SEric Dumazet 			goto failure;
43942e4e4410SEric Dumazet fill_page:
43952e4e4410SEric Dumazet 		chunk = min_t(unsigned long, data_len,
43962e4e4410SEric Dumazet 			      PAGE_SIZE << order);
43972e4e4410SEric Dumazet 		skb_fill_page_desc(skb, i, page, 0, chunk);
43982e4e4410SEric Dumazet 		data_len -= chunk;
43992e4e4410SEric Dumazet 		npages -= 1 << order;
44002e4e4410SEric Dumazet 	}
44012e4e4410SEric Dumazet 	return skb;
44022e4e4410SEric Dumazet 
44032e4e4410SEric Dumazet failure:
44042e4e4410SEric Dumazet 	kfree_skb(skb);
44052e4e4410SEric Dumazet 	return NULL;
44062e4e4410SEric Dumazet }
44072e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags);
4408