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 | |
| 41 | #include <linux/config.h> |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/types.h> |
| 44 | #include <linux/kernel.h> |
| 45 | #include <linux/sched.h> |
| 46 | #include <linux/mm.h> |
| 47 | #include <linux/interrupt.h> |
| 48 | #include <linux/in.h> |
| 49 | #include <linux/inet.h> |
| 50 | #include <linux/slab.h> |
| 51 | #include <linux/netdevice.h> |
| 52 | #ifdef CONFIG_NET_CLS_ACT |
| 53 | #include <net/pkt_sched.h> |
| 54 | #endif |
| 55 | #include <linux/string.h> |
| 56 | #include <linux/skbuff.h> |
| 57 | #include <linux/cache.h> |
| 58 | #include <linux/rtnetlink.h> |
| 59 | #include <linux/init.h> |
| 60 | #include <linux/highmem.h> |
| 61 | |
| 62 | #include <net/protocol.h> |
| 63 | #include <net/dst.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/checksum.h> |
| 66 | #include <net/xfrm.h> |
| 67 | |
| 68 | #include <asm/uaccess.h> |
| 69 | #include <asm/system.h> |
| 70 | |
| 71 | static kmem_cache_t *skbuff_head_cache; |
| 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 | { |
| 89 | printk(KERN_INFO "skput:over: %p:%d put:%d dev:%s", |
| 90 | here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>"); |
| 91 | BUG(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * skb_under_panic - private function |
| 96 | * @skb: buffer |
| 97 | * @sz: size |
| 98 | * @here: address |
| 99 | * |
| 100 | * Out of line support code for skb_push(). Not user callable. |
| 101 | */ |
| 102 | |
| 103 | void skb_under_panic(struct sk_buff *skb, int sz, void *here) |
| 104 | { |
| 105 | printk(KERN_INFO "skput:under: %p:%d put:%d dev:%s", |
| 106 | here, skb->len, sz, skb->dev ? skb->dev->name : "<NULL>"); |
| 107 | BUG(); |
| 108 | } |
| 109 | |
| 110 | /* Allocate a new skbuff. We do this ourselves so we can fill in a few |
| 111 | * 'private' fields and also do memory statistics to find all the |
| 112 | * [BEEP] leaks. |
| 113 | * |
| 114 | */ |
| 115 | |
| 116 | /** |
| 117 | * alloc_skb - allocate a network buffer |
| 118 | * @size: size to allocate |
| 119 | * @gfp_mask: allocation mask |
| 120 | * |
| 121 | * Allocate a new &sk_buff. The returned buffer has no headroom and a |
| 122 | * tail room of size bytes. The object has a reference count of one. |
| 123 | * The return is the buffer. On a failure the return is %NULL. |
| 124 | * |
| 125 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 126 | * %GFP_ATOMIC. |
| 127 | */ |
| 128 | struct sk_buff *alloc_skb(unsigned int size, int gfp_mask) |
| 129 | { |
| 130 | struct sk_buff *skb; |
| 131 | u8 *data; |
| 132 | |
| 133 | /* Get the HEAD */ |
| 134 | skb = kmem_cache_alloc(skbuff_head_cache, |
| 135 | gfp_mask & ~__GFP_DMA); |
| 136 | if (!skb) |
| 137 | goto out; |
| 138 | |
| 139 | /* Get the DATA. Size must match skb_add_mtu(). */ |
| 140 | size = SKB_DATA_ALIGN(size); |
| 141 | data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); |
| 142 | if (!data) |
| 143 | goto nodata; |
| 144 | |
| 145 | memset(skb, 0, offsetof(struct sk_buff, truesize)); |
| 146 | skb->truesize = size + sizeof(struct sk_buff); |
| 147 | atomic_set(&skb->users, 1); |
| 148 | skb->head = data; |
| 149 | skb->data = data; |
| 150 | skb->tail = data; |
| 151 | skb->end = data + size; |
| 152 | |
| 153 | atomic_set(&(skb_shinfo(skb)->dataref), 1); |
| 154 | skb_shinfo(skb)->nr_frags = 0; |
| 155 | skb_shinfo(skb)->tso_size = 0; |
| 156 | skb_shinfo(skb)->tso_segs = 0; |
| 157 | skb_shinfo(skb)->frag_list = NULL; |
| 158 | out: |
| 159 | return skb; |
| 160 | nodata: |
| 161 | kmem_cache_free(skbuff_head_cache, skb); |
| 162 | skb = NULL; |
| 163 | goto out; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * alloc_skb_from_cache - allocate a network buffer |
| 168 | * @cp: kmem_cache from which to allocate the data area |
| 169 | * (object size must be big enough for @size bytes + skb overheads) |
| 170 | * @size: size to allocate |
| 171 | * @gfp_mask: allocation mask |
| 172 | * |
| 173 | * Allocate a new &sk_buff. The returned buffer has no headroom and |
| 174 | * tail room of size bytes. The object has a reference count of one. |
| 175 | * The return is the buffer. On a failure the return is %NULL. |
| 176 | * |
| 177 | * Buffers may only be allocated from interrupts using a @gfp_mask of |
| 178 | * %GFP_ATOMIC. |
| 179 | */ |
| 180 | struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp, |
| 181 | unsigned int size, int gfp_mask) |
| 182 | { |
| 183 | struct sk_buff *skb; |
| 184 | u8 *data; |
| 185 | |
| 186 | /* Get the HEAD */ |
| 187 | skb = kmem_cache_alloc(skbuff_head_cache, |
| 188 | gfp_mask & ~__GFP_DMA); |
| 189 | if (!skb) |
| 190 | goto out; |
| 191 | |
| 192 | /* Get the DATA. */ |
| 193 | size = SKB_DATA_ALIGN(size); |
| 194 | data = kmem_cache_alloc(cp, gfp_mask); |
| 195 | if (!data) |
| 196 | goto nodata; |
| 197 | |
| 198 | memset(skb, 0, offsetof(struct sk_buff, truesize)); |
| 199 | skb->truesize = size + sizeof(struct sk_buff); |
| 200 | atomic_set(&skb->users, 1); |
| 201 | skb->head = data; |
| 202 | skb->data = data; |
| 203 | skb->tail = data; |
| 204 | skb->end = data + size; |
| 205 | |
| 206 | atomic_set(&(skb_shinfo(skb)->dataref), 1); |
| 207 | skb_shinfo(skb)->nr_frags = 0; |
| 208 | skb_shinfo(skb)->tso_size = 0; |
| 209 | skb_shinfo(skb)->tso_segs = 0; |
| 210 | skb_shinfo(skb)->frag_list = NULL; |
| 211 | out: |
| 212 | return skb; |
| 213 | nodata: |
| 214 | kmem_cache_free(skbuff_head_cache, skb); |
| 215 | skb = NULL; |
| 216 | goto out; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static void skb_drop_fraglist(struct sk_buff *skb) |
| 221 | { |
| 222 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 223 | |
| 224 | skb_shinfo(skb)->frag_list = NULL; |
| 225 | |
| 226 | do { |
| 227 | struct sk_buff *this = list; |
| 228 | list = list->next; |
| 229 | kfree_skb(this); |
| 230 | } while (list); |
| 231 | } |
| 232 | |
| 233 | static void skb_clone_fraglist(struct sk_buff *skb) |
| 234 | { |
| 235 | struct sk_buff *list; |
| 236 | |
| 237 | for (list = skb_shinfo(skb)->frag_list; list; list = list->next) |
| 238 | skb_get(list); |
| 239 | } |
| 240 | |
| 241 | void skb_release_data(struct sk_buff *skb) |
| 242 | { |
| 243 | if (!skb->cloned || |
| 244 | !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, |
| 245 | &skb_shinfo(skb)->dataref)) { |
| 246 | if (skb_shinfo(skb)->nr_frags) { |
| 247 | int i; |
| 248 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 249 | put_page(skb_shinfo(skb)->frags[i].page); |
| 250 | } |
| 251 | |
| 252 | if (skb_shinfo(skb)->frag_list) |
| 253 | skb_drop_fraglist(skb); |
| 254 | |
| 255 | kfree(skb->head); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Free an skbuff by memory without cleaning the state. |
| 261 | */ |
| 262 | void kfree_skbmem(struct sk_buff *skb) |
| 263 | { |
| 264 | skb_release_data(skb); |
| 265 | kmem_cache_free(skbuff_head_cache, skb); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * __kfree_skb - private function |
| 270 | * @skb: buffer |
| 271 | * |
| 272 | * Free an sk_buff. Release anything attached to the buffer. |
| 273 | * Clean the state. This is an internal helper function. Users should |
| 274 | * always call kfree_skb |
| 275 | */ |
| 276 | |
| 277 | void __kfree_skb(struct sk_buff *skb) |
| 278 | { |
| 279 | if (skb->list) { |
| 280 | printk(KERN_WARNING "Warning: kfree_skb passed an skb still " |
| 281 | "on a list (from %p).\n", NET_CALLER(skb)); |
| 282 | BUG(); |
| 283 | } |
| 284 | |
| 285 | dst_release(skb->dst); |
| 286 | #ifdef CONFIG_XFRM |
| 287 | secpath_put(skb->sp); |
| 288 | #endif |
| 289 | if(skb->destructor) { |
| 290 | if (in_irq()) |
| 291 | printk(KERN_WARNING "Warning: kfree_skb on " |
| 292 | "hard IRQ %p\n", NET_CALLER(skb)); |
| 293 | skb->destructor(skb); |
| 294 | } |
| 295 | #ifdef CONFIG_NETFILTER |
| 296 | nf_conntrack_put(skb->nfct); |
| 297 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 298 | nf_bridge_put(skb->nf_bridge); |
| 299 | #endif |
| 300 | #endif |
| 301 | /* XXX: IS this still necessary? - JHS */ |
| 302 | #ifdef CONFIG_NET_SCHED |
| 303 | skb->tc_index = 0; |
| 304 | #ifdef CONFIG_NET_CLS_ACT |
| 305 | skb->tc_verd = 0; |
| 306 | skb->tc_classid = 0; |
| 307 | #endif |
| 308 | #endif |
| 309 | |
| 310 | kfree_skbmem(skb); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * skb_clone - duplicate an sk_buff |
| 315 | * @skb: buffer to clone |
| 316 | * @gfp_mask: allocation priority |
| 317 | * |
| 318 | * Duplicate an &sk_buff. The new one is not owned by a socket. Both |
| 319 | * copies share the same packet data but not structure. The new |
| 320 | * buffer has a reference count of 1. If the allocation fails the |
| 321 | * function returns %NULL otherwise the new buffer is returned. |
| 322 | * |
| 323 | * If this function is called from an interrupt gfp_mask() must be |
| 324 | * %GFP_ATOMIC. |
| 325 | */ |
| 326 | |
| 327 | struct sk_buff *skb_clone(struct sk_buff *skb, int gfp_mask) |
| 328 | { |
| 329 | struct sk_buff *n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); |
| 330 | |
| 331 | if (!n) |
| 332 | return NULL; |
| 333 | |
| 334 | #define C(x) n->x = skb->x |
| 335 | |
| 336 | n->next = n->prev = NULL; |
| 337 | n->list = NULL; |
| 338 | n->sk = NULL; |
| 339 | C(stamp); |
| 340 | C(dev); |
| 341 | C(real_dev); |
| 342 | C(h); |
| 343 | C(nh); |
| 344 | C(mac); |
| 345 | C(dst); |
| 346 | dst_clone(skb->dst); |
| 347 | C(sp); |
| 348 | #ifdef CONFIG_INET |
| 349 | secpath_get(skb->sp); |
| 350 | #endif |
| 351 | memcpy(n->cb, skb->cb, sizeof(skb->cb)); |
| 352 | C(len); |
| 353 | C(data_len); |
| 354 | C(csum); |
| 355 | C(local_df); |
| 356 | n->cloned = 1; |
| 357 | n->nohdr = 0; |
| 358 | C(pkt_type); |
| 359 | C(ip_summed); |
| 360 | C(priority); |
| 361 | C(protocol); |
| 362 | C(security); |
| 363 | n->destructor = NULL; |
| 364 | #ifdef CONFIG_NETFILTER |
| 365 | C(nfmark); |
| 366 | C(nfcache); |
| 367 | C(nfct); |
| 368 | nf_conntrack_get(skb->nfct); |
| 369 | C(nfctinfo); |
| 370 | #ifdef CONFIG_NETFILTER_DEBUG |
| 371 | C(nf_debug); |
| 372 | #endif |
| 373 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 374 | C(nf_bridge); |
| 375 | nf_bridge_get(skb->nf_bridge); |
| 376 | #endif |
| 377 | #endif /*CONFIG_NETFILTER*/ |
| 378 | #if defined(CONFIG_HIPPI) |
| 379 | C(private); |
| 380 | #endif |
| 381 | #ifdef CONFIG_NET_SCHED |
| 382 | C(tc_index); |
| 383 | #ifdef CONFIG_NET_CLS_ACT |
| 384 | n->tc_verd = SET_TC_VERD(skb->tc_verd,0); |
| 385 | n->tc_verd = CLR_TC_OK2MUNGE(skb->tc_verd); |
| 386 | n->tc_verd = CLR_TC_MUNGED(skb->tc_verd); |
| 387 | C(input_dev); |
| 388 | C(tc_classid); |
| 389 | #endif |
| 390 | |
| 391 | #endif |
| 392 | C(truesize); |
| 393 | atomic_set(&n->users, 1); |
| 394 | C(head); |
| 395 | C(data); |
| 396 | C(tail); |
| 397 | C(end); |
| 398 | |
| 399 | atomic_inc(&(skb_shinfo(skb)->dataref)); |
| 400 | skb->cloned = 1; |
| 401 | |
| 402 | return n; |
| 403 | } |
| 404 | |
| 405 | static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
| 406 | { |
| 407 | /* |
| 408 | * Shift between the two data areas in bytes |
| 409 | */ |
| 410 | unsigned long offset = new->data - old->data; |
| 411 | |
| 412 | new->list = NULL; |
| 413 | new->sk = NULL; |
| 414 | new->dev = old->dev; |
| 415 | new->real_dev = old->real_dev; |
| 416 | new->priority = old->priority; |
| 417 | new->protocol = old->protocol; |
| 418 | new->dst = dst_clone(old->dst); |
| 419 | #ifdef CONFIG_INET |
| 420 | new->sp = secpath_get(old->sp); |
| 421 | #endif |
| 422 | new->h.raw = old->h.raw + offset; |
| 423 | new->nh.raw = old->nh.raw + offset; |
| 424 | new->mac.raw = old->mac.raw + offset; |
| 425 | memcpy(new->cb, old->cb, sizeof(old->cb)); |
| 426 | new->local_df = old->local_df; |
| 427 | new->pkt_type = old->pkt_type; |
| 428 | new->stamp = old->stamp; |
| 429 | new->destructor = NULL; |
| 430 | new->security = old->security; |
| 431 | #ifdef CONFIG_NETFILTER |
| 432 | new->nfmark = old->nfmark; |
| 433 | new->nfcache = old->nfcache; |
| 434 | new->nfct = old->nfct; |
| 435 | nf_conntrack_get(old->nfct); |
| 436 | new->nfctinfo = old->nfctinfo; |
| 437 | #ifdef CONFIG_NETFILTER_DEBUG |
| 438 | new->nf_debug = old->nf_debug; |
| 439 | #endif |
| 440 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 441 | new->nf_bridge = old->nf_bridge; |
| 442 | nf_bridge_get(old->nf_bridge); |
| 443 | #endif |
| 444 | #endif |
| 445 | #ifdef CONFIG_NET_SCHED |
| 446 | #ifdef CONFIG_NET_CLS_ACT |
| 447 | new->tc_verd = old->tc_verd; |
| 448 | #endif |
| 449 | new->tc_index = old->tc_index; |
| 450 | #endif |
| 451 | atomic_set(&new->users, 1); |
| 452 | skb_shinfo(new)->tso_size = skb_shinfo(old)->tso_size; |
| 453 | skb_shinfo(new)->tso_segs = skb_shinfo(old)->tso_segs; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * skb_copy - create private copy of an sk_buff |
| 458 | * @skb: buffer to copy |
| 459 | * @gfp_mask: allocation priority |
| 460 | * |
| 461 | * Make a copy of both an &sk_buff and its data. This is used when the |
| 462 | * caller wishes to modify the data and needs a private copy of the |
| 463 | * data to alter. Returns %NULL on failure or the pointer to the buffer |
| 464 | * on success. The returned buffer has a reference count of 1. |
| 465 | * |
| 466 | * As by-product this function converts non-linear &sk_buff to linear |
| 467 | * one, so that &sk_buff becomes completely private and caller is allowed |
| 468 | * to modify all the data of returned buffer. This means that this |
| 469 | * function is not recommended for use in circumstances when only |
| 470 | * header is going to be modified. Use pskb_copy() instead. |
| 471 | */ |
| 472 | |
| 473 | struct sk_buff *skb_copy(const struct sk_buff *skb, int gfp_mask) |
| 474 | { |
| 475 | int headerlen = skb->data - skb->head; |
| 476 | /* |
| 477 | * Allocate the copy buffer |
| 478 | */ |
| 479 | struct sk_buff *n = alloc_skb(skb->end - skb->head + skb->data_len, |
| 480 | gfp_mask); |
| 481 | if (!n) |
| 482 | return NULL; |
| 483 | |
| 484 | /* Set the data pointer */ |
| 485 | skb_reserve(n, headerlen); |
| 486 | /* Set the tail pointer and length */ |
| 487 | skb_put(n, skb->len); |
| 488 | n->csum = skb->csum; |
| 489 | n->ip_summed = skb->ip_summed; |
| 490 | |
| 491 | if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) |
| 492 | BUG(); |
| 493 | |
| 494 | copy_skb_header(n, skb); |
| 495 | return n; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * pskb_copy - create copy of an sk_buff with private head. |
| 501 | * @skb: buffer to copy |
| 502 | * @gfp_mask: allocation priority |
| 503 | * |
| 504 | * Make a copy of both an &sk_buff and part of its data, located |
| 505 | * in header. Fragmented data remain shared. This is used when |
| 506 | * the caller wishes to modify only header of &sk_buff and needs |
| 507 | * private copy of the header to alter. Returns %NULL on failure |
| 508 | * or the pointer to the buffer on success. |
| 509 | * The returned buffer has a reference count of 1. |
| 510 | */ |
| 511 | |
| 512 | struct sk_buff *pskb_copy(struct sk_buff *skb, int gfp_mask) |
| 513 | { |
| 514 | /* |
| 515 | * Allocate the copy buffer |
| 516 | */ |
| 517 | struct sk_buff *n = alloc_skb(skb->end - skb->head, gfp_mask); |
| 518 | |
| 519 | if (!n) |
| 520 | goto out; |
| 521 | |
| 522 | /* Set the data pointer */ |
| 523 | skb_reserve(n, skb->data - skb->head); |
| 524 | /* Set the tail pointer and length */ |
| 525 | skb_put(n, skb_headlen(skb)); |
| 526 | /* Copy the bytes */ |
| 527 | memcpy(n->data, skb->data, n->len); |
| 528 | n->csum = skb->csum; |
| 529 | n->ip_summed = skb->ip_summed; |
| 530 | |
| 531 | n->data_len = skb->data_len; |
| 532 | n->len = skb->len; |
| 533 | |
| 534 | if (skb_shinfo(skb)->nr_frags) { |
| 535 | int i; |
| 536 | |
| 537 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 538 | skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 539 | get_page(skb_shinfo(n)->frags[i].page); |
| 540 | } |
| 541 | skb_shinfo(n)->nr_frags = i; |
| 542 | } |
| 543 | |
| 544 | if (skb_shinfo(skb)->frag_list) { |
| 545 | skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; |
| 546 | skb_clone_fraglist(n); |
| 547 | } |
| 548 | |
| 549 | copy_skb_header(n, skb); |
| 550 | out: |
| 551 | return n; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * pskb_expand_head - reallocate header of &sk_buff |
| 556 | * @skb: buffer to reallocate |
| 557 | * @nhead: room to add at head |
| 558 | * @ntail: room to add at tail |
| 559 | * @gfp_mask: allocation priority |
| 560 | * |
| 561 | * Expands (or creates identical copy, if &nhead and &ntail are zero) |
| 562 | * header of skb. &sk_buff itself is not changed. &sk_buff MUST have |
| 563 | * reference count of 1. Returns zero in the case of success or error, |
| 564 | * if expansion failed. In the last case, &sk_buff is not changed. |
| 565 | * |
| 566 | * All the pointers pointing into skb header may change and must be |
| 567 | * reloaded after call to this function. |
| 568 | */ |
| 569 | |
| 570 | int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, int gfp_mask) |
| 571 | { |
| 572 | int i; |
| 573 | u8 *data; |
| 574 | int size = nhead + (skb->end - skb->head) + ntail; |
| 575 | long off; |
| 576 | |
| 577 | if (skb_shared(skb)) |
| 578 | BUG(); |
| 579 | |
| 580 | size = SKB_DATA_ALIGN(size); |
| 581 | |
| 582 | data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); |
| 583 | if (!data) |
| 584 | goto nodata; |
| 585 | |
| 586 | /* Copy only real data... and, alas, header. This should be |
| 587 | * optimized for the cases when header is void. */ |
| 588 | memcpy(data + nhead, skb->head, skb->tail - skb->head); |
| 589 | memcpy(data + size, skb->end, sizeof(struct skb_shared_info)); |
| 590 | |
| 591 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 592 | get_page(skb_shinfo(skb)->frags[i].page); |
| 593 | |
| 594 | if (skb_shinfo(skb)->frag_list) |
| 595 | skb_clone_fraglist(skb); |
| 596 | |
| 597 | skb_release_data(skb); |
| 598 | |
| 599 | off = (data + nhead) - skb->head; |
| 600 | |
| 601 | skb->head = data; |
| 602 | skb->end = data + size; |
| 603 | skb->data += off; |
| 604 | skb->tail += off; |
| 605 | skb->mac.raw += off; |
| 606 | skb->h.raw += off; |
| 607 | skb->nh.raw += off; |
| 608 | skb->cloned = 0; |
| 609 | skb->nohdr = 0; |
| 610 | atomic_set(&skb_shinfo(skb)->dataref, 1); |
| 611 | return 0; |
| 612 | |
| 613 | nodata: |
| 614 | return -ENOMEM; |
| 615 | } |
| 616 | |
| 617 | /* Make private copy of skb with writable head and some headroom */ |
| 618 | |
| 619 | struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) |
| 620 | { |
| 621 | struct sk_buff *skb2; |
| 622 | int delta = headroom - skb_headroom(skb); |
| 623 | |
| 624 | if (delta <= 0) |
| 625 | skb2 = pskb_copy(skb, GFP_ATOMIC); |
| 626 | else { |
| 627 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 628 | if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, |
| 629 | GFP_ATOMIC)) { |
| 630 | kfree_skb(skb2); |
| 631 | skb2 = NULL; |
| 632 | } |
| 633 | } |
| 634 | return skb2; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /** |
| 639 | * skb_copy_expand - copy and expand sk_buff |
| 640 | * @skb: buffer to copy |
| 641 | * @newheadroom: new free bytes at head |
| 642 | * @newtailroom: new free bytes at tail |
| 643 | * @gfp_mask: allocation priority |
| 644 | * |
| 645 | * Make a copy of both an &sk_buff and its data and while doing so |
| 646 | * allocate additional space. |
| 647 | * |
| 648 | * This is used when the caller wishes to modify the data and needs a |
| 649 | * private copy of the data to alter as well as more space for new fields. |
| 650 | * Returns %NULL on failure or the pointer to the buffer |
| 651 | * on success. The returned buffer has a reference count of 1. |
| 652 | * |
| 653 | * You must pass %GFP_ATOMIC as the allocation priority if this function |
| 654 | * is called from an interrupt. |
| 655 | * |
| 656 | * BUG ALERT: ip_summed is not copied. Why does this work? Is it used |
| 657 | * only by netfilter in the cases when checksum is recalculated? --ANK |
| 658 | */ |
| 659 | struct sk_buff *skb_copy_expand(const struct sk_buff *skb, |
| 660 | int newheadroom, int newtailroom, int gfp_mask) |
| 661 | { |
| 662 | /* |
| 663 | * Allocate the copy buffer |
| 664 | */ |
| 665 | struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, |
| 666 | gfp_mask); |
| 667 | int head_copy_len, head_copy_off; |
| 668 | |
| 669 | if (!n) |
| 670 | return NULL; |
| 671 | |
| 672 | skb_reserve(n, newheadroom); |
| 673 | |
| 674 | /* Set the tail pointer and length */ |
| 675 | skb_put(n, skb->len); |
| 676 | |
| 677 | head_copy_len = skb_headroom(skb); |
| 678 | head_copy_off = 0; |
| 679 | if (newheadroom <= head_copy_len) |
| 680 | head_copy_len = newheadroom; |
| 681 | else |
| 682 | head_copy_off = newheadroom - head_copy_len; |
| 683 | |
| 684 | /* Copy the linear header and data. */ |
| 685 | if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, |
| 686 | skb->len + head_copy_len)) |
| 687 | BUG(); |
| 688 | |
| 689 | copy_skb_header(n, skb); |
| 690 | |
| 691 | return n; |
| 692 | } |
| 693 | |
| 694 | /** |
| 695 | * skb_pad - zero pad the tail of an skb |
| 696 | * @skb: buffer to pad |
| 697 | * @pad: space to pad |
| 698 | * |
| 699 | * Ensure that a buffer is followed by a padding area that is zero |
| 700 | * filled. Used by network drivers which may DMA or transfer data |
| 701 | * beyond the buffer end onto the wire. |
| 702 | * |
| 703 | * May return NULL in out of memory cases. |
| 704 | */ |
| 705 | |
| 706 | struct sk_buff *skb_pad(struct sk_buff *skb, int pad) |
| 707 | { |
| 708 | struct sk_buff *nskb; |
| 709 | |
| 710 | /* If the skbuff is non linear tailroom is always zero.. */ |
| 711 | if (skb_tailroom(skb) >= pad) { |
| 712 | memset(skb->data+skb->len, 0, pad); |
| 713 | return skb; |
| 714 | } |
| 715 | |
| 716 | nskb = skb_copy_expand(skb, skb_headroom(skb), skb_tailroom(skb) + pad, GFP_ATOMIC); |
| 717 | kfree_skb(skb); |
| 718 | if (nskb) |
| 719 | memset(nskb->data+nskb->len, 0, pad); |
| 720 | return nskb; |
| 721 | } |
| 722 | |
| 723 | /* Trims skb to length len. It can change skb pointers, if "realloc" is 1. |
| 724 | * If realloc==0 and trimming is impossible without change of data, |
| 725 | * it is BUG(). |
| 726 | */ |
| 727 | |
| 728 | int ___pskb_trim(struct sk_buff *skb, unsigned int len, int realloc) |
| 729 | { |
| 730 | int offset = skb_headlen(skb); |
| 731 | int nfrags = skb_shinfo(skb)->nr_frags; |
| 732 | int i; |
| 733 | |
| 734 | for (i = 0; i < nfrags; i++) { |
| 735 | int end = offset + skb_shinfo(skb)->frags[i].size; |
| 736 | if (end > len) { |
| 737 | if (skb_cloned(skb)) { |
| 738 | if (!realloc) |
| 739 | BUG(); |
| 740 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
| 741 | return -ENOMEM; |
| 742 | } |
| 743 | if (len <= offset) { |
| 744 | put_page(skb_shinfo(skb)->frags[i].page); |
| 745 | skb_shinfo(skb)->nr_frags--; |
| 746 | } else { |
| 747 | skb_shinfo(skb)->frags[i].size = len - offset; |
| 748 | } |
| 749 | } |
| 750 | offset = end; |
| 751 | } |
| 752 | |
| 753 | if (offset < len) { |
| 754 | skb->data_len -= skb->len - len; |
| 755 | skb->len = len; |
| 756 | } else { |
| 757 | if (len <= skb_headlen(skb)) { |
| 758 | skb->len = len; |
| 759 | skb->data_len = 0; |
| 760 | skb->tail = skb->data + len; |
| 761 | if (skb_shinfo(skb)->frag_list && !skb_cloned(skb)) |
| 762 | skb_drop_fraglist(skb); |
| 763 | } else { |
| 764 | skb->data_len -= skb->len - len; |
| 765 | skb->len = len; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * __pskb_pull_tail - advance tail of skb header |
| 774 | * @skb: buffer to reallocate |
| 775 | * @delta: number of bytes to advance tail |
| 776 | * |
| 777 | * The function makes a sense only on a fragmented &sk_buff, |
| 778 | * it expands header moving its tail forward and copying necessary |
| 779 | * data from fragmented part. |
| 780 | * |
| 781 | * &sk_buff MUST have reference count of 1. |
| 782 | * |
| 783 | * Returns %NULL (and &sk_buff does not change) if pull failed |
| 784 | * or value of new tail of skb in the case of success. |
| 785 | * |
| 786 | * All the pointers pointing into skb header may change and must be |
| 787 | * reloaded after call to this function. |
| 788 | */ |
| 789 | |
| 790 | /* Moves tail of skb head forward, copying data from fragmented part, |
| 791 | * when it is necessary. |
| 792 | * 1. It may fail due to malloc failure. |
| 793 | * 2. It may change skb pointers. |
| 794 | * |
| 795 | * It is pretty complicated. Luckily, it is called only in exceptional cases. |
| 796 | */ |
| 797 | unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) |
| 798 | { |
| 799 | /* If skb has not enough free space at tail, get new one |
| 800 | * plus 128 bytes for future expansions. If we have enough |
| 801 | * room at tail, reallocate without expansion only if skb is cloned. |
| 802 | */ |
| 803 | int i, k, eat = (skb->tail + delta) - skb->end; |
| 804 | |
| 805 | if (eat > 0 || skb_cloned(skb)) { |
| 806 | if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, |
| 807 | GFP_ATOMIC)) |
| 808 | return NULL; |
| 809 | } |
| 810 | |
| 811 | if (skb_copy_bits(skb, skb_headlen(skb), skb->tail, delta)) |
| 812 | BUG(); |
| 813 | |
| 814 | /* Optimization: no fragments, no reasons to preestimate |
| 815 | * size of pulled pages. Superb. |
| 816 | */ |
| 817 | if (!skb_shinfo(skb)->frag_list) |
| 818 | goto pull_pages; |
| 819 | |
| 820 | /* Estimate size of pulled pages. */ |
| 821 | eat = delta; |
| 822 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 823 | if (skb_shinfo(skb)->frags[i].size >= eat) |
| 824 | goto pull_pages; |
| 825 | eat -= skb_shinfo(skb)->frags[i].size; |
| 826 | } |
| 827 | |
| 828 | /* If we need update frag list, we are in troubles. |
| 829 | * Certainly, it possible to add an offset to skb data, |
| 830 | * but taking into account that pulling is expected to |
| 831 | * be very rare operation, it is worth to fight against |
| 832 | * further bloating skb head and crucify ourselves here instead. |
| 833 | * Pure masohism, indeed. 8)8) |
| 834 | */ |
| 835 | if (eat) { |
| 836 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 837 | struct sk_buff *clone = NULL; |
| 838 | struct sk_buff *insp = NULL; |
| 839 | |
| 840 | do { |
| 841 | if (!list) |
| 842 | BUG(); |
| 843 | |
| 844 | if (list->len <= eat) { |
| 845 | /* Eaten as whole. */ |
| 846 | eat -= list->len; |
| 847 | list = list->next; |
| 848 | insp = list; |
| 849 | } else { |
| 850 | /* Eaten partially. */ |
| 851 | |
| 852 | if (skb_shared(list)) { |
| 853 | /* Sucks! We need to fork list. :-( */ |
| 854 | clone = skb_clone(list, GFP_ATOMIC); |
| 855 | if (!clone) |
| 856 | return NULL; |
| 857 | insp = list->next; |
| 858 | list = clone; |
| 859 | } else { |
| 860 | /* This may be pulled without |
| 861 | * problems. */ |
| 862 | insp = list; |
| 863 | } |
| 864 | if (!pskb_pull(list, eat)) { |
| 865 | if (clone) |
| 866 | kfree_skb(clone); |
| 867 | return NULL; |
| 868 | } |
| 869 | break; |
| 870 | } |
| 871 | } while (eat); |
| 872 | |
| 873 | /* Free pulled out fragments. */ |
| 874 | while ((list = skb_shinfo(skb)->frag_list) != insp) { |
| 875 | skb_shinfo(skb)->frag_list = list->next; |
| 876 | kfree_skb(list); |
| 877 | } |
| 878 | /* And insert new clone at head. */ |
| 879 | if (clone) { |
| 880 | clone->next = list; |
| 881 | skb_shinfo(skb)->frag_list = clone; |
| 882 | } |
| 883 | } |
| 884 | /* Success! Now we may commit changes to skb data. */ |
| 885 | |
| 886 | pull_pages: |
| 887 | eat = delta; |
| 888 | k = 0; |
| 889 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 890 | if (skb_shinfo(skb)->frags[i].size <= eat) { |
| 891 | put_page(skb_shinfo(skb)->frags[i].page); |
| 892 | eat -= skb_shinfo(skb)->frags[i].size; |
| 893 | } else { |
| 894 | skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 895 | if (eat) { |
| 896 | skb_shinfo(skb)->frags[k].page_offset += eat; |
| 897 | skb_shinfo(skb)->frags[k].size -= eat; |
| 898 | eat = 0; |
| 899 | } |
| 900 | k++; |
| 901 | } |
| 902 | } |
| 903 | skb_shinfo(skb)->nr_frags = k; |
| 904 | |
| 905 | skb->tail += delta; |
| 906 | skb->data_len -= delta; |
| 907 | |
| 908 | return skb->tail; |
| 909 | } |
| 910 | |
| 911 | /* Copy some data bits from skb to kernel buffer. */ |
| 912 | |
| 913 | int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) |
| 914 | { |
| 915 | int i, copy; |
| 916 | int start = skb_headlen(skb); |
| 917 | |
| 918 | if (offset > (int)skb->len - len) |
| 919 | goto fault; |
| 920 | |
| 921 | /* Copy header. */ |
| 922 | if ((copy = start - offset) > 0) { |
| 923 | if (copy > len) |
| 924 | copy = len; |
| 925 | memcpy(to, skb->data + offset, copy); |
| 926 | if ((len -= copy) == 0) |
| 927 | return 0; |
| 928 | offset += copy; |
| 929 | to += copy; |
| 930 | } |
| 931 | |
| 932 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 933 | int end; |
| 934 | |
| 935 | BUG_TRAP(start <= offset + len); |
| 936 | |
| 937 | end = start + skb_shinfo(skb)->frags[i].size; |
| 938 | if ((copy = end - offset) > 0) { |
| 939 | u8 *vaddr; |
| 940 | |
| 941 | if (copy > len) |
| 942 | copy = len; |
| 943 | |
| 944 | vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); |
| 945 | memcpy(to, |
| 946 | vaddr + skb_shinfo(skb)->frags[i].page_offset+ |
| 947 | offset - start, copy); |
| 948 | kunmap_skb_frag(vaddr); |
| 949 | |
| 950 | if ((len -= copy) == 0) |
| 951 | return 0; |
| 952 | offset += copy; |
| 953 | to += copy; |
| 954 | } |
| 955 | start = end; |
| 956 | } |
| 957 | |
| 958 | if (skb_shinfo(skb)->frag_list) { |
| 959 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 960 | |
| 961 | for (; list; list = list->next) { |
| 962 | int end; |
| 963 | |
| 964 | BUG_TRAP(start <= offset + len); |
| 965 | |
| 966 | end = start + list->len; |
| 967 | if ((copy = end - offset) > 0) { |
| 968 | if (copy > len) |
| 969 | copy = len; |
| 970 | if (skb_copy_bits(list, offset - start, |
| 971 | to, copy)) |
| 972 | goto fault; |
| 973 | if ((len -= copy) == 0) |
| 974 | return 0; |
| 975 | offset += copy; |
| 976 | to += copy; |
| 977 | } |
| 978 | start = end; |
| 979 | } |
| 980 | } |
| 981 | if (!len) |
| 982 | return 0; |
| 983 | |
| 984 | fault: |
| 985 | return -EFAULT; |
| 986 | } |
| 987 | |
Herbert Xu | 357b40a | 2005-04-19 22:30:14 -0700 | [diff] [blame^] | 988 | /** |
| 989 | * skb_store_bits - store bits from kernel buffer to skb |
| 990 | * @skb: destination buffer |
| 991 | * @offset: offset in destination |
| 992 | * @from: source buffer |
| 993 | * @len: number of bytes to copy |
| 994 | * |
| 995 | * Copy the specified number of bytes from the source buffer to the |
| 996 | * destination skb. This function handles all the messy bits of |
| 997 | * traversing fragment lists and such. |
| 998 | */ |
| 999 | |
| 1000 | int skb_store_bits(const struct sk_buff *skb, int offset, void *from, int len) |
| 1001 | { |
| 1002 | int i, copy; |
| 1003 | int start = skb_headlen(skb); |
| 1004 | |
| 1005 | if (offset > (int)skb->len - len) |
| 1006 | goto fault; |
| 1007 | |
| 1008 | if ((copy = start - offset) > 0) { |
| 1009 | if (copy > len) |
| 1010 | copy = len; |
| 1011 | memcpy(skb->data + offset, from, copy); |
| 1012 | if ((len -= copy) == 0) |
| 1013 | return 0; |
| 1014 | offset += copy; |
| 1015 | from += copy; |
| 1016 | } |
| 1017 | |
| 1018 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1019 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1020 | int end; |
| 1021 | |
| 1022 | BUG_TRAP(start <= offset + len); |
| 1023 | |
| 1024 | end = start + frag->size; |
| 1025 | if ((copy = end - offset) > 0) { |
| 1026 | u8 *vaddr; |
| 1027 | |
| 1028 | if (copy > len) |
| 1029 | copy = len; |
| 1030 | |
| 1031 | vaddr = kmap_skb_frag(frag); |
| 1032 | memcpy(vaddr + frag->page_offset + offset - start, |
| 1033 | from, copy); |
| 1034 | kunmap_skb_frag(vaddr); |
| 1035 | |
| 1036 | if ((len -= copy) == 0) |
| 1037 | return 0; |
| 1038 | offset += copy; |
| 1039 | from += copy; |
| 1040 | } |
| 1041 | start = end; |
| 1042 | } |
| 1043 | |
| 1044 | if (skb_shinfo(skb)->frag_list) { |
| 1045 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1046 | |
| 1047 | for (; list; list = list->next) { |
| 1048 | int end; |
| 1049 | |
| 1050 | BUG_TRAP(start <= offset + len); |
| 1051 | |
| 1052 | end = start + list->len; |
| 1053 | if ((copy = end - offset) > 0) { |
| 1054 | if (copy > len) |
| 1055 | copy = len; |
| 1056 | if (skb_store_bits(list, offset - start, |
| 1057 | from, copy)) |
| 1058 | goto fault; |
| 1059 | if ((len -= copy) == 0) |
| 1060 | return 0; |
| 1061 | offset += copy; |
| 1062 | from += copy; |
| 1063 | } |
| 1064 | start = end; |
| 1065 | } |
| 1066 | } |
| 1067 | if (!len) |
| 1068 | return 0; |
| 1069 | |
| 1070 | fault: |
| 1071 | return -EFAULT; |
| 1072 | } |
| 1073 | |
| 1074 | EXPORT_SYMBOL(skb_store_bits); |
| 1075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | /* Checksum skb data. */ |
| 1077 | |
| 1078 | unsigned int skb_checksum(const struct sk_buff *skb, int offset, |
| 1079 | int len, unsigned int csum) |
| 1080 | { |
| 1081 | int start = skb_headlen(skb); |
| 1082 | int i, copy = start - offset; |
| 1083 | int pos = 0; |
| 1084 | |
| 1085 | /* Checksum header. */ |
| 1086 | if (copy > 0) { |
| 1087 | if (copy > len) |
| 1088 | copy = len; |
| 1089 | csum = csum_partial(skb->data + offset, copy, csum); |
| 1090 | if ((len -= copy) == 0) |
| 1091 | return csum; |
| 1092 | offset += copy; |
| 1093 | pos = copy; |
| 1094 | } |
| 1095 | |
| 1096 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1097 | int end; |
| 1098 | |
| 1099 | BUG_TRAP(start <= offset + len); |
| 1100 | |
| 1101 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1102 | if ((copy = end - offset) > 0) { |
| 1103 | unsigned int csum2; |
| 1104 | u8 *vaddr; |
| 1105 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1106 | |
| 1107 | if (copy > len) |
| 1108 | copy = len; |
| 1109 | vaddr = kmap_skb_frag(frag); |
| 1110 | csum2 = csum_partial(vaddr + frag->page_offset + |
| 1111 | offset - start, copy, 0); |
| 1112 | kunmap_skb_frag(vaddr); |
| 1113 | csum = csum_block_add(csum, csum2, pos); |
| 1114 | if (!(len -= copy)) |
| 1115 | return csum; |
| 1116 | offset += copy; |
| 1117 | pos += copy; |
| 1118 | } |
| 1119 | start = end; |
| 1120 | } |
| 1121 | |
| 1122 | if (skb_shinfo(skb)->frag_list) { |
| 1123 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1124 | |
| 1125 | for (; list; list = list->next) { |
| 1126 | int end; |
| 1127 | |
| 1128 | BUG_TRAP(start <= offset + len); |
| 1129 | |
| 1130 | end = start + list->len; |
| 1131 | if ((copy = end - offset) > 0) { |
| 1132 | unsigned int csum2; |
| 1133 | if (copy > len) |
| 1134 | copy = len; |
| 1135 | csum2 = skb_checksum(list, offset - start, |
| 1136 | copy, 0); |
| 1137 | csum = csum_block_add(csum, csum2, pos); |
| 1138 | if ((len -= copy) == 0) |
| 1139 | return csum; |
| 1140 | offset += copy; |
| 1141 | pos += copy; |
| 1142 | } |
| 1143 | start = end; |
| 1144 | } |
| 1145 | } |
| 1146 | if (len) |
| 1147 | BUG(); |
| 1148 | |
| 1149 | return csum; |
| 1150 | } |
| 1151 | |
| 1152 | /* Both of above in one bottle. */ |
| 1153 | |
| 1154 | unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, |
| 1155 | u8 *to, int len, unsigned int csum) |
| 1156 | { |
| 1157 | int start = skb_headlen(skb); |
| 1158 | int i, copy = start - offset; |
| 1159 | int pos = 0; |
| 1160 | |
| 1161 | /* Copy header. */ |
| 1162 | if (copy > 0) { |
| 1163 | if (copy > len) |
| 1164 | copy = len; |
| 1165 | csum = csum_partial_copy_nocheck(skb->data + offset, to, |
| 1166 | copy, csum); |
| 1167 | if ((len -= copy) == 0) |
| 1168 | return csum; |
| 1169 | offset += copy; |
| 1170 | to += copy; |
| 1171 | pos = copy; |
| 1172 | } |
| 1173 | |
| 1174 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 1175 | int end; |
| 1176 | |
| 1177 | BUG_TRAP(start <= offset + len); |
| 1178 | |
| 1179 | end = start + skb_shinfo(skb)->frags[i].size; |
| 1180 | if ((copy = end - offset) > 0) { |
| 1181 | unsigned int csum2; |
| 1182 | u8 *vaddr; |
| 1183 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 1184 | |
| 1185 | if (copy > len) |
| 1186 | copy = len; |
| 1187 | vaddr = kmap_skb_frag(frag); |
| 1188 | csum2 = csum_partial_copy_nocheck(vaddr + |
| 1189 | frag->page_offset + |
| 1190 | offset - start, to, |
| 1191 | copy, 0); |
| 1192 | kunmap_skb_frag(vaddr); |
| 1193 | csum = csum_block_add(csum, csum2, pos); |
| 1194 | if (!(len -= copy)) |
| 1195 | return csum; |
| 1196 | offset += copy; |
| 1197 | to += copy; |
| 1198 | pos += copy; |
| 1199 | } |
| 1200 | start = end; |
| 1201 | } |
| 1202 | |
| 1203 | if (skb_shinfo(skb)->frag_list) { |
| 1204 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 1205 | |
| 1206 | for (; list; list = list->next) { |
| 1207 | unsigned int csum2; |
| 1208 | int end; |
| 1209 | |
| 1210 | BUG_TRAP(start <= offset + len); |
| 1211 | |
| 1212 | end = start + list->len; |
| 1213 | if ((copy = end - offset) > 0) { |
| 1214 | if (copy > len) |
| 1215 | copy = len; |
| 1216 | csum2 = skb_copy_and_csum_bits(list, |
| 1217 | offset - start, |
| 1218 | to, copy, 0); |
| 1219 | csum = csum_block_add(csum, csum2, pos); |
| 1220 | if ((len -= copy) == 0) |
| 1221 | return csum; |
| 1222 | offset += copy; |
| 1223 | to += copy; |
| 1224 | pos += copy; |
| 1225 | } |
| 1226 | start = end; |
| 1227 | } |
| 1228 | } |
| 1229 | if (len) |
| 1230 | BUG(); |
| 1231 | return csum; |
| 1232 | } |
| 1233 | |
| 1234 | void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) |
| 1235 | { |
| 1236 | unsigned int csum; |
| 1237 | long csstart; |
| 1238 | |
| 1239 | if (skb->ip_summed == CHECKSUM_HW) |
| 1240 | csstart = skb->h.raw - skb->data; |
| 1241 | else |
| 1242 | csstart = skb_headlen(skb); |
| 1243 | |
| 1244 | if (csstart > skb_headlen(skb)) |
| 1245 | BUG(); |
| 1246 | |
| 1247 | memcpy(to, skb->data, csstart); |
| 1248 | |
| 1249 | csum = 0; |
| 1250 | if (csstart != skb->len) |
| 1251 | csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, |
| 1252 | skb->len - csstart, 0); |
| 1253 | |
| 1254 | if (skb->ip_summed == CHECKSUM_HW) { |
| 1255 | long csstuff = csstart + skb->csum; |
| 1256 | |
| 1257 | *((unsigned short *)(to + csstuff)) = csum_fold(csum); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * skb_dequeue - remove from the head of the queue |
| 1263 | * @list: list to dequeue from |
| 1264 | * |
| 1265 | * Remove the head of the list. The list lock is taken so the function |
| 1266 | * may be used safely with other locking list functions. The head item is |
| 1267 | * returned or %NULL if the list is empty. |
| 1268 | */ |
| 1269 | |
| 1270 | struct sk_buff *skb_dequeue(struct sk_buff_head *list) |
| 1271 | { |
| 1272 | unsigned long flags; |
| 1273 | struct sk_buff *result; |
| 1274 | |
| 1275 | spin_lock_irqsave(&list->lock, flags); |
| 1276 | result = __skb_dequeue(list); |
| 1277 | spin_unlock_irqrestore(&list->lock, flags); |
| 1278 | return result; |
| 1279 | } |
| 1280 | |
| 1281 | /** |
| 1282 | * skb_dequeue_tail - remove from the tail of the queue |
| 1283 | * @list: list to dequeue from |
| 1284 | * |
| 1285 | * Remove the tail of the list. The list lock is taken so the function |
| 1286 | * may be used safely with other locking list functions. The tail item is |
| 1287 | * returned or %NULL if the list is empty. |
| 1288 | */ |
| 1289 | struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) |
| 1290 | { |
| 1291 | unsigned long flags; |
| 1292 | struct sk_buff *result; |
| 1293 | |
| 1294 | spin_lock_irqsave(&list->lock, flags); |
| 1295 | result = __skb_dequeue_tail(list); |
| 1296 | spin_unlock_irqrestore(&list->lock, flags); |
| 1297 | return result; |
| 1298 | } |
| 1299 | |
| 1300 | /** |
| 1301 | * skb_queue_purge - empty a list |
| 1302 | * @list: list to empty |
| 1303 | * |
| 1304 | * Delete all buffers on an &sk_buff list. Each buffer is removed from |
| 1305 | * the list and one reference dropped. This function takes the list |
| 1306 | * lock and is atomic with respect to other list locking functions. |
| 1307 | */ |
| 1308 | void skb_queue_purge(struct sk_buff_head *list) |
| 1309 | { |
| 1310 | struct sk_buff *skb; |
| 1311 | while ((skb = skb_dequeue(list)) != NULL) |
| 1312 | kfree_skb(skb); |
| 1313 | } |
| 1314 | |
| 1315 | /** |
| 1316 | * skb_queue_head - queue a buffer at the list head |
| 1317 | * @list: list to use |
| 1318 | * @newsk: buffer to queue |
| 1319 | * |
| 1320 | * Queue a buffer at the start of the list. This function takes the |
| 1321 | * list lock and can be used safely with other locking &sk_buff functions |
| 1322 | * safely. |
| 1323 | * |
| 1324 | * A buffer cannot be placed on two lists at the same time. |
| 1325 | */ |
| 1326 | void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) |
| 1327 | { |
| 1328 | unsigned long flags; |
| 1329 | |
| 1330 | spin_lock_irqsave(&list->lock, flags); |
| 1331 | __skb_queue_head(list, newsk); |
| 1332 | spin_unlock_irqrestore(&list->lock, flags); |
| 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * skb_queue_tail - queue a buffer at the list tail |
| 1337 | * @list: list to use |
| 1338 | * @newsk: buffer to queue |
| 1339 | * |
| 1340 | * Queue a buffer at the tail of the list. This function takes the |
| 1341 | * list lock and can be used safely with other locking &sk_buff functions |
| 1342 | * safely. |
| 1343 | * |
| 1344 | * A buffer cannot be placed on two lists at the same time. |
| 1345 | */ |
| 1346 | void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) |
| 1347 | { |
| 1348 | unsigned long flags; |
| 1349 | |
| 1350 | spin_lock_irqsave(&list->lock, flags); |
| 1351 | __skb_queue_tail(list, newsk); |
| 1352 | spin_unlock_irqrestore(&list->lock, flags); |
| 1353 | } |
| 1354 | /** |
| 1355 | * skb_unlink - remove a buffer from a list |
| 1356 | * @skb: buffer to remove |
| 1357 | * |
| 1358 | * Place a packet after a given packet in a list. The list locks are taken |
| 1359 | * and this function is atomic with respect to other list locked calls |
| 1360 | * |
| 1361 | * Works even without knowing the list it is sitting on, which can be |
| 1362 | * handy at times. It also means that THE LIST MUST EXIST when you |
| 1363 | * unlink. Thus a list must have its contents unlinked before it is |
| 1364 | * destroyed. |
| 1365 | */ |
| 1366 | void skb_unlink(struct sk_buff *skb) |
| 1367 | { |
| 1368 | struct sk_buff_head *list = skb->list; |
| 1369 | |
| 1370 | if (list) { |
| 1371 | unsigned long flags; |
| 1372 | |
| 1373 | spin_lock_irqsave(&list->lock, flags); |
| 1374 | if (skb->list == list) |
| 1375 | __skb_unlink(skb, skb->list); |
| 1376 | spin_unlock_irqrestore(&list->lock, flags); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | /** |
| 1382 | * skb_append - append a buffer |
| 1383 | * @old: buffer to insert after |
| 1384 | * @newsk: buffer to insert |
| 1385 | * |
| 1386 | * Place a packet after a given packet in a list. The list locks are taken |
| 1387 | * and this function is atomic with respect to other list locked calls. |
| 1388 | * A buffer cannot be placed on two lists at the same time. |
| 1389 | */ |
| 1390 | |
| 1391 | void skb_append(struct sk_buff *old, struct sk_buff *newsk) |
| 1392 | { |
| 1393 | unsigned long flags; |
| 1394 | |
| 1395 | spin_lock_irqsave(&old->list->lock, flags); |
| 1396 | __skb_append(old, newsk); |
| 1397 | spin_unlock_irqrestore(&old->list->lock, flags); |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | /** |
| 1402 | * skb_insert - insert a buffer |
| 1403 | * @old: buffer to insert before |
| 1404 | * @newsk: buffer to insert |
| 1405 | * |
| 1406 | * Place a packet before a given packet in a list. The list locks are taken |
| 1407 | * and this function is atomic with respect to other list locked calls |
| 1408 | * A buffer cannot be placed on two lists at the same time. |
| 1409 | */ |
| 1410 | |
| 1411 | void skb_insert(struct sk_buff *old, struct sk_buff *newsk) |
| 1412 | { |
| 1413 | unsigned long flags; |
| 1414 | |
| 1415 | spin_lock_irqsave(&old->list->lock, flags); |
| 1416 | __skb_insert(newsk, old->prev, old, old->list); |
| 1417 | spin_unlock_irqrestore(&old->list->lock, flags); |
| 1418 | } |
| 1419 | |
| 1420 | #if 0 |
| 1421 | /* |
| 1422 | * Tune the memory allocator for a new MTU size. |
| 1423 | */ |
| 1424 | void skb_add_mtu(int mtu) |
| 1425 | { |
| 1426 | /* Must match allocation in alloc_skb */ |
| 1427 | mtu = SKB_DATA_ALIGN(mtu) + sizeof(struct skb_shared_info); |
| 1428 | |
| 1429 | kmem_add_cache_size(mtu); |
| 1430 | } |
| 1431 | #endif |
| 1432 | |
| 1433 | static inline void skb_split_inside_header(struct sk_buff *skb, |
| 1434 | struct sk_buff* skb1, |
| 1435 | const u32 len, const int pos) |
| 1436 | { |
| 1437 | int i; |
| 1438 | |
| 1439 | memcpy(skb_put(skb1, pos - len), skb->data + len, pos - len); |
| 1440 | |
| 1441 | /* And move data appendix as is. */ |
| 1442 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) |
| 1443 | skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; |
| 1444 | |
| 1445 | skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; |
| 1446 | skb_shinfo(skb)->nr_frags = 0; |
| 1447 | skb1->data_len = skb->data_len; |
| 1448 | skb1->len += skb1->data_len; |
| 1449 | skb->data_len = 0; |
| 1450 | skb->len = len; |
| 1451 | skb->tail = skb->data + len; |
| 1452 | } |
| 1453 | |
| 1454 | static inline void skb_split_no_header(struct sk_buff *skb, |
| 1455 | struct sk_buff* skb1, |
| 1456 | const u32 len, int pos) |
| 1457 | { |
| 1458 | int i, k = 0; |
| 1459 | const int nfrags = skb_shinfo(skb)->nr_frags; |
| 1460 | |
| 1461 | skb_shinfo(skb)->nr_frags = 0; |
| 1462 | skb1->len = skb1->data_len = skb->len - len; |
| 1463 | skb->len = len; |
| 1464 | skb->data_len = len - pos; |
| 1465 | |
| 1466 | for (i = 0; i < nfrags; i++) { |
| 1467 | int size = skb_shinfo(skb)->frags[i].size; |
| 1468 | |
| 1469 | if (pos + size > len) { |
| 1470 | skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; |
| 1471 | |
| 1472 | if (pos < len) { |
| 1473 | /* Split frag. |
| 1474 | * We have two variants in this case: |
| 1475 | * 1. Move all the frag to the second |
| 1476 | * part, if it is possible. F.e. |
| 1477 | * this approach is mandatory for TUX, |
| 1478 | * where splitting is expensive. |
| 1479 | * 2. Split is accurately. We make this. |
| 1480 | */ |
| 1481 | get_page(skb_shinfo(skb)->frags[i].page); |
| 1482 | skb_shinfo(skb1)->frags[0].page_offset += len - pos; |
| 1483 | skb_shinfo(skb1)->frags[0].size -= len - pos; |
| 1484 | skb_shinfo(skb)->frags[i].size = len - pos; |
| 1485 | skb_shinfo(skb)->nr_frags++; |
| 1486 | } |
| 1487 | k++; |
| 1488 | } else |
| 1489 | skb_shinfo(skb)->nr_frags++; |
| 1490 | pos += size; |
| 1491 | } |
| 1492 | skb_shinfo(skb1)->nr_frags = k; |
| 1493 | } |
| 1494 | |
| 1495 | /** |
| 1496 | * skb_split - Split fragmented skb to two parts at length len. |
| 1497 | * @skb: the buffer to split |
| 1498 | * @skb1: the buffer to receive the second part |
| 1499 | * @len: new length for skb |
| 1500 | */ |
| 1501 | void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) |
| 1502 | { |
| 1503 | int pos = skb_headlen(skb); |
| 1504 | |
| 1505 | if (len < pos) /* Split line is inside header. */ |
| 1506 | skb_split_inside_header(skb, skb1, len, pos); |
| 1507 | else /* Second chunk has no header, nothing to copy. */ |
| 1508 | skb_split_no_header(skb, skb1, len, pos); |
| 1509 | } |
| 1510 | |
| 1511 | void __init skb_init(void) |
| 1512 | { |
| 1513 | skbuff_head_cache = kmem_cache_create("skbuff_head_cache", |
| 1514 | sizeof(struct sk_buff), |
| 1515 | 0, |
| 1516 | SLAB_HWCACHE_ALIGN, |
| 1517 | NULL, NULL); |
| 1518 | if (!skbuff_head_cache) |
| 1519 | panic("cannot create skbuff cache"); |
| 1520 | } |
| 1521 | |
| 1522 | EXPORT_SYMBOL(___pskb_trim); |
| 1523 | EXPORT_SYMBOL(__kfree_skb); |
| 1524 | EXPORT_SYMBOL(__pskb_pull_tail); |
| 1525 | EXPORT_SYMBOL(alloc_skb); |
| 1526 | EXPORT_SYMBOL(pskb_copy); |
| 1527 | EXPORT_SYMBOL(pskb_expand_head); |
| 1528 | EXPORT_SYMBOL(skb_checksum); |
| 1529 | EXPORT_SYMBOL(skb_clone); |
| 1530 | EXPORT_SYMBOL(skb_clone_fraglist); |
| 1531 | EXPORT_SYMBOL(skb_copy); |
| 1532 | EXPORT_SYMBOL(skb_copy_and_csum_bits); |
| 1533 | EXPORT_SYMBOL(skb_copy_and_csum_dev); |
| 1534 | EXPORT_SYMBOL(skb_copy_bits); |
| 1535 | EXPORT_SYMBOL(skb_copy_expand); |
| 1536 | EXPORT_SYMBOL(skb_over_panic); |
| 1537 | EXPORT_SYMBOL(skb_pad); |
| 1538 | EXPORT_SYMBOL(skb_realloc_headroom); |
| 1539 | EXPORT_SYMBOL(skb_under_panic); |
| 1540 | EXPORT_SYMBOL(skb_dequeue); |
| 1541 | EXPORT_SYMBOL(skb_dequeue_tail); |
| 1542 | EXPORT_SYMBOL(skb_insert); |
| 1543 | EXPORT_SYMBOL(skb_queue_purge); |
| 1544 | EXPORT_SYMBOL(skb_queue_head); |
| 1545 | EXPORT_SYMBOL(skb_queue_tail); |
| 1546 | EXPORT_SYMBOL(skb_unlink); |
| 1547 | EXPORT_SYMBOL(skb_append); |
| 1548 | EXPORT_SYMBOL(skb_split); |