xref: /openbmc/linux/net/core/skbuff.c (revision b530256d)
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>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <net/protocol.h>
601da177e4SLinus Torvalds #include <net/dst.h>
611da177e4SLinus Torvalds #include <net/sock.h>
621da177e4SLinus Torvalds #include <net/checksum.h>
631da177e4SLinus Torvalds #include <net/xfrm.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include <asm/uaccess.h>
661da177e4SLinus Torvalds #include <asm/system.h>
671da177e4SLinus Torvalds 
68a1f8e7f7SAl Viro #include "kmap_skb.h"
69a1f8e7f7SAl Viro 
70e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly;
71e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
721da177e4SLinus Torvalds 
739c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
749c55e01cSJens Axboe 				  struct pipe_buffer *buf)
759c55e01cSJens Axboe {
769c55e01cSJens Axboe 	struct sk_buff *skb = (struct sk_buff *) buf->private;
779c55e01cSJens Axboe 
789c55e01cSJens Axboe 	kfree_skb(skb);
799c55e01cSJens Axboe }
809c55e01cSJens Axboe 
819c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
829c55e01cSJens Axboe 				struct pipe_buffer *buf)
839c55e01cSJens Axboe {
849c55e01cSJens Axboe 	struct sk_buff *skb = (struct sk_buff *) buf->private;
859c55e01cSJens Axboe 
869c55e01cSJens Axboe 	skb_get(skb);
879c55e01cSJens Axboe }
889c55e01cSJens Axboe 
899c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
909c55e01cSJens Axboe 			       struct pipe_buffer *buf)
919c55e01cSJens Axboe {
929c55e01cSJens Axboe 	return 1;
939c55e01cSJens Axboe }
949c55e01cSJens Axboe 
959c55e01cSJens Axboe 
969c55e01cSJens Axboe /* Pipe buffer operations for a socket. */
979c55e01cSJens Axboe static struct pipe_buf_operations sock_pipe_buf_ops = {
989c55e01cSJens Axboe 	.can_merge = 0,
999c55e01cSJens Axboe 	.map = generic_pipe_buf_map,
1009c55e01cSJens Axboe 	.unmap = generic_pipe_buf_unmap,
1019c55e01cSJens Axboe 	.confirm = generic_pipe_buf_confirm,
1029c55e01cSJens Axboe 	.release = sock_pipe_buf_release,
1039c55e01cSJens Axboe 	.steal = sock_pipe_buf_steal,
1049c55e01cSJens Axboe 	.get = sock_pipe_buf_get,
1059c55e01cSJens Axboe };
1069c55e01cSJens Axboe 
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  *	Keep out-of-line to prevent kernel bloat.
1091da177e4SLinus Torvalds  *	__builtin_return_address is not used because it is not always
1101da177e4SLinus Torvalds  *	reliable.
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /**
1141da177e4SLinus Torvalds  *	skb_over_panic	- 	private function
1151da177e4SLinus Torvalds  *	@skb: buffer
1161da177e4SLinus Torvalds  *	@sz: size
1171da177e4SLinus Torvalds  *	@here: address
1181da177e4SLinus Torvalds  *
1191da177e4SLinus Torvalds  *	Out of line support code for skb_put(). Not user callable.
1201da177e4SLinus Torvalds  */
1211da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here)
1221da177e4SLinus Torvalds {
12326095455SPatrick McHardy 	printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
1244305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
12527a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1264305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
12726095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1281da177e4SLinus Torvalds 	BUG();
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /**
1321da177e4SLinus Torvalds  *	skb_under_panic	- 	private function
1331da177e4SLinus Torvalds  *	@skb: buffer
1341da177e4SLinus Torvalds  *	@sz: size
1351da177e4SLinus Torvalds  *	@here: address
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  *	Out of line support code for skb_push(). Not user callable.
1381da177e4SLinus Torvalds  */
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here)
1411da177e4SLinus Torvalds {
14226095455SPatrick McHardy 	printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
1434305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
14427a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1454305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
14626095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1471da177e4SLinus Torvalds 	BUG();
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
150dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb)
151dc6de336SDavid S. Miller {
1528f480c0eSArjan van de Ven 	WARN(net_ratelimit(), KERN_ERR "SKB BUG: Invalid truesize (%u) "
153dc6de336SDavid S. Miller 	       "len=%u, sizeof(sk_buff)=%Zd\n",
154dc6de336SDavid S. Miller 	       skb->truesize, skb->len, sizeof(struct sk_buff));
155dc6de336SDavid S. Miller }
156dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug);
157dc6de336SDavid S. Miller 
1581da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1591da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1601da177e4SLinus Torvalds  *	[BEEP] leaks.
1611da177e4SLinus Torvalds  *
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /**
165d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1661da177e4SLinus Torvalds  *	@size: size to allocate
1671da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
168c83c2486SRandy Dunlap  *	@fclone: allocate from fclone cache instead of head cache
169c83c2486SRandy Dunlap  *		and allocate a cloned (child) skb
170b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1711da177e4SLinus Torvalds  *
1721da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
1731da177e4SLinus Torvalds  *	tail room of size bytes. The object has a reference count of one.
1741da177e4SLinus Torvalds  *	The return is the buffer. On a failure the return is %NULL.
1751da177e4SLinus Torvalds  *
1761da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1771da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1781da177e4SLinus Torvalds  */
179dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
180b30973f8SChristoph Hellwig 			    int fclone, int node)
1811da177e4SLinus Torvalds {
182e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1834947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1841da177e4SLinus Torvalds 	struct sk_buff *skb;
1851da177e4SLinus Torvalds 	u8 *data;
1861da177e4SLinus Torvalds 
1878798b3fbSHerbert Xu 	cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
1888798b3fbSHerbert Xu 
1891da177e4SLinus Torvalds 	/* Get the HEAD */
190b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1911da177e4SLinus Torvalds 	if (!skb)
1921da177e4SLinus Torvalds 		goto out;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
195b30973f8SChristoph Hellwig 	data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
196b30973f8SChristoph Hellwig 			gfp_mask, node);
1971da177e4SLinus Torvalds 	if (!data)
1981da177e4SLinus Torvalds 		goto nodata;
1991da177e4SLinus Torvalds 
200ca0605a7SArnaldo Carvalho de Melo 	/*
201c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
202c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
203c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
204ca0605a7SArnaldo Carvalho de Melo 	 */
205ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
2061da177e4SLinus Torvalds 	skb->truesize = size + sizeof(struct sk_buff);
2071da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
2081da177e4SLinus Torvalds 	skb->head = data;
2091da177e4SLinus Torvalds 	skb->data = data;
21027a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2114305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
2124947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2134947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
2144947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
2154947d3efSBenjamin LaHaise 	shinfo->nr_frags  = 0;
2167967168cSHerbert Xu 	shinfo->gso_size = 0;
2177967168cSHerbert Xu 	shinfo->gso_segs = 0;
2187967168cSHerbert Xu 	shinfo->gso_type = 0;
2194947d3efSBenjamin LaHaise 	shinfo->ip6_frag_id = 0;
2204947d3efSBenjamin LaHaise 	shinfo->frag_list = NULL;
2214947d3efSBenjamin LaHaise 
222d179cd12SDavid S. Miller 	if (fclone) {
223d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
224d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
2251da177e4SLinus Torvalds 
226d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
227d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
228d179cd12SDavid S. Miller 
229d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
230d179cd12SDavid S. Miller 	}
2311da177e4SLinus Torvalds out:
2321da177e4SLinus Torvalds 	return skb;
2331da177e4SLinus Torvalds nodata:
2348798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2351da177e4SLinus Torvalds 	skb = NULL;
2361da177e4SLinus Torvalds 	goto out;
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds /**
2408af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
2418af27456SChristoph Hellwig  *	@dev: network device to receive on
2428af27456SChristoph Hellwig  *	@length: length to allocate
2438af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
2448af27456SChristoph Hellwig  *
2458af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
2468af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
2478af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
2488af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
2498af27456SChristoph Hellwig  *
2508af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
2518af27456SChristoph Hellwig  */
2528af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
2538af27456SChristoph Hellwig 		unsigned int length, gfp_t gfp_mask)
2548af27456SChristoph Hellwig {
25543cb76d9SGreg Kroah-Hartman 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
2568af27456SChristoph Hellwig 	struct sk_buff *skb;
2578af27456SChristoph Hellwig 
258b30973f8SChristoph Hellwig 	skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
2597b2e497aSChristoph Hellwig 	if (likely(skb)) {
2608af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
2617b2e497aSChristoph Hellwig 		skb->dev = dev;
2627b2e497aSChristoph Hellwig 	}
2638af27456SChristoph Hellwig 	return skb;
2648af27456SChristoph Hellwig }
2651da177e4SLinus Torvalds 
266654bed16SPeter Zijlstra struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
267654bed16SPeter Zijlstra {
268654bed16SPeter Zijlstra 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
269654bed16SPeter Zijlstra 	struct page *page;
270654bed16SPeter Zijlstra 
271654bed16SPeter Zijlstra 	page = alloc_pages_node(node, gfp_mask, 0);
272654bed16SPeter Zijlstra 	return page;
273654bed16SPeter Zijlstra }
274654bed16SPeter Zijlstra EXPORT_SYMBOL(__netdev_alloc_page);
275654bed16SPeter Zijlstra 
276654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
277654bed16SPeter Zijlstra 		int size)
278654bed16SPeter Zijlstra {
279654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
280654bed16SPeter Zijlstra 	skb->len += size;
281654bed16SPeter Zijlstra 	skb->data_len += size;
282654bed16SPeter Zijlstra 	skb->truesize += size;
283654bed16SPeter Zijlstra }
284654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
285654bed16SPeter Zijlstra 
286f58518e6SIlpo Järvinen /**
287f58518e6SIlpo Järvinen  *	dev_alloc_skb - allocate an skbuff for receiving
288f58518e6SIlpo Järvinen  *	@length: length to allocate
289f58518e6SIlpo Järvinen  *
290f58518e6SIlpo Järvinen  *	Allocate a new &sk_buff and assign it a usage count of one. The
291f58518e6SIlpo Järvinen  *	buffer has unspecified headroom built in. Users should allocate
292f58518e6SIlpo Järvinen  *	the headroom they think they need without accounting for the
293f58518e6SIlpo Järvinen  *	built in space. The built in space is used for optimisations.
294f58518e6SIlpo Järvinen  *
295f58518e6SIlpo Järvinen  *	%NULL is returned if there is no free memory. Although this function
296f58518e6SIlpo Järvinen  *	allocates memory it can be called from an interrupt.
297f58518e6SIlpo Järvinen  */
298f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length)
299f58518e6SIlpo Järvinen {
3001483b874SDenys Vlasenko 	/*
3011483b874SDenys Vlasenko 	 * There is more code here than it seems:
302a0f55e0eSDavid S. Miller 	 * __dev_alloc_skb is an inline
3031483b874SDenys Vlasenko 	 */
304f58518e6SIlpo Järvinen 	return __dev_alloc_skb(length, GFP_ATOMIC);
305f58518e6SIlpo Järvinen }
306f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb);
307f58518e6SIlpo Järvinen 
30827b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
3091da177e4SLinus Torvalds {
31027b437c8SHerbert Xu 	struct sk_buff *list = *listp;
3111da177e4SLinus Torvalds 
31227b437c8SHerbert Xu 	*listp = NULL;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	do {
3151da177e4SLinus Torvalds 		struct sk_buff *this = list;
3161da177e4SLinus Torvalds 		list = list->next;
3171da177e4SLinus Torvalds 		kfree_skb(this);
3181da177e4SLinus Torvalds 	} while (list);
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
32127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
32227b437c8SHerbert Xu {
32327b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
32427b437c8SHerbert Xu }
32527b437c8SHerbert Xu 
3261da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct sk_buff *list;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
3311da177e4SLinus Torvalds 		skb_get(list);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3345bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	if (!skb->cloned ||
3371da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
3381da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
3391da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
3401da177e4SLinus Torvalds 			int i;
3411da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3421da177e4SLinus Torvalds 				put_page(skb_shinfo(skb)->frags[i].page);
3431da177e4SLinus Torvalds 		}
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frag_list)
3461da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 		kfree(skb->head);
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds /*
3531da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
3541da177e4SLinus Torvalds  */
3552d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
3561da177e4SLinus Torvalds {
357d179cd12SDavid S. Miller 	struct sk_buff *other;
358d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
359d179cd12SDavid S. Miller 
360d179cd12SDavid S. Miller 	switch (skb->fclone) {
361d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
3621da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
363d179cd12SDavid S. Miller 		break;
364d179cd12SDavid S. Miller 
365d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
366d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
367d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
368d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
369d179cd12SDavid S. Miller 		break;
370d179cd12SDavid S. Miller 
371d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
372d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
373d179cd12SDavid S. Miller 		other = skb - 1;
374d179cd12SDavid S. Miller 
375d179cd12SDavid S. Miller 		/* The clone portion is available for
376d179cd12SDavid S. Miller 		 * fast-cloning again.
377d179cd12SDavid S. Miller 		 */
378d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
379d179cd12SDavid S. Miller 
380d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
381d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
382d179cd12SDavid S. Miller 		break;
3833ff50b79SStephen Hemminger 	}
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
38604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	dst_release(skb->dst);
3891da177e4SLinus Torvalds #ifdef CONFIG_XFRM
3901da177e4SLinus Torvalds 	secpath_put(skb->sp);
3911da177e4SLinus Torvalds #endif
3921da177e4SLinus Torvalds 	if (skb->destructor) {
3939c2b3328SStephen Hemminger 		WARN_ON(in_irq());
3941da177e4SLinus Torvalds 		skb->destructor(skb);
3951da177e4SLinus Torvalds 	}
3969fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
3975f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
3989fb9cbb1SYasuyuki Kozakai 	nf_conntrack_put_reasm(skb->nfct_reasm);
3999fb9cbb1SYasuyuki Kozakai #endif
4001da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
4011da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
4021da177e4SLinus Torvalds #endif
4031da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
4041da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4051da177e4SLinus Torvalds 	skb->tc_index = 0;
4061da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
4071da177e4SLinus Torvalds 	skb->tc_verd = 0;
4081da177e4SLinus Torvalds #endif
4091da177e4SLinus Torvalds #endif
41004a4bb55SLennert Buytenhek }
41104a4bb55SLennert Buytenhek 
41204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
41304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
41404a4bb55SLennert Buytenhek {
41504a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
4162d4baff8SHerbert Xu 	skb_release_data(skb);
4172d4baff8SHerbert Xu }
4181da177e4SLinus Torvalds 
4192d4baff8SHerbert Xu /**
4202d4baff8SHerbert Xu  *	__kfree_skb - private function
4212d4baff8SHerbert Xu  *	@skb: buffer
4222d4baff8SHerbert Xu  *
4232d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
4242d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
4252d4baff8SHerbert Xu  *	always call kfree_skb
4262d4baff8SHerbert Xu  */
4272d4baff8SHerbert Xu 
4282d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
4292d4baff8SHerbert Xu {
4302d4baff8SHerbert Xu 	skb_release_all(skb);
4311da177e4SLinus Torvalds 	kfree_skbmem(skb);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /**
435231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
436231d06aeSJörn Engel  *	@skb: buffer to free
437231d06aeSJörn Engel  *
438231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
439231d06aeSJörn Engel  *	hit zero.
440231d06aeSJörn Engel  */
441231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
442231d06aeSJörn Engel {
443231d06aeSJörn Engel 	if (unlikely(!skb))
444231d06aeSJörn Engel 		return;
445231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
446231d06aeSJörn Engel 		smp_rmb();
447231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
448231d06aeSJörn Engel 		return;
449231d06aeSJörn Engel 	__kfree_skb(skb);
450231d06aeSJörn Engel }
451231d06aeSJörn Engel 
452d1a203eaSStephen Hemminger /**
453d1a203eaSStephen Hemminger  *	skb_recycle_check - check if skb can be reused for receive
454d1a203eaSStephen Hemminger  *	@skb: buffer
455d1a203eaSStephen Hemminger  *	@skb_size: minimum receive buffer size
456d1a203eaSStephen Hemminger  *
457d1a203eaSStephen Hemminger  *	Checks that the skb passed in is not shared or cloned, and
458d1a203eaSStephen Hemminger  *	that it is linear and its head portion at least as large as
459d1a203eaSStephen Hemminger  *	skb_size so that it can be recycled as a receive buffer.
460d1a203eaSStephen Hemminger  *	If these conditions are met, this function does any necessary
461d1a203eaSStephen Hemminger  *	reference count dropping and cleans up the skbuff as if it
462d1a203eaSStephen Hemminger  *	just came from __alloc_skb().
463d1a203eaSStephen Hemminger  */
46404a4bb55SLennert Buytenhek int skb_recycle_check(struct sk_buff *skb, int skb_size)
46504a4bb55SLennert Buytenhek {
46604a4bb55SLennert Buytenhek 	struct skb_shared_info *shinfo;
46704a4bb55SLennert Buytenhek 
46804a4bb55SLennert Buytenhek 	if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
46904a4bb55SLennert Buytenhek 		return 0;
47004a4bb55SLennert Buytenhek 
47104a4bb55SLennert Buytenhek 	skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
47204a4bb55SLennert Buytenhek 	if (skb_end_pointer(skb) - skb->head < skb_size)
47304a4bb55SLennert Buytenhek 		return 0;
47404a4bb55SLennert Buytenhek 
47504a4bb55SLennert Buytenhek 	if (skb_shared(skb) || skb_cloned(skb))
47604a4bb55SLennert Buytenhek 		return 0;
47704a4bb55SLennert Buytenhek 
47804a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
47904a4bb55SLennert Buytenhek 	shinfo = skb_shinfo(skb);
48004a4bb55SLennert Buytenhek 	atomic_set(&shinfo->dataref, 1);
48104a4bb55SLennert Buytenhek 	shinfo->nr_frags = 0;
48204a4bb55SLennert Buytenhek 	shinfo->gso_size = 0;
48304a4bb55SLennert Buytenhek 	shinfo->gso_segs = 0;
48404a4bb55SLennert Buytenhek 	shinfo->gso_type = 0;
48504a4bb55SLennert Buytenhek 	shinfo->ip6_frag_id = 0;
48604a4bb55SLennert Buytenhek 	shinfo->frag_list = NULL;
48704a4bb55SLennert Buytenhek 
48804a4bb55SLennert Buytenhek 	memset(skb, 0, offsetof(struct sk_buff, tail));
48904a4bb55SLennert Buytenhek 	skb->data = skb->head + NET_SKB_PAD;
4905cd33db2SLennert Buytenhek 	skb_reset_tail_pointer(skb);
49104a4bb55SLennert Buytenhek 
49204a4bb55SLennert Buytenhek 	return 1;
49304a4bb55SLennert Buytenhek }
49404a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check);
49504a4bb55SLennert Buytenhek 
496dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
497dec18810SHerbert Xu {
498dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
499dec18810SHerbert Xu 	new->dev		= old->dev;
500dec18810SHerbert Xu 	new->transport_header	= old->transport_header;
501dec18810SHerbert Xu 	new->network_header	= old->network_header;
502dec18810SHerbert Xu 	new->mac_header		= old->mac_header;
503dec18810SHerbert Xu 	new->dst		= dst_clone(old->dst);
504def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
505dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
506dec18810SHerbert Xu #endif
507dec18810SHerbert Xu 	memcpy(new->cb, old->cb, sizeof(old->cb));
508dec18810SHerbert Xu 	new->csum_start		= old->csum_start;
509dec18810SHerbert Xu 	new->csum_offset	= old->csum_offset;
510dec18810SHerbert Xu 	new->local_df		= old->local_df;
511dec18810SHerbert Xu 	new->pkt_type		= old->pkt_type;
512dec18810SHerbert Xu 	new->ip_summed		= old->ip_summed;
513dec18810SHerbert Xu 	skb_copy_queue_mapping(new, old);
514dec18810SHerbert Xu 	new->priority		= old->priority;
515dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
516dec18810SHerbert Xu 	new->ipvs_property	= old->ipvs_property;
517dec18810SHerbert Xu #endif
518dec18810SHerbert Xu 	new->protocol		= old->protocol;
519dec18810SHerbert Xu 	new->mark		= old->mark;
520dec18810SHerbert Xu 	__nf_copy(new, old);
521dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
522dec18810SHerbert Xu     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
523dec18810SHerbert Xu 	new->nf_trace		= old->nf_trace;
524dec18810SHerbert Xu #endif
525dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED
526dec18810SHerbert Xu 	new->tc_index		= old->tc_index;
527dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT
528dec18810SHerbert Xu 	new->tc_verd		= old->tc_verd;
529dec18810SHerbert Xu #endif
530dec18810SHerbert Xu #endif
5316aa895b0SPatrick McHardy 	new->vlan_tci		= old->vlan_tci;
5326aa895b0SPatrick McHardy 
533dec18810SHerbert Xu 	skb_copy_secmark(new, old);
534dec18810SHerbert Xu }
535dec18810SHerbert Xu 
536e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
5371da177e4SLinus Torvalds {
5381da177e4SLinus Torvalds #define C(x) n->x = skb->x
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	n->next = n->prev = NULL;
5411da177e4SLinus Torvalds 	n->sk = NULL;
542dec18810SHerbert Xu 	__copy_skb_header(n, skb);
543dec18810SHerbert Xu 
5441da177e4SLinus Torvalds 	C(len);
5451da177e4SLinus Torvalds 	C(data_len);
5463e6b3b2eSAlexey Dobriyan 	C(mac_len);
547334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
54802f1c89dSPaul Moore 	n->cloned = 1;
5491da177e4SLinus Torvalds 	n->nohdr = 0;
5501da177e4SLinus Torvalds 	n->destructor = NULL;
55102f1c89dSPaul Moore 	C(iif);
5521da177e4SLinus Torvalds 	C(tail);
5531da177e4SLinus Torvalds 	C(end);
55402f1c89dSPaul Moore 	C(head);
55502f1c89dSPaul Moore 	C(data);
55602f1c89dSPaul Moore 	C(truesize);
557d0f09804SJohannes Berg #if defined(CONFIG_MAC80211) || defined(CONFIG_MAC80211_MODULE)
558d0f09804SJohannes Berg 	C(do_not_encrypt);
5598b30b1feSSujith 	C(requeue);
560d0f09804SJohannes Berg #endif
56102f1c89dSPaul Moore 	atomic_set(&n->users, 1);
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
5641da177e4SLinus Torvalds 	skb->cloned = 1;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	return n;
567e0053ec0SHerbert Xu #undef C
568e0053ec0SHerbert Xu }
569e0053ec0SHerbert Xu 
570e0053ec0SHerbert Xu /**
571e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
572e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
573e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
574e0053ec0SHerbert Xu  *
575e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
576e0053ec0SHerbert Xu  *	supplied by the user.
577e0053ec0SHerbert Xu  *
578e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
579e0053ec0SHerbert Xu  */
580e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
581e0053ec0SHerbert Xu {
5822d4baff8SHerbert Xu 	skb_release_all(dst);
583e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
584e0053ec0SHerbert Xu }
585e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
586e0053ec0SHerbert Xu 
587e0053ec0SHerbert Xu /**
588e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
589e0053ec0SHerbert Xu  *	@skb: buffer to clone
590e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
591e0053ec0SHerbert Xu  *
592e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
593e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
594e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
595e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
596e0053ec0SHerbert Xu  *
597e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
598e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
599e0053ec0SHerbert Xu  */
600e0053ec0SHerbert Xu 
601e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
602e0053ec0SHerbert Xu {
603e0053ec0SHerbert Xu 	struct sk_buff *n;
604e0053ec0SHerbert Xu 
605e0053ec0SHerbert Xu 	n = skb + 1;
606e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
607e0053ec0SHerbert Xu 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
608e0053ec0SHerbert Xu 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
609e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
610e0053ec0SHerbert Xu 		atomic_inc(fclone_ref);
611e0053ec0SHerbert Xu 	} else {
612e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
613e0053ec0SHerbert Xu 		if (!n)
614e0053ec0SHerbert Xu 			return NULL;
615e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
616e0053ec0SHerbert Xu 	}
617e0053ec0SHerbert Xu 
618e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
6221da177e4SLinus Torvalds {
6232e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
6241da177e4SLinus Torvalds 	/*
6251da177e4SLinus Torvalds 	 *	Shift between the two data areas in bytes
6261da177e4SLinus Torvalds 	 */
6271da177e4SLinus Torvalds 	unsigned long offset = new->data - old->data;
6282e07fa9cSArnaldo Carvalho de Melo #endif
629dec18810SHerbert Xu 
630dec18810SHerbert Xu 	__copy_skb_header(new, old);
631dec18810SHerbert Xu 
6322e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
6332e07fa9cSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header are relative to skb->head */
6342e07fa9cSArnaldo Carvalho de Melo 	new->transport_header += offset;
6352e07fa9cSArnaldo Carvalho de Melo 	new->network_header   += offset;
6362e07fa9cSArnaldo Carvalho de Melo 	new->mac_header	      += offset;
6372e07fa9cSArnaldo Carvalho de Melo #endif
6387967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
6397967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
6407967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds /**
6441da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
6451da177e4SLinus Torvalds  *	@skb: buffer to copy
6461da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6471da177e4SLinus Torvalds  *
6481da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
6491da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
6501da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
6511da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
6521da177e4SLinus Torvalds  *
6531da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
6541da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
6551da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
6561da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
6571da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
6581da177e4SLinus Torvalds  */
6591da177e4SLinus Torvalds 
660dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	int headerlen = skb->data - skb->head;
6631da177e4SLinus Torvalds 	/*
6641da177e4SLinus Torvalds 	 *	Allocate the copy buffer
6651da177e4SLinus Torvalds 	 */
6664305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
6674305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
6684305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end + skb->data_len, gfp_mask);
6694305b541SArnaldo Carvalho de Melo #else
6704305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
6714305b541SArnaldo Carvalho de Melo #endif
6721da177e4SLinus Torvalds 	if (!n)
6731da177e4SLinus Torvalds 		return NULL;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	/* Set the data pointer */
6761da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
6771da177e4SLinus Torvalds 	/* Set the tail pointer and length */
6781da177e4SLinus Torvalds 	skb_put(n, skb->len);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
6811da177e4SLinus Torvalds 		BUG();
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	copy_skb_header(n, skb);
6841da177e4SLinus Torvalds 	return n;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds /**
6891da177e4SLinus Torvalds  *	pskb_copy	-	create copy of an sk_buff with private head.
6901da177e4SLinus Torvalds  *	@skb: buffer to copy
6911da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6921da177e4SLinus Torvalds  *
6931da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
6941da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
6951da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
6961da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
6971da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
6981da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
6991da177e4SLinus Torvalds  */
7001da177e4SLinus Torvalds 
701dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	/*
7041da177e4SLinus Torvalds 	 *	Allocate the copy buffer
7051da177e4SLinus Torvalds 	 */
7064305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
7074305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
7084305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end, gfp_mask);
7094305b541SArnaldo Carvalho de Melo #else
7104305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head, gfp_mask);
7114305b541SArnaldo Carvalho de Melo #endif
7121da177e4SLinus Torvalds 	if (!n)
7131da177e4SLinus Torvalds 		goto out;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	/* Set the data pointer */
7161da177e4SLinus Torvalds 	skb_reserve(n, skb->data - skb->head);
7171da177e4SLinus Torvalds 	/* Set the tail pointer and length */
7181da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
7191da177e4SLinus Torvalds 	/* Copy the bytes */
720d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
7211da177e4SLinus Torvalds 
72225f484a6SHerbert Xu 	n->truesize += skb->data_len;
7231da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
7241da177e4SLinus Torvalds 	n->len	     = skb->len;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
7271da177e4SLinus Torvalds 		int i;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
7301da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
7311da177e4SLinus Torvalds 			get_page(skb_shinfo(n)->frags[i].page);
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
7371da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
7381da177e4SLinus Torvalds 		skb_clone_fraglist(n);
7391da177e4SLinus Torvalds 	}
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7421da177e4SLinus Torvalds out:
7431da177e4SLinus Torvalds 	return n;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds /**
7471da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
7481da177e4SLinus Torvalds  *	@skb: buffer to reallocate
7491da177e4SLinus Torvalds  *	@nhead: room to add at head
7501da177e4SLinus Torvalds  *	@ntail: room to add at tail
7511da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7521da177e4SLinus Torvalds  *
7531da177e4SLinus Torvalds  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
7541da177e4SLinus Torvalds  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
7551da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
7561da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
7571da177e4SLinus Torvalds  *
7581da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
7591da177e4SLinus Torvalds  *	reloaded after call to this function.
7601da177e4SLinus Torvalds  */
7611da177e4SLinus Torvalds 
76286a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
763dd0fc66fSAl Viro 		     gfp_t gfp_mask)
7641da177e4SLinus Torvalds {
7651da177e4SLinus Torvalds 	int i;
7661da177e4SLinus Torvalds 	u8 *data;
7674305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
7684305b541SArnaldo Carvalho de Melo 	int size = nhead + skb->end + ntail;
7694305b541SArnaldo Carvalho de Melo #else
7701da177e4SLinus Torvalds 	int size = nhead + (skb->end - skb->head) + ntail;
7714305b541SArnaldo Carvalho de Melo #endif
7721da177e4SLinus Torvalds 	long off;
7731da177e4SLinus Torvalds 
7744edd87adSHerbert Xu 	BUG_ON(nhead < 0);
7754edd87adSHerbert Xu 
7761da177e4SLinus Torvalds 	if (skb_shared(skb))
7771da177e4SLinus Torvalds 		BUG();
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
7821da177e4SLinus Torvalds 	if (!data)
7831da177e4SLinus Torvalds 		goto nodata;
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
7861da177e4SLinus Torvalds 	 * optimized for the cases when header is void. */
7874305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
788b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail);
7894305b541SArnaldo Carvalho de Melo #else
790b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail - skb->head);
79127a884dcSArnaldo Carvalho de Melo #endif
7924305b541SArnaldo Carvalho de Melo 	memcpy(data + size, skb_end_pointer(skb),
7934305b541SArnaldo Carvalho de Melo 	       sizeof(struct skb_shared_info));
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
7961da177e4SLinus Torvalds 		get_page(skb_shinfo(skb)->frags[i].page);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list)
7991da177e4SLinus Torvalds 		skb_clone_fraglist(skb);
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	skb_release_data(skb);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	skb->head     = data;
8061da177e4SLinus Torvalds 	skb->data    += off;
8074305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
8084305b541SArnaldo Carvalho de Melo 	skb->end      = size;
80956eb8882SPatrick McHardy 	off           = nhead;
8104305b541SArnaldo Carvalho de Melo #else
8114305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
81256eb8882SPatrick McHardy #endif
81327a884dcSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header and tail are relative to skb->head */
81427a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
815b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header += off;
816b0e380b1SArnaldo Carvalho de Melo 	skb->network_header   += off;
817b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header	      += off;
818172a863fSHerbert Xu 	skb->csum_start       += nhead;
8191da177e4SLinus Torvalds 	skb->cloned   = 0;
820334a8132SPatrick McHardy 	skb->hdr_len  = 0;
8211da177e4SLinus Torvalds 	skb->nohdr    = 0;
8221da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
8231da177e4SLinus Torvalds 	return 0;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds nodata:
8261da177e4SLinus Torvalds 	return -ENOMEM;
8271da177e4SLinus Torvalds }
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct sk_buff *skb2;
8341da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	if (delta <= 0)
8371da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
8381da177e4SLinus Torvalds 	else {
8391da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
8401da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
8411da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
8421da177e4SLinus Torvalds 			kfree_skb(skb2);
8431da177e4SLinus Torvalds 			skb2 = NULL;
8441da177e4SLinus Torvalds 		}
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 	return skb2;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds /**
8511da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
8521da177e4SLinus Torvalds  *	@skb: buffer to copy
8531da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
8541da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
8551da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
8561da177e4SLinus Torvalds  *
8571da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
8581da177e4SLinus Torvalds  *	allocate additional space.
8591da177e4SLinus Torvalds  *
8601da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
8611da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
8621da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
8631da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
8641da177e4SLinus Torvalds  *
8651da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
8661da177e4SLinus Torvalds  *	is called from an interrupt.
8671da177e4SLinus Torvalds  */
8681da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
86986a76cafSVictor Fusco 				int newheadroom, int newtailroom,
870dd0fc66fSAl Viro 				gfp_t gfp_mask)
8711da177e4SLinus Torvalds {
8721da177e4SLinus Torvalds 	/*
8731da177e4SLinus Torvalds 	 *	Allocate the copy buffer
8741da177e4SLinus Torvalds 	 */
8751da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
8761da177e4SLinus Torvalds 				      gfp_mask);
877efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
8781da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
87952886051SHerbert Xu 	int off;
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	if (!n)
8821da177e4SLinus Torvalds 		return NULL;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	/* Set the tail pointer and length */
8871da177e4SLinus Torvalds 	skb_put(n, skb->len);
8881da177e4SLinus Torvalds 
889efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
8901da177e4SLinus Torvalds 	head_copy_off = 0;
8911da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
8921da177e4SLinus Torvalds 		head_copy_len = newheadroom;
8931da177e4SLinus Torvalds 	else
8941da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	/* Copy the linear header and data. */
8971da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
8981da177e4SLinus Torvalds 			  skb->len + head_copy_len))
8991da177e4SLinus Torvalds 		BUG();
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	copy_skb_header(n, skb);
9021da177e4SLinus Torvalds 
903efd1e8d5SPatrick McHardy 	off                  = newheadroom - oldheadroom;
90452886051SHerbert Xu 	n->csum_start       += off;
90552886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET
906efd1e8d5SPatrick McHardy 	n->transport_header += off;
907efd1e8d5SPatrick McHardy 	n->network_header   += off;
908efd1e8d5SPatrick McHardy 	n->mac_header	    += off;
90952886051SHerbert Xu #endif
910efd1e8d5SPatrick McHardy 
9111da177e4SLinus Torvalds 	return n;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds /**
9151da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
9161da177e4SLinus Torvalds  *	@skb: buffer to pad
9171da177e4SLinus Torvalds  *	@pad: space to pad
9181da177e4SLinus Torvalds  *
9191da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
9201da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
9211da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
9221da177e4SLinus Torvalds  *
9235b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
9241da177e4SLinus Torvalds  */
9251da177e4SLinus Torvalds 
9265b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
9271da177e4SLinus Torvalds {
9285b057c6bSHerbert Xu 	int err;
9295b057c6bSHerbert Xu 	int ntail;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
9325b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
9331da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
9345b057c6bSHerbert Xu 		return 0;
9351da177e4SLinus Torvalds 	}
9361da177e4SLinus Torvalds 
9374305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
9385b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
9395b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
9405b057c6bSHerbert Xu 		if (unlikely(err))
9415b057c6bSHerbert Xu 			goto free_skb;
9425b057c6bSHerbert Xu 	}
9435b057c6bSHerbert Xu 
9445b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
9455b057c6bSHerbert Xu 	 * to be audited.
9465b057c6bSHerbert Xu 	 */
9475b057c6bSHerbert Xu 	err = skb_linearize(skb);
9485b057c6bSHerbert Xu 	if (unlikely(err))
9495b057c6bSHerbert Xu 		goto free_skb;
9505b057c6bSHerbert Xu 
9515b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
9525b057c6bSHerbert Xu 	return 0;
9535b057c6bSHerbert Xu 
9545b057c6bSHerbert Xu free_skb:
9551da177e4SLinus Torvalds 	kfree_skb(skb);
9565b057c6bSHerbert Xu 	return err;
9571da177e4SLinus Torvalds }
9581da177e4SLinus Torvalds 
9590dde3e16SIlpo Järvinen /**
9600dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
9610dde3e16SIlpo Järvinen  *	@skb: buffer to use
9620dde3e16SIlpo Järvinen  *	@len: amount of data to add
9630dde3e16SIlpo Järvinen  *
9640dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
9650dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
9660dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
9670dde3e16SIlpo Järvinen  */
9680dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
9690dde3e16SIlpo Järvinen {
9700dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
9710dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
9720dde3e16SIlpo Järvinen 	skb->tail += len;
9730dde3e16SIlpo Järvinen 	skb->len  += len;
9740dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
9750dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
9760dde3e16SIlpo Järvinen 	return tmp;
9770dde3e16SIlpo Järvinen }
9780dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
9790dde3e16SIlpo Järvinen 
9806be8ac2fSIlpo Järvinen /**
981c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
982c2aa270aSIlpo Järvinen  *	@skb: buffer to use
983c2aa270aSIlpo Järvinen  *	@len: amount of data to add
984c2aa270aSIlpo Järvinen  *
985c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
986c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
987c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
988c2aa270aSIlpo Järvinen  */
989c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
990c2aa270aSIlpo Järvinen {
991c2aa270aSIlpo Järvinen 	skb->data -= len;
992c2aa270aSIlpo Järvinen 	skb->len  += len;
993c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
994c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
995c2aa270aSIlpo Järvinen 	return skb->data;
996c2aa270aSIlpo Järvinen }
997c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
998c2aa270aSIlpo Järvinen 
999c2aa270aSIlpo Järvinen /**
10006be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
10016be8ac2fSIlpo Järvinen  *	@skb: buffer to use
10026be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
10036be8ac2fSIlpo Järvinen  *
10046be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
10056be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
10066be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
10076be8ac2fSIlpo Järvinen  *	the old data.
10086be8ac2fSIlpo Järvinen  */
10096be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
10106be8ac2fSIlpo Järvinen {
10116be8ac2fSIlpo Järvinen 	return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
10126be8ac2fSIlpo Järvinen }
10136be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
10146be8ac2fSIlpo Järvinen 
1015419ae74eSIlpo Järvinen /**
1016419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1017419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1018419ae74eSIlpo Järvinen  *	@len: new length
1019419ae74eSIlpo Järvinen  *
1020419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1021419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1022419ae74eSIlpo Järvinen  *	The skb must be linear.
1023419ae74eSIlpo Järvinen  */
1024419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1025419ae74eSIlpo Järvinen {
1026419ae74eSIlpo Järvinen 	if (skb->len > len)
1027419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1028419ae74eSIlpo Järvinen }
1029419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1030419ae74eSIlpo Järvinen 
10313cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
10321da177e4SLinus Torvalds  */
10331da177e4SLinus Torvalds 
10343cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
10351da177e4SLinus Torvalds {
103627b437c8SHerbert Xu 	struct sk_buff **fragp;
103727b437c8SHerbert Xu 	struct sk_buff *frag;
10381da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
10391da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
10401da177e4SLinus Torvalds 	int i;
104127b437c8SHerbert Xu 	int err;
104227b437c8SHerbert Xu 
104327b437c8SHerbert Xu 	if (skb_cloned(skb) &&
104427b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
104527b437c8SHerbert Xu 		return err;
10461da177e4SLinus Torvalds 
1047f4d26fb3SHerbert Xu 	i = 0;
1048f4d26fb3SHerbert Xu 	if (offset >= len)
1049f4d26fb3SHerbert Xu 		goto drop_pages;
1050f4d26fb3SHerbert Xu 
1051f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
10521da177e4SLinus Torvalds 		int end = offset + skb_shinfo(skb)->frags[i].size;
105327b437c8SHerbert Xu 
105427b437c8SHerbert Xu 		if (end < len) {
10551da177e4SLinus Torvalds 			offset = end;
105627b437c8SHerbert Xu 			continue;
10571da177e4SLinus Torvalds 		}
10581da177e4SLinus Torvalds 
105927b437c8SHerbert Xu 		skb_shinfo(skb)->frags[i++].size = len - offset;
106027b437c8SHerbert Xu 
1061f4d26fb3SHerbert Xu drop_pages:
106227b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
106327b437c8SHerbert Xu 
106427b437c8SHerbert Xu 		for (; i < nfrags; i++)
106527b437c8SHerbert Xu 			put_page(skb_shinfo(skb)->frags[i].page);
106627b437c8SHerbert Xu 
106727b437c8SHerbert Xu 		if (skb_shinfo(skb)->frag_list)
106827b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1069f4d26fb3SHerbert Xu 		goto done;
107027b437c8SHerbert Xu 	}
107127b437c8SHerbert Xu 
107227b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
107327b437c8SHerbert Xu 	     fragp = &frag->next) {
107427b437c8SHerbert Xu 		int end = offset + frag->len;
107527b437c8SHerbert Xu 
107627b437c8SHerbert Xu 		if (skb_shared(frag)) {
107727b437c8SHerbert Xu 			struct sk_buff *nfrag;
107827b437c8SHerbert Xu 
107927b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
108027b437c8SHerbert Xu 			if (unlikely(!nfrag))
108127b437c8SHerbert Xu 				return -ENOMEM;
108227b437c8SHerbert Xu 
108327b437c8SHerbert Xu 			nfrag->next = frag->next;
1084f4d26fb3SHerbert Xu 			kfree_skb(frag);
108527b437c8SHerbert Xu 			frag = nfrag;
108627b437c8SHerbert Xu 			*fragp = frag;
108727b437c8SHerbert Xu 		}
108827b437c8SHerbert Xu 
108927b437c8SHerbert Xu 		if (end < len) {
109027b437c8SHerbert Xu 			offset = end;
109127b437c8SHerbert Xu 			continue;
109227b437c8SHerbert Xu 		}
109327b437c8SHerbert Xu 
109427b437c8SHerbert Xu 		if (end > len &&
109527b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
109627b437c8SHerbert Xu 			return err;
109727b437c8SHerbert Xu 
109827b437c8SHerbert Xu 		if (frag->next)
109927b437c8SHerbert Xu 			skb_drop_list(&frag->next);
110027b437c8SHerbert Xu 		break;
110127b437c8SHerbert Xu 	}
110227b437c8SHerbert Xu 
1103f4d26fb3SHerbert Xu done:
110427b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
11051da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
11061da177e4SLinus Torvalds 		skb->len       = len;
11071da177e4SLinus Torvalds 	} else {
11081da177e4SLinus Torvalds 		skb->len       = len;
11091da177e4SLinus Torvalds 		skb->data_len  = 0;
111027a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	return 0;
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds /**
11171da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
11181da177e4SLinus Torvalds  *	@skb: buffer to reallocate
11191da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
11201da177e4SLinus Torvalds  *
11211da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
11221da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
11231da177e4SLinus Torvalds  *	data from fragmented part.
11241da177e4SLinus Torvalds  *
11251da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
11261da177e4SLinus Torvalds  *
11271da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
11281da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
11291da177e4SLinus Torvalds  *
11301da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
11311da177e4SLinus Torvalds  *	reloaded after call to this function.
11321da177e4SLinus Torvalds  */
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
11351da177e4SLinus Torvalds  * when it is necessary.
11361da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
11371da177e4SLinus Torvalds  * 2. It may change skb pointers.
11381da177e4SLinus Torvalds  *
11391da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
11401da177e4SLinus Torvalds  */
11411da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
11421da177e4SLinus Torvalds {
11431da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
11441da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
11451da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
11461da177e4SLinus Torvalds 	 */
11474305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
11501da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
11511da177e4SLinus Torvalds 				     GFP_ATOMIC))
11521da177e4SLinus Torvalds 			return NULL;
11531da177e4SLinus Torvalds 	}
11541da177e4SLinus Torvalds 
115527a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
11561da177e4SLinus Torvalds 		BUG();
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
11591da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
11601da177e4SLinus Torvalds 	 */
11611da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->frag_list)
11621da177e4SLinus Torvalds 		goto pull_pages;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
11651da177e4SLinus Torvalds 	eat = delta;
11661da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
11671da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size >= eat)
11681da177e4SLinus Torvalds 			goto pull_pages;
11691da177e4SLinus Torvalds 		eat -= skb_shinfo(skb)->frags[i].size;
11701da177e4SLinus Torvalds 	}
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
11731da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
11741da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
11751da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
11761da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
11771da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
11781da177e4SLinus Torvalds 	 */
11791da177e4SLinus Torvalds 	if (eat) {
11801da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
11811da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
11821da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 		do {
118509a62660SKris Katterjohn 			BUG_ON(!list);
11861da177e4SLinus Torvalds 
11871da177e4SLinus Torvalds 			if (list->len <= eat) {
11881da177e4SLinus Torvalds 				/* Eaten as whole. */
11891da177e4SLinus Torvalds 				eat -= list->len;
11901da177e4SLinus Torvalds 				list = list->next;
11911da177e4SLinus Torvalds 				insp = list;
11921da177e4SLinus Torvalds 			} else {
11931da177e4SLinus Torvalds 				/* Eaten partially. */
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 				if (skb_shared(list)) {
11961da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
11971da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
11981da177e4SLinus Torvalds 					if (!clone)
11991da177e4SLinus Torvalds 						return NULL;
12001da177e4SLinus Torvalds 					insp = list->next;
12011da177e4SLinus Torvalds 					list = clone;
12021da177e4SLinus Torvalds 				} else {
12031da177e4SLinus Torvalds 					/* This may be pulled without
12041da177e4SLinus Torvalds 					 * problems. */
12051da177e4SLinus Torvalds 					insp = list;
12061da177e4SLinus Torvalds 				}
12071da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
12081da177e4SLinus Torvalds 					if (clone)
12091da177e4SLinus Torvalds 						kfree_skb(clone);
12101da177e4SLinus Torvalds 					return NULL;
12111da177e4SLinus Torvalds 				}
12121da177e4SLinus Torvalds 				break;
12131da177e4SLinus Torvalds 			}
12141da177e4SLinus Torvalds 		} while (eat);
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 		/* Free pulled out fragments. */
12171da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
12181da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
12191da177e4SLinus Torvalds 			kfree_skb(list);
12201da177e4SLinus Torvalds 		}
12211da177e4SLinus Torvalds 		/* And insert new clone at head. */
12221da177e4SLinus Torvalds 		if (clone) {
12231da177e4SLinus Torvalds 			clone->next = list;
12241da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds pull_pages:
12301da177e4SLinus Torvalds 	eat = delta;
12311da177e4SLinus Torvalds 	k = 0;
12321da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12331da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size <= eat) {
12341da177e4SLinus Torvalds 			put_page(skb_shinfo(skb)->frags[i].page);
12351da177e4SLinus Torvalds 			eat -= skb_shinfo(skb)->frags[i].size;
12361da177e4SLinus Torvalds 		} else {
12371da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
12381da177e4SLinus Torvalds 			if (eat) {
12391da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
12401da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].size -= eat;
12411da177e4SLinus Torvalds 				eat = 0;
12421da177e4SLinus Torvalds 			}
12431da177e4SLinus Torvalds 			k++;
12441da177e4SLinus Torvalds 		}
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	skb->tail     += delta;
12491da177e4SLinus Torvalds 	skb->data_len -= delta;
12501da177e4SLinus Torvalds 
125127a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	int i, copy;
12591a028e50SDavid S. Miller 	int start = skb_headlen(skb);
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
12621da177e4SLinus Torvalds 		goto fault;
12631da177e4SLinus Torvalds 
12641da177e4SLinus Torvalds 	/* Copy header. */
12651a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
12661da177e4SLinus Torvalds 		if (copy > len)
12671da177e4SLinus Torvalds 			copy = len;
1268d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
12691da177e4SLinus Torvalds 		if ((len -= copy) == 0)
12701da177e4SLinus Torvalds 			return 0;
12711da177e4SLinus Torvalds 		offset += copy;
12721da177e4SLinus Torvalds 		to     += copy;
12731da177e4SLinus Torvalds 	}
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12761a028e50SDavid S. Miller 		int end;
12771da177e4SLinus Torvalds 
1278547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
12791a028e50SDavid S. Miller 
12801a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
12811da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
12821da177e4SLinus Torvalds 			u8 *vaddr;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 			if (copy > len)
12851da177e4SLinus Torvalds 				copy = len;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
12881da177e4SLinus Torvalds 			memcpy(to,
12891a028e50SDavid S. Miller 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
12901a028e50SDavid S. Miller 			       offset - start, copy);
12911da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 			if ((len -= copy) == 0)
12941da177e4SLinus Torvalds 				return 0;
12951da177e4SLinus Torvalds 			offset += copy;
12961da177e4SLinus Torvalds 			to     += copy;
12971da177e4SLinus Torvalds 		}
12981a028e50SDavid S. Miller 		start = end;
12991da177e4SLinus Torvalds 	}
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
13021da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		for (; list; list = list->next) {
13051a028e50SDavid S. Miller 			int end;
13061da177e4SLinus Torvalds 
1307547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
13081a028e50SDavid S. Miller 
13091a028e50SDavid S. Miller 			end = start + list->len;
13101da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
13111da177e4SLinus Torvalds 				if (copy > len)
13121da177e4SLinus Torvalds 					copy = len;
13131a028e50SDavid S. Miller 				if (skb_copy_bits(list, offset - start,
13141a028e50SDavid S. Miller 						  to, copy))
13151da177e4SLinus Torvalds 					goto fault;
13161da177e4SLinus Torvalds 				if ((len -= copy) == 0)
13171da177e4SLinus Torvalds 					return 0;
13181da177e4SLinus Torvalds 				offset += copy;
13191da177e4SLinus Torvalds 				to     += copy;
13201da177e4SLinus Torvalds 			}
13211a028e50SDavid S. Miller 			start = end;
13221da177e4SLinus Torvalds 		}
13231da177e4SLinus Torvalds 	}
13241da177e4SLinus Torvalds 	if (!len)
13251da177e4SLinus Torvalds 		return 0;
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds fault:
13281da177e4SLinus Torvalds 	return -EFAULT;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds 
13319c55e01cSJens Axboe /*
13329c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
13339c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
13349c55e01cSJens Axboe  */
13359c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
13369c55e01cSJens Axboe {
13379c55e01cSJens Axboe 	struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private;
13389c55e01cSJens Axboe 
13399c55e01cSJens Axboe 	kfree_skb(skb);
13409c55e01cSJens Axboe }
13419c55e01cSJens Axboe 
13429c55e01cSJens Axboe /*
13439c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
13449c55e01cSJens Axboe  */
13459c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
13469c55e01cSJens Axboe 				unsigned int len, unsigned int offset,
13479c55e01cSJens Axboe 				struct sk_buff *skb)
13489c55e01cSJens Axboe {
13499c55e01cSJens Axboe 	if (unlikely(spd->nr_pages == PIPE_BUFFERS))
13509c55e01cSJens Axboe 		return 1;
13519c55e01cSJens Axboe 
13529c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
13539c55e01cSJens Axboe 	spd->partial[spd->nr_pages].len = len;
13549c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
13559c55e01cSJens Axboe 	spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb);
13569c55e01cSJens Axboe 	spd->nr_pages++;
13579c55e01cSJens Axboe 	return 0;
13589c55e01cSJens Axboe }
13599c55e01cSJens Axboe 
13602870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff,
13612870c43dSOctavian Purdila 				  unsigned int *plen, unsigned int off)
13622870c43dSOctavian Purdila {
13632870c43dSOctavian Purdila 	*poff += off;
13642870c43dSOctavian Purdila 	*page += *poff / PAGE_SIZE;
13652870c43dSOctavian Purdila 	*poff = *poff % PAGE_SIZE;
13662870c43dSOctavian Purdila 	*plen -= off;
13672870c43dSOctavian Purdila }
13682870c43dSOctavian Purdila 
13692870c43dSOctavian Purdila static inline int __splice_segment(struct page *page, unsigned int poff,
13702870c43dSOctavian Purdila 				   unsigned int plen, unsigned int *off,
13712870c43dSOctavian Purdila 				   unsigned int *len, struct sk_buff *skb,
13729c55e01cSJens Axboe 				   struct splice_pipe_desc *spd)
13739c55e01cSJens Axboe {
13742870c43dSOctavian Purdila 	if (!*len)
13752870c43dSOctavian Purdila 		return 1;
13769c55e01cSJens Axboe 
13772870c43dSOctavian Purdila 	/* skip this segment if already processed */
13782870c43dSOctavian Purdila 	if (*off >= plen) {
13792870c43dSOctavian Purdila 		*off -= plen;
13802870c43dSOctavian Purdila 		return 0;
13812870c43dSOctavian Purdila 	}
13822870c43dSOctavian Purdila 
13832870c43dSOctavian Purdila 	/* ignore any bits we already processed */
13842870c43dSOctavian Purdila 	if (*off) {
13852870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, *off);
13862870c43dSOctavian Purdila 		*off = 0;
13872870c43dSOctavian Purdila 	}
13882870c43dSOctavian Purdila 
13892870c43dSOctavian Purdila 	do {
13902870c43dSOctavian Purdila 		unsigned int flen = min(*len, plen);
13912870c43dSOctavian Purdila 
13922870c43dSOctavian Purdila 		/* the linear region may spread across several pages  */
13932870c43dSOctavian Purdila 		flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
13942870c43dSOctavian Purdila 
13952870c43dSOctavian Purdila 		if (spd_fill_page(spd, page, flen, poff, skb))
13962870c43dSOctavian Purdila 			return 1;
13972870c43dSOctavian Purdila 
13982870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, flen);
13992870c43dSOctavian Purdila 		*len -= flen;
14002870c43dSOctavian Purdila 
14012870c43dSOctavian Purdila 	} while (*len && plen);
14022870c43dSOctavian Purdila 
14032870c43dSOctavian Purdila 	return 0;
1404db43a282SOctavian Purdila }
14059c55e01cSJens Axboe 
14069c55e01cSJens Axboe /*
14072870c43dSOctavian Purdila  * Map linear and fragment data from the skb to spd. It reports failure if the
14082870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
14099c55e01cSJens Axboe  */
14107b1c65faSHarvey Harrison static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
14112870c43dSOctavian Purdila 		      unsigned int *len,
14122870c43dSOctavian Purdila 		      struct splice_pipe_desc *spd)
14132870c43dSOctavian Purdila {
14142870c43dSOctavian Purdila 	int seg;
14159c55e01cSJens Axboe 
14169c55e01cSJens Axboe 	/*
14172870c43dSOctavian Purdila 	 * map the linear part
14189c55e01cSJens Axboe 	 */
14192870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
14202870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
14212870c43dSOctavian Purdila 			     skb_headlen(skb),
14222870c43dSOctavian Purdila 			     offset, len, skb, spd))
14232870c43dSOctavian Purdila 		return 1;
14249c55e01cSJens Axboe 
14259c55e01cSJens Axboe 	/*
14269c55e01cSJens Axboe 	 * then map the fragments
14279c55e01cSJens Axboe 	 */
14289c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
14299c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
14309c55e01cSJens Axboe 
14312870c43dSOctavian Purdila 		if (__splice_segment(f->page, f->page_offset, f->size,
14322870c43dSOctavian Purdila 				     offset, len, skb, spd))
14332870c43dSOctavian Purdila 			return 1;
14349c55e01cSJens Axboe 	}
14359c55e01cSJens Axboe 
14369c55e01cSJens Axboe 	return 0;
14379c55e01cSJens Axboe }
14389c55e01cSJens Axboe 
14399c55e01cSJens Axboe /*
14409c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
14419c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
14429c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
14439c55e01cSJens Axboe  * handle that cleanly.
14449c55e01cSJens Axboe  */
14459c55e01cSJens Axboe int skb_splice_bits(struct sk_buff *__skb, unsigned int offset,
14469c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
14479c55e01cSJens Axboe 		    unsigned int flags)
14489c55e01cSJens Axboe {
14499c55e01cSJens Axboe 	struct partial_page partial[PIPE_BUFFERS];
14509c55e01cSJens Axboe 	struct page *pages[PIPE_BUFFERS];
14519c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
14529c55e01cSJens Axboe 		.pages = pages,
14539c55e01cSJens Axboe 		.partial = partial,
14549c55e01cSJens Axboe 		.flags = flags,
14559c55e01cSJens Axboe 		.ops = &sock_pipe_buf_ops,
14569c55e01cSJens Axboe 		.spd_release = sock_spd_release,
14579c55e01cSJens Axboe 	};
14589c55e01cSJens Axboe 	struct sk_buff *skb;
14599c55e01cSJens Axboe 
14609c55e01cSJens Axboe 	/*
14619c55e01cSJens Axboe 	 * I'd love to avoid the clone here, but tcp_read_sock()
14629c55e01cSJens Axboe 	 * ignores reference counts and unconditonally kills the sk_buff
14639c55e01cSJens Axboe 	 * on return from the actor.
14649c55e01cSJens Axboe 	 */
14659c55e01cSJens Axboe 	skb = skb_clone(__skb, GFP_KERNEL);
14669c55e01cSJens Axboe 	if (unlikely(!skb))
14679c55e01cSJens Axboe 		return -ENOMEM;
14689c55e01cSJens Axboe 
14699c55e01cSJens Axboe 	/*
14709c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
14719c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
14729c55e01cSJens Axboe 	 */
14739c55e01cSJens Axboe 	if (__skb_splice_bits(skb, &offset, &tlen, &spd))
14749c55e01cSJens Axboe 		goto done;
14759c55e01cSJens Axboe 	else if (!tlen)
14769c55e01cSJens Axboe 		goto done;
14779c55e01cSJens Axboe 
14789c55e01cSJens Axboe 	/*
14799c55e01cSJens Axboe 	 * now see if we have a frag_list to map
14809c55e01cSJens Axboe 	 */
14819c55e01cSJens Axboe 	if (skb_shinfo(skb)->frag_list) {
14829c55e01cSJens Axboe 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
14839c55e01cSJens Axboe 
14849c55e01cSJens Axboe 		for (; list && tlen; list = list->next) {
14859c55e01cSJens Axboe 			if (__skb_splice_bits(list, &offset, &tlen, &spd))
14869c55e01cSJens Axboe 				break;
14879c55e01cSJens Axboe 		}
14889c55e01cSJens Axboe 	}
14899c55e01cSJens Axboe 
14909c55e01cSJens Axboe done:
14919c55e01cSJens Axboe 	/*
14929c55e01cSJens Axboe 	 * drop our reference to the clone, the pipe consumption will
14939c55e01cSJens Axboe 	 * drop the rest.
14949c55e01cSJens Axboe 	 */
14959c55e01cSJens Axboe 	kfree_skb(skb);
14969c55e01cSJens Axboe 
14979c55e01cSJens Axboe 	if (spd.nr_pages) {
14989c55e01cSJens Axboe 		int ret;
1499293ad604SOctavian Purdila 		struct sock *sk = __skb->sk;
15009c55e01cSJens Axboe 
15019c55e01cSJens Axboe 		/*
15029c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
15039c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
15049c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
15059c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
15069c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
15079c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
15089c55e01cSJens Axboe 		 * and networking will grab the socket lock.
15099c55e01cSJens Axboe 		 */
1510293ad604SOctavian Purdila 		release_sock(sk);
15119c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1512293ad604SOctavian Purdila 		lock_sock(sk);
15139c55e01cSJens Axboe 		return ret;
15149c55e01cSJens Axboe 	}
15159c55e01cSJens Axboe 
15169c55e01cSJens Axboe 	return 0;
15179c55e01cSJens Axboe }
15189c55e01cSJens Axboe 
1519357b40a1SHerbert Xu /**
1520357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1521357b40a1SHerbert Xu  *	@skb: destination buffer
1522357b40a1SHerbert Xu  *	@offset: offset in destination
1523357b40a1SHerbert Xu  *	@from: source buffer
1524357b40a1SHerbert Xu  *	@len: number of bytes to copy
1525357b40a1SHerbert Xu  *
1526357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1527357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1528357b40a1SHerbert Xu  *	traversing fragment lists and such.
1529357b40a1SHerbert Xu  */
1530357b40a1SHerbert Xu 
15310c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1532357b40a1SHerbert Xu {
1533357b40a1SHerbert Xu 	int i, copy;
15341a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1535357b40a1SHerbert Xu 
1536357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1537357b40a1SHerbert Xu 		goto fault;
1538357b40a1SHerbert Xu 
15391a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1540357b40a1SHerbert Xu 		if (copy > len)
1541357b40a1SHerbert Xu 			copy = len;
154227d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1543357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1544357b40a1SHerbert Xu 			return 0;
1545357b40a1SHerbert Xu 		offset += copy;
1546357b40a1SHerbert Xu 		from += copy;
1547357b40a1SHerbert Xu 	}
1548357b40a1SHerbert Xu 
1549357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1550357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
15511a028e50SDavid S. Miller 		int end;
1552357b40a1SHerbert Xu 
1553547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
15541a028e50SDavid S. Miller 
15551a028e50SDavid S. Miller 		end = start + frag->size;
1556357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1557357b40a1SHerbert Xu 			u8 *vaddr;
1558357b40a1SHerbert Xu 
1559357b40a1SHerbert Xu 			if (copy > len)
1560357b40a1SHerbert Xu 				copy = len;
1561357b40a1SHerbert Xu 
1562357b40a1SHerbert Xu 			vaddr = kmap_skb_frag(frag);
15631a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
15641a028e50SDavid S. Miller 			       from, copy);
1565357b40a1SHerbert Xu 			kunmap_skb_frag(vaddr);
1566357b40a1SHerbert Xu 
1567357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1568357b40a1SHerbert Xu 				return 0;
1569357b40a1SHerbert Xu 			offset += copy;
1570357b40a1SHerbert Xu 			from += copy;
1571357b40a1SHerbert Xu 		}
15721a028e50SDavid S. Miller 		start = end;
1573357b40a1SHerbert Xu 	}
1574357b40a1SHerbert Xu 
1575357b40a1SHerbert Xu 	if (skb_shinfo(skb)->frag_list) {
1576357b40a1SHerbert Xu 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1577357b40a1SHerbert Xu 
1578357b40a1SHerbert Xu 		for (; list; list = list->next) {
15791a028e50SDavid S. Miller 			int end;
1580357b40a1SHerbert Xu 
1581547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
15821a028e50SDavid S. Miller 
15831a028e50SDavid S. Miller 			end = start + list->len;
1584357b40a1SHerbert Xu 			if ((copy = end - offset) > 0) {
1585357b40a1SHerbert Xu 				if (copy > len)
1586357b40a1SHerbert Xu 					copy = len;
15871a028e50SDavid S. Miller 				if (skb_store_bits(list, offset - start,
15881a028e50SDavid S. Miller 						   from, copy))
1589357b40a1SHerbert Xu 					goto fault;
1590357b40a1SHerbert Xu 				if ((len -= copy) == 0)
1591357b40a1SHerbert Xu 					return 0;
1592357b40a1SHerbert Xu 				offset += copy;
1593357b40a1SHerbert Xu 				from += copy;
1594357b40a1SHerbert Xu 			}
15951a028e50SDavid S. Miller 			start = end;
1596357b40a1SHerbert Xu 		}
1597357b40a1SHerbert Xu 	}
1598357b40a1SHerbert Xu 	if (!len)
1599357b40a1SHerbert Xu 		return 0;
1600357b40a1SHerbert Xu 
1601357b40a1SHerbert Xu fault:
1602357b40a1SHerbert Xu 	return -EFAULT;
1603357b40a1SHerbert Xu }
1604357b40a1SHerbert Xu 
1605357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1606357b40a1SHerbert Xu 
16071da177e4SLinus Torvalds /* Checksum skb data. */
16081da177e4SLinus Torvalds 
16092bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset,
16102bbbc868SAl Viro 			  int len, __wsum csum)
16111da177e4SLinus Torvalds {
16121a028e50SDavid S. Miller 	int start = skb_headlen(skb);
16131a028e50SDavid S. Miller 	int i, copy = start - offset;
16141da177e4SLinus Torvalds 	int pos = 0;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	/* Checksum header. */
16171da177e4SLinus Torvalds 	if (copy > 0) {
16181da177e4SLinus Torvalds 		if (copy > len)
16191da177e4SLinus Torvalds 			copy = len;
16201da177e4SLinus Torvalds 		csum = csum_partial(skb->data + offset, copy, csum);
16211da177e4SLinus Torvalds 		if ((len -= copy) == 0)
16221da177e4SLinus Torvalds 			return csum;
16231da177e4SLinus Torvalds 		offset += copy;
16241da177e4SLinus Torvalds 		pos	= copy;
16251da177e4SLinus Torvalds 	}
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16281a028e50SDavid S. Miller 		int end;
16291da177e4SLinus Torvalds 
1630547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16311a028e50SDavid S. Miller 
16321a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
16331da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
163444bb9363SAl Viro 			__wsum csum2;
16351da177e4SLinus Torvalds 			u8 *vaddr;
16361da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
16371da177e4SLinus Torvalds 
16381da177e4SLinus Torvalds 			if (copy > len)
16391da177e4SLinus Torvalds 				copy = len;
16401da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
16411a028e50SDavid S. Miller 			csum2 = csum_partial(vaddr + frag->page_offset +
16421a028e50SDavid S. Miller 					     offset - start, copy, 0);
16431da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
16441da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
16451da177e4SLinus Torvalds 			if (!(len -= copy))
16461da177e4SLinus Torvalds 				return csum;
16471da177e4SLinus Torvalds 			offset += copy;
16481da177e4SLinus Torvalds 			pos    += copy;
16491da177e4SLinus Torvalds 		}
16501a028e50SDavid S. Miller 		start = end;
16511da177e4SLinus Torvalds 	}
16521da177e4SLinus Torvalds 
16531da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
16541da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
16551da177e4SLinus Torvalds 
16561da177e4SLinus Torvalds 		for (; list; list = list->next) {
16571a028e50SDavid S. Miller 			int end;
16581da177e4SLinus Torvalds 
1659547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
16601a028e50SDavid S. Miller 
16611a028e50SDavid S. Miller 			end = start + list->len;
16621da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
16635f92a738SAl Viro 				__wsum csum2;
16641da177e4SLinus Torvalds 				if (copy > len)
16651da177e4SLinus Torvalds 					copy = len;
16661a028e50SDavid S. Miller 				csum2 = skb_checksum(list, offset - start,
16671a028e50SDavid S. Miller 						     copy, 0);
16681da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
16691da177e4SLinus Torvalds 				if ((len -= copy) == 0)
16701da177e4SLinus Torvalds 					return csum;
16711da177e4SLinus Torvalds 				offset += copy;
16721da177e4SLinus Torvalds 				pos    += copy;
16731da177e4SLinus Torvalds 			}
16741a028e50SDavid S. Miller 			start = end;
16751da177e4SLinus Torvalds 		}
16761da177e4SLinus Torvalds 	}
167709a62660SKris Katterjohn 	BUG_ON(len);
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	return csum;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds /* Both of above in one bottle. */
16831da177e4SLinus Torvalds 
168481d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
168581d77662SAl Viro 				    u8 *to, int len, __wsum csum)
16861da177e4SLinus Torvalds {
16871a028e50SDavid S. Miller 	int start = skb_headlen(skb);
16881a028e50SDavid S. Miller 	int i, copy = start - offset;
16891da177e4SLinus Torvalds 	int pos = 0;
16901da177e4SLinus Torvalds 
16911da177e4SLinus Torvalds 	/* Copy header. */
16921da177e4SLinus Torvalds 	if (copy > 0) {
16931da177e4SLinus Torvalds 		if (copy > len)
16941da177e4SLinus Torvalds 			copy = len;
16951da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
16961da177e4SLinus Torvalds 						 copy, csum);
16971da177e4SLinus Torvalds 		if ((len -= copy) == 0)
16981da177e4SLinus Torvalds 			return csum;
16991da177e4SLinus Torvalds 		offset += copy;
17001da177e4SLinus Torvalds 		to     += copy;
17011da177e4SLinus Torvalds 		pos	= copy;
17021da177e4SLinus Torvalds 	}
17031da177e4SLinus Torvalds 
17041da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
17051a028e50SDavid S. Miller 		int end;
17061da177e4SLinus Torvalds 
1707547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17081a028e50SDavid S. Miller 
17091a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
17101da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17115084205fSAl Viro 			__wsum csum2;
17121da177e4SLinus Torvalds 			u8 *vaddr;
17131da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds 			if (copy > len)
17161da177e4SLinus Torvalds 				copy = len;
17171da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
17181da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
17191a028e50SDavid S. Miller 							  frag->page_offset +
17201a028e50SDavid S. Miller 							  offset - start, to,
17211a028e50SDavid S. Miller 							  copy, 0);
17221da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
17231da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
17241da177e4SLinus Torvalds 			if (!(len -= copy))
17251da177e4SLinus Torvalds 				return csum;
17261da177e4SLinus Torvalds 			offset += copy;
17271da177e4SLinus Torvalds 			to     += copy;
17281da177e4SLinus Torvalds 			pos    += copy;
17291da177e4SLinus Torvalds 		}
17301a028e50SDavid S. Miller 		start = end;
17311da177e4SLinus Torvalds 	}
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
17341da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
17351da177e4SLinus Torvalds 
17361da177e4SLinus Torvalds 		for (; list; list = list->next) {
173781d77662SAl Viro 			__wsum csum2;
17381a028e50SDavid S. Miller 			int end;
17391da177e4SLinus Torvalds 
1740547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
17411a028e50SDavid S. Miller 
17421a028e50SDavid S. Miller 			end = start + list->len;
17431da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
17441da177e4SLinus Torvalds 				if (copy > len)
17451da177e4SLinus Torvalds 					copy = len;
17461a028e50SDavid S. Miller 				csum2 = skb_copy_and_csum_bits(list,
17471a028e50SDavid S. Miller 							       offset - start,
17481da177e4SLinus Torvalds 							       to, copy, 0);
17491da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
17501da177e4SLinus Torvalds 				if ((len -= copy) == 0)
17511da177e4SLinus Torvalds 					return csum;
17521da177e4SLinus Torvalds 				offset += copy;
17531da177e4SLinus Torvalds 				to     += copy;
17541da177e4SLinus Torvalds 				pos    += copy;
17551da177e4SLinus Torvalds 			}
17561a028e50SDavid S. Miller 			start = end;
17571da177e4SLinus Torvalds 		}
17581da177e4SLinus Torvalds 	}
175909a62660SKris Katterjohn 	BUG_ON(len);
17601da177e4SLinus Torvalds 	return csum;
17611da177e4SLinus Torvalds }
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
17641da177e4SLinus Torvalds {
1765d3bc23e7SAl Viro 	__wsum csum;
17661da177e4SLinus Torvalds 	long csstart;
17671da177e4SLinus Torvalds 
176884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1769663ead3bSHerbert Xu 		csstart = skb->csum_start - skb_headroom(skb);
17701da177e4SLinus Torvalds 	else
17711da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
17721da177e4SLinus Torvalds 
177309a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
17741da177e4SLinus Torvalds 
1775d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	csum = 0;
17781da177e4SLinus Torvalds 	if (csstart != skb->len)
17791da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
17801da177e4SLinus Torvalds 					      skb->len - csstart, 0);
17811da177e4SLinus Torvalds 
178284fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1783ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
17841da177e4SLinus Torvalds 
1785d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
17861da177e4SLinus Torvalds 	}
17871da177e4SLinus Torvalds }
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds /**
17901da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
17911da177e4SLinus Torvalds  *	@list: list to dequeue from
17921da177e4SLinus Torvalds  *
17931da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
17941da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
17951da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
17961da177e4SLinus Torvalds  */
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
17991da177e4SLinus Torvalds {
18001da177e4SLinus Torvalds 	unsigned long flags;
18011da177e4SLinus Torvalds 	struct sk_buff *result;
18021da177e4SLinus Torvalds 
18031da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18041da177e4SLinus Torvalds 	result = __skb_dequeue(list);
18051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18061da177e4SLinus Torvalds 	return result;
18071da177e4SLinus Torvalds }
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds /**
18101da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
18111da177e4SLinus Torvalds  *	@list: list to dequeue from
18121da177e4SLinus Torvalds  *
18131da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
18141da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
18151da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
18161da177e4SLinus Torvalds  */
18171da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
18181da177e4SLinus Torvalds {
18191da177e4SLinus Torvalds 	unsigned long flags;
18201da177e4SLinus Torvalds 	struct sk_buff *result;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18231da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
18241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18251da177e4SLinus Torvalds 	return result;
18261da177e4SLinus Torvalds }
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds /**
18291da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
18301da177e4SLinus Torvalds  *	@list: list to empty
18311da177e4SLinus Torvalds  *
18321da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
18331da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
18341da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
18351da177e4SLinus Torvalds  */
18361da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
18371da177e4SLinus Torvalds {
18381da177e4SLinus Torvalds 	struct sk_buff *skb;
18391da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
18401da177e4SLinus Torvalds 		kfree_skb(skb);
18411da177e4SLinus Torvalds }
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds /**
18441da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
18451da177e4SLinus Torvalds  *	@list: list to use
18461da177e4SLinus Torvalds  *	@newsk: buffer to queue
18471da177e4SLinus Torvalds  *
18481da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
18491da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
18501da177e4SLinus Torvalds  *	safely.
18511da177e4SLinus Torvalds  *
18521da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
18531da177e4SLinus Torvalds  */
18541da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
18551da177e4SLinus Torvalds {
18561da177e4SLinus Torvalds 	unsigned long flags;
18571da177e4SLinus Torvalds 
18581da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18591da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
18601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18611da177e4SLinus Torvalds }
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds /**
18641da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
18651da177e4SLinus Torvalds  *	@list: list to use
18661da177e4SLinus Torvalds  *	@newsk: buffer to queue
18671da177e4SLinus Torvalds  *
18681da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
18691da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
18701da177e4SLinus Torvalds  *	safely.
18711da177e4SLinus Torvalds  *
18721da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
18731da177e4SLinus Torvalds  */
18741da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
18751da177e4SLinus Torvalds {
18761da177e4SLinus Torvalds 	unsigned long flags;
18771da177e4SLinus Torvalds 
18781da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18791da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
18801da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18811da177e4SLinus Torvalds }
18828728b834SDavid S. Miller 
18831da177e4SLinus Torvalds /**
18841da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
18851da177e4SLinus Torvalds  *	@skb: buffer to remove
18868728b834SDavid S. Miller  *	@list: list to use
18871da177e4SLinus Torvalds  *
18888728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
18898728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
18901da177e4SLinus Torvalds  *
18918728b834SDavid S. Miller  *	You must know what list the SKB is on.
18921da177e4SLinus Torvalds  */
18938728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
18941da177e4SLinus Torvalds {
18951da177e4SLinus Torvalds 	unsigned long flags;
18961da177e4SLinus Torvalds 
18971da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18988728b834SDavid S. Miller 	__skb_unlink(skb, list);
18991da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19001da177e4SLinus Torvalds }
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds /**
19031da177e4SLinus Torvalds  *	skb_append	-	append a buffer
19041da177e4SLinus Torvalds  *	@old: buffer to insert after
19051da177e4SLinus Torvalds  *	@newsk: buffer to insert
19068728b834SDavid S. Miller  *	@list: list to use
19071da177e4SLinus Torvalds  *
19081da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
19091da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
19101da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19111da177e4SLinus Torvalds  */
19128728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
19131da177e4SLinus Torvalds {
19141da177e4SLinus Torvalds 	unsigned long flags;
19151da177e4SLinus Torvalds 
19168728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
19177de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
19188728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds /**
19231da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
19241da177e4SLinus Torvalds  *	@old: buffer to insert before
19251da177e4SLinus Torvalds  *	@newsk: buffer to insert
19268728b834SDavid S. Miller  *	@list: list to use
19271da177e4SLinus Torvalds  *
19288728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
19298728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
19308728b834SDavid S. Miller  *	calls.
19318728b834SDavid S. Miller  *
19321da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19331da177e4SLinus Torvalds  */
19348728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
19351da177e4SLinus Torvalds {
19361da177e4SLinus Torvalds 	unsigned long flags;
19371da177e4SLinus Torvalds 
19388728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
19398728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
19408728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
19441da177e4SLinus Torvalds 					   struct sk_buff* skb1,
19451da177e4SLinus Torvalds 					   const u32 len, const int pos)
19461da177e4SLinus Torvalds {
19471da177e4SLinus Torvalds 	int i;
19481da177e4SLinus Torvalds 
1949d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1950d626f62bSArnaldo Carvalho de Melo 					 pos - len);
19511da177e4SLinus Torvalds 	/* And move data appendix as is. */
19521da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
19531da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
19561da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
19571da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
19581da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
19591da177e4SLinus Torvalds 	skb->data_len		   = 0;
19601da177e4SLinus Torvalds 	skb->len		   = len;
196127a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
19621da177e4SLinus Torvalds }
19631da177e4SLinus Torvalds 
19641da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
19651da177e4SLinus Torvalds 				       struct sk_buff* skb1,
19661da177e4SLinus Torvalds 				       const u32 len, int pos)
19671da177e4SLinus Torvalds {
19681da177e4SLinus Torvalds 	int i, k = 0;
19691da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
19721da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
19731da177e4SLinus Torvalds 	skb->len		  = len;
19741da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
19771da177e4SLinus Torvalds 		int size = skb_shinfo(skb)->frags[i].size;
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 		if (pos + size > len) {
19801da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 			if (pos < len) {
19831da177e4SLinus Torvalds 				/* Split frag.
19841da177e4SLinus Torvalds 				 * We have two variants in this case:
19851da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
19861da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
19871da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
19881da177e4SLinus Torvalds 				 *    where splitting is expensive.
19891da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
19901da177e4SLinus Torvalds 				 */
19911da177e4SLinus Torvalds 				get_page(skb_shinfo(skb)->frags[i].page);
19921da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
19931da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].size -= len - pos;
19941da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[i].size	= len - pos;
19951da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
19961da177e4SLinus Torvalds 			}
19971da177e4SLinus Torvalds 			k++;
19981da177e4SLinus Torvalds 		} else
19991da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
20001da177e4SLinus Torvalds 		pos += size;
20011da177e4SLinus Torvalds 	}
20021da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds 
20051da177e4SLinus Torvalds /**
20061da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
20071da177e4SLinus Torvalds  * @skb: the buffer to split
20081da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
20091da177e4SLinus Torvalds  * @len: new length for skb
20101da177e4SLinus Torvalds  */
20111da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
20121da177e4SLinus Torvalds {
20131da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
20161da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
20171da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
20181da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20219f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
20229f782db3SIlpo Järvinen  *
20239f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
20249f782db3SIlpo Järvinen  */
2025832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2026832d11c5SIlpo Järvinen {
20270ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2028832d11c5SIlpo Järvinen }
2029832d11c5SIlpo Järvinen 
2030832d11c5SIlpo Järvinen /**
2031832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2032832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2033832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2034832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2035832d11c5SIlpo Järvinen  *
2036832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
2037832d11c5SIlpo Järvinen  * the length of the skb, from tgt to skb. Returns number bytes shifted.
2038832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2039832d11c5SIlpo Järvinen  *
2040832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2041832d11c5SIlpo Järvinen  *
2042832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2043832d11c5SIlpo Järvinen  * to have non-paged data as well.
2044832d11c5SIlpo Järvinen  *
2045832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2046832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2047832d11c5SIlpo Järvinen  */
2048832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2049832d11c5SIlpo Järvinen {
2050832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2051832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2052832d11c5SIlpo Järvinen 
2053832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2054832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2055832d11c5SIlpo Järvinen 
2056832d11c5SIlpo Järvinen 	todo = shiftlen;
2057832d11c5SIlpo Järvinen 	from = 0;
2058832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2059832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2060832d11c5SIlpo Järvinen 
2061832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2062832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2063832d11c5SIlpo Järvinen 	 */
2064832d11c5SIlpo Järvinen 	if (!to ||
2065832d11c5SIlpo Järvinen 	    !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2066832d11c5SIlpo Järvinen 		merge = -1;
2067832d11c5SIlpo Järvinen 	} else {
2068832d11c5SIlpo Järvinen 		merge = to - 1;
2069832d11c5SIlpo Järvinen 
2070832d11c5SIlpo Järvinen 		todo -= fragfrom->size;
2071832d11c5SIlpo Järvinen 		if (todo < 0) {
2072832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2073832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2074832d11c5SIlpo Järvinen 				return 0;
2075832d11c5SIlpo Järvinen 
20769f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
20779f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2078832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2079832d11c5SIlpo Järvinen 
2080832d11c5SIlpo Järvinen 			fragto->size += shiftlen;
2081832d11c5SIlpo Järvinen 			fragfrom->size -= shiftlen;
2082832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2083832d11c5SIlpo Järvinen 
2084832d11c5SIlpo Järvinen 			goto onlymerged;
2085832d11c5SIlpo Järvinen 		}
2086832d11c5SIlpo Järvinen 
2087832d11c5SIlpo Järvinen 		from++;
2088832d11c5SIlpo Järvinen 	}
2089832d11c5SIlpo Järvinen 
2090832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2091832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2092832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2093832d11c5SIlpo Järvinen 		return 0;
2094832d11c5SIlpo Järvinen 
2095832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2096832d11c5SIlpo Järvinen 		return 0;
2097832d11c5SIlpo Järvinen 
2098832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2099832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2100832d11c5SIlpo Järvinen 			return 0;
2101832d11c5SIlpo Järvinen 
2102832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2103832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2104832d11c5SIlpo Järvinen 
2105832d11c5SIlpo Järvinen 		if (todo >= fragfrom->size) {
2106832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
2107832d11c5SIlpo Järvinen 			todo -= fragfrom->size;
2108832d11c5SIlpo Järvinen 			from++;
2109832d11c5SIlpo Järvinen 			to++;
2110832d11c5SIlpo Järvinen 
2111832d11c5SIlpo Järvinen 		} else {
2112832d11c5SIlpo Järvinen 			get_page(fragfrom->page);
2113832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2114832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
2115832d11c5SIlpo Järvinen 			fragto->size = todo;
2116832d11c5SIlpo Järvinen 
2117832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
2118832d11c5SIlpo Järvinen 			fragfrom->size -= todo;
2119832d11c5SIlpo Järvinen 			todo = 0;
2120832d11c5SIlpo Järvinen 
2121832d11c5SIlpo Järvinen 			to++;
2122832d11c5SIlpo Järvinen 			break;
2123832d11c5SIlpo Järvinen 		}
2124832d11c5SIlpo Järvinen 	}
2125832d11c5SIlpo Järvinen 
2126832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2127832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2128832d11c5SIlpo Järvinen 
2129832d11c5SIlpo Järvinen 	if (merge >= 0) {
2130832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2131832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2132832d11c5SIlpo Järvinen 
2133832d11c5SIlpo Järvinen 		fragto->size += fragfrom->size;
2134832d11c5SIlpo Järvinen 		put_page(fragfrom->page);
2135832d11c5SIlpo Järvinen 	}
2136832d11c5SIlpo Järvinen 
2137832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2138832d11c5SIlpo Järvinen 	to = 0;
2139832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2140832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2141832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2142832d11c5SIlpo Järvinen 
2143832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2144832d11c5SIlpo Järvinen 
2145832d11c5SIlpo Järvinen onlymerged:
2146832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2147832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2148832d11c5SIlpo Järvinen 	 */
2149832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2150832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2151832d11c5SIlpo Järvinen 
2152832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2153832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2154832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2155832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2156832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2157832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2158832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2159832d11c5SIlpo Järvinen 
2160832d11c5SIlpo Järvinen 	return shiftlen;
2161832d11c5SIlpo Järvinen }
2162832d11c5SIlpo Järvinen 
2163677e90edSThomas Graf /**
2164677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2165677e90edSThomas Graf  * @skb: the buffer to read
2166677e90edSThomas Graf  * @from: lower offset of data to be read
2167677e90edSThomas Graf  * @to: upper offset of data to be read
2168677e90edSThomas Graf  * @st: state variable
2169677e90edSThomas Graf  *
2170677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2171677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2172677e90edSThomas Graf  */
2173677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2174677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2175677e90edSThomas Graf {
2176677e90edSThomas Graf 	st->lower_offset = from;
2177677e90edSThomas Graf 	st->upper_offset = to;
2178677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2179677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2180677e90edSThomas Graf 	st->frag_data = NULL;
2181677e90edSThomas Graf }
2182677e90edSThomas Graf 
2183677e90edSThomas Graf /**
2184677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2185677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2186677e90edSThomas Graf  * @data: destination pointer for data to be returned
2187677e90edSThomas Graf  * @st: state variable
2188677e90edSThomas Graf  *
2189677e90edSThomas Graf  * Reads a block of skb data at &consumed relative to the
2190677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2191677e90edSThomas Graf  * the head of the data block to &data and returns the length
2192677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2193677e90edSThomas Graf  * offset has been reached.
2194677e90edSThomas Graf  *
2195677e90edSThomas Graf  * The caller is not required to consume all of the data
2196677e90edSThomas Graf  * returned, i.e. &consumed is typically set to the number
2197677e90edSThomas Graf  * of bytes already consumed and the next call to
2198677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2199677e90edSThomas Graf  *
2200bc2cda1eSRandy Dunlap  * Note 1: The size of each block of data returned can be arbitary,
2201677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
2202677e90edSThomas Graf  *       reads of potentially non linear data.
2203677e90edSThomas Graf  *
2204bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2205677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2206677e90edSThomas Graf  *       a stack for this purpose.
2207677e90edSThomas Graf  */
2208677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2209677e90edSThomas Graf 			  struct skb_seq_state *st)
2210677e90edSThomas Graf {
2211677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2212677e90edSThomas Graf 	skb_frag_t *frag;
2213677e90edSThomas Graf 
2214677e90edSThomas Graf 	if (unlikely(abs_offset >= st->upper_offset))
2215677e90edSThomas Graf 		return 0;
2216677e90edSThomas Graf 
2217677e90edSThomas Graf next_skb:
2218677e90edSThomas Graf 	block_limit = skb_headlen(st->cur_skb);
2219677e90edSThomas Graf 
2220677e90edSThomas Graf 	if (abs_offset < block_limit) {
2221677e90edSThomas Graf 		*data = st->cur_skb->data + abs_offset;
2222677e90edSThomas Graf 		return block_limit - abs_offset;
2223677e90edSThomas Graf 	}
2224677e90edSThomas Graf 
2225677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2226677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2227677e90edSThomas Graf 
2228677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2229677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2230677e90edSThomas Graf 		block_limit = frag->size + st->stepped_offset;
2231677e90edSThomas Graf 
2232677e90edSThomas Graf 		if (abs_offset < block_limit) {
2233677e90edSThomas Graf 			if (!st->frag_data)
2234677e90edSThomas Graf 				st->frag_data = kmap_skb_frag(frag);
2235677e90edSThomas Graf 
2236677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2237677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2238677e90edSThomas Graf 
2239677e90edSThomas Graf 			return block_limit - abs_offset;
2240677e90edSThomas Graf 		}
2241677e90edSThomas Graf 
2242677e90edSThomas Graf 		if (st->frag_data) {
2243677e90edSThomas Graf 			kunmap_skb_frag(st->frag_data);
2244677e90edSThomas Graf 			st->frag_data = NULL;
2245677e90edSThomas Graf 		}
2246677e90edSThomas Graf 
2247677e90edSThomas Graf 		st->frag_idx++;
2248677e90edSThomas Graf 		st->stepped_offset += frag->size;
2249677e90edSThomas Graf 	}
2250677e90edSThomas Graf 
22515b5a60daSOlaf Kirch 	if (st->frag_data) {
22525b5a60daSOlaf Kirch 		kunmap_skb_frag(st->frag_data);
22535b5a60daSOlaf Kirch 		st->frag_data = NULL;
22545b5a60daSOlaf Kirch 	}
22555b5a60daSOlaf Kirch 
2256677e90edSThomas Graf 	if (st->cur_skb->next) {
2257677e90edSThomas Graf 		st->cur_skb = st->cur_skb->next;
2258677e90edSThomas Graf 		st->frag_idx = 0;
2259677e90edSThomas Graf 		goto next_skb;
2260677e90edSThomas Graf 	} else if (st->root_skb == st->cur_skb &&
2261677e90edSThomas Graf 		   skb_shinfo(st->root_skb)->frag_list) {
2262677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
2263677e90edSThomas Graf 		goto next_skb;
2264677e90edSThomas Graf 	}
2265677e90edSThomas Graf 
2266677e90edSThomas Graf 	return 0;
2267677e90edSThomas Graf }
2268677e90edSThomas Graf 
2269677e90edSThomas Graf /**
2270677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2271677e90edSThomas Graf  * @st: state variable
2272677e90edSThomas Graf  *
2273677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2274677e90edSThomas Graf  * returned 0.
2275677e90edSThomas Graf  */
2276677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2277677e90edSThomas Graf {
2278677e90edSThomas Graf 	if (st->frag_data)
2279677e90edSThomas Graf 		kunmap_skb_frag(st->frag_data);
2280677e90edSThomas Graf }
2281677e90edSThomas Graf 
22823fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
22833fc7e8a6SThomas Graf 
22843fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
22853fc7e8a6SThomas Graf 					  struct ts_config *conf,
22863fc7e8a6SThomas Graf 					  struct ts_state *state)
22873fc7e8a6SThomas Graf {
22883fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
22893fc7e8a6SThomas Graf }
22903fc7e8a6SThomas Graf 
22913fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
22923fc7e8a6SThomas Graf {
22933fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
22943fc7e8a6SThomas Graf }
22953fc7e8a6SThomas Graf 
22963fc7e8a6SThomas Graf /**
22973fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
22983fc7e8a6SThomas Graf  * @skb: the buffer to look in
22993fc7e8a6SThomas Graf  * @from: search offset
23003fc7e8a6SThomas Graf  * @to: search limit
23013fc7e8a6SThomas Graf  * @config: textsearch configuration
23023fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
23033fc7e8a6SThomas Graf  *
23043fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
23053fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
23063fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
23073fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
23083fc7e8a6SThomas Graf  */
23093fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
23103fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
23113fc7e8a6SThomas Graf 			   struct ts_state *state)
23123fc7e8a6SThomas Graf {
2313f72b948dSPhil Oester 	unsigned int ret;
2314f72b948dSPhil Oester 
23153fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
23163fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
23173fc7e8a6SThomas Graf 
23183fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
23193fc7e8a6SThomas Graf 
2320f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2321f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
23223fc7e8a6SThomas Graf }
23233fc7e8a6SThomas Graf 
2324e89e9cf5SAnanda Raju /**
2325e89e9cf5SAnanda Raju  * skb_append_datato_frags: - append the user data to a skb
2326e89e9cf5SAnanda Raju  * @sk: sock  structure
2327e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
2328e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2329e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2330e89e9cf5SAnanda Raju  * @length: length of the iov message
2331e89e9cf5SAnanda Raju  *
2332e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2333e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2334e89e9cf5SAnanda Raju  */
2335e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2336dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2337e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2338e89e9cf5SAnanda Raju 			void *from, int length)
2339e89e9cf5SAnanda Raju {
2340e89e9cf5SAnanda Raju 	int frg_cnt = 0;
2341e89e9cf5SAnanda Raju 	skb_frag_t *frag = NULL;
2342e89e9cf5SAnanda Raju 	struct page *page = NULL;
2343e89e9cf5SAnanda Raju 	int copy, left;
2344e89e9cf5SAnanda Raju 	int offset = 0;
2345e89e9cf5SAnanda Raju 	int ret;
2346e89e9cf5SAnanda Raju 
2347e89e9cf5SAnanda Raju 	do {
2348e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2349e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2350e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2351e89e9cf5SAnanda Raju 			return -EFAULT;
2352e89e9cf5SAnanda Raju 
2353e89e9cf5SAnanda Raju 		/* allocate a new page for next frag */
2354e89e9cf5SAnanda Raju 		page = alloc_pages(sk->sk_allocation, 0);
2355e89e9cf5SAnanda Raju 
2356e89e9cf5SAnanda Raju 		/* If alloc_page fails just return failure and caller will
2357e89e9cf5SAnanda Raju 		 * free previous allocated pages by doing kfree_skb()
2358e89e9cf5SAnanda Raju 		 */
2359e89e9cf5SAnanda Raju 		if (page == NULL)
2360e89e9cf5SAnanda Raju 			return -ENOMEM;
2361e89e9cf5SAnanda Raju 
2362e89e9cf5SAnanda Raju 		/* initialize the next frag */
2363e89e9cf5SAnanda Raju 		sk->sk_sndmsg_page = page;
2364e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off = 0;
2365e89e9cf5SAnanda Raju 		skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2366e89e9cf5SAnanda Raju 		skb->truesize += PAGE_SIZE;
2367e89e9cf5SAnanda Raju 		atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2368e89e9cf5SAnanda Raju 
2369e89e9cf5SAnanda Raju 		/* get the new initialized frag */
2370e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2371e89e9cf5SAnanda Raju 		frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2372e89e9cf5SAnanda Raju 
2373e89e9cf5SAnanda Raju 		/* copy the user data to page */
2374e89e9cf5SAnanda Raju 		left = PAGE_SIZE - frag->page_offset;
2375e89e9cf5SAnanda Raju 		copy = (length > left)? left : length;
2376e89e9cf5SAnanda Raju 
2377e89e9cf5SAnanda Raju 		ret = getfrag(from, (page_address(frag->page) +
2378e89e9cf5SAnanda Raju 			    frag->page_offset + frag->size),
2379e89e9cf5SAnanda Raju 			    offset, copy, 0, skb);
2380e89e9cf5SAnanda Raju 		if (ret < 0)
2381e89e9cf5SAnanda Raju 			return -EFAULT;
2382e89e9cf5SAnanda Raju 
2383e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2384e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off += copy;
2385e89e9cf5SAnanda Raju 		frag->size += copy;
2386e89e9cf5SAnanda Raju 		skb->len += copy;
2387e89e9cf5SAnanda Raju 		skb->data_len += copy;
2388e89e9cf5SAnanda Raju 		offset += copy;
2389e89e9cf5SAnanda Raju 		length -= copy;
2390e89e9cf5SAnanda Raju 
2391e89e9cf5SAnanda Raju 	} while (length > 0);
2392e89e9cf5SAnanda Raju 
2393e89e9cf5SAnanda Raju 	return 0;
2394e89e9cf5SAnanda Raju }
2395e89e9cf5SAnanda Raju 
2396cbb042f9SHerbert Xu /**
2397cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2398cbb042f9SHerbert Xu  *	@skb: buffer to update
2399cbb042f9SHerbert Xu  *	@len: length of data pulled
2400cbb042f9SHerbert Xu  *
2401cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2402fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
240384fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
240484fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
240584fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2406cbb042f9SHerbert Xu  */
2407cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2408cbb042f9SHerbert Xu {
2409cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2410cbb042f9SHerbert Xu 	skb->len -= len;
2411cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2412cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2413cbb042f9SHerbert Xu 	return skb->data += len;
2414cbb042f9SHerbert Xu }
2415cbb042f9SHerbert Xu 
2416f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2417f94691acSArnaldo Carvalho de Melo 
2418f4c50d99SHerbert Xu /**
2419f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2420f4c50d99SHerbert Xu  *	@skb: buffer to segment
2421576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2422f4c50d99SHerbert Xu  *
2423f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
24244c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
24254c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2426f4c50d99SHerbert Xu  */
2427576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2428f4c50d99SHerbert Xu {
2429f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2430f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
243189319d38SHerbert Xu 	struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2432f4c50d99SHerbert Xu 	unsigned int mss = skb_shinfo(skb)->gso_size;
243398e399f8SArnaldo Carvalho de Melo 	unsigned int doffset = skb->data - skb_mac_header(skb);
2434f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2435f4c50d99SHerbert Xu 	unsigned int headroom;
2436f4c50d99SHerbert Xu 	unsigned int len;
2437576a30ebSHerbert Xu 	int sg = features & NETIF_F_SG;
2438f4c50d99SHerbert Xu 	int nfrags = skb_shinfo(skb)->nr_frags;
2439f4c50d99SHerbert Xu 	int err = -ENOMEM;
2440f4c50d99SHerbert Xu 	int i = 0;
2441f4c50d99SHerbert Xu 	int pos;
2442f4c50d99SHerbert Xu 
2443f4c50d99SHerbert Xu 	__skb_push(skb, doffset);
2444f4c50d99SHerbert Xu 	headroom = skb_headroom(skb);
2445f4c50d99SHerbert Xu 	pos = skb_headlen(skb);
2446f4c50d99SHerbert Xu 
2447f4c50d99SHerbert Xu 	do {
2448f4c50d99SHerbert Xu 		struct sk_buff *nskb;
2449f4c50d99SHerbert Xu 		skb_frag_t *frag;
2450c8884eddSHerbert Xu 		int hsize;
2451f4c50d99SHerbert Xu 		int size;
2452f4c50d99SHerbert Xu 
2453f4c50d99SHerbert Xu 		len = skb->len - offset;
2454f4c50d99SHerbert Xu 		if (len > mss)
2455f4c50d99SHerbert Xu 			len = mss;
2456f4c50d99SHerbert Xu 
2457f4c50d99SHerbert Xu 		hsize = skb_headlen(skb) - offset;
2458f4c50d99SHerbert Xu 		if (hsize < 0)
2459f4c50d99SHerbert Xu 			hsize = 0;
2460c8884eddSHerbert Xu 		if (hsize > len || !sg)
2461c8884eddSHerbert Xu 			hsize = len;
2462f4c50d99SHerbert Xu 
246389319d38SHerbert Xu 		if (!hsize && i >= nfrags) {
246489319d38SHerbert Xu 			BUG_ON(fskb->len != len);
246589319d38SHerbert Xu 
246689319d38SHerbert Xu 			pos += len;
246789319d38SHerbert Xu 			nskb = skb_clone(fskb, GFP_ATOMIC);
246889319d38SHerbert Xu 			fskb = fskb->next;
246989319d38SHerbert Xu 
2470f4c50d99SHerbert Xu 			if (unlikely(!nskb))
2471f4c50d99SHerbert Xu 				goto err;
2472f4c50d99SHerbert Xu 
247389319d38SHerbert Xu 			hsize = skb_end_pointer(nskb) - nskb->head;
247489319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
247589319d38SHerbert Xu 				kfree_skb(nskb);
247689319d38SHerbert Xu 				goto err;
247789319d38SHerbert Xu 			}
247889319d38SHerbert Xu 
247989319d38SHerbert Xu 			nskb->truesize += skb_end_pointer(nskb) - nskb->head -
248089319d38SHerbert Xu 					  hsize;
248189319d38SHerbert Xu 			skb_release_head_state(nskb);
248289319d38SHerbert Xu 			__skb_push(nskb, doffset);
248389319d38SHerbert Xu 		} else {
248489319d38SHerbert Xu 			nskb = alloc_skb(hsize + doffset + headroom,
248589319d38SHerbert Xu 					 GFP_ATOMIC);
248689319d38SHerbert Xu 
248789319d38SHerbert Xu 			if (unlikely(!nskb))
248889319d38SHerbert Xu 				goto err;
248989319d38SHerbert Xu 
249089319d38SHerbert Xu 			skb_reserve(nskb, headroom);
249189319d38SHerbert Xu 			__skb_put(nskb, doffset);
249289319d38SHerbert Xu 		}
249389319d38SHerbert Xu 
2494f4c50d99SHerbert Xu 		if (segs)
2495f4c50d99SHerbert Xu 			tail->next = nskb;
2496f4c50d99SHerbert Xu 		else
2497f4c50d99SHerbert Xu 			segs = nskb;
2498f4c50d99SHerbert Xu 		tail = nskb;
2499f4c50d99SHerbert Xu 
25006f85a124SHerbert Xu 		__copy_skb_header(nskb, skb);
2501f4c50d99SHerbert Xu 		nskb->mac_len = skb->mac_len;
2502f4c50d99SHerbert Xu 
2503459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(nskb);
2504ddc7b8e3SArnaldo Carvalho de Melo 		skb_set_network_header(nskb, skb->mac_len);
2505b0e380b1SArnaldo Carvalho de Melo 		nskb->transport_header = (nskb->network_header +
2506b0e380b1SArnaldo Carvalho de Melo 					  skb_network_header_len(skb));
250789319d38SHerbert Xu 		skb_copy_from_linear_data(skb, nskb->data, doffset);
250889319d38SHerbert Xu 
250989319d38SHerbert Xu 		if (pos >= offset + len)
251089319d38SHerbert Xu 			continue;
251189319d38SHerbert Xu 
2512f4c50d99SHerbert Xu 		if (!sg) {
25136f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
2514f4c50d99SHerbert Xu 			nskb->csum = skb_copy_and_csum_bits(skb, offset,
2515f4c50d99SHerbert Xu 							    skb_put(nskb, len),
2516f4c50d99SHerbert Xu 							    len, 0);
2517f4c50d99SHerbert Xu 			continue;
2518f4c50d99SHerbert Xu 		}
2519f4c50d99SHerbert Xu 
2520f4c50d99SHerbert Xu 		frag = skb_shinfo(nskb)->frags;
2521f4c50d99SHerbert Xu 
2522d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset,
2523d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
2524f4c50d99SHerbert Xu 
252589319d38SHerbert Xu 		while (pos < offset + len && i < nfrags) {
2526f4c50d99SHerbert Xu 			*frag = skb_shinfo(skb)->frags[i];
2527f4c50d99SHerbert Xu 			get_page(frag->page);
2528f4c50d99SHerbert Xu 			size = frag->size;
2529f4c50d99SHerbert Xu 
2530f4c50d99SHerbert Xu 			if (pos < offset) {
2531f4c50d99SHerbert Xu 				frag->page_offset += offset - pos;
2532f4c50d99SHerbert Xu 				frag->size -= offset - pos;
2533f4c50d99SHerbert Xu 			}
2534f4c50d99SHerbert Xu 
253589319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
2536f4c50d99SHerbert Xu 
2537f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
2538f4c50d99SHerbert Xu 				i++;
2539f4c50d99SHerbert Xu 				pos += size;
2540f4c50d99SHerbert Xu 			} else {
2541f4c50d99SHerbert Xu 				frag->size -= pos + size - (offset + len);
254289319d38SHerbert Xu 				goto skip_fraglist;
2543f4c50d99SHerbert Xu 			}
2544f4c50d99SHerbert Xu 
2545f4c50d99SHerbert Xu 			frag++;
2546f4c50d99SHerbert Xu 		}
2547f4c50d99SHerbert Xu 
254889319d38SHerbert Xu 		if (pos < offset + len) {
254989319d38SHerbert Xu 			struct sk_buff *fskb2 = fskb;
255089319d38SHerbert Xu 
255189319d38SHerbert Xu 			BUG_ON(pos + fskb->len != offset + len);
255289319d38SHerbert Xu 
255389319d38SHerbert Xu 			pos += fskb->len;
255489319d38SHerbert Xu 			fskb = fskb->next;
255589319d38SHerbert Xu 
255689319d38SHerbert Xu 			if (fskb2->next) {
255789319d38SHerbert Xu 				fskb2 = skb_clone(fskb2, GFP_ATOMIC);
255889319d38SHerbert Xu 				if (!fskb2)
255989319d38SHerbert Xu 					goto err;
256089319d38SHerbert Xu 			} else
256189319d38SHerbert Xu 				skb_get(fskb2);
256289319d38SHerbert Xu 
256389319d38SHerbert Xu 			BUG_ON(skb_shinfo(nskb)->frag_list);
256489319d38SHerbert Xu 			skb_shinfo(nskb)->frag_list = fskb2;
256589319d38SHerbert Xu 		}
256689319d38SHerbert Xu 
256789319d38SHerbert Xu skip_fraglist:
2568f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
2569f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
2570f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
2571f4c50d99SHerbert Xu 	} while ((offset += len) < skb->len);
2572f4c50d99SHerbert Xu 
2573f4c50d99SHerbert Xu 	return segs;
2574f4c50d99SHerbert Xu 
2575f4c50d99SHerbert Xu err:
2576f4c50d99SHerbert Xu 	while ((skb = segs)) {
2577f4c50d99SHerbert Xu 		segs = skb->next;
2578b08d5840SPatrick McHardy 		kfree_skb(skb);
2579f4c50d99SHerbert Xu 	}
2580f4c50d99SHerbert Xu 	return ERR_PTR(err);
2581f4c50d99SHerbert Xu }
2582f4c50d99SHerbert Xu 
2583f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
2584f4c50d99SHerbert Xu 
258571d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
258671d93b39SHerbert Xu {
258771d93b39SHerbert Xu 	struct sk_buff *p = *head;
258871d93b39SHerbert Xu 	struct sk_buff *nskb;
258971d93b39SHerbert Xu 	unsigned int headroom;
259071d93b39SHerbert Xu 	unsigned int hlen = p->data - skb_mac_header(p);
259171d93b39SHerbert Xu 
259271d93b39SHerbert Xu 	if (hlen + p->len + skb->len >= 65536)
259371d93b39SHerbert Xu 		return -E2BIG;
259471d93b39SHerbert Xu 
259571d93b39SHerbert Xu 	if (skb_shinfo(p)->frag_list)
259671d93b39SHerbert Xu 		goto merge;
259771d93b39SHerbert Xu 
259871d93b39SHerbert Xu 	headroom = skb_headroom(p);
259971d93b39SHerbert Xu 	nskb = netdev_alloc_skb(p->dev, headroom);
260071d93b39SHerbert Xu 	if (unlikely(!nskb))
260171d93b39SHerbert Xu 		return -ENOMEM;
260271d93b39SHerbert Xu 
260371d93b39SHerbert Xu 	__copy_skb_header(nskb, p);
260471d93b39SHerbert Xu 	nskb->mac_len = p->mac_len;
260571d93b39SHerbert Xu 
260671d93b39SHerbert Xu 	skb_reserve(nskb, headroom);
260771d93b39SHerbert Xu 
260871d93b39SHerbert Xu 	skb_set_mac_header(nskb, -hlen);
260971d93b39SHerbert Xu 	skb_set_network_header(nskb, skb_network_offset(p));
261071d93b39SHerbert Xu 	skb_set_transport_header(nskb, skb_transport_offset(p));
261171d93b39SHerbert Xu 
261271d93b39SHerbert Xu 	memcpy(skb_mac_header(nskb), skb_mac_header(p), hlen);
261371d93b39SHerbert Xu 
261471d93b39SHerbert Xu 	*NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
261571d93b39SHerbert Xu 	skb_shinfo(nskb)->frag_list = p;
2616b530256dSHerbert Xu 	skb_shinfo(nskb)->gso_size = skb_shinfo(p)->gso_size;
261771d93b39SHerbert Xu 	skb_header_release(p);
261871d93b39SHerbert Xu 	nskb->prev = p;
261971d93b39SHerbert Xu 
262071d93b39SHerbert Xu 	nskb->data_len += p->len;
262171d93b39SHerbert Xu 	nskb->truesize += p->len;
262271d93b39SHerbert Xu 	nskb->len += p->len;
262371d93b39SHerbert Xu 
262471d93b39SHerbert Xu 	*head = nskb;
262571d93b39SHerbert Xu 	nskb->next = p->next;
262671d93b39SHerbert Xu 	p->next = NULL;
262771d93b39SHerbert Xu 
262871d93b39SHerbert Xu 	p = nskb;
262971d93b39SHerbert Xu 
263071d93b39SHerbert Xu merge:
263171d93b39SHerbert Xu 	NAPI_GRO_CB(p)->count++;
263271d93b39SHerbert Xu 	p->prev->next = skb;
263371d93b39SHerbert Xu 	p->prev = skb;
263471d93b39SHerbert Xu 	skb_header_release(skb);
263571d93b39SHerbert Xu 
263671d93b39SHerbert Xu 	p->data_len += skb->len;
263771d93b39SHerbert Xu 	p->truesize += skb->len;
263871d93b39SHerbert Xu 	p->len += skb->len;
263971d93b39SHerbert Xu 
264071d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
264171d93b39SHerbert Xu 	return 0;
264271d93b39SHerbert Xu }
264371d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive);
264471d93b39SHerbert Xu 
26451da177e4SLinus Torvalds void __init skb_init(void)
26461da177e4SLinus Torvalds {
26471da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
26481da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
26491da177e4SLinus Torvalds 					      0,
2650e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
265120c2df83SPaul Mundt 					      NULL);
2652d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2653d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
2654d179cd12SDavid S. Miller 						sizeof(atomic_t),
2655d179cd12SDavid S. Miller 						0,
2656e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
265720c2df83SPaul Mundt 						NULL);
26581da177e4SLinus Torvalds }
26591da177e4SLinus Torvalds 
2660716ea3a7SDavid Howells /**
2661716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2662716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
2663716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
2664716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
2665716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
2666716ea3a7SDavid Howells  *
2667716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
2668716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
2669716ea3a7SDavid Howells  */
267051c739d1SDavid S. Miller static int
267151c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2672716ea3a7SDavid Howells {
26731a028e50SDavid S. Miller 	int start = skb_headlen(skb);
26741a028e50SDavid S. Miller 	int i, copy = start - offset;
2675716ea3a7SDavid Howells 	int elt = 0;
2676716ea3a7SDavid Howells 
2677716ea3a7SDavid Howells 	if (copy > 0) {
2678716ea3a7SDavid Howells 		if (copy > len)
2679716ea3a7SDavid Howells 			copy = len;
2680642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
2681716ea3a7SDavid Howells 		elt++;
2682716ea3a7SDavid Howells 		if ((len -= copy) == 0)
2683716ea3a7SDavid Howells 			return elt;
2684716ea3a7SDavid Howells 		offset += copy;
2685716ea3a7SDavid Howells 	}
2686716ea3a7SDavid Howells 
2687716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
26881a028e50SDavid S. Miller 		int end;
2689716ea3a7SDavid Howells 
2690547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
26911a028e50SDavid S. Miller 
26921a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
2693716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2694716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2695716ea3a7SDavid Howells 
2696716ea3a7SDavid Howells 			if (copy > len)
2697716ea3a7SDavid Howells 				copy = len;
2698642f1490SJens Axboe 			sg_set_page(&sg[elt], frag->page, copy,
2699642f1490SJens Axboe 					frag->page_offset+offset-start);
2700716ea3a7SDavid Howells 			elt++;
2701716ea3a7SDavid Howells 			if (!(len -= copy))
2702716ea3a7SDavid Howells 				return elt;
2703716ea3a7SDavid Howells 			offset += copy;
2704716ea3a7SDavid Howells 		}
27051a028e50SDavid S. Miller 		start = end;
2706716ea3a7SDavid Howells 	}
2707716ea3a7SDavid Howells 
2708716ea3a7SDavid Howells 	if (skb_shinfo(skb)->frag_list) {
2709716ea3a7SDavid Howells 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
2710716ea3a7SDavid Howells 
2711716ea3a7SDavid Howells 		for (; list; list = list->next) {
27121a028e50SDavid S. Miller 			int end;
2713716ea3a7SDavid Howells 
2714547b792cSIlpo Järvinen 			WARN_ON(start > offset + len);
27151a028e50SDavid S. Miller 
27161a028e50SDavid S. Miller 			end = start + list->len;
2717716ea3a7SDavid Howells 			if ((copy = end - offset) > 0) {
2718716ea3a7SDavid Howells 				if (copy > len)
2719716ea3a7SDavid Howells 					copy = len;
272051c739d1SDavid S. Miller 				elt += __skb_to_sgvec(list, sg+elt, offset - start,
272151c739d1SDavid S. Miller 						      copy);
2722716ea3a7SDavid Howells 				if ((len -= copy) == 0)
2723716ea3a7SDavid Howells 					return elt;
2724716ea3a7SDavid Howells 				offset += copy;
2725716ea3a7SDavid Howells 			}
27261a028e50SDavid S. Miller 			start = end;
2727716ea3a7SDavid Howells 		}
2728716ea3a7SDavid Howells 	}
2729716ea3a7SDavid Howells 	BUG_ON(len);
2730716ea3a7SDavid Howells 	return elt;
2731716ea3a7SDavid Howells }
2732716ea3a7SDavid Howells 
273351c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
273451c739d1SDavid S. Miller {
273551c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
273651c739d1SDavid S. Miller 
2737c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
273851c739d1SDavid S. Miller 
273951c739d1SDavid S. Miller 	return nsg;
274051c739d1SDavid S. Miller }
274151c739d1SDavid S. Miller 
2742716ea3a7SDavid Howells /**
2743716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
2744716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
2745716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
2746716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
2747716ea3a7SDavid Howells  *
2748716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
2749716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
2750716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
2751716ea3a7SDavid Howells  *
2752716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
2753716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
2754716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
2755716ea3a7SDavid Howells  *
2756716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
2757716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
2758716ea3a7SDavid Howells  */
2759716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2760716ea3a7SDavid Howells {
2761716ea3a7SDavid Howells 	int copyflag;
2762716ea3a7SDavid Howells 	int elt;
2763716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
2764716ea3a7SDavid Howells 
2765716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
2766716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
2767716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
2768716ea3a7SDavid Howells 	 */
2769716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2770716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2771716ea3a7SDavid Howells 		return -ENOMEM;
2772716ea3a7SDavid Howells 
2773716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
2774716ea3a7SDavid Howells 	if (!skb_shinfo(skb)->frag_list) {
2775716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
2776716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
2777716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
2778716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
2779716ea3a7SDavid Howells 
2780716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
2781716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2782716ea3a7SDavid Howells 			return -ENOMEM;
2783716ea3a7SDavid Howells 
2784716ea3a7SDavid Howells 		/* Voila! */
2785716ea3a7SDavid Howells 		*trailer = skb;
2786716ea3a7SDavid Howells 		return 1;
2787716ea3a7SDavid Howells 	}
2788716ea3a7SDavid Howells 
2789716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
2790716ea3a7SDavid Howells 
2791716ea3a7SDavid Howells 	elt = 1;
2792716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
2793716ea3a7SDavid Howells 	copyflag = 0;
2794716ea3a7SDavid Howells 
2795716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
2796716ea3a7SDavid Howells 		int ntail = 0;
2797716ea3a7SDavid Howells 
2798716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
2799716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
2800716ea3a7SDavid Howells 		 * after it. */
2801716ea3a7SDavid Howells 
2802716ea3a7SDavid Howells 		if (skb_shared(skb1))
2803716ea3a7SDavid Howells 			copyflag = 1;
2804716ea3a7SDavid Howells 
2805716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
2806716ea3a7SDavid Howells 
2807716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
2808716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
2809716ea3a7SDavid Howells 			    skb_shinfo(skb1)->frag_list ||
2810716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
2811716ea3a7SDavid Howells 				ntail = tailbits + 128;
2812716ea3a7SDavid Howells 		}
2813716ea3a7SDavid Howells 
2814716ea3a7SDavid Howells 		if (copyflag ||
2815716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
2816716ea3a7SDavid Howells 		    ntail ||
2817716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
2818716ea3a7SDavid Howells 		    skb_shinfo(skb1)->frag_list) {
2819716ea3a7SDavid Howells 			struct sk_buff *skb2;
2820716ea3a7SDavid Howells 
2821716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
2822716ea3a7SDavid Howells 			if (ntail == 0)
2823716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
2824716ea3a7SDavid Howells 			else
2825716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
2826716ea3a7SDavid Howells 						       skb_headroom(skb1),
2827716ea3a7SDavid Howells 						       ntail,
2828716ea3a7SDavid Howells 						       GFP_ATOMIC);
2829716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
2830716ea3a7SDavid Howells 				return -ENOMEM;
2831716ea3a7SDavid Howells 
2832716ea3a7SDavid Howells 			if (skb1->sk)
2833716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
2834716ea3a7SDavid Howells 
2835716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
2836716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
2837716ea3a7SDavid Howells 
2838716ea3a7SDavid Howells 			skb2->next = skb1->next;
2839716ea3a7SDavid Howells 			*skb_p = skb2;
2840716ea3a7SDavid Howells 			kfree_skb(skb1);
2841716ea3a7SDavid Howells 			skb1 = skb2;
2842716ea3a7SDavid Howells 		}
2843716ea3a7SDavid Howells 		elt++;
2844716ea3a7SDavid Howells 		*trailer = skb1;
2845716ea3a7SDavid Howells 		skb_p = &skb1->next;
2846716ea3a7SDavid Howells 	}
2847716ea3a7SDavid Howells 
2848716ea3a7SDavid Howells 	return elt;
2849716ea3a7SDavid Howells }
2850716ea3a7SDavid Howells 
2851f35d9d8aSRusty Russell /**
2852f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
2853f35d9d8aSRusty Russell  * @skb: the skb to set
2854f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
2855f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
2856f35d9d8aSRusty Russell  *
2857f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
2858f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
2859f35d9d8aSRusty Russell  *
2860f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
2861f35d9d8aSRusty Russell  * returns false you should drop the packet.
2862f35d9d8aSRusty Russell  */
2863f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
2864f35d9d8aSRusty Russell {
2865f35d9d8aSRusty Russell 	if (unlikely(start > skb->len - 2) ||
2866f35d9d8aSRusty Russell 	    unlikely((int)start + off > skb->len - 2)) {
2867f35d9d8aSRusty Russell 		if (net_ratelimit())
2868f35d9d8aSRusty Russell 			printk(KERN_WARNING
2869f35d9d8aSRusty Russell 			       "bad partial csum: csum=%u/%u len=%u\n",
2870f35d9d8aSRusty Russell 			       start, off, skb->len);
2871f35d9d8aSRusty Russell 		return false;
2872f35d9d8aSRusty Russell 	}
2873f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
2874f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
2875f35d9d8aSRusty Russell 	skb->csum_offset = off;
2876f35d9d8aSRusty Russell 	return true;
2877f35d9d8aSRusty Russell }
2878f35d9d8aSRusty Russell 
28794497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
28804497b076SBen Hutchings {
28814497b076SBen Hutchings 	if (net_ratelimit())
28824497b076SBen Hutchings 		pr_warning("%s: received packets cannot be forwarded"
28834497b076SBen Hutchings 			   " while LRO is enabled\n", skb->dev->name);
28844497b076SBen Hutchings }
28854497b076SBen Hutchings 
28861da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim);
28871da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb);
2888231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb);
28891da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail);
2890d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
28918af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb);
28921da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy);
28931da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head);
28941da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum);
28951da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone);
28961da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy);
28971da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits);
28981da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev);
28991da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits);
29001da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand);
29011da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic);
29021da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad);
29031da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom);
29041da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic);
29051da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue);
29061da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail);
29071da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert);
29081da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge);
29091da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head);
29101da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail);
29111da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink);
29121da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append);
29131da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split);
2914677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read);
2915677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read);
2916677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read);
29173fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text);
2918e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags);
29194497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
2920716ea3a7SDavid Howells 
2921716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec);
2922716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data);
2923f35d9d8aSRusty Russell EXPORT_SYMBOL_GPL(skb_partial_csum_set);
2924