xref: /openbmc/linux/net/core/skbuff.c (revision edda553c)
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>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <net/protocol.h>
601da177e4SLinus Torvalds #include <net/dst.h>
611da177e4SLinus Torvalds #include <net/sock.h>
621da177e4SLinus Torvalds #include <net/checksum.h>
631da177e4SLinus Torvalds #include <net/xfrm.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include <asm/uaccess.h>
661da177e4SLinus Torvalds #include <asm/system.h>
671da177e4SLinus Torvalds 
68a1f8e7f7SAl Viro #include "kmap_skb.h"
69a1f8e7f7SAl Viro 
70e18b890bSChristoph Lameter static struct kmem_cache *skbuff_head_cache __read_mostly;
71e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  *	Keep out-of-line to prevent kernel bloat.
751da177e4SLinus Torvalds  *	__builtin_return_address is not used because it is not always
761da177e4SLinus Torvalds  *	reliable.
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /**
801da177e4SLinus Torvalds  *	skb_over_panic	- 	private function
811da177e4SLinus Torvalds  *	@skb: buffer
821da177e4SLinus Torvalds  *	@sz: size
831da177e4SLinus Torvalds  *	@here: address
841da177e4SLinus Torvalds  *
851da177e4SLinus Torvalds  *	Out of line support code for skb_put(). Not user callable.
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds void skb_over_panic(struct sk_buff *skb, int sz, void *here)
881da177e4SLinus Torvalds {
8926095455SPatrick McHardy 	printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p "
9026095455SPatrick McHardy 			  "data:%p tail:%p end:%p dev:%s\n",
9126095455SPatrick McHardy 	       here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
9226095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
931da177e4SLinus Torvalds 	BUG();
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /**
971da177e4SLinus Torvalds  *	skb_under_panic	- 	private function
981da177e4SLinus Torvalds  *	@skb: buffer
991da177e4SLinus Torvalds  *	@sz: size
1001da177e4SLinus Torvalds  *	@here: address
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  *	Out of line support code for skb_push(). Not user callable.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds void skb_under_panic(struct sk_buff *skb, int sz, void *here)
1061da177e4SLinus Torvalds {
10726095455SPatrick McHardy 	printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p "
10826095455SPatrick McHardy 			  "data:%p tail:%p end:%p dev:%s\n",
10926095455SPatrick McHardy 	       here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end,
11026095455SPatrick McHardy 	       skb->dev ? skb->dev->name : "<NULL>");
1111da177e4SLinus Torvalds 	BUG();
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
114dc6de336SDavid S. Miller void skb_truesize_bug(struct sk_buff *skb)
115dc6de336SDavid S. Miller {
116dc6de336SDavid S. Miller 	printk(KERN_ERR "SKB BUG: Invalid truesize (%u) "
117dc6de336SDavid S. Miller 	       "len=%u, sizeof(sk_buff)=%Zd\n",
118dc6de336SDavid S. Miller 	       skb->truesize, skb->len, sizeof(struct sk_buff));
119dc6de336SDavid S. Miller }
120dc6de336SDavid S. Miller EXPORT_SYMBOL(skb_truesize_bug);
121dc6de336SDavid S. Miller 
1221da177e4SLinus Torvalds /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few
1231da177e4SLinus Torvalds  *	'private' fields and also do memory statistics to find all the
1241da177e4SLinus Torvalds  *	[BEEP] leaks.
1251da177e4SLinus Torvalds  *
1261da177e4SLinus Torvalds  */
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /**
129d179cd12SDavid S. Miller  *	__alloc_skb	-	allocate a network buffer
1301da177e4SLinus Torvalds  *	@size: size to allocate
1311da177e4SLinus Torvalds  *	@gfp_mask: allocation mask
132c83c2486SRandy Dunlap  *	@fclone: allocate from fclone cache instead of head cache
133c83c2486SRandy Dunlap  *		and allocate a cloned (child) skb
134b30973f8SChristoph Hellwig  *	@node: numa node to allocate memory on
1351da177e4SLinus Torvalds  *
1361da177e4SLinus Torvalds  *	Allocate a new &sk_buff. The returned buffer has no headroom and a
1371da177e4SLinus Torvalds  *	tail room of size bytes. The object has a reference count of one.
1381da177e4SLinus Torvalds  *	The return is the buffer. On a failure the return is %NULL.
1391da177e4SLinus Torvalds  *
1401da177e4SLinus Torvalds  *	Buffers may only be allocated from interrupts using a @gfp_mask of
1411da177e4SLinus Torvalds  *	%GFP_ATOMIC.
1421da177e4SLinus Torvalds  */
143dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
144b30973f8SChristoph Hellwig 			    int fclone, int node)
1451da177e4SLinus Torvalds {
146e18b890bSChristoph Lameter 	struct kmem_cache *cache;
1474947d3efSBenjamin LaHaise 	struct skb_shared_info *shinfo;
1481da177e4SLinus Torvalds 	struct sk_buff *skb;
1491da177e4SLinus Torvalds 	u8 *data;
1501da177e4SLinus Torvalds 
1518798b3fbSHerbert Xu 	cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
1528798b3fbSHerbert Xu 
1531da177e4SLinus Torvalds 	/* Get the HEAD */
154b30973f8SChristoph Hellwig 	skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);
1551da177e4SLinus Torvalds 	if (!skb)
1561da177e4SLinus Torvalds 		goto out;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	/* Get the DATA. Size must match skb_add_mtu(). */
1591da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
160b30973f8SChristoph Hellwig 	data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info),
161b30973f8SChristoph Hellwig 			gfp_mask, node);
1621da177e4SLinus Torvalds 	if (!data)
1631da177e4SLinus Torvalds 		goto nodata;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	memset(skb, 0, offsetof(struct sk_buff, truesize));
1661da177e4SLinus Torvalds 	skb->truesize = size + sizeof(struct sk_buff);
1671da177e4SLinus Torvalds 	atomic_set(&skb->users, 1);
1681da177e4SLinus Torvalds 	skb->head = data;
1691da177e4SLinus Torvalds 	skb->data = data;
1701da177e4SLinus Torvalds 	skb->tail = data;
1711da177e4SLinus Torvalds 	skb->end  = data + size;
1724947d3efSBenjamin LaHaise 	/* make sure we initialize shinfo sequentially */
1734947d3efSBenjamin LaHaise 	shinfo = skb_shinfo(skb);
1744947d3efSBenjamin LaHaise 	atomic_set(&shinfo->dataref, 1);
1754947d3efSBenjamin LaHaise 	shinfo->nr_frags  = 0;
1767967168cSHerbert Xu 	shinfo->gso_size = 0;
1777967168cSHerbert Xu 	shinfo->gso_segs = 0;
1787967168cSHerbert Xu 	shinfo->gso_type = 0;
1794947d3efSBenjamin LaHaise 	shinfo->ip6_frag_id = 0;
1804947d3efSBenjamin LaHaise 	shinfo->frag_list = NULL;
1814947d3efSBenjamin LaHaise 
182d179cd12SDavid S. Miller 	if (fclone) {
183d179cd12SDavid S. Miller 		struct sk_buff *child = skb + 1;
184d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (child + 1);
1851da177e4SLinus Torvalds 
186d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_ORIG;
187d179cd12SDavid S. Miller 		atomic_set(fclone_ref, 1);
188d179cd12SDavid S. Miller 
189d179cd12SDavid S. Miller 		child->fclone = SKB_FCLONE_UNAVAILABLE;
190d179cd12SDavid S. Miller 	}
1911da177e4SLinus Torvalds out:
1921da177e4SLinus Torvalds 	return skb;
1931da177e4SLinus Torvalds nodata:
1948798b3fbSHerbert Xu 	kmem_cache_free(cache, skb);
1951da177e4SLinus Torvalds 	skb = NULL;
1961da177e4SLinus Torvalds 	goto out;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /**
2008af27456SChristoph Hellwig  *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device
2018af27456SChristoph Hellwig  *	@dev: network device to receive on
2028af27456SChristoph Hellwig  *	@length: length to allocate
2038af27456SChristoph Hellwig  *	@gfp_mask: get_free_pages mask, passed to alloc_skb
2048af27456SChristoph Hellwig  *
2058af27456SChristoph Hellwig  *	Allocate a new &sk_buff and assign it a usage count of one. The
2068af27456SChristoph Hellwig  *	buffer has unspecified headroom built in. Users should allocate
2078af27456SChristoph Hellwig  *	the headroom they think they need without accounting for the
2088af27456SChristoph Hellwig  *	built in space. The built in space is used for optimisations.
2098af27456SChristoph Hellwig  *
2108af27456SChristoph Hellwig  *	%NULL is returned if there is no free memory.
2118af27456SChristoph Hellwig  */
2128af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
2138af27456SChristoph Hellwig 		unsigned int length, gfp_t gfp_mask)
2148af27456SChristoph Hellwig {
21543cb76d9SGreg Kroah-Hartman 	int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1;
2168af27456SChristoph Hellwig 	struct sk_buff *skb;
2178af27456SChristoph Hellwig 
218b30973f8SChristoph Hellwig 	skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node);
2197b2e497aSChristoph Hellwig 	if (likely(skb)) {
2208af27456SChristoph Hellwig 		skb_reserve(skb, NET_SKB_PAD);
2217b2e497aSChristoph Hellwig 		skb->dev = dev;
2227b2e497aSChristoph Hellwig 	}
2238af27456SChristoph Hellwig 	return skb;
2248af27456SChristoph Hellwig }
2251da177e4SLinus Torvalds 
22627b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp)
2271da177e4SLinus Torvalds {
22827b437c8SHerbert Xu 	struct sk_buff *list = *listp;
2291da177e4SLinus Torvalds 
23027b437c8SHerbert Xu 	*listp = NULL;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	do {
2331da177e4SLinus Torvalds 		struct sk_buff *this = list;
2341da177e4SLinus Torvalds 		list = list->next;
2351da177e4SLinus Torvalds 		kfree_skb(this);
2361da177e4SLinus Torvalds 	} while (list);
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
23927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb)
24027b437c8SHerbert Xu {
24127b437c8SHerbert Xu 	skb_drop_list(&skb_shinfo(skb)->frag_list);
24227b437c8SHerbert Xu }
24327b437c8SHerbert Xu 
2441da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct sk_buff *list;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	for (list = skb_shinfo(skb)->frag_list; list; list = list->next)
2491da177e4SLinus Torvalds 		skb_get(list);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2525bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb)
2531da177e4SLinus Torvalds {
2541da177e4SLinus Torvalds 	if (!skb->cloned ||
2551da177e4SLinus Torvalds 	    !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
2561da177e4SLinus Torvalds 			       &skb_shinfo(skb)->dataref)) {
2571da177e4SLinus Torvalds 		if (skb_shinfo(skb)->nr_frags) {
2581da177e4SLinus Torvalds 			int i;
2591da177e4SLinus Torvalds 			for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
2601da177e4SLinus Torvalds 				put_page(skb_shinfo(skb)->frags[i].page);
2611da177e4SLinus Torvalds 		}
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frag_list)
2641da177e4SLinus Torvalds 			skb_drop_fraglist(skb);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 		kfree(skb->head);
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds /*
2711da177e4SLinus Torvalds  *	Free an skbuff by memory without cleaning the state.
2721da177e4SLinus Torvalds  */
2731da177e4SLinus Torvalds void kfree_skbmem(struct sk_buff *skb)
2741da177e4SLinus Torvalds {
275d179cd12SDavid S. Miller 	struct sk_buff *other;
276d179cd12SDavid S. Miller 	atomic_t *fclone_ref;
277d179cd12SDavid S. Miller 
2781da177e4SLinus Torvalds 	skb_release_data(skb);
279d179cd12SDavid S. Miller 	switch (skb->fclone) {
280d179cd12SDavid S. Miller 	case SKB_FCLONE_UNAVAILABLE:
2811da177e4SLinus Torvalds 		kmem_cache_free(skbuff_head_cache, skb);
282d179cd12SDavid S. Miller 		break;
283d179cd12SDavid S. Miller 
284d179cd12SDavid S. Miller 	case SKB_FCLONE_ORIG:
285d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 2);
286d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
287d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, skb);
288d179cd12SDavid S. Miller 		break;
289d179cd12SDavid S. Miller 
290d179cd12SDavid S. Miller 	case SKB_FCLONE_CLONE:
291d179cd12SDavid S. Miller 		fclone_ref = (atomic_t *) (skb + 1);
292d179cd12SDavid S. Miller 		other = skb - 1;
293d179cd12SDavid S. Miller 
294d179cd12SDavid S. Miller 		/* The clone portion is available for
295d179cd12SDavid S. Miller 		 * fast-cloning again.
296d179cd12SDavid S. Miller 		 */
297d179cd12SDavid S. Miller 		skb->fclone = SKB_FCLONE_UNAVAILABLE;
298d179cd12SDavid S. Miller 
299d179cd12SDavid S. Miller 		if (atomic_dec_and_test(fclone_ref))
300d179cd12SDavid S. Miller 			kmem_cache_free(skbuff_fclone_cache, other);
301d179cd12SDavid S. Miller 		break;
302d179cd12SDavid S. Miller 	};
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /**
3061da177e4SLinus Torvalds  *	__kfree_skb - private function
3071da177e4SLinus Torvalds  *	@skb: buffer
3081da177e4SLinus Torvalds  *
3091da177e4SLinus Torvalds  *	Free an sk_buff. Release anything attached to the buffer.
3101da177e4SLinus Torvalds  *	Clean the state. This is an internal helper function. Users should
3111da177e4SLinus Torvalds  *	always call kfree_skb
3121da177e4SLinus Torvalds  */
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds void __kfree_skb(struct sk_buff *skb)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	dst_release(skb->dst);
3171da177e4SLinus Torvalds #ifdef CONFIG_XFRM
3181da177e4SLinus Torvalds 	secpath_put(skb->sp);
3191da177e4SLinus Torvalds #endif
3201da177e4SLinus Torvalds 	if (skb->destructor) {
3219c2b3328SStephen Hemminger 		WARN_ON(in_irq());
3221da177e4SLinus Torvalds 		skb->destructor(skb);
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
3251da177e4SLinus Torvalds 	nf_conntrack_put(skb->nfct);
3269fb9cbb1SYasuyuki Kozakai #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
3279fb9cbb1SYasuyuki Kozakai 	nf_conntrack_put_reasm(skb->nfct_reasm);
3289fb9cbb1SYasuyuki Kozakai #endif
3291da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
3301da177e4SLinus Torvalds 	nf_bridge_put(skb->nf_bridge);
3311da177e4SLinus Torvalds #endif
3321da177e4SLinus Torvalds #endif
3331da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */
3341da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
3351da177e4SLinus Torvalds 	skb->tc_index = 0;
3361da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
3371da177e4SLinus Torvalds 	skb->tc_verd = 0;
3381da177e4SLinus Torvalds #endif
3391da177e4SLinus Torvalds #endif
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	kfree_skbmem(skb);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds /**
345231d06aeSJörn Engel  *	kfree_skb - free an sk_buff
346231d06aeSJörn Engel  *	@skb: buffer to free
347231d06aeSJörn Engel  *
348231d06aeSJörn Engel  *	Drop a reference to the buffer and free it if the usage count has
349231d06aeSJörn Engel  *	hit zero.
350231d06aeSJörn Engel  */
351231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb)
352231d06aeSJörn Engel {
353231d06aeSJörn Engel 	if (unlikely(!skb))
354231d06aeSJörn Engel 		return;
355231d06aeSJörn Engel 	if (likely(atomic_read(&skb->users) == 1))
356231d06aeSJörn Engel 		smp_rmb();
357231d06aeSJörn Engel 	else if (likely(!atomic_dec_and_test(&skb->users)))
358231d06aeSJörn Engel 		return;
359231d06aeSJörn Engel 	__kfree_skb(skb);
360231d06aeSJörn Engel }
361231d06aeSJörn Engel 
362231d06aeSJörn Engel /**
3631da177e4SLinus Torvalds  *	skb_clone	-	duplicate an sk_buff
3641da177e4SLinus Torvalds  *	@skb: buffer to clone
3651da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
3661da177e4SLinus Torvalds  *
3671da177e4SLinus Torvalds  *	Duplicate an &sk_buff. The new one is not owned by a socket. Both
3681da177e4SLinus Torvalds  *	copies share the same packet data but not structure. The new
3691da177e4SLinus Torvalds  *	buffer has a reference count of 1. If the allocation fails the
3701da177e4SLinus Torvalds  *	function returns %NULL otherwise the new buffer is returned.
3711da177e4SLinus Torvalds  *
3721da177e4SLinus Torvalds  *	If this function is called from an interrupt gfp_mask() must be
3731da177e4SLinus Torvalds  *	%GFP_ATOMIC.
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds 
376dd0fc66fSAl Viro struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
3771da177e4SLinus Torvalds {
378d179cd12SDavid S. Miller 	struct sk_buff *n;
3791da177e4SLinus Torvalds 
380d179cd12SDavid S. Miller 	n = skb + 1;
381d179cd12SDavid S. Miller 	if (skb->fclone == SKB_FCLONE_ORIG &&
382d179cd12SDavid S. Miller 	    n->fclone == SKB_FCLONE_UNAVAILABLE) {
383d179cd12SDavid S. Miller 		atomic_t *fclone_ref = (atomic_t *) (n + 1);
384d179cd12SDavid S. Miller 		n->fclone = SKB_FCLONE_CLONE;
385d179cd12SDavid S. Miller 		atomic_inc(fclone_ref);
386d179cd12SDavid S. Miller 	} else {
387d179cd12SDavid S. Miller 		n = kmem_cache_alloc(skbuff_head_cache, gfp_mask);
3881da177e4SLinus Torvalds 		if (!n)
3891da177e4SLinus Torvalds 			return NULL;
390d179cd12SDavid S. Miller 		n->fclone = SKB_FCLONE_UNAVAILABLE;
391d179cd12SDavid S. Miller 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds #define C(x) n->x = skb->x
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	n->next = n->prev = NULL;
3961da177e4SLinus Torvalds 	n->sk = NULL;
397a61bbcf2SPatrick McHardy 	C(tstamp);
3981da177e4SLinus Torvalds 	C(dev);
3991da177e4SLinus Torvalds 	C(h);
4001da177e4SLinus Torvalds 	C(nh);
4011da177e4SLinus Torvalds 	C(mac);
4021da177e4SLinus Torvalds 	C(dst);
4031da177e4SLinus Torvalds 	dst_clone(skb->dst);
4041da177e4SLinus Torvalds 	C(sp);
4051da177e4SLinus Torvalds #ifdef CONFIG_INET
4061da177e4SLinus Torvalds 	secpath_get(skb->sp);
4071da177e4SLinus Torvalds #endif
4081da177e4SLinus Torvalds 	memcpy(n->cb, skb->cb, sizeof(skb->cb));
4091da177e4SLinus Torvalds 	C(len);
4101da177e4SLinus Torvalds 	C(data_len);
4113e6b3b2eSAlexey Dobriyan 	C(mac_len);
4121da177e4SLinus Torvalds 	C(csum);
4131da177e4SLinus Torvalds 	C(local_df);
4141da177e4SLinus Torvalds 	n->cloned = 1;
4151da177e4SLinus Torvalds 	n->nohdr = 0;
4161da177e4SLinus Torvalds 	C(pkt_type);
4171da177e4SLinus Torvalds 	C(ip_summed);
4181da177e4SLinus Torvalds 	C(priority);
419a8372f03SYOSHIFUJI Hideaki #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
420a8372f03SYOSHIFUJI Hideaki 	C(ipvs_property);
421a8372f03SYOSHIFUJI Hideaki #endif
4221da177e4SLinus Torvalds 	C(protocol);
4231da177e4SLinus Torvalds 	n->destructor = NULL;
42482e91ffeSThomas Graf 	C(mark);
425edda553cSYasuyuki Kozakai 	__nf_copy(n, skb);
4261da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4271da177e4SLinus Torvalds 	C(tc_index);
4281da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
4291da177e4SLinus Torvalds 	n->tc_verd = SET_TC_VERD(skb->tc_verd,0);
430b72f6eccSDavid S. Miller 	n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
431b72f6eccSDavid S. Miller 	n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
432c01003c2SPatrick McHardy 	C(iif);
4331da177e4SLinus Torvalds #endif
434984bc16cSJames Morris 	skb_copy_secmark(n, skb);
4351da177e4SLinus Torvalds #endif
4361da177e4SLinus Torvalds 	C(truesize);
4371da177e4SLinus Torvalds 	atomic_set(&n->users, 1);
4381da177e4SLinus Torvalds 	C(head);
4391da177e4SLinus Torvalds 	C(data);
4401da177e4SLinus Torvalds 	C(tail);
4411da177e4SLinus Torvalds 	C(end);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	atomic_inc(&(skb_shinfo(skb)->dataref));
4441da177e4SLinus Torvalds 	skb->cloned = 1;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	return n;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds 	/*
4521da177e4SLinus Torvalds 	 *	Shift between the two data areas in bytes
4531da177e4SLinus Torvalds 	 */
4541da177e4SLinus Torvalds 	unsigned long offset = new->data - old->data;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	new->sk		= NULL;
4571da177e4SLinus Torvalds 	new->dev	= old->dev;
4581da177e4SLinus Torvalds 	new->priority	= old->priority;
4591da177e4SLinus Torvalds 	new->protocol	= old->protocol;
4601da177e4SLinus Torvalds 	new->dst	= dst_clone(old->dst);
4611da177e4SLinus Torvalds #ifdef CONFIG_INET
4621da177e4SLinus Torvalds 	new->sp		= secpath_get(old->sp);
4631da177e4SLinus Torvalds #endif
4641da177e4SLinus Torvalds 	new->h.raw	= old->h.raw + offset;
4651da177e4SLinus Torvalds 	new->nh.raw	= old->nh.raw + offset;
4661da177e4SLinus Torvalds 	new->mac.raw	= old->mac.raw + offset;
4671da177e4SLinus Torvalds 	memcpy(new->cb, old->cb, sizeof(old->cb));
4681da177e4SLinus Torvalds 	new->local_df	= old->local_df;
469d179cd12SDavid S. Miller 	new->fclone	= SKB_FCLONE_UNAVAILABLE;
4701da177e4SLinus Torvalds 	new->pkt_type	= old->pkt_type;
471a61bbcf2SPatrick McHardy 	new->tstamp	= old->tstamp;
4721da177e4SLinus Torvalds 	new->destructor = NULL;
47382e91ffeSThomas Graf 	new->mark	= old->mark;
474edda553cSYasuyuki Kozakai 	__nf_copy(new, old);
475c98d80edSJulian Anastasov #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
476c98d80edSJulian Anastasov 	new->ipvs_property = old->ipvs_property;
477c98d80edSJulian Anastasov #endif
4781da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4791da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
4801da177e4SLinus Torvalds 	new->tc_verd = old->tc_verd;
4811da177e4SLinus Torvalds #endif
4821da177e4SLinus Torvalds 	new->tc_index	= old->tc_index;
4831da177e4SLinus Torvalds #endif
484984bc16cSJames Morris 	skb_copy_secmark(new, old);
4851da177e4SLinus Torvalds 	atomic_set(&new->users, 1);
4867967168cSHerbert Xu 	skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
4877967168cSHerbert Xu 	skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
4887967168cSHerbert Xu 	skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /**
4921da177e4SLinus Torvalds  *	skb_copy	-	create private copy of an sk_buff
4931da177e4SLinus Torvalds  *	@skb: buffer to copy
4941da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
4951da177e4SLinus Torvalds  *
4961da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data. This is used when the
4971da177e4SLinus Torvalds  *	caller wishes to modify the data and needs a private copy of the
4981da177e4SLinus Torvalds  *	data to alter. Returns %NULL on failure or the pointer to the buffer
4991da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
5001da177e4SLinus Torvalds  *
5011da177e4SLinus Torvalds  *	As by-product this function converts non-linear &sk_buff to linear
5021da177e4SLinus Torvalds  *	one, so that &sk_buff becomes completely private and caller is allowed
5031da177e4SLinus Torvalds  *	to modify all the data of returned buffer. This means that this
5041da177e4SLinus Torvalds  *	function is not recommended for use in circumstances when only
5051da177e4SLinus Torvalds  *	header is going to be modified. Use pskb_copy() instead.
5061da177e4SLinus Torvalds  */
5071da177e4SLinus Torvalds 
508dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	int headerlen = skb->data - skb->head;
5111da177e4SLinus Torvalds 	/*
5121da177e4SLinus Torvalds 	 *	Allocate the copy buffer
5131da177e4SLinus Torvalds 	 */
5141da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len,
5151da177e4SLinus Torvalds 				      gfp_mask);
5161da177e4SLinus Torvalds 	if (!n)
5171da177e4SLinus Torvalds 		return NULL;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/* Set the data pointer */
5201da177e4SLinus Torvalds 	skb_reserve(n, headerlen);
5211da177e4SLinus Torvalds 	/* Set the tail pointer and length */
5221da177e4SLinus Torvalds 	skb_put(n, skb->len);
5231da177e4SLinus Torvalds 	n->csum	     = skb->csum;
5241da177e4SLinus Torvalds 	n->ip_summed = skb->ip_summed;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len))
5271da177e4SLinus Torvalds 		BUG();
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	copy_skb_header(n, skb);
5301da177e4SLinus Torvalds 	return n;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds /**
5351da177e4SLinus Torvalds  *	pskb_copy	-	create copy of an sk_buff with private head.
5361da177e4SLinus Torvalds  *	@skb: buffer to copy
5371da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
5381da177e4SLinus Torvalds  *
5391da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and part of its data, located
5401da177e4SLinus Torvalds  *	in header. Fragmented data remain shared. This is used when
5411da177e4SLinus Torvalds  *	the caller wishes to modify only header of &sk_buff and needs
5421da177e4SLinus Torvalds  *	private copy of the header to alter. Returns %NULL on failure
5431da177e4SLinus Torvalds  *	or the pointer to the buffer on success.
5441da177e4SLinus Torvalds  *	The returned buffer has a reference count of 1.
5451da177e4SLinus Torvalds  */
5461da177e4SLinus Torvalds 
547dd0fc66fSAl Viro struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	/*
5501da177e4SLinus Torvalds 	 *	Allocate the copy buffer
5511da177e4SLinus Torvalds 	 */
5521da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask);
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	if (!n)
5551da177e4SLinus Torvalds 		goto out;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	/* Set the data pointer */
5581da177e4SLinus Torvalds 	skb_reserve(n, skb->data - skb->head);
5591da177e4SLinus Torvalds 	/* Set the tail pointer and length */
5601da177e4SLinus Torvalds 	skb_put(n, skb_headlen(skb));
5611da177e4SLinus Torvalds 	/* Copy the bytes */
5621da177e4SLinus Torvalds 	memcpy(n->data, skb->data, n->len);
5631da177e4SLinus Torvalds 	n->csum	     = skb->csum;
5641da177e4SLinus Torvalds 	n->ip_summed = skb->ip_summed;
5651da177e4SLinus Torvalds 
56625f484a6SHerbert Xu 	n->truesize += skb->data_len;
5671da177e4SLinus Torvalds 	n->data_len  = skb->data_len;
5681da177e4SLinus Torvalds 	n->len	     = skb->len;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	if (skb_shinfo(skb)->nr_frags) {
5711da177e4SLinus Torvalds 		int i;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
5741da177e4SLinus Torvalds 			skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i];
5751da177e4SLinus Torvalds 			get_page(skb_shinfo(n)->frags[i].page);
5761da177e4SLinus Torvalds 		}
5771da177e4SLinus Torvalds 		skb_shinfo(n)->nr_frags = i;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
5811da177e4SLinus Torvalds 		skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list;
5821da177e4SLinus Torvalds 		skb_clone_fraglist(n);
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	copy_skb_header(n, skb);
5861da177e4SLinus Torvalds out:
5871da177e4SLinus Torvalds 	return n;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds /**
5911da177e4SLinus Torvalds  *	pskb_expand_head - reallocate header of &sk_buff
5921da177e4SLinus Torvalds  *	@skb: buffer to reallocate
5931da177e4SLinus Torvalds  *	@nhead: room to add at head
5941da177e4SLinus Torvalds  *	@ntail: room to add at tail
5951da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
5961da177e4SLinus Torvalds  *
5971da177e4SLinus Torvalds  *	Expands (or creates identical copy, if &nhead and &ntail are zero)
5981da177e4SLinus Torvalds  *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have
5991da177e4SLinus Torvalds  *	reference count of 1. Returns zero in the case of success or error,
6001da177e4SLinus Torvalds  *	if expansion failed. In the last case, &sk_buff is not changed.
6011da177e4SLinus Torvalds  *
6021da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
6031da177e4SLinus Torvalds  *	reloaded after call to this function.
6041da177e4SLinus Torvalds  */
6051da177e4SLinus Torvalds 
60686a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
607dd0fc66fSAl Viro 		     gfp_t gfp_mask)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	int i;
6101da177e4SLinus Torvalds 	u8 *data;
6111da177e4SLinus Torvalds 	int size = nhead + (skb->end - skb->head) + ntail;
6121da177e4SLinus Torvalds 	long off;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	if (skb_shared(skb))
6151da177e4SLinus Torvalds 		BUG();
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	size = SKB_DATA_ALIGN(size);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
6201da177e4SLinus Torvalds 	if (!data)
6211da177e4SLinus Torvalds 		goto nodata;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	/* Copy only real data... and, alas, header. This should be
6241da177e4SLinus Torvalds 	 * optimized for the cases when header is void. */
6251da177e4SLinus Torvalds 	memcpy(data + nhead, skb->head, skb->tail - skb->head);
6261da177e4SLinus Torvalds 	memcpy(data + size, skb->end, sizeof(struct skb_shared_info));
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
6291da177e4SLinus Torvalds 		get_page(skb_shinfo(skb)->frags[i].page);
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list)
6321da177e4SLinus Torvalds 		skb_clone_fraglist(skb);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	skb_release_data(skb);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	off = (data + nhead) - skb->head;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	skb->head     = data;
6391da177e4SLinus Torvalds 	skb->end      = data + size;
6401da177e4SLinus Torvalds 	skb->data    += off;
6411da177e4SLinus Torvalds 	skb->tail    += off;
6421da177e4SLinus Torvalds 	skb->mac.raw += off;
6431da177e4SLinus Torvalds 	skb->h.raw   += off;
6441da177e4SLinus Torvalds 	skb->nh.raw  += off;
6451da177e4SLinus Torvalds 	skb->cloned   = 0;
6461da177e4SLinus Torvalds 	skb->nohdr    = 0;
6471da177e4SLinus Torvalds 	atomic_set(&skb_shinfo(skb)->dataref, 1);
6481da177e4SLinus Torvalds 	return 0;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds nodata:
6511da177e4SLinus Torvalds 	return -ENOMEM;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	struct sk_buff *skb2;
6591da177e4SLinus Torvalds 	int delta = headroom - skb_headroom(skb);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	if (delta <= 0)
6621da177e4SLinus Torvalds 		skb2 = pskb_copy(skb, GFP_ATOMIC);
6631da177e4SLinus Torvalds 	else {
6641da177e4SLinus Torvalds 		skb2 = skb_clone(skb, GFP_ATOMIC);
6651da177e4SLinus Torvalds 		if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0,
6661da177e4SLinus Torvalds 					     GFP_ATOMIC)) {
6671da177e4SLinus Torvalds 			kfree_skb(skb2);
6681da177e4SLinus Torvalds 			skb2 = NULL;
6691da177e4SLinus Torvalds 		}
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 	return skb2;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds /**
6761da177e4SLinus Torvalds  *	skb_copy_expand	-	copy and expand sk_buff
6771da177e4SLinus Torvalds  *	@skb: buffer to copy
6781da177e4SLinus Torvalds  *	@newheadroom: new free bytes at head
6791da177e4SLinus Torvalds  *	@newtailroom: new free bytes at tail
6801da177e4SLinus Torvalds  *	@gfp_mask: allocation priority
6811da177e4SLinus Torvalds  *
6821da177e4SLinus Torvalds  *	Make a copy of both an &sk_buff and its data and while doing so
6831da177e4SLinus Torvalds  *	allocate additional space.
6841da177e4SLinus Torvalds  *
6851da177e4SLinus Torvalds  *	This is used when the caller wishes to modify the data and needs a
6861da177e4SLinus Torvalds  *	private copy of the data to alter as well as more space for new fields.
6871da177e4SLinus Torvalds  *	Returns %NULL on failure or the pointer to the buffer
6881da177e4SLinus Torvalds  *	on success. The returned buffer has a reference count of 1.
6891da177e4SLinus Torvalds  *
6901da177e4SLinus Torvalds  *	You must pass %GFP_ATOMIC as the allocation priority if this function
6911da177e4SLinus Torvalds  *	is called from an interrupt.
6921da177e4SLinus Torvalds  *
6931da177e4SLinus Torvalds  *	BUG ALERT: ip_summed is not copied. Why does this work? Is it used
6941da177e4SLinus Torvalds  *	only by netfilter in the cases when checksum is recalculated? --ANK
6951da177e4SLinus Torvalds  */
6961da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
69786a76cafSVictor Fusco 				int newheadroom, int newtailroom,
698dd0fc66fSAl Viro 				gfp_t gfp_mask)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	/*
7011da177e4SLinus Torvalds 	 *	Allocate the copy buffer
7021da177e4SLinus Torvalds 	 */
7031da177e4SLinus Torvalds 	struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom,
7041da177e4SLinus Torvalds 				      gfp_mask);
7051da177e4SLinus Torvalds 	int head_copy_len, head_copy_off;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	if (!n)
7081da177e4SLinus Torvalds 		return NULL;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	skb_reserve(n, newheadroom);
7111da177e4SLinus Torvalds 
7121da177e4SLinus Torvalds 	/* Set the tail pointer and length */
7131da177e4SLinus Torvalds 	skb_put(n, skb->len);
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	head_copy_len = skb_headroom(skb);
7161da177e4SLinus Torvalds 	head_copy_off = 0;
7171da177e4SLinus Torvalds 	if (newheadroom <= head_copy_len)
7181da177e4SLinus Torvalds 		head_copy_len = newheadroom;
7191da177e4SLinus Torvalds 	else
7201da177e4SLinus Torvalds 		head_copy_off = newheadroom - head_copy_len;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 	/* Copy the linear header and data. */
7231da177e4SLinus Torvalds 	if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off,
7241da177e4SLinus Torvalds 			  skb->len + head_copy_len))
7251da177e4SLinus Torvalds 		BUG();
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	copy_skb_header(n, skb);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	return n;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds /**
7331da177e4SLinus Torvalds  *	skb_pad			-	zero pad the tail of an skb
7341da177e4SLinus Torvalds  *	@skb: buffer to pad
7351da177e4SLinus Torvalds  *	@pad: space to pad
7361da177e4SLinus Torvalds  *
7371da177e4SLinus Torvalds  *	Ensure that a buffer is followed by a padding area that is zero
7381da177e4SLinus Torvalds  *	filled. Used by network drivers which may DMA or transfer data
7391da177e4SLinus Torvalds  *	beyond the buffer end onto the wire.
7401da177e4SLinus Torvalds  *
7415b057c6bSHerbert Xu  *	May return error in out of memory cases. The skb is freed on error.
7421da177e4SLinus Torvalds  */
7431da177e4SLinus Torvalds 
7445b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad)
7451da177e4SLinus Torvalds {
7465b057c6bSHerbert Xu 	int err;
7475b057c6bSHerbert Xu 	int ntail;
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	/* If the skbuff is non linear tailroom is always zero.. */
7505b057c6bSHerbert Xu 	if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) {
7511da177e4SLinus Torvalds 		memset(skb->data+skb->len, 0, pad);
7525b057c6bSHerbert Xu 		return 0;
7531da177e4SLinus Torvalds 	}
7541da177e4SLinus Torvalds 
7555b057c6bSHerbert Xu 	ntail = skb->data_len + pad - (skb->end - skb->tail);
7565b057c6bSHerbert Xu 	if (likely(skb_cloned(skb) || ntail > 0)) {
7575b057c6bSHerbert Xu 		err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC);
7585b057c6bSHerbert Xu 		if (unlikely(err))
7595b057c6bSHerbert Xu 			goto free_skb;
7605b057c6bSHerbert Xu 	}
7615b057c6bSHerbert Xu 
7625b057c6bSHerbert Xu 	/* FIXME: The use of this function with non-linear skb's really needs
7635b057c6bSHerbert Xu 	 * to be audited.
7645b057c6bSHerbert Xu 	 */
7655b057c6bSHerbert Xu 	err = skb_linearize(skb);
7665b057c6bSHerbert Xu 	if (unlikely(err))
7675b057c6bSHerbert Xu 		goto free_skb;
7685b057c6bSHerbert Xu 
7695b057c6bSHerbert Xu 	memset(skb->data + skb->len, 0, pad);
7705b057c6bSHerbert Xu 	return 0;
7715b057c6bSHerbert Xu 
7725b057c6bSHerbert Xu free_skb:
7731da177e4SLinus Torvalds 	kfree_skb(skb);
7745b057c6bSHerbert Xu 	return err;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7773cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers.
7781da177e4SLinus Torvalds  */
7791da177e4SLinus Torvalds 
7803cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len)
7811da177e4SLinus Torvalds {
78227b437c8SHerbert Xu 	struct sk_buff **fragp;
78327b437c8SHerbert Xu 	struct sk_buff *frag;
7841da177e4SLinus Torvalds 	int offset = skb_headlen(skb);
7851da177e4SLinus Torvalds 	int nfrags = skb_shinfo(skb)->nr_frags;
7861da177e4SLinus Torvalds 	int i;
78727b437c8SHerbert Xu 	int err;
78827b437c8SHerbert Xu 
78927b437c8SHerbert Xu 	if (skb_cloned(skb) &&
79027b437c8SHerbert Xu 	    unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))))
79127b437c8SHerbert Xu 		return err;
7921da177e4SLinus Torvalds 
793f4d26fb3SHerbert Xu 	i = 0;
794f4d26fb3SHerbert Xu 	if (offset >= len)
795f4d26fb3SHerbert Xu 		goto drop_pages;
796f4d26fb3SHerbert Xu 
797f4d26fb3SHerbert Xu 	for (; i < nfrags; i++) {
7981da177e4SLinus Torvalds 		int end = offset + skb_shinfo(skb)->frags[i].size;
79927b437c8SHerbert Xu 
80027b437c8SHerbert Xu 		if (end < len) {
8011da177e4SLinus Torvalds 			offset = end;
80227b437c8SHerbert Xu 			continue;
8031da177e4SLinus Torvalds 		}
8041da177e4SLinus Torvalds 
80527b437c8SHerbert Xu 		skb_shinfo(skb)->frags[i++].size = len - offset;
80627b437c8SHerbert Xu 
807f4d26fb3SHerbert Xu drop_pages:
80827b437c8SHerbert Xu 		skb_shinfo(skb)->nr_frags = i;
80927b437c8SHerbert Xu 
81027b437c8SHerbert Xu 		for (; i < nfrags; i++)
81127b437c8SHerbert Xu 			put_page(skb_shinfo(skb)->frags[i].page);
81227b437c8SHerbert Xu 
81327b437c8SHerbert Xu 		if (skb_shinfo(skb)->frag_list)
81427b437c8SHerbert Xu 			skb_drop_fraglist(skb);
815f4d26fb3SHerbert Xu 		goto done;
81627b437c8SHerbert Xu 	}
81727b437c8SHerbert Xu 
81827b437c8SHerbert Xu 	for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp);
81927b437c8SHerbert Xu 	     fragp = &frag->next) {
82027b437c8SHerbert Xu 		int end = offset + frag->len;
82127b437c8SHerbert Xu 
82227b437c8SHerbert Xu 		if (skb_shared(frag)) {
82327b437c8SHerbert Xu 			struct sk_buff *nfrag;
82427b437c8SHerbert Xu 
82527b437c8SHerbert Xu 			nfrag = skb_clone(frag, GFP_ATOMIC);
82627b437c8SHerbert Xu 			if (unlikely(!nfrag))
82727b437c8SHerbert Xu 				return -ENOMEM;
82827b437c8SHerbert Xu 
82927b437c8SHerbert Xu 			nfrag->next = frag->next;
830f4d26fb3SHerbert Xu 			kfree_skb(frag);
83127b437c8SHerbert Xu 			frag = nfrag;
83227b437c8SHerbert Xu 			*fragp = frag;
83327b437c8SHerbert Xu 		}
83427b437c8SHerbert Xu 
83527b437c8SHerbert Xu 		if (end < len) {
83627b437c8SHerbert Xu 			offset = end;
83727b437c8SHerbert Xu 			continue;
83827b437c8SHerbert Xu 		}
83927b437c8SHerbert Xu 
84027b437c8SHerbert Xu 		if (end > len &&
84127b437c8SHerbert Xu 		    unlikely((err = pskb_trim(frag, len - offset))))
84227b437c8SHerbert Xu 			return err;
84327b437c8SHerbert Xu 
84427b437c8SHerbert Xu 		if (frag->next)
84527b437c8SHerbert Xu 			skb_drop_list(&frag->next);
84627b437c8SHerbert Xu 		break;
84727b437c8SHerbert Xu 	}
84827b437c8SHerbert Xu 
849f4d26fb3SHerbert Xu done:
85027b437c8SHerbert Xu 	if (len > skb_headlen(skb)) {
8511da177e4SLinus Torvalds 		skb->data_len -= skb->len - len;
8521da177e4SLinus Torvalds 		skb->len       = len;
8531da177e4SLinus Torvalds 	} else {
8541da177e4SLinus Torvalds 		skb->len       = len;
8551da177e4SLinus Torvalds 		skb->data_len  = 0;
8561da177e4SLinus Torvalds 		skb->tail      = skb->data + len;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 	return 0;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds /**
8631da177e4SLinus Torvalds  *	__pskb_pull_tail - advance tail of skb header
8641da177e4SLinus Torvalds  *	@skb: buffer to reallocate
8651da177e4SLinus Torvalds  *	@delta: number of bytes to advance tail
8661da177e4SLinus Torvalds  *
8671da177e4SLinus Torvalds  *	The function makes a sense only on a fragmented &sk_buff,
8681da177e4SLinus Torvalds  *	it expands header moving its tail forward and copying necessary
8691da177e4SLinus Torvalds  *	data from fragmented part.
8701da177e4SLinus Torvalds  *
8711da177e4SLinus Torvalds  *	&sk_buff MUST have reference count of 1.
8721da177e4SLinus Torvalds  *
8731da177e4SLinus Torvalds  *	Returns %NULL (and &sk_buff does not change) if pull failed
8741da177e4SLinus Torvalds  *	or value of new tail of skb in the case of success.
8751da177e4SLinus Torvalds  *
8761da177e4SLinus Torvalds  *	All the pointers pointing into skb header may change and must be
8771da177e4SLinus Torvalds  *	reloaded after call to this function.
8781da177e4SLinus Torvalds  */
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part,
8811da177e4SLinus Torvalds  * when it is necessary.
8821da177e4SLinus Torvalds  * 1. It may fail due to malloc failure.
8831da177e4SLinus Torvalds  * 2. It may change skb pointers.
8841da177e4SLinus Torvalds  *
8851da177e4SLinus Torvalds  * It is pretty complicated. Luckily, it is called only in exceptional cases.
8861da177e4SLinus Torvalds  */
8871da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	/* If skb has not enough free space at tail, get new one
8901da177e4SLinus Torvalds 	 * plus 128 bytes for future expansions. If we have enough
8911da177e4SLinus Torvalds 	 * room at tail, reallocate without expansion only if skb is cloned.
8921da177e4SLinus Torvalds 	 */
8931da177e4SLinus Torvalds 	int i, k, eat = (skb->tail + delta) - skb->end;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	if (eat > 0 || skb_cloned(skb)) {
8961da177e4SLinus Torvalds 		if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0,
8971da177e4SLinus Torvalds 				     GFP_ATOMIC))
8981da177e4SLinus Torvalds 			return NULL;
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta))
9021da177e4SLinus Torvalds 		BUG();
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	/* Optimization: no fragments, no reasons to preestimate
9051da177e4SLinus Torvalds 	 * size of pulled pages. Superb.
9061da177e4SLinus Torvalds 	 */
9071da177e4SLinus Torvalds 	if (!skb_shinfo(skb)->frag_list)
9081da177e4SLinus Torvalds 		goto pull_pages;
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	/* Estimate size of pulled pages. */
9111da177e4SLinus Torvalds 	eat = delta;
9121da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
9131da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size >= eat)
9141da177e4SLinus Torvalds 			goto pull_pages;
9151da177e4SLinus Torvalds 		eat -= skb_shinfo(skb)->frags[i].size;
9161da177e4SLinus Torvalds 	}
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	/* If we need update frag list, we are in troubles.
9191da177e4SLinus Torvalds 	 * Certainly, it possible to add an offset to skb data,
9201da177e4SLinus Torvalds 	 * but taking into account that pulling is expected to
9211da177e4SLinus Torvalds 	 * be very rare operation, it is worth to fight against
9221da177e4SLinus Torvalds 	 * further bloating skb head and crucify ourselves here instead.
9231da177e4SLinus Torvalds 	 * Pure masohism, indeed. 8)8)
9241da177e4SLinus Torvalds 	 */
9251da177e4SLinus Torvalds 	if (eat) {
9261da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
9271da177e4SLinus Torvalds 		struct sk_buff *clone = NULL;
9281da177e4SLinus Torvalds 		struct sk_buff *insp = NULL;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 		do {
93109a62660SKris Katterjohn 			BUG_ON(!list);
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 			if (list->len <= eat) {
9341da177e4SLinus Torvalds 				/* Eaten as whole. */
9351da177e4SLinus Torvalds 				eat -= list->len;
9361da177e4SLinus Torvalds 				list = list->next;
9371da177e4SLinus Torvalds 				insp = list;
9381da177e4SLinus Torvalds 			} else {
9391da177e4SLinus Torvalds 				/* Eaten partially. */
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 				if (skb_shared(list)) {
9421da177e4SLinus Torvalds 					/* Sucks! We need to fork list. :-( */
9431da177e4SLinus Torvalds 					clone = skb_clone(list, GFP_ATOMIC);
9441da177e4SLinus Torvalds 					if (!clone)
9451da177e4SLinus Torvalds 						return NULL;
9461da177e4SLinus Torvalds 					insp = list->next;
9471da177e4SLinus Torvalds 					list = clone;
9481da177e4SLinus Torvalds 				} else {
9491da177e4SLinus Torvalds 					/* This may be pulled without
9501da177e4SLinus Torvalds 					 * problems. */
9511da177e4SLinus Torvalds 					insp = list;
9521da177e4SLinus Torvalds 				}
9531da177e4SLinus Torvalds 				if (!pskb_pull(list, eat)) {
9541da177e4SLinus Torvalds 					if (clone)
9551da177e4SLinus Torvalds 						kfree_skb(clone);
9561da177e4SLinus Torvalds 					return NULL;
9571da177e4SLinus Torvalds 				}
9581da177e4SLinus Torvalds 				break;
9591da177e4SLinus Torvalds 			}
9601da177e4SLinus Torvalds 		} while (eat);
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 		/* Free pulled out fragments. */
9631da177e4SLinus Torvalds 		while ((list = skb_shinfo(skb)->frag_list) != insp) {
9641da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = list->next;
9651da177e4SLinus Torvalds 			kfree_skb(list);
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds 		/* And insert new clone at head. */
9681da177e4SLinus Torvalds 		if (clone) {
9691da177e4SLinus Torvalds 			clone->next = list;
9701da177e4SLinus Torvalds 			skb_shinfo(skb)->frag_list = clone;
9711da177e4SLinus Torvalds 		}
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	/* Success! Now we may commit changes to skb data. */
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds pull_pages:
9761da177e4SLinus Torvalds 	eat = delta;
9771da177e4SLinus Torvalds 	k = 0;
9781da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
9791da177e4SLinus Torvalds 		if (skb_shinfo(skb)->frags[i].size <= eat) {
9801da177e4SLinus Torvalds 			put_page(skb_shinfo(skb)->frags[i].page);
9811da177e4SLinus Torvalds 			eat -= skb_shinfo(skb)->frags[i].size;
9821da177e4SLinus Torvalds 		} else {
9831da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i];
9841da177e4SLinus Torvalds 			if (eat) {
9851da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].page_offset += eat;
9861da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[k].size -= eat;
9871da177e4SLinus Torvalds 				eat = 0;
9881da177e4SLinus Torvalds 			}
9891da177e4SLinus Torvalds 			k++;
9901da177e4SLinus Torvalds 		}
9911da177e4SLinus Torvalds 	}
9921da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = k;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	skb->tail     += delta;
9951da177e4SLinus Torvalds 	skb->data_len -= delta;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	return skb->tail;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds /* Copy some data bits from skb to kernel buffer. */
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len)
10031da177e4SLinus Torvalds {
10041da177e4SLinus Torvalds 	int i, copy;
10051da177e4SLinus Torvalds 	int start = skb_headlen(skb);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	if (offset > (int)skb->len - len)
10081da177e4SLinus Torvalds 		goto fault;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	/* Copy header. */
10111da177e4SLinus Torvalds 	if ((copy = start - offset) > 0) {
10121da177e4SLinus Torvalds 		if (copy > len)
10131da177e4SLinus Torvalds 			copy = len;
10141da177e4SLinus Torvalds 		memcpy(to, skb->data + offset, copy);
10151da177e4SLinus Torvalds 		if ((len -= copy) == 0)
10161da177e4SLinus Torvalds 			return 0;
10171da177e4SLinus Torvalds 		offset += copy;
10181da177e4SLinus Torvalds 		to     += copy;
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
10221da177e4SLinus Torvalds 		int end;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 		BUG_TRAP(start <= offset + len);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 		end = start + skb_shinfo(skb)->frags[i].size;
10271da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
10281da177e4SLinus Torvalds 			u8 *vaddr;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 			if (copy > len)
10311da177e4SLinus Torvalds 				copy = len;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]);
10341da177e4SLinus Torvalds 			memcpy(to,
10351da177e4SLinus Torvalds 			       vaddr + skb_shinfo(skb)->frags[i].page_offset+
10361da177e4SLinus Torvalds 			       offset - start, copy);
10371da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds 			if ((len -= copy) == 0)
10401da177e4SLinus Torvalds 				return 0;
10411da177e4SLinus Torvalds 			offset += copy;
10421da177e4SLinus Torvalds 			to     += copy;
10431da177e4SLinus Torvalds 		}
10441da177e4SLinus Torvalds 		start = end;
10451da177e4SLinus Torvalds 	}
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
10481da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 		for (; list; list = list->next) {
10511da177e4SLinus Torvalds 			int end;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 			BUG_TRAP(start <= offset + len);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 			end = start + list->len;
10561da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
10571da177e4SLinus Torvalds 				if (copy > len)
10581da177e4SLinus Torvalds 					copy = len;
10591da177e4SLinus Torvalds 				if (skb_copy_bits(list, offset - start,
10601da177e4SLinus Torvalds 						  to, copy))
10611da177e4SLinus Torvalds 					goto fault;
10621da177e4SLinus Torvalds 				if ((len -= copy) == 0)
10631da177e4SLinus Torvalds 					return 0;
10641da177e4SLinus Torvalds 				offset += copy;
10651da177e4SLinus Torvalds 				to     += copy;
10661da177e4SLinus Torvalds 			}
10671da177e4SLinus Torvalds 			start = end;
10681da177e4SLinus Torvalds 		}
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 	if (!len)
10711da177e4SLinus Torvalds 		return 0;
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds fault:
10741da177e4SLinus Torvalds 	return -EFAULT;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
1077357b40a1SHerbert Xu /**
1078357b40a1SHerbert Xu  *	skb_store_bits - store bits from kernel buffer to skb
1079357b40a1SHerbert Xu  *	@skb: destination buffer
1080357b40a1SHerbert Xu  *	@offset: offset in destination
1081357b40a1SHerbert Xu  *	@from: source buffer
1082357b40a1SHerbert Xu  *	@len: number of bytes to copy
1083357b40a1SHerbert Xu  *
1084357b40a1SHerbert Xu  *	Copy the specified number of bytes from the source buffer to the
1085357b40a1SHerbert Xu  *	destination skb.  This function handles all the messy bits of
1086357b40a1SHerbert Xu  *	traversing fragment lists and such.
1087357b40a1SHerbert Xu  */
1088357b40a1SHerbert Xu 
1089357b40a1SHerbert Xu int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len)
1090357b40a1SHerbert Xu {
1091357b40a1SHerbert Xu 	int i, copy;
1092357b40a1SHerbert Xu 	int start = skb_headlen(skb);
1093357b40a1SHerbert Xu 
1094357b40a1SHerbert Xu 	if (offset > (int)skb->len - len)
1095357b40a1SHerbert Xu 		goto fault;
1096357b40a1SHerbert Xu 
1097357b40a1SHerbert Xu 	if ((copy = start - offset) > 0) {
1098357b40a1SHerbert Xu 		if (copy > len)
1099357b40a1SHerbert Xu 			copy = len;
1100357b40a1SHerbert Xu 		memcpy(skb->data + offset, from, copy);
1101357b40a1SHerbert Xu 		if ((len -= copy) == 0)
1102357b40a1SHerbert Xu 			return 0;
1103357b40a1SHerbert Xu 		offset += copy;
1104357b40a1SHerbert Xu 		from += copy;
1105357b40a1SHerbert Xu 	}
1106357b40a1SHerbert Xu 
1107357b40a1SHerbert Xu 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1108357b40a1SHerbert Xu 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1109357b40a1SHerbert Xu 		int end;
1110357b40a1SHerbert Xu 
1111357b40a1SHerbert Xu 		BUG_TRAP(start <= offset + len);
1112357b40a1SHerbert Xu 
1113357b40a1SHerbert Xu 		end = start + frag->size;
1114357b40a1SHerbert Xu 		if ((copy = end - offset) > 0) {
1115357b40a1SHerbert Xu 			u8 *vaddr;
1116357b40a1SHerbert Xu 
1117357b40a1SHerbert Xu 			if (copy > len)
1118357b40a1SHerbert Xu 				copy = len;
1119357b40a1SHerbert Xu 
1120357b40a1SHerbert Xu 			vaddr = kmap_skb_frag(frag);
1121357b40a1SHerbert Xu 			memcpy(vaddr + frag->page_offset + offset - start,
1122357b40a1SHerbert Xu 			       from, copy);
1123357b40a1SHerbert Xu 			kunmap_skb_frag(vaddr);
1124357b40a1SHerbert Xu 
1125357b40a1SHerbert Xu 			if ((len -= copy) == 0)
1126357b40a1SHerbert Xu 				return 0;
1127357b40a1SHerbert Xu 			offset += copy;
1128357b40a1SHerbert Xu 			from += copy;
1129357b40a1SHerbert Xu 		}
1130357b40a1SHerbert Xu 		start = end;
1131357b40a1SHerbert Xu 	}
1132357b40a1SHerbert Xu 
1133357b40a1SHerbert Xu 	if (skb_shinfo(skb)->frag_list) {
1134357b40a1SHerbert Xu 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
1135357b40a1SHerbert Xu 
1136357b40a1SHerbert Xu 		for (; list; list = list->next) {
1137357b40a1SHerbert Xu 			int end;
1138357b40a1SHerbert Xu 
1139357b40a1SHerbert Xu 			BUG_TRAP(start <= offset + len);
1140357b40a1SHerbert Xu 
1141357b40a1SHerbert Xu 			end = start + list->len;
1142357b40a1SHerbert Xu 			if ((copy = end - offset) > 0) {
1143357b40a1SHerbert Xu 				if (copy > len)
1144357b40a1SHerbert Xu 					copy = len;
1145357b40a1SHerbert Xu 				if (skb_store_bits(list, offset - start,
1146357b40a1SHerbert Xu 						   from, copy))
1147357b40a1SHerbert Xu 					goto fault;
1148357b40a1SHerbert Xu 				if ((len -= copy) == 0)
1149357b40a1SHerbert Xu 					return 0;
1150357b40a1SHerbert Xu 				offset += copy;
1151357b40a1SHerbert Xu 				from += copy;
1152357b40a1SHerbert Xu 			}
1153357b40a1SHerbert Xu 			start = end;
1154357b40a1SHerbert Xu 		}
1155357b40a1SHerbert Xu 	}
1156357b40a1SHerbert Xu 	if (!len)
1157357b40a1SHerbert Xu 		return 0;
1158357b40a1SHerbert Xu 
1159357b40a1SHerbert Xu fault:
1160357b40a1SHerbert Xu 	return -EFAULT;
1161357b40a1SHerbert Xu }
1162357b40a1SHerbert Xu 
1163357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits);
1164357b40a1SHerbert Xu 
11651da177e4SLinus Torvalds /* Checksum skb data. */
11661da177e4SLinus Torvalds 
11672bbbc868SAl Viro __wsum skb_checksum(const struct sk_buff *skb, int offset,
11682bbbc868SAl Viro 			  int len, __wsum csum)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	int start = skb_headlen(skb);
11711da177e4SLinus Torvalds 	int i, copy = start - offset;
11721da177e4SLinus Torvalds 	int pos = 0;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	/* Checksum header. */
11751da177e4SLinus Torvalds 	if (copy > 0) {
11761da177e4SLinus Torvalds 		if (copy > len)
11771da177e4SLinus Torvalds 			copy = len;
11781da177e4SLinus Torvalds 		csum = csum_partial(skb->data + offset, copy, csum);
11791da177e4SLinus Torvalds 		if ((len -= copy) == 0)
11801da177e4SLinus Torvalds 			return csum;
11811da177e4SLinus Torvalds 		offset += copy;
11821da177e4SLinus Torvalds 		pos	= copy;
11831da177e4SLinus Torvalds 	}
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
11861da177e4SLinus Torvalds 		int end;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 		BUG_TRAP(start <= offset + len);
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 		end = start + skb_shinfo(skb)->frags[i].size;
11911da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
119244bb9363SAl Viro 			__wsum csum2;
11931da177e4SLinus Torvalds 			u8 *vaddr;
11941da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 			if (copy > len)
11971da177e4SLinus Torvalds 				copy = len;
11981da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
11991da177e4SLinus Torvalds 			csum2 = csum_partial(vaddr + frag->page_offset +
12001da177e4SLinus Torvalds 					     offset - start, copy, 0);
12011da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
12021da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
12031da177e4SLinus Torvalds 			if (!(len -= copy))
12041da177e4SLinus Torvalds 				return csum;
12051da177e4SLinus Torvalds 			offset += copy;
12061da177e4SLinus Torvalds 			pos    += copy;
12071da177e4SLinus Torvalds 		}
12081da177e4SLinus Torvalds 		start = end;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
12121da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 		for (; list; list = list->next) {
12151da177e4SLinus Torvalds 			int end;
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 			BUG_TRAP(start <= offset + len);
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 			end = start + list->len;
12201da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
12215f92a738SAl Viro 				__wsum csum2;
12221da177e4SLinus Torvalds 				if (copy > len)
12231da177e4SLinus Torvalds 					copy = len;
12241da177e4SLinus Torvalds 				csum2 = skb_checksum(list, offset - start,
12251da177e4SLinus Torvalds 						     copy, 0);
12261da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
12271da177e4SLinus Torvalds 				if ((len -= copy) == 0)
12281da177e4SLinus Torvalds 					return csum;
12291da177e4SLinus Torvalds 				offset += copy;
12301da177e4SLinus Torvalds 				pos    += copy;
12311da177e4SLinus Torvalds 			}
12321da177e4SLinus Torvalds 			start = end;
12331da177e4SLinus Torvalds 		}
12341da177e4SLinus Torvalds 	}
123509a62660SKris Katterjohn 	BUG_ON(len);
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	return csum;
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds /* Both of above in one bottle. */
12411da177e4SLinus Torvalds 
124281d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset,
124381d77662SAl Viro 				    u8 *to, int len, __wsum csum)
12441da177e4SLinus Torvalds {
12451da177e4SLinus Torvalds 	int start = skb_headlen(skb);
12461da177e4SLinus Torvalds 	int i, copy = start - offset;
12471da177e4SLinus Torvalds 	int pos = 0;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	/* Copy header. */
12501da177e4SLinus Torvalds 	if (copy > 0) {
12511da177e4SLinus Torvalds 		if (copy > len)
12521da177e4SLinus Torvalds 			copy = len;
12531da177e4SLinus Torvalds 		csum = csum_partial_copy_nocheck(skb->data + offset, to,
12541da177e4SLinus Torvalds 						 copy, csum);
12551da177e4SLinus Torvalds 		if ((len -= copy) == 0)
12561da177e4SLinus Torvalds 			return csum;
12571da177e4SLinus Torvalds 		offset += copy;
12581da177e4SLinus Torvalds 		to     += copy;
12591da177e4SLinus Torvalds 		pos	= copy;
12601da177e4SLinus Torvalds 	}
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
12631da177e4SLinus Torvalds 		int end;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 		BUG_TRAP(start <= offset + len);
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds 		end = start + skb_shinfo(skb)->frags[i].size;
12681da177e4SLinus Torvalds 		if ((copy = end - offset) > 0) {
12695084205fSAl Viro 			__wsum csum2;
12701da177e4SLinus Torvalds 			u8 *vaddr;
12711da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 			if (copy > len)
12741da177e4SLinus Torvalds 				copy = len;
12751da177e4SLinus Torvalds 			vaddr = kmap_skb_frag(frag);
12761da177e4SLinus Torvalds 			csum2 = csum_partial_copy_nocheck(vaddr +
12771da177e4SLinus Torvalds 							  frag->page_offset +
12781da177e4SLinus Torvalds 							  offset - start, to,
12791da177e4SLinus Torvalds 							  copy, 0);
12801da177e4SLinus Torvalds 			kunmap_skb_frag(vaddr);
12811da177e4SLinus Torvalds 			csum = csum_block_add(csum, csum2, pos);
12821da177e4SLinus Torvalds 			if (!(len -= copy))
12831da177e4SLinus Torvalds 				return csum;
12841da177e4SLinus Torvalds 			offset += copy;
12851da177e4SLinus Torvalds 			to     += copy;
12861da177e4SLinus Torvalds 			pos    += copy;
12871da177e4SLinus Torvalds 		}
12881da177e4SLinus Torvalds 		start = end;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
12921da177e4SLinus Torvalds 		struct sk_buff *list = skb_shinfo(skb)->frag_list;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 		for (; list; list = list->next) {
129581d77662SAl Viro 			__wsum csum2;
12961da177e4SLinus Torvalds 			int end;
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 			BUG_TRAP(start <= offset + len);
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 			end = start + list->len;
13011da177e4SLinus Torvalds 			if ((copy = end - offset) > 0) {
13021da177e4SLinus Torvalds 				if (copy > len)
13031da177e4SLinus Torvalds 					copy = len;
13041da177e4SLinus Torvalds 				csum2 = skb_copy_and_csum_bits(list,
13051da177e4SLinus Torvalds 							       offset - start,
13061da177e4SLinus Torvalds 							       to, copy, 0);
13071da177e4SLinus Torvalds 				csum = csum_block_add(csum, csum2, pos);
13081da177e4SLinus Torvalds 				if ((len -= copy) == 0)
13091da177e4SLinus Torvalds 					return csum;
13101da177e4SLinus Torvalds 				offset += copy;
13111da177e4SLinus Torvalds 				to     += copy;
13121da177e4SLinus Torvalds 				pos    += copy;
13131da177e4SLinus Torvalds 			}
13141da177e4SLinus Torvalds 			start = end;
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds 	}
131709a62660SKris Katterjohn 	BUG_ON(len);
13181da177e4SLinus Torvalds 	return csum;
13191da177e4SLinus Torvalds }
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to)
13221da177e4SLinus Torvalds {
1323d3bc23e7SAl Viro 	__wsum csum;
13241da177e4SLinus Torvalds 	long csstart;
13251da177e4SLinus Torvalds 
132684fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1327ea2ae17dSArnaldo Carvalho de Melo 		csstart = skb_transport_offset(skb);
13281da177e4SLinus Torvalds 	else
13291da177e4SLinus Torvalds 		csstart = skb_headlen(skb);
13301da177e4SLinus Torvalds 
133109a62660SKris Katterjohn 	BUG_ON(csstart > skb_headlen(skb));
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	memcpy(to, skb->data, csstart);
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	csum = 0;
13361da177e4SLinus Torvalds 	if (csstart != skb->len)
13371da177e4SLinus Torvalds 		csum = skb_copy_and_csum_bits(skb, csstart, to + csstart,
13381da177e4SLinus Torvalds 					      skb->len - csstart, 0);
13391da177e4SLinus Torvalds 
134084fa7933SPatrick McHardy 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1341ff1dcadbSAl Viro 		long csstuff = csstart + skb->csum_offset;
13421da177e4SLinus Torvalds 
1343d3bc23e7SAl Viro 		*((__sum16 *)(to + csstuff)) = csum_fold(csum);
13441da177e4SLinus Torvalds 	}
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds /**
13481da177e4SLinus Torvalds  *	skb_dequeue - remove from the head of the queue
13491da177e4SLinus Torvalds  *	@list: list to dequeue from
13501da177e4SLinus Torvalds  *
13511da177e4SLinus Torvalds  *	Remove the head of the list. The list lock is taken so the function
13521da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The head item is
13531da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
13541da177e4SLinus Torvalds  */
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	unsigned long flags;
13591da177e4SLinus Torvalds 	struct sk_buff *result;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
13621da177e4SLinus Torvalds 	result = __skb_dequeue(list);
13631da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
13641da177e4SLinus Torvalds 	return result;
13651da177e4SLinus Torvalds }
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds /**
13681da177e4SLinus Torvalds  *	skb_dequeue_tail - remove from the tail of the queue
13691da177e4SLinus Torvalds  *	@list: list to dequeue from
13701da177e4SLinus Torvalds  *
13711da177e4SLinus Torvalds  *	Remove the tail of the list. The list lock is taken so the function
13721da177e4SLinus Torvalds  *	may be used safely with other locking list functions. The tail item is
13731da177e4SLinus Torvalds  *	returned or %NULL if the list is empty.
13741da177e4SLinus Torvalds  */
13751da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list)
13761da177e4SLinus Torvalds {
13771da177e4SLinus Torvalds 	unsigned long flags;
13781da177e4SLinus Torvalds 	struct sk_buff *result;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
13811da177e4SLinus Torvalds 	result = __skb_dequeue_tail(list);
13821da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
13831da177e4SLinus Torvalds 	return result;
13841da177e4SLinus Torvalds }
13851da177e4SLinus Torvalds 
13861da177e4SLinus Torvalds /**
13871da177e4SLinus Torvalds  *	skb_queue_purge - empty a list
13881da177e4SLinus Torvalds  *	@list: list to empty
13891da177e4SLinus Torvalds  *
13901da177e4SLinus Torvalds  *	Delete all buffers on an &sk_buff list. Each buffer is removed from
13911da177e4SLinus Torvalds  *	the list and one reference dropped. This function takes the list
13921da177e4SLinus Torvalds  *	lock and is atomic with respect to other list locking functions.
13931da177e4SLinus Torvalds  */
13941da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	struct sk_buff *skb;
13971da177e4SLinus Torvalds 	while ((skb = skb_dequeue(list)) != NULL)
13981da177e4SLinus Torvalds 		kfree_skb(skb);
13991da177e4SLinus Torvalds }
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds /**
14021da177e4SLinus Torvalds  *	skb_queue_head - queue a buffer at the list head
14031da177e4SLinus Torvalds  *	@list: list to use
14041da177e4SLinus Torvalds  *	@newsk: buffer to queue
14051da177e4SLinus Torvalds  *
14061da177e4SLinus Torvalds  *	Queue a buffer at the start of the list. This function takes the
14071da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
14081da177e4SLinus Torvalds  *	safely.
14091da177e4SLinus Torvalds  *
14101da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
14111da177e4SLinus Torvalds  */
14121da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)
14131da177e4SLinus Torvalds {
14141da177e4SLinus Torvalds 	unsigned long flags;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14171da177e4SLinus Torvalds 	__skb_queue_head(list, newsk);
14181da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
14191da177e4SLinus Torvalds }
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds /**
14221da177e4SLinus Torvalds  *	skb_queue_tail - queue a buffer at the list tail
14231da177e4SLinus Torvalds  *	@list: list to use
14241da177e4SLinus Torvalds  *	@newsk: buffer to queue
14251da177e4SLinus Torvalds  *
14261da177e4SLinus Torvalds  *	Queue a buffer at the tail of the list. This function takes the
14271da177e4SLinus Torvalds  *	list lock and can be used safely with other locking &sk_buff functions
14281da177e4SLinus Torvalds  *	safely.
14291da177e4SLinus Torvalds  *
14301da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
14311da177e4SLinus Torvalds  */
14321da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk)
14331da177e4SLinus Torvalds {
14341da177e4SLinus Torvalds 	unsigned long flags;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14371da177e4SLinus Torvalds 	__skb_queue_tail(list, newsk);
14381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
14391da177e4SLinus Torvalds }
14408728b834SDavid S. Miller 
14411da177e4SLinus Torvalds /**
14421da177e4SLinus Torvalds  *	skb_unlink	-	remove a buffer from a list
14431da177e4SLinus Torvalds  *	@skb: buffer to remove
14448728b834SDavid S. Miller  *	@list: list to use
14451da177e4SLinus Torvalds  *
14468728b834SDavid S. Miller  *	Remove a packet from a list. The list locks are taken and this
14478728b834SDavid S. Miller  *	function is atomic with respect to other list locked calls
14481da177e4SLinus Torvalds  *
14498728b834SDavid S. Miller  *	You must know what list the SKB is on.
14501da177e4SLinus Torvalds  */
14518728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
14521da177e4SLinus Torvalds {
14531da177e4SLinus Torvalds 	unsigned long flags;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	spin_lock_irqsave(&list->lock, flags);
14568728b834SDavid S. Miller 	__skb_unlink(skb, list);
14571da177e4SLinus Torvalds 	spin_unlock_irqrestore(&list->lock, flags);
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds /**
14611da177e4SLinus Torvalds  *	skb_append	-	append a buffer
14621da177e4SLinus Torvalds  *	@old: buffer to insert after
14631da177e4SLinus Torvalds  *	@newsk: buffer to insert
14648728b834SDavid S. Miller  *	@list: list to use
14651da177e4SLinus Torvalds  *
14661da177e4SLinus Torvalds  *	Place a packet after a given packet in a list. The list locks are taken
14671da177e4SLinus Torvalds  *	and this function is atomic with respect to other list locked calls.
14681da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
14691da177e4SLinus Torvalds  */
14708728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
14711da177e4SLinus Torvalds {
14721da177e4SLinus Torvalds 	unsigned long flags;
14731da177e4SLinus Torvalds 
14748728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
14758728b834SDavid S. Miller 	__skb_append(old, newsk, list);
14768728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
14791da177e4SLinus Torvalds 
14801da177e4SLinus Torvalds /**
14811da177e4SLinus Torvalds  *	skb_insert	-	insert a buffer
14821da177e4SLinus Torvalds  *	@old: buffer to insert before
14831da177e4SLinus Torvalds  *	@newsk: buffer to insert
14848728b834SDavid S. Miller  *	@list: list to use
14851da177e4SLinus Torvalds  *
14868728b834SDavid S. Miller  *	Place a packet before a given packet in a list. The list locks are
14878728b834SDavid S. Miller  * 	taken and this function is atomic with respect to other list locked
14888728b834SDavid S. Miller  *	calls.
14898728b834SDavid S. Miller  *
14901da177e4SLinus Torvalds  *	A buffer cannot be placed on two lists at the same time.
14911da177e4SLinus Torvalds  */
14928728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds 	unsigned long flags;
14951da177e4SLinus Torvalds 
14968728b834SDavid S. Miller 	spin_lock_irqsave(&list->lock, flags);
14978728b834SDavid S. Miller 	__skb_insert(newsk, old->prev, old, list);
14988728b834SDavid S. Miller 	spin_unlock_irqrestore(&list->lock, flags);
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds #if 0
15021da177e4SLinus Torvalds /*
15031da177e4SLinus Torvalds  * 	Tune the memory allocator for a new MTU size.
15041da177e4SLinus Torvalds  */
15051da177e4SLinus Torvalds void skb_add_mtu(int mtu)
15061da177e4SLinus Torvalds {
15071da177e4SLinus Torvalds 	/* Must match allocation in alloc_skb */
15081da177e4SLinus Torvalds 	mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info);
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	kmem_add_cache_size(mtu);
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds #endif
15131da177e4SLinus Torvalds 
15141da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb,
15151da177e4SLinus Torvalds 					   struct sk_buff* skb1,
15161da177e4SLinus Torvalds 					   const u32 len, const int pos)
15171da177e4SLinus Torvalds {
15181da177e4SLinus Torvalds 	int i;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len);
15211da177e4SLinus Torvalds 
15221da177e4SLinus Torvalds 	/* And move data appendix as is. */
15231da177e4SLinus Torvalds 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
15241da177e4SLinus Torvalds 		skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i];
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags;
15271da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags  = 0;
15281da177e4SLinus Torvalds 	skb1->data_len		   = skb->data_len;
15291da177e4SLinus Torvalds 	skb1->len		   += skb1->data_len;
15301da177e4SLinus Torvalds 	skb->data_len		   = 0;
15311da177e4SLinus Torvalds 	skb->len		   = len;
15321da177e4SLinus Torvalds 	skb->tail		   = skb->data + len;
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb,
15361da177e4SLinus Torvalds 				       struct sk_buff* skb1,
15371da177e4SLinus Torvalds 				       const u32 len, int pos)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	int i, k = 0;
15401da177e4SLinus Torvalds 	const int nfrags = skb_shinfo(skb)->nr_frags;
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds 	skb_shinfo(skb)->nr_frags = 0;
15431da177e4SLinus Torvalds 	skb1->len		  = skb1->data_len = skb->len - len;
15441da177e4SLinus Torvalds 	skb->len		  = len;
15451da177e4SLinus Torvalds 	skb->data_len		  = len - pos;
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	for (i = 0; i < nfrags; i++) {
15481da177e4SLinus Torvalds 		int size = skb_shinfo(skb)->frags[i].size;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 		if (pos + size > len) {
15511da177e4SLinus Torvalds 			skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i];
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 			if (pos < len) {
15541da177e4SLinus Torvalds 				/* Split frag.
15551da177e4SLinus Torvalds 				 * We have two variants in this case:
15561da177e4SLinus Torvalds 				 * 1. Move all the frag to the second
15571da177e4SLinus Torvalds 				 *    part, if it is possible. F.e.
15581da177e4SLinus Torvalds 				 *    this approach is mandatory for TUX,
15591da177e4SLinus Torvalds 				 *    where splitting is expensive.
15601da177e4SLinus Torvalds 				 * 2. Split is accurately. We make this.
15611da177e4SLinus Torvalds 				 */
15621da177e4SLinus Torvalds 				get_page(skb_shinfo(skb)->frags[i].page);
15631da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].page_offset += len - pos;
15641da177e4SLinus Torvalds 				skb_shinfo(skb1)->frags[0].size -= len - pos;
15651da177e4SLinus Torvalds 				skb_shinfo(skb)->frags[i].size	= len - pos;
15661da177e4SLinus Torvalds 				skb_shinfo(skb)->nr_frags++;
15671da177e4SLinus Torvalds 			}
15681da177e4SLinus Torvalds 			k++;
15691da177e4SLinus Torvalds 		} else
15701da177e4SLinus Torvalds 			skb_shinfo(skb)->nr_frags++;
15711da177e4SLinus Torvalds 		pos += size;
15721da177e4SLinus Torvalds 	}
15731da177e4SLinus Torvalds 	skb_shinfo(skb1)->nr_frags = k;
15741da177e4SLinus Torvalds }
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds /**
15771da177e4SLinus Torvalds  * skb_split - Split fragmented skb to two parts at length len.
15781da177e4SLinus Torvalds  * @skb: the buffer to split
15791da177e4SLinus Torvalds  * @skb1: the buffer to receive the second part
15801da177e4SLinus Torvalds  * @len: new length for skb
15811da177e4SLinus Torvalds  */
15821da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len)
15831da177e4SLinus Torvalds {
15841da177e4SLinus Torvalds 	int pos = skb_headlen(skb);
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	if (len < pos)	/* Split line is inside header. */
15871da177e4SLinus Torvalds 		skb_split_inside_header(skb, skb1, len, pos);
15881da177e4SLinus Torvalds 	else		/* Second chunk has no header, nothing to copy. */
15891da177e4SLinus Torvalds 		skb_split_no_header(skb, skb1, len, pos);
15901da177e4SLinus Torvalds }
15911da177e4SLinus Torvalds 
1592677e90edSThomas Graf /**
1593677e90edSThomas Graf  * skb_prepare_seq_read - Prepare a sequential read of skb data
1594677e90edSThomas Graf  * @skb: the buffer to read
1595677e90edSThomas Graf  * @from: lower offset of data to be read
1596677e90edSThomas Graf  * @to: upper offset of data to be read
1597677e90edSThomas Graf  * @st: state variable
1598677e90edSThomas Graf  *
1599677e90edSThomas Graf  * Initializes the specified state variable. Must be called before
1600677e90edSThomas Graf  * invoking skb_seq_read() for the first time.
1601677e90edSThomas Graf  */
1602677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
1603677e90edSThomas Graf 			  unsigned int to, struct skb_seq_state *st)
1604677e90edSThomas Graf {
1605677e90edSThomas Graf 	st->lower_offset = from;
1606677e90edSThomas Graf 	st->upper_offset = to;
1607677e90edSThomas Graf 	st->root_skb = st->cur_skb = skb;
1608677e90edSThomas Graf 	st->frag_idx = st->stepped_offset = 0;
1609677e90edSThomas Graf 	st->frag_data = NULL;
1610677e90edSThomas Graf }
1611677e90edSThomas Graf 
1612677e90edSThomas Graf /**
1613677e90edSThomas Graf  * skb_seq_read - Sequentially read skb data
1614677e90edSThomas Graf  * @consumed: number of bytes consumed by the caller so far
1615677e90edSThomas Graf  * @data: destination pointer for data to be returned
1616677e90edSThomas Graf  * @st: state variable
1617677e90edSThomas Graf  *
1618677e90edSThomas Graf  * Reads a block of skb data at &consumed relative to the
1619677e90edSThomas Graf  * lower offset specified to skb_prepare_seq_read(). Assigns
1620677e90edSThomas Graf  * the head of the data block to &data and returns the length
1621677e90edSThomas Graf  * of the block or 0 if the end of the skb data or the upper
1622677e90edSThomas Graf  * offset has been reached.
1623677e90edSThomas Graf  *
1624677e90edSThomas Graf  * The caller is not required to consume all of the data
1625677e90edSThomas Graf  * returned, i.e. &consumed is typically set to the number
1626677e90edSThomas Graf  * of bytes already consumed and the next call to
1627677e90edSThomas Graf  * skb_seq_read() will return the remaining part of the block.
1628677e90edSThomas Graf  *
1629677e90edSThomas Graf  * Note: The size of each block of data returned can be arbitary,
1630677e90edSThomas Graf  *       this limitation is the cost for zerocopy seqeuental
1631677e90edSThomas Graf  *       reads of potentially non linear data.
1632677e90edSThomas Graf  *
1633677e90edSThomas Graf  * Note: Fragment lists within fragments are not implemented
1634677e90edSThomas Graf  *       at the moment, state->root_skb could be replaced with
1635677e90edSThomas Graf  *       a stack for this purpose.
1636677e90edSThomas Graf  */
1637677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
1638677e90edSThomas Graf 			  struct skb_seq_state *st)
1639677e90edSThomas Graf {
1640677e90edSThomas Graf 	unsigned int block_limit, abs_offset = consumed + st->lower_offset;
1641677e90edSThomas Graf 	skb_frag_t *frag;
1642677e90edSThomas Graf 
1643677e90edSThomas Graf 	if (unlikely(abs_offset >= st->upper_offset))
1644677e90edSThomas Graf 		return 0;
1645677e90edSThomas Graf 
1646677e90edSThomas Graf next_skb:
1647677e90edSThomas Graf 	block_limit = skb_headlen(st->cur_skb);
1648677e90edSThomas Graf 
1649677e90edSThomas Graf 	if (abs_offset < block_limit) {
1650677e90edSThomas Graf 		*data = st->cur_skb->data + abs_offset;
1651677e90edSThomas Graf 		return block_limit - abs_offset;
1652677e90edSThomas Graf 	}
1653677e90edSThomas Graf 
1654677e90edSThomas Graf 	if (st->frag_idx == 0 && !st->frag_data)
1655677e90edSThomas Graf 		st->stepped_offset += skb_headlen(st->cur_skb);
1656677e90edSThomas Graf 
1657677e90edSThomas Graf 	while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) {
1658677e90edSThomas Graf 		frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx];
1659677e90edSThomas Graf 		block_limit = frag->size + st->stepped_offset;
1660677e90edSThomas Graf 
1661677e90edSThomas Graf 		if (abs_offset < block_limit) {
1662677e90edSThomas Graf 			if (!st->frag_data)
1663677e90edSThomas Graf 				st->frag_data = kmap_skb_frag(frag);
1664677e90edSThomas Graf 
1665677e90edSThomas Graf 			*data = (u8 *) st->frag_data + frag->page_offset +
1666677e90edSThomas Graf 				(abs_offset - st->stepped_offset);
1667677e90edSThomas Graf 
1668677e90edSThomas Graf 			return block_limit - abs_offset;
1669677e90edSThomas Graf 		}
1670677e90edSThomas Graf 
1671677e90edSThomas Graf 		if (st->frag_data) {
1672677e90edSThomas Graf 			kunmap_skb_frag(st->frag_data);
1673677e90edSThomas Graf 			st->frag_data = NULL;
1674677e90edSThomas Graf 		}
1675677e90edSThomas Graf 
1676677e90edSThomas Graf 		st->frag_idx++;
1677677e90edSThomas Graf 		st->stepped_offset += frag->size;
1678677e90edSThomas Graf 	}
1679677e90edSThomas Graf 
1680677e90edSThomas Graf 	if (st->cur_skb->next) {
1681677e90edSThomas Graf 		st->cur_skb = st->cur_skb->next;
1682677e90edSThomas Graf 		st->frag_idx = 0;
1683677e90edSThomas Graf 		goto next_skb;
1684677e90edSThomas Graf 	} else if (st->root_skb == st->cur_skb &&
1685677e90edSThomas Graf 		   skb_shinfo(st->root_skb)->frag_list) {
1686677e90edSThomas Graf 		st->cur_skb = skb_shinfo(st->root_skb)->frag_list;
1687677e90edSThomas Graf 		goto next_skb;
1688677e90edSThomas Graf 	}
1689677e90edSThomas Graf 
1690677e90edSThomas Graf 	return 0;
1691677e90edSThomas Graf }
1692677e90edSThomas Graf 
1693677e90edSThomas Graf /**
1694677e90edSThomas Graf  * skb_abort_seq_read - Abort a sequential read of skb data
1695677e90edSThomas Graf  * @st: state variable
1696677e90edSThomas Graf  *
1697677e90edSThomas Graf  * Must be called if skb_seq_read() was not called until it
1698677e90edSThomas Graf  * returned 0.
1699677e90edSThomas Graf  */
1700677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st)
1701677e90edSThomas Graf {
1702677e90edSThomas Graf 	if (st->frag_data)
1703677e90edSThomas Graf 		kunmap_skb_frag(st->frag_data);
1704677e90edSThomas Graf }
1705677e90edSThomas Graf 
17063fc7e8a6SThomas Graf #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb))
17073fc7e8a6SThomas Graf 
17083fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text,
17093fc7e8a6SThomas Graf 					  struct ts_config *conf,
17103fc7e8a6SThomas Graf 					  struct ts_state *state)
17113fc7e8a6SThomas Graf {
17123fc7e8a6SThomas Graf 	return skb_seq_read(offset, text, TS_SKB_CB(state));
17133fc7e8a6SThomas Graf }
17143fc7e8a6SThomas Graf 
17153fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
17163fc7e8a6SThomas Graf {
17173fc7e8a6SThomas Graf 	skb_abort_seq_read(TS_SKB_CB(state));
17183fc7e8a6SThomas Graf }
17193fc7e8a6SThomas Graf 
17203fc7e8a6SThomas Graf /**
17213fc7e8a6SThomas Graf  * skb_find_text - Find a text pattern in skb data
17223fc7e8a6SThomas Graf  * @skb: the buffer to look in
17233fc7e8a6SThomas Graf  * @from: search offset
17243fc7e8a6SThomas Graf  * @to: search limit
17253fc7e8a6SThomas Graf  * @config: textsearch configuration
17263fc7e8a6SThomas Graf  * @state: uninitialized textsearch state variable
17273fc7e8a6SThomas Graf  *
17283fc7e8a6SThomas Graf  * Finds a pattern in the skb data according to the specified
17293fc7e8a6SThomas Graf  * textsearch configuration. Use textsearch_next() to retrieve
17303fc7e8a6SThomas Graf  * subsequent occurrences of the pattern. Returns the offset
17313fc7e8a6SThomas Graf  * to the first occurrence or UINT_MAX if no match was found.
17323fc7e8a6SThomas Graf  */
17333fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
17343fc7e8a6SThomas Graf 			   unsigned int to, struct ts_config *config,
17353fc7e8a6SThomas Graf 			   struct ts_state *state)
17363fc7e8a6SThomas Graf {
1737f72b948dSPhil Oester 	unsigned int ret;
1738f72b948dSPhil Oester 
17393fc7e8a6SThomas Graf 	config->get_next_block = skb_ts_get_next_block;
17403fc7e8a6SThomas Graf 	config->finish = skb_ts_finish;
17413fc7e8a6SThomas Graf 
17423fc7e8a6SThomas Graf 	skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state));
17433fc7e8a6SThomas Graf 
1744f72b948dSPhil Oester 	ret = textsearch_find(config, state);
1745f72b948dSPhil Oester 	return (ret <= to - from ? ret : UINT_MAX);
17463fc7e8a6SThomas Graf }
17473fc7e8a6SThomas Graf 
1748e89e9cf5SAnanda Raju /**
1749e89e9cf5SAnanda Raju  * skb_append_datato_frags: - append the user data to a skb
1750e89e9cf5SAnanda Raju  * @sk: sock  structure
1751e89e9cf5SAnanda Raju  * @skb: skb structure to be appened with user data.
1752e89e9cf5SAnanda Raju  * @getfrag: call back function to be used for getting the user data
1753e89e9cf5SAnanda Raju  * @from: pointer to user message iov
1754e89e9cf5SAnanda Raju  * @length: length of the iov message
1755e89e9cf5SAnanda Raju  *
1756e89e9cf5SAnanda Raju  * Description: This procedure append the user data in the fragment part
1757e89e9cf5SAnanda Raju  * of the skb if any page alloc fails user this procedure returns  -ENOMEM
1758e89e9cf5SAnanda Raju  */
1759e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
1760dab9630fSMartin Waitz 			int (*getfrag)(void *from, char *to, int offset,
1761e89e9cf5SAnanda Raju 					int len, int odd, struct sk_buff *skb),
1762e89e9cf5SAnanda Raju 			void *from, int length)
1763e89e9cf5SAnanda Raju {
1764e89e9cf5SAnanda Raju 	int frg_cnt = 0;
1765e89e9cf5SAnanda Raju 	skb_frag_t *frag = NULL;
1766e89e9cf5SAnanda Raju 	struct page *page = NULL;
1767e89e9cf5SAnanda Raju 	int copy, left;
1768e89e9cf5SAnanda Raju 	int offset = 0;
1769e89e9cf5SAnanda Raju 	int ret;
1770e89e9cf5SAnanda Raju 
1771e89e9cf5SAnanda Raju 	do {
1772e89e9cf5SAnanda Raju 		/* Return error if we don't have space for new frag */
1773e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
1774e89e9cf5SAnanda Raju 		if (frg_cnt >= MAX_SKB_FRAGS)
1775e89e9cf5SAnanda Raju 			return -EFAULT;
1776e89e9cf5SAnanda Raju 
1777e89e9cf5SAnanda Raju 		/* allocate a new page for next frag */
1778e89e9cf5SAnanda Raju 		page = alloc_pages(sk->sk_allocation, 0);
1779e89e9cf5SAnanda Raju 
1780e89e9cf5SAnanda Raju 		/* If alloc_page fails just return failure and caller will
1781e89e9cf5SAnanda Raju 		 * free previous allocated pages by doing kfree_skb()
1782e89e9cf5SAnanda Raju 		 */
1783e89e9cf5SAnanda Raju 		if (page == NULL)
1784e89e9cf5SAnanda Raju 			return -ENOMEM;
1785e89e9cf5SAnanda Raju 
1786e89e9cf5SAnanda Raju 		/* initialize the next frag */
1787e89e9cf5SAnanda Raju 		sk->sk_sndmsg_page = page;
1788e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off = 0;
1789e89e9cf5SAnanda Raju 		skb_fill_page_desc(skb, frg_cnt, page, 0, 0);
1790e89e9cf5SAnanda Raju 		skb->truesize += PAGE_SIZE;
1791e89e9cf5SAnanda Raju 		atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
1792e89e9cf5SAnanda Raju 
1793e89e9cf5SAnanda Raju 		/* get the new initialized frag */
1794e89e9cf5SAnanda Raju 		frg_cnt = skb_shinfo(skb)->nr_frags;
1795e89e9cf5SAnanda Raju 		frag = &skb_shinfo(skb)->frags[frg_cnt - 1];
1796e89e9cf5SAnanda Raju 
1797e89e9cf5SAnanda Raju 		/* copy the user data to page */
1798e89e9cf5SAnanda Raju 		left = PAGE_SIZE - frag->page_offset;
1799e89e9cf5SAnanda Raju 		copy = (length > left)? left : length;
1800e89e9cf5SAnanda Raju 
1801e89e9cf5SAnanda Raju 		ret = getfrag(from, (page_address(frag->page) +
1802e89e9cf5SAnanda Raju 			    frag->page_offset + frag->size),
1803e89e9cf5SAnanda Raju 			    offset, copy, 0, skb);
1804e89e9cf5SAnanda Raju 		if (ret < 0)
1805e89e9cf5SAnanda Raju 			return -EFAULT;
1806e89e9cf5SAnanda Raju 
1807e89e9cf5SAnanda Raju 		/* copy was successful so update the size parameters */
1808e89e9cf5SAnanda Raju 		sk->sk_sndmsg_off += copy;
1809e89e9cf5SAnanda Raju 		frag->size += copy;
1810e89e9cf5SAnanda Raju 		skb->len += copy;
1811e89e9cf5SAnanda Raju 		skb->data_len += copy;
1812e89e9cf5SAnanda Raju 		offset += copy;
1813e89e9cf5SAnanda Raju 		length -= copy;
1814e89e9cf5SAnanda Raju 
1815e89e9cf5SAnanda Raju 	} while (length > 0);
1816e89e9cf5SAnanda Raju 
1817e89e9cf5SAnanda Raju 	return 0;
1818e89e9cf5SAnanda Raju }
1819e89e9cf5SAnanda Raju 
1820cbb042f9SHerbert Xu /**
1821cbb042f9SHerbert Xu  *	skb_pull_rcsum - pull skb and update receive checksum
1822cbb042f9SHerbert Xu  *	@skb: buffer to update
1823cbb042f9SHerbert Xu  *	@start: start of data before pull
1824cbb042f9SHerbert Xu  *	@len: length of data pulled
1825cbb042f9SHerbert Xu  *
1826cbb042f9SHerbert Xu  *	This function performs an skb_pull on the packet and updates
182784fa7933SPatrick McHardy  *	update the CHECKSUM_COMPLETE checksum.  It should be used on
182884fa7933SPatrick McHardy  *	receive path processing instead of skb_pull unless you know
182984fa7933SPatrick McHardy  *	that the checksum difference is zero (e.g., a valid IP header)
183084fa7933SPatrick McHardy  *	or you are setting ip_summed to CHECKSUM_NONE.
1831cbb042f9SHerbert Xu  */
1832cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len)
1833cbb042f9SHerbert Xu {
1834cbb042f9SHerbert Xu 	BUG_ON(len > skb->len);
1835cbb042f9SHerbert Xu 	skb->len -= len;
1836cbb042f9SHerbert Xu 	BUG_ON(skb->len < skb->data_len);
1837cbb042f9SHerbert Xu 	skb_postpull_rcsum(skb, skb->data, len);
1838cbb042f9SHerbert Xu 	return skb->data += len;
1839cbb042f9SHerbert Xu }
1840cbb042f9SHerbert Xu 
1841f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum);
1842f94691acSArnaldo Carvalho de Melo 
1843f4c50d99SHerbert Xu /**
1844f4c50d99SHerbert Xu  *	skb_segment - Perform protocol segmentation on skb.
1845f4c50d99SHerbert Xu  *	@skb: buffer to segment
1846576a30ebSHerbert Xu  *	@features: features for the output path (see dev->features)
1847f4c50d99SHerbert Xu  *
1848f4c50d99SHerbert Xu  *	This function performs segmentation on the given skb.  It returns
1849f4c50d99SHerbert Xu  *	the segment at the given position.  It returns NULL if there are
1850f4c50d99SHerbert Xu  *	no more segments to generate, or when an error is encountered.
1851f4c50d99SHerbert Xu  */
1852576a30ebSHerbert Xu struct sk_buff *skb_segment(struct sk_buff *skb, int features)
1853f4c50d99SHerbert Xu {
1854f4c50d99SHerbert Xu 	struct sk_buff *segs = NULL;
1855f4c50d99SHerbert Xu 	struct sk_buff *tail = NULL;
1856f4c50d99SHerbert Xu 	unsigned int mss = skb_shinfo(skb)->gso_size;
185798e399f8SArnaldo Carvalho de Melo 	unsigned int doffset = skb->data - skb_mac_header(skb);
1858f4c50d99SHerbert Xu 	unsigned int offset = doffset;
1859f4c50d99SHerbert Xu 	unsigned int headroom;
1860f4c50d99SHerbert Xu 	unsigned int len;
1861576a30ebSHerbert Xu 	int sg = features & NETIF_F_SG;
1862f4c50d99SHerbert Xu 	int nfrags = skb_shinfo(skb)->nr_frags;
1863f4c50d99SHerbert Xu 	int err = -ENOMEM;
1864f4c50d99SHerbert Xu 	int i = 0;
1865f4c50d99SHerbert Xu 	int pos;
1866f4c50d99SHerbert Xu 
1867f4c50d99SHerbert Xu 	__skb_push(skb, doffset);
1868f4c50d99SHerbert Xu 	headroom = skb_headroom(skb);
1869f4c50d99SHerbert Xu 	pos = skb_headlen(skb);
1870f4c50d99SHerbert Xu 
1871f4c50d99SHerbert Xu 	do {
1872f4c50d99SHerbert Xu 		struct sk_buff *nskb;
1873f4c50d99SHerbert Xu 		skb_frag_t *frag;
1874c8884eddSHerbert Xu 		int hsize;
1875f4c50d99SHerbert Xu 		int k;
1876f4c50d99SHerbert Xu 		int size;
1877f4c50d99SHerbert Xu 
1878f4c50d99SHerbert Xu 		len = skb->len - offset;
1879f4c50d99SHerbert Xu 		if (len > mss)
1880f4c50d99SHerbert Xu 			len = mss;
1881f4c50d99SHerbert Xu 
1882f4c50d99SHerbert Xu 		hsize = skb_headlen(skb) - offset;
1883f4c50d99SHerbert Xu 		if (hsize < 0)
1884f4c50d99SHerbert Xu 			hsize = 0;
1885c8884eddSHerbert Xu 		if (hsize > len || !sg)
1886c8884eddSHerbert Xu 			hsize = len;
1887f4c50d99SHerbert Xu 
1888c8884eddSHerbert Xu 		nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC);
1889f4c50d99SHerbert Xu 		if (unlikely(!nskb))
1890f4c50d99SHerbert Xu 			goto err;
1891f4c50d99SHerbert Xu 
1892f4c50d99SHerbert Xu 		if (segs)
1893f4c50d99SHerbert Xu 			tail->next = nskb;
1894f4c50d99SHerbert Xu 		else
1895f4c50d99SHerbert Xu 			segs = nskb;
1896f4c50d99SHerbert Xu 		tail = nskb;
1897f4c50d99SHerbert Xu 
1898f4c50d99SHerbert Xu 		nskb->dev = skb->dev;
1899f4c50d99SHerbert Xu 		nskb->priority = skb->priority;
1900f4c50d99SHerbert Xu 		nskb->protocol = skb->protocol;
1901f4c50d99SHerbert Xu 		nskb->dst = dst_clone(skb->dst);
1902f4c50d99SHerbert Xu 		memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
1903f4c50d99SHerbert Xu 		nskb->pkt_type = skb->pkt_type;
1904f4c50d99SHerbert Xu 		nskb->mac_len = skb->mac_len;
1905f4c50d99SHerbert Xu 
1906f4c50d99SHerbert Xu 		skb_reserve(nskb, headroom);
1907459a98edSArnaldo Carvalho de Melo 		skb_reset_mac_header(nskb);
1908f4c50d99SHerbert Xu 		nskb->nh.raw = nskb->data + skb->mac_len;
1909f4c50d99SHerbert Xu 		nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw);
1910f4c50d99SHerbert Xu 		memcpy(skb_put(nskb, doffset), skb->data, doffset);
1911f4c50d99SHerbert Xu 
1912f4c50d99SHerbert Xu 		if (!sg) {
1913f4c50d99SHerbert Xu 			nskb->csum = skb_copy_and_csum_bits(skb, offset,
1914f4c50d99SHerbert Xu 							    skb_put(nskb, len),
1915f4c50d99SHerbert Xu 							    len, 0);
1916f4c50d99SHerbert Xu 			continue;
1917f4c50d99SHerbert Xu 		}
1918f4c50d99SHerbert Xu 
1919f4c50d99SHerbert Xu 		frag = skb_shinfo(nskb)->frags;
1920f4c50d99SHerbert Xu 		k = 0;
1921f4c50d99SHerbert Xu 
192284fa7933SPatrick McHardy 		nskb->ip_summed = CHECKSUM_PARTIAL;
1923f4c50d99SHerbert Xu 		nskb->csum = skb->csum;
1924f4c50d99SHerbert Xu 		memcpy(skb_put(nskb, hsize), skb->data + offset, hsize);
1925f4c50d99SHerbert Xu 
1926f4c50d99SHerbert Xu 		while (pos < offset + len) {
1927f4c50d99SHerbert Xu 			BUG_ON(i >= nfrags);
1928f4c50d99SHerbert Xu 
1929f4c50d99SHerbert Xu 			*frag = skb_shinfo(skb)->frags[i];
1930f4c50d99SHerbert Xu 			get_page(frag->page);
1931f4c50d99SHerbert Xu 			size = frag->size;
1932f4c50d99SHerbert Xu 
1933f4c50d99SHerbert Xu 			if (pos < offset) {
1934f4c50d99SHerbert Xu 				frag->page_offset += offset - pos;
1935f4c50d99SHerbert Xu 				frag->size -= offset - pos;
1936f4c50d99SHerbert Xu 			}
1937f4c50d99SHerbert Xu 
1938f4c50d99SHerbert Xu 			k++;
1939f4c50d99SHerbert Xu 
1940f4c50d99SHerbert Xu 			if (pos + size <= offset + len) {
1941f4c50d99SHerbert Xu 				i++;
1942f4c50d99SHerbert Xu 				pos += size;
1943f4c50d99SHerbert Xu 			} else {
1944f4c50d99SHerbert Xu 				frag->size -= pos + size - (offset + len);
1945f4c50d99SHerbert Xu 				break;
1946f4c50d99SHerbert Xu 			}
1947f4c50d99SHerbert Xu 
1948f4c50d99SHerbert Xu 			frag++;
1949f4c50d99SHerbert Xu 		}
1950f4c50d99SHerbert Xu 
1951f4c50d99SHerbert Xu 		skb_shinfo(nskb)->nr_frags = k;
1952f4c50d99SHerbert Xu 		nskb->data_len = len - hsize;
1953f4c50d99SHerbert Xu 		nskb->len += nskb->data_len;
1954f4c50d99SHerbert Xu 		nskb->truesize += nskb->data_len;
1955f4c50d99SHerbert Xu 	} while ((offset += len) < skb->len);
1956f4c50d99SHerbert Xu 
1957f4c50d99SHerbert Xu 	return segs;
1958f4c50d99SHerbert Xu 
1959f4c50d99SHerbert Xu err:
1960f4c50d99SHerbert Xu 	while ((skb = segs)) {
1961f4c50d99SHerbert Xu 		segs = skb->next;
1962b08d5840SPatrick McHardy 		kfree_skb(skb);
1963f4c50d99SHerbert Xu 	}
1964f4c50d99SHerbert Xu 	return ERR_PTR(err);
1965f4c50d99SHerbert Xu }
1966f4c50d99SHerbert Xu 
1967f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment);
1968f4c50d99SHerbert Xu 
19691da177e4SLinus Torvalds void __init skb_init(void)
19701da177e4SLinus Torvalds {
19711da177e4SLinus Torvalds 	skbuff_head_cache = kmem_cache_create("skbuff_head_cache",
19721da177e4SLinus Torvalds 					      sizeof(struct sk_buff),
19731da177e4SLinus Torvalds 					      0,
1974e5d679f3SAlexey Dobriyan 					      SLAB_HWCACHE_ALIGN|SLAB_PANIC,
19751da177e4SLinus Torvalds 					      NULL, NULL);
1976d179cd12SDavid S. Miller 	skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
1977d179cd12SDavid S. Miller 						(2*sizeof(struct sk_buff)) +
1978d179cd12SDavid S. Miller 						sizeof(atomic_t),
1979d179cd12SDavid S. Miller 						0,
1980e5d679f3SAlexey Dobriyan 						SLAB_HWCACHE_ALIGN|SLAB_PANIC,
1981d179cd12SDavid S. Miller 						NULL, NULL);
19821da177e4SLinus Torvalds }
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds EXPORT_SYMBOL(___pskb_trim);
19851da177e4SLinus Torvalds EXPORT_SYMBOL(__kfree_skb);
1986231d06aeSJörn Engel EXPORT_SYMBOL(kfree_skb);
19871da177e4SLinus Torvalds EXPORT_SYMBOL(__pskb_pull_tail);
1988d179cd12SDavid S. Miller EXPORT_SYMBOL(__alloc_skb);
19898af27456SChristoph Hellwig EXPORT_SYMBOL(__netdev_alloc_skb);
19901da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_copy);
19911da177e4SLinus Torvalds EXPORT_SYMBOL(pskb_expand_head);
19921da177e4SLinus Torvalds EXPORT_SYMBOL(skb_checksum);
19931da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone);
19941da177e4SLinus Torvalds EXPORT_SYMBOL(skb_clone_fraglist);
19951da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy);
19961da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_bits);
19971da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_and_csum_dev);
19981da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_bits);
19991da177e4SLinus Torvalds EXPORT_SYMBOL(skb_copy_expand);
20001da177e4SLinus Torvalds EXPORT_SYMBOL(skb_over_panic);
20011da177e4SLinus Torvalds EXPORT_SYMBOL(skb_pad);
20021da177e4SLinus Torvalds EXPORT_SYMBOL(skb_realloc_headroom);
20031da177e4SLinus Torvalds EXPORT_SYMBOL(skb_under_panic);
20041da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue);
20051da177e4SLinus Torvalds EXPORT_SYMBOL(skb_dequeue_tail);
20061da177e4SLinus Torvalds EXPORT_SYMBOL(skb_insert);
20071da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_purge);
20081da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_head);
20091da177e4SLinus Torvalds EXPORT_SYMBOL(skb_queue_tail);
20101da177e4SLinus Torvalds EXPORT_SYMBOL(skb_unlink);
20111da177e4SLinus Torvalds EXPORT_SYMBOL(skb_append);
20121da177e4SLinus Torvalds EXPORT_SYMBOL(skb_split);
2013677e90edSThomas Graf EXPORT_SYMBOL(skb_prepare_seq_read);
2014677e90edSThomas Graf EXPORT_SYMBOL(skb_seq_read);
2015677e90edSThomas Graf EXPORT_SYMBOL(skb_abort_seq_read);
20163fc7e8a6SThomas Graf EXPORT_SYMBOL(skb_find_text);
2017e89e9cf5SAnanda Raju EXPORT_SYMBOL(skb_append_datato_frags);
2018