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