xref: /openbmc/linux/net/core/skbuff.c (revision 6aa895b0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Authors:	Alan Cox <iiitac@pyr.swan.ac.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>
421da177e4SLinus Torvalds #include <linux/mm.h>
431da177e4SLinus Torvalds #include <linux/interrupt.h>
441da177e4SLinus Torvalds #include <linux/in.h>
451da177e4SLinus Torvalds #include <linux/inet.h>
461da177e4SLinus Torvalds #include <linux/slab.h>
471da177e4SLinus Torvalds #include <linux/netdevice.h>
481da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
491da177e4SLinus Torvalds #include <net/pkt_sched.h>
501da177e4SLinus Torvalds #endif
511da177e4SLinus Torvalds #include <linux/string.h>
521da177e4SLinus Torvalds #include <linux/skbuff.h>
539c55e01cSJens Axboe #include <linux/splice.h>
541da177e4SLinus Torvalds #include <linux/cache.h>
551da177e4SLinus Torvalds #include <linux/rtnetlink.h>
561da177e4SLinus Torvalds #include <linux/init.h>
57716ea3a7SDavid Howells #include <linux/scatterlist.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <net/protocol.h>
601da177e4SLinus Torvalds #include <net/dst.h>
611da177e4SLinus Torvalds #include <net/sock.h>
621da177e4SLinus Torvalds #include <net/checksum.h>
631da177e4SLinus Torvalds #include <net/xfrm.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include <asm/uaccess.h>
661da177e4SLinus Torvalds #include <asm/system.h>
671da177e4SLinus Torvalds 
68a1f8e7f7SAl Viro #include "kmap_skb.h"
69a1f8e7f7SAl Viro 
70e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly;
71e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
721da177e4SLinus Torvalds 
739c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
749c55e01cSJens Axboe 				  struct pipe_buffer *buf)
759c55e01cSJens Axboe {
769c55e01cSJens Axboe 	struct sk_buff *skb = (struct sk_buff *) buf->private;
779c55e01cSJens Axboe 
789c55e01cSJens Axboe 	kfree_skb(skb);
799c55e01cSJens Axboe }
809c55e01cSJens Axboe 
819c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
829c55e01cSJens Axboe 				struct pipe_buffer *buf)
839c55e01cSJens Axboe {
849c55e01cSJens Axboe 	struct sk_buff *skb = (struct sk_buff *) buf->private;
859c55e01cSJens Axboe 
869c55e01cSJens Axboe 	skb_get(skb);
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. */
979c55e01cSJens Axboe static 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  */
1211da177e4SLinus Torvalds 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 
1401da177e4SLinus Torvalds 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 
150dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb)
151dc6de336SDavid S. Miller {
152dc6de336SDavid S. Miller 	printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
153dc6de336SDavid S. Miller 	       "len=%u, sizeof(sk_buff)=%Zd\n",
154dc6de336SDavid S. Miller 	       skb->truesize, skb->len, sizeof(struct sk_buff));
155dc6de336SDavid S. Miller }
156dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug);
157dc6de336SDavid S. Miller 
1581da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1591da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1601da177e4SLinus Torvalds  *	[BEEP] leaks.
1611da177e4SLinus Torvalds  *
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /**
165d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1661da177e4SLinus Torvalds  *	@size: size to allocate
1671da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
168c83c2486SRandy Dunlap  *	@fclone: allocate from fclone cache instead of head cache
169c83c2486SRandy Dunlap  *		and allocate a cloned (child) skb
170b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1711da177e4SLinus Torvalds  *
1721da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
1731da177e4SLinus Torvalds  *	tail room of size bytes. The object has a reference count of one.
1741da177e4SLinus Torvalds  *	The return is the buffer. On a failure the return is %NULL.
1751da177e4SLinus Torvalds  *
1761da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1771da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1781da177e4SLinus Torvalds  */
179dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
180b30973f8SChristoph Hellwig 			    int fclone, int node)
1811da177e4SLinus Torvalds {
182e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1834947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1841da177e4SLinus Torvalds 	struct sk_buff *skb;
1851da177e4SLinus Torvalds 	u8 *data;
1861da177e4SLinus Torvalds 
1878798b3fbSHerbert Xu 	cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
1888798b3fbSHerbert Xu 
1891da177e4SLinus Torvalds 	/* Get the HEAD */
190b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1911da177e4SLinus Torvalds 	if (!skb)
1921da177e4SLinus Torvalds 		goto out;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
195b30973f8SChristoph Hellwig 	data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
196b30973f8SChristoph Hellwig 			gfp_mask, node);
1971da177e4SLinus Torvalds 	if (!data)
1981da177e4SLinus Torvalds 		goto nodata;
1991da177e4SLinus Torvalds 
200ca0605a7SArnaldo Carvalho de Melo 	/*
201c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
202c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
203c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
204ca0605a7SArnaldo Carvalho de Melo 	 */
205ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
2061da177e4SLinus Torvalds 	skb->truesize = size + sizeof(struct sk_buff);
2071da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2081da177e4SLinus Torvalds 	skb->head = data;
2091da177e4SLinus Torvalds 	skb->data = data;
21027a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2114305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
2124947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2134947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
2144947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
2154947d3efSBenjamin LaHaise 	shinfo->nr_frags  = 0;
2167967168cSHerbert Xu 	shinfo->gso_size = 0;
2177967168cSHerbert Xu 	shinfo->gso_segs = 0;
2187967168cSHerbert Xu 	shinfo->gso_type = 0;
2194947d3efSBenjamin LaHaise 	shinfo->ip6_frag_id = 0;
2204947d3efSBenjamin LaHaise 	shinfo->frag_list = NULL;
2214947d3efSBenjamin LaHaise 
222d179cd12SDavid S. Miller 	if (fclone) {
223d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
224d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
2251da177e4SLinus Torvalds 
226d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
227d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
228d179cd12SDavid S. Miller 
229d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
230d179cd12SDavid S. Miller 	}
2311da177e4SLinus Torvalds out:
2321da177e4SLinus Torvalds 	return skb;
2331da177e4SLinus Torvalds nodata:
2348798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2351da177e4SLinus Torvalds 	skb = NULL;
2361da177e4SLinus Torvalds 	goto out;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**
2408af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
2418af27456SChristoph Hellwig  *	@dev: network device to receive on
2428af27456SChristoph Hellwig  *	@length: length to allocate
2438af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
2448af27456SChristoph Hellwig  *
2458af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
2468af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
2478af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
2488af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
2498af27456SChristoph Hellwig  *
2508af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
2518af27456SChristoph Hellwig  */
2528af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
2538af27456SChristoph Hellwig 		unsigned int length, gfp_t gfp_mask)
2548af27456SChristoph Hellwig {
25543cb76d9SGreg Kroah-Hartman 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
2568af27456SChristoph Hellwig 	struct sk_buff *skb;
2578af27456SChristoph Hellwig 
258b30973f8SChristoph Hellwig 	skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
2597b2e497aSChristoph Hellwig 	if (likely(skb)) {
2608af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
2617b2e497aSChristoph Hellwig 		skb->dev = dev;
2627b2e497aSChristoph Hellwig 	}
2638af27456SChristoph Hellwig 	return skb;
2648af27456SChristoph Hellwig }
2651da177e4SLinus Torvalds 
266f58518e6SIlpo Järvinen /**
267f58518e6SIlpo Järvinen  *	dev_alloc_skb - allocate an skbuff for receiving
268f58518e6SIlpo Järvinen  *	@length: length to allocate
269f58518e6SIlpo Järvinen  *
270f58518e6SIlpo Järvinen  *	Allocate a new &sk_buff and assign it a usage count of one. The
271f58518e6SIlpo Järvinen  *	buffer has unspecified headroom built in. Users should allocate
272f58518e6SIlpo Järvinen  *	the headroom they think they need without accounting for the
273f58518e6SIlpo Järvinen  *	built in space. The built in space is used for optimisations.
274f58518e6SIlpo Järvinen  *
275f58518e6SIlpo Järvinen  *	%NULL is returned if there is no free memory. Although this function
276f58518e6SIlpo Järvinen  *	allocates memory it can be called from an interrupt.
277f58518e6SIlpo Järvinen  */
278f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length)
279f58518e6SIlpo Järvinen {
2801483b874SDenys Vlasenko 	/*
2811483b874SDenys Vlasenko 	 * There is more code here than it seems:
282a0f55e0eSDavid S. Miller 	 * __dev_alloc_skb is an inline
2831483b874SDenys Vlasenko 	 */
284f58518e6SIlpo Järvinen 	return __dev_alloc_skb(length, GFP_ATOMIC);
285f58518e6SIlpo Järvinen }
286f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb);
287f58518e6SIlpo Järvinen 
28827b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
2891da177e4SLinus Torvalds {
29027b437c8SHerbert Xu 	struct sk_buff *list = *listp;
2911da177e4SLinus Torvalds 
29227b437c8SHerbert Xu 	*listp = NULL;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	do {
2951da177e4SLinus Torvalds 		struct sk_buff *this = list;
2961da177e4SLinus Torvalds 		list = list->next;
2971da177e4SLinus Torvalds 		kfree_skb(this);
2981da177e4SLinus Torvalds 	} while (list);
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
30127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
30227b437c8SHerbert Xu {
30327b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
30427b437c8SHerbert Xu }
30527b437c8SHerbert Xu 
3061da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
3071da177e4SLinus Torvalds {
3081da177e4SLinus Torvalds 	struct sk_buff *list;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
3111da177e4SLinus Torvalds 		skb_get(list);
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
3145bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	if (!skb->cloned ||
3171da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
3181da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
3191da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
3201da177e4SLinus Torvalds 			int i;
3211da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3221da177e4SLinus Torvalds 				put_page(skb_shinfo(skb)->frags[i].page);
3231da177e4SLinus Torvalds 		}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frag_list)
3261da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		kfree(skb->head);
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
3341da177e4SLinus Torvalds  */
3352d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
3361da177e4SLinus Torvalds {
337d179cd12SDavid S. Miller 	struct sk_buff *other;
338d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
339d179cd12SDavid S. Miller 
340d179cd12SDavid S. Miller 	switch (skb->fclone) {
341d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
3421da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
343d179cd12SDavid S. Miller 		break;
344d179cd12SDavid S. Miller 
345d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
346d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
347d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
348d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
349d179cd12SDavid S. Miller 		break;
350d179cd12SDavid S. Miller 
351d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
352d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
353d179cd12SDavid S. Miller 		other = skb - 1;
354d179cd12SDavid S. Miller 
355d179cd12SDavid S. Miller 		/* The clone portion is available for
356d179cd12SDavid S. Miller 		 * fast-cloning again.
357d179cd12SDavid S. Miller 		 */
358d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
359d179cd12SDavid S. Miller 
360d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
361d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
362d179cd12SDavid S. Miller 		break;
3633ff50b79SStephen Hemminger 	}
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
3662d4baff8SHerbert Xu /* Free everything but the sk_buff shell. */
3672d4baff8SHerbert Xu static void skb_release_all(struct sk_buff *skb)
3681da177e4SLinus Torvalds {
3691da177e4SLinus Torvalds 	dst_release(skb->dst);
3701da177e4SLinus Torvalds #ifdef CONFIG_XFRM
3711da177e4SLinus Torvalds 	secpath_put(skb->sp);
3721da177e4SLinus Torvalds #endif
3731da177e4SLinus Torvalds 	if (skb->destructor) {
3749c2b3328SStephen Hemminger 		WARN_ON(in_irq());
3751da177e4SLinus Torvalds 		skb->destructor(skb);
3761da177e4SLinus Torvalds 	}
3779fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
3785f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
3799fb9cbb1SYasuyuki Kozakai 	nf_conntrack_put_reasm(skb->nfct_reasm);
3809fb9cbb1SYasuyuki Kozakai #endif
3811da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
3821da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
3831da177e4SLinus Torvalds #endif
3841da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
3851da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
3861da177e4SLinus Torvalds 	skb->tc_index = 0;
3871da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
3881da177e4SLinus Torvalds 	skb->tc_verd = 0;
3891da177e4SLinus Torvalds #endif
3901da177e4SLinus Torvalds #endif
3912d4baff8SHerbert Xu 	skb_release_data(skb);
3922d4baff8SHerbert Xu }
3931da177e4SLinus Torvalds 
3942d4baff8SHerbert Xu /**
3952d4baff8SHerbert Xu  *	__kfree_skb - private function
3962d4baff8SHerbert Xu  *	@skb: buffer
3972d4baff8SHerbert Xu  *
3982d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
3992d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
4002d4baff8SHerbert Xu  *	always call kfree_skb
4012d4baff8SHerbert Xu  */
4022d4baff8SHerbert Xu 
4032d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
4042d4baff8SHerbert Xu {
4052d4baff8SHerbert Xu 	skb_release_all(skb);
4061da177e4SLinus Torvalds 	kfree_skbmem(skb);
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds /**
410231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
411231d06aeSJörn Engel  *	@skb: buffer to free
412231d06aeSJörn Engel  *
413231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
414231d06aeSJörn Engel  *	hit zero.
415231d06aeSJörn Engel  */
416231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
417231d06aeSJörn Engel {
418231d06aeSJörn Engel 	if (unlikely(!skb))
419231d06aeSJörn Engel 		return;
420231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
421231d06aeSJörn Engel 		smp_rmb();
422231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
423231d06aeSJörn Engel 		return;
424231d06aeSJörn Engel 	__kfree_skb(skb);
425231d06aeSJörn Engel }
426231d06aeSJörn Engel 
427dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
428dec18810SHerbert Xu {
429dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
430dec18810SHerbert Xu 	new->dev		= old->dev;
431dec18810SHerbert Xu 	new->transport_header	= old->transport_header;
432dec18810SHerbert Xu 	new->network_header	= old->network_header;
433dec18810SHerbert Xu 	new->mac_header		= old->mac_header;
434dec18810SHerbert Xu 	new->dst		= dst_clone(old->dst);
435dec18810SHerbert Xu #ifdef CONFIG_INET
436dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
437dec18810SHerbert Xu #endif
438dec18810SHerbert Xu 	memcpy(new->cb, old->cb, sizeof(old->cb));
439dec18810SHerbert Xu 	new->csum_start		= old->csum_start;
440dec18810SHerbert Xu 	new->csum_offset	= old->csum_offset;
441dec18810SHerbert Xu 	new->local_df		= old->local_df;
442dec18810SHerbert Xu 	new->pkt_type		= old->pkt_type;
443dec18810SHerbert Xu 	new->ip_summed		= old->ip_summed;
444dec18810SHerbert Xu 	skb_copy_queue_mapping(new, old);
445dec18810SHerbert Xu 	new->priority		= old->priority;
446dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
447dec18810SHerbert Xu 	new->ipvs_property	= old->ipvs_property;
448dec18810SHerbert Xu #endif
449dec18810SHerbert Xu 	new->protocol		= old->protocol;
450dec18810SHerbert Xu 	new->mark		= old->mark;
451dec18810SHerbert Xu 	__nf_copy(new, old);
452dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
453dec18810SHerbert Xu     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
454dec18810SHerbert Xu 	new->nf_trace		= old->nf_trace;
455dec18810SHerbert Xu #endif
456dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED
457dec18810SHerbert Xu 	new->tc_index		= old->tc_index;
458dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT
459dec18810SHerbert Xu 	new->tc_verd		= old->tc_verd;
460dec18810SHerbert Xu #endif
461dec18810SHerbert Xu #endif
4626aa895b0SPatrick McHardy 	new->vlan_tci		= old->vlan_tci;
4636aa895b0SPatrick McHardy 
464dec18810SHerbert Xu 	skb_copy_secmark(new, old);
465dec18810SHerbert Xu }
466dec18810SHerbert Xu 
467e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds #define C(x) n->x = skb->x
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	n->next = n->prev = NULL;
4721da177e4SLinus Torvalds 	n->sk = NULL;
473dec18810SHerbert Xu 	__copy_skb_header(n, skb);
474dec18810SHerbert Xu 
4751da177e4SLinus Torvalds 	C(len);
4761da177e4SLinus Torvalds 	C(data_len);
4773e6b3b2eSAlexey Dobriyan 	C(mac_len);
478334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
47902f1c89dSPaul Moore 	n->cloned = 1;
4801da177e4SLinus Torvalds 	n->nohdr = 0;
4811da177e4SLinus Torvalds 	n->destructor = NULL;
48202f1c89dSPaul Moore 	C(iif);
4831da177e4SLinus Torvalds 	C(tail);
4841da177e4SLinus Torvalds 	C(end);
48502f1c89dSPaul Moore 	C(head);
48602f1c89dSPaul Moore 	C(data);
48702f1c89dSPaul Moore 	C(truesize);
48802f1c89dSPaul Moore 	atomic_set(&n->users, 1);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
4911da177e4SLinus Torvalds 	skb->cloned = 1;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	return n;
494e0053ec0SHerbert Xu #undef C
495e0053ec0SHerbert Xu }
496e0053ec0SHerbert Xu 
497e0053ec0SHerbert Xu /**
498e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
499e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
500e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
501e0053ec0SHerbert Xu  *
502e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
503e0053ec0SHerbert Xu  *	supplied by the user.
504e0053ec0SHerbert Xu  *
505e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
506e0053ec0SHerbert Xu  */
507e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
508e0053ec0SHerbert Xu {
5092d4baff8SHerbert Xu 	skb_release_all(dst);
510e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
511e0053ec0SHerbert Xu }
512e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
513e0053ec0SHerbert Xu 
514e0053ec0SHerbert Xu /**
515e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
516e0053ec0SHerbert Xu  *	@skb: buffer to clone
517e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
518e0053ec0SHerbert Xu  *
519e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
520e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
521e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
522e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
523e0053ec0SHerbert Xu  *
524e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
525e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
526e0053ec0SHerbert Xu  */
527e0053ec0SHerbert Xu 
528e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
529e0053ec0SHerbert Xu {
530e0053ec0SHerbert Xu 	struct sk_buff *n;
531e0053ec0SHerbert Xu 
532e0053ec0SHerbert Xu 	n = skb + 1;
533e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
534e0053ec0SHerbert Xu 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
535e0053ec0SHerbert Xu 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
536e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
537e0053ec0SHerbert Xu 		atomic_inc(fclone_ref);
538e0053ec0SHerbert Xu 	} else {
539e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
540e0053ec0SHerbert Xu 		if (!n)
541e0053ec0SHerbert Xu 			return NULL;
542e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
543e0053ec0SHerbert Xu 	}
544e0053ec0SHerbert Xu 
545e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
5491da177e4SLinus Torvalds {
5502e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
5511da177e4SLinus Torvalds 	/*
5521da177e4SLinus Torvalds 	 *	Shift between the two data areas in bytes
5531da177e4SLinus Torvalds 	 */
5541da177e4SLinus Torvalds 	unsigned long offset = new->data - old->data;
5552e07fa9cSArnaldo Carvalho de Melo #endif
556dec18810SHerbert Xu 
557dec18810SHerbert Xu 	__copy_skb_header(new, old);
558dec18810SHerbert Xu 
5592e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
5602e07fa9cSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header are relative to skb->head */
5612e07fa9cSArnaldo Carvalho de Melo 	new->transport_header += offset;
5622e07fa9cSArnaldo Carvalho de Melo 	new->network_header   += offset;
5632e07fa9cSArnaldo Carvalho de Melo 	new->mac_header	      += offset;
5642e07fa9cSArnaldo Carvalho de Melo #endif
5657967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
5667967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
5677967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds /**
5711da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
5721da177e4SLinus Torvalds  *	@skb: buffer to copy
5731da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
5741da177e4SLinus Torvalds  *
5751da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
5761da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
5771da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
5781da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
5791da177e4SLinus Torvalds  *
5801da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
5811da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
5821da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
5831da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
5841da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
5851da177e4SLinus Torvalds  */
5861da177e4SLinus Torvalds 
587dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	int headerlen = skb->data - skb->head;
5901da177e4SLinus Torvalds 	/*
5911da177e4SLinus Torvalds 	 *	Allocate the copy buffer
5921da177e4SLinus Torvalds 	 */
5934305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
5944305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
5954305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end + skb->data_len, gfp_mask);
5964305b541SArnaldo Carvalho de Melo #else
5974305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
5984305b541SArnaldo Carvalho de Melo #endif
5991da177e4SLinus Torvalds 	if (!n)
6001da177e4SLinus Torvalds 		return NULL;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	/* Set the data pointer */
6031da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
6041da177e4SLinus Torvalds 	/* Set the tail pointer and length */
6051da177e4SLinus Torvalds 	skb_put(n, skb->len);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
6081da177e4SLinus Torvalds 		BUG();
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	copy_skb_header(n, skb);
6111da177e4SLinus Torvalds 	return n;
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds /**
6161da177e4SLinus Torvalds  *	pskb_copy	-	create copy of an sk_buff with private head.
6171da177e4SLinus Torvalds  *	@skb: buffer to copy
6181da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6191da177e4SLinus Torvalds  *
6201da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
6211da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
6221da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
6231da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
6241da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
6251da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
6261da177e4SLinus Torvalds  */
6271da177e4SLinus Torvalds 
628dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
6291da177e4SLinus Torvalds {
6301da177e4SLinus Torvalds 	/*
6311da177e4SLinus Torvalds 	 *	Allocate the copy buffer
6321da177e4SLinus Torvalds 	 */
6334305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
6344305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
6354305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end, gfp_mask);
6364305b541SArnaldo Carvalho de Melo #else
6374305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head, gfp_mask);
6384305b541SArnaldo Carvalho de Melo #endif
6391da177e4SLinus Torvalds 	if (!n)
6401da177e4SLinus Torvalds 		goto out;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	/* Set the data pointer */
6431da177e4SLinus Torvalds 	skb_reserve(n, skb->data - skb->head);
6441da177e4SLinus Torvalds 	/* Set the tail pointer and length */
6451da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
6461da177e4SLinus Torvalds 	/* Copy the bytes */
647d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
6481da177e4SLinus Torvalds 
64925f484a6SHerbert Xu 	n->truesize += skb->data_len;
6501da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
6511da177e4SLinus Torvalds 	n->len	     = skb->len;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
6541da177e4SLinus Torvalds 		int i;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6571da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
6581da177e4SLinus Torvalds 			get_page(skb_shinfo(n)->frags[i].page);
6591da177e4SLinus Torvalds 		}
6601da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
6641da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
6651da177e4SLinus Torvalds 		skb_clone_fraglist(n);
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	copy_skb_header(n, skb);
6691da177e4SLinus Torvalds out:
6701da177e4SLinus Torvalds 	return n;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds /**
6741da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
6751da177e4SLinus Torvalds  *	@skb: buffer to reallocate
6761da177e4SLinus Torvalds  *	@nhead: room to add at head
6771da177e4SLinus Torvalds  *	@ntail: room to add at tail
6781da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6791da177e4SLinus Torvalds  *
6801da177e4SLinus Torvalds  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
6811da177e4SLinus Torvalds  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
6821da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
6831da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
6861da177e4SLinus Torvalds  *	reloaded after call to this function.
6871da177e4SLinus Torvalds  */
6881da177e4SLinus Torvalds 
68986a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
690dd0fc66fSAl Viro 		     gfp_t gfp_mask)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds 	int i;
6931da177e4SLinus Torvalds 	u8 *data;
6944305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
6954305b541SArnaldo Carvalho de Melo 	int size = nhead + skb->end + ntail;
6964305b541SArnaldo Carvalho de Melo #else
6971da177e4SLinus Torvalds 	int size = nhead + (skb->end - skb->head) + ntail;
6984305b541SArnaldo Carvalho de Melo #endif
6991da177e4SLinus Torvalds 	long off;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (skb_shared(skb))
7021da177e4SLinus Torvalds 		BUG();
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
7071da177e4SLinus Torvalds 	if (!data)
7081da177e4SLinus Torvalds 		goto nodata;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
7111da177e4SLinus Torvalds 	 * optimized for the cases when header is void. */
7124305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
713b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail);
7144305b541SArnaldo Carvalho de Melo #else
715b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail - skb->head);
71627a884dcSArnaldo Carvalho de Melo #endif
7174305b541SArnaldo Carvalho de Melo 	memcpy(data + size, skb_end_pointer(skb),
7184305b541SArnaldo Carvalho de Melo 	       sizeof(struct skb_shared_info));
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
7211da177e4SLinus Torvalds 		get_page(skb_shinfo(skb)->frags[i].page);
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list)
7241da177e4SLinus Torvalds 		skb_clone_fraglist(skb);
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	skb_release_data(skb);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	skb->head     = data;
7311da177e4SLinus Torvalds 	skb->data    += off;
7324305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
7334305b541SArnaldo Carvalho de Melo 	skb->end      = size;
73456eb8882SPatrick McHardy 	off           = nhead;
7354305b541SArnaldo Carvalho de Melo #else
7364305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
73756eb8882SPatrick McHardy #endif
73827a884dcSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header and tail are relative to skb->head */
73927a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
740b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header += off;
741b0e380b1SArnaldo Carvalho de Melo 	skb->network_header   += off;
742b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header	      += off;
743172a863fSHerbert Xu 	skb->csum_start       += nhead;
7441da177e4SLinus Torvalds 	skb->cloned   = 0;
745334a8132SPatrick McHardy 	skb->hdr_len  = 0;
7461da177e4SLinus Torvalds 	skb->nohdr    = 0;
7471da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
7481da177e4SLinus Torvalds 	return 0;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds nodata:
7511da177e4SLinus Torvalds 	return -ENOMEM;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	struct sk_buff *skb2;
7591da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	if (delta <= 0)
7621da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
7631da177e4SLinus Torvalds 	else {
7641da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
7651da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
7661da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
7671da177e4SLinus Torvalds 			kfree_skb(skb2);
7681da177e4SLinus Torvalds 			skb2 = NULL;
7691da177e4SLinus Torvalds 		}
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 	return skb2;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds /**
7761da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
7771da177e4SLinus Torvalds  *	@skb: buffer to copy
7781da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
7791da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
7801da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7811da177e4SLinus Torvalds  *
7821da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
7831da177e4SLinus Torvalds  *	allocate additional space.
7841da177e4SLinus Torvalds  *
7851da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
7861da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
7871da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
7881da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
7891da177e4SLinus Torvalds  *
7901da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
7911da177e4SLinus Torvalds  *	is called from an interrupt.
7921da177e4SLinus Torvalds  */
7931da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
79486a76cafSVictor Fusco 				int newheadroom, int newtailroom,
795dd0fc66fSAl Viro 				gfp_t gfp_mask)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	/*
7981da177e4SLinus Torvalds 	 *	Allocate the copy buffer
7991da177e4SLinus Torvalds 	 */
8001da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
8011da177e4SLinus Torvalds 				      gfp_mask);
802efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
8031da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
80452886051SHerbert Xu 	int off;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	if (!n)
8071da177e4SLinus Torvalds 		return NULL;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	/* Set the tail pointer and length */
8121da177e4SLinus Torvalds 	skb_put(n, skb->len);
8131da177e4SLinus Torvalds 
814efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
8151da177e4SLinus Torvalds 	head_copy_off = 0;
8161da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
8171da177e4SLinus Torvalds 		head_copy_len = newheadroom;
8181da177e4SLinus Torvalds 	else
8191da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/* Copy the linear header and data. */
8221da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
8231da177e4SLinus Torvalds 			  skb->len + head_copy_len))
8241da177e4SLinus Torvalds 		BUG();
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	copy_skb_header(n, skb);
8271da177e4SLinus Torvalds 
828efd1e8d5SPatrick McHardy 	off                  = newheadroom - oldheadroom;
82952886051SHerbert Xu 	n->csum_start       += off;
83052886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET
831efd1e8d5SPatrick McHardy 	n->transport_header += off;
832efd1e8d5SPatrick McHardy 	n->network_header   += off;
833efd1e8d5SPatrick McHardy 	n->mac_header	    += off;
83452886051SHerbert Xu #endif
835efd1e8d5SPatrick McHardy 
8361da177e4SLinus Torvalds 	return n;
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds /**
8401da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
8411da177e4SLinus Torvalds  *	@skb: buffer to pad
8421da177e4SLinus Torvalds  *	@pad: space to pad
8431da177e4SLinus Torvalds  *
8441da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
8451da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
8461da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
8471da177e4SLinus Torvalds  *
8485b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
8491da177e4SLinus Torvalds  */
8501da177e4SLinus Torvalds 
8515b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
8521da177e4SLinus Torvalds {
8535b057c6bSHerbert Xu 	int err;
8545b057c6bSHerbert Xu 	int ntail;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
8575b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
8581da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
8595b057c6bSHerbert Xu 		return 0;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8624305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
8635b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
8645b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
8655b057c6bSHerbert Xu 		if (unlikely(err))
8665b057c6bSHerbert Xu 			goto free_skb;
8675b057c6bSHerbert Xu 	}
8685b057c6bSHerbert Xu 
8695b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
8705b057c6bSHerbert Xu 	 * to be audited.
8715b057c6bSHerbert Xu 	 */
8725b057c6bSHerbert Xu 	err = skb_linearize(skb);
8735b057c6bSHerbert Xu 	if (unlikely(err))
8745b057c6bSHerbert Xu 		goto free_skb;
8755b057c6bSHerbert Xu 
8765b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
8775b057c6bSHerbert Xu 	return 0;
8785b057c6bSHerbert Xu 
8795b057c6bSHerbert Xu free_skb:
8801da177e4SLinus Torvalds 	kfree_skb(skb);
8815b057c6bSHerbert Xu 	return err;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8840dde3e16SIlpo Järvinen /**
8850dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
8860dde3e16SIlpo Järvinen  *	@skb: buffer to use
8870dde3e16SIlpo Järvinen  *	@len: amount of data to add
8880dde3e16SIlpo Järvinen  *
8890dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
8900dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
8910dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
8920dde3e16SIlpo Järvinen  */
8930dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
8940dde3e16SIlpo Järvinen {
8950dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
8960dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
8970dde3e16SIlpo Järvinen 	skb->tail += len;
8980dde3e16SIlpo Järvinen 	skb->len  += len;
8990dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
9000dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
9010dde3e16SIlpo Järvinen 	return tmp;
9020dde3e16SIlpo Järvinen }
9030dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
9040dde3e16SIlpo Järvinen 
9056be8ac2fSIlpo Järvinen /**
906c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
907c2aa270aSIlpo Järvinen  *	@skb: buffer to use
908c2aa270aSIlpo Järvinen  *	@len: amount of data to add
909c2aa270aSIlpo Järvinen  *
910c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
911c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
912c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
913c2aa270aSIlpo Järvinen  */
914c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
915c2aa270aSIlpo Järvinen {
916c2aa270aSIlpo Järvinen 	skb->data -= len;
917c2aa270aSIlpo Järvinen 	skb->len  += len;
918c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
919c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
920c2aa270aSIlpo Järvinen 	return skb->data;
921c2aa270aSIlpo Järvinen }
922c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
923c2aa270aSIlpo Järvinen 
924c2aa270aSIlpo Järvinen /**
9256be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
9266be8ac2fSIlpo Järvinen  *	@skb: buffer to use
9276be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
9286be8ac2fSIlpo Järvinen  *
9296be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
9306be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
9316be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
9326be8ac2fSIlpo Järvinen  *	the old data.
9336be8ac2fSIlpo Järvinen  */
9346be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
9356be8ac2fSIlpo Järvinen {
9366be8ac2fSIlpo Järvinen 	return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
9376be8ac2fSIlpo Järvinen }
9386be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
9396be8ac2fSIlpo Järvinen 
940419ae74eSIlpo Järvinen /**
941419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
942419ae74eSIlpo Järvinen  *	@skb: buffer to alter
943419ae74eSIlpo Järvinen  *	@len: new length
944419ae74eSIlpo Järvinen  *
945419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
946419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
947419ae74eSIlpo Järvinen  *	The skb must be linear.
948419ae74eSIlpo Järvinen  */
949419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
950419ae74eSIlpo Järvinen {
951419ae74eSIlpo Järvinen 	if (skb->len > len)
952419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
953419ae74eSIlpo Järvinen }
954419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
955419ae74eSIlpo Järvinen 
9563cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
9571da177e4SLinus Torvalds  */
9581da177e4SLinus Torvalds 
9593cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
9601da177e4SLinus Torvalds {
96127b437c8SHerbert Xu 	struct sk_buff **fragp;
96227b437c8SHerbert Xu 	struct sk_buff *frag;
9631da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
9641da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
9651da177e4SLinus Torvalds 	int i;
96627b437c8SHerbert Xu 	int err;
96727b437c8SHerbert Xu 
96827b437c8SHerbert Xu 	if (skb_cloned(skb) &&
96927b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
97027b437c8SHerbert Xu 		return err;
9711da177e4SLinus Torvalds 
972f4d26fb3SHerbert Xu 	i = 0;
973f4d26fb3SHerbert Xu 	if (offset >= len)
974f4d26fb3SHerbert Xu 		goto drop_pages;
975f4d26fb3SHerbert Xu 
976f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
9771da177e4SLinus Torvalds 		int end = offset + skb_shinfo(skb)->frags[i].size;
97827b437c8SHerbert Xu 
97927b437c8SHerbert Xu 		if (end < len) {
9801da177e4SLinus Torvalds 			offset = end;
98127b437c8SHerbert Xu 			continue;
9821da177e4SLinus Torvalds 		}
9831da177e4SLinus Torvalds 
98427b437c8SHerbert Xu 		skb_shinfo(skb)->frags[i++].size = len - offset;
98527b437c8SHerbert Xu 
986f4d26fb3SHerbert Xu drop_pages:
98727b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
98827b437c8SHerbert Xu 
98927b437c8SHerbert Xu 		for (; i < nfrags; i++)
99027b437c8SHerbert Xu 			put_page(skb_shinfo(skb)->frags[i].page);
99127b437c8SHerbert Xu 
99227b437c8SHerbert Xu 		if (skb_shinfo(skb)->frag_list)
99327b437c8SHerbert Xu 			skb_drop_fraglist(skb);
994f4d26fb3SHerbert Xu 		goto done;
99527b437c8SHerbert Xu 	}
99627b437c8SHerbert Xu 
99727b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
99827b437c8SHerbert Xu 	     fragp = &frag->next) {
99927b437c8SHerbert Xu 		int end = offset + frag->len;
100027b437c8SHerbert Xu 
100127b437c8SHerbert Xu 		if (skb_shared(frag)) {
100227b437c8SHerbert Xu 			struct sk_buff *nfrag;
100327b437c8SHerbert Xu 
100427b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
100527b437c8SHerbert Xu 			if (unlikely(!nfrag))
100627b437c8SHerbert Xu 				return -ENOMEM;
100727b437c8SHerbert Xu 
100827b437c8SHerbert Xu 			nfrag->next = frag->next;
1009f4d26fb3SHerbert Xu 			kfree_skb(frag);
101027b437c8SHerbert Xu 			frag = nfrag;
101127b437c8SHerbert Xu 			*fragp = frag;
101227b437c8SHerbert Xu 		}
101327b437c8SHerbert Xu 
101427b437c8SHerbert Xu 		if (end < len) {
101527b437c8SHerbert Xu 			offset = end;
101627b437c8SHerbert Xu 			continue;
101727b437c8SHerbert Xu 		}
101827b437c8SHerbert Xu 
101927b437c8SHerbert Xu 		if (end > len &&
102027b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
102127b437c8SHerbert Xu 			return err;
102227b437c8SHerbert Xu 
102327b437c8SHerbert Xu 		if (frag->next)
102427b437c8SHerbert Xu 			skb_drop_list(&frag->next);
102527b437c8SHerbert Xu 		break;
102627b437c8SHerbert Xu 	}
102727b437c8SHerbert Xu 
1028f4d26fb3SHerbert Xu done:
102927b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
10301da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
10311da177e4SLinus Torvalds 		skb->len       = len;
10321da177e4SLinus Torvalds 	} else {
10331da177e4SLinus Torvalds 		skb->len       = len;
10341da177e4SLinus Torvalds 		skb->data_len  = 0;
103527a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	return 0;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds /**
10421da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
10431da177e4SLinus Torvalds  *	@skb: buffer to reallocate
10441da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
10451da177e4SLinus Torvalds  *
10461da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
10471da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
10481da177e4SLinus Torvalds  *	data from fragmented part.
10491da177e4SLinus Torvalds  *
10501da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
10511da177e4SLinus Torvalds  *
10521da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
10531da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
10541da177e4SLinus Torvalds  *
10551da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
10561da177e4SLinus Torvalds  *	reloaded after call to this function.
10571da177e4SLinus Torvalds  */
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
10601da177e4SLinus Torvalds  * when it is necessary.
10611da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
10621da177e4SLinus Torvalds  * 2. It may change skb pointers.
10631da177e4SLinus Torvalds  *
10641da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
10651da177e4SLinus Torvalds  */
10661da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
10671da177e4SLinus Torvalds {
10681da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
10691da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
10701da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
10711da177e4SLinus Torvalds 	 */
10724305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
10751da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
10761da177e4SLinus Torvalds 				     GFP_ATOMIC))
10771da177e4SLinus Torvalds 			return NULL;
10781da177e4SLinus Torvalds 	}
10791da177e4SLinus Torvalds 
108027a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
10811da177e4SLinus Torvalds 		BUG();
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
10841da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
10851da177e4SLinus Torvalds 	 */
10861da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->frag_list)
10871da177e4SLinus Torvalds 		goto pull_pages;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
10901da177e4SLinus Torvalds 	eat = delta;
10911da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
10921da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size >= eat)
10931da177e4SLinus Torvalds 			goto pull_pages;
10941da177e4SLinus Torvalds 		eat -= skb_shinfo(skb)->frags[i].size;
10951da177e4SLinus Torvalds 	}
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
10981da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
10991da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
11001da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
11011da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
11021da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
11031da177e4SLinus Torvalds 	 */
11041da177e4SLinus Torvalds 	if (eat) {
11051da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
11061da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
11071da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 		do {
111009a62660SKris Katterjohn 			BUG_ON(!list);
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 			if (list->len <= eat) {
11131da177e4SLinus Torvalds 				/* Eaten as whole. */
11141da177e4SLinus Torvalds 				eat -= list->len;
11151da177e4SLinus Torvalds 				list = list->next;
11161da177e4SLinus Torvalds 				insp = list;
11171da177e4SLinus Torvalds 			} else {
11181da177e4SLinus Torvalds 				/* Eaten partially. */
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds 				if (skb_shared(list)) {
11211da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
11221da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
11231da177e4SLinus Torvalds 					if (!clone)
11241da177e4SLinus Torvalds 						return NULL;
11251da177e4SLinus Torvalds 					insp = list->next;
11261da177e4SLinus Torvalds 					list = clone;
11271da177e4SLinus Torvalds 				} else {
11281da177e4SLinus Torvalds 					/* This may be pulled without
11291da177e4SLinus Torvalds 					 * problems. */
11301da177e4SLinus Torvalds 					insp = list;
11311da177e4SLinus Torvalds 				}
11321da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
11331da177e4SLinus Torvalds 					if (clone)
11341da177e4SLinus Torvalds 						kfree_skb(clone);
11351da177e4SLinus Torvalds 					return NULL;
11361da177e4SLinus Torvalds 				}
11371da177e4SLinus Torvalds 				break;
11381da177e4SLinus Torvalds 			}
11391da177e4SLinus Torvalds 		} while (eat);
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 		/* Free pulled out fragments. */
11421da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
11431da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
11441da177e4SLinus Torvalds 			kfree_skb(list);
11451da177e4SLinus Torvalds 		}
11461da177e4SLinus Torvalds 		/* And insert new clone at head. */
11471da177e4SLinus Torvalds 		if (clone) {
11481da177e4SLinus Torvalds 			clone->next = list;
11491da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
11501da177e4SLinus Torvalds 		}
11511da177e4SLinus Torvalds 	}
11521da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds pull_pages:
11551da177e4SLinus Torvalds 	eat = delta;
11561da177e4SLinus Torvalds 	k = 0;
11571da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
11581da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size <= eat) {
11591da177e4SLinus Torvalds 			put_page(skb_shinfo(skb)->frags[i].page);
11601da177e4SLinus Torvalds 			eat -= skb_shinfo(skb)->frags[i].size;
11611da177e4SLinus Torvalds 		} else {
11621da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
11631da177e4SLinus Torvalds 			if (eat) {
11641da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
11651da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].size -= eat;
11661da177e4SLinus Torvalds 				eat = 0;
11671da177e4SLinus Torvalds 			}
11681da177e4SLinus Torvalds 			k++;
11691da177e4SLinus Torvalds 		}
11701da177e4SLinus Torvalds 	}
11711da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	skb->tail     += delta;
11741da177e4SLinus Torvalds 	skb->data_len -= delta;
11751da177e4SLinus Torvalds 
117627a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
11821da177e4SLinus Torvalds {
11831da177e4SLinus Torvalds 	int i, copy;
11841a028e50SDavid S. Miller 	int start = skb_headlen(skb);
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
11871da177e4SLinus Torvalds 		goto fault;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	/* Copy header. */
11901a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
11911da177e4SLinus Torvalds 		if (copy > len)
11921da177e4SLinus Torvalds 			copy = len;
1193d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
11941da177e4SLinus Torvalds 		if ((len -= copy) == 0)
11951da177e4SLinus Torvalds 			return 0;
11961da177e4SLinus Torvalds 		offset += copy;
11971da177e4SLinus Torvalds 		to     += copy;
11981da177e4SLinus Torvalds 	}
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12011a028e50SDavid S. Miller 		int end;
12021da177e4SLinus Torvalds 
12031a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
12041a028e50SDavid S. Miller 
12051a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
12061da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
12071da177e4SLinus Torvalds 			u8 *vaddr;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 			if (copy > len)
12101da177e4SLinus Torvalds 				copy = len;
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
12131da177e4SLinus Torvalds 			memcpy(to,
12141a028e50SDavid S. Miller 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
12151a028e50SDavid S. Miller 			       offset - start, copy);
12161da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 			if ((len -= copy) == 0)
12191da177e4SLinus Torvalds 				return 0;
12201da177e4SLinus Torvalds 			offset += copy;
12211da177e4SLinus Torvalds 			to     += copy;
12221da177e4SLinus Torvalds 		}
12231a028e50SDavid S. Miller 		start = end;
12241da177e4SLinus Torvalds 	}
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
12271da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 		for (; list; list = list->next) {
12301a028e50SDavid S. Miller 			int end;
12311da177e4SLinus Torvalds 
12321a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
12331a028e50SDavid S. Miller 
12341a028e50SDavid S. Miller 			end = start + list->len;
12351da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
12361da177e4SLinus Torvalds 				if (copy > len)
12371da177e4SLinus Torvalds 					copy = len;
12381a028e50SDavid S. Miller 				if (skb_copy_bits(list, offset - start,
12391a028e50SDavid S. Miller 						  to, copy))
12401da177e4SLinus Torvalds 					goto fault;
12411da177e4SLinus Torvalds 				if ((len -= copy) == 0)
12421da177e4SLinus Torvalds 					return 0;
12431da177e4SLinus Torvalds 				offset += copy;
12441da177e4SLinus Torvalds 				to     += copy;
12451da177e4SLinus Torvalds 			}
12461a028e50SDavid S. Miller 			start = end;
12471da177e4SLinus Torvalds 		}
12481da177e4SLinus Torvalds 	}
12491da177e4SLinus Torvalds 	if (!len)
12501da177e4SLinus Torvalds 		return 0;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds fault:
12531da177e4SLinus Torvalds 	return -EFAULT;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12569c55e01cSJens Axboe /*
12579c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
12589c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
12599c55e01cSJens Axboe  */
12609c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
12619c55e01cSJens Axboe {
12629c55e01cSJens Axboe 	struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
12639c55e01cSJens Axboe 
12649c55e01cSJens Axboe 	kfree_skb(skb);
12659c55e01cSJens Axboe }
12669c55e01cSJens Axboe 
12679c55e01cSJens Axboe /*
12689c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
12699c55e01cSJens Axboe  */
12709c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
12719c55e01cSJens Axboe 				unsigned int len, unsigned int offset,
12729c55e01cSJens Axboe 				struct sk_buff *skb)
12739c55e01cSJens Axboe {
12749c55e01cSJens Axboe 	if (unlikely(spd->nr_pages == PIPE_BUFFERS))
12759c55e01cSJens Axboe 		return 1;
12769c55e01cSJens Axboe 
12779c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
12789c55e01cSJens Axboe 	spd->partial[spd->nr_pages].len = len;
12799c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
12809c55e01cSJens Axboe 	spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
12819c55e01cSJens Axboe 	spd->nr_pages++;
12829c55e01cSJens Axboe 	return 0;
12839c55e01cSJens Axboe }
12849c55e01cSJens Axboe 
12859c55e01cSJens Axboe /*
12869c55e01cSJens Axboe  * Map linear and fragment data from the skb to spd. Returns number of
12879c55e01cSJens Axboe  * pages mapped.
12889c55e01cSJens Axboe  */
12899c55e01cSJens Axboe static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
12909c55e01cSJens Axboe 			     unsigned int *total_len,
12919c55e01cSJens Axboe 			     struct splice_pipe_desc *spd)
12929c55e01cSJens Axboe {
12939c55e01cSJens Axboe 	unsigned int nr_pages = spd->nr_pages;
12949c55e01cSJens Axboe 	unsigned int poff, plen, len, toff, tlen;
1295db43a282SOctavian Purdila 	int headlen, seg, error = 0;
12969c55e01cSJens Axboe 
12979c55e01cSJens Axboe 	toff = *offset;
12989c55e01cSJens Axboe 	tlen = *total_len;
1299db43a282SOctavian Purdila 	if (!tlen) {
1300db43a282SOctavian Purdila 		error = 1;
13019c55e01cSJens Axboe 		goto err;
1302db43a282SOctavian Purdila 	}
13039c55e01cSJens Axboe 
13049c55e01cSJens Axboe 	/*
13059c55e01cSJens Axboe 	 * if the offset is greater than the linear part, go directly to
13069c55e01cSJens Axboe 	 * the fragments.
13079c55e01cSJens Axboe 	 */
13089c55e01cSJens Axboe 	headlen = skb_headlen(skb);
13099c55e01cSJens Axboe 	if (toff >= headlen) {
13109c55e01cSJens Axboe 		toff -= headlen;
13119c55e01cSJens Axboe 		goto map_frag;
13129c55e01cSJens Axboe 	}
13139c55e01cSJens Axboe 
13149c55e01cSJens Axboe 	/*
13159c55e01cSJens Axboe 	 * first map the linear region into the pages/partial map, skipping
13169c55e01cSJens Axboe 	 * any potential initial offset.
13179c55e01cSJens Axboe 	 */
13189c55e01cSJens Axboe 	len = 0;
13199c55e01cSJens Axboe 	while (len < headlen) {
13209c55e01cSJens Axboe 		void *p = skb->data + len;
13219c55e01cSJens Axboe 
13229c55e01cSJens Axboe 		poff = (unsigned long) p & (PAGE_SIZE - 1);
13239c55e01cSJens Axboe 		plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff);
13249c55e01cSJens Axboe 		len += plen;
13259c55e01cSJens Axboe 
13269c55e01cSJens Axboe 		if (toff) {
13279c55e01cSJens Axboe 			if (plen <= toff) {
13289c55e01cSJens Axboe 				toff -= plen;
13299c55e01cSJens Axboe 				continue;
13309c55e01cSJens Axboe 			}
13319c55e01cSJens Axboe 			plen -= toff;
13329c55e01cSJens Axboe 			poff += toff;
13339c55e01cSJens Axboe 			toff = 0;
13349c55e01cSJens Axboe 		}
13359c55e01cSJens Axboe 
13369c55e01cSJens Axboe 		plen = min(plen, tlen);
13379c55e01cSJens Axboe 		if (!plen)
13389c55e01cSJens Axboe 			break;
13399c55e01cSJens Axboe 
13409c55e01cSJens Axboe 		/*
13419c55e01cSJens Axboe 		 * just jump directly to update and return, no point
13429c55e01cSJens Axboe 		 * in going over fragments when the output is full.
13439c55e01cSJens Axboe 		 */
1344db43a282SOctavian Purdila 		error = spd_fill_page(spd, virt_to_page(p), plen, poff, skb);
1345db43a282SOctavian Purdila 		if (error)
13469c55e01cSJens Axboe 			goto done;
13479c55e01cSJens Axboe 
13489c55e01cSJens Axboe 		tlen -= plen;
13499c55e01cSJens Axboe 	}
13509c55e01cSJens Axboe 
13519c55e01cSJens Axboe 	/*
13529c55e01cSJens Axboe 	 * then map the fragments
13539c55e01cSJens Axboe 	 */
13549c55e01cSJens Axboe map_frag:
13559c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
13569c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
13579c55e01cSJens Axboe 
13589c55e01cSJens Axboe 		plen = f->size;
13599c55e01cSJens Axboe 		poff = f->page_offset;
13609c55e01cSJens Axboe 
13619c55e01cSJens Axboe 		if (toff) {
13629c55e01cSJens Axboe 			if (plen <= toff) {
13639c55e01cSJens Axboe 				toff -= plen;
13649c55e01cSJens Axboe 				continue;
13659c55e01cSJens Axboe 			}
13669c55e01cSJens Axboe 			plen -= toff;
13679c55e01cSJens Axboe 			poff += toff;
13689c55e01cSJens Axboe 			toff = 0;
13699c55e01cSJens Axboe 		}
13709c55e01cSJens Axboe 
13719c55e01cSJens Axboe 		plen = min(plen, tlen);
13729c55e01cSJens Axboe 		if (!plen)
13739c55e01cSJens Axboe 			break;
13749c55e01cSJens Axboe 
1375db43a282SOctavian Purdila 		error = spd_fill_page(spd, f->page, plen, poff, skb);
1376db43a282SOctavian Purdila 		if (error)
13779c55e01cSJens Axboe 			break;
13789c55e01cSJens Axboe 
13799c55e01cSJens Axboe 		tlen -= plen;
13809c55e01cSJens Axboe 	}
13819c55e01cSJens Axboe 
13829c55e01cSJens Axboe done:
13839c55e01cSJens Axboe 	if (spd->nr_pages - nr_pages) {
13849c55e01cSJens Axboe 		*offset = 0;
13859c55e01cSJens Axboe 		*total_len = tlen;
13869c55e01cSJens Axboe 		return 0;
13879c55e01cSJens Axboe 	}
13889c55e01cSJens Axboe err:
1389db43a282SOctavian Purdila 	/* update the offset to reflect the linear part skip, if any */
1390db43a282SOctavian Purdila 	if (!error)
1391db43a282SOctavian Purdila 		*offset = toff;
1392db43a282SOctavian Purdila 	return error;
13939c55e01cSJens Axboe }
13949c55e01cSJens Axboe 
13959c55e01cSJens Axboe /*
13969c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
13979c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
13989c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
13999c55e01cSJens Axboe  * handle that cleanly.
14009c55e01cSJens Axboe  */
14019c55e01cSJens Axboe int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
14029c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
14039c55e01cSJens Axboe 		    unsigned int flags)
14049c55e01cSJens Axboe {
14059c55e01cSJens Axboe 	struct partial_page partial[PIPE_BUFFERS];
14069c55e01cSJens Axboe 	struct page *pages[PIPE_BUFFERS];
14079c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
14089c55e01cSJens Axboe 		.pages = pages,
14099c55e01cSJens Axboe 		.partial = partial,
14109c55e01cSJens Axboe 		.flags = flags,
14119c55e01cSJens Axboe 		.ops = &sock_pipe_buf_ops,
14129c55e01cSJens Axboe 		.spd_release = sock_spd_release,
14139c55e01cSJens Axboe 	};
14149c55e01cSJens Axboe 	struct sk_buff *skb;
14159c55e01cSJens Axboe 
14169c55e01cSJens Axboe 	/*
14179c55e01cSJens Axboe 	 * I'd love to avoid the clone here, but tcp_read_sock()
14189c55e01cSJens Axboe 	 * ignores reference counts and unconditonally kills the sk_buff
14199c55e01cSJens Axboe 	 * on return from the actor.
14209c55e01cSJens Axboe 	 */
14219c55e01cSJens Axboe 	skb = skb_clone(__skb, GFP_KERNEL);
14229c55e01cSJens Axboe 	if (unlikely(!skb))
14239c55e01cSJens Axboe 		return -ENOMEM;
14249c55e01cSJens Axboe 
14259c55e01cSJens Axboe 	/*
14269c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
14279c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
14289c55e01cSJens Axboe 	 */
14299c55e01cSJens Axboe 	if (__skb_splice_bits(skb, &offset, &tlen, &spd))
14309c55e01cSJens Axboe 		goto done;
14319c55e01cSJens Axboe 	else if (!tlen)
14329c55e01cSJens Axboe 		goto done;
14339c55e01cSJens Axboe 
14349c55e01cSJens Axboe 	/*
14359c55e01cSJens Axboe 	 * now see if we have a frag_list to map
14369c55e01cSJens Axboe 	 */
14379c55e01cSJens Axboe 	if (skb_shinfo(skb)->frag_list) {
14389c55e01cSJens Axboe 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
14399c55e01cSJens Axboe 
14409c55e01cSJens Axboe 		for (; list && tlen; list = list->next) {
14419c55e01cSJens Axboe 			if (__skb_splice_bits(list, &offset, &tlen, &spd))
14429c55e01cSJens Axboe 				break;
14439c55e01cSJens Axboe 		}
14449c55e01cSJens Axboe 	}
14459c55e01cSJens Axboe 
14469c55e01cSJens Axboe done:
14479c55e01cSJens Axboe 	/*
14489c55e01cSJens Axboe 	 * drop our reference to the clone, the pipe consumption will
14499c55e01cSJens Axboe 	 * drop the rest.
14509c55e01cSJens Axboe 	 */
14519c55e01cSJens Axboe 	kfree_skb(skb);
14529c55e01cSJens Axboe 
14539c55e01cSJens Axboe 	if (spd.nr_pages) {
14549c55e01cSJens Axboe 		int ret;
1455293ad604SOctavian Purdila 		struct sock *sk = __skb->sk;
14569c55e01cSJens Axboe 
14579c55e01cSJens Axboe 		/*
14589c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
14599c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
14609c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
14619c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
14629c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
14639c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
14649c55e01cSJens Axboe 		 * and networking will grab the socket lock.
14659c55e01cSJens Axboe 		 */
1466293ad604SOctavian Purdila 		release_sock(sk);
14679c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1468293ad604SOctavian Purdila 		lock_sock(sk);
14699c55e01cSJens Axboe 		return ret;
14709c55e01cSJens Axboe 	}
14719c55e01cSJens Axboe 
14729c55e01cSJens Axboe 	return 0;
14739c55e01cSJens Axboe }
14749c55e01cSJens Axboe 
1475357b40a1SHerbert Xu /**
1476357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1477357b40a1SHerbert Xu  *	@skb: destination buffer
1478357b40a1SHerbert Xu  *	@offset: offset in destination
1479357b40a1SHerbert Xu  *	@from: source buffer
1480357b40a1SHerbert Xu  *	@len: number of bytes to copy
1481357b40a1SHerbert Xu  *
1482357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1483357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1484357b40a1SHerbert Xu  *	traversing fragment lists and such.
1485357b40a1SHerbert Xu  */
1486357b40a1SHerbert Xu 
14870c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1488357b40a1SHerbert Xu {
1489357b40a1SHerbert Xu 	int i, copy;
14901a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1491357b40a1SHerbert Xu 
1492357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1493357b40a1SHerbert Xu 		goto fault;
1494357b40a1SHerbert Xu 
14951a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1496357b40a1SHerbert Xu 		if (copy > len)
1497357b40a1SHerbert Xu 			copy = len;
149827d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1499357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1500357b40a1SHerbert Xu 			return 0;
1501357b40a1SHerbert Xu 		offset += copy;
1502357b40a1SHerbert Xu 		from += copy;
1503357b40a1SHerbert Xu 	}
1504357b40a1SHerbert Xu 
1505357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1506357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
15071a028e50SDavid S. Miller 		int end;
1508357b40a1SHerbert Xu 
15091a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
15101a028e50SDavid S. Miller 
15111a028e50SDavid S. Miller 		end = start + frag->size;
1512357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1513357b40a1SHerbert Xu 			u8 *vaddr;
1514357b40a1SHerbert Xu 
1515357b40a1SHerbert Xu 			if (copy > len)
1516357b40a1SHerbert Xu 				copy = len;
1517357b40a1SHerbert Xu 
1518357b40a1SHerbert Xu 			vaddr = kmap_skb_frag(frag);
15191a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
15201a028e50SDavid S. Miller 			       from, copy);
1521357b40a1SHerbert Xu 			kunmap_skb_frag(vaddr);
1522357b40a1SHerbert Xu 
1523357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1524357b40a1SHerbert Xu 				return 0;
1525357b40a1SHerbert Xu 			offset += copy;
1526357b40a1SHerbert Xu 			from += copy;
1527357b40a1SHerbert Xu 		}
15281a028e50SDavid S. Miller 		start = end;
1529357b40a1SHerbert Xu 	}
1530357b40a1SHerbert Xu 
1531357b40a1SHerbert Xu 	if (skb_shinfo(skb)->frag_list) {
1532357b40a1SHerbert Xu 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1533357b40a1SHerbert Xu 
1534357b40a1SHerbert Xu 		for (; list; list = list->next) {
15351a028e50SDavid S. Miller 			int end;
1536357b40a1SHerbert Xu 
15371a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
15381a028e50SDavid S. Miller 
15391a028e50SDavid S. Miller 			end = start + list->len;
1540357b40a1SHerbert Xu 			if ((copy = end - offset) > 0) {
1541357b40a1SHerbert Xu 				if (copy > len)
1542357b40a1SHerbert Xu 					copy = len;
15431a028e50SDavid S. Miller 				if (skb_store_bits(list, offset - start,
15441a028e50SDavid S. Miller 						   from, copy))
1545357b40a1SHerbert Xu 					goto fault;
1546357b40a1SHerbert Xu 				if ((len -= copy) == 0)
1547357b40a1SHerbert Xu 					return 0;
1548357b40a1SHerbert Xu 				offset += copy;
1549357b40a1SHerbert Xu 				from += copy;
1550357b40a1SHerbert Xu 			}
15511a028e50SDavid S. Miller 			start = end;
1552357b40a1SHerbert Xu 		}
1553357b40a1SHerbert Xu 	}
1554357b40a1SHerbert Xu 	if (!len)
1555357b40a1SHerbert Xu 		return 0;
1556357b40a1SHerbert Xu 
1557357b40a1SHerbert Xu fault:
1558357b40a1SHerbert Xu 	return -EFAULT;
1559357b40a1SHerbert Xu }
1560357b40a1SHerbert Xu 
1561357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1562357b40a1SHerbert Xu 
15631da177e4SLinus Torvalds /* Checksum skb data. */
15641da177e4SLinus Torvalds 
15652bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset,
15662bbbc868SAl Viro 			  int len, __wsum csum)
15671da177e4SLinus Torvalds {
15681a028e50SDavid S. Miller 	int start = skb_headlen(skb);
15691a028e50SDavid S. Miller 	int i, copy = start - offset;
15701da177e4SLinus Torvalds 	int pos = 0;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	/* Checksum header. */
15731da177e4SLinus Torvalds 	if (copy > 0) {
15741da177e4SLinus Torvalds 		if (copy > len)
15751da177e4SLinus Torvalds 			copy = len;
15761da177e4SLinus Torvalds 		csum = csum_partial(skb->data + offset, copy, csum);
15771da177e4SLinus Torvalds 		if ((len -= copy) == 0)
15781da177e4SLinus Torvalds 			return csum;
15791da177e4SLinus Torvalds 		offset += copy;
15801da177e4SLinus Torvalds 		pos	= copy;
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
15841a028e50SDavid S. Miller 		int end;
15851da177e4SLinus Torvalds 
15861a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
15871a028e50SDavid S. Miller 
15881a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
15891da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
159044bb9363SAl Viro 			__wsum csum2;
15911da177e4SLinus Torvalds 			u8 *vaddr;
15921da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 			if (copy > len)
15951da177e4SLinus Torvalds 				copy = len;
15961da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
15971a028e50SDavid S. Miller 			csum2 = csum_partial(vaddr + frag->page_offset +
15981a028e50SDavid S. Miller 					     offset - start, copy, 0);
15991da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
16001da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
16011da177e4SLinus Torvalds 			if (!(len -= copy))
16021da177e4SLinus Torvalds 				return csum;
16031da177e4SLinus Torvalds 			offset += copy;
16041da177e4SLinus Torvalds 			pos    += copy;
16051da177e4SLinus Torvalds 		}
16061a028e50SDavid S. Miller 		start = end;
16071da177e4SLinus Torvalds 	}
16081da177e4SLinus Torvalds 
16091da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
16101da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 		for (; list; list = list->next) {
16131a028e50SDavid S. Miller 			int end;
16141da177e4SLinus Torvalds 
16151a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
16161a028e50SDavid S. Miller 
16171a028e50SDavid S. Miller 			end = start + list->len;
16181da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
16195f92a738SAl Viro 				__wsum csum2;
16201da177e4SLinus Torvalds 				if (copy > len)
16211da177e4SLinus Torvalds 					copy = len;
16221a028e50SDavid S. Miller 				csum2 = skb_checksum(list, offset - start,
16231a028e50SDavid S. Miller 						     copy, 0);
16241da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
16251da177e4SLinus Torvalds 				if ((len -= copy) == 0)
16261da177e4SLinus Torvalds 					return csum;
16271da177e4SLinus Torvalds 				offset += copy;
16281da177e4SLinus Torvalds 				pos    += copy;
16291da177e4SLinus Torvalds 			}
16301a028e50SDavid S. Miller 			start = end;
16311da177e4SLinus Torvalds 		}
16321da177e4SLinus Torvalds 	}
163309a62660SKris Katterjohn 	BUG_ON(len);
16341da177e4SLinus Torvalds 
16351da177e4SLinus Torvalds 	return csum;
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds /* Both of above in one bottle. */
16391da177e4SLinus Torvalds 
164081d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
164181d77662SAl Viro 				    u8 *to, int len, __wsum csum)
16421da177e4SLinus Torvalds {
16431a028e50SDavid S. Miller 	int start = skb_headlen(skb);
16441a028e50SDavid S. Miller 	int i, copy = start - offset;
16451da177e4SLinus Torvalds 	int pos = 0;
16461da177e4SLinus Torvalds 
16471da177e4SLinus Torvalds 	/* Copy header. */
16481da177e4SLinus Torvalds 	if (copy > 0) {
16491da177e4SLinus Torvalds 		if (copy > len)
16501da177e4SLinus Torvalds 			copy = len;
16511da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
16521da177e4SLinus Torvalds 						 copy, csum);
16531da177e4SLinus Torvalds 		if ((len -= copy) == 0)
16541da177e4SLinus Torvalds 			return csum;
16551da177e4SLinus Torvalds 		offset += copy;
16561da177e4SLinus Torvalds 		to     += copy;
16571da177e4SLinus Torvalds 		pos	= copy;
16581da177e4SLinus Torvalds 	}
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16611a028e50SDavid S. Miller 		int end;
16621da177e4SLinus Torvalds 
16631a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
16641a028e50SDavid S. Miller 
16651a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
16661da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
16675084205fSAl Viro 			__wsum csum2;
16681da177e4SLinus Torvalds 			u8 *vaddr;
16691da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 			if (copy > len)
16721da177e4SLinus Torvalds 				copy = len;
16731da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
16741da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
16751a028e50SDavid S. Miller 							  frag->page_offset +
16761a028e50SDavid S. Miller 							  offset - start, to,
16771a028e50SDavid S. Miller 							  copy, 0);
16781da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
16791da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
16801da177e4SLinus Torvalds 			if (!(len -= copy))
16811da177e4SLinus Torvalds 				return csum;
16821da177e4SLinus Torvalds 			offset += copy;
16831da177e4SLinus Torvalds 			to     += copy;
16841da177e4SLinus Torvalds 			pos    += copy;
16851da177e4SLinus Torvalds 		}
16861a028e50SDavid S. Miller 		start = end;
16871da177e4SLinus Torvalds 	}
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
16901da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 		for (; list; list = list->next) {
169381d77662SAl Viro 			__wsum csum2;
16941a028e50SDavid S. Miller 			int end;
16951da177e4SLinus Torvalds 
16961a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
16971a028e50SDavid S. Miller 
16981a028e50SDavid S. Miller 			end = start + list->len;
16991da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
17001da177e4SLinus Torvalds 				if (copy > len)
17011da177e4SLinus Torvalds 					copy = len;
17021a028e50SDavid S. Miller 				csum2 = skb_copy_and_csum_bits(list,
17031a028e50SDavid S. Miller 							       offset - start,
17041da177e4SLinus Torvalds 							       to, copy, 0);
17051da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
17061da177e4SLinus Torvalds 				if ((len -= copy) == 0)
17071da177e4SLinus Torvalds 					return csum;
17081da177e4SLinus Torvalds 				offset += copy;
17091da177e4SLinus Torvalds 				to     += copy;
17101da177e4SLinus Torvalds 				pos    += copy;
17111da177e4SLinus Torvalds 			}
17121a028e50SDavid S. Miller 			start = end;
17131da177e4SLinus Torvalds 		}
17141da177e4SLinus Torvalds 	}
171509a62660SKris Katterjohn 	BUG_ON(len);
17161da177e4SLinus Torvalds 	return csum;
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds 
17191da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
17201da177e4SLinus Torvalds {
1721d3bc23e7SAl Viro 	__wsum csum;
17221da177e4SLinus Torvalds 	long csstart;
17231da177e4SLinus Torvalds 
172484fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1725663ead3bSHerbert Xu 		csstart = skb->csum_start - skb_headroom(skb);
17261da177e4SLinus Torvalds 	else
17271da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
17281da177e4SLinus Torvalds 
172909a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
17301da177e4SLinus Torvalds 
1731d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	csum = 0;
17341da177e4SLinus Torvalds 	if (csstart != skb->len)
17351da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
17361da177e4SLinus Torvalds 					      skb->len - csstart, 0);
17371da177e4SLinus Torvalds 
173884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1739ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
17401da177e4SLinus Torvalds 
1741d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
17421da177e4SLinus Torvalds 	}
17431da177e4SLinus Torvalds }
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds /**
17461da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
17471da177e4SLinus Torvalds  *	@list: list to dequeue from
17481da177e4SLinus Torvalds  *
17491da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
17501da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
17511da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
17521da177e4SLinus Torvalds  */
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
17551da177e4SLinus Torvalds {
17561da177e4SLinus Torvalds 	unsigned long flags;
17571da177e4SLinus Torvalds 	struct sk_buff *result;
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
17601da177e4SLinus Torvalds 	result = __skb_dequeue(list);
17611da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
17621da177e4SLinus Torvalds 	return result;
17631da177e4SLinus Torvalds }
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds /**
17661da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
17671da177e4SLinus Torvalds  *	@list: list to dequeue from
17681da177e4SLinus Torvalds  *
17691da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
17701da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
17711da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
17721da177e4SLinus Torvalds  */
17731da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
17741da177e4SLinus Torvalds {
17751da177e4SLinus Torvalds 	unsigned long flags;
17761da177e4SLinus Torvalds 	struct sk_buff *result;
17771da177e4SLinus Torvalds 
17781da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
17791da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
17801da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
17811da177e4SLinus Torvalds 	return result;
17821da177e4SLinus Torvalds }
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds /**
17851da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
17861da177e4SLinus Torvalds  *	@list: list to empty
17871da177e4SLinus Torvalds  *
17881da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
17891da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
17901da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
17911da177e4SLinus Torvalds  */
17921da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
17931da177e4SLinus Torvalds {
17941da177e4SLinus Torvalds 	struct sk_buff *skb;
17951da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
17961da177e4SLinus Torvalds 		kfree_skb(skb);
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds /**
18001da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
18011da177e4SLinus Torvalds  *	@list: list to use
18021da177e4SLinus Torvalds  *	@newsk: buffer to queue
18031da177e4SLinus Torvalds  *
18041da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
18051da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
18061da177e4SLinus Torvalds  *	safely.
18071da177e4SLinus Torvalds  *
18081da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
18091da177e4SLinus Torvalds  */
18101da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	unsigned long flags;
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18151da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
18161da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds /**
18201da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
18211da177e4SLinus Torvalds  *	@list: list to use
18221da177e4SLinus Torvalds  *	@newsk: buffer to queue
18231da177e4SLinus Torvalds  *
18241da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
18251da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
18261da177e4SLinus Torvalds  *	safely.
18271da177e4SLinus Torvalds  *
18281da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
18291da177e4SLinus Torvalds  */
18301da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
18311da177e4SLinus Torvalds {
18321da177e4SLinus Torvalds 	unsigned long flags;
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18351da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
18361da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18371da177e4SLinus Torvalds }
18388728b834SDavid S. Miller 
18391da177e4SLinus Torvalds /**
18401da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
18411da177e4SLinus Torvalds  *	@skb: buffer to remove
18428728b834SDavid S. Miller  *	@list: list to use
18431da177e4SLinus Torvalds  *
18448728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
18458728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
18461da177e4SLinus Torvalds  *
18478728b834SDavid S. Miller  *	You must know what list the SKB is on.
18481da177e4SLinus Torvalds  */
18498728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
18501da177e4SLinus Torvalds {
18511da177e4SLinus Torvalds 	unsigned long flags;
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18548728b834SDavid S. Miller 	__skb_unlink(skb, list);
18551da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18561da177e4SLinus Torvalds }
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds /**
18591da177e4SLinus Torvalds  *	skb_append	-	append a buffer
18601da177e4SLinus Torvalds  *	@old: buffer to insert after
18611da177e4SLinus Torvalds  *	@newsk: buffer to insert
18628728b834SDavid S. Miller  *	@list: list to use
18631da177e4SLinus Torvalds  *
18641da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
18651da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
18661da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
18671da177e4SLinus Torvalds  */
18688728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
18691da177e4SLinus Torvalds {
18701da177e4SLinus Torvalds 	unsigned long flags;
18711da177e4SLinus Torvalds 
18728728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
18737de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
18748728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
18751da177e4SLinus Torvalds }
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds /**
18791da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
18801da177e4SLinus Torvalds  *	@old: buffer to insert before
18811da177e4SLinus Torvalds  *	@newsk: buffer to insert
18828728b834SDavid S. Miller  *	@list: list to use
18831da177e4SLinus Torvalds  *
18848728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
18858728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
18868728b834SDavid S. Miller  *	calls.
18878728b834SDavid S. Miller  *
18881da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
18891da177e4SLinus Torvalds  */
18908728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
18911da177e4SLinus Torvalds {
18921da177e4SLinus Torvalds 	unsigned long flags;
18931da177e4SLinus Torvalds 
18948728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
18958728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
18968728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
19001da177e4SLinus Torvalds 					   struct sk_buff* skb1,
19011da177e4SLinus Torvalds 					   const u32 len, const int pos)
19021da177e4SLinus Torvalds {
19031da177e4SLinus Torvalds 	int i;
19041da177e4SLinus Torvalds 
1905d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1906d626f62bSArnaldo Carvalho de Melo 					 pos - len);
19071da177e4SLinus Torvalds 	/* And move data appendix as is. */
19081da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
19091da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
19121da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
19131da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
19141da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
19151da177e4SLinus Torvalds 	skb->data_len		   = 0;
19161da177e4SLinus Torvalds 	skb->len		   = len;
191727a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
19211da177e4SLinus Torvalds 				       struct sk_buff* skb1,
19221da177e4SLinus Torvalds 				       const u32 len, int pos)
19231da177e4SLinus Torvalds {
19241da177e4SLinus Torvalds 	int i, k = 0;
19251da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
19281da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
19291da177e4SLinus Torvalds 	skb->len		  = len;
19301da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
19331da177e4SLinus Torvalds 		int size = skb_shinfo(skb)->frags[i].size;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 		if (pos + size > len) {
19361da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 			if (pos < len) {
19391da177e4SLinus Torvalds 				/* Split frag.
19401da177e4SLinus Torvalds 				 * We have two variants in this case:
19411da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
19421da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
19431da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
19441da177e4SLinus Torvalds 				 *    where splitting is expensive.
19451da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
19461da177e4SLinus Torvalds 				 */
19471da177e4SLinus Torvalds 				get_page(skb_shinfo(skb)->frags[i].page);
19481da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
19491da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].size -= len - pos;
19501da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[i].size	= len - pos;
19511da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
19521da177e4SLinus Torvalds 			}
19531da177e4SLinus Torvalds 			k++;
19541da177e4SLinus Torvalds 		} else
19551da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
19561da177e4SLinus Torvalds 		pos += size;
19571da177e4SLinus Torvalds 	}
19581da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds /**
19621da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
19631da177e4SLinus Torvalds  * @skb: the buffer to split
19641da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
19651da177e4SLinus Torvalds  * @len: new length for skb
19661da177e4SLinus Torvalds  */
19671da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
19681da177e4SLinus Torvalds {
19691da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
19721da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
19731da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
19741da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
1977677e90edSThomas Graf /**
1978677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
1979677e90edSThomas Graf  * @skb: the buffer to read
1980677e90edSThomas Graf  * @from: lower offset of data to be read
1981677e90edSThomas Graf  * @to: upper offset of data to be read
1982677e90edSThomas Graf  * @st: state variable
1983677e90edSThomas Graf  *
1984677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
1985677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
1986677e90edSThomas Graf  */
1987677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1988677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
1989677e90edSThomas Graf {
1990677e90edSThomas Graf 	st->lower_offset = from;
1991677e90edSThomas Graf 	st->upper_offset = to;
1992677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
1993677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
1994677e90edSThomas Graf 	st->frag_data = NULL;
1995677e90edSThomas Graf }
1996677e90edSThomas Graf 
1997677e90edSThomas Graf /**
1998677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
1999677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2000677e90edSThomas Graf  * @data: destination pointer for data to be returned
2001677e90edSThomas Graf  * @st: state variable
2002677e90edSThomas Graf  *
2003677e90edSThomas Graf  * Reads a block of skb data at &consumed relative to the
2004677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2005677e90edSThomas Graf  * the head of the data block to &data and returns the length
2006677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2007677e90edSThomas Graf  * offset has been reached.
2008677e90edSThomas Graf  *
2009677e90edSThomas Graf  * The caller is not required to consume all of the data
2010677e90edSThomas Graf  * returned, i.e. &consumed is typically set to the number
2011677e90edSThomas Graf  * of bytes already consumed and the next call to
2012677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2013677e90edSThomas Graf  *
2014bc2cda1eSRandy Dunlap  * Note 1: The size of each block of data returned can be arbitary,
2015677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
2016677e90edSThomas Graf  *       reads of potentially non linear data.
2017677e90edSThomas Graf  *
2018bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2019677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2020677e90edSThomas Graf  *       a stack for this purpose.
2021677e90edSThomas Graf  */
2022677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2023677e90edSThomas Graf 			  struct skb_seq_state *st)
2024677e90edSThomas Graf {
2025677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2026677e90edSThomas Graf 	skb_frag_t *frag;
2027677e90edSThomas Graf 
2028677e90edSThomas Graf 	if (unlikely(abs_offset >= st->upper_offset))
2029677e90edSThomas Graf 		return 0;
2030677e90edSThomas Graf 
2031677e90edSThomas Graf next_skb:
2032677e90edSThomas Graf 	block_limit = skb_headlen(st->cur_skb);
2033677e90edSThomas Graf 
2034677e90edSThomas Graf 	if (abs_offset < block_limit) {
2035677e90edSThomas Graf 		*data = st->cur_skb->data + abs_offset;
2036677e90edSThomas Graf 		return block_limit - abs_offset;
2037677e90edSThomas Graf 	}
2038677e90edSThomas Graf 
2039677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2040677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2041677e90edSThomas Graf 
2042677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2043677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2044677e90edSThomas Graf 		block_limit = frag->size + st->stepped_offset;
2045677e90edSThomas Graf 
2046677e90edSThomas Graf 		if (abs_offset < block_limit) {
2047677e90edSThomas Graf 			if (!st->frag_data)
2048677e90edSThomas Graf 				st->frag_data = kmap_skb_frag(frag);
2049677e90edSThomas Graf 
2050677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2051677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2052677e90edSThomas Graf 
2053677e90edSThomas Graf 			return block_limit - abs_offset;
2054677e90edSThomas Graf 		}
2055677e90edSThomas Graf 
2056677e90edSThomas Graf 		if (st->frag_data) {
2057677e90edSThomas Graf 			kunmap_skb_frag(st->frag_data);
2058677e90edSThomas Graf 			st->frag_data = NULL;
2059677e90edSThomas Graf 		}
2060677e90edSThomas Graf 
2061677e90edSThomas Graf 		st->frag_idx++;
2062677e90edSThomas Graf 		st->stepped_offset += frag->size;
2063677e90edSThomas Graf 	}
2064677e90edSThomas Graf 
20655b5a60daSOlaf Kirch 	if (st->frag_data) {
20665b5a60daSOlaf Kirch 		kunmap_skb_frag(st->frag_data);
20675b5a60daSOlaf Kirch 		st->frag_data = NULL;
20685b5a60daSOlaf Kirch 	}
20695b5a60daSOlaf Kirch 
2070677e90edSThomas Graf 	if (st->cur_skb->next) {
2071677e90edSThomas Graf 		st->cur_skb = st->cur_skb->next;
2072677e90edSThomas Graf 		st->frag_idx = 0;
2073677e90edSThomas Graf 		goto next_skb;
2074677e90edSThomas Graf 	} else if (st->root_skb == st->cur_skb &&
2075677e90edSThomas Graf 		   skb_shinfo(st->root_skb)->frag_list) {
2076677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2077677e90edSThomas Graf 		goto next_skb;
2078677e90edSThomas Graf 	}
2079677e90edSThomas Graf 
2080677e90edSThomas Graf 	return 0;
2081677e90edSThomas Graf }
2082677e90edSThomas Graf 
2083677e90edSThomas Graf /**
2084677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2085677e90edSThomas Graf  * @st: state variable
2086677e90edSThomas Graf  *
2087677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2088677e90edSThomas Graf  * returned 0.
2089677e90edSThomas Graf  */
2090677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2091677e90edSThomas Graf {
2092677e90edSThomas Graf 	if (st->frag_data)
2093677e90edSThomas Graf 		kunmap_skb_frag(st->frag_data);
2094677e90edSThomas Graf }
2095677e90edSThomas Graf 
20963fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
20973fc7e8a6SThomas Graf 
20983fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
20993fc7e8a6SThomas Graf 					  struct ts_config *conf,
21003fc7e8a6SThomas Graf 					  struct ts_state *state)
21013fc7e8a6SThomas Graf {
21023fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
21033fc7e8a6SThomas Graf }
21043fc7e8a6SThomas Graf 
21053fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
21063fc7e8a6SThomas Graf {
21073fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
21083fc7e8a6SThomas Graf }
21093fc7e8a6SThomas Graf 
21103fc7e8a6SThomas Graf /**
21113fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
21123fc7e8a6SThomas Graf  * @skb: the buffer to look in
21133fc7e8a6SThomas Graf  * @from: search offset
21143fc7e8a6SThomas Graf  * @to: search limit
21153fc7e8a6SThomas Graf  * @config: textsearch configuration
21163fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
21173fc7e8a6SThomas Graf  *
21183fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
21193fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
21203fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
21213fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
21223fc7e8a6SThomas Graf  */
21233fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
21243fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
21253fc7e8a6SThomas Graf 			   struct ts_state *state)
21263fc7e8a6SThomas Graf {
2127f72b948dSPhil Oester 	unsigned int ret;
2128f72b948dSPhil Oester 
21293fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
21303fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
21313fc7e8a6SThomas Graf 
21323fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
21333fc7e8a6SThomas Graf 
2134f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2135f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
21363fc7e8a6SThomas Graf }
21373fc7e8a6SThomas Graf 
2138e89e9cf5SAnanda Raju /**
2139e89e9cf5SAnanda Raju  * skb_append_datato_frags: - append the user data to a skb
2140e89e9cf5SAnanda Raju  * @sk: sock  structure
2141e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
2142e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2143e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2144e89e9cf5SAnanda Raju  * @length: length of the iov message
2145e89e9cf5SAnanda Raju  *
2146e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2147e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2148e89e9cf5SAnanda Raju  */
2149e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2150dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2151e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2152e89e9cf5SAnanda Raju 			void *from, int length)
2153e89e9cf5SAnanda Raju {
2154e89e9cf5SAnanda Raju 	int frg_cnt = 0;
2155e89e9cf5SAnanda Raju 	skb_frag_t *frag = NULL;
2156e89e9cf5SAnanda Raju 	struct page *page = NULL;
2157e89e9cf5SAnanda Raju 	int copy, left;
2158e89e9cf5SAnanda Raju 	int offset = 0;
2159e89e9cf5SAnanda Raju 	int ret;
2160e89e9cf5SAnanda Raju 
2161e89e9cf5SAnanda Raju 	do {
2162e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2163e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2164e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2165e89e9cf5SAnanda Raju 			return -EFAULT;
2166e89e9cf5SAnanda Raju 
2167e89e9cf5SAnanda Raju 		/* allocate a new page for next frag */
2168e89e9cf5SAnanda Raju 		page = alloc_pages(sk->sk_allocation, 0);
2169e89e9cf5SAnanda Raju 
2170e89e9cf5SAnanda Raju 		/* If alloc_page fails just return failure and caller will
2171e89e9cf5SAnanda Raju 		 * free previous allocated pages by doing kfree_skb()
2172e89e9cf5SAnanda Raju 		 */
2173e89e9cf5SAnanda Raju 		if (page == NULL)
2174e89e9cf5SAnanda Raju 			return -ENOMEM;
2175e89e9cf5SAnanda Raju 
2176e89e9cf5SAnanda Raju 		/* initialize the next frag */
2177e89e9cf5SAnanda Raju 		sk->sk_sndmsg_page = page;
2178e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off = 0;
2179e89e9cf5SAnanda Raju 		skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2180e89e9cf5SAnanda Raju 		skb->truesize += PAGE_SIZE;
2181e89e9cf5SAnanda Raju 		atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2182e89e9cf5SAnanda Raju 
2183e89e9cf5SAnanda Raju 		/* get the new initialized frag */
2184e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2185e89e9cf5SAnanda Raju 		frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2186e89e9cf5SAnanda Raju 
2187e89e9cf5SAnanda Raju 		/* copy the user data to page */
2188e89e9cf5SAnanda Raju 		left = PAGE_SIZE - frag->page_offset;
2189e89e9cf5SAnanda Raju 		copy = (length > left)? left : length;
2190e89e9cf5SAnanda Raju 
2191e89e9cf5SAnanda Raju 		ret = getfrag(from, (page_address(frag->page) +
2192e89e9cf5SAnanda Raju 			    frag->page_offset + frag->size),
2193e89e9cf5SAnanda Raju 			    offset, copy, 0, skb);
2194e89e9cf5SAnanda Raju 		if (ret < 0)
2195e89e9cf5SAnanda Raju 			return -EFAULT;
2196e89e9cf5SAnanda Raju 
2197e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2198e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off += copy;
2199e89e9cf5SAnanda Raju 		frag->size += copy;
2200e89e9cf5SAnanda Raju 		skb->len += copy;
2201e89e9cf5SAnanda Raju 		skb->data_len += copy;
2202e89e9cf5SAnanda Raju 		offset += copy;
2203e89e9cf5SAnanda Raju 		length -= copy;
2204e89e9cf5SAnanda Raju 
2205e89e9cf5SAnanda Raju 	} while (length > 0);
2206e89e9cf5SAnanda Raju 
2207e89e9cf5SAnanda Raju 	return 0;
2208e89e9cf5SAnanda Raju }
2209e89e9cf5SAnanda Raju 
2210cbb042f9SHerbert Xu /**
2211cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2212cbb042f9SHerbert Xu  *	@skb: buffer to update
2213cbb042f9SHerbert Xu  *	@len: length of data pulled
2214cbb042f9SHerbert Xu  *
2215cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2216fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
221784fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
221884fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
221984fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2220cbb042f9SHerbert Xu  */
2221cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2222cbb042f9SHerbert Xu {
2223cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2224cbb042f9SHerbert Xu 	skb->len -= len;
2225cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2226cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2227cbb042f9SHerbert Xu 	return skb->data += len;
2228cbb042f9SHerbert Xu }
2229cbb042f9SHerbert Xu 
2230f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2231f94691acSArnaldo Carvalho de Melo 
2232f4c50d99SHerbert Xu /**
2233f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2234f4c50d99SHerbert Xu  *	@skb: buffer to segment
2235576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2236f4c50d99SHerbert Xu  *
2237f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
22384c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
22394c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2240f4c50d99SHerbert Xu  */
2241576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2242f4c50d99SHerbert Xu {
2243f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2244f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
2245f4c50d99SHerbert Xu 	unsigned int mss = skb_shinfo(skb)->gso_size;
224698e399f8SArnaldo Carvalho de Melo 	unsigned int doffset = skb->data - skb_mac_header(skb);
2247f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2248f4c50d99SHerbert Xu 	unsigned int headroom;
2249f4c50d99SHerbert Xu 	unsigned int len;
2250576a30ebSHerbert Xu 	int sg = features & NETIF_F_SG;
2251f4c50d99SHerbert Xu 	int nfrags = skb_shinfo(skb)->nr_frags;
2252f4c50d99SHerbert Xu 	int err = -ENOMEM;
2253f4c50d99SHerbert Xu 	int i = 0;
2254f4c50d99SHerbert Xu 	int pos;
2255f4c50d99SHerbert Xu 
2256f4c50d99SHerbert Xu 	__skb_push(skb, doffset);
2257f4c50d99SHerbert Xu 	headroom = skb_headroom(skb);
2258f4c50d99SHerbert Xu 	pos = skb_headlen(skb);
2259f4c50d99SHerbert Xu 
2260f4c50d99SHerbert Xu 	do {
2261f4c50d99SHerbert Xu 		struct sk_buff *nskb;
2262f4c50d99SHerbert Xu 		skb_frag_t *frag;
2263c8884eddSHerbert Xu 		int hsize;
2264f4c50d99SHerbert Xu 		int k;
2265f4c50d99SHerbert Xu 		int size;
2266f4c50d99SHerbert Xu 
2267f4c50d99SHerbert Xu 		len = skb->len - offset;
2268f4c50d99SHerbert Xu 		if (len > mss)
2269f4c50d99SHerbert Xu 			len = mss;
2270f4c50d99SHerbert Xu 
2271f4c50d99SHerbert Xu 		hsize = skb_headlen(skb) - offset;
2272f4c50d99SHerbert Xu 		if (hsize < 0)
2273f4c50d99SHerbert Xu 			hsize = 0;
2274c8884eddSHerbert Xu 		if (hsize > len || !sg)
2275c8884eddSHerbert Xu 			hsize = len;
2276f4c50d99SHerbert Xu 
2277c8884eddSHerbert Xu 		nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
2278f4c50d99SHerbert Xu 		if (unlikely(!nskb))
2279f4c50d99SHerbert Xu 			goto err;
2280f4c50d99SHerbert Xu 
2281f4c50d99SHerbert Xu 		if (segs)
2282f4c50d99SHerbert Xu 			tail->next = nskb;
2283f4c50d99SHerbert Xu 		else
2284f4c50d99SHerbert Xu 			segs = nskb;
2285f4c50d99SHerbert Xu 		tail = nskb;
2286f4c50d99SHerbert Xu 
2287f4c50d99SHerbert Xu 		nskb->dev = skb->dev;
2288f25f4e44SPeter P Waskiewicz Jr 		skb_copy_queue_mapping(nskb, skb);
2289f4c50d99SHerbert Xu 		nskb->priority = skb->priority;
2290f4c50d99SHerbert Xu 		nskb->protocol = skb->protocol;
22916aa895b0SPatrick McHardy 		nskb->vlan_tci = skb->vlan_tci;
2292f4c50d99SHerbert Xu 		nskb->dst = dst_clone(skb->dst);
2293f4c50d99SHerbert Xu 		memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
2294f4c50d99SHerbert Xu 		nskb->pkt_type = skb->pkt_type;
2295f4c50d99SHerbert Xu 		nskb->mac_len = skb->mac_len;
2296f4c50d99SHerbert Xu 
2297f4c50d99SHerbert Xu 		skb_reserve(nskb, headroom);
2298459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(nskb);
2299ddc7b8e3SArnaldo Carvalho de Melo 		skb_set_network_header(nskb, skb->mac_len);
2300b0e380b1SArnaldo Carvalho de Melo 		nskb->transport_header = (nskb->network_header +
2301b0e380b1SArnaldo Carvalho de Melo 					  skb_network_header_len(skb));
2302d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
2303d626f62bSArnaldo Carvalho de Melo 					  doffset);
2304f4c50d99SHerbert Xu 		if (!sg) {
2305f4c50d99SHerbert Xu 			nskb->csum = skb_copy_and_csum_bits(skb, offset,
2306f4c50d99SHerbert Xu 							    skb_put(nskb, len),
2307f4c50d99SHerbert Xu 							    len, 0);
2308f4c50d99SHerbert Xu 			continue;
2309f4c50d99SHerbert Xu 		}
2310f4c50d99SHerbert Xu 
2311f4c50d99SHerbert Xu 		frag = skb_shinfo(nskb)->frags;
2312f4c50d99SHerbert Xu 		k = 0;
2313f4c50d99SHerbert Xu 
231484fa7933SPatrick McHardy 		nskb->ip_summed = CHECKSUM_PARTIAL;
2315f4c50d99SHerbert Xu 		nskb->csum = skb->csum;
2316d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset,
2317d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
2318f4c50d99SHerbert Xu 
2319f4c50d99SHerbert Xu 		while (pos < offset + len) {
2320f4c50d99SHerbert Xu 			BUG_ON(i >= nfrags);
2321f4c50d99SHerbert Xu 
2322f4c50d99SHerbert Xu 			*frag = skb_shinfo(skb)->frags[i];
2323f4c50d99SHerbert Xu 			get_page(frag->page);
2324f4c50d99SHerbert Xu 			size = frag->size;
2325f4c50d99SHerbert Xu 
2326f4c50d99SHerbert Xu 			if (pos < offset) {
2327f4c50d99SHerbert Xu 				frag->page_offset += offset - pos;
2328f4c50d99SHerbert Xu 				frag->size -= offset - pos;
2329f4c50d99SHerbert Xu 			}
2330f4c50d99SHerbert Xu 
2331f4c50d99SHerbert Xu 			k++;
2332f4c50d99SHerbert Xu 
2333f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
2334f4c50d99SHerbert Xu 				i++;
2335f4c50d99SHerbert Xu 				pos += size;
2336f4c50d99SHerbert Xu 			} else {
2337f4c50d99SHerbert Xu 				frag->size -= pos + size - (offset + len);
2338f4c50d99SHerbert Xu 				break;
2339f4c50d99SHerbert Xu 			}
2340f4c50d99SHerbert Xu 
2341f4c50d99SHerbert Xu 			frag++;
2342f4c50d99SHerbert Xu 		}
2343f4c50d99SHerbert Xu 
2344f4c50d99SHerbert Xu 		skb_shinfo(nskb)->nr_frags = k;
2345f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
2346f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
2347f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
2348f4c50d99SHerbert Xu 	} while ((offset += len) < skb->len);
2349f4c50d99SHerbert Xu 
2350f4c50d99SHerbert Xu 	return segs;
2351f4c50d99SHerbert Xu 
2352f4c50d99SHerbert Xu err:
2353f4c50d99SHerbert Xu 	while ((skb = segs)) {
2354f4c50d99SHerbert Xu 		segs = skb->next;
2355b08d5840SPatrick McHardy 		kfree_skb(skb);
2356f4c50d99SHerbert Xu 	}
2357f4c50d99SHerbert Xu 	return ERR_PTR(err);
2358f4c50d99SHerbert Xu }
2359f4c50d99SHerbert Xu 
2360f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
2361f4c50d99SHerbert Xu 
23621da177e4SLinus Torvalds void __init skb_init(void)
23631da177e4SLinus Torvalds {
23641da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
23651da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
23661da177e4SLinus Torvalds 					      0,
2367e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
236820c2df83SPaul Mundt 					      NULL);
2369d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2370d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
2371d179cd12SDavid S. Miller 						sizeof(atomic_t),
2372d179cd12SDavid S. Miller 						0,
2373e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
237420c2df83SPaul Mundt 						NULL);
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds 
2377716ea3a7SDavid Howells /**
2378716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2379716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
2380716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
2381716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
2382716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
2383716ea3a7SDavid Howells  *
2384716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
2385716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
2386716ea3a7SDavid Howells  */
238751c739d1SDavid S. Miller static int
238851c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2389716ea3a7SDavid Howells {
23901a028e50SDavid S. Miller 	int start = skb_headlen(skb);
23911a028e50SDavid S. Miller 	int i, copy = start - offset;
2392716ea3a7SDavid Howells 	int elt = 0;
2393716ea3a7SDavid Howells 
2394716ea3a7SDavid Howells 	if (copy > 0) {
2395716ea3a7SDavid Howells 		if (copy > len)
2396716ea3a7SDavid Howells 			copy = len;
2397642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
2398716ea3a7SDavid Howells 		elt++;
2399716ea3a7SDavid Howells 		if ((len -= copy) == 0)
2400716ea3a7SDavid Howells 			return elt;
2401716ea3a7SDavid Howells 		offset += copy;
2402716ea3a7SDavid Howells 	}
2403716ea3a7SDavid Howells 
2404716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
24051a028e50SDavid S. Miller 		int end;
2406716ea3a7SDavid Howells 
24071a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
24081a028e50SDavid S. Miller 
24091a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
2410716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2411716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2412716ea3a7SDavid Howells 
2413716ea3a7SDavid Howells 			if (copy > len)
2414716ea3a7SDavid Howells 				copy = len;
2415642f1490SJens Axboe 			sg_set_page(&sg[elt], frag->page, copy,
2416642f1490SJens Axboe 					frag->page_offset+offset-start);
2417716ea3a7SDavid Howells 			elt++;
2418716ea3a7SDavid Howells 			if (!(len -= copy))
2419716ea3a7SDavid Howells 				return elt;
2420716ea3a7SDavid Howells 			offset += copy;
2421716ea3a7SDavid Howells 		}
24221a028e50SDavid S. Miller 		start = end;
2423716ea3a7SDavid Howells 	}
2424716ea3a7SDavid Howells 
2425716ea3a7SDavid Howells 	if (skb_shinfo(skb)->frag_list) {
2426716ea3a7SDavid Howells 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
2427716ea3a7SDavid Howells 
2428716ea3a7SDavid Howells 		for (; list; list = list->next) {
24291a028e50SDavid S. Miller 			int end;
2430716ea3a7SDavid Howells 
24311a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
24321a028e50SDavid S. Miller 
24331a028e50SDavid S. Miller 			end = start + list->len;
2434716ea3a7SDavid Howells 			if ((copy = end - offset) > 0) {
2435716ea3a7SDavid Howells 				if (copy > len)
2436716ea3a7SDavid Howells 					copy = len;
243751c739d1SDavid S. Miller 				elt += __skb_to_sgvec(list, sg+elt, offset - start,
243851c739d1SDavid S. Miller 						      copy);
2439716ea3a7SDavid Howells 				if ((len -= copy) == 0)
2440716ea3a7SDavid Howells 					return elt;
2441716ea3a7SDavid Howells 				offset += copy;
2442716ea3a7SDavid Howells 			}
24431a028e50SDavid S. Miller 			start = end;
2444716ea3a7SDavid Howells 		}
2445716ea3a7SDavid Howells 	}
2446716ea3a7SDavid Howells 	BUG_ON(len);
2447716ea3a7SDavid Howells 	return elt;
2448716ea3a7SDavid Howells }
2449716ea3a7SDavid Howells 
245051c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
245151c739d1SDavid S. Miller {
245251c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
245351c739d1SDavid S. Miller 
2454c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
245551c739d1SDavid S. Miller 
245651c739d1SDavid S. Miller 	return nsg;
245751c739d1SDavid S. Miller }
245851c739d1SDavid S. Miller 
2459716ea3a7SDavid Howells /**
2460716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
2461716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
2462716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
2463716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
2464716ea3a7SDavid Howells  *
2465716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
2466716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
2467716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
2468716ea3a7SDavid Howells  *
2469716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
2470716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
2471716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
2472716ea3a7SDavid Howells  *
2473716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
2474716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
2475716ea3a7SDavid Howells  */
2476716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2477716ea3a7SDavid Howells {
2478716ea3a7SDavid Howells 	int copyflag;
2479716ea3a7SDavid Howells 	int elt;
2480716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
2481716ea3a7SDavid Howells 
2482716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
2483716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
2484716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
2485716ea3a7SDavid Howells 	 */
2486716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2487716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2488716ea3a7SDavid Howells 		return -ENOMEM;
2489716ea3a7SDavid Howells 
2490716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
2491716ea3a7SDavid Howells 	if (!skb_shinfo(skb)->frag_list) {
2492716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
2493716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
2494716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
2495716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
2496716ea3a7SDavid Howells 
2497716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
2498716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2499716ea3a7SDavid Howells 			return -ENOMEM;
2500716ea3a7SDavid Howells 
2501716ea3a7SDavid Howells 		/* Voila! */
2502716ea3a7SDavid Howells 		*trailer = skb;
2503716ea3a7SDavid Howells 		return 1;
2504716ea3a7SDavid Howells 	}
2505716ea3a7SDavid Howells 
2506716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
2507716ea3a7SDavid Howells 
2508716ea3a7SDavid Howells 	elt = 1;
2509716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
2510716ea3a7SDavid Howells 	copyflag = 0;
2511716ea3a7SDavid Howells 
2512716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
2513716ea3a7SDavid Howells 		int ntail = 0;
2514716ea3a7SDavid Howells 
2515716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
2516716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
2517716ea3a7SDavid Howells 		 * after it. */
2518716ea3a7SDavid Howells 
2519716ea3a7SDavid Howells 		if (skb_shared(skb1))
2520716ea3a7SDavid Howells 			copyflag = 1;
2521716ea3a7SDavid Howells 
2522716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
2523716ea3a7SDavid Howells 
2524716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
2525716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
2526716ea3a7SDavid Howells 			    skb_shinfo(skb1)->frag_list ||
2527716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
2528716ea3a7SDavid Howells 				ntail = tailbits + 128;
2529716ea3a7SDavid Howells 		}
2530716ea3a7SDavid Howells 
2531716ea3a7SDavid Howells 		if (copyflag ||
2532716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
2533716ea3a7SDavid Howells 		    ntail ||
2534716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
2535716ea3a7SDavid Howells 		    skb_shinfo(skb1)->frag_list) {
2536716ea3a7SDavid Howells 			struct sk_buff *skb2;
2537716ea3a7SDavid Howells 
2538716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
2539716ea3a7SDavid Howells 			if (ntail == 0)
2540716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
2541716ea3a7SDavid Howells 			else
2542716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
2543716ea3a7SDavid Howells 						       skb_headroom(skb1),
2544716ea3a7SDavid Howells 						       ntail,
2545716ea3a7SDavid Howells 						       GFP_ATOMIC);
2546716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
2547716ea3a7SDavid Howells 				return -ENOMEM;
2548716ea3a7SDavid Howells 
2549716ea3a7SDavid Howells 			if (skb1->sk)
2550716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
2551716ea3a7SDavid Howells 
2552716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
2553716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
2554716ea3a7SDavid Howells 
2555716ea3a7SDavid Howells 			skb2->next = skb1->next;
2556716ea3a7SDavid Howells 			*skb_p = skb2;
2557716ea3a7SDavid Howells 			kfree_skb(skb1);
2558716ea3a7SDavid Howells 			skb1 = skb2;
2559716ea3a7SDavid Howells 		}
2560716ea3a7SDavid Howells 		elt++;
2561716ea3a7SDavid Howells 		*trailer = skb1;
2562716ea3a7SDavid Howells 		skb_p = &skb1->next;
2563716ea3a7SDavid Howells 	}
2564716ea3a7SDavid Howells 
2565716ea3a7SDavid Howells 	return elt;
2566716ea3a7SDavid Howells }
2567716ea3a7SDavid Howells 
2568f35d9d8aSRusty Russell /**
2569f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
2570f35d9d8aSRusty Russell  * @skb: the skb to set
2571f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
2572f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
2573f35d9d8aSRusty Russell  *
2574f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
2575f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2576f35d9d8aSRusty Russell  *
2577f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
2578f35d9d8aSRusty Russell  * returns false you should drop the packet.
2579f35d9d8aSRusty Russell  */
2580f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2581f35d9d8aSRusty Russell {
2582f35d9d8aSRusty Russell 	if (unlikely(start > skb->len - 2) ||
2583f35d9d8aSRusty Russell 	    unlikely((int)start + off > skb->len - 2)) {
2584f35d9d8aSRusty Russell 		if (net_ratelimit())
2585f35d9d8aSRusty Russell 			printk(KERN_WARNING
2586f35d9d8aSRusty Russell 			       "bad partial csum: csum=%u/%u len=%u\n",
2587f35d9d8aSRusty Russell 			       start, off, skb->len);
2588f35d9d8aSRusty Russell 		return false;
2589f35d9d8aSRusty Russell 	}
2590f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
2591f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
2592f35d9d8aSRusty Russell 	skb->csum_offset = off;
2593f35d9d8aSRusty Russell 	return true;
2594f35d9d8aSRusty Russell }
2595f35d9d8aSRusty Russell 
25964497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
25974497b076SBen Hutchings {
25984497b076SBen Hutchings 	if (net_ratelimit())
25994497b076SBen Hutchings 		pr_warning("%s: received packets cannot be forwarded"
26004497b076SBen Hutchings 			   " while LRO is enabled\n", skb->dev->name);
26014497b076SBen Hutchings }
26024497b076SBen Hutchings 
26031da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim);
26041da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb);
2605231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb);
26061da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail);
2607d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
26088af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb);
26091da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy);
26101da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head);
26111da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum);
26121da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone);
26131da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy);
26141da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits);
26151da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev);
26161da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits);
26171da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand);
26181da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic);
26191da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad);
26201da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom);
26211da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic);
26221da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue);
26231da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail);
26241da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert);
26251da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge);
26261da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head);
26271da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail);
26281da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink);
26291da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append);
26301da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split);
2631677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read);
2632677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read);
2633677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read);
26343fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text);
2635e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags);
26364497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2637716ea3a7SDavid Howells 
2638716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec);
2639716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data);
2640f35d9d8aSRusty Russell EXPORT_SYMBOL_GPL(skb_partial_csum_set);
2641