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