xref: /openbmc/linux/net/core/skbuff.c (revision 47d29646)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
31da177e4SLinus Torvalds  *
4113aa838SAlan Cox  *	Authors:	Alan Cox <alan@lxorguk.ukuu.org.uk>
51da177e4SLinus Torvalds  *			Florian La Roche <rzsfl@rz.uni-sb.de>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	Fixes:
81da177e4SLinus Torvalds  *		Alan Cox	:	Fixed the worst of the load
91da177e4SLinus Torvalds  *					balancer bugs.
101da177e4SLinus Torvalds  *		Dave Platt	:	Interrupt stacking fix.
111da177e4SLinus Torvalds  *	Richard Kooijman	:	Timestamp fixes.
121da177e4SLinus Torvalds  *		Alan Cox	:	Changed buffer format.
131da177e4SLinus Torvalds  *		Alan Cox	:	destructor hook for AF_UNIX etc.
141da177e4SLinus Torvalds  *		Linus Torvalds	:	Better skb_clone.
151da177e4SLinus Torvalds  *		Alan Cox	:	Added skb_copy.
161da177e4SLinus Torvalds  *		Alan Cox	:	Added all the changed routines Linus
171da177e4SLinus Torvalds  *					only put in the headers
181da177e4SLinus Torvalds  *		Ray VanTassle	:	Fixed --skb->lock in free
191da177e4SLinus Torvalds  *		Alan Cox	:	skb_copy copy arp field
201da177e4SLinus Torvalds  *		Andi Kleen	:	slabified it.
211da177e4SLinus Torvalds  *		Robert Olsson	:	Removed skb_head_pool
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *	NOTE:
241da177e4SLinus Torvalds  *		The __skb_ routines should be called with interrupts
251da177e4SLinus Torvalds  *	disabled, or you better be *real* sure that the operation is atomic
261da177e4SLinus Torvalds  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
271da177e4SLinus Torvalds  *	or via disabling bottom half handlers, etc).
281da177e4SLinus Torvalds  *
291da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
301da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
311da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
321da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /*
361da177e4SLinus Torvalds  *	The functions in this file will not compile correctly with gcc 2.4.x
371da177e4SLinus Torvalds  */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/types.h>
411da177e4SLinus Torvalds #include <linux/kernel.h>
42fe55f6d5SVegard Nossum #include <linux/kmemcheck.h>
431da177e4SLinus Torvalds #include <linux/mm.h>
441da177e4SLinus Torvalds #include <linux/interrupt.h>
451da177e4SLinus Torvalds #include <linux/in.h>
461da177e4SLinus Torvalds #include <linux/inet.h>
471da177e4SLinus Torvalds #include <linux/slab.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
501da177e4SLinus Torvalds #include <net/pkt_sched.h>
511da177e4SLinus Torvalds #endif
521da177e4SLinus Torvalds #include <linux/string.h>
531da177e4SLinus Torvalds #include <linux/skbuff.h>
549c55e01cSJens Axboe #include <linux/splice.h>
551da177e4SLinus Torvalds #include <linux/cache.h>
561da177e4SLinus Torvalds #include <linux/rtnetlink.h>
571da177e4SLinus Torvalds #include <linux/init.h>
58716ea3a7SDavid Howells #include <linux/scatterlist.h>
59ac45f602SPatrick Ohly #include <linux/errqueue.h>
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds #include <net/protocol.h>
621da177e4SLinus Torvalds #include <net/dst.h>
631da177e4SLinus Torvalds #include <net/sock.h>
641da177e4SLinus Torvalds #include <net/checksum.h>
651da177e4SLinus Torvalds #include <net/xfrm.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <asm/uaccess.h>
681da177e4SLinus Torvalds #include <asm/system.h>
69ad8d75ffSSteven Rostedt #include <trace/events/skb.h>
701da177e4SLinus Torvalds 
71a1f8e7f7SAl Viro #include "kmap_skb.h"
72a1f8e7f7SAl Viro 
73e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly;
74e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
751da177e4SLinus Torvalds 
769c55e01cSJens Axboe static void sock_pipe_buf_release(struct pipe_inode_info *pipe,
779c55e01cSJens Axboe 				  struct pipe_buffer *buf)
789c55e01cSJens Axboe {
798b9d3728SJarek Poplawski 	put_page(buf->page);
809c55e01cSJens Axboe }
819c55e01cSJens Axboe 
829c55e01cSJens Axboe static void sock_pipe_buf_get(struct pipe_inode_info *pipe,
839c55e01cSJens Axboe 				struct pipe_buffer *buf)
849c55e01cSJens Axboe {
858b9d3728SJarek Poplawski 	get_page(buf->page);
869c55e01cSJens Axboe }
879c55e01cSJens Axboe 
889c55e01cSJens Axboe static int sock_pipe_buf_steal(struct pipe_inode_info *pipe,
899c55e01cSJens Axboe 			       struct pipe_buffer *buf)
909c55e01cSJens Axboe {
919c55e01cSJens Axboe 	return 1;
929c55e01cSJens Axboe }
939c55e01cSJens Axboe 
949c55e01cSJens Axboe 
959c55e01cSJens Axboe /* Pipe buffer operations for a socket. */
9628dfef8fSAlexey Dobriyan static const struct pipe_buf_operations sock_pipe_buf_ops = {
979c55e01cSJens Axboe 	.can_merge = 0,
989c55e01cSJens Axboe 	.map = generic_pipe_buf_map,
999c55e01cSJens Axboe 	.unmap = generic_pipe_buf_unmap,
1009c55e01cSJens Axboe 	.confirm = generic_pipe_buf_confirm,
1019c55e01cSJens Axboe 	.release = sock_pipe_buf_release,
1029c55e01cSJens Axboe 	.steal = sock_pipe_buf_steal,
1039c55e01cSJens Axboe 	.get = sock_pipe_buf_get,
1049c55e01cSJens Axboe };
1059c55e01cSJens Axboe 
1061da177e4SLinus Torvalds /*
1071da177e4SLinus Torvalds  *	Keep out-of-line to prevent kernel bloat.
1081da177e4SLinus Torvalds  *	__builtin_return_address is not used because it is not always
1091da177e4SLinus Torvalds  *	reliable.
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /**
1131da177e4SLinus Torvalds  *	skb_over_panic	- 	private function
1141da177e4SLinus Torvalds  *	@skb: buffer
1151da177e4SLinus Torvalds  *	@sz: size
1161da177e4SLinus Torvalds  *	@here: address
1171da177e4SLinus Torvalds  *
1181da177e4SLinus Torvalds  *	Out of line support code for skb_put(). Not user callable.
1191da177e4SLinus Torvalds  */
120ccb7c773SRami Rosen static void skb_over_panic(struct sk_buff *skb, int sz, void *here)
1211da177e4SLinus Torvalds {
12226095455SPatrick McHardy 	printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
1234305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
12427a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1254305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
12626095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1271da177e4SLinus Torvalds 	BUG();
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds /**
1311da177e4SLinus Torvalds  *	skb_under_panic	- 	private function
1321da177e4SLinus Torvalds  *	@skb: buffer
1331da177e4SLinus Torvalds  *	@sz: size
1341da177e4SLinus Torvalds  *	@here: address
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  *	Out of line support code for skb_push(). Not user callable.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds 
139ccb7c773SRami Rosen static void skb_under_panic(struct sk_buff *skb, int sz, void *here)
1401da177e4SLinus Torvalds {
14126095455SPatrick McHardy 	printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
1424305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
14327a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1444305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
14526095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1461da177e4SLinus Torvalds 	BUG();
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1501da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1511da177e4SLinus Torvalds  *	[BEEP] leaks.
1521da177e4SLinus Torvalds  *
1531da177e4SLinus Torvalds  */
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /**
156d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1571da177e4SLinus Torvalds  *	@size: size to allocate
1581da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
159c83c2486SRandy Dunlap  *	@fclone: allocate from fclone cache instead of head cache
160c83c2486SRandy Dunlap  *		and allocate a cloned (child) skb
161b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1621da177e4SLinus Torvalds  *
1631da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
1641da177e4SLinus Torvalds  *	tail room of size bytes. The object has a reference count of one.
1651da177e4SLinus Torvalds  *	The return is the buffer. On a failure the return is %NULL.
1661da177e4SLinus Torvalds  *
1671da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1681da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1691da177e4SLinus Torvalds  */
170dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
171b30973f8SChristoph Hellwig 			    int fclone, int node)
1721da177e4SLinus Torvalds {
173e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1744947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1751da177e4SLinus Torvalds 	struct sk_buff *skb;
1761da177e4SLinus Torvalds 	u8 *data;
1771da177e4SLinus Torvalds 
1788798b3fbSHerbert Xu 	cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
1798798b3fbSHerbert Xu 
1801da177e4SLinus Torvalds 	/* Get the HEAD */
181b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1821da177e4SLinus Torvalds 	if (!skb)
1831da177e4SLinus Torvalds 		goto out;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
186b30973f8SChristoph Hellwig 	data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
187b30973f8SChristoph Hellwig 			gfp_mask, node);
1881da177e4SLinus Torvalds 	if (!data)
1891da177e4SLinus Torvalds 		goto nodata;
1901da177e4SLinus Torvalds 
191ca0605a7SArnaldo Carvalho de Melo 	/*
192c8005785SJohannes Berg 	 * Only clear those fields we need to clear, not those that we will
193c8005785SJohannes Berg 	 * actually initialise below. Hence, don't put any more fields after
194c8005785SJohannes Berg 	 * the tail pointer in struct sk_buff!
195ca0605a7SArnaldo Carvalho de Melo 	 */
196ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
1971da177e4SLinus Torvalds 	skb->truesize = size + sizeof(struct sk_buff);
1981da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
1991da177e4SLinus Torvalds 	skb->head = data;
2001da177e4SLinus Torvalds 	skb->data = data;
20127a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
2024305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
203fe55f6d5SVegard Nossum 	kmemcheck_annotate_bitfield(skb, flags1);
204fe55f6d5SVegard Nossum 	kmemcheck_annotate_bitfield(skb, flags2);
20519633e12SStephen Hemminger #ifdef NET_SKBUFF_DATA_USES_OFFSET
20619633e12SStephen Hemminger 	skb->mac_header = ~0U;
20719633e12SStephen Hemminger #endif
20819633e12SStephen Hemminger 
2094947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
2104947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
2114947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
2124947d3efSBenjamin LaHaise 	shinfo->nr_frags  = 0;
2137967168cSHerbert Xu 	shinfo->gso_size = 0;
2147967168cSHerbert Xu 	shinfo->gso_segs = 0;
2157967168cSHerbert Xu 	shinfo->gso_type = 0;
2164947d3efSBenjamin LaHaise 	shinfo->ip6_frag_id = 0;
217ac45f602SPatrick Ohly 	shinfo->tx_flags.flags = 0;
218fbb398a8SDavid S. Miller 	skb_frag_list_init(skb);
219ac45f602SPatrick Ohly 	memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
2204947d3efSBenjamin LaHaise 
221d179cd12SDavid S. Miller 	if (fclone) {
222d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
223d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
2241da177e4SLinus Torvalds 
225fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(child, flags1);
226fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(child, flags2);
227d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
228d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
229d179cd12SDavid S. Miller 
230d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
231d179cd12SDavid S. Miller 	}
2321da177e4SLinus Torvalds out:
2331da177e4SLinus Torvalds 	return skb;
2341da177e4SLinus Torvalds nodata:
2358798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2361da177e4SLinus Torvalds 	skb = NULL;
2371da177e4SLinus Torvalds 	goto out;
2381da177e4SLinus Torvalds }
239b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /**
2428af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
2438af27456SChristoph Hellwig  *	@dev: network device to receive on
2448af27456SChristoph Hellwig  *	@length: length to allocate
2458af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
2468af27456SChristoph Hellwig  *
2478af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
2488af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
2498af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
2508af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
2518af27456SChristoph Hellwig  *
2528af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
2538af27456SChristoph Hellwig  */
2548af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
2558af27456SChristoph Hellwig 		unsigned int length, gfp_t gfp_mask)
2568af27456SChristoph Hellwig {
25743cb76d9SGreg Kroah-Hartman 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
2588af27456SChristoph Hellwig 	struct sk_buff *skb;
2598af27456SChristoph Hellwig 
260b30973f8SChristoph Hellwig 	skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
2617b2e497aSChristoph Hellwig 	if (likely(skb)) {
2628af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
2637b2e497aSChristoph Hellwig 		skb->dev = dev;
2647b2e497aSChristoph Hellwig 	}
2658af27456SChristoph Hellwig 	return skb;
2668af27456SChristoph Hellwig }
267b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb);
2681da177e4SLinus Torvalds 
269654bed16SPeter Zijlstra struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
270654bed16SPeter Zijlstra {
271654bed16SPeter Zijlstra 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
272654bed16SPeter Zijlstra 	struct page *page;
273654bed16SPeter Zijlstra 
274654bed16SPeter Zijlstra 	page = alloc_pages_node(node, gfp_mask, 0);
275654bed16SPeter Zijlstra 	return page;
276654bed16SPeter Zijlstra }
277654bed16SPeter Zijlstra EXPORT_SYMBOL(__netdev_alloc_page);
278654bed16SPeter Zijlstra 
279654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
280654bed16SPeter Zijlstra 		int size)
281654bed16SPeter Zijlstra {
282654bed16SPeter Zijlstra 	skb_fill_page_desc(skb, i, page, off, size);
283654bed16SPeter Zijlstra 	skb->len += size;
284654bed16SPeter Zijlstra 	skb->data_len += size;
285654bed16SPeter Zijlstra 	skb->truesize += size;
286654bed16SPeter Zijlstra }
287654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag);
288654bed16SPeter Zijlstra 
289f58518e6SIlpo Järvinen /**
290f58518e6SIlpo Järvinen  *	dev_alloc_skb - allocate an skbuff for receiving
291f58518e6SIlpo Järvinen  *	@length: length to allocate
292f58518e6SIlpo Järvinen  *
293f58518e6SIlpo Järvinen  *	Allocate a new &sk_buff and assign it a usage count of one. The
294f58518e6SIlpo Järvinen  *	buffer has unspecified headroom built in. Users should allocate
295f58518e6SIlpo Järvinen  *	the headroom they think they need without accounting for the
296f58518e6SIlpo Järvinen  *	built in space. The built in space is used for optimisations.
297f58518e6SIlpo Järvinen  *
298f58518e6SIlpo Järvinen  *	%NULL is returned if there is no free memory. Although this function
299f58518e6SIlpo Järvinen  *	allocates memory it can be called from an interrupt.
300f58518e6SIlpo Järvinen  */
301f58518e6SIlpo Järvinen struct sk_buff *dev_alloc_skb(unsigned int length)
302f58518e6SIlpo Järvinen {
3031483b874SDenys Vlasenko 	/*
3041483b874SDenys Vlasenko 	 * There is more code here than it seems:
305a0f55e0eSDavid S. Miller 	 * __dev_alloc_skb is an inline
3061483b874SDenys Vlasenko 	 */
307f58518e6SIlpo Järvinen 	return __dev_alloc_skb(length, GFP_ATOMIC);
308f58518e6SIlpo Järvinen }
309f58518e6SIlpo Järvinen EXPORT_SYMBOL(dev_alloc_skb);
310f58518e6SIlpo Järvinen 
31127b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
3121da177e4SLinus Torvalds {
31327b437c8SHerbert Xu 	struct sk_buff *list = *listp;
3141da177e4SLinus Torvalds 
31527b437c8SHerbert Xu 	*listp = NULL;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	do {
3181da177e4SLinus Torvalds 		struct sk_buff *this = list;
3191da177e4SLinus Torvalds 		list = list->next;
3201da177e4SLinus Torvalds 		kfree_skb(this);
3211da177e4SLinus Torvalds 	} while (list);
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
32427b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
32527b437c8SHerbert Xu {
32627b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
32727b437c8SHerbert Xu }
32827b437c8SHerbert Xu 
3291da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	struct sk_buff *list;
3321da177e4SLinus Torvalds 
333fbb398a8SDavid S. Miller 	skb_walk_frags(skb, list)
3341da177e4SLinus Torvalds 		skb_get(list);
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3375bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	if (!skb->cloned ||
3401da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
3411da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
3421da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
3431da177e4SLinus Torvalds 			int i;
3441da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
3451da177e4SLinus Torvalds 				put_page(skb_shinfo(skb)->frags[i].page);
3461da177e4SLinus Torvalds 		}
3471da177e4SLinus Torvalds 
348fbb398a8SDavid S. Miller 		if (skb_has_frags(skb))
3491da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		kfree(skb->head);
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds /*
3561da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
3571da177e4SLinus Torvalds  */
3582d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb)
3591da177e4SLinus Torvalds {
360d179cd12SDavid S. Miller 	struct sk_buff *other;
361d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
362d179cd12SDavid S. Miller 
363d179cd12SDavid S. Miller 	switch (skb->fclone) {
364d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
3651da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
366d179cd12SDavid S. Miller 		break;
367d179cd12SDavid S. Miller 
368d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
369d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
370d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
371d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
372d179cd12SDavid S. Miller 		break;
373d179cd12SDavid S. Miller 
374d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
375d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
376d179cd12SDavid S. Miller 		other = skb - 1;
377d179cd12SDavid S. Miller 
378d179cd12SDavid S. Miller 		/* The clone portion is available for
379d179cd12SDavid S. Miller 		 * fast-cloning again.
380d179cd12SDavid S. Miller 		 */
381d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
382d179cd12SDavid S. Miller 
383d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
384d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
385d179cd12SDavid S. Miller 		break;
3863ff50b79SStephen Hemminger 	}
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds 
38904a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb)
3901da177e4SLinus Torvalds {
391adf30907SEric Dumazet 	skb_dst_drop(skb);
3921da177e4SLinus Torvalds #ifdef CONFIG_XFRM
3931da177e4SLinus Torvalds 	secpath_put(skb->sp);
3941da177e4SLinus Torvalds #endif
3951da177e4SLinus Torvalds 	if (skb->destructor) {
3969c2b3328SStephen Hemminger 		WARN_ON(in_irq());
3971da177e4SLinus Torvalds 		skb->destructor(skb);
3981da177e4SLinus Torvalds 	}
3999fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
4005f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
4019fb9cbb1SYasuyuki Kozakai 	nf_conntrack_put_reasm(skb->nfct_reasm);
4029fb9cbb1SYasuyuki Kozakai #endif
4031da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
4041da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
4051da177e4SLinus Torvalds #endif
4061da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
4071da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4081da177e4SLinus Torvalds 	skb->tc_index = 0;
4091da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
4101da177e4SLinus Torvalds 	skb->tc_verd = 0;
4111da177e4SLinus Torvalds #endif
4121da177e4SLinus Torvalds #endif
41304a4bb55SLennert Buytenhek }
41404a4bb55SLennert Buytenhek 
41504a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */
41604a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb)
41704a4bb55SLennert Buytenhek {
41804a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
4192d4baff8SHerbert Xu 	skb_release_data(skb);
4202d4baff8SHerbert Xu }
4211da177e4SLinus Torvalds 
4222d4baff8SHerbert Xu /**
4232d4baff8SHerbert Xu  *	__kfree_skb - private function
4242d4baff8SHerbert Xu  *	@skb: buffer
4252d4baff8SHerbert Xu  *
4262d4baff8SHerbert Xu  *	Free an sk_buff. Release anything attached to the buffer.
4272d4baff8SHerbert Xu  *	Clean the state. This is an internal helper function. Users should
4282d4baff8SHerbert Xu  *	always call kfree_skb
4292d4baff8SHerbert Xu  */
4302d4baff8SHerbert Xu 
4312d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb)
4322d4baff8SHerbert Xu {
4332d4baff8SHerbert Xu 	skb_release_all(skb);
4341da177e4SLinus Torvalds 	kfree_skbmem(skb);
4351da177e4SLinus Torvalds }
436b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb);
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds /**
439231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
440231d06aeSJörn Engel  *	@skb: buffer to free
441231d06aeSJörn Engel  *
442231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
443231d06aeSJörn Engel  *	hit zero.
444231d06aeSJörn Engel  */
445231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
446231d06aeSJörn Engel {
447231d06aeSJörn Engel 	if (unlikely(!skb))
448231d06aeSJörn Engel 		return;
449231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
450231d06aeSJörn Engel 		smp_rmb();
451231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
452231d06aeSJörn Engel 		return;
453ead2ceb0SNeil Horman 	trace_kfree_skb(skb, __builtin_return_address(0));
454231d06aeSJörn Engel 	__kfree_skb(skb);
455231d06aeSJörn Engel }
456b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb);
457231d06aeSJörn Engel 
458d1a203eaSStephen Hemminger /**
459ead2ceb0SNeil Horman  *	consume_skb - free an skbuff
460ead2ceb0SNeil Horman  *	@skb: buffer to free
461ead2ceb0SNeil Horman  *
462ead2ceb0SNeil Horman  *	Drop a ref to the buffer and free it if the usage count has hit zero
463ead2ceb0SNeil Horman  *	Functions identically to kfree_skb, but kfree_skb assumes that the frame
464ead2ceb0SNeil Horman  *	is being dropped after a failure and notes that
465ead2ceb0SNeil Horman  */
466ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb)
467ead2ceb0SNeil Horman {
468ead2ceb0SNeil Horman 	if (unlikely(!skb))
469ead2ceb0SNeil Horman 		return;
470ead2ceb0SNeil Horman 	if (likely(atomic_read(&skb->users) == 1))
471ead2ceb0SNeil Horman 		smp_rmb();
472ead2ceb0SNeil Horman 	else if (likely(!atomic_dec_and_test(&skb->users)))
473ead2ceb0SNeil Horman 		return;
474ead2ceb0SNeil Horman 	__kfree_skb(skb);
475ead2ceb0SNeil Horman }
476ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb);
477ead2ceb0SNeil Horman 
478ead2ceb0SNeil Horman /**
479d1a203eaSStephen Hemminger  *	skb_recycle_check - check if skb can be reused for receive
480d1a203eaSStephen Hemminger  *	@skb: buffer
481d1a203eaSStephen Hemminger  *	@skb_size: minimum receive buffer size
482d1a203eaSStephen Hemminger  *
483d1a203eaSStephen Hemminger  *	Checks that the skb passed in is not shared or cloned, and
484d1a203eaSStephen Hemminger  *	that it is linear and its head portion at least as large as
485d1a203eaSStephen Hemminger  *	skb_size so that it can be recycled as a receive buffer.
486d1a203eaSStephen Hemminger  *	If these conditions are met, this function does any necessary
487d1a203eaSStephen Hemminger  *	reference count dropping and cleans up the skbuff as if it
488d1a203eaSStephen Hemminger  *	just came from __alloc_skb().
489d1a203eaSStephen Hemminger  */
49004a4bb55SLennert Buytenhek int skb_recycle_check(struct sk_buff *skb, int skb_size)
49104a4bb55SLennert Buytenhek {
49204a4bb55SLennert Buytenhek 	struct skb_shared_info *shinfo;
49304a4bb55SLennert Buytenhek 
494e84af6ddSAnton Vorontsov 	if (irqs_disabled())
495e84af6ddSAnton Vorontsov 		return 0;
496e84af6ddSAnton Vorontsov 
49704a4bb55SLennert Buytenhek 	if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE)
49804a4bb55SLennert Buytenhek 		return 0;
49904a4bb55SLennert Buytenhek 
50004a4bb55SLennert Buytenhek 	skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD);
50104a4bb55SLennert Buytenhek 	if (skb_end_pointer(skb) - skb->head < skb_size)
50204a4bb55SLennert Buytenhek 		return 0;
50304a4bb55SLennert Buytenhek 
50404a4bb55SLennert Buytenhek 	if (skb_shared(skb) || skb_cloned(skb))
50504a4bb55SLennert Buytenhek 		return 0;
50604a4bb55SLennert Buytenhek 
50704a4bb55SLennert Buytenhek 	skb_release_head_state(skb);
50804a4bb55SLennert Buytenhek 	shinfo = skb_shinfo(skb);
50904a4bb55SLennert Buytenhek 	atomic_set(&shinfo->dataref, 1);
51004a4bb55SLennert Buytenhek 	shinfo->nr_frags = 0;
51104a4bb55SLennert Buytenhek 	shinfo->gso_size = 0;
51204a4bb55SLennert Buytenhek 	shinfo->gso_segs = 0;
51304a4bb55SLennert Buytenhek 	shinfo->gso_type = 0;
51404a4bb55SLennert Buytenhek 	shinfo->ip6_frag_id = 0;
515b8050075SLennert Buytenhek 	shinfo->tx_flags.flags = 0;
516fbb398a8SDavid S. Miller 	skb_frag_list_init(skb);
517b8050075SLennert Buytenhek 	memset(&shinfo->hwtstamps, 0, sizeof(shinfo->hwtstamps));
51804a4bb55SLennert Buytenhek 
51904a4bb55SLennert Buytenhek 	memset(skb, 0, offsetof(struct sk_buff, tail));
52004a4bb55SLennert Buytenhek 	skb->data = skb->head + NET_SKB_PAD;
5215cd33db2SLennert Buytenhek 	skb_reset_tail_pointer(skb);
52204a4bb55SLennert Buytenhek 
52304a4bb55SLennert Buytenhek 	return 1;
52404a4bb55SLennert Buytenhek }
52504a4bb55SLennert Buytenhek EXPORT_SYMBOL(skb_recycle_check);
52604a4bb55SLennert Buytenhek 
527dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
528dec18810SHerbert Xu {
529dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
530dec18810SHerbert Xu 	new->dev		= old->dev;
531dec18810SHerbert Xu 	new->transport_header	= old->transport_header;
532dec18810SHerbert Xu 	new->network_header	= old->network_header;
533dec18810SHerbert Xu 	new->mac_header		= old->mac_header;
534adf30907SEric Dumazet 	skb_dst_set(new, dst_clone(skb_dst(old)));
5350a9627f2STom Herbert 	new->rxhash		= old->rxhash;
536def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM
537dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
538dec18810SHerbert Xu #endif
539dec18810SHerbert Xu 	memcpy(new->cb, old->cb, sizeof(old->cb));
5409bcb97caSHerbert Xu 	new->csum		= old->csum;
541dec18810SHerbert Xu 	new->local_df		= old->local_df;
542dec18810SHerbert Xu 	new->pkt_type		= old->pkt_type;
543dec18810SHerbert Xu 	new->ip_summed		= old->ip_summed;
544dec18810SHerbert Xu 	skb_copy_queue_mapping(new, old);
545dec18810SHerbert Xu 	new->priority		= old->priority;
546dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
547dec18810SHerbert Xu 	new->ipvs_property	= old->ipvs_property;
548dec18810SHerbert Xu #endif
549dec18810SHerbert Xu 	new->protocol		= old->protocol;
550dec18810SHerbert Xu 	new->mark		= old->mark;
5518964be4aSEric Dumazet 	new->skb_iif		= old->skb_iif;
552dec18810SHerbert Xu 	__nf_copy(new, old);
553dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
554dec18810SHerbert Xu     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
555dec18810SHerbert Xu 	new->nf_trace		= old->nf_trace;
556dec18810SHerbert Xu #endif
557dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED
558dec18810SHerbert Xu 	new->tc_index		= old->tc_index;
559dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT
560dec18810SHerbert Xu 	new->tc_verd		= old->tc_verd;
561dec18810SHerbert Xu #endif
562dec18810SHerbert Xu #endif
5636aa895b0SPatrick McHardy 	new->vlan_tci		= old->vlan_tci;
5646aa895b0SPatrick McHardy 
565dec18810SHerbert Xu 	skb_copy_secmark(new, old);
566dec18810SHerbert Xu }
567dec18810SHerbert Xu 
56882c49a35SHerbert Xu /*
56982c49a35SHerbert Xu  * You should not add any new code to this function.  Add it to
57082c49a35SHerbert Xu  * __copy_skb_header above instead.
57182c49a35SHerbert Xu  */
572e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds #define C(x) n->x = skb->x
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	n->next = n->prev = NULL;
5771da177e4SLinus Torvalds 	n->sk = NULL;
578dec18810SHerbert Xu 	__copy_skb_header(n, skb);
579dec18810SHerbert Xu 
5801da177e4SLinus Torvalds 	C(len);
5811da177e4SLinus Torvalds 	C(data_len);
5823e6b3b2eSAlexey Dobriyan 	C(mac_len);
5830a9627f2STom Herbert 	C(rxhash);
584334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
58502f1c89dSPaul Moore 	n->cloned = 1;
5861da177e4SLinus Torvalds 	n->nohdr = 0;
5871da177e4SLinus Torvalds 	n->destructor = NULL;
5881da177e4SLinus Torvalds 	C(tail);
5891da177e4SLinus Torvalds 	C(end);
59002f1c89dSPaul Moore 	C(head);
59102f1c89dSPaul Moore 	C(data);
59202f1c89dSPaul Moore 	C(truesize);
59302f1c89dSPaul Moore 	atomic_set(&n->users, 1);
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
5961da177e4SLinus Torvalds 	skb->cloned = 1;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	return n;
599e0053ec0SHerbert Xu #undef C
600e0053ec0SHerbert Xu }
601e0053ec0SHerbert Xu 
602e0053ec0SHerbert Xu /**
603e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
604e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
605e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
606e0053ec0SHerbert Xu  *
607e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
608e0053ec0SHerbert Xu  *	supplied by the user.
609e0053ec0SHerbert Xu  *
610e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
611e0053ec0SHerbert Xu  */
612e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
613e0053ec0SHerbert Xu {
6142d4baff8SHerbert Xu 	skb_release_all(dst);
615e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
616e0053ec0SHerbert Xu }
617e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
618e0053ec0SHerbert Xu 
619e0053ec0SHerbert Xu /**
620e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
621e0053ec0SHerbert Xu  *	@skb: buffer to clone
622e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
623e0053ec0SHerbert Xu  *
624e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
625e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
626e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
627e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
628e0053ec0SHerbert Xu  *
629e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
630e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
631e0053ec0SHerbert Xu  */
632e0053ec0SHerbert Xu 
633e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
634e0053ec0SHerbert Xu {
635e0053ec0SHerbert Xu 	struct sk_buff *n;
636e0053ec0SHerbert Xu 
637e0053ec0SHerbert Xu 	n = skb + 1;
638e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
639e0053ec0SHerbert Xu 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
640e0053ec0SHerbert Xu 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
641e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
642e0053ec0SHerbert Xu 		atomic_inc(fclone_ref);
643e0053ec0SHerbert Xu 	} else {
644e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
645e0053ec0SHerbert Xu 		if (!n)
646e0053ec0SHerbert Xu 			return NULL;
647fe55f6d5SVegard Nossum 
648fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags1);
649fe55f6d5SVegard Nossum 		kmemcheck_annotate_bitfield(n, flags2);
650e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
651e0053ec0SHerbert Xu 	}
652e0053ec0SHerbert Xu 
653e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
6541da177e4SLinus Torvalds }
655b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
6581da177e4SLinus Torvalds {
6592e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
6601da177e4SLinus Torvalds 	/*
6611da177e4SLinus Torvalds 	 *	Shift between the two data areas in bytes
6621da177e4SLinus Torvalds 	 */
6631da177e4SLinus Torvalds 	unsigned long offset = new->data - old->data;
6642e07fa9cSArnaldo Carvalho de Melo #endif
665dec18810SHerbert Xu 
666dec18810SHerbert Xu 	__copy_skb_header(new, old);
667dec18810SHerbert Xu 
6682e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
6692e07fa9cSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header are relative to skb->head */
6702e07fa9cSArnaldo Carvalho de Melo 	new->transport_header += offset;
6712e07fa9cSArnaldo Carvalho de Melo 	new->network_header   += offset;
672603a8bbeSStephen Hemminger 	if (skb_mac_header_was_set(new))
6732e07fa9cSArnaldo Carvalho de Melo 		new->mac_header	      += offset;
6742e07fa9cSArnaldo Carvalho de Melo #endif
6757967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
6767967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
6777967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds /**
6811da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
6821da177e4SLinus Torvalds  *	@skb: buffer to copy
6831da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
6861da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
6871da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
6881da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
6891da177e4SLinus Torvalds  *
6901da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
6911da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
6921da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
6931da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
6941da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds 
697dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	int headerlen = skb->data - skb->head;
7001da177e4SLinus Torvalds 	/*
7011da177e4SLinus Torvalds 	 *	Allocate the copy buffer
7021da177e4SLinus Torvalds 	 */
7034305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
7044305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
7054305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end + skb->data_len, gfp_mask);
7064305b541SArnaldo Carvalho de Melo #else
7074305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
7084305b541SArnaldo Carvalho de Melo #endif
7091da177e4SLinus Torvalds 	if (!n)
7101da177e4SLinus Torvalds 		return NULL;
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* Set the data pointer */
7131da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
7141da177e4SLinus Torvalds 	/* Set the tail pointer and length */
7151da177e4SLinus Torvalds 	skb_put(n, skb->len);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
7181da177e4SLinus Torvalds 		BUG();
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7211da177e4SLinus Torvalds 	return n;
7221da177e4SLinus Torvalds }
723b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds /**
7261da177e4SLinus Torvalds  *	pskb_copy	-	create copy of an sk_buff with private head.
7271da177e4SLinus Torvalds  *	@skb: buffer to copy
7281da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7291da177e4SLinus Torvalds  *
7301da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
7311da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
7321da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
7331da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
7341da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
7351da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
7361da177e4SLinus Torvalds  */
7371da177e4SLinus Torvalds 
738dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	/*
7411da177e4SLinus Torvalds 	 *	Allocate the copy buffer
7421da177e4SLinus Torvalds 	 */
7434305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
7444305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
7454305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end, gfp_mask);
7464305b541SArnaldo Carvalho de Melo #else
7474305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head, gfp_mask);
7484305b541SArnaldo Carvalho de Melo #endif
7491da177e4SLinus Torvalds 	if (!n)
7501da177e4SLinus Torvalds 		goto out;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	/* Set the data pointer */
7531da177e4SLinus Torvalds 	skb_reserve(n, skb->data - skb->head);
7541da177e4SLinus Torvalds 	/* Set the tail pointer and length */
7551da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
7561da177e4SLinus Torvalds 	/* Copy the bytes */
757d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
7581da177e4SLinus Torvalds 
75925f484a6SHerbert Xu 	n->truesize += skb->data_len;
7601da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
7611da177e4SLinus Torvalds 	n->len	     = skb->len;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
7641da177e4SLinus Torvalds 		int i;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
7671da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
7681da177e4SLinus Torvalds 			get_page(skb_shinfo(n)->frags[i].page);
7691da177e4SLinus Torvalds 		}
7701da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds 
773fbb398a8SDavid S. Miller 	if (skb_has_frags(skb)) {
7741da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
7751da177e4SLinus Torvalds 		skb_clone_fraglist(n);
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7791da177e4SLinus Torvalds out:
7801da177e4SLinus Torvalds 	return n;
7811da177e4SLinus Torvalds }
782b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_copy);
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /**
7851da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
7861da177e4SLinus Torvalds  *	@skb: buffer to reallocate
7871da177e4SLinus Torvalds  *	@nhead: room to add at head
7881da177e4SLinus Torvalds  *	@ntail: room to add at tail
7891da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7901da177e4SLinus Torvalds  *
7911da177e4SLinus Torvalds  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
7921da177e4SLinus Torvalds  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
7931da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
7941da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
7951da177e4SLinus Torvalds  *
7961da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
7971da177e4SLinus Torvalds  *	reloaded after call to this function.
7981da177e4SLinus Torvalds  */
7991da177e4SLinus Torvalds 
80086a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
801dd0fc66fSAl Viro 		     gfp_t gfp_mask)
8021da177e4SLinus Torvalds {
8031da177e4SLinus Torvalds 	int i;
8041da177e4SLinus Torvalds 	u8 *data;
8054305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
8064305b541SArnaldo Carvalho de Melo 	int size = nhead + skb->end + ntail;
8074305b541SArnaldo Carvalho de Melo #else
8081da177e4SLinus Torvalds 	int size = nhead + (skb->end - skb->head) + ntail;
8094305b541SArnaldo Carvalho de Melo #endif
8101da177e4SLinus Torvalds 	long off;
8111da177e4SLinus Torvalds 
8124edd87adSHerbert Xu 	BUG_ON(nhead < 0);
8134edd87adSHerbert Xu 
8141da177e4SLinus Torvalds 	if (skb_shared(skb))
8151da177e4SLinus Torvalds 		BUG();
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
8201da177e4SLinus Torvalds 	if (!data)
8211da177e4SLinus Torvalds 		goto nodata;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
8241da177e4SLinus Torvalds 	 * optimized for the cases when header is void. */
8254305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
826b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail);
8274305b541SArnaldo Carvalho de Melo #else
828b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail - skb->head);
82927a884dcSArnaldo Carvalho de Melo #endif
8304305b541SArnaldo Carvalho de Melo 	memcpy(data + size, skb_end_pointer(skb),
8314305b541SArnaldo Carvalho de Melo 	       sizeof(struct skb_shared_info));
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
8341da177e4SLinus Torvalds 		get_page(skb_shinfo(skb)->frags[i].page);
8351da177e4SLinus Torvalds 
836fbb398a8SDavid S. Miller 	if (skb_has_frags(skb))
8371da177e4SLinus Torvalds 		skb_clone_fraglist(skb);
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	skb_release_data(skb);
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	skb->head     = data;
8441da177e4SLinus Torvalds 	skb->data    += off;
8454305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
8464305b541SArnaldo Carvalho de Melo 	skb->end      = size;
84756eb8882SPatrick McHardy 	off           = nhead;
8484305b541SArnaldo Carvalho de Melo #else
8494305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
85056eb8882SPatrick McHardy #endif
85127a884dcSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header and tail are relative to skb->head */
85227a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
853b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header += off;
854b0e380b1SArnaldo Carvalho de Melo 	skb->network_header   += off;
855603a8bbeSStephen Hemminger 	if (skb_mac_header_was_set(skb))
856b0e380b1SArnaldo Carvalho de Melo 		skb->mac_header += off;
857172a863fSHerbert Xu 	skb->csum_start       += nhead;
8581da177e4SLinus Torvalds 	skb->cloned   = 0;
859334a8132SPatrick McHardy 	skb->hdr_len  = 0;
8601da177e4SLinus Torvalds 	skb->nohdr    = 0;
8611da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
8621da177e4SLinus Torvalds 	return 0;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds nodata:
8651da177e4SLinus Torvalds 	return -ENOMEM;
8661da177e4SLinus Torvalds }
867b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head);
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
8721da177e4SLinus Torvalds {
8731da177e4SLinus Torvalds 	struct sk_buff *skb2;
8741da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	if (delta <= 0)
8771da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
8781da177e4SLinus Torvalds 	else {
8791da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
8801da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
8811da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
8821da177e4SLinus Torvalds 			kfree_skb(skb2);
8831da177e4SLinus Torvalds 			skb2 = NULL;
8841da177e4SLinus Torvalds 		}
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds 	return skb2;
8871da177e4SLinus Torvalds }
888b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom);
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds /**
8911da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
8921da177e4SLinus Torvalds  *	@skb: buffer to copy
8931da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
8941da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
8951da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
8961da177e4SLinus Torvalds  *
8971da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
8981da177e4SLinus Torvalds  *	allocate additional space.
8991da177e4SLinus Torvalds  *
9001da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
9011da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
9021da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
9031da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
9041da177e4SLinus Torvalds  *
9051da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
9061da177e4SLinus Torvalds  *	is called from an interrupt.
9071da177e4SLinus Torvalds  */
9081da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
90986a76cafSVictor Fusco 				int newheadroom, int newtailroom,
910dd0fc66fSAl Viro 				gfp_t gfp_mask)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	/*
9131da177e4SLinus Torvalds 	 *	Allocate the copy buffer
9141da177e4SLinus Torvalds 	 */
9151da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
9161da177e4SLinus Torvalds 				      gfp_mask);
917efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
9181da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
91952886051SHerbert Xu 	int off;
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	if (!n)
9221da177e4SLinus Torvalds 		return NULL;
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	/* Set the tail pointer and length */
9271da177e4SLinus Torvalds 	skb_put(n, skb->len);
9281da177e4SLinus Torvalds 
929efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
9301da177e4SLinus Torvalds 	head_copy_off = 0;
9311da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
9321da177e4SLinus Torvalds 		head_copy_len = newheadroom;
9331da177e4SLinus Torvalds 	else
9341da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	/* Copy the linear header and data. */
9371da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
9381da177e4SLinus Torvalds 			  skb->len + head_copy_len))
9391da177e4SLinus Torvalds 		BUG();
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	copy_skb_header(n, skb);
9421da177e4SLinus Torvalds 
943efd1e8d5SPatrick McHardy 	off                  = newheadroom - oldheadroom;
94452886051SHerbert Xu 	n->csum_start       += off;
94552886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET
946efd1e8d5SPatrick McHardy 	n->transport_header += off;
947efd1e8d5SPatrick McHardy 	n->network_header   += off;
948603a8bbeSStephen Hemminger 	if (skb_mac_header_was_set(skb))
949efd1e8d5SPatrick McHardy 		n->mac_header += off;
95052886051SHerbert Xu #endif
951efd1e8d5SPatrick McHardy 
9521da177e4SLinus Torvalds 	return n;
9531da177e4SLinus Torvalds }
954b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand);
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds /**
9571da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
9581da177e4SLinus Torvalds  *	@skb: buffer to pad
9591da177e4SLinus Torvalds  *	@pad: space to pad
9601da177e4SLinus Torvalds  *
9611da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
9621da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
9631da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
9641da177e4SLinus Torvalds  *
9655b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
9661da177e4SLinus Torvalds  */
9671da177e4SLinus Torvalds 
9685b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
9691da177e4SLinus Torvalds {
9705b057c6bSHerbert Xu 	int err;
9715b057c6bSHerbert Xu 	int ntail;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
9745b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
9751da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
9765b057c6bSHerbert Xu 		return 0;
9771da177e4SLinus Torvalds 	}
9781da177e4SLinus Torvalds 
9794305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
9805b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
9815b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
9825b057c6bSHerbert Xu 		if (unlikely(err))
9835b057c6bSHerbert Xu 			goto free_skb;
9845b057c6bSHerbert Xu 	}
9855b057c6bSHerbert Xu 
9865b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
9875b057c6bSHerbert Xu 	 * to be audited.
9885b057c6bSHerbert Xu 	 */
9895b057c6bSHerbert Xu 	err = skb_linearize(skb);
9905b057c6bSHerbert Xu 	if (unlikely(err))
9915b057c6bSHerbert Xu 		goto free_skb;
9925b057c6bSHerbert Xu 
9935b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
9945b057c6bSHerbert Xu 	return 0;
9955b057c6bSHerbert Xu 
9965b057c6bSHerbert Xu free_skb:
9971da177e4SLinus Torvalds 	kfree_skb(skb);
9985b057c6bSHerbert Xu 	return err;
9991da177e4SLinus Torvalds }
1000b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad);
10011da177e4SLinus Torvalds 
10020dde3e16SIlpo Järvinen /**
10030dde3e16SIlpo Järvinen  *	skb_put - add data to a buffer
10040dde3e16SIlpo Järvinen  *	@skb: buffer to use
10050dde3e16SIlpo Järvinen  *	@len: amount of data to add
10060dde3e16SIlpo Järvinen  *
10070dde3e16SIlpo Järvinen  *	This function extends the used data area of the buffer. If this would
10080dde3e16SIlpo Järvinen  *	exceed the total buffer size the kernel will panic. A pointer to the
10090dde3e16SIlpo Järvinen  *	first byte of the extra data is returned.
10100dde3e16SIlpo Järvinen  */
10110dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
10120dde3e16SIlpo Järvinen {
10130dde3e16SIlpo Järvinen 	unsigned char *tmp = skb_tail_pointer(skb);
10140dde3e16SIlpo Järvinen 	SKB_LINEAR_ASSERT(skb);
10150dde3e16SIlpo Järvinen 	skb->tail += len;
10160dde3e16SIlpo Järvinen 	skb->len  += len;
10170dde3e16SIlpo Järvinen 	if (unlikely(skb->tail > skb->end))
10180dde3e16SIlpo Järvinen 		skb_over_panic(skb, len, __builtin_return_address(0));
10190dde3e16SIlpo Järvinen 	return tmp;
10200dde3e16SIlpo Järvinen }
10210dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put);
10220dde3e16SIlpo Järvinen 
10236be8ac2fSIlpo Järvinen /**
1024c2aa270aSIlpo Järvinen  *	skb_push - add data to the start of a buffer
1025c2aa270aSIlpo Järvinen  *	@skb: buffer to use
1026c2aa270aSIlpo Järvinen  *	@len: amount of data to add
1027c2aa270aSIlpo Järvinen  *
1028c2aa270aSIlpo Järvinen  *	This function extends the used data area of the buffer at the buffer
1029c2aa270aSIlpo Järvinen  *	start. If this would exceed the total buffer headroom the kernel will
1030c2aa270aSIlpo Järvinen  *	panic. A pointer to the first byte of the extra data is returned.
1031c2aa270aSIlpo Järvinen  */
1032c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
1033c2aa270aSIlpo Järvinen {
1034c2aa270aSIlpo Järvinen 	skb->data -= len;
1035c2aa270aSIlpo Järvinen 	skb->len  += len;
1036c2aa270aSIlpo Järvinen 	if (unlikely(skb->data<skb->head))
1037c2aa270aSIlpo Järvinen 		skb_under_panic(skb, len, __builtin_return_address(0));
1038c2aa270aSIlpo Järvinen 	return skb->data;
1039c2aa270aSIlpo Järvinen }
1040c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push);
1041c2aa270aSIlpo Järvinen 
1042c2aa270aSIlpo Järvinen /**
10436be8ac2fSIlpo Järvinen  *	skb_pull - remove data from the start of a buffer
10446be8ac2fSIlpo Järvinen  *	@skb: buffer to use
10456be8ac2fSIlpo Järvinen  *	@len: amount of data to remove
10466be8ac2fSIlpo Järvinen  *
10476be8ac2fSIlpo Järvinen  *	This function removes data from the start of a buffer, returning
10486be8ac2fSIlpo Järvinen  *	the memory to the headroom. A pointer to the next data in the buffer
10496be8ac2fSIlpo Järvinen  *	is returned. Once the data has been pulled future pushes will overwrite
10506be8ac2fSIlpo Järvinen  *	the old data.
10516be8ac2fSIlpo Järvinen  */
10526be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
10536be8ac2fSIlpo Järvinen {
105447d29646SDavid S. Miller 	return skb_pull_inline(skb, len);
10556be8ac2fSIlpo Järvinen }
10566be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull);
10576be8ac2fSIlpo Järvinen 
1058419ae74eSIlpo Järvinen /**
1059419ae74eSIlpo Järvinen  *	skb_trim - remove end from a buffer
1060419ae74eSIlpo Järvinen  *	@skb: buffer to alter
1061419ae74eSIlpo Järvinen  *	@len: new length
1062419ae74eSIlpo Järvinen  *
1063419ae74eSIlpo Järvinen  *	Cut the length of a buffer down by removing data from the tail. If
1064419ae74eSIlpo Järvinen  *	the buffer is already under the length specified it is not modified.
1065419ae74eSIlpo Järvinen  *	The skb must be linear.
1066419ae74eSIlpo Järvinen  */
1067419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len)
1068419ae74eSIlpo Järvinen {
1069419ae74eSIlpo Järvinen 	if (skb->len > len)
1070419ae74eSIlpo Järvinen 		__skb_trim(skb, len);
1071419ae74eSIlpo Järvinen }
1072419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim);
1073419ae74eSIlpo Järvinen 
10743cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
10751da177e4SLinus Torvalds  */
10761da177e4SLinus Torvalds 
10773cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
10781da177e4SLinus Torvalds {
107927b437c8SHerbert Xu 	struct sk_buff **fragp;
108027b437c8SHerbert Xu 	struct sk_buff *frag;
10811da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
10821da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
10831da177e4SLinus Torvalds 	int i;
108427b437c8SHerbert Xu 	int err;
108527b437c8SHerbert Xu 
108627b437c8SHerbert Xu 	if (skb_cloned(skb) &&
108727b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
108827b437c8SHerbert Xu 		return err;
10891da177e4SLinus Torvalds 
1090f4d26fb3SHerbert Xu 	i = 0;
1091f4d26fb3SHerbert Xu 	if (offset >= len)
1092f4d26fb3SHerbert Xu 		goto drop_pages;
1093f4d26fb3SHerbert Xu 
1094f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
10951da177e4SLinus Torvalds 		int end = offset + skb_shinfo(skb)->frags[i].size;
109627b437c8SHerbert Xu 
109727b437c8SHerbert Xu 		if (end < len) {
10981da177e4SLinus Torvalds 			offset = end;
109927b437c8SHerbert Xu 			continue;
11001da177e4SLinus Torvalds 		}
11011da177e4SLinus Torvalds 
110227b437c8SHerbert Xu 		skb_shinfo(skb)->frags[i++].size = len - offset;
110327b437c8SHerbert Xu 
1104f4d26fb3SHerbert Xu drop_pages:
110527b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
110627b437c8SHerbert Xu 
110727b437c8SHerbert Xu 		for (; i < nfrags; i++)
110827b437c8SHerbert Xu 			put_page(skb_shinfo(skb)->frags[i].page);
110927b437c8SHerbert Xu 
1110fbb398a8SDavid S. Miller 		if (skb_has_frags(skb))
111127b437c8SHerbert Xu 			skb_drop_fraglist(skb);
1112f4d26fb3SHerbert Xu 		goto done;
111327b437c8SHerbert Xu 	}
111427b437c8SHerbert Xu 
111527b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
111627b437c8SHerbert Xu 	     fragp = &frag->next) {
111727b437c8SHerbert Xu 		int end = offset + frag->len;
111827b437c8SHerbert Xu 
111927b437c8SHerbert Xu 		if (skb_shared(frag)) {
112027b437c8SHerbert Xu 			struct sk_buff *nfrag;
112127b437c8SHerbert Xu 
112227b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
112327b437c8SHerbert Xu 			if (unlikely(!nfrag))
112427b437c8SHerbert Xu 				return -ENOMEM;
112527b437c8SHerbert Xu 
112627b437c8SHerbert Xu 			nfrag->next = frag->next;
1127f4d26fb3SHerbert Xu 			kfree_skb(frag);
112827b437c8SHerbert Xu 			frag = nfrag;
112927b437c8SHerbert Xu 			*fragp = frag;
113027b437c8SHerbert Xu 		}
113127b437c8SHerbert Xu 
113227b437c8SHerbert Xu 		if (end < len) {
113327b437c8SHerbert Xu 			offset = end;
113427b437c8SHerbert Xu 			continue;
113527b437c8SHerbert Xu 		}
113627b437c8SHerbert Xu 
113727b437c8SHerbert Xu 		if (end > len &&
113827b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
113927b437c8SHerbert Xu 			return err;
114027b437c8SHerbert Xu 
114127b437c8SHerbert Xu 		if (frag->next)
114227b437c8SHerbert Xu 			skb_drop_list(&frag->next);
114327b437c8SHerbert Xu 		break;
114427b437c8SHerbert Xu 	}
114527b437c8SHerbert Xu 
1146f4d26fb3SHerbert Xu done:
114727b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
11481da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
11491da177e4SLinus Torvalds 		skb->len       = len;
11501da177e4SLinus Torvalds 	} else {
11511da177e4SLinus Torvalds 		skb->len       = len;
11521da177e4SLinus Torvalds 		skb->data_len  = 0;
115327a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
11541da177e4SLinus Torvalds 	}
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	return 0;
11571da177e4SLinus Torvalds }
1158b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds /**
11611da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
11621da177e4SLinus Torvalds  *	@skb: buffer to reallocate
11631da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
11641da177e4SLinus Torvalds  *
11651da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
11661da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
11671da177e4SLinus Torvalds  *	data from fragmented part.
11681da177e4SLinus Torvalds  *
11691da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
11701da177e4SLinus Torvalds  *
11711da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
11721da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
11731da177e4SLinus Torvalds  *
11741da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
11751da177e4SLinus Torvalds  *	reloaded after call to this function.
11761da177e4SLinus Torvalds  */
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
11791da177e4SLinus Torvalds  * when it is necessary.
11801da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
11811da177e4SLinus Torvalds  * 2. It may change skb pointers.
11821da177e4SLinus Torvalds  *
11831da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
11841da177e4SLinus Torvalds  */
11851da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
11861da177e4SLinus Torvalds {
11871da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
11881da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
11891da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
11901da177e4SLinus Torvalds 	 */
11914305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
11921da177e4SLinus Torvalds 
11931da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
11941da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
11951da177e4SLinus Torvalds 				     GFP_ATOMIC))
11961da177e4SLinus Torvalds 			return NULL;
11971da177e4SLinus Torvalds 	}
11981da177e4SLinus Torvalds 
119927a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
12001da177e4SLinus Torvalds 		BUG();
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
12031da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
12041da177e4SLinus Torvalds 	 */
1205fbb398a8SDavid S. Miller 	if (!skb_has_frags(skb))
12061da177e4SLinus Torvalds 		goto pull_pages;
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
12091da177e4SLinus Torvalds 	eat = delta;
12101da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12111da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size >= eat)
12121da177e4SLinus Torvalds 			goto pull_pages;
12131da177e4SLinus Torvalds 		eat -= skb_shinfo(skb)->frags[i].size;
12141da177e4SLinus Torvalds 	}
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
12171da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
12181da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
12191da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
12201da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
12211da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
12221da177e4SLinus Torvalds 	 */
12231da177e4SLinus Torvalds 	if (eat) {
12241da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
12251da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
12261da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 		do {
122909a62660SKris Katterjohn 			BUG_ON(!list);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 			if (list->len <= eat) {
12321da177e4SLinus Torvalds 				/* Eaten as whole. */
12331da177e4SLinus Torvalds 				eat -= list->len;
12341da177e4SLinus Torvalds 				list = list->next;
12351da177e4SLinus Torvalds 				insp = list;
12361da177e4SLinus Torvalds 			} else {
12371da177e4SLinus Torvalds 				/* Eaten partially. */
12381da177e4SLinus Torvalds 
12391da177e4SLinus Torvalds 				if (skb_shared(list)) {
12401da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
12411da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
12421da177e4SLinus Torvalds 					if (!clone)
12431da177e4SLinus Torvalds 						return NULL;
12441da177e4SLinus Torvalds 					insp = list->next;
12451da177e4SLinus Torvalds 					list = clone;
12461da177e4SLinus Torvalds 				} else {
12471da177e4SLinus Torvalds 					/* This may be pulled without
12481da177e4SLinus Torvalds 					 * problems. */
12491da177e4SLinus Torvalds 					insp = list;
12501da177e4SLinus Torvalds 				}
12511da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
12521da177e4SLinus Torvalds 					kfree_skb(clone);
12531da177e4SLinus Torvalds 					return NULL;
12541da177e4SLinus Torvalds 				}
12551da177e4SLinus Torvalds 				break;
12561da177e4SLinus Torvalds 			}
12571da177e4SLinus Torvalds 		} while (eat);
12581da177e4SLinus Torvalds 
12591da177e4SLinus Torvalds 		/* Free pulled out fragments. */
12601da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
12611da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
12621da177e4SLinus Torvalds 			kfree_skb(list);
12631da177e4SLinus Torvalds 		}
12641da177e4SLinus Torvalds 		/* And insert new clone at head. */
12651da177e4SLinus Torvalds 		if (clone) {
12661da177e4SLinus Torvalds 			clone->next = list;
12671da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
12681da177e4SLinus Torvalds 		}
12691da177e4SLinus Torvalds 	}
12701da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
12711da177e4SLinus Torvalds 
12721da177e4SLinus Torvalds pull_pages:
12731da177e4SLinus Torvalds 	eat = delta;
12741da177e4SLinus Torvalds 	k = 0;
12751da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12761da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size <= eat) {
12771da177e4SLinus Torvalds 			put_page(skb_shinfo(skb)->frags[i].page);
12781da177e4SLinus Torvalds 			eat -= skb_shinfo(skb)->frags[i].size;
12791da177e4SLinus Torvalds 		} else {
12801da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
12811da177e4SLinus Torvalds 			if (eat) {
12821da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
12831da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].size -= eat;
12841da177e4SLinus Torvalds 				eat = 0;
12851da177e4SLinus Torvalds 			}
12861da177e4SLinus Torvalds 			k++;
12871da177e4SLinus Torvalds 		}
12881da177e4SLinus Torvalds 	}
12891da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	skb->tail     += delta;
12921da177e4SLinus Torvalds 	skb->data_len -= delta;
12931da177e4SLinus Torvalds 
129427a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
12951da177e4SLinus Torvalds }
1296b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
13011da177e4SLinus Torvalds {
13021a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1303fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1304fbb398a8SDavid S. Miller 	int i, copy;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
13071da177e4SLinus Torvalds 		goto fault;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	/* Copy header. */
13101a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
13111da177e4SLinus Torvalds 		if (copy > len)
13121da177e4SLinus Torvalds 			copy = len;
1313d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
13141da177e4SLinus Torvalds 		if ((len -= copy) == 0)
13151da177e4SLinus Torvalds 			return 0;
13161da177e4SLinus Torvalds 		offset += copy;
13171da177e4SLinus Torvalds 		to     += copy;
13181da177e4SLinus Torvalds 	}
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
13211a028e50SDavid S. Miller 		int end;
13221da177e4SLinus Torvalds 
1323547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
13241a028e50SDavid S. Miller 
13251a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
13261da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
13271da177e4SLinus Torvalds 			u8 *vaddr;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 			if (copy > len)
13301da177e4SLinus Torvalds 				copy = len;
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
13331da177e4SLinus Torvalds 			memcpy(to,
13341a028e50SDavid S. Miller 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
13351a028e50SDavid S. Miller 			       offset - start, copy);
13361da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 			if ((len -= copy) == 0)
13391da177e4SLinus Torvalds 				return 0;
13401da177e4SLinus Torvalds 			offset += copy;
13411da177e4SLinus Torvalds 			to     += copy;
13421da177e4SLinus Torvalds 		}
13431a028e50SDavid S. Miller 		start = end;
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds 
1346fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
13471a028e50SDavid S. Miller 		int end;
13481da177e4SLinus Torvalds 
1349547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
13501a028e50SDavid S. Miller 
1351fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
13521da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
13531da177e4SLinus Torvalds 			if (copy > len)
13541da177e4SLinus Torvalds 				copy = len;
1355fbb398a8SDavid S. Miller 			if (skb_copy_bits(frag_iter, offset - start, to, copy))
13561da177e4SLinus Torvalds 				goto fault;
13571da177e4SLinus Torvalds 			if ((len -= copy) == 0)
13581da177e4SLinus Torvalds 				return 0;
13591da177e4SLinus Torvalds 			offset += copy;
13601da177e4SLinus Torvalds 			to     += copy;
13611da177e4SLinus Torvalds 		}
13621a028e50SDavid S. Miller 		start = end;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 	if (!len)
13651da177e4SLinus Torvalds 		return 0;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds fault:
13681da177e4SLinus Torvalds 	return -EFAULT;
13691da177e4SLinus Torvalds }
1370b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits);
13711da177e4SLinus Torvalds 
13729c55e01cSJens Axboe /*
13739c55e01cSJens Axboe  * Callback from splice_to_pipe(), if we need to release some pages
13749c55e01cSJens Axboe  * at the end of the spd in case we error'ed out in filling the pipe.
13759c55e01cSJens Axboe  */
13769c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i)
13779c55e01cSJens Axboe {
13788b9d3728SJarek Poplawski 	put_page(spd->pages[i]);
13798b9d3728SJarek Poplawski }
13809c55e01cSJens Axboe 
13814fb66994SJarek Poplawski static inline struct page *linear_to_page(struct page *page, unsigned int *len,
13824fb66994SJarek Poplawski 					  unsigned int *offset,
13837a67e56fSJarek Poplawski 					  struct sk_buff *skb, struct sock *sk)
13848b9d3728SJarek Poplawski {
13854fb66994SJarek Poplawski 	struct page *p = sk->sk_sndmsg_page;
13864fb66994SJarek Poplawski 	unsigned int off;
13878b9d3728SJarek Poplawski 
13884fb66994SJarek Poplawski 	if (!p) {
13894fb66994SJarek Poplawski new_page:
13904fb66994SJarek Poplawski 		p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0);
13918b9d3728SJarek Poplawski 		if (!p)
13928b9d3728SJarek Poplawski 			return NULL;
13934fb66994SJarek Poplawski 
13944fb66994SJarek Poplawski 		off = sk->sk_sndmsg_off = 0;
13954fb66994SJarek Poplawski 		/* hold one ref to this page until it's full */
13964fb66994SJarek Poplawski 	} else {
13974fb66994SJarek Poplawski 		unsigned int mlen;
13984fb66994SJarek Poplawski 
13994fb66994SJarek Poplawski 		off = sk->sk_sndmsg_off;
14004fb66994SJarek Poplawski 		mlen = PAGE_SIZE - off;
14014fb66994SJarek Poplawski 		if (mlen < 64 && mlen < *len) {
14024fb66994SJarek Poplawski 			put_page(p);
14034fb66994SJarek Poplawski 			goto new_page;
14044fb66994SJarek Poplawski 		}
14054fb66994SJarek Poplawski 
14064fb66994SJarek Poplawski 		*len = min_t(unsigned int, *len, mlen);
14074fb66994SJarek Poplawski 	}
14084fb66994SJarek Poplawski 
14094fb66994SJarek Poplawski 	memcpy(page_address(p) + off, page_address(page) + *offset, *len);
14104fb66994SJarek Poplawski 	sk->sk_sndmsg_off += *len;
14114fb66994SJarek Poplawski 	*offset = off;
14124fb66994SJarek Poplawski 	get_page(p);
14138b9d3728SJarek Poplawski 
14148b9d3728SJarek Poplawski 	return p;
14159c55e01cSJens Axboe }
14169c55e01cSJens Axboe 
14179c55e01cSJens Axboe /*
14189c55e01cSJens Axboe  * Fill page/offset/length into spd, if it can hold more pages.
14199c55e01cSJens Axboe  */
14209c55e01cSJens Axboe static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
14214fb66994SJarek Poplawski 				unsigned int *len, unsigned int offset,
14227a67e56fSJarek Poplawski 				struct sk_buff *skb, int linear,
14237a67e56fSJarek Poplawski 				struct sock *sk)
14249c55e01cSJens Axboe {
14259c55e01cSJens Axboe 	if (unlikely(spd->nr_pages == PIPE_BUFFERS))
14269c55e01cSJens Axboe 		return 1;
14279c55e01cSJens Axboe 
14288b9d3728SJarek Poplawski 	if (linear) {
14297a67e56fSJarek Poplawski 		page = linear_to_page(page, len, &offset, skb, sk);
14308b9d3728SJarek Poplawski 		if (!page)
14318b9d3728SJarek Poplawski 			return 1;
14328b9d3728SJarek Poplawski 	} else
14338b9d3728SJarek Poplawski 		get_page(page);
14348b9d3728SJarek Poplawski 
14359c55e01cSJens Axboe 	spd->pages[spd->nr_pages] = page;
14364fb66994SJarek Poplawski 	spd->partial[spd->nr_pages].len = *len;
14379c55e01cSJens Axboe 	spd->partial[spd->nr_pages].offset = offset;
14389c55e01cSJens Axboe 	spd->nr_pages++;
14398b9d3728SJarek Poplawski 
14409c55e01cSJens Axboe 	return 0;
14419c55e01cSJens Axboe }
14429c55e01cSJens Axboe 
14432870c43dSOctavian Purdila static inline void __segment_seek(struct page **page, unsigned int *poff,
14442870c43dSOctavian Purdila 				  unsigned int *plen, unsigned int off)
14452870c43dSOctavian Purdila {
1446ce3dd395SJarek Poplawski 	unsigned long n;
1447ce3dd395SJarek Poplawski 
14482870c43dSOctavian Purdila 	*poff += off;
1449ce3dd395SJarek Poplawski 	n = *poff / PAGE_SIZE;
1450ce3dd395SJarek Poplawski 	if (n)
1451ce3dd395SJarek Poplawski 		*page = nth_page(*page, n);
1452ce3dd395SJarek Poplawski 
14532870c43dSOctavian Purdila 	*poff = *poff % PAGE_SIZE;
14542870c43dSOctavian Purdila 	*plen -= off;
14552870c43dSOctavian Purdila }
14562870c43dSOctavian Purdila 
14572870c43dSOctavian Purdila static inline int __splice_segment(struct page *page, unsigned int poff,
14582870c43dSOctavian Purdila 				   unsigned int plen, unsigned int *off,
14592870c43dSOctavian Purdila 				   unsigned int *len, struct sk_buff *skb,
14607a67e56fSJarek Poplawski 				   struct splice_pipe_desc *spd, int linear,
14617a67e56fSJarek Poplawski 				   struct sock *sk)
14629c55e01cSJens Axboe {
14632870c43dSOctavian Purdila 	if (!*len)
14642870c43dSOctavian Purdila 		return 1;
14659c55e01cSJens Axboe 
14662870c43dSOctavian Purdila 	/* skip this segment if already processed */
14672870c43dSOctavian Purdila 	if (*off >= plen) {
14682870c43dSOctavian Purdila 		*off -= plen;
14692870c43dSOctavian Purdila 		return 0;
14702870c43dSOctavian Purdila 	}
14712870c43dSOctavian Purdila 
14722870c43dSOctavian Purdila 	/* ignore any bits we already processed */
14732870c43dSOctavian Purdila 	if (*off) {
14742870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, *off);
14752870c43dSOctavian Purdila 		*off = 0;
14762870c43dSOctavian Purdila 	}
14772870c43dSOctavian Purdila 
14782870c43dSOctavian Purdila 	do {
14792870c43dSOctavian Purdila 		unsigned int flen = min(*len, plen);
14802870c43dSOctavian Purdila 
14812870c43dSOctavian Purdila 		/* the linear region may spread across several pages  */
14822870c43dSOctavian Purdila 		flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
14832870c43dSOctavian Purdila 
14847a67e56fSJarek Poplawski 		if (spd_fill_page(spd, page, &flen, poff, skb, linear, sk))
14852870c43dSOctavian Purdila 			return 1;
14862870c43dSOctavian Purdila 
14872870c43dSOctavian Purdila 		__segment_seek(&page, &poff, &plen, flen);
14882870c43dSOctavian Purdila 		*len -= flen;
14892870c43dSOctavian Purdila 
14902870c43dSOctavian Purdila 	} while (*len && plen);
14912870c43dSOctavian Purdila 
14922870c43dSOctavian Purdila 	return 0;
1493db43a282SOctavian Purdila }
14949c55e01cSJens Axboe 
14959c55e01cSJens Axboe /*
14962870c43dSOctavian Purdila  * Map linear and fragment data from the skb to spd. It reports failure if the
14972870c43dSOctavian Purdila  * pipe is full or if we already spliced the requested length.
14989c55e01cSJens Axboe  */
14997b1c65faSHarvey Harrison static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
15007a67e56fSJarek Poplawski 			     unsigned int *len, struct splice_pipe_desc *spd,
15017a67e56fSJarek Poplawski 			     struct sock *sk)
15022870c43dSOctavian Purdila {
15032870c43dSOctavian Purdila 	int seg;
15049c55e01cSJens Axboe 
15059c55e01cSJens Axboe 	/*
15062870c43dSOctavian Purdila 	 * map the linear part
15079c55e01cSJens Axboe 	 */
15082870c43dSOctavian Purdila 	if (__splice_segment(virt_to_page(skb->data),
15092870c43dSOctavian Purdila 			     (unsigned long) skb->data & (PAGE_SIZE - 1),
15102870c43dSOctavian Purdila 			     skb_headlen(skb),
15117a67e56fSJarek Poplawski 			     offset, len, skb, spd, 1, sk))
15122870c43dSOctavian Purdila 		return 1;
15139c55e01cSJens Axboe 
15149c55e01cSJens Axboe 	/*
15159c55e01cSJens Axboe 	 * then map the fragments
15169c55e01cSJens Axboe 	 */
15179c55e01cSJens Axboe 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
15189c55e01cSJens Axboe 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
15199c55e01cSJens Axboe 
15202870c43dSOctavian Purdila 		if (__splice_segment(f->page, f->page_offset, f->size,
15217a67e56fSJarek Poplawski 				     offset, len, skb, spd, 0, sk))
15222870c43dSOctavian Purdila 			return 1;
15239c55e01cSJens Axboe 	}
15249c55e01cSJens Axboe 
15259c55e01cSJens Axboe 	return 0;
15269c55e01cSJens Axboe }
15279c55e01cSJens Axboe 
15289c55e01cSJens Axboe /*
15299c55e01cSJens Axboe  * Map data from the skb to a pipe. Should handle both the linear part,
15309c55e01cSJens Axboe  * the fragments, and the frag list. It does NOT handle frag lists within
15319c55e01cSJens Axboe  * the frag list, if such a thing exists. We'd probably need to recurse to
15329c55e01cSJens Axboe  * handle that cleanly.
15339c55e01cSJens Axboe  */
15348b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset,
15359c55e01cSJens Axboe 		    struct pipe_inode_info *pipe, unsigned int tlen,
15369c55e01cSJens Axboe 		    unsigned int flags)
15379c55e01cSJens Axboe {
15389c55e01cSJens Axboe 	struct partial_page partial[PIPE_BUFFERS];
15399c55e01cSJens Axboe 	struct page *pages[PIPE_BUFFERS];
15409c55e01cSJens Axboe 	struct splice_pipe_desc spd = {
15419c55e01cSJens Axboe 		.pages = pages,
15429c55e01cSJens Axboe 		.partial = partial,
15439c55e01cSJens Axboe 		.flags = flags,
15449c55e01cSJens Axboe 		.ops = &sock_pipe_buf_ops,
15459c55e01cSJens Axboe 		.spd_release = sock_spd_release,
15469c55e01cSJens Axboe 	};
1547fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
15487a67e56fSJarek Poplawski 	struct sock *sk = skb->sk;
15499c55e01cSJens Axboe 
15509c55e01cSJens Axboe 	/*
15519c55e01cSJens Axboe 	 * __skb_splice_bits() only fails if the output has no room left,
15529c55e01cSJens Axboe 	 * so no point in going over the frag_list for the error case.
15539c55e01cSJens Axboe 	 */
15547a67e56fSJarek Poplawski 	if (__skb_splice_bits(skb, &offset, &tlen, &spd, sk))
15559c55e01cSJens Axboe 		goto done;
15569c55e01cSJens Axboe 	else if (!tlen)
15579c55e01cSJens Axboe 		goto done;
15589c55e01cSJens Axboe 
15599c55e01cSJens Axboe 	/*
15609c55e01cSJens Axboe 	 * now see if we have a frag_list to map
15619c55e01cSJens Axboe 	 */
1562fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
1563fbb398a8SDavid S. Miller 		if (!tlen)
15649c55e01cSJens Axboe 			break;
1565fbb398a8SDavid S. Miller 		if (__skb_splice_bits(frag_iter, &offset, &tlen, &spd, sk))
1566fbb398a8SDavid S. Miller 			break;
15679c55e01cSJens Axboe 	}
15689c55e01cSJens Axboe 
15699c55e01cSJens Axboe done:
15709c55e01cSJens Axboe 	if (spd.nr_pages) {
15719c55e01cSJens Axboe 		int ret;
15729c55e01cSJens Axboe 
15739c55e01cSJens Axboe 		/*
15749c55e01cSJens Axboe 		 * Drop the socket lock, otherwise we have reverse
15759c55e01cSJens Axboe 		 * locking dependencies between sk_lock and i_mutex
15769c55e01cSJens Axboe 		 * here as compared to sendfile(). We enter here
15779c55e01cSJens Axboe 		 * with the socket lock held, and splice_to_pipe() will
15789c55e01cSJens Axboe 		 * grab the pipe inode lock. For sendfile() emulation,
15799c55e01cSJens Axboe 		 * we call into ->sendpage() with the i_mutex lock held
15809c55e01cSJens Axboe 		 * and networking will grab the socket lock.
15819c55e01cSJens Axboe 		 */
1582293ad604SOctavian Purdila 		release_sock(sk);
15839c55e01cSJens Axboe 		ret = splice_to_pipe(pipe, &spd);
1584293ad604SOctavian Purdila 		lock_sock(sk);
15859c55e01cSJens Axboe 		return ret;
15869c55e01cSJens Axboe 	}
15879c55e01cSJens Axboe 
15889c55e01cSJens Axboe 	return 0;
15899c55e01cSJens Axboe }
15909c55e01cSJens Axboe 
1591357b40a1SHerbert Xu /**
1592357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1593357b40a1SHerbert Xu  *	@skb: destination buffer
1594357b40a1SHerbert Xu  *	@offset: offset in destination
1595357b40a1SHerbert Xu  *	@from: source buffer
1596357b40a1SHerbert Xu  *	@len: number of bytes to copy
1597357b40a1SHerbert Xu  *
1598357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1599357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1600357b40a1SHerbert Xu  *	traversing fragment lists and such.
1601357b40a1SHerbert Xu  */
1602357b40a1SHerbert Xu 
16030c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1604357b40a1SHerbert Xu {
16051a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1606fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
1607fbb398a8SDavid S. Miller 	int i, copy;
1608357b40a1SHerbert Xu 
1609357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1610357b40a1SHerbert Xu 		goto fault;
1611357b40a1SHerbert Xu 
16121a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1613357b40a1SHerbert Xu 		if (copy > len)
1614357b40a1SHerbert Xu 			copy = len;
161527d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1616357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1617357b40a1SHerbert Xu 			return 0;
1618357b40a1SHerbert Xu 		offset += copy;
1619357b40a1SHerbert Xu 		from += copy;
1620357b40a1SHerbert Xu 	}
1621357b40a1SHerbert Xu 
1622357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1623357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
16241a028e50SDavid S. Miller 		int end;
1625357b40a1SHerbert Xu 
1626547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16271a028e50SDavid S. Miller 
16281a028e50SDavid S. Miller 		end = start + frag->size;
1629357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1630357b40a1SHerbert Xu 			u8 *vaddr;
1631357b40a1SHerbert Xu 
1632357b40a1SHerbert Xu 			if (copy > len)
1633357b40a1SHerbert Xu 				copy = len;
1634357b40a1SHerbert Xu 
1635357b40a1SHerbert Xu 			vaddr = kmap_skb_frag(frag);
16361a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
16371a028e50SDavid S. Miller 			       from, copy);
1638357b40a1SHerbert Xu 			kunmap_skb_frag(vaddr);
1639357b40a1SHerbert Xu 
1640357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1641357b40a1SHerbert Xu 				return 0;
1642357b40a1SHerbert Xu 			offset += copy;
1643357b40a1SHerbert Xu 			from += copy;
1644357b40a1SHerbert Xu 		}
16451a028e50SDavid S. Miller 		start = end;
1646357b40a1SHerbert Xu 	}
1647357b40a1SHerbert Xu 
1648fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
16491a028e50SDavid S. Miller 		int end;
1650357b40a1SHerbert Xu 
1651547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
16521a028e50SDavid S. Miller 
1653fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
1654357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1655357b40a1SHerbert Xu 			if (copy > len)
1656357b40a1SHerbert Xu 				copy = len;
1657fbb398a8SDavid S. Miller 			if (skb_store_bits(frag_iter, offset - start,
16581a028e50SDavid S. Miller 					   from, copy))
1659357b40a1SHerbert Xu 				goto fault;
1660357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1661357b40a1SHerbert Xu 				return 0;
1662357b40a1SHerbert Xu 			offset += copy;
1663357b40a1SHerbert Xu 			from += copy;
1664357b40a1SHerbert Xu 		}
16651a028e50SDavid S. Miller 		start = end;
1666357b40a1SHerbert Xu 	}
1667357b40a1SHerbert Xu 	if (!len)
1668357b40a1SHerbert Xu 		return 0;
1669357b40a1SHerbert Xu 
1670357b40a1SHerbert Xu fault:
1671357b40a1SHerbert Xu 	return -EFAULT;
1672357b40a1SHerbert Xu }
1673357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1674357b40a1SHerbert Xu 
16751da177e4SLinus Torvalds /* Checksum skb data. */
16761da177e4SLinus Torvalds 
16772bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset,
16782bbbc868SAl Viro 			  int len, __wsum csum)
16791da177e4SLinus Torvalds {
16801a028e50SDavid S. Miller 	int start = skb_headlen(skb);
16811a028e50SDavid S. Miller 	int i, copy = start - offset;
1682fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
16831da177e4SLinus Torvalds 	int pos = 0;
16841da177e4SLinus Torvalds 
16851da177e4SLinus Torvalds 	/* Checksum header. */
16861da177e4SLinus Torvalds 	if (copy > 0) {
16871da177e4SLinus Torvalds 		if (copy > len)
16881da177e4SLinus Torvalds 			copy = len;
16891da177e4SLinus Torvalds 		csum = csum_partial(skb->data + offset, copy, csum);
16901da177e4SLinus Torvalds 		if ((len -= copy) == 0)
16911da177e4SLinus Torvalds 			return csum;
16921da177e4SLinus Torvalds 		offset += copy;
16931da177e4SLinus Torvalds 		pos	= copy;
16941da177e4SLinus Torvalds 	}
16951da177e4SLinus Torvalds 
16961da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
16971a028e50SDavid S. Miller 		int end;
16981da177e4SLinus Torvalds 
1699547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17001a028e50SDavid S. Miller 
17011a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
17021da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
170344bb9363SAl Viro 			__wsum csum2;
17041da177e4SLinus Torvalds 			u8 *vaddr;
17051da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 			if (copy > len)
17081da177e4SLinus Torvalds 				copy = len;
17091da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
17101a028e50SDavid S. Miller 			csum2 = csum_partial(vaddr + frag->page_offset +
17111a028e50SDavid S. Miller 					     offset - start, copy, 0);
17121da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
17131da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
17141da177e4SLinus Torvalds 			if (!(len -= copy))
17151da177e4SLinus Torvalds 				return csum;
17161da177e4SLinus Torvalds 			offset += copy;
17171da177e4SLinus Torvalds 			pos    += copy;
17181da177e4SLinus Torvalds 		}
17191a028e50SDavid S. Miller 		start = end;
17201da177e4SLinus Torvalds 	}
17211da177e4SLinus Torvalds 
1722fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
17231a028e50SDavid S. Miller 		int end;
17241da177e4SLinus Torvalds 
1725547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17261a028e50SDavid S. Miller 
1727fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
17281da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17295f92a738SAl Viro 			__wsum csum2;
17301da177e4SLinus Torvalds 			if (copy > len)
17311da177e4SLinus Torvalds 				copy = len;
1732fbb398a8SDavid S. Miller 			csum2 = skb_checksum(frag_iter, offset - start,
17331a028e50SDavid S. Miller 					     copy, 0);
17341da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
17351da177e4SLinus Torvalds 			if ((len -= copy) == 0)
17361da177e4SLinus Torvalds 				return csum;
17371da177e4SLinus Torvalds 			offset += copy;
17381da177e4SLinus Torvalds 			pos    += copy;
17391da177e4SLinus Torvalds 		}
17401a028e50SDavid S. Miller 		start = end;
17411da177e4SLinus Torvalds 	}
174209a62660SKris Katterjohn 	BUG_ON(len);
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	return csum;
17451da177e4SLinus Torvalds }
1746b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum);
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds /* Both of above in one bottle. */
17491da177e4SLinus Torvalds 
175081d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
175181d77662SAl Viro 				    u8 *to, int len, __wsum csum)
17521da177e4SLinus Torvalds {
17531a028e50SDavid S. Miller 	int start = skb_headlen(skb);
17541a028e50SDavid S. Miller 	int i, copy = start - offset;
1755fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
17561da177e4SLinus Torvalds 	int pos = 0;
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	/* Copy header. */
17591da177e4SLinus Torvalds 	if (copy > 0) {
17601da177e4SLinus Torvalds 		if (copy > len)
17611da177e4SLinus Torvalds 			copy = len;
17621da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
17631da177e4SLinus Torvalds 						 copy, csum);
17641da177e4SLinus Torvalds 		if ((len -= copy) == 0)
17651da177e4SLinus Torvalds 			return csum;
17661da177e4SLinus Torvalds 		offset += copy;
17671da177e4SLinus Torvalds 		to     += copy;
17681da177e4SLinus Torvalds 		pos	= copy;
17691da177e4SLinus Torvalds 	}
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
17721a028e50SDavid S. Miller 		int end;
17731da177e4SLinus Torvalds 
1774547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
17751a028e50SDavid S. Miller 
17761a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
17771da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
17785084205fSAl Viro 			__wsum csum2;
17791da177e4SLinus Torvalds 			u8 *vaddr;
17801da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 			if (copy > len)
17831da177e4SLinus Torvalds 				copy = len;
17841da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
17851da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
17861a028e50SDavid S. Miller 							  frag->page_offset +
17871a028e50SDavid S. Miller 							  offset - start, to,
17881a028e50SDavid S. Miller 							  copy, 0);
17891da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
17901da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
17911da177e4SLinus Torvalds 			if (!(len -= copy))
17921da177e4SLinus Torvalds 				return csum;
17931da177e4SLinus Torvalds 			offset += copy;
17941da177e4SLinus Torvalds 			to     += copy;
17951da177e4SLinus Torvalds 			pos    += copy;
17961da177e4SLinus Torvalds 		}
17971a028e50SDavid S. Miller 		start = end;
17981da177e4SLinus Torvalds 	}
17991da177e4SLinus Torvalds 
1800fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
180181d77662SAl Viro 		__wsum csum2;
18021a028e50SDavid S. Miller 		int end;
18031da177e4SLinus Torvalds 
1804547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
18051a028e50SDavid S. Miller 
1806fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
18071da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
18081da177e4SLinus Torvalds 			if (copy > len)
18091da177e4SLinus Torvalds 				copy = len;
1810fbb398a8SDavid S. Miller 			csum2 = skb_copy_and_csum_bits(frag_iter,
18111a028e50SDavid S. Miller 						       offset - start,
18121da177e4SLinus Torvalds 						       to, copy, 0);
18131da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
18141da177e4SLinus Torvalds 			if ((len -= copy) == 0)
18151da177e4SLinus Torvalds 				return csum;
18161da177e4SLinus Torvalds 			offset += copy;
18171da177e4SLinus Torvalds 			to     += copy;
18181da177e4SLinus Torvalds 			pos    += copy;
18191da177e4SLinus Torvalds 		}
18201a028e50SDavid S. Miller 		start = end;
18211da177e4SLinus Torvalds 	}
182209a62660SKris Katterjohn 	BUG_ON(len);
18231da177e4SLinus Torvalds 	return csum;
18241da177e4SLinus Torvalds }
1825b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits);
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
18281da177e4SLinus Torvalds {
1829d3bc23e7SAl Viro 	__wsum csum;
18301da177e4SLinus Torvalds 	long csstart;
18311da177e4SLinus Torvalds 
183284fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1833663ead3bSHerbert Xu 		csstart = skb->csum_start - skb_headroom(skb);
18341da177e4SLinus Torvalds 	else
18351da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
18361da177e4SLinus Torvalds 
183709a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
18381da177e4SLinus Torvalds 
1839d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	csum = 0;
18421da177e4SLinus Torvalds 	if (csstart != skb->len)
18431da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
18441da177e4SLinus Torvalds 					      skb->len - csstart, 0);
18451da177e4SLinus Torvalds 
184684fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1847ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
18481da177e4SLinus Torvalds 
1849d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
18501da177e4SLinus Torvalds 	}
18511da177e4SLinus Torvalds }
1852b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev);
18531da177e4SLinus Torvalds 
18541da177e4SLinus Torvalds /**
18551da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
18561da177e4SLinus Torvalds  *	@list: list to dequeue from
18571da177e4SLinus Torvalds  *
18581da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
18591da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
18601da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
18611da177e4SLinus Torvalds  */
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	unsigned long flags;
18661da177e4SLinus Torvalds 	struct sk_buff *result;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18691da177e4SLinus Torvalds 	result = __skb_dequeue(list);
18701da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18711da177e4SLinus Torvalds 	return result;
18721da177e4SLinus Torvalds }
1873b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue);
18741da177e4SLinus Torvalds 
18751da177e4SLinus Torvalds /**
18761da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
18771da177e4SLinus Torvalds  *	@list: list to dequeue from
18781da177e4SLinus Torvalds  *
18791da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
18801da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
18811da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
18821da177e4SLinus Torvalds  */
18831da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
18841da177e4SLinus Torvalds {
18851da177e4SLinus Torvalds 	unsigned long flags;
18861da177e4SLinus Torvalds 	struct sk_buff *result;
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
18891da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
18901da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
18911da177e4SLinus Torvalds 	return result;
18921da177e4SLinus Torvalds }
1893b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail);
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds /**
18961da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
18971da177e4SLinus Torvalds  *	@list: list to empty
18981da177e4SLinus Torvalds  *
18991da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
19001da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
19011da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
19021da177e4SLinus Torvalds  */
19031da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
19041da177e4SLinus Torvalds {
19051da177e4SLinus Torvalds 	struct sk_buff *skb;
19061da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
19071da177e4SLinus Torvalds 		kfree_skb(skb);
19081da177e4SLinus Torvalds }
1909b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge);
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds /**
19121da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
19131da177e4SLinus Torvalds  *	@list: list to use
19141da177e4SLinus Torvalds  *	@newsk: buffer to queue
19151da177e4SLinus Torvalds  *
19161da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
19171da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
19181da177e4SLinus Torvalds  *	safely.
19191da177e4SLinus Torvalds  *
19201da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19211da177e4SLinus Torvalds  */
19221da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
19231da177e4SLinus Torvalds {
19241da177e4SLinus Torvalds 	unsigned long flags;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
19271da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
19281da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19291da177e4SLinus Torvalds }
1930b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head);
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds /**
19331da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
19341da177e4SLinus Torvalds  *	@list: list to use
19351da177e4SLinus Torvalds  *	@newsk: buffer to queue
19361da177e4SLinus Torvalds  *
19371da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
19381da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
19391da177e4SLinus Torvalds  *	safely.
19401da177e4SLinus Torvalds  *
19411da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19421da177e4SLinus Torvalds  */
19431da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
19441da177e4SLinus Torvalds {
19451da177e4SLinus Torvalds 	unsigned long flags;
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
19481da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
19491da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19501da177e4SLinus Torvalds }
1951b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail);
19528728b834SDavid S. Miller 
19531da177e4SLinus Torvalds /**
19541da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
19551da177e4SLinus Torvalds  *	@skb: buffer to remove
19568728b834SDavid S. Miller  *	@list: list to use
19571da177e4SLinus Torvalds  *
19588728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
19598728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
19601da177e4SLinus Torvalds  *
19618728b834SDavid S. Miller  *	You must know what list the SKB is on.
19621da177e4SLinus Torvalds  */
19638728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
19641da177e4SLinus Torvalds {
19651da177e4SLinus Torvalds 	unsigned long flags;
19661da177e4SLinus Torvalds 
19671da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
19688728b834SDavid S. Miller 	__skb_unlink(skb, list);
19691da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
19701da177e4SLinus Torvalds }
1971b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink);
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds /**
19741da177e4SLinus Torvalds  *	skb_append	-	append a buffer
19751da177e4SLinus Torvalds  *	@old: buffer to insert after
19761da177e4SLinus Torvalds  *	@newsk: buffer to insert
19778728b834SDavid S. Miller  *	@list: list to use
19781da177e4SLinus Torvalds  *
19791da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
19801da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
19811da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
19821da177e4SLinus Torvalds  */
19838728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
19841da177e4SLinus Torvalds {
19851da177e4SLinus Torvalds 	unsigned long flags;
19861da177e4SLinus Torvalds 
19878728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
19887de6c033SGerrit Renker 	__skb_queue_after(list, old, newsk);
19898728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
19901da177e4SLinus Torvalds }
1991b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append);
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds /**
19941da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
19951da177e4SLinus Torvalds  *	@old: buffer to insert before
19961da177e4SLinus Torvalds  *	@newsk: buffer to insert
19978728b834SDavid S. Miller  *	@list: list to use
19981da177e4SLinus Torvalds  *
19998728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
20008728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
20018728b834SDavid S. Miller  *	calls.
20028728b834SDavid S. Miller  *
20031da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
20041da177e4SLinus Torvalds  */
20058728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
20061da177e4SLinus Torvalds {
20071da177e4SLinus Torvalds 	unsigned long flags;
20081da177e4SLinus Torvalds 
20098728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
20108728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
20118728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
20121da177e4SLinus Torvalds }
2013b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
20161da177e4SLinus Torvalds 					   struct sk_buff* skb1,
20171da177e4SLinus Torvalds 					   const u32 len, const int pos)
20181da177e4SLinus Torvalds {
20191da177e4SLinus Torvalds 	int i;
20201da177e4SLinus Torvalds 
2021d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
2022d626f62bSArnaldo Carvalho de Melo 					 pos - len);
20231da177e4SLinus Torvalds 	/* And move data appendix as is. */
20241da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
20251da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
20281da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
20291da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
20301da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
20311da177e4SLinus Torvalds 	skb->data_len		   = 0;
20321da177e4SLinus Torvalds 	skb->len		   = len;
203327a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
20341da177e4SLinus Torvalds }
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
20371da177e4SLinus Torvalds 				       struct sk_buff* skb1,
20381da177e4SLinus Torvalds 				       const u32 len, int pos)
20391da177e4SLinus Torvalds {
20401da177e4SLinus Torvalds 	int i, k = 0;
20411da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
20441da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
20451da177e4SLinus Torvalds 	skb->len		  = len;
20461da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
20491da177e4SLinus Torvalds 		int size = skb_shinfo(skb)->frags[i].size;
20501da177e4SLinus Torvalds 
20511da177e4SLinus Torvalds 		if (pos + size > len) {
20521da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 			if (pos < len) {
20551da177e4SLinus Torvalds 				/* Split frag.
20561da177e4SLinus Torvalds 				 * We have two variants in this case:
20571da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
20581da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
20591da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
20601da177e4SLinus Torvalds 				 *    where splitting is expensive.
20611da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
20621da177e4SLinus Torvalds 				 */
20631da177e4SLinus Torvalds 				get_page(skb_shinfo(skb)->frags[i].page);
20641da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
20651da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].size -= len - pos;
20661da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[i].size	= len - pos;
20671da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
20681da177e4SLinus Torvalds 			}
20691da177e4SLinus Torvalds 			k++;
20701da177e4SLinus Torvalds 		} else
20711da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
20721da177e4SLinus Torvalds 		pos += size;
20731da177e4SLinus Torvalds 	}
20741da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds /**
20781da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
20791da177e4SLinus Torvalds  * @skb: the buffer to split
20801da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
20811da177e4SLinus Torvalds  * @len: new length for skb
20821da177e4SLinus Torvalds  */
20831da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
20841da177e4SLinus Torvalds {
20851da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
20881da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
20891da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
20901da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
20911da177e4SLinus Torvalds }
2092b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split);
20931da177e4SLinus Torvalds 
20949f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go.
20959f782db3SIlpo Järvinen  *
20969f782db3SIlpo Järvinen  * Caller cannot keep skb_shinfo related pointers past calling here!
20979f782db3SIlpo Järvinen  */
2098832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb)
2099832d11c5SIlpo Järvinen {
21000ace2856SIlpo Järvinen 	return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2101832d11c5SIlpo Järvinen }
2102832d11c5SIlpo Järvinen 
2103832d11c5SIlpo Järvinen /**
2104832d11c5SIlpo Järvinen  * skb_shift - Shifts paged data partially from skb to another
2105832d11c5SIlpo Järvinen  * @tgt: buffer into which tail data gets added
2106832d11c5SIlpo Järvinen  * @skb: buffer from which the paged data comes from
2107832d11c5SIlpo Järvinen  * @shiftlen: shift up to this many bytes
2108832d11c5SIlpo Järvinen  *
2109832d11c5SIlpo Järvinen  * Attempts to shift up to shiftlen worth of bytes, which may be less than
2110832d11c5SIlpo Järvinen  * the length of the skb, from tgt to skb. Returns number bytes shifted.
2111832d11c5SIlpo Järvinen  * It's up to caller to free skb if everything was shifted.
2112832d11c5SIlpo Järvinen  *
2113832d11c5SIlpo Järvinen  * If @tgt runs out of frags, the whole operation is aborted.
2114832d11c5SIlpo Järvinen  *
2115832d11c5SIlpo Järvinen  * Skb cannot include anything else but paged data while tgt is allowed
2116832d11c5SIlpo Järvinen  * to have non-paged data as well.
2117832d11c5SIlpo Järvinen  *
2118832d11c5SIlpo Järvinen  * TODO: full sized shift could be optimized but that would need
2119832d11c5SIlpo Järvinen  * specialized skb free'er to handle frags without up-to-date nr_frags.
2120832d11c5SIlpo Järvinen  */
2121832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
2122832d11c5SIlpo Järvinen {
2123832d11c5SIlpo Järvinen 	int from, to, merge, todo;
2124832d11c5SIlpo Järvinen 	struct skb_frag_struct *fragfrom, *fragto;
2125832d11c5SIlpo Järvinen 
2126832d11c5SIlpo Järvinen 	BUG_ON(shiftlen > skb->len);
2127832d11c5SIlpo Järvinen 	BUG_ON(skb_headlen(skb));	/* Would corrupt stream */
2128832d11c5SIlpo Järvinen 
2129832d11c5SIlpo Järvinen 	todo = shiftlen;
2130832d11c5SIlpo Järvinen 	from = 0;
2131832d11c5SIlpo Järvinen 	to = skb_shinfo(tgt)->nr_frags;
2132832d11c5SIlpo Järvinen 	fragfrom = &skb_shinfo(skb)->frags[from];
2133832d11c5SIlpo Järvinen 
2134832d11c5SIlpo Järvinen 	/* Actual merge is delayed until the point when we know we can
2135832d11c5SIlpo Järvinen 	 * commit all, so that we don't have to undo partial changes
2136832d11c5SIlpo Järvinen 	 */
2137832d11c5SIlpo Järvinen 	if (!to ||
2138832d11c5SIlpo Järvinen 	    !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) {
2139832d11c5SIlpo Järvinen 		merge = -1;
2140832d11c5SIlpo Järvinen 	} else {
2141832d11c5SIlpo Järvinen 		merge = to - 1;
2142832d11c5SIlpo Järvinen 
2143832d11c5SIlpo Järvinen 		todo -= fragfrom->size;
2144832d11c5SIlpo Järvinen 		if (todo < 0) {
2145832d11c5SIlpo Järvinen 			if (skb_prepare_for_shift(skb) ||
2146832d11c5SIlpo Järvinen 			    skb_prepare_for_shift(tgt))
2147832d11c5SIlpo Järvinen 				return 0;
2148832d11c5SIlpo Järvinen 
21499f782db3SIlpo Järvinen 			/* All previous frag pointers might be stale! */
21509f782db3SIlpo Järvinen 			fragfrom = &skb_shinfo(skb)->frags[from];
2151832d11c5SIlpo Järvinen 			fragto = &skb_shinfo(tgt)->frags[merge];
2152832d11c5SIlpo Järvinen 
2153832d11c5SIlpo Järvinen 			fragto->size += shiftlen;
2154832d11c5SIlpo Järvinen 			fragfrom->size -= shiftlen;
2155832d11c5SIlpo Järvinen 			fragfrom->page_offset += shiftlen;
2156832d11c5SIlpo Järvinen 
2157832d11c5SIlpo Järvinen 			goto onlymerged;
2158832d11c5SIlpo Järvinen 		}
2159832d11c5SIlpo Järvinen 
2160832d11c5SIlpo Järvinen 		from++;
2161832d11c5SIlpo Järvinen 	}
2162832d11c5SIlpo Järvinen 
2163832d11c5SIlpo Järvinen 	/* Skip full, not-fitting skb to avoid expensive operations */
2164832d11c5SIlpo Järvinen 	if ((shiftlen == skb->len) &&
2165832d11c5SIlpo Järvinen 	    (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to))
2166832d11c5SIlpo Järvinen 		return 0;
2167832d11c5SIlpo Järvinen 
2168832d11c5SIlpo Järvinen 	if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt))
2169832d11c5SIlpo Järvinen 		return 0;
2170832d11c5SIlpo Järvinen 
2171832d11c5SIlpo Järvinen 	while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) {
2172832d11c5SIlpo Järvinen 		if (to == MAX_SKB_FRAGS)
2173832d11c5SIlpo Järvinen 			return 0;
2174832d11c5SIlpo Järvinen 
2175832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[from];
2176832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[to];
2177832d11c5SIlpo Järvinen 
2178832d11c5SIlpo Järvinen 		if (todo >= fragfrom->size) {
2179832d11c5SIlpo Järvinen 			*fragto = *fragfrom;
2180832d11c5SIlpo Järvinen 			todo -= fragfrom->size;
2181832d11c5SIlpo Järvinen 			from++;
2182832d11c5SIlpo Järvinen 			to++;
2183832d11c5SIlpo Järvinen 
2184832d11c5SIlpo Järvinen 		} else {
2185832d11c5SIlpo Järvinen 			get_page(fragfrom->page);
2186832d11c5SIlpo Järvinen 			fragto->page = fragfrom->page;
2187832d11c5SIlpo Järvinen 			fragto->page_offset = fragfrom->page_offset;
2188832d11c5SIlpo Järvinen 			fragto->size = todo;
2189832d11c5SIlpo Järvinen 
2190832d11c5SIlpo Järvinen 			fragfrom->page_offset += todo;
2191832d11c5SIlpo Järvinen 			fragfrom->size -= todo;
2192832d11c5SIlpo Järvinen 			todo = 0;
2193832d11c5SIlpo Järvinen 
2194832d11c5SIlpo Järvinen 			to++;
2195832d11c5SIlpo Järvinen 			break;
2196832d11c5SIlpo Järvinen 		}
2197832d11c5SIlpo Järvinen 	}
2198832d11c5SIlpo Järvinen 
2199832d11c5SIlpo Järvinen 	/* Ready to "commit" this state change to tgt */
2200832d11c5SIlpo Järvinen 	skb_shinfo(tgt)->nr_frags = to;
2201832d11c5SIlpo Järvinen 
2202832d11c5SIlpo Järvinen 	if (merge >= 0) {
2203832d11c5SIlpo Järvinen 		fragfrom = &skb_shinfo(skb)->frags[0];
2204832d11c5SIlpo Järvinen 		fragto = &skb_shinfo(tgt)->frags[merge];
2205832d11c5SIlpo Järvinen 
2206832d11c5SIlpo Järvinen 		fragto->size += fragfrom->size;
2207832d11c5SIlpo Järvinen 		put_page(fragfrom->page);
2208832d11c5SIlpo Järvinen 	}
2209832d11c5SIlpo Järvinen 
2210832d11c5SIlpo Järvinen 	/* Reposition in the original skb */
2211832d11c5SIlpo Järvinen 	to = 0;
2212832d11c5SIlpo Järvinen 	while (from < skb_shinfo(skb)->nr_frags)
2213832d11c5SIlpo Järvinen 		skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++];
2214832d11c5SIlpo Järvinen 	skb_shinfo(skb)->nr_frags = to;
2215832d11c5SIlpo Järvinen 
2216832d11c5SIlpo Järvinen 	BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags);
2217832d11c5SIlpo Järvinen 
2218832d11c5SIlpo Järvinen onlymerged:
2219832d11c5SIlpo Järvinen 	/* Most likely the tgt won't ever need its checksum anymore, skb on
2220832d11c5SIlpo Järvinen 	 * the other hand might need it if it needs to be resent
2221832d11c5SIlpo Järvinen 	 */
2222832d11c5SIlpo Järvinen 	tgt->ip_summed = CHECKSUM_PARTIAL;
2223832d11c5SIlpo Järvinen 	skb->ip_summed = CHECKSUM_PARTIAL;
2224832d11c5SIlpo Järvinen 
2225832d11c5SIlpo Järvinen 	/* Yak, is it really working this way? Some helper please? */
2226832d11c5SIlpo Järvinen 	skb->len -= shiftlen;
2227832d11c5SIlpo Järvinen 	skb->data_len -= shiftlen;
2228832d11c5SIlpo Järvinen 	skb->truesize -= shiftlen;
2229832d11c5SIlpo Järvinen 	tgt->len += shiftlen;
2230832d11c5SIlpo Järvinen 	tgt->data_len += shiftlen;
2231832d11c5SIlpo Järvinen 	tgt->truesize += shiftlen;
2232832d11c5SIlpo Järvinen 
2233832d11c5SIlpo Järvinen 	return shiftlen;
2234832d11c5SIlpo Järvinen }
2235832d11c5SIlpo Järvinen 
2236677e90edSThomas Graf /**
2237677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
2238677e90edSThomas Graf  * @skb: the buffer to read
2239677e90edSThomas Graf  * @from: lower offset of data to be read
2240677e90edSThomas Graf  * @to: upper offset of data to be read
2241677e90edSThomas Graf  * @st: state variable
2242677e90edSThomas Graf  *
2243677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
2244677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
2245677e90edSThomas Graf  */
2246677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
2247677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
2248677e90edSThomas Graf {
2249677e90edSThomas Graf 	st->lower_offset = from;
2250677e90edSThomas Graf 	st->upper_offset = to;
2251677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
2252677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
2253677e90edSThomas Graf 	st->frag_data = NULL;
2254677e90edSThomas Graf }
2255b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read);
2256677e90edSThomas Graf 
2257677e90edSThomas Graf /**
2258677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
2259677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
2260677e90edSThomas Graf  * @data: destination pointer for data to be returned
2261677e90edSThomas Graf  * @st: state variable
2262677e90edSThomas Graf  *
2263677e90edSThomas Graf  * Reads a block of skb data at &consumed relative to the
2264677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
2265677e90edSThomas Graf  * the head of the data block to &data and returns the length
2266677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
2267677e90edSThomas Graf  * offset has been reached.
2268677e90edSThomas Graf  *
2269677e90edSThomas Graf  * The caller is not required to consume all of the data
2270677e90edSThomas Graf  * returned, i.e. &consumed is typically set to the number
2271677e90edSThomas Graf  * of bytes already consumed and the next call to
2272677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
2273677e90edSThomas Graf  *
2274bc2cda1eSRandy Dunlap  * Note 1: The size of each block of data returned can be arbitary,
2275677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
2276677e90edSThomas Graf  *       reads of potentially non linear data.
2277677e90edSThomas Graf  *
2278bc2cda1eSRandy Dunlap  * Note 2: Fragment lists within fragments are not implemented
2279677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
2280677e90edSThomas Graf  *       a stack for this purpose.
2281677e90edSThomas Graf  */
2282677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
2283677e90edSThomas Graf 			  struct skb_seq_state *st)
2284677e90edSThomas Graf {
2285677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
2286677e90edSThomas Graf 	skb_frag_t *frag;
2287677e90edSThomas Graf 
2288677e90edSThomas Graf 	if (unlikely(abs_offset >= st->upper_offset))
2289677e90edSThomas Graf 		return 0;
2290677e90edSThomas Graf 
2291677e90edSThomas Graf next_skb:
229295e3b24cSHerbert Xu 	block_limit = skb_headlen(st->cur_skb) + st->stepped_offset;
2293677e90edSThomas Graf 
2294995b3379SThomas Chenault 	if (abs_offset < block_limit && !st->frag_data) {
229595e3b24cSHerbert Xu 		*data = st->cur_skb->data + (abs_offset - st->stepped_offset);
2296677e90edSThomas Graf 		return block_limit - abs_offset;
2297677e90edSThomas Graf 	}
2298677e90edSThomas Graf 
2299677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
2300677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
2301677e90edSThomas Graf 
2302677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
2303677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
2304677e90edSThomas Graf 		block_limit = frag->size + st->stepped_offset;
2305677e90edSThomas Graf 
2306677e90edSThomas Graf 		if (abs_offset < block_limit) {
2307677e90edSThomas Graf 			if (!st->frag_data)
2308677e90edSThomas Graf 				st->frag_data = kmap_skb_frag(frag);
2309677e90edSThomas Graf 
2310677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
2311677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
2312677e90edSThomas Graf 
2313677e90edSThomas Graf 			return block_limit - abs_offset;
2314677e90edSThomas Graf 		}
2315677e90edSThomas Graf 
2316677e90edSThomas Graf 		if (st->frag_data) {
2317677e90edSThomas Graf 			kunmap_skb_frag(st->frag_data);
2318677e90edSThomas Graf 			st->frag_data = NULL;
2319677e90edSThomas Graf 		}
2320677e90edSThomas Graf 
2321677e90edSThomas Graf 		st->frag_idx++;
2322677e90edSThomas Graf 		st->stepped_offset += frag->size;
2323677e90edSThomas Graf 	}
2324677e90edSThomas Graf 
23255b5a60daSOlaf Kirch 	if (st->frag_data) {
23265b5a60daSOlaf Kirch 		kunmap_skb_frag(st->frag_data);
23275b5a60daSOlaf Kirch 		st->frag_data = NULL;
23285b5a60daSOlaf Kirch 	}
23295b5a60daSOlaf Kirch 
2330fbb398a8SDavid S. Miller 	if (st->root_skb == st->cur_skb && skb_has_frags(st->root_skb)) {
2331677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
233295e3b24cSHerbert Xu 		st->frag_idx = 0;
2333677e90edSThomas Graf 		goto next_skb;
233471b3346dSShyam Iyer 	} else if (st->cur_skb->next) {
233571b3346dSShyam Iyer 		st->cur_skb = st->cur_skb->next;
233671b3346dSShyam Iyer 		st->frag_idx = 0;
2337677e90edSThomas Graf 		goto next_skb;
2338677e90edSThomas Graf 	}
2339677e90edSThomas Graf 
2340677e90edSThomas Graf 	return 0;
2341677e90edSThomas Graf }
2342b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read);
2343677e90edSThomas Graf 
2344677e90edSThomas Graf /**
2345677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
2346677e90edSThomas Graf  * @st: state variable
2347677e90edSThomas Graf  *
2348677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
2349677e90edSThomas Graf  * returned 0.
2350677e90edSThomas Graf  */
2351677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
2352677e90edSThomas Graf {
2353677e90edSThomas Graf 	if (st->frag_data)
2354677e90edSThomas Graf 		kunmap_skb_frag(st->frag_data);
2355677e90edSThomas Graf }
2356b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read);
2357677e90edSThomas Graf 
23583fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
23593fc7e8a6SThomas Graf 
23603fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
23613fc7e8a6SThomas Graf 					  struct ts_config *conf,
23623fc7e8a6SThomas Graf 					  struct ts_state *state)
23633fc7e8a6SThomas Graf {
23643fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
23653fc7e8a6SThomas Graf }
23663fc7e8a6SThomas Graf 
23673fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
23683fc7e8a6SThomas Graf {
23693fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
23703fc7e8a6SThomas Graf }
23713fc7e8a6SThomas Graf 
23723fc7e8a6SThomas Graf /**
23733fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
23743fc7e8a6SThomas Graf  * @skb: the buffer to look in
23753fc7e8a6SThomas Graf  * @from: search offset
23763fc7e8a6SThomas Graf  * @to: search limit
23773fc7e8a6SThomas Graf  * @config: textsearch configuration
23783fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
23793fc7e8a6SThomas Graf  *
23803fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
23813fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
23823fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
23833fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
23843fc7e8a6SThomas Graf  */
23853fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
23863fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
23873fc7e8a6SThomas Graf 			   struct ts_state *state)
23883fc7e8a6SThomas Graf {
2389f72b948dSPhil Oester 	unsigned int ret;
2390f72b948dSPhil Oester 
23913fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
23923fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
23933fc7e8a6SThomas Graf 
23943fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
23953fc7e8a6SThomas Graf 
2396f72b948dSPhil Oester 	ret = textsearch_find(config, state);
2397f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
23983fc7e8a6SThomas Graf }
2399b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text);
24003fc7e8a6SThomas Graf 
2401e89e9cf5SAnanda Raju /**
2402e89e9cf5SAnanda Raju  * skb_append_datato_frags: - append the user data to a skb
2403e89e9cf5SAnanda Raju  * @sk: sock  structure
2404e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
2405e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
2406e89e9cf5SAnanda Raju  * @from: pointer to user message iov
2407e89e9cf5SAnanda Raju  * @length: length of the iov message
2408e89e9cf5SAnanda Raju  *
2409e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
2410e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
2411e89e9cf5SAnanda Raju  */
2412e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
2413dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
2414e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
2415e89e9cf5SAnanda Raju 			void *from, int length)
2416e89e9cf5SAnanda Raju {
2417e89e9cf5SAnanda Raju 	int frg_cnt = 0;
2418e89e9cf5SAnanda Raju 	skb_frag_t *frag = NULL;
2419e89e9cf5SAnanda Raju 	struct page *page = NULL;
2420e89e9cf5SAnanda Raju 	int copy, left;
2421e89e9cf5SAnanda Raju 	int offset = 0;
2422e89e9cf5SAnanda Raju 	int ret;
2423e89e9cf5SAnanda Raju 
2424e89e9cf5SAnanda Raju 	do {
2425e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
2426e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2427e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
2428e89e9cf5SAnanda Raju 			return -EFAULT;
2429e89e9cf5SAnanda Raju 
2430e89e9cf5SAnanda Raju 		/* allocate a new page for next frag */
2431e89e9cf5SAnanda Raju 		page = alloc_pages(sk->sk_allocation, 0);
2432e89e9cf5SAnanda Raju 
2433e89e9cf5SAnanda Raju 		/* If alloc_page fails just return failure and caller will
2434e89e9cf5SAnanda Raju 		 * free previous allocated pages by doing kfree_skb()
2435e89e9cf5SAnanda Raju 		 */
2436e89e9cf5SAnanda Raju 		if (page == NULL)
2437e89e9cf5SAnanda Raju 			return -ENOMEM;
2438e89e9cf5SAnanda Raju 
2439e89e9cf5SAnanda Raju 		/* initialize the next frag */
2440e89e9cf5SAnanda Raju 		sk->sk_sndmsg_page = page;
2441e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off = 0;
2442e89e9cf5SAnanda Raju 		skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
2443e89e9cf5SAnanda Raju 		skb->truesize += PAGE_SIZE;
2444e89e9cf5SAnanda Raju 		atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
2445e89e9cf5SAnanda Raju 
2446e89e9cf5SAnanda Raju 		/* get the new initialized frag */
2447e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
2448e89e9cf5SAnanda Raju 		frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
2449e89e9cf5SAnanda Raju 
2450e89e9cf5SAnanda Raju 		/* copy the user data to page */
2451e89e9cf5SAnanda Raju 		left = PAGE_SIZE - frag->page_offset;
2452e89e9cf5SAnanda Raju 		copy = (length > left)? left : length;
2453e89e9cf5SAnanda Raju 
2454e89e9cf5SAnanda Raju 		ret = getfrag(from, (page_address(frag->page) +
2455e89e9cf5SAnanda Raju 			    frag->page_offset + frag->size),
2456e89e9cf5SAnanda Raju 			    offset, copy, 0, skb);
2457e89e9cf5SAnanda Raju 		if (ret < 0)
2458e89e9cf5SAnanda Raju 			return -EFAULT;
2459e89e9cf5SAnanda Raju 
2460e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
2461e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off += copy;
2462e89e9cf5SAnanda Raju 		frag->size += copy;
2463e89e9cf5SAnanda Raju 		skb->len += copy;
2464e89e9cf5SAnanda Raju 		skb->data_len += copy;
2465e89e9cf5SAnanda Raju 		offset += copy;
2466e89e9cf5SAnanda Raju 		length -= copy;
2467e89e9cf5SAnanda Raju 
2468e89e9cf5SAnanda Raju 	} while (length > 0);
2469e89e9cf5SAnanda Raju 
2470e89e9cf5SAnanda Raju 	return 0;
2471e89e9cf5SAnanda Raju }
2472b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags);
2473e89e9cf5SAnanda Raju 
2474cbb042f9SHerbert Xu /**
2475cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
2476cbb042f9SHerbert Xu  *	@skb: buffer to update
2477cbb042f9SHerbert Xu  *	@len: length of data pulled
2478cbb042f9SHerbert Xu  *
2479cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
2480fee54fa5SUrs Thuermann  *	the CHECKSUM_COMPLETE checksum.  It should be used on
248184fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
248284fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
248384fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
2484cbb042f9SHerbert Xu  */
2485cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
2486cbb042f9SHerbert Xu {
2487cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
2488cbb042f9SHerbert Xu 	skb->len -= len;
2489cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
2490cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
2491cbb042f9SHerbert Xu 	return skb->data += len;
2492cbb042f9SHerbert Xu }
2493cbb042f9SHerbert Xu 
2494f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
2495f94691acSArnaldo Carvalho de Melo 
2496f4c50d99SHerbert Xu /**
2497f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
2498f4c50d99SHerbert Xu  *	@skb: buffer to segment
2499576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
2500f4c50d99SHerbert Xu  *
2501f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
25024c821d75SBen Hutchings  *	a pointer to the first in a list of new skbs for the segments.
25034c821d75SBen Hutchings  *	In case of error it returns ERR_PTR(err).
2504f4c50d99SHerbert Xu  */
2505576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features)
2506f4c50d99SHerbert Xu {
2507f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
2508f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
250989319d38SHerbert Xu 	struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2510f4c50d99SHerbert Xu 	unsigned int mss = skb_shinfo(skb)->gso_size;
251198e399f8SArnaldo Carvalho de Melo 	unsigned int doffset = skb->data - skb_mac_header(skb);
2512f4c50d99SHerbert Xu 	unsigned int offset = doffset;
2513f4c50d99SHerbert Xu 	unsigned int headroom;
2514f4c50d99SHerbert Xu 	unsigned int len;
2515576a30ebSHerbert Xu 	int sg = features & NETIF_F_SG;
2516f4c50d99SHerbert Xu 	int nfrags = skb_shinfo(skb)->nr_frags;
2517f4c50d99SHerbert Xu 	int err = -ENOMEM;
2518f4c50d99SHerbert Xu 	int i = 0;
2519f4c50d99SHerbert Xu 	int pos;
2520f4c50d99SHerbert Xu 
2521f4c50d99SHerbert Xu 	__skb_push(skb, doffset);
2522f4c50d99SHerbert Xu 	headroom = skb_headroom(skb);
2523f4c50d99SHerbert Xu 	pos = skb_headlen(skb);
2524f4c50d99SHerbert Xu 
2525f4c50d99SHerbert Xu 	do {
2526f4c50d99SHerbert Xu 		struct sk_buff *nskb;
2527f4c50d99SHerbert Xu 		skb_frag_t *frag;
2528c8884eddSHerbert Xu 		int hsize;
2529f4c50d99SHerbert Xu 		int size;
2530f4c50d99SHerbert Xu 
2531f4c50d99SHerbert Xu 		len = skb->len - offset;
2532f4c50d99SHerbert Xu 		if (len > mss)
2533f4c50d99SHerbert Xu 			len = mss;
2534f4c50d99SHerbert Xu 
2535f4c50d99SHerbert Xu 		hsize = skb_headlen(skb) - offset;
2536f4c50d99SHerbert Xu 		if (hsize < 0)
2537f4c50d99SHerbert Xu 			hsize = 0;
2538c8884eddSHerbert Xu 		if (hsize > len || !sg)
2539c8884eddSHerbert Xu 			hsize = len;
2540f4c50d99SHerbert Xu 
254189319d38SHerbert Xu 		if (!hsize && i >= nfrags) {
254289319d38SHerbert Xu 			BUG_ON(fskb->len != len);
254389319d38SHerbert Xu 
254489319d38SHerbert Xu 			pos += len;
254589319d38SHerbert Xu 			nskb = skb_clone(fskb, GFP_ATOMIC);
254689319d38SHerbert Xu 			fskb = fskb->next;
254789319d38SHerbert Xu 
2548f4c50d99SHerbert Xu 			if (unlikely(!nskb))
2549f4c50d99SHerbert Xu 				goto err;
2550f4c50d99SHerbert Xu 
255189319d38SHerbert Xu 			hsize = skb_end_pointer(nskb) - nskb->head;
255289319d38SHerbert Xu 			if (skb_cow_head(nskb, doffset + headroom)) {
255389319d38SHerbert Xu 				kfree_skb(nskb);
255489319d38SHerbert Xu 				goto err;
255589319d38SHerbert Xu 			}
255689319d38SHerbert Xu 
255789319d38SHerbert Xu 			nskb->truesize += skb_end_pointer(nskb) - nskb->head -
255889319d38SHerbert Xu 					  hsize;
255989319d38SHerbert Xu 			skb_release_head_state(nskb);
256089319d38SHerbert Xu 			__skb_push(nskb, doffset);
256189319d38SHerbert Xu 		} else {
256289319d38SHerbert Xu 			nskb = alloc_skb(hsize + doffset + headroom,
256389319d38SHerbert Xu 					 GFP_ATOMIC);
256489319d38SHerbert Xu 
256589319d38SHerbert Xu 			if (unlikely(!nskb))
256689319d38SHerbert Xu 				goto err;
256789319d38SHerbert Xu 
256889319d38SHerbert Xu 			skb_reserve(nskb, headroom);
256989319d38SHerbert Xu 			__skb_put(nskb, doffset);
257089319d38SHerbert Xu 		}
257189319d38SHerbert Xu 
2572f4c50d99SHerbert Xu 		if (segs)
2573f4c50d99SHerbert Xu 			tail->next = nskb;
2574f4c50d99SHerbert Xu 		else
2575f4c50d99SHerbert Xu 			segs = nskb;
2576f4c50d99SHerbert Xu 		tail = nskb;
2577f4c50d99SHerbert Xu 
25786f85a124SHerbert Xu 		__copy_skb_header(nskb, skb);
2579f4c50d99SHerbert Xu 		nskb->mac_len = skb->mac_len;
2580f4c50d99SHerbert Xu 
2581459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(nskb);
2582ddc7b8e3SArnaldo Carvalho de Melo 		skb_set_network_header(nskb, skb->mac_len);
2583b0e380b1SArnaldo Carvalho de Melo 		nskb->transport_header = (nskb->network_header +
2584b0e380b1SArnaldo Carvalho de Melo 					  skb_network_header_len(skb));
258589319d38SHerbert Xu 		skb_copy_from_linear_data(skb, nskb->data, doffset);
258689319d38SHerbert Xu 
25872f181855SHerbert Xu 		if (fskb != skb_shinfo(skb)->frag_list)
258889319d38SHerbert Xu 			continue;
258989319d38SHerbert Xu 
2590f4c50d99SHerbert Xu 		if (!sg) {
25916f85a124SHerbert Xu 			nskb->ip_summed = CHECKSUM_NONE;
2592f4c50d99SHerbert Xu 			nskb->csum = skb_copy_and_csum_bits(skb, offset,
2593f4c50d99SHerbert Xu 							    skb_put(nskb, len),
2594f4c50d99SHerbert Xu 							    len, 0);
2595f4c50d99SHerbert Xu 			continue;
2596f4c50d99SHerbert Xu 		}
2597f4c50d99SHerbert Xu 
2598f4c50d99SHerbert Xu 		frag = skb_shinfo(nskb)->frags;
2599f4c50d99SHerbert Xu 
2600d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset,
2601d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
2602f4c50d99SHerbert Xu 
260389319d38SHerbert Xu 		while (pos < offset + len && i < nfrags) {
2604f4c50d99SHerbert Xu 			*frag = skb_shinfo(skb)->frags[i];
2605f4c50d99SHerbert Xu 			get_page(frag->page);
2606f4c50d99SHerbert Xu 			size = frag->size;
2607f4c50d99SHerbert Xu 
2608f4c50d99SHerbert Xu 			if (pos < offset) {
2609f4c50d99SHerbert Xu 				frag->page_offset += offset - pos;
2610f4c50d99SHerbert Xu 				frag->size -= offset - pos;
2611f4c50d99SHerbert Xu 			}
2612f4c50d99SHerbert Xu 
261389319d38SHerbert Xu 			skb_shinfo(nskb)->nr_frags++;
2614f4c50d99SHerbert Xu 
2615f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
2616f4c50d99SHerbert Xu 				i++;
2617f4c50d99SHerbert Xu 				pos += size;
2618f4c50d99SHerbert Xu 			} else {
2619f4c50d99SHerbert Xu 				frag->size -= pos + size - (offset + len);
262089319d38SHerbert Xu 				goto skip_fraglist;
2621f4c50d99SHerbert Xu 			}
2622f4c50d99SHerbert Xu 
2623f4c50d99SHerbert Xu 			frag++;
2624f4c50d99SHerbert Xu 		}
2625f4c50d99SHerbert Xu 
262689319d38SHerbert Xu 		if (pos < offset + len) {
262789319d38SHerbert Xu 			struct sk_buff *fskb2 = fskb;
262889319d38SHerbert Xu 
262989319d38SHerbert Xu 			BUG_ON(pos + fskb->len != offset + len);
263089319d38SHerbert Xu 
263189319d38SHerbert Xu 			pos += fskb->len;
263289319d38SHerbert Xu 			fskb = fskb->next;
263389319d38SHerbert Xu 
263489319d38SHerbert Xu 			if (fskb2->next) {
263589319d38SHerbert Xu 				fskb2 = skb_clone(fskb2, GFP_ATOMIC);
263689319d38SHerbert Xu 				if (!fskb2)
263789319d38SHerbert Xu 					goto err;
263889319d38SHerbert Xu 			} else
263989319d38SHerbert Xu 				skb_get(fskb2);
264089319d38SHerbert Xu 
2641fbb398a8SDavid S. Miller 			SKB_FRAG_ASSERT(nskb);
264289319d38SHerbert Xu 			skb_shinfo(nskb)->frag_list = fskb2;
264389319d38SHerbert Xu 		}
264489319d38SHerbert Xu 
264589319d38SHerbert Xu skip_fraglist:
2646f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
2647f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
2648f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
2649f4c50d99SHerbert Xu 	} while ((offset += len) < skb->len);
2650f4c50d99SHerbert Xu 
2651f4c50d99SHerbert Xu 	return segs;
2652f4c50d99SHerbert Xu 
2653f4c50d99SHerbert Xu err:
2654f4c50d99SHerbert Xu 	while ((skb = segs)) {
2655f4c50d99SHerbert Xu 		segs = skb->next;
2656b08d5840SPatrick McHardy 		kfree_skb(skb);
2657f4c50d99SHerbert Xu 	}
2658f4c50d99SHerbert Xu 	return ERR_PTR(err);
2659f4c50d99SHerbert Xu }
2660f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
2661f4c50d99SHerbert Xu 
266271d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
266371d93b39SHerbert Xu {
266471d93b39SHerbert Xu 	struct sk_buff *p = *head;
266571d93b39SHerbert Xu 	struct sk_buff *nskb;
26669aaa156cSHerbert Xu 	struct skb_shared_info *skbinfo = skb_shinfo(skb);
26679aaa156cSHerbert Xu 	struct skb_shared_info *pinfo = skb_shinfo(p);
266871d93b39SHerbert Xu 	unsigned int headroom;
266986911732SHerbert Xu 	unsigned int len = skb_gro_len(skb);
267067147ba9SHerbert Xu 	unsigned int offset = skb_gro_offset(skb);
267167147ba9SHerbert Xu 	unsigned int headlen = skb_headlen(skb);
267271d93b39SHerbert Xu 
267386911732SHerbert Xu 	if (p->len + len >= 65536)
267471d93b39SHerbert Xu 		return -E2BIG;
267571d93b39SHerbert Xu 
26769aaa156cSHerbert Xu 	if (pinfo->frag_list)
267771d93b39SHerbert Xu 		goto merge;
267867147ba9SHerbert Xu 	else if (headlen <= offset) {
267942da6994SHerbert Xu 		skb_frag_t *frag;
268066e92fcfSHerbert Xu 		skb_frag_t *frag2;
26819aaa156cSHerbert Xu 		int i = skbinfo->nr_frags;
26829aaa156cSHerbert Xu 		int nr_frags = pinfo->nr_frags + i;
268342da6994SHerbert Xu 
268466e92fcfSHerbert Xu 		offset -= headlen;
268566e92fcfSHerbert Xu 
268666e92fcfSHerbert Xu 		if (nr_frags > MAX_SKB_FRAGS)
268781705ad1SHerbert Xu 			return -E2BIG;
268881705ad1SHerbert Xu 
26899aaa156cSHerbert Xu 		pinfo->nr_frags = nr_frags;
26909aaa156cSHerbert Xu 		skbinfo->nr_frags = 0;
2691f5572068SHerbert Xu 
26929aaa156cSHerbert Xu 		frag = pinfo->frags + nr_frags;
26939aaa156cSHerbert Xu 		frag2 = skbinfo->frags + i;
269466e92fcfSHerbert Xu 		do {
269566e92fcfSHerbert Xu 			*--frag = *--frag2;
269666e92fcfSHerbert Xu 		} while (--i);
269766e92fcfSHerbert Xu 
269866e92fcfSHerbert Xu 		frag->page_offset += offset;
269966e92fcfSHerbert Xu 		frag->size -= offset;
270066e92fcfSHerbert Xu 
2701f5572068SHerbert Xu 		skb->truesize -= skb->data_len;
2702f5572068SHerbert Xu 		skb->len -= skb->data_len;
2703f5572068SHerbert Xu 		skb->data_len = 0;
2704f5572068SHerbert Xu 
27055d38a079SHerbert Xu 		NAPI_GRO_CB(skb)->free = 1;
27065d38a079SHerbert Xu 		goto done;
270769c0cab1SHerbert Xu 	} else if (skb_gro_len(p) != pinfo->gso_size)
270869c0cab1SHerbert Xu 		return -E2BIG;
270971d93b39SHerbert Xu 
271071d93b39SHerbert Xu 	headroom = skb_headroom(p);
271186911732SHerbert Xu 	nskb = netdev_alloc_skb(p->dev, headroom + skb_gro_offset(p));
271271d93b39SHerbert Xu 	if (unlikely(!nskb))
271371d93b39SHerbert Xu 		return -ENOMEM;
271471d93b39SHerbert Xu 
271571d93b39SHerbert Xu 	__copy_skb_header(nskb, p);
271671d93b39SHerbert Xu 	nskb->mac_len = p->mac_len;
271771d93b39SHerbert Xu 
271871d93b39SHerbert Xu 	skb_reserve(nskb, headroom);
271986911732SHerbert Xu 	__skb_put(nskb, skb_gro_offset(p));
272071d93b39SHerbert Xu 
272186911732SHerbert Xu 	skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
272271d93b39SHerbert Xu 	skb_set_network_header(nskb, skb_network_offset(p));
272371d93b39SHerbert Xu 	skb_set_transport_header(nskb, skb_transport_offset(p));
272471d93b39SHerbert Xu 
272586911732SHerbert Xu 	__skb_pull(p, skb_gro_offset(p));
272686911732SHerbert Xu 	memcpy(skb_mac_header(nskb), skb_mac_header(p),
272786911732SHerbert Xu 	       p->data - skb_mac_header(p));
272871d93b39SHerbert Xu 
272971d93b39SHerbert Xu 	*NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p);
273071d93b39SHerbert Xu 	skb_shinfo(nskb)->frag_list = p;
27319aaa156cSHerbert Xu 	skb_shinfo(nskb)->gso_size = pinfo->gso_size;
273271d93b39SHerbert Xu 	skb_header_release(p);
273371d93b39SHerbert Xu 	nskb->prev = p;
273471d93b39SHerbert Xu 
273571d93b39SHerbert Xu 	nskb->data_len += p->len;
273671d93b39SHerbert Xu 	nskb->truesize += p->len;
273771d93b39SHerbert Xu 	nskb->len += p->len;
273871d93b39SHerbert Xu 
273971d93b39SHerbert Xu 	*head = nskb;
274071d93b39SHerbert Xu 	nskb->next = p->next;
274171d93b39SHerbert Xu 	p->next = NULL;
274271d93b39SHerbert Xu 
274371d93b39SHerbert Xu 	p = nskb;
274471d93b39SHerbert Xu 
274571d93b39SHerbert Xu merge:
274667147ba9SHerbert Xu 	if (offset > headlen) {
27479aaa156cSHerbert Xu 		skbinfo->frags[0].page_offset += offset - headlen;
27489aaa156cSHerbert Xu 		skbinfo->frags[0].size -= offset - headlen;
274967147ba9SHerbert Xu 		offset = headlen;
275056035022SHerbert Xu 	}
275156035022SHerbert Xu 
275267147ba9SHerbert Xu 	__skb_pull(skb, offset);
275356035022SHerbert Xu 
275471d93b39SHerbert Xu 	p->prev->next = skb;
275571d93b39SHerbert Xu 	p->prev = skb;
275671d93b39SHerbert Xu 	skb_header_release(skb);
275771d93b39SHerbert Xu 
27585d38a079SHerbert Xu done:
27595d38a079SHerbert Xu 	NAPI_GRO_CB(p)->count++;
276037fe4732SHerbert Xu 	p->data_len += len;
276137fe4732SHerbert Xu 	p->truesize += len;
276237fe4732SHerbert Xu 	p->len += len;
276371d93b39SHerbert Xu 
276471d93b39SHerbert Xu 	NAPI_GRO_CB(skb)->same_flow = 1;
276571d93b39SHerbert Xu 	return 0;
276671d93b39SHerbert Xu }
276771d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive);
276871d93b39SHerbert Xu 
27691da177e4SLinus Torvalds void __init skb_init(void)
27701da177e4SLinus Torvalds {
27711da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
27721da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
27731da177e4SLinus Torvalds 					      0,
2774e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
277520c2df83SPaul Mundt 					      NULL);
2776d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2777d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
2778d179cd12SDavid S. Miller 						sizeof(atomic_t),
2779d179cd12SDavid S. Miller 						0,
2780e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
278120c2df83SPaul Mundt 						NULL);
27821da177e4SLinus Torvalds }
27831da177e4SLinus Torvalds 
2784716ea3a7SDavid Howells /**
2785716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2786716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
2787716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
2788716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
2789716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
2790716ea3a7SDavid Howells  *
2791716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
2792716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
2793716ea3a7SDavid Howells  */
279451c739d1SDavid S. Miller static int
279551c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2796716ea3a7SDavid Howells {
27971a028e50SDavid S. Miller 	int start = skb_headlen(skb);
27981a028e50SDavid S. Miller 	int i, copy = start - offset;
2799fbb398a8SDavid S. Miller 	struct sk_buff *frag_iter;
2800716ea3a7SDavid Howells 	int elt = 0;
2801716ea3a7SDavid Howells 
2802716ea3a7SDavid Howells 	if (copy > 0) {
2803716ea3a7SDavid Howells 		if (copy > len)
2804716ea3a7SDavid Howells 			copy = len;
2805642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
2806716ea3a7SDavid Howells 		elt++;
2807716ea3a7SDavid Howells 		if ((len -= copy) == 0)
2808716ea3a7SDavid Howells 			return elt;
2809716ea3a7SDavid Howells 		offset += copy;
2810716ea3a7SDavid Howells 	}
2811716ea3a7SDavid Howells 
2812716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
28131a028e50SDavid S. Miller 		int end;
2814716ea3a7SDavid Howells 
2815547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
28161a028e50SDavid S. Miller 
28171a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
2818716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2819716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2820716ea3a7SDavid Howells 
2821716ea3a7SDavid Howells 			if (copy > len)
2822716ea3a7SDavid Howells 				copy = len;
2823642f1490SJens Axboe 			sg_set_page(&sg[elt], frag->page, copy,
2824642f1490SJens Axboe 					frag->page_offset+offset-start);
2825716ea3a7SDavid Howells 			elt++;
2826716ea3a7SDavid Howells 			if (!(len -= copy))
2827716ea3a7SDavid Howells 				return elt;
2828716ea3a7SDavid Howells 			offset += copy;
2829716ea3a7SDavid Howells 		}
28301a028e50SDavid S. Miller 		start = end;
2831716ea3a7SDavid Howells 	}
2832716ea3a7SDavid Howells 
2833fbb398a8SDavid S. Miller 	skb_walk_frags(skb, frag_iter) {
28341a028e50SDavid S. Miller 		int end;
2835716ea3a7SDavid Howells 
2836547b792cSIlpo Järvinen 		WARN_ON(start > offset + len);
28371a028e50SDavid S. Miller 
2838fbb398a8SDavid S. Miller 		end = start + frag_iter->len;
2839716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2840716ea3a7SDavid Howells 			if (copy > len)
2841716ea3a7SDavid Howells 				copy = len;
2842fbb398a8SDavid S. Miller 			elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start,
284351c739d1SDavid S. Miller 					      copy);
2844716ea3a7SDavid Howells 			if ((len -= copy) == 0)
2845716ea3a7SDavid Howells 				return elt;
2846716ea3a7SDavid Howells 			offset += copy;
2847716ea3a7SDavid Howells 		}
28481a028e50SDavid S. Miller 		start = end;
2849716ea3a7SDavid Howells 	}
2850716ea3a7SDavid Howells 	BUG_ON(len);
2851716ea3a7SDavid Howells 	return elt;
2852716ea3a7SDavid Howells }
2853716ea3a7SDavid Howells 
285451c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
285551c739d1SDavid S. Miller {
285651c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
285751c739d1SDavid S. Miller 
2858c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
285951c739d1SDavid S. Miller 
286051c739d1SDavid S. Miller 	return nsg;
286151c739d1SDavid S. Miller }
2862b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec);
286351c739d1SDavid S. Miller 
2864716ea3a7SDavid Howells /**
2865716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
2866716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
2867716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
2868716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
2869716ea3a7SDavid Howells  *
2870716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
2871716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
2872716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
2873716ea3a7SDavid Howells  *
2874716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
2875716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
2876716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
2877716ea3a7SDavid Howells  *
2878716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
2879716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
2880716ea3a7SDavid Howells  */
2881716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2882716ea3a7SDavid Howells {
2883716ea3a7SDavid Howells 	int copyflag;
2884716ea3a7SDavid Howells 	int elt;
2885716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
2886716ea3a7SDavid Howells 
2887716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
2888716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
2889716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
2890716ea3a7SDavid Howells 	 */
2891716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2892716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2893716ea3a7SDavid Howells 		return -ENOMEM;
2894716ea3a7SDavid Howells 
2895716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
2896fbb398a8SDavid S. Miller 	if (!skb_has_frags(skb)) {
2897716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
2898716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
2899716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
2900716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
2901716ea3a7SDavid Howells 
2902716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
2903716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2904716ea3a7SDavid Howells 			return -ENOMEM;
2905716ea3a7SDavid Howells 
2906716ea3a7SDavid Howells 		/* Voila! */
2907716ea3a7SDavid Howells 		*trailer = skb;
2908716ea3a7SDavid Howells 		return 1;
2909716ea3a7SDavid Howells 	}
2910716ea3a7SDavid Howells 
2911716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
2912716ea3a7SDavid Howells 
2913716ea3a7SDavid Howells 	elt = 1;
2914716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
2915716ea3a7SDavid Howells 	copyflag = 0;
2916716ea3a7SDavid Howells 
2917716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
2918716ea3a7SDavid Howells 		int ntail = 0;
2919716ea3a7SDavid Howells 
2920716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
2921716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
2922716ea3a7SDavid Howells 		 * after it. */
2923716ea3a7SDavid Howells 
2924716ea3a7SDavid Howells 		if (skb_shared(skb1))
2925716ea3a7SDavid Howells 			copyflag = 1;
2926716ea3a7SDavid Howells 
2927716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
2928716ea3a7SDavid Howells 
2929716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
2930716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
2931fbb398a8SDavid S. Miller 			    skb_has_frags(skb1) ||
2932716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
2933716ea3a7SDavid Howells 				ntail = tailbits + 128;
2934716ea3a7SDavid Howells 		}
2935716ea3a7SDavid Howells 
2936716ea3a7SDavid Howells 		if (copyflag ||
2937716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
2938716ea3a7SDavid Howells 		    ntail ||
2939716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
2940fbb398a8SDavid S. Miller 		    skb_has_frags(skb1)) {
2941716ea3a7SDavid Howells 			struct sk_buff *skb2;
2942716ea3a7SDavid Howells 
2943716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
2944716ea3a7SDavid Howells 			if (ntail == 0)
2945716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
2946716ea3a7SDavid Howells 			else
2947716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
2948716ea3a7SDavid Howells 						       skb_headroom(skb1),
2949716ea3a7SDavid Howells 						       ntail,
2950716ea3a7SDavid Howells 						       GFP_ATOMIC);
2951716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
2952716ea3a7SDavid Howells 				return -ENOMEM;
2953716ea3a7SDavid Howells 
2954716ea3a7SDavid Howells 			if (skb1->sk)
2955716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
2956716ea3a7SDavid Howells 
2957716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
2958716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
2959716ea3a7SDavid Howells 
2960716ea3a7SDavid Howells 			skb2->next = skb1->next;
2961716ea3a7SDavid Howells 			*skb_p = skb2;
2962716ea3a7SDavid Howells 			kfree_skb(skb1);
2963716ea3a7SDavid Howells 			skb1 = skb2;
2964716ea3a7SDavid Howells 		}
2965716ea3a7SDavid Howells 		elt++;
2966716ea3a7SDavid Howells 		*trailer = skb1;
2967716ea3a7SDavid Howells 		skb_p = &skb1->next;
2968716ea3a7SDavid Howells 	}
2969716ea3a7SDavid Howells 
2970716ea3a7SDavid Howells 	return elt;
2971716ea3a7SDavid Howells }
2972b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data);
2973716ea3a7SDavid Howells 
2974ac45f602SPatrick Ohly void skb_tstamp_tx(struct sk_buff *orig_skb,
2975ac45f602SPatrick Ohly 		struct skb_shared_hwtstamps *hwtstamps)
2976ac45f602SPatrick Ohly {
2977ac45f602SPatrick Ohly 	struct sock *sk = orig_skb->sk;
2978ac45f602SPatrick Ohly 	struct sock_exterr_skb *serr;
2979ac45f602SPatrick Ohly 	struct sk_buff *skb;
2980ac45f602SPatrick Ohly 	int err;
2981ac45f602SPatrick Ohly 
2982ac45f602SPatrick Ohly 	if (!sk)
2983ac45f602SPatrick Ohly 		return;
2984ac45f602SPatrick Ohly 
2985ac45f602SPatrick Ohly 	skb = skb_clone(orig_skb, GFP_ATOMIC);
2986ac45f602SPatrick Ohly 	if (!skb)
2987ac45f602SPatrick Ohly 		return;
2988ac45f602SPatrick Ohly 
2989ac45f602SPatrick Ohly 	if (hwtstamps) {
2990ac45f602SPatrick Ohly 		*skb_hwtstamps(skb) =
2991ac45f602SPatrick Ohly 			*hwtstamps;
2992ac45f602SPatrick Ohly 	} else {
2993ac45f602SPatrick Ohly 		/*
2994ac45f602SPatrick Ohly 		 * no hardware time stamps available,
2995ac45f602SPatrick Ohly 		 * so keep the skb_shared_tx and only
2996ac45f602SPatrick Ohly 		 * store software time stamp
2997ac45f602SPatrick Ohly 		 */
2998ac45f602SPatrick Ohly 		skb->tstamp = ktime_get_real();
2999ac45f602SPatrick Ohly 	}
3000ac45f602SPatrick Ohly 
3001ac45f602SPatrick Ohly 	serr = SKB_EXT_ERR(skb);
3002ac45f602SPatrick Ohly 	memset(serr, 0, sizeof(*serr));
3003ac45f602SPatrick Ohly 	serr->ee.ee_errno = ENOMSG;
3004ac45f602SPatrick Ohly 	serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
3005ac45f602SPatrick Ohly 	err = sock_queue_err_skb(sk, skb);
3006ac45f602SPatrick Ohly 	if (err)
3007ac45f602SPatrick Ohly 		kfree_skb(skb);
3008ac45f602SPatrick Ohly }
3009ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx);
3010ac45f602SPatrick Ohly 
3011ac45f602SPatrick Ohly 
3012f35d9d8aSRusty Russell /**
3013f35d9d8aSRusty Russell  * skb_partial_csum_set - set up and verify partial csum values for packet
3014f35d9d8aSRusty Russell  * @skb: the skb to set
3015f35d9d8aSRusty Russell  * @start: the number of bytes after skb->data to start checksumming.
3016f35d9d8aSRusty Russell  * @off: the offset from start to place the checksum.
3017f35d9d8aSRusty Russell  *
3018f35d9d8aSRusty Russell  * For untrusted partially-checksummed packets, we need to make sure the values
3019f35d9d8aSRusty Russell  * for skb->csum_start and skb->csum_offset are valid so we don't oops.
3020f35d9d8aSRusty Russell  *
3021f35d9d8aSRusty Russell  * This function checks and sets those values and skb->ip_summed: if this
3022f35d9d8aSRusty Russell  * returns false you should drop the packet.
3023f35d9d8aSRusty Russell  */
3024f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off)
3025f35d9d8aSRusty Russell {
30265ff8dda3SHerbert Xu 	if (unlikely(start > skb_headlen(skb)) ||
30275ff8dda3SHerbert Xu 	    unlikely((int)start + off > skb_headlen(skb) - 2)) {
3028f35d9d8aSRusty Russell 		if (net_ratelimit())
3029f35d9d8aSRusty Russell 			printk(KERN_WARNING
3030f35d9d8aSRusty Russell 			       "bad partial csum: csum=%u/%u len=%u\n",
30315ff8dda3SHerbert Xu 			       start, off, skb_headlen(skb));
3032f35d9d8aSRusty Russell 		return false;
3033f35d9d8aSRusty Russell 	}
3034f35d9d8aSRusty Russell 	skb->ip_summed = CHECKSUM_PARTIAL;
3035f35d9d8aSRusty Russell 	skb->csum_start = skb_headroom(skb) + start;
3036f35d9d8aSRusty Russell 	skb->csum_offset = off;
3037f35d9d8aSRusty Russell 	return true;
3038f35d9d8aSRusty Russell }
3039b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set);
3040f35d9d8aSRusty Russell 
30414497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb)
30424497b076SBen Hutchings {
30434497b076SBen Hutchings 	if (net_ratelimit())
30444497b076SBen Hutchings 		pr_warning("%s: received packets cannot be forwarded"
30454497b076SBen Hutchings 			   " while LRO is enabled\n", skb->dev->name);
30464497b076SBen Hutchings }
30474497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding);
3048