xref: /openbmc/linux/net/core/skbuff.c (revision 20e994a0)
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 
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/types.h>
411da177e4SLinus Torvalds #include <linux/kernel.h>
42fe55f6d5SVegard Nossum #include <linux/kmemcheck.h>
431da177e4SLinus Torvalds #include <linux/mm.h>
441da177e4SLinus Torvalds #include <linux/interrupt.h>
451da177e4SLinus Torvalds #include <linux/in.h>
461da177e4SLinus Torvalds #include <linux/inet.h>
471da177e4SLinus Torvalds #include <linux/slab.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
501da177e4SLinus Torvalds #include <net/pkt_sched.h>
511da177e4SLinus Torvalds #endif
521da177e4SLinus Torvalds #include <linux/string.h>
531da177e4SLinus Torvalds #include <linux/skbuff.h>
549c55e01cSJens Axboe #include <linux/splice.h>
551da177e4SLinus Torvalds #include <linux/cache.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <linux/init.h>
58716ea3a7SDavid Howells #include <linux/scatterlist.h>
59ac45f602SPatrick Ohly #include <linux/errqueue.h>
60268bb0ceSLinus Torvalds #include <linux/prefetch.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <net/protocol.h>
631da177e4SLinus Torvalds #include <net/dst.h>
641da177e4SLinus Torvalds #include <net/sock.h>
651da177e4SLinus Torvalds #include <net/checksum.h>
661da177e4SLinus Torvalds #include <net/xfrm.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <asm/uaccess.h>
691da177e4SLinus Torvalds #include <asm/system.h>
70ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
711da177e4SLinus Torvalds 
72a1f8e7f7SAl Viro #include "kmap_skb.h"
73a1f8e7f7SAl Viro 
74e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly;
75e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
761da177e4SLinus Torvalds 
779c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
789c55e01cSJens Axboe 				  struct pipe_buffer *buf)
799c55e01cSJens Axboe {
808b9d3728SJarek Poplawski 	put_page(buf->page);
819c55e01cSJens Axboe }
829c55e01cSJens Axboe 
839c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
849c55e01cSJens Axboe 				struct pipe_buffer *buf)
859c55e01cSJens Axboe {
868b9d3728SJarek Poplawski 	get_page(buf->page);
879c55e01cSJens Axboe }
889c55e01cSJens Axboe 
899c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
909c55e01cSJens Axboe 			       struct pipe_buffer *buf)
919c55e01cSJens Axboe {
929c55e01cSJens Axboe 	return 1;
939c55e01cSJens Axboe }
949c55e01cSJens Axboe 
959c55e01cSJens Axboe 
969c55e01cSJens Axboe /* Pipe buffer operations for a socket. */
9728dfef8fSAlexey Dobriyan static const struct pipe_buf_operations sock_pipe_buf_ops = {
989c55e01cSJens Axboe 	.can_merge = 0,
999c55e01cSJens Axboe 	.map = generic_pipe_buf_map,
1009c55e01cSJens Axboe 	.unmap = generic_pipe_buf_unmap,
1019c55e01cSJens Axboe 	.confirm = generic_pipe_buf_confirm,
1029c55e01cSJens Axboe 	.release = sock_pipe_buf_release,
1039c55e01cSJens Axboe 	.steal = sock_pipe_buf_steal,
1049c55e01cSJens Axboe 	.get = sock_pipe_buf_get,
1059c55e01cSJens Axboe };
1069c55e01cSJens Axboe 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  *	Keep out-of-line to prevent kernel bloat.
1091da177e4SLinus Torvalds  *	__builtin_return_address is not used because it is not always
1101da177e4SLinus Torvalds  *	reliable.
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /**
1141da177e4SLinus Torvalds  *	skb_over_panic	- 	private function
1151da177e4SLinus Torvalds  *	@skb: buffer
1161da177e4SLinus Torvalds  *	@sz: size
1171da177e4SLinus Torvalds  *	@here: address
1181da177e4SLinus Torvalds  *
1191da177e4SLinus Torvalds  *	Out of line support code for skb_put(). Not user callable.
1201da177e4SLinus Torvalds  */
121ccb7c773SRami Rosen static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
1221da177e4SLinus Torvalds {
12326095455SPatrick McHardy 	printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
1244305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
12527a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1264305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
12726095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1281da177e4SLinus Torvalds 	BUG();
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /**
1321da177e4SLinus Torvalds  *	skb_under_panic	- 	private function
1331da177e4SLinus Torvalds  *	@skb: buffer
1341da177e4SLinus Torvalds  *	@sz: size
1351da177e4SLinus Torvalds  *	@here: address
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  *	Out of line support code for skb_push(). Not user callable.
1381da177e4SLinus Torvalds  */
1391da177e4SLinus Torvalds 
140ccb7c773SRami Rosen static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
1411da177e4SLinus Torvalds {
14226095455SPatrick McHardy 	printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
1434305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
14427a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1454305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
14626095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1471da177e4SLinus Torvalds 	BUG();
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1511da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1521da177e4SLinus Torvalds  *	[BEEP] leaks.
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  */
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /**
157d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1581da177e4SLinus Torvalds  *	@size: size to allocate
1591da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
160c83c2486SRandy Dunlap  *	@fclone: allocate from fclone cache instead of head cache
161c83c2486SRandy Dunlap  *		and allocate a cloned (child) skb
162b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1631da177e4SLinus Torvalds  *
1641da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
1651da177e4SLinus Torvalds  *	tail room of size bytes. The object has a reference count of one.
1661da177e4SLinus Torvalds  *	The return is the buffer. On a failure the return is %NULL.
1671da177e4SLinus Torvalds  *
1681da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1691da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1701da177e4SLinus Torvalds  */
171dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
172b30973f8SChristoph Hellwig 			    int fclone, int node)
1731da177e4SLinus Torvalds {
174e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1754947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1761da177e4SLinus Torvalds 	struct sk_buff *skb;
1771da177e4SLinus Torvalds 	u8 *data;
1781da177e4SLinus Torvalds 
1798798b3fbSHerbert Xu 	cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
1808798b3fbSHerbert Xu 
1811da177e4SLinus Torvalds 	/* Get the HEAD */
182b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1831da177e4SLinus Torvalds 	if (!skb)
1841da177e4SLinus Torvalds 		goto out;
185ec7d2f2cSEric Dumazet 	prefetchw(skb);
1861da177e4SLinus Torvalds 
18787fb4b7bSEric Dumazet 	/* We do our best to align skb_shared_info on a separate cache
18887fb4b7bSEric Dumazet 	 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
18987fb4b7bSEric Dumazet 	 * aligned memory blocks, unless SLUB/SLAB debug is enabled.
19087fb4b7bSEric Dumazet 	 * Both skb->head and skb_shared_info are cache line aligned.
19187fb4b7bSEric Dumazet 	 */
192bc417e30STony Lindgren 	size = SKB_DATA_ALIGN(size);
19387fb4b7bSEric Dumazet 	size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
19487fb4b7bSEric Dumazet 	data = kmalloc_node_track_caller(size, gfp_mask, node);
1951da177e4SLinus Torvalds 	if (!data)
1961da177e4SLinus Torvalds 		goto nodata;
19787fb4b7bSEric Dumazet 	/* kmalloc(size) might give us more room than requested.
19887fb4b7bSEric Dumazet 	 * Put skb_shared_info exactly at the end of allocated zone,
19987fb4b7bSEric Dumazet 	 * to allow max possible filling before reallocation.
20087fb4b7bSEric Dumazet 	 */
20187fb4b7bSEric Dumazet 	size = SKB_WITH_OVERHEAD(ksize(data));
202ec7d2f2cSEric Dumazet 	prefetchw(data + size);
2031da177e4SLinus Torvalds 
204ca0605a7SArnaldo Carvalho de Melo 	/*
205c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
206c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
207c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
208ca0605a7SArnaldo Carvalho de Melo 	 */
209ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
21087fb4b7bSEric Dumazet 	/* Account for allocated memory : skb + skb->head */
21187fb4b7bSEric Dumazet 	skb->truesize = SKB_TRUESIZE(size);
2121da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2131da177e4SLinus Torvalds 	skb->head = data;
2141da177e4SLinus Torvalds 	skb->data = data;
21527a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2164305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
21719633e12SStephen Hemminger #ifdef NET_SKBUFF_DATA_USES_OFFSET
21819633e12SStephen Hemminger 	skb->mac_header = ~0U;
21919633e12SStephen Hemminger #endif
22019633e12SStephen Hemminger 
2214947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2224947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
223ec7d2f2cSEric Dumazet 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
2244947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
225c2aa3665SEric Dumazet 	kmemcheck_annotate_variable(shinfo->destructor_arg);
2264947d3efSBenjamin LaHaise 
227d179cd12SDavid S. Miller 	if (fclone) {
228d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
229d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
2301da177e4SLinus Torvalds 
231fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(child, flags1);
232fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(child, flags2);
233d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
234d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
235d179cd12SDavid S. Miller 
236d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
237d179cd12SDavid S. Miller 	}
2381da177e4SLinus Torvalds out:
2391da177e4SLinus Torvalds 	return skb;
2401da177e4SLinus Torvalds nodata:
2418798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2421da177e4SLinus Torvalds 	skb = NULL;
2431da177e4SLinus Torvalds 	goto out;
2441da177e4SLinus Torvalds }
245b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /**
2488af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
2498af27456SChristoph Hellwig  *	@dev: network device to receive on
2508af27456SChristoph Hellwig  *	@length: length to allocate
2518af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
2528af27456SChristoph Hellwig  *
2538af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
2548af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
2558af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
2568af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
2578af27456SChristoph Hellwig  *
2588af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
2598af27456SChristoph Hellwig  */
2608af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
2618af27456SChristoph Hellwig 		unsigned int length, gfp_t gfp_mask)
2628af27456SChristoph Hellwig {
2638af27456SChristoph Hellwig 	struct sk_buff *skb;
2648af27456SChristoph Hellwig 
265564824b0SEric Dumazet 	skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, NUMA_NO_NODE);
2667b2e497aSChristoph Hellwig 	if (likely(skb)) {
2678af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
2687b2e497aSChristoph Hellwig 		skb->dev = dev;
2697b2e497aSChristoph Hellwig 	}
2708af27456SChristoph Hellwig 	return skb;
2718af27456SChristoph Hellwig }
272b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
2731da177e4SLinus Torvalds 
274654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
275654bed16SPeter Zijlstra 		int size)
276654bed16SPeter Zijlstra {
277654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
278654bed16SPeter Zijlstra 	skb->len += size;
279654bed16SPeter Zijlstra 	skb->data_len += size;
280654bed16SPeter Zijlstra 	skb->truesize += size;
281654bed16SPeter Zijlstra }
282654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
283654bed16SPeter Zijlstra 
284f58518e6SIlpo Järvinen /**
285f58518e6SIlpo Järvinen  *	dev_alloc_skb - allocate an skbuff for receiving
286f58518e6SIlpo Järvinen  *	@length: length to allocate
287f58518e6SIlpo Järvinen  *
288f58518e6SIlpo Järvinen  *	Allocate a new &sk_buff and assign it a usage count of one. The
289f58518e6SIlpo Järvinen  *	buffer has unspecified headroom built in. Users should allocate
290f58518e6SIlpo Järvinen  *	the headroom they think they need without accounting for the
291f58518e6SIlpo Järvinen  *	built in space. The built in space is used for optimisations.
292f58518e6SIlpo Järvinen  *
293f58518e6SIlpo Järvinen  *	%NULL is returned if there is no free memory. Although this function
294f58518e6SIlpo Järvinen  *	allocates memory it can be called from an interrupt.
295f58518e6SIlpo Järvinen  */
296f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length)
297f58518e6SIlpo Järvinen {
2981483b874SDenys Vlasenko 	/*
2991483b874SDenys Vlasenko 	 * There is more code here than it seems:
300a0f55e0eSDavid S. Miller 	 * __dev_alloc_skb is an inline
3011483b874SDenys Vlasenko 	 */
302f58518e6SIlpo Järvinen 	return __dev_alloc_skb(length, GFP_ATOMIC);
303f58518e6SIlpo Järvinen }
304f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb);
305f58518e6SIlpo Järvinen 
30627b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
3071da177e4SLinus Torvalds {
30827b437c8SHerbert Xu 	struct sk_buff *list = *listp;
3091da177e4SLinus Torvalds 
31027b437c8SHerbert Xu 	*listp = NULL;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	do {
3131da177e4SLinus Torvalds 		struct sk_buff *this = list;
3141da177e4SLinus Torvalds 		list = list->next;
3151da177e4SLinus Torvalds 		kfree_skb(this);
3161da177e4SLinus Torvalds 	} while (list);
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
31927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
32027b437c8SHerbert Xu {
32127b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
32227b437c8SHerbert Xu }
32327b437c8SHerbert Xu 
3241da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
3251da177e4SLinus Torvalds {
3261da177e4SLinus Torvalds 	struct sk_buff *list;
3271da177e4SLinus Torvalds 
328fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
3291da177e4SLinus Torvalds 		skb_get(list);
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3325bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	if (!skb->cloned ||
3351da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
3361da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
3371da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
3381da177e4SLinus Torvalds 			int i;
3391da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
340ea2ab693SIan Campbell 				skb_frag_unref(skb, i);
3411da177e4SLinus Torvalds 		}
3421da177e4SLinus Torvalds 
343a6686f2fSShirley Ma 		/*
344a6686f2fSShirley Ma 		 * If skb buf is from userspace, we need to notify the caller
345a6686f2fSShirley Ma 		 * the lower device DMA has done;
346a6686f2fSShirley Ma 		 */
347a6686f2fSShirley Ma 		if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
348a6686f2fSShirley Ma 			struct ubuf_info *uarg;
349a6686f2fSShirley Ma 
350a6686f2fSShirley Ma 			uarg = skb_shinfo(skb)->destructor_arg;
351a6686f2fSShirley Ma 			if (uarg->callback)
352a6686f2fSShirley Ma 				uarg->callback(uarg);
353a6686f2fSShirley Ma 		}
354a6686f2fSShirley Ma 
35521dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
3561da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 		kfree(skb->head);
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
3631da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
3641da177e4SLinus Torvalds  */
3652d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
3661da177e4SLinus Torvalds {
367d179cd12SDavid S. Miller 	struct sk_buff *other;
368d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
369d179cd12SDavid S. Miller 
370d179cd12SDavid S. Miller 	switch (skb->fclone) {
371d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
3721da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
373d179cd12SDavid S. Miller 		break;
374d179cd12SDavid S. Miller 
375d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
376d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
377d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
378d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
379d179cd12SDavid S. Miller 		break;
380d179cd12SDavid S. Miller 
381d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
382d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
383d179cd12SDavid S. Miller 		other = skb - 1;
384d179cd12SDavid S. Miller 
385d179cd12SDavid S. Miller 		/* The clone portion is available for
386d179cd12SDavid S. Miller 		 * fast-cloning again.
387d179cd12SDavid S. Miller 		 */
388d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
389d179cd12SDavid S. Miller 
390d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
391d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
392d179cd12SDavid S. Miller 		break;
3933ff50b79SStephen Hemminger 	}
3941da177e4SLinus Torvalds }
3951da177e4SLinus Torvalds 
39604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
3971da177e4SLinus Torvalds {
398adf30907SEric Dumazet 	skb_dst_drop(skb);
3991da177e4SLinus Torvalds #ifdef CONFIG_XFRM
4001da177e4SLinus Torvalds 	secpath_put(skb->sp);
4011da177e4SLinus Torvalds #endif
4021da177e4SLinus Torvalds 	if (skb->destructor) {
4039c2b3328SStephen Hemminger 		WARN_ON(in_irq());
4041da177e4SLinus Torvalds 		skb->destructor(skb);
4051da177e4SLinus Torvalds 	}
4069fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4075f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
4082fc72c7bSKOVACS Krisztian #endif
4092fc72c7bSKOVACS Krisztian #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
4109fb9cbb1SYasuyuki Kozakai 	nf_conntrack_put_reasm(skb->nfct_reasm);
4119fb9cbb1SYasuyuki Kozakai #endif
4121da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
4131da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
4141da177e4SLinus Torvalds #endif
4151da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
4161da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4171da177e4SLinus Torvalds 	skb->tc_index = 0;
4181da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
4191da177e4SLinus Torvalds 	skb->tc_verd = 0;
4201da177e4SLinus Torvalds #endif
4211da177e4SLinus Torvalds #endif
42204a4bb55SLennert Buytenhek }
42304a4bb55SLennert Buytenhek 
42404a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
42504a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
42604a4bb55SLennert Buytenhek {
42704a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
4282d4baff8SHerbert Xu 	skb_release_data(skb);
4292d4baff8SHerbert Xu }
4301da177e4SLinus Torvalds 
4312d4baff8SHerbert Xu /**
4322d4baff8SHerbert Xu  *	__kfree_skb - private function
4332d4baff8SHerbert Xu  *	@skb: buffer
4342d4baff8SHerbert Xu  *
4352d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
4362d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
4372d4baff8SHerbert Xu  *	always call kfree_skb
4382d4baff8SHerbert Xu  */
4392d4baff8SHerbert Xu 
4402d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
4412d4baff8SHerbert Xu {
4422d4baff8SHerbert Xu 	skb_release_all(skb);
4431da177e4SLinus Torvalds 	kfree_skbmem(skb);
4441da177e4SLinus Torvalds }
445b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /**
448231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
449231d06aeSJörn Engel  *	@skb: buffer to free
450231d06aeSJörn Engel  *
451231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
452231d06aeSJörn Engel  *	hit zero.
453231d06aeSJörn Engel  */
454231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
455231d06aeSJörn Engel {
456231d06aeSJörn Engel 	if (unlikely(!skb))
457231d06aeSJörn Engel 		return;
458231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
459231d06aeSJörn Engel 		smp_rmb();
460231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
461231d06aeSJörn Engel 		return;
462ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
463231d06aeSJörn Engel 	__kfree_skb(skb);
464231d06aeSJörn Engel }
465b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
466231d06aeSJörn Engel 
467d1a203eaSStephen Hemminger /**
468ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
469ead2ceb0SNeil Horman  *	@skb: buffer to free
470ead2ceb0SNeil Horman  *
471ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
472ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
473ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
474ead2ceb0SNeil Horman  */
475ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
476ead2ceb0SNeil Horman {
477ead2ceb0SNeil Horman 	if (unlikely(!skb))
478ead2ceb0SNeil Horman 		return;
479ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
480ead2ceb0SNeil Horman 		smp_rmb();
481ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
482ead2ceb0SNeil Horman 		return;
48307dc22e7SKoki Sanagi 	trace_consume_skb(skb);
484ead2ceb0SNeil Horman 	__kfree_skb(skb);
485ead2ceb0SNeil Horman }
486ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
487ead2ceb0SNeil Horman 
488ead2ceb0SNeil Horman /**
4893d153a7cSAndy Fleming  * 	skb_recycle - clean up an skb for reuse
4903d153a7cSAndy Fleming  * 	@skb: buffer
4913d153a7cSAndy Fleming  *
4923d153a7cSAndy Fleming  * 	Recycles the skb to be reused as a receive buffer. This
4933d153a7cSAndy Fleming  * 	function does any necessary reference count dropping, and
4943d153a7cSAndy Fleming  * 	cleans up the skbuff as if it just came from __alloc_skb().
4953d153a7cSAndy Fleming  */
4963d153a7cSAndy Fleming void skb_recycle(struct sk_buff *skb)
4973d153a7cSAndy Fleming {
4983d153a7cSAndy Fleming 	struct skb_shared_info *shinfo;
4993d153a7cSAndy Fleming 
5003d153a7cSAndy Fleming 	skb_release_head_state(skb);
5013d153a7cSAndy Fleming 
5023d153a7cSAndy Fleming 	shinfo = skb_shinfo(skb);
5033d153a7cSAndy Fleming 	memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
5043d153a7cSAndy Fleming 	atomic_set(&shinfo->dataref, 1);
5053d153a7cSAndy Fleming 
5063d153a7cSAndy Fleming 	memset(skb, 0, offsetof(struct sk_buff, tail));
5073d153a7cSAndy Fleming 	skb->data = skb->head + NET_SKB_PAD;
5083d153a7cSAndy Fleming 	skb_reset_tail_pointer(skb);
5093d153a7cSAndy Fleming }
5103d153a7cSAndy Fleming EXPORT_SYMBOL(skb_recycle);
5113d153a7cSAndy Fleming 
5123d153a7cSAndy Fleming /**
513d1a203eaSStephen Hemminger  *	skb_recycle_check - check if skb can be reused for receive
514d1a203eaSStephen Hemminger  *	@skb: buffer
515d1a203eaSStephen Hemminger  *	@skb_size: minimum receive buffer size
516d1a203eaSStephen Hemminger  *
517d1a203eaSStephen Hemminger  *	Checks that the skb passed in is not shared or cloned, and
518d1a203eaSStephen Hemminger  *	that it is linear and its head portion at least as large as
519d1a203eaSStephen Hemminger  *	skb_size so that it can be recycled as a receive buffer.
520d1a203eaSStephen Hemminger  *	If these conditions are met, this function does any necessary
521d1a203eaSStephen Hemminger  *	reference count dropping and cleans up the skbuff as if it
522d1a203eaSStephen Hemminger  *	just came from __alloc_skb().
523d1a203eaSStephen Hemminger  */
5245b0daa34SChangli Gao bool skb_recycle_check(struct sk_buff *skb, int skb_size)
52504a4bb55SLennert Buytenhek {
5263d153a7cSAndy Fleming 	if (!skb_is_recycleable(skb, skb_size))
5275b0daa34SChangli Gao 		return false;
528e84af6ddSAnton Vorontsov 
5293d153a7cSAndy Fleming 	skb_recycle(skb);
53004a4bb55SLennert Buytenhek 
5315b0daa34SChangli Gao 	return true;
53204a4bb55SLennert Buytenhek }
53304a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check);
53404a4bb55SLennert Buytenhek 
535dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
536dec18810SHerbert Xu {
537dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
538dec18810SHerbert Xu 	new->dev		= old->dev;
539dec18810SHerbert Xu 	new->transport_header	= old->transport_header;
540dec18810SHerbert Xu 	new->network_header	= old->network_header;
541dec18810SHerbert Xu 	new->mac_header		= old->mac_header;
5427fee226aSEric Dumazet 	skb_dst_copy(new, old);
5430a9627f2STom Herbert 	new->rxhash		= old->rxhash;
5446461be3aSChangli Gao 	new->ooo_okay		= old->ooo_okay;
545bdeab991STom Herbert 	new->l4_rxhash		= old->l4_rxhash;
546def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
547dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
548dec18810SHerbert Xu #endif
549dec18810SHerbert Xu 	memcpy(new->cb, old->cb, sizeof(old->cb));
5509bcb97caSHerbert Xu 	new->csum		= old->csum;
551dec18810SHerbert Xu 	new->local_df		= old->local_df;
552dec18810SHerbert Xu 	new->pkt_type		= old->pkt_type;
553dec18810SHerbert Xu 	new->ip_summed		= old->ip_summed;
554dec18810SHerbert Xu 	skb_copy_queue_mapping(new, old);
555dec18810SHerbert Xu 	new->priority		= old->priority;
556dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
557dec18810SHerbert Xu 	new->ipvs_property	= old->ipvs_property;
558dec18810SHerbert Xu #endif
559dec18810SHerbert Xu 	new->protocol		= old->protocol;
560dec18810SHerbert Xu 	new->mark		= old->mark;
5618964be4aSEric Dumazet 	new->skb_iif		= old->skb_iif;
562dec18810SHerbert Xu 	__nf_copy(new, old);
563dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
564dec18810SHerbert Xu     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
565dec18810SHerbert Xu 	new->nf_trace		= old->nf_trace;
566dec18810SHerbert Xu #endif
567dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED
568dec18810SHerbert Xu 	new->tc_index		= old->tc_index;
569dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT
570dec18810SHerbert Xu 	new->tc_verd		= old->tc_verd;
571dec18810SHerbert Xu #endif
572dec18810SHerbert Xu #endif
5736aa895b0SPatrick McHardy 	new->vlan_tci		= old->vlan_tci;
5746aa895b0SPatrick McHardy 
575dec18810SHerbert Xu 	skb_copy_secmark(new, old);
576dec18810SHerbert Xu }
577dec18810SHerbert Xu 
57882c49a35SHerbert Xu /*
57982c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
58082c49a35SHerbert Xu  * __copy_skb_header above instead.
58182c49a35SHerbert Xu  */
582e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds #define C(x) n->x = skb->x
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	n->next = n->prev = NULL;
5871da177e4SLinus Torvalds 	n->sk = NULL;
588dec18810SHerbert Xu 	__copy_skb_header(n, skb);
589dec18810SHerbert Xu 
5901da177e4SLinus Torvalds 	C(len);
5911da177e4SLinus Torvalds 	C(data_len);
5923e6b3b2eSAlexey Dobriyan 	C(mac_len);
593334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
59402f1c89dSPaul Moore 	n->cloned = 1;
5951da177e4SLinus Torvalds 	n->nohdr = 0;
5961da177e4SLinus Torvalds 	n->destructor = NULL;
5971da177e4SLinus Torvalds 	C(tail);
5981da177e4SLinus Torvalds 	C(end);
59902f1c89dSPaul Moore 	C(head);
60002f1c89dSPaul Moore 	C(data);
60102f1c89dSPaul Moore 	C(truesize);
60202f1c89dSPaul Moore 	atomic_set(&n->users, 1);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
6051da177e4SLinus Torvalds 	skb->cloned = 1;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	return n;
608e0053ec0SHerbert Xu #undef C
609e0053ec0SHerbert Xu }
610e0053ec0SHerbert Xu 
611e0053ec0SHerbert Xu /**
612e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
613e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
614e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
615e0053ec0SHerbert Xu  *
616e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
617e0053ec0SHerbert Xu  *	supplied by the user.
618e0053ec0SHerbert Xu  *
619e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
620e0053ec0SHerbert Xu  */
621e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
622e0053ec0SHerbert Xu {
6232d4baff8SHerbert Xu 	skb_release_all(dst);
624e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
625e0053ec0SHerbert Xu }
626e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
627e0053ec0SHerbert Xu 
62848c83012SMichael S. Tsirkin /*	skb_copy_ubufs	-	copy userspace skb frags buffers to kernel
62948c83012SMichael S. Tsirkin  *	@skb: the skb to modify
63048c83012SMichael S. Tsirkin  *	@gfp_mask: allocation priority
63148c83012SMichael S. Tsirkin  *
63248c83012SMichael S. Tsirkin  *	This must be called on SKBTX_DEV_ZEROCOPY skb.
63348c83012SMichael S. Tsirkin  *	It will copy all frags into kernel and drop the reference
63448c83012SMichael S. Tsirkin  *	to userspace pages.
63548c83012SMichael S. Tsirkin  *
63648c83012SMichael S. Tsirkin  *	If this function is called from an interrupt gfp_mask() must be
63748c83012SMichael S. Tsirkin  *	%GFP_ATOMIC.
63848c83012SMichael S. Tsirkin  *
63948c83012SMichael S. Tsirkin  *	Returns 0 on success or a negative error code on failure
64048c83012SMichael S. Tsirkin  *	to allocate kernel memory to copy to.
64148c83012SMichael S. Tsirkin  */
64248c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask)
643a6686f2fSShirley Ma {
644a6686f2fSShirley Ma 	int i;
645a6686f2fSShirley Ma 	int num_frags = skb_shinfo(skb)->nr_frags;
646a6686f2fSShirley Ma 	struct page *page, *head = NULL;
647a6686f2fSShirley Ma 	struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg;
648a6686f2fSShirley Ma 
649a6686f2fSShirley Ma 	for (i = 0; i < num_frags; i++) {
650a6686f2fSShirley Ma 		u8 *vaddr;
651a6686f2fSShirley Ma 		skb_frag_t *f = &skb_shinfo(skb)->frags[i];
652a6686f2fSShirley Ma 
653a6686f2fSShirley Ma 		page = alloc_page(GFP_ATOMIC);
654a6686f2fSShirley Ma 		if (!page) {
655a6686f2fSShirley Ma 			while (head) {
656a6686f2fSShirley Ma 				struct page *next = (struct page *)head->private;
657a6686f2fSShirley Ma 				put_page(head);
658a6686f2fSShirley Ma 				head = next;
659a6686f2fSShirley Ma 			}
660a6686f2fSShirley Ma 			return -ENOMEM;
661a6686f2fSShirley Ma 		}
662a6686f2fSShirley Ma 		vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
663a6686f2fSShirley Ma 		memcpy(page_address(page),
6649e903e08SEric Dumazet 		       vaddr + f->page_offset, skb_frag_size(f));
665a6686f2fSShirley Ma 		kunmap_skb_frag(vaddr);
666a6686f2fSShirley Ma 		page->private = (unsigned long)head;
667a6686f2fSShirley Ma 		head = page;
668a6686f2fSShirley Ma 	}
669a6686f2fSShirley Ma 
670a6686f2fSShirley Ma 	/* skb frags release userspace buffers */
671a6686f2fSShirley Ma 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
672a8605c60SIan Campbell 		skb_frag_unref(skb, i);
673a6686f2fSShirley Ma 
674a6686f2fSShirley Ma 	uarg->callback(uarg);
675a6686f2fSShirley Ma 
676a6686f2fSShirley Ma 	/* skb frags point to kernel buffers */
677a6686f2fSShirley Ma 	for (i = skb_shinfo(skb)->nr_frags; i > 0; i--) {
678a8605c60SIan Campbell 		__skb_fill_page_desc(skb, i-1, head, 0,
679a8605c60SIan Campbell 				     skb_shinfo(skb)->frags[i - 1].size);
680a6686f2fSShirley Ma 		head = (struct page *)head->private;
681a6686f2fSShirley Ma 	}
68248c83012SMichael S. Tsirkin 
68348c83012SMichael S. Tsirkin 	skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY;
684a6686f2fSShirley Ma 	return 0;
685a6686f2fSShirley Ma }
686a6686f2fSShirley Ma 
687a6686f2fSShirley Ma 
688e0053ec0SHerbert Xu /**
689e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
690e0053ec0SHerbert Xu  *	@skb: buffer to clone
691e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
692e0053ec0SHerbert Xu  *
693e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
694e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
695e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
696e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
697e0053ec0SHerbert Xu  *
698e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
699e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
700e0053ec0SHerbert Xu  */
701e0053ec0SHerbert Xu 
702e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
703e0053ec0SHerbert Xu {
704e0053ec0SHerbert Xu 	struct sk_buff *n;
705e0053ec0SHerbert Xu 
706a6686f2fSShirley Ma 	if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
707a6686f2fSShirley Ma 		if (skb_copy_ubufs(skb, gfp_mask))
708a6686f2fSShirley Ma 			return NULL;
709a6686f2fSShirley Ma 	}
710a6686f2fSShirley Ma 
711e0053ec0SHerbert Xu 	n = skb + 1;
712e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
713e0053ec0SHerbert Xu 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
714e0053ec0SHerbert Xu 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
715e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
716e0053ec0SHerbert Xu 		atomic_inc(fclone_ref);
717e0053ec0SHerbert Xu 	} else {
718e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
719e0053ec0SHerbert Xu 		if (!n)
720e0053ec0SHerbert Xu 			return NULL;
721fe55f6d5SVegard Nossum 
722fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags1);
723fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags2);
724e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
725e0053ec0SHerbert Xu 	}
726e0053ec0SHerbert Xu 
727e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
7281da177e4SLinus Torvalds }
729b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
7321da177e4SLinus Torvalds {
7332e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
7341da177e4SLinus Torvalds 	/*
7351da177e4SLinus Torvalds 	 *	Shift between the two data areas in bytes
7361da177e4SLinus Torvalds 	 */
7371da177e4SLinus Torvalds 	unsigned long offset = new->data - old->data;
7382e07fa9cSArnaldo Carvalho de Melo #endif
739dec18810SHerbert Xu 
740dec18810SHerbert Xu 	__copy_skb_header(new, old);
741dec18810SHerbert Xu 
7422e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
7432e07fa9cSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header are relative to skb->head */
7442e07fa9cSArnaldo Carvalho de Melo 	new->transport_header += offset;
7452e07fa9cSArnaldo Carvalho de Melo 	new->network_header   += offset;
746603a8bbeSStephen Hemminger 	if (skb_mac_header_was_set(new))
7472e07fa9cSArnaldo Carvalho de Melo 		new->mac_header	      += offset;
7482e07fa9cSArnaldo Carvalho de Melo #endif
7497967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
7507967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
7517967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /**
7551da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
7561da177e4SLinus Torvalds  *	@skb: buffer to copy
7571da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7581da177e4SLinus Torvalds  *
7591da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
7601da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
7611da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
7621da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
7631da177e4SLinus Torvalds  *
7641da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
7651da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
7661da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
7671da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
7681da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
7691da177e4SLinus Torvalds  */
7701da177e4SLinus Torvalds 
771dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
7721da177e4SLinus Torvalds {
7736602cebbSEric Dumazet 	int headerlen = skb_headroom(skb);
7746602cebbSEric Dumazet 	unsigned int size = (skb_end_pointer(skb) - skb->head) + skb->data_len;
7756602cebbSEric Dumazet 	struct sk_buff *n = alloc_skb(size, gfp_mask);
7766602cebbSEric Dumazet 
7771da177e4SLinus Torvalds 	if (!n)
7781da177e4SLinus Torvalds 		return NULL;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	/* Set the data pointer */
7811da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
7821da177e4SLinus Torvalds 	/* Set the tail pointer and length */
7831da177e4SLinus Torvalds 	skb_put(n, skb->len);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
7861da177e4SLinus Torvalds 		BUG();
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7891da177e4SLinus Torvalds 	return n;
7901da177e4SLinus Torvalds }
791b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds /**
7941da177e4SLinus Torvalds  *	pskb_copy	-	create copy of an sk_buff with private head.
7951da177e4SLinus Torvalds  *	@skb: buffer to copy
7961da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7971da177e4SLinus Torvalds  *
7981da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
7991da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
8001da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
8011da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
8021da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
8031da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
8041da177e4SLinus Torvalds  */
8051da177e4SLinus Torvalds 
806dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
8071da177e4SLinus Torvalds {
8086602cebbSEric Dumazet 	unsigned int size = skb_end_pointer(skb) - skb->head;
8096602cebbSEric Dumazet 	struct sk_buff *n = alloc_skb(size, gfp_mask);
8106602cebbSEric Dumazet 
8111da177e4SLinus Torvalds 	if (!n)
8121da177e4SLinus Torvalds 		goto out;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	/* Set the data pointer */
8156602cebbSEric Dumazet 	skb_reserve(n, skb_headroom(skb));
8161da177e4SLinus Torvalds 	/* Set the tail pointer and length */
8171da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
8181da177e4SLinus Torvalds 	/* Copy the bytes */
819d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
8201da177e4SLinus Torvalds 
82125f484a6SHerbert Xu 	n->truesize += skb->data_len;
8221da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
8231da177e4SLinus Torvalds 	n->len	     = skb->len;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
8261da177e4SLinus Torvalds 		int i;
8271da177e4SLinus Torvalds 
828a6686f2fSShirley Ma 		if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
829a6686f2fSShirley Ma 			if (skb_copy_ubufs(skb, gfp_mask)) {
8301511022cSDan Carpenter 				kfree_skb(n);
8311511022cSDan Carpenter 				n = NULL;
832a6686f2fSShirley Ma 				goto out;
833a6686f2fSShirley Ma 			}
834a6686f2fSShirley Ma 		}
8351da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
8361da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
837ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
8381da177e4SLinus Torvalds 		}
8391da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 
84221dc3301SDavid S. Miller 	if (skb_has_frag_list(skb)) {
8431da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
8441da177e4SLinus Torvalds 		skb_clone_fraglist(n);
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	copy_skb_header(n, skb);
8481da177e4SLinus Torvalds out:
8491da177e4SLinus Torvalds 	return n;
8501da177e4SLinus Torvalds }
851b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_copy);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds /**
8541da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
8551da177e4SLinus Torvalds  *	@skb: buffer to reallocate
8561da177e4SLinus Torvalds  *	@nhead: room to add at head
8571da177e4SLinus Torvalds  *	@ntail: room to add at tail
8581da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
8591da177e4SLinus Torvalds  *
8601da177e4SLinus Torvalds  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
8611da177e4SLinus Torvalds  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
8621da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
8631da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
8641da177e4SLinus Torvalds  *
8651da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
8661da177e4SLinus Torvalds  *	reloaded after call to this function.
8671da177e4SLinus Torvalds  */
8681da177e4SLinus Torvalds 
86986a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
870dd0fc66fSAl Viro 		     gfp_t gfp_mask)
8711da177e4SLinus Torvalds {
8721da177e4SLinus Torvalds 	int i;
8731da177e4SLinus Torvalds 	u8 *data;
8746602cebbSEric Dumazet 	int size = nhead + (skb_end_pointer(skb) - skb->head) + ntail;
8751da177e4SLinus Torvalds 	long off;
8761fd63041SEric Dumazet 	bool fastpath;
8771da177e4SLinus Torvalds 
8784edd87adSHerbert Xu 	BUG_ON(nhead < 0);
8794edd87adSHerbert Xu 
8801da177e4SLinus Torvalds 	if (skb_shared(skb))
8811da177e4SLinus Torvalds 		BUG();
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
8841da177e4SLinus Torvalds 
885ca44ac38SChangli Gao 	/* Check if we can avoid taking references on fragments if we own
886ca44ac38SChangli Gao 	 * the last reference on skb->head. (see skb_release_data())
887ca44ac38SChangli Gao 	 */
888ca44ac38SChangli Gao 	if (!skb->cloned)
889ca44ac38SChangli Gao 		fastpath = true;
890ca44ac38SChangli Gao 	else {
891ca44ac38SChangli Gao 		int delta = skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1;
892ca44ac38SChangli Gao 		fastpath = atomic_read(&skb_shinfo(skb)->dataref) == delta;
893ca44ac38SChangli Gao 	}
894ca44ac38SChangli Gao 
895ca44ac38SChangli Gao 	if (fastpath &&
896ca44ac38SChangli Gao 	    size + sizeof(struct skb_shared_info) <= ksize(skb->head)) {
897ca44ac38SChangli Gao 		memmove(skb->head + size, skb_shinfo(skb),
898ca44ac38SChangli Gao 			offsetof(struct skb_shared_info,
899ca44ac38SChangli Gao 				 frags[skb_shinfo(skb)->nr_frags]));
900ca44ac38SChangli Gao 		memmove(skb->head + nhead, skb->head,
901ca44ac38SChangli Gao 			skb_tail_pointer(skb) - skb->head);
902ca44ac38SChangli Gao 		off = nhead;
903ca44ac38SChangli Gao 		goto adjust_others;
904ca44ac38SChangli Gao 	}
905ca44ac38SChangli Gao 
9061da177e4SLinus Torvalds 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
9071da177e4SLinus Torvalds 	if (!data)
9081da177e4SLinus Torvalds 		goto nodata;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
9116602cebbSEric Dumazet 	 * optimized for the cases when header is void.
9126602cebbSEric Dumazet 	 */
9136602cebbSEric Dumazet 	memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head);
9146602cebbSEric Dumazet 
9156602cebbSEric Dumazet 	memcpy((struct skb_shared_info *)(data + size),
9166602cebbSEric Dumazet 	       skb_shinfo(skb),
917fed66381SEric Dumazet 	       offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags]));
9181da177e4SLinus Torvalds 
9191fd63041SEric Dumazet 	if (fastpath) {
9201fd63041SEric Dumazet 		kfree(skb->head);
9211fd63041SEric Dumazet 	} else {
922a6686f2fSShirley Ma 		/* copy this zero copy skb frags */
923a6686f2fSShirley Ma 		if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) {
924a6686f2fSShirley Ma 			if (skb_copy_ubufs(skb, gfp_mask))
925a6686f2fSShirley Ma 				goto nofrags;
926a6686f2fSShirley Ma 		}
9271da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
928ea2ab693SIan Campbell 			skb_frag_ref(skb, i);
9291da177e4SLinus Torvalds 
93021dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
9311da177e4SLinus Torvalds 			skb_clone_fraglist(skb);
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 		skb_release_data(skb);
9341fd63041SEric Dumazet 	}
9351da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	skb->head     = data;
938ca44ac38SChangli Gao adjust_others:
9391da177e4SLinus Torvalds 	skb->data    += off;
9404305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
9414305b541SArnaldo Carvalho de Melo 	skb->end      = size;
94256eb8882SPatrick McHardy 	off           = nhead;
9434305b541SArnaldo Carvalho de Melo #else
9444305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
94556eb8882SPatrick McHardy #endif
94627a884dcSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header and tail are relative to skb->head */
94727a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
948b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header += off;
949b0e380b1SArnaldo Carvalho de Melo 	skb->network_header   += off;
950603a8bbeSStephen Hemminger 	if (skb_mac_header_was_set(skb))
951b0e380b1SArnaldo Carvalho de Melo 		skb->mac_header += off;
95200c5a983SAndrea Shepard 	/* Only adjust this if it actually is csum_start rather than csum */
95300c5a983SAndrea Shepard 	if (skb->ip_summed == CHECKSUM_PARTIAL)
954172a863fSHerbert Xu 		skb->csum_start += nhead;
9551da177e4SLinus Torvalds 	skb->cloned   = 0;
956334a8132SPatrick McHardy 	skb->hdr_len  = 0;
9571da177e4SLinus Torvalds 	skb->nohdr    = 0;
9581da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
9591da177e4SLinus Torvalds 	return 0;
9601da177e4SLinus Torvalds 
961a6686f2fSShirley Ma nofrags:
962a6686f2fSShirley Ma 	kfree(data);
9631da177e4SLinus Torvalds nodata:
9641da177e4SLinus Torvalds 	return -ENOMEM;
9651da177e4SLinus Torvalds }
966b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
9711da177e4SLinus Torvalds {
9721da177e4SLinus Torvalds 	struct sk_buff *skb2;
9731da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	if (delta <= 0)
9761da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
9771da177e4SLinus Torvalds 	else {
9781da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
9791da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
9801da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
9811da177e4SLinus Torvalds 			kfree_skb(skb2);
9821da177e4SLinus Torvalds 			skb2 = NULL;
9831da177e4SLinus Torvalds 		}
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 	return skb2;
9861da177e4SLinus Torvalds }
987b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds /**
9901da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
9911da177e4SLinus Torvalds  *	@skb: buffer to copy
9921da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
9931da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
9941da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
9951da177e4SLinus Torvalds  *
9961da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
9971da177e4SLinus Torvalds  *	allocate additional space.
9981da177e4SLinus Torvalds  *
9991da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
10001da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
10011da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
10021da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
10031da177e4SLinus Torvalds  *
10041da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
10051da177e4SLinus Torvalds  *	is called from an interrupt.
10061da177e4SLinus Torvalds  */
10071da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
100886a76cafSVictor Fusco 				int newheadroom, int newtailroom,
1009dd0fc66fSAl Viro 				gfp_t gfp_mask)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	/*
10121da177e4SLinus Torvalds 	 *	Allocate the copy buffer
10131da177e4SLinus Torvalds 	 */
10141da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
10151da177e4SLinus Torvalds 				      gfp_mask);
1016efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
10171da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
101852886051SHerbert Xu 	int off;
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	if (!n)
10211da177e4SLinus Torvalds 		return NULL;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	/* Set the tail pointer and length */
10261da177e4SLinus Torvalds 	skb_put(n, skb->len);
10271da177e4SLinus Torvalds 
1028efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
10291da177e4SLinus Torvalds 	head_copy_off = 0;
10301da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
10311da177e4SLinus Torvalds 		head_copy_len = newheadroom;
10321da177e4SLinus Torvalds 	else
10331da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	/* Copy the linear header and data. */
10361da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
10371da177e4SLinus Torvalds 			  skb->len + head_copy_len))
10381da177e4SLinus Torvalds 		BUG();
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	copy_skb_header(n, skb);
10411da177e4SLinus Torvalds 
1042efd1e8d5SPatrick McHardy 	off                  = newheadroom - oldheadroom;
1043be2b6e62SDavid S. Miller 	if (n->ip_summed == CHECKSUM_PARTIAL)
104452886051SHerbert Xu 		n->csum_start += off;
104552886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET
1046efd1e8d5SPatrick McHardy 	n->transport_header += off;
1047efd1e8d5SPatrick McHardy 	n->network_header   += off;
1048603a8bbeSStephen Hemminger 	if (skb_mac_header_was_set(skb))
1049efd1e8d5SPatrick McHardy 		n->mac_header += off;
105052886051SHerbert Xu #endif
1051efd1e8d5SPatrick McHardy 
10521da177e4SLinus Torvalds 	return n;
10531da177e4SLinus Torvalds }
1054b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds /**
10571da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
10581da177e4SLinus Torvalds  *	@skb: buffer to pad
10591da177e4SLinus Torvalds  *	@pad: space to pad
10601da177e4SLinus Torvalds  *
10611da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
10621da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
10631da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
10641da177e4SLinus Torvalds  *
10655b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
10661da177e4SLinus Torvalds  */
10671da177e4SLinus Torvalds 
10685b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
10691da177e4SLinus Torvalds {
10705b057c6bSHerbert Xu 	int err;
10715b057c6bSHerbert Xu 	int ntail;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
10745b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
10751da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
10765b057c6bSHerbert Xu 		return 0;
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 
10794305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
10805b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
10815b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
10825b057c6bSHerbert Xu 		if (unlikely(err))
10835b057c6bSHerbert Xu 			goto free_skb;
10845b057c6bSHerbert Xu 	}
10855b057c6bSHerbert Xu 
10865b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
10875b057c6bSHerbert Xu 	 * to be audited.
10885b057c6bSHerbert Xu 	 */
10895b057c6bSHerbert Xu 	err = skb_linearize(skb);
10905b057c6bSHerbert Xu 	if (unlikely(err))
10915b057c6bSHerbert Xu 		goto free_skb;
10925b057c6bSHerbert Xu 
10935b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
10945b057c6bSHerbert Xu 	return 0;
10955b057c6bSHerbert Xu 
10965b057c6bSHerbert Xu free_skb:
10971da177e4SLinus Torvalds 	kfree_skb(skb);
10985b057c6bSHerbert Xu 	return err;
10991da177e4SLinus Torvalds }
1100b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
11011da177e4SLinus Torvalds 
11020dde3e16SIlpo Järvinen /**
11030dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
11040dde3e16SIlpo Järvinen  *	@skb: buffer to use
11050dde3e16SIlpo Järvinen  *	@len: amount of data to add
11060dde3e16SIlpo Järvinen  *
11070dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
11080dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
11090dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
11100dde3e16SIlpo Järvinen  */
11110dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
11120dde3e16SIlpo Järvinen {
11130dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
11140dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
11150dde3e16SIlpo Järvinen 	skb->tail += len;
11160dde3e16SIlpo Järvinen 	skb->len  += len;
11170dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
11180dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
11190dde3e16SIlpo Järvinen 	return tmp;
11200dde3e16SIlpo Järvinen }
11210dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
11220dde3e16SIlpo Järvinen 
11236be8ac2fSIlpo Järvinen /**
1124c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1125c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1126c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1127c2aa270aSIlpo Järvinen  *
1128c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1129c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1130c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1131c2aa270aSIlpo Järvinen  */
1132c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1133c2aa270aSIlpo Järvinen {
1134c2aa270aSIlpo Järvinen 	skb->data -= len;
1135c2aa270aSIlpo Järvinen 	skb->len  += len;
1136c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1137c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1138c2aa270aSIlpo Järvinen 	return skb->data;
1139c2aa270aSIlpo Järvinen }
1140c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1141c2aa270aSIlpo Järvinen 
1142c2aa270aSIlpo Järvinen /**
11436be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
11446be8ac2fSIlpo Järvinen  *	@skb: buffer to use
11456be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
11466be8ac2fSIlpo Järvinen  *
11476be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
11486be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
11496be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
11506be8ac2fSIlpo Järvinen  *	the old data.
11516be8ac2fSIlpo Järvinen  */
11526be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
11536be8ac2fSIlpo Järvinen {
115447d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
11556be8ac2fSIlpo Järvinen }
11566be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
11576be8ac2fSIlpo Järvinen 
1158419ae74eSIlpo Järvinen /**
1159419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1160419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1161419ae74eSIlpo Järvinen  *	@len: new length
1162419ae74eSIlpo Järvinen  *
1163419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1164419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1165419ae74eSIlpo Järvinen  *	The skb must be linear.
1166419ae74eSIlpo Järvinen  */
1167419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1168419ae74eSIlpo Järvinen {
1169419ae74eSIlpo Järvinen 	if (skb->len > len)
1170419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1171419ae74eSIlpo Järvinen }
1172419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1173419ae74eSIlpo Järvinen 
11743cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
11751da177e4SLinus Torvalds  */
11761da177e4SLinus Torvalds 
11773cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
11781da177e4SLinus Torvalds {
117927b437c8SHerbert Xu 	struct sk_buff **fragp;
118027b437c8SHerbert Xu 	struct sk_buff *frag;
11811da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
11821da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
11831da177e4SLinus Torvalds 	int i;
118427b437c8SHerbert Xu 	int err;
118527b437c8SHerbert Xu 
118627b437c8SHerbert Xu 	if (skb_cloned(skb) &&
118727b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
118827b437c8SHerbert Xu 		return err;
11891da177e4SLinus Torvalds 
1190f4d26fb3SHerbert Xu 	i = 0;
1191f4d26fb3SHerbert Xu 	if (offset >= len)
1192f4d26fb3SHerbert Xu 		goto drop_pages;
1193f4d26fb3SHerbert Xu 
1194f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
11959e903e08SEric Dumazet 		int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]);
119627b437c8SHerbert Xu 
119727b437c8SHerbert Xu 		if (end < len) {
11981da177e4SLinus Torvalds 			offset = end;
119927b437c8SHerbert Xu 			continue;
12001da177e4SLinus Torvalds 		}
12011da177e4SLinus Torvalds 
12029e903e08SEric Dumazet 		skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset);
120327b437c8SHerbert Xu 
1204f4d26fb3SHerbert Xu drop_pages:
120527b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
120627b437c8SHerbert Xu 
120727b437c8SHerbert Xu 		for (; i < nfrags; i++)
1208ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
120927b437c8SHerbert Xu 
121021dc3301SDavid S. Miller 		if (skb_has_frag_list(skb))
121127b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1212f4d26fb3SHerbert Xu 		goto done;
121327b437c8SHerbert Xu 	}
121427b437c8SHerbert Xu 
121527b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
121627b437c8SHerbert Xu 	     fragp = &frag->next) {
121727b437c8SHerbert Xu 		int end = offset + frag->len;
121827b437c8SHerbert Xu 
121927b437c8SHerbert Xu 		if (skb_shared(frag)) {
122027b437c8SHerbert Xu 			struct sk_buff *nfrag;
122127b437c8SHerbert Xu 
122227b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
122327b437c8SHerbert Xu 			if (unlikely(!nfrag))
122427b437c8SHerbert Xu 				return -ENOMEM;
122527b437c8SHerbert Xu 
122627b437c8SHerbert Xu 			nfrag->next = frag->next;
1227f4d26fb3SHerbert Xu 			kfree_skb(frag);
122827b437c8SHerbert Xu 			frag = nfrag;
122927b437c8SHerbert Xu 			*fragp = frag;
123027b437c8SHerbert Xu 		}
123127b437c8SHerbert Xu 
123227b437c8SHerbert Xu 		if (end < len) {
123327b437c8SHerbert Xu 			offset = end;
123427b437c8SHerbert Xu 			continue;
123527b437c8SHerbert Xu 		}
123627b437c8SHerbert Xu 
123727b437c8SHerbert Xu 		if (end > len &&
123827b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
123927b437c8SHerbert Xu 			return err;
124027b437c8SHerbert Xu 
124127b437c8SHerbert Xu 		if (frag->next)
124227b437c8SHerbert Xu 			skb_drop_list(&frag->next);
124327b437c8SHerbert Xu 		break;
124427b437c8SHerbert Xu 	}
124527b437c8SHerbert Xu 
1246f4d26fb3SHerbert Xu done:
124727b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
12481da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
12491da177e4SLinus Torvalds 		skb->len       = len;
12501da177e4SLinus Torvalds 	} else {
12511da177e4SLinus Torvalds 		skb->len       = len;
12521da177e4SLinus Torvalds 		skb->data_len  = 0;
125327a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	return 0;
12571da177e4SLinus Torvalds }
1258b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds /**
12611da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
12621da177e4SLinus Torvalds  *	@skb: buffer to reallocate
12631da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
12641da177e4SLinus Torvalds  *
12651da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
12661da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
12671da177e4SLinus Torvalds  *	data from fragmented part.
12681da177e4SLinus Torvalds  *
12691da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
12701da177e4SLinus Torvalds  *
12711da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
12721da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
12731da177e4SLinus Torvalds  *
12741da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
12751da177e4SLinus Torvalds  *	reloaded after call to this function.
12761da177e4SLinus Torvalds  */
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
12791da177e4SLinus Torvalds  * when it is necessary.
12801da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
12811da177e4SLinus Torvalds  * 2. It may change skb pointers.
12821da177e4SLinus Torvalds  *
12831da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
12841da177e4SLinus Torvalds  */
12851da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
12861da177e4SLinus Torvalds {
12871da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
12881da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
12891da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
12901da177e4SLinus Torvalds 	 */
12914305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
12941da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
12951da177e4SLinus Torvalds 				     GFP_ATOMIC))
12961da177e4SLinus Torvalds 			return NULL;
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 
129927a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
13001da177e4SLinus Torvalds 		BUG();
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
13031da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
13041da177e4SLinus Torvalds 	 */
130521dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb))
13061da177e4SLinus Torvalds 		goto pull_pages;
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
13091da177e4SLinus Torvalds 	eat = delta;
13101da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
13119e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
13129e903e08SEric Dumazet 
13139e903e08SEric Dumazet 		if (size >= eat)
13141da177e4SLinus Torvalds 			goto pull_pages;
13159e903e08SEric Dumazet 		eat -= size;
13161da177e4SLinus Torvalds 	}
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
13191da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
13201da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
13211da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
13221da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
13231da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
13241da177e4SLinus Torvalds 	 */
13251da177e4SLinus Torvalds 	if (eat) {
13261da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
13271da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
13281da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 		do {
133109a62660SKris Katterjohn 			BUG_ON(!list);
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 			if (list->len <= eat) {
13341da177e4SLinus Torvalds 				/* Eaten as whole. */
13351da177e4SLinus Torvalds 				eat -= list->len;
13361da177e4SLinus Torvalds 				list = list->next;
13371da177e4SLinus Torvalds 				insp = list;
13381da177e4SLinus Torvalds 			} else {
13391da177e4SLinus Torvalds 				/* Eaten partially. */
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 				if (skb_shared(list)) {
13421da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
13431da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
13441da177e4SLinus Torvalds 					if (!clone)
13451da177e4SLinus Torvalds 						return NULL;
13461da177e4SLinus Torvalds 					insp = list->next;
13471da177e4SLinus Torvalds 					list = clone;
13481da177e4SLinus Torvalds 				} else {
13491da177e4SLinus Torvalds 					/* This may be pulled without
13501da177e4SLinus Torvalds 					 * problems. */
13511da177e4SLinus Torvalds 					insp = list;
13521da177e4SLinus Torvalds 				}
13531da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
13541da177e4SLinus Torvalds 					kfree_skb(clone);
13551da177e4SLinus Torvalds 					return NULL;
13561da177e4SLinus Torvalds 				}
13571da177e4SLinus Torvalds 				break;
13581da177e4SLinus Torvalds 			}
13591da177e4SLinus Torvalds 		} while (eat);
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 		/* Free pulled out fragments. */
13621da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
13631da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
13641da177e4SLinus Torvalds 			kfree_skb(list);
13651da177e4SLinus Torvalds 		}
13661da177e4SLinus Torvalds 		/* And insert new clone at head. */
13671da177e4SLinus Torvalds 		if (clone) {
13681da177e4SLinus Torvalds 			clone->next = list;
13691da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
13701da177e4SLinus Torvalds 		}
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds pull_pages:
13751da177e4SLinus Torvalds 	eat = delta;
13761da177e4SLinus Torvalds 	k = 0;
13771da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
13789e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
13799e903e08SEric Dumazet 
13809e903e08SEric Dumazet 		if (size <= eat) {
1381ea2ab693SIan Campbell 			skb_frag_unref(skb, i);
13829e903e08SEric Dumazet 			eat -= size;
13831da177e4SLinus Torvalds 		} else {
13841da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
13851da177e4SLinus Torvalds 			if (eat) {
13861da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
13879e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat);
13881da177e4SLinus Torvalds 				eat = 0;
13891da177e4SLinus Torvalds 			}
13901da177e4SLinus Torvalds 			k++;
13911da177e4SLinus Torvalds 		}
13921da177e4SLinus Torvalds 	}
13931da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	skb->tail     += delta;
13961da177e4SLinus Torvalds 	skb->data_len -= delta;
13971da177e4SLinus Torvalds 
139827a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
13991da177e4SLinus Torvalds }
1400b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
14011da177e4SLinus Torvalds 
140222019b17SEric Dumazet /**
140322019b17SEric Dumazet  *	skb_copy_bits - copy bits from skb to kernel buffer
140422019b17SEric Dumazet  *	@skb: source skb
140522019b17SEric Dumazet  *	@offset: offset in source
140622019b17SEric Dumazet  *	@to: destination buffer
140722019b17SEric Dumazet  *	@len: number of bytes to copy
140822019b17SEric Dumazet  *
140922019b17SEric Dumazet  *	Copy the specified number of bytes from the source skb to the
141022019b17SEric Dumazet  *	destination buffer.
141122019b17SEric Dumazet  *
141222019b17SEric Dumazet  *	CAUTION ! :
141322019b17SEric Dumazet  *		If its prototype is ever changed,
141422019b17SEric Dumazet  *		check arch/{*}/net/{*}.S files,
141522019b17SEric Dumazet  *		since it is called from BPF assembly code.
141622019b17SEric Dumazet  */
14171da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
14181da177e4SLinus Torvalds {
14191a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1420fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1421fbb398a8SDavid S. Miller 	int i, copy;
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
14241da177e4SLinus Torvalds 		goto fault;
14251da177e4SLinus Torvalds 
14261da177e4SLinus Torvalds 	/* Copy header. */
14271a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
14281da177e4SLinus Torvalds 		if (copy > len)
14291da177e4SLinus Torvalds 			copy = len;
1430d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
14311da177e4SLinus Torvalds 		if ((len -= copy) == 0)
14321da177e4SLinus Torvalds 			return 0;
14331da177e4SLinus Torvalds 		offset += copy;
14341da177e4SLinus Torvalds 		to     += copy;
14351da177e4SLinus Torvalds 	}
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
14381a028e50SDavid S. Miller 		int end;
14391da177e4SLinus Torvalds 
1440547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
14411a028e50SDavid S. Miller 
14429e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
14431da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
14441da177e4SLinus Torvalds 			u8 *vaddr;
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds 			if (copy > len)
14471da177e4SLinus Torvalds 				copy = len;
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
14501da177e4SLinus Torvalds 			memcpy(to,
14511a028e50SDavid S. Miller 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
14521a028e50SDavid S. Miller 			       offset - start, copy);
14531da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 			if ((len -= copy) == 0)
14561da177e4SLinus Torvalds 				return 0;
14571da177e4SLinus Torvalds 			offset += copy;
14581da177e4SLinus Torvalds 			to     += copy;
14591da177e4SLinus Torvalds 		}
14601a028e50SDavid S. Miller 		start = end;
14611da177e4SLinus Torvalds 	}
14621da177e4SLinus Torvalds 
1463fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
14641a028e50SDavid S. Miller 		int end;
14651da177e4SLinus Torvalds 
1466547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
14671a028e50SDavid S. Miller 
1468fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
14691da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
14701da177e4SLinus Torvalds 			if (copy > len)
14711da177e4SLinus Torvalds 				copy = len;
1472fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
14731da177e4SLinus Torvalds 				goto fault;
14741da177e4SLinus Torvalds 			if ((len -= copy) == 0)
14751da177e4SLinus Torvalds 				return 0;
14761da177e4SLinus Torvalds 			offset += copy;
14771da177e4SLinus Torvalds 			to     += copy;
14781da177e4SLinus Torvalds 		}
14791a028e50SDavid S. Miller 		start = end;
14801da177e4SLinus Torvalds 	}
1481a6686f2fSShirley Ma 
14821da177e4SLinus Torvalds 	if (!len)
14831da177e4SLinus Torvalds 		return 0;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds fault:
14861da177e4SLinus Torvalds 	return -EFAULT;
14871da177e4SLinus Torvalds }
1488b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
14891da177e4SLinus Torvalds 
14909c55e01cSJens Axboe /*
14919c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
14929c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
14939c55e01cSJens Axboe  */
14949c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
14959c55e01cSJens Axboe {
14968b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
14978b9d3728SJarek Poplawski }
14989c55e01cSJens Axboe 
14994fb66994SJarek Poplawski static inline struct page *linear_to_page(struct page *page, unsigned int *len,
15004fb66994SJarek Poplawski 					  unsigned int *offset,
15017a67e56fSJarek Poplawski 					  struct sk_buff *skb, struct sock *sk)
15028b9d3728SJarek Poplawski {
15034fb66994SJarek Poplawski 	struct page *p = sk->sk_sndmsg_page;
15044fb66994SJarek Poplawski 	unsigned int off;
15058b9d3728SJarek Poplawski 
15064fb66994SJarek Poplawski 	if (!p) {
15074fb66994SJarek Poplawski new_page:
15084fb66994SJarek Poplawski 		p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
15098b9d3728SJarek Poplawski 		if (!p)
15108b9d3728SJarek Poplawski 			return NULL;
15114fb66994SJarek Poplawski 
15124fb66994SJarek Poplawski 		off = sk->sk_sndmsg_off = 0;
15134fb66994SJarek Poplawski 		/* hold one ref to this page until it's full */
15144fb66994SJarek Poplawski 	} else {
15154fb66994SJarek Poplawski 		unsigned int mlen;
15164fb66994SJarek Poplawski 
15174fb66994SJarek Poplawski 		off = sk->sk_sndmsg_off;
15184fb66994SJarek Poplawski 		mlen = PAGE_SIZE - off;
15194fb66994SJarek Poplawski 		if (mlen < 64 && mlen < *len) {
15204fb66994SJarek Poplawski 			put_page(p);
15214fb66994SJarek Poplawski 			goto new_page;
15224fb66994SJarek Poplawski 		}
15234fb66994SJarek Poplawski 
15244fb66994SJarek Poplawski 		*len = min_t(unsigned int, *len, mlen);
15254fb66994SJarek Poplawski 	}
15264fb66994SJarek Poplawski 
15274fb66994SJarek Poplawski 	memcpy(page_address(p) + off, page_address(page) + *offset, *len);
15284fb66994SJarek Poplawski 	sk->sk_sndmsg_off += *len;
15294fb66994SJarek Poplawski 	*offset = off;
15304fb66994SJarek Poplawski 	get_page(p);
15318b9d3728SJarek Poplawski 
15328b9d3728SJarek Poplawski 	return p;
15339c55e01cSJens Axboe }
15349c55e01cSJens Axboe 
15359c55e01cSJens Axboe /*
15369c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
15379c55e01cSJens Axboe  */
153835f3d14dSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd,
153935f3d14dSJens Axboe 				struct pipe_inode_info *pipe, struct page *page,
15404fb66994SJarek Poplawski 				unsigned int *len, unsigned int offset,
15417a67e56fSJarek Poplawski 				struct sk_buff *skb, int linear,
15427a67e56fSJarek Poplawski 				struct sock *sk)
15439c55e01cSJens Axboe {
154435f3d14dSJens Axboe 	if (unlikely(spd->nr_pages == pipe->buffers))
15459c55e01cSJens Axboe 		return 1;
15469c55e01cSJens Axboe 
15478b9d3728SJarek Poplawski 	if (linear) {
15487a67e56fSJarek Poplawski 		page = linear_to_page(page, len, &offset, skb, sk);
15498b9d3728SJarek Poplawski 		if (!page)
15508b9d3728SJarek Poplawski 			return 1;
15518b9d3728SJarek Poplawski 	} else
15528b9d3728SJarek Poplawski 		get_page(page);
15538b9d3728SJarek Poplawski 
15549c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
15554fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
15569c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
15579c55e01cSJens Axboe 	spd->nr_pages++;
15588b9d3728SJarek Poplawski 
15599c55e01cSJens Axboe 	return 0;
15609c55e01cSJens Axboe }
15619c55e01cSJens Axboe 
15622870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff,
15632870c43dSOctavian Purdila 				  unsigned int *plen, unsigned int off)
15642870c43dSOctavian Purdila {
1565ce3dd395SJarek Poplawski 	unsigned long n;
1566ce3dd395SJarek Poplawski 
15672870c43dSOctavian Purdila 	*poff += off;
1568ce3dd395SJarek Poplawski 	n = *poff / PAGE_SIZE;
1569ce3dd395SJarek Poplawski 	if (n)
1570ce3dd395SJarek Poplawski 		*page = nth_page(*page, n);
1571ce3dd395SJarek Poplawski 
15722870c43dSOctavian Purdila 	*poff = *poff % PAGE_SIZE;
15732870c43dSOctavian Purdila 	*plen -= off;
15742870c43dSOctavian Purdila }
15752870c43dSOctavian Purdila 
15762870c43dSOctavian Purdila static inline int __splice_segment(struct page *page, unsigned int poff,
15772870c43dSOctavian Purdila 				   unsigned int plen, unsigned int *off,
15782870c43dSOctavian Purdila 				   unsigned int *len, struct sk_buff *skb,
15797a67e56fSJarek Poplawski 				   struct splice_pipe_desc *spd, int linear,
158035f3d14dSJens Axboe 				   struct sock *sk,
158135f3d14dSJens Axboe 				   struct pipe_inode_info *pipe)
15829c55e01cSJens Axboe {
15832870c43dSOctavian Purdila 	if (!*len)
15842870c43dSOctavian Purdila 		return 1;
15859c55e01cSJens Axboe 
15862870c43dSOctavian Purdila 	/* skip this segment if already processed */
15872870c43dSOctavian Purdila 	if (*off >= plen) {
15882870c43dSOctavian Purdila 		*off -= plen;
15892870c43dSOctavian Purdila 		return 0;
15902870c43dSOctavian Purdila 	}
15912870c43dSOctavian Purdila 
15922870c43dSOctavian Purdila 	/* ignore any bits we already processed */
15932870c43dSOctavian Purdila 	if (*off) {
15942870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, *off);
15952870c43dSOctavian Purdila 		*off = 0;
15962870c43dSOctavian Purdila 	}
15972870c43dSOctavian Purdila 
15982870c43dSOctavian Purdila 	do {
15992870c43dSOctavian Purdila 		unsigned int flen = min(*len, plen);
16002870c43dSOctavian Purdila 
16012870c43dSOctavian Purdila 		/* the linear region may spread across several pages  */
16022870c43dSOctavian Purdila 		flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
16032870c43dSOctavian Purdila 
160435f3d14dSJens Axboe 		if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk))
16052870c43dSOctavian Purdila 			return 1;
16062870c43dSOctavian Purdila 
16072870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, flen);
16082870c43dSOctavian Purdila 		*len -= flen;
16092870c43dSOctavian Purdila 
16102870c43dSOctavian Purdila 	} while (*len && plen);
16112870c43dSOctavian Purdila 
16122870c43dSOctavian Purdila 	return 0;
1613db43a282SOctavian Purdila }
16149c55e01cSJens Axboe 
16159c55e01cSJens Axboe /*
16162870c43dSOctavian Purdila  * Map linear and fragment data from the skb to spd. It reports failure if the
16172870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
16189c55e01cSJens Axboe  */
161935f3d14dSJens Axboe static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe,
162035f3d14dSJens Axboe 			     unsigned int *offset, unsigned int *len,
162135f3d14dSJens Axboe 			     struct splice_pipe_desc *spd, struct sock *sk)
16222870c43dSOctavian Purdila {
16232870c43dSOctavian Purdila 	int seg;
16249c55e01cSJens Axboe 
16259c55e01cSJens Axboe 	/*
16262870c43dSOctavian Purdila 	 * map the linear part
16279c55e01cSJens Axboe 	 */
16282870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
16292870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
16302870c43dSOctavian Purdila 			     skb_headlen(skb),
163135f3d14dSJens Axboe 			     offset, len, skb, spd, 1, sk, pipe))
16322870c43dSOctavian Purdila 		return 1;
16339c55e01cSJens Axboe 
16349c55e01cSJens Axboe 	/*
16359c55e01cSJens Axboe 	 * then map the fragments
16369c55e01cSJens Axboe 	 */
16379c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
16389c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
16399c55e01cSJens Axboe 
1640ea2ab693SIan Campbell 		if (__splice_segment(skb_frag_page(f),
16419e903e08SEric Dumazet 				     f->page_offset, skb_frag_size(f),
164235f3d14dSJens Axboe 				     offset, len, skb, spd, 0, sk, pipe))
16432870c43dSOctavian Purdila 			return 1;
16449c55e01cSJens Axboe 	}
16459c55e01cSJens Axboe 
16469c55e01cSJens Axboe 	return 0;
16479c55e01cSJens Axboe }
16489c55e01cSJens Axboe 
16499c55e01cSJens Axboe /*
16509c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
16519c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
16529c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
16539c55e01cSJens Axboe  * handle that cleanly.
16549c55e01cSJens Axboe  */
16558b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
16569c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
16579c55e01cSJens Axboe 		    unsigned int flags)
16589c55e01cSJens Axboe {
165935f3d14dSJens Axboe 	struct partial_page partial[PIPE_DEF_BUFFERS];
166035f3d14dSJens Axboe 	struct page *pages[PIPE_DEF_BUFFERS];
16619c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
16629c55e01cSJens Axboe 		.pages = pages,
16639c55e01cSJens Axboe 		.partial = partial,
16649c55e01cSJens Axboe 		.flags = flags,
16659c55e01cSJens Axboe 		.ops = &sock_pipe_buf_ops,
16669c55e01cSJens Axboe 		.spd_release = sock_spd_release,
16679c55e01cSJens Axboe 	};
1668fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
16697a67e56fSJarek Poplawski 	struct sock *sk = skb->sk;
167035f3d14dSJens Axboe 	int ret = 0;
167135f3d14dSJens Axboe 
167235f3d14dSJens Axboe 	if (splice_grow_spd(pipe, &spd))
167335f3d14dSJens Axboe 		return -ENOMEM;
16749c55e01cSJens Axboe 
16759c55e01cSJens Axboe 	/*
16769c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
16779c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
16789c55e01cSJens Axboe 	 */
167935f3d14dSJens Axboe 	if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk))
16809c55e01cSJens Axboe 		goto done;
16819c55e01cSJens Axboe 	else if (!tlen)
16829c55e01cSJens Axboe 		goto done;
16839c55e01cSJens Axboe 
16849c55e01cSJens Axboe 	/*
16859c55e01cSJens Axboe 	 * now see if we have a frag_list to map
16869c55e01cSJens Axboe 	 */
1687fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
1688fbb398a8SDavid S. Miller 		if (!tlen)
16899c55e01cSJens Axboe 			break;
169035f3d14dSJens Axboe 		if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk))
1691fbb398a8SDavid S. Miller 			break;
16929c55e01cSJens Axboe 	}
16939c55e01cSJens Axboe 
16949c55e01cSJens Axboe done:
16959c55e01cSJens Axboe 	if (spd.nr_pages) {
16969c55e01cSJens Axboe 		/*
16979c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
16989c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
16999c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
17009c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
17019c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
17029c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
17039c55e01cSJens Axboe 		 * and networking will grab the socket lock.
17049c55e01cSJens Axboe 		 */
1705293ad604SOctavian Purdila 		release_sock(sk);
17069c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1707293ad604SOctavian Purdila 		lock_sock(sk);
17089c55e01cSJens Axboe 	}
17099c55e01cSJens Axboe 
171035f3d14dSJens Axboe 	splice_shrink_spd(pipe, &spd);
171135f3d14dSJens Axboe 	return ret;
17129c55e01cSJens Axboe }
17139c55e01cSJens Axboe 
1714357b40a1SHerbert Xu /**
1715357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1716357b40a1SHerbert Xu  *	@skb: destination buffer
1717357b40a1SHerbert Xu  *	@offset: offset in destination
1718357b40a1SHerbert Xu  *	@from: source buffer
1719357b40a1SHerbert Xu  *	@len: number of bytes to copy
1720357b40a1SHerbert Xu  *
1721357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1722357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1723357b40a1SHerbert Xu  *	traversing fragment lists and such.
1724357b40a1SHerbert Xu  */
1725357b40a1SHerbert Xu 
17260c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1727357b40a1SHerbert Xu {
17281a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1729fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1730fbb398a8SDavid S. Miller 	int i, copy;
1731357b40a1SHerbert Xu 
1732357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1733357b40a1SHerbert Xu 		goto fault;
1734357b40a1SHerbert Xu 
17351a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1736357b40a1SHerbert Xu 		if (copy > len)
1737357b40a1SHerbert Xu 			copy = len;
173827d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1739357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1740357b40a1SHerbert Xu 			return 0;
1741357b40a1SHerbert Xu 		offset += copy;
1742357b40a1SHerbert Xu 		from += copy;
1743357b40a1SHerbert Xu 	}
1744357b40a1SHerbert Xu 
1745357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1746357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
17471a028e50SDavid S. Miller 		int end;
1748357b40a1SHerbert Xu 
1749547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17501a028e50SDavid S. Miller 
17519e903e08SEric Dumazet 		end = start + skb_frag_size(frag);
1752357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1753357b40a1SHerbert Xu 			u8 *vaddr;
1754357b40a1SHerbert Xu 
1755357b40a1SHerbert Xu 			if (copy > len)
1756357b40a1SHerbert Xu 				copy = len;
1757357b40a1SHerbert Xu 
1758357b40a1SHerbert Xu 			vaddr = kmap_skb_frag(frag);
17591a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
17601a028e50SDavid S. Miller 			       from, copy);
1761357b40a1SHerbert Xu 			kunmap_skb_frag(vaddr);
1762357b40a1SHerbert Xu 
1763357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1764357b40a1SHerbert Xu 				return 0;
1765357b40a1SHerbert Xu 			offset += copy;
1766357b40a1SHerbert Xu 			from += copy;
1767357b40a1SHerbert Xu 		}
17681a028e50SDavid S. Miller 		start = end;
1769357b40a1SHerbert Xu 	}
1770357b40a1SHerbert Xu 
1771fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
17721a028e50SDavid S. Miller 		int end;
1773357b40a1SHerbert Xu 
1774547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17751a028e50SDavid S. Miller 
1776fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
1777357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1778357b40a1SHerbert Xu 			if (copy > len)
1779357b40a1SHerbert Xu 				copy = len;
1780fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
17811a028e50SDavid S. Miller 					   from, copy))
1782357b40a1SHerbert Xu 				goto fault;
1783357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1784357b40a1SHerbert Xu 				return 0;
1785357b40a1SHerbert Xu 			offset += copy;
1786357b40a1SHerbert Xu 			from += copy;
1787357b40a1SHerbert Xu 		}
17881a028e50SDavid S. Miller 		start = end;
1789357b40a1SHerbert Xu 	}
1790357b40a1SHerbert Xu 	if (!len)
1791357b40a1SHerbert Xu 		return 0;
1792357b40a1SHerbert Xu 
1793357b40a1SHerbert Xu fault:
1794357b40a1SHerbert Xu 	return -EFAULT;
1795357b40a1SHerbert Xu }
1796357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1797357b40a1SHerbert Xu 
17981da177e4SLinus Torvalds /* Checksum skb data. */
17991da177e4SLinus Torvalds 
18002bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset,
18012bbbc868SAl Viro 			  int len, __wsum csum)
18021da177e4SLinus Torvalds {
18031a028e50SDavid S. Miller 	int start = skb_headlen(skb);
18041a028e50SDavid S. Miller 	int i, copy = start - offset;
1805fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
18061da177e4SLinus Torvalds 	int pos = 0;
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	/* Checksum header. */
18091da177e4SLinus Torvalds 	if (copy > 0) {
18101da177e4SLinus Torvalds 		if (copy > len)
18111da177e4SLinus Torvalds 			copy = len;
18121da177e4SLinus Torvalds 		csum = csum_partial(skb->data + offset, copy, csum);
18131da177e4SLinus Torvalds 		if ((len -= copy) == 0)
18141da177e4SLinus Torvalds 			return csum;
18151da177e4SLinus Torvalds 		offset += copy;
18161da177e4SLinus Torvalds 		pos	= copy;
18171da177e4SLinus Torvalds 	}
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
18201a028e50SDavid S. Miller 		int end;
18211da177e4SLinus Torvalds 
1822547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
18231a028e50SDavid S. Miller 
18249e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
18251da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
182644bb9363SAl Viro 			__wsum csum2;
18271da177e4SLinus Torvalds 			u8 *vaddr;
18281da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 			if (copy > len)
18311da177e4SLinus Torvalds 				copy = len;
18321da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
18331a028e50SDavid S. Miller 			csum2 = csum_partial(vaddr + frag->page_offset +
18341a028e50SDavid S. Miller 					     offset - start, copy, 0);
18351da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
18361da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
18371da177e4SLinus Torvalds 			if (!(len -= copy))
18381da177e4SLinus Torvalds 				return csum;
18391da177e4SLinus Torvalds 			offset += copy;
18401da177e4SLinus Torvalds 			pos    += copy;
18411da177e4SLinus Torvalds 		}
18421a028e50SDavid S. Miller 		start = end;
18431da177e4SLinus Torvalds 	}
18441da177e4SLinus Torvalds 
1845fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
18461a028e50SDavid S. Miller 		int end;
18471da177e4SLinus Torvalds 
1848547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
18491a028e50SDavid S. Miller 
1850fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
18511da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
18525f92a738SAl Viro 			__wsum csum2;
18531da177e4SLinus Torvalds 			if (copy > len)
18541da177e4SLinus Torvalds 				copy = len;
1855fbb398a8SDavid S. Miller 			csum2 = skb_checksum(frag_iter, offset - start,
18561a028e50SDavid S. Miller 					     copy, 0);
18571da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
18581da177e4SLinus Torvalds 			if ((len -= copy) == 0)
18591da177e4SLinus Torvalds 				return csum;
18601da177e4SLinus Torvalds 			offset += copy;
18611da177e4SLinus Torvalds 			pos    += copy;
18621da177e4SLinus Torvalds 		}
18631a028e50SDavid S. Miller 		start = end;
18641da177e4SLinus Torvalds 	}
186509a62660SKris Katterjohn 	BUG_ON(len);
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	return csum;
18681da177e4SLinus Torvalds }
1869b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds /* Both of above in one bottle. */
18721da177e4SLinus Torvalds 
187381d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
187481d77662SAl Viro 				    u8 *to, int len, __wsum csum)
18751da177e4SLinus Torvalds {
18761a028e50SDavid S. Miller 	int start = skb_headlen(skb);
18771a028e50SDavid S. Miller 	int i, copy = start - offset;
1878fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
18791da177e4SLinus Torvalds 	int pos = 0;
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	/* Copy header. */
18821da177e4SLinus Torvalds 	if (copy > 0) {
18831da177e4SLinus Torvalds 		if (copy > len)
18841da177e4SLinus Torvalds 			copy = len;
18851da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
18861da177e4SLinus Torvalds 						 copy, csum);
18871da177e4SLinus Torvalds 		if ((len -= copy) == 0)
18881da177e4SLinus Torvalds 			return csum;
18891da177e4SLinus Torvalds 		offset += copy;
18901da177e4SLinus Torvalds 		to     += copy;
18911da177e4SLinus Torvalds 		pos	= copy;
18921da177e4SLinus Torvalds 	}
18931da177e4SLinus Torvalds 
18941da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
18951a028e50SDavid S. Miller 		int end;
18961da177e4SLinus Torvalds 
1897547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
18981a028e50SDavid S. Miller 
18999e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
19001da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
19015084205fSAl Viro 			__wsum csum2;
19021da177e4SLinus Torvalds 			u8 *vaddr;
19031da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds 			if (copy > len)
19061da177e4SLinus Torvalds 				copy = len;
19071da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
19081da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
19091a028e50SDavid S. Miller 							  frag->page_offset +
19101a028e50SDavid S. Miller 							  offset - start, to,
19111a028e50SDavid S. Miller 							  copy, 0);
19121da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
19131da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
19141da177e4SLinus Torvalds 			if (!(len -= copy))
19151da177e4SLinus Torvalds 				return csum;
19161da177e4SLinus Torvalds 			offset += copy;
19171da177e4SLinus Torvalds 			to     += copy;
19181da177e4SLinus Torvalds 			pos    += copy;
19191da177e4SLinus Torvalds 		}
19201a028e50SDavid S. Miller 		start = end;
19211da177e4SLinus Torvalds 	}
19221da177e4SLinus Torvalds 
1923fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
192481d77662SAl Viro 		__wsum csum2;
19251a028e50SDavid S. Miller 		int end;
19261da177e4SLinus Torvalds 
1927547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
19281a028e50SDavid S. Miller 
1929fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
19301da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
19311da177e4SLinus Torvalds 			if (copy > len)
19321da177e4SLinus Torvalds 				copy = len;
1933fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
19341a028e50SDavid S. Miller 						       offset - start,
19351da177e4SLinus Torvalds 						       to, copy, 0);
19361da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
19371da177e4SLinus Torvalds 			if ((len -= copy) == 0)
19381da177e4SLinus Torvalds 				return csum;
19391da177e4SLinus Torvalds 			offset += copy;
19401da177e4SLinus Torvalds 			to     += copy;
19411da177e4SLinus Torvalds 			pos    += copy;
19421da177e4SLinus Torvalds 		}
19431a028e50SDavid S. Miller 		start = end;
19441da177e4SLinus Torvalds 	}
194509a62660SKris Katterjohn 	BUG_ON(len);
19461da177e4SLinus Torvalds 	return csum;
19471da177e4SLinus Torvalds }
1948b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
19491da177e4SLinus Torvalds 
19501da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
19511da177e4SLinus Torvalds {
1952d3bc23e7SAl Viro 	__wsum csum;
19531da177e4SLinus Torvalds 	long csstart;
19541da177e4SLinus Torvalds 
195584fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
195655508d60SMichał Mirosław 		csstart = skb_checksum_start_offset(skb);
19571da177e4SLinus Torvalds 	else
19581da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
19591da177e4SLinus Torvalds 
196009a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
19611da177e4SLinus Torvalds 
1962d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds 	csum = 0;
19651da177e4SLinus Torvalds 	if (csstart != skb->len)
19661da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
19671da177e4SLinus Torvalds 					      skb->len - csstart, 0);
19681da177e4SLinus Torvalds 
196984fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1970ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
19711da177e4SLinus Torvalds 
1972d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
19731da177e4SLinus Torvalds 	}
19741da177e4SLinus Torvalds }
1975b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds /**
19781da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
19791da177e4SLinus Torvalds  *	@list: list to dequeue from
19801da177e4SLinus Torvalds  *
19811da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
19821da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
19831da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
19841da177e4SLinus Torvalds  */
19851da177e4SLinus Torvalds 
19861da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
19871da177e4SLinus Torvalds {
19881da177e4SLinus Torvalds 	unsigned long flags;
19891da177e4SLinus Torvalds 	struct sk_buff *result;
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
19921da177e4SLinus Torvalds 	result = __skb_dequeue(list);
19931da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19941da177e4SLinus Torvalds 	return result;
19951da177e4SLinus Torvalds }
1996b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds /**
19991da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
20001da177e4SLinus Torvalds  *	@list: list to dequeue from
20011da177e4SLinus Torvalds  *
20021da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
20031da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
20041da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
20051da177e4SLinus Torvalds  */
20061da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
20071da177e4SLinus Torvalds {
20081da177e4SLinus Torvalds 	unsigned long flags;
20091da177e4SLinus Torvalds 	struct sk_buff *result;
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
20121da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
20131da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
20141da177e4SLinus Torvalds 	return result;
20151da177e4SLinus Torvalds }
2016b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds /**
20191da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
20201da177e4SLinus Torvalds  *	@list: list to empty
20211da177e4SLinus Torvalds  *
20221da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
20231da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
20241da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
20251da177e4SLinus Torvalds  */
20261da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
20271da177e4SLinus Torvalds {
20281da177e4SLinus Torvalds 	struct sk_buff *skb;
20291da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
20301da177e4SLinus Torvalds 		kfree_skb(skb);
20311da177e4SLinus Torvalds }
2032b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
20331da177e4SLinus Torvalds 
20341da177e4SLinus Torvalds /**
20351da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
20361da177e4SLinus Torvalds  *	@list: list to use
20371da177e4SLinus Torvalds  *	@newsk: buffer to queue
20381da177e4SLinus Torvalds  *
20391da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
20401da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
20411da177e4SLinus Torvalds  *	safely.
20421da177e4SLinus Torvalds  *
20431da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
20441da177e4SLinus Torvalds  */
20451da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
20461da177e4SLinus Torvalds {
20471da177e4SLinus Torvalds 	unsigned long flags;
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
20501da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
20511da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
20521da177e4SLinus Torvalds }
2053b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds /**
20561da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
20571da177e4SLinus Torvalds  *	@list: list to use
20581da177e4SLinus Torvalds  *	@newsk: buffer to queue
20591da177e4SLinus Torvalds  *
20601da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
20611da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
20621da177e4SLinus Torvalds  *	safely.
20631da177e4SLinus Torvalds  *
20641da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
20651da177e4SLinus Torvalds  */
20661da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
20671da177e4SLinus Torvalds {
20681da177e4SLinus Torvalds 	unsigned long flags;
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
20711da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
20721da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
20731da177e4SLinus Torvalds }
2074b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
20758728b834SDavid S. Miller 
20761da177e4SLinus Torvalds /**
20771da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
20781da177e4SLinus Torvalds  *	@skb: buffer to remove
20798728b834SDavid S. Miller  *	@list: list to use
20801da177e4SLinus Torvalds  *
20818728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
20828728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
20831da177e4SLinus Torvalds  *
20848728b834SDavid S. Miller  *	You must know what list the SKB is on.
20851da177e4SLinus Torvalds  */
20868728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
20871da177e4SLinus Torvalds {
20881da177e4SLinus Torvalds 	unsigned long flags;
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
20918728b834SDavid S. Miller 	__skb_unlink(skb, list);
20921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
20931da177e4SLinus Torvalds }
2094b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds /**
20971da177e4SLinus Torvalds  *	skb_append	-	append a buffer
20981da177e4SLinus Torvalds  *	@old: buffer to insert after
20991da177e4SLinus Torvalds  *	@newsk: buffer to insert
21008728b834SDavid S. Miller  *	@list: list to use
21011da177e4SLinus Torvalds  *
21021da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
21031da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
21041da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
21051da177e4SLinus Torvalds  */
21068728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
21071da177e4SLinus Torvalds {
21081da177e4SLinus Torvalds 	unsigned long flags;
21091da177e4SLinus Torvalds 
21108728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
21117de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
21128728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
21131da177e4SLinus Torvalds }
2114b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds /**
21171da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
21181da177e4SLinus Torvalds  *	@old: buffer to insert before
21191da177e4SLinus Torvalds  *	@newsk: buffer to insert
21208728b834SDavid S. Miller  *	@list: list to use
21211da177e4SLinus Torvalds  *
21228728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
21238728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
21248728b834SDavid S. Miller  *	calls.
21258728b834SDavid S. Miller  *
21261da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
21271da177e4SLinus Torvalds  */
21288728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
21291da177e4SLinus Torvalds {
21301da177e4SLinus Torvalds 	unsigned long flags;
21311da177e4SLinus Torvalds 
21328728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
21338728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
21348728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
21351da177e4SLinus Torvalds }
2136b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
21391da177e4SLinus Torvalds 					   struct sk_buff* skb1,
21401da177e4SLinus Torvalds 					   const u32 len, const int pos)
21411da177e4SLinus Torvalds {
21421da177e4SLinus Torvalds 	int i;
21431da177e4SLinus Torvalds 
2144d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2145d626f62bSArnaldo Carvalho de Melo 					 pos - len);
21461da177e4SLinus Torvalds 	/* And move data appendix as is. */
21471da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
21481da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
21511da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
21521da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
21531da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
21541da177e4SLinus Torvalds 	skb->data_len		   = 0;
21551da177e4SLinus Torvalds 	skb->len		   = len;
215627a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
21571da177e4SLinus Torvalds }
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
21601da177e4SLinus Torvalds 				       struct sk_buff* skb1,
21611da177e4SLinus Torvalds 				       const u32 len, int pos)
21621da177e4SLinus Torvalds {
21631da177e4SLinus Torvalds 	int i, k = 0;
21641da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
21671da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
21681da177e4SLinus Torvalds 	skb->len		  = len;
21691da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
21729e903e08SEric Dumazet 		int size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds 		if (pos + size > len) {
21751da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
21761da177e4SLinus Torvalds 
21771da177e4SLinus Torvalds 			if (pos < len) {
21781da177e4SLinus Torvalds 				/* Split frag.
21791da177e4SLinus Torvalds 				 * We have two variants in this case:
21801da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
21811da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
21821da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
21831da177e4SLinus Torvalds 				 *    where splitting is expensive.
21841da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
21851da177e4SLinus Torvalds 				 */
2186ea2ab693SIan Campbell 				skb_frag_ref(skb, i);
21871da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
21889e903e08SEric Dumazet 				skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos);
21899e903e08SEric Dumazet 				skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos);
21901da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
21911da177e4SLinus Torvalds 			}
21921da177e4SLinus Torvalds 			k++;
21931da177e4SLinus Torvalds 		} else
21941da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
21951da177e4SLinus Torvalds 		pos += size;
21961da177e4SLinus Torvalds 	}
21971da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
21981da177e4SLinus Torvalds }
21991da177e4SLinus Torvalds 
22001da177e4SLinus Torvalds /**
22011da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
22021da177e4SLinus Torvalds  * @skb: the buffer to split
22031da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
22041da177e4SLinus Torvalds  * @len: new length for skb
22051da177e4SLinus Torvalds  */
22061da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
22071da177e4SLinus Torvalds {
22081da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
22091da177e4SLinus Torvalds 
22101da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
22111da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
22121da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
22131da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
22141da177e4SLinus Torvalds }
2215b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
22161da177e4SLinus Torvalds 
22179f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
22189f782db3SIlpo Järvinen  *
22199f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
22209f782db3SIlpo Järvinen  */
2221832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2222832d11c5SIlpo Järvinen {
22230ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2224832d11c5SIlpo Järvinen }
2225832d11c5SIlpo Järvinen 
2226832d11c5SIlpo Järvinen /**
2227832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2228832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2229832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2230832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2231832d11c5SIlpo Järvinen  *
2232832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
223320e994a0SFeng King  * the length of the skb, from skb to tgt. Returns number bytes shifted.
2234832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2235832d11c5SIlpo Järvinen  *
2236832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2237832d11c5SIlpo Järvinen  *
2238832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2239832d11c5SIlpo Järvinen  * to have non-paged data as well.
2240832d11c5SIlpo Järvinen  *
2241832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2242832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2243832d11c5SIlpo Järvinen  */
2244832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2245832d11c5SIlpo Järvinen {
2246832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2247832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2248832d11c5SIlpo Järvinen 
2249832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2250832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2251832d11c5SIlpo Järvinen 
2252832d11c5SIlpo Järvinen 	todo = shiftlen;
2253832d11c5SIlpo Järvinen 	from = 0;
2254832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2255832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2256832d11c5SIlpo Järvinen 
2257832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2258832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2259832d11c5SIlpo Järvinen 	 */
2260832d11c5SIlpo Järvinen 	if (!to ||
2261ea2ab693SIan Campbell 	    !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom),
2262ea2ab693SIan Campbell 			      fragfrom->page_offset)) {
2263832d11c5SIlpo Järvinen 		merge = -1;
2264832d11c5SIlpo Järvinen 	} else {
2265832d11c5SIlpo Järvinen 		merge = to - 1;
2266832d11c5SIlpo Järvinen 
22679e903e08SEric Dumazet 		todo -= skb_frag_size(fragfrom);
2268832d11c5SIlpo Järvinen 		if (todo < 0) {
2269832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2270832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2271832d11c5SIlpo Järvinen 				return 0;
2272832d11c5SIlpo Järvinen 
22739f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
22749f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2275832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2276832d11c5SIlpo Järvinen 
22779e903e08SEric Dumazet 			skb_frag_size_add(fragto, shiftlen);
22789e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, shiftlen);
2279832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2280832d11c5SIlpo Järvinen 
2281832d11c5SIlpo Järvinen 			goto onlymerged;
2282832d11c5SIlpo Järvinen 		}
2283832d11c5SIlpo Järvinen 
2284832d11c5SIlpo Järvinen 		from++;
2285832d11c5SIlpo Järvinen 	}
2286832d11c5SIlpo Järvinen 
2287832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2288832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2289832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2290832d11c5SIlpo Järvinen 		return 0;
2291832d11c5SIlpo Järvinen 
2292832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2293832d11c5SIlpo Järvinen 		return 0;
2294832d11c5SIlpo Järvinen 
2295832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2296832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2297832d11c5SIlpo Järvinen 			return 0;
2298832d11c5SIlpo Järvinen 
2299832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2300832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2301832d11c5SIlpo Järvinen 
23029e903e08SEric Dumazet 		if (todo >= skb_frag_size(fragfrom)) {
2303832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
23049e903e08SEric Dumazet 			todo -= skb_frag_size(fragfrom);
2305832d11c5SIlpo Järvinen 			from++;
2306832d11c5SIlpo Järvinen 			to++;
2307832d11c5SIlpo Järvinen 
2308832d11c5SIlpo Järvinen 		} else {
2309ea2ab693SIan Campbell 			__skb_frag_ref(fragfrom);
2310832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2311832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
23129e903e08SEric Dumazet 			skb_frag_size_set(fragto, todo);
2313832d11c5SIlpo Järvinen 
2314832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
23159e903e08SEric Dumazet 			skb_frag_size_sub(fragfrom, todo);
2316832d11c5SIlpo Järvinen 			todo = 0;
2317832d11c5SIlpo Järvinen 
2318832d11c5SIlpo Järvinen 			to++;
2319832d11c5SIlpo Järvinen 			break;
2320832d11c5SIlpo Järvinen 		}
2321832d11c5SIlpo Järvinen 	}
2322832d11c5SIlpo Järvinen 
2323832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2324832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2325832d11c5SIlpo Järvinen 
2326832d11c5SIlpo Järvinen 	if (merge >= 0) {
2327832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2328832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2329832d11c5SIlpo Järvinen 
23309e903e08SEric Dumazet 		skb_frag_size_add(fragto, skb_frag_size(fragfrom));
2331ea2ab693SIan Campbell 		__skb_frag_unref(fragfrom);
2332832d11c5SIlpo Järvinen 	}
2333832d11c5SIlpo Järvinen 
2334832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2335832d11c5SIlpo Järvinen 	to = 0;
2336832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2337832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2338832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2339832d11c5SIlpo Järvinen 
2340832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2341832d11c5SIlpo Järvinen 
2342832d11c5SIlpo Järvinen onlymerged:
2343832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2344832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2345832d11c5SIlpo Järvinen 	 */
2346832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2347832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2348832d11c5SIlpo Järvinen 
2349832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2350832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2351832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2352832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2353832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2354832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2355832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2356832d11c5SIlpo Järvinen 
2357832d11c5SIlpo Järvinen 	return shiftlen;
2358832d11c5SIlpo Järvinen }
2359832d11c5SIlpo Järvinen 
2360677e90edSThomas Graf /**
2361677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2362677e90edSThomas Graf  * @skb: the buffer to read
2363677e90edSThomas Graf  * @from: lower offset of data to be read
2364677e90edSThomas Graf  * @to: upper offset of data to be read
2365677e90edSThomas Graf  * @st: state variable
2366677e90edSThomas Graf  *
2367677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2368677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2369677e90edSThomas Graf  */
2370677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2371677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2372677e90edSThomas Graf {
2373677e90edSThomas Graf 	st->lower_offset = from;
2374677e90edSThomas Graf 	st->upper_offset = to;
2375677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2376677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2377677e90edSThomas Graf 	st->frag_data = NULL;
2378677e90edSThomas Graf }
2379b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2380677e90edSThomas Graf 
2381677e90edSThomas Graf /**
2382677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2383677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2384677e90edSThomas Graf  * @data: destination pointer for data to be returned
2385677e90edSThomas Graf  * @st: state variable
2386677e90edSThomas Graf  *
2387677e90edSThomas Graf  * Reads a block of skb data at &consumed relative to the
2388677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2389677e90edSThomas Graf  * the head of the data block to &data and returns the length
2390677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2391677e90edSThomas Graf  * offset has been reached.
2392677e90edSThomas Graf  *
2393677e90edSThomas Graf  * The caller is not required to consume all of the data
2394677e90edSThomas Graf  * returned, i.e. &consumed is typically set to the number
2395677e90edSThomas Graf  * of bytes already consumed and the next call to
2396677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2397677e90edSThomas Graf  *
239825985edcSLucas De Marchi  * Note 1: The size of each block of data returned can be arbitrary,
2399677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
2400677e90edSThomas Graf  *       reads of potentially non linear data.
2401677e90edSThomas Graf  *
2402bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2403677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2404677e90edSThomas Graf  *       a stack for this purpose.
2405677e90edSThomas Graf  */
2406677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2407677e90edSThomas Graf 			  struct skb_seq_state *st)
2408677e90edSThomas Graf {
2409677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2410677e90edSThomas Graf 	skb_frag_t *frag;
2411677e90edSThomas Graf 
2412677e90edSThomas Graf 	if (unlikely(abs_offset >= st->upper_offset))
2413677e90edSThomas Graf 		return 0;
2414677e90edSThomas Graf 
2415677e90edSThomas Graf next_skb:
241695e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2417677e90edSThomas Graf 
2418995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
241995e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2420677e90edSThomas Graf 		return block_limit - abs_offset;
2421677e90edSThomas Graf 	}
2422677e90edSThomas Graf 
2423677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2424677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2425677e90edSThomas Graf 
2426677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2427677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
24289e903e08SEric Dumazet 		block_limit = skb_frag_size(frag) + st->stepped_offset;
2429677e90edSThomas Graf 
2430677e90edSThomas Graf 		if (abs_offset < block_limit) {
2431677e90edSThomas Graf 			if (!st->frag_data)
2432677e90edSThomas Graf 				st->frag_data = kmap_skb_frag(frag);
2433677e90edSThomas Graf 
2434677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2435677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2436677e90edSThomas Graf 
2437677e90edSThomas Graf 			return block_limit - abs_offset;
2438677e90edSThomas Graf 		}
2439677e90edSThomas Graf 
2440677e90edSThomas Graf 		if (st->frag_data) {
2441677e90edSThomas Graf 			kunmap_skb_frag(st->frag_data);
2442677e90edSThomas Graf 			st->frag_data = NULL;
2443677e90edSThomas Graf 		}
2444677e90edSThomas Graf 
2445677e90edSThomas Graf 		st->frag_idx++;
24469e903e08SEric Dumazet 		st->stepped_offset += skb_frag_size(frag);
2447677e90edSThomas Graf 	}
2448677e90edSThomas Graf 
24495b5a60daSOlaf Kirch 	if (st->frag_data) {
24505b5a60daSOlaf Kirch 		kunmap_skb_frag(st->frag_data);
24515b5a60daSOlaf Kirch 		st->frag_data = NULL;
24525b5a60daSOlaf Kirch 	}
24535b5a60daSOlaf Kirch 
245421dc3301SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) {
2455677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
245695e3b24cSHerbert Xu 		st->frag_idx = 0;
2457677e90edSThomas Graf 		goto next_skb;
245871b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
245971b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
246071b3346dSShyam Iyer 		st->frag_idx = 0;
2461677e90edSThomas Graf 		goto next_skb;
2462677e90edSThomas Graf 	}
2463677e90edSThomas Graf 
2464677e90edSThomas Graf 	return 0;
2465677e90edSThomas Graf }
2466b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2467677e90edSThomas Graf 
2468677e90edSThomas Graf /**
2469677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2470677e90edSThomas Graf  * @st: state variable
2471677e90edSThomas Graf  *
2472677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2473677e90edSThomas Graf  * returned 0.
2474677e90edSThomas Graf  */
2475677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2476677e90edSThomas Graf {
2477677e90edSThomas Graf 	if (st->frag_data)
2478677e90edSThomas Graf 		kunmap_skb_frag(st->frag_data);
2479677e90edSThomas Graf }
2480b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2481677e90edSThomas Graf 
24823fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
24833fc7e8a6SThomas Graf 
24843fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
24853fc7e8a6SThomas Graf 					  struct ts_config *conf,
24863fc7e8a6SThomas Graf 					  struct ts_state *state)
24873fc7e8a6SThomas Graf {
24883fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
24893fc7e8a6SThomas Graf }
24903fc7e8a6SThomas Graf 
24913fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
24923fc7e8a6SThomas Graf {
24933fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
24943fc7e8a6SThomas Graf }
24953fc7e8a6SThomas Graf 
24963fc7e8a6SThomas Graf /**
24973fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
24983fc7e8a6SThomas Graf  * @skb: the buffer to look in
24993fc7e8a6SThomas Graf  * @from: search offset
25003fc7e8a6SThomas Graf  * @to: search limit
25013fc7e8a6SThomas Graf  * @config: textsearch configuration
25023fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
25033fc7e8a6SThomas Graf  *
25043fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
25053fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
25063fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
25073fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
25083fc7e8a6SThomas Graf  */
25093fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
25103fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
25113fc7e8a6SThomas Graf 			   struct ts_state *state)
25123fc7e8a6SThomas Graf {
2513f72b948dSPhil Oester 	unsigned int ret;
2514f72b948dSPhil Oester 
25153fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
25163fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
25173fc7e8a6SThomas Graf 
25183fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
25193fc7e8a6SThomas Graf 
2520f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2521f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
25223fc7e8a6SThomas Graf }
2523b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
25243fc7e8a6SThomas Graf 
2525e89e9cf5SAnanda Raju /**
2526e89e9cf5SAnanda Raju  * skb_append_datato_frags: - append the user data to a skb
2527e89e9cf5SAnanda Raju  * @sk: sock  structure
2528e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
2529e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2530e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2531e89e9cf5SAnanda Raju  * @length: length of the iov message
2532e89e9cf5SAnanda Raju  *
2533e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2534e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2535e89e9cf5SAnanda Raju  */
2536e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2537dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2538e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2539e89e9cf5SAnanda Raju 			void *from, int length)
2540e89e9cf5SAnanda Raju {
2541e89e9cf5SAnanda Raju 	int frg_cnt = 0;
2542e89e9cf5SAnanda Raju 	skb_frag_t *frag = NULL;
2543e89e9cf5SAnanda Raju 	struct page *page = NULL;
2544e89e9cf5SAnanda Raju 	int copy, left;
2545e89e9cf5SAnanda Raju 	int offset = 0;
2546e89e9cf5SAnanda Raju 	int ret;
2547e89e9cf5SAnanda Raju 
2548e89e9cf5SAnanda Raju 	do {
2549e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2550e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2551e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2552e89e9cf5SAnanda Raju 			return -EFAULT;
2553e89e9cf5SAnanda Raju 
2554e89e9cf5SAnanda Raju 		/* allocate a new page for next frag */
2555e89e9cf5SAnanda Raju 		page = alloc_pages(sk->sk_allocation, 0);
2556e89e9cf5SAnanda Raju 
2557e89e9cf5SAnanda Raju 		/* If alloc_page fails just return failure and caller will
2558e89e9cf5SAnanda Raju 		 * free previous allocated pages by doing kfree_skb()
2559e89e9cf5SAnanda Raju 		 */
2560e89e9cf5SAnanda Raju 		if (page == NULL)
2561e89e9cf5SAnanda Raju 			return -ENOMEM;
2562e89e9cf5SAnanda Raju 
2563e89e9cf5SAnanda Raju 		/* initialize the next frag */
2564e89e9cf5SAnanda Raju 		skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2565e89e9cf5SAnanda Raju 		skb->truesize += PAGE_SIZE;
2566e89e9cf5SAnanda Raju 		atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2567e89e9cf5SAnanda Raju 
2568e89e9cf5SAnanda Raju 		/* get the new initialized frag */
2569e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2570e89e9cf5SAnanda Raju 		frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2571e89e9cf5SAnanda Raju 
2572e89e9cf5SAnanda Raju 		/* copy the user data to page */
2573e89e9cf5SAnanda Raju 		left = PAGE_SIZE - frag->page_offset;
2574e89e9cf5SAnanda Raju 		copy = (length > left)? left : length;
2575e89e9cf5SAnanda Raju 
25769e903e08SEric Dumazet 		ret = getfrag(from, skb_frag_address(frag) + skb_frag_size(frag),
2577e89e9cf5SAnanda Raju 			    offset, copy, 0, skb);
2578e89e9cf5SAnanda Raju 		if (ret < 0)
2579e89e9cf5SAnanda Raju 			return -EFAULT;
2580e89e9cf5SAnanda Raju 
2581e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
25829e903e08SEric Dumazet 		skb_frag_size_add(frag, copy);
2583e89e9cf5SAnanda Raju 		skb->len += copy;
2584e89e9cf5SAnanda Raju 		skb->data_len += copy;
2585e89e9cf5SAnanda Raju 		offset += copy;
2586e89e9cf5SAnanda Raju 		length -= copy;
2587e89e9cf5SAnanda Raju 
2588e89e9cf5SAnanda Raju 	} while (length > 0);
2589e89e9cf5SAnanda Raju 
2590e89e9cf5SAnanda Raju 	return 0;
2591e89e9cf5SAnanda Raju }
2592b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
2593e89e9cf5SAnanda Raju 
2594cbb042f9SHerbert Xu /**
2595cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2596cbb042f9SHerbert Xu  *	@skb: buffer to update
2597cbb042f9SHerbert Xu  *	@len: length of data pulled
2598cbb042f9SHerbert Xu  *
2599cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2600fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
260184fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
260284fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
260384fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2604cbb042f9SHerbert Xu  */
2605cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2606cbb042f9SHerbert Xu {
2607cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2608cbb042f9SHerbert Xu 	skb->len -= len;
2609cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2610cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2611cbb042f9SHerbert Xu 	return skb->data += len;
2612cbb042f9SHerbert Xu }
2613f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2614f94691acSArnaldo Carvalho de Melo 
2615f4c50d99SHerbert Xu /**
2616f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2617f4c50d99SHerbert Xu  *	@skb: buffer to segment
2618576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2619f4c50d99SHerbert Xu  *
2620f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
26214c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
26224c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2623f4c50d99SHerbert Xu  */
262404ed3e74SMichał Mirosław struct sk_buff *skb_segment(struct sk_buff *skb, u32 features)
2625f4c50d99SHerbert Xu {
2626f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2627f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
262889319d38SHerbert Xu 	struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2629f4c50d99SHerbert Xu 	unsigned int mss = skb_shinfo(skb)->gso_size;
263098e399f8SArnaldo Carvalho de Melo 	unsigned int doffset = skb->data - skb_mac_header(skb);
2631f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2632f4c50d99SHerbert Xu 	unsigned int headroom;
2633f4c50d99SHerbert Xu 	unsigned int len;
263404ed3e74SMichał Mirosław 	int sg = !!(features & NETIF_F_SG);
2635f4c50d99SHerbert Xu 	int nfrags = skb_shinfo(skb)->nr_frags;
2636f4c50d99SHerbert Xu 	int err = -ENOMEM;
2637f4c50d99SHerbert Xu 	int i = 0;
2638f4c50d99SHerbert Xu 	int pos;
2639f4c50d99SHerbert Xu 
2640f4c50d99SHerbert Xu 	__skb_push(skb, doffset);
2641f4c50d99SHerbert Xu 	headroom = skb_headroom(skb);
2642f4c50d99SHerbert Xu 	pos = skb_headlen(skb);
2643f4c50d99SHerbert Xu 
2644f4c50d99SHerbert Xu 	do {
2645f4c50d99SHerbert Xu 		struct sk_buff *nskb;
2646f4c50d99SHerbert Xu 		skb_frag_t *frag;
2647c8884eddSHerbert Xu 		int hsize;
2648f4c50d99SHerbert Xu 		int size;
2649f4c50d99SHerbert Xu 
2650f4c50d99SHerbert Xu 		len = skb->len - offset;
2651f4c50d99SHerbert Xu 		if (len > mss)
2652f4c50d99SHerbert Xu 			len = mss;
2653f4c50d99SHerbert Xu 
2654f4c50d99SHerbert Xu 		hsize = skb_headlen(skb) - offset;
2655f4c50d99SHerbert Xu 		if (hsize < 0)
2656f4c50d99SHerbert Xu 			hsize = 0;
2657c8884eddSHerbert Xu 		if (hsize > len || !sg)
2658c8884eddSHerbert Xu 			hsize = len;
2659f4c50d99SHerbert Xu 
266089319d38SHerbert Xu 		if (!hsize && i >= nfrags) {
266189319d38SHerbert Xu 			BUG_ON(fskb->len != len);
266289319d38SHerbert Xu 
266389319d38SHerbert Xu 			pos += len;
266489319d38SHerbert Xu 			nskb = skb_clone(fskb, GFP_ATOMIC);
266589319d38SHerbert Xu 			fskb = fskb->next;
266689319d38SHerbert Xu 
2667f4c50d99SHerbert Xu 			if (unlikely(!nskb))
2668f4c50d99SHerbert Xu 				goto err;
2669f4c50d99SHerbert Xu 
267089319d38SHerbert Xu 			hsize = skb_end_pointer(nskb) - nskb->head;
267189319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
267289319d38SHerbert Xu 				kfree_skb(nskb);
267389319d38SHerbert Xu 				goto err;
267489319d38SHerbert Xu 			}
267589319d38SHerbert Xu 
267689319d38SHerbert Xu 			nskb->truesize += skb_end_pointer(nskb) - nskb->head -
267789319d38SHerbert Xu 					  hsize;
267889319d38SHerbert Xu 			skb_release_head_state(nskb);
267989319d38SHerbert Xu 			__skb_push(nskb, doffset);
268089319d38SHerbert Xu 		} else {
268189319d38SHerbert Xu 			nskb = alloc_skb(hsize + doffset + headroom,
268289319d38SHerbert Xu 					 GFP_ATOMIC);
268389319d38SHerbert Xu 
268489319d38SHerbert Xu 			if (unlikely(!nskb))
268589319d38SHerbert Xu 				goto err;
268689319d38SHerbert Xu 
268789319d38SHerbert Xu 			skb_reserve(nskb, headroom);
268889319d38SHerbert Xu 			__skb_put(nskb, doffset);
268989319d38SHerbert Xu 		}
269089319d38SHerbert Xu 
2691f4c50d99SHerbert Xu 		if (segs)
2692f4c50d99SHerbert Xu 			tail->next = nskb;
2693f4c50d99SHerbert Xu 		else
2694f4c50d99SHerbert Xu 			segs = nskb;
2695f4c50d99SHerbert Xu 		tail = nskb;
2696f4c50d99SHerbert Xu 
26976f85a124SHerbert Xu 		__copy_skb_header(nskb, skb);
2698f4c50d99SHerbert Xu 		nskb->mac_len = skb->mac_len;
2699f4c50d99SHerbert Xu 
27003d3be433SEric Dumazet 		/* nskb and skb might have different headroom */
27013d3be433SEric Dumazet 		if (nskb->ip_summed == CHECKSUM_PARTIAL)
27023d3be433SEric Dumazet 			nskb->csum_start += skb_headroom(nskb) - headroom;
27033d3be433SEric Dumazet 
2704459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(nskb);
2705ddc7b8e3SArnaldo Carvalho de Melo 		skb_set_network_header(nskb, skb->mac_len);
2706b0e380b1SArnaldo Carvalho de Melo 		nskb->transport_header = (nskb->network_header +
2707b0e380b1SArnaldo Carvalho de Melo 					  skb_network_header_len(skb));
270889319d38SHerbert Xu 		skb_copy_from_linear_data(skb, nskb->data, doffset);
270989319d38SHerbert Xu 
27102f181855SHerbert Xu 		if (fskb != skb_shinfo(skb)->frag_list)
271189319d38SHerbert Xu 			continue;
271289319d38SHerbert Xu 
2713f4c50d99SHerbert Xu 		if (!sg) {
27146f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
2715f4c50d99SHerbert Xu 			nskb->csum = skb_copy_and_csum_bits(skb, offset,
2716f4c50d99SHerbert Xu 							    skb_put(nskb, len),
2717f4c50d99SHerbert Xu 							    len, 0);
2718f4c50d99SHerbert Xu 			continue;
2719f4c50d99SHerbert Xu 		}
2720f4c50d99SHerbert Xu 
2721f4c50d99SHerbert Xu 		frag = skb_shinfo(nskb)->frags;
2722f4c50d99SHerbert Xu 
2723d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset,
2724d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
2725f4c50d99SHerbert Xu 
272689319d38SHerbert Xu 		while (pos < offset + len && i < nfrags) {
2727f4c50d99SHerbert Xu 			*frag = skb_shinfo(skb)->frags[i];
2728ea2ab693SIan Campbell 			__skb_frag_ref(frag);
27299e903e08SEric Dumazet 			size = skb_frag_size(frag);
2730f4c50d99SHerbert Xu 
2731f4c50d99SHerbert Xu 			if (pos < offset) {
2732f4c50d99SHerbert Xu 				frag->page_offset += offset - pos;
27339e903e08SEric Dumazet 				skb_frag_size_sub(frag, offset - pos);
2734f4c50d99SHerbert Xu 			}
2735f4c50d99SHerbert Xu 
273689319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
2737f4c50d99SHerbert Xu 
2738f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
2739f4c50d99SHerbert Xu 				i++;
2740f4c50d99SHerbert Xu 				pos += size;
2741f4c50d99SHerbert Xu 			} else {
27429e903e08SEric Dumazet 				skb_frag_size_sub(frag, pos + size - (offset + len));
274389319d38SHerbert Xu 				goto skip_fraglist;
2744f4c50d99SHerbert Xu 			}
2745f4c50d99SHerbert Xu 
2746f4c50d99SHerbert Xu 			frag++;
2747f4c50d99SHerbert Xu 		}
2748f4c50d99SHerbert Xu 
274989319d38SHerbert Xu 		if (pos < offset + len) {
275089319d38SHerbert Xu 			struct sk_buff *fskb2 = fskb;
275189319d38SHerbert Xu 
275289319d38SHerbert Xu 			BUG_ON(pos + fskb->len != offset + len);
275389319d38SHerbert Xu 
275489319d38SHerbert Xu 			pos += fskb->len;
275589319d38SHerbert Xu 			fskb = fskb->next;
275689319d38SHerbert Xu 
275789319d38SHerbert Xu 			if (fskb2->next) {
275889319d38SHerbert Xu 				fskb2 = skb_clone(fskb2, GFP_ATOMIC);
275989319d38SHerbert Xu 				if (!fskb2)
276089319d38SHerbert Xu 					goto err;
276189319d38SHerbert Xu 			} else
276289319d38SHerbert Xu 				skb_get(fskb2);
276389319d38SHerbert Xu 
2764fbb398a8SDavid S. Miller 			SKB_FRAG_ASSERT(nskb);
276589319d38SHerbert Xu 			skb_shinfo(nskb)->frag_list = fskb2;
276689319d38SHerbert Xu 		}
276789319d38SHerbert Xu 
276889319d38SHerbert Xu skip_fraglist:
2769f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
2770f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
2771f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
2772f4c50d99SHerbert Xu 	} while ((offset += len) < skb->len);
2773f4c50d99SHerbert Xu 
2774f4c50d99SHerbert Xu 	return segs;
2775f4c50d99SHerbert Xu 
2776f4c50d99SHerbert Xu err:
2777f4c50d99SHerbert Xu 	while ((skb = segs)) {
2778f4c50d99SHerbert Xu 		segs = skb->next;
2779b08d5840SPatrick McHardy 		kfree_skb(skb);
2780f4c50d99SHerbert Xu 	}
2781f4c50d99SHerbert Xu 	return ERR_PTR(err);
2782f4c50d99SHerbert Xu }
2783f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
2784f4c50d99SHerbert Xu 
278571d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
278671d93b39SHerbert Xu {
278771d93b39SHerbert Xu 	struct sk_buff *p = *head;
278871d93b39SHerbert Xu 	struct sk_buff *nskb;
27899aaa156cSHerbert Xu 	struct skb_shared_info *skbinfo = skb_shinfo(skb);
27909aaa156cSHerbert Xu 	struct skb_shared_info *pinfo = skb_shinfo(p);
279171d93b39SHerbert Xu 	unsigned int headroom;
279286911732SHerbert Xu 	unsigned int len = skb_gro_len(skb);
279367147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
279467147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
279571d93b39SHerbert Xu 
279686911732SHerbert Xu 	if (p->len + len >= 65536)
279771d93b39SHerbert Xu 		return -E2BIG;
279871d93b39SHerbert Xu 
27999aaa156cSHerbert Xu 	if (pinfo->frag_list)
280071d93b39SHerbert Xu 		goto merge;
280167147ba9SHerbert Xu 	else if (headlen <= offset) {
280242da6994SHerbert Xu 		skb_frag_t *frag;
280366e92fcfSHerbert Xu 		skb_frag_t *frag2;
28049aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
28059aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
280642da6994SHerbert Xu 
280766e92fcfSHerbert Xu 		offset -= headlen;
280866e92fcfSHerbert Xu 
280966e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
281081705ad1SHerbert Xu 			return -E2BIG;
281181705ad1SHerbert Xu 
28129aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
28139aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
2814f5572068SHerbert Xu 
28159aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
28169aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
281766e92fcfSHerbert Xu 		do {
281866e92fcfSHerbert Xu 			*--frag = *--frag2;
281966e92fcfSHerbert Xu 		} while (--i);
282066e92fcfSHerbert Xu 
282166e92fcfSHerbert Xu 		frag->page_offset += offset;
28229e903e08SEric Dumazet 		skb_frag_size_sub(frag, offset);
282366e92fcfSHerbert Xu 
2824f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
2825f5572068SHerbert Xu 		skb->len -= skb->data_len;
2826f5572068SHerbert Xu 		skb->data_len = 0;
2827f5572068SHerbert Xu 
28285d38a079SHerbert Xu 		NAPI_GRO_CB(skb)->free = 1;
28295d38a079SHerbert Xu 		goto done;
283069c0cab1SHerbert Xu 	} else if (skb_gro_len(p) != pinfo->gso_size)
283169c0cab1SHerbert Xu 		return -E2BIG;
283271d93b39SHerbert Xu 
283371d93b39SHerbert Xu 	headroom = skb_headroom(p);
28343d3be433SEric Dumazet 	nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
283571d93b39SHerbert Xu 	if (unlikely(!nskb))
283671d93b39SHerbert Xu 		return -ENOMEM;
283771d93b39SHerbert Xu 
283871d93b39SHerbert Xu 	__copy_skb_header(nskb, p);
283971d93b39SHerbert Xu 	nskb->mac_len = p->mac_len;
284071d93b39SHerbert Xu 
284171d93b39SHerbert Xu 	skb_reserve(nskb, headroom);
284286911732SHerbert Xu 	__skb_put(nskb, skb_gro_offset(p));
284371d93b39SHerbert Xu 
284486911732SHerbert Xu 	skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
284571d93b39SHerbert Xu 	skb_set_network_header(nskb, skb_network_offset(p));
284671d93b39SHerbert Xu 	skb_set_transport_header(nskb, skb_transport_offset(p));
284771d93b39SHerbert Xu 
284886911732SHerbert Xu 	__skb_pull(p, skb_gro_offset(p));
284986911732SHerbert Xu 	memcpy(skb_mac_header(nskb), skb_mac_header(p),
285086911732SHerbert Xu 	       p->data - skb_mac_header(p));
285171d93b39SHerbert Xu 
285271d93b39SHerbert Xu 	*NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
285371d93b39SHerbert Xu 	skb_shinfo(nskb)->frag_list = p;
28549aaa156cSHerbert Xu 	skb_shinfo(nskb)->gso_size = pinfo->gso_size;
2855622e0ca1SHerbert Xu 	pinfo->gso_size = 0;
285671d93b39SHerbert Xu 	skb_header_release(p);
285771d93b39SHerbert Xu 	nskb->prev = p;
285871d93b39SHerbert Xu 
285971d93b39SHerbert Xu 	nskb->data_len += p->len;
286071d93b39SHerbert Xu 	nskb->truesize += p->len;
286171d93b39SHerbert Xu 	nskb->len += p->len;
286271d93b39SHerbert Xu 
286371d93b39SHerbert Xu 	*head = nskb;
286471d93b39SHerbert Xu 	nskb->next = p->next;
286571d93b39SHerbert Xu 	p->next = NULL;
286671d93b39SHerbert Xu 
286771d93b39SHerbert Xu 	p = nskb;
286871d93b39SHerbert Xu 
286971d93b39SHerbert Xu merge:
287067147ba9SHerbert Xu 	if (offset > headlen) {
2871d1dc7abfSMichal Schmidt 		unsigned int eat = offset - headlen;
2872d1dc7abfSMichal Schmidt 
2873d1dc7abfSMichal Schmidt 		skbinfo->frags[0].page_offset += eat;
28749e903e08SEric Dumazet 		skb_frag_size_sub(&skbinfo->frags[0], eat);
2875d1dc7abfSMichal Schmidt 		skb->data_len -= eat;
2876d1dc7abfSMichal Schmidt 		skb->len -= eat;
287767147ba9SHerbert Xu 		offset = headlen;
287856035022SHerbert Xu 	}
287956035022SHerbert Xu 
288067147ba9SHerbert Xu 	__skb_pull(skb, offset);
288156035022SHerbert Xu 
288271d93b39SHerbert Xu 	p->prev->next = skb;
288371d93b39SHerbert Xu 	p->prev = skb;
288471d93b39SHerbert Xu 	skb_header_release(skb);
288571d93b39SHerbert Xu 
28865d38a079SHerbert Xu done:
28875d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
288837fe4732SHerbert Xu 	p->data_len += len;
288937fe4732SHerbert Xu 	p->truesize += len;
289037fe4732SHerbert Xu 	p->len += len;
289171d93b39SHerbert Xu 
289271d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
289371d93b39SHerbert Xu 	return 0;
289471d93b39SHerbert Xu }
289571d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive);
289671d93b39SHerbert Xu 
28971da177e4SLinus Torvalds void __init skb_init(void)
28981da177e4SLinus Torvalds {
28991da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
29001da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
29011da177e4SLinus Torvalds 					      0,
2902e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
290320c2df83SPaul Mundt 					      NULL);
2904d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2905d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
2906d179cd12SDavid S. Miller 						sizeof(atomic_t),
2907d179cd12SDavid S. Miller 						0,
2908e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
290920c2df83SPaul Mundt 						NULL);
29101da177e4SLinus Torvalds }
29111da177e4SLinus Torvalds 
2912716ea3a7SDavid Howells /**
2913716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2914716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
2915716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
2916716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
2917716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
2918716ea3a7SDavid Howells  *
2919716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
2920716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
2921716ea3a7SDavid Howells  */
292251c739d1SDavid S. Miller static int
292351c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2924716ea3a7SDavid Howells {
29251a028e50SDavid S. Miller 	int start = skb_headlen(skb);
29261a028e50SDavid S. Miller 	int i, copy = start - offset;
2927fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2928716ea3a7SDavid Howells 	int elt = 0;
2929716ea3a7SDavid Howells 
2930716ea3a7SDavid Howells 	if (copy > 0) {
2931716ea3a7SDavid Howells 		if (copy > len)
2932716ea3a7SDavid Howells 			copy = len;
2933642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
2934716ea3a7SDavid Howells 		elt++;
2935716ea3a7SDavid Howells 		if ((len -= copy) == 0)
2936716ea3a7SDavid Howells 			return elt;
2937716ea3a7SDavid Howells 		offset += copy;
2938716ea3a7SDavid Howells 	}
2939716ea3a7SDavid Howells 
2940716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
29411a028e50SDavid S. Miller 		int end;
2942716ea3a7SDavid Howells 
2943547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
29441a028e50SDavid S. Miller 
29459e903e08SEric Dumazet 		end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
2946716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2947716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2948716ea3a7SDavid Howells 
2949716ea3a7SDavid Howells 			if (copy > len)
2950716ea3a7SDavid Howells 				copy = len;
2951ea2ab693SIan Campbell 			sg_set_page(&sg[elt], skb_frag_page(frag), copy,
2952642f1490SJens Axboe 					frag->page_offset+offset-start);
2953716ea3a7SDavid Howells 			elt++;
2954716ea3a7SDavid Howells 			if (!(len -= copy))
2955716ea3a7SDavid Howells 				return elt;
2956716ea3a7SDavid Howells 			offset += copy;
2957716ea3a7SDavid Howells 		}
29581a028e50SDavid S. Miller 		start = end;
2959716ea3a7SDavid Howells 	}
2960716ea3a7SDavid Howells 
2961fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
29621a028e50SDavid S. Miller 		int end;
2963716ea3a7SDavid Howells 
2964547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
29651a028e50SDavid S. Miller 
2966fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
2967716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2968716ea3a7SDavid Howells 			if (copy > len)
2969716ea3a7SDavid Howells 				copy = len;
2970fbb398a8SDavid S. Miller 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
297151c739d1SDavid S. Miller 					      copy);
2972716ea3a7SDavid Howells 			if ((len -= copy) == 0)
2973716ea3a7SDavid Howells 				return elt;
2974716ea3a7SDavid Howells 			offset += copy;
2975716ea3a7SDavid Howells 		}
29761a028e50SDavid S. Miller 		start = end;
2977716ea3a7SDavid Howells 	}
2978716ea3a7SDavid Howells 	BUG_ON(len);
2979716ea3a7SDavid Howells 	return elt;
2980716ea3a7SDavid Howells }
2981716ea3a7SDavid Howells 
298251c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
298351c739d1SDavid S. Miller {
298451c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
298551c739d1SDavid S. Miller 
2986c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
298751c739d1SDavid S. Miller 
298851c739d1SDavid S. Miller 	return nsg;
298951c739d1SDavid S. Miller }
2990b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
299151c739d1SDavid S. Miller 
2992716ea3a7SDavid Howells /**
2993716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
2994716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
2995716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
2996716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
2997716ea3a7SDavid Howells  *
2998716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
2999716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
3000716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
3001716ea3a7SDavid Howells  *
3002716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
3003716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
3004716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
3005716ea3a7SDavid Howells  *
3006716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
3007716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
3008716ea3a7SDavid Howells  */
3009716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
3010716ea3a7SDavid Howells {
3011716ea3a7SDavid Howells 	int copyflag;
3012716ea3a7SDavid Howells 	int elt;
3013716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
3014716ea3a7SDavid Howells 
3015716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
3016716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
3017716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
3018716ea3a7SDavid Howells 	 */
3019716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
3020716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
3021716ea3a7SDavid Howells 		return -ENOMEM;
3022716ea3a7SDavid Howells 
3023716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
302421dc3301SDavid S. Miller 	if (!skb_has_frag_list(skb)) {
3025716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
3026716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
3027716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
3028716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
3029716ea3a7SDavid Howells 
3030716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
3031716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
3032716ea3a7SDavid Howells 			return -ENOMEM;
3033716ea3a7SDavid Howells 
3034716ea3a7SDavid Howells 		/* Voila! */
3035716ea3a7SDavid Howells 		*trailer = skb;
3036716ea3a7SDavid Howells 		return 1;
3037716ea3a7SDavid Howells 	}
3038716ea3a7SDavid Howells 
3039716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
3040716ea3a7SDavid Howells 
3041716ea3a7SDavid Howells 	elt = 1;
3042716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
3043716ea3a7SDavid Howells 	copyflag = 0;
3044716ea3a7SDavid Howells 
3045716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
3046716ea3a7SDavid Howells 		int ntail = 0;
3047716ea3a7SDavid Howells 
3048716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
3049716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
3050716ea3a7SDavid Howells 		 * after it. */
3051716ea3a7SDavid Howells 
3052716ea3a7SDavid Howells 		if (skb_shared(skb1))
3053716ea3a7SDavid Howells 			copyflag = 1;
3054716ea3a7SDavid Howells 
3055716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
3056716ea3a7SDavid Howells 
3057716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
3058716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
305921dc3301SDavid S. Miller 			    skb_has_frag_list(skb1) ||
3060716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
3061716ea3a7SDavid Howells 				ntail = tailbits + 128;
3062716ea3a7SDavid Howells 		}
3063716ea3a7SDavid Howells 
3064716ea3a7SDavid Howells 		if (copyflag ||
3065716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
3066716ea3a7SDavid Howells 		    ntail ||
3067716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
306821dc3301SDavid S. Miller 		    skb_has_frag_list(skb1)) {
3069716ea3a7SDavid Howells 			struct sk_buff *skb2;
3070716ea3a7SDavid Howells 
3071716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
3072716ea3a7SDavid Howells 			if (ntail == 0)
3073716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
3074716ea3a7SDavid Howells 			else
3075716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
3076716ea3a7SDavid Howells 						       skb_headroom(skb1),
3077716ea3a7SDavid Howells 						       ntail,
3078716ea3a7SDavid Howells 						       GFP_ATOMIC);
3079716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
3080716ea3a7SDavid Howells 				return -ENOMEM;
3081716ea3a7SDavid Howells 
3082716ea3a7SDavid Howells 			if (skb1->sk)
3083716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
3084716ea3a7SDavid Howells 
3085716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
3086716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
3087716ea3a7SDavid Howells 
3088716ea3a7SDavid Howells 			skb2->next = skb1->next;
3089716ea3a7SDavid Howells 			*skb_p = skb2;
3090716ea3a7SDavid Howells 			kfree_skb(skb1);
3091716ea3a7SDavid Howells 			skb1 = skb2;
3092716ea3a7SDavid Howells 		}
3093716ea3a7SDavid Howells 		elt++;
3094716ea3a7SDavid Howells 		*trailer = skb1;
3095716ea3a7SDavid Howells 		skb_p = &skb1->next;
3096716ea3a7SDavid Howells 	}
3097716ea3a7SDavid Howells 
3098716ea3a7SDavid Howells 	return elt;
3099716ea3a7SDavid Howells }
3100b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
3101716ea3a7SDavid Howells 
3102b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb)
3103b1faf566SEric Dumazet {
3104b1faf566SEric Dumazet 	struct sock *sk = skb->sk;
3105b1faf566SEric Dumazet 
3106b1faf566SEric Dumazet 	atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
3107b1faf566SEric Dumazet }
3108b1faf566SEric Dumazet 
3109b1faf566SEric Dumazet /*
3110b1faf566SEric Dumazet  * Note: We dont mem charge error packets (no sk_forward_alloc changes)
3111b1faf566SEric Dumazet  */
3112b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb)
3113b1faf566SEric Dumazet {
3114b1faf566SEric Dumazet 	if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
3115b1faf566SEric Dumazet 	    (unsigned)sk->sk_rcvbuf)
3116b1faf566SEric Dumazet 		return -ENOMEM;
3117b1faf566SEric Dumazet 
3118b1faf566SEric Dumazet 	skb_orphan(skb);
3119b1faf566SEric Dumazet 	skb->sk = sk;
3120b1faf566SEric Dumazet 	skb->destructor = sock_rmem_free;
3121b1faf566SEric Dumazet 	atomic_add(skb->truesize, &sk->sk_rmem_alloc);
3122b1faf566SEric Dumazet 
3123abb57ea4SEric Dumazet 	/* before exiting rcu section, make sure dst is refcounted */
3124abb57ea4SEric Dumazet 	skb_dst_force(skb);
3125abb57ea4SEric Dumazet 
3126b1faf566SEric Dumazet 	skb_queue_tail(&sk->sk_error_queue, skb);
3127b1faf566SEric Dumazet 	if (!sock_flag(sk, SOCK_DEAD))
3128b1faf566SEric Dumazet 		sk->sk_data_ready(sk, skb->len);
3129b1faf566SEric Dumazet 	return 0;
3130b1faf566SEric Dumazet }
3131b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb);
3132b1faf566SEric Dumazet 
3133ac45f602SPatrick Ohly void skb_tstamp_tx(struct sk_buff *orig_skb,
3134ac45f602SPatrick Ohly 		struct skb_shared_hwtstamps *hwtstamps)
3135ac45f602SPatrick Ohly {
3136ac45f602SPatrick Ohly 	struct sock *sk = orig_skb->sk;
3137ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
3138ac45f602SPatrick Ohly 	struct sk_buff *skb;
3139ac45f602SPatrick Ohly 	int err;
3140ac45f602SPatrick Ohly 
3141ac45f602SPatrick Ohly 	if (!sk)
3142ac45f602SPatrick Ohly 		return;
3143ac45f602SPatrick Ohly 
3144ac45f602SPatrick Ohly 	skb = skb_clone(orig_skb, GFP_ATOMIC);
3145ac45f602SPatrick Ohly 	if (!skb)
3146ac45f602SPatrick Ohly 		return;
3147ac45f602SPatrick Ohly 
3148ac45f602SPatrick Ohly 	if (hwtstamps) {
3149ac45f602SPatrick Ohly 		*skb_hwtstamps(skb) =
3150ac45f602SPatrick Ohly 			*hwtstamps;
3151ac45f602SPatrick Ohly 	} else {
3152ac45f602SPatrick Ohly 		/*
3153ac45f602SPatrick Ohly 		 * no hardware time stamps available,
31542244d07bSOliver Hartkopp 		 * so keep the shared tx_flags and only
3155ac45f602SPatrick Ohly 		 * store software time stamp
3156ac45f602SPatrick Ohly 		 */
3157ac45f602SPatrick Ohly 		skb->tstamp = ktime_get_real();
3158ac45f602SPatrick Ohly 	}
3159ac45f602SPatrick Ohly 
3160ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
3161ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
3162ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
3163ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
316429030374SEric Dumazet 
3165ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
316629030374SEric Dumazet 
3167ac45f602SPatrick Ohly 	if (err)
3168ac45f602SPatrick Ohly 		kfree_skb(skb);
3169ac45f602SPatrick Ohly }
3170ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3171ac45f602SPatrick Ohly 
3172ac45f602SPatrick Ohly 
3173f35d9d8aSRusty Russell /**
3174f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3175f35d9d8aSRusty Russell  * @skb: the skb to set
3176f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3177f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3178f35d9d8aSRusty Russell  *
3179f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3180f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3181f35d9d8aSRusty Russell  *
3182f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3183f35d9d8aSRusty Russell  * returns false you should drop the packet.
3184f35d9d8aSRusty Russell  */
3185f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3186f35d9d8aSRusty Russell {
31875ff8dda3SHerbert Xu 	if (unlikely(start > skb_headlen(skb)) ||
31885ff8dda3SHerbert Xu 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3189f35d9d8aSRusty Russell 		if (net_ratelimit())
3190f35d9d8aSRusty Russell 			printk(KERN_WARNING
3191f35d9d8aSRusty Russell 			       "bad partial csum: csum=%u/%u len=%u\n",
31925ff8dda3SHerbert Xu 			       start, off, skb_headlen(skb));
3193f35d9d8aSRusty Russell 		return false;
3194f35d9d8aSRusty Russell 	}
3195f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3196f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3197f35d9d8aSRusty Russell 	skb->csum_offset = off;
3198f35d9d8aSRusty Russell 	return true;
3199f35d9d8aSRusty Russell }
3200b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3201f35d9d8aSRusty Russell 
32024497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
32034497b076SBen Hutchings {
32044497b076SBen Hutchings 	if (net_ratelimit())
32054497b076SBen Hutchings 		pr_warning("%s: received packets cannot be forwarded"
32064497b076SBen Hutchings 			   " while LRO is enabled\n", skb->dev->name);
32074497b076SBen Hutchings }
32084497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3209