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