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> |
| 44 | #include <linux/sched.h> |
| 45 | #include <linux/mm.h> |
| 46 | #include <linux/interrupt.h> |
| 47 | #include <linux/in.h> |
| 48 | #include <linux/inet.h> |
| 49 | #include <linux/slab.h> |
| 50 | #include <linux/netdevice.h> |
| 51 | #ifdef CONFIG_NET_CLS_ACT |
| 52 | #include <net/pkt_sched.h> |
| 53 | #endif |
| 54 | #include <linux/string.h> |
| 55 | #include <linux/skbuff.h> |
| 56 | #include <linux/cache.h> |
| 57 | #include <linux/rtnetlink.h> |
| 58 | #include <linux/init.h> |
| 59 | #include <linux/highmem.h> |
| 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 | |
Eric Dumazet | ba89966 | 2005-08-26 12:05:31 -0700 | [diff] [blame] | 70 | static kmem_cache_t *skbuff_head_cache __read_mostly; |
| 71 | static kmem_cache_t *skbuff_fclone_cache __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Keep out-of-line to prevent kernel bloat. |
| 75 | * __builtin_return_address is not used because it is not always |
| 76 | * reliable. |
| 77 | */ |
| 78 | |
| 79 | /** |
| 80 | * skb_over_panic - private function |
| 81 | * @skb: buffer |
| 82 | * @sz: size |
| 83 | * @here: address |
| 84 | * |
| 85 | * Out of line support code for skb_put(). Not user callable. |
| 86 | */ |
| 87 | void skb_over_panic(struct sk_buff *skb, int sz, void *here) |
| 88 | { |
Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 89 | printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " |
| 90 | "data:%p tail:%p end:%p dev:%s\n", |
| 91 | here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end, |
| 92 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | BUG(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * skb_under_panic - private function |
| 98 | * @skb: buffer |
| 99 | * @sz: size |
| 100 | * @here: address |
| 101 | * |
| 102 | * Out of line support code for skb_push(). Not user callable. |
| 103 | */ |
| 104 | |
| 105 | void skb_under_panic(struct sk_buff *skb, int sz, void *here) |
| 106 | { |
Patrick McHardy | 2609545 | 2005-04-21 16:43:02 -0700 | [diff] [blame] | 107 | printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " |
| 108 | "data:%p tail:%p end:%p dev:%s\n", |
| 109 | here, skb->len, sz, skb->head, skb->data, skb->tail, skb->end, |
| 110 | skb->dev ? skb->dev->name : "<NULL>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | BUG(); |
| 112 | } |
| 113 | |
David S. Miller | dc6de33 | 2006-04-20 00:10:50 -0700 | [diff] [blame] | 114 | void skb_truesize_bug(struct sk_buff *skb) |
| 115 | { |
| 116 | printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " |
| 117 | "len=%u, sizeof(sk_buff)=%Zd\n", |
| 118 | skb->truesize, skb->len, sizeof(struct sk_buff)); |
| 119 | } |
| 120 | EXPORT_SYMBOL(skb_truesize_bug); |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 123 | * 'private' fields and also do memory statistics to find all the |
| 124 | * [BEEP] leaks. |
| 125 | * |
| 126 | */ |
| 127 | |
| 128 | /** |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 129 | * __alloc_skb - allocate a network buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | * @size: size to allocate |
| 131 | * @gfp_mask: allocation mask |
Randy Dunlap | c83c248 | 2005-10-18 22:07:41 -0700 | [diff] [blame] | 132 | * @fclone: allocate from fclone cache instead of head cache |
| 133 | * and allocate a cloned (child) skb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | * |
| 135 | * Allocate a new &sk_buff. The returned buffer has no headroom and a |
| 136 | * tail room of size bytes. The object has a reference count of one. |
| 137 | * The return is the buffer. On a failure the return is %NULL. |
| 138 | * |
| 139 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 140 | * %GFP_ATOMIC. |
| 141 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 142 | struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 143 | int fclone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 145 | kmem_cache_t *cache; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 146 | struct skb_shared_info *shinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | struct sk_buff *skb; |
| 148 | u8 *data; |
| 149 | |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 150 | cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* Get the HEAD */ |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 153 | skb = kmem_cache_alloc(cache, gfp_mask & ~__GFP_DMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | if (!skb) |
| 155 | goto out; |
| 156 | |
| 157 | /* Get the DATA. Size must match skb_add_mtu(). */ |
| 158 | size = SKB_DATA_ALIGN(size); |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 159 | data = kmalloc_track_caller(size + sizeof(struct skb_shared_info), |
| 160 | gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | if (!data) |
| 162 | goto nodata; |
| 163 | |
| 164 | memset(skb, 0, offsetof(struct sk_buff, truesize)); |
| 165 | skb->truesize = size + sizeof(struct sk_buff); |
| 166 | atomic_set(&skb->users, 1); |
| 167 | skb->head = data; |
| 168 | skb->data = data; |
| 169 | skb->tail = data; |
| 170 | skb->end = data + size; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 171 | /* make sure we initialize shinfo sequentially */ |
| 172 | shinfo = skb_shinfo(skb); |
| 173 | atomic_set(&shinfo->dataref, 1); |
| 174 | shinfo->nr_frags = 0; |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 175 | shinfo->gso_size = 0; |
| 176 | shinfo->gso_segs = 0; |
| 177 | shinfo->gso_type = 0; |
Benjamin LaHaise | 4947d3e | 2006-01-03 14:06:50 -0800 | [diff] [blame] | 178 | shinfo->ip6_frag_id = 0; |
| 179 | shinfo->frag_list = NULL; |
| 180 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 181 | if (fclone) { |
| 182 | struct sk_buff *child = skb + 1; |
| 183 | atomic_t *fclone_ref = (atomic_t *) (child + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 185 | skb->fclone = SKB_FCLONE_ORIG; |
| 186 | atomic_set(fclone_ref, 1); |
| 187 | |
| 188 | child->fclone = SKB_FCLONE_UNAVAILABLE; |
| 189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | out: |
| 191 | return skb; |
| 192 | nodata: |
Herbert Xu | 8798b3f | 2006-01-23 16:32:45 -0800 | [diff] [blame] | 193 | kmem_cache_free(cache, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | skb = NULL; |
| 195 | goto out; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * alloc_skb_from_cache - allocate a network buffer |
| 200 | * @cp: kmem_cache from which to allocate the data area |
| 201 | * (object size must be big enough for @size bytes + skb overheads) |
| 202 | * @size: size to allocate |
| 203 | * @gfp_mask: allocation mask |
| 204 | * |
| 205 | * Allocate a new &sk_buff. The returned buffer has no headroom and |
| 206 | * tail room of size bytes. The object has a reference count of one. |
| 207 | * The return is the buffer. On a failure the return is %NULL. |
| 208 | * |
| 209 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 210 | * %GFP_ATOMIC. |
| 211 | */ |
| 212 | struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 213 | unsigned int size, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 214 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | struct sk_buff *skb; |
| 217 | u8 *data; |
| 218 | |
| 219 | /* Get the HEAD */ |
| 220 | skb = kmem_cache_alloc(skbuff_head_cache, |
| 221 | gfp_mask & ~__GFP_DMA); |
| 222 | if (!skb) |
| 223 | goto out; |
| 224 | |
| 225 | /* Get the DATA. */ |
| 226 | size = SKB_DATA_ALIGN(size); |
| 227 | data = kmem_cache_alloc(cp, gfp_mask); |
| 228 | if (!data) |
| 229 | goto nodata; |
| 230 | |
| 231 | memset(skb, 0, offsetof(struct sk_buff, truesize)); |
| 232 | skb->truesize = size + sizeof(struct sk_buff); |
| 233 | atomic_set(&skb->users, 1); |
| 234 | skb->head = data; |
| 235 | skb->data = data; |
| 236 | skb->tail = data; |
| 237 | skb->end = data + size; |
| 238 | |
| 239 | atomic_set(&(skb_shinfo(skb)->dataref), 1); |
| 240 | skb_shinfo(skb)->nr_frags = 0; |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 241 | skb_shinfo(skb)->gso_size = 0; |
| 242 | skb_shinfo(skb)->gso_segs = 0; |
| 243 | skb_shinfo(skb)->gso_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | skb_shinfo(skb)->frag_list = NULL; |
| 245 | out: |
| 246 | return skb; |
| 247 | nodata: |
| 248 | kmem_cache_free(skbuff_head_cache, skb); |
| 249 | skb = NULL; |
| 250 | goto out; |
| 251 | } |
| 252 | |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 253 | /** |
| 254 | * __netdev_alloc_skb - allocate an skbuff for rx on a specific device |
| 255 | * @dev: network device to receive on |
| 256 | * @length: length to allocate |
| 257 | * @gfp_mask: get_free_pages mask, passed to alloc_skb |
| 258 | * |
| 259 | * Allocate a new &sk_buff and assign it a usage count of one. The |
| 260 | * buffer has unspecified headroom built in. Users should allocate |
| 261 | * the headroom they think they need without accounting for the |
| 262 | * built in space. The built in space is used for optimisations. |
| 263 | * |
| 264 | * %NULL is returned if there is no free memory. |
| 265 | */ |
| 266 | struct sk_buff *__netdev_alloc_skb(struct net_device *dev, |
| 267 | unsigned int length, gfp_t gfp_mask) |
| 268 | { |
| 269 | struct sk_buff *skb; |
| 270 | |
| 271 | skb = alloc_skb(length + NET_SKB_PAD, gfp_mask); |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 272 | if (likely(skb)) { |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 273 | skb_reserve(skb, NET_SKB_PAD); |
Christoph Hellwig | 7b2e497 | 2006-08-07 16:09:04 -0700 | [diff] [blame] | 274 | skb->dev = dev; |
| 275 | } |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 276 | return skb; |
| 277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 279 | static void skb_drop_list(struct sk_buff **listp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 281 | struct sk_buff *list = *listp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 283 | *listp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | do { |
| 286 | struct sk_buff *this = list; |
| 287 | list = list->next; |
| 288 | kfree_skb(this); |
| 289 | } while (list); |
| 290 | } |
| 291 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 292 | static inline void skb_drop_fraglist(struct sk_buff *skb) |
| 293 | { |
| 294 | skb_drop_list(&skb_shinfo(skb)->frag_list); |
| 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 298 | { |
| 299 | struct sk_buff *list; |
| 300 | |
| 301 | for (list = skb_shinfo(skb)->frag_list; list; list = list->next) |
| 302 | skb_get(list); |
| 303 | } |
| 304 | |
Adrian Bunk | 5bba171 | 2006-06-29 13:02:35 -0700 | [diff] [blame] | 305 | static void skb_release_data(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
| 307 | if (!skb->cloned || |
| 308 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 309 | &skb_shinfo(skb)->dataref)) { |
| 310 | if (skb_shinfo(skb)->nr_frags) { |
| 311 | int i; |
| 312 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 313 | put_page(skb_shinfo(skb)->frags[i].page); |
| 314 | } |
| 315 | |
| 316 | if (skb_shinfo(skb)->frag_list) |
| 317 | skb_drop_fraglist(skb); |
| 318 | |
| 319 | kfree(skb->head); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Free an skbuff by memory without cleaning the state. |
| 325 | */ |
| 326 | void kfree_skbmem(struct sk_buff *skb) |
| 327 | { |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 328 | struct sk_buff *other; |
| 329 | atomic_t *fclone_ref; |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | skb_release_data(skb); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 332 | switch (skb->fclone) { |
| 333 | case SKB_FCLONE_UNAVAILABLE: |
| 334 | kmem_cache_free(skbuff_head_cache, skb); |
| 335 | break; |
| 336 | |
| 337 | case SKB_FCLONE_ORIG: |
| 338 | fclone_ref = (atomic_t *) (skb + 2); |
| 339 | if (atomic_dec_and_test(fclone_ref)) |
| 340 | kmem_cache_free(skbuff_fclone_cache, skb); |
| 341 | break; |
| 342 | |
| 343 | case SKB_FCLONE_CLONE: |
| 344 | fclone_ref = (atomic_t *) (skb + 1); |
| 345 | other = skb - 1; |
| 346 | |
| 347 | /* The clone portion is available for |
| 348 | * fast-cloning again. |
| 349 | */ |
| 350 | skb->fclone = SKB_FCLONE_UNAVAILABLE; |
| 351 | |
| 352 | if (atomic_dec_and_test(fclone_ref)) |
| 353 | kmem_cache_free(skbuff_fclone_cache, other); |
| 354 | break; |
| 355 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * __kfree_skb - private function |
| 360 | * @skb: buffer |
| 361 | * |
| 362 | * Free an sk_buff. Release anything attached to the buffer. |
| 363 | * Clean the state. This is an internal helper function. Users should |
| 364 | * always call kfree_skb |
| 365 | */ |
| 366 | |
| 367 | void __kfree_skb(struct sk_buff *skb) |
| 368 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | dst_release(skb->dst); |
| 370 | #ifdef CONFIG_XFRM |
| 371 | secpath_put(skb->sp); |
| 372 | #endif |
Stephen Hemminger | 9c2b332 | 2005-04-19 22:39:42 -0700 | [diff] [blame] | 373 | if (skb->destructor) { |
| 374 | WARN_ON(in_irq()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | skb->destructor(skb); |
| 376 | } |
| 377 | #ifdef CONFIG_NETFILTER |
| 378 | nf_conntrack_put(skb->nfct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 379 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 380 | nf_conntrack_put_reasm(skb->nfct_reasm); |
| 381 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 383 | nf_bridge_put(skb->nf_bridge); |
| 384 | #endif |
| 385 | #endif |
| 386 | /* XXX: IS this still necessary? - JHS */ |
| 387 | #ifdef CONFIG_NET_SCHED |
| 388 | skb->tc_index = 0; |
| 389 | #ifdef CONFIG_NET_CLS_ACT |
| 390 | skb->tc_verd = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | #endif |
| 392 | #endif |
| 393 | |
| 394 | kfree_skbmem(skb); |
| 395 | } |
| 396 | |
| 397 | /** |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 398 | * kfree_skb - free an sk_buff |
| 399 | * @skb: buffer to free |
| 400 | * |
| 401 | * Drop a reference to the buffer and free it if the usage count has |
| 402 | * hit zero. |
| 403 | */ |
| 404 | void kfree_skb(struct sk_buff *skb) |
| 405 | { |
| 406 | if (unlikely(!skb)) |
| 407 | return; |
| 408 | if (likely(atomic_read(&skb->users) == 1)) |
| 409 | smp_rmb(); |
| 410 | else if (likely(!atomic_dec_and_test(&skb->users))) |
| 411 | return; |
| 412 | __kfree_skb(skb); |
| 413 | } |
| 414 | |
| 415 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | * skb_clone - duplicate an sk_buff |
| 417 | * @skb: buffer to clone |
| 418 | * @gfp_mask: allocation priority |
| 419 | * |
| 420 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 421 | * copies share the same packet data but not structure. The new |
| 422 | * buffer has a reference count of 1. If the allocation fails the |
| 423 | * function returns %NULL otherwise the new buffer is returned. |
| 424 | * |
| 425 | * If this function is called from an interrupt gfp_mask() must be |
| 426 | * %GFP_ATOMIC. |
| 427 | */ |
| 428 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 429 | struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 431 | struct sk_buff *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 433 | n = skb + 1; |
| 434 | if (skb->fclone == SKB_FCLONE_ORIG && |
| 435 | n->fclone == SKB_FCLONE_UNAVAILABLE) { |
| 436 | atomic_t *fclone_ref = (atomic_t *) (n + 1); |
| 437 | n->fclone = SKB_FCLONE_CLONE; |
| 438 | atomic_inc(fclone_ref); |
| 439 | } else { |
| 440 | n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 441 | if (!n) |
| 442 | return NULL; |
| 443 | n->fclone = SKB_FCLONE_UNAVAILABLE; |
| 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | #define C(x) n->x = skb->x |
| 447 | |
| 448 | n->next = n->prev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | n->sk = NULL; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 450 | C(tstamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | C(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | C(h); |
| 453 | C(nh); |
| 454 | C(mac); |
| 455 | C(dst); |
| 456 | dst_clone(skb->dst); |
| 457 | C(sp); |
| 458 | #ifdef CONFIG_INET |
| 459 | secpath_get(skb->sp); |
| 460 | #endif |
| 461 | memcpy(n->cb, skb->cb, sizeof(skb->cb)); |
| 462 | C(len); |
| 463 | C(data_len); |
| 464 | C(csum); |
| 465 | C(local_df); |
| 466 | n->cloned = 1; |
| 467 | n->nohdr = 0; |
| 468 | C(pkt_type); |
| 469 | C(ip_summed); |
| 470 | C(priority); |
YOSHIFUJI Hideaki | a8372f0 | 2006-02-19 22:32:06 -0800 | [diff] [blame] | 471 | #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) |
| 472 | C(ipvs_property); |
| 473 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | C(protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | n->destructor = NULL; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 476 | C(mark); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | #ifdef CONFIG_NETFILTER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | C(nfct); |
| 479 | nf_conntrack_get(skb->nfct); |
| 480 | C(nfctinfo); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 481 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 482 | C(nfct_reasm); |
| 483 | nf_conntrack_get_reasm(skb->nfct_reasm); |
| 484 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 486 | C(nf_bridge); |
| 487 | nf_bridge_get(skb->nf_bridge); |
| 488 | #endif |
| 489 | #endif /*CONFIG_NETFILTER*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | #ifdef CONFIG_NET_SCHED |
| 491 | C(tc_index); |
| 492 | #ifdef CONFIG_NET_CLS_ACT |
| 493 | n->tc_verd = SET_TC_VERD(skb->tc_verd,0); |
David S. Miller | b72f6ec | 2005-07-19 14:13:54 -0700 | [diff] [blame] | 494 | n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd); |
| 495 | n->tc_verd = CLR_TC_MUNGED(n->tc_verd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | C(input_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | #endif |
James Morris | 984bc16 | 2006-06-09 00:29:17 -0700 | [diff] [blame] | 498 | skb_copy_secmark(n, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | #endif |
| 500 | C(truesize); |
| 501 | atomic_set(&n->users, 1); |
| 502 | C(head); |
| 503 | C(data); |
| 504 | C(tail); |
| 505 | C(end); |
| 506 | |
| 507 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 508 | skb->cloned = 1; |
| 509 | |
| 510 | return n; |
| 511 | } |
| 512 | |
| 513 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 514 | { |
| 515 | /* |
| 516 | * Shift between the two data areas in bytes |
| 517 | */ |
| 518 | unsigned long offset = new->data - old->data; |
| 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | new->sk = NULL; |
| 521 | new->dev = old->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | new->priority = old->priority; |
| 523 | new->protocol = old->protocol; |
| 524 | new->dst = dst_clone(old->dst); |
| 525 | #ifdef CONFIG_INET |
| 526 | new->sp = secpath_get(old->sp); |
| 527 | #endif |
| 528 | new->h.raw = old->h.raw + offset; |
| 529 | new->nh.raw = old->nh.raw + offset; |
| 530 | new->mac.raw = old->mac.raw + offset; |
| 531 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
| 532 | new->local_df = old->local_df; |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 533 | new->fclone = SKB_FCLONE_UNAVAILABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | new->pkt_type = old->pkt_type; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 535 | new->tstamp = old->tstamp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | new->destructor = NULL; |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 537 | new->mark = old->mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | #ifdef CONFIG_NETFILTER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | new->nfct = old->nfct; |
| 540 | nf_conntrack_get(old->nfct); |
| 541 | new->nfctinfo = old->nfctinfo; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 542 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 543 | new->nfct_reasm = old->nfct_reasm; |
| 544 | nf_conntrack_get_reasm(old->nfct_reasm); |
| 545 | #endif |
Julian Anastasov | c98d80e | 2005-10-22 13:39:21 +0300 | [diff] [blame] | 546 | #if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) |
| 547 | new->ipvs_property = old->ipvs_property; |
| 548 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 550 | new->nf_bridge = old->nf_bridge; |
| 551 | nf_bridge_get(old->nf_bridge); |
| 552 | #endif |
| 553 | #endif |
| 554 | #ifdef CONFIG_NET_SCHED |
| 555 | #ifdef CONFIG_NET_CLS_ACT |
| 556 | new->tc_verd = old->tc_verd; |
| 557 | #endif |
| 558 | new->tc_index = old->tc_index; |
| 559 | #endif |
James Morris | 984bc16 | 2006-06-09 00:29:17 -0700 | [diff] [blame] | 560 | skb_copy_secmark(new, old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | atomic_set(&new->users, 1); |
Herbert Xu | 7967168 | 2006-06-22 02:40:14 -0700 | [diff] [blame] | 562 | skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; |
| 563 | skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; |
| 564 | skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /** |
| 568 | * skb_copy - create private copy of an sk_buff |
| 569 | * @skb: buffer to copy |
| 570 | * @gfp_mask: allocation priority |
| 571 | * |
| 572 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 573 | * caller wishes to modify the data and needs a private copy of the |
| 574 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 575 | * on success. The returned buffer has a reference count of 1. |
| 576 | * |
| 577 | * As by-product this function converts non-linear &sk_buff to linear |
| 578 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 579 | * to modify all the data of returned buffer. This means that this |
| 580 | * function is not recommended for use in circumstances when only |
| 581 | * header is going to be modified. Use pskb_copy() instead. |
| 582 | */ |
| 583 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 584 | 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] | 585 | { |
| 586 | int headerlen = skb->data - skb->head; |
| 587 | /* |
| 588 | * Allocate the copy buffer |
| 589 | */ |
| 590 | struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len, |
| 591 | gfp_mask); |
| 592 | if (!n) |
| 593 | return NULL; |
| 594 | |
| 595 | /* Set the data pointer */ |
| 596 | skb_reserve(n, headerlen); |
| 597 | /* Set the tail pointer and length */ |
| 598 | skb_put(n, skb->len); |
| 599 | n->csum = skb->csum; |
| 600 | n->ip_summed = skb->ip_summed; |
| 601 | |
| 602 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) |
| 603 | BUG(); |
| 604 | |
| 605 | copy_skb_header(n, skb); |
| 606 | return n; |
| 607 | } |
| 608 | |
| 609 | |
| 610 | /** |
| 611 | * pskb_copy - create copy of an sk_buff with private head. |
| 612 | * @skb: buffer to copy |
| 613 | * @gfp_mask: allocation priority |
| 614 | * |
| 615 | * Make a copy of both an &sk_buff and part of its data, located |
| 616 | * in header. Fragmented data remain shared. This is used when |
| 617 | * the caller wishes to modify only header of &sk_buff and needs |
| 618 | * private copy of the header to alter. Returns %NULL on failure |
| 619 | * or the pointer to the buffer on success. |
| 620 | * The returned buffer has a reference count of 1. |
| 621 | */ |
| 622 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 623 | 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] | 624 | { |
| 625 | /* |
| 626 | * Allocate the copy buffer |
| 627 | */ |
| 628 | struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask); |
| 629 | |
| 630 | if (!n) |
| 631 | goto out; |
| 632 | |
| 633 | /* Set the data pointer */ |
| 634 | skb_reserve(n, skb->data - skb->head); |
| 635 | /* Set the tail pointer and length */ |
| 636 | skb_put(n, skb_headlen(skb)); |
| 637 | /* Copy the bytes */ |
| 638 | memcpy(n->data, skb->data, n->len); |
| 639 | n->csum = skb->csum; |
| 640 | n->ip_summed = skb->ip_summed; |
| 641 | |
Herbert Xu | 25f484a | 2006-11-07 14:57:15 -0800 | [diff] [blame] | 642 | n->truesize += skb->data_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | n->data_len = skb->data_len; |
| 644 | n->len = skb->len; |
| 645 | |
| 646 | if (skb_shinfo(skb)->nr_frags) { |
| 647 | int i; |
| 648 | |
| 649 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 650 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 651 | get_page(skb_shinfo(n)->frags[i].page); |
| 652 | } |
| 653 | skb_shinfo(n)->nr_frags = i; |
| 654 | } |
| 655 | |
| 656 | if (skb_shinfo(skb)->frag_list) { |
| 657 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 658 | skb_clone_fraglist(n); |
| 659 | } |
| 660 | |
| 661 | copy_skb_header(n, skb); |
| 662 | out: |
| 663 | return n; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * pskb_expand_head - reallocate header of &sk_buff |
| 668 | * @skb: buffer to reallocate |
| 669 | * @nhead: room to add at head |
| 670 | * @ntail: room to add at tail |
| 671 | * @gfp_mask: allocation priority |
| 672 | * |
| 673 | * Expands (or creates identical copy, if &nhead and &ntail are zero) |
| 674 | * header of skb. &sk_buff itself is not changed. &sk_buff MUST have |
| 675 | * reference count of 1. Returns zero in the case of success or error, |
| 676 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 677 | * |
| 678 | * All the pointers pointing into skb header may change and must be |
| 679 | * reloaded after call to this function. |
| 680 | */ |
| 681 | |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 682 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 683 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
| 685 | int i; |
| 686 | u8 *data; |
| 687 | int size = nhead + (skb->end - skb->head) + ntail; |
| 688 | long off; |
| 689 | |
| 690 | if (skb_shared(skb)) |
| 691 | BUG(); |
| 692 | |
| 693 | size = SKB_DATA_ALIGN(size); |
| 694 | |
| 695 | data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); |
| 696 | if (!data) |
| 697 | goto nodata; |
| 698 | |
| 699 | /* Copy only real data... and, alas, header. This should be |
| 700 | * optimized for the cases when header is void. */ |
| 701 | memcpy(data + nhead, skb->head, skb->tail - skb->head); |
| 702 | memcpy(data + size, skb->end, sizeof(struct skb_shared_info)); |
| 703 | |
| 704 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 705 | get_page(skb_shinfo(skb)->frags[i].page); |
| 706 | |
| 707 | if (skb_shinfo(skb)->frag_list) |
| 708 | skb_clone_fraglist(skb); |
| 709 | |
| 710 | skb_release_data(skb); |
| 711 | |
| 712 | off = (data + nhead) - skb->head; |
| 713 | |
| 714 | skb->head = data; |
| 715 | skb->end = data + size; |
| 716 | skb->data += off; |
| 717 | skb->tail += off; |
| 718 | skb->mac.raw += off; |
| 719 | skb->h.raw += off; |
| 720 | skb->nh.raw += off; |
| 721 | skb->cloned = 0; |
| 722 | skb->nohdr = 0; |
| 723 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 724 | return 0; |
| 725 | |
| 726 | nodata: |
| 727 | return -ENOMEM; |
| 728 | } |
| 729 | |
| 730 | /* Make private copy of skb with writable head and some headroom */ |
| 731 | |
| 732 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 733 | { |
| 734 | struct sk_buff *skb2; |
| 735 | int delta = headroom - skb_headroom(skb); |
| 736 | |
| 737 | if (delta <= 0) |
| 738 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 739 | else { |
| 740 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 741 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 742 | GFP_ATOMIC)) { |
| 743 | kfree_skb(skb2); |
| 744 | skb2 = NULL; |
| 745 | } |
| 746 | } |
| 747 | return skb2; |
| 748 | } |
| 749 | |
| 750 | |
| 751 | /** |
| 752 | * skb_copy_expand - copy and expand sk_buff |
| 753 | * @skb: buffer to copy |
| 754 | * @newheadroom: new free bytes at head |
| 755 | * @newtailroom: new free bytes at tail |
| 756 | * @gfp_mask: allocation priority |
| 757 | * |
| 758 | * Make a copy of both an &sk_buff and its data and while doing so |
| 759 | * allocate additional space. |
| 760 | * |
| 761 | * This is used when the caller wishes to modify the data and needs a |
| 762 | * private copy of the data to alter as well as more space for new fields. |
| 763 | * Returns %NULL on failure or the pointer to the buffer |
| 764 | * on success. The returned buffer has a reference count of 1. |
| 765 | * |
| 766 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 767 | * is called from an interrupt. |
| 768 | * |
| 769 | * BUG ALERT: ip_summed is not copied. Why does this work? Is it used |
| 770 | * only by netfilter in the cases when checksum is recalculated? --ANK |
| 771 | */ |
| 772 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
Victor Fusco | 86a76ca | 2005-07-08 14:57:47 -0700 | [diff] [blame] | 773 | int newheadroom, int newtailroom, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 774 | gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | { |
| 776 | /* |
| 777 | * Allocate the copy buffer |
| 778 | */ |
| 779 | struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, |
| 780 | gfp_mask); |
| 781 | int head_copy_len, head_copy_off; |
| 782 | |
| 783 | if (!n) |
| 784 | return NULL; |
| 785 | |
| 786 | skb_reserve(n, newheadroom); |
| 787 | |
| 788 | /* Set the tail pointer and length */ |
| 789 | skb_put(n, skb->len); |
| 790 | |
| 791 | head_copy_len = skb_headroom(skb); |
| 792 | head_copy_off = 0; |
| 793 | if (newheadroom <= head_copy_len) |
| 794 | head_copy_len = newheadroom; |
| 795 | else |
| 796 | head_copy_off = newheadroom - head_copy_len; |
| 797 | |
| 798 | /* Copy the linear header and data. */ |
| 799 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 800 | skb->len + head_copy_len)) |
| 801 | BUG(); |
| 802 | |
| 803 | copy_skb_header(n, skb); |
| 804 | |
| 805 | return n; |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * skb_pad - zero pad the tail of an skb |
| 810 | * @skb: buffer to pad |
| 811 | * @pad: space to pad |
| 812 | * |
| 813 | * Ensure that a buffer is followed by a padding area that is zero |
| 814 | * filled. Used by network drivers which may DMA or transfer data |
| 815 | * beyond the buffer end onto the wire. |
| 816 | * |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 817 | * 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] | 818 | */ |
| 819 | |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 820 | int skb_pad(struct sk_buff *skb, int pad) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 822 | int err; |
| 823 | int ntail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | /* If the skbuff is non linear tailroom is always zero.. */ |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 826 | if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | memset(skb->data+skb->len, 0, pad); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 828 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 830 | |
| 831 | ntail = skb->data_len + pad - (skb->end - skb->tail); |
| 832 | if (likely(skb_cloned(skb) || ntail > 0)) { |
| 833 | err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); |
| 834 | if (unlikely(err)) |
| 835 | goto free_skb; |
| 836 | } |
| 837 | |
| 838 | /* FIXME: The use of this function with non-linear skb's really needs |
| 839 | * to be audited. |
| 840 | */ |
| 841 | err = skb_linearize(skb); |
| 842 | if (unlikely(err)) |
| 843 | goto free_skb; |
| 844 | |
| 845 | memset(skb->data + skb->len, 0, pad); |
| 846 | return 0; |
| 847 | |
| 848 | free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | kfree_skb(skb); |
Herbert Xu | 5b057c6 | 2006-06-23 02:06:41 -0700 | [diff] [blame] | 850 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 853 | /* Trims skb to length len. It can change skb pointers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | */ |
| 855 | |
Herbert Xu | 3cc0e87 | 2006-06-09 16:13:38 -0700 | [diff] [blame] | 856 | int ___pskb_trim(struct sk_buff *skb, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 858 | struct sk_buff **fragp; |
| 859 | struct sk_buff *frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | int offset = skb_headlen(skb); |
| 861 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 862 | int i; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 863 | int err; |
| 864 | |
| 865 | if (skb_cloned(skb) && |
| 866 | unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) |
| 867 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 869 | i = 0; |
| 870 | if (offset >= len) |
| 871 | goto drop_pages; |
| 872 | |
| 873 | for (; i < nfrags; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | int end = offset + skb_shinfo(skb)->frags[i].size; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 875 | |
| 876 | if (end < len) { |
| 877 | offset = end; |
| 878 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | } |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 880 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 881 | skb_shinfo(skb)->frags[i++].size = len - offset; |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 882 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 883 | drop_pages: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 884 | skb_shinfo(skb)->nr_frags = i; |
| 885 | |
| 886 | for (; i < nfrags; i++) |
| 887 | put_page(skb_shinfo(skb)->frags[i].page); |
| 888 | |
| 889 | if (skb_shinfo(skb)->frag_list) |
| 890 | skb_drop_fraglist(skb); |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 891 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 894 | for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); |
| 895 | fragp = &frag->next) { |
| 896 | int end = offset + frag->len; |
| 897 | |
| 898 | if (skb_shared(frag)) { |
| 899 | struct sk_buff *nfrag; |
| 900 | |
| 901 | nfrag = skb_clone(frag, GFP_ATOMIC); |
| 902 | if (unlikely(!nfrag)) |
| 903 | return -ENOMEM; |
| 904 | |
| 905 | nfrag->next = frag->next; |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 906 | kfree_skb(frag); |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 907 | frag = nfrag; |
| 908 | *fragp = frag; |
| 909 | } |
| 910 | |
| 911 | if (end < len) { |
| 912 | offset = end; |
| 913 | continue; |
| 914 | } |
| 915 | |
| 916 | if (end > len && |
| 917 | unlikely((err = pskb_trim(frag, len - offset)))) |
| 918 | return err; |
| 919 | |
| 920 | if (frag->next) |
| 921 | skb_drop_list(&frag->next); |
| 922 | break; |
| 923 | } |
| 924 | |
Herbert Xu | f4d26fb | 2006-07-30 20:20:28 -0700 | [diff] [blame] | 925 | done: |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 926 | if (len > skb_headlen(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | skb->data_len -= skb->len - len; |
| 928 | skb->len = len; |
| 929 | } else { |
Herbert Xu | 27b437c | 2006-07-13 19:26:39 -0700 | [diff] [blame] | 930 | skb->len = len; |
| 931 | skb->data_len = 0; |
| 932 | skb->tail = skb->data + len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * __pskb_pull_tail - advance tail of skb header |
| 940 | * @skb: buffer to reallocate |
| 941 | * @delta: number of bytes to advance tail |
| 942 | * |
| 943 | * The function makes a sense only on a fragmented &sk_buff, |
| 944 | * it expands header moving its tail forward and copying necessary |
| 945 | * data from fragmented part. |
| 946 | * |
| 947 | * &sk_buff MUST have reference count of 1. |
| 948 | * |
| 949 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 950 | * or value of new tail of skb in the case of success. |
| 951 | * |
| 952 | * All the pointers pointing into skb header may change and must be |
| 953 | * reloaded after call to this function. |
| 954 | */ |
| 955 | |
| 956 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 957 | * when it is necessary. |
| 958 | * 1. It may fail due to malloc failure. |
| 959 | * 2. It may change skb pointers. |
| 960 | * |
| 961 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 962 | */ |
| 963 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) |
| 964 | { |
| 965 | /* If skb has not enough free space at tail, get new one |
| 966 | * plus 128 bytes for future expansions. If we have enough |
| 967 | * room at tail, reallocate without expansion only if skb is cloned. |
| 968 | */ |
| 969 | int i, k, eat = (skb->tail + delta) - skb->end; |
| 970 | |
| 971 | if (eat > 0 || skb_cloned(skb)) { |
| 972 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 973 | GFP_ATOMIC)) |
| 974 | return NULL; |
| 975 | } |
| 976 | |
| 977 | if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta)) |
| 978 | BUG(); |
| 979 | |
| 980 | /* Optimization: no fragments, no reasons to preestimate |
| 981 | * size of pulled pages. Superb. |
| 982 | */ |
| 983 | if (!skb_shinfo(skb)->frag_list) |
| 984 | goto pull_pages; |
| 985 | |
| 986 | /* Estimate size of pulled pages. */ |
| 987 | eat = delta; |
| 988 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 989 | if (skb_shinfo(skb)->frags[i].size >= eat) |
| 990 | goto pull_pages; |
| 991 | eat -= skb_shinfo(skb)->frags[i].size; |
| 992 | } |
| 993 | |
| 994 | /* If we need update frag list, we are in troubles. |
| 995 | * Certainly, it possible to add an offset to skb data, |
| 996 | * but taking into account that pulling is expected to |
| 997 | * be very rare operation, it is worth to fight against |
| 998 | * further bloating skb head and crucify ourselves here instead. |
| 999 | * Pure masohism, indeed. 8)8) |
| 1000 | */ |
| 1001 | if (eat) { |
| 1002 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1003 | struct sk_buff *clone = NULL; |
| 1004 | struct sk_buff *insp = NULL; |
| 1005 | |
| 1006 | do { |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1007 | BUG_ON(!list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | if (list->len <= eat) { |
| 1010 | /* Eaten as whole. */ |
| 1011 | eat -= list->len; |
| 1012 | list = list->next; |
| 1013 | insp = list; |
| 1014 | } else { |
| 1015 | /* Eaten partially. */ |
| 1016 | |
| 1017 | if (skb_shared(list)) { |
| 1018 | /* Sucks! We need to fork list. :-( */ |
| 1019 | clone = skb_clone(list, GFP_ATOMIC); |
| 1020 | if (!clone) |
| 1021 | return NULL; |
| 1022 | insp = list->next; |
| 1023 | list = clone; |
| 1024 | } else { |
| 1025 | /* This may be pulled without |
| 1026 | * problems. */ |
| 1027 | insp = list; |
| 1028 | } |
| 1029 | if (!pskb_pull(list, eat)) { |
| 1030 | if (clone) |
| 1031 | kfree_skb(clone); |
| 1032 | return NULL; |
| 1033 | } |
| 1034 | break; |
| 1035 | } |
| 1036 | } while (eat); |
| 1037 | |
| 1038 | /* Free pulled out fragments. */ |
| 1039 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 1040 | skb_shinfo(skb)->frag_list = list->next; |
| 1041 | kfree_skb(list); |
| 1042 | } |
| 1043 | /* And insert new clone at head. */ |
| 1044 | if (clone) { |
| 1045 | clone->next = list; |
| 1046 | skb_shinfo(skb)->frag_list = clone; |
| 1047 | } |
| 1048 | } |
| 1049 | /* Success! Now we may commit changes to skb data. */ |
| 1050 | |
| 1051 | pull_pages: |
| 1052 | eat = delta; |
| 1053 | k = 0; |
| 1054 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1055 | if (skb_shinfo(skb)->frags[i].size <= eat) { |
| 1056 | put_page(skb_shinfo(skb)->frags[i].page); |
| 1057 | eat -= skb_shinfo(skb)->frags[i].size; |
| 1058 | } else { |
| 1059 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1060 | if (eat) { |
| 1061 | skb_shinfo(skb)->frags[k].page_offset += eat; |
| 1062 | skb_shinfo(skb)->frags[k].size -= eat; |
| 1063 | eat = 0; |
| 1064 | } |
| 1065 | k++; |
| 1066 | } |
| 1067 | } |
| 1068 | skb_shinfo(skb)->nr_frags = k; |
| 1069 | |
| 1070 | skb->tail += delta; |
| 1071 | skb->data_len -= delta; |
| 1072 | |
| 1073 | return skb->tail; |
| 1074 | } |
| 1075 | |
| 1076 | /* Copy some data bits from skb to kernel buffer. */ |
| 1077 | |
| 1078 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 1079 | { |
| 1080 | int i, copy; |
| 1081 | int start = skb_headlen(skb); |
| 1082 | |
| 1083 | if (offset > (int)skb->len - len) |
| 1084 | goto fault; |
| 1085 | |
| 1086 | /* Copy header. */ |
| 1087 | if ((copy = start - offset) > 0) { |
| 1088 | if (copy > len) |
| 1089 | copy = len; |
| 1090 | memcpy(to, skb->data + offset, copy); |
| 1091 | if ((len -= copy) == 0) |
| 1092 | return 0; |
| 1093 | offset += copy; |
| 1094 | to += copy; |
| 1095 | } |
| 1096 | |
| 1097 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1098 | int end; |
| 1099 | |
| 1100 | BUG_TRAP(start <= offset + len); |
| 1101 | |
| 1102 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1103 | if ((copy = end - offset) > 0) { |
| 1104 | u8 *vaddr; |
| 1105 | |
| 1106 | if (copy > len) |
| 1107 | copy = len; |
| 1108 | |
| 1109 | vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); |
| 1110 | memcpy(to, |
| 1111 | vaddr + skb_shinfo(skb)->frags[i].page_offset+ |
| 1112 | offset - start, copy); |
| 1113 | kunmap_skb_frag(vaddr); |
| 1114 | |
| 1115 | if ((len -= copy) == 0) |
| 1116 | return 0; |
| 1117 | offset += copy; |
| 1118 | to += copy; |
| 1119 | } |
| 1120 | start = end; |
| 1121 | } |
| 1122 | |
| 1123 | if (skb_shinfo(skb)->frag_list) { |
| 1124 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1125 | |
| 1126 | for (; list; list = list->next) { |
| 1127 | int end; |
| 1128 | |
| 1129 | BUG_TRAP(start <= offset + len); |
| 1130 | |
| 1131 | end = start + list->len; |
| 1132 | if ((copy = end - offset) > 0) { |
| 1133 | if (copy > len) |
| 1134 | copy = len; |
| 1135 | if (skb_copy_bits(list, offset - start, |
| 1136 | to, copy)) |
| 1137 | goto fault; |
| 1138 | if ((len -= copy) == 0) |
| 1139 | return 0; |
| 1140 | offset += copy; |
| 1141 | to += copy; |
| 1142 | } |
| 1143 | start = end; |
| 1144 | } |
| 1145 | } |
| 1146 | if (!len) |
| 1147 | return 0; |
| 1148 | |
| 1149 | fault: |
| 1150 | return -EFAULT; |
| 1151 | } |
| 1152 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame] | 1153 | /** |
| 1154 | * skb_store_bits - store bits from kernel buffer to skb |
| 1155 | * @skb: destination buffer |
| 1156 | * @offset: offset in destination |
| 1157 | * @from: source buffer |
| 1158 | * @len: number of bytes to copy |
| 1159 | * |
| 1160 | * Copy the specified number of bytes from the source buffer to the |
| 1161 | * destination skb. This function handles all the messy bits of |
| 1162 | * traversing fragment lists and such. |
| 1163 | */ |
| 1164 | |
| 1165 | int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len) |
| 1166 | { |
| 1167 | int i, copy; |
| 1168 | int start = skb_headlen(skb); |
| 1169 | |
| 1170 | if (offset > (int)skb->len - len) |
| 1171 | goto fault; |
| 1172 | |
| 1173 | if ((copy = start - offset) > 0) { |
| 1174 | if (copy > len) |
| 1175 | copy = len; |
| 1176 | memcpy(skb->data + offset, from, copy); |
| 1177 | if ((len -= copy) == 0) |
| 1178 | return 0; |
| 1179 | offset += copy; |
| 1180 | from += copy; |
| 1181 | } |
| 1182 | |
| 1183 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1184 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1185 | int end; |
| 1186 | |
| 1187 | BUG_TRAP(start <= offset + len); |
| 1188 | |
| 1189 | end = start + frag->size; |
| 1190 | if ((copy = end - offset) > 0) { |
| 1191 | u8 *vaddr; |
| 1192 | |
| 1193 | if (copy > len) |
| 1194 | copy = len; |
| 1195 | |
| 1196 | vaddr = kmap_skb_frag(frag); |
| 1197 | memcpy(vaddr + frag->page_offset + offset - start, |
| 1198 | from, copy); |
| 1199 | kunmap_skb_frag(vaddr); |
| 1200 | |
| 1201 | if ((len -= copy) == 0) |
| 1202 | return 0; |
| 1203 | offset += copy; |
| 1204 | from += copy; |
| 1205 | } |
| 1206 | start = end; |
| 1207 | } |
| 1208 | |
| 1209 | if (skb_shinfo(skb)->frag_list) { |
| 1210 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1211 | |
| 1212 | for (; list; list = list->next) { |
| 1213 | int end; |
| 1214 | |
| 1215 | BUG_TRAP(start <= offset + len); |
| 1216 | |
| 1217 | end = start + list->len; |
| 1218 | if ((copy = end - offset) > 0) { |
| 1219 | if (copy > len) |
| 1220 | copy = len; |
| 1221 | if (skb_store_bits(list, offset - start, |
| 1222 | from, copy)) |
| 1223 | goto fault; |
| 1224 | if ((len -= copy) == 0) |
| 1225 | return 0; |
| 1226 | offset += copy; |
| 1227 | from += copy; |
| 1228 | } |
| 1229 | start = end; |
| 1230 | } |
| 1231 | } |
| 1232 | if (!len) |
| 1233 | return 0; |
| 1234 | |
| 1235 | fault: |
| 1236 | return -EFAULT; |
| 1237 | } |
| 1238 | |
| 1239 | EXPORT_SYMBOL(skb_store_bits); |
| 1240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | /* Checksum skb data. */ |
| 1242 | |
| 1243 | unsigned int skb_checksum(const struct sk_buff *skb, int offset, |
| 1244 | int len, unsigned int csum) |
| 1245 | { |
| 1246 | int start = skb_headlen(skb); |
| 1247 | int i, copy = start - offset; |
| 1248 | int pos = 0; |
| 1249 | |
| 1250 | /* Checksum header. */ |
| 1251 | if (copy > 0) { |
| 1252 | if (copy > len) |
| 1253 | copy = len; |
| 1254 | csum = csum_partial(skb->data + offset, copy, csum); |
| 1255 | if ((len -= copy) == 0) |
| 1256 | return csum; |
| 1257 | offset += copy; |
| 1258 | pos = copy; |
| 1259 | } |
| 1260 | |
| 1261 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1262 | int end; |
| 1263 | |
| 1264 | BUG_TRAP(start <= offset + len); |
| 1265 | |
| 1266 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1267 | if ((copy = end - offset) > 0) { |
Al Viro | 44bb936 | 2006-11-14 21:36:14 -0800 | [diff] [blame^] | 1268 | __wsum csum2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | u8 *vaddr; |
| 1270 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1271 | |
| 1272 | if (copy > len) |
| 1273 | copy = len; |
| 1274 | vaddr = kmap_skb_frag(frag); |
| 1275 | csum2 = csum_partial(vaddr + frag->page_offset + |
| 1276 | offset - start, copy, 0); |
| 1277 | kunmap_skb_frag(vaddr); |
| 1278 | csum = csum_block_add(csum, csum2, pos); |
| 1279 | if (!(len -= copy)) |
| 1280 | return csum; |
| 1281 | offset += copy; |
| 1282 | pos += copy; |
| 1283 | } |
| 1284 | start = end; |
| 1285 | } |
| 1286 | |
| 1287 | if (skb_shinfo(skb)->frag_list) { |
| 1288 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1289 | |
| 1290 | for (; list; list = list->next) { |
| 1291 | int end; |
| 1292 | |
| 1293 | BUG_TRAP(start <= offset + len); |
| 1294 | |
| 1295 | end = start + list->len; |
| 1296 | if ((copy = end - offset) > 0) { |
| 1297 | unsigned int csum2; |
| 1298 | if (copy > len) |
| 1299 | copy = len; |
| 1300 | csum2 = skb_checksum(list, offset - start, |
| 1301 | copy, 0); |
| 1302 | csum = csum_block_add(csum, csum2, pos); |
| 1303 | if ((len -= copy) == 0) |
| 1304 | return csum; |
| 1305 | offset += copy; |
| 1306 | pos += copy; |
| 1307 | } |
| 1308 | start = end; |
| 1309 | } |
| 1310 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1311 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
| 1313 | return csum; |
| 1314 | } |
| 1315 | |
| 1316 | /* Both of above in one bottle. */ |
| 1317 | |
| 1318 | unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 1319 | u8 *to, int len, unsigned int csum) |
| 1320 | { |
| 1321 | int start = skb_headlen(skb); |
| 1322 | int i, copy = start - offset; |
| 1323 | int pos = 0; |
| 1324 | |
| 1325 | /* Copy header. */ |
| 1326 | if (copy > 0) { |
| 1327 | if (copy > len) |
| 1328 | copy = len; |
| 1329 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 1330 | copy, csum); |
| 1331 | if ((len -= copy) == 0) |
| 1332 | return csum; |
| 1333 | offset += copy; |
| 1334 | to += copy; |
| 1335 | pos = copy; |
| 1336 | } |
| 1337 | |
| 1338 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1339 | int end; |
| 1340 | |
| 1341 | BUG_TRAP(start <= offset + len); |
| 1342 | |
| 1343 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1344 | if ((copy = end - offset) > 0) { |
| 1345 | unsigned int csum2; |
| 1346 | u8 *vaddr; |
| 1347 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1348 | |
| 1349 | if (copy > len) |
| 1350 | copy = len; |
| 1351 | vaddr = kmap_skb_frag(frag); |
| 1352 | csum2 = csum_partial_copy_nocheck(vaddr + |
| 1353 | frag->page_offset + |
| 1354 | offset - start, to, |
| 1355 | copy, 0); |
| 1356 | kunmap_skb_frag(vaddr); |
| 1357 | csum = csum_block_add(csum, csum2, pos); |
| 1358 | if (!(len -= copy)) |
| 1359 | return csum; |
| 1360 | offset += copy; |
| 1361 | to += copy; |
| 1362 | pos += copy; |
| 1363 | } |
| 1364 | start = end; |
| 1365 | } |
| 1366 | |
| 1367 | if (skb_shinfo(skb)->frag_list) { |
| 1368 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1369 | |
| 1370 | for (; list; list = list->next) { |
| 1371 | unsigned int csum2; |
| 1372 | int end; |
| 1373 | |
| 1374 | BUG_TRAP(start <= offset + len); |
| 1375 | |
| 1376 | end = start + list->len; |
| 1377 | if ((copy = end - offset) > 0) { |
| 1378 | if (copy > len) |
| 1379 | copy = len; |
| 1380 | csum2 = skb_copy_and_csum_bits(list, |
| 1381 | offset - start, |
| 1382 | to, copy, 0); |
| 1383 | csum = csum_block_add(csum, csum2, pos); |
| 1384 | if ((len -= copy) == 0) |
| 1385 | return csum; |
| 1386 | offset += copy; |
| 1387 | to += copy; |
| 1388 | pos += copy; |
| 1389 | } |
| 1390 | start = end; |
| 1391 | } |
| 1392 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1393 | BUG_ON(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | return csum; |
| 1395 | } |
| 1396 | |
| 1397 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 1398 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 1399 | __wsum csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | long csstart; |
| 1401 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1402 | if (skb->ip_summed == CHECKSUM_PARTIAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | csstart = skb->h.raw - skb->data; |
| 1404 | else |
| 1405 | csstart = skb_headlen(skb); |
| 1406 | |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 1407 | BUG_ON(csstart > skb_headlen(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
| 1409 | memcpy(to, skb->data, csstart); |
| 1410 | |
| 1411 | csum = 0; |
| 1412 | if (csstart != skb->len) |
| 1413 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 1414 | skb->len - csstart, 0); |
| 1415 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1416 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | long csstuff = csstart + skb->csum; |
| 1418 | |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 1419 | *((__sum16 *)(to + csstuff)) = csum_fold(csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | /** |
| 1424 | * skb_dequeue - remove from the head of the queue |
| 1425 | * @list: list to dequeue from |
| 1426 | * |
| 1427 | * Remove the head of the list. The list lock is taken so the function |
| 1428 | * may be used safely with other locking list functions. The head item is |
| 1429 | * returned or %NULL if the list is empty. |
| 1430 | */ |
| 1431 | |
| 1432 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 1433 | { |
| 1434 | unsigned long flags; |
| 1435 | struct sk_buff *result; |
| 1436 | |
| 1437 | spin_lock_irqsave(&list->lock, flags); |
| 1438 | result = __skb_dequeue(list); |
| 1439 | spin_unlock_irqrestore(&list->lock, flags); |
| 1440 | return result; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * skb_dequeue_tail - remove from the tail of the queue |
| 1445 | * @list: list to dequeue from |
| 1446 | * |
| 1447 | * Remove the tail of the list. The list lock is taken so the function |
| 1448 | * may be used safely with other locking list functions. The tail item is |
| 1449 | * returned or %NULL if the list is empty. |
| 1450 | */ |
| 1451 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 1452 | { |
| 1453 | unsigned long flags; |
| 1454 | struct sk_buff *result; |
| 1455 | |
| 1456 | spin_lock_irqsave(&list->lock, flags); |
| 1457 | result = __skb_dequeue_tail(list); |
| 1458 | spin_unlock_irqrestore(&list->lock, flags); |
| 1459 | return result; |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * skb_queue_purge - empty a list |
| 1464 | * @list: list to empty |
| 1465 | * |
| 1466 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 1467 | * the list and one reference dropped. This function takes the list |
| 1468 | * lock and is atomic with respect to other list locking functions. |
| 1469 | */ |
| 1470 | void skb_queue_purge(struct sk_buff_head *list) |
| 1471 | { |
| 1472 | struct sk_buff *skb; |
| 1473 | while ((skb = skb_dequeue(list)) != NULL) |
| 1474 | kfree_skb(skb); |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * skb_queue_head - queue a buffer at the list head |
| 1479 | * @list: list to use |
| 1480 | * @newsk: buffer to queue |
| 1481 | * |
| 1482 | * Queue a buffer at the start of the list. This function takes the |
| 1483 | * list lock and can be used safely with other locking &sk_buff functions |
| 1484 | * safely. |
| 1485 | * |
| 1486 | * A buffer cannot be placed on two lists at the same time. |
| 1487 | */ |
| 1488 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 1489 | { |
| 1490 | unsigned long flags; |
| 1491 | |
| 1492 | spin_lock_irqsave(&list->lock, flags); |
| 1493 | __skb_queue_head(list, newsk); |
| 1494 | spin_unlock_irqrestore(&list->lock, flags); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * skb_queue_tail - queue a buffer at the list tail |
| 1499 | * @list: list to use |
| 1500 | * @newsk: buffer to queue |
| 1501 | * |
| 1502 | * Queue a buffer at the tail of the list. This function takes the |
| 1503 | * list lock and can be used safely with other locking &sk_buff functions |
| 1504 | * safely. |
| 1505 | * |
| 1506 | * A buffer cannot be placed on two lists at the same time. |
| 1507 | */ |
| 1508 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 1509 | { |
| 1510 | unsigned long flags; |
| 1511 | |
| 1512 | spin_lock_irqsave(&list->lock, flags); |
| 1513 | __skb_queue_tail(list, newsk); |
| 1514 | spin_unlock_irqrestore(&list->lock, flags); |
| 1515 | } |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | /** |
| 1518 | * skb_unlink - remove a buffer from a list |
| 1519 | * @skb: buffer to remove |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1520 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1522 | * Remove a packet from a list. The list locks are taken and this |
| 1523 | * function is atomic with respect to other list locked calls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1525 | * You must know what list the SKB is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1527 | void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | { |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1529 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1531 | spin_lock_irqsave(&list->lock, flags); |
| 1532 | __skb_unlink(skb, list); |
| 1533 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | /** |
| 1537 | * skb_append - append a buffer |
| 1538 | * @old: buffer to insert after |
| 1539 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1540 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | * |
| 1542 | * Place a packet after a given packet in a list. The list locks are taken |
| 1543 | * and this function is atomic with respect to other list locked calls. |
| 1544 | * A buffer cannot be placed on two lists at the same time. |
| 1545 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1546 | 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] | 1547 | { |
| 1548 | unsigned long flags; |
| 1549 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1550 | spin_lock_irqsave(&list->lock, flags); |
| 1551 | __skb_append(old, newsk, list); |
| 1552 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | /** |
| 1557 | * skb_insert - insert a buffer |
| 1558 | * @old: buffer to insert before |
| 1559 | * @newsk: buffer to insert |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1560 | * @list: list to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | * |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1562 | * Place a packet before a given packet in a list. The list locks are |
| 1563 | * taken and this function is atomic with respect to other list locked |
| 1564 | * calls. |
| 1565 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | * A buffer cannot be placed on two lists at the same time. |
| 1567 | */ |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1568 | 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] | 1569 | { |
| 1570 | unsigned long flags; |
| 1571 | |
David S. Miller | 8728b83 | 2005-08-09 19:25:21 -0700 | [diff] [blame] | 1572 | spin_lock_irqsave(&list->lock, flags); |
| 1573 | __skb_insert(newsk, old->prev, old, list); |
| 1574 | spin_unlock_irqrestore(&list->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | #if 0 |
| 1578 | /* |
| 1579 | * Tune the memory allocator for a new MTU size. |
| 1580 | */ |
| 1581 | void skb_add_mtu(int mtu) |
| 1582 | { |
| 1583 | /* Must match allocation in alloc_skb */ |
| 1584 | mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info); |
| 1585 | |
| 1586 | kmem_add_cache_size(mtu); |
| 1587 | } |
| 1588 | #endif |
| 1589 | |
| 1590 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 1591 | struct sk_buff* skb1, |
| 1592 | const u32 len, const int pos) |
| 1593 | { |
| 1594 | int i; |
| 1595 | |
| 1596 | memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len); |
| 1597 | |
| 1598 | /* And move data appendix as is. */ |
| 1599 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 1600 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 1601 | |
| 1602 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 1603 | skb_shinfo(skb)->nr_frags = 0; |
| 1604 | skb1->data_len = skb->data_len; |
| 1605 | skb1->len += skb1->data_len; |
| 1606 | skb->data_len = 0; |
| 1607 | skb->len = len; |
| 1608 | skb->tail = skb->data + len; |
| 1609 | } |
| 1610 | |
| 1611 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 1612 | struct sk_buff* skb1, |
| 1613 | const u32 len, int pos) |
| 1614 | { |
| 1615 | int i, k = 0; |
| 1616 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 1617 | |
| 1618 | skb_shinfo(skb)->nr_frags = 0; |
| 1619 | skb1->len = skb1->data_len = skb->len - len; |
| 1620 | skb->len = len; |
| 1621 | skb->data_len = len - pos; |
| 1622 | |
| 1623 | for (i = 0; i < nfrags; i++) { |
| 1624 | int size = skb_shinfo(skb)->frags[i].size; |
| 1625 | |
| 1626 | if (pos + size > len) { |
| 1627 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1628 | |
| 1629 | if (pos < len) { |
| 1630 | /* Split frag. |
| 1631 | * We have two variants in this case: |
| 1632 | * 1. Move all the frag to the second |
| 1633 | * part, if it is possible. F.e. |
| 1634 | * this approach is mandatory for TUX, |
| 1635 | * where splitting is expensive. |
| 1636 | * 2. Split is accurately. We make this. |
| 1637 | */ |
| 1638 | get_page(skb_shinfo(skb)->frags[i].page); |
| 1639 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; |
| 1640 | skb_shinfo(skb1)->frags[0].size -= len - pos; |
| 1641 | skb_shinfo(skb)->frags[i].size = len - pos; |
| 1642 | skb_shinfo(skb)->nr_frags++; |
| 1643 | } |
| 1644 | k++; |
| 1645 | } else |
| 1646 | skb_shinfo(skb)->nr_frags++; |
| 1647 | pos += size; |
| 1648 | } |
| 1649 | skb_shinfo(skb1)->nr_frags = k; |
| 1650 | } |
| 1651 | |
| 1652 | /** |
| 1653 | * skb_split - Split fragmented skb to two parts at length len. |
| 1654 | * @skb: the buffer to split |
| 1655 | * @skb1: the buffer to receive the second part |
| 1656 | * @len: new length for skb |
| 1657 | */ |
| 1658 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 1659 | { |
| 1660 | int pos = skb_headlen(skb); |
| 1661 | |
| 1662 | if (len < pos) /* Split line is inside header. */ |
| 1663 | skb_split_inside_header(skb, skb1, len, pos); |
| 1664 | else /* Second chunk has no header, nothing to copy. */ |
| 1665 | skb_split_no_header(skb, skb1, len, pos); |
| 1666 | } |
| 1667 | |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 1668 | /** |
| 1669 | * skb_prepare_seq_read - Prepare a sequential read of skb data |
| 1670 | * @skb: the buffer to read |
| 1671 | * @from: lower offset of data to be read |
| 1672 | * @to: upper offset of data to be read |
| 1673 | * @st: state variable |
| 1674 | * |
| 1675 | * Initializes the specified state variable. Must be called before |
| 1676 | * invoking skb_seq_read() for the first time. |
| 1677 | */ |
| 1678 | void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, |
| 1679 | unsigned int to, struct skb_seq_state *st) |
| 1680 | { |
| 1681 | st->lower_offset = from; |
| 1682 | st->upper_offset = to; |
| 1683 | st->root_skb = st->cur_skb = skb; |
| 1684 | st->frag_idx = st->stepped_offset = 0; |
| 1685 | st->frag_data = NULL; |
| 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * skb_seq_read - Sequentially read skb data |
| 1690 | * @consumed: number of bytes consumed by the caller so far |
| 1691 | * @data: destination pointer for data to be returned |
| 1692 | * @st: state variable |
| 1693 | * |
| 1694 | * Reads a block of skb data at &consumed relative to the |
| 1695 | * lower offset specified to skb_prepare_seq_read(). Assigns |
| 1696 | * the head of the data block to &data and returns the length |
| 1697 | * of the block or 0 if the end of the skb data or the upper |
| 1698 | * offset has been reached. |
| 1699 | * |
| 1700 | * The caller is not required to consume all of the data |
| 1701 | * returned, i.e. &consumed is typically set to the number |
| 1702 | * of bytes already consumed and the next call to |
| 1703 | * skb_seq_read() will return the remaining part of the block. |
| 1704 | * |
| 1705 | * Note: The size of each block of data returned can be arbitary, |
| 1706 | * this limitation is the cost for zerocopy seqeuental |
| 1707 | * reads of potentially non linear data. |
| 1708 | * |
| 1709 | * Note: Fragment lists within fragments are not implemented |
| 1710 | * at the moment, state->root_skb could be replaced with |
| 1711 | * a stack for this purpose. |
| 1712 | */ |
| 1713 | unsigned int skb_seq_read(unsigned int consumed, const u8 **data, |
| 1714 | struct skb_seq_state *st) |
| 1715 | { |
| 1716 | unsigned int block_limit, abs_offset = consumed + st->lower_offset; |
| 1717 | skb_frag_t *frag; |
| 1718 | |
| 1719 | if (unlikely(abs_offset >= st->upper_offset)) |
| 1720 | return 0; |
| 1721 | |
| 1722 | next_skb: |
| 1723 | block_limit = skb_headlen(st->cur_skb); |
| 1724 | |
| 1725 | if (abs_offset < block_limit) { |
| 1726 | *data = st->cur_skb->data + abs_offset; |
| 1727 | return block_limit - abs_offset; |
| 1728 | } |
| 1729 | |
| 1730 | if (st->frag_idx == 0 && !st->frag_data) |
| 1731 | st->stepped_offset += skb_headlen(st->cur_skb); |
| 1732 | |
| 1733 | while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { |
| 1734 | frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; |
| 1735 | block_limit = frag->size + st->stepped_offset; |
| 1736 | |
| 1737 | if (abs_offset < block_limit) { |
| 1738 | if (!st->frag_data) |
| 1739 | st->frag_data = kmap_skb_frag(frag); |
| 1740 | |
| 1741 | *data = (u8 *) st->frag_data + frag->page_offset + |
| 1742 | (abs_offset - st->stepped_offset); |
| 1743 | |
| 1744 | return block_limit - abs_offset; |
| 1745 | } |
| 1746 | |
| 1747 | if (st->frag_data) { |
| 1748 | kunmap_skb_frag(st->frag_data); |
| 1749 | st->frag_data = NULL; |
| 1750 | } |
| 1751 | |
| 1752 | st->frag_idx++; |
| 1753 | st->stepped_offset += frag->size; |
| 1754 | } |
| 1755 | |
| 1756 | if (st->cur_skb->next) { |
| 1757 | st->cur_skb = st->cur_skb->next; |
| 1758 | st->frag_idx = 0; |
| 1759 | goto next_skb; |
| 1760 | } else if (st->root_skb == st->cur_skb && |
| 1761 | skb_shinfo(st->root_skb)->frag_list) { |
| 1762 | st->cur_skb = skb_shinfo(st->root_skb)->frag_list; |
| 1763 | goto next_skb; |
| 1764 | } |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | /** |
| 1770 | * skb_abort_seq_read - Abort a sequential read of skb data |
| 1771 | * @st: state variable |
| 1772 | * |
| 1773 | * Must be called if skb_seq_read() was not called until it |
| 1774 | * returned 0. |
| 1775 | */ |
| 1776 | void skb_abort_seq_read(struct skb_seq_state *st) |
| 1777 | { |
| 1778 | if (st->frag_data) |
| 1779 | kunmap_skb_frag(st->frag_data); |
| 1780 | } |
| 1781 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 1782 | #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) |
| 1783 | |
| 1784 | static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, |
| 1785 | struct ts_config *conf, |
| 1786 | struct ts_state *state) |
| 1787 | { |
| 1788 | return skb_seq_read(offset, text, TS_SKB_CB(state)); |
| 1789 | } |
| 1790 | |
| 1791 | static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) |
| 1792 | { |
| 1793 | skb_abort_seq_read(TS_SKB_CB(state)); |
| 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * skb_find_text - Find a text pattern in skb data |
| 1798 | * @skb: the buffer to look in |
| 1799 | * @from: search offset |
| 1800 | * @to: search limit |
| 1801 | * @config: textsearch configuration |
| 1802 | * @state: uninitialized textsearch state variable |
| 1803 | * |
| 1804 | * Finds a pattern in the skb data according to the specified |
| 1805 | * textsearch configuration. Use textsearch_next() to retrieve |
| 1806 | * subsequent occurrences of the pattern. Returns the offset |
| 1807 | * to the first occurrence or UINT_MAX if no match was found. |
| 1808 | */ |
| 1809 | unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, |
| 1810 | unsigned int to, struct ts_config *config, |
| 1811 | struct ts_state *state) |
| 1812 | { |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 1813 | unsigned int ret; |
| 1814 | |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 1815 | config->get_next_block = skb_ts_get_next_block; |
| 1816 | config->finish = skb_ts_finish; |
| 1817 | |
| 1818 | skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); |
| 1819 | |
Phil Oester | f72b948 | 2006-06-26 00:00:57 -0700 | [diff] [blame] | 1820 | ret = textsearch_find(config, state); |
| 1821 | return (ret <= to - from ? ret : UINT_MAX); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1824 | /** |
| 1825 | * skb_append_datato_frags: - append the user data to a skb |
| 1826 | * @sk: sock structure |
| 1827 | * @skb: skb structure to be appened with user data. |
| 1828 | * @getfrag: call back function to be used for getting the user data |
| 1829 | * @from: pointer to user message iov |
| 1830 | * @length: length of the iov message |
| 1831 | * |
| 1832 | * Description: This procedure append the user data in the fragment part |
| 1833 | * of the skb if any page alloc fails user this procedure returns -ENOMEM |
| 1834 | */ |
| 1835 | int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, |
Martin Waitz | dab9630 | 2005-12-05 13:40:12 -0800 | [diff] [blame] | 1836 | int (*getfrag)(void *from, char *to, int offset, |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 1837 | int len, int odd, struct sk_buff *skb), |
| 1838 | void *from, int length) |
| 1839 | { |
| 1840 | int frg_cnt = 0; |
| 1841 | skb_frag_t *frag = NULL; |
| 1842 | struct page *page = NULL; |
| 1843 | int copy, left; |
| 1844 | int offset = 0; |
| 1845 | int ret; |
| 1846 | |
| 1847 | do { |
| 1848 | /* Return error if we don't have space for new frag */ |
| 1849 | frg_cnt = skb_shinfo(skb)->nr_frags; |
| 1850 | if (frg_cnt >= MAX_SKB_FRAGS) |
| 1851 | return -EFAULT; |
| 1852 | |
| 1853 | /* allocate a new page for next frag */ |
| 1854 | page = alloc_pages(sk->sk_allocation, 0); |
| 1855 | |
| 1856 | /* If alloc_page fails just return failure and caller will |
| 1857 | * free previous allocated pages by doing kfree_skb() |
| 1858 | */ |
| 1859 | if (page == NULL) |
| 1860 | return -ENOMEM; |
| 1861 | |
| 1862 | /* initialize the next frag */ |
| 1863 | sk->sk_sndmsg_page = page; |
| 1864 | sk->sk_sndmsg_off = 0; |
| 1865 | skb_fill_page_desc(skb, frg_cnt, page, 0, 0); |
| 1866 | skb->truesize += PAGE_SIZE; |
| 1867 | atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); |
| 1868 | |
| 1869 | /* get the new initialized frag */ |
| 1870 | frg_cnt = skb_shinfo(skb)->nr_frags; |
| 1871 | frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; |
| 1872 | |
| 1873 | /* copy the user data to page */ |
| 1874 | left = PAGE_SIZE - frag->page_offset; |
| 1875 | copy = (length > left)? left : length; |
| 1876 | |
| 1877 | ret = getfrag(from, (page_address(frag->page) + |
| 1878 | frag->page_offset + frag->size), |
| 1879 | offset, copy, 0, skb); |
| 1880 | if (ret < 0) |
| 1881 | return -EFAULT; |
| 1882 | |
| 1883 | /* copy was successful so update the size parameters */ |
| 1884 | sk->sk_sndmsg_off += copy; |
| 1885 | frag->size += copy; |
| 1886 | skb->len += copy; |
| 1887 | skb->data_len += copy; |
| 1888 | offset += copy; |
| 1889 | length -= copy; |
| 1890 | |
| 1891 | } while (length > 0); |
| 1892 | |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 1896 | /** |
| 1897 | * skb_pull_rcsum - pull skb and update receive checksum |
| 1898 | * @skb: buffer to update |
| 1899 | * @start: start of data before pull |
| 1900 | * @len: length of data pulled |
| 1901 | * |
| 1902 | * This function performs an skb_pull on the packet and updates |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1903 | * update the CHECKSUM_COMPLETE checksum. It should be used on |
| 1904 | * receive path processing instead of skb_pull unless you know |
| 1905 | * that the checksum difference is zero (e.g., a valid IP header) |
| 1906 | * or you are setting ip_summed to CHECKSUM_NONE. |
Herbert Xu | cbb042f | 2006-03-20 22:43:56 -0800 | [diff] [blame] | 1907 | */ |
| 1908 | unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) |
| 1909 | { |
| 1910 | BUG_ON(len > skb->len); |
| 1911 | skb->len -= len; |
| 1912 | BUG_ON(skb->len < skb->data_len); |
| 1913 | skb_postpull_rcsum(skb, skb->data, len); |
| 1914 | return skb->data += len; |
| 1915 | } |
| 1916 | |
Arnaldo Carvalho de Melo | f94691a | 2006-03-20 22:47:55 -0800 | [diff] [blame] | 1917 | EXPORT_SYMBOL_GPL(skb_pull_rcsum); |
| 1918 | |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1919 | /** |
| 1920 | * skb_segment - Perform protocol segmentation on skb. |
| 1921 | * @skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 1922 | * @features: features for the output path (see dev->features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1923 | * |
| 1924 | * This function performs segmentation on the given skb. It returns |
| 1925 | * the segment at the given position. It returns NULL if there are |
| 1926 | * no more segments to generate, or when an error is encountered. |
| 1927 | */ |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 1928 | struct sk_buff *skb_segment(struct sk_buff *skb, int features) |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1929 | { |
| 1930 | struct sk_buff *segs = NULL; |
| 1931 | struct sk_buff *tail = NULL; |
| 1932 | unsigned int mss = skb_shinfo(skb)->gso_size; |
| 1933 | unsigned int doffset = skb->data - skb->mac.raw; |
| 1934 | unsigned int offset = doffset; |
| 1935 | unsigned int headroom; |
| 1936 | unsigned int len; |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 1937 | int sg = features & NETIF_F_SG; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1938 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 1939 | int err = -ENOMEM; |
| 1940 | int i = 0; |
| 1941 | int pos; |
| 1942 | |
| 1943 | __skb_push(skb, doffset); |
| 1944 | headroom = skb_headroom(skb); |
| 1945 | pos = skb_headlen(skb); |
| 1946 | |
| 1947 | do { |
| 1948 | struct sk_buff *nskb; |
| 1949 | skb_frag_t *frag; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 1950 | int hsize; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1951 | int k; |
| 1952 | int size; |
| 1953 | |
| 1954 | len = skb->len - offset; |
| 1955 | if (len > mss) |
| 1956 | len = mss; |
| 1957 | |
| 1958 | hsize = skb_headlen(skb) - offset; |
| 1959 | if (hsize < 0) |
| 1960 | hsize = 0; |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 1961 | if (hsize > len || !sg) |
| 1962 | hsize = len; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1963 | |
Herbert Xu | c8884ed | 2006-10-29 15:59:41 -0800 | [diff] [blame] | 1964 | nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1965 | if (unlikely(!nskb)) |
| 1966 | goto err; |
| 1967 | |
| 1968 | if (segs) |
| 1969 | tail->next = nskb; |
| 1970 | else |
| 1971 | segs = nskb; |
| 1972 | tail = nskb; |
| 1973 | |
| 1974 | nskb->dev = skb->dev; |
| 1975 | nskb->priority = skb->priority; |
| 1976 | nskb->protocol = skb->protocol; |
| 1977 | nskb->dst = dst_clone(skb->dst); |
| 1978 | memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); |
| 1979 | nskb->pkt_type = skb->pkt_type; |
| 1980 | nskb->mac_len = skb->mac_len; |
| 1981 | |
| 1982 | skb_reserve(nskb, headroom); |
| 1983 | nskb->mac.raw = nskb->data; |
| 1984 | nskb->nh.raw = nskb->data + skb->mac_len; |
| 1985 | nskb->h.raw = nskb->nh.raw + (skb->h.raw - skb->nh.raw); |
| 1986 | memcpy(skb_put(nskb, doffset), skb->data, doffset); |
| 1987 | |
| 1988 | if (!sg) { |
| 1989 | nskb->csum = skb_copy_and_csum_bits(skb, offset, |
| 1990 | skb_put(nskb, len), |
| 1991 | len, 0); |
| 1992 | continue; |
| 1993 | } |
| 1994 | |
| 1995 | frag = skb_shinfo(nskb)->frags; |
| 1996 | k = 0; |
| 1997 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1998 | nskb->ip_summed = CHECKSUM_PARTIAL; |
Herbert Xu | f4c50d9 | 2006-06-22 03:02:40 -0700 | [diff] [blame] | 1999 | nskb->csum = skb->csum; |
| 2000 | memcpy(skb_put(nskb, hsize), skb->data + offset, hsize); |
| 2001 | |
| 2002 | while (pos < offset + len) { |
| 2003 | BUG_ON(i >= nfrags); |
| 2004 | |
| 2005 | *frag = skb_shinfo(skb)->frags[i]; |
| 2006 | get_page(frag->page); |
| 2007 | size = frag->size; |
| 2008 | |
| 2009 | if (pos < offset) { |
| 2010 | frag->page_offset += offset - pos; |
| 2011 | frag->size -= offset - pos; |
| 2012 | } |
| 2013 | |
| 2014 | k++; |
| 2015 | |
| 2016 | if (pos + size <= offset + len) { |
| 2017 | i++; |
| 2018 | pos += size; |
| 2019 | } else { |
| 2020 | frag->size -= pos + size - (offset + len); |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | frag++; |
| 2025 | } |
| 2026 | |
| 2027 | skb_shinfo(nskb)->nr_frags = k; |
| 2028 | nskb->data_len = len - hsize; |
| 2029 | nskb->len += nskb->data_len; |
| 2030 | nskb->truesize += nskb->data_len; |
| 2031 | } while ((offset += len) < skb->len); |
| 2032 | |
| 2033 | return segs; |
| 2034 | |
| 2035 | err: |
| 2036 | while ((skb = segs)) { |
| 2037 | segs = skb->next; |
| 2038 | kfree(skb); |
| 2039 | } |
| 2040 | return ERR_PTR(err); |
| 2041 | } |
| 2042 | |
| 2043 | EXPORT_SYMBOL_GPL(skb_segment); |
| 2044 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | void __init skb_init(void) |
| 2046 | { |
| 2047 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", |
| 2048 | sizeof(struct sk_buff), |
| 2049 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 2050 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | NULL, NULL); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 2052 | skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", |
| 2053 | (2*sizeof(struct sk_buff)) + |
| 2054 | sizeof(atomic_t), |
| 2055 | 0, |
Alexey Dobriyan | e5d679f33 | 2006-08-26 19:25:52 -0700 | [diff] [blame] | 2056 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 2057 | NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | EXPORT_SYMBOL(___pskb_trim); |
| 2061 | EXPORT_SYMBOL(__kfree_skb); |
Jörn Engel | 231d06a | 2006-03-20 21:28:35 -0800 | [diff] [blame] | 2062 | EXPORT_SYMBOL(kfree_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | EXPORT_SYMBOL(__pskb_pull_tail); |
David S. Miller | d179cd1 | 2005-08-17 14:57:30 -0700 | [diff] [blame] | 2064 | EXPORT_SYMBOL(__alloc_skb); |
Christoph Hellwig | 8af2745 | 2006-07-31 22:35:23 -0700 | [diff] [blame] | 2065 | EXPORT_SYMBOL(__netdev_alloc_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | EXPORT_SYMBOL(pskb_copy); |
| 2067 | EXPORT_SYMBOL(pskb_expand_head); |
| 2068 | EXPORT_SYMBOL(skb_checksum); |
| 2069 | EXPORT_SYMBOL(skb_clone); |
| 2070 | EXPORT_SYMBOL(skb_clone_fraglist); |
| 2071 | EXPORT_SYMBOL(skb_copy); |
| 2072 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
| 2073 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
| 2074 | EXPORT_SYMBOL(skb_copy_bits); |
| 2075 | EXPORT_SYMBOL(skb_copy_expand); |
| 2076 | EXPORT_SYMBOL(skb_over_panic); |
| 2077 | EXPORT_SYMBOL(skb_pad); |
| 2078 | EXPORT_SYMBOL(skb_realloc_headroom); |
| 2079 | EXPORT_SYMBOL(skb_under_panic); |
| 2080 | EXPORT_SYMBOL(skb_dequeue); |
| 2081 | EXPORT_SYMBOL(skb_dequeue_tail); |
| 2082 | EXPORT_SYMBOL(skb_insert); |
| 2083 | EXPORT_SYMBOL(skb_queue_purge); |
| 2084 | EXPORT_SYMBOL(skb_queue_head); |
| 2085 | EXPORT_SYMBOL(skb_queue_tail); |
| 2086 | EXPORT_SYMBOL(skb_unlink); |
| 2087 | EXPORT_SYMBOL(skb_append); |
| 2088 | EXPORT_SYMBOL(skb_split); |
Thomas Graf | 677e90e | 2005-06-23 20:59:51 -0700 | [diff] [blame] | 2089 | EXPORT_SYMBOL(skb_prepare_seq_read); |
| 2090 | EXPORT_SYMBOL(skb_seq_read); |
| 2091 | EXPORT_SYMBOL(skb_abort_seq_read); |
Thomas Graf | 3fc7e8a | 2005-06-23 21:00:17 -0700 | [diff] [blame] | 2092 | EXPORT_SYMBOL(skb_find_text); |
Ananda Raju | e89e9cf | 2005-10-18 15:46:41 -0700 | [diff] [blame] | 2093 | EXPORT_SYMBOL(skb_append_datato_frags); |