at v2.6.26-rc2 2623 lines 66 kB view raw
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/module.h> 42#include <linux/types.h> 43#include <linux/kernel.h> 44#include <linux/mm.h> 45#include <linux/interrupt.h> 46#include <linux/in.h> 47#include <linux/inet.h> 48#include <linux/slab.h> 49#include <linux/netdevice.h> 50#ifdef CONFIG_NET_CLS_ACT 51#include <net/pkt_sched.h> 52#endif 53#include <linux/string.h> 54#include <linux/skbuff.h> 55#include <linux/splice.h> 56#include <linux/cache.h> 57#include <linux/rtnetlink.h> 58#include <linux/init.h> 59#include <linux/scatterlist.h> 60 61#include <net/protocol.h> 62#include <net/dst.h> 63#include <net/sock.h> 64#include <net/checksum.h> 65#include <net/xfrm.h> 66 67#include <asm/uaccess.h> 68#include <asm/system.h> 69 70#include "kmap_skb.h" 71 72static struct kmem_cache *skbuff_head_cache __read_mostly; 73static struct kmem_cache *skbuff_fclone_cache __read_mostly; 74 75static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 76 struct pipe_buffer *buf) 77{ 78 struct sk_buff *skb = (struct sk_buff *) buf->private; 79 80 kfree_skb(skb); 81} 82 83static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 84 struct pipe_buffer *buf) 85{ 86 struct sk_buff *skb = (struct sk_buff *) buf->private; 87 88 skb_get(skb); 89} 90 91static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, 92 struct pipe_buffer *buf) 93{ 94 return 1; 95} 96 97 98/* Pipe buffer operations for a socket. */ 99static struct pipe_buf_operations sock_pipe_buf_ops = { 100 .can_merge = 0, 101 .map = generic_pipe_buf_map, 102 .unmap = generic_pipe_buf_unmap, 103 .confirm = generic_pipe_buf_confirm, 104 .release = sock_pipe_buf_release, 105 .steal = sock_pipe_buf_steal, 106 .get = sock_pipe_buf_get, 107}; 108 109/* 110 * Keep out-of-line to prevent kernel bloat. 111 * __builtin_return_address is not used because it is not always 112 * reliable. 113 */ 114 115/** 116 * skb_over_panic - private function 117 * @skb: buffer 118 * @sz: size 119 * @here: address 120 * 121 * Out of line support code for skb_put(). Not user callable. 122 */ 123void skb_over_panic(struct sk_buff *skb, int sz, void *here) 124{ 125 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " 126 "data:%p tail:%#lx end:%#lx dev:%s\n", 127 here, skb->len, sz, skb->head, skb->data, 128 (unsigned long)skb->tail, (unsigned long)skb->end, 129 skb->dev ? skb->dev->name : "<NULL>"); 130 BUG(); 131} 132 133/** 134 * skb_under_panic - private function 135 * @skb: buffer 136 * @sz: size 137 * @here: address 138 * 139 * Out of line support code for skb_push(). Not user callable. 140 */ 141 142void skb_under_panic(struct sk_buff *skb, int sz, void *here) 143{ 144 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " 145 "data:%p tail:%#lx end:%#lx dev:%s\n", 146 here, skb->len, sz, skb->head, skb->data, 147 (unsigned long)skb->tail, (unsigned long)skb->end, 148 skb->dev ? skb->dev->name : "<NULL>"); 149 BUG(); 150} 151 152void skb_truesize_bug(struct sk_buff *skb) 153{ 154 printk(KERN_ERR "SKB BUG: Invalid truesize (%u) " 155 "len=%u, sizeof(sk_buff)=%Zd\n", 156 skb->truesize, skb->len, sizeof(struct sk_buff)); 157} 158EXPORT_SYMBOL(skb_truesize_bug); 159 160/* Allocate a new skbuff. We do this ourselves so we can fill in a few 161 * 'private' fields and also do memory statistics to find all the 162 * [BEEP] leaks. 163 * 164 */ 165 166/** 167 * __alloc_skb - allocate a network buffer 168 * @size: size to allocate 169 * @gfp_mask: allocation mask 170 * @fclone: allocate from fclone cache instead of head cache 171 * and allocate a cloned (child) skb 172 * @node: numa node to allocate memory on 173 * 174 * Allocate a new &sk_buff. The returned buffer has no headroom and a 175 * tail room of size bytes. The object has a reference count of one. 176 * The return is the buffer. On a failure the return is %NULL. 177 * 178 * Buffers may only be allocated from interrupts using a @gfp_mask of 179 * %GFP_ATOMIC. 180 */ 181struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 182 int fclone, int node) 183{ 184 struct kmem_cache *cache; 185 struct skb_shared_info *shinfo; 186 struct sk_buff *skb; 187 u8 *data; 188 189 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; 190 191 /* Get the HEAD */ 192 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 193 if (!skb) 194 goto out; 195 196 size = SKB_DATA_ALIGN(size); 197 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), 198 gfp_mask, node); 199 if (!data) 200 goto nodata; 201 202 /* 203 * Only clear those fields we need to clear, not those that we will 204 * actually initialise below. Hence, don't put any more fields after 205 * the tail pointer in struct sk_buff! 206 */ 207 memset(skb, 0, offsetof(struct sk_buff, tail)); 208 skb->truesize = size + sizeof(struct sk_buff); 209 atomic_set(&skb->users, 1); 210 skb->head = data; 211 skb->data = data; 212 skb_reset_tail_pointer(skb); 213 skb->end = skb->tail + size; 214 /* make sure we initialize shinfo sequentially */ 215 shinfo = skb_shinfo(skb); 216 atomic_set(&shinfo->dataref, 1); 217 shinfo->nr_frags = 0; 218 shinfo->gso_size = 0; 219 shinfo->gso_segs = 0; 220 shinfo->gso_type = 0; 221 shinfo->ip6_frag_id = 0; 222 shinfo->frag_list = NULL; 223 224 if (fclone) { 225 struct sk_buff *child = skb + 1; 226 atomic_t *fclone_ref = (atomic_t *) (child + 1); 227 228 skb->fclone = SKB_FCLONE_ORIG; 229 atomic_set(fclone_ref, 1); 230 231 child->fclone = SKB_FCLONE_UNAVAILABLE; 232 } 233out: 234 return skb; 235nodata: 236 kmem_cache_free(cache, skb); 237 skb = NULL; 238 goto out; 239} 240 241/** 242 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 243 * @dev: network device to receive on 244 * @length: length to allocate 245 * @gfp_mask: get_free_pages mask, passed to alloc_skb 246 * 247 * Allocate a new &sk_buff and assign it a usage count of one. The 248 * buffer has unspecified headroom built in. Users should allocate 249 * the headroom they think they need without accounting for the 250 * built in space. The built in space is used for optimisations. 251 * 252 * %NULL is returned if there is no free memory. 253 */ 254struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 255 unsigned int length, gfp_t gfp_mask) 256{ 257 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; 258 struct sk_buff *skb; 259 260 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node); 261 if (likely(skb)) { 262 skb_reserve(skb, NET_SKB_PAD); 263 skb->dev = dev; 264 } 265 return skb; 266} 267 268/** 269 * dev_alloc_skb - allocate an skbuff for receiving 270 * @length: length to allocate 271 * 272 * Allocate a new &sk_buff and assign it a usage count of one. The 273 * buffer has unspecified headroom built in. Users should allocate 274 * the headroom they think they need without accounting for the 275 * built in space. The built in space is used for optimisations. 276 * 277 * %NULL is returned if there is no free memory. Although this function 278 * allocates memory it can be called from an interrupt. 279 */ 280struct sk_buff *dev_alloc_skb(unsigned int length) 281{ 282 /* 283 * There is more code here than it seems: 284 * __dev_alloc_skb is an inline 285 */ 286 return __dev_alloc_skb(length, GFP_ATOMIC); 287} 288EXPORT_SYMBOL(dev_alloc_skb); 289 290static void skb_drop_list(struct sk_buff **listp) 291{ 292 struct sk_buff *list = *listp; 293 294 *listp = NULL; 295 296 do { 297 struct sk_buff *this = list; 298 list = list->next; 299 kfree_skb(this); 300 } while (list); 301} 302 303static inline void skb_drop_fraglist(struct sk_buff *skb) 304{ 305 skb_drop_list(&skb_shinfo(skb)->frag_list); 306} 307 308static void skb_clone_fraglist(struct sk_buff *skb) 309{ 310 struct sk_buff *list; 311 312 for (list = skb_shinfo(skb)->frag_list; list; list = list->next) 313 skb_get(list); 314} 315 316static void skb_release_data(struct sk_buff *skb) 317{ 318 if (!skb->cloned || 319 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 320 &skb_shinfo(skb)->dataref)) { 321 if (skb_shinfo(skb)->nr_frags) { 322 int i; 323 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 324 put_page(skb_shinfo(skb)->frags[i].page); 325 } 326 327 if (skb_shinfo(skb)->frag_list) 328 skb_drop_fraglist(skb); 329 330 kfree(skb->head); 331 } 332} 333 334/* 335 * Free an skbuff by memory without cleaning the state. 336 */ 337static void kfree_skbmem(struct sk_buff *skb) 338{ 339 struct sk_buff *other; 340 atomic_t *fclone_ref; 341 342 switch (skb->fclone) { 343 case SKB_FCLONE_UNAVAILABLE: 344 kmem_cache_free(skbuff_head_cache, skb); 345 break; 346 347 case SKB_FCLONE_ORIG: 348 fclone_ref = (atomic_t *) (skb + 2); 349 if (atomic_dec_and_test(fclone_ref)) 350 kmem_cache_free(skbuff_fclone_cache, skb); 351 break; 352 353 case SKB_FCLONE_CLONE: 354 fclone_ref = (atomic_t *) (skb + 1); 355 other = skb - 1; 356 357 /* The clone portion is available for 358 * fast-cloning again. 359 */ 360 skb->fclone = SKB_FCLONE_UNAVAILABLE; 361 362 if (atomic_dec_and_test(fclone_ref)) 363 kmem_cache_free(skbuff_fclone_cache, other); 364 break; 365 } 366} 367 368/* Free everything but the sk_buff shell. */ 369static void skb_release_all(struct sk_buff *skb) 370{ 371 dst_release(skb->dst); 372#ifdef CONFIG_XFRM 373 secpath_put(skb->sp); 374#endif 375 if (skb->destructor) { 376 WARN_ON(in_irq()); 377 skb->destructor(skb); 378 } 379#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 380 nf_conntrack_put(skb->nfct); 381 nf_conntrack_put_reasm(skb->nfct_reasm); 382#endif 383#ifdef CONFIG_BRIDGE_NETFILTER 384 nf_bridge_put(skb->nf_bridge); 385#endif 386/* XXX: IS this still necessary? - JHS */ 387#ifdef CONFIG_NET_SCHED 388 skb->tc_index = 0; 389#ifdef CONFIG_NET_CLS_ACT 390 skb->tc_verd = 0; 391#endif 392#endif 393 skb_release_data(skb); 394} 395 396/** 397 * __kfree_skb - private function 398 * @skb: buffer 399 * 400 * Free an sk_buff. Release anything attached to the buffer. 401 * Clean the state. This is an internal helper function. Users should 402 * always call kfree_skb 403 */ 404 405void __kfree_skb(struct sk_buff *skb) 406{ 407 skb_release_all(skb); 408 kfree_skbmem(skb); 409} 410 411/** 412 * kfree_skb - free an sk_buff 413 * @skb: buffer to free 414 * 415 * Drop a reference to the buffer and free it if the usage count has 416 * hit zero. 417 */ 418void kfree_skb(struct sk_buff *skb) 419{ 420 if (unlikely(!skb)) 421 return; 422 if (likely(atomic_read(&skb->users) == 1)) 423 smp_rmb(); 424 else if (likely(!atomic_dec_and_test(&skb->users))) 425 return; 426 __kfree_skb(skb); 427} 428 429static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 430{ 431 new->tstamp = old->tstamp; 432 new->dev = old->dev; 433 new->transport_header = old->transport_header; 434 new->network_header = old->network_header; 435 new->mac_header = old->mac_header; 436 new->dst = dst_clone(old->dst); 437#ifdef CONFIG_INET 438 new->sp = secpath_get(old->sp); 439#endif 440 memcpy(new->cb, old->cb, sizeof(old->cb)); 441 new->csum_start = old->csum_start; 442 new->csum_offset = old->csum_offset; 443 new->local_df = old->local_df; 444 new->pkt_type = old->pkt_type; 445 new->ip_summed = old->ip_summed; 446 skb_copy_queue_mapping(new, old); 447 new->priority = old->priority; 448#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 449 new->ipvs_property = old->ipvs_property; 450#endif 451 new->protocol = old->protocol; 452 new->mark = old->mark; 453 __nf_copy(new, old); 454#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ 455 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) 456 new->nf_trace = old->nf_trace; 457#endif 458#ifdef CONFIG_NET_SCHED 459 new->tc_index = old->tc_index; 460#ifdef CONFIG_NET_CLS_ACT 461 new->tc_verd = old->tc_verd; 462#endif 463#endif 464 skb_copy_secmark(new, old); 465} 466 467static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 468{ 469#define C(x) n->x = skb->x 470 471 n->next = n->prev = NULL; 472 n->sk = NULL; 473 __copy_skb_header(n, skb); 474 475 C(len); 476 C(data_len); 477 C(mac_len); 478 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 479 n->cloned = 1; 480 n->nohdr = 0; 481 n->destructor = NULL; 482 C(iif); 483 C(tail); 484 C(end); 485 C(head); 486 C(data); 487 C(truesize); 488 atomic_set(&n->users, 1); 489 490 atomic_inc(&(skb_shinfo(skb)->dataref)); 491 skb->cloned = 1; 492 493 return n; 494#undef C 495} 496 497/** 498 * skb_morph - morph one skb into another 499 * @dst: the skb to receive the contents 500 * @src: the skb to supply the contents 501 * 502 * This is identical to skb_clone except that the target skb is 503 * supplied by the user. 504 * 505 * The target skb is returned upon exit. 506 */ 507struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 508{ 509 skb_release_all(dst); 510 return __skb_clone(dst, src); 511} 512EXPORT_SYMBOL_GPL(skb_morph); 513 514/** 515 * skb_clone - duplicate an sk_buff 516 * @skb: buffer to clone 517 * @gfp_mask: allocation priority 518 * 519 * Duplicate an &sk_buff. The new one is not owned by a socket. Both 520 * copies share the same packet data but not structure. The new 521 * buffer has a reference count of 1. If the allocation fails the 522 * function returns %NULL otherwise the new buffer is returned. 523 * 524 * If this function is called from an interrupt gfp_mask() must be 525 * %GFP_ATOMIC. 526 */ 527 528struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 529{ 530 struct sk_buff *n; 531 532 n = skb + 1; 533 if (skb->fclone == SKB_FCLONE_ORIG && 534 n->fclone == SKB_FCLONE_UNAVAILABLE) { 535 atomic_t *fclone_ref = (atomic_t *) (n + 1); 536 n->fclone = SKB_FCLONE_CLONE; 537 atomic_inc(fclone_ref); 538 } else { 539 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 540 if (!n) 541 return NULL; 542 n->fclone = SKB_FCLONE_UNAVAILABLE; 543 } 544 545 return __skb_clone(n, skb); 546} 547 548static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 549{ 550#ifndef NET_SKBUFF_DATA_USES_OFFSET 551 /* 552 * Shift between the two data areas in bytes 553 */ 554 unsigned long offset = new->data - old->data; 555#endif 556 557 __copy_skb_header(new, old); 558 559#ifndef NET_SKBUFF_DATA_USES_OFFSET 560 /* {transport,network,mac}_header are relative to skb->head */ 561 new->transport_header += offset; 562 new->network_header += offset; 563 new->mac_header += offset; 564#endif 565 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 566 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 567 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 568} 569 570/** 571 * skb_copy - create private copy of an sk_buff 572 * @skb: buffer to copy 573 * @gfp_mask: allocation priority 574 * 575 * Make a copy of both an &sk_buff and its data. This is used when the 576 * caller wishes to modify the data and needs a private copy of the 577 * data to alter. Returns %NULL on failure or the pointer to the buffer 578 * on success. The returned buffer has a reference count of 1. 579 * 580 * As by-product this function converts non-linear &sk_buff to linear 581 * one, so that &sk_buff becomes completely private and caller is allowed 582 * to modify all the data of returned buffer. This means that this 583 * function is not recommended for use in circumstances when only 584 * header is going to be modified. Use pskb_copy() instead. 585 */ 586 587struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 588{ 589 int headerlen = skb->data - skb->head; 590 /* 591 * Allocate the copy buffer 592 */ 593 struct sk_buff *n; 594#ifdef NET_SKBUFF_DATA_USES_OFFSET 595 n = alloc_skb(skb->end + skb->data_len, gfp_mask); 596#else 597 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); 598#endif 599 if (!n) 600 return NULL; 601 602 /* Set the data pointer */ 603 skb_reserve(n, headerlen); 604 /* Set the tail pointer and length */ 605 skb_put(n, skb->len); 606 607 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 608 BUG(); 609 610 copy_skb_header(n, skb); 611 return n; 612} 613 614 615/** 616 * pskb_copy - create copy of an sk_buff with private head. 617 * @skb: buffer to copy 618 * @gfp_mask: allocation priority 619 * 620 * Make a copy of both an &sk_buff and part of its data, located 621 * in header. Fragmented data remain shared. This is used when 622 * the caller wishes to modify only header of &sk_buff and needs 623 * private copy of the header to alter. Returns %NULL on failure 624 * or the pointer to the buffer on success. 625 * The returned buffer has a reference count of 1. 626 */ 627 628struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) 629{ 630 /* 631 * Allocate the copy buffer 632 */ 633 struct sk_buff *n; 634#ifdef NET_SKBUFF_DATA_USES_OFFSET 635 n = alloc_skb(skb->end, gfp_mask); 636#else 637 n = alloc_skb(skb->end - skb->head, gfp_mask); 638#endif 639 if (!n) 640 goto out; 641 642 /* Set the data pointer */ 643 skb_reserve(n, skb->data - skb->head); 644 /* Set the tail pointer and length */ 645 skb_put(n, skb_headlen(skb)); 646 /* Copy the bytes */ 647 skb_copy_from_linear_data(skb, n->data, n->len); 648 649 n->truesize += skb->data_len; 650 n->data_len = skb->data_len; 651 n->len = skb->len; 652 653 if (skb_shinfo(skb)->nr_frags) { 654 int i; 655 656 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 657 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 658 get_page(skb_shinfo(n)->frags[i].page); 659 } 660 skb_shinfo(n)->nr_frags = i; 661 } 662 663 if (skb_shinfo(skb)->frag_list) { 664 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 665 skb_clone_fraglist(n); 666 } 667 668 copy_skb_header(n, skb); 669out: 670 return n; 671} 672 673/** 674 * pskb_expand_head - reallocate header of &sk_buff 675 * @skb: buffer to reallocate 676 * @nhead: room to add at head 677 * @ntail: room to add at tail 678 * @gfp_mask: allocation priority 679 * 680 * Expands (or creates identical copy, if &nhead and &ntail are zero) 681 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 682 * reference count of 1. Returns zero in the case of success or error, 683 * if expansion failed. In the last case, &sk_buff is not changed. 684 * 685 * All the pointers pointing into skb header may change and must be 686 * reloaded after call to this function. 687 */ 688 689int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 690 gfp_t gfp_mask) 691{ 692 int i; 693 u8 *data; 694#ifdef NET_SKBUFF_DATA_USES_OFFSET 695 int size = nhead + skb->end + ntail; 696#else 697 int size = nhead + (skb->end - skb->head) + ntail; 698#endif 699 long off; 700 701 if (skb_shared(skb)) 702 BUG(); 703 704 size = SKB_DATA_ALIGN(size); 705 706 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); 707 if (!data) 708 goto nodata; 709 710 /* Copy only real data... and, alas, header. This should be 711 * optimized for the cases when header is void. */ 712#ifdef NET_SKBUFF_DATA_USES_OFFSET 713 memcpy(data + nhead, skb->head, skb->tail); 714#else 715 memcpy(data + nhead, skb->head, skb->tail - skb->head); 716#endif 717 memcpy(data + size, skb_end_pointer(skb), 718 sizeof(struct skb_shared_info)); 719 720 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 721 get_page(skb_shinfo(skb)->frags[i].page); 722 723 if (skb_shinfo(skb)->frag_list) 724 skb_clone_fraglist(skb); 725 726 skb_release_data(skb); 727 728 off = (data + nhead) - skb->head; 729 730 skb->head = data; 731 skb->data += off; 732#ifdef NET_SKBUFF_DATA_USES_OFFSET 733 skb->end = size; 734 off = nhead; 735#else 736 skb->end = skb->head + size; 737#endif 738 /* {transport,network,mac}_header and tail are relative to skb->head */ 739 skb->tail += off; 740 skb->transport_header += off; 741 skb->network_header += off; 742 skb->mac_header += off; 743 skb->csum_start += nhead; 744 skb->cloned = 0; 745 skb->hdr_len = 0; 746 skb->nohdr = 0; 747 atomic_set(&skb_shinfo(skb)->dataref, 1); 748 return 0; 749 750nodata: 751 return -ENOMEM; 752} 753 754/* Make private copy of skb with writable head and some headroom */ 755 756struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 757{ 758 struct sk_buff *skb2; 759 int delta = headroom - skb_headroom(skb); 760 761 if (delta <= 0) 762 skb2 = pskb_copy(skb, GFP_ATOMIC); 763 else { 764 skb2 = skb_clone(skb, GFP_ATOMIC); 765 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 766 GFP_ATOMIC)) { 767 kfree_skb(skb2); 768 skb2 = NULL; 769 } 770 } 771 return skb2; 772} 773 774 775/** 776 * skb_copy_expand - copy and expand sk_buff 777 * @skb: buffer to copy 778 * @newheadroom: new free bytes at head 779 * @newtailroom: new free bytes at tail 780 * @gfp_mask: allocation priority 781 * 782 * Make a copy of both an &sk_buff and its data and while doing so 783 * allocate additional space. 784 * 785 * This is used when the caller wishes to modify the data and needs a 786 * private copy of the data to alter as well as more space for new fields. 787 * Returns %NULL on failure or the pointer to the buffer 788 * on success. The returned buffer has a reference count of 1. 789 * 790 * You must pass %GFP_ATOMIC as the allocation priority if this function 791 * is called from an interrupt. 792 */ 793struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 794 int newheadroom, int newtailroom, 795 gfp_t gfp_mask) 796{ 797 /* 798 * Allocate the copy buffer 799 */ 800 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 801 gfp_mask); 802 int oldheadroom = skb_headroom(skb); 803 int head_copy_len, head_copy_off; 804 int off; 805 806 if (!n) 807 return NULL; 808 809 skb_reserve(n, newheadroom); 810 811 /* Set the tail pointer and length */ 812 skb_put(n, skb->len); 813 814 head_copy_len = oldheadroom; 815 head_copy_off = 0; 816 if (newheadroom <= head_copy_len) 817 head_copy_len = newheadroom; 818 else 819 head_copy_off = newheadroom - head_copy_len; 820 821 /* Copy the linear header and data. */ 822 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 823 skb->len + head_copy_len)) 824 BUG(); 825 826 copy_skb_header(n, skb); 827 828 off = newheadroom - oldheadroom; 829 n->csum_start += off; 830#ifdef NET_SKBUFF_DATA_USES_OFFSET 831 n->transport_header += off; 832 n->network_header += off; 833 n->mac_header += off; 834#endif 835 836 return n; 837} 838 839/** 840 * skb_pad - zero pad the tail of an skb 841 * @skb: buffer to pad 842 * @pad: space to pad 843 * 844 * Ensure that a buffer is followed by a padding area that is zero 845 * filled. Used by network drivers which may DMA or transfer data 846 * beyond the buffer end onto the wire. 847 * 848 * May return error in out of memory cases. The skb is freed on error. 849 */ 850 851int skb_pad(struct sk_buff *skb, int pad) 852{ 853 int err; 854 int ntail; 855 856 /* If the skbuff is non linear tailroom is always zero.. */ 857 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 858 memset(skb->data+skb->len, 0, pad); 859 return 0; 860 } 861 862 ntail = skb->data_len + pad - (skb->end - skb->tail); 863 if (likely(skb_cloned(skb) || ntail > 0)) { 864 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 865 if (unlikely(err)) 866 goto free_skb; 867 } 868 869 /* FIXME: The use of this function with non-linear skb's really needs 870 * to be audited. 871 */ 872 err = skb_linearize(skb); 873 if (unlikely(err)) 874 goto free_skb; 875 876 memset(skb->data + skb->len, 0, pad); 877 return 0; 878 879free_skb: 880 kfree_skb(skb); 881 return err; 882} 883 884/** 885 * skb_put - add data to a buffer 886 * @skb: buffer to use 887 * @len: amount of data to add 888 * 889 * This function extends the used data area of the buffer. If this would 890 * exceed the total buffer size the kernel will panic. A pointer to the 891 * first byte of the extra data is returned. 892 */ 893unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 894{ 895 unsigned char *tmp = skb_tail_pointer(skb); 896 SKB_LINEAR_ASSERT(skb); 897 skb->tail += len; 898 skb->len += len; 899 if (unlikely(skb->tail > skb->end)) 900 skb_over_panic(skb, len, __builtin_return_address(0)); 901 return tmp; 902} 903EXPORT_SYMBOL(skb_put); 904 905/** 906 * skb_push - add data to the start of a buffer 907 * @skb: buffer to use 908 * @len: amount of data to add 909 * 910 * This function extends the used data area of the buffer at the buffer 911 * start. If this would exceed the total buffer headroom the kernel will 912 * panic. A pointer to the first byte of the extra data is returned. 913 */ 914unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 915{ 916 skb->data -= len; 917 skb->len += len; 918 if (unlikely(skb->data<skb->head)) 919 skb_under_panic(skb, len, __builtin_return_address(0)); 920 return skb->data; 921} 922EXPORT_SYMBOL(skb_push); 923 924/** 925 * skb_pull - remove data from the start of a buffer 926 * @skb: buffer to use 927 * @len: amount of data to remove 928 * 929 * This function removes data from the start of a buffer, returning 930 * the memory to the headroom. A pointer to the next data in the buffer 931 * is returned. Once the data has been pulled future pushes will overwrite 932 * the old data. 933 */ 934unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 935{ 936 return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len); 937} 938EXPORT_SYMBOL(skb_pull); 939 940/** 941 * skb_trim - remove end from a buffer 942 * @skb: buffer to alter 943 * @len: new length 944 * 945 * Cut the length of a buffer down by removing data from the tail. If 946 * the buffer is already under the length specified it is not modified. 947 * The skb must be linear. 948 */ 949void skb_trim(struct sk_buff *skb, unsigned int len) 950{ 951 if (skb->len > len) 952 __skb_trim(skb, len); 953} 954EXPORT_SYMBOL(skb_trim); 955 956/* Trims skb to length len. It can change skb pointers. 957 */ 958 959int ___pskb_trim(struct sk_buff *skb, unsigned int len) 960{ 961 struct sk_buff **fragp; 962 struct sk_buff *frag; 963 int offset = skb_headlen(skb); 964 int nfrags = skb_shinfo(skb)->nr_frags; 965 int i; 966 int err; 967 968 if (skb_cloned(skb) && 969 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 970 return err; 971 972 i = 0; 973 if (offset >= len) 974 goto drop_pages; 975 976 for (; i < nfrags; i++) { 977 int end = offset + skb_shinfo(skb)->frags[i].size; 978 979 if (end < len) { 980 offset = end; 981 continue; 982 } 983 984 skb_shinfo(skb)->frags[i++].size = len - offset; 985 986drop_pages: 987 skb_shinfo(skb)->nr_frags = i; 988 989 for (; i < nfrags; i++) 990 put_page(skb_shinfo(skb)->frags[i].page); 991 992 if (skb_shinfo(skb)->frag_list) 993 skb_drop_fraglist(skb); 994 goto done; 995 } 996 997 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 998 fragp = &frag->next) { 999 int end = offset + frag->len; 1000 1001 if (skb_shared(frag)) { 1002 struct sk_buff *nfrag; 1003 1004 nfrag = skb_clone(frag, GFP_ATOMIC); 1005 if (unlikely(!nfrag)) 1006 return -ENOMEM; 1007 1008 nfrag->next = frag->next; 1009 kfree_skb(frag); 1010 frag = nfrag; 1011 *fragp = frag; 1012 } 1013 1014 if (end < len) { 1015 offset = end; 1016 continue; 1017 } 1018 1019 if (end > len && 1020 unlikely((err = pskb_trim(frag, len - offset)))) 1021 return err; 1022 1023 if (frag->next) 1024 skb_drop_list(&frag->next); 1025 break; 1026 } 1027 1028done: 1029 if (len > skb_headlen(skb)) { 1030 skb->data_len -= skb->len - len; 1031 skb->len = len; 1032 } else { 1033 skb->len = len; 1034 skb->data_len = 0; 1035 skb_set_tail_pointer(skb, len); 1036 } 1037 1038 return 0; 1039} 1040 1041/** 1042 * __pskb_pull_tail - advance tail of skb header 1043 * @skb: buffer to reallocate 1044 * @delta: number of bytes to advance tail 1045 * 1046 * The function makes a sense only on a fragmented &sk_buff, 1047 * it expands header moving its tail forward and copying necessary 1048 * data from fragmented part. 1049 * 1050 * &sk_buff MUST have reference count of 1. 1051 * 1052 * Returns %NULL (and &sk_buff does not change) if pull failed 1053 * or value of new tail of skb in the case of success. 1054 * 1055 * All the pointers pointing into skb header may change and must be 1056 * reloaded after call to this function. 1057 */ 1058 1059/* Moves tail of skb head forward, copying data from fragmented part, 1060 * when it is necessary. 1061 * 1. It may fail due to malloc failure. 1062 * 2. It may change skb pointers. 1063 * 1064 * It is pretty complicated. Luckily, it is called only in exceptional cases. 1065 */ 1066unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 1067{ 1068 /* If skb has not enough free space at tail, get new one 1069 * plus 128 bytes for future expansions. If we have enough 1070 * room at tail, reallocate without expansion only if skb is cloned. 1071 */ 1072 int i, k, eat = (skb->tail + delta) - skb->end; 1073 1074 if (eat > 0 || skb_cloned(skb)) { 1075 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 1076 GFP_ATOMIC)) 1077 return NULL; 1078 } 1079 1080 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 1081 BUG(); 1082 1083 /* Optimization: no fragments, no reasons to preestimate 1084 * size of pulled pages. Superb. 1085 */ 1086 if (!skb_shinfo(skb)->frag_list) 1087 goto pull_pages; 1088 1089 /* Estimate size of pulled pages. */ 1090 eat = delta; 1091 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1092 if (skb_shinfo(skb)->frags[i].size >= eat) 1093 goto pull_pages; 1094 eat -= skb_shinfo(skb)->frags[i].size; 1095 } 1096 1097 /* If we need update frag list, we are in troubles. 1098 * Certainly, it possible to add an offset to skb data, 1099 * but taking into account that pulling is expected to 1100 * be very rare operation, it is worth to fight against 1101 * further bloating skb head and crucify ourselves here instead. 1102 * Pure masohism, indeed. 8)8) 1103 */ 1104 if (eat) { 1105 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1106 struct sk_buff *clone = NULL; 1107 struct sk_buff *insp = NULL; 1108 1109 do { 1110 BUG_ON(!list); 1111 1112 if (list->len <= eat) { 1113 /* Eaten as whole. */ 1114 eat -= list->len; 1115 list = list->next; 1116 insp = list; 1117 } else { 1118 /* Eaten partially. */ 1119 1120 if (skb_shared(list)) { 1121 /* Sucks! We need to fork list. :-( */ 1122 clone = skb_clone(list, GFP_ATOMIC); 1123 if (!clone) 1124 return NULL; 1125 insp = list->next; 1126 list = clone; 1127 } else { 1128 /* This may be pulled without 1129 * problems. */ 1130 insp = list; 1131 } 1132 if (!pskb_pull(list, eat)) { 1133 if (clone) 1134 kfree_skb(clone); 1135 return NULL; 1136 } 1137 break; 1138 } 1139 } while (eat); 1140 1141 /* Free pulled out fragments. */ 1142 while ((list = skb_shinfo(skb)->frag_list) != insp) { 1143 skb_shinfo(skb)->frag_list = list->next; 1144 kfree_skb(list); 1145 } 1146 /* And insert new clone at head. */ 1147 if (clone) { 1148 clone->next = list; 1149 skb_shinfo(skb)->frag_list = clone; 1150 } 1151 } 1152 /* Success! Now we may commit changes to skb data. */ 1153 1154pull_pages: 1155 eat = delta; 1156 k = 0; 1157 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1158 if (skb_shinfo(skb)->frags[i].size <= eat) { 1159 put_page(skb_shinfo(skb)->frags[i].page); 1160 eat -= skb_shinfo(skb)->frags[i].size; 1161 } else { 1162 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 1163 if (eat) { 1164 skb_shinfo(skb)->frags[k].page_offset += eat; 1165 skb_shinfo(skb)->frags[k].size -= eat; 1166 eat = 0; 1167 } 1168 k++; 1169 } 1170 } 1171 skb_shinfo(skb)->nr_frags = k; 1172 1173 skb->tail += delta; 1174 skb->data_len -= delta; 1175 1176 return skb_tail_pointer(skb); 1177} 1178 1179/* Copy some data bits from skb to kernel buffer. */ 1180 1181int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 1182{ 1183 int i, copy; 1184 int start = skb_headlen(skb); 1185 1186 if (offset > (int)skb->len - len) 1187 goto fault; 1188 1189 /* Copy header. */ 1190 if ((copy = start - offset) > 0) { 1191 if (copy > len) 1192 copy = len; 1193 skb_copy_from_linear_data_offset(skb, offset, to, copy); 1194 if ((len -= copy) == 0) 1195 return 0; 1196 offset += copy; 1197 to += copy; 1198 } 1199 1200 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1201 int end; 1202 1203 BUG_TRAP(start <= offset + len); 1204 1205 end = start + skb_shinfo(skb)->frags[i].size; 1206 if ((copy = end - offset) > 0) { 1207 u8 *vaddr; 1208 1209 if (copy > len) 1210 copy = len; 1211 1212 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); 1213 memcpy(to, 1214 vaddr + skb_shinfo(skb)->frags[i].page_offset+ 1215 offset - start, copy); 1216 kunmap_skb_frag(vaddr); 1217 1218 if ((len -= copy) == 0) 1219 return 0; 1220 offset += copy; 1221 to += copy; 1222 } 1223 start = end; 1224 } 1225 1226 if (skb_shinfo(skb)->frag_list) { 1227 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1228 1229 for (; list; list = list->next) { 1230 int end; 1231 1232 BUG_TRAP(start <= offset + len); 1233 1234 end = start + list->len; 1235 if ((copy = end - offset) > 0) { 1236 if (copy > len) 1237 copy = len; 1238 if (skb_copy_bits(list, offset - start, 1239 to, copy)) 1240 goto fault; 1241 if ((len -= copy) == 0) 1242 return 0; 1243 offset += copy; 1244 to += copy; 1245 } 1246 start = end; 1247 } 1248 } 1249 if (!len) 1250 return 0; 1251 1252fault: 1253 return -EFAULT; 1254} 1255 1256/* 1257 * Callback from splice_to_pipe(), if we need to release some pages 1258 * at the end of the spd in case we error'ed out in filling the pipe. 1259 */ 1260static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 1261{ 1262 struct sk_buff *skb = (struct sk_buff *) spd->partial[i].private; 1263 1264 kfree_skb(skb); 1265} 1266 1267/* 1268 * Fill page/offset/length into spd, if it can hold more pages. 1269 */ 1270static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page, 1271 unsigned int len, unsigned int offset, 1272 struct sk_buff *skb) 1273{ 1274 if (unlikely(spd->nr_pages == PIPE_BUFFERS)) 1275 return 1; 1276 1277 spd->pages[spd->nr_pages] = page; 1278 spd->partial[spd->nr_pages].len = len; 1279 spd->partial[spd->nr_pages].offset = offset; 1280 spd->partial[spd->nr_pages].private = (unsigned long) skb_get(skb); 1281 spd->nr_pages++; 1282 return 0; 1283} 1284 1285/* 1286 * Map linear and fragment data from the skb to spd. Returns number of 1287 * pages mapped. 1288 */ 1289static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset, 1290 unsigned int *total_len, 1291 struct splice_pipe_desc *spd) 1292{ 1293 unsigned int nr_pages = spd->nr_pages; 1294 unsigned int poff, plen, len, toff, tlen; 1295 int headlen, seg; 1296 1297 toff = *offset; 1298 tlen = *total_len; 1299 if (!tlen) 1300 goto err; 1301 1302 /* 1303 * if the offset is greater than the linear part, go directly to 1304 * the fragments. 1305 */ 1306 headlen = skb_headlen(skb); 1307 if (toff >= headlen) { 1308 toff -= headlen; 1309 goto map_frag; 1310 } 1311 1312 /* 1313 * first map the linear region into the pages/partial map, skipping 1314 * any potential initial offset. 1315 */ 1316 len = 0; 1317 while (len < headlen) { 1318 void *p = skb->data + len; 1319 1320 poff = (unsigned long) p & (PAGE_SIZE - 1); 1321 plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff); 1322 len += plen; 1323 1324 if (toff) { 1325 if (plen <= toff) { 1326 toff -= plen; 1327 continue; 1328 } 1329 plen -= toff; 1330 poff += toff; 1331 toff = 0; 1332 } 1333 1334 plen = min(plen, tlen); 1335 if (!plen) 1336 break; 1337 1338 /* 1339 * just jump directly to update and return, no point 1340 * in going over fragments when the output is full. 1341 */ 1342 if (spd_fill_page(spd, virt_to_page(p), plen, poff, skb)) 1343 goto done; 1344 1345 tlen -= plen; 1346 } 1347 1348 /* 1349 * then map the fragments 1350 */ 1351map_frag: 1352 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 1353 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 1354 1355 plen = f->size; 1356 poff = f->page_offset; 1357 1358 if (toff) { 1359 if (plen <= toff) { 1360 toff -= plen; 1361 continue; 1362 } 1363 plen -= toff; 1364 poff += toff; 1365 toff = 0; 1366 } 1367 1368 plen = min(plen, tlen); 1369 if (!plen) 1370 break; 1371 1372 if (spd_fill_page(spd, f->page, plen, poff, skb)) 1373 break; 1374 1375 tlen -= plen; 1376 } 1377 1378done: 1379 if (spd->nr_pages - nr_pages) { 1380 *offset = 0; 1381 *total_len = tlen; 1382 return 0; 1383 } 1384err: 1385 return 1; 1386} 1387 1388/* 1389 * Map data from the skb to a pipe. Should handle both the linear part, 1390 * the fragments, and the frag list. It does NOT handle frag lists within 1391 * the frag list, if such a thing exists. We'd probably need to recurse to 1392 * handle that cleanly. 1393 */ 1394int skb_splice_bits(struct sk_buff *__skb, unsigned int offset, 1395 struct pipe_inode_info *pipe, unsigned int tlen, 1396 unsigned int flags) 1397{ 1398 struct partial_page partial[PIPE_BUFFERS]; 1399 struct page *pages[PIPE_BUFFERS]; 1400 struct splice_pipe_desc spd = { 1401 .pages = pages, 1402 .partial = partial, 1403 .flags = flags, 1404 .ops = &sock_pipe_buf_ops, 1405 .spd_release = sock_spd_release, 1406 }; 1407 struct sk_buff *skb; 1408 1409 /* 1410 * I'd love to avoid the clone here, but tcp_read_sock() 1411 * ignores reference counts and unconditonally kills the sk_buff 1412 * on return from the actor. 1413 */ 1414 skb = skb_clone(__skb, GFP_KERNEL); 1415 if (unlikely(!skb)) 1416 return -ENOMEM; 1417 1418 /* 1419 * __skb_splice_bits() only fails if the output has no room left, 1420 * so no point in going over the frag_list for the error case. 1421 */ 1422 if (__skb_splice_bits(skb, &offset, &tlen, &spd)) 1423 goto done; 1424 else if (!tlen) 1425 goto done; 1426 1427 /* 1428 * now see if we have a frag_list to map 1429 */ 1430 if (skb_shinfo(skb)->frag_list) { 1431 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1432 1433 for (; list && tlen; list = list->next) { 1434 if (__skb_splice_bits(list, &offset, &tlen, &spd)) 1435 break; 1436 } 1437 } 1438 1439done: 1440 /* 1441 * drop our reference to the clone, the pipe consumption will 1442 * drop the rest. 1443 */ 1444 kfree_skb(skb); 1445 1446 if (spd.nr_pages) { 1447 int ret; 1448 1449 /* 1450 * Drop the socket lock, otherwise we have reverse 1451 * locking dependencies between sk_lock and i_mutex 1452 * here as compared to sendfile(). We enter here 1453 * with the socket lock held, and splice_to_pipe() will 1454 * grab the pipe inode lock. For sendfile() emulation, 1455 * we call into ->sendpage() with the i_mutex lock held 1456 * and networking will grab the socket lock. 1457 */ 1458 release_sock(__skb->sk); 1459 ret = splice_to_pipe(pipe, &spd); 1460 lock_sock(__skb->sk); 1461 return ret; 1462 } 1463 1464 return 0; 1465} 1466 1467/** 1468 * skb_store_bits - store bits from kernel buffer to skb 1469 * @skb: destination buffer 1470 * @offset: offset in destination 1471 * @from: source buffer 1472 * @len: number of bytes to copy 1473 * 1474 * Copy the specified number of bytes from the source buffer to the 1475 * destination skb. This function handles all the messy bits of 1476 * traversing fragment lists and such. 1477 */ 1478 1479int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1480{ 1481 int i, copy; 1482 int start = skb_headlen(skb); 1483 1484 if (offset > (int)skb->len - len) 1485 goto fault; 1486 1487 if ((copy = start - offset) > 0) { 1488 if (copy > len) 1489 copy = len; 1490 skb_copy_to_linear_data_offset(skb, offset, from, copy); 1491 if ((len -= copy) == 0) 1492 return 0; 1493 offset += copy; 1494 from += copy; 1495 } 1496 1497 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1498 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1499 int end; 1500 1501 BUG_TRAP(start <= offset + len); 1502 1503 end = start + frag->size; 1504 if ((copy = end - offset) > 0) { 1505 u8 *vaddr; 1506 1507 if (copy > len) 1508 copy = len; 1509 1510 vaddr = kmap_skb_frag(frag); 1511 memcpy(vaddr + frag->page_offset + offset - start, 1512 from, copy); 1513 kunmap_skb_frag(vaddr); 1514 1515 if ((len -= copy) == 0) 1516 return 0; 1517 offset += copy; 1518 from += copy; 1519 } 1520 start = end; 1521 } 1522 1523 if (skb_shinfo(skb)->frag_list) { 1524 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1525 1526 for (; list; list = list->next) { 1527 int end; 1528 1529 BUG_TRAP(start <= offset + len); 1530 1531 end = start + list->len; 1532 if ((copy = end - offset) > 0) { 1533 if (copy > len) 1534 copy = len; 1535 if (skb_store_bits(list, offset - start, 1536 from, copy)) 1537 goto fault; 1538 if ((len -= copy) == 0) 1539 return 0; 1540 offset += copy; 1541 from += copy; 1542 } 1543 start = end; 1544 } 1545 } 1546 if (!len) 1547 return 0; 1548 1549fault: 1550 return -EFAULT; 1551} 1552 1553EXPORT_SYMBOL(skb_store_bits); 1554 1555/* Checksum skb data. */ 1556 1557__wsum skb_checksum(const struct sk_buff *skb, int offset, 1558 int len, __wsum csum) 1559{ 1560 int start = skb_headlen(skb); 1561 int i, copy = start - offset; 1562 int pos = 0; 1563 1564 /* Checksum header. */ 1565 if (copy > 0) { 1566 if (copy > len) 1567 copy = len; 1568 csum = csum_partial(skb->data + offset, copy, csum); 1569 if ((len -= copy) == 0) 1570 return csum; 1571 offset += copy; 1572 pos = copy; 1573 } 1574 1575 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1576 int end; 1577 1578 BUG_TRAP(start <= offset + len); 1579 1580 end = start + skb_shinfo(skb)->frags[i].size; 1581 if ((copy = end - offset) > 0) { 1582 __wsum csum2; 1583 u8 *vaddr; 1584 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1585 1586 if (copy > len) 1587 copy = len; 1588 vaddr = kmap_skb_frag(frag); 1589 csum2 = csum_partial(vaddr + frag->page_offset + 1590 offset - start, copy, 0); 1591 kunmap_skb_frag(vaddr); 1592 csum = csum_block_add(csum, csum2, pos); 1593 if (!(len -= copy)) 1594 return csum; 1595 offset += copy; 1596 pos += copy; 1597 } 1598 start = end; 1599 } 1600 1601 if (skb_shinfo(skb)->frag_list) { 1602 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1603 1604 for (; list; list = list->next) { 1605 int end; 1606 1607 BUG_TRAP(start <= offset + len); 1608 1609 end = start + list->len; 1610 if ((copy = end - offset) > 0) { 1611 __wsum csum2; 1612 if (copy > len) 1613 copy = len; 1614 csum2 = skb_checksum(list, offset - start, 1615 copy, 0); 1616 csum = csum_block_add(csum, csum2, pos); 1617 if ((len -= copy) == 0) 1618 return csum; 1619 offset += copy; 1620 pos += copy; 1621 } 1622 start = end; 1623 } 1624 } 1625 BUG_ON(len); 1626 1627 return csum; 1628} 1629 1630/* Both of above in one bottle. */ 1631 1632__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 1633 u8 *to, int len, __wsum csum) 1634{ 1635 int start = skb_headlen(skb); 1636 int i, copy = start - offset; 1637 int pos = 0; 1638 1639 /* Copy header. */ 1640 if (copy > 0) { 1641 if (copy > len) 1642 copy = len; 1643 csum = csum_partial_copy_nocheck(skb->data + offset, to, 1644 copy, csum); 1645 if ((len -= copy) == 0) 1646 return csum; 1647 offset += copy; 1648 to += copy; 1649 pos = copy; 1650 } 1651 1652 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1653 int end; 1654 1655 BUG_TRAP(start <= offset + len); 1656 1657 end = start + skb_shinfo(skb)->frags[i].size; 1658 if ((copy = end - offset) > 0) { 1659 __wsum csum2; 1660 u8 *vaddr; 1661 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1662 1663 if (copy > len) 1664 copy = len; 1665 vaddr = kmap_skb_frag(frag); 1666 csum2 = csum_partial_copy_nocheck(vaddr + 1667 frag->page_offset + 1668 offset - start, to, 1669 copy, 0); 1670 kunmap_skb_frag(vaddr); 1671 csum = csum_block_add(csum, csum2, pos); 1672 if (!(len -= copy)) 1673 return csum; 1674 offset += copy; 1675 to += copy; 1676 pos += copy; 1677 } 1678 start = end; 1679 } 1680 1681 if (skb_shinfo(skb)->frag_list) { 1682 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1683 1684 for (; list; list = list->next) { 1685 __wsum csum2; 1686 int end; 1687 1688 BUG_TRAP(start <= offset + len); 1689 1690 end = start + list->len; 1691 if ((copy = end - offset) > 0) { 1692 if (copy > len) 1693 copy = len; 1694 csum2 = skb_copy_and_csum_bits(list, 1695 offset - start, 1696 to, copy, 0); 1697 csum = csum_block_add(csum, csum2, pos); 1698 if ((len -= copy) == 0) 1699 return csum; 1700 offset += copy; 1701 to += copy; 1702 pos += copy; 1703 } 1704 start = end; 1705 } 1706 } 1707 BUG_ON(len); 1708 return csum; 1709} 1710 1711void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 1712{ 1713 __wsum csum; 1714 long csstart; 1715 1716 if (skb->ip_summed == CHECKSUM_PARTIAL) 1717 csstart = skb->csum_start - skb_headroom(skb); 1718 else 1719 csstart = skb_headlen(skb); 1720 1721 BUG_ON(csstart > skb_headlen(skb)); 1722 1723 skb_copy_from_linear_data(skb, to, csstart); 1724 1725 csum = 0; 1726 if (csstart != skb->len) 1727 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 1728 skb->len - csstart, 0); 1729 1730 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1731 long csstuff = csstart + skb->csum_offset; 1732 1733 *((__sum16 *)(to + csstuff)) = csum_fold(csum); 1734 } 1735} 1736 1737/** 1738 * skb_dequeue - remove from the head of the queue 1739 * @list: list to dequeue from 1740 * 1741 * Remove the head of the list. The list lock is taken so the function 1742 * may be used safely with other locking list functions. The head item is 1743 * returned or %NULL if the list is empty. 1744 */ 1745 1746struct sk_buff *skb_dequeue(struct sk_buff_head *list) 1747{ 1748 unsigned long flags; 1749 struct sk_buff *result; 1750 1751 spin_lock_irqsave(&list->lock, flags); 1752 result = __skb_dequeue(list); 1753 spin_unlock_irqrestore(&list->lock, flags); 1754 return result; 1755} 1756 1757/** 1758 * skb_dequeue_tail - remove from the tail of the queue 1759 * @list: list to dequeue from 1760 * 1761 * Remove the tail of the list. The list lock is taken so the function 1762 * may be used safely with other locking list functions. The tail item is 1763 * returned or %NULL if the list is empty. 1764 */ 1765struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 1766{ 1767 unsigned long flags; 1768 struct sk_buff *result; 1769 1770 spin_lock_irqsave(&list->lock, flags); 1771 result = __skb_dequeue_tail(list); 1772 spin_unlock_irqrestore(&list->lock, flags); 1773 return result; 1774} 1775 1776/** 1777 * skb_queue_purge - empty a list 1778 * @list: list to empty 1779 * 1780 * Delete all buffers on an &sk_buff list. Each buffer is removed from 1781 * the list and one reference dropped. This function takes the list 1782 * lock and is atomic with respect to other list locking functions. 1783 */ 1784void skb_queue_purge(struct sk_buff_head *list) 1785{ 1786 struct sk_buff *skb; 1787 while ((skb = skb_dequeue(list)) != NULL) 1788 kfree_skb(skb); 1789} 1790 1791/** 1792 * skb_queue_head - queue a buffer at the list head 1793 * @list: list to use 1794 * @newsk: buffer to queue 1795 * 1796 * Queue a buffer at the start of the list. This function takes the 1797 * list lock and can be used safely with other locking &sk_buff functions 1798 * safely. 1799 * 1800 * A buffer cannot be placed on two lists at the same time. 1801 */ 1802void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 1803{ 1804 unsigned long flags; 1805 1806 spin_lock_irqsave(&list->lock, flags); 1807 __skb_queue_head(list, newsk); 1808 spin_unlock_irqrestore(&list->lock, flags); 1809} 1810 1811/** 1812 * skb_queue_tail - queue a buffer at the list tail 1813 * @list: list to use 1814 * @newsk: buffer to queue 1815 * 1816 * Queue a buffer at the tail of the list. This function takes the 1817 * list lock and can be used safely with other locking &sk_buff functions 1818 * safely. 1819 * 1820 * A buffer cannot be placed on two lists at the same time. 1821 */ 1822void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 1823{ 1824 unsigned long flags; 1825 1826 spin_lock_irqsave(&list->lock, flags); 1827 __skb_queue_tail(list, newsk); 1828 spin_unlock_irqrestore(&list->lock, flags); 1829} 1830 1831/** 1832 * skb_unlink - remove a buffer from a list 1833 * @skb: buffer to remove 1834 * @list: list to use 1835 * 1836 * Remove a packet from a list. The list locks are taken and this 1837 * function is atomic with respect to other list locked calls 1838 * 1839 * You must know what list the SKB is on. 1840 */ 1841void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 1842{ 1843 unsigned long flags; 1844 1845 spin_lock_irqsave(&list->lock, flags); 1846 __skb_unlink(skb, list); 1847 spin_unlock_irqrestore(&list->lock, flags); 1848} 1849 1850/** 1851 * skb_append - append a buffer 1852 * @old: buffer to insert after 1853 * @newsk: buffer to insert 1854 * @list: list to use 1855 * 1856 * Place a packet after a given packet in a list. The list locks are taken 1857 * and this function is atomic with respect to other list locked calls. 1858 * A buffer cannot be placed on two lists at the same time. 1859 */ 1860void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 1861{ 1862 unsigned long flags; 1863 1864 spin_lock_irqsave(&list->lock, flags); 1865 __skb_queue_after(list, old, newsk); 1866 spin_unlock_irqrestore(&list->lock, flags); 1867} 1868 1869 1870/** 1871 * skb_insert - insert a buffer 1872 * @old: buffer to insert before 1873 * @newsk: buffer to insert 1874 * @list: list to use 1875 * 1876 * Place a packet before a given packet in a list. The list locks are 1877 * taken and this function is atomic with respect to other list locked 1878 * calls. 1879 * 1880 * A buffer cannot be placed on two lists at the same time. 1881 */ 1882void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 1883{ 1884 unsigned long flags; 1885 1886 spin_lock_irqsave(&list->lock, flags); 1887 __skb_insert(newsk, old->prev, old, list); 1888 spin_unlock_irqrestore(&list->lock, flags); 1889} 1890 1891static inline void skb_split_inside_header(struct sk_buff *skb, 1892 struct sk_buff* skb1, 1893 const u32 len, const int pos) 1894{ 1895 int i; 1896 1897 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 1898 pos - len); 1899 /* And move data appendix as is. */ 1900 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1901 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 1902 1903 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 1904 skb_shinfo(skb)->nr_frags = 0; 1905 skb1->data_len = skb->data_len; 1906 skb1->len += skb1->data_len; 1907 skb->data_len = 0; 1908 skb->len = len; 1909 skb_set_tail_pointer(skb, len); 1910} 1911 1912static inline void skb_split_no_header(struct sk_buff *skb, 1913 struct sk_buff* skb1, 1914 const u32 len, int pos) 1915{ 1916 int i, k = 0; 1917 const int nfrags = skb_shinfo(skb)->nr_frags; 1918 1919 skb_shinfo(skb)->nr_frags = 0; 1920 skb1->len = skb1->data_len = skb->len - len; 1921 skb->len = len; 1922 skb->data_len = len - pos; 1923 1924 for (i = 0; i < nfrags; i++) { 1925 int size = skb_shinfo(skb)->frags[i].size; 1926 1927 if (pos + size > len) { 1928 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 1929 1930 if (pos < len) { 1931 /* Split frag. 1932 * We have two variants in this case: 1933 * 1. Move all the frag to the second 1934 * part, if it is possible. F.e. 1935 * this approach is mandatory for TUX, 1936 * where splitting is expensive. 1937 * 2. Split is accurately. We make this. 1938 */ 1939 get_page(skb_shinfo(skb)->frags[i].page); 1940 skb_shinfo(skb1)->frags[0].page_offset += len - pos; 1941 skb_shinfo(skb1)->frags[0].size -= len - pos; 1942 skb_shinfo(skb)->frags[i].size = len - pos; 1943 skb_shinfo(skb)->nr_frags++; 1944 } 1945 k++; 1946 } else 1947 skb_shinfo(skb)->nr_frags++; 1948 pos += size; 1949 } 1950 skb_shinfo(skb1)->nr_frags = k; 1951} 1952 1953/** 1954 * skb_split - Split fragmented skb to two parts at length len. 1955 * @skb: the buffer to split 1956 * @skb1: the buffer to receive the second part 1957 * @len: new length for skb 1958 */ 1959void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 1960{ 1961 int pos = skb_headlen(skb); 1962 1963 if (len < pos) /* Split line is inside header. */ 1964 skb_split_inside_header(skb, skb1, len, pos); 1965 else /* Second chunk has no header, nothing to copy. */ 1966 skb_split_no_header(skb, skb1, len, pos); 1967} 1968 1969/** 1970 * skb_prepare_seq_read - Prepare a sequential read of skb data 1971 * @skb: the buffer to read 1972 * @from: lower offset of data to be read 1973 * @to: upper offset of data to be read 1974 * @st: state variable 1975 * 1976 * Initializes the specified state variable. Must be called before 1977 * invoking skb_seq_read() for the first time. 1978 */ 1979void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 1980 unsigned int to, struct skb_seq_state *st) 1981{ 1982 st->lower_offset = from; 1983 st->upper_offset = to; 1984 st->root_skb = st->cur_skb = skb; 1985 st->frag_idx = st->stepped_offset = 0; 1986 st->frag_data = NULL; 1987} 1988 1989/** 1990 * skb_seq_read - Sequentially read skb data 1991 * @consumed: number of bytes consumed by the caller so far 1992 * @data: destination pointer for data to be returned 1993 * @st: state variable 1994 * 1995 * Reads a block of skb data at &consumed relative to the 1996 * lower offset specified to skb_prepare_seq_read(). Assigns 1997 * the head of the data block to &data and returns the length 1998 * of the block or 0 if the end of the skb data or the upper 1999 * offset has been reached. 2000 * 2001 * The caller is not required to consume all of the data 2002 * returned, i.e. &consumed is typically set to the number 2003 * of bytes already consumed and the next call to 2004 * skb_seq_read() will return the remaining part of the block. 2005 * 2006 * Note 1: The size of each block of data returned can be arbitary, 2007 * this limitation is the cost for zerocopy seqeuental 2008 * reads of potentially non linear data. 2009 * 2010 * Note 2: Fragment lists within fragments are not implemented 2011 * at the moment, state->root_skb could be replaced with 2012 * a stack for this purpose. 2013 */ 2014unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2015 struct skb_seq_state *st) 2016{ 2017 unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2018 skb_frag_t *frag; 2019 2020 if (unlikely(abs_offset >= st->upper_offset)) 2021 return 0; 2022 2023next_skb: 2024 block_limit = skb_headlen(st->cur_skb); 2025 2026 if (abs_offset < block_limit) { 2027 *data = st->cur_skb->data + abs_offset; 2028 return block_limit - abs_offset; 2029 } 2030 2031 if (st->frag_idx == 0 && !st->frag_data) 2032 st->stepped_offset += skb_headlen(st->cur_skb); 2033 2034 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2035 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 2036 block_limit = frag->size + st->stepped_offset; 2037 2038 if (abs_offset < block_limit) { 2039 if (!st->frag_data) 2040 st->frag_data = kmap_skb_frag(frag); 2041 2042 *data = (u8 *) st->frag_data + frag->page_offset + 2043 (abs_offset - st->stepped_offset); 2044 2045 return block_limit - abs_offset; 2046 } 2047 2048 if (st->frag_data) { 2049 kunmap_skb_frag(st->frag_data); 2050 st->frag_data = NULL; 2051 } 2052 2053 st->frag_idx++; 2054 st->stepped_offset += frag->size; 2055 } 2056 2057 if (st->frag_data) { 2058 kunmap_skb_frag(st->frag_data); 2059 st->frag_data = NULL; 2060 } 2061 2062 if (st->cur_skb->next) { 2063 st->cur_skb = st->cur_skb->next; 2064 st->frag_idx = 0; 2065 goto next_skb; 2066 } else if (st->root_skb == st->cur_skb && 2067 skb_shinfo(st->root_skb)->frag_list) { 2068 st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 2069 goto next_skb; 2070 } 2071 2072 return 0; 2073} 2074 2075/** 2076 * skb_abort_seq_read - Abort a sequential read of skb data 2077 * @st: state variable 2078 * 2079 * Must be called if skb_seq_read() was not called until it 2080 * returned 0. 2081 */ 2082void skb_abort_seq_read(struct skb_seq_state *st) 2083{ 2084 if (st->frag_data) 2085 kunmap_skb_frag(st->frag_data); 2086} 2087 2088#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 2089 2090static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 2091 struct ts_config *conf, 2092 struct ts_state *state) 2093{ 2094 return skb_seq_read(offset, text, TS_SKB_CB(state)); 2095} 2096 2097static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 2098{ 2099 skb_abort_seq_read(TS_SKB_CB(state)); 2100} 2101 2102/** 2103 * skb_find_text - Find a text pattern in skb data 2104 * @skb: the buffer to look in 2105 * @from: search offset 2106 * @to: search limit 2107 * @config: textsearch configuration 2108 * @state: uninitialized textsearch state variable 2109 * 2110 * Finds a pattern in the skb data according to the specified 2111 * textsearch configuration. Use textsearch_next() to retrieve 2112 * subsequent occurrences of the pattern. Returns the offset 2113 * to the first occurrence or UINT_MAX if no match was found. 2114 */ 2115unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2116 unsigned int to, struct ts_config *config, 2117 struct ts_state *state) 2118{ 2119 unsigned int ret; 2120 2121 config->get_next_block = skb_ts_get_next_block; 2122 config->finish = skb_ts_finish; 2123 2124 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 2125 2126 ret = textsearch_find(config, state); 2127 return (ret <= to - from ? ret : UINT_MAX); 2128} 2129 2130/** 2131 * skb_append_datato_frags: - append the user data to a skb 2132 * @sk: sock structure 2133 * @skb: skb structure to be appened with user data. 2134 * @getfrag: call back function to be used for getting the user data 2135 * @from: pointer to user message iov 2136 * @length: length of the iov message 2137 * 2138 * Description: This procedure append the user data in the fragment part 2139 * of the skb if any page alloc fails user this procedure returns -ENOMEM 2140 */ 2141int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2142 int (*getfrag)(void *from, char *to, int offset, 2143 int len, int odd, struct sk_buff *skb), 2144 void *from, int length) 2145{ 2146 int frg_cnt = 0; 2147 skb_frag_t *frag = NULL; 2148 struct page *page = NULL; 2149 int copy, left; 2150 int offset = 0; 2151 int ret; 2152 2153 do { 2154 /* Return error if we don't have space for new frag */ 2155 frg_cnt = skb_shinfo(skb)->nr_frags; 2156 if (frg_cnt >= MAX_SKB_FRAGS) 2157 return -EFAULT; 2158 2159 /* allocate a new page for next frag */ 2160 page = alloc_pages(sk->sk_allocation, 0); 2161 2162 /* If alloc_page fails just return failure and caller will 2163 * free previous allocated pages by doing kfree_skb() 2164 */ 2165 if (page == NULL) 2166 return -ENOMEM; 2167 2168 /* initialize the next frag */ 2169 sk->sk_sndmsg_page = page; 2170 sk->sk_sndmsg_off = 0; 2171 skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2172 skb->truesize += PAGE_SIZE; 2173 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2174 2175 /* get the new initialized frag */ 2176 frg_cnt = skb_shinfo(skb)->nr_frags; 2177 frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2178 2179 /* copy the user data to page */ 2180 left = PAGE_SIZE - frag->page_offset; 2181 copy = (length > left)? left : length; 2182 2183 ret = getfrag(from, (page_address(frag->page) + 2184 frag->page_offset + frag->size), 2185 offset, copy, 0, skb); 2186 if (ret < 0) 2187 return -EFAULT; 2188 2189 /* copy was successful so update the size parameters */ 2190 sk->sk_sndmsg_off += copy; 2191 frag->size += copy; 2192 skb->len += copy; 2193 skb->data_len += copy; 2194 offset += copy; 2195 length -= copy; 2196 2197 } while (length > 0); 2198 2199 return 0; 2200} 2201 2202/** 2203 * skb_pull_rcsum - pull skb and update receive checksum 2204 * @skb: buffer to update 2205 * @len: length of data pulled 2206 * 2207 * This function performs an skb_pull on the packet and updates 2208 * the CHECKSUM_COMPLETE checksum. It should be used on 2209 * receive path processing instead of skb_pull unless you know 2210 * that the checksum difference is zero (e.g., a valid IP header) 2211 * or you are setting ip_summed to CHECKSUM_NONE. 2212 */ 2213unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2214{ 2215 BUG_ON(len > skb->len); 2216 skb->len -= len; 2217 BUG_ON(skb->len < skb->data_len); 2218 skb_postpull_rcsum(skb, skb->data, len); 2219 return skb->data += len; 2220} 2221 2222EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2223 2224/** 2225 * skb_segment - Perform protocol segmentation on skb. 2226 * @skb: buffer to segment 2227 * @features: features for the output path (see dev->features) 2228 * 2229 * This function performs segmentation on the given skb. It returns 2230 * a pointer to the first in a list of new skbs for the segments. 2231 * In case of error it returns ERR_PTR(err). 2232 */ 2233struct sk_buff *skb_segment(struct sk_buff *skb, int features) 2234{ 2235 struct sk_buff *segs = NULL; 2236 struct sk_buff *tail = NULL; 2237 unsigned int mss = skb_shinfo(skb)->gso_size; 2238 unsigned int doffset = skb->data - skb_mac_header(skb); 2239 unsigned int offset = doffset; 2240 unsigned int headroom; 2241 unsigned int len; 2242 int sg = features & NETIF_F_SG; 2243 int nfrags = skb_shinfo(skb)->nr_frags; 2244 int err = -ENOMEM; 2245 int i = 0; 2246 int pos; 2247 2248 __skb_push(skb, doffset); 2249 headroom = skb_headroom(skb); 2250 pos = skb_headlen(skb); 2251 2252 do { 2253 struct sk_buff *nskb; 2254 skb_frag_t *frag; 2255 int hsize; 2256 int k; 2257 int size; 2258 2259 len = skb->len - offset; 2260 if (len > mss) 2261 len = mss; 2262 2263 hsize = skb_headlen(skb) - offset; 2264 if (hsize < 0) 2265 hsize = 0; 2266 if (hsize > len || !sg) 2267 hsize = len; 2268 2269 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); 2270 if (unlikely(!nskb)) 2271 goto err; 2272 2273 if (segs) 2274 tail->next = nskb; 2275 else 2276 segs = nskb; 2277 tail = nskb; 2278 2279 nskb->dev = skb->dev; 2280 skb_copy_queue_mapping(nskb, skb); 2281 nskb->priority = skb->priority; 2282 nskb->protocol = skb->protocol; 2283 nskb->dst = dst_clone(skb->dst); 2284 memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 2285 nskb->pkt_type = skb->pkt_type; 2286 nskb->mac_len = skb->mac_len; 2287 2288 skb_reserve(nskb, headroom); 2289 skb_reset_mac_header(nskb); 2290 skb_set_network_header(nskb, skb->mac_len); 2291 nskb->transport_header = (nskb->network_header + 2292 skb_network_header_len(skb)); 2293 skb_copy_from_linear_data(skb, skb_put(nskb, doffset), 2294 doffset); 2295 if (!sg) { 2296 nskb->csum = skb_copy_and_csum_bits(skb, offset, 2297 skb_put(nskb, len), 2298 len, 0); 2299 continue; 2300 } 2301 2302 frag = skb_shinfo(nskb)->frags; 2303 k = 0; 2304 2305 nskb->ip_summed = CHECKSUM_PARTIAL; 2306 nskb->csum = skb->csum; 2307 skb_copy_from_linear_data_offset(skb, offset, 2308 skb_put(nskb, hsize), hsize); 2309 2310 while (pos < offset + len) { 2311 BUG_ON(i >= nfrags); 2312 2313 *frag = skb_shinfo(skb)->frags[i]; 2314 get_page(frag->page); 2315 size = frag->size; 2316 2317 if (pos < offset) { 2318 frag->page_offset += offset - pos; 2319 frag->size -= offset - pos; 2320 } 2321 2322 k++; 2323 2324 if (pos + size <= offset + len) { 2325 i++; 2326 pos += size; 2327 } else { 2328 frag->size -= pos + size - (offset + len); 2329 break; 2330 } 2331 2332 frag++; 2333 } 2334 2335 skb_shinfo(nskb)->nr_frags = k; 2336 nskb->data_len = len - hsize; 2337 nskb->len += nskb->data_len; 2338 nskb->truesize += nskb->data_len; 2339 } while ((offset += len) < skb->len); 2340 2341 return segs; 2342 2343err: 2344 while ((skb = segs)) { 2345 segs = skb->next; 2346 kfree_skb(skb); 2347 } 2348 return ERR_PTR(err); 2349} 2350 2351EXPORT_SYMBOL_GPL(skb_segment); 2352 2353void __init skb_init(void) 2354{ 2355 skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 2356 sizeof(struct sk_buff), 2357 0, 2358 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 2359 NULL); 2360 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 2361 (2*sizeof(struct sk_buff)) + 2362 sizeof(atomic_t), 2363 0, 2364 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 2365 NULL); 2366} 2367 2368/** 2369 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 2370 * @skb: Socket buffer containing the buffers to be mapped 2371 * @sg: The scatter-gather list to map into 2372 * @offset: The offset into the buffer's contents to start mapping 2373 * @len: Length of buffer space to be mapped 2374 * 2375 * Fill the specified scatter-gather list with mappings/pointers into a 2376 * region of the buffer space attached to a socket buffer. 2377 */ 2378static int 2379__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2380{ 2381 int start = skb_headlen(skb); 2382 int i, copy = start - offset; 2383 int elt = 0; 2384 2385 if (copy > 0) { 2386 if (copy > len) 2387 copy = len; 2388 sg_set_buf(sg, skb->data + offset, copy); 2389 elt++; 2390 if ((len -= copy) == 0) 2391 return elt; 2392 offset += copy; 2393 } 2394 2395 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2396 int end; 2397 2398 BUG_TRAP(start <= offset + len); 2399 2400 end = start + skb_shinfo(skb)->frags[i].size; 2401 if ((copy = end - offset) > 0) { 2402 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2403 2404 if (copy > len) 2405 copy = len; 2406 sg_set_page(&sg[elt], frag->page, copy, 2407 frag->page_offset+offset-start); 2408 elt++; 2409 if (!(len -= copy)) 2410 return elt; 2411 offset += copy; 2412 } 2413 start = end; 2414 } 2415 2416 if (skb_shinfo(skb)->frag_list) { 2417 struct sk_buff *list = skb_shinfo(skb)->frag_list; 2418 2419 for (; list; list = list->next) { 2420 int end; 2421 2422 BUG_TRAP(start <= offset + len); 2423 2424 end = start + list->len; 2425 if ((copy = end - offset) > 0) { 2426 if (copy > len) 2427 copy = len; 2428 elt += __skb_to_sgvec(list, sg+elt, offset - start, 2429 copy); 2430 if ((len -= copy) == 0) 2431 return elt; 2432 offset += copy; 2433 } 2434 start = end; 2435 } 2436 } 2437 BUG_ON(len); 2438 return elt; 2439} 2440 2441int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 2442{ 2443 int nsg = __skb_to_sgvec(skb, sg, offset, len); 2444 2445 sg_mark_end(&sg[nsg - 1]); 2446 2447 return nsg; 2448} 2449 2450/** 2451 * skb_cow_data - Check that a socket buffer's data buffers are writable 2452 * @skb: The socket buffer to check. 2453 * @tailbits: Amount of trailing space to be added 2454 * @trailer: Returned pointer to the skb where the @tailbits space begins 2455 * 2456 * Make sure that the data buffers attached to a socket buffer are 2457 * writable. If they are not, private copies are made of the data buffers 2458 * and the socket buffer is set to use these instead. 2459 * 2460 * If @tailbits is given, make sure that there is space to write @tailbits 2461 * bytes of data beyond current end of socket buffer. @trailer will be 2462 * set to point to the skb in which this space begins. 2463 * 2464 * The number of scatterlist elements required to completely map the 2465 * COW'd and extended socket buffer will be returned. 2466 */ 2467int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 2468{ 2469 int copyflag; 2470 int elt; 2471 struct sk_buff *skb1, **skb_p; 2472 2473 /* If skb is cloned or its head is paged, reallocate 2474 * head pulling out all the pages (pages are considered not writable 2475 * at the moment even if they are anonymous). 2476 */ 2477 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 2478 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 2479 return -ENOMEM; 2480 2481 /* Easy case. Most of packets will go this way. */ 2482 if (!skb_shinfo(skb)->frag_list) { 2483 /* A little of trouble, not enough of space for trailer. 2484 * This should not happen, when stack is tuned to generate 2485 * good frames. OK, on miss we reallocate and reserve even more 2486 * space, 128 bytes is fair. */ 2487 2488 if (skb_tailroom(skb) < tailbits && 2489 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 2490 return -ENOMEM; 2491 2492 /* Voila! */ 2493 *trailer = skb; 2494 return 1; 2495 } 2496 2497 /* Misery. We are in troubles, going to mincer fragments... */ 2498 2499 elt = 1; 2500 skb_p = &skb_shinfo(skb)->frag_list; 2501 copyflag = 0; 2502 2503 while ((skb1 = *skb_p) != NULL) { 2504 int ntail = 0; 2505 2506 /* The fragment is partially pulled by someone, 2507 * this can happen on input. Copy it and everything 2508 * after it. */ 2509 2510 if (skb_shared(skb1)) 2511 copyflag = 1; 2512 2513 /* If the skb is the last, worry about trailer. */ 2514 2515 if (skb1->next == NULL && tailbits) { 2516 if (skb_shinfo(skb1)->nr_frags || 2517 skb_shinfo(skb1)->frag_list || 2518 skb_tailroom(skb1) < tailbits) 2519 ntail = tailbits + 128; 2520 } 2521 2522 if (copyflag || 2523 skb_cloned(skb1) || 2524 ntail || 2525 skb_shinfo(skb1)->nr_frags || 2526 skb_shinfo(skb1)->frag_list) { 2527 struct sk_buff *skb2; 2528 2529 /* Fuck, we are miserable poor guys... */ 2530 if (ntail == 0) 2531 skb2 = skb_copy(skb1, GFP_ATOMIC); 2532 else 2533 skb2 = skb_copy_expand(skb1, 2534 skb_headroom(skb1), 2535 ntail, 2536 GFP_ATOMIC); 2537 if (unlikely(skb2 == NULL)) 2538 return -ENOMEM; 2539 2540 if (skb1->sk) 2541 skb_set_owner_w(skb2, skb1->sk); 2542 2543 /* Looking around. Are we still alive? 2544 * OK, link new skb, drop old one */ 2545 2546 skb2->next = skb1->next; 2547 *skb_p = skb2; 2548 kfree_skb(skb1); 2549 skb1 = skb2; 2550 } 2551 elt++; 2552 *trailer = skb1; 2553 skb_p = &skb1->next; 2554 } 2555 2556 return elt; 2557} 2558 2559/** 2560 * skb_partial_csum_set - set up and verify partial csum values for packet 2561 * @skb: the skb to set 2562 * @start: the number of bytes after skb->data to start checksumming. 2563 * @off: the offset from start to place the checksum. 2564 * 2565 * For untrusted partially-checksummed packets, we need to make sure the values 2566 * for skb->csum_start and skb->csum_offset are valid so we don't oops. 2567 * 2568 * This function checks and sets those values and skb->ip_summed: if this 2569 * returns false you should drop the packet. 2570 */ 2571bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 2572{ 2573 if (unlikely(start > skb->len - 2) || 2574 unlikely((int)start + off > skb->len - 2)) { 2575 if (net_ratelimit()) 2576 printk(KERN_WARNING 2577 "bad partial csum: csum=%u/%u len=%u\n", 2578 start, off, skb->len); 2579 return false; 2580 } 2581 skb->ip_summed = CHECKSUM_PARTIAL; 2582 skb->csum_start = skb_headroom(skb) + start; 2583 skb->csum_offset = off; 2584 return true; 2585} 2586 2587EXPORT_SYMBOL(___pskb_trim); 2588EXPORT_SYMBOL(__kfree_skb); 2589EXPORT_SYMBOL(kfree_skb); 2590EXPORT_SYMBOL(__pskb_pull_tail); 2591EXPORT_SYMBOL(__alloc_skb); 2592EXPORT_SYMBOL(__netdev_alloc_skb); 2593EXPORT_SYMBOL(pskb_copy); 2594EXPORT_SYMBOL(pskb_expand_head); 2595EXPORT_SYMBOL(skb_checksum); 2596EXPORT_SYMBOL(skb_clone); 2597EXPORT_SYMBOL(skb_copy); 2598EXPORT_SYMBOL(skb_copy_and_csum_bits); 2599EXPORT_SYMBOL(skb_copy_and_csum_dev); 2600EXPORT_SYMBOL(skb_copy_bits); 2601EXPORT_SYMBOL(skb_copy_expand); 2602EXPORT_SYMBOL(skb_over_panic); 2603EXPORT_SYMBOL(skb_pad); 2604EXPORT_SYMBOL(skb_realloc_headroom); 2605EXPORT_SYMBOL(skb_under_panic); 2606EXPORT_SYMBOL(skb_dequeue); 2607EXPORT_SYMBOL(skb_dequeue_tail); 2608EXPORT_SYMBOL(skb_insert); 2609EXPORT_SYMBOL(skb_queue_purge); 2610EXPORT_SYMBOL(skb_queue_head); 2611EXPORT_SYMBOL(skb_queue_tail); 2612EXPORT_SYMBOL(skb_unlink); 2613EXPORT_SYMBOL(skb_append); 2614EXPORT_SYMBOL(skb_split); 2615EXPORT_SYMBOL(skb_prepare_seq_read); 2616EXPORT_SYMBOL(skb_seq_read); 2617EXPORT_SYMBOL(skb_abort_seq_read); 2618EXPORT_SYMBOL(skb_find_text); 2619EXPORT_SYMBOL(skb_append_datato_frags); 2620 2621EXPORT_SYMBOL_GPL(skb_to_sgvec); 2622EXPORT_SYMBOL_GPL(skb_cow_data); 2623EXPORT_SYMBOL_GPL(skb_partial_csum_set);