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