xref: /openbmc/linux/net/core/skbuff.c (revision c46f2334)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Routines having to do with the 'struct sk_buff' memory handlers.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Authors:	Alan Cox <iiitac@pyr.swan.ac.uk>
51da177e4SLinus Torvalds  *			Florian La Roche <rzsfl@rz.uni-sb.de>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	Version:	$Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Fixes:
101da177e4SLinus Torvalds  *		Alan Cox	:	Fixed the worst of the load
111da177e4SLinus Torvalds  *					balancer bugs.
121da177e4SLinus Torvalds  *		Dave Platt	:	Interrupt stacking fix.
131da177e4SLinus Torvalds  *	Richard Kooijman	:	Timestamp fixes.
141da177e4SLinus Torvalds  *		Alan Cox	:	Changed buffer format.
151da177e4SLinus Torvalds  *		Alan Cox	:	destructor hook for AF_UNIX etc.
161da177e4SLinus Torvalds  *		Linus Torvalds	:	Better skb_clone.
171da177e4SLinus Torvalds  *		Alan Cox	:	Added skb_copy.
181da177e4SLinus Torvalds  *		Alan Cox	:	Added all the changed routines Linus
191da177e4SLinus Torvalds  *					only put in the headers
201da177e4SLinus Torvalds  *		Ray VanTassle	:	Fixed --skb->lock in free
211da177e4SLinus Torvalds  *		Alan Cox	:	skb_copy copy arp field
221da177e4SLinus Torvalds  *		Andi Kleen	:	slabified it.
231da177e4SLinus Torvalds  *		Robert Olsson	:	Removed skb_head_pool
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *	NOTE:
261da177e4SLinus Torvalds  *		The __skb_ routines should be called with interrupts
271da177e4SLinus Torvalds  *	disabled, or you better be *real* sure that the operation is atomic
281da177e4SLinus Torvalds  *	with respect to whatever list is being frobbed (e.g. via lock_sock()
291da177e4SLinus Torvalds  *	or via disabling bottom half handlers, etc).
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
321da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License
331da177e4SLinus Torvalds  *	as published by the Free Software Foundation; either version
341da177e4SLinus Torvalds  *	2 of the License, or (at your option) any later version.
351da177e4SLinus Torvalds  */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /*
381da177e4SLinus Torvalds  *	The functions in this file will not compile correctly with gcc 2.4.x
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/module.h>
421da177e4SLinus Torvalds #include <linux/types.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
441da177e4SLinus Torvalds #include <linux/mm.h>
451da177e4SLinus Torvalds #include <linux/interrupt.h>
461da177e4SLinus Torvalds #include <linux/in.h>
471da177e4SLinus Torvalds #include <linux/inet.h>
481da177e4SLinus Torvalds #include <linux/slab.h>
491da177e4SLinus Torvalds #include <linux/netdevice.h>
501da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
511da177e4SLinus Torvalds #include <net/pkt_sched.h>
521da177e4SLinus Torvalds #endif
531da177e4SLinus Torvalds #include <linux/string.h>
541da177e4SLinus Torvalds #include <linux/skbuff.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>
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds #include <net/protocol.h>
611da177e4SLinus Torvalds #include <net/dst.h>
621da177e4SLinus Torvalds #include <net/sock.h>
631da177e4SLinus Torvalds #include <net/checksum.h>
641da177e4SLinus Torvalds #include <net/xfrm.h>
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds #include <asm/uaccess.h>
671da177e4SLinus Torvalds #include <asm/system.h>
681da177e4SLinus Torvalds 
69a1f8e7f7SAl Viro #include "kmap_skb.h"
70a1f8e7f7SAl Viro 
71e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly;
72e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /*
751da177e4SLinus Torvalds  *	Keep out-of-line to prevent kernel bloat.
761da177e4SLinus Torvalds  *	__builtin_return_address is not used because it is not always
771da177e4SLinus Torvalds  *	reliable.
781da177e4SLinus Torvalds  */
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /**
811da177e4SLinus Torvalds  *	skb_over_panic	- 	private function
821da177e4SLinus Torvalds  *	@skb: buffer
831da177e4SLinus Torvalds  *	@sz: size
841da177e4SLinus Torvalds  *	@here: address
851da177e4SLinus Torvalds  *
861da177e4SLinus Torvalds  *	Out of line support code for skb_put(). Not user callable.
871da177e4SLinus Torvalds  */
881da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here)
891da177e4SLinus Torvalds {
9026095455SPatrick McHardy 	printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
914305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
9227a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
934305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
9426095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
951da177e4SLinus Torvalds 	BUG();
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds /**
991da177e4SLinus Torvalds  *	skb_under_panic	- 	private function
1001da177e4SLinus Torvalds  *	@skb: buffer
1011da177e4SLinus Torvalds  *	@sz: size
1021da177e4SLinus Torvalds  *	@here: address
1031da177e4SLinus Torvalds  *
1041da177e4SLinus Torvalds  *	Out of line support code for skb_push(). Not user callable.
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here)
1081da177e4SLinus Torvalds {
10926095455SPatrick McHardy 	printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
1104305b541SArnaldo Carvalho de Melo 			  "data:%p tail:%#lx end:%#lx dev:%s\n",
11127a884dcSArnaldo Carvalho de Melo 	       here, skb->len, sz, skb->head, skb->data,
1124305b541SArnaldo Carvalho de Melo 	       (unsigned long)skb->tail, (unsigned long)skb->end,
11326095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1141da177e4SLinus Torvalds 	BUG();
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
117dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb)
118dc6de336SDavid S. Miller {
119dc6de336SDavid S. Miller 	printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
120dc6de336SDavid S. Miller 	       "len=%u, sizeof(sk_buff)=%Zd\n",
121dc6de336SDavid S. Miller 	       skb->truesize, skb->len, sizeof(struct sk_buff));
122dc6de336SDavid S. Miller }
123dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug);
124dc6de336SDavid S. Miller 
1251da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1261da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1271da177e4SLinus Torvalds  *	[BEEP] leaks.
1281da177e4SLinus Torvalds  *
1291da177e4SLinus Torvalds  */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /**
132d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1331da177e4SLinus Torvalds  *	@size: size to allocate
1341da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
135c83c2486SRandy Dunlap  *	@fclone: allocate from fclone cache instead of head cache
136c83c2486SRandy Dunlap  *		and allocate a cloned (child) skb
137b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
1401da177e4SLinus Torvalds  *	tail room of size bytes. The object has a reference count of one.
1411da177e4SLinus Torvalds  *	The return is the buffer. On a failure the return is %NULL.
1421da177e4SLinus Torvalds  *
1431da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1441da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1451da177e4SLinus Torvalds  */
146dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
147b30973f8SChristoph Hellwig 			    int fclone, int node)
1481da177e4SLinus Torvalds {
149e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1504947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1511da177e4SLinus Torvalds 	struct sk_buff *skb;
1521da177e4SLinus Torvalds 	u8 *data;
1531da177e4SLinus Torvalds 
1548798b3fbSHerbert Xu 	cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
1558798b3fbSHerbert Xu 
1561da177e4SLinus Torvalds 	/* Get the HEAD */
157b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1581da177e4SLinus Torvalds 	if (!skb)
1591da177e4SLinus Torvalds 		goto out;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
162b30973f8SChristoph Hellwig 	data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
163b30973f8SChristoph Hellwig 			gfp_mask, node);
1641da177e4SLinus Torvalds 	if (!data)
1651da177e4SLinus Torvalds 		goto nodata;
1661da177e4SLinus Torvalds 
167ca0605a7SArnaldo Carvalho de Melo 	/*
168ca0605a7SArnaldo Carvalho de Melo 	 * See comment in sk_buff definition, just before the 'tail' member
169ca0605a7SArnaldo Carvalho de Melo 	 */
170ca0605a7SArnaldo Carvalho de Melo 	memset(skb, 0, offsetof(struct sk_buff, tail));
1711da177e4SLinus Torvalds 	skb->truesize = size + sizeof(struct sk_buff);
1721da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
1731da177e4SLinus Torvalds 	skb->head = data;
1741da177e4SLinus Torvalds 	skb->data = data;
17527a884dcSArnaldo Carvalho de Melo 	skb_reset_tail_pointer(skb);
1764305b541SArnaldo Carvalho de Melo 	skb->end = skb->tail + size;
1774947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
1784947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
1794947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
1804947d3efSBenjamin LaHaise 	shinfo->nr_frags  = 0;
1817967168cSHerbert Xu 	shinfo->gso_size = 0;
1827967168cSHerbert Xu 	shinfo->gso_segs = 0;
1837967168cSHerbert Xu 	shinfo->gso_type = 0;
1844947d3efSBenjamin LaHaise 	shinfo->ip6_frag_id = 0;
1854947d3efSBenjamin LaHaise 	shinfo->frag_list = NULL;
1864947d3efSBenjamin LaHaise 
187d179cd12SDavid S. Miller 	if (fclone) {
188d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
189d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
1901da177e4SLinus Torvalds 
191d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
192d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
193d179cd12SDavid S. Miller 
194d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
195d179cd12SDavid S. Miller 	}
1961da177e4SLinus Torvalds out:
1971da177e4SLinus Torvalds 	return skb;
1981da177e4SLinus Torvalds nodata:
1998798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
2001da177e4SLinus Torvalds 	skb = NULL;
2011da177e4SLinus Torvalds 	goto out;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /**
2058af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
2068af27456SChristoph Hellwig  *	@dev: network device to receive on
2078af27456SChristoph Hellwig  *	@length: length to allocate
2088af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
2098af27456SChristoph Hellwig  *
2108af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
2118af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
2128af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
2138af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
2148af27456SChristoph Hellwig  *
2158af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
2168af27456SChristoph Hellwig  */
2178af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
2188af27456SChristoph Hellwig 		unsigned int length, gfp_t gfp_mask)
2198af27456SChristoph Hellwig {
22043cb76d9SGreg Kroah-Hartman 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
2218af27456SChristoph Hellwig 	struct sk_buff *skb;
2228af27456SChristoph Hellwig 
223b30973f8SChristoph Hellwig 	skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
2247b2e497aSChristoph Hellwig 	if (likely(skb)) {
2258af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
2267b2e497aSChristoph Hellwig 		skb->dev = dev;
2277b2e497aSChristoph Hellwig 	}
2288af27456SChristoph Hellwig 	return skb;
2298af27456SChristoph Hellwig }
2301da177e4SLinus Torvalds 
23127b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
2321da177e4SLinus Torvalds {
23327b437c8SHerbert Xu 	struct sk_buff *list = *listp;
2341da177e4SLinus Torvalds 
23527b437c8SHerbert Xu 	*listp = NULL;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	do {
2381da177e4SLinus Torvalds 		struct sk_buff *this = list;
2391da177e4SLinus Torvalds 		list = list->next;
2401da177e4SLinus Torvalds 		kfree_skb(this);
2411da177e4SLinus Torvalds 	} while (list);
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
24427b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
24527b437c8SHerbert Xu {
24627b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
24727b437c8SHerbert Xu }
24827b437c8SHerbert Xu 
2491da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	struct sk_buff *list;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
2541da177e4SLinus Torvalds 		skb_get(list);
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2575bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	if (!skb->cloned ||
2601da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
2611da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
2621da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
2631da177e4SLinus Torvalds 			int i;
2641da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2651da177e4SLinus Torvalds 				put_page(skb_shinfo(skb)->frags[i].page);
2661da177e4SLinus Torvalds 		}
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frag_list)
2691da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		kfree(skb->head);
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /*
2761da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
2771da177e4SLinus Torvalds  */
2781da177e4SLinus Torvalds void kfree_skbmem(struct sk_buff *skb)
2791da177e4SLinus Torvalds {
280d179cd12SDavid S. Miller 	struct sk_buff *other;
281d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
282d179cd12SDavid S. Miller 
2831da177e4SLinus Torvalds 	skb_release_data(skb);
284d179cd12SDavid S. Miller 	switch (skb->fclone) {
285d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
2861da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
287d179cd12SDavid S. Miller 		break;
288d179cd12SDavid S. Miller 
289d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
290d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
291d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
292d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
293d179cd12SDavid S. Miller 		break;
294d179cd12SDavid S. Miller 
295d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
296d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
297d179cd12SDavid S. Miller 		other = skb - 1;
298d179cd12SDavid S. Miller 
299d179cd12SDavid S. Miller 		/* The clone portion is available for
300d179cd12SDavid S. Miller 		 * fast-cloning again.
301d179cd12SDavid S. Miller 		 */
302d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
303d179cd12SDavid S. Miller 
304d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
305d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
306d179cd12SDavid S. Miller 		break;
3073ff50b79SStephen Hemminger 	}
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds /**
3111da177e4SLinus Torvalds  *	__kfree_skb - private function
3121da177e4SLinus Torvalds  *	@skb: buffer
3131da177e4SLinus Torvalds  *
3141da177e4SLinus Torvalds  *	Free an sk_buff. Release anything attached to the buffer.
3151da177e4SLinus Torvalds  *	Clean the state. This is an internal helper function. Users should
3161da177e4SLinus Torvalds  *	always call kfree_skb
3171da177e4SLinus Torvalds  */
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds void __kfree_skb(struct sk_buff *skb)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	dst_release(skb->dst);
3221da177e4SLinus Torvalds #ifdef CONFIG_XFRM
3231da177e4SLinus Torvalds 	secpath_put(skb->sp);
3241da177e4SLinus Torvalds #endif
3251da177e4SLinus Torvalds 	if (skb->destructor) {
3269c2b3328SStephen Hemminger 		WARN_ON(in_irq());
3271da177e4SLinus Torvalds 		skb->destructor(skb);
3281da177e4SLinus Torvalds 	}
3299fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
3305f79e0f9SYasuyuki Kozakai 	nf_conntrack_put(skb->nfct);
3319fb9cbb1SYasuyuki Kozakai 	nf_conntrack_put_reasm(skb->nfct_reasm);
3329fb9cbb1SYasuyuki Kozakai #endif
3331da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
3341da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
3351da177e4SLinus Torvalds #endif
3361da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
3371da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
3381da177e4SLinus Torvalds 	skb->tc_index = 0;
3391da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
3401da177e4SLinus Torvalds 	skb->tc_verd = 0;
3411da177e4SLinus Torvalds #endif
3421da177e4SLinus Torvalds #endif
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	kfree_skbmem(skb);
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /**
348231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
349231d06aeSJörn Engel  *	@skb: buffer to free
350231d06aeSJörn Engel  *
351231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
352231d06aeSJörn Engel  *	hit zero.
353231d06aeSJörn Engel  */
354231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
355231d06aeSJörn Engel {
356231d06aeSJörn Engel 	if (unlikely(!skb))
357231d06aeSJörn Engel 		return;
358231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
359231d06aeSJörn Engel 		smp_rmb();
360231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
361231d06aeSJörn Engel 		return;
362231d06aeSJörn Engel 	__kfree_skb(skb);
363231d06aeSJörn Engel }
364231d06aeSJörn Engel 
365dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
366dec18810SHerbert Xu {
367dec18810SHerbert Xu 	new->tstamp		= old->tstamp;
368dec18810SHerbert Xu 	new->dev		= old->dev;
369dec18810SHerbert Xu 	new->transport_header	= old->transport_header;
370dec18810SHerbert Xu 	new->network_header	= old->network_header;
371dec18810SHerbert Xu 	new->mac_header		= old->mac_header;
372dec18810SHerbert Xu 	new->dst		= dst_clone(old->dst);
373dec18810SHerbert Xu #ifdef CONFIG_INET
374dec18810SHerbert Xu 	new->sp			= secpath_get(old->sp);
375dec18810SHerbert Xu #endif
376dec18810SHerbert Xu 	memcpy(new->cb, old->cb, sizeof(old->cb));
377dec18810SHerbert Xu 	new->csum_start		= old->csum_start;
378dec18810SHerbert Xu 	new->csum_offset	= old->csum_offset;
379dec18810SHerbert Xu 	new->local_df		= old->local_df;
380dec18810SHerbert Xu 	new->pkt_type		= old->pkt_type;
381dec18810SHerbert Xu 	new->ip_summed		= old->ip_summed;
382dec18810SHerbert Xu 	skb_copy_queue_mapping(new, old);
383dec18810SHerbert Xu 	new->priority		= old->priority;
384dec18810SHerbert Xu #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
385dec18810SHerbert Xu 	new->ipvs_property	= old->ipvs_property;
386dec18810SHerbert Xu #endif
387dec18810SHerbert Xu 	new->protocol		= old->protocol;
388dec18810SHerbert Xu 	new->mark		= old->mark;
389dec18810SHerbert Xu 	__nf_copy(new, old);
390dec18810SHerbert Xu #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
391dec18810SHerbert Xu     defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
392dec18810SHerbert Xu 	new->nf_trace		= old->nf_trace;
393dec18810SHerbert Xu #endif
394dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED
395dec18810SHerbert Xu 	new->tc_index		= old->tc_index;
396dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT
397dec18810SHerbert Xu 	new->tc_verd		= old->tc_verd;
398dec18810SHerbert Xu #endif
399dec18810SHerbert Xu #endif
400dec18810SHerbert Xu 	skb_copy_secmark(new, old);
401dec18810SHerbert Xu }
402dec18810SHerbert Xu 
403e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds #define C(x) n->x = skb->x
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	n->next = n->prev = NULL;
4081da177e4SLinus Torvalds 	n->sk = NULL;
409dec18810SHerbert Xu 	__copy_skb_header(n, skb);
410dec18810SHerbert Xu 
4111da177e4SLinus Torvalds 	C(len);
4121da177e4SLinus Torvalds 	C(data_len);
4133e6b3b2eSAlexey Dobriyan 	C(mac_len);
4141da177e4SLinus Torvalds 	n->cloned = 1;
415334a8132SPatrick McHardy 	n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len;
4161da177e4SLinus Torvalds 	n->nohdr = 0;
4171da177e4SLinus Torvalds 	n->destructor = NULL;
4181da177e4SLinus Torvalds 	C(truesize);
4191da177e4SLinus Torvalds 	atomic_set(&n->users, 1);
4201da177e4SLinus Torvalds 	C(head);
4211da177e4SLinus Torvalds 	C(data);
4221da177e4SLinus Torvalds 	C(tail);
4231da177e4SLinus Torvalds 	C(end);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
4261da177e4SLinus Torvalds 	skb->cloned = 1;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	return n;
429e0053ec0SHerbert Xu #undef C
430e0053ec0SHerbert Xu }
431e0053ec0SHerbert Xu 
432e0053ec0SHerbert Xu /**
433e0053ec0SHerbert Xu  *	skb_morph	-	morph one skb into another
434e0053ec0SHerbert Xu  *	@dst: the skb to receive the contents
435e0053ec0SHerbert Xu  *	@src: the skb to supply the contents
436e0053ec0SHerbert Xu  *
437e0053ec0SHerbert Xu  *	This is identical to skb_clone except that the target skb is
438e0053ec0SHerbert Xu  *	supplied by the user.
439e0053ec0SHerbert Xu  *
440e0053ec0SHerbert Xu  *	The target skb is returned upon exit.
441e0053ec0SHerbert Xu  */
442e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src)
443e0053ec0SHerbert Xu {
444e0053ec0SHerbert Xu 	skb_release_data(dst);
445e0053ec0SHerbert Xu 	return __skb_clone(dst, src);
446e0053ec0SHerbert Xu }
447e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph);
448e0053ec0SHerbert Xu 
449e0053ec0SHerbert Xu /**
450e0053ec0SHerbert Xu  *	skb_clone	-	duplicate an sk_buff
451e0053ec0SHerbert Xu  *	@skb: buffer to clone
452e0053ec0SHerbert Xu  *	@gfp_mask: allocation priority
453e0053ec0SHerbert Xu  *
454e0053ec0SHerbert Xu  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
455e0053ec0SHerbert Xu  *	copies share the same packet data but not structure. The new
456e0053ec0SHerbert Xu  *	buffer has a reference count of 1. If the allocation fails the
457e0053ec0SHerbert Xu  *	function returns %NULL otherwise the new buffer is returned.
458e0053ec0SHerbert Xu  *
459e0053ec0SHerbert Xu  *	If this function is called from an interrupt gfp_mask() must be
460e0053ec0SHerbert Xu  *	%GFP_ATOMIC.
461e0053ec0SHerbert Xu  */
462e0053ec0SHerbert Xu 
463e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
464e0053ec0SHerbert Xu {
465e0053ec0SHerbert Xu 	struct sk_buff *n;
466e0053ec0SHerbert Xu 
467e0053ec0SHerbert Xu 	n = skb + 1;
468e0053ec0SHerbert Xu 	if (skb->fclone == SKB_FCLONE_ORIG &&
469e0053ec0SHerbert Xu 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
470e0053ec0SHerbert Xu 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
471e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_CLONE;
472e0053ec0SHerbert Xu 		atomic_inc(fclone_ref);
473e0053ec0SHerbert Xu 	} else {
474e0053ec0SHerbert Xu 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
475e0053ec0SHerbert Xu 		if (!n)
476e0053ec0SHerbert Xu 			return NULL;
477e0053ec0SHerbert Xu 		n->fclone = SKB_FCLONE_UNAVAILABLE;
478e0053ec0SHerbert Xu 	}
479e0053ec0SHerbert Xu 
480e0053ec0SHerbert Xu 	return __skb_clone(n, skb);
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
4841da177e4SLinus Torvalds {
4852e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
4861da177e4SLinus Torvalds 	/*
4871da177e4SLinus Torvalds 	 *	Shift between the two data areas in bytes
4881da177e4SLinus Torvalds 	 */
4891da177e4SLinus Torvalds 	unsigned long offset = new->data - old->data;
4902e07fa9cSArnaldo Carvalho de Melo #endif
491dec18810SHerbert Xu 
492dec18810SHerbert Xu 	__copy_skb_header(new, old);
493dec18810SHerbert Xu 
4942e07fa9cSArnaldo Carvalho de Melo #ifndef NET_SKBUFF_DATA_USES_OFFSET
4952e07fa9cSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header are relative to skb->head */
4962e07fa9cSArnaldo Carvalho de Melo 	new->transport_header += offset;
4972e07fa9cSArnaldo Carvalho de Melo 	new->network_header   += offset;
4982e07fa9cSArnaldo Carvalho de Melo 	new->mac_header	      += offset;
4992e07fa9cSArnaldo Carvalho de Melo #endif
5007967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
5017967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
5027967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds /**
5061da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
5071da177e4SLinus Torvalds  *	@skb: buffer to copy
5081da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
5091da177e4SLinus Torvalds  *
5101da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
5111da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
5121da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
5131da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
5141da177e4SLinus Torvalds  *
5151da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
5161da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
5171da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
5181da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
5191da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
5201da177e4SLinus Torvalds  */
5211da177e4SLinus Torvalds 
522dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	int headerlen = skb->data - skb->head;
5251da177e4SLinus Torvalds 	/*
5261da177e4SLinus Torvalds 	 *	Allocate the copy buffer
5271da177e4SLinus Torvalds 	 */
5284305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
5294305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
5304305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end + skb->data_len, gfp_mask);
5314305b541SArnaldo Carvalho de Melo #else
5324305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask);
5334305b541SArnaldo Carvalho de Melo #endif
5341da177e4SLinus Torvalds 	if (!n)
5351da177e4SLinus Torvalds 		return NULL;
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	/* Set the data pointer */
5381da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
5391da177e4SLinus Torvalds 	/* Set the tail pointer and length */
5401da177e4SLinus Torvalds 	skb_put(n, skb->len);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
5431da177e4SLinus Torvalds 		BUG();
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	copy_skb_header(n, skb);
5461da177e4SLinus Torvalds 	return n;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /**
5511da177e4SLinus Torvalds  *	pskb_copy	-	create copy of an sk_buff with private head.
5521da177e4SLinus Torvalds  *	@skb: buffer to copy
5531da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
5541da177e4SLinus Torvalds  *
5551da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
5561da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
5571da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
5581da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
5591da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
5601da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
5611da177e4SLinus Torvalds  */
5621da177e4SLinus Torvalds 
563dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	/*
5661da177e4SLinus Torvalds 	 *	Allocate the copy buffer
5671da177e4SLinus Torvalds 	 */
5684305b541SArnaldo Carvalho de Melo 	struct sk_buff *n;
5694305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
5704305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end, gfp_mask);
5714305b541SArnaldo Carvalho de Melo #else
5724305b541SArnaldo Carvalho de Melo 	n = alloc_skb(skb->end - skb->head, gfp_mask);
5734305b541SArnaldo Carvalho de Melo #endif
5741da177e4SLinus Torvalds 	if (!n)
5751da177e4SLinus Torvalds 		goto out;
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	/* Set the data pointer */
5781da177e4SLinus Torvalds 	skb_reserve(n, skb->data - skb->head);
5791da177e4SLinus Torvalds 	/* Set the tail pointer and length */
5801da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
5811da177e4SLinus Torvalds 	/* Copy the bytes */
582d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, n->data, n->len);
5831da177e4SLinus Torvalds 
58425f484a6SHerbert Xu 	n->truesize += skb->data_len;
5851da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
5861da177e4SLinus Torvalds 	n->len	     = skb->len;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
5891da177e4SLinus Torvalds 		int i;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5921da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
5931da177e4SLinus Torvalds 			get_page(skb_shinfo(n)->frags[i].page);
5941da177e4SLinus Torvalds 		}
5951da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
5991da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
6001da177e4SLinus Torvalds 		skb_clone_fraglist(n);
6011da177e4SLinus Torvalds 	}
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	copy_skb_header(n, skb);
6041da177e4SLinus Torvalds out:
6051da177e4SLinus Torvalds 	return n;
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds /**
6091da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
6101da177e4SLinus Torvalds  *	@skb: buffer to reallocate
6111da177e4SLinus Torvalds  *	@nhead: room to add at head
6121da177e4SLinus Torvalds  *	@ntail: room to add at tail
6131da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6141da177e4SLinus Torvalds  *
6151da177e4SLinus Torvalds  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
6161da177e4SLinus Torvalds  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
6171da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
6181da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
6191da177e4SLinus Torvalds  *
6201da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
6211da177e4SLinus Torvalds  *	reloaded after call to this function.
6221da177e4SLinus Torvalds  */
6231da177e4SLinus Torvalds 
62486a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
625dd0fc66fSAl Viro 		     gfp_t gfp_mask)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds 	int i;
6281da177e4SLinus Torvalds 	u8 *data;
6294305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
6304305b541SArnaldo Carvalho de Melo 	int size = nhead + skb->end + ntail;
6314305b541SArnaldo Carvalho de Melo #else
6321da177e4SLinus Torvalds 	int size = nhead + (skb->end - skb->head) + ntail;
6334305b541SArnaldo Carvalho de Melo #endif
6341da177e4SLinus Torvalds 	long off;
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	if (skb_shared(skb))
6371da177e4SLinus Torvalds 		BUG();
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
6421da177e4SLinus Torvalds 	if (!data)
6431da177e4SLinus Torvalds 		goto nodata;
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
6461da177e4SLinus Torvalds 	 * optimized for the cases when header is void. */
6474305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
648b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail);
6494305b541SArnaldo Carvalho de Melo #else
650b6ccc67dSMikael Pettersson 	memcpy(data + nhead, skb->head, skb->tail - skb->head);
65127a884dcSArnaldo Carvalho de Melo #endif
6524305b541SArnaldo Carvalho de Melo 	memcpy(data + size, skb_end_pointer(skb),
6534305b541SArnaldo Carvalho de Melo 	       sizeof(struct skb_shared_info));
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6561da177e4SLinus Torvalds 		get_page(skb_shinfo(skb)->frags[i].page);
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list)
6591da177e4SLinus Torvalds 		skb_clone_fraglist(skb);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	skb_release_data(skb);
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	skb->head     = data;
6661da177e4SLinus Torvalds 	skb->data    += off;
6674305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET
6684305b541SArnaldo Carvalho de Melo 	skb->end      = size;
66956eb8882SPatrick McHardy 	off           = nhead;
6704305b541SArnaldo Carvalho de Melo #else
6714305b541SArnaldo Carvalho de Melo 	skb->end      = skb->head + size;
67256eb8882SPatrick McHardy #endif
67327a884dcSArnaldo Carvalho de Melo 	/* {transport,network,mac}_header and tail are relative to skb->head */
67427a884dcSArnaldo Carvalho de Melo 	skb->tail	      += off;
675b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header += off;
676b0e380b1SArnaldo Carvalho de Melo 	skb->network_header   += off;
677b0e380b1SArnaldo Carvalho de Melo 	skb->mac_header	      += off;
678172a863fSHerbert Xu 	skb->csum_start       += nhead;
6791da177e4SLinus Torvalds 	skb->cloned   = 0;
680334a8132SPatrick McHardy 	skb->hdr_len  = 0;
6811da177e4SLinus Torvalds 	skb->nohdr    = 0;
6821da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6831da177e4SLinus Torvalds 	return 0;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds nodata:
6861da177e4SLinus Torvalds 	return -ENOMEM;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	struct sk_buff *skb2;
6941da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	if (delta <= 0)
6971da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
6981da177e4SLinus Torvalds 	else {
6991da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
7001da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
7011da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
7021da177e4SLinus Torvalds 			kfree_skb(skb2);
7031da177e4SLinus Torvalds 			skb2 = NULL;
7041da177e4SLinus Torvalds 		}
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds 	return skb2;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /**
7111da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
7121da177e4SLinus Torvalds  *	@skb: buffer to copy
7131da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
7141da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
7151da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
7161da177e4SLinus Torvalds  *
7171da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
7181da177e4SLinus Torvalds  *	allocate additional space.
7191da177e4SLinus Torvalds  *
7201da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
7211da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
7221da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
7231da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
7241da177e4SLinus Torvalds  *
7251da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
7261da177e4SLinus Torvalds  *	is called from an interrupt.
7271da177e4SLinus Torvalds  */
7281da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
72986a76cafSVictor Fusco 				int newheadroom, int newtailroom,
730dd0fc66fSAl Viro 				gfp_t gfp_mask)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	/*
7331da177e4SLinus Torvalds 	 *	Allocate the copy buffer
7341da177e4SLinus Torvalds 	 */
7351da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
7361da177e4SLinus Torvalds 				      gfp_mask);
737efd1e8d5SPatrick McHardy 	int oldheadroom = skb_headroom(skb);
7381da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
73952886051SHerbert Xu 	int off;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	if (!n)
7421da177e4SLinus Torvalds 		return NULL;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	/* Set the tail pointer and length */
7471da177e4SLinus Torvalds 	skb_put(n, skb->len);
7481da177e4SLinus Torvalds 
749efd1e8d5SPatrick McHardy 	head_copy_len = oldheadroom;
7501da177e4SLinus Torvalds 	head_copy_off = 0;
7511da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
7521da177e4SLinus Torvalds 		head_copy_len = newheadroom;
7531da177e4SLinus Torvalds 	else
7541da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds 	/* Copy the linear header and data. */
7571da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
7581da177e4SLinus Torvalds 			  skb->len + head_copy_len))
7591da177e4SLinus Torvalds 		BUG();
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7621da177e4SLinus Torvalds 
763efd1e8d5SPatrick McHardy 	off                  = newheadroom - oldheadroom;
76452886051SHerbert Xu 	n->csum_start       += off;
76552886051SHerbert Xu #ifdef NET_SKBUFF_DATA_USES_OFFSET
766efd1e8d5SPatrick McHardy 	n->transport_header += off;
767efd1e8d5SPatrick McHardy 	n->network_header   += off;
768efd1e8d5SPatrick McHardy 	n->mac_header	    += off;
76952886051SHerbert Xu #endif
770efd1e8d5SPatrick McHardy 
7711da177e4SLinus Torvalds 	return n;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds /**
7751da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
7761da177e4SLinus Torvalds  *	@skb: buffer to pad
7771da177e4SLinus Torvalds  *	@pad: space to pad
7781da177e4SLinus Torvalds  *
7791da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
7801da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
7811da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
7821da177e4SLinus Torvalds  *
7835b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
7841da177e4SLinus Torvalds  */
7851da177e4SLinus Torvalds 
7865b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
7871da177e4SLinus Torvalds {
7885b057c6bSHerbert Xu 	int err;
7895b057c6bSHerbert Xu 	int ntail;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
7925b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
7931da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
7945b057c6bSHerbert Xu 		return 0;
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds 
7974305b541SArnaldo Carvalho de Melo 	ntail = skb->data_len + pad - (skb->end - skb->tail);
7985b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
7995b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
8005b057c6bSHerbert Xu 		if (unlikely(err))
8015b057c6bSHerbert Xu 			goto free_skb;
8025b057c6bSHerbert Xu 	}
8035b057c6bSHerbert Xu 
8045b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
8055b057c6bSHerbert Xu 	 * to be audited.
8065b057c6bSHerbert Xu 	 */
8075b057c6bSHerbert Xu 	err = skb_linearize(skb);
8085b057c6bSHerbert Xu 	if (unlikely(err))
8095b057c6bSHerbert Xu 		goto free_skb;
8105b057c6bSHerbert Xu 
8115b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
8125b057c6bSHerbert Xu 	return 0;
8135b057c6bSHerbert Xu 
8145b057c6bSHerbert Xu free_skb:
8151da177e4SLinus Torvalds 	kfree_skb(skb);
8165b057c6bSHerbert Xu 	return err;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8193cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
8201da177e4SLinus Torvalds  */
8211da177e4SLinus Torvalds 
8223cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
8231da177e4SLinus Torvalds {
82427b437c8SHerbert Xu 	struct sk_buff **fragp;
82527b437c8SHerbert Xu 	struct sk_buff *frag;
8261da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
8271da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
8281da177e4SLinus Torvalds 	int i;
82927b437c8SHerbert Xu 	int err;
83027b437c8SHerbert Xu 
83127b437c8SHerbert Xu 	if (skb_cloned(skb) &&
83227b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
83327b437c8SHerbert Xu 		return err;
8341da177e4SLinus Torvalds 
835f4d26fb3SHerbert Xu 	i = 0;
836f4d26fb3SHerbert Xu 	if (offset >= len)
837f4d26fb3SHerbert Xu 		goto drop_pages;
838f4d26fb3SHerbert Xu 
839f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
8401da177e4SLinus Torvalds 		int end = offset + skb_shinfo(skb)->frags[i].size;
84127b437c8SHerbert Xu 
84227b437c8SHerbert Xu 		if (end < len) {
8431da177e4SLinus Torvalds 			offset = end;
84427b437c8SHerbert Xu 			continue;
8451da177e4SLinus Torvalds 		}
8461da177e4SLinus Torvalds 
84727b437c8SHerbert Xu 		skb_shinfo(skb)->frags[i++].size = len - offset;
84827b437c8SHerbert Xu 
849f4d26fb3SHerbert Xu drop_pages:
85027b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
85127b437c8SHerbert Xu 
85227b437c8SHerbert Xu 		for (; i < nfrags; i++)
85327b437c8SHerbert Xu 			put_page(skb_shinfo(skb)->frags[i].page);
85427b437c8SHerbert Xu 
85527b437c8SHerbert Xu 		if (skb_shinfo(skb)->frag_list)
85627b437c8SHerbert Xu 			skb_drop_fraglist(skb);
857f4d26fb3SHerbert Xu 		goto done;
85827b437c8SHerbert Xu 	}
85927b437c8SHerbert Xu 
86027b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
86127b437c8SHerbert Xu 	     fragp = &frag->next) {
86227b437c8SHerbert Xu 		int end = offset + frag->len;
86327b437c8SHerbert Xu 
86427b437c8SHerbert Xu 		if (skb_shared(frag)) {
86527b437c8SHerbert Xu 			struct sk_buff *nfrag;
86627b437c8SHerbert Xu 
86727b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
86827b437c8SHerbert Xu 			if (unlikely(!nfrag))
86927b437c8SHerbert Xu 				return -ENOMEM;
87027b437c8SHerbert Xu 
87127b437c8SHerbert Xu 			nfrag->next = frag->next;
872f4d26fb3SHerbert Xu 			kfree_skb(frag);
87327b437c8SHerbert Xu 			frag = nfrag;
87427b437c8SHerbert Xu 			*fragp = frag;
87527b437c8SHerbert Xu 		}
87627b437c8SHerbert Xu 
87727b437c8SHerbert Xu 		if (end < len) {
87827b437c8SHerbert Xu 			offset = end;
87927b437c8SHerbert Xu 			continue;
88027b437c8SHerbert Xu 		}
88127b437c8SHerbert Xu 
88227b437c8SHerbert Xu 		if (end > len &&
88327b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
88427b437c8SHerbert Xu 			return err;
88527b437c8SHerbert Xu 
88627b437c8SHerbert Xu 		if (frag->next)
88727b437c8SHerbert Xu 			skb_drop_list(&frag->next);
88827b437c8SHerbert Xu 		break;
88927b437c8SHerbert Xu 	}
89027b437c8SHerbert Xu 
891f4d26fb3SHerbert Xu done:
89227b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
8931da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
8941da177e4SLinus Torvalds 		skb->len       = len;
8951da177e4SLinus Torvalds 	} else {
8961da177e4SLinus Torvalds 		skb->len       = len;
8971da177e4SLinus Torvalds 		skb->data_len  = 0;
89827a884dcSArnaldo Carvalho de Melo 		skb_set_tail_pointer(skb, len);
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	return 0;
9021da177e4SLinus Torvalds }
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds /**
9051da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
9061da177e4SLinus Torvalds  *	@skb: buffer to reallocate
9071da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
9081da177e4SLinus Torvalds  *
9091da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
9101da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
9111da177e4SLinus Torvalds  *	data from fragmented part.
9121da177e4SLinus Torvalds  *
9131da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
9141da177e4SLinus Torvalds  *
9151da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
9161da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
9171da177e4SLinus Torvalds  *
9181da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
9191da177e4SLinus Torvalds  *	reloaded after call to this function.
9201da177e4SLinus Torvalds  */
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
9231da177e4SLinus Torvalds  * when it is necessary.
9241da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
9251da177e4SLinus Torvalds  * 2. It may change skb pointers.
9261da177e4SLinus Torvalds  *
9271da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
9281da177e4SLinus Torvalds  */
9291da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
9301da177e4SLinus Torvalds {
9311da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
9321da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
9331da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
9341da177e4SLinus Torvalds 	 */
9354305b541SArnaldo Carvalho de Melo 	int i, k, eat = (skb->tail + delta) - skb->end;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
9381da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
9391da177e4SLinus Torvalds 				     GFP_ATOMIC))
9401da177e4SLinus Torvalds 			return NULL;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
94327a884dcSArnaldo Carvalho de Melo 	if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta))
9441da177e4SLinus Torvalds 		BUG();
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
9471da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
9481da177e4SLinus Torvalds 	 */
9491da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->frag_list)
9501da177e4SLinus Torvalds 		goto pull_pages;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
9531da177e4SLinus Torvalds 	eat = delta;
9541da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
9551da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size >= eat)
9561da177e4SLinus Torvalds 			goto pull_pages;
9571da177e4SLinus Torvalds 		eat -= skb_shinfo(skb)->frags[i].size;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
9611da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
9621da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
9631da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
9641da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
9651da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
9661da177e4SLinus Torvalds 	 */
9671da177e4SLinus Torvalds 	if (eat) {
9681da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
9691da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
9701da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds 		do {
97309a62660SKris Katterjohn 			BUG_ON(!list);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 			if (list->len <= eat) {
9761da177e4SLinus Torvalds 				/* Eaten as whole. */
9771da177e4SLinus Torvalds 				eat -= list->len;
9781da177e4SLinus Torvalds 				list = list->next;
9791da177e4SLinus Torvalds 				insp = list;
9801da177e4SLinus Torvalds 			} else {
9811da177e4SLinus Torvalds 				/* Eaten partially. */
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 				if (skb_shared(list)) {
9841da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
9851da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
9861da177e4SLinus Torvalds 					if (!clone)
9871da177e4SLinus Torvalds 						return NULL;
9881da177e4SLinus Torvalds 					insp = list->next;
9891da177e4SLinus Torvalds 					list = clone;
9901da177e4SLinus Torvalds 				} else {
9911da177e4SLinus Torvalds 					/* This may be pulled without
9921da177e4SLinus Torvalds 					 * problems. */
9931da177e4SLinus Torvalds 					insp = list;
9941da177e4SLinus Torvalds 				}
9951da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
9961da177e4SLinus Torvalds 					if (clone)
9971da177e4SLinus Torvalds 						kfree_skb(clone);
9981da177e4SLinus Torvalds 					return NULL;
9991da177e4SLinus Torvalds 				}
10001da177e4SLinus Torvalds 				break;
10011da177e4SLinus Torvalds 			}
10021da177e4SLinus Torvalds 		} while (eat);
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 		/* Free pulled out fragments. */
10051da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
10061da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
10071da177e4SLinus Torvalds 			kfree_skb(list);
10081da177e4SLinus Torvalds 		}
10091da177e4SLinus Torvalds 		/* And insert new clone at head. */
10101da177e4SLinus Torvalds 		if (clone) {
10111da177e4SLinus Torvalds 			clone->next = list;
10121da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
10131da177e4SLinus Torvalds 		}
10141da177e4SLinus Torvalds 	}
10151da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds pull_pages:
10181da177e4SLinus Torvalds 	eat = delta;
10191da177e4SLinus Torvalds 	k = 0;
10201da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
10211da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size <= eat) {
10221da177e4SLinus Torvalds 			put_page(skb_shinfo(skb)->frags[i].page);
10231da177e4SLinus Torvalds 			eat -= skb_shinfo(skb)->frags[i].size;
10241da177e4SLinus Torvalds 		} else {
10251da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
10261da177e4SLinus Torvalds 			if (eat) {
10271da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
10281da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].size -= eat;
10291da177e4SLinus Torvalds 				eat = 0;
10301da177e4SLinus Torvalds 			}
10311da177e4SLinus Torvalds 			k++;
10321da177e4SLinus Torvalds 		}
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	skb->tail     += delta;
10371da177e4SLinus Torvalds 	skb->data_len -= delta;
10381da177e4SLinus Torvalds 
103927a884dcSArnaldo Carvalho de Melo 	return skb_tail_pointer(skb);
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	int i, copy;
10471a028e50SDavid S. Miller 	int start = skb_headlen(skb);
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
10501da177e4SLinus Torvalds 		goto fault;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	/* Copy header. */
10531a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
10541da177e4SLinus Torvalds 		if (copy > len)
10551da177e4SLinus Torvalds 			copy = len;
1056d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset, to, copy);
10571da177e4SLinus Torvalds 		if ((len -= copy) == 0)
10581da177e4SLinus Torvalds 			return 0;
10591da177e4SLinus Torvalds 		offset += copy;
10601da177e4SLinus Torvalds 		to     += copy;
10611da177e4SLinus Torvalds 	}
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
10641a028e50SDavid S. Miller 		int end;
10651da177e4SLinus Torvalds 
10661a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
10671a028e50SDavid S. Miller 
10681a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
10691da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
10701da177e4SLinus Torvalds 			u8 *vaddr;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 			if (copy > len)
10731da177e4SLinus Torvalds 				copy = len;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
10761da177e4SLinus Torvalds 			memcpy(to,
10771a028e50SDavid S. Miller 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
10781a028e50SDavid S. Miller 			       offset - start, copy);
10791da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 			if ((len -= copy) == 0)
10821da177e4SLinus Torvalds 				return 0;
10831da177e4SLinus Torvalds 			offset += copy;
10841da177e4SLinus Torvalds 			to     += copy;
10851da177e4SLinus Torvalds 		}
10861a028e50SDavid S. Miller 		start = end;
10871da177e4SLinus Torvalds 	}
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
10901da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 		for (; list; list = list->next) {
10931a028e50SDavid S. Miller 			int end;
10941da177e4SLinus Torvalds 
10951a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
10961a028e50SDavid S. Miller 
10971a028e50SDavid S. Miller 			end = start + list->len;
10981da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
10991da177e4SLinus Torvalds 				if (copy > len)
11001da177e4SLinus Torvalds 					copy = len;
11011a028e50SDavid S. Miller 				if (skb_copy_bits(list, offset - start,
11021a028e50SDavid S. Miller 						  to, copy))
11031da177e4SLinus Torvalds 					goto fault;
11041da177e4SLinus Torvalds 				if ((len -= copy) == 0)
11051da177e4SLinus Torvalds 					return 0;
11061da177e4SLinus Torvalds 				offset += copy;
11071da177e4SLinus Torvalds 				to     += copy;
11081da177e4SLinus Torvalds 			}
11091a028e50SDavid S. Miller 			start = end;
11101da177e4SLinus Torvalds 		}
11111da177e4SLinus Torvalds 	}
11121da177e4SLinus Torvalds 	if (!len)
11131da177e4SLinus Torvalds 		return 0;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds fault:
11161da177e4SLinus Torvalds 	return -EFAULT;
11171da177e4SLinus Torvalds }
11181da177e4SLinus Torvalds 
1119357b40a1SHerbert Xu /**
1120357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1121357b40a1SHerbert Xu  *	@skb: destination buffer
1122357b40a1SHerbert Xu  *	@offset: offset in destination
1123357b40a1SHerbert Xu  *	@from: source buffer
1124357b40a1SHerbert Xu  *	@len: number of bytes to copy
1125357b40a1SHerbert Xu  *
1126357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1127357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1128357b40a1SHerbert Xu  *	traversing fragment lists and such.
1129357b40a1SHerbert Xu  */
1130357b40a1SHerbert Xu 
11310c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len)
1132357b40a1SHerbert Xu {
1133357b40a1SHerbert Xu 	int i, copy;
11341a028e50SDavid S. Miller 	int start = skb_headlen(skb);
1135357b40a1SHerbert Xu 
1136357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1137357b40a1SHerbert Xu 		goto fault;
1138357b40a1SHerbert Xu 
11391a028e50SDavid S. Miller 	if ((copy = start - offset) > 0) {
1140357b40a1SHerbert Xu 		if (copy > len)
1141357b40a1SHerbert Xu 			copy = len;
114227d7ff46SArnaldo Carvalho de Melo 		skb_copy_to_linear_data_offset(skb, offset, from, copy);
1143357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1144357b40a1SHerbert Xu 			return 0;
1145357b40a1SHerbert Xu 		offset += copy;
1146357b40a1SHerbert Xu 		from += copy;
1147357b40a1SHerbert Xu 	}
1148357b40a1SHerbert Xu 
1149357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1150357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
11511a028e50SDavid S. Miller 		int end;
1152357b40a1SHerbert Xu 
11531a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
11541a028e50SDavid S. Miller 
11551a028e50SDavid S. Miller 		end = start + frag->size;
1156357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1157357b40a1SHerbert Xu 			u8 *vaddr;
1158357b40a1SHerbert Xu 
1159357b40a1SHerbert Xu 			if (copy > len)
1160357b40a1SHerbert Xu 				copy = len;
1161357b40a1SHerbert Xu 
1162357b40a1SHerbert Xu 			vaddr = kmap_skb_frag(frag);
11631a028e50SDavid S. Miller 			memcpy(vaddr + frag->page_offset + offset - start,
11641a028e50SDavid S. Miller 			       from, copy);
1165357b40a1SHerbert Xu 			kunmap_skb_frag(vaddr);
1166357b40a1SHerbert Xu 
1167357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1168357b40a1SHerbert Xu 				return 0;
1169357b40a1SHerbert Xu 			offset += copy;
1170357b40a1SHerbert Xu 			from += copy;
1171357b40a1SHerbert Xu 		}
11721a028e50SDavid S. Miller 		start = end;
1173357b40a1SHerbert Xu 	}
1174357b40a1SHerbert Xu 
1175357b40a1SHerbert Xu 	if (skb_shinfo(skb)->frag_list) {
1176357b40a1SHerbert Xu 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1177357b40a1SHerbert Xu 
1178357b40a1SHerbert Xu 		for (; list; list = list->next) {
11791a028e50SDavid S. Miller 			int end;
1180357b40a1SHerbert Xu 
11811a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
11821a028e50SDavid S. Miller 
11831a028e50SDavid S. Miller 			end = start + list->len;
1184357b40a1SHerbert Xu 			if ((copy = end - offset) > 0) {
1185357b40a1SHerbert Xu 				if (copy > len)
1186357b40a1SHerbert Xu 					copy = len;
11871a028e50SDavid S. Miller 				if (skb_store_bits(list, offset - start,
11881a028e50SDavid S. Miller 						   from, copy))
1189357b40a1SHerbert Xu 					goto fault;
1190357b40a1SHerbert Xu 				if ((len -= copy) == 0)
1191357b40a1SHerbert Xu 					return 0;
1192357b40a1SHerbert Xu 				offset += copy;
1193357b40a1SHerbert Xu 				from += copy;
1194357b40a1SHerbert Xu 			}
11951a028e50SDavid S. Miller 			start = end;
1196357b40a1SHerbert Xu 		}
1197357b40a1SHerbert Xu 	}
1198357b40a1SHerbert Xu 	if (!len)
1199357b40a1SHerbert Xu 		return 0;
1200357b40a1SHerbert Xu 
1201357b40a1SHerbert Xu fault:
1202357b40a1SHerbert Xu 	return -EFAULT;
1203357b40a1SHerbert Xu }
1204357b40a1SHerbert Xu 
1205357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1206357b40a1SHerbert Xu 
12071da177e4SLinus Torvalds /* Checksum skb data. */
12081da177e4SLinus Torvalds 
12092bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset,
12102bbbc868SAl Viro 			  int len, __wsum csum)
12111da177e4SLinus Torvalds {
12121a028e50SDavid S. Miller 	int start = skb_headlen(skb);
12131a028e50SDavid S. Miller 	int i, copy = start - offset;
12141da177e4SLinus Torvalds 	int pos = 0;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	/* Checksum header. */
12171da177e4SLinus Torvalds 	if (copy > 0) {
12181da177e4SLinus Torvalds 		if (copy > len)
12191da177e4SLinus Torvalds 			copy = len;
12201da177e4SLinus Torvalds 		csum = csum_partial(skb->data + offset, copy, csum);
12211da177e4SLinus Torvalds 		if ((len -= copy) == 0)
12221da177e4SLinus Torvalds 			return csum;
12231da177e4SLinus Torvalds 		offset += copy;
12241da177e4SLinus Torvalds 		pos	= copy;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12281a028e50SDavid S. Miller 		int end;
12291da177e4SLinus Torvalds 
12301a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
12311a028e50SDavid S. Miller 
12321a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
12331da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
123444bb9363SAl Viro 			__wsum csum2;
12351da177e4SLinus Torvalds 			u8 *vaddr;
12361da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
12371da177e4SLinus Torvalds 
12381da177e4SLinus Torvalds 			if (copy > len)
12391da177e4SLinus Torvalds 				copy = len;
12401da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
12411a028e50SDavid S. Miller 			csum2 = csum_partial(vaddr + frag->page_offset +
12421a028e50SDavid S. Miller 					     offset - start, copy, 0);
12431da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
12441da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
12451da177e4SLinus Torvalds 			if (!(len -= copy))
12461da177e4SLinus Torvalds 				return csum;
12471da177e4SLinus Torvalds 			offset += copy;
12481da177e4SLinus Torvalds 			pos    += copy;
12491da177e4SLinus Torvalds 		}
12501a028e50SDavid S. Miller 		start = end;
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
12541da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 		for (; list; list = list->next) {
12571a028e50SDavid S. Miller 			int end;
12581da177e4SLinus Torvalds 
12591a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
12601a028e50SDavid S. Miller 
12611a028e50SDavid S. Miller 			end = start + list->len;
12621da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
12635f92a738SAl Viro 				__wsum csum2;
12641da177e4SLinus Torvalds 				if (copy > len)
12651da177e4SLinus Torvalds 					copy = len;
12661a028e50SDavid S. Miller 				csum2 = skb_checksum(list, offset - start,
12671a028e50SDavid S. Miller 						     copy, 0);
12681da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
12691da177e4SLinus Torvalds 				if ((len -= copy) == 0)
12701da177e4SLinus Torvalds 					return csum;
12711da177e4SLinus Torvalds 				offset += copy;
12721da177e4SLinus Torvalds 				pos    += copy;
12731da177e4SLinus Torvalds 			}
12741a028e50SDavid S. Miller 			start = end;
12751da177e4SLinus Torvalds 		}
12761da177e4SLinus Torvalds 	}
127709a62660SKris Katterjohn 	BUG_ON(len);
12781da177e4SLinus Torvalds 
12791da177e4SLinus Torvalds 	return csum;
12801da177e4SLinus Torvalds }
12811da177e4SLinus Torvalds 
12821da177e4SLinus Torvalds /* Both of above in one bottle. */
12831da177e4SLinus Torvalds 
128481d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
128581d77662SAl Viro 				    u8 *to, int len, __wsum csum)
12861da177e4SLinus Torvalds {
12871a028e50SDavid S. Miller 	int start = skb_headlen(skb);
12881a028e50SDavid S. Miller 	int i, copy = start - offset;
12891da177e4SLinus Torvalds 	int pos = 0;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	/* Copy header. */
12921da177e4SLinus Torvalds 	if (copy > 0) {
12931da177e4SLinus Torvalds 		if (copy > len)
12941da177e4SLinus Torvalds 			copy = len;
12951da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
12961da177e4SLinus Torvalds 						 copy, csum);
12971da177e4SLinus Torvalds 		if ((len -= copy) == 0)
12981da177e4SLinus Torvalds 			return csum;
12991da177e4SLinus Torvalds 		offset += copy;
13001da177e4SLinus Torvalds 		to     += copy;
13011da177e4SLinus Torvalds 		pos	= copy;
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
13051a028e50SDavid S. Miller 		int end;
13061da177e4SLinus Torvalds 
13071a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
13081a028e50SDavid S. Miller 
13091a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
13101da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
13115084205fSAl Viro 			__wsum csum2;
13121da177e4SLinus Torvalds 			u8 *vaddr;
13131da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
13141da177e4SLinus Torvalds 
13151da177e4SLinus Torvalds 			if (copy > len)
13161da177e4SLinus Torvalds 				copy = len;
13171da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
13181da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
13191a028e50SDavid S. Miller 							  frag->page_offset +
13201a028e50SDavid S. Miller 							  offset - start, to,
13211a028e50SDavid S. Miller 							  copy, 0);
13221da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
13231da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
13241da177e4SLinus Torvalds 			if (!(len -= copy))
13251da177e4SLinus Torvalds 				return csum;
13261da177e4SLinus Torvalds 			offset += copy;
13271da177e4SLinus Torvalds 			to     += copy;
13281da177e4SLinus Torvalds 			pos    += copy;
13291da177e4SLinus Torvalds 		}
13301a028e50SDavid S. Miller 		start = end;
13311da177e4SLinus Torvalds 	}
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
13341da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 		for (; list; list = list->next) {
133781d77662SAl Viro 			__wsum csum2;
13381a028e50SDavid S. Miller 			int end;
13391da177e4SLinus Torvalds 
13401a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
13411a028e50SDavid S. Miller 
13421a028e50SDavid S. Miller 			end = start + list->len;
13431da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
13441da177e4SLinus Torvalds 				if (copy > len)
13451da177e4SLinus Torvalds 					copy = len;
13461a028e50SDavid S. Miller 				csum2 = skb_copy_and_csum_bits(list,
13471a028e50SDavid S. Miller 							       offset - start,
13481da177e4SLinus Torvalds 							       to, copy, 0);
13491da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
13501da177e4SLinus Torvalds 				if ((len -= copy) == 0)
13511da177e4SLinus Torvalds 					return csum;
13521da177e4SLinus Torvalds 				offset += copy;
13531da177e4SLinus Torvalds 				to     += copy;
13541da177e4SLinus Torvalds 				pos    += copy;
13551da177e4SLinus Torvalds 			}
13561a028e50SDavid S. Miller 			start = end;
13571da177e4SLinus Torvalds 		}
13581da177e4SLinus Torvalds 	}
135909a62660SKris Katterjohn 	BUG_ON(len);
13601da177e4SLinus Torvalds 	return csum;
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
13631da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
13641da177e4SLinus Torvalds {
1365d3bc23e7SAl Viro 	__wsum csum;
13661da177e4SLinus Torvalds 	long csstart;
13671da177e4SLinus Torvalds 
136884fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1369663ead3bSHerbert Xu 		csstart = skb->csum_start - skb_headroom(skb);
13701da177e4SLinus Torvalds 	else
13711da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
13721da177e4SLinus Torvalds 
137309a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
13741da177e4SLinus Torvalds 
1375d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, to, csstart);
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	csum = 0;
13781da177e4SLinus Torvalds 	if (csstart != skb->len)
13791da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
13801da177e4SLinus Torvalds 					      skb->len - csstart, 0);
13811da177e4SLinus Torvalds 
138284fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1383ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
13841da177e4SLinus Torvalds 
1385d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds /**
13901da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
13911da177e4SLinus Torvalds  *	@list: list to dequeue from
13921da177e4SLinus Torvalds  *
13931da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
13941da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
13951da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
13961da177e4SLinus Torvalds  */
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
13991da177e4SLinus Torvalds {
14001da177e4SLinus Torvalds 	unsigned long flags;
14011da177e4SLinus Torvalds 	struct sk_buff *result;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14041da177e4SLinus Torvalds 	result = __skb_dequeue(list);
14051da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
14061da177e4SLinus Torvalds 	return result;
14071da177e4SLinus Torvalds }
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds /**
14101da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
14111da177e4SLinus Torvalds  *	@list: list to dequeue from
14121da177e4SLinus Torvalds  *
14131da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
14141da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
14151da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
14161da177e4SLinus Torvalds  */
14171da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
14181da177e4SLinus Torvalds {
14191da177e4SLinus Torvalds 	unsigned long flags;
14201da177e4SLinus Torvalds 	struct sk_buff *result;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14231da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
14241da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
14251da177e4SLinus Torvalds 	return result;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds /**
14291da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
14301da177e4SLinus Torvalds  *	@list: list to empty
14311da177e4SLinus Torvalds  *
14321da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
14331da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
14341da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
14351da177e4SLinus Torvalds  */
14361da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
14371da177e4SLinus Torvalds {
14381da177e4SLinus Torvalds 	struct sk_buff *skb;
14391da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
14401da177e4SLinus Torvalds 		kfree_skb(skb);
14411da177e4SLinus Torvalds }
14421da177e4SLinus Torvalds 
14431da177e4SLinus Torvalds /**
14441da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
14451da177e4SLinus Torvalds  *	@list: list to use
14461da177e4SLinus Torvalds  *	@newsk: buffer to queue
14471da177e4SLinus Torvalds  *
14481da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
14491da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
14501da177e4SLinus Torvalds  *	safely.
14511da177e4SLinus Torvalds  *
14521da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
14531da177e4SLinus Torvalds  */
14541da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
14551da177e4SLinus Torvalds {
14561da177e4SLinus Torvalds 	unsigned long flags;
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14591da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
14601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
14611da177e4SLinus Torvalds }
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds /**
14641da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
14651da177e4SLinus Torvalds  *	@list: list to use
14661da177e4SLinus Torvalds  *	@newsk: buffer to queue
14671da177e4SLinus Torvalds  *
14681da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
14691da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
14701da177e4SLinus Torvalds  *	safely.
14711da177e4SLinus Torvalds  *
14721da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
14731da177e4SLinus Torvalds  */
14741da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
14751da177e4SLinus Torvalds {
14761da177e4SLinus Torvalds 	unsigned long flags;
14771da177e4SLinus Torvalds 
14781da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14791da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
14801da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
14811da177e4SLinus Torvalds }
14828728b834SDavid S. Miller 
14831da177e4SLinus Torvalds /**
14841da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
14851da177e4SLinus Torvalds  *	@skb: buffer to remove
14868728b834SDavid S. Miller  *	@list: list to use
14871da177e4SLinus Torvalds  *
14888728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
14898728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
14901da177e4SLinus Torvalds  *
14918728b834SDavid S. Miller  *	You must know what list the SKB is on.
14921da177e4SLinus Torvalds  */
14938728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
14941da177e4SLinus Torvalds {
14951da177e4SLinus Torvalds 	unsigned long flags;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14988728b834SDavid S. Miller 	__skb_unlink(skb, list);
14991da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds /**
15031da177e4SLinus Torvalds  *	skb_append	-	append a buffer
15041da177e4SLinus Torvalds  *	@old: buffer to insert after
15051da177e4SLinus Torvalds  *	@newsk: buffer to insert
15068728b834SDavid S. Miller  *	@list: list to use
15071da177e4SLinus Torvalds  *
15081da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
15091da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
15101da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
15111da177e4SLinus Torvalds  */
15128728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
15131da177e4SLinus Torvalds {
15141da177e4SLinus Torvalds 	unsigned long flags;
15151da177e4SLinus Torvalds 
15168728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
15178728b834SDavid S. Miller 	__skb_append(old, newsk, list);
15188728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds /**
15231da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
15241da177e4SLinus Torvalds  *	@old: buffer to insert before
15251da177e4SLinus Torvalds  *	@newsk: buffer to insert
15268728b834SDavid S. Miller  *	@list: list to use
15271da177e4SLinus Torvalds  *
15288728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
15298728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
15308728b834SDavid S. Miller  *	calls.
15318728b834SDavid S. Miller  *
15321da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
15331da177e4SLinus Torvalds  */
15348728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
15351da177e4SLinus Torvalds {
15361da177e4SLinus Torvalds 	unsigned long flags;
15371da177e4SLinus Torvalds 
15388728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
15398728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
15408728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
15411da177e4SLinus Torvalds }
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
15441da177e4SLinus Torvalds 					   struct sk_buff* skb1,
15451da177e4SLinus Torvalds 					   const u32 len, const int pos)
15461da177e4SLinus Torvalds {
15471da177e4SLinus Torvalds 	int i;
15481da177e4SLinus Torvalds 
1549d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len),
1550d626f62bSArnaldo Carvalho de Melo 					 pos - len);
15511da177e4SLinus Torvalds 	/* And move data appendix as is. */
15521da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
15531da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
15561da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
15571da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
15581da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
15591da177e4SLinus Torvalds 	skb->data_len		   = 0;
15601da177e4SLinus Torvalds 	skb->len		   = len;
156127a884dcSArnaldo Carvalho de Melo 	skb_set_tail_pointer(skb, len);
15621da177e4SLinus Torvalds }
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
15651da177e4SLinus Torvalds 				       struct sk_buff* skb1,
15661da177e4SLinus Torvalds 				       const u32 len, int pos)
15671da177e4SLinus Torvalds {
15681da177e4SLinus Torvalds 	int i, k = 0;
15691da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
15721da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
15731da177e4SLinus Torvalds 	skb->len		  = len;
15741da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
15771da177e4SLinus Torvalds 		int size = skb_shinfo(skb)->frags[i].size;
15781da177e4SLinus Torvalds 
15791da177e4SLinus Torvalds 		if (pos + size > len) {
15801da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 			if (pos < len) {
15831da177e4SLinus Torvalds 				/* Split frag.
15841da177e4SLinus Torvalds 				 * We have two variants in this case:
15851da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
15861da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
15871da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
15881da177e4SLinus Torvalds 				 *    where splitting is expensive.
15891da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
15901da177e4SLinus Torvalds 				 */
15911da177e4SLinus Torvalds 				get_page(skb_shinfo(skb)->frags[i].page);
15921da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
15931da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].size -= len - pos;
15941da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[i].size	= len - pos;
15951da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
15961da177e4SLinus Torvalds 			}
15971da177e4SLinus Torvalds 			k++;
15981da177e4SLinus Torvalds 		} else
15991da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
16001da177e4SLinus Torvalds 		pos += size;
16011da177e4SLinus Torvalds 	}
16021da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
16031da177e4SLinus Torvalds }
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds /**
16061da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
16071da177e4SLinus Torvalds  * @skb: the buffer to split
16081da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
16091da177e4SLinus Torvalds  * @len: new length for skb
16101da177e4SLinus Torvalds  */
16111da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
16121da177e4SLinus Torvalds {
16131da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
16161da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
16171da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
16181da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
16191da177e4SLinus Torvalds }
16201da177e4SLinus Torvalds 
1621677e90edSThomas Graf /**
1622677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
1623677e90edSThomas Graf  * @skb: the buffer to read
1624677e90edSThomas Graf  * @from: lower offset of data to be read
1625677e90edSThomas Graf  * @to: upper offset of data to be read
1626677e90edSThomas Graf  * @st: state variable
1627677e90edSThomas Graf  *
1628677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
1629677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
1630677e90edSThomas Graf  */
1631677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1632677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
1633677e90edSThomas Graf {
1634677e90edSThomas Graf 	st->lower_offset = from;
1635677e90edSThomas Graf 	st->upper_offset = to;
1636677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
1637677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
1638677e90edSThomas Graf 	st->frag_data = NULL;
1639677e90edSThomas Graf }
1640677e90edSThomas Graf 
1641677e90edSThomas Graf /**
1642677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
1643677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
1644677e90edSThomas Graf  * @data: destination pointer for data to be returned
1645677e90edSThomas Graf  * @st: state variable
1646677e90edSThomas Graf  *
1647677e90edSThomas Graf  * Reads a block of skb data at &consumed relative to the
1648677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
1649677e90edSThomas Graf  * the head of the data block to &data and returns the length
1650677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
1651677e90edSThomas Graf  * offset has been reached.
1652677e90edSThomas Graf  *
1653677e90edSThomas Graf  * The caller is not required to consume all of the data
1654677e90edSThomas Graf  * returned, i.e. &consumed is typically set to the number
1655677e90edSThomas Graf  * of bytes already consumed and the next call to
1656677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
1657677e90edSThomas Graf  *
1658677e90edSThomas Graf  * Note: The size of each block of data returned can be arbitary,
1659677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
1660677e90edSThomas Graf  *       reads of potentially non linear data.
1661677e90edSThomas Graf  *
1662677e90edSThomas Graf  * Note: Fragment lists within fragments are not implemented
1663677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
1664677e90edSThomas Graf  *       a stack for this purpose.
1665677e90edSThomas Graf  */
1666677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1667677e90edSThomas Graf 			  struct skb_seq_state *st)
1668677e90edSThomas Graf {
1669677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1670677e90edSThomas Graf 	skb_frag_t *frag;
1671677e90edSThomas Graf 
1672677e90edSThomas Graf 	if (unlikely(abs_offset >= st->upper_offset))
1673677e90edSThomas Graf 		return 0;
1674677e90edSThomas Graf 
1675677e90edSThomas Graf next_skb:
1676677e90edSThomas Graf 	block_limit = skb_headlen(st->cur_skb);
1677677e90edSThomas Graf 
1678677e90edSThomas Graf 	if (abs_offset < block_limit) {
1679677e90edSThomas Graf 		*data = st->cur_skb->data + abs_offset;
1680677e90edSThomas Graf 		return block_limit - abs_offset;
1681677e90edSThomas Graf 	}
1682677e90edSThomas Graf 
1683677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
1684677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
1685677e90edSThomas Graf 
1686677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1687677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1688677e90edSThomas Graf 		block_limit = frag->size + st->stepped_offset;
1689677e90edSThomas Graf 
1690677e90edSThomas Graf 		if (abs_offset < block_limit) {
1691677e90edSThomas Graf 			if (!st->frag_data)
1692677e90edSThomas Graf 				st->frag_data = kmap_skb_frag(frag);
1693677e90edSThomas Graf 
1694677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
1695677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
1696677e90edSThomas Graf 
1697677e90edSThomas Graf 			return block_limit - abs_offset;
1698677e90edSThomas Graf 		}
1699677e90edSThomas Graf 
1700677e90edSThomas Graf 		if (st->frag_data) {
1701677e90edSThomas Graf 			kunmap_skb_frag(st->frag_data);
1702677e90edSThomas Graf 			st->frag_data = NULL;
1703677e90edSThomas Graf 		}
1704677e90edSThomas Graf 
1705677e90edSThomas Graf 		st->frag_idx++;
1706677e90edSThomas Graf 		st->stepped_offset += frag->size;
1707677e90edSThomas Graf 	}
1708677e90edSThomas Graf 
17095b5a60daSOlaf Kirch 	if (st->frag_data) {
17105b5a60daSOlaf Kirch 		kunmap_skb_frag(st->frag_data);
17115b5a60daSOlaf Kirch 		st->frag_data = NULL;
17125b5a60daSOlaf Kirch 	}
17135b5a60daSOlaf Kirch 
1714677e90edSThomas Graf 	if (st->cur_skb->next) {
1715677e90edSThomas Graf 		st->cur_skb = st->cur_skb->next;
1716677e90edSThomas Graf 		st->frag_idx = 0;
1717677e90edSThomas Graf 		goto next_skb;
1718677e90edSThomas Graf 	} else if (st->root_skb == st->cur_skb &&
1719677e90edSThomas Graf 		   skb_shinfo(st->root_skb)->frag_list) {
1720677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1721677e90edSThomas Graf 		goto next_skb;
1722677e90edSThomas Graf 	}
1723677e90edSThomas Graf 
1724677e90edSThomas Graf 	return 0;
1725677e90edSThomas Graf }
1726677e90edSThomas Graf 
1727677e90edSThomas Graf /**
1728677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
1729677e90edSThomas Graf  * @st: state variable
1730677e90edSThomas Graf  *
1731677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
1732677e90edSThomas Graf  * returned 0.
1733677e90edSThomas Graf  */
1734677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
1735677e90edSThomas Graf {
1736677e90edSThomas Graf 	if (st->frag_data)
1737677e90edSThomas Graf 		kunmap_skb_frag(st->frag_data);
1738677e90edSThomas Graf }
1739677e90edSThomas Graf 
17403fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
17413fc7e8a6SThomas Graf 
17423fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
17433fc7e8a6SThomas Graf 					  struct ts_config *conf,
17443fc7e8a6SThomas Graf 					  struct ts_state *state)
17453fc7e8a6SThomas Graf {
17463fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
17473fc7e8a6SThomas Graf }
17483fc7e8a6SThomas Graf 
17493fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
17503fc7e8a6SThomas Graf {
17513fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
17523fc7e8a6SThomas Graf }
17533fc7e8a6SThomas Graf 
17543fc7e8a6SThomas Graf /**
17553fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
17563fc7e8a6SThomas Graf  * @skb: the buffer to look in
17573fc7e8a6SThomas Graf  * @from: search offset
17583fc7e8a6SThomas Graf  * @to: search limit
17593fc7e8a6SThomas Graf  * @config: textsearch configuration
17603fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
17613fc7e8a6SThomas Graf  *
17623fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
17633fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
17643fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
17653fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
17663fc7e8a6SThomas Graf  */
17673fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
17683fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
17693fc7e8a6SThomas Graf 			   struct ts_state *state)
17703fc7e8a6SThomas Graf {
1771f72b948dSPhil Oester 	unsigned int ret;
1772f72b948dSPhil Oester 
17733fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
17743fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
17753fc7e8a6SThomas Graf 
17763fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
17773fc7e8a6SThomas Graf 
1778f72b948dSPhil Oester 	ret = textsearch_find(config, state);
1779f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
17803fc7e8a6SThomas Graf }
17813fc7e8a6SThomas Graf 
1782e89e9cf5SAnanda Raju /**
1783e89e9cf5SAnanda Raju  * skb_append_datato_frags: - append the user data to a skb
1784e89e9cf5SAnanda Raju  * @sk: sock  structure
1785e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
1786e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
1787e89e9cf5SAnanda Raju  * @from: pointer to user message iov
1788e89e9cf5SAnanda Raju  * @length: length of the iov message
1789e89e9cf5SAnanda Raju  *
1790e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
1791e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
1792e89e9cf5SAnanda Raju  */
1793e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1794dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
1795e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
1796e89e9cf5SAnanda Raju 			void *from, int length)
1797e89e9cf5SAnanda Raju {
1798e89e9cf5SAnanda Raju 	int frg_cnt = 0;
1799e89e9cf5SAnanda Raju 	skb_frag_t *frag = NULL;
1800e89e9cf5SAnanda Raju 	struct page *page = NULL;
1801e89e9cf5SAnanda Raju 	int copy, left;
1802e89e9cf5SAnanda Raju 	int offset = 0;
1803e89e9cf5SAnanda Raju 	int ret;
1804e89e9cf5SAnanda Raju 
1805e89e9cf5SAnanda Raju 	do {
1806e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
1807e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
1808e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
1809e89e9cf5SAnanda Raju 			return -EFAULT;
1810e89e9cf5SAnanda Raju 
1811e89e9cf5SAnanda Raju 		/* allocate a new page for next frag */
1812e89e9cf5SAnanda Raju 		page = alloc_pages(sk->sk_allocation, 0);
1813e89e9cf5SAnanda Raju 
1814e89e9cf5SAnanda Raju 		/* If alloc_page fails just return failure and caller will
1815e89e9cf5SAnanda Raju 		 * free previous allocated pages by doing kfree_skb()
1816e89e9cf5SAnanda Raju 		 */
1817e89e9cf5SAnanda Raju 		if (page == NULL)
1818e89e9cf5SAnanda Raju 			return -ENOMEM;
1819e89e9cf5SAnanda Raju 
1820e89e9cf5SAnanda Raju 		/* initialize the next frag */
1821e89e9cf5SAnanda Raju 		sk->sk_sndmsg_page = page;
1822e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off = 0;
1823e89e9cf5SAnanda Raju 		skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1824e89e9cf5SAnanda Raju 		skb->truesize += PAGE_SIZE;
1825e89e9cf5SAnanda Raju 		atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1826e89e9cf5SAnanda Raju 
1827e89e9cf5SAnanda Raju 		/* get the new initialized frag */
1828e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
1829e89e9cf5SAnanda Raju 		frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1830e89e9cf5SAnanda Raju 
1831e89e9cf5SAnanda Raju 		/* copy the user data to page */
1832e89e9cf5SAnanda Raju 		left = PAGE_SIZE - frag->page_offset;
1833e89e9cf5SAnanda Raju 		copy = (length > left)? left : length;
1834e89e9cf5SAnanda Raju 
1835e89e9cf5SAnanda Raju 		ret = getfrag(from, (page_address(frag->page) +
1836e89e9cf5SAnanda Raju 			    frag->page_offset + frag->size),
1837e89e9cf5SAnanda Raju 			    offset, copy, 0, skb);
1838e89e9cf5SAnanda Raju 		if (ret < 0)
1839e89e9cf5SAnanda Raju 			return -EFAULT;
1840e89e9cf5SAnanda Raju 
1841e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
1842e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off += copy;
1843e89e9cf5SAnanda Raju 		frag->size += copy;
1844e89e9cf5SAnanda Raju 		skb->len += copy;
1845e89e9cf5SAnanda Raju 		skb->data_len += copy;
1846e89e9cf5SAnanda Raju 		offset += copy;
1847e89e9cf5SAnanda Raju 		length -= copy;
1848e89e9cf5SAnanda Raju 
1849e89e9cf5SAnanda Raju 	} while (length > 0);
1850e89e9cf5SAnanda Raju 
1851e89e9cf5SAnanda Raju 	return 0;
1852e89e9cf5SAnanda Raju }
1853e89e9cf5SAnanda Raju 
1854cbb042f9SHerbert Xu /**
1855cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
1856cbb042f9SHerbert Xu  *	@skb: buffer to update
1857cbb042f9SHerbert Xu  *	@start: start of data before pull
1858cbb042f9SHerbert Xu  *	@len: length of data pulled
1859cbb042f9SHerbert Xu  *
1860cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
186184fa7933SPatrick McHardy  *	update the CHECKSUM_COMPLETE checksum.  It should be used on
186284fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
186384fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
186484fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
1865cbb042f9SHerbert Xu  */
1866cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1867cbb042f9SHerbert Xu {
1868cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
1869cbb042f9SHerbert Xu 	skb->len -= len;
1870cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
1871cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
1872cbb042f9SHerbert Xu 	return skb->data += len;
1873cbb042f9SHerbert Xu }
1874cbb042f9SHerbert Xu 
1875f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1876f94691acSArnaldo Carvalho de Melo 
1877f4c50d99SHerbert Xu /**
1878f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
1879f4c50d99SHerbert Xu  *	@skb: buffer to segment
1880576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
1881f4c50d99SHerbert Xu  *
1882f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
1883f4c50d99SHerbert Xu  *	the segment at the given position.  It returns NULL if there are
1884f4c50d99SHerbert Xu  *	no more segments to generate, or when an error is encountered.
1885f4c50d99SHerbert Xu  */
1886576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1887f4c50d99SHerbert Xu {
1888f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
1889f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
1890f4c50d99SHerbert Xu 	unsigned int mss = skb_shinfo(skb)->gso_size;
189198e399f8SArnaldo Carvalho de Melo 	unsigned int doffset = skb->data - skb_mac_header(skb);
1892f4c50d99SHerbert Xu 	unsigned int offset = doffset;
1893f4c50d99SHerbert Xu 	unsigned int headroom;
1894f4c50d99SHerbert Xu 	unsigned int len;
1895576a30ebSHerbert Xu 	int sg = features & NETIF_F_SG;
1896f4c50d99SHerbert Xu 	int nfrags = skb_shinfo(skb)->nr_frags;
1897f4c50d99SHerbert Xu 	int err = -ENOMEM;
1898f4c50d99SHerbert Xu 	int i = 0;
1899f4c50d99SHerbert Xu 	int pos;
1900f4c50d99SHerbert Xu 
1901f4c50d99SHerbert Xu 	__skb_push(skb, doffset);
1902f4c50d99SHerbert Xu 	headroom = skb_headroom(skb);
1903f4c50d99SHerbert Xu 	pos = skb_headlen(skb);
1904f4c50d99SHerbert Xu 
1905f4c50d99SHerbert Xu 	do {
1906f4c50d99SHerbert Xu 		struct sk_buff *nskb;
1907f4c50d99SHerbert Xu 		skb_frag_t *frag;
1908c8884eddSHerbert Xu 		int hsize;
1909f4c50d99SHerbert Xu 		int k;
1910f4c50d99SHerbert Xu 		int size;
1911f4c50d99SHerbert Xu 
1912f4c50d99SHerbert Xu 		len = skb->len - offset;
1913f4c50d99SHerbert Xu 		if (len > mss)
1914f4c50d99SHerbert Xu 			len = mss;
1915f4c50d99SHerbert Xu 
1916f4c50d99SHerbert Xu 		hsize = skb_headlen(skb) - offset;
1917f4c50d99SHerbert Xu 		if (hsize < 0)
1918f4c50d99SHerbert Xu 			hsize = 0;
1919c8884eddSHerbert Xu 		if (hsize > len || !sg)
1920c8884eddSHerbert Xu 			hsize = len;
1921f4c50d99SHerbert Xu 
1922c8884eddSHerbert Xu 		nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1923f4c50d99SHerbert Xu 		if (unlikely(!nskb))
1924f4c50d99SHerbert Xu 			goto err;
1925f4c50d99SHerbert Xu 
1926f4c50d99SHerbert Xu 		if (segs)
1927f4c50d99SHerbert Xu 			tail->next = nskb;
1928f4c50d99SHerbert Xu 		else
1929f4c50d99SHerbert Xu 			segs = nskb;
1930f4c50d99SHerbert Xu 		tail = nskb;
1931f4c50d99SHerbert Xu 
1932f4c50d99SHerbert Xu 		nskb->dev = skb->dev;
1933f25f4e44SPeter P Waskiewicz Jr 		skb_copy_queue_mapping(nskb, skb);
1934f4c50d99SHerbert Xu 		nskb->priority = skb->priority;
1935f4c50d99SHerbert Xu 		nskb->protocol = skb->protocol;
1936f4c50d99SHerbert Xu 		nskb->dst = dst_clone(skb->dst);
1937f4c50d99SHerbert Xu 		memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1938f4c50d99SHerbert Xu 		nskb->pkt_type = skb->pkt_type;
1939f4c50d99SHerbert Xu 		nskb->mac_len = skb->mac_len;
1940f4c50d99SHerbert Xu 
1941f4c50d99SHerbert Xu 		skb_reserve(nskb, headroom);
1942459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(nskb);
1943ddc7b8e3SArnaldo Carvalho de Melo 		skb_set_network_header(nskb, skb->mac_len);
1944b0e380b1SArnaldo Carvalho de Melo 		nskb->transport_header = (nskb->network_header +
1945b0e380b1SArnaldo Carvalho de Melo 					  skb_network_header_len(skb));
1946d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data(skb, skb_put(nskb, doffset),
1947d626f62bSArnaldo Carvalho de Melo 					  doffset);
1948f4c50d99SHerbert Xu 		if (!sg) {
1949f4c50d99SHerbert Xu 			nskb->csum = skb_copy_and_csum_bits(skb, offset,
1950f4c50d99SHerbert Xu 							    skb_put(nskb, len),
1951f4c50d99SHerbert Xu 							    len, 0);
1952f4c50d99SHerbert Xu 			continue;
1953f4c50d99SHerbert Xu 		}
1954f4c50d99SHerbert Xu 
1955f4c50d99SHerbert Xu 		frag = skb_shinfo(nskb)->frags;
1956f4c50d99SHerbert Xu 		k = 0;
1957f4c50d99SHerbert Xu 
195884fa7933SPatrick McHardy 		nskb->ip_summed = CHECKSUM_PARTIAL;
1959f4c50d99SHerbert Xu 		nskb->csum = skb->csum;
1960d626f62bSArnaldo Carvalho de Melo 		skb_copy_from_linear_data_offset(skb, offset,
1961d626f62bSArnaldo Carvalho de Melo 						 skb_put(nskb, hsize), hsize);
1962f4c50d99SHerbert Xu 
1963f4c50d99SHerbert Xu 		while (pos < offset + len) {
1964f4c50d99SHerbert Xu 			BUG_ON(i >= nfrags);
1965f4c50d99SHerbert Xu 
1966f4c50d99SHerbert Xu 			*frag = skb_shinfo(skb)->frags[i];
1967f4c50d99SHerbert Xu 			get_page(frag->page);
1968f4c50d99SHerbert Xu 			size = frag->size;
1969f4c50d99SHerbert Xu 
1970f4c50d99SHerbert Xu 			if (pos < offset) {
1971f4c50d99SHerbert Xu 				frag->page_offset += offset - pos;
1972f4c50d99SHerbert Xu 				frag->size -= offset - pos;
1973f4c50d99SHerbert Xu 			}
1974f4c50d99SHerbert Xu 
1975f4c50d99SHerbert Xu 			k++;
1976f4c50d99SHerbert Xu 
1977f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
1978f4c50d99SHerbert Xu 				i++;
1979f4c50d99SHerbert Xu 				pos += size;
1980f4c50d99SHerbert Xu 			} else {
1981f4c50d99SHerbert Xu 				frag->size -= pos + size - (offset + len);
1982f4c50d99SHerbert Xu 				break;
1983f4c50d99SHerbert Xu 			}
1984f4c50d99SHerbert Xu 
1985f4c50d99SHerbert Xu 			frag++;
1986f4c50d99SHerbert Xu 		}
1987f4c50d99SHerbert Xu 
1988f4c50d99SHerbert Xu 		skb_shinfo(nskb)->nr_frags = k;
1989f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
1990f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
1991f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
1992f4c50d99SHerbert Xu 	} while ((offset += len) < skb->len);
1993f4c50d99SHerbert Xu 
1994f4c50d99SHerbert Xu 	return segs;
1995f4c50d99SHerbert Xu 
1996f4c50d99SHerbert Xu err:
1997f4c50d99SHerbert Xu 	while ((skb = segs)) {
1998f4c50d99SHerbert Xu 		segs = skb->next;
1999b08d5840SPatrick McHardy 		kfree_skb(skb);
2000f4c50d99SHerbert Xu 	}
2001f4c50d99SHerbert Xu 	return ERR_PTR(err);
2002f4c50d99SHerbert Xu }
2003f4c50d99SHerbert Xu 
2004f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
2005f4c50d99SHerbert Xu 
20061da177e4SLinus Torvalds void __init skb_init(void)
20071da177e4SLinus Torvalds {
20081da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
20091da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
20101da177e4SLinus Torvalds 					      0,
2011e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
201220c2df83SPaul Mundt 					      NULL);
2013d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
2014d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
2015d179cd12SDavid S. Miller 						sizeof(atomic_t),
2016d179cd12SDavid S. Miller 						0,
2017e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
201820c2df83SPaul Mundt 						NULL);
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
2021716ea3a7SDavid Howells /**
2022716ea3a7SDavid Howells  *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer
2023716ea3a7SDavid Howells  *	@skb: Socket buffer containing the buffers to be mapped
2024716ea3a7SDavid Howells  *	@sg: The scatter-gather list to map into
2025716ea3a7SDavid Howells  *	@offset: The offset into the buffer's contents to start mapping
2026716ea3a7SDavid Howells  *	@len: Length of buffer space to be mapped
2027716ea3a7SDavid Howells  *
2028716ea3a7SDavid Howells  *	Fill the specified scatter-gather list with mappings/pointers into a
2029716ea3a7SDavid Howells  *	region of the buffer space attached to a socket buffer.
2030716ea3a7SDavid Howells  */
203151c739d1SDavid S. Miller static int
203251c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
2033716ea3a7SDavid Howells {
20341a028e50SDavid S. Miller 	int start = skb_headlen(skb);
20351a028e50SDavid S. Miller 	int i, copy = start - offset;
2036716ea3a7SDavid Howells 	int elt = 0;
2037716ea3a7SDavid Howells 
2038716ea3a7SDavid Howells 	if (copy > 0) {
2039716ea3a7SDavid Howells 		if (copy > len)
2040716ea3a7SDavid Howells 			copy = len;
2041642f1490SJens Axboe 		sg_set_buf(sg, skb->data + offset, copy);
2042716ea3a7SDavid Howells 		elt++;
2043716ea3a7SDavid Howells 		if ((len -= copy) == 0)
2044716ea3a7SDavid Howells 			return elt;
2045716ea3a7SDavid Howells 		offset += copy;
2046716ea3a7SDavid Howells 	}
2047716ea3a7SDavid Howells 
2048716ea3a7SDavid Howells 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
20491a028e50SDavid S. Miller 		int end;
2050716ea3a7SDavid Howells 
20511a028e50SDavid S. Miller 		BUG_TRAP(start <= offset + len);
20521a028e50SDavid S. Miller 
20531a028e50SDavid S. Miller 		end = start + skb_shinfo(skb)->frags[i].size;
2054716ea3a7SDavid Howells 		if ((copy = end - offset) > 0) {
2055716ea3a7SDavid Howells 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2056716ea3a7SDavid Howells 
2057716ea3a7SDavid Howells 			if (copy > len)
2058716ea3a7SDavid Howells 				copy = len;
2059642f1490SJens Axboe 			sg_set_page(&sg[elt], frag->page, copy,
2060642f1490SJens Axboe 					frag->page_offset+offset-start);
2061716ea3a7SDavid Howells 			elt++;
2062716ea3a7SDavid Howells 			if (!(len -= copy))
2063716ea3a7SDavid Howells 				return elt;
2064716ea3a7SDavid Howells 			offset += copy;
2065716ea3a7SDavid Howells 		}
20661a028e50SDavid S. Miller 		start = end;
2067716ea3a7SDavid Howells 	}
2068716ea3a7SDavid Howells 
2069716ea3a7SDavid Howells 	if (skb_shinfo(skb)->frag_list) {
2070716ea3a7SDavid Howells 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
2071716ea3a7SDavid Howells 
2072716ea3a7SDavid Howells 		for (; list; list = list->next) {
20731a028e50SDavid S. Miller 			int end;
2074716ea3a7SDavid Howells 
20751a028e50SDavid S. Miller 			BUG_TRAP(start <= offset + len);
20761a028e50SDavid S. Miller 
20771a028e50SDavid S. Miller 			end = start + list->len;
2078716ea3a7SDavid Howells 			if ((copy = end - offset) > 0) {
2079716ea3a7SDavid Howells 				if (copy > len)
2080716ea3a7SDavid Howells 					copy = len;
208151c739d1SDavid S. Miller 				elt += __skb_to_sgvec(list, sg+elt, offset - start,
208251c739d1SDavid S. Miller 						      copy);
2083716ea3a7SDavid Howells 				if ((len -= copy) == 0)
2084716ea3a7SDavid Howells 					return elt;
2085716ea3a7SDavid Howells 				offset += copy;
2086716ea3a7SDavid Howells 			}
20871a028e50SDavid S. Miller 			start = end;
2088716ea3a7SDavid Howells 		}
2089716ea3a7SDavid Howells 	}
2090716ea3a7SDavid Howells 	BUG_ON(len);
2091716ea3a7SDavid Howells 	return elt;
2092716ea3a7SDavid Howells }
2093716ea3a7SDavid Howells 
209451c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
209551c739d1SDavid S. Miller {
209651c739d1SDavid S. Miller 	int nsg = __skb_to_sgvec(skb, sg, offset, len);
209751c739d1SDavid S. Miller 
2098c46f2334SJens Axboe 	sg_mark_end(&sg[nsg - 1]);
209951c739d1SDavid S. Miller 
210051c739d1SDavid S. Miller 	return nsg;
210151c739d1SDavid S. Miller }
210251c739d1SDavid S. Miller 
2103716ea3a7SDavid Howells /**
2104716ea3a7SDavid Howells  *	skb_cow_data - Check that a socket buffer's data buffers are writable
2105716ea3a7SDavid Howells  *	@skb: The socket buffer to check.
2106716ea3a7SDavid Howells  *	@tailbits: Amount of trailing space to be added
2107716ea3a7SDavid Howells  *	@trailer: Returned pointer to the skb where the @tailbits space begins
2108716ea3a7SDavid Howells  *
2109716ea3a7SDavid Howells  *	Make sure that the data buffers attached to a socket buffer are
2110716ea3a7SDavid Howells  *	writable. If they are not, private copies are made of the data buffers
2111716ea3a7SDavid Howells  *	and the socket buffer is set to use these instead.
2112716ea3a7SDavid Howells  *
2113716ea3a7SDavid Howells  *	If @tailbits is given, make sure that there is space to write @tailbits
2114716ea3a7SDavid Howells  *	bytes of data beyond current end of socket buffer.  @trailer will be
2115716ea3a7SDavid Howells  *	set to point to the skb in which this space begins.
2116716ea3a7SDavid Howells  *
2117716ea3a7SDavid Howells  *	The number of scatterlist elements required to completely map the
2118716ea3a7SDavid Howells  *	COW'd and extended socket buffer will be returned.
2119716ea3a7SDavid Howells  */
2120716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
2121716ea3a7SDavid Howells {
2122716ea3a7SDavid Howells 	int copyflag;
2123716ea3a7SDavid Howells 	int elt;
2124716ea3a7SDavid Howells 	struct sk_buff *skb1, **skb_p;
2125716ea3a7SDavid Howells 
2126716ea3a7SDavid Howells 	/* If skb is cloned or its head is paged, reallocate
2127716ea3a7SDavid Howells 	 * head pulling out all the pages (pages are considered not writable
2128716ea3a7SDavid Howells 	 * at the moment even if they are anonymous).
2129716ea3a7SDavid Howells 	 */
2130716ea3a7SDavid Howells 	if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
2131716ea3a7SDavid Howells 	    __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
2132716ea3a7SDavid Howells 		return -ENOMEM;
2133716ea3a7SDavid Howells 
2134716ea3a7SDavid Howells 	/* Easy case. Most of packets will go this way. */
2135716ea3a7SDavid Howells 	if (!skb_shinfo(skb)->frag_list) {
2136716ea3a7SDavid Howells 		/* A little of trouble, not enough of space for trailer.
2137716ea3a7SDavid Howells 		 * This should not happen, when stack is tuned to generate
2138716ea3a7SDavid Howells 		 * good frames. OK, on miss we reallocate and reserve even more
2139716ea3a7SDavid Howells 		 * space, 128 bytes is fair. */
2140716ea3a7SDavid Howells 
2141716ea3a7SDavid Howells 		if (skb_tailroom(skb) < tailbits &&
2142716ea3a7SDavid Howells 		    pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
2143716ea3a7SDavid Howells 			return -ENOMEM;
2144716ea3a7SDavid Howells 
2145716ea3a7SDavid Howells 		/* Voila! */
2146716ea3a7SDavid Howells 		*trailer = skb;
2147716ea3a7SDavid Howells 		return 1;
2148716ea3a7SDavid Howells 	}
2149716ea3a7SDavid Howells 
2150716ea3a7SDavid Howells 	/* Misery. We are in troubles, going to mincer fragments... */
2151716ea3a7SDavid Howells 
2152716ea3a7SDavid Howells 	elt = 1;
2153716ea3a7SDavid Howells 	skb_p = &skb_shinfo(skb)->frag_list;
2154716ea3a7SDavid Howells 	copyflag = 0;
2155716ea3a7SDavid Howells 
2156716ea3a7SDavid Howells 	while ((skb1 = *skb_p) != NULL) {
2157716ea3a7SDavid Howells 		int ntail = 0;
2158716ea3a7SDavid Howells 
2159716ea3a7SDavid Howells 		/* The fragment is partially pulled by someone,
2160716ea3a7SDavid Howells 		 * this can happen on input. Copy it and everything
2161716ea3a7SDavid Howells 		 * after it. */
2162716ea3a7SDavid Howells 
2163716ea3a7SDavid Howells 		if (skb_shared(skb1))
2164716ea3a7SDavid Howells 			copyflag = 1;
2165716ea3a7SDavid Howells 
2166716ea3a7SDavid Howells 		/* If the skb is the last, worry about trailer. */
2167716ea3a7SDavid Howells 
2168716ea3a7SDavid Howells 		if (skb1->next == NULL && tailbits) {
2169716ea3a7SDavid Howells 			if (skb_shinfo(skb1)->nr_frags ||
2170716ea3a7SDavid Howells 			    skb_shinfo(skb1)->frag_list ||
2171716ea3a7SDavid Howells 			    skb_tailroom(skb1) < tailbits)
2172716ea3a7SDavid Howells 				ntail = tailbits + 128;
2173716ea3a7SDavid Howells 		}
2174716ea3a7SDavid Howells 
2175716ea3a7SDavid Howells 		if (copyflag ||
2176716ea3a7SDavid Howells 		    skb_cloned(skb1) ||
2177716ea3a7SDavid Howells 		    ntail ||
2178716ea3a7SDavid Howells 		    skb_shinfo(skb1)->nr_frags ||
2179716ea3a7SDavid Howells 		    skb_shinfo(skb1)->frag_list) {
2180716ea3a7SDavid Howells 			struct sk_buff *skb2;
2181716ea3a7SDavid Howells 
2182716ea3a7SDavid Howells 			/* Fuck, we are miserable poor guys... */
2183716ea3a7SDavid Howells 			if (ntail == 0)
2184716ea3a7SDavid Howells 				skb2 = skb_copy(skb1, GFP_ATOMIC);
2185716ea3a7SDavid Howells 			else
2186716ea3a7SDavid Howells 				skb2 = skb_copy_expand(skb1,
2187716ea3a7SDavid Howells 						       skb_headroom(skb1),
2188716ea3a7SDavid Howells 						       ntail,
2189716ea3a7SDavid Howells 						       GFP_ATOMIC);
2190716ea3a7SDavid Howells 			if (unlikely(skb2 == NULL))
2191716ea3a7SDavid Howells 				return -ENOMEM;
2192716ea3a7SDavid Howells 
2193716ea3a7SDavid Howells 			if (skb1->sk)
2194716ea3a7SDavid Howells 				skb_set_owner_w(skb2, skb1->sk);
2195716ea3a7SDavid Howells 
2196716ea3a7SDavid Howells 			/* Looking around. Are we still alive?
2197716ea3a7SDavid Howells 			 * OK, link new skb, drop old one */
2198716ea3a7SDavid Howells 
2199716ea3a7SDavid Howells 			skb2->next = skb1->next;
2200716ea3a7SDavid Howells 			*skb_p = skb2;
2201716ea3a7SDavid Howells 			kfree_skb(skb1);
2202716ea3a7SDavid Howells 			skb1 = skb2;
2203716ea3a7SDavid Howells 		}
2204716ea3a7SDavid Howells 		elt++;
2205716ea3a7SDavid Howells 		*trailer = skb1;
2206716ea3a7SDavid Howells 		skb_p = &skb1->next;
2207716ea3a7SDavid Howells 	}
2208716ea3a7SDavid Howells 
2209716ea3a7SDavid Howells 	return elt;
2210716ea3a7SDavid Howells }
2211716ea3a7SDavid Howells 
22121da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim);
22131da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb);
2214231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb);
22151da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail);
2216d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
22178af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb);
22181da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy);
22191da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head);
22201da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum);
22211da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone);
22221da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy);
22231da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits);
22241da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev);
22251da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits);
22261da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand);
22271da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic);
22281da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad);
22291da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom);
22301da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic);
22311da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue);
22321da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail);
22331da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert);
22341da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge);
22351da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head);
22361da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail);
22371da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink);
22381da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append);
22391da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split);
2240677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read);
2241677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read);
2242677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read);
22433fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text);
2244e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags);
2245716ea3a7SDavid Howells 
2246716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_to_sgvec);
2247716ea3a7SDavid Howells EXPORT_SYMBOL_GPL(skb_cow_data);
2248