xref: /openbmc/linux/net/core/skbuff.c (revision ead2ceb0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
31da177e4SLinus Torvalds  *
4113aa838SAlan Cox  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
51da177e4SLinus Torvalds  *			Florian La Roche <rzsfl@rz.uni-sb.de>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	Fixes:
81da177e4SLinus Torvalds  *		Alan Cox	:	Fixed the worst of the load
91da177e4SLinus Torvalds  *					balancer bugs.
101da177e4SLinus Torvalds  *		Dave Platt	:	Interrupt stacking fix.
111da177e4SLinus Torvalds  *	Richard Kooijman	:	Timestamp fixes.
121da177e4SLinus Torvalds  *		Alan Cox	:	Changed buffer format.
131da177e4SLinus Torvalds  *		Alan Cox	:	destructor hook for AF_UNIX etc.
141da177e4SLinus Torvalds  *		Linus Torvalds	:	Better skb_clone.
151da177e4SLinus Torvalds  *		Alan Cox	:	Added skb_copy.
161da177e4SLinus Torvalds  *		Alan Cox	:	Added all the changed routines Linus
171da177e4SLinus Torvalds  *					only put in the headers
181da177e4SLinus Torvalds  *		Ray VanTassle	:	Fixed --skb->lock in free
191da177e4SLinus Torvalds  *		Alan Cox	:	skb_copy copy arp field
201da177e4SLinus Torvalds  *		Andi Kleen	:	slabified it.
211da177e4SLinus Torvalds  *		Robert Olsson	:	Removed skb_head_pool
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *	NOTE:
241da177e4SLinus Torvalds  *		The __skb_ routines should be called with interrupts
251da177e4SLinus Torvalds  *	disabled, or you better be *real* sure that the operation is atomic
261da177e4SLinus Torvalds  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
271da177e4SLinus Torvalds  *	or via disabling bottom half handlers, etc).
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
301da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
311da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
321da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  *	The functions in this file will not compile correctly with gcc 2.4.x
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/types.h>
411da177e4SLinus Torvalds #include <linux/kernel.h>
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>
58ac45f602SPatrick Ohly #include <linux/errqueue.h>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #include <net/protocol.h>
611da177e4SLinus Torvalds #include <net/dst.h>
621da177e4SLinus Torvalds #include <net/sock.h>
631da177e4SLinus Torvalds #include <net/checksum.h>
641da177e4SLinus Torvalds #include <net/xfrm.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <asm/uaccess.h>
671da177e4SLinus Torvalds #include <asm/system.h>
68ead2ceb0SNeil Horman #include <trace/skb.h>
691da177e4SLinus Torvalds 
70a1f8e7f7SAl Viro #include "kmap_skb.h"
71a1f8e7f7SAl Viro 
72e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly;
73e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
741da177e4SLinus Torvalds 
759c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
769c55e01cSJens Axboe 				  struct pipe_buffer *buf)
779c55e01cSJens Axboe {
788b9d3728SJarek Poplawski 	put_page(buf->page);
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 {
848b9d3728SJarek Poplawski 	get_page(buf->page);
859c55e01cSJens Axboe }
869c55e01cSJens Axboe 
879c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
889c55e01cSJens Axboe 			       struct pipe_buffer *buf)
899c55e01cSJens Axboe {
909c55e01cSJens Axboe 	return 1;
919c55e01cSJens Axboe }
929c55e01cSJens Axboe 
939c55e01cSJens Axboe 
949c55e01cSJens Axboe /* Pipe buffer operations for a socket. */
959c55e01cSJens Axboe static struct pipe_buf_operations sock_pipe_buf_ops = {
969c55e01cSJens Axboe 	.can_merge = 0,
979c55e01cSJens Axboe 	.map = generic_pipe_buf_map,
989c55e01cSJens Axboe 	.unmap = generic_pipe_buf_unmap,
999c55e01cSJens Axboe 	.confirm = generic_pipe_buf_confirm,
1009c55e01cSJens Axboe 	.release = sock_pipe_buf_release,
1019c55e01cSJens Axboe 	.steal = sock_pipe_buf_steal,
1029c55e01cSJens Axboe 	.get = sock_pipe_buf_get,
1039c55e01cSJens Axboe };
1049c55e01cSJens Axboe 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  *	Keep out-of-line to prevent kernel bloat.
1071da177e4SLinus Torvalds  *	__builtin_return_address is not used because it is not always
1081da177e4SLinus Torvalds  *	reliable.
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /**
1121da177e4SLinus Torvalds  *	skb_over_panic	- 	private function
1131da177e4SLinus Torvalds  *	@skb: buffer
1141da177e4SLinus Torvalds  *	@sz: size
1151da177e4SLinus Torvalds  *	@here: address
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  *	Out of line support code for skb_put(). Not user callable.
1181da177e4SLinus Torvalds  */
1191da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here)
1201da177e4SLinus Torvalds {
12126095455SPatrick McHardy 	printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
1224305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
12327a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1244305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
12526095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1261da177e4SLinus Torvalds 	BUG();
1271da177e4SLinus Torvalds }
128b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_over_panic);
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /**
1311da177e4SLinus Torvalds  *	skb_under_panic	- 	private function
1321da177e4SLinus Torvalds  *	@skb: buffer
1331da177e4SLinus Torvalds  *	@sz: size
1341da177e4SLinus Torvalds  *	@here: address
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  *	Out of line support code for skb_push(). Not user callable.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here)
1401da177e4SLinus Torvalds {
14126095455SPatrick McHardy 	printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
1424305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
14327a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1444305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
14526095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1461da177e4SLinus Torvalds 	BUG();
1471da177e4SLinus Torvalds }
148b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_under_panic);
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1511da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1521da177e4SLinus Torvalds  *	[BEEP] leaks.
1531da177e4SLinus Torvalds  *
1541da177e4SLinus Torvalds  */
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /**
157d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1581da177e4SLinus Torvalds  *	@size: size to allocate
1591da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
160c83c2486SRandy Dunlap  *	@fclone: allocate from fclone cache instead of head cache
161c83c2486SRandy Dunlap  *		and allocate a cloned (child) skb
162b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1631da177e4SLinus Torvalds  *
1641da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
1651da177e4SLinus Torvalds  *	tail room of size bytes. The object has a reference count of one.
1661da177e4SLinus Torvalds  *	The return is the buffer. On a failure the return is %NULL.
1671da177e4SLinus Torvalds  *
1681da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1691da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1701da177e4SLinus Torvalds  */
171dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
172b30973f8SChristoph Hellwig 			    int fclone, int node)
1731da177e4SLinus Torvalds {
174e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1754947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1761da177e4SLinus Torvalds 	struct sk_buff *skb;
1771da177e4SLinus Torvalds 	u8 *data;
1781da177e4SLinus Torvalds 
1798798b3fbSHerbert Xu 	cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
1808798b3fbSHerbert Xu 
1811da177e4SLinus Torvalds 	/* Get the HEAD */
182b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1831da177e4SLinus Torvalds 	if (!skb)
1841da177e4SLinus Torvalds 		goto out;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
187b30973f8SChristoph Hellwig 	data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
188b30973f8SChristoph Hellwig 			gfp_mask, node);
1891da177e4SLinus Torvalds 	if (!data)
1901da177e4SLinus Torvalds 		goto nodata;
1911da177e4SLinus Torvalds 
192ca0605a7SArnaldo Carvalho de Melo 	/*
193c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
194c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
195c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
196ca0605a7SArnaldo Carvalho de Melo 	 */
197ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
1981da177e4SLinus Torvalds 	skb->truesize = size + sizeof(struct sk_buff);
1991da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2001da177e4SLinus Torvalds 	skb->head = data;
2011da177e4SLinus Torvalds 	skb->data = data;
20227a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2034305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
2044947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2054947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
2064947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
2074947d3efSBenjamin LaHaise 	shinfo->nr_frags  = 0;
2087967168cSHerbert Xu 	shinfo->gso_size = 0;
2097967168cSHerbert Xu 	shinfo->gso_segs = 0;
2107967168cSHerbert Xu 	shinfo->gso_type = 0;
2114947d3efSBenjamin LaHaise 	shinfo->ip6_frag_id = 0;
212ac45f602SPatrick Ohly 	shinfo->tx_flags.flags = 0;
2134947d3efSBenjamin LaHaise 	shinfo->frag_list = NULL;
214ac45f602SPatrick Ohly 	memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
2154947d3efSBenjamin LaHaise 
216d179cd12SDavid S. Miller 	if (fclone) {
217d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
218d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
2191da177e4SLinus Torvalds 
220d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
221d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
222d179cd12SDavid S. Miller 
223d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
224d179cd12SDavid S. Miller 	}
2251da177e4SLinus Torvalds out:
2261da177e4SLinus Torvalds 	return skb;
2271da177e4SLinus Torvalds nodata:
2288798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2291da177e4SLinus Torvalds 	skb = NULL;
2301da177e4SLinus Torvalds 	goto out;
2311da177e4SLinus Torvalds }
232b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds /**
2358af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
2368af27456SChristoph Hellwig  *	@dev: network device to receive on
2378af27456SChristoph Hellwig  *	@length: length to allocate
2388af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
2398af27456SChristoph Hellwig  *
2408af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
2418af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
2428af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
2438af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
2448af27456SChristoph Hellwig  *
2458af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
2468af27456SChristoph Hellwig  */
2478af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
2488af27456SChristoph Hellwig 		unsigned int length, gfp_t gfp_mask)
2498af27456SChristoph Hellwig {
25043cb76d9SGreg Kroah-Hartman 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
2518af27456SChristoph Hellwig 	struct sk_buff *skb;
2528af27456SChristoph Hellwig 
253b30973f8SChristoph Hellwig 	skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
2547b2e497aSChristoph Hellwig 	if (likely(skb)) {
2558af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
2567b2e497aSChristoph Hellwig 		skb->dev = dev;
2577b2e497aSChristoph Hellwig 	}
2588af27456SChristoph Hellwig 	return skb;
2598af27456SChristoph Hellwig }
260b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
2611da177e4SLinus Torvalds 
262654bed16SPeter Zijlstra struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
263654bed16SPeter Zijlstra {
264654bed16SPeter Zijlstra 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
265654bed16SPeter Zijlstra 	struct page *page;
266654bed16SPeter Zijlstra 
267654bed16SPeter Zijlstra 	page = alloc_pages_node(node, gfp_mask, 0);
268654bed16SPeter Zijlstra 	return page;
269654bed16SPeter Zijlstra }
270654bed16SPeter Zijlstra EXPORT_SYMBOL(__netdev_alloc_page);
271654bed16SPeter Zijlstra 
272654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
273654bed16SPeter Zijlstra 		int size)
274654bed16SPeter Zijlstra {
275654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
276654bed16SPeter Zijlstra 	skb->len += size;
277654bed16SPeter Zijlstra 	skb->data_len += size;
278654bed16SPeter Zijlstra 	skb->truesize += size;
279654bed16SPeter Zijlstra }
280654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
281654bed16SPeter Zijlstra 
282f58518e6SIlpo Järvinen /**
283f58518e6SIlpo Järvinen  *	dev_alloc_skb - allocate an skbuff for receiving
284f58518e6SIlpo Järvinen  *	@length: length to allocate
285f58518e6SIlpo Järvinen  *
286f58518e6SIlpo Järvinen  *	Allocate a new &sk_buff and assign it a usage count of one. The
287f58518e6SIlpo Järvinen  *	buffer has unspecified headroom built in. Users should allocate
288f58518e6SIlpo Järvinen  *	the headroom they think they need without accounting for the
289f58518e6SIlpo Järvinen  *	built in space. The built in space is used for optimisations.
290f58518e6SIlpo Järvinen  *
291f58518e6SIlpo Järvinen  *	%NULL is returned if there is no free memory. Although this function
292f58518e6SIlpo Järvinen  *	allocates memory it can be called from an interrupt.
293f58518e6SIlpo Järvinen  */
294f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length)
295f58518e6SIlpo Järvinen {
2961483b874SDenys Vlasenko 	/*
2971483b874SDenys Vlasenko 	 * There is more code here than it seems:
298a0f55e0eSDavid S. Miller 	 * __dev_alloc_skb is an inline
2991483b874SDenys Vlasenko 	 */
300f58518e6SIlpo Järvinen 	return __dev_alloc_skb(length, GFP_ATOMIC);
301f58518e6SIlpo Järvinen }
302f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb);
303f58518e6SIlpo Järvinen 
30427b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
3051da177e4SLinus Torvalds {
30627b437c8SHerbert Xu 	struct sk_buff *list = *listp;
3071da177e4SLinus Torvalds 
30827b437c8SHerbert Xu 	*listp = NULL;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	do {
3111da177e4SLinus Torvalds 		struct sk_buff *this = list;
3121da177e4SLinus Torvalds 		list = list->next;
3131da177e4SLinus Torvalds 		kfree_skb(this);
3141da177e4SLinus Torvalds 	} while (list);
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
31727b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
31827b437c8SHerbert Xu {
31927b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
32027b437c8SHerbert Xu }
32127b437c8SHerbert Xu 
3221da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	struct sk_buff *list;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
3271da177e4SLinus Torvalds 		skb_get(list);
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3305bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	if (!skb->cloned ||
3331da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
3341da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
3351da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
3361da177e4SLinus Torvalds 			int i;
3371da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3381da177e4SLinus Torvalds 				put_page(skb_shinfo(skb)->frags[i].page);
3391da177e4SLinus Torvalds 		}
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frag_list)
3421da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 		kfree(skb->head);
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /*
3491da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
3501da177e4SLinus Torvalds  */
3512d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
3521da177e4SLinus Torvalds {
353d179cd12SDavid S. Miller 	struct sk_buff *other;
354d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
355d179cd12SDavid S. Miller 
356d179cd12SDavid S. Miller 	switch (skb->fclone) {
357d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
3581da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
359d179cd12SDavid S. Miller 		break;
360d179cd12SDavid S. Miller 
361d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
362d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
363d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
364d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
365d179cd12SDavid S. Miller 		break;
366d179cd12SDavid S. Miller 
367d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
368d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
369d179cd12SDavid S. Miller 		other = skb - 1;
370d179cd12SDavid S. Miller 
371d179cd12SDavid S. Miller 		/* The clone portion is available for
372d179cd12SDavid S. Miller 		 * fast-cloning again.
373d179cd12SDavid S. Miller 		 */
374d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
375d179cd12SDavid S. Miller 
376d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
377d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
378d179cd12SDavid S. Miller 		break;
3793ff50b79SStephen Hemminger 	}
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
38204a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	dst_release(skb->dst);
3851da177e4SLinus Torvalds #ifdef CONFIG_XFRM
3861da177e4SLinus Torvalds 	secpath_put(skb->sp);
3871da177e4SLinus Torvalds #endif
3881da177e4SLinus Torvalds 	if (skb->destructor) {
3899c2b3328SStephen Hemminger 		WARN_ON(in_irq());
3901da177e4SLinus Torvalds 		skb->destructor(skb);
3911da177e4SLinus Torvalds 	}
3929fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
3935f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
3949fb9cbb1SYasuyuki Kozakai 	nf_conntrack_put_reasm(skb->nfct_reasm);
3959fb9cbb1SYasuyuki Kozakai #endif
3961da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
3971da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
3981da177e4SLinus Torvalds #endif
3991da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
4001da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4011da177e4SLinus Torvalds 	skb->tc_index = 0;
4021da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
4031da177e4SLinus Torvalds 	skb->tc_verd = 0;
4041da177e4SLinus Torvalds #endif
4051da177e4SLinus Torvalds #endif
40604a4bb55SLennert Buytenhek }
40704a4bb55SLennert Buytenhek 
40804a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
40904a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
41004a4bb55SLennert Buytenhek {
41104a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
4122d4baff8SHerbert Xu 	skb_release_data(skb);
4132d4baff8SHerbert Xu }
4141da177e4SLinus Torvalds 
4152d4baff8SHerbert Xu /**
4162d4baff8SHerbert Xu  *	__kfree_skb - private function
4172d4baff8SHerbert Xu  *	@skb: buffer
4182d4baff8SHerbert Xu  *
4192d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
4202d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
4212d4baff8SHerbert Xu  *	always call kfree_skb
4222d4baff8SHerbert Xu  */
4232d4baff8SHerbert Xu 
4242d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
4252d4baff8SHerbert Xu {
4262d4baff8SHerbert Xu 	skb_release_all(skb);
4271da177e4SLinus Torvalds 	kfree_skbmem(skb);
4281da177e4SLinus Torvalds }
429b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /**
432231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
433231d06aeSJörn Engel  *	@skb: buffer to free
434231d06aeSJörn Engel  *
435231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
436231d06aeSJörn Engel  *	hit zero.
437231d06aeSJörn Engel  */
438231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
439231d06aeSJörn Engel {
440231d06aeSJörn Engel 	if (unlikely(!skb))
441231d06aeSJörn Engel 		return;
442231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
443231d06aeSJörn Engel 		smp_rmb();
444231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
445231d06aeSJörn Engel 		return;
446ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
447231d06aeSJörn Engel 	__kfree_skb(skb);
448231d06aeSJörn Engel }
449b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
450231d06aeSJörn Engel 
451d1a203eaSStephen Hemminger /**
452ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
453ead2ceb0SNeil Horman  *	@skb: buffer to free
454ead2ceb0SNeil Horman  *
455ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
456ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
457ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
458ead2ceb0SNeil Horman  */
459ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
460ead2ceb0SNeil Horman {
461ead2ceb0SNeil Horman 	if (unlikely(!skb))
462ead2ceb0SNeil Horman 		return;
463ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
464ead2ceb0SNeil Horman 		smp_rmb();
465ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
466ead2ceb0SNeil Horman 		return;
467ead2ceb0SNeil Horman 	__kfree_skb(skb);
468ead2ceb0SNeil Horman }
469ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
470ead2ceb0SNeil Horman 
471ead2ceb0SNeil Horman /**
472d1a203eaSStephen Hemminger  *	skb_recycle_check - check if skb can be reused for receive
473d1a203eaSStephen Hemminger  *	@skb: buffer
474d1a203eaSStephen Hemminger  *	@skb_size: minimum receive buffer size
475d1a203eaSStephen Hemminger  *
476d1a203eaSStephen Hemminger  *	Checks that the skb passed in is not shared or cloned, and
477d1a203eaSStephen Hemminger  *	that it is linear and its head portion at least as large as
478d1a203eaSStephen Hemminger  *	skb_size so that it can be recycled as a receive buffer.
479d1a203eaSStephen Hemminger  *	If these conditions are met, this function does any necessary
480d1a203eaSStephen Hemminger  *	reference count dropping and cleans up the skbuff as if it
481d1a203eaSStephen Hemminger  *	just came from __alloc_skb().
482d1a203eaSStephen Hemminger  */
48304a4bb55SLennert Buytenhek int skb_recycle_check(struct sk_buff *skb, int skb_size)
48404a4bb55SLennert Buytenhek {
48504a4bb55SLennert Buytenhek 	struct skb_shared_info *shinfo;
48604a4bb55SLennert Buytenhek 
48704a4bb55SLennert Buytenhek 	if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
48804a4bb55SLennert Buytenhek 		return 0;
48904a4bb55SLennert Buytenhek 
49004a4bb55SLennert Buytenhek 	skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
49104a4bb55SLennert Buytenhek 	if (skb_end_pointer(skb) - skb->head < skb_size)
49204a4bb55SLennert Buytenhek 		return 0;
49304a4bb55SLennert Buytenhek 
49404a4bb55SLennert Buytenhek 	if (skb_shared(skb) || skb_cloned(skb))
49504a4bb55SLennert Buytenhek 		return 0;
49604a4bb55SLennert Buytenhek 
49704a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
49804a4bb55SLennert Buytenhek 	shinfo = skb_shinfo(skb);
49904a4bb55SLennert Buytenhek 	atomic_set(&shinfo->dataref, 1);
50004a4bb55SLennert Buytenhek 	shinfo->nr_frags = 0;
50104a4bb55SLennert Buytenhek 	shinfo->gso_size = 0;
50204a4bb55SLennert Buytenhek 	shinfo->gso_segs = 0;
50304a4bb55SLennert Buytenhek 	shinfo->gso_type = 0;
50404a4bb55SLennert Buytenhek 	shinfo->ip6_frag_id = 0;
50504a4bb55SLennert Buytenhek 	shinfo->frag_list = NULL;
50604a4bb55SLennert Buytenhek 
50704a4bb55SLennert Buytenhek 	memset(skb, 0, offsetof(struct sk_buff, tail));
50804a4bb55SLennert Buytenhek 	skb->data = skb->head + NET_SKB_PAD;
5095cd33db2SLennert Buytenhek 	skb_reset_tail_pointer(skb);
51004a4bb55SLennert Buytenhek 
51104a4bb55SLennert Buytenhek 	return 1;
51204a4bb55SLennert Buytenhek }
51304a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check);
51404a4bb55SLennert Buytenhek 
515dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
516dec18810SHerbert Xu {
517dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
518dec18810SHerbert Xu 	new->dev		= old->dev;
519dec18810SHerbert Xu 	new->transport_header	= old->transport_header;
520dec18810SHerbert Xu 	new->network_header	= old->network_header;
521dec18810SHerbert Xu 	new->mac_header		= old->mac_header;
522dec18810SHerbert Xu 	new->dst		= dst_clone(old->dst);
523def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
524dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
525dec18810SHerbert Xu #endif
526dec18810SHerbert Xu 	memcpy(new->cb, old->cb, sizeof(old->cb));
527dec18810SHerbert Xu 	new->csum_start		= old->csum_start;
528dec18810SHerbert Xu 	new->csum_offset	= old->csum_offset;
529dec18810SHerbert Xu 	new->local_df		= old->local_df;
530dec18810SHerbert Xu 	new->pkt_type		= old->pkt_type;
531dec18810SHerbert Xu 	new->ip_summed		= old->ip_summed;
532dec18810SHerbert Xu 	skb_copy_queue_mapping(new, old);
533dec18810SHerbert Xu 	new->priority		= old->priority;
534dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
535dec18810SHerbert Xu 	new->ipvs_property	= old->ipvs_property;
536dec18810SHerbert Xu #endif
537dec18810SHerbert Xu 	new->protocol		= old->protocol;
538dec18810SHerbert Xu 	new->mark		= old->mark;
539dec18810SHerbert Xu 	__nf_copy(new, old);
540dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
541dec18810SHerbert Xu     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
542dec18810SHerbert Xu 	new->nf_trace		= old->nf_trace;
543dec18810SHerbert Xu #endif
544dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED
545dec18810SHerbert Xu 	new->tc_index		= old->tc_index;
546dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT
547dec18810SHerbert Xu 	new->tc_verd		= old->tc_verd;
548dec18810SHerbert Xu #endif
549dec18810SHerbert Xu #endif
5506aa895b0SPatrick McHardy 	new->vlan_tci		= old->vlan_tci;
5516aa895b0SPatrick McHardy 
552dec18810SHerbert Xu 	skb_copy_secmark(new, old);
553dec18810SHerbert Xu }
554dec18810SHerbert Xu 
555e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds #define C(x) n->x = skb->x
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	n->next = n->prev = NULL;
5601da177e4SLinus Torvalds 	n->sk = NULL;
561dec18810SHerbert Xu 	__copy_skb_header(n, skb);
562dec18810SHerbert Xu 
5631da177e4SLinus Torvalds 	C(len);
5641da177e4SLinus Torvalds 	C(data_len);
5653e6b3b2eSAlexey Dobriyan 	C(mac_len);
566334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
56702f1c89dSPaul Moore 	n->cloned = 1;
5681da177e4SLinus Torvalds 	n->nohdr = 0;
5691da177e4SLinus Torvalds 	n->destructor = NULL;
57002f1c89dSPaul Moore 	C(iif);
5711da177e4SLinus Torvalds 	C(tail);
5721da177e4SLinus Torvalds 	C(end);
57302f1c89dSPaul Moore 	C(head);
57402f1c89dSPaul Moore 	C(data);
57502f1c89dSPaul Moore 	C(truesize);
576d0f09804SJohannes Berg #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
577d0f09804SJohannes Berg 	C(do_not_encrypt);
5788b30b1feSSujith 	C(requeue);
579d0f09804SJohannes Berg #endif
58002f1c89dSPaul Moore 	atomic_set(&n->users, 1);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
5831da177e4SLinus Torvalds 	skb->cloned = 1;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	return n;
586e0053ec0SHerbert Xu #undef C
587e0053ec0SHerbert Xu }
588e0053ec0SHerbert Xu 
589e0053ec0SHerbert Xu /**
590e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
591e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
592e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
593e0053ec0SHerbert Xu  *
594e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
595e0053ec0SHerbert Xu  *	supplied by the user.
596e0053ec0SHerbert Xu  *
597e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
598e0053ec0SHerbert Xu  */
599e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
600e0053ec0SHerbert Xu {
6012d4baff8SHerbert Xu 	skb_release_all(dst);
602e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
603e0053ec0SHerbert Xu }
604e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
605e0053ec0SHerbert Xu 
606e0053ec0SHerbert Xu /**
607e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
608e0053ec0SHerbert Xu  *	@skb: buffer to clone
609e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
610e0053ec0SHerbert Xu  *
611e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
612e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
613e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
614e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
615e0053ec0SHerbert Xu  *
616e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
617e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
618e0053ec0SHerbert Xu  */
619e0053ec0SHerbert Xu 
620e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
621e0053ec0SHerbert Xu {
622e0053ec0SHerbert Xu 	struct sk_buff *n;
623e0053ec0SHerbert Xu 
624e0053ec0SHerbert Xu 	n = skb + 1;
625e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
626e0053ec0SHerbert Xu 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
627e0053ec0SHerbert Xu 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
628e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
629e0053ec0SHerbert Xu 		atomic_inc(fclone_ref);
630e0053ec0SHerbert Xu 	} else {
631e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
632e0053ec0SHerbert Xu 		if (!n)
633e0053ec0SHerbert Xu 			return NULL;
634e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
635e0053ec0SHerbert Xu 	}
636e0053ec0SHerbert Xu 
637e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
6381da177e4SLinus Torvalds }
639b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
6421da177e4SLinus Torvalds {
6432e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
6441da177e4SLinus Torvalds 	/*
6451da177e4SLinus Torvalds 	 *	Shift between the two data areas in bytes
6461da177e4SLinus Torvalds 	 */
6471da177e4SLinus Torvalds 	unsigned long offset = new->data - old->data;
6482e07fa9cSArnaldo Carvalho de Melo #endif
649dec18810SHerbert Xu 
650dec18810SHerbert Xu 	__copy_skb_header(new, old);
651dec18810SHerbert Xu 
6522e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
6532e07fa9cSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header are relative to skb->head */
6542e07fa9cSArnaldo Carvalho de Melo 	new->transport_header += offset;
6552e07fa9cSArnaldo Carvalho de Melo 	new->network_header   += offset;
6562e07fa9cSArnaldo Carvalho de Melo 	new->mac_header	      += offset;
6572e07fa9cSArnaldo Carvalho de Melo #endif
6587967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
6597967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
6607967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds /**
6641da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
6651da177e4SLinus Torvalds  *	@skb: buffer to copy
6661da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6671da177e4SLinus Torvalds  *
6681da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
6691da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
6701da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
6711da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
6721da177e4SLinus Torvalds  *
6731da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
6741da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
6751da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
6761da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
6771da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
6781da177e4SLinus Torvalds  */
6791da177e4SLinus Torvalds 
680dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	int headerlen = skb->data - skb->head;
6831da177e4SLinus Torvalds 	/*
6841da177e4SLinus Torvalds 	 *	Allocate the copy buffer
6851da177e4SLinus Torvalds 	 */
6864305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
6874305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
6884305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end + skb->data_len, gfp_mask);
6894305b541SArnaldo Carvalho de Melo #else
6904305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
6914305b541SArnaldo Carvalho de Melo #endif
6921da177e4SLinus Torvalds 	if (!n)
6931da177e4SLinus Torvalds 		return NULL;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	/* Set the data pointer */
6961da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
6971da177e4SLinus Torvalds 	/* Set the tail pointer and length */
6981da177e4SLinus Torvalds 	skb_put(n, skb->len);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
7011da177e4SLinus Torvalds 		BUG();
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7041da177e4SLinus Torvalds 	return n;
7051da177e4SLinus Torvalds }
706b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /**
7091da177e4SLinus Torvalds  *	pskb_copy	-	create copy of an sk_buff with private head.
7101da177e4SLinus Torvalds  *	@skb: buffer to copy
7111da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7121da177e4SLinus Torvalds  *
7131da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
7141da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
7151da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
7161da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
7171da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
7181da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
7191da177e4SLinus Torvalds  */
7201da177e4SLinus Torvalds 
721dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	/*
7241da177e4SLinus Torvalds 	 *	Allocate the copy buffer
7251da177e4SLinus Torvalds 	 */
7264305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
7274305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
7284305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end, gfp_mask);
7294305b541SArnaldo Carvalho de Melo #else
7304305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head, gfp_mask);
7314305b541SArnaldo Carvalho de Melo #endif
7321da177e4SLinus Torvalds 	if (!n)
7331da177e4SLinus Torvalds 		goto out;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/* Set the data pointer */
7361da177e4SLinus Torvalds 	skb_reserve(n, skb->data - skb->head);
7371da177e4SLinus Torvalds 	/* Set the tail pointer and length */
7381da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
7391da177e4SLinus Torvalds 	/* Copy the bytes */
740d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
7411da177e4SLinus Torvalds 
74225f484a6SHerbert Xu 	n->truesize += skb->data_len;
7431da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
7441da177e4SLinus Torvalds 	n->len	     = skb->len;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
7471da177e4SLinus Torvalds 		int i;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
7501da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
7511da177e4SLinus Torvalds 			get_page(skb_shinfo(n)->frags[i].page);
7521da177e4SLinus Torvalds 		}
7531da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
7541da177e4SLinus Torvalds 	}
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
7571da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
7581da177e4SLinus Torvalds 		skb_clone_fraglist(n);
7591da177e4SLinus Torvalds 	}
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7621da177e4SLinus Torvalds out:
7631da177e4SLinus Torvalds 	return n;
7641da177e4SLinus Torvalds }
765b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_copy);
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds /**
7681da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
7691da177e4SLinus Torvalds  *	@skb: buffer to reallocate
7701da177e4SLinus Torvalds  *	@nhead: room to add at head
7711da177e4SLinus Torvalds  *	@ntail: room to add at tail
7721da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7731da177e4SLinus Torvalds  *
7741da177e4SLinus Torvalds  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
7751da177e4SLinus Torvalds  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
7761da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
7771da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
7781da177e4SLinus Torvalds  *
7791da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
7801da177e4SLinus Torvalds  *	reloaded after call to this function.
7811da177e4SLinus Torvalds  */
7821da177e4SLinus Torvalds 
78386a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
784dd0fc66fSAl Viro 		     gfp_t gfp_mask)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	int i;
7871da177e4SLinus Torvalds 	u8 *data;
7884305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
7894305b541SArnaldo Carvalho de Melo 	int size = nhead + skb->end + ntail;
7904305b541SArnaldo Carvalho de Melo #else
7911da177e4SLinus Torvalds 	int size = nhead + (skb->end - skb->head) + ntail;
7924305b541SArnaldo Carvalho de Melo #endif
7931da177e4SLinus Torvalds 	long off;
7941da177e4SLinus Torvalds 
7954edd87adSHerbert Xu 	BUG_ON(nhead < 0);
7964edd87adSHerbert Xu 
7971da177e4SLinus Torvalds 	if (skb_shared(skb))
7981da177e4SLinus Torvalds 		BUG();
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
8031da177e4SLinus Torvalds 	if (!data)
8041da177e4SLinus Torvalds 		goto nodata;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
8071da177e4SLinus Torvalds 	 * optimized for the cases when header is void. */
8084305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
809b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail);
8104305b541SArnaldo Carvalho de Melo #else
811b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail - skb->head);
81227a884dcSArnaldo Carvalho de Melo #endif
8134305b541SArnaldo Carvalho de Melo 	memcpy(data + size, skb_end_pointer(skb),
8144305b541SArnaldo Carvalho de Melo 	       sizeof(struct skb_shared_info));
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
8171da177e4SLinus Torvalds 		get_page(skb_shinfo(skb)->frags[i].page);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list)
8201da177e4SLinus Torvalds 		skb_clone_fraglist(skb);
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	skb_release_data(skb);
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	skb->head     = data;
8271da177e4SLinus Torvalds 	skb->data    += off;
8284305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
8294305b541SArnaldo Carvalho de Melo 	skb->end      = size;
83056eb8882SPatrick McHardy 	off           = nhead;
8314305b541SArnaldo Carvalho de Melo #else
8324305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
83356eb8882SPatrick McHardy #endif
83427a884dcSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header and tail are relative to skb->head */
83527a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
836b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header += off;
837b0e380b1SArnaldo Carvalho de Melo 	skb->network_header   += off;
838b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header	      += off;
839172a863fSHerbert Xu 	skb->csum_start       += nhead;
8401da177e4SLinus Torvalds 	skb->cloned   = 0;
841334a8132SPatrick McHardy 	skb->hdr_len  = 0;
8421da177e4SLinus Torvalds 	skb->nohdr    = 0;
8431da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
8441da177e4SLinus Torvalds 	return 0;
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds nodata:
8471da177e4SLinus Torvalds 	return -ENOMEM;
8481da177e4SLinus Torvalds }
849b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	struct sk_buff *skb2;
8561da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	if (delta <= 0)
8591da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
8601da177e4SLinus Torvalds 	else {
8611da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
8621da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
8631da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
8641da177e4SLinus Torvalds 			kfree_skb(skb2);
8651da177e4SLinus Torvalds 			skb2 = NULL;
8661da177e4SLinus Torvalds 		}
8671da177e4SLinus Torvalds 	}
8681da177e4SLinus Torvalds 	return skb2;
8691da177e4SLinus Torvalds }
870b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds /**
8731da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
8741da177e4SLinus Torvalds  *	@skb: buffer to copy
8751da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
8761da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
8771da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
8781da177e4SLinus Torvalds  *
8791da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
8801da177e4SLinus Torvalds  *	allocate additional space.
8811da177e4SLinus Torvalds  *
8821da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
8831da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
8841da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
8851da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
8861da177e4SLinus Torvalds  *
8871da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
8881da177e4SLinus Torvalds  *	is called from an interrupt.
8891da177e4SLinus Torvalds  */
8901da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
89186a76cafSVictor Fusco 				int newheadroom, int newtailroom,
892dd0fc66fSAl Viro 				gfp_t gfp_mask)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	/*
8951da177e4SLinus Torvalds 	 *	Allocate the copy buffer
8961da177e4SLinus Torvalds 	 */
8971da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
8981da177e4SLinus Torvalds 				      gfp_mask);
899efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
9001da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
90152886051SHerbert Xu 	int off;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds 	if (!n)
9041da177e4SLinus Torvalds 		return NULL;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	/* Set the tail pointer and length */
9091da177e4SLinus Torvalds 	skb_put(n, skb->len);
9101da177e4SLinus Torvalds 
911efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
9121da177e4SLinus Torvalds 	head_copy_off = 0;
9131da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
9141da177e4SLinus Torvalds 		head_copy_len = newheadroom;
9151da177e4SLinus Torvalds 	else
9161da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/* Copy the linear header and data. */
9191da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
9201da177e4SLinus Torvalds 			  skb->len + head_copy_len))
9211da177e4SLinus Torvalds 		BUG();
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds 	copy_skb_header(n, skb);
9241da177e4SLinus Torvalds 
925efd1e8d5SPatrick McHardy 	off                  = newheadroom - oldheadroom;
92652886051SHerbert Xu 	n->csum_start       += off;
92752886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET
928efd1e8d5SPatrick McHardy 	n->transport_header += off;
929efd1e8d5SPatrick McHardy 	n->network_header   += off;
930efd1e8d5SPatrick McHardy 	n->mac_header	    += off;
93152886051SHerbert Xu #endif
932efd1e8d5SPatrick McHardy 
9331da177e4SLinus Torvalds 	return n;
9341da177e4SLinus Torvalds }
935b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds /**
9381da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
9391da177e4SLinus Torvalds  *	@skb: buffer to pad
9401da177e4SLinus Torvalds  *	@pad: space to pad
9411da177e4SLinus Torvalds  *
9421da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
9431da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
9441da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
9451da177e4SLinus Torvalds  *
9465b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
9471da177e4SLinus Torvalds  */
9481da177e4SLinus Torvalds 
9495b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
9501da177e4SLinus Torvalds {
9515b057c6bSHerbert Xu 	int err;
9525b057c6bSHerbert Xu 	int ntail;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
9555b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
9561da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
9575b057c6bSHerbert Xu 		return 0;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9604305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
9615b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
9625b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
9635b057c6bSHerbert Xu 		if (unlikely(err))
9645b057c6bSHerbert Xu 			goto free_skb;
9655b057c6bSHerbert Xu 	}
9665b057c6bSHerbert Xu 
9675b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
9685b057c6bSHerbert Xu 	 * to be audited.
9695b057c6bSHerbert Xu 	 */
9705b057c6bSHerbert Xu 	err = skb_linearize(skb);
9715b057c6bSHerbert Xu 	if (unlikely(err))
9725b057c6bSHerbert Xu 		goto free_skb;
9735b057c6bSHerbert Xu 
9745b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
9755b057c6bSHerbert Xu 	return 0;
9765b057c6bSHerbert Xu 
9775b057c6bSHerbert Xu free_skb:
9781da177e4SLinus Torvalds 	kfree_skb(skb);
9795b057c6bSHerbert Xu 	return err;
9801da177e4SLinus Torvalds }
981b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
9821da177e4SLinus Torvalds 
9830dde3e16SIlpo Järvinen /**
9840dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
9850dde3e16SIlpo Järvinen  *	@skb: buffer to use
9860dde3e16SIlpo Järvinen  *	@len: amount of data to add
9870dde3e16SIlpo Järvinen  *
9880dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
9890dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
9900dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
9910dde3e16SIlpo Järvinen  */
9920dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
9930dde3e16SIlpo Järvinen {
9940dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
9950dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
9960dde3e16SIlpo Järvinen 	skb->tail += len;
9970dde3e16SIlpo Järvinen 	skb->len  += len;
9980dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
9990dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
10000dde3e16SIlpo Järvinen 	return tmp;
10010dde3e16SIlpo Järvinen }
10020dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
10030dde3e16SIlpo Järvinen 
10046be8ac2fSIlpo Järvinen /**
1005c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1006c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1007c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1008c2aa270aSIlpo Järvinen  *
1009c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1010c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1011c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1012c2aa270aSIlpo Järvinen  */
1013c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1014c2aa270aSIlpo Järvinen {
1015c2aa270aSIlpo Järvinen 	skb->data -= len;
1016c2aa270aSIlpo Järvinen 	skb->len  += len;
1017c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1018c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1019c2aa270aSIlpo Järvinen 	return skb->data;
1020c2aa270aSIlpo Järvinen }
1021c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1022c2aa270aSIlpo Järvinen 
1023c2aa270aSIlpo Järvinen /**
10246be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
10256be8ac2fSIlpo Järvinen  *	@skb: buffer to use
10266be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
10276be8ac2fSIlpo Järvinen  *
10286be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
10296be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
10306be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
10316be8ac2fSIlpo Järvinen  *	the old data.
10326be8ac2fSIlpo Järvinen  */
10336be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
10346be8ac2fSIlpo Järvinen {
10356be8ac2fSIlpo Järvinen 	return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
10366be8ac2fSIlpo Järvinen }
10376be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
10386be8ac2fSIlpo Järvinen 
1039419ae74eSIlpo Järvinen /**
1040419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1041419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1042419ae74eSIlpo Järvinen  *	@len: new length
1043419ae74eSIlpo Järvinen  *
1044419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1045419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1046419ae74eSIlpo Järvinen  *	The skb must be linear.
1047419ae74eSIlpo Järvinen  */
1048419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1049419ae74eSIlpo Järvinen {
1050419ae74eSIlpo Järvinen 	if (skb->len > len)
1051419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1052419ae74eSIlpo Järvinen }
1053419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1054419ae74eSIlpo Järvinen 
10553cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
10561da177e4SLinus Torvalds  */
10571da177e4SLinus Torvalds 
10583cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
10591da177e4SLinus Torvalds {
106027b437c8SHerbert Xu 	struct sk_buff **fragp;
106127b437c8SHerbert Xu 	struct sk_buff *frag;
10621da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
10631da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
10641da177e4SLinus Torvalds 	int i;
106527b437c8SHerbert Xu 	int err;
106627b437c8SHerbert Xu 
106727b437c8SHerbert Xu 	if (skb_cloned(skb) &&
106827b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
106927b437c8SHerbert Xu 		return err;
10701da177e4SLinus Torvalds 
1071f4d26fb3SHerbert Xu 	i = 0;
1072f4d26fb3SHerbert Xu 	if (offset >= len)
1073f4d26fb3SHerbert Xu 		goto drop_pages;
1074f4d26fb3SHerbert Xu 
1075f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
10761da177e4SLinus Torvalds 		int end = offset + skb_shinfo(skb)->frags[i].size;
107727b437c8SHerbert Xu 
107827b437c8SHerbert Xu 		if (end < len) {
10791da177e4SLinus Torvalds 			offset = end;
108027b437c8SHerbert Xu 			continue;
10811da177e4SLinus Torvalds 		}
10821da177e4SLinus Torvalds 
108327b437c8SHerbert Xu 		skb_shinfo(skb)->frags[i++].size = len - offset;
108427b437c8SHerbert Xu 
1085f4d26fb3SHerbert Xu drop_pages:
108627b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
108727b437c8SHerbert Xu 
108827b437c8SHerbert Xu 		for (; i < nfrags; i++)
108927b437c8SHerbert Xu 			put_page(skb_shinfo(skb)->frags[i].page);
109027b437c8SHerbert Xu 
109127b437c8SHerbert Xu 		if (skb_shinfo(skb)->frag_list)
109227b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1093f4d26fb3SHerbert Xu 		goto done;
109427b437c8SHerbert Xu 	}
109527b437c8SHerbert Xu 
109627b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
109727b437c8SHerbert Xu 	     fragp = &frag->next) {
109827b437c8SHerbert Xu 		int end = offset + frag->len;
109927b437c8SHerbert Xu 
110027b437c8SHerbert Xu 		if (skb_shared(frag)) {
110127b437c8SHerbert Xu 			struct sk_buff *nfrag;
110227b437c8SHerbert Xu 
110327b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
110427b437c8SHerbert Xu 			if (unlikely(!nfrag))
110527b437c8SHerbert Xu 				return -ENOMEM;
110627b437c8SHerbert Xu 
110727b437c8SHerbert Xu 			nfrag->next = frag->next;
1108f4d26fb3SHerbert Xu 			kfree_skb(frag);
110927b437c8SHerbert Xu 			frag = nfrag;
111027b437c8SHerbert Xu 			*fragp = frag;
111127b437c8SHerbert Xu 		}
111227b437c8SHerbert Xu 
111327b437c8SHerbert Xu 		if (end < len) {
111427b437c8SHerbert Xu 			offset = end;
111527b437c8SHerbert Xu 			continue;
111627b437c8SHerbert Xu 		}
111727b437c8SHerbert Xu 
111827b437c8SHerbert Xu 		if (end > len &&
111927b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
112027b437c8SHerbert Xu 			return err;
112127b437c8SHerbert Xu 
112227b437c8SHerbert Xu 		if (frag->next)
112327b437c8SHerbert Xu 			skb_drop_list(&frag->next);
112427b437c8SHerbert Xu 		break;
112527b437c8SHerbert Xu 	}
112627b437c8SHerbert Xu 
1127f4d26fb3SHerbert Xu done:
112827b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
11291da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
11301da177e4SLinus Torvalds 		skb->len       = len;
11311da177e4SLinus Torvalds 	} else {
11321da177e4SLinus Torvalds 		skb->len       = len;
11331da177e4SLinus Torvalds 		skb->data_len  = 0;
113427a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
11351da177e4SLinus Torvalds 	}
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	return 0;
11381da177e4SLinus Torvalds }
1139b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds /**
11421da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
11431da177e4SLinus Torvalds  *	@skb: buffer to reallocate
11441da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
11451da177e4SLinus Torvalds  *
11461da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
11471da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
11481da177e4SLinus Torvalds  *	data from fragmented part.
11491da177e4SLinus Torvalds  *
11501da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
11511da177e4SLinus Torvalds  *
11521da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
11531da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
11541da177e4SLinus Torvalds  *
11551da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
11561da177e4SLinus Torvalds  *	reloaded after call to this function.
11571da177e4SLinus Torvalds  */
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
11601da177e4SLinus Torvalds  * when it is necessary.
11611da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
11621da177e4SLinus Torvalds  * 2. It may change skb pointers.
11631da177e4SLinus Torvalds  *
11641da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
11651da177e4SLinus Torvalds  */
11661da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
11691da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
11701da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
11711da177e4SLinus Torvalds 	 */
11724305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
11751da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
11761da177e4SLinus Torvalds 				     GFP_ATOMIC))
11771da177e4SLinus Torvalds 			return NULL;
11781da177e4SLinus Torvalds 	}
11791da177e4SLinus Torvalds 
118027a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
11811da177e4SLinus Torvalds 		BUG();
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
11841da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
11851da177e4SLinus Torvalds 	 */
11861da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->frag_list)
11871da177e4SLinus Torvalds 		goto pull_pages;
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
11901da177e4SLinus Torvalds 	eat = delta;
11911da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
11921da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size >= eat)
11931da177e4SLinus Torvalds 			goto pull_pages;
11941da177e4SLinus Torvalds 		eat -= skb_shinfo(skb)->frags[i].size;
11951da177e4SLinus Torvalds 	}
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
11981da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
11991da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
12001da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
12011da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
12021da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
12031da177e4SLinus Torvalds 	 */
12041da177e4SLinus Torvalds 	if (eat) {
12051da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
12061da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
12071da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 		do {
121009a62660SKris Katterjohn 			BUG_ON(!list);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 			if (list->len <= eat) {
12131da177e4SLinus Torvalds 				/* Eaten as whole. */
12141da177e4SLinus Torvalds 				eat -= list->len;
12151da177e4SLinus Torvalds 				list = list->next;
12161da177e4SLinus Torvalds 				insp = list;
12171da177e4SLinus Torvalds 			} else {
12181da177e4SLinus Torvalds 				/* Eaten partially. */
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 				if (skb_shared(list)) {
12211da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
12221da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
12231da177e4SLinus Torvalds 					if (!clone)
12241da177e4SLinus Torvalds 						return NULL;
12251da177e4SLinus Torvalds 					insp = list->next;
12261da177e4SLinus Torvalds 					list = clone;
12271da177e4SLinus Torvalds 				} else {
12281da177e4SLinus Torvalds 					/* This may be pulled without
12291da177e4SLinus Torvalds 					 * problems. */
12301da177e4SLinus Torvalds 					insp = list;
12311da177e4SLinus Torvalds 				}
12321da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
12331da177e4SLinus Torvalds 					kfree_skb(clone);
12341da177e4SLinus Torvalds 					return NULL;
12351da177e4SLinus Torvalds 				}
12361da177e4SLinus Torvalds 				break;
12371da177e4SLinus Torvalds 			}
12381da177e4SLinus Torvalds 		} while (eat);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 		/* Free pulled out fragments. */
12411da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
12421da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
12431da177e4SLinus Torvalds 			kfree_skb(list);
12441da177e4SLinus Torvalds 		}
12451da177e4SLinus Torvalds 		/* And insert new clone at head. */
12461da177e4SLinus Torvalds 		if (clone) {
12471da177e4SLinus Torvalds 			clone->next = list;
12481da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
12491da177e4SLinus Torvalds 		}
12501da177e4SLinus Torvalds 	}
12511da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds pull_pages:
12541da177e4SLinus Torvalds 	eat = delta;
12551da177e4SLinus Torvalds 	k = 0;
12561da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12571da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size <= eat) {
12581da177e4SLinus Torvalds 			put_page(skb_shinfo(skb)->frags[i].page);
12591da177e4SLinus Torvalds 			eat -= skb_shinfo(skb)->frags[i].size;
12601da177e4SLinus Torvalds 		} else {
12611da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
12621da177e4SLinus Torvalds 			if (eat) {
12631da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
12641da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].size -= eat;
12651da177e4SLinus Torvalds 				eat = 0;
12661da177e4SLinus Torvalds 			}
12671da177e4SLinus Torvalds 			k++;
12681da177e4SLinus Torvalds 		}
12691da177e4SLinus Torvalds 	}
12701da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds 	skb->tail     += delta;
12731da177e4SLinus Torvalds 	skb->data_len -= delta;
12741da177e4SLinus Torvalds 
127527a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
12761da177e4SLinus Torvalds }
1277b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
12821da177e4SLinus Torvalds {
12831da177e4SLinus Torvalds 	int i, copy;
12841a028e50SDavid S. Miller 	int start = skb_headlen(skb);
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
12871da177e4SLinus Torvalds 		goto fault;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 	/* Copy header. */
12901a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
12911da177e4SLinus Torvalds 		if (copy > len)
12921da177e4SLinus Torvalds 			copy = len;
1293d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
12941da177e4SLinus Torvalds 		if ((len -= copy) == 0)
12951da177e4SLinus Torvalds 			return 0;
12961da177e4SLinus Torvalds 		offset += copy;
12971da177e4SLinus Torvalds 		to     += copy;
12981da177e4SLinus Torvalds 	}
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
13011a028e50SDavid S. Miller 		int end;
13021da177e4SLinus Torvalds 
1303547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
13041a028e50SDavid S. Miller 
13051a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
13061da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
13071da177e4SLinus Torvalds 			u8 *vaddr;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 			if (copy > len)
13101da177e4SLinus Torvalds 				copy = len;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
13131da177e4SLinus Torvalds 			memcpy(to,
13141a028e50SDavid S. Miller 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
13151a028e50SDavid S. Miller 			       offset - start, copy);
13161da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 			if ((len -= copy) == 0)
13191da177e4SLinus Torvalds 				return 0;
13201da177e4SLinus Torvalds 			offset += copy;
13211da177e4SLinus Torvalds 			to     += copy;
13221da177e4SLinus Torvalds 		}
13231a028e50SDavid S. Miller 		start = end;
13241da177e4SLinus Torvalds 	}
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
13271da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		for (; list; list = list->next) {
13301a028e50SDavid S. Miller 			int end;
13311da177e4SLinus Torvalds 
1332547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
13331a028e50SDavid S. Miller 
13341a028e50SDavid S. Miller 			end = start + list->len;
13351da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
13361da177e4SLinus Torvalds 				if (copy > len)
13371da177e4SLinus Torvalds 					copy = len;
13381a028e50SDavid S. Miller 				if (skb_copy_bits(list, offset - start,
13391a028e50SDavid S. Miller 						  to, copy))
13401da177e4SLinus Torvalds 					goto fault;
13411da177e4SLinus Torvalds 				if ((len -= copy) == 0)
13421da177e4SLinus Torvalds 					return 0;
13431da177e4SLinus Torvalds 				offset += copy;
13441da177e4SLinus Torvalds 				to     += copy;
13451da177e4SLinus Torvalds 			}
13461a028e50SDavid S. Miller 			start = end;
13471da177e4SLinus Torvalds 		}
13481da177e4SLinus Torvalds 	}
13491da177e4SLinus Torvalds 	if (!len)
13501da177e4SLinus Torvalds 		return 0;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds fault:
13531da177e4SLinus Torvalds 	return -EFAULT;
13541da177e4SLinus Torvalds }
1355b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
13561da177e4SLinus Torvalds 
13579c55e01cSJens Axboe /*
13589c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
13599c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
13609c55e01cSJens Axboe  */
13619c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
13629c55e01cSJens Axboe {
13638b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
13648b9d3728SJarek Poplawski }
13659c55e01cSJens Axboe 
13664fb66994SJarek Poplawski static inline struct page *linear_to_page(struct page *page, unsigned int *len,
13674fb66994SJarek Poplawski 					  unsigned int *offset,
13684fb66994SJarek Poplawski 					  struct sk_buff *skb)
13698b9d3728SJarek Poplawski {
13704fb66994SJarek Poplawski 	struct sock *sk = skb->sk;
13714fb66994SJarek Poplawski 	struct page *p = sk->sk_sndmsg_page;
13724fb66994SJarek Poplawski 	unsigned int off;
13738b9d3728SJarek Poplawski 
13744fb66994SJarek Poplawski 	if (!p) {
13754fb66994SJarek Poplawski new_page:
13764fb66994SJarek Poplawski 		p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
13778b9d3728SJarek Poplawski 		if (!p)
13788b9d3728SJarek Poplawski 			return NULL;
13794fb66994SJarek Poplawski 
13804fb66994SJarek Poplawski 		off = sk->sk_sndmsg_off = 0;
13814fb66994SJarek Poplawski 		/* hold one ref to this page until it's full */
13824fb66994SJarek Poplawski 	} else {
13834fb66994SJarek Poplawski 		unsigned int mlen;
13844fb66994SJarek Poplawski 
13854fb66994SJarek Poplawski 		off = sk->sk_sndmsg_off;
13864fb66994SJarek Poplawski 		mlen = PAGE_SIZE - off;
13874fb66994SJarek Poplawski 		if (mlen < 64 && mlen < *len) {
13884fb66994SJarek Poplawski 			put_page(p);
13894fb66994SJarek Poplawski 			goto new_page;
13904fb66994SJarek Poplawski 		}
13914fb66994SJarek Poplawski 
13924fb66994SJarek Poplawski 		*len = min_t(unsigned int, *len, mlen);
13934fb66994SJarek Poplawski 	}
13944fb66994SJarek Poplawski 
13954fb66994SJarek Poplawski 	memcpy(page_address(p) + off, page_address(page) + *offset, *len);
13964fb66994SJarek Poplawski 	sk->sk_sndmsg_off += *len;
13974fb66994SJarek Poplawski 	*offset = off;
13984fb66994SJarek Poplawski 	get_page(p);
13998b9d3728SJarek Poplawski 
14008b9d3728SJarek Poplawski 	return p;
14019c55e01cSJens Axboe }
14029c55e01cSJens Axboe 
14039c55e01cSJens Axboe /*
14049c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
14059c55e01cSJens Axboe  */
14069c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
14074fb66994SJarek Poplawski 				unsigned int *len, unsigned int offset,
14088b9d3728SJarek Poplawski 				struct sk_buff *skb, int linear)
14099c55e01cSJens Axboe {
14109c55e01cSJens Axboe 	if (unlikely(spd->nr_pages == PIPE_BUFFERS))
14119c55e01cSJens Axboe 		return 1;
14129c55e01cSJens Axboe 
14138b9d3728SJarek Poplawski 	if (linear) {
14144fb66994SJarek Poplawski 		page = linear_to_page(page, len, &offset, skb);
14158b9d3728SJarek Poplawski 		if (!page)
14168b9d3728SJarek Poplawski 			return 1;
14178b9d3728SJarek Poplawski 	} else
14188b9d3728SJarek Poplawski 		get_page(page);
14198b9d3728SJarek Poplawski 
14209c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
14214fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
14229c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
14239c55e01cSJens Axboe 	spd->nr_pages++;
14248b9d3728SJarek Poplawski 
14259c55e01cSJens Axboe 	return 0;
14269c55e01cSJens Axboe }
14279c55e01cSJens Axboe 
14282870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff,
14292870c43dSOctavian Purdila 				  unsigned int *plen, unsigned int off)
14302870c43dSOctavian Purdila {
1431ce3dd395SJarek Poplawski 	unsigned long n;
1432ce3dd395SJarek Poplawski 
14332870c43dSOctavian Purdila 	*poff += off;
1434ce3dd395SJarek Poplawski 	n = *poff / PAGE_SIZE;
1435ce3dd395SJarek Poplawski 	if (n)
1436ce3dd395SJarek Poplawski 		*page = nth_page(*page, n);
1437ce3dd395SJarek Poplawski 
14382870c43dSOctavian Purdila 	*poff = *poff % PAGE_SIZE;
14392870c43dSOctavian Purdila 	*plen -= off;
14402870c43dSOctavian Purdila }
14412870c43dSOctavian Purdila 
14422870c43dSOctavian Purdila static inline int __splice_segment(struct page *page, unsigned int poff,
14432870c43dSOctavian Purdila 				   unsigned int plen, unsigned int *off,
14442870c43dSOctavian Purdila 				   unsigned int *len, struct sk_buff *skb,
14458b9d3728SJarek Poplawski 				   struct splice_pipe_desc *spd, int linear)
14469c55e01cSJens Axboe {
14472870c43dSOctavian Purdila 	if (!*len)
14482870c43dSOctavian Purdila 		return 1;
14499c55e01cSJens Axboe 
14502870c43dSOctavian Purdila 	/* skip this segment if already processed */
14512870c43dSOctavian Purdila 	if (*off >= plen) {
14522870c43dSOctavian Purdila 		*off -= plen;
14532870c43dSOctavian Purdila 		return 0;
14542870c43dSOctavian Purdila 	}
14552870c43dSOctavian Purdila 
14562870c43dSOctavian Purdila 	/* ignore any bits we already processed */
14572870c43dSOctavian Purdila 	if (*off) {
14582870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, *off);
14592870c43dSOctavian Purdila 		*off = 0;
14602870c43dSOctavian Purdila 	}
14612870c43dSOctavian Purdila 
14622870c43dSOctavian Purdila 	do {
14632870c43dSOctavian Purdila 		unsigned int flen = min(*len, plen);
14642870c43dSOctavian Purdila 
14652870c43dSOctavian Purdila 		/* the linear region may spread across several pages  */
14662870c43dSOctavian Purdila 		flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
14672870c43dSOctavian Purdila 
14684fb66994SJarek Poplawski 		if (spd_fill_page(spd, page, &flen, poff, skb, linear))
14692870c43dSOctavian Purdila 			return 1;
14702870c43dSOctavian Purdila 
14712870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, flen);
14722870c43dSOctavian Purdila 		*len -= flen;
14732870c43dSOctavian Purdila 
14742870c43dSOctavian Purdila 	} while (*len && plen);
14752870c43dSOctavian Purdila 
14762870c43dSOctavian Purdila 	return 0;
1477db43a282SOctavian Purdila }
14789c55e01cSJens Axboe 
14799c55e01cSJens Axboe /*
14802870c43dSOctavian Purdila  * Map linear and fragment data from the skb to spd. It reports failure if the
14812870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
14829c55e01cSJens Axboe  */
14837b1c65faSHarvey Harrison static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
14842870c43dSOctavian Purdila 		      unsigned int *len,
14852870c43dSOctavian Purdila 		      struct splice_pipe_desc *spd)
14862870c43dSOctavian Purdila {
14872870c43dSOctavian Purdila 	int seg;
14889c55e01cSJens Axboe 
14899c55e01cSJens Axboe 	/*
14902870c43dSOctavian Purdila 	 * map the linear part
14919c55e01cSJens Axboe 	 */
14922870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
14932870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
14942870c43dSOctavian Purdila 			     skb_headlen(skb),
14958b9d3728SJarek Poplawski 			     offset, len, skb, spd, 1))
14962870c43dSOctavian Purdila 		return 1;
14979c55e01cSJens Axboe 
14989c55e01cSJens Axboe 	/*
14999c55e01cSJens Axboe 	 * then map the fragments
15009c55e01cSJens Axboe 	 */
15019c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
15029c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
15039c55e01cSJens Axboe 
15042870c43dSOctavian Purdila 		if (__splice_segment(f->page, f->page_offset, f->size,
15058b9d3728SJarek Poplawski 				     offset, len, skb, spd, 0))
15062870c43dSOctavian Purdila 			return 1;
15079c55e01cSJens Axboe 	}
15089c55e01cSJens Axboe 
15099c55e01cSJens Axboe 	return 0;
15109c55e01cSJens Axboe }
15119c55e01cSJens Axboe 
15129c55e01cSJens Axboe /*
15139c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
15149c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
15159c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
15169c55e01cSJens Axboe  * handle that cleanly.
15179c55e01cSJens Axboe  */
15188b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
15199c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
15209c55e01cSJens Axboe 		    unsigned int flags)
15219c55e01cSJens Axboe {
15229c55e01cSJens Axboe 	struct partial_page partial[PIPE_BUFFERS];
15239c55e01cSJens Axboe 	struct page *pages[PIPE_BUFFERS];
15249c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
15259c55e01cSJens Axboe 		.pages = pages,
15269c55e01cSJens Axboe 		.partial = partial,
15279c55e01cSJens Axboe 		.flags = flags,
15289c55e01cSJens Axboe 		.ops = &sock_pipe_buf_ops,
15299c55e01cSJens Axboe 		.spd_release = sock_spd_release,
15309c55e01cSJens Axboe 	};
15319c55e01cSJens Axboe 
15329c55e01cSJens Axboe 	/*
15339c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
15349c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
15359c55e01cSJens Axboe 	 */
15369c55e01cSJens Axboe 	if (__skb_splice_bits(skb, &offset, &tlen, &spd))
15379c55e01cSJens Axboe 		goto done;
15389c55e01cSJens Axboe 	else if (!tlen)
15399c55e01cSJens Axboe 		goto done;
15409c55e01cSJens Axboe 
15419c55e01cSJens Axboe 	/*
15429c55e01cSJens Axboe 	 * now see if we have a frag_list to map
15439c55e01cSJens Axboe 	 */
15449c55e01cSJens Axboe 	if (skb_shinfo(skb)->frag_list) {
15459c55e01cSJens Axboe 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
15469c55e01cSJens Axboe 
15479c55e01cSJens Axboe 		for (; list && tlen; list = list->next) {
15489c55e01cSJens Axboe 			if (__skb_splice_bits(list, &offset, &tlen, &spd))
15499c55e01cSJens Axboe 				break;
15509c55e01cSJens Axboe 		}
15519c55e01cSJens Axboe 	}
15529c55e01cSJens Axboe 
15539c55e01cSJens Axboe done:
15549c55e01cSJens Axboe 	if (spd.nr_pages) {
15558b9d3728SJarek Poplawski 		struct sock *sk = skb->sk;
15569c55e01cSJens Axboe 		int ret;
15579c55e01cSJens Axboe 
15589c55e01cSJens Axboe 		/*
15599c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
15609c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
15619c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
15629c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
15639c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
15649c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
15659c55e01cSJens Axboe 		 * and networking will grab the socket lock.
15669c55e01cSJens Axboe 		 */
1567293ad604SOctavian Purdila 		release_sock(sk);
15689c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1569293ad604SOctavian Purdila 		lock_sock(sk);
15709c55e01cSJens Axboe 		return ret;
15719c55e01cSJens Axboe 	}
15729c55e01cSJens Axboe 
15739c55e01cSJens Axboe 	return 0;
15749c55e01cSJens Axboe }
15759c55e01cSJens Axboe 
1576357b40a1SHerbert Xu /**
1577357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1578357b40a1SHerbert Xu  *	@skb: destination buffer
1579357b40a1SHerbert Xu  *	@offset: offset in destination
1580357b40a1SHerbert Xu  *	@from: source buffer
1581357b40a1SHerbert Xu  *	@len: number of bytes to copy
1582357b40a1SHerbert Xu  *
1583357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1584357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1585357b40a1SHerbert Xu  *	traversing fragment lists and such.
1586357b40a1SHerbert Xu  */
1587357b40a1SHerbert Xu 
15880c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1589357b40a1SHerbert Xu {
1590357b40a1SHerbert Xu 	int i, copy;
15911a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1592357b40a1SHerbert Xu 
1593357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1594357b40a1SHerbert Xu 		goto fault;
1595357b40a1SHerbert Xu 
15961a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1597357b40a1SHerbert Xu 		if (copy > len)
1598357b40a1SHerbert Xu 			copy = len;
159927d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1600357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1601357b40a1SHerbert Xu 			return 0;
1602357b40a1SHerbert Xu 		offset += copy;
1603357b40a1SHerbert Xu 		from += copy;
1604357b40a1SHerbert Xu 	}
1605357b40a1SHerbert Xu 
1606357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1607357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
16081a028e50SDavid S. Miller 		int end;
1609357b40a1SHerbert Xu 
1610547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16111a028e50SDavid S. Miller 
16121a028e50SDavid S. Miller 		end = start + frag->size;
1613357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1614357b40a1SHerbert Xu 			u8 *vaddr;
1615357b40a1SHerbert Xu 
1616357b40a1SHerbert Xu 			if (copy > len)
1617357b40a1SHerbert Xu 				copy = len;
1618357b40a1SHerbert Xu 
1619357b40a1SHerbert Xu 			vaddr = kmap_skb_frag(frag);
16201a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
16211a028e50SDavid S. Miller 			       from, copy);
1622357b40a1SHerbert Xu 			kunmap_skb_frag(vaddr);
1623357b40a1SHerbert Xu 
1624357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1625357b40a1SHerbert Xu 				return 0;
1626357b40a1SHerbert Xu 			offset += copy;
1627357b40a1SHerbert Xu 			from += copy;
1628357b40a1SHerbert Xu 		}
16291a028e50SDavid S. Miller 		start = end;
1630357b40a1SHerbert Xu 	}
1631357b40a1SHerbert Xu 
1632357b40a1SHerbert Xu 	if (skb_shinfo(skb)->frag_list) {
1633357b40a1SHerbert Xu 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1634357b40a1SHerbert Xu 
1635357b40a1SHerbert Xu 		for (; list; list = list->next) {
16361a028e50SDavid S. Miller 			int end;
1637357b40a1SHerbert Xu 
1638547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
16391a028e50SDavid S. Miller 
16401a028e50SDavid S. Miller 			end = start + list->len;
1641357b40a1SHerbert Xu 			if ((copy = end - offset) > 0) {
1642357b40a1SHerbert Xu 				if (copy > len)
1643357b40a1SHerbert Xu 					copy = len;
16441a028e50SDavid S. Miller 				if (skb_store_bits(list, offset - start,
16451a028e50SDavid S. Miller 						   from, copy))
1646357b40a1SHerbert Xu 					goto fault;
1647357b40a1SHerbert Xu 				if ((len -= copy) == 0)
1648357b40a1SHerbert Xu 					return 0;
1649357b40a1SHerbert Xu 				offset += copy;
1650357b40a1SHerbert Xu 				from += copy;
1651357b40a1SHerbert Xu 			}
16521a028e50SDavid S. Miller 			start = end;
1653357b40a1SHerbert Xu 		}
1654357b40a1SHerbert Xu 	}
1655357b40a1SHerbert Xu 	if (!len)
1656357b40a1SHerbert Xu 		return 0;
1657357b40a1SHerbert Xu 
1658357b40a1SHerbert Xu fault:
1659357b40a1SHerbert Xu 	return -EFAULT;
1660357b40a1SHerbert Xu }
1661357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1662357b40a1SHerbert Xu 
16631da177e4SLinus Torvalds /* Checksum skb data. */
16641da177e4SLinus Torvalds 
16652bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset,
16662bbbc868SAl Viro 			  int len, __wsum csum)
16671da177e4SLinus Torvalds {
16681a028e50SDavid S. Miller 	int start = skb_headlen(skb);
16691a028e50SDavid S. Miller 	int i, copy = start - offset;
16701da177e4SLinus Torvalds 	int pos = 0;
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds 	/* Checksum header. */
16731da177e4SLinus Torvalds 	if (copy > 0) {
16741da177e4SLinus Torvalds 		if (copy > len)
16751da177e4SLinus Torvalds 			copy = len;
16761da177e4SLinus Torvalds 		csum = csum_partial(skb->data + offset, copy, csum);
16771da177e4SLinus Torvalds 		if ((len -= copy) == 0)
16781da177e4SLinus Torvalds 			return csum;
16791da177e4SLinus Torvalds 		offset += copy;
16801da177e4SLinus Torvalds 		pos	= copy;
16811da177e4SLinus Torvalds 	}
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16841a028e50SDavid S. Miller 		int end;
16851da177e4SLinus Torvalds 
1686547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16871a028e50SDavid S. Miller 
16881a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
16891da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
169044bb9363SAl Viro 			__wsum csum2;
16911da177e4SLinus Torvalds 			u8 *vaddr;
16921da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 			if (copy > len)
16951da177e4SLinus Torvalds 				copy = len;
16961da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
16971a028e50SDavid S. Miller 			csum2 = csum_partial(vaddr + frag->page_offset +
16981a028e50SDavid S. Miller 					     offset - start, copy, 0);
16991da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
17001da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
17011da177e4SLinus Torvalds 			if (!(len -= copy))
17021da177e4SLinus Torvalds 				return csum;
17031da177e4SLinus Torvalds 			offset += copy;
17041da177e4SLinus Torvalds 			pos    += copy;
17051da177e4SLinus Torvalds 		}
17061a028e50SDavid S. Miller 		start = end;
17071da177e4SLinus Torvalds 	}
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
17101da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 		for (; list; list = list->next) {
17131a028e50SDavid S. Miller 			int end;
17141da177e4SLinus Torvalds 
1715547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
17161a028e50SDavid S. Miller 
17171a028e50SDavid S. Miller 			end = start + list->len;
17181da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
17195f92a738SAl Viro 				__wsum csum2;
17201da177e4SLinus Torvalds 				if (copy > len)
17211da177e4SLinus Torvalds 					copy = len;
17221a028e50SDavid S. Miller 				csum2 = skb_checksum(list, offset - start,
17231a028e50SDavid S. Miller 						     copy, 0);
17241da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
17251da177e4SLinus Torvalds 				if ((len -= copy) == 0)
17261da177e4SLinus Torvalds 					return csum;
17271da177e4SLinus Torvalds 				offset += copy;
17281da177e4SLinus Torvalds 				pos    += copy;
17291da177e4SLinus Torvalds 			}
17301a028e50SDavid S. Miller 			start = end;
17311da177e4SLinus Torvalds 		}
17321da177e4SLinus Torvalds 	}
173309a62660SKris Katterjohn 	BUG_ON(len);
17341da177e4SLinus Torvalds 
17351da177e4SLinus Torvalds 	return csum;
17361da177e4SLinus Torvalds }
1737b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds /* Both of above in one bottle. */
17401da177e4SLinus Torvalds 
174181d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
174281d77662SAl Viro 				    u8 *to, int len, __wsum csum)
17431da177e4SLinus Torvalds {
17441a028e50SDavid S. Miller 	int start = skb_headlen(skb);
17451a028e50SDavid S. Miller 	int i, copy = start - offset;
17461da177e4SLinus Torvalds 	int pos = 0;
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds 	/* Copy header. */
17491da177e4SLinus Torvalds 	if (copy > 0) {
17501da177e4SLinus Torvalds 		if (copy > len)
17511da177e4SLinus Torvalds 			copy = len;
17521da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
17531da177e4SLinus Torvalds 						 copy, csum);
17541da177e4SLinus Torvalds 		if ((len -= copy) == 0)
17551da177e4SLinus Torvalds 			return csum;
17561da177e4SLinus Torvalds 		offset += copy;
17571da177e4SLinus Torvalds 		to     += copy;
17581da177e4SLinus Torvalds 		pos	= copy;
17591da177e4SLinus Torvalds 	}
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
17621a028e50SDavid S. Miller 		int end;
17631da177e4SLinus Torvalds 
1764547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17651a028e50SDavid S. Miller 
17661a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
17671da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17685084205fSAl Viro 			__wsum csum2;
17691da177e4SLinus Torvalds 			u8 *vaddr;
17701da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 			if (copy > len)
17731da177e4SLinus Torvalds 				copy = len;
17741da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
17751da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
17761a028e50SDavid S. Miller 							  frag->page_offset +
17771a028e50SDavid S. Miller 							  offset - start, to,
17781a028e50SDavid S. Miller 							  copy, 0);
17791da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
17801da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
17811da177e4SLinus Torvalds 			if (!(len -= copy))
17821da177e4SLinus Torvalds 				return csum;
17831da177e4SLinus Torvalds 			offset += copy;
17841da177e4SLinus Torvalds 			to     += copy;
17851da177e4SLinus Torvalds 			pos    += copy;
17861da177e4SLinus Torvalds 		}
17871a028e50SDavid S. Miller 		start = end;
17881da177e4SLinus Torvalds 	}
17891da177e4SLinus Torvalds 
17901da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
17911da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 		for (; list; list = list->next) {
179481d77662SAl Viro 			__wsum csum2;
17951a028e50SDavid S. Miller 			int end;
17961da177e4SLinus Torvalds 
1797547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
17981a028e50SDavid S. Miller 
17991a028e50SDavid S. Miller 			end = start + list->len;
18001da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
18011da177e4SLinus Torvalds 				if (copy > len)
18021da177e4SLinus Torvalds 					copy = len;
18031a028e50SDavid S. Miller 				csum2 = skb_copy_and_csum_bits(list,
18041a028e50SDavid S. Miller 							       offset - start,
18051da177e4SLinus Torvalds 							       to, copy, 0);
18061da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
18071da177e4SLinus Torvalds 				if ((len -= copy) == 0)
18081da177e4SLinus Torvalds 					return csum;
18091da177e4SLinus Torvalds 				offset += copy;
18101da177e4SLinus Torvalds 				to     += copy;
18111da177e4SLinus Torvalds 				pos    += copy;
18121da177e4SLinus Torvalds 			}
18131a028e50SDavid S. Miller 			start = end;
18141da177e4SLinus Torvalds 		}
18151da177e4SLinus Torvalds 	}
181609a62660SKris Katterjohn 	BUG_ON(len);
18171da177e4SLinus Torvalds 	return csum;
18181da177e4SLinus Torvalds }
1819b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
18221da177e4SLinus Torvalds {
1823d3bc23e7SAl Viro 	__wsum csum;
18241da177e4SLinus Torvalds 	long csstart;
18251da177e4SLinus Torvalds 
182684fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1827663ead3bSHerbert Xu 		csstart = skb->csum_start - skb_headroom(skb);
18281da177e4SLinus Torvalds 	else
18291da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
18301da177e4SLinus Torvalds 
183109a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
18321da177e4SLinus Torvalds 
1833d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	csum = 0;
18361da177e4SLinus Torvalds 	if (csstart != skb->len)
18371da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
18381da177e4SLinus Torvalds 					      skb->len - csstart, 0);
18391da177e4SLinus Torvalds 
184084fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1841ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
18421da177e4SLinus Torvalds 
1843d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
18441da177e4SLinus Torvalds 	}
18451da177e4SLinus Torvalds }
1846b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds /**
18491da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
18501da177e4SLinus Torvalds  *	@list: list to dequeue from
18511da177e4SLinus Torvalds  *
18521da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
18531da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
18541da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
18551da177e4SLinus Torvalds  */
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
18581da177e4SLinus Torvalds {
18591da177e4SLinus Torvalds 	unsigned long flags;
18601da177e4SLinus Torvalds 	struct sk_buff *result;
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18631da177e4SLinus Torvalds 	result = __skb_dequeue(list);
18641da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18651da177e4SLinus Torvalds 	return result;
18661da177e4SLinus Torvalds }
1867b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds /**
18701da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
18711da177e4SLinus Torvalds  *	@list: list to dequeue from
18721da177e4SLinus Torvalds  *
18731da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
18741da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
18751da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
18761da177e4SLinus Torvalds  */
18771da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
18781da177e4SLinus Torvalds {
18791da177e4SLinus Torvalds 	unsigned long flags;
18801da177e4SLinus Torvalds 	struct sk_buff *result;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18831da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
18841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18851da177e4SLinus Torvalds 	return result;
18861da177e4SLinus Torvalds }
1887b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds /**
18901da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
18911da177e4SLinus Torvalds  *	@list: list to empty
18921da177e4SLinus Torvalds  *
18931da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
18941da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
18951da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
18961da177e4SLinus Torvalds  */
18971da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
18981da177e4SLinus Torvalds {
18991da177e4SLinus Torvalds 	struct sk_buff *skb;
19001da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
19011da177e4SLinus Torvalds 		kfree_skb(skb);
19021da177e4SLinus Torvalds }
1903b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
19041da177e4SLinus Torvalds 
19051da177e4SLinus Torvalds /**
19061da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
19071da177e4SLinus Torvalds  *	@list: list to use
19081da177e4SLinus Torvalds  *	@newsk: buffer to queue
19091da177e4SLinus Torvalds  *
19101da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
19111da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
19121da177e4SLinus Torvalds  *	safely.
19131da177e4SLinus Torvalds  *
19141da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19151da177e4SLinus Torvalds  */
19161da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
19171da177e4SLinus Torvalds {
19181da177e4SLinus Torvalds 	unsigned long flags;
19191da177e4SLinus Torvalds 
19201da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
19211da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
19221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19231da177e4SLinus Torvalds }
1924b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds /**
19271da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
19281da177e4SLinus Torvalds  *	@list: list to use
19291da177e4SLinus Torvalds  *	@newsk: buffer to queue
19301da177e4SLinus Torvalds  *
19311da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
19321da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
19331da177e4SLinus Torvalds  *	safely.
19341da177e4SLinus Torvalds  *
19351da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19361da177e4SLinus Torvalds  */
19371da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
19381da177e4SLinus Torvalds {
19391da177e4SLinus Torvalds 	unsigned long flags;
19401da177e4SLinus Torvalds 
19411da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
19421da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
19431da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19441da177e4SLinus Torvalds }
1945b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
19468728b834SDavid S. Miller 
19471da177e4SLinus Torvalds /**
19481da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
19491da177e4SLinus Torvalds  *	@skb: buffer to remove
19508728b834SDavid S. Miller  *	@list: list to use
19511da177e4SLinus Torvalds  *
19528728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
19538728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
19541da177e4SLinus Torvalds  *
19558728b834SDavid S. Miller  *	You must know what list the SKB is on.
19561da177e4SLinus Torvalds  */
19578728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
19581da177e4SLinus Torvalds {
19591da177e4SLinus Torvalds 	unsigned long flags;
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
19628728b834SDavid S. Miller 	__skb_unlink(skb, list);
19631da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19641da177e4SLinus Torvalds }
1965b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds /**
19681da177e4SLinus Torvalds  *	skb_append	-	append a buffer
19691da177e4SLinus Torvalds  *	@old: buffer to insert after
19701da177e4SLinus Torvalds  *	@newsk: buffer to insert
19718728b834SDavid S. Miller  *	@list: list to use
19721da177e4SLinus Torvalds  *
19731da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
19741da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
19751da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19761da177e4SLinus Torvalds  */
19778728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
19781da177e4SLinus Torvalds {
19791da177e4SLinus Torvalds 	unsigned long flags;
19801da177e4SLinus Torvalds 
19818728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
19827de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
19838728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
19841da177e4SLinus Torvalds }
1985b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds /**
19881da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
19891da177e4SLinus Torvalds  *	@old: buffer to insert before
19901da177e4SLinus Torvalds  *	@newsk: buffer to insert
19918728b834SDavid S. Miller  *	@list: list to use
19921da177e4SLinus Torvalds  *
19938728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
19948728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
19958728b834SDavid S. Miller  *	calls.
19968728b834SDavid S. Miller  *
19971da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19981da177e4SLinus Torvalds  */
19998728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
20001da177e4SLinus Torvalds {
20011da177e4SLinus Torvalds 	unsigned long flags;
20021da177e4SLinus Torvalds 
20038728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
20048728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
20058728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
20061da177e4SLinus Torvalds }
2007b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
20081da177e4SLinus Torvalds 
20091da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
20101da177e4SLinus Torvalds 					   struct sk_buff* skb1,
20111da177e4SLinus Torvalds 					   const u32 len, const int pos)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	int i;
20141da177e4SLinus Torvalds 
2015d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2016d626f62bSArnaldo Carvalho de Melo 					 pos - len);
20171da177e4SLinus Torvalds 	/* And move data appendix as is. */
20181da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
20191da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
20221da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
20231da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
20241da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
20251da177e4SLinus Torvalds 	skb->data_len		   = 0;
20261da177e4SLinus Torvalds 	skb->len		   = len;
202727a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
20281da177e4SLinus Torvalds }
20291da177e4SLinus Torvalds 
20301da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
20311da177e4SLinus Torvalds 				       struct sk_buff* skb1,
20321da177e4SLinus Torvalds 				       const u32 len, int pos)
20331da177e4SLinus Torvalds {
20341da177e4SLinus Torvalds 	int i, k = 0;
20351da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
20381da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
20391da177e4SLinus Torvalds 	skb->len		  = len;
20401da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
20431da177e4SLinus Torvalds 		int size = skb_shinfo(skb)->frags[i].size;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 		if (pos + size > len) {
20461da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 			if (pos < len) {
20491da177e4SLinus Torvalds 				/* Split frag.
20501da177e4SLinus Torvalds 				 * We have two variants in this case:
20511da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
20521da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
20531da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
20541da177e4SLinus Torvalds 				 *    where splitting is expensive.
20551da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
20561da177e4SLinus Torvalds 				 */
20571da177e4SLinus Torvalds 				get_page(skb_shinfo(skb)->frags[i].page);
20581da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
20591da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].size -= len - pos;
20601da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[i].size	= len - pos;
20611da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
20621da177e4SLinus Torvalds 			}
20631da177e4SLinus Torvalds 			k++;
20641da177e4SLinus Torvalds 		} else
20651da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
20661da177e4SLinus Torvalds 		pos += size;
20671da177e4SLinus Torvalds 	}
20681da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds /**
20721da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
20731da177e4SLinus Torvalds  * @skb: the buffer to split
20741da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
20751da177e4SLinus Torvalds  * @len: new length for skb
20761da177e4SLinus Torvalds  */
20771da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
20781da177e4SLinus Torvalds {
20791da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
20821da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
20831da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
20841da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
20851da177e4SLinus Torvalds }
2086b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
20871da177e4SLinus Torvalds 
20889f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
20899f782db3SIlpo Järvinen  *
20909f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
20919f782db3SIlpo Järvinen  */
2092832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2093832d11c5SIlpo Järvinen {
20940ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2095832d11c5SIlpo Järvinen }
2096832d11c5SIlpo Järvinen 
2097832d11c5SIlpo Järvinen /**
2098832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2099832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2100832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2101832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2102832d11c5SIlpo Järvinen  *
2103832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
2104832d11c5SIlpo Järvinen  * the length of the skb, from tgt to skb. Returns number bytes shifted.
2105832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2106832d11c5SIlpo Järvinen  *
2107832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2108832d11c5SIlpo Järvinen  *
2109832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2110832d11c5SIlpo Järvinen  * to have non-paged data as well.
2111832d11c5SIlpo Järvinen  *
2112832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2113832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2114832d11c5SIlpo Järvinen  */
2115832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2116832d11c5SIlpo Järvinen {
2117832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2118832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2119832d11c5SIlpo Järvinen 
2120832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2121832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2122832d11c5SIlpo Järvinen 
2123832d11c5SIlpo Järvinen 	todo = shiftlen;
2124832d11c5SIlpo Järvinen 	from = 0;
2125832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2126832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2127832d11c5SIlpo Järvinen 
2128832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2129832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2130832d11c5SIlpo Järvinen 	 */
2131832d11c5SIlpo Järvinen 	if (!to ||
2132832d11c5SIlpo Järvinen 	    !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2133832d11c5SIlpo Järvinen 		merge = -1;
2134832d11c5SIlpo Järvinen 	} else {
2135832d11c5SIlpo Järvinen 		merge = to - 1;
2136832d11c5SIlpo Järvinen 
2137832d11c5SIlpo Järvinen 		todo -= fragfrom->size;
2138832d11c5SIlpo Järvinen 		if (todo < 0) {
2139832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2140832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2141832d11c5SIlpo Järvinen 				return 0;
2142832d11c5SIlpo Järvinen 
21439f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
21449f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2145832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2146832d11c5SIlpo Järvinen 
2147832d11c5SIlpo Järvinen 			fragto->size += shiftlen;
2148832d11c5SIlpo Järvinen 			fragfrom->size -= shiftlen;
2149832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2150832d11c5SIlpo Järvinen 
2151832d11c5SIlpo Järvinen 			goto onlymerged;
2152832d11c5SIlpo Järvinen 		}
2153832d11c5SIlpo Järvinen 
2154832d11c5SIlpo Järvinen 		from++;
2155832d11c5SIlpo Järvinen 	}
2156832d11c5SIlpo Järvinen 
2157832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2158832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2159832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2160832d11c5SIlpo Järvinen 		return 0;
2161832d11c5SIlpo Järvinen 
2162832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2163832d11c5SIlpo Järvinen 		return 0;
2164832d11c5SIlpo Järvinen 
2165832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2166832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2167832d11c5SIlpo Järvinen 			return 0;
2168832d11c5SIlpo Järvinen 
2169832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2170832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2171832d11c5SIlpo Järvinen 
2172832d11c5SIlpo Järvinen 		if (todo >= fragfrom->size) {
2173832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
2174832d11c5SIlpo Järvinen 			todo -= fragfrom->size;
2175832d11c5SIlpo Järvinen 			from++;
2176832d11c5SIlpo Järvinen 			to++;
2177832d11c5SIlpo Järvinen 
2178832d11c5SIlpo Järvinen 		} else {
2179832d11c5SIlpo Järvinen 			get_page(fragfrom->page);
2180832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2181832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
2182832d11c5SIlpo Järvinen 			fragto->size = todo;
2183832d11c5SIlpo Järvinen 
2184832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
2185832d11c5SIlpo Järvinen 			fragfrom->size -= todo;
2186832d11c5SIlpo Järvinen 			todo = 0;
2187832d11c5SIlpo Järvinen 
2188832d11c5SIlpo Järvinen 			to++;
2189832d11c5SIlpo Järvinen 			break;
2190832d11c5SIlpo Järvinen 		}
2191832d11c5SIlpo Järvinen 	}
2192832d11c5SIlpo Järvinen 
2193832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2194832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2195832d11c5SIlpo Järvinen 
2196832d11c5SIlpo Järvinen 	if (merge >= 0) {
2197832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2198832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2199832d11c5SIlpo Järvinen 
2200832d11c5SIlpo Järvinen 		fragto->size += fragfrom->size;
2201832d11c5SIlpo Järvinen 		put_page(fragfrom->page);
2202832d11c5SIlpo Järvinen 	}
2203832d11c5SIlpo Järvinen 
2204832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2205832d11c5SIlpo Järvinen 	to = 0;
2206832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2207832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2208832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2209832d11c5SIlpo Järvinen 
2210832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2211832d11c5SIlpo Järvinen 
2212832d11c5SIlpo Järvinen onlymerged:
2213832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2214832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2215832d11c5SIlpo Järvinen 	 */
2216832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2217832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2218832d11c5SIlpo Järvinen 
2219832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2220832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2221832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2222832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2223832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2224832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2225832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2226832d11c5SIlpo Järvinen 
2227832d11c5SIlpo Järvinen 	return shiftlen;
2228832d11c5SIlpo Järvinen }
2229832d11c5SIlpo Järvinen 
2230677e90edSThomas Graf /**
2231677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2232677e90edSThomas Graf  * @skb: the buffer to read
2233677e90edSThomas Graf  * @from: lower offset of data to be read
2234677e90edSThomas Graf  * @to: upper offset of data to be read
2235677e90edSThomas Graf  * @st: state variable
2236677e90edSThomas Graf  *
2237677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2238677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2239677e90edSThomas Graf  */
2240677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2241677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2242677e90edSThomas Graf {
2243677e90edSThomas Graf 	st->lower_offset = from;
2244677e90edSThomas Graf 	st->upper_offset = to;
2245677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2246677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2247677e90edSThomas Graf 	st->frag_data = NULL;
2248677e90edSThomas Graf }
2249b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2250677e90edSThomas Graf 
2251677e90edSThomas Graf /**
2252677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2253677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2254677e90edSThomas Graf  * @data: destination pointer for data to be returned
2255677e90edSThomas Graf  * @st: state variable
2256677e90edSThomas Graf  *
2257677e90edSThomas Graf  * Reads a block of skb data at &consumed relative to the
2258677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2259677e90edSThomas Graf  * the head of the data block to &data and returns the length
2260677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2261677e90edSThomas Graf  * offset has been reached.
2262677e90edSThomas Graf  *
2263677e90edSThomas Graf  * The caller is not required to consume all of the data
2264677e90edSThomas Graf  * returned, i.e. &consumed is typically set to the number
2265677e90edSThomas Graf  * of bytes already consumed and the next call to
2266677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2267677e90edSThomas Graf  *
2268bc2cda1eSRandy Dunlap  * Note 1: The size of each block of data returned can be arbitary,
2269677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
2270677e90edSThomas Graf  *       reads of potentially non linear data.
2271677e90edSThomas Graf  *
2272bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2273677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2274677e90edSThomas Graf  *       a stack for this purpose.
2275677e90edSThomas Graf  */
2276677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2277677e90edSThomas Graf 			  struct skb_seq_state *st)
2278677e90edSThomas Graf {
2279677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2280677e90edSThomas Graf 	skb_frag_t *frag;
2281677e90edSThomas Graf 
2282677e90edSThomas Graf 	if (unlikely(abs_offset >= st->upper_offset))
2283677e90edSThomas Graf 		return 0;
2284677e90edSThomas Graf 
2285677e90edSThomas Graf next_skb:
228695e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2287677e90edSThomas Graf 
2288677e90edSThomas Graf 	if (abs_offset < block_limit) {
228995e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2290677e90edSThomas Graf 		return block_limit - abs_offset;
2291677e90edSThomas Graf 	}
2292677e90edSThomas Graf 
2293677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2294677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2295677e90edSThomas Graf 
2296677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2297677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2298677e90edSThomas Graf 		block_limit = frag->size + st->stepped_offset;
2299677e90edSThomas Graf 
2300677e90edSThomas Graf 		if (abs_offset < block_limit) {
2301677e90edSThomas Graf 			if (!st->frag_data)
2302677e90edSThomas Graf 				st->frag_data = kmap_skb_frag(frag);
2303677e90edSThomas Graf 
2304677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2305677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2306677e90edSThomas Graf 
2307677e90edSThomas Graf 			return block_limit - abs_offset;
2308677e90edSThomas Graf 		}
2309677e90edSThomas Graf 
2310677e90edSThomas Graf 		if (st->frag_data) {
2311677e90edSThomas Graf 			kunmap_skb_frag(st->frag_data);
2312677e90edSThomas Graf 			st->frag_data = NULL;
2313677e90edSThomas Graf 		}
2314677e90edSThomas Graf 
2315677e90edSThomas Graf 		st->frag_idx++;
2316677e90edSThomas Graf 		st->stepped_offset += frag->size;
2317677e90edSThomas Graf 	}
2318677e90edSThomas Graf 
23195b5a60daSOlaf Kirch 	if (st->frag_data) {
23205b5a60daSOlaf Kirch 		kunmap_skb_frag(st->frag_data);
23215b5a60daSOlaf Kirch 		st->frag_data = NULL;
23225b5a60daSOlaf Kirch 	}
23235b5a60daSOlaf Kirch 
232471b3346dSShyam Iyer 	if (st->root_skb == st->cur_skb &&
2325677e90edSThomas Graf 	    skb_shinfo(st->root_skb)->frag_list) {
2326677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
232795e3b24cSHerbert Xu 		st->frag_idx = 0;
2328677e90edSThomas Graf 		goto next_skb;
232971b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
233071b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
233171b3346dSShyam Iyer 		st->frag_idx = 0;
2332677e90edSThomas Graf 		goto next_skb;
2333677e90edSThomas Graf 	}
2334677e90edSThomas Graf 
2335677e90edSThomas Graf 	return 0;
2336677e90edSThomas Graf }
2337b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2338677e90edSThomas Graf 
2339677e90edSThomas Graf /**
2340677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2341677e90edSThomas Graf  * @st: state variable
2342677e90edSThomas Graf  *
2343677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2344677e90edSThomas Graf  * returned 0.
2345677e90edSThomas Graf  */
2346677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2347677e90edSThomas Graf {
2348677e90edSThomas Graf 	if (st->frag_data)
2349677e90edSThomas Graf 		kunmap_skb_frag(st->frag_data);
2350677e90edSThomas Graf }
2351b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2352677e90edSThomas Graf 
23533fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
23543fc7e8a6SThomas Graf 
23553fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
23563fc7e8a6SThomas Graf 					  struct ts_config *conf,
23573fc7e8a6SThomas Graf 					  struct ts_state *state)
23583fc7e8a6SThomas Graf {
23593fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
23603fc7e8a6SThomas Graf }
23613fc7e8a6SThomas Graf 
23623fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
23633fc7e8a6SThomas Graf {
23643fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
23653fc7e8a6SThomas Graf }
23663fc7e8a6SThomas Graf 
23673fc7e8a6SThomas Graf /**
23683fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
23693fc7e8a6SThomas Graf  * @skb: the buffer to look in
23703fc7e8a6SThomas Graf  * @from: search offset
23713fc7e8a6SThomas Graf  * @to: search limit
23723fc7e8a6SThomas Graf  * @config: textsearch configuration
23733fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
23743fc7e8a6SThomas Graf  *
23753fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
23763fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
23773fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
23783fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
23793fc7e8a6SThomas Graf  */
23803fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
23813fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
23823fc7e8a6SThomas Graf 			   struct ts_state *state)
23833fc7e8a6SThomas Graf {
2384f72b948dSPhil Oester 	unsigned int ret;
2385f72b948dSPhil Oester 
23863fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
23873fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
23883fc7e8a6SThomas Graf 
23893fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
23903fc7e8a6SThomas Graf 
2391f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2392f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
23933fc7e8a6SThomas Graf }
2394b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
23953fc7e8a6SThomas Graf 
2396e89e9cf5SAnanda Raju /**
2397e89e9cf5SAnanda Raju  * skb_append_datato_frags: - append the user data to a skb
2398e89e9cf5SAnanda Raju  * @sk: sock  structure
2399e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
2400e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2401e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2402e89e9cf5SAnanda Raju  * @length: length of the iov message
2403e89e9cf5SAnanda Raju  *
2404e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2405e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2406e89e9cf5SAnanda Raju  */
2407e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2408dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2409e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2410e89e9cf5SAnanda Raju 			void *from, int length)
2411e89e9cf5SAnanda Raju {
2412e89e9cf5SAnanda Raju 	int frg_cnt = 0;
2413e89e9cf5SAnanda Raju 	skb_frag_t *frag = NULL;
2414e89e9cf5SAnanda Raju 	struct page *page = NULL;
2415e89e9cf5SAnanda Raju 	int copy, left;
2416e89e9cf5SAnanda Raju 	int offset = 0;
2417e89e9cf5SAnanda Raju 	int ret;
2418e89e9cf5SAnanda Raju 
2419e89e9cf5SAnanda Raju 	do {
2420e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2421e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2422e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2423e89e9cf5SAnanda Raju 			return -EFAULT;
2424e89e9cf5SAnanda Raju 
2425e89e9cf5SAnanda Raju 		/* allocate a new page for next frag */
2426e89e9cf5SAnanda Raju 		page = alloc_pages(sk->sk_allocation, 0);
2427e89e9cf5SAnanda Raju 
2428e89e9cf5SAnanda Raju 		/* If alloc_page fails just return failure and caller will
2429e89e9cf5SAnanda Raju 		 * free previous allocated pages by doing kfree_skb()
2430e89e9cf5SAnanda Raju 		 */
2431e89e9cf5SAnanda Raju 		if (page == NULL)
2432e89e9cf5SAnanda Raju 			return -ENOMEM;
2433e89e9cf5SAnanda Raju 
2434e89e9cf5SAnanda Raju 		/* initialize the next frag */
2435e89e9cf5SAnanda Raju 		sk->sk_sndmsg_page = page;
2436e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off = 0;
2437e89e9cf5SAnanda Raju 		skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2438e89e9cf5SAnanda Raju 		skb->truesize += PAGE_SIZE;
2439e89e9cf5SAnanda Raju 		atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2440e89e9cf5SAnanda Raju 
2441e89e9cf5SAnanda Raju 		/* get the new initialized frag */
2442e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2443e89e9cf5SAnanda Raju 		frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2444e89e9cf5SAnanda Raju 
2445e89e9cf5SAnanda Raju 		/* copy the user data to page */
2446e89e9cf5SAnanda Raju 		left = PAGE_SIZE - frag->page_offset;
2447e89e9cf5SAnanda Raju 		copy = (length > left)? left : length;
2448e89e9cf5SAnanda Raju 
2449e89e9cf5SAnanda Raju 		ret = getfrag(from, (page_address(frag->page) +
2450e89e9cf5SAnanda Raju 			    frag->page_offset + frag->size),
2451e89e9cf5SAnanda Raju 			    offset, copy, 0, skb);
2452e89e9cf5SAnanda Raju 		if (ret < 0)
2453e89e9cf5SAnanda Raju 			return -EFAULT;
2454e89e9cf5SAnanda Raju 
2455e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2456e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off += copy;
2457e89e9cf5SAnanda Raju 		frag->size += copy;
2458e89e9cf5SAnanda Raju 		skb->len += copy;
2459e89e9cf5SAnanda Raju 		skb->data_len += copy;
2460e89e9cf5SAnanda Raju 		offset += copy;
2461e89e9cf5SAnanda Raju 		length -= copy;
2462e89e9cf5SAnanda Raju 
2463e89e9cf5SAnanda Raju 	} while (length > 0);
2464e89e9cf5SAnanda Raju 
2465e89e9cf5SAnanda Raju 	return 0;
2466e89e9cf5SAnanda Raju }
2467b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
2468e89e9cf5SAnanda Raju 
2469cbb042f9SHerbert Xu /**
2470cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2471cbb042f9SHerbert Xu  *	@skb: buffer to update
2472cbb042f9SHerbert Xu  *	@len: length of data pulled
2473cbb042f9SHerbert Xu  *
2474cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2475fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
247684fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
247784fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
247884fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2479cbb042f9SHerbert Xu  */
2480cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2481cbb042f9SHerbert Xu {
2482cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2483cbb042f9SHerbert Xu 	skb->len -= len;
2484cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2485cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2486cbb042f9SHerbert Xu 	return skb->data += len;
2487cbb042f9SHerbert Xu }
2488cbb042f9SHerbert Xu 
2489f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2490f94691acSArnaldo Carvalho de Melo 
2491f4c50d99SHerbert Xu /**
2492f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2493f4c50d99SHerbert Xu  *	@skb: buffer to segment
2494576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2495f4c50d99SHerbert Xu  *
2496f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
24974c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
24984c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2499f4c50d99SHerbert Xu  */
2500576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2501f4c50d99SHerbert Xu {
2502f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2503f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
250489319d38SHerbert Xu 	struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2505f4c50d99SHerbert Xu 	unsigned int mss = skb_shinfo(skb)->gso_size;
250698e399f8SArnaldo Carvalho de Melo 	unsigned int doffset = skb->data - skb_mac_header(skb);
2507f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2508f4c50d99SHerbert Xu 	unsigned int headroom;
2509f4c50d99SHerbert Xu 	unsigned int len;
2510576a30ebSHerbert Xu 	int sg = features & NETIF_F_SG;
2511f4c50d99SHerbert Xu 	int nfrags = skb_shinfo(skb)->nr_frags;
2512f4c50d99SHerbert Xu 	int err = -ENOMEM;
2513f4c50d99SHerbert Xu 	int i = 0;
2514f4c50d99SHerbert Xu 	int pos;
2515f4c50d99SHerbert Xu 
2516f4c50d99SHerbert Xu 	__skb_push(skb, doffset);
2517f4c50d99SHerbert Xu 	headroom = skb_headroom(skb);
2518f4c50d99SHerbert Xu 	pos = skb_headlen(skb);
2519f4c50d99SHerbert Xu 
2520f4c50d99SHerbert Xu 	do {
2521f4c50d99SHerbert Xu 		struct sk_buff *nskb;
2522f4c50d99SHerbert Xu 		skb_frag_t *frag;
2523c8884eddSHerbert Xu 		int hsize;
2524f4c50d99SHerbert Xu 		int size;
2525f4c50d99SHerbert Xu 
2526f4c50d99SHerbert Xu 		len = skb->len - offset;
2527f4c50d99SHerbert Xu 		if (len > mss)
2528f4c50d99SHerbert Xu 			len = mss;
2529f4c50d99SHerbert Xu 
2530f4c50d99SHerbert Xu 		hsize = skb_headlen(skb) - offset;
2531f4c50d99SHerbert Xu 		if (hsize < 0)
2532f4c50d99SHerbert Xu 			hsize = 0;
2533c8884eddSHerbert Xu 		if (hsize > len || !sg)
2534c8884eddSHerbert Xu 			hsize = len;
2535f4c50d99SHerbert Xu 
253689319d38SHerbert Xu 		if (!hsize && i >= nfrags) {
253789319d38SHerbert Xu 			BUG_ON(fskb->len != len);
253889319d38SHerbert Xu 
253989319d38SHerbert Xu 			pos += len;
254089319d38SHerbert Xu 			nskb = skb_clone(fskb, GFP_ATOMIC);
254189319d38SHerbert Xu 			fskb = fskb->next;
254289319d38SHerbert Xu 
2543f4c50d99SHerbert Xu 			if (unlikely(!nskb))
2544f4c50d99SHerbert Xu 				goto err;
2545f4c50d99SHerbert Xu 
254689319d38SHerbert Xu 			hsize = skb_end_pointer(nskb) - nskb->head;
254789319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
254889319d38SHerbert Xu 				kfree_skb(nskb);
254989319d38SHerbert Xu 				goto err;
255089319d38SHerbert Xu 			}
255189319d38SHerbert Xu 
255289319d38SHerbert Xu 			nskb->truesize += skb_end_pointer(nskb) - nskb->head -
255389319d38SHerbert Xu 					  hsize;
255489319d38SHerbert Xu 			skb_release_head_state(nskb);
255589319d38SHerbert Xu 			__skb_push(nskb, doffset);
255689319d38SHerbert Xu 		} else {
255789319d38SHerbert Xu 			nskb = alloc_skb(hsize + doffset + headroom,
255889319d38SHerbert Xu 					 GFP_ATOMIC);
255989319d38SHerbert Xu 
256089319d38SHerbert Xu 			if (unlikely(!nskb))
256189319d38SHerbert Xu 				goto err;
256289319d38SHerbert Xu 
256389319d38SHerbert Xu 			skb_reserve(nskb, headroom);
256489319d38SHerbert Xu 			__skb_put(nskb, doffset);
256589319d38SHerbert Xu 		}
256689319d38SHerbert Xu 
2567f4c50d99SHerbert Xu 		if (segs)
2568f4c50d99SHerbert Xu 			tail->next = nskb;
2569f4c50d99SHerbert Xu 		else
2570f4c50d99SHerbert Xu 			segs = nskb;
2571f4c50d99SHerbert Xu 		tail = nskb;
2572f4c50d99SHerbert Xu 
25736f85a124SHerbert Xu 		__copy_skb_header(nskb, skb);
2574f4c50d99SHerbert Xu 		nskb->mac_len = skb->mac_len;
2575f4c50d99SHerbert Xu 
2576459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(nskb);
2577ddc7b8e3SArnaldo Carvalho de Melo 		skb_set_network_header(nskb, skb->mac_len);
2578b0e380b1SArnaldo Carvalho de Melo 		nskb->transport_header = (nskb->network_header +
2579b0e380b1SArnaldo Carvalho de Melo 					  skb_network_header_len(skb));
258089319d38SHerbert Xu 		skb_copy_from_linear_data(skb, nskb->data, doffset);
258189319d38SHerbert Xu 
258289319d38SHerbert Xu 		if (pos >= offset + len)
258389319d38SHerbert Xu 			continue;
258489319d38SHerbert Xu 
2585f4c50d99SHerbert Xu 		if (!sg) {
25866f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
2587f4c50d99SHerbert Xu 			nskb->csum = skb_copy_and_csum_bits(skb, offset,
2588f4c50d99SHerbert Xu 							    skb_put(nskb, len),
2589f4c50d99SHerbert Xu 							    len, 0);
2590f4c50d99SHerbert Xu 			continue;
2591f4c50d99SHerbert Xu 		}
2592f4c50d99SHerbert Xu 
2593f4c50d99SHerbert Xu 		frag = skb_shinfo(nskb)->frags;
2594f4c50d99SHerbert Xu 
2595d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset,
2596d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
2597f4c50d99SHerbert Xu 
259889319d38SHerbert Xu 		while (pos < offset + len && i < nfrags) {
2599f4c50d99SHerbert Xu 			*frag = skb_shinfo(skb)->frags[i];
2600f4c50d99SHerbert Xu 			get_page(frag->page);
2601f4c50d99SHerbert Xu 			size = frag->size;
2602f4c50d99SHerbert Xu 
2603f4c50d99SHerbert Xu 			if (pos < offset) {
2604f4c50d99SHerbert Xu 				frag->page_offset += offset - pos;
2605f4c50d99SHerbert Xu 				frag->size -= offset - pos;
2606f4c50d99SHerbert Xu 			}
2607f4c50d99SHerbert Xu 
260889319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
2609f4c50d99SHerbert Xu 
2610f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
2611f4c50d99SHerbert Xu 				i++;
2612f4c50d99SHerbert Xu 				pos += size;
2613f4c50d99SHerbert Xu 			} else {
2614f4c50d99SHerbert Xu 				frag->size -= pos + size - (offset + len);
261589319d38SHerbert Xu 				goto skip_fraglist;
2616f4c50d99SHerbert Xu 			}
2617f4c50d99SHerbert Xu 
2618f4c50d99SHerbert Xu 			frag++;
2619f4c50d99SHerbert Xu 		}
2620f4c50d99SHerbert Xu 
262189319d38SHerbert Xu 		if (pos < offset + len) {
262289319d38SHerbert Xu 			struct sk_buff *fskb2 = fskb;
262389319d38SHerbert Xu 
262489319d38SHerbert Xu 			BUG_ON(pos + fskb->len != offset + len);
262589319d38SHerbert Xu 
262689319d38SHerbert Xu 			pos += fskb->len;
262789319d38SHerbert Xu 			fskb = fskb->next;
262889319d38SHerbert Xu 
262989319d38SHerbert Xu 			if (fskb2->next) {
263089319d38SHerbert Xu 				fskb2 = skb_clone(fskb2, GFP_ATOMIC);
263189319d38SHerbert Xu 				if (!fskb2)
263289319d38SHerbert Xu 					goto err;
263389319d38SHerbert Xu 			} else
263489319d38SHerbert Xu 				skb_get(fskb2);
263589319d38SHerbert Xu 
263689319d38SHerbert Xu 			BUG_ON(skb_shinfo(nskb)->frag_list);
263789319d38SHerbert Xu 			skb_shinfo(nskb)->frag_list = fskb2;
263889319d38SHerbert Xu 		}
263989319d38SHerbert Xu 
264089319d38SHerbert Xu skip_fraglist:
2641f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
2642f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
2643f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
2644f4c50d99SHerbert Xu 	} while ((offset += len) < skb->len);
2645f4c50d99SHerbert Xu 
2646f4c50d99SHerbert Xu 	return segs;
2647f4c50d99SHerbert Xu 
2648f4c50d99SHerbert Xu err:
2649f4c50d99SHerbert Xu 	while ((skb = segs)) {
2650f4c50d99SHerbert Xu 		segs = skb->next;
2651b08d5840SPatrick McHardy 		kfree_skb(skb);
2652f4c50d99SHerbert Xu 	}
2653f4c50d99SHerbert Xu 	return ERR_PTR(err);
2654f4c50d99SHerbert Xu }
2655f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
2656f4c50d99SHerbert Xu 
265771d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
265871d93b39SHerbert Xu {
265971d93b39SHerbert Xu 	struct sk_buff *p = *head;
266071d93b39SHerbert Xu 	struct sk_buff *nskb;
266171d93b39SHerbert Xu 	unsigned int headroom;
266286911732SHerbert Xu 	unsigned int len = skb_gro_len(skb);
266371d93b39SHerbert Xu 
266486911732SHerbert Xu 	if (p->len + len >= 65536)
266571d93b39SHerbert Xu 		return -E2BIG;
266671d93b39SHerbert Xu 
266771d93b39SHerbert Xu 	if (skb_shinfo(p)->frag_list)
266871d93b39SHerbert Xu 		goto merge;
266981705ad1SHerbert Xu 	else if (skb_headlen(skb) <= skb_gro_offset(skb)) {
267081705ad1SHerbert Xu 		if (skb_shinfo(p)->nr_frags + skb_shinfo(skb)->nr_frags >
267181705ad1SHerbert Xu 		    MAX_SKB_FRAGS)
267281705ad1SHerbert Xu 			return -E2BIG;
267381705ad1SHerbert Xu 
267486911732SHerbert Xu 		skb_shinfo(skb)->frags[0].page_offset +=
267586911732SHerbert Xu 			skb_gro_offset(skb) - skb_headlen(skb);
267686911732SHerbert Xu 		skb_shinfo(skb)->frags[0].size -=
267786911732SHerbert Xu 			skb_gro_offset(skb) - skb_headlen(skb);
267886911732SHerbert Xu 
26795d38a079SHerbert Xu 		memcpy(skb_shinfo(p)->frags + skb_shinfo(p)->nr_frags,
26805d38a079SHerbert Xu 		       skb_shinfo(skb)->frags,
26815d38a079SHerbert Xu 		       skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t));
26825d38a079SHerbert Xu 
26835d38a079SHerbert Xu 		skb_shinfo(p)->nr_frags += skb_shinfo(skb)->nr_frags;
2684f5572068SHerbert Xu 		skb_shinfo(skb)->nr_frags = 0;
2685f5572068SHerbert Xu 
2686f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
2687f5572068SHerbert Xu 		skb->len -= skb->data_len;
2688f5572068SHerbert Xu 		skb->data_len = 0;
2689f5572068SHerbert Xu 
26905d38a079SHerbert Xu 		NAPI_GRO_CB(skb)->free = 1;
26915d38a079SHerbert Xu 		goto done;
26925d38a079SHerbert Xu 	}
269371d93b39SHerbert Xu 
269471d93b39SHerbert Xu 	headroom = skb_headroom(p);
269586911732SHerbert Xu 	nskb = netdev_alloc_skb(p->dev, headroom + skb_gro_offset(p));
269671d93b39SHerbert Xu 	if (unlikely(!nskb))
269771d93b39SHerbert Xu 		return -ENOMEM;
269871d93b39SHerbert Xu 
269971d93b39SHerbert Xu 	__copy_skb_header(nskb, p);
270071d93b39SHerbert Xu 	nskb->mac_len = p->mac_len;
270171d93b39SHerbert Xu 
270271d93b39SHerbert Xu 	skb_reserve(nskb, headroom);
270386911732SHerbert Xu 	__skb_put(nskb, skb_gro_offset(p));
270471d93b39SHerbert Xu 
270586911732SHerbert Xu 	skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
270671d93b39SHerbert Xu 	skb_set_network_header(nskb, skb_network_offset(p));
270771d93b39SHerbert Xu 	skb_set_transport_header(nskb, skb_transport_offset(p));
270871d93b39SHerbert Xu 
270986911732SHerbert Xu 	__skb_pull(p, skb_gro_offset(p));
271086911732SHerbert Xu 	memcpy(skb_mac_header(nskb), skb_mac_header(p),
271186911732SHerbert Xu 	       p->data - skb_mac_header(p));
271271d93b39SHerbert Xu 
271371d93b39SHerbert Xu 	*NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
271471d93b39SHerbert Xu 	skb_shinfo(nskb)->frag_list = p;
2715b530256dSHerbert Xu 	skb_shinfo(nskb)->gso_size = skb_shinfo(p)->gso_size;
271671d93b39SHerbert Xu 	skb_header_release(p);
271771d93b39SHerbert Xu 	nskb->prev = p;
271871d93b39SHerbert Xu 
271971d93b39SHerbert Xu 	nskb->data_len += p->len;
272071d93b39SHerbert Xu 	nskb->truesize += p->len;
272171d93b39SHerbert Xu 	nskb->len += p->len;
272271d93b39SHerbert Xu 
272371d93b39SHerbert Xu 	*head = nskb;
272471d93b39SHerbert Xu 	nskb->next = p->next;
272571d93b39SHerbert Xu 	p->next = NULL;
272671d93b39SHerbert Xu 
272771d93b39SHerbert Xu 	p = nskb;
272871d93b39SHerbert Xu 
272971d93b39SHerbert Xu merge:
273056035022SHerbert Xu 	if (skb_gro_offset(skb) > skb_headlen(skb)) {
273156035022SHerbert Xu 		skb_shinfo(skb)->frags[0].page_offset +=
273256035022SHerbert Xu 			skb_gro_offset(skb) - skb_headlen(skb);
273356035022SHerbert Xu 		skb_shinfo(skb)->frags[0].size -=
273456035022SHerbert Xu 			skb_gro_offset(skb) - skb_headlen(skb);
273556035022SHerbert Xu 		skb_gro_reset_offset(skb);
273656035022SHerbert Xu 		skb_gro_pull(skb, skb_headlen(skb));
273756035022SHerbert Xu 	}
273856035022SHerbert Xu 
273956035022SHerbert Xu 	__skb_pull(skb, skb_gro_offset(skb));
274056035022SHerbert Xu 
274171d93b39SHerbert Xu 	p->prev->next = skb;
274271d93b39SHerbert Xu 	p->prev = skb;
274371d93b39SHerbert Xu 	skb_header_release(skb);
274471d93b39SHerbert Xu 
27455d38a079SHerbert Xu done:
27465d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
274737fe4732SHerbert Xu 	p->data_len += len;
274837fe4732SHerbert Xu 	p->truesize += len;
274937fe4732SHerbert Xu 	p->len += len;
275071d93b39SHerbert Xu 
275171d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
275271d93b39SHerbert Xu 	return 0;
275371d93b39SHerbert Xu }
275471d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive);
275571d93b39SHerbert Xu 
27561da177e4SLinus Torvalds void __init skb_init(void)
27571da177e4SLinus Torvalds {
27581da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
27591da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
27601da177e4SLinus Torvalds 					      0,
2761e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
276220c2df83SPaul Mundt 					      NULL);
2763d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2764d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
2765d179cd12SDavid S. Miller 						sizeof(atomic_t),
2766d179cd12SDavid S. Miller 						0,
2767e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
276820c2df83SPaul Mundt 						NULL);
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds 
2771716ea3a7SDavid Howells /**
2772716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2773716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
2774716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
2775716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
2776716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
2777716ea3a7SDavid Howells  *
2778716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
2779716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
2780716ea3a7SDavid Howells  */
278151c739d1SDavid S. Miller static int
278251c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2783716ea3a7SDavid Howells {
27841a028e50SDavid S. Miller 	int start = skb_headlen(skb);
27851a028e50SDavid S. Miller 	int i, copy = start - offset;
2786716ea3a7SDavid Howells 	int elt = 0;
2787716ea3a7SDavid Howells 
2788716ea3a7SDavid Howells 	if (copy > 0) {
2789716ea3a7SDavid Howells 		if (copy > len)
2790716ea3a7SDavid Howells 			copy = len;
2791642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
2792716ea3a7SDavid Howells 		elt++;
2793716ea3a7SDavid Howells 		if ((len -= copy) == 0)
2794716ea3a7SDavid Howells 			return elt;
2795716ea3a7SDavid Howells 		offset += copy;
2796716ea3a7SDavid Howells 	}
2797716ea3a7SDavid Howells 
2798716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
27991a028e50SDavid S. Miller 		int end;
2800716ea3a7SDavid Howells 
2801547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
28021a028e50SDavid S. Miller 
28031a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
2804716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2805716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2806716ea3a7SDavid Howells 
2807716ea3a7SDavid Howells 			if (copy > len)
2808716ea3a7SDavid Howells 				copy = len;
2809642f1490SJens Axboe 			sg_set_page(&sg[elt], frag->page, copy,
2810642f1490SJens Axboe 					frag->page_offset+offset-start);
2811716ea3a7SDavid Howells 			elt++;
2812716ea3a7SDavid Howells 			if (!(len -= copy))
2813716ea3a7SDavid Howells 				return elt;
2814716ea3a7SDavid Howells 			offset += copy;
2815716ea3a7SDavid Howells 		}
28161a028e50SDavid S. Miller 		start = end;
2817716ea3a7SDavid Howells 	}
2818716ea3a7SDavid Howells 
2819716ea3a7SDavid Howells 	if (skb_shinfo(skb)->frag_list) {
2820716ea3a7SDavid Howells 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
2821716ea3a7SDavid Howells 
2822716ea3a7SDavid Howells 		for (; list; list = list->next) {
28231a028e50SDavid S. Miller 			int end;
2824716ea3a7SDavid Howells 
2825547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
28261a028e50SDavid S. Miller 
28271a028e50SDavid S. Miller 			end = start + list->len;
2828716ea3a7SDavid Howells 			if ((copy = end - offset) > 0) {
2829716ea3a7SDavid Howells 				if (copy > len)
2830716ea3a7SDavid Howells 					copy = len;
283151c739d1SDavid S. Miller 				elt += __skb_to_sgvec(list, sg+elt, offset - start,
283251c739d1SDavid S. Miller 						      copy);
2833716ea3a7SDavid Howells 				if ((len -= copy) == 0)
2834716ea3a7SDavid Howells 					return elt;
2835716ea3a7SDavid Howells 				offset += copy;
2836716ea3a7SDavid Howells 			}
28371a028e50SDavid S. Miller 			start = end;
2838716ea3a7SDavid Howells 		}
2839716ea3a7SDavid Howells 	}
2840716ea3a7SDavid Howells 	BUG_ON(len);
2841716ea3a7SDavid Howells 	return elt;
2842716ea3a7SDavid Howells }
2843716ea3a7SDavid Howells 
284451c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
284551c739d1SDavid S. Miller {
284651c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
284751c739d1SDavid S. Miller 
2848c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
284951c739d1SDavid S. Miller 
285051c739d1SDavid S. Miller 	return nsg;
285151c739d1SDavid S. Miller }
2852b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
285351c739d1SDavid S. Miller 
2854716ea3a7SDavid Howells /**
2855716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
2856716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
2857716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
2858716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
2859716ea3a7SDavid Howells  *
2860716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
2861716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
2862716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
2863716ea3a7SDavid Howells  *
2864716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
2865716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
2866716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
2867716ea3a7SDavid Howells  *
2868716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
2869716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
2870716ea3a7SDavid Howells  */
2871716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2872716ea3a7SDavid Howells {
2873716ea3a7SDavid Howells 	int copyflag;
2874716ea3a7SDavid Howells 	int elt;
2875716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
2876716ea3a7SDavid Howells 
2877716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
2878716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
2879716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
2880716ea3a7SDavid Howells 	 */
2881716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2882716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2883716ea3a7SDavid Howells 		return -ENOMEM;
2884716ea3a7SDavid Howells 
2885716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
2886716ea3a7SDavid Howells 	if (!skb_shinfo(skb)->frag_list) {
2887716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
2888716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
2889716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
2890716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
2891716ea3a7SDavid Howells 
2892716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
2893716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2894716ea3a7SDavid Howells 			return -ENOMEM;
2895716ea3a7SDavid Howells 
2896716ea3a7SDavid Howells 		/* Voila! */
2897716ea3a7SDavid Howells 		*trailer = skb;
2898716ea3a7SDavid Howells 		return 1;
2899716ea3a7SDavid Howells 	}
2900716ea3a7SDavid Howells 
2901716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
2902716ea3a7SDavid Howells 
2903716ea3a7SDavid Howells 	elt = 1;
2904716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
2905716ea3a7SDavid Howells 	copyflag = 0;
2906716ea3a7SDavid Howells 
2907716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
2908716ea3a7SDavid Howells 		int ntail = 0;
2909716ea3a7SDavid Howells 
2910716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
2911716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
2912716ea3a7SDavid Howells 		 * after it. */
2913716ea3a7SDavid Howells 
2914716ea3a7SDavid Howells 		if (skb_shared(skb1))
2915716ea3a7SDavid Howells 			copyflag = 1;
2916716ea3a7SDavid Howells 
2917716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
2918716ea3a7SDavid Howells 
2919716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
2920716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
2921716ea3a7SDavid Howells 			    skb_shinfo(skb1)->frag_list ||
2922716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
2923716ea3a7SDavid Howells 				ntail = tailbits + 128;
2924716ea3a7SDavid Howells 		}
2925716ea3a7SDavid Howells 
2926716ea3a7SDavid Howells 		if (copyflag ||
2927716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
2928716ea3a7SDavid Howells 		    ntail ||
2929716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
2930716ea3a7SDavid Howells 		    skb_shinfo(skb1)->frag_list) {
2931716ea3a7SDavid Howells 			struct sk_buff *skb2;
2932716ea3a7SDavid Howells 
2933716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
2934716ea3a7SDavid Howells 			if (ntail == 0)
2935716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
2936716ea3a7SDavid Howells 			else
2937716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
2938716ea3a7SDavid Howells 						       skb_headroom(skb1),
2939716ea3a7SDavid Howells 						       ntail,
2940716ea3a7SDavid Howells 						       GFP_ATOMIC);
2941716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
2942716ea3a7SDavid Howells 				return -ENOMEM;
2943716ea3a7SDavid Howells 
2944716ea3a7SDavid Howells 			if (skb1->sk)
2945716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
2946716ea3a7SDavid Howells 
2947716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
2948716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
2949716ea3a7SDavid Howells 
2950716ea3a7SDavid Howells 			skb2->next = skb1->next;
2951716ea3a7SDavid Howells 			*skb_p = skb2;
2952716ea3a7SDavid Howells 			kfree_skb(skb1);
2953716ea3a7SDavid Howells 			skb1 = skb2;
2954716ea3a7SDavid Howells 		}
2955716ea3a7SDavid Howells 		elt++;
2956716ea3a7SDavid Howells 		*trailer = skb1;
2957716ea3a7SDavid Howells 		skb_p = &skb1->next;
2958716ea3a7SDavid Howells 	}
2959716ea3a7SDavid Howells 
2960716ea3a7SDavid Howells 	return elt;
2961716ea3a7SDavid Howells }
2962b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
2963716ea3a7SDavid Howells 
2964ac45f602SPatrick Ohly void skb_tstamp_tx(struct sk_buff *orig_skb,
2965ac45f602SPatrick Ohly 		struct skb_shared_hwtstamps *hwtstamps)
2966ac45f602SPatrick Ohly {
2967ac45f602SPatrick Ohly 	struct sock *sk = orig_skb->sk;
2968ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
2969ac45f602SPatrick Ohly 	struct sk_buff *skb;
2970ac45f602SPatrick Ohly 	int err;
2971ac45f602SPatrick Ohly 
2972ac45f602SPatrick Ohly 	if (!sk)
2973ac45f602SPatrick Ohly 		return;
2974ac45f602SPatrick Ohly 
2975ac45f602SPatrick Ohly 	skb = skb_clone(orig_skb, GFP_ATOMIC);
2976ac45f602SPatrick Ohly 	if (!skb)
2977ac45f602SPatrick Ohly 		return;
2978ac45f602SPatrick Ohly 
2979ac45f602SPatrick Ohly 	if (hwtstamps) {
2980ac45f602SPatrick Ohly 		*skb_hwtstamps(skb) =
2981ac45f602SPatrick Ohly 			*hwtstamps;
2982ac45f602SPatrick Ohly 	} else {
2983ac45f602SPatrick Ohly 		/*
2984ac45f602SPatrick Ohly 		 * no hardware time stamps available,
2985ac45f602SPatrick Ohly 		 * so keep the skb_shared_tx and only
2986ac45f602SPatrick Ohly 		 * store software time stamp
2987ac45f602SPatrick Ohly 		 */
2988ac45f602SPatrick Ohly 		skb->tstamp = ktime_get_real();
2989ac45f602SPatrick Ohly 	}
2990ac45f602SPatrick Ohly 
2991ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
2992ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
2993ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
2994ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
2995ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
2996ac45f602SPatrick Ohly 	if (err)
2997ac45f602SPatrick Ohly 		kfree_skb(skb);
2998ac45f602SPatrick Ohly }
2999ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3000ac45f602SPatrick Ohly 
3001ac45f602SPatrick Ohly 
3002f35d9d8aSRusty Russell /**
3003f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3004f35d9d8aSRusty Russell  * @skb: the skb to set
3005f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3006f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3007f35d9d8aSRusty Russell  *
3008f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3009f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3010f35d9d8aSRusty Russell  *
3011f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3012f35d9d8aSRusty Russell  * returns false you should drop the packet.
3013f35d9d8aSRusty Russell  */
3014f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3015f35d9d8aSRusty Russell {
3016f35d9d8aSRusty Russell 	if (unlikely(start > skb->len - 2) ||
3017f35d9d8aSRusty Russell 	    unlikely((int)start + off > skb->len - 2)) {
3018f35d9d8aSRusty Russell 		if (net_ratelimit())
3019f35d9d8aSRusty Russell 			printk(KERN_WARNING
3020f35d9d8aSRusty Russell 			       "bad partial csum: csum=%u/%u len=%u\n",
3021f35d9d8aSRusty Russell 			       start, off, skb->len);
3022f35d9d8aSRusty Russell 		return false;
3023f35d9d8aSRusty Russell 	}
3024f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3025f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3026f35d9d8aSRusty Russell 	skb->csum_offset = off;
3027f35d9d8aSRusty Russell 	return true;
3028f35d9d8aSRusty Russell }
3029b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3030f35d9d8aSRusty Russell 
30314497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
30324497b076SBen Hutchings {
30334497b076SBen Hutchings 	if (net_ratelimit())
30344497b076SBen Hutchings 		pr_warning("%s: received packets cannot be forwarded"
30354497b076SBen Hutchings 			   " while LRO is enabled\n", skb->dev->name);
30364497b076SBen Hutchings }
30374497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3038