| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	Routines having to do with the 'struct sk_buff' memory handlers. | 
|  | 3 | * | 
|  | 4 | *	Authors:	Alan Cox <iiitac@pyr.swan.ac.uk> | 
|  | 5 | *			Florian La Roche <rzsfl@rz.uni-sb.de> | 
|  | 6 | * | 
|  | 7 | *	Version:	$Id: skbuff.c,v 1.90 2001/11/07 05:56:19 davem Exp $ | 
|  | 8 | * | 
|  | 9 | *	Fixes: | 
|  | 10 | *		Alan Cox	:	Fixed the worst of the load | 
|  | 11 | *					balancer bugs. | 
|  | 12 | *		Dave Platt	:	Interrupt stacking fix. | 
|  | 13 | *	Richard Kooijman	:	Timestamp fixes. | 
|  | 14 | *		Alan Cox	:	Changed buffer format. | 
|  | 15 | *		Alan Cox	:	destructor hook for AF_UNIX etc. | 
|  | 16 | *		Linus Torvalds	:	Better skb_clone. | 
|  | 17 | *		Alan Cox	:	Added skb_copy. | 
|  | 18 | *		Alan Cox	:	Added all the changed routines Linus | 
|  | 19 | *					only put in the headers | 
|  | 20 | *		Ray VanTassle	:	Fixed --skb->lock in free | 
|  | 21 | *		Alan Cox	:	skb_copy copy arp field | 
|  | 22 | *		Andi Kleen	:	slabified it. | 
|  | 23 | *		Robert Olsson	:	Removed skb_head_pool | 
|  | 24 | * | 
|  | 25 | *	NOTE: | 
|  | 26 | *		The __skb_ routines should be called with interrupts | 
|  | 27 | *	disabled, or you better be *real* sure that the operation is atomic | 
|  | 28 | *	with respect to whatever list is being frobbed (e.g. via lock_sock() | 
|  | 29 | *	or via disabling bottom half handlers, etc). | 
|  | 30 | * | 
|  | 31 | *	This program is free software; you can redistribute it and/or | 
|  | 32 | *	modify it under the terms of the GNU General Public License | 
|  | 33 | *	as published by the Free Software Foundation; either version | 
|  | 34 | *	2 of the License, or (at your option) any later version. | 
|  | 35 | */ | 
|  | 36 |  | 
|  | 37 | /* | 
|  | 38 | *	The functions in this file will not compile correctly with gcc 2.4.x | 
|  | 39 | */ | 
|  | 40 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/module.h> | 
|  | 42 | #include <linux/types.h> | 
|  | 43 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/mm.h> | 
|  | 45 | #include <linux/interrupt.h> | 
|  | 46 | #include <linux/in.h> | 
|  | 47 | #include <linux/inet.h> | 
|  | 48 | #include <linux/slab.h> | 
|  | 49 | #include <linux/netdevice.h> | 
|  | 50 | #ifdef CONFIG_NET_CLS_ACT | 
|  | 51 | #include <net/pkt_sched.h> | 
|  | 52 | #endif | 
|  | 53 | #include <linux/string.h> | 
|  | 54 | #include <linux/skbuff.h> | 
| Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 55 | #include <linux/splice.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/cache.h> | 
|  | 57 | #include <linux/rtnetlink.h> | 
|  | 58 | #include <linux/init.h> | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 59 | #include <linux/scatterlist.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | #include <net/protocol.h> | 
|  | 62 | #include <net/dst.h> | 
|  | 63 | #include <net/sock.h> | 
|  | 64 | #include <net/checksum.h> | 
|  | 65 | #include <net/xfrm.h> | 
|  | 66 |  | 
|  | 67 | #include <asm/uaccess.h> | 
|  | 68 | #include <asm/system.h> | 
|  | 69 |  | 
| Al Viro | a1f8e7f7 | 2006-10-19 16:08:53 -0400 | [diff] [blame] | 70 | #include "kmap_skb.h" | 
|  | 71 |  | 
| Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 72 | static struct kmem_cache *skbuff_head_cache __read_mostly; | 
|  | 73 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 |  | 
| Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 75 | static void sock_pipe_buf_release(struct pipe_inode_info *pipe, | 
|  | 76 | struct pipe_buffer *buf) | 
|  | 77 | { | 
|  | 78 | struct sk_buff *skb = (struct sk_buff *) buf->private; | 
|  | 79 |  | 
|  | 80 | kfree_skb(skb); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | static void sock_pipe_buf_get(struct pipe_inode_info *pipe, | 
|  | 84 | struct pipe_buffer *buf) | 
|  | 85 | { | 
|  | 86 | struct sk_buff *skb = (struct sk_buff *) buf->private; | 
|  | 87 |  | 
|  | 88 | skb_get(skb); | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, | 
|  | 92 | struct pipe_buffer *buf) | 
|  | 93 | { | 
|  | 94 | return 1; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 |  | 
|  | 98 | /* Pipe buffer operations for a socket. */ | 
|  | 99 | static struct pipe_buf_operations sock_pipe_buf_ops = { | 
|  | 100 | .can_merge = 0, | 
|  | 101 | .map = generic_pipe_buf_map, | 
|  | 102 | .unmap = generic_pipe_buf_unmap, | 
|  | 103 | .confirm = generic_pipe_buf_confirm, | 
|  | 104 | .release = sock_pipe_buf_release, | 
|  | 105 | .steal = sock_pipe_buf_steal, | 
|  | 106 | .get = sock_pipe_buf_get, | 
|  | 107 | }; | 
|  | 108 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | /* | 
|  | 110 | *	Keep out-of-line to prevent kernel bloat. | 
|  | 111 | *	__builtin_return_address is not used because it is not always | 
|  | 112 | *	reliable. | 
|  | 113 | */ | 
|  | 114 |  | 
|  | 115 | /** | 
|  | 116 | *	skb_over_panic	- 	private function | 
|  | 117 | *	@skb: buffer | 
|  | 118 | *	@sz: size | 
|  | 119 | *	@here: address | 
|  | 120 | * | 
|  | 121 | *	Out of line support code for skb_put(). Not user callable. | 
|  | 122 | */ | 
|  | 123 | void skb_over_panic(struct sk_buff *skb, int sz, void *here) | 
|  | 124 | { | 
| Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 125 | printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 126 | "data:%p tail:%#lx end:%#lx dev:%s\n", | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 127 | here, skb->len, sz, skb->head, skb->data, | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 128 | (unsigned long)skb->tail, (unsigned long)skb->end, | 
| Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 129 | skb->dev ? skb->dev->name : "<NULL>"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | BUG(); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | /** | 
|  | 134 | *	skb_under_panic	- 	private function | 
|  | 135 | *	@skb: buffer | 
|  | 136 | *	@sz: size | 
|  | 137 | *	@here: address | 
|  | 138 | * | 
|  | 139 | *	Out of line support code for skb_push(). Not user callable. | 
|  | 140 | */ | 
|  | 141 |  | 
|  | 142 | void skb_under_panic(struct sk_buff *skb, int sz, void *here) | 
|  | 143 | { | 
| Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 144 | printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 145 | "data:%p tail:%#lx end:%#lx dev:%s\n", | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 146 | here, skb->len, sz, skb->head, skb->data, | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 147 | (unsigned long)skb->tail, (unsigned long)skb->end, | 
| Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 148 | skb->dev ? skb->dev->name : "<NULL>"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | BUG(); | 
|  | 150 | } | 
|  | 151 |  | 
| David S. Miller | dc6de33 | 2006-04-20 00:10:50 -0700 | [diff] [blame] | 152 | void skb_truesize_bug(struct sk_buff *skb) | 
|  | 153 | { | 
|  | 154 | printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " | 
|  | 155 | "len=%u, sizeof(sk_buff)=%Zd\n", | 
|  | 156 | skb->truesize, skb->len, sizeof(struct sk_buff)); | 
|  | 157 | } | 
|  | 158 | EXPORT_SYMBOL(skb_truesize_bug); | 
|  | 159 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | /* 	Allocate a new skbuff. We do this ourselves so we can fill in a few | 
|  | 161 | *	'private' fields and also do memory statistics to find all the | 
|  | 162 | *	[BEEP] leaks. | 
|  | 163 | * | 
|  | 164 | */ | 
|  | 165 |  | 
|  | 166 | /** | 
| David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 167 | *	__alloc_skb	-	allocate a network buffer | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | *	@size: size to allocate | 
|  | 169 | *	@gfp_mask: allocation mask | 
| Randy Dunlap | c83c248 | 2005-10-18 22:07:41 -0700 | [diff] [blame] | 170 | *	@fclone: allocate from fclone cache instead of head cache | 
|  | 171 | *		and allocate a cloned (child) skb | 
| Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 172 | *	@node: numa node to allocate memory on | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | * | 
|  | 174 | *	Allocate a new &sk_buff. The returned buffer has no headroom and a | 
|  | 175 | *	tail room of size bytes. The object has a reference count of one. | 
|  | 176 | *	The return is the buffer. On a failure the return is %NULL. | 
|  | 177 | * | 
|  | 178 | *	Buffers may only be allocated from interrupts using a @gfp_mask of | 
|  | 179 | *	%GFP_ATOMIC. | 
|  | 180 | */ | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 181 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, | 
| Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 182 | int fclone, int node) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { | 
| Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 184 | struct kmem_cache *cache; | 
| Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 185 | struct skb_shared_info *shinfo; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | struct sk_buff *skb; | 
|  | 187 | u8 *data; | 
|  | 188 |  | 
| Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 189 | cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; | 
|  | 190 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | /* Get the HEAD */ | 
| Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 192 | skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!skb) | 
|  | 194 | goto out; | 
|  | 195 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | size = SKB_DATA_ALIGN(size); | 
| Christoph Hellwig | b30973f | 2006-12-06 20:32:36 -0800 | [diff] [blame] | 197 | data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), | 
|  | 198 | gfp_mask, node); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (!data) | 
|  | 200 | goto nodata; | 
|  | 201 |  | 
| Arnaldo Carvalho de Melo | ca0605a | 2007-03-19 10:48:59 -0300 | [diff] [blame] | 202 | /* | 
|  | 203 | * See comment in sk_buff definition, just before the 'tail' member | 
|  | 204 | */ | 
|  | 205 | memset(skb, 0, offsetof(struct sk_buff, tail)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | skb->truesize = size + sizeof(struct sk_buff); | 
|  | 207 | atomic_set(&skb->users, 1); | 
|  | 208 | skb->head = data; | 
|  | 209 | skb->data = data; | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 210 | skb_reset_tail_pointer(skb); | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 211 | skb->end = skb->tail + size; | 
| Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 212 | /* make sure we initialize shinfo sequentially */ | 
|  | 213 | shinfo = skb_shinfo(skb); | 
|  | 214 | atomic_set(&shinfo->dataref, 1); | 
|  | 215 | shinfo->nr_frags  = 0; | 
| Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 216 | shinfo->gso_size = 0; | 
|  | 217 | shinfo->gso_segs = 0; | 
|  | 218 | shinfo->gso_type = 0; | 
| Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 219 | shinfo->ip6_frag_id = 0; | 
|  | 220 | shinfo->frag_list = NULL; | 
|  | 221 |  | 
| David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 222 | if (fclone) { | 
|  | 223 | struct sk_buff *child = skb + 1; | 
|  | 224 | atomic_t *fclone_ref = (atomic_t *) (child + 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 |  | 
| David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 226 | skb->fclone = SKB_FCLONE_ORIG; | 
|  | 227 | atomic_set(fclone_ref, 1); | 
|  | 228 |  | 
|  | 229 | child->fclone = SKB_FCLONE_UNAVAILABLE; | 
|  | 230 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | out: | 
|  | 232 | return skb; | 
|  | 233 | nodata: | 
| Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 234 | kmem_cache_free(cache, skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | skb = NULL; | 
|  | 236 | goto out; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | /** | 
| Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 240 | *	__netdev_alloc_skb - allocate an skbuff for rx on a specific device | 
|  | 241 | *	@dev: network device to receive on | 
|  | 242 | *	@length: length to allocate | 
|  | 243 | *	@gfp_mask: get_free_pages mask, passed to alloc_skb | 
|  | 244 | * | 
|  | 245 | *	Allocate a new &sk_buff and assign it a usage count of one. The | 
|  | 246 | *	buffer has unspecified headroom built in. Users should allocate | 
|  | 247 | *	the headroom they think they need without accounting for the | 
|  | 248 | *	built in space. The built in space is used for optimisations. | 
|  | 249 | * | 
|  | 250 | *	%NULL is returned if there is no free memory. | 
|  | 251 | */ | 
|  | 252 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, | 
|  | 253 | unsigned int length, gfp_t gfp_mask) | 
|  | 254 | { | 
| Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 255 | int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; | 
| Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 256 | struct sk_buff *skb; | 
|  | 257 |  | 
| YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 258 | skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node); | 
| Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 259 | if (likely(skb)) { | 
| Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 260 | skb_reserve(skb, NET_SKB_PAD); | 
| Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 261 | skb->dev = dev; | 
|  | 262 | } | 
| Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 263 | return skb; | 
|  | 264 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 |  | 
| Ilpo Järvinen | f58518e | 2008-03-27 17:51:31 -0700 | [diff] [blame] | 266 | /** | 
|  | 267 | *	dev_alloc_skb - allocate an skbuff for receiving | 
|  | 268 | *	@length: length to allocate | 
|  | 269 | * | 
|  | 270 | *	Allocate a new &sk_buff and assign it a usage count of one. The | 
|  | 271 | *	buffer has unspecified headroom built in. Users should allocate | 
|  | 272 | *	the headroom they think they need without accounting for the | 
|  | 273 | *	built in space. The built in space is used for optimisations. | 
|  | 274 | * | 
|  | 275 | *	%NULL is returned if there is no free memory. Although this function | 
|  | 276 | *	allocates memory it can be called from an interrupt. | 
|  | 277 | */ | 
|  | 278 | struct sk_buff *dev_alloc_skb(unsigned int length) | 
|  | 279 | { | 
| Denys Vlasenko | 1483b87 | 2008-03-28 15:57:39 -0700 | [diff] [blame] | 280 | /* | 
|  | 281 | * There is more code here than it seems: | 
| David S. Miller | a0f55e0 | 2008-03-28 18:22:32 -0700 | [diff] [blame] | 282 | * __dev_alloc_skb is an inline | 
| Denys Vlasenko | 1483b87 | 2008-03-28 15:57:39 -0700 | [diff] [blame] | 283 | */ | 
| Ilpo Järvinen | f58518e | 2008-03-27 17:51:31 -0700 | [diff] [blame] | 284 | return __dev_alloc_skb(length, GFP_ATOMIC); | 
|  | 285 | } | 
|  | 286 | EXPORT_SYMBOL(dev_alloc_skb); | 
|  | 287 |  | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 288 | static void skb_drop_list(struct sk_buff **listp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 290 | struct sk_buff *list = *listp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 |  | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 292 | *listp = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 |  | 
|  | 294 | do { | 
|  | 295 | struct sk_buff *this = list; | 
|  | 296 | list = list->next; | 
|  | 297 | kfree_skb(this); | 
|  | 298 | } while (list); | 
|  | 299 | } | 
|  | 300 |  | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 301 | static inline void skb_drop_fraglist(struct sk_buff *skb) | 
|  | 302 | { | 
|  | 303 | skb_drop_list(&skb_shinfo(skb)->frag_list); | 
|  | 304 | } | 
|  | 305 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | static void skb_clone_fraglist(struct sk_buff *skb) | 
|  | 307 | { | 
|  | 308 | struct sk_buff *list; | 
|  | 309 |  | 
|  | 310 | for (list = skb_shinfo(skb)->frag_list; list; list = list->next) | 
|  | 311 | skb_get(list); | 
|  | 312 | } | 
|  | 313 |  | 
| Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 314 | static void skb_release_data(struct sk_buff *skb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { | 
|  | 316 | if (!skb->cloned || | 
|  | 317 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, | 
|  | 318 | &skb_shinfo(skb)->dataref)) { | 
|  | 319 | if (skb_shinfo(skb)->nr_frags) { | 
|  | 320 | int i; | 
|  | 321 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | 
|  | 322 | put_page(skb_shinfo(skb)->frags[i].page); | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | if (skb_shinfo(skb)->frag_list) | 
|  | 326 | skb_drop_fraglist(skb); | 
|  | 327 |  | 
|  | 328 | kfree(skb->head); | 
|  | 329 | } | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | /* | 
|  | 333 | *	Free an skbuff by memory without cleaning the state. | 
|  | 334 | */ | 
| Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 335 | static void kfree_skbmem(struct sk_buff *skb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { | 
| David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 337 | struct sk_buff *other; | 
|  | 338 | atomic_t *fclone_ref; | 
|  | 339 |  | 
| David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 340 | switch (skb->fclone) { | 
|  | 341 | case SKB_FCLONE_UNAVAILABLE: | 
|  | 342 | kmem_cache_free(skbuff_head_cache, skb); | 
|  | 343 | break; | 
|  | 344 |  | 
|  | 345 | case SKB_FCLONE_ORIG: | 
|  | 346 | fclone_ref = (atomic_t *) (skb + 2); | 
|  | 347 | if (atomic_dec_and_test(fclone_ref)) | 
|  | 348 | kmem_cache_free(skbuff_fclone_cache, skb); | 
|  | 349 | break; | 
|  | 350 |  | 
|  | 351 | case SKB_FCLONE_CLONE: | 
|  | 352 | fclone_ref = (atomic_t *) (skb + 1); | 
|  | 353 | other = skb - 1; | 
|  | 354 |  | 
|  | 355 | /* The clone portion is available for | 
|  | 356 | * fast-cloning again. | 
|  | 357 | */ | 
|  | 358 | skb->fclone = SKB_FCLONE_UNAVAILABLE; | 
|  | 359 |  | 
|  | 360 | if (atomic_dec_and_test(fclone_ref)) | 
|  | 361 | kmem_cache_free(skbuff_fclone_cache, other); | 
|  | 362 | break; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 363 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } | 
|  | 365 |  | 
| Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 366 | /* Free everything but the sk_buff shell. */ | 
|  | 367 | static void skb_release_all(struct sk_buff *skb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | dst_release(skb->dst); | 
|  | 370 | #ifdef CONFIG_XFRM | 
|  | 371 | secpath_put(skb->sp); | 
|  | 372 | #endif | 
| Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 373 | if (skb->destructor) { | 
|  | 374 | WARN_ON(in_irq()); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | skb->destructor(skb); | 
|  | 376 | } | 
| Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 377 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | 
| Yasuyuki Kozakai | 5f79e0f | 2007-03-23 11:17:07 -0700 | [diff] [blame] | 378 | nf_conntrack_put(skb->nfct); | 
| Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 379 | nf_conntrack_put_reasm(skb->nfct_reasm); | 
|  | 380 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | #ifdef CONFIG_BRIDGE_NETFILTER | 
|  | 382 | nf_bridge_put(skb->nf_bridge); | 
|  | 383 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* XXX: IS this still necessary? - JHS */ | 
|  | 385 | #ifdef CONFIG_NET_SCHED | 
|  | 386 | skb->tc_index = 0; | 
|  | 387 | #ifdef CONFIG_NET_CLS_ACT | 
|  | 388 | skb->tc_verd = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | #endif | 
|  | 390 | #endif | 
| Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 391 | skb_release_data(skb); | 
|  | 392 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 |  | 
| Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 394 | /** | 
|  | 395 | *	__kfree_skb - private function | 
|  | 396 | *	@skb: buffer | 
|  | 397 | * | 
|  | 398 | *	Free an sk_buff. Release anything attached to the buffer. | 
|  | 399 | *	Clean the state. This is an internal helper function. Users should | 
|  | 400 | *	always call kfree_skb | 
|  | 401 | */ | 
|  | 402 |  | 
|  | 403 | void __kfree_skb(struct sk_buff *skb) | 
|  | 404 | { | 
|  | 405 | skb_release_all(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | kfree_skbmem(skb); | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | /** | 
| Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 410 | *	kfree_skb - free an sk_buff | 
|  | 411 | *	@skb: buffer to free | 
|  | 412 | * | 
|  | 413 | *	Drop a reference to the buffer and free it if the usage count has | 
|  | 414 | *	hit zero. | 
|  | 415 | */ | 
|  | 416 | void kfree_skb(struct sk_buff *skb) | 
|  | 417 | { | 
|  | 418 | if (unlikely(!skb)) | 
|  | 419 | return; | 
|  | 420 | if (likely(atomic_read(&skb->users) == 1)) | 
|  | 421 | smp_rmb(); | 
|  | 422 | else if (likely(!atomic_dec_and_test(&skb->users))) | 
|  | 423 | return; | 
|  | 424 | __kfree_skb(skb); | 
|  | 425 | } | 
|  | 426 |  | 
| Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 427 | static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) | 
|  | 428 | { | 
|  | 429 | new->tstamp		= old->tstamp; | 
|  | 430 | new->dev		= old->dev; | 
|  | 431 | new->transport_header	= old->transport_header; | 
|  | 432 | new->network_header	= old->network_header; | 
|  | 433 | new->mac_header		= old->mac_header; | 
|  | 434 | new->dst		= dst_clone(old->dst); | 
|  | 435 | #ifdef CONFIG_INET | 
|  | 436 | new->sp			= secpath_get(old->sp); | 
|  | 437 | #endif | 
|  | 438 | memcpy(new->cb, old->cb, sizeof(old->cb)); | 
|  | 439 | new->csum_start		= old->csum_start; | 
|  | 440 | new->csum_offset	= old->csum_offset; | 
|  | 441 | new->local_df		= old->local_df; | 
|  | 442 | new->pkt_type		= old->pkt_type; | 
|  | 443 | new->ip_summed		= old->ip_summed; | 
|  | 444 | skb_copy_queue_mapping(new, old); | 
|  | 445 | new->priority		= old->priority; | 
|  | 446 | #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) | 
|  | 447 | new->ipvs_property	= old->ipvs_property; | 
|  | 448 | #endif | 
|  | 449 | new->protocol		= old->protocol; | 
|  | 450 | new->mark		= old->mark; | 
|  | 451 | __nf_copy(new, old); | 
|  | 452 | #if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ | 
|  | 453 | defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) | 
|  | 454 | new->nf_trace		= old->nf_trace; | 
|  | 455 | #endif | 
|  | 456 | #ifdef CONFIG_NET_SCHED | 
|  | 457 | new->tc_index		= old->tc_index; | 
|  | 458 | #ifdef CONFIG_NET_CLS_ACT | 
|  | 459 | new->tc_verd		= old->tc_verd; | 
|  | 460 | #endif | 
|  | 461 | #endif | 
|  | 462 | skb_copy_secmark(new, old); | 
|  | 463 | } | 
|  | 464 |  | 
| Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 465 | static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | #define C(x) n->x = skb->x | 
|  | 468 |  | 
|  | 469 | n->next = n->prev = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | n->sk = NULL; | 
| Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 471 | __copy_skb_header(n, skb); | 
|  | 472 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | C(len); | 
|  | 474 | C(data_len); | 
| Alexey Dobriyan | 3e6b3b2 | 2007-03-16 15:00:46 -0700 | [diff] [blame] | 475 | C(mac_len); | 
| Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 476 | n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; | 
| Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 477 | n->cloned = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | n->nohdr = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | n->destructor = NULL; | 
| Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 480 | C(iif); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | C(tail); | 
|  | 482 | C(end); | 
| Paul Moore | 02f1c89 | 2008-01-07 21:56:41 -0800 | [diff] [blame] | 483 | C(head); | 
|  | 484 | C(data); | 
|  | 485 | C(truesize); | 
|  | 486 | atomic_set(&n->users, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 |  | 
|  | 488 | atomic_inc(&(skb_shinfo(skb)->dataref)); | 
|  | 489 | skb->cloned = 1; | 
|  | 490 |  | 
|  | 491 | return n; | 
| Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 492 | #undef C | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | /** | 
|  | 496 | *	skb_morph	-	morph one skb into another | 
|  | 497 | *	@dst: the skb to receive the contents | 
|  | 498 | *	@src: the skb to supply the contents | 
|  | 499 | * | 
|  | 500 | *	This is identical to skb_clone except that the target skb is | 
|  | 501 | *	supplied by the user. | 
|  | 502 | * | 
|  | 503 | *	The target skb is returned upon exit. | 
|  | 504 | */ | 
|  | 505 | struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) | 
|  | 506 | { | 
| Herbert Xu | 2d4baff | 2007-11-26 23:11:19 +0800 | [diff] [blame] | 507 | skb_release_all(dst); | 
| Herbert Xu | e0053ec | 2007-10-14 00:37:52 -0700 | [diff] [blame] | 508 | return __skb_clone(dst, src); | 
|  | 509 | } | 
|  | 510 | EXPORT_SYMBOL_GPL(skb_morph); | 
|  | 511 |  | 
|  | 512 | /** | 
|  | 513 | *	skb_clone	-	duplicate an sk_buff | 
|  | 514 | *	@skb: buffer to clone | 
|  | 515 | *	@gfp_mask: allocation priority | 
|  | 516 | * | 
|  | 517 | *	Duplicate an &sk_buff. The new one is not owned by a socket. Both | 
|  | 518 | *	copies share the same packet data but not structure. The new | 
|  | 519 | *	buffer has a reference count of 1. If the allocation fails the | 
|  | 520 | *	function returns %NULL otherwise the new buffer is returned. | 
|  | 521 | * | 
|  | 522 | *	If this function is called from an interrupt gfp_mask() must be | 
|  | 523 | *	%GFP_ATOMIC. | 
|  | 524 | */ | 
|  | 525 |  | 
|  | 526 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) | 
|  | 527 | { | 
|  | 528 | struct sk_buff *n; | 
|  | 529 |  | 
|  | 530 | n = skb + 1; | 
|  | 531 | if (skb->fclone == SKB_FCLONE_ORIG && | 
|  | 532 | n->fclone == SKB_FCLONE_UNAVAILABLE) { | 
|  | 533 | atomic_t *fclone_ref = (atomic_t *) (n + 1); | 
|  | 534 | n->fclone = SKB_FCLONE_CLONE; | 
|  | 535 | atomic_inc(fclone_ref); | 
|  | 536 | } else { | 
|  | 537 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); | 
|  | 538 | if (!n) | 
|  | 539 | return NULL; | 
|  | 540 | n->fclone = SKB_FCLONE_UNAVAILABLE; | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | return __skb_clone(n, skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } | 
|  | 545 |  | 
|  | 546 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) | 
|  | 547 | { | 
| Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 548 | #ifndef NET_SKBUFF_DATA_USES_OFFSET | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | /* | 
|  | 550 | *	Shift between the two data areas in bytes | 
|  | 551 | */ | 
|  | 552 | unsigned long offset = new->data - old->data; | 
| Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 553 | #endif | 
| Herbert Xu | dec1881 | 2007-10-14 00:37:30 -0700 | [diff] [blame] | 554 |  | 
|  | 555 | __copy_skb_header(new, old); | 
|  | 556 |  | 
| Arnaldo Carvalho de Melo | 2e07fa9 | 2007-04-10 21:22:35 -0700 | [diff] [blame] | 557 | #ifndef NET_SKBUFF_DATA_USES_OFFSET | 
|  | 558 | /* {transport,network,mac}_header are relative to skb->head */ | 
|  | 559 | new->transport_header += offset; | 
|  | 560 | new->network_header   += offset; | 
|  | 561 | new->mac_header	      += offset; | 
|  | 562 | #endif | 
| Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 563 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; | 
|  | 564 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; | 
|  | 565 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } | 
|  | 567 |  | 
|  | 568 | /** | 
|  | 569 | *	skb_copy	-	create private copy of an sk_buff | 
|  | 570 | *	@skb: buffer to copy | 
|  | 571 | *	@gfp_mask: allocation priority | 
|  | 572 | * | 
|  | 573 | *	Make a copy of both an &sk_buff and its data. This is used when the | 
|  | 574 | *	caller wishes to modify the data and needs a private copy of the | 
|  | 575 | *	data to alter. Returns %NULL on failure or the pointer to the buffer | 
|  | 576 | *	on success. The returned buffer has a reference count of 1. | 
|  | 577 | * | 
|  | 578 | *	As by-product this function converts non-linear &sk_buff to linear | 
|  | 579 | *	one, so that &sk_buff becomes completely private and caller is allowed | 
|  | 580 | *	to modify all the data of returned buffer. This means that this | 
|  | 581 | *	function is not recommended for use in circumstances when only | 
|  | 582 | *	header is going to be modified. Use pskb_copy() instead. | 
|  | 583 | */ | 
|  | 584 |  | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 585 | struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | { | 
|  | 587 | int headerlen = skb->data - skb->head; | 
|  | 588 | /* | 
|  | 589 | *	Allocate the copy buffer | 
|  | 590 | */ | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 591 | struct sk_buff *n; | 
|  | 592 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | 
|  | 593 | n = alloc_skb(skb->end + skb->data_len, gfp_mask); | 
|  | 594 | #else | 
|  | 595 | n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); | 
|  | 596 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | if (!n) | 
|  | 598 | return NULL; | 
|  | 599 |  | 
|  | 600 | /* Set the data pointer */ | 
|  | 601 | skb_reserve(n, headerlen); | 
|  | 602 | /* Set the tail pointer and length */ | 
|  | 603 | skb_put(n, skb->len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 |  | 
|  | 605 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) | 
|  | 606 | BUG(); | 
|  | 607 |  | 
|  | 608 | copy_skb_header(n, skb); | 
|  | 609 | return n; | 
|  | 610 | } | 
|  | 611 |  | 
|  | 612 |  | 
|  | 613 | /** | 
|  | 614 | *	pskb_copy	-	create copy of an sk_buff with private head. | 
|  | 615 | *	@skb: buffer to copy | 
|  | 616 | *	@gfp_mask: allocation priority | 
|  | 617 | * | 
|  | 618 | *	Make a copy of both an &sk_buff and part of its data, located | 
|  | 619 | *	in header. Fragmented data remain shared. This is used when | 
|  | 620 | *	the caller wishes to modify only header of &sk_buff and needs | 
|  | 621 | *	private copy of the header to alter. Returns %NULL on failure | 
|  | 622 | *	or the pointer to the buffer on success. | 
|  | 623 | *	The returned buffer has a reference count of 1. | 
|  | 624 | */ | 
|  | 625 |  | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 626 | struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { | 
|  | 628 | /* | 
|  | 629 | *	Allocate the copy buffer | 
|  | 630 | */ | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 631 | struct sk_buff *n; | 
|  | 632 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | 
|  | 633 | n = alloc_skb(skb->end, gfp_mask); | 
|  | 634 | #else | 
|  | 635 | n = alloc_skb(skb->end - skb->head, gfp_mask); | 
|  | 636 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | if (!n) | 
|  | 638 | goto out; | 
|  | 639 |  | 
|  | 640 | /* Set the data pointer */ | 
|  | 641 | skb_reserve(n, skb->data - skb->head); | 
|  | 642 | /* Set the tail pointer and length */ | 
|  | 643 | skb_put(n, skb_headlen(skb)); | 
|  | 644 | /* Copy the bytes */ | 
| Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 645 | skb_copy_from_linear_data(skb, n->data, n->len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 |  | 
| Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 647 | n->truesize += skb->data_len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | n->data_len  = skb->data_len; | 
|  | 649 | n->len	     = skb->len; | 
|  | 650 |  | 
|  | 651 | if (skb_shinfo(skb)->nr_frags) { | 
|  | 652 | int i; | 
|  | 653 |  | 
|  | 654 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
|  | 655 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; | 
|  | 656 | get_page(skb_shinfo(n)->frags[i].page); | 
|  | 657 | } | 
|  | 658 | skb_shinfo(n)->nr_frags = i; | 
|  | 659 | } | 
|  | 660 |  | 
|  | 661 | if (skb_shinfo(skb)->frag_list) { | 
|  | 662 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; | 
|  | 663 | skb_clone_fraglist(n); | 
|  | 664 | } | 
|  | 665 |  | 
|  | 666 | copy_skb_header(n, skb); | 
|  | 667 | out: | 
|  | 668 | return n; | 
|  | 669 | } | 
|  | 670 |  | 
|  | 671 | /** | 
|  | 672 | *	pskb_expand_head - reallocate header of &sk_buff | 
|  | 673 | *	@skb: buffer to reallocate | 
|  | 674 | *	@nhead: room to add at head | 
|  | 675 | *	@ntail: room to add at tail | 
|  | 676 | *	@gfp_mask: allocation priority | 
|  | 677 | * | 
|  | 678 | *	Expands (or creates identical copy, if &nhead and &ntail are zero) | 
|  | 679 | *	header of skb. &sk_buff itself is not changed. &sk_buff MUST have | 
|  | 680 | *	reference count of 1. Returns zero in the case of success or error, | 
|  | 681 | *	if expansion failed. In the last case, &sk_buff is not changed. | 
|  | 682 | * | 
|  | 683 | *	All the pointers pointing into skb header may change and must be | 
|  | 684 | *	reloaded after call to this function. | 
|  | 685 | */ | 
|  | 686 |  | 
| Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 687 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 688 | gfp_t gfp_mask) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { | 
|  | 690 | int i; | 
|  | 691 | u8 *data; | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 692 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | 
|  | 693 | int size = nhead + skb->end + ntail; | 
|  | 694 | #else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | int size = nhead + (skb->end - skb->head) + ntail; | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 696 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | long off; | 
|  | 698 |  | 
|  | 699 | if (skb_shared(skb)) | 
|  | 700 | BUG(); | 
|  | 701 |  | 
|  | 702 | size = SKB_DATA_ALIGN(size); | 
|  | 703 |  | 
|  | 704 | data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); | 
|  | 705 | if (!data) | 
|  | 706 | goto nodata; | 
|  | 707 |  | 
|  | 708 | /* Copy only real data... and, alas, header. This should be | 
|  | 709 | * optimized for the cases when header is void. */ | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 710 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | 
| Mikael Pettersson | b6ccc67 | 2007-05-19 13:55:25 -0700 | [diff] [blame] | 711 | memcpy(data + nhead, skb->head, skb->tail); | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 712 | #else | 
| Mikael Pettersson | b6ccc67 | 2007-05-19 13:55:25 -0700 | [diff] [blame] | 713 | memcpy(data + nhead, skb->head, skb->tail - skb->head); | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 714 | #endif | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 715 | memcpy(data + size, skb_end_pointer(skb), | 
|  | 716 | sizeof(struct skb_shared_info)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 |  | 
|  | 718 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | 
|  | 719 | get_page(skb_shinfo(skb)->frags[i].page); | 
|  | 720 |  | 
|  | 721 | if (skb_shinfo(skb)->frag_list) | 
|  | 722 | skb_clone_fraglist(skb); | 
|  | 723 |  | 
|  | 724 | skb_release_data(skb); | 
|  | 725 |  | 
|  | 726 | off = (data + nhead) - skb->head; | 
|  | 727 |  | 
|  | 728 | skb->head     = data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | skb->data    += off; | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 730 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | 
|  | 731 | skb->end      = size; | 
| Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 732 | off           = nhead; | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 733 | #else | 
|  | 734 | skb->end      = skb->head + size; | 
| Patrick McHardy | 56eb888 | 2007-04-09 11:45:04 -0700 | [diff] [blame] | 735 | #endif | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 736 | /* {transport,network,mac}_header and tail are relative to skb->head */ | 
|  | 737 | skb->tail	      += off; | 
| Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 738 | skb->transport_header += off; | 
|  | 739 | skb->network_header   += off; | 
|  | 740 | skb->mac_header	      += off; | 
| Herbert Xu | 172a863 | 2007-10-15 01:46:08 -0700 | [diff] [blame] | 741 | skb->csum_start       += nhead; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | skb->cloned   = 0; | 
| Patrick McHardy | 334a813 | 2007-06-25 04:35:20 -0700 | [diff] [blame] | 743 | skb->hdr_len  = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | skb->nohdr    = 0; | 
|  | 745 | atomic_set(&skb_shinfo(skb)->dataref, 1); | 
|  | 746 | return 0; | 
|  | 747 |  | 
|  | 748 | nodata: | 
|  | 749 | return -ENOMEM; | 
|  | 750 | } | 
|  | 751 |  | 
|  | 752 | /* Make private copy of skb with writable head and some headroom */ | 
|  | 753 |  | 
|  | 754 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) | 
|  | 755 | { | 
|  | 756 | struct sk_buff *skb2; | 
|  | 757 | int delta = headroom - skb_headroom(skb); | 
|  | 758 |  | 
|  | 759 | if (delta <= 0) | 
|  | 760 | skb2 = pskb_copy(skb, GFP_ATOMIC); | 
|  | 761 | else { | 
|  | 762 | skb2 = skb_clone(skb, GFP_ATOMIC); | 
|  | 763 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, | 
|  | 764 | GFP_ATOMIC)) { | 
|  | 765 | kfree_skb(skb2); | 
|  | 766 | skb2 = NULL; | 
|  | 767 | } | 
|  | 768 | } | 
|  | 769 | return skb2; | 
|  | 770 | } | 
|  | 771 |  | 
|  | 772 |  | 
|  | 773 | /** | 
|  | 774 | *	skb_copy_expand	-	copy and expand sk_buff | 
|  | 775 | *	@skb: buffer to copy | 
|  | 776 | *	@newheadroom: new free bytes at head | 
|  | 777 | *	@newtailroom: new free bytes at tail | 
|  | 778 | *	@gfp_mask: allocation priority | 
|  | 779 | * | 
|  | 780 | *	Make a copy of both an &sk_buff and its data and while doing so | 
|  | 781 | *	allocate additional space. | 
|  | 782 | * | 
|  | 783 | *	This is used when the caller wishes to modify the data and needs a | 
|  | 784 | *	private copy of the data to alter as well as more space for new fields. | 
|  | 785 | *	Returns %NULL on failure or the pointer to the buffer | 
|  | 786 | *	on success. The returned buffer has a reference count of 1. | 
|  | 787 | * | 
|  | 788 | *	You must pass %GFP_ATOMIC as the allocation priority if this function | 
|  | 789 | *	is called from an interrupt. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | */ | 
|  | 791 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, | 
| Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 792 | int newheadroom, int newtailroom, | 
| Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 793 | gfp_t gfp_mask) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { | 
|  | 795 | /* | 
|  | 796 | *	Allocate the copy buffer | 
|  | 797 | */ | 
|  | 798 | struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, | 
|  | 799 | gfp_mask); | 
| Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 800 | int oldheadroom = skb_headroom(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | int head_copy_len, head_copy_off; | 
| Herbert Xu | 5288605 | 2007-09-16 16:32:11 -0700 | [diff] [blame] | 802 | int off; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 |  | 
|  | 804 | if (!n) | 
|  | 805 | return NULL; | 
|  | 806 |  | 
|  | 807 | skb_reserve(n, newheadroom); | 
|  | 808 |  | 
|  | 809 | /* Set the tail pointer and length */ | 
|  | 810 | skb_put(n, skb->len); | 
|  | 811 |  | 
| Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 812 | head_copy_len = oldheadroom; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | head_copy_off = 0; | 
|  | 814 | if (newheadroom <= head_copy_len) | 
|  | 815 | head_copy_len = newheadroom; | 
|  | 816 | else | 
|  | 817 | head_copy_off = newheadroom - head_copy_len; | 
|  | 818 |  | 
|  | 819 | /* Copy the linear header and data. */ | 
|  | 820 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, | 
|  | 821 | skb->len + head_copy_len)) | 
|  | 822 | BUG(); | 
|  | 823 |  | 
|  | 824 | copy_skb_header(n, skb); | 
|  | 825 |  | 
| Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 826 | off                  = newheadroom - oldheadroom; | 
| Herbert Xu | 5288605 | 2007-09-16 16:32:11 -0700 | [diff] [blame] | 827 | n->csum_start       += off; | 
|  | 828 | #ifdef NET_SKBUFF_DATA_USES_OFFSET | 
| Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 829 | n->transport_header += off; | 
|  | 830 | n->network_header   += off; | 
|  | 831 | n->mac_header	    += off; | 
| Herbert Xu | 5288605 | 2007-09-16 16:32:11 -0700 | [diff] [blame] | 832 | #endif | 
| Patrick McHardy | efd1e8d | 2007-04-10 18:30:09 -0700 | [diff] [blame] | 833 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | return n; | 
|  | 835 | } | 
|  | 836 |  | 
|  | 837 | /** | 
|  | 838 | *	skb_pad			-	zero pad the tail of an skb | 
|  | 839 | *	@skb: buffer to pad | 
|  | 840 | *	@pad: space to pad | 
|  | 841 | * | 
|  | 842 | *	Ensure that a buffer is followed by a padding area that is zero | 
|  | 843 | *	filled. Used by network drivers which may DMA or transfer data | 
|  | 844 | *	beyond the buffer end onto the wire. | 
|  | 845 | * | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 846 | *	May return error in out of memory cases. The skb is freed on error. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | */ | 
| YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 848 |  | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 849 | int skb_pad(struct sk_buff *skb, int pad) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | { | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 851 | int err; | 
|  | 852 | int ntail; | 
| YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 853 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | /* If the skbuff is non linear tailroom is always zero.. */ | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 855 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | memset(skb->data+skb->len, 0, pad); | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 857 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | } | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 859 |  | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 860 | ntail = skb->data_len + pad - (skb->end - skb->tail); | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 861 | if (likely(skb_cloned(skb) || ntail > 0)) { | 
|  | 862 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); | 
|  | 863 | if (unlikely(err)) | 
|  | 864 | goto free_skb; | 
|  | 865 | } | 
|  | 866 |  | 
|  | 867 | /* FIXME: The use of this function with non-linear skb's really needs | 
|  | 868 | * to be audited. | 
|  | 869 | */ | 
|  | 870 | err = skb_linearize(skb); | 
|  | 871 | if (unlikely(err)) | 
|  | 872 | goto free_skb; | 
|  | 873 |  | 
|  | 874 | memset(skb->data + skb->len, 0, pad); | 
|  | 875 | return 0; | 
|  | 876 |  | 
|  | 877 | free_skb: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | kfree_skb(skb); | 
| Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 879 | return err; | 
| YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 880 | } | 
|  | 881 |  | 
| Ilpo Järvinen | 0dde3e1 | 2008-03-27 17:43:41 -0700 | [diff] [blame] | 882 | /** | 
|  | 883 | *	skb_put - add data to a buffer | 
|  | 884 | *	@skb: buffer to use | 
|  | 885 | *	@len: amount of data to add | 
|  | 886 | * | 
|  | 887 | *	This function extends the used data area of the buffer. If this would | 
|  | 888 | *	exceed the total buffer size the kernel will panic. A pointer to the | 
|  | 889 | *	first byte of the extra data is returned. | 
|  | 890 | */ | 
|  | 891 | unsigned char *skb_put(struct sk_buff *skb, unsigned int len) | 
|  | 892 | { | 
|  | 893 | unsigned char *tmp = skb_tail_pointer(skb); | 
|  | 894 | SKB_LINEAR_ASSERT(skb); | 
|  | 895 | skb->tail += len; | 
|  | 896 | skb->len  += len; | 
|  | 897 | if (unlikely(skb->tail > skb->end)) | 
|  | 898 | skb_over_panic(skb, len, __builtin_return_address(0)); | 
|  | 899 | return tmp; | 
|  | 900 | } | 
|  | 901 | EXPORT_SYMBOL(skb_put); | 
|  | 902 |  | 
| Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 903 | /** | 
| Ilpo Järvinen | c2aa270 | 2008-03-27 17:52:40 -0700 | [diff] [blame] | 904 | *	skb_push - add data to the start of a buffer | 
|  | 905 | *	@skb: buffer to use | 
|  | 906 | *	@len: amount of data to add | 
|  | 907 | * | 
|  | 908 | *	This function extends the used data area of the buffer at the buffer | 
|  | 909 | *	start. If this would exceed the total buffer headroom the kernel will | 
|  | 910 | *	panic. A pointer to the first byte of the extra data is returned. | 
|  | 911 | */ | 
|  | 912 | unsigned char *skb_push(struct sk_buff *skb, unsigned int len) | 
|  | 913 | { | 
|  | 914 | skb->data -= len; | 
|  | 915 | skb->len  += len; | 
|  | 916 | if (unlikely(skb->data<skb->head)) | 
|  | 917 | skb_under_panic(skb, len, __builtin_return_address(0)); | 
|  | 918 | return skb->data; | 
|  | 919 | } | 
|  | 920 | EXPORT_SYMBOL(skb_push); | 
|  | 921 |  | 
|  | 922 | /** | 
| Ilpo Järvinen | 6be8ac2 | 2008-03-27 17:47:24 -0700 | [diff] [blame] | 923 | *	skb_pull - remove data from the start of a buffer | 
|  | 924 | *	@skb: buffer to use | 
|  | 925 | *	@len: amount of data to remove | 
|  | 926 | * | 
|  | 927 | *	This function removes data from the start of a buffer, returning | 
|  | 928 | *	the memory to the headroom. A pointer to the next data in the buffer | 
|  | 929 | *	is returned. Once the data has been pulled future pushes will overwrite | 
|  | 930 | *	the old data. | 
|  | 931 | */ | 
|  | 932 | unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) | 
|  | 933 | { | 
|  | 934 | return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len); | 
|  | 935 | } | 
|  | 936 | EXPORT_SYMBOL(skb_pull); | 
|  | 937 |  | 
| Ilpo Järvinen | 419ae74 | 2008-03-27 17:54:01 -0700 | [diff] [blame] | 938 | /** | 
|  | 939 | *	skb_trim - remove end from a buffer | 
|  | 940 | *	@skb: buffer to alter | 
|  | 941 | *	@len: new length | 
|  | 942 | * | 
|  | 943 | *	Cut the length of a buffer down by removing data from the tail. If | 
|  | 944 | *	the buffer is already under the length specified it is not modified. | 
|  | 945 | *	The skb must be linear. | 
|  | 946 | */ | 
|  | 947 | void skb_trim(struct sk_buff *skb, unsigned int len) | 
|  | 948 | { | 
|  | 949 | if (skb->len > len) | 
|  | 950 | __skb_trim(skb, len); | 
|  | 951 | } | 
|  | 952 | EXPORT_SYMBOL(skb_trim); | 
|  | 953 |  | 
| Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 954 | /* Trims skb to length len. It can change skb pointers. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | */ | 
|  | 956 |  | 
| Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 957 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 959 | struct sk_buff **fragp; | 
|  | 960 | struct sk_buff *frag; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | int offset = skb_headlen(skb); | 
|  | 962 | int nfrags = skb_shinfo(skb)->nr_frags; | 
|  | 963 | int i; | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 964 | int err; | 
|  | 965 |  | 
|  | 966 | if (skb_cloned(skb) && | 
|  | 967 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) | 
|  | 968 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 |  | 
| Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 970 | i = 0; | 
|  | 971 | if (offset >= len) | 
|  | 972 | goto drop_pages; | 
|  | 973 |  | 
|  | 974 | for (; i < nfrags; i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | int end = offset + skb_shinfo(skb)->frags[i].size; | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 976 |  | 
|  | 977 | if (end < len) { | 
|  | 978 | offset = end; | 
|  | 979 | continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 981 |  | 
| Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 982 | skb_shinfo(skb)->frags[i++].size = len - offset; | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 983 |  | 
| Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 984 | drop_pages: | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 985 | skb_shinfo(skb)->nr_frags = i; | 
|  | 986 |  | 
|  | 987 | for (; i < nfrags; i++) | 
|  | 988 | put_page(skb_shinfo(skb)->frags[i].page); | 
|  | 989 |  | 
|  | 990 | if (skb_shinfo(skb)->frag_list) | 
|  | 991 | skb_drop_fraglist(skb); | 
| Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 992 | goto done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | } | 
|  | 994 |  | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 995 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); | 
|  | 996 | fragp = &frag->next) { | 
|  | 997 | int end = offset + frag->len; | 
|  | 998 |  | 
|  | 999 | if (skb_shared(frag)) { | 
|  | 1000 | struct sk_buff *nfrag; | 
|  | 1001 |  | 
|  | 1002 | nfrag = skb_clone(frag, GFP_ATOMIC); | 
|  | 1003 | if (unlikely(!nfrag)) | 
|  | 1004 | return -ENOMEM; | 
|  | 1005 |  | 
|  | 1006 | nfrag->next = frag->next; | 
| Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1007 | kfree_skb(frag); | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1008 | frag = nfrag; | 
|  | 1009 | *fragp = frag; | 
|  | 1010 | } | 
|  | 1011 |  | 
|  | 1012 | if (end < len) { | 
|  | 1013 | offset = end; | 
|  | 1014 | continue; | 
|  | 1015 | } | 
|  | 1016 |  | 
|  | 1017 | if (end > len && | 
|  | 1018 | unlikely((err = pskb_trim(frag, len - offset)))) | 
|  | 1019 | return err; | 
|  | 1020 |  | 
|  | 1021 | if (frag->next) | 
|  | 1022 | skb_drop_list(&frag->next); | 
|  | 1023 | break; | 
|  | 1024 | } | 
|  | 1025 |  | 
| Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 1026 | done: | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1027 | if (len > skb_headlen(skb)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | skb->data_len -= skb->len - len; | 
|  | 1029 | skb->len       = len; | 
|  | 1030 | } else { | 
| Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 1031 | skb->len       = len; | 
|  | 1032 | skb->data_len  = 0; | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1033 | skb_set_tail_pointer(skb, len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } | 
|  | 1035 |  | 
|  | 1036 | return 0; | 
|  | 1037 | } | 
|  | 1038 |  | 
|  | 1039 | /** | 
|  | 1040 | *	__pskb_pull_tail - advance tail of skb header | 
|  | 1041 | *	@skb: buffer to reallocate | 
|  | 1042 | *	@delta: number of bytes to advance tail | 
|  | 1043 | * | 
|  | 1044 | *	The function makes a sense only on a fragmented &sk_buff, | 
|  | 1045 | *	it expands header moving its tail forward and copying necessary | 
|  | 1046 | *	data from fragmented part. | 
|  | 1047 | * | 
|  | 1048 | *	&sk_buff MUST have reference count of 1. | 
|  | 1049 | * | 
|  | 1050 | *	Returns %NULL (and &sk_buff does not change) if pull failed | 
|  | 1051 | *	or value of new tail of skb in the case of success. | 
|  | 1052 | * | 
|  | 1053 | *	All the pointers pointing into skb header may change and must be | 
|  | 1054 | *	reloaded after call to this function. | 
|  | 1055 | */ | 
|  | 1056 |  | 
|  | 1057 | /* Moves tail of skb head forward, copying data from fragmented part, | 
|  | 1058 | * when it is necessary. | 
|  | 1059 | * 1. It may fail due to malloc failure. | 
|  | 1060 | * 2. It may change skb pointers. | 
|  | 1061 | * | 
|  | 1062 | * It is pretty complicated. Luckily, it is called only in exceptional cases. | 
|  | 1063 | */ | 
|  | 1064 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) | 
|  | 1065 | { | 
|  | 1066 | /* If skb has not enough free space at tail, get new one | 
|  | 1067 | * plus 128 bytes for future expansions. If we have enough | 
|  | 1068 | * room at tail, reallocate without expansion only if skb is cloned. | 
|  | 1069 | */ | 
| Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1070 | int i, k, eat = (skb->tail + delta) - skb->end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 |  | 
|  | 1072 | if (eat > 0 || skb_cloned(skb)) { | 
|  | 1073 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, | 
|  | 1074 | GFP_ATOMIC)) | 
|  | 1075 | return NULL; | 
|  | 1076 | } | 
|  | 1077 |  | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1078 | if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | BUG(); | 
|  | 1080 |  | 
|  | 1081 | /* Optimization: no fragments, no reasons to preestimate | 
|  | 1082 | * size of pulled pages. Superb. | 
|  | 1083 | */ | 
|  | 1084 | if (!skb_shinfo(skb)->frag_list) | 
|  | 1085 | goto pull_pages; | 
|  | 1086 |  | 
|  | 1087 | /* Estimate size of pulled pages. */ | 
|  | 1088 | eat = delta; | 
|  | 1089 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
|  | 1090 | if (skb_shinfo(skb)->frags[i].size >= eat) | 
|  | 1091 | goto pull_pages; | 
|  | 1092 | eat -= skb_shinfo(skb)->frags[i].size; | 
|  | 1093 | } | 
|  | 1094 |  | 
|  | 1095 | /* If we need update frag list, we are in troubles. | 
|  | 1096 | * Certainly, it possible to add an offset to skb data, | 
|  | 1097 | * but taking into account that pulling is expected to | 
|  | 1098 | * be very rare operation, it is worth to fight against | 
|  | 1099 | * further bloating skb head and crucify ourselves here instead. | 
|  | 1100 | * Pure masohism, indeed. 8)8) | 
|  | 1101 | */ | 
|  | 1102 | if (eat) { | 
|  | 1103 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | 
|  | 1104 | struct sk_buff *clone = NULL; | 
|  | 1105 | struct sk_buff *insp = NULL; | 
|  | 1106 |  | 
|  | 1107 | do { | 
| Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1108 | BUG_ON(!list); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 |  | 
|  | 1110 | if (list->len <= eat) { | 
|  | 1111 | /* Eaten as whole. */ | 
|  | 1112 | eat -= list->len; | 
|  | 1113 | list = list->next; | 
|  | 1114 | insp = list; | 
|  | 1115 | } else { | 
|  | 1116 | /* Eaten partially. */ | 
|  | 1117 |  | 
|  | 1118 | if (skb_shared(list)) { | 
|  | 1119 | /* Sucks! We need to fork list. :-( */ | 
|  | 1120 | clone = skb_clone(list, GFP_ATOMIC); | 
|  | 1121 | if (!clone) | 
|  | 1122 | return NULL; | 
|  | 1123 | insp = list->next; | 
|  | 1124 | list = clone; | 
|  | 1125 | } else { | 
|  | 1126 | /* This may be pulled without | 
|  | 1127 | * problems. */ | 
|  | 1128 | insp = list; | 
|  | 1129 | } | 
|  | 1130 | if (!pskb_pull(list, eat)) { | 
|  | 1131 | if (clone) | 
|  | 1132 | kfree_skb(clone); | 
|  | 1133 | return NULL; | 
|  | 1134 | } | 
|  | 1135 | break; | 
|  | 1136 | } | 
|  | 1137 | } while (eat); | 
|  | 1138 |  | 
|  | 1139 | /* Free pulled out fragments. */ | 
|  | 1140 | while ((list = skb_shinfo(skb)->frag_list) != insp) { | 
|  | 1141 | skb_shinfo(skb)->frag_list = list->next; | 
|  | 1142 | kfree_skb(list); | 
|  | 1143 | } | 
|  | 1144 | /* And insert new clone at head. */ | 
|  | 1145 | if (clone) { | 
|  | 1146 | clone->next = list; | 
|  | 1147 | skb_shinfo(skb)->frag_list = clone; | 
|  | 1148 | } | 
|  | 1149 | } | 
|  | 1150 | /* Success! Now we may commit changes to skb data. */ | 
|  | 1151 |  | 
|  | 1152 | pull_pages: | 
|  | 1153 | eat = delta; | 
|  | 1154 | k = 0; | 
|  | 1155 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
|  | 1156 | if (skb_shinfo(skb)->frags[i].size <= eat) { | 
|  | 1157 | put_page(skb_shinfo(skb)->frags[i].page); | 
|  | 1158 | eat -= skb_shinfo(skb)->frags[i].size; | 
|  | 1159 | } else { | 
|  | 1160 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; | 
|  | 1161 | if (eat) { | 
|  | 1162 | skb_shinfo(skb)->frags[k].page_offset += eat; | 
|  | 1163 | skb_shinfo(skb)->frags[k].size -= eat; | 
|  | 1164 | eat = 0; | 
|  | 1165 | } | 
|  | 1166 | k++; | 
|  | 1167 | } | 
|  | 1168 | } | 
|  | 1169 | skb_shinfo(skb)->nr_frags = k; | 
|  | 1170 |  | 
|  | 1171 | skb->tail     += delta; | 
|  | 1172 | skb->data_len -= delta; | 
|  | 1173 |  | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1174 | return skb_tail_pointer(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | } | 
|  | 1176 |  | 
|  | 1177 | /* Copy some data bits from skb to kernel buffer. */ | 
|  | 1178 |  | 
|  | 1179 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) | 
|  | 1180 | { | 
|  | 1181 | int i, copy; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1182 | int start = skb_headlen(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 |  | 
|  | 1184 | if (offset > (int)skb->len - len) | 
|  | 1185 | goto fault; | 
|  | 1186 |  | 
|  | 1187 | /* Copy header. */ | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1188 | if ((copy = start - offset) > 0) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | if (copy > len) | 
|  | 1190 | copy = len; | 
| Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1191 | skb_copy_from_linear_data_offset(skb, offset, to, copy); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | if ((len -= copy) == 0) | 
|  | 1193 | return 0; | 
|  | 1194 | offset += copy; | 
|  | 1195 | to     += copy; | 
|  | 1196 | } | 
|  | 1197 |  | 
|  | 1198 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1199 | int end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1201 | BUG_TRAP(start <= offset + len); | 
|  | 1202 |  | 
|  | 1203 | end = start + skb_shinfo(skb)->frags[i].size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | if ((copy = end - offset) > 0) { | 
|  | 1205 | u8 *vaddr; | 
|  | 1206 |  | 
|  | 1207 | if (copy > len) | 
|  | 1208 | copy = len; | 
|  | 1209 |  | 
|  | 1210 | vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); | 
|  | 1211 | memcpy(to, | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1212 | vaddr + skb_shinfo(skb)->frags[i].page_offset+ | 
|  | 1213 | offset - start, copy); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | kunmap_skb_frag(vaddr); | 
|  | 1215 |  | 
|  | 1216 | if ((len -= copy) == 0) | 
|  | 1217 | return 0; | 
|  | 1218 | offset += copy; | 
|  | 1219 | to     += copy; | 
|  | 1220 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1221 | start = end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | if (skb_shinfo(skb)->frag_list) { | 
|  | 1225 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | 
|  | 1226 |  | 
|  | 1227 | for (; list; list = list->next) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1228 | int end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1230 | BUG_TRAP(start <= offset + len); | 
|  | 1231 |  | 
|  | 1232 | end = start + list->len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | if ((copy = end - offset) > 0) { | 
|  | 1234 | if (copy > len) | 
|  | 1235 | copy = len; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1236 | if (skb_copy_bits(list, offset - start, | 
|  | 1237 | to, copy)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | goto fault; | 
|  | 1239 | if ((len -= copy) == 0) | 
|  | 1240 | return 0; | 
|  | 1241 | offset += copy; | 
|  | 1242 | to     += copy; | 
|  | 1243 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1244 | start = end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | } | 
|  | 1246 | } | 
|  | 1247 | if (!len) | 
|  | 1248 | return 0; | 
|  | 1249 |  | 
|  | 1250 | fault: | 
|  | 1251 | return -EFAULT; | 
|  | 1252 | } | 
|  | 1253 |  | 
| Jens Axboe | 9c55e01 | 2007-11-06 23:30:13 -0800 | [diff] [blame] | 1254 | /* | 
|  | 1255 | * Callback from splice_to_pipe(), if we need to release some pages | 
|  | 1256 | * at the end of the spd in case we error'ed out in filling the pipe. | 
|  | 1257 | */ | 
|  | 1258 | static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) | 
|  | 1259 | { | 
|  | 1260 | struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private; | 
|  | 1261 |  | 
|  | 1262 | kfree_skb(skb); | 
|  | 1263 | } | 
|  | 1264 |  | 
|  | 1265 | /* | 
|  | 1266 | * Fill page/offset/length into spd, if it can hold more pages. | 
|  | 1267 | */ | 
|  | 1268 | static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page, | 
|  | 1269 | unsigned int len, unsigned int offset, | 
|  | 1270 | struct sk_buff *skb) | 
|  | 1271 | { | 
|  | 1272 | if (unlikely(spd->nr_pages == PIPE_BUFFERS)) | 
|  | 1273 | return 1; | 
|  | 1274 |  | 
|  | 1275 | spd->pages[spd->nr_pages] = page; | 
|  | 1276 | spd->partial[spd->nr_pages].len = len; | 
|  | 1277 | spd->partial[spd->nr_pages].offset = offset; | 
|  | 1278 | spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb); | 
|  | 1279 | spd->nr_pages++; | 
|  | 1280 | return 0; | 
|  | 1281 | } | 
|  | 1282 |  | 
|  | 1283 | /* | 
|  | 1284 | * Map linear and fragment data from the skb to spd. Returns number of | 
|  | 1285 | * pages mapped. | 
|  | 1286 | */ | 
|  | 1287 | static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset, | 
|  | 1288 | unsigned int *total_len, | 
|  | 1289 | struct splice_pipe_desc *spd) | 
|  | 1290 | { | 
|  | 1291 | unsigned int nr_pages = spd->nr_pages; | 
|  | 1292 | unsigned int poff, plen, len, toff, tlen; | 
|  | 1293 | int headlen, seg; | 
|  | 1294 |  | 
|  | 1295 | toff = *offset; | 
|  | 1296 | tlen = *total_len; | 
|  | 1297 | if (!tlen) | 
|  | 1298 | goto err; | 
|  | 1299 |  | 
|  | 1300 | /* | 
|  | 1301 | * if the offset is greater than the linear part, go directly to | 
|  | 1302 | * the fragments. | 
|  | 1303 | */ | 
|  | 1304 | headlen = skb_headlen(skb); | 
|  | 1305 | if (toff >= headlen) { | 
|  | 1306 | toff -= headlen; | 
|  | 1307 | goto map_frag; | 
|  | 1308 | } | 
|  | 1309 |  | 
|  | 1310 | /* | 
|  | 1311 | * first map the linear region into the pages/partial map, skipping | 
|  | 1312 | * any potential initial offset. | 
|  | 1313 | */ | 
|  | 1314 | len = 0; | 
|  | 1315 | while (len < headlen) { | 
|  | 1316 | void *p = skb->data + len; | 
|  | 1317 |  | 
|  | 1318 | poff = (unsigned long) p & (PAGE_SIZE - 1); | 
|  | 1319 | plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff); | 
|  | 1320 | len += plen; | 
|  | 1321 |  | 
|  | 1322 | if (toff) { | 
|  | 1323 | if (plen <= toff) { | 
|  | 1324 | toff -= plen; | 
|  | 1325 | continue; | 
|  | 1326 | } | 
|  | 1327 | plen -= toff; | 
|  | 1328 | poff += toff; | 
|  | 1329 | toff = 0; | 
|  | 1330 | } | 
|  | 1331 |  | 
|  | 1332 | plen = min(plen, tlen); | 
|  | 1333 | if (!plen) | 
|  | 1334 | break; | 
|  | 1335 |  | 
|  | 1336 | /* | 
|  | 1337 | * just jump directly to update and return, no point | 
|  | 1338 | * in going over fragments when the output is full. | 
|  | 1339 | */ | 
|  | 1340 | if (spd_fill_page(spd, virt_to_page(p), plen, poff, skb)) | 
|  | 1341 | goto done; | 
|  | 1342 |  | 
|  | 1343 | tlen -= plen; | 
|  | 1344 | } | 
|  | 1345 |  | 
|  | 1346 | /* | 
|  | 1347 | * then map the fragments | 
|  | 1348 | */ | 
|  | 1349 | map_frag: | 
|  | 1350 | for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { | 
|  | 1351 | const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; | 
|  | 1352 |  | 
|  | 1353 | plen = f->size; | 
|  | 1354 | poff = f->page_offset; | 
|  | 1355 |  | 
|  | 1356 | if (toff) { | 
|  | 1357 | if (plen <= toff) { | 
|  | 1358 | toff -= plen; | 
|  | 1359 | continue; | 
|  | 1360 | } | 
|  | 1361 | plen -= toff; | 
|  | 1362 | poff += toff; | 
|  | 1363 | toff = 0; | 
|  | 1364 | } | 
|  | 1365 |  | 
|  | 1366 | plen = min(plen, tlen); | 
|  | 1367 | if (!plen) | 
|  | 1368 | break; | 
|  | 1369 |  | 
|  | 1370 | if (spd_fill_page(spd, f->page, plen, poff, skb)) | 
|  | 1371 | break; | 
|  | 1372 |  | 
|  | 1373 | tlen -= plen; | 
|  | 1374 | } | 
|  | 1375 |  | 
|  | 1376 | done: | 
|  | 1377 | if (spd->nr_pages - nr_pages) { | 
|  | 1378 | *offset = 0; | 
|  | 1379 | *total_len = tlen; | 
|  | 1380 | return 0; | 
|  | 1381 | } | 
|  | 1382 | err: | 
|  | 1383 | return 1; | 
|  | 1384 | } | 
|  | 1385 |  | 
|  | 1386 | /* | 
|  | 1387 | * Map data from the skb to a pipe. Should handle both the linear part, | 
|  | 1388 | * the fragments, and the frag list. It does NOT handle frag lists within | 
|  | 1389 | * the frag list, if such a thing exists. We'd probably need to recurse to | 
|  | 1390 | * handle that cleanly. | 
|  | 1391 | */ | 
|  | 1392 | int skb_splice_bits(struct sk_buff *__skb, unsigned int offset, | 
|  | 1393 | struct pipe_inode_info *pipe, unsigned int tlen, | 
|  | 1394 | unsigned int flags) | 
|  | 1395 | { | 
|  | 1396 | struct partial_page partial[PIPE_BUFFERS]; | 
|  | 1397 | struct page *pages[PIPE_BUFFERS]; | 
|  | 1398 | struct splice_pipe_desc spd = { | 
|  | 1399 | .pages = pages, | 
|  | 1400 | .partial = partial, | 
|  | 1401 | .flags = flags, | 
|  | 1402 | .ops = &sock_pipe_buf_ops, | 
|  | 1403 | .spd_release = sock_spd_release, | 
|  | 1404 | }; | 
|  | 1405 | struct sk_buff *skb; | 
|  | 1406 |  | 
|  | 1407 | /* | 
|  | 1408 | * I'd love to avoid the clone here, but tcp_read_sock() | 
|  | 1409 | * ignores reference counts and unconditonally kills the sk_buff | 
|  | 1410 | * on return from the actor. | 
|  | 1411 | */ | 
|  | 1412 | skb = skb_clone(__skb, GFP_KERNEL); | 
|  | 1413 | if (unlikely(!skb)) | 
|  | 1414 | return -ENOMEM; | 
|  | 1415 |  | 
|  | 1416 | /* | 
|  | 1417 | * __skb_splice_bits() only fails if the output has no room left, | 
|  | 1418 | * so no point in going over the frag_list for the error case. | 
|  | 1419 | */ | 
|  | 1420 | if (__skb_splice_bits(skb, &offset, &tlen, &spd)) | 
|  | 1421 | goto done; | 
|  | 1422 | else if (!tlen) | 
|  | 1423 | goto done; | 
|  | 1424 |  | 
|  | 1425 | /* | 
|  | 1426 | * now see if we have a frag_list to map | 
|  | 1427 | */ | 
|  | 1428 | if (skb_shinfo(skb)->frag_list) { | 
|  | 1429 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | 
|  | 1430 |  | 
|  | 1431 | for (; list && tlen; list = list->next) { | 
|  | 1432 | if (__skb_splice_bits(list, &offset, &tlen, &spd)) | 
|  | 1433 | break; | 
|  | 1434 | } | 
|  | 1435 | } | 
|  | 1436 |  | 
|  | 1437 | done: | 
|  | 1438 | /* | 
|  | 1439 | * drop our reference to the clone, the pipe consumption will | 
|  | 1440 | * drop the rest. | 
|  | 1441 | */ | 
|  | 1442 | kfree_skb(skb); | 
|  | 1443 |  | 
|  | 1444 | if (spd.nr_pages) { | 
|  | 1445 | int ret; | 
|  | 1446 |  | 
|  | 1447 | /* | 
|  | 1448 | * Drop the socket lock, otherwise we have reverse | 
|  | 1449 | * locking dependencies between sk_lock and i_mutex | 
|  | 1450 | * here as compared to sendfile(). We enter here | 
|  | 1451 | * with the socket lock held, and splice_to_pipe() will | 
|  | 1452 | * grab the pipe inode lock. For sendfile() emulation, | 
|  | 1453 | * we call into ->sendpage() with the i_mutex lock held | 
|  | 1454 | * and networking will grab the socket lock. | 
|  | 1455 | */ | 
|  | 1456 | release_sock(__skb->sk); | 
|  | 1457 | ret = splice_to_pipe(pipe, &spd); | 
|  | 1458 | lock_sock(__skb->sk); | 
|  | 1459 | return ret; | 
|  | 1460 | } | 
|  | 1461 |  | 
|  | 1462 | return 0; | 
|  | 1463 | } | 
|  | 1464 |  | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1465 | /** | 
|  | 1466 | *	skb_store_bits - store bits from kernel buffer to skb | 
|  | 1467 | *	@skb: destination buffer | 
|  | 1468 | *	@offset: offset in destination | 
|  | 1469 | *	@from: source buffer | 
|  | 1470 | *	@len: number of bytes to copy | 
|  | 1471 | * | 
|  | 1472 | *	Copy the specified number of bytes from the source buffer to the | 
|  | 1473 | *	destination skb.  This function handles all the messy bits of | 
|  | 1474 | *	traversing fragment lists and such. | 
|  | 1475 | */ | 
|  | 1476 |  | 
| Stephen Hemminger | 0c6fcc8 | 2007-04-20 16:40:01 -0700 | [diff] [blame] | 1477 | int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1478 | { | 
|  | 1479 | int i, copy; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1480 | int start = skb_headlen(skb); | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1481 |  | 
|  | 1482 | if (offset > (int)skb->len - len) | 
|  | 1483 | goto fault; | 
|  | 1484 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1485 | if ((copy = start - offset) > 0) { | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1486 | if (copy > len) | 
|  | 1487 | copy = len; | 
| Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 1488 | skb_copy_to_linear_data_offset(skb, offset, from, copy); | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1489 | if ((len -= copy) == 0) | 
|  | 1490 | return 0; | 
|  | 1491 | offset += copy; | 
|  | 1492 | from += copy; | 
|  | 1493 | } | 
|  | 1494 |  | 
|  | 1495 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
|  | 1496 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1497 | int end; | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1498 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1499 | BUG_TRAP(start <= offset + len); | 
|  | 1500 |  | 
|  | 1501 | end = start + frag->size; | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1502 | if ((copy = end - offset) > 0) { | 
|  | 1503 | u8 *vaddr; | 
|  | 1504 |  | 
|  | 1505 | if (copy > len) | 
|  | 1506 | copy = len; | 
|  | 1507 |  | 
|  | 1508 | vaddr = kmap_skb_frag(frag); | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1509 | memcpy(vaddr + frag->page_offset + offset - start, | 
|  | 1510 | from, copy); | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1511 | kunmap_skb_frag(vaddr); | 
|  | 1512 |  | 
|  | 1513 | if ((len -= copy) == 0) | 
|  | 1514 | return 0; | 
|  | 1515 | offset += copy; | 
|  | 1516 | from += copy; | 
|  | 1517 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1518 | start = end; | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1519 | } | 
|  | 1520 |  | 
|  | 1521 | if (skb_shinfo(skb)->frag_list) { | 
|  | 1522 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | 
|  | 1523 |  | 
|  | 1524 | for (; list; list = list->next) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1525 | int end; | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1526 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1527 | BUG_TRAP(start <= offset + len); | 
|  | 1528 |  | 
|  | 1529 | end = start + list->len; | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1530 | if ((copy = end - offset) > 0) { | 
|  | 1531 | if (copy > len) | 
|  | 1532 | copy = len; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1533 | if (skb_store_bits(list, offset - start, | 
|  | 1534 | from, copy)) | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1535 | goto fault; | 
|  | 1536 | if ((len -= copy) == 0) | 
|  | 1537 | return 0; | 
|  | 1538 | offset += copy; | 
|  | 1539 | from += copy; | 
|  | 1540 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1541 | start = end; | 
| Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1542 | } | 
|  | 1543 | } | 
|  | 1544 | if (!len) | 
|  | 1545 | return 0; | 
|  | 1546 |  | 
|  | 1547 | fault: | 
|  | 1548 | return -EFAULT; | 
|  | 1549 | } | 
|  | 1550 |  | 
|  | 1551 | EXPORT_SYMBOL(skb_store_bits); | 
|  | 1552 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | /* Checksum skb data. */ | 
|  | 1554 |  | 
| Al Viro | 2bbbc86 | 2006-11-14 21:37:14 -0800 | [diff] [blame] | 1555 | __wsum skb_checksum(const struct sk_buff *skb, int offset, | 
|  | 1556 | int len, __wsum csum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1558 | int start = skb_headlen(skb); | 
|  | 1559 | int i, copy = start - offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | int pos = 0; | 
|  | 1561 |  | 
|  | 1562 | /* Checksum header. */ | 
|  | 1563 | if (copy > 0) { | 
|  | 1564 | if (copy > len) | 
|  | 1565 | copy = len; | 
|  | 1566 | csum = csum_partial(skb->data + offset, copy, csum); | 
|  | 1567 | if ((len -= copy) == 0) | 
|  | 1568 | return csum; | 
|  | 1569 | offset += copy; | 
|  | 1570 | pos	= copy; | 
|  | 1571 | } | 
|  | 1572 |  | 
|  | 1573 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1574 | int end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1576 | BUG_TRAP(start <= offset + len); | 
|  | 1577 |  | 
|  | 1578 | end = start + skb_shinfo(skb)->frags[i].size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | if ((copy = end - offset) > 0) { | 
| Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame] | 1580 | __wsum csum2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | u8 *vaddr; | 
|  | 1582 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 
|  | 1583 |  | 
|  | 1584 | if (copy > len) | 
|  | 1585 | copy = len; | 
|  | 1586 | vaddr = kmap_skb_frag(frag); | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1587 | csum2 = csum_partial(vaddr + frag->page_offset + | 
|  | 1588 | offset - start, copy, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | kunmap_skb_frag(vaddr); | 
|  | 1590 | csum = csum_block_add(csum, csum2, pos); | 
|  | 1591 | if (!(len -= copy)) | 
|  | 1592 | return csum; | 
|  | 1593 | offset += copy; | 
|  | 1594 | pos    += copy; | 
|  | 1595 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1596 | start = end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } | 
|  | 1598 |  | 
|  | 1599 | if (skb_shinfo(skb)->frag_list) { | 
|  | 1600 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | 
|  | 1601 |  | 
|  | 1602 | for (; list; list = list->next) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1603 | int end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1605 | BUG_TRAP(start <= offset + len); | 
|  | 1606 |  | 
|  | 1607 | end = start + list->len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | if ((copy = end - offset) > 0) { | 
| Al Viro | 5f92a73 | 2006-11-14 21:36:54 -0800 | [diff] [blame] | 1609 | __wsum csum2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | if (copy > len) | 
|  | 1611 | copy = len; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1612 | csum2 = skb_checksum(list, offset - start, | 
|  | 1613 | copy, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | csum = csum_block_add(csum, csum2, pos); | 
|  | 1615 | if ((len -= copy) == 0) | 
|  | 1616 | return csum; | 
|  | 1617 | offset += copy; | 
|  | 1618 | pos    += copy; | 
|  | 1619 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1620 | start = end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | } | 
|  | 1622 | } | 
| Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1623 | BUG_ON(len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 |  | 
|  | 1625 | return csum; | 
|  | 1626 | } | 
|  | 1627 |  | 
|  | 1628 | /* Both of above in one bottle. */ | 
|  | 1629 |  | 
| Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 1630 | __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, | 
|  | 1631 | u8 *to, int len, __wsum csum) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1633 | int start = skb_headlen(skb); | 
|  | 1634 | int i, copy = start - offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | int pos = 0; | 
|  | 1636 |  | 
|  | 1637 | /* Copy header. */ | 
|  | 1638 | if (copy > 0) { | 
|  | 1639 | if (copy > len) | 
|  | 1640 | copy = len; | 
|  | 1641 | csum = csum_partial_copy_nocheck(skb->data + offset, to, | 
|  | 1642 | copy, csum); | 
|  | 1643 | if ((len -= copy) == 0) | 
|  | 1644 | return csum; | 
|  | 1645 | offset += copy; | 
|  | 1646 | to     += copy; | 
|  | 1647 | pos	= copy; | 
|  | 1648 | } | 
|  | 1649 |  | 
|  | 1650 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1651 | int end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1653 | BUG_TRAP(start <= offset + len); | 
|  | 1654 |  | 
|  | 1655 | end = start + skb_shinfo(skb)->frags[i].size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | if ((copy = end - offset) > 0) { | 
| Al Viro | 5084205 | 2006-11-14 21:36:34 -0800 | [diff] [blame] | 1657 | __wsum csum2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | u8 *vaddr; | 
|  | 1659 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 
|  | 1660 |  | 
|  | 1661 | if (copy > len) | 
|  | 1662 | copy = len; | 
|  | 1663 | vaddr = kmap_skb_frag(frag); | 
|  | 1664 | csum2 = csum_partial_copy_nocheck(vaddr + | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1665 | frag->page_offset + | 
|  | 1666 | offset - start, to, | 
|  | 1667 | copy, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | kunmap_skb_frag(vaddr); | 
|  | 1669 | csum = csum_block_add(csum, csum2, pos); | 
|  | 1670 | if (!(len -= copy)) | 
|  | 1671 | return csum; | 
|  | 1672 | offset += copy; | 
|  | 1673 | to     += copy; | 
|  | 1674 | pos    += copy; | 
|  | 1675 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1676 | start = end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | } | 
|  | 1678 |  | 
|  | 1679 | if (skb_shinfo(skb)->frag_list) { | 
|  | 1680 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | 
|  | 1681 |  | 
|  | 1682 | for (; list; list = list->next) { | 
| Al Viro | 81d7766 | 2006-11-14 21:37:33 -0800 | [diff] [blame] | 1683 | __wsum csum2; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1684 | int end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1686 | BUG_TRAP(start <= offset + len); | 
|  | 1687 |  | 
|  | 1688 | end = start + list->len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | if ((copy = end - offset) > 0) { | 
|  | 1690 | if (copy > len) | 
|  | 1691 | copy = len; | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1692 | csum2 = skb_copy_and_csum_bits(list, | 
|  | 1693 | offset - start, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | to, copy, 0); | 
|  | 1695 | csum = csum_block_add(csum, csum2, pos); | 
|  | 1696 | if ((len -= copy) == 0) | 
|  | 1697 | return csum; | 
|  | 1698 | offset += copy; | 
|  | 1699 | to     += copy; | 
|  | 1700 | pos    += copy; | 
|  | 1701 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 1702 | start = end; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1703 | } | 
|  | 1704 | } | 
| Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1705 | BUG_ON(len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | return csum; | 
|  | 1707 | } | 
|  | 1708 |  | 
|  | 1709 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) | 
|  | 1710 | { | 
| Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 1711 | __wsum csum; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | long csstart; | 
|  | 1713 |  | 
| Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1714 | if (skb->ip_summed == CHECKSUM_PARTIAL) | 
| Herbert Xu | 663ead3 | 2007-04-09 11:59:07 -0700 | [diff] [blame] | 1715 | csstart = skb->csum_start - skb_headroom(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | else | 
|  | 1717 | csstart = skb_headlen(skb); | 
|  | 1718 |  | 
| Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1719 | BUG_ON(csstart > skb_headlen(skb)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 |  | 
| Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1721 | skb_copy_from_linear_data(skb, to, csstart); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 |  | 
|  | 1723 | csum = 0; | 
|  | 1724 | if (csstart != skb->len) | 
|  | 1725 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, | 
|  | 1726 | skb->len - csstart, 0); | 
|  | 1727 |  | 
| Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1728 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | 
| Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 1729 | long csstuff = csstart + skb->csum_offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 |  | 
| Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 1731 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | } | 
|  | 1733 | } | 
|  | 1734 |  | 
|  | 1735 | /** | 
|  | 1736 | *	skb_dequeue - remove from the head of the queue | 
|  | 1737 | *	@list: list to dequeue from | 
|  | 1738 | * | 
|  | 1739 | *	Remove the head of the list. The list lock is taken so the function | 
|  | 1740 | *	may be used safely with other locking list functions. The head item is | 
|  | 1741 | *	returned or %NULL if the list is empty. | 
|  | 1742 | */ | 
|  | 1743 |  | 
|  | 1744 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) | 
|  | 1745 | { | 
|  | 1746 | unsigned long flags; | 
|  | 1747 | struct sk_buff *result; | 
|  | 1748 |  | 
|  | 1749 | spin_lock_irqsave(&list->lock, flags); | 
|  | 1750 | result = __skb_dequeue(list); | 
|  | 1751 | spin_unlock_irqrestore(&list->lock, flags); | 
|  | 1752 | return result; | 
|  | 1753 | } | 
|  | 1754 |  | 
|  | 1755 | /** | 
|  | 1756 | *	skb_dequeue_tail - remove from the tail of the queue | 
|  | 1757 | *	@list: list to dequeue from | 
|  | 1758 | * | 
|  | 1759 | *	Remove the tail of the list. The list lock is taken so the function | 
|  | 1760 | *	may be used safely with other locking list functions. The tail item is | 
|  | 1761 | *	returned or %NULL if the list is empty. | 
|  | 1762 | */ | 
|  | 1763 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) | 
|  | 1764 | { | 
|  | 1765 | unsigned long flags; | 
|  | 1766 | struct sk_buff *result; | 
|  | 1767 |  | 
|  | 1768 | spin_lock_irqsave(&list->lock, flags); | 
|  | 1769 | result = __skb_dequeue_tail(list); | 
|  | 1770 | spin_unlock_irqrestore(&list->lock, flags); | 
|  | 1771 | return result; | 
|  | 1772 | } | 
|  | 1773 |  | 
|  | 1774 | /** | 
|  | 1775 | *	skb_queue_purge - empty a list | 
|  | 1776 | *	@list: list to empty | 
|  | 1777 | * | 
|  | 1778 | *	Delete all buffers on an &sk_buff list. Each buffer is removed from | 
|  | 1779 | *	the list and one reference dropped. This function takes the list | 
|  | 1780 | *	lock and is atomic with respect to other list locking functions. | 
|  | 1781 | */ | 
|  | 1782 | void skb_queue_purge(struct sk_buff_head *list) | 
|  | 1783 | { | 
|  | 1784 | struct sk_buff *skb; | 
|  | 1785 | while ((skb = skb_dequeue(list)) != NULL) | 
|  | 1786 | kfree_skb(skb); | 
|  | 1787 | } | 
|  | 1788 |  | 
|  | 1789 | /** | 
|  | 1790 | *	skb_queue_head - queue a buffer at the list head | 
|  | 1791 | *	@list: list to use | 
|  | 1792 | *	@newsk: buffer to queue | 
|  | 1793 | * | 
|  | 1794 | *	Queue a buffer at the start of the list. This function takes the | 
|  | 1795 | *	list lock and can be used safely with other locking &sk_buff functions | 
|  | 1796 | *	safely. | 
|  | 1797 | * | 
|  | 1798 | *	A buffer cannot be placed on two lists at the same time. | 
|  | 1799 | */ | 
|  | 1800 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) | 
|  | 1801 | { | 
|  | 1802 | unsigned long flags; | 
|  | 1803 |  | 
|  | 1804 | spin_lock_irqsave(&list->lock, flags); | 
|  | 1805 | __skb_queue_head(list, newsk); | 
|  | 1806 | spin_unlock_irqrestore(&list->lock, flags); | 
|  | 1807 | } | 
|  | 1808 |  | 
|  | 1809 | /** | 
|  | 1810 | *	skb_queue_tail - queue a buffer at the list tail | 
|  | 1811 | *	@list: list to use | 
|  | 1812 | *	@newsk: buffer to queue | 
|  | 1813 | * | 
|  | 1814 | *	Queue a buffer at the tail of the list. This function takes the | 
|  | 1815 | *	list lock and can be used safely with other locking &sk_buff functions | 
|  | 1816 | *	safely. | 
|  | 1817 | * | 
|  | 1818 | *	A buffer cannot be placed on two lists at the same time. | 
|  | 1819 | */ | 
|  | 1820 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) | 
|  | 1821 | { | 
|  | 1822 | unsigned long flags; | 
|  | 1823 |  | 
|  | 1824 | spin_lock_irqsave(&list->lock, flags); | 
|  | 1825 | __skb_queue_tail(list, newsk); | 
|  | 1826 | spin_unlock_irqrestore(&list->lock, flags); | 
|  | 1827 | } | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1828 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | /** | 
|  | 1830 | *	skb_unlink	-	remove a buffer from a list | 
|  | 1831 | *	@skb: buffer to remove | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1832 | *	@list: list to use | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | * | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1834 | *	Remove a packet from a list. The list locks are taken and this | 
|  | 1835 | *	function is atomic with respect to other list locked calls | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | * | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1837 | *	You must know what list the SKB is on. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | */ | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1839 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | { | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1841 | unsigned long flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 |  | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1843 | spin_lock_irqsave(&list->lock, flags); | 
|  | 1844 | __skb_unlink(skb, list); | 
|  | 1845 | spin_unlock_irqrestore(&list->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | } | 
|  | 1847 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | /** | 
|  | 1849 | *	skb_append	-	append a buffer | 
|  | 1850 | *	@old: buffer to insert after | 
|  | 1851 | *	@newsk: buffer to insert | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1852 | *	@list: list to use | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | * | 
|  | 1854 | *	Place a packet after a given packet in a list. The list locks are taken | 
|  | 1855 | *	and this function is atomic with respect to other list locked calls. | 
|  | 1856 | *	A buffer cannot be placed on two lists at the same time. | 
|  | 1857 | */ | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1858 | void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | { | 
|  | 1860 | unsigned long flags; | 
|  | 1861 |  | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1862 | spin_lock_irqsave(&list->lock, flags); | 
| Gerrit Renker | 7de6c03 | 2008-04-14 00:05:09 -0700 | [diff] [blame] | 1863 | __skb_queue_after(list, old, newsk); | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1864 | spin_unlock_irqrestore(&list->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | } | 
|  | 1866 |  | 
|  | 1867 |  | 
|  | 1868 | /** | 
|  | 1869 | *	skb_insert	-	insert a buffer | 
|  | 1870 | *	@old: buffer to insert before | 
|  | 1871 | *	@newsk: buffer to insert | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1872 | *	@list: list to use | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | * | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1874 | *	Place a packet before a given packet in a list. The list locks are | 
|  | 1875 | * 	taken and this function is atomic with respect to other list locked | 
|  | 1876 | *	calls. | 
|  | 1877 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | *	A buffer cannot be placed on two lists at the same time. | 
|  | 1879 | */ | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1880 | void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | { | 
|  | 1882 | unsigned long flags; | 
|  | 1883 |  | 
| David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1884 | spin_lock_irqsave(&list->lock, flags); | 
|  | 1885 | __skb_insert(newsk, old->prev, old, list); | 
|  | 1886 | spin_unlock_irqrestore(&list->lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | } | 
|  | 1888 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | static inline void skb_split_inside_header(struct sk_buff *skb, | 
|  | 1890 | struct sk_buff* skb1, | 
|  | 1891 | const u32 len, const int pos) | 
|  | 1892 | { | 
|  | 1893 | int i; | 
|  | 1894 |  | 
| Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 1895 | skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), | 
|  | 1896 | pos - len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | /* And move data appendix as is. */ | 
|  | 1898 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) | 
|  | 1899 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; | 
|  | 1900 |  | 
|  | 1901 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; | 
|  | 1902 | skb_shinfo(skb)->nr_frags  = 0; | 
|  | 1903 | skb1->data_len		   = skb->data_len; | 
|  | 1904 | skb1->len		   += skb1->data_len; | 
|  | 1905 | skb->data_len		   = 0; | 
|  | 1906 | skb->len		   = len; | 
| Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1907 | skb_set_tail_pointer(skb, len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | } | 
|  | 1909 |  | 
|  | 1910 | static inline void skb_split_no_header(struct sk_buff *skb, | 
|  | 1911 | struct sk_buff* skb1, | 
|  | 1912 | const u32 len, int pos) | 
|  | 1913 | { | 
|  | 1914 | int i, k = 0; | 
|  | 1915 | const int nfrags = skb_shinfo(skb)->nr_frags; | 
|  | 1916 |  | 
|  | 1917 | skb_shinfo(skb)->nr_frags = 0; | 
|  | 1918 | skb1->len		  = skb1->data_len = skb->len - len; | 
|  | 1919 | skb->len		  = len; | 
|  | 1920 | skb->data_len		  = len - pos; | 
|  | 1921 |  | 
|  | 1922 | for (i = 0; i < nfrags; i++) { | 
|  | 1923 | int size = skb_shinfo(skb)->frags[i].size; | 
|  | 1924 |  | 
|  | 1925 | if (pos + size > len) { | 
|  | 1926 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; | 
|  | 1927 |  | 
|  | 1928 | if (pos < len) { | 
|  | 1929 | /* Split frag. | 
|  | 1930 | * We have two variants in this case: | 
|  | 1931 | * 1. Move all the frag to the second | 
|  | 1932 | *    part, if it is possible. F.e. | 
|  | 1933 | *    this approach is mandatory for TUX, | 
|  | 1934 | *    where splitting is expensive. | 
|  | 1935 | * 2. Split is accurately. We make this. | 
|  | 1936 | */ | 
|  | 1937 | get_page(skb_shinfo(skb)->frags[i].page); | 
|  | 1938 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; | 
|  | 1939 | skb_shinfo(skb1)->frags[0].size -= len - pos; | 
|  | 1940 | skb_shinfo(skb)->frags[i].size	= len - pos; | 
|  | 1941 | skb_shinfo(skb)->nr_frags++; | 
|  | 1942 | } | 
|  | 1943 | k++; | 
|  | 1944 | } else | 
|  | 1945 | skb_shinfo(skb)->nr_frags++; | 
|  | 1946 | pos += size; | 
|  | 1947 | } | 
|  | 1948 | skb_shinfo(skb1)->nr_frags = k; | 
|  | 1949 | } | 
|  | 1950 |  | 
|  | 1951 | /** | 
|  | 1952 | * skb_split - Split fragmented skb to two parts at length len. | 
|  | 1953 | * @skb: the buffer to split | 
|  | 1954 | * @skb1: the buffer to receive the second part | 
|  | 1955 | * @len: new length for skb | 
|  | 1956 | */ | 
|  | 1957 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) | 
|  | 1958 | { | 
|  | 1959 | int pos = skb_headlen(skb); | 
|  | 1960 |  | 
|  | 1961 | if (len < pos)	/* Split line is inside header. */ | 
|  | 1962 | skb_split_inside_header(skb, skb1, len, pos); | 
|  | 1963 | else		/* Second chunk has no header, nothing to copy. */ | 
|  | 1964 | skb_split_no_header(skb, skb1, len, pos); | 
|  | 1965 | } | 
|  | 1966 |  | 
| Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 1967 | /** | 
|  | 1968 | * skb_prepare_seq_read - Prepare a sequential read of skb data | 
|  | 1969 | * @skb: the buffer to read | 
|  | 1970 | * @from: lower offset of data to be read | 
|  | 1971 | * @to: upper offset of data to be read | 
|  | 1972 | * @st: state variable | 
|  | 1973 | * | 
|  | 1974 | * Initializes the specified state variable. Must be called before | 
|  | 1975 | * invoking skb_seq_read() for the first time. | 
|  | 1976 | */ | 
|  | 1977 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, | 
|  | 1978 | unsigned int to, struct skb_seq_state *st) | 
|  | 1979 | { | 
|  | 1980 | st->lower_offset = from; | 
|  | 1981 | st->upper_offset = to; | 
|  | 1982 | st->root_skb = st->cur_skb = skb; | 
|  | 1983 | st->frag_idx = st->stepped_offset = 0; | 
|  | 1984 | st->frag_data = NULL; | 
|  | 1985 | } | 
|  | 1986 |  | 
|  | 1987 | /** | 
|  | 1988 | * skb_seq_read - Sequentially read skb data | 
|  | 1989 | * @consumed: number of bytes consumed by the caller so far | 
|  | 1990 | * @data: destination pointer for data to be returned | 
|  | 1991 | * @st: state variable | 
|  | 1992 | * | 
|  | 1993 | * Reads a block of skb data at &consumed relative to the | 
|  | 1994 | * lower offset specified to skb_prepare_seq_read(). Assigns | 
|  | 1995 | * the head of the data block to &data and returns the length | 
|  | 1996 | * of the block or 0 if the end of the skb data or the upper | 
|  | 1997 | * offset has been reached. | 
|  | 1998 | * | 
|  | 1999 | * The caller is not required to consume all of the data | 
|  | 2000 | * returned, i.e. &consumed is typically set to the number | 
|  | 2001 | * of bytes already consumed and the next call to | 
|  | 2002 | * skb_seq_read() will return the remaining part of the block. | 
|  | 2003 | * | 
| Randy Dunlap | bc2cda1 | 2008-02-13 15:03:25 -0800 | [diff] [blame] | 2004 | * Note 1: The size of each block of data returned can be arbitary, | 
| Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2005 | *       this limitation is the cost for zerocopy seqeuental | 
|  | 2006 | *       reads of potentially non linear data. | 
|  | 2007 | * | 
| Randy Dunlap | bc2cda1 | 2008-02-13 15:03:25 -0800 | [diff] [blame] | 2008 | * Note 2: Fragment lists within fragments are not implemented | 
| Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2009 | *       at the moment, state->root_skb could be replaced with | 
|  | 2010 | *       a stack for this purpose. | 
|  | 2011 | */ | 
|  | 2012 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, | 
|  | 2013 | struct skb_seq_state *st) | 
|  | 2014 | { | 
|  | 2015 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; | 
|  | 2016 | skb_frag_t *frag; | 
|  | 2017 |  | 
|  | 2018 | if (unlikely(abs_offset >= st->upper_offset)) | 
|  | 2019 | return 0; | 
|  | 2020 |  | 
|  | 2021 | next_skb: | 
|  | 2022 | block_limit = skb_headlen(st->cur_skb); | 
|  | 2023 |  | 
|  | 2024 | if (abs_offset < block_limit) { | 
|  | 2025 | *data = st->cur_skb->data + abs_offset; | 
|  | 2026 | return block_limit - abs_offset; | 
|  | 2027 | } | 
|  | 2028 |  | 
|  | 2029 | if (st->frag_idx == 0 && !st->frag_data) | 
|  | 2030 | st->stepped_offset += skb_headlen(st->cur_skb); | 
|  | 2031 |  | 
|  | 2032 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { | 
|  | 2033 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; | 
|  | 2034 | block_limit = frag->size + st->stepped_offset; | 
|  | 2035 |  | 
|  | 2036 | if (abs_offset < block_limit) { | 
|  | 2037 | if (!st->frag_data) | 
|  | 2038 | st->frag_data = kmap_skb_frag(frag); | 
|  | 2039 |  | 
|  | 2040 | *data = (u8 *) st->frag_data + frag->page_offset + | 
|  | 2041 | (abs_offset - st->stepped_offset); | 
|  | 2042 |  | 
|  | 2043 | return block_limit - abs_offset; | 
|  | 2044 | } | 
|  | 2045 |  | 
|  | 2046 | if (st->frag_data) { | 
|  | 2047 | kunmap_skb_frag(st->frag_data); | 
|  | 2048 | st->frag_data = NULL; | 
|  | 2049 | } | 
|  | 2050 |  | 
|  | 2051 | st->frag_idx++; | 
|  | 2052 | st->stepped_offset += frag->size; | 
|  | 2053 | } | 
|  | 2054 |  | 
| Olaf Kirch | 5b5a60d | 2007-06-23 23:11:52 -0700 | [diff] [blame] | 2055 | if (st->frag_data) { | 
|  | 2056 | kunmap_skb_frag(st->frag_data); | 
|  | 2057 | st->frag_data = NULL; | 
|  | 2058 | } | 
|  | 2059 |  | 
| Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2060 | if (st->cur_skb->next) { | 
|  | 2061 | st->cur_skb = st->cur_skb->next; | 
|  | 2062 | st->frag_idx = 0; | 
|  | 2063 | goto next_skb; | 
|  | 2064 | } else if (st->root_skb == st->cur_skb && | 
|  | 2065 | skb_shinfo(st->root_skb)->frag_list) { | 
|  | 2066 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; | 
|  | 2067 | goto next_skb; | 
|  | 2068 | } | 
|  | 2069 |  | 
|  | 2070 | return 0; | 
|  | 2071 | } | 
|  | 2072 |  | 
|  | 2073 | /** | 
|  | 2074 | * skb_abort_seq_read - Abort a sequential read of skb data | 
|  | 2075 | * @st: state variable | 
|  | 2076 | * | 
|  | 2077 | * Must be called if skb_seq_read() was not called until it | 
|  | 2078 | * returned 0. | 
|  | 2079 | */ | 
|  | 2080 | void skb_abort_seq_read(struct skb_seq_state *st) | 
|  | 2081 | { | 
|  | 2082 | if (st->frag_data) | 
|  | 2083 | kunmap_skb_frag(st->frag_data); | 
|  | 2084 | } | 
|  | 2085 |  | 
| Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2086 | #define TS_SKB_CB(state)	((struct skb_seq_state *) &((state)->cb)) | 
|  | 2087 |  | 
|  | 2088 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, | 
|  | 2089 | struct ts_config *conf, | 
|  | 2090 | struct ts_state *state) | 
|  | 2091 | { | 
|  | 2092 | return skb_seq_read(offset, text, TS_SKB_CB(state)); | 
|  | 2093 | } | 
|  | 2094 |  | 
|  | 2095 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) | 
|  | 2096 | { | 
|  | 2097 | skb_abort_seq_read(TS_SKB_CB(state)); | 
|  | 2098 | } | 
|  | 2099 |  | 
|  | 2100 | /** | 
|  | 2101 | * skb_find_text - Find a text pattern in skb data | 
|  | 2102 | * @skb: the buffer to look in | 
|  | 2103 | * @from: search offset | 
|  | 2104 | * @to: search limit | 
|  | 2105 | * @config: textsearch configuration | 
|  | 2106 | * @state: uninitialized textsearch state variable | 
|  | 2107 | * | 
|  | 2108 | * Finds a pattern in the skb data according to the specified | 
|  | 2109 | * textsearch configuration. Use textsearch_next() to retrieve | 
|  | 2110 | * subsequent occurrences of the pattern. Returns the offset | 
|  | 2111 | * to the first occurrence or UINT_MAX if no match was found. | 
|  | 2112 | */ | 
|  | 2113 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, | 
|  | 2114 | unsigned int to, struct ts_config *config, | 
|  | 2115 | struct ts_state *state) | 
|  | 2116 | { | 
| Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2117 | unsigned int ret; | 
|  | 2118 |  | 
| Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2119 | config->get_next_block = skb_ts_get_next_block; | 
|  | 2120 | config->finish = skb_ts_finish; | 
|  | 2121 |  | 
|  | 2122 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); | 
|  | 2123 |  | 
| Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 2124 | ret = textsearch_find(config, state); | 
|  | 2125 | return (ret <= to - from ? ret : UINT_MAX); | 
| Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2126 | } | 
|  | 2127 |  | 
| Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2128 | /** | 
|  | 2129 | * skb_append_datato_frags: - append the user data to a skb | 
|  | 2130 | * @sk: sock  structure | 
|  | 2131 | * @skb: skb structure to be appened with user data. | 
|  | 2132 | * @getfrag: call back function to be used for getting the user data | 
|  | 2133 | * @from: pointer to user message iov | 
|  | 2134 | * @length: length of the iov message | 
|  | 2135 | * | 
|  | 2136 | * Description: This procedure append the user data in the fragment part | 
|  | 2137 | * of the skb if any page alloc fails user this procedure returns  -ENOMEM | 
|  | 2138 | */ | 
|  | 2139 | int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, | 
| Martin Waitz | dab9630 | 2005-12-05 13:40:12 -0800 | [diff] [blame] | 2140 | int (*getfrag)(void *from, char *to, int offset, | 
| Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2141 | int len, int odd, struct sk_buff *skb), | 
|  | 2142 | void *from, int length) | 
|  | 2143 | { | 
|  | 2144 | int frg_cnt = 0; | 
|  | 2145 | skb_frag_t *frag = NULL; | 
|  | 2146 | struct page *page = NULL; | 
|  | 2147 | int copy, left; | 
|  | 2148 | int offset = 0; | 
|  | 2149 | int ret; | 
|  | 2150 |  | 
|  | 2151 | do { | 
|  | 2152 | /* Return error if we don't have space for new frag */ | 
|  | 2153 | frg_cnt = skb_shinfo(skb)->nr_frags; | 
|  | 2154 | if (frg_cnt >= MAX_SKB_FRAGS) | 
|  | 2155 | return -EFAULT; | 
|  | 2156 |  | 
|  | 2157 | /* allocate a new page for next frag */ | 
|  | 2158 | page = alloc_pages(sk->sk_allocation, 0); | 
|  | 2159 |  | 
|  | 2160 | /* If alloc_page fails just return failure and caller will | 
|  | 2161 | * free previous allocated pages by doing kfree_skb() | 
|  | 2162 | */ | 
|  | 2163 | if (page == NULL) | 
|  | 2164 | return -ENOMEM; | 
|  | 2165 |  | 
|  | 2166 | /* initialize the next frag */ | 
|  | 2167 | sk->sk_sndmsg_page = page; | 
|  | 2168 | sk->sk_sndmsg_off = 0; | 
|  | 2169 | skb_fill_page_desc(skb, frg_cnt, page, 0, 0); | 
|  | 2170 | skb->truesize += PAGE_SIZE; | 
|  | 2171 | atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); | 
|  | 2172 |  | 
|  | 2173 | /* get the new initialized frag */ | 
|  | 2174 | frg_cnt = skb_shinfo(skb)->nr_frags; | 
|  | 2175 | frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; | 
|  | 2176 |  | 
|  | 2177 | /* copy the user data to page */ | 
|  | 2178 | left = PAGE_SIZE - frag->page_offset; | 
|  | 2179 | copy = (length > left)? left : length; | 
|  | 2180 |  | 
|  | 2181 | ret = getfrag(from, (page_address(frag->page) + | 
|  | 2182 | frag->page_offset + frag->size), | 
|  | 2183 | offset, copy, 0, skb); | 
|  | 2184 | if (ret < 0) | 
|  | 2185 | return -EFAULT; | 
|  | 2186 |  | 
|  | 2187 | /* copy was successful so update the size parameters */ | 
|  | 2188 | sk->sk_sndmsg_off += copy; | 
|  | 2189 | frag->size += copy; | 
|  | 2190 | skb->len += copy; | 
|  | 2191 | skb->data_len += copy; | 
|  | 2192 | offset += copy; | 
|  | 2193 | length -= copy; | 
|  | 2194 |  | 
|  | 2195 | } while (length > 0); | 
|  | 2196 |  | 
|  | 2197 | return 0; | 
|  | 2198 | } | 
|  | 2199 |  | 
| Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2200 | /** | 
|  | 2201 | *	skb_pull_rcsum - pull skb and update receive checksum | 
|  | 2202 | *	@skb: buffer to update | 
| Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2203 | *	@len: length of data pulled | 
|  | 2204 | * | 
|  | 2205 | *	This function performs an skb_pull on the packet and updates | 
| Urs Thuermann | fee54fa | 2008-02-12 22:03:25 -0800 | [diff] [blame] | 2206 | *	the CHECKSUM_COMPLETE checksum.  It should be used on | 
| Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2207 | *	receive path processing instead of skb_pull unless you know | 
|  | 2208 | *	that the checksum difference is zero (e.g., a valid IP header) | 
|  | 2209 | *	or you are setting ip_summed to CHECKSUM_NONE. | 
| Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 2210 | */ | 
|  | 2211 | unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) | 
|  | 2212 | { | 
|  | 2213 | BUG_ON(len > skb->len); | 
|  | 2214 | skb->len -= len; | 
|  | 2215 | BUG_ON(skb->len < skb->data_len); | 
|  | 2216 | skb_postpull_rcsum(skb, skb->data, len); | 
|  | 2217 | return skb->data += len; | 
|  | 2218 | } | 
|  | 2219 |  | 
| Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 2220 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); | 
|  | 2221 |  | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2222 | /** | 
|  | 2223 | *	skb_segment - Perform protocol segmentation on skb. | 
|  | 2224 | *	@skb: buffer to segment | 
| Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2225 | *	@features: features for the output path (see dev->features) | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2226 | * | 
|  | 2227 | *	This function performs segmentation on the given skb.  It returns | 
| Ben Hutchings | 4c821d7 | 2008-04-13 21:52:48 -0700 | [diff] [blame] | 2228 | *	a pointer to the first in a list of new skbs for the segments. | 
|  | 2229 | *	In case of error it returns ERR_PTR(err). | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2230 | */ | 
| Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2231 | struct sk_buff *skb_segment(struct sk_buff *skb, int features) | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2232 | { | 
|  | 2233 | struct sk_buff *segs = NULL; | 
|  | 2234 | struct sk_buff *tail = NULL; | 
|  | 2235 | unsigned int mss = skb_shinfo(skb)->gso_size; | 
| Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 2236 | unsigned int doffset = skb->data - skb_mac_header(skb); | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2237 | unsigned int offset = doffset; | 
|  | 2238 | unsigned int headroom; | 
|  | 2239 | unsigned int len; | 
| Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2240 | int sg = features & NETIF_F_SG; | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2241 | int nfrags = skb_shinfo(skb)->nr_frags; | 
|  | 2242 | int err = -ENOMEM; | 
|  | 2243 | int i = 0; | 
|  | 2244 | int pos; | 
|  | 2245 |  | 
|  | 2246 | __skb_push(skb, doffset); | 
|  | 2247 | headroom = skb_headroom(skb); | 
|  | 2248 | pos = skb_headlen(skb); | 
|  | 2249 |  | 
|  | 2250 | do { | 
|  | 2251 | struct sk_buff *nskb; | 
|  | 2252 | skb_frag_t *frag; | 
| Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 2253 | int hsize; | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2254 | int k; | 
|  | 2255 | int size; | 
|  | 2256 |  | 
|  | 2257 | len = skb->len - offset; | 
|  | 2258 | if (len > mss) | 
|  | 2259 | len = mss; | 
|  | 2260 |  | 
|  | 2261 | hsize = skb_headlen(skb) - offset; | 
|  | 2262 | if (hsize < 0) | 
|  | 2263 | hsize = 0; | 
| Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 2264 | if (hsize > len || !sg) | 
|  | 2265 | hsize = len; | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2266 |  | 
| Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 2267 | nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2268 | if (unlikely(!nskb)) | 
|  | 2269 | goto err; | 
|  | 2270 |  | 
|  | 2271 | if (segs) | 
|  | 2272 | tail->next = nskb; | 
|  | 2273 | else | 
|  | 2274 | segs = nskb; | 
|  | 2275 | tail = nskb; | 
|  | 2276 |  | 
|  | 2277 | nskb->dev = skb->dev; | 
| Peter P Waskiewicz Jr | f25f4e4 | 2007-07-06 13:36:20 -0700 | [diff] [blame] | 2278 | skb_copy_queue_mapping(nskb, skb); | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2279 | nskb->priority = skb->priority; | 
|  | 2280 | nskb->protocol = skb->protocol; | 
|  | 2281 | nskb->dst = dst_clone(skb->dst); | 
|  | 2282 | memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); | 
|  | 2283 | nskb->pkt_type = skb->pkt_type; | 
|  | 2284 | nskb->mac_len = skb->mac_len; | 
|  | 2285 |  | 
|  | 2286 | skb_reserve(nskb, headroom); | 
| Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 2287 | skb_reset_mac_header(nskb); | 
| Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame] | 2288 | skb_set_network_header(nskb, skb->mac_len); | 
| Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 2289 | nskb->transport_header = (nskb->network_header + | 
|  | 2290 | skb_network_header_len(skb)); | 
| Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2291 | skb_copy_from_linear_data(skb, skb_put(nskb, doffset), | 
|  | 2292 | doffset); | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2293 | if (!sg) { | 
|  | 2294 | nskb->csum = skb_copy_and_csum_bits(skb, offset, | 
|  | 2295 | skb_put(nskb, len), | 
|  | 2296 | len, 0); | 
|  | 2297 | continue; | 
|  | 2298 | } | 
|  | 2299 |  | 
|  | 2300 | frag = skb_shinfo(nskb)->frags; | 
|  | 2301 | k = 0; | 
|  | 2302 |  | 
| Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2303 | nskb->ip_summed = CHECKSUM_PARTIAL; | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2304 | nskb->csum = skb->csum; | 
| Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 2305 | skb_copy_from_linear_data_offset(skb, offset, | 
|  | 2306 | skb_put(nskb, hsize), hsize); | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2307 |  | 
|  | 2308 | while (pos < offset + len) { | 
|  | 2309 | BUG_ON(i >= nfrags); | 
|  | 2310 |  | 
|  | 2311 | *frag = skb_shinfo(skb)->frags[i]; | 
|  | 2312 | get_page(frag->page); | 
|  | 2313 | size = frag->size; | 
|  | 2314 |  | 
|  | 2315 | if (pos < offset) { | 
|  | 2316 | frag->page_offset += offset - pos; | 
|  | 2317 | frag->size -= offset - pos; | 
|  | 2318 | } | 
|  | 2319 |  | 
|  | 2320 | k++; | 
|  | 2321 |  | 
|  | 2322 | if (pos + size <= offset + len) { | 
|  | 2323 | i++; | 
|  | 2324 | pos += size; | 
|  | 2325 | } else { | 
|  | 2326 | frag->size -= pos + size - (offset + len); | 
|  | 2327 | break; | 
|  | 2328 | } | 
|  | 2329 |  | 
|  | 2330 | frag++; | 
|  | 2331 | } | 
|  | 2332 |  | 
|  | 2333 | skb_shinfo(nskb)->nr_frags = k; | 
|  | 2334 | nskb->data_len = len - hsize; | 
|  | 2335 | nskb->len += nskb->data_len; | 
|  | 2336 | nskb->truesize += nskb->data_len; | 
|  | 2337 | } while ((offset += len) < skb->len); | 
|  | 2338 |  | 
|  | 2339 | return segs; | 
|  | 2340 |  | 
|  | 2341 | err: | 
|  | 2342 | while ((skb = segs)) { | 
|  | 2343 | segs = skb->next; | 
| Patrick McHardy | b08d584 | 2007-02-27 09:57:37 -0800 | [diff] [blame] | 2344 | kfree_skb(skb); | 
| Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 2345 | } | 
|  | 2346 | return ERR_PTR(err); | 
|  | 2347 | } | 
|  | 2348 |  | 
|  | 2349 | EXPORT_SYMBOL_GPL(skb_segment); | 
|  | 2350 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | void __init skb_init(void) | 
|  | 2352 | { | 
|  | 2353 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", | 
|  | 2354 | sizeof(struct sk_buff), | 
|  | 2355 | 0, | 
| Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 2356 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2357 | NULL); | 
| David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 2358 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", | 
|  | 2359 | (2*sizeof(struct sk_buff)) + | 
|  | 2360 | sizeof(atomic_t), | 
|  | 2361 | 0, | 
| Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 2362 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, | 
| Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2363 | NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | } | 
|  | 2365 |  | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2366 | /** | 
|  | 2367 | *	skb_to_sgvec - Fill a scatter-gather list from a socket buffer | 
|  | 2368 | *	@skb: Socket buffer containing the buffers to be mapped | 
|  | 2369 | *	@sg: The scatter-gather list to map into | 
|  | 2370 | *	@offset: The offset into the buffer's contents to start mapping | 
|  | 2371 | *	@len: Length of buffer space to be mapped | 
|  | 2372 | * | 
|  | 2373 | *	Fill the specified scatter-gather list with mappings/pointers into a | 
|  | 2374 | *	region of the buffer space attached to a socket buffer. | 
|  | 2375 | */ | 
| David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 2376 | static int | 
|  | 2377 | __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2378 | { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2379 | int start = skb_headlen(skb); | 
|  | 2380 | int i, copy = start - offset; | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2381 | int elt = 0; | 
|  | 2382 |  | 
|  | 2383 | if (copy > 0) { | 
|  | 2384 | if (copy > len) | 
|  | 2385 | copy = len; | 
| Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 2386 | sg_set_buf(sg, skb->data + offset, copy); | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2387 | elt++; | 
|  | 2388 | if ((len -= copy) == 0) | 
|  | 2389 | return elt; | 
|  | 2390 | offset += copy; | 
|  | 2391 | } | 
|  | 2392 |  | 
|  | 2393 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2394 | int end; | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2395 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2396 | BUG_TRAP(start <= offset + len); | 
|  | 2397 |  | 
|  | 2398 | end = start + skb_shinfo(skb)->frags[i].size; | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2399 | if ((copy = end - offset) > 0) { | 
|  | 2400 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | 
|  | 2401 |  | 
|  | 2402 | if (copy > len) | 
|  | 2403 | copy = len; | 
| Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 2404 | sg_set_page(&sg[elt], frag->page, copy, | 
|  | 2405 | frag->page_offset+offset-start); | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2406 | elt++; | 
|  | 2407 | if (!(len -= copy)) | 
|  | 2408 | return elt; | 
|  | 2409 | offset += copy; | 
|  | 2410 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2411 | start = end; | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2412 | } | 
|  | 2413 |  | 
|  | 2414 | if (skb_shinfo(skb)->frag_list) { | 
|  | 2415 | struct sk_buff *list = skb_shinfo(skb)->frag_list; | 
|  | 2416 |  | 
|  | 2417 | for (; list; list = list->next) { | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2418 | int end; | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2419 |  | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2420 | BUG_TRAP(start <= offset + len); | 
|  | 2421 |  | 
|  | 2422 | end = start + list->len; | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2423 | if ((copy = end - offset) > 0) { | 
|  | 2424 | if (copy > len) | 
|  | 2425 | copy = len; | 
| David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 2426 | elt += __skb_to_sgvec(list, sg+elt, offset - start, | 
|  | 2427 | copy); | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2428 | if ((len -= copy) == 0) | 
|  | 2429 | return elt; | 
|  | 2430 | offset += copy; | 
|  | 2431 | } | 
| David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 2432 | start = end; | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2433 | } | 
|  | 2434 | } | 
|  | 2435 | BUG_ON(len); | 
|  | 2436 | return elt; | 
|  | 2437 | } | 
|  | 2438 |  | 
| David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 2439 | int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) | 
|  | 2440 | { | 
|  | 2441 | int nsg = __skb_to_sgvec(skb, sg, offset, len); | 
|  | 2442 |  | 
| Jens Axboe | c46f233 | 2007-10-31 12:06:37 +0100 | [diff] [blame] | 2443 | sg_mark_end(&sg[nsg - 1]); | 
| David S. Miller | 51c739d | 2007-10-30 21:29:29 -0700 | [diff] [blame] | 2444 |  | 
|  | 2445 | return nsg; | 
|  | 2446 | } | 
|  | 2447 |  | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2448 | /** | 
|  | 2449 | *	skb_cow_data - Check that a socket buffer's data buffers are writable | 
|  | 2450 | *	@skb: The socket buffer to check. | 
|  | 2451 | *	@tailbits: Amount of trailing space to be added | 
|  | 2452 | *	@trailer: Returned pointer to the skb where the @tailbits space begins | 
|  | 2453 | * | 
|  | 2454 | *	Make sure that the data buffers attached to a socket buffer are | 
|  | 2455 | *	writable. If they are not, private copies are made of the data buffers | 
|  | 2456 | *	and the socket buffer is set to use these instead. | 
|  | 2457 | * | 
|  | 2458 | *	If @tailbits is given, make sure that there is space to write @tailbits | 
|  | 2459 | *	bytes of data beyond current end of socket buffer.  @trailer will be | 
|  | 2460 | *	set to point to the skb in which this space begins. | 
|  | 2461 | * | 
|  | 2462 | *	The number of scatterlist elements required to completely map the | 
|  | 2463 | *	COW'd and extended socket buffer will be returned. | 
|  | 2464 | */ | 
|  | 2465 | int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) | 
|  | 2466 | { | 
|  | 2467 | int copyflag; | 
|  | 2468 | int elt; | 
|  | 2469 | struct sk_buff *skb1, **skb_p; | 
|  | 2470 |  | 
|  | 2471 | /* If skb is cloned or its head is paged, reallocate | 
|  | 2472 | * head pulling out all the pages (pages are considered not writable | 
|  | 2473 | * at the moment even if they are anonymous). | 
|  | 2474 | */ | 
|  | 2475 | if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && | 
|  | 2476 | __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) | 
|  | 2477 | return -ENOMEM; | 
|  | 2478 |  | 
|  | 2479 | /* Easy case. Most of packets will go this way. */ | 
|  | 2480 | if (!skb_shinfo(skb)->frag_list) { | 
|  | 2481 | /* A little of trouble, not enough of space for trailer. | 
|  | 2482 | * This should not happen, when stack is tuned to generate | 
|  | 2483 | * good frames. OK, on miss we reallocate and reserve even more | 
|  | 2484 | * space, 128 bytes is fair. */ | 
|  | 2485 |  | 
|  | 2486 | if (skb_tailroom(skb) < tailbits && | 
|  | 2487 | pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) | 
|  | 2488 | return -ENOMEM; | 
|  | 2489 |  | 
|  | 2490 | /* Voila! */ | 
|  | 2491 | *trailer = skb; | 
|  | 2492 | return 1; | 
|  | 2493 | } | 
|  | 2494 |  | 
|  | 2495 | /* Misery. We are in troubles, going to mincer fragments... */ | 
|  | 2496 |  | 
|  | 2497 | elt = 1; | 
|  | 2498 | skb_p = &skb_shinfo(skb)->frag_list; | 
|  | 2499 | copyflag = 0; | 
|  | 2500 |  | 
|  | 2501 | while ((skb1 = *skb_p) != NULL) { | 
|  | 2502 | int ntail = 0; | 
|  | 2503 |  | 
|  | 2504 | /* The fragment is partially pulled by someone, | 
|  | 2505 | * this can happen on input. Copy it and everything | 
|  | 2506 | * after it. */ | 
|  | 2507 |  | 
|  | 2508 | if (skb_shared(skb1)) | 
|  | 2509 | copyflag = 1; | 
|  | 2510 |  | 
|  | 2511 | /* If the skb is the last, worry about trailer. */ | 
|  | 2512 |  | 
|  | 2513 | if (skb1->next == NULL && tailbits) { | 
|  | 2514 | if (skb_shinfo(skb1)->nr_frags || | 
|  | 2515 | skb_shinfo(skb1)->frag_list || | 
|  | 2516 | skb_tailroom(skb1) < tailbits) | 
|  | 2517 | ntail = tailbits + 128; | 
|  | 2518 | } | 
|  | 2519 |  | 
|  | 2520 | if (copyflag || | 
|  | 2521 | skb_cloned(skb1) || | 
|  | 2522 | ntail || | 
|  | 2523 | skb_shinfo(skb1)->nr_frags || | 
|  | 2524 | skb_shinfo(skb1)->frag_list) { | 
|  | 2525 | struct sk_buff *skb2; | 
|  | 2526 |  | 
|  | 2527 | /* Fuck, we are miserable poor guys... */ | 
|  | 2528 | if (ntail == 0) | 
|  | 2529 | skb2 = skb_copy(skb1, GFP_ATOMIC); | 
|  | 2530 | else | 
|  | 2531 | skb2 = skb_copy_expand(skb1, | 
|  | 2532 | skb_headroom(skb1), | 
|  | 2533 | ntail, | 
|  | 2534 | GFP_ATOMIC); | 
|  | 2535 | if (unlikely(skb2 == NULL)) | 
|  | 2536 | return -ENOMEM; | 
|  | 2537 |  | 
|  | 2538 | if (skb1->sk) | 
|  | 2539 | skb_set_owner_w(skb2, skb1->sk); | 
|  | 2540 |  | 
|  | 2541 | /* Looking around. Are we still alive? | 
|  | 2542 | * OK, link new skb, drop old one */ | 
|  | 2543 |  | 
|  | 2544 | skb2->next = skb1->next; | 
|  | 2545 | *skb_p = skb2; | 
|  | 2546 | kfree_skb(skb1); | 
|  | 2547 | skb1 = skb2; | 
|  | 2548 | } | 
|  | 2549 | elt++; | 
|  | 2550 | *trailer = skb1; | 
|  | 2551 | skb_p = &skb1->next; | 
|  | 2552 | } | 
|  | 2553 |  | 
|  | 2554 | return elt; | 
|  | 2555 | } | 
|  | 2556 |  | 
| Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 2557 | /** | 
|  | 2558 | * skb_partial_csum_set - set up and verify partial csum values for packet | 
|  | 2559 | * @skb: the skb to set | 
|  | 2560 | * @start: the number of bytes after skb->data to start checksumming. | 
|  | 2561 | * @off: the offset from start to place the checksum. | 
|  | 2562 | * | 
|  | 2563 | * For untrusted partially-checksummed packets, we need to make sure the values | 
|  | 2564 | * for skb->csum_start and skb->csum_offset are valid so we don't oops. | 
|  | 2565 | * | 
|  | 2566 | * This function checks and sets those values and skb->ip_summed: if this | 
|  | 2567 | * returns false you should drop the packet. | 
|  | 2568 | */ | 
|  | 2569 | bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) | 
|  | 2570 | { | 
|  | 2571 | if (unlikely(start > skb->len - 2) || | 
|  | 2572 | unlikely((int)start + off > skb->len - 2)) { | 
|  | 2573 | if (net_ratelimit()) | 
|  | 2574 | printk(KERN_WARNING | 
|  | 2575 | "bad partial csum: csum=%u/%u len=%u\n", | 
|  | 2576 | start, off, skb->len); | 
|  | 2577 | return false; | 
|  | 2578 | } | 
|  | 2579 | skb->ip_summed = CHECKSUM_PARTIAL; | 
|  | 2580 | skb->csum_start = skb_headroom(skb) + start; | 
|  | 2581 | skb->csum_offset = off; | 
|  | 2582 | return true; | 
|  | 2583 | } | 
|  | 2584 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | EXPORT_SYMBOL(___pskb_trim); | 
|  | 2586 | EXPORT_SYMBOL(__kfree_skb); | 
| Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 2587 | EXPORT_SYMBOL(kfree_skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | EXPORT_SYMBOL(__pskb_pull_tail); | 
| David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 2589 | EXPORT_SYMBOL(__alloc_skb); | 
| Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 2590 | EXPORT_SYMBOL(__netdev_alloc_skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | EXPORT_SYMBOL(pskb_copy); | 
|  | 2592 | EXPORT_SYMBOL(pskb_expand_head); | 
|  | 2593 | EXPORT_SYMBOL(skb_checksum); | 
|  | 2594 | EXPORT_SYMBOL(skb_clone); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | EXPORT_SYMBOL(skb_copy); | 
|  | 2596 | EXPORT_SYMBOL(skb_copy_and_csum_bits); | 
|  | 2597 | EXPORT_SYMBOL(skb_copy_and_csum_dev); | 
|  | 2598 | EXPORT_SYMBOL(skb_copy_bits); | 
|  | 2599 | EXPORT_SYMBOL(skb_copy_expand); | 
|  | 2600 | EXPORT_SYMBOL(skb_over_panic); | 
|  | 2601 | EXPORT_SYMBOL(skb_pad); | 
|  | 2602 | EXPORT_SYMBOL(skb_realloc_headroom); | 
|  | 2603 | EXPORT_SYMBOL(skb_under_panic); | 
|  | 2604 | EXPORT_SYMBOL(skb_dequeue); | 
|  | 2605 | EXPORT_SYMBOL(skb_dequeue_tail); | 
|  | 2606 | EXPORT_SYMBOL(skb_insert); | 
|  | 2607 | EXPORT_SYMBOL(skb_queue_purge); | 
|  | 2608 | EXPORT_SYMBOL(skb_queue_head); | 
|  | 2609 | EXPORT_SYMBOL(skb_queue_tail); | 
|  | 2610 | EXPORT_SYMBOL(skb_unlink); | 
|  | 2611 | EXPORT_SYMBOL(skb_append); | 
|  | 2612 | EXPORT_SYMBOL(skb_split); | 
| Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2613 | EXPORT_SYMBOL(skb_prepare_seq_read); | 
|  | 2614 | EXPORT_SYMBOL(skb_seq_read); | 
|  | 2615 | EXPORT_SYMBOL(skb_abort_seq_read); | 
| Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2616 | EXPORT_SYMBOL(skb_find_text); | 
| Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2617 | EXPORT_SYMBOL(skb_append_datato_frags); | 
| David Howells | 716ea3a | 2007-04-02 20:19:53 -0700 | [diff] [blame] | 2618 |  | 
|  | 2619 | EXPORT_SYMBOL_GPL(skb_to_sgvec); | 
|  | 2620 | EXPORT_SYMBOL_GPL(skb_cow_data); | 
| Rusty Russell | f35d9d8 | 2008-02-04 23:49:54 -0500 | [diff] [blame] | 2621 | EXPORT_SYMBOL_GPL(skb_partial_csum_set); |