1/* Modified by Broadcom Corp. Portions Copyright (c) Broadcom Corp, 2013. */ 2/* 3 * Routines having to do with the 'struct sk_buff' memory handlers. 4 * 5 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 6 * Florian La Roche <rzsfl@rz.uni-sb.de> 7 * 8 * Fixes: 9 * Alan Cox : Fixed the worst of the load 10 * balancer bugs. 11 * Dave Platt : Interrupt stacking fix. 12 * Richard Kooijman : Timestamp fixes. 13 * Alan Cox : Changed buffer format. 14 * Alan Cox : destructor hook for AF_UNIX etc. 15 * Linus Torvalds : Better skb_clone. 16 * Alan Cox : Added skb_copy. 17 * Alan Cox : Added all the changed routines Linus 18 * only put in the headers 19 * Ray VanTassle : Fixed --skb->lock in free 20 * Alan Cox : skb_copy copy arp field 21 * Andi Kleen : slabified it. 22 * Robert Olsson : Removed skb_head_pool 23 * 24 * NOTE: 25 * The __skb_ routines should be called with interrupts 26 * disabled, or you better be *real* sure that the operation is atomic 27 * with respect to whatever list is being frobbed (e.g. via lock_sock() 28 * or via disabling bottom half handlers, etc). 29 * 30 * This program is free software; you can redistribute it and/or 31 * modify it under the terms of the GNU General Public License 32 * as published by the Free Software Foundation; either version 33 * 2 of the License, or (at your option) any later version. 34 */ 35 36/* 37 * The functions in this file will not compile correctly with gcc 2.4.x 38 */ 39 40#include <linux/module.h> 41#include <linux/types.h> 42#include <linux/kernel.h> 43#include <linux/kmemcheck.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#include <linux/errqueue.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#include <trace/events/skb.h> 71 72#include "kmap_skb.h" 73 74#include <typedefs.h> 75#include <bcmdefs.h> 76#include <osl.h> 77 78/* TCP header skb pool */ 79typedef struct tcph_pool { 80 struct sk_buff *head; 81 uint32 obj_size, max_obj, curr_obj; 82 spinlock_t lock; 83} tcph_pool_t; 84 85static tcph_pool_t *tcph_pool; 86 87static inline struct sk_buff *skb_tcph_pool_alloc(tcph_pool_t *tcph_pool, uint len); 88static inline void skb_tcph_pool_free(tcph_pool_t *tcph_pool, struct sk_buff *skb); 89 90static struct kmem_cache *skbuff_head_cache __read_mostly; 91static struct kmem_cache *skbuff_fclone_cache __read_mostly; 92 93static void sock_pipe_buf_release(struct pipe_inode_info *pipe, 94 struct pipe_buffer *buf) 95{ 96 put_page(buf->page); 97} 98 99static void sock_pipe_buf_get(struct pipe_inode_info *pipe, 100 struct pipe_buffer *buf) 101{ 102 get_page(buf->page); 103} 104 105static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, 106 struct pipe_buffer *buf) 107{ 108 return 1; 109} 110 111 112/* Pipe buffer operations for a socket. */ 113static const struct pipe_buf_operations sock_pipe_buf_ops = { 114 .can_merge = 0, 115 .map = generic_pipe_buf_map, 116 .unmap = generic_pipe_buf_unmap, 117 .confirm = generic_pipe_buf_confirm, 118 .release = sock_pipe_buf_release, 119 .steal = sock_pipe_buf_steal, 120 .get = sock_pipe_buf_get, 121}; 122 123/* 124 * Keep out-of-line to prevent kernel bloat. 125 * __builtin_return_address is not used because it is not always 126 * reliable. 127 */ 128 129/** 130 * skb_over_panic - private function 131 * @skb: buffer 132 * @sz: size 133 * @here: address 134 * 135 * Out of line support code for skb_put(). Not user callable. 136 */ 137static void skb_over_panic(struct sk_buff *skb, int sz, void *here) 138{ 139 printk(KERN_EMERG "skb_over_panic: text:%p len:%d put:%d head:%p " 140 "data:%p tail:%#lx end:%#lx dev:%s\n", 141 here, skb->len, sz, skb->head, skb->data, 142 (unsigned long)skb->tail, (unsigned long)skb->end, 143 skb->dev ? skb->dev->name : "<NULL>"); 144 BUG(); 145} 146 147/** 148 * skb_under_panic - private function 149 * @skb: buffer 150 * @sz: size 151 * @here: address 152 * 153 * Out of line support code for skb_push(). Not user callable. 154 */ 155 156static void skb_under_panic(struct sk_buff *skb, int sz, void *here) 157{ 158 printk(KERN_EMERG "skb_under_panic: text:%p len:%d put:%d head:%p " 159 "data:%p tail:%#lx end:%#lx dev:%s\n", 160 here, skb->len, sz, skb->head, skb->data, 161 (unsigned long)skb->tail, (unsigned long)skb->end, 162 skb->dev ? skb->dev->name : "<NULL>"); 163 BUG(); 164} 165 166/* Allocate a new skbuff. We do this ourselves so we can fill in a few 167 * 'private' fields and also do memory statistics to find all the 168 * [BEEP] leaks. 169 * 170 */ 171 172/** 173 * __alloc_skb - allocate a network buffer 174 * @size: size to allocate 175 * @gfp_mask: allocation mask 176 * @fclone: allocate from fclone cache instead of head cache 177 * and allocate a cloned (child) skb 178 * @node: numa node to allocate memory on 179 * 180 * Allocate a new &sk_buff. The returned buffer has no headroom and a 181 * tail room of size bytes. The object has a reference count of one. 182 * The return is the buffer. On a failure the return is %NULL. 183 * 184 * Buffers may only be allocated from interrupts using a @gfp_mask of 185 * %GFP_ATOMIC. 186 */ 187struct sk_buff * BCMFASTPATH_HOST __alloc_skb(unsigned int size, gfp_t gfp_mask, 188 int fclone, int node) 189{ 190 struct kmem_cache *cache; 191 struct skb_shared_info *shinfo; 192 struct sk_buff *skb; 193 u8 *data; 194 195 cache = fclone ? skbuff_fclone_cache : skbuff_head_cache; 196 197 /* Get the HEAD */ 198 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 199 if (!skb) 200 goto out; 201 prefetchw(skb); 202 203 size = SKB_DATA_ALIGN(size); 204 data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), 205 gfp_mask, node); 206 if (!data) 207 goto nodata; 208 prefetchw(data + size); 209 210 /* 211 * Only clear those fields we need to clear, not those that we will 212 * actually initialise below. Hence, don't put any more fields after 213 * the tail pointer in struct sk_buff! 214 */ 215 memset(skb, 0, offsetof(struct sk_buff, tail)); 216 217 /* Clear the members that were shifted to end of struct */ 218 memset(((u8 *)&skb->users)+sizeof(atomic_t), 0, 219 (sizeof(struct sk_buff) - (sizeof(atomic_t) + offsetof(struct sk_buff, users)))); 220 221 skb->truesize = size + sizeof(struct sk_buff); 222 atomic_set(&skb->users, 1); 223 skb->head = data; 224 skb->data = data; 225 226#ifdef BCMDBG_CTRACE 227 INIT_LIST_HEAD(&skb->ctrace_list); 228 skb->func[0] = (char *)__FUNCTION__; 229 skb->line[0] = __LINE__; 230 skb->ctrace_start = 0; 231 skb->ctrace_count = 1; 232#endif /* BCMDBG_CTRACE */ 233 234 skb_reset_tail_pointer(skb); 235 skb->end = skb->tail + size; 236 kmemcheck_annotate_bitfield(skb, flags1); 237 kmemcheck_annotate_bitfield(skb, flags2); 238#ifdef NET_SKBUFF_DATA_USES_OFFSET 239 skb->mac_header = ~0U; 240#endif 241#ifdef BCMFA 242 skb->napt_idx = BCM_FA_INVALID_IDX_VAL; 243 skb->napt_flags = 0; 244#endif 245 246 /* make sure we initialize shinfo sequentially */ 247 shinfo = skb_shinfo(skb); 248 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 249 atomic_set(&shinfo->dataref, 1); 250 251 if (fclone) { 252 struct sk_buff *child = skb + 1; 253 atomic_t *fclone_ref = (atomic_t *) (child + 1); 254 255 kmemcheck_annotate_bitfield(child, flags1); 256 kmemcheck_annotate_bitfield(child, flags2); 257 skb->fclone = SKB_FCLONE_ORIG; 258 atomic_set(fclone_ref, 1); 259 260 child->fclone = SKB_FCLONE_UNAVAILABLE; 261 } 262out: 263 return skb; 264nodata: 265 kmem_cache_free(cache, skb); 266 skb = NULL; 267 goto out; 268} 269EXPORT_SYMBOL(__alloc_skb); 270 271/** 272 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 273 * @dev: network device to receive on 274 * @length: length to allocate 275 * @gfp_mask: get_free_pages mask, passed to alloc_skb 276 * 277 * Allocate a new &sk_buff and assign it a usage count of one. The 278 * buffer has unspecified headroom built in. Users should allocate 279 * the headroom they think they need without accounting for the 280 * built in space. The built in space is used for optimisations. 281 * 282 * %NULL is returned if there is no free memory. 283 */ 284struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 285 unsigned int length, gfp_t gfp_mask) 286{ 287 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; 288 struct sk_buff *skb; 289 290 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 0, node); 291 if (likely(skb)) { 292 skb_reserve(skb, NET_SKB_PAD); 293 skb->dev = dev; 294 } 295 return skb; 296} 297EXPORT_SYMBOL(__netdev_alloc_skb); 298 299struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask) 300{ 301 int node = dev->dev.parent ? dev_to_node(dev->dev.parent) : -1; 302 struct page *page; 303 304 page = alloc_pages_node(node, gfp_mask, 0); 305 return page; 306} 307EXPORT_SYMBOL(__netdev_alloc_page); 308 309void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 310 int size) 311{ 312 skb_fill_page_desc(skb, i, page, off, size); 313 skb->len += size; 314 skb->data_len += size; 315 skb->truesize += size; 316} 317EXPORT_SYMBOL(skb_add_rx_frag); 318 319/** 320 * dev_alloc_skb - allocate an skbuff for receiving 321 * @length: length to allocate 322 * 323 * Allocate a new &sk_buff and assign it a usage count of one. The 324 * buffer has unspecified headroom built in. Users should allocate 325 * the headroom they think they need without accounting for the 326 * built in space. The built in space is used for optimisations. 327 * 328 * %NULL is returned if there is no free memory. Although this function 329 * allocates memory it can be called from an interrupt. 330 */ 331struct sk_buff *dev_alloc_skb(unsigned int length) 332{ 333 /* 334 * There is more code here than it seems: 335 * __dev_alloc_skb is an inline 336 */ 337 return __dev_alloc_skb(length, GFP_ATOMIC); 338} 339EXPORT_SYMBOL(dev_alloc_skb); 340 341static void skb_drop_list(struct sk_buff **listp) 342{ 343 struct sk_buff *list = *listp; 344 345 *listp = NULL; 346 347 do { 348 struct sk_buff *this = list; 349 list = list->next; 350 kfree_skb(this); 351 } while (list); 352} 353 354static inline void skb_drop_fraglist(struct sk_buff *skb) 355{ 356 skb_drop_list(&skb_shinfo(skb)->frag_list); 357} 358 359static void skb_clone_fraglist(struct sk_buff *skb) 360{ 361 struct sk_buff *list; 362 363 skb_walk_frags(skb, list) 364 skb_get(list); 365} 366 367static void BCMFASTPATH_HOST skb_release_data(struct sk_buff *skb) 368{ 369 if (!skb->cloned || 370 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 371 &skb_shinfo(skb)->dataref)) { 372 if (skb_shinfo(skb)->nr_frags) { 373 int i; 374 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 375 put_page(skb_shinfo(skb)->frags[i].page); 376 } 377 378 if (skb_has_frags(skb)) 379 skb_drop_fraglist(skb); 380 381 kfree(skb->head); 382 } 383} 384 385/* 386 * Free an skbuff by memory without cleaning the state. 387 */ 388static void kfree_skbmem(struct sk_buff *skb) 389{ 390 struct sk_buff *other; 391 atomic_t *fclone_ref; 392 393 switch (skb->fclone) { 394 case SKB_FCLONE_UNAVAILABLE: 395 kmem_cache_free(skbuff_head_cache, skb); 396 break; 397 398 case SKB_FCLONE_ORIG: 399 fclone_ref = (atomic_t *) (skb + 2); 400 if (atomic_dec_and_test(fclone_ref)) 401 kmem_cache_free(skbuff_fclone_cache, skb); 402 break; 403 404 case SKB_FCLONE_CLONE: 405 fclone_ref = (atomic_t *) (skb + 1); 406 other = skb - 1; 407 408 /* The clone portion is available for 409 * fast-cloning again. 410 */ 411 skb->fclone = SKB_FCLONE_UNAVAILABLE; 412 413 if (atomic_dec_and_test(fclone_ref)) 414 kmem_cache_free(skbuff_fclone_cache, other); 415 break; 416 } 417} 418 419static void BCMFASTPATH_HOST skb_release_head_state(struct sk_buff *skb) 420{ 421 skb_dst_drop(skb); 422#ifdef CONFIG_XFRM 423 secpath_put(skb->sp); 424#endif 425 if (skb->destructor) { 426 WARN_ON(in_irq()); 427 skb->destructor(skb); 428 } 429#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) 430 nf_conntrack_put(skb->nfct); 431 nf_conntrack_put_reasm(skb->nfct_reasm); 432#endif 433#ifdef CONFIG_BRIDGE_NETFILTER 434 nf_bridge_put(skb->nf_bridge); 435#endif 436#ifdef CONFIG_NET_SCHED 437 skb->tc_index = 0; 438#ifdef CONFIG_NET_CLS_ACT 439 skb->tc_verd = 0; 440#endif 441#endif 442} 443 444/* Free everything but the sk_buff shell. */ 445static void skb_release_all(struct sk_buff *skb) 446{ 447 skb_release_head_state(skb); 448 skb_release_data(skb); 449} 450 451/** 452 * __kfree_skb - private function 453 * @skb: buffer 454 * 455 * Free an sk_buff. Release anything attached to the buffer. 456 * Clean the state. This is an internal helper function. Users should 457 * always call kfree_skb 458 */ 459 460void BCMFASTPATH_HOST __kfree_skb(struct sk_buff *skb) 461{ 462 if (skb->tcpf_hdrbuf) { 463 skb_tcph_pool_free(tcph_pool, skb); 464 return; 465 } 466 skb_release_all(skb); 467 kfree_skbmem(skb); 468} 469EXPORT_SYMBOL(__kfree_skb); 470 471/** 472 * kfree_skb - free an sk_buff 473 * @skb: buffer to free 474 * 475 * Drop a reference to the buffer and free it if the usage count has 476 * hit zero. 477 */ 478void kfree_skb(struct sk_buff *skb) 479{ 480 if (unlikely(!skb)) 481 return; 482 if (likely(atomic_read(&skb->users) == 1)) 483 smp_rmb(); 484 else if (likely(!atomic_dec_and_test(&skb->users))) 485 return; 486 trace_kfree_skb(skb, __builtin_return_address(0)); 487 __kfree_skb(skb); 488} 489EXPORT_SYMBOL(kfree_skb); 490 491/** 492 * consume_skb - free an skbuff 493 * @skb: buffer to free 494 * 495 * Drop a ref to the buffer and free it if the usage count has hit zero 496 * Functions identically to kfree_skb, but kfree_skb assumes that the frame 497 * is being dropped after a failure and notes that 498 */ 499void consume_skb(struct sk_buff *skb) 500{ 501 if (unlikely(!skb)) 502 return; 503 if (likely(atomic_read(&skb->users) == 1)) 504 smp_rmb(); 505 else if (likely(!atomic_dec_and_test(&skb->users))) 506 return; 507 __kfree_skb(skb); 508} 509EXPORT_SYMBOL(consume_skb); 510 511/** 512 * skb_recycle_check - check if skb can be reused for receive 513 * @skb: buffer 514 * @skb_size: minimum receive buffer size 515 * 516 * Checks that the skb passed in is not shared or cloned, and 517 * that it is linear and its head portion at least as large as 518 * skb_size so that it can be recycled as a receive buffer. 519 * If these conditions are met, this function does any necessary 520 * reference count dropping and cleans up the skbuff as if it 521 * just came from __alloc_skb(). 522 */ 523bool skb_recycle_check(struct sk_buff *skb, int skb_size) 524{ 525 struct skb_shared_info *shinfo; 526 527 if (irqs_disabled()) 528 return false; 529 530 if (skb_is_nonlinear(skb) || skb->fclone != SKB_FCLONE_UNAVAILABLE) 531 return false; 532 533 skb_size = SKB_DATA_ALIGN(skb_size + NET_SKB_PAD); 534 if (skb_end_pointer(skb) - skb->head < skb_size) 535 return false; 536 537 if (skb_shared(skb) || skb_cloned(skb)) 538 return false; 539 540 skb_release_head_state(skb); 541 542 shinfo = skb_shinfo(skb); 543 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 544 atomic_set(&shinfo->dataref, 1); 545 546 memset(skb, 0, offsetof(struct sk_buff, tail)); 547 548 /* Clear the members that were shifted to end of struct */ 549 memset(((u8 *)&skb->users)+sizeof(atomic_t), 0, 550 (sizeof(struct sk_buff) - (sizeof(atomic_t) + offsetof(struct sk_buff, users)))); 551 552 skb->data = skb->head + NET_SKB_PAD; 553 skb_reset_tail_pointer(skb); 554 555 return true; 556} 557EXPORT_SYMBOL(skb_recycle_check); 558 559static void BCMFASTPATH_HOST __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 560{ 561#ifdef PKTC 562 memset(new->pktc_cb, 0, sizeof(new->pktc_cb)); 563#endif 564#ifdef CTF_PPPOE 565 memset(new->ctf_pppoe_cb, 0, sizeof(new->ctf_pppoe_cb)); 566#endif 567 new->tstamp = old->tstamp; 568 new->dev = old->dev; 569 new->transport_header = old->transport_header; 570 new->network_header = old->network_header; 571 new->mac_header = old->mac_header; 572 skb_dst_copy(new, old); 573 new->rxhash = old->rxhash; 574#ifdef CONFIG_XFRM 575#if defined(HNDCTF) && defined(CTFMAP) 576 if (PKTISCTF(NULL, old)) 577 new->sp = NULL; 578 else 579#endif 580 new->sp = secpath_get(old->sp); 581#endif 582 memcpy(new->cb, old->cb, sizeof(old->cb)); 583 new->csum = old->csum; 584 new->local_df = old->local_df; 585 new->pkt_type = old->pkt_type; 586 new->ip_summed = old->ip_summed; 587 skb_copy_queue_mapping(new, old); 588 new->priority = old->priority; 589 new->deliver_no_wcard = old->deliver_no_wcard; 590#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE) 591 new->ipvs_property = old->ipvs_property; 592#endif 593 new->protocol = old->protocol; 594 new->mark = old->mark; 595 new->skb_iif = old->skb_iif; 596 __nf_copy(new, old); 597#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \ 598 defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE) 599 new->nf_trace = old->nf_trace; 600#endif 601#ifdef CONFIG_NET_SCHED 602 new->tc_index = old->tc_index; 603#ifdef CONFIG_NET_CLS_ACT 604 new->tc_verd = old->tc_verd; 605#endif 606#endif 607 new->vlan_tci = old->vlan_tci; 608#if defined(HNDCTF) || defined(CTFPOOL) 609 new->pktc_flags = old->pktc_flags; 610#endif 611#ifdef CTFPOOL 612 new->ctfpool = NULL; 613#endif 614 new->tcpf_hdrbuf = 0; 615 new->tcpf_smb = old->tcpf_smb; 616 617#ifdef BCMDBG_CTRACE 618 INIT_LIST_HEAD(&new->ctrace_list); 619 new->func[0] = (char *)__FUNCTION__; 620 new->line[0] = __LINE__; 621 new->ctrace_start = 0; 622 new->ctrace_count = 1; 623#endif /* BCMDBG_CTRACE */ 624 625#ifdef BCMFA 626 new->napt_idx = BCM_FA_INVALID_IDX_VAL; 627 new->napt_flags = 0; 628#endif 629 630 skb_copy_secmark(new, old); 631} 632 633/* 634 * You should not add any new code to this function. Add it to 635 * __copy_skb_header above instead. 636 */ 637static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 638{ 639#define C(x) n->x = skb->x 640 641 n->next = n->prev = NULL; 642 n->sk = NULL; 643 __copy_skb_header(n, skb); 644 645 C(len); 646 C(data_len); 647 C(mac_len); 648 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 649 n->cloned = 1; 650 n->nohdr = 0; 651 n->destructor = NULL; 652 C(tail); 653 C(end); 654 C(head); 655 C(data); 656 C(truesize); 657 atomic_set(&n->users, 1); 658 659 atomic_inc(&(skb_shinfo(skb)->dataref)); 660 skb->cloned = 1; 661 662 return n; 663#undef C 664} 665 666/** 667 * skb_morph - morph one skb into another 668 * @dst: the skb to receive the contents 669 * @src: the skb to supply the contents 670 * 671 * This is identical to skb_clone except that the target skb is 672 * supplied by the user. 673 * 674 * The target skb is returned upon exit. 675 */ 676struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 677{ 678 skb_release_all(dst); 679 return __skb_clone(dst, src); 680} 681EXPORT_SYMBOL_GPL(skb_morph); 682 683/** 684 * skb_clone - duplicate an sk_buff 685 * @skb: buffer to clone 686 * @gfp_mask: allocation priority 687 * 688 * Duplicate an &sk_buff. The new one is not owned by a socket. Both 689 * copies share the same packet data but not structure. The new 690 * buffer has a reference count of 1. If the allocation fails the 691 * function returns %NULL otherwise the new buffer is returned. 692 * 693 * If this function is called from an interrupt gfp_mask() must be 694 * %GFP_ATOMIC. 695 */ 696 697struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 698{ 699 struct sk_buff *n; 700 701 n = skb + 1; 702 if (skb->fclone == SKB_FCLONE_ORIG && 703 n->fclone == SKB_FCLONE_UNAVAILABLE) { 704 atomic_t *fclone_ref = (atomic_t *) (n + 1); 705 n->fclone = SKB_FCLONE_CLONE; 706 atomic_inc(fclone_ref); 707 } else { 708 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 709 if (!n) 710 return NULL; 711 712 kmemcheck_annotate_bitfield(n, flags1); 713 kmemcheck_annotate_bitfield(n, flags2); 714 n->fclone = SKB_FCLONE_UNAVAILABLE; 715 } 716 717 return __skb_clone(n, skb); 718} 719EXPORT_SYMBOL(skb_clone); 720 721static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 722{ 723#ifndef NET_SKBUFF_DATA_USES_OFFSET 724 /* 725 * Shift between the two data areas in bytes 726 */ 727 unsigned long offset = new->data - old->data; 728#endif 729 730 __copy_skb_header(new, old); 731 732#ifndef NET_SKBUFF_DATA_USES_OFFSET 733 /* {transport,network,mac}_header are relative to skb->head */ 734 new->transport_header += offset; 735 new->network_header += offset; 736 if (skb_mac_header_was_set(new)) 737 new->mac_header += offset; 738#endif 739 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 740 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 741 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 742} 743 744/** 745 * skb_copy - create private copy of an sk_buff 746 * @skb: buffer to copy 747 * @gfp_mask: allocation priority 748 * 749 * Make a copy of both an &sk_buff and its data. This is used when the 750 * caller wishes to modify the data and needs a private copy of the 751 * data to alter. Returns %NULL on failure or the pointer to the buffer 752 * on success. The returned buffer has a reference count of 1. 753 * 754 * As by-product this function converts non-linear &sk_buff to linear 755 * one, so that &sk_buff becomes completely private and caller is allowed 756 * to modify all the data of returned buffer. This means that this 757 * function is not recommended for use in circumstances when only 758 * header is going to be modified. Use pskb_copy() instead. 759 */ 760 761struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 762{ 763 int headerlen = skb->data - skb->head; 764 /* 765 * Allocate the copy buffer 766 */ 767 struct sk_buff *n; 768#ifdef NET_SKBUFF_DATA_USES_OFFSET 769 n = alloc_skb(skb->end + skb->data_len, gfp_mask); 770#else 771 n = alloc_skb(skb->end - skb->head + skb->data_len, gfp_mask); 772#endif 773 if (!n) 774 return NULL; 775 776 /* Set the data pointer */ 777 skb_reserve(n, headerlen); 778 /* Set the tail pointer and length */ 779 skb_put(n, skb->len); 780 781 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 782 BUG(); 783 784 copy_skb_header(n, skb); 785 return n; 786} 787EXPORT_SYMBOL(skb_copy); 788 789/** 790 * pskb_copy - create copy of an sk_buff with private head. 791 * @skb: buffer to copy 792 * @gfp_mask: allocation priority 793 * 794 * Make a copy of both an &sk_buff and part of its data, located 795 * in header. Fragmented data remain shared. This is used when 796 * the caller wishes to modify only header of &sk_buff and needs 797 * private copy of the header to alter. Returns %NULL on failure 798 * or the pointer to the buffer on success. 799 * The returned buffer has a reference count of 1. 800 */ 801 802struct sk_buff *pskb_copy(struct sk_buff *skb, gfp_t gfp_mask) 803{ 804 /* 805 * Allocate the copy buffer 806 */ 807 struct sk_buff *n; 808#ifdef NET_SKBUFF_DATA_USES_OFFSET 809 n = alloc_skb(skb->end, gfp_mask); 810#else 811 n = alloc_skb(skb->end - skb->head, gfp_mask); 812#endif 813 if (!n) 814 goto out; 815 816 /* Set the data pointer */ 817 skb_reserve(n, skb->data - skb->head); 818 /* Set the tail pointer and length */ 819 skb_put(n, skb_headlen(skb)); 820 /* Copy the bytes */ 821 skb_copy_from_linear_data(skb, n->data, n->len); 822 823 n->truesize += skb->data_len; 824 n->data_len = skb->data_len; 825 n->len = skb->len; 826 827 if (skb_shinfo(skb)->nr_frags) { 828 int i; 829 830 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 831 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 832 get_page(skb_shinfo(n)->frags[i].page); 833 } 834 skb_shinfo(n)->nr_frags = i; 835 } 836 837 if (skb_has_frags(skb)) { 838 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 839 skb_clone_fraglist(n); 840 } 841 842 copy_skb_header(n, skb); 843out: 844 return n; 845} 846EXPORT_SYMBOL(pskb_copy); 847 848/** 849 * pskb_expand_head - reallocate header of &sk_buff 850 * @skb: buffer to reallocate 851 * @nhead: room to add at head 852 * @ntail: room to add at tail 853 * @gfp_mask: allocation priority 854 * 855 * Expands (or creates identical copy, if &nhead and &ntail are zero) 856 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 857 * reference count of 1. Returns zero in the case of success or error, 858 * if expansion failed. In the last case, &sk_buff is not changed. 859 * 860 * All the pointers pointing into skb header may change and must be 861 * reloaded after call to this function. 862 */ 863 864int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 865 gfp_t gfp_mask) 866{ 867 int i; 868 u8 *data; 869#ifdef NET_SKBUFF_DATA_USES_OFFSET 870 int size = nhead + skb->end + ntail; 871#else 872 int size = nhead + (skb->end - skb->head) + ntail; 873#endif 874 long off; 875 876 BUG_ON(nhead < 0); 877 878 if (skb_shared(skb)) 879 BUG(); 880 881 size = SKB_DATA_ALIGN(size); 882 883 data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask); 884 if (!data) 885 goto nodata; 886 887 /* Copy only real data... and, alas, header. This should be 888 * optimized for the cases when header is void. */ 889#ifdef NET_SKBUFF_DATA_USES_OFFSET 890 memcpy(data + nhead, skb->head, skb->tail); 891#else 892 memcpy(data + nhead, skb->head, skb->tail - skb->head); 893#endif 894 memcpy(data + size, skb_end_pointer(skb), 895 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 896 897 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 898 get_page(skb_shinfo(skb)->frags[i].page); 899 900 if (skb_has_frags(skb)) 901 skb_clone_fraglist(skb); 902 903 skb_release_data(skb); 904 905 off = (data + nhead) - skb->head; 906 907 skb->head = data; 908 skb->data += off; 909#ifdef NET_SKBUFF_DATA_USES_OFFSET 910 skb->end = size; 911 off = nhead; 912#else 913 skb->end = skb->head + size; 914#endif 915 /* {transport,network,mac}_header and tail are relative to skb->head */ 916 skb->tail += off; 917 skb->transport_header += off; 918 skb->network_header += off; 919 if (skb_mac_header_was_set(skb)) 920 skb->mac_header += off; 921 /* Only adjust this if it actually is csum_start rather than csum */ 922 if (skb->ip_summed == CHECKSUM_PARTIAL) 923 skb->csum_start += nhead; 924 skb->cloned = 0; 925 skb->hdr_len = 0; 926 skb->nohdr = 0; 927 atomic_set(&skb_shinfo(skb)->dataref, 1); 928 return 0; 929 930nodata: 931 return -ENOMEM; 932} 933EXPORT_SYMBOL(pskb_expand_head); 934 935/* Make private copy of skb with writable head and some headroom */ 936 937struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 938{ 939 struct sk_buff *skb2; 940 int delta = headroom - skb_headroom(skb); 941 942 if (delta <= 0) 943 skb2 = pskb_copy(skb, GFP_ATOMIC); 944 else { 945 skb2 = skb_clone(skb, GFP_ATOMIC); 946 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 947 GFP_ATOMIC)) { 948 kfree_skb(skb2); 949 skb2 = NULL; 950 } 951 } 952 return skb2; 953} 954EXPORT_SYMBOL(skb_realloc_headroom); 955 956/** 957 * skb_copy_expand - copy and expand sk_buff 958 * @skb: buffer to copy 959 * @newheadroom: new free bytes at head 960 * @newtailroom: new free bytes at tail 961 * @gfp_mask: allocation priority 962 * 963 * Make a copy of both an &sk_buff and its data and while doing so 964 * allocate additional space. 965 * 966 * This is used when the caller wishes to modify the data and needs a 967 * private copy of the data to alter as well as more space for new fields. 968 * Returns %NULL on failure or the pointer to the buffer 969 * on success. The returned buffer has a reference count of 1. 970 * 971 * You must pass %GFP_ATOMIC as the allocation priority if this function 972 * is called from an interrupt. 973 */ 974struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 975 int newheadroom, int newtailroom, 976 gfp_t gfp_mask) 977{ 978 /* 979 * Allocate the copy buffer 980 */ 981 struct sk_buff *n = alloc_skb(newheadroom + skb->len + newtailroom, 982 gfp_mask); 983 int oldheadroom = skb_headroom(skb); 984 int head_copy_len, head_copy_off; 985 int off; 986 987 if (!n) 988 return NULL; 989 990 skb_reserve(n, newheadroom); 991 992 /* Set the tail pointer and length */ 993 skb_put(n, skb->len); 994 995 head_copy_len = oldheadroom; 996 head_copy_off = 0; 997 if (newheadroom <= head_copy_len) 998 head_copy_len = newheadroom; 999 else 1000 head_copy_off = newheadroom - head_copy_len; 1001 1002 /* Copy the linear header and data. */ 1003 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 1004 skb->len + head_copy_len)) 1005 BUG(); 1006 1007 copy_skb_header(n, skb); 1008 1009 off = newheadroom - oldheadroom; 1010 if (n->ip_summed == CHECKSUM_PARTIAL) 1011 n->csum_start += off; 1012#ifdef NET_SKBUFF_DATA_USES_OFFSET 1013 n->transport_header += off; 1014 n->network_header += off; 1015 if (skb_mac_header_was_set(skb)) 1016 n->mac_header += off; 1017#endif 1018 1019 return n; 1020} 1021EXPORT_SYMBOL(skb_copy_expand); 1022 1023/** 1024 * skb_pad - zero pad the tail of an skb 1025 * @skb: buffer to pad 1026 * @pad: space to pad 1027 * 1028 * Ensure that a buffer is followed by a padding area that is zero 1029 * filled. Used by network drivers which may DMA or transfer data 1030 * beyond the buffer end onto the wire. 1031 * 1032 * May return error in out of memory cases. The skb is freed on error. 1033 */ 1034 1035int skb_pad(struct sk_buff *skb, int pad) 1036{ 1037 int err; 1038 int ntail; 1039 1040 /* If the skbuff is non linear tailroom is always zero.. */ 1041 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 1042 memset(skb->data+skb->len, 0, pad); 1043 return 0; 1044 } 1045 1046 ntail = skb->data_len + pad - (skb->end - skb->tail); 1047 if (likely(skb_cloned(skb) || ntail > 0)) { 1048 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 1049 if (unlikely(err)) 1050 goto free_skb; 1051 } 1052 1053 err = skb_linearize(skb); 1054 if (unlikely(err)) 1055 goto free_skb; 1056 1057 memset(skb->data + skb->len, 0, pad); 1058 return 0; 1059 1060free_skb: 1061 kfree_skb(skb); 1062 return err; 1063} 1064EXPORT_SYMBOL(skb_pad); 1065 1066/** 1067 * skb_put - add data to a buffer 1068 * @skb: buffer to use 1069 * @len: amount of data to add 1070 * 1071 * This function extends the used data area of the buffer. If this would 1072 * exceed the total buffer size the kernel will panic. A pointer to the 1073 * first byte of the extra data is returned. 1074 */ 1075unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 1076{ 1077 unsigned char *tmp = skb_tail_pointer(skb); 1078 SKB_LINEAR_ASSERT(skb); 1079 skb->tail += len; 1080 skb->len += len; 1081 if (unlikely(skb->tail > skb->end)) 1082 skb_over_panic(skb, len, __builtin_return_address(0)); 1083 return tmp; 1084} 1085EXPORT_SYMBOL(skb_put); 1086 1087/** 1088 * skb_push - add data to the start of a buffer 1089 * @skb: buffer to use 1090 * @len: amount of data to add 1091 * 1092 * This function extends the used data area of the buffer at the buffer 1093 * start. If this would exceed the total buffer headroom the kernel will 1094 * panic. A pointer to the first byte of the extra data is returned. 1095 */ 1096unsigned char BCMFASTPATH *skb_push(struct sk_buff *skb, unsigned int len) 1097{ 1098 skb->data -= len; 1099 skb->len += len; 1100 if (unlikely(skb->data<skb->head)) 1101 skb_under_panic(skb, len, __builtin_return_address(0)); 1102 return skb->data; 1103} 1104EXPORT_SYMBOL(skb_push); 1105 1106/** 1107 * skb_pull - remove data from the start of a buffer 1108 * @skb: buffer to use 1109 * @len: amount of data to remove 1110 * 1111 * This function removes data from the start of a buffer, returning 1112 * the memory to the headroom. A pointer to the next data in the buffer 1113 * is returned. Once the data has been pulled future pushes will overwrite 1114 * the old data. 1115 */ 1116unsigned char BCMFASTPATH *skb_pull(struct sk_buff *skb, unsigned int len) 1117{ 1118 return skb_pull_inline(skb, len); 1119} 1120EXPORT_SYMBOL(skb_pull); 1121 1122/** 1123 * skb_trim - remove end from a buffer 1124 * @skb: buffer to alter 1125 * @len: new length 1126 * 1127 * Cut the length of a buffer down by removing data from the tail. If 1128 * the buffer is already under the length specified it is not modified. 1129 * The skb must be linear. 1130 */ 1131void skb_trim(struct sk_buff *skb, unsigned int len) 1132{ 1133 if (skb->len > len) 1134 __skb_trim(skb, len); 1135} 1136EXPORT_SYMBOL(skb_trim); 1137 1138/* Trims skb to length len. It can change skb pointers. 1139 */ 1140 1141int ___pskb_trim(struct sk_buff *skb, unsigned int len) 1142{ 1143 struct sk_buff **fragp; 1144 struct sk_buff *frag; 1145 int offset = skb_headlen(skb); 1146 int nfrags = skb_shinfo(skb)->nr_frags; 1147 int i; 1148 int err; 1149 1150 if (skb_cloned(skb) && 1151 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 1152 return err; 1153 1154 i = 0; 1155 if (offset >= len) 1156 goto drop_pages; 1157 1158 for (; i < nfrags; i++) { 1159 int end = offset + skb_shinfo(skb)->frags[i].size; 1160 1161 if (end < len) { 1162 offset = end; 1163 continue; 1164 } 1165 1166 skb_shinfo(skb)->frags[i++].size = len - offset; 1167 1168drop_pages: 1169 skb_shinfo(skb)->nr_frags = i; 1170 1171 for (; i < nfrags; i++) 1172 put_page(skb_shinfo(skb)->frags[i].page); 1173 1174 if (skb_has_frags(skb)) 1175 skb_drop_fraglist(skb); 1176 goto done; 1177 } 1178 1179 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 1180 fragp = &frag->next) { 1181 int end = offset + frag->len; 1182 1183 if (skb_shared(frag)) { 1184 struct sk_buff *nfrag; 1185 1186 nfrag = skb_clone(frag, GFP_ATOMIC); 1187 if (unlikely(!nfrag)) 1188 return -ENOMEM; 1189 1190 nfrag->next = frag->next; 1191 kfree_skb(frag); 1192 frag = nfrag; 1193 *fragp = frag; 1194 } 1195 1196 if (end < len) { 1197 offset = end; 1198 continue; 1199 } 1200 1201 if (end > len && 1202 unlikely((err = pskb_trim(frag, len - offset)))) 1203 return err; 1204 1205 if (frag->next) 1206 skb_drop_list(&frag->next); 1207 break; 1208 } 1209 1210done: 1211 if (len > skb_headlen(skb)) { 1212 skb->data_len -= skb->len - len; 1213 skb->len = len; 1214 } else { 1215 skb->len = len; 1216 skb->data_len = 0; 1217 skb_set_tail_pointer(skb, len); 1218 } 1219 1220 return 0; 1221} 1222EXPORT_SYMBOL(___pskb_trim); 1223 1224/** 1225 * __pskb_pull_tail - advance tail of skb header 1226 * @skb: buffer to reallocate 1227 * @delta: number of bytes to advance tail 1228 * 1229 * The function makes a sense only on a fragmented &sk_buff, 1230 * it expands header moving its tail forward and copying necessary 1231 * data from fragmented part. 1232 * 1233 * &sk_buff MUST have reference count of 1. 1234 * 1235 * Returns %NULL (and &sk_buff does not change) if pull failed 1236 * or value of new tail of skb in the case of success. 1237 * 1238 * All the pointers pointing into skb header may change and must be 1239 * reloaded after call to this function. 1240 */ 1241 1242/* Moves tail of skb head forward, copying data from fragmented part, 1243 * when it is necessary. 1244 * 1. It may fail due to malloc failure. 1245 * 2. It may change skb pointers. 1246 * 1247 * It is pretty complicated. Luckily, it is called only in exceptional cases. 1248 */ 1249unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 1250{ 1251 /* If skb has not enough free space at tail, get new one 1252 * plus 128 bytes for future expansions. If we have enough 1253 * room at tail, reallocate without expansion only if skb is cloned. 1254 */ 1255 int i, k, eat = (skb->tail + delta) - skb->end; 1256 1257 if (eat > 0 || skb_cloned(skb)) { 1258 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 1259 GFP_ATOMIC)) 1260 return NULL; 1261 } 1262 1263 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 1264 BUG(); 1265 1266 /* Optimization: no fragments, no reasons to preestimate 1267 * size of pulled pages. Superb. 1268 */ 1269 if (!skb_has_frags(skb)) 1270 goto pull_pages; 1271 1272 /* Estimate size of pulled pages. */ 1273 eat = delta; 1274 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1275 if (skb_shinfo(skb)->frags[i].size >= eat) 1276 goto pull_pages; 1277 eat -= skb_shinfo(skb)->frags[i].size; 1278 } 1279 1280 /* If we need update frag list, we are in troubles. 1281 * Certainly, it possible to add an offset to skb data, 1282 * but taking into account that pulling is expected to 1283 * be very rare operation, it is worth to fight against 1284 * further bloating skb head and crucify ourselves here instead. 1285 * Pure masohism, indeed. 8)8) 1286 */ 1287 if (eat) { 1288 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1289 struct sk_buff *clone = NULL; 1290 struct sk_buff *insp = NULL; 1291 1292 do { 1293 BUG_ON(!list); 1294 1295 if (list->len <= eat) { 1296 /* Eaten as whole. */ 1297 eat -= list->len; 1298 list = list->next; 1299 insp = list; 1300 } else { 1301 /* Eaten partially. */ 1302 1303 if (skb_shared(list)) { 1304 /* Sucks! We need to fork list. :-( */ 1305 clone = skb_clone(list, GFP_ATOMIC); 1306 if (!clone) 1307 return NULL; 1308 insp = list->next; 1309 list = clone; 1310 } else { 1311 /* This may be pulled without 1312 * problems. */ 1313 insp = list; 1314 } 1315 if (!pskb_pull(list, eat)) { 1316 kfree_skb(clone); 1317 return NULL; 1318 } 1319 break; 1320 } 1321 } while (eat); 1322 1323 /* Free pulled out fragments. */ 1324 while ((list = skb_shinfo(skb)->frag_list) != insp) { 1325 skb_shinfo(skb)->frag_list = list->next; 1326 kfree_skb(list); 1327 } 1328 /* And insert new clone at head. */ 1329 if (clone) { 1330 clone->next = list; 1331 skb_shinfo(skb)->frag_list = clone; 1332 } 1333 } 1334 /* Success! Now we may commit changes to skb data. */ 1335 1336pull_pages: 1337 eat = delta; 1338 k = 0; 1339 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1340 if (skb_shinfo(skb)->frags[i].size <= eat) { 1341 put_page(skb_shinfo(skb)->frags[i].page); 1342 eat -= skb_shinfo(skb)->frags[i].size; 1343 } else { 1344 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 1345 if (eat) { 1346 skb_shinfo(skb)->frags[k].page_offset += eat; 1347 skb_shinfo(skb)->frags[k].size -= eat; 1348 eat = 0; 1349 } 1350 k++; 1351 } 1352 } 1353 skb_shinfo(skb)->nr_frags = k; 1354 1355 skb->tail += delta; 1356 skb->data_len -= delta; 1357 1358 return skb_tail_pointer(skb); 1359} 1360EXPORT_SYMBOL(__pskb_pull_tail); 1361 1362/* Copy some data bits from skb to kernel buffer. */ 1363 1364int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 1365{ 1366 int start = skb_headlen(skb); 1367 struct sk_buff *frag_iter; 1368 int i, copy; 1369 1370 if (offset > (int)skb->len - len) 1371 goto fault; 1372 1373 /* Copy header. */ 1374 if ((copy = start - offset) > 0) { 1375 if (copy > len) 1376 copy = len; 1377 skb_copy_from_linear_data_offset(skb, offset, to, copy); 1378 if ((len -= copy) == 0) 1379 return 0; 1380 offset += copy; 1381 to += copy; 1382 } 1383 1384 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1385 int end; 1386 1387 WARN_ON(start > offset + len); 1388 1389 end = start + skb_shinfo(skb)->frags[i].size; 1390 if ((copy = end - offset) > 0) { 1391 u8 *vaddr; 1392 1393 if (copy > len) 1394 copy = len; 1395 1396 vaddr = kmap_skb_frag(&skb_shinfo(skb)->frags[i]); 1397 memcpy(to, 1398 vaddr + skb_shinfo(skb)->frags[i].page_offset+ 1399 offset - start, copy); 1400 kunmap_skb_frag(vaddr); 1401 1402 if ((len -= copy) == 0) 1403 return 0; 1404 offset += copy; 1405 to += copy; 1406 } 1407 start = end; 1408 } 1409 1410 skb_walk_frags(skb, frag_iter) { 1411 int end; 1412 1413 WARN_ON(start > offset + len); 1414 1415 end = start + frag_iter->len; 1416 if ((copy = end - offset) > 0) { 1417 if (copy > len) 1418 copy = len; 1419 if (skb_copy_bits(frag_iter, offset - start, to, copy)) 1420 goto fault; 1421 if ((len -= copy) == 0) 1422 return 0; 1423 offset += copy; 1424 to += copy; 1425 } 1426 start = end; 1427 } 1428 if (!len) 1429 return 0; 1430 1431fault: 1432 return -EFAULT; 1433} 1434EXPORT_SYMBOL(skb_copy_bits); 1435 1436/* 1437 * Callback from splice_to_pipe(), if we need to release some pages 1438 * at the end of the spd in case we error'ed out in filling the pipe. 1439 */ 1440static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 1441{ 1442 put_page(spd->pages[i]); 1443} 1444 1445static inline struct page *linear_to_page(struct page *page, unsigned int *len, 1446 unsigned int *offset, 1447 struct sk_buff *skb, struct sock *sk) 1448{ 1449 struct page *p = sk->sk_sndmsg_page; 1450 unsigned int off; 1451 1452 if (!p) { 1453new_page: 1454 p = sk->sk_sndmsg_page = alloc_pages(sk->sk_allocation, 0); 1455 if (!p) 1456 return NULL; 1457 1458 off = sk->sk_sndmsg_off = 0; 1459 /* hold one ref to this page until it's full */ 1460 } else { 1461 unsigned int mlen; 1462 1463 off = sk->sk_sndmsg_off; 1464 mlen = PAGE_SIZE - off; 1465 if (mlen < 64 && mlen < *len) { 1466 put_page(p); 1467 goto new_page; 1468 } 1469 1470 *len = min_t(unsigned int, *len, mlen); 1471 } 1472 1473 memcpy(page_address(p) + off, page_address(page) + *offset, *len); 1474 sk->sk_sndmsg_off += *len; 1475 *offset = off; 1476 get_page(p); 1477 1478 return p; 1479} 1480 1481/* 1482 * Fill page/offset/length into spd, if it can hold more pages. 1483 */ 1484static inline int spd_fill_page(struct splice_pipe_desc *spd, 1485 struct pipe_inode_info *pipe, struct page *page, 1486 unsigned int *len, unsigned int offset, 1487 struct sk_buff *skb, int linear, 1488 struct sock *sk) 1489{ 1490 if (unlikely(spd->nr_pages == pipe->buffers)) 1491 return 1; 1492 1493 if (linear) { 1494 page = linear_to_page(page, len, &offset, skb, sk); 1495 if (!page) 1496 return 1; 1497 } else 1498 get_page(page); 1499 1500 spd->pages[spd->nr_pages] = page; 1501 spd->partial[spd->nr_pages].len = *len; 1502 spd->partial[spd->nr_pages].offset = offset; 1503 spd->nr_pages++; 1504 1505 return 0; 1506} 1507 1508static inline void __segment_seek(struct page **page, unsigned int *poff, 1509 unsigned int *plen, unsigned int off) 1510{ 1511 unsigned long n; 1512 1513 *poff += off; 1514 n = *poff / PAGE_SIZE; 1515 if (n) 1516 *page = nth_page(*page, n); 1517 1518 *poff = *poff % PAGE_SIZE; 1519 *plen -= off; 1520} 1521 1522static inline int __splice_segment(struct page *page, unsigned int poff, 1523 unsigned int plen, unsigned int *off, 1524 unsigned int *len, struct sk_buff *skb, 1525 struct splice_pipe_desc *spd, int linear, 1526 struct sock *sk, 1527 struct pipe_inode_info *pipe) 1528{ 1529 if (!*len) 1530 return 1; 1531 1532 /* skip this segment if already processed */ 1533 if (*off >= plen) { 1534 *off -= plen; 1535 return 0; 1536 } 1537 1538 /* ignore any bits we already processed */ 1539 if (*off) { 1540 __segment_seek(&page, &poff, &plen, *off); 1541 *off = 0; 1542 } 1543 1544 do { 1545 unsigned int flen = min(*len, plen); 1546 1547 /* the linear region may spread across several pages */ 1548 flen = min_t(unsigned int, flen, PAGE_SIZE - poff); 1549 1550 if (spd_fill_page(spd, pipe, page, &flen, poff, skb, linear, sk)) 1551 return 1; 1552 1553 __segment_seek(&page, &poff, &plen, flen); 1554 *len -= flen; 1555 1556 } while (*len && plen); 1557 1558 return 0; 1559} 1560 1561/* 1562 * Map linear and fragment data from the skb to spd. It reports failure if the 1563 * pipe is full or if we already spliced the requested length. 1564 */ 1565static int __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 1566 unsigned int *offset, unsigned int *len, 1567 struct splice_pipe_desc *spd, struct sock *sk) 1568{ 1569 int seg; 1570 1571 /* 1572 * map the linear part 1573 */ 1574 if (__splice_segment(virt_to_page(skb->data), 1575 (unsigned long) skb->data & (PAGE_SIZE - 1), 1576 skb_headlen(skb), 1577 offset, len, skb, spd, 1, sk, pipe)) 1578 return 1; 1579 1580 /* 1581 * then map the fragments 1582 */ 1583 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 1584 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 1585 1586 if (__splice_segment(f->page, f->page_offset, f->size, 1587 offset, len, skb, spd, 0, sk, pipe)) 1588 return 1; 1589 } 1590 1591 return 0; 1592} 1593 1594/* 1595 * Map data from the skb to a pipe. Should handle both the linear part, 1596 * the fragments, and the frag list. It does NOT handle frag lists within 1597 * the frag list, if such a thing exists. We'd probably need to recurse to 1598 * handle that cleanly. 1599 */ 1600int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 1601 struct pipe_inode_info *pipe, unsigned int tlen, 1602 unsigned int flags) 1603{ 1604 struct partial_page partial[PIPE_DEF_BUFFERS]; 1605 struct page *pages[PIPE_DEF_BUFFERS]; 1606 struct splice_pipe_desc spd = { 1607 .pages = pages, 1608 .partial = partial, 1609 .flags = flags, 1610 .ops = &sock_pipe_buf_ops, 1611 .spd_release = sock_spd_release, 1612 }; 1613 struct sk_buff *frag_iter; 1614 struct sock *sk = skb->sk; 1615 int ret = 0; 1616 1617 if (splice_grow_spd(pipe, &spd)) 1618 return -ENOMEM; 1619 1620 /* 1621 * __skb_splice_bits() only fails if the output has no room left, 1622 * so no point in going over the frag_list for the error case. 1623 */ 1624 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 1625 goto done; 1626 else if (!tlen) 1627 goto done; 1628 1629 /* 1630 * now see if we have a frag_list to map 1631 */ 1632 skb_walk_frags(skb, frag_iter) { 1633 if (!tlen) 1634 break; 1635 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1636 break; 1637 } 1638 1639done: 1640 if (spd.nr_pages) { 1641 /* 1642 * Drop the socket lock, otherwise we have reverse 1643 * locking dependencies between sk_lock and i_mutex 1644 * here as compared to sendfile(). We enter here 1645 * with the socket lock held, and splice_to_pipe() will 1646 * grab the pipe inode lock. For sendfile() emulation, 1647 * we call into ->sendpage() with the i_mutex lock held 1648 * and networking will grab the socket lock. 1649 */ 1650 release_sock(sk); 1651 ret = splice_to_pipe(pipe, &spd); 1652 lock_sock(sk); 1653 } 1654 1655 splice_shrink_spd(pipe, &spd); 1656 return ret; 1657} 1658 1659/** 1660 * skb_store_bits - store bits from kernel buffer to skb 1661 * @skb: destination buffer 1662 * @offset: offset in destination 1663 * @from: source buffer 1664 * @len: number of bytes to copy 1665 * 1666 * Copy the specified number of bytes from the source buffer to the 1667 * destination skb. This function handles all the messy bits of 1668 * traversing fragment lists and such. 1669 */ 1670 1671int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1672{ 1673 int start = skb_headlen(skb); 1674 struct sk_buff *frag_iter; 1675 int i, copy; 1676 1677 if (offset > (int)skb->len - len) 1678 goto fault; 1679 1680 if ((copy = start - offset) > 0) { 1681 if (copy > len) 1682 copy = len; 1683 skb_copy_to_linear_data_offset(skb, offset, from, copy); 1684 if ((len -= copy) == 0) 1685 return 0; 1686 offset += copy; 1687 from += copy; 1688 } 1689 1690 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1691 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1692 int end; 1693 1694 WARN_ON(start > offset + len); 1695 1696 end = start + frag->size; 1697 if ((copy = end - offset) > 0) { 1698 u8 *vaddr; 1699 1700 if (copy > len) 1701 copy = len; 1702 1703 vaddr = kmap_skb_frag(frag); 1704 memcpy(vaddr + frag->page_offset + offset - start, 1705 from, copy); 1706 kunmap_skb_frag(vaddr); 1707 1708 if ((len -= copy) == 0) 1709 return 0; 1710 offset += copy; 1711 from += copy; 1712 } 1713 start = end; 1714 } 1715 1716 skb_walk_frags(skb, frag_iter) { 1717 int end; 1718 1719 WARN_ON(start > offset + len); 1720 1721 end = start + frag_iter->len; 1722 if ((copy = end - offset) > 0) { 1723 if (copy > len) 1724 copy = len; 1725 if (skb_store_bits(frag_iter, offset - start, 1726 from, copy)) 1727 goto fault; 1728 if ((len -= copy) == 0) 1729 return 0; 1730 offset += copy; 1731 from += copy; 1732 } 1733 start = end; 1734 } 1735 if (!len) 1736 return 0; 1737 1738fault: 1739 return -EFAULT; 1740} 1741EXPORT_SYMBOL(skb_store_bits); 1742 1743static inline void 1744pref_range(void *pref_ptr, void *pref_end) 1745{ 1746 do { 1747 prefetch(pref_ptr); 1748 pref_ptr += L1_CACHE_BYTES; 1749 } while (pref_ptr < pref_end); 1750} 1751 1752/* Checksum skb data. */ 1753 1754__wsum BCMFASTPATH_HOST skb_checksum(const struct sk_buff *skb, int offset, 1755 int len, __wsum csum) 1756{ 1757 int start = skb_headlen(skb); 1758 int i, copy = start - offset; 1759 struct sk_buff *frag_iter; 1760 int pos = 0; 1761 1762 /* Checksum header. */ 1763 if (copy > 0) { 1764 if (copy > len) 1765 copy = len; 1766 pref_range(skb->data + offset, skb->data + offset + copy - L1_CACHE_BYTES); 1767 csum = csum_partial(skb->data + offset, copy, csum); 1768 if ((len -= copy) == 0) 1769 return csum; 1770 offset += copy; 1771 pos = copy; 1772 } 1773 1774 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1775 int end; 1776 1777 WARN_ON(start > offset + len); 1778 1779 end = start + skb_shinfo(skb)->frags[i].size; 1780 if ((copy = end - offset) > 0) { 1781 __wsum csum2; 1782 u8 *vaddr; 1783 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1784 1785 if (copy > len) 1786 copy = len; 1787 vaddr = kmap_skb_frag(frag); 1788 pref_range(vaddr + frag->page_offset + offset - start, 1789 vaddr + frag->page_offset + offset - start + copy - L1_CACHE_BYTES); 1790 csum2 = csum_partial(vaddr + frag->page_offset + 1791 offset - start, copy, 0); 1792 kunmap_skb_frag(vaddr); 1793 csum = csum_block_add(csum, csum2, pos); 1794 if (!(len -= copy)) 1795 return csum; 1796 offset += copy; 1797 pos += copy; 1798 } 1799 start = end; 1800 } 1801 1802 skb_walk_frags(skb, frag_iter) { 1803 int end; 1804 1805 WARN_ON(start > offset + len); 1806 1807 end = start + frag_iter->len; 1808 if ((copy = end - offset) > 0) { 1809 __wsum csum2; 1810 if (copy > len) 1811 copy = len; 1812 csum2 = skb_checksum(frag_iter, offset - start, 1813 copy, 0); 1814 csum = csum_block_add(csum, csum2, pos); 1815 if ((len -= copy) == 0) 1816 return csum; 1817 offset += copy; 1818 pos += copy; 1819 } 1820 start = end; 1821 } 1822 BUG_ON(len); 1823 1824 return csum; 1825} 1826EXPORT_SYMBOL(skb_checksum); 1827 1828/* Both of above in one bottle. */ 1829 1830__wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 1831 u8 *to, int len, __wsum csum) 1832{ 1833 int start = skb_headlen(skb); 1834 int i, copy = start - offset; 1835 struct sk_buff *frag_iter; 1836 int pos = 0; 1837 1838 /* Copy header. */ 1839 if (copy > 0) { 1840 if (copy > len) 1841 copy = len; 1842 csum = csum_partial_copy_nocheck(skb->data + offset, to, 1843 copy, csum); 1844 if ((len -= copy) == 0) 1845 return csum; 1846 offset += copy; 1847 to += copy; 1848 pos = copy; 1849 } 1850 1851 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1852 int end; 1853 1854 WARN_ON(start > offset + len); 1855 1856 end = start + skb_shinfo(skb)->frags[i].size; 1857 if ((copy = end - offset) > 0) { 1858 __wsum csum2; 1859 u8 *vaddr; 1860 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1861 1862 if (copy > len) 1863 copy = len; 1864 vaddr = kmap_skb_frag(frag); 1865 csum2 = csum_partial_copy_nocheck(vaddr + 1866 frag->page_offset + 1867 offset - start, to, 1868 copy, 0); 1869 kunmap_skb_frag(vaddr); 1870 csum = csum_block_add(csum, csum2, pos); 1871 if (!(len -= copy)) 1872 return csum; 1873 offset += copy; 1874 to += copy; 1875 pos += copy; 1876 } 1877 start = end; 1878 } 1879 1880 skb_walk_frags(skb, frag_iter) { 1881 __wsum csum2; 1882 int end; 1883 1884 WARN_ON(start > offset + len); 1885 1886 end = start + frag_iter->len; 1887 if ((copy = end - offset) > 0) { 1888 if (copy > len) 1889 copy = len; 1890 csum2 = skb_copy_and_csum_bits(frag_iter, 1891 offset - start, 1892 to, copy, 0); 1893 csum = csum_block_add(csum, csum2, pos); 1894 if ((len -= copy) == 0) 1895 return csum; 1896 offset += copy; 1897 to += copy; 1898 pos += copy; 1899 } 1900 start = end; 1901 } 1902 BUG_ON(len); 1903 return csum; 1904} 1905EXPORT_SYMBOL(skb_copy_and_csum_bits); 1906 1907void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 1908{ 1909 __wsum csum; 1910 long csstart; 1911 1912 if (skb->ip_summed == CHECKSUM_PARTIAL) 1913 csstart = skb->csum_start - skb_headroom(skb); 1914 else 1915 csstart = skb_headlen(skb); 1916 1917 BUG_ON(csstart > skb_headlen(skb)); 1918 1919 skb_copy_from_linear_data(skb, to, csstart); 1920 1921 csum = 0; 1922 if (csstart != skb->len) 1923 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 1924 skb->len - csstart, 0); 1925 1926 if (skb->ip_summed == CHECKSUM_PARTIAL) { 1927 long csstuff = csstart + skb->csum_offset; 1928 1929 *((__sum16 *)(to + csstuff)) = csum_fold(csum); 1930 } 1931} 1932EXPORT_SYMBOL(skb_copy_and_csum_dev); 1933 1934/** 1935 * skb_dequeue - remove from the head of the queue 1936 * @list: list to dequeue from 1937 * 1938 * Remove the head of the list. The list lock is taken so the function 1939 * may be used safely with other locking list functions. The head item is 1940 * returned or %NULL if the list is empty. 1941 */ 1942 1943struct sk_buff *skb_dequeue(struct sk_buff_head *list) 1944{ 1945 unsigned long flags; 1946 struct sk_buff *result; 1947 1948 spin_lock_irqsave(&list->lock, flags); 1949 result = __skb_dequeue(list); 1950 spin_unlock_irqrestore(&list->lock, flags); 1951 return result; 1952} 1953EXPORT_SYMBOL(skb_dequeue); 1954 1955/** 1956 * skb_dequeue_tail - remove from the tail of the queue 1957 * @list: list to dequeue from 1958 * 1959 * Remove the tail of the list. The list lock is taken so the function 1960 * may be used safely with other locking list functions. The tail item is 1961 * returned or %NULL if the list is empty. 1962 */ 1963struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 1964{ 1965 unsigned long flags; 1966 struct sk_buff *result; 1967 1968 spin_lock_irqsave(&list->lock, flags); 1969 result = __skb_dequeue_tail(list); 1970 spin_unlock_irqrestore(&list->lock, flags); 1971 return result; 1972} 1973EXPORT_SYMBOL(skb_dequeue_tail); 1974 1975/** 1976 * skb_queue_purge - empty a list 1977 * @list: list to empty 1978 * 1979 * Delete all buffers on an &sk_buff list. Each buffer is removed from 1980 * the list and one reference dropped. This function takes the list 1981 * lock and is atomic with respect to other list locking functions. 1982 */ 1983void skb_queue_purge(struct sk_buff_head *list) 1984{ 1985 struct sk_buff *skb; 1986 while ((skb = skb_dequeue(list)) != NULL) 1987 kfree_skb(skb); 1988} 1989EXPORT_SYMBOL(skb_queue_purge); 1990 1991/** 1992 * skb_queue_head - queue a buffer at the list head 1993 * @list: list to use 1994 * @newsk: buffer to queue 1995 * 1996 * Queue a buffer at the start of the list. This function takes the 1997 * list lock and can be used safely with other locking &sk_buff functions 1998 * safely. 1999 * 2000 * A buffer cannot be placed on two lists at the same time. 2001 */ 2002void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 2003{ 2004 unsigned long flags; 2005 2006 spin_lock_irqsave(&list->lock, flags); 2007 __skb_queue_head(list, newsk); 2008 spin_unlock_irqrestore(&list->lock, flags); 2009} 2010EXPORT_SYMBOL(skb_queue_head); 2011 2012/** 2013 * skb_queue_tail - queue a buffer at the list tail 2014 * @list: list to use 2015 * @newsk: buffer to queue 2016 * 2017 * Queue a buffer at the tail of the list. This function takes the 2018 * list lock and can be used safely with other locking &sk_buff functions 2019 * safely. 2020 * 2021 * A buffer cannot be placed on two lists at the same time. 2022 */ 2023void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 2024{ 2025 unsigned long flags; 2026 2027 spin_lock_irqsave(&list->lock, flags); 2028 __skb_queue_tail(list, newsk); 2029 spin_unlock_irqrestore(&list->lock, flags); 2030} 2031EXPORT_SYMBOL(skb_queue_tail); 2032 2033/** 2034 * skb_unlink - remove a buffer from a list 2035 * @skb: buffer to remove 2036 * @list: list to use 2037 * 2038 * Remove a packet from a list. The list locks are taken and this 2039 * function is atomic with respect to other list locked calls 2040 * 2041 * You must know what list the SKB is on. 2042 */ 2043void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 2044{ 2045 unsigned long flags; 2046 2047 spin_lock_irqsave(&list->lock, flags); 2048 __skb_unlink(skb, list); 2049 spin_unlock_irqrestore(&list->lock, flags); 2050} 2051EXPORT_SYMBOL(skb_unlink); 2052 2053/** 2054 * skb_append - append a buffer 2055 * @old: buffer to insert after 2056 * @newsk: buffer to insert 2057 * @list: list to use 2058 * 2059 * Place a packet after a given packet in a list. The list locks are taken 2060 * and this function is atomic with respect to other list locked calls. 2061 * A buffer cannot be placed on two lists at the same time. 2062 */ 2063void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 2064{ 2065 unsigned long flags; 2066 2067 spin_lock_irqsave(&list->lock, flags); 2068 __skb_queue_after(list, old, newsk); 2069 spin_unlock_irqrestore(&list->lock, flags); 2070} 2071EXPORT_SYMBOL(skb_append); 2072 2073/** 2074 * skb_insert - insert a buffer 2075 * @old: buffer to insert before 2076 * @newsk: buffer to insert 2077 * @list: list to use 2078 * 2079 * Place a packet before a given packet in a list. The list locks are 2080 * taken and this function is atomic with respect to other list locked 2081 * calls. 2082 * 2083 * A buffer cannot be placed on two lists at the same time. 2084 */ 2085void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 2086{ 2087 unsigned long flags; 2088 2089 spin_lock_irqsave(&list->lock, flags); 2090 __skb_insert(newsk, old->prev, old, list); 2091 spin_unlock_irqrestore(&list->lock, flags); 2092} 2093EXPORT_SYMBOL(skb_insert); 2094 2095static inline void skb_split_inside_header(struct sk_buff *skb, 2096 struct sk_buff* skb1, 2097 const u32 len, const int pos) 2098{ 2099 int i; 2100 2101 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2102 pos - len); 2103 /* And move data appendix as is. */ 2104 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2105 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 2106 2107 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 2108 skb_shinfo(skb)->nr_frags = 0; 2109 skb1->data_len = skb->data_len; 2110 skb1->len += skb1->data_len; 2111 skb->data_len = 0; 2112 skb->len = len; 2113 skb_set_tail_pointer(skb, len); 2114} 2115 2116static inline void skb_split_no_header(struct sk_buff *skb, 2117 struct sk_buff* skb1, 2118 const u32 len, int pos) 2119{ 2120 int i, k = 0; 2121 const int nfrags = skb_shinfo(skb)->nr_frags; 2122 2123 skb_shinfo(skb)->nr_frags = 0; 2124 skb1->len = skb1->data_len = skb->len - len; 2125 skb->len = len; 2126 skb->data_len = len - pos; 2127 2128 for (i = 0; i < nfrags; i++) { 2129 int size = skb_shinfo(skb)->frags[i].size; 2130 2131 if (pos + size > len) { 2132 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 2133 2134 if (pos < len) { 2135 /* Split frag. 2136 * We have two variants in this case: 2137 * 1. Move all the frag to the second 2138 * part, if it is possible. F.e. 2139 * this approach is mandatory for TUX, 2140 * where splitting is expensive. 2141 * 2. Split is accurately. We make this. 2142 */ 2143 get_page(skb_shinfo(skb)->frags[i].page); 2144 skb_shinfo(skb1)->frags[0].page_offset += len - pos; 2145 skb_shinfo(skb1)->frags[0].size -= len - pos; 2146 skb_shinfo(skb)->frags[i].size = len - pos; 2147 skb_shinfo(skb)->nr_frags++; 2148 } 2149 k++; 2150 } else 2151 skb_shinfo(skb)->nr_frags++; 2152 pos += size; 2153 } 2154 skb_shinfo(skb1)->nr_frags = k; 2155} 2156 2157/** 2158 * skb_split - Split fragmented skb to two parts at length len. 2159 * @skb: the buffer to split 2160 * @skb1: the buffer to receive the second part 2161 * @len: new length for skb 2162 */ 2163void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 2164{ 2165 int pos = skb_headlen(skb); 2166 2167 if (len < pos) /* Split line is inside header. */ 2168 skb_split_inside_header(skb, skb1, len, pos); 2169 else /* Second chunk has no header, nothing to copy. */ 2170 skb_split_no_header(skb, skb1, len, pos); 2171} 2172EXPORT_SYMBOL(skb_split); 2173 2174/* Shifting from/to a cloned skb is a no-go. 2175 * 2176 * Caller cannot keep skb_shinfo related pointers past calling here! 2177 */ 2178static int skb_prepare_for_shift(struct sk_buff *skb) 2179{ 2180 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2181} 2182 2183/** 2184 * skb_shift - Shifts paged data partially from skb to another 2185 * @tgt: buffer into which tail data gets added 2186 * @skb: buffer from which the paged data comes from 2187 * @shiftlen: shift up to this many bytes 2188 * 2189 * Attempts to shift up to shiftlen worth of bytes, which may be less than 2190 * the length of the skb, from tgt to skb. Returns number bytes shifted. 2191 * It's up to caller to free skb if everything was shifted. 2192 * 2193 * If @tgt runs out of frags, the whole operation is aborted. 2194 * 2195 * Skb cannot include anything else but paged data while tgt is allowed 2196 * to have non-paged data as well. 2197 * 2198 * TODO: full sized shift could be optimized but that would need 2199 * specialized skb free'er to handle frags without up-to-date nr_frags. 2200 */ 2201int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2202{ 2203 int from, to, merge, todo; 2204 struct skb_frag_struct *fragfrom, *fragto; 2205 2206 BUG_ON(shiftlen > skb->len); 2207 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2208 2209 todo = shiftlen; 2210 from = 0; 2211 to = skb_shinfo(tgt)->nr_frags; 2212 fragfrom = &skb_shinfo(skb)->frags[from]; 2213 2214 /* Actual merge is delayed until the point when we know we can 2215 * commit all, so that we don't have to undo partial changes 2216 */ 2217 if (!to || 2218 !skb_can_coalesce(tgt, to, fragfrom->page, fragfrom->page_offset)) { 2219 merge = -1; 2220 } else { 2221 merge = to - 1; 2222 2223 todo -= fragfrom->size; 2224 if (todo < 0) { 2225 if (skb_prepare_for_shift(skb) || 2226 skb_prepare_for_shift(tgt)) 2227 return 0; 2228 2229 /* All previous frag pointers might be stale! */ 2230 fragfrom = &skb_shinfo(skb)->frags[from]; 2231 fragto = &skb_shinfo(tgt)->frags[merge]; 2232 2233 fragto->size += shiftlen; 2234 fragfrom->size -= shiftlen; 2235 fragfrom->page_offset += shiftlen; 2236 2237 goto onlymerged; 2238 } 2239 2240 from++; 2241 } 2242 2243 /* Skip full, not-fitting skb to avoid expensive operations */ 2244 if ((shiftlen == skb->len) && 2245 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2246 return 0; 2247 2248 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2249 return 0; 2250 2251 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2252 if (to == MAX_SKB_FRAGS) 2253 return 0; 2254 2255 fragfrom = &skb_shinfo(skb)->frags[from]; 2256 fragto = &skb_shinfo(tgt)->frags[to]; 2257 2258 if (todo >= fragfrom->size) { 2259 *fragto = *fragfrom; 2260 todo -= fragfrom->size; 2261 from++; 2262 to++; 2263 2264 } else { 2265 get_page(fragfrom->page); 2266 fragto->page = fragfrom->page; 2267 fragto->page_offset = fragfrom->page_offset; 2268 fragto->size = todo; 2269 2270 fragfrom->page_offset += todo; 2271 fragfrom->size -= todo; 2272 todo = 0; 2273 2274 to++; 2275 break; 2276 } 2277 } 2278 2279 /* Ready to "commit" this state change to tgt */ 2280 skb_shinfo(tgt)->nr_frags = to; 2281 2282 if (merge >= 0) { 2283 fragfrom = &skb_shinfo(skb)->frags[0]; 2284 fragto = &skb_shinfo(tgt)->frags[merge]; 2285 2286 fragto->size += fragfrom->size; 2287 put_page(fragfrom->page); 2288 } 2289 2290 /* Reposition in the original skb */ 2291 to = 0; 2292 while (from < skb_shinfo(skb)->nr_frags) 2293 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2294 skb_shinfo(skb)->nr_frags = to; 2295 2296 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2297 2298onlymerged: 2299 /* Most likely the tgt won't ever need its checksum anymore, skb on 2300 * the other hand might need it if it needs to be resent 2301 */ 2302 tgt->ip_summed = CHECKSUM_PARTIAL; 2303 skb->ip_summed = CHECKSUM_PARTIAL; 2304 2305 /* Yak, is it really working this way? Some helper please? */ 2306 skb->len -= shiftlen; 2307 skb->data_len -= shiftlen; 2308 skb->truesize -= shiftlen; 2309 tgt->len += shiftlen; 2310 tgt->data_len += shiftlen; 2311 tgt->truesize += shiftlen; 2312 2313 return shiftlen; 2314} 2315 2316/** 2317 * skb_prepare_seq_read - Prepare a sequential read of skb data 2318 * @skb: the buffer to read 2319 * @from: lower offset of data to be read 2320 * @to: upper offset of data to be read 2321 * @st: state variable 2322 * 2323 * Initializes the specified state variable. Must be called before 2324 * invoking skb_seq_read() for the first time. 2325 */ 2326void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2327 unsigned int to, struct skb_seq_state *st) 2328{ 2329 st->lower_offset = from; 2330 st->upper_offset = to; 2331 st->root_skb = st->cur_skb = skb; 2332 st->frag_idx = st->stepped_offset = 0; 2333 st->frag_data = NULL; 2334} 2335EXPORT_SYMBOL(skb_prepare_seq_read); 2336 2337/** 2338 * skb_seq_read - Sequentially read skb data 2339 * @consumed: number of bytes consumed by the caller so far 2340 * @data: destination pointer for data to be returned 2341 * @st: state variable 2342 * 2343 * Reads a block of skb data at &consumed relative to the 2344 * lower offset specified to skb_prepare_seq_read(). Assigns 2345 * the head of the data block to &data and returns the length 2346 * of the block or 0 if the end of the skb data or the upper 2347 * offset has been reached. 2348 * 2349 * The caller is not required to consume all of the data 2350 * returned, i.e. &consumed is typically set to the number 2351 * of bytes already consumed and the next call to 2352 * skb_seq_read() will return the remaining part of the block. 2353 * 2354 * Note 1: The size of each block of data returned can be arbitary, 2355 * this limitation is the cost for zerocopy seqeuental 2356 * reads of potentially non linear data. 2357 * 2358 * Note 2: Fragment lists within fragments are not implemented 2359 * at the moment, state->root_skb could be replaced with 2360 * a stack for this purpose. 2361 */ 2362unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2363 struct skb_seq_state *st) 2364{ 2365 unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2366 skb_frag_t *frag; 2367 2368 if (unlikely(abs_offset >= st->upper_offset)) 2369 return 0; 2370 2371next_skb: 2372 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2373 2374 if (abs_offset < block_limit && !st->frag_data) { 2375 *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2376 return block_limit - abs_offset; 2377 } 2378 2379 if (st->frag_idx == 0 && !st->frag_data) 2380 st->stepped_offset += skb_headlen(st->cur_skb); 2381 2382 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2383 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 2384 block_limit = frag->size + st->stepped_offset; 2385 2386 if (abs_offset < block_limit) { 2387 if (!st->frag_data) 2388 st->frag_data = kmap_skb_frag(frag); 2389 2390 *data = (u8 *) st->frag_data + frag->page_offset + 2391 (abs_offset - st->stepped_offset); 2392 2393 return block_limit - abs_offset; 2394 } 2395 2396 if (st->frag_data) { 2397 kunmap_skb_frag(st->frag_data); 2398 st->frag_data = NULL; 2399 } 2400 2401 st->frag_idx++; 2402 st->stepped_offset += frag->size; 2403 } 2404 2405 if (st->frag_data) { 2406 kunmap_skb_frag(st->frag_data); 2407 st->frag_data = NULL; 2408 } 2409 2410 if (st->root_skb == st->cur_skb && skb_has_frags(st->root_skb)) { 2411 st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 2412 st->frag_idx = 0; 2413 goto next_skb; 2414 } else if (st->cur_skb->next) { 2415 st->cur_skb = st->cur_skb->next; 2416 st->frag_idx = 0; 2417 goto next_skb; 2418 } 2419 2420 return 0; 2421} 2422EXPORT_SYMBOL(skb_seq_read); 2423 2424/** 2425 * skb_abort_seq_read - Abort a sequential read of skb data 2426 * @st: state variable 2427 * 2428 * Must be called if skb_seq_read() was not called until it 2429 * returned 0. 2430 */ 2431void skb_abort_seq_read(struct skb_seq_state *st) 2432{ 2433 if (st->frag_data) 2434 kunmap_skb_frag(st->frag_data); 2435} 2436EXPORT_SYMBOL(skb_abort_seq_read); 2437 2438#define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 2439 2440static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 2441 struct ts_config *conf, 2442 struct ts_state *state) 2443{ 2444 return skb_seq_read(offset, text, TS_SKB_CB(state)); 2445} 2446 2447static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 2448{ 2449 skb_abort_seq_read(TS_SKB_CB(state)); 2450} 2451 2452/** 2453 * skb_find_text - Find a text pattern in skb data 2454 * @skb: the buffer to look in 2455 * @from: search offset 2456 * @to: search limit 2457 * @config: textsearch configuration 2458 * @state: uninitialized textsearch state variable 2459 * 2460 * Finds a pattern in the skb data according to the specified 2461 * textsearch configuration. Use textsearch_next() to retrieve 2462 * subsequent occurrences of the pattern. Returns the offset 2463 * to the first occurrence or UINT_MAX if no match was found. 2464 */ 2465unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2466 unsigned int to, struct ts_config *config, 2467 struct ts_state *state) 2468{ 2469 unsigned int ret; 2470 2471 config->get_next_block = skb_ts_get_next_block; 2472 config->finish = skb_ts_finish; 2473 2474 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 2475 2476 ret = textsearch_find(config, state); 2477 return (ret <= to - from ? ret : UINT_MAX); 2478} 2479EXPORT_SYMBOL(skb_find_text); 2480 2481/** 2482 * skb_append_datato_frags: - append the user data to a skb 2483 * @sk: sock structure 2484 * @skb: skb structure to be appened with user data. 2485 * @getfrag: call back function to be used for getting the user data 2486 * @from: pointer to user message iov 2487 * @length: length of the iov message 2488 * 2489 * Description: This procedure append the user data in the fragment part 2490 * of the skb if any page alloc fails user this procedure returns -ENOMEM 2491 */ 2492int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2493 int (*getfrag)(void *from, char *to, int offset, 2494 int len, int odd, struct sk_buff *skb), 2495 void *from, int length) 2496{ 2497 int frg_cnt = 0; 2498 skb_frag_t *frag = NULL; 2499 struct page *page = NULL; 2500 int copy, left; 2501 int offset = 0; 2502 int ret; 2503 2504 do { 2505 /* Return error if we don't have space for new frag */ 2506 frg_cnt = skb_shinfo(skb)->nr_frags; 2507 if (frg_cnt >= MAX_SKB_FRAGS) 2508 return -EFAULT; 2509 2510 /* allocate a new page for next frag */ 2511 page = alloc_pages(sk->sk_allocation, 0); 2512 2513 /* If alloc_page fails just return failure and caller will 2514 * free previous allocated pages by doing kfree_skb() 2515 */ 2516 if (page == NULL) 2517 return -ENOMEM; 2518 2519 /* initialize the next frag */ 2520 sk->sk_sndmsg_page = page; 2521 sk->sk_sndmsg_off = 0; 2522 skb_fill_page_desc(skb, frg_cnt, page, 0, 0); 2523 skb->truesize += PAGE_SIZE; 2524 atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc); 2525 2526 /* get the new initialized frag */ 2527 frg_cnt = skb_shinfo(skb)->nr_frags; 2528 frag = &skb_shinfo(skb)->frags[frg_cnt - 1]; 2529 2530 /* copy the user data to page */ 2531 left = PAGE_SIZE - frag->page_offset; 2532 copy = (length > left)? left : length; 2533 2534 ret = getfrag(from, (page_address(frag->page) + 2535 frag->page_offset + frag->size), 2536 offset, copy, 0, skb); 2537 if (ret < 0) 2538 return -EFAULT; 2539 2540 /* copy was successful so update the size parameters */ 2541 sk->sk_sndmsg_off += copy; 2542 frag->size += copy; 2543 skb->len += copy; 2544 skb->data_len += copy; 2545 offset += copy; 2546 length -= copy; 2547 2548 } while (length > 0); 2549 2550 return 0; 2551} 2552EXPORT_SYMBOL(skb_append_datato_frags); 2553 2554/** 2555 * skb_pull_rcsum - pull skb and update receive checksum 2556 * @skb: buffer to update 2557 * @len: length of data pulled 2558 * 2559 * This function performs an skb_pull on the packet and updates 2560 * the CHECKSUM_COMPLETE checksum. It should be used on 2561 * receive path processing instead of skb_pull unless you know 2562 * that the checksum difference is zero (e.g., a valid IP header) 2563 * or you are setting ip_summed to CHECKSUM_NONE. 2564 */ 2565unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2566{ 2567 BUG_ON(len > skb->len); 2568 skb->len -= len; 2569 BUG_ON(skb->len < skb->data_len); 2570 skb_postpull_rcsum(skb, skb->data, len); 2571 return skb->data += len; 2572} 2573EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2574 2575/** 2576 * skb_segment - Perform protocol segmentation on skb. 2577 * @skb: buffer to segment 2578 * @features: features for the output path (see dev->features) 2579 * 2580 * This function performs segmentation on the given skb. It returns 2581 * a pointer to the first in a list of new skbs for the segments. 2582 * In case of error it returns ERR_PTR(err). 2583 */ 2584struct sk_buff *skb_segment(struct sk_buff *skb, int features) 2585{ 2586 struct sk_buff *segs = NULL; 2587 struct sk_buff *tail = NULL; 2588 struct sk_buff *fskb = skb_shinfo(skb)->frag_list; 2589 unsigned int mss = skb_shinfo(skb)->gso_size; 2590 unsigned int doffset = skb->data - skb_mac_header(skb); 2591 unsigned int offset = doffset; 2592 unsigned int headroom; 2593 unsigned int len; 2594 int sg = features & NETIF_F_SG; 2595 int nfrags = skb_shinfo(skb)->nr_frags; 2596 int err = -ENOMEM; 2597 int i = 0; 2598 int pos; 2599 2600 __skb_push(skb, doffset); 2601 headroom = skb_headroom(skb); 2602 pos = skb_headlen(skb); 2603 2604 do { 2605 struct sk_buff *nskb; 2606 skb_frag_t *frag; 2607 int hsize; 2608 int size; 2609 2610 len = skb->len - offset; 2611 if (len > mss) 2612 len = mss; 2613 2614 hsize = skb_headlen(skb) - offset; 2615 if (hsize < 0) 2616 hsize = 0; 2617 if (hsize > len || !sg) 2618 hsize = len; 2619 2620 if (!hsize && i >= nfrags) { 2621 BUG_ON(fskb->len != len); 2622 2623 pos += len; 2624 nskb = skb_clone(fskb, GFP_ATOMIC); 2625 fskb = fskb->next; 2626 2627 if (unlikely(!nskb)) 2628 goto err; 2629 2630 hsize = skb_end_pointer(nskb) - nskb->head; 2631 if (skb_cow_head(nskb, doffset + headroom)) { 2632 kfree_skb(nskb); 2633 goto err; 2634 } 2635 2636 nskb->truesize += skb_end_pointer(nskb) - nskb->head - 2637 hsize; 2638 skb_release_head_state(nskb); 2639 __skb_push(nskb, doffset); 2640 } else { 2641 nskb = alloc_skb(hsize + doffset + headroom, 2642 GFP_ATOMIC); 2643 2644 if (unlikely(!nskb)) 2645 goto err; 2646 2647 skb_reserve(nskb, headroom); 2648 __skb_put(nskb, doffset); 2649 } 2650 2651 if (segs) 2652 tail->next = nskb; 2653 else 2654 segs = nskb; 2655 tail = nskb; 2656 2657 __copy_skb_header(nskb, skb); 2658 nskb->mac_len = skb->mac_len; 2659 2660 /* nskb and skb might have different headroom */ 2661 if (nskb->ip_summed == CHECKSUM_PARTIAL) 2662 nskb->csum_start += skb_headroom(nskb) - headroom; 2663 2664 skb_reset_mac_header(nskb); 2665 skb_set_network_header(nskb, skb->mac_len); 2666 nskb->transport_header = (nskb->network_header + 2667 skb_network_header_len(skb)); 2668 skb_copy_from_linear_data(skb, nskb->data, doffset); 2669 2670 if (fskb != skb_shinfo(skb)->frag_list) 2671 continue; 2672 2673 if (!sg) { 2674 nskb->ip_summed = CHECKSUM_NONE; 2675 nskb->csum = skb_copy_and_csum_bits(skb, offset, 2676 skb_put(nskb, len), 2677 len, 0); 2678 continue; 2679 } 2680 2681 frag = skb_shinfo(nskb)->frags; 2682 2683 skb_copy_from_linear_data_offset(skb, offset, 2684 skb_put(nskb, hsize), hsize); 2685 2686 while (pos < offset + len && i < nfrags) { 2687 *frag = skb_shinfo(skb)->frags[i]; 2688 get_page(frag->page); 2689 size = frag->size; 2690 2691 if (pos < offset) { 2692 frag->page_offset += offset - pos; 2693 frag->size -= offset - pos; 2694 } 2695 2696 skb_shinfo(nskb)->nr_frags++; 2697 2698 if (pos + size <= offset + len) { 2699 i++; 2700 pos += size; 2701 } else { 2702 frag->size -= pos + size - (offset + len); 2703 goto skip_fraglist; 2704 } 2705 2706 frag++; 2707 } 2708 2709 if (pos < offset + len) { 2710 struct sk_buff *fskb2 = fskb; 2711 2712 BUG_ON(pos + fskb->len != offset + len); 2713 2714 pos += fskb->len; 2715 fskb = fskb->next; 2716 2717 if (fskb2->next) { 2718 fskb2 = skb_clone(fskb2, GFP_ATOMIC); 2719 if (!fskb2) 2720 goto err; 2721 } else 2722 skb_get(fskb2); 2723 2724 SKB_FRAG_ASSERT(nskb); 2725 skb_shinfo(nskb)->frag_list = fskb2; 2726 } 2727 2728skip_fraglist: 2729 nskb->data_len = len - hsize; 2730 nskb->len += nskb->data_len; 2731 nskb->truesize += nskb->data_len; 2732 } while ((offset += len) < skb->len); 2733 2734 return segs; 2735 2736err: 2737 while ((skb = segs)) { 2738 segs = skb->next; 2739 kfree_skb(skb); 2740 } 2741 return ERR_PTR(err); 2742} 2743EXPORT_SYMBOL_GPL(skb_segment); 2744 2745 2746/** 2747 * skb_segment - Perform protocol segmentation on skb. 2748 * @skb: buffer to segment 2749 * @features: features for the output path (see dev->features) 2750 * 2751 * This function performs segmentation on the given skb. It returns 2752 * a pointer to the first in a list of new skbs for the segments. 2753 * In case of error it returns ERR_PTR(err). 2754 */ 2755struct sk_buff BCMFASTPATH_HOST *skb_tcp_segment(struct sk_buff *skb, int features, 2756 unsigned int oldlen, unsigned thlen) 2757{ 2758 int nsegs = 0, tcpf_hdrbuf = 0; 2759 int id = ntohs(ip_hdr(skb)->id); 2760 struct iphdr *iph; 2761 struct tcphdr *th; 2762 __be32 delta = 0; 2763 unsigned int seq = 0; 2764 struct sk_buff *fskb = skb_shinfo(skb)->frag_list; 2765 unsigned int mss = skb_shinfo(skb)->gso_size; 2766 unsigned int doffset = skb->data - skb_mac_header(skb); 2767 unsigned int offset = doffset; 2768 unsigned int headroom; 2769 unsigned int len; 2770 int sg = features & NETIF_F_SG; 2771 int nfrags = skb_shinfo(skb)->nr_frags; 2772 int err = -ENOMEM; 2773 int i = 0; 2774 int pos; 2775 2776 __skb_push(skb, doffset); 2777 headroom = skb_headroom(skb); 2778 pos = skb_headlen(skb); 2779 2780 do { 2781 struct sk_buff *nskb; 2782 skb_frag_t *frag; 2783 int hsize; 2784 int size; 2785 2786 len = skb->len - offset; 2787 if (len > mss) 2788 len = mss; 2789 2790 hsize = skb_headlen(skb) - offset; 2791 if (hsize < 0) 2792 hsize = 0; 2793 if (hsize > len || !sg) 2794 hsize = len; 2795 2796 if (!hsize && i >= nfrags) { 2797 BUG_ON(fskb->len != len); 2798 2799 pos += len; 2800 nskb = skb_clone(fskb, GFP_ATOMIC); 2801 fskb = fskb->next; 2802 2803 if (unlikely(!nskb)) 2804 goto err; 2805 2806 hsize = skb_end_pointer(nskb) - nskb->head; 2807 if (skb_cow_head(nskb, doffset + headroom)) { 2808 kfree_skb(nskb); 2809 goto err; 2810 } 2811 2812 nskb->truesize += skb_end_pointer(nskb) - nskb->head - 2813 hsize; 2814 skb_release_head_state(nskb); 2815 __skb_push(nskb, doffset); 2816 } else { 2817 nskb = skb_tcph_pool_alloc(tcph_pool, hsize + doffset + headroom); 2818 if (nskb == NULL) { 2819 tcpf_hdrbuf = 0; 2820 nskb = alloc_skb(hsize + doffset + headroom, GFP_ATOMIC); 2821 if (unlikely(!nskb)) 2822 goto err; 2823 } else 2824 tcpf_hdrbuf = 1; 2825 2826 skb_reserve(nskb, headroom); 2827 __skb_put(nskb, doffset); 2828 } 2829 2830 nsegs++; 2831 2832 __copy_skb_header(nskb, skb); 2833 if (tcpf_hdrbuf) 2834 nskb->tcpf_hdrbuf = 1; 2835 nskb->mac_len = skb->mac_len; 2836 2837 /* nskb and skb might have different headroom */ 2838 if (nskb->ip_summed == CHECKSUM_PARTIAL) 2839 nskb->csum_start += skb_headroom(nskb) - headroom; 2840 2841 skb_reset_mac_header(nskb); 2842 skb_set_network_header(nskb, skb->mac_len); 2843 nskb->transport_header = (nskb->network_header + 2844 skb_network_header_len(skb)); 2845 skb_copy_from_linear_data(skb, nskb->data, doffset); 2846 2847 if (fskb != skb_shinfo(skb)->frag_list) 2848 continue; 2849 2850 if (!sg) { 2851 nskb->ip_summed = CHECKSUM_NONE; 2852 nskb->csum = skb_copy_and_csum_bits(skb, offset, 2853 skb_put(nskb, len), 2854 len, 0); 2855 goto not_sg; 2856 } 2857 2858 frag = skb_shinfo(nskb)->frags; 2859 2860 skb_copy_from_linear_data_offset(skb, offset, 2861 skb_put(nskb, hsize), hsize); 2862 2863 while (pos < offset + len && i < nfrags) { 2864 *frag = skb_shinfo(skb)->frags[i]; 2865 get_page(frag->page); 2866 size = frag->size; 2867 2868 if (pos < offset) { 2869 frag->page_offset += offset - pos; 2870 frag->size -= offset - pos; 2871 } 2872 2873 skb_shinfo(nskb)->nr_frags++; 2874 2875 if (pos + size <= offset + len) { 2876 i++; 2877 pos += size; 2878 } else { 2879 frag->size -= pos + size - (offset + len); 2880 goto skip_fraglist; 2881 } 2882 2883 frag++; 2884 } 2885 2886 if (pos < offset + len) { 2887 struct sk_buff *fskb2 = fskb; 2888 2889 BUG_ON(pos + fskb->len != offset + len); 2890 2891 pos += fskb->len; 2892 fskb = fskb->next; 2893 2894 if (fskb2->next) { 2895 fskb2 = skb_clone(fskb2, GFP_ATOMIC); 2896 if (!fskb2) 2897 goto err; 2898 } else 2899 skb_get(fskb2); 2900 2901 SKB_FRAG_ASSERT(nskb); 2902 skb_shinfo(nskb)->frag_list = fskb2; 2903 } 2904 2905skip_fraglist: 2906 nskb->data_len = len - hsize; 2907 nskb->len += nskb->data_len; 2908 nskb->truesize += nskb->data_len; 2909 2910not_sg: 2911 /* IP hdr fixup */ 2912 iph = ip_hdr(nskb); 2913 iph->id = htons(id++); 2914 iph->tot_len = htons(nskb->len - nskb->mac_len); 2915 iph->check = 0; 2916 iph->check = ip_fast_csum(skb_network_header(nskb), iph->ihl); 2917 2918 /* TCP hdr fixup */ 2919 th = tcp_hdr(nskb); 2920 if ((offset + len) >= skb->len) { 2921 /* Last segment */ 2922 delta = htonl(oldlen + (nskb->tail - nskb->transport_header) + 2923 nskb->data_len); 2924 seq += mss; 2925 th->seq = htonl(seq); 2926 th->cwr = 0; 2927 } else { 2928 th->fin = th->psh = 0; 2929 if (nsegs == 1) { 2930 /* First segment */ 2931 delta = htonl(oldlen + (thlen + mss)); 2932 seq = ntohl(th->seq); 2933 } else { 2934 /* Mid segment */ 2935 seq += mss; 2936 th->seq = htonl(seq); 2937 th->cwr = 0; 2938 } 2939 } 2940 2941 th->check = ~csum_fold((__force __wsum)((__force u32)th->check + 2942 (__force u32)delta)); 2943 if (nskb->ip_summed != CHECKSUM_PARTIAL) 2944 th->check = 2945 csum_fold(csum_partial(skb_transport_header(nskb), 2946 thlen, nskb->csum)); 2947 dev_queue_xmit(nskb); 2948 } while ((offset += len) < skb->len); 2949 2950 return NULL; 2951 2952err: 2953 return ERR_PTR(err); 2954} 2955EXPORT_SYMBOL_GPL(skb_tcp_segment); 2956 2957int BCMFASTPATH_HOST skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 2958{ 2959 struct sk_buff *p = *head; 2960 struct sk_buff *nskb; 2961 struct skb_shared_info *skbinfo = skb_shinfo(skb); 2962 struct skb_shared_info *pinfo = skb_shinfo(p); 2963 unsigned int headroom; 2964 unsigned int len = skb_gro_len(skb); 2965 unsigned int offset = skb_gro_offset(skb); 2966 unsigned int headlen = skb_headlen(skb); 2967 2968 if (p->len + len >= 65536) 2969 return -E2BIG; 2970 2971 if (pinfo->frag_list) 2972 goto merge; 2973 else if (headlen <= offset) { 2974 skb_frag_t *frag; 2975 skb_frag_t *frag2; 2976 int i = skbinfo->nr_frags; 2977 int nr_frags = pinfo->nr_frags + i; 2978 2979 offset -= headlen; 2980 2981 if (nr_frags > MAX_SKB_FRAGS) 2982 return -E2BIG; 2983 2984 pinfo->nr_frags = nr_frags; 2985 skbinfo->nr_frags = 0; 2986 2987 frag = pinfo->frags + nr_frags; 2988 frag2 = skbinfo->frags + i; 2989 do { 2990 *--frag = *--frag2; 2991 } while (--i); 2992 2993 frag->page_offset += offset; 2994 frag->size -= offset; 2995 2996 skb->truesize -= skb->data_len; 2997 skb->len -= skb->data_len; 2998 skb->data_len = 0; 2999 3000 NAPI_GRO_CB(skb)->free = 1; 3001 goto done; 3002 } else if (skb_gro_len(p) != pinfo->gso_size) 3003 return -E2BIG; 3004 3005 headroom = skb_headroom(p); 3006 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 3007 if (unlikely(!nskb)) 3008 return -ENOMEM; 3009 3010 __copy_skb_header(nskb, p); 3011 nskb->mac_len = p->mac_len; 3012 3013 skb_reserve(nskb, headroom); 3014 __skb_put(nskb, skb_gro_offset(p)); 3015 3016 skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 3017 skb_set_network_header(nskb, skb_network_offset(p)); 3018 skb_set_transport_header(nskb, skb_transport_offset(p)); 3019 3020 __skb_pull(p, skb_gro_offset(p)); 3021 memcpy(skb_mac_header(nskb), skb_mac_header(p), 3022 p->data - skb_mac_header(p)); 3023 3024 *NAPI_GRO_CB(nskb) = *NAPI_GRO_CB(p); 3025 skb_shinfo(nskb)->frag_list = p; 3026 skb_shinfo(nskb)->gso_size = pinfo->gso_size; 3027 pinfo->gso_size = 0; 3028 skb_header_release(p); 3029 nskb->prev = p; 3030 3031 nskb->data_len += p->len; 3032 nskb->truesize += p->len; 3033 nskb->len += p->len; 3034 3035 *head = nskb; 3036 nskb->next = p->next; 3037 p->next = NULL; 3038 3039 p = nskb; 3040 3041merge: 3042 if (offset > headlen) { 3043 skbinfo->frags[0].page_offset += offset - headlen; 3044 skbinfo->frags[0].size -= offset - headlen; 3045 offset = headlen; 3046 } 3047 3048 __skb_pull(skb, offset); 3049 3050 p->prev->next = skb; 3051 p->prev = skb; 3052 skb_header_release(skb); 3053 3054done: 3055 NAPI_GRO_CB(p)->count++; 3056 p->data_len += len; 3057 p->truesize += len; 3058 p->len += len; 3059 3060 NAPI_GRO_CB(skb)->same_flow = 1; 3061 return 0; 3062} 3063EXPORT_SYMBOL_GPL(skb_gro_receive); 3064 3065void __init skb_init(void) 3066{ 3067 skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 3068 sizeof(struct sk_buff), 3069 0, 3070 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 3071 NULL); 3072 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3073 (2*sizeof(struct sk_buff)) + 3074 sizeof(atomic_t), 3075 0, 3076 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 3077 NULL); 3078} 3079 3080/** 3081 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3082 * @skb: Socket buffer containing the buffers to be mapped 3083 * @sg: The scatter-gather list to map into 3084 * @offset: The offset into the buffer's contents to start mapping 3085 * @len: Length of buffer space to be mapped 3086 * 3087 * Fill the specified scatter-gather list with mappings/pointers into a 3088 * region of the buffer space attached to a socket buffer. 3089 */ 3090static int BCMFASTPATH_HOST 3091__skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3092{ 3093 int start = skb_headlen(skb); 3094 int i, copy = start - offset; 3095 struct sk_buff *frag_iter; 3096 int elt = 0; 3097 3098 if (copy > 0) { 3099 if (copy > len) 3100 copy = len; 3101 sg->page_link = 0; 3102 sg_set_buf(sg, skb->data + offset, copy); 3103 elt++; 3104 if ((len -= copy) == 0) 3105 return elt; 3106 offset += copy; 3107 } 3108 3109 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3110 int end; 3111 3112 WARN_ON(start > offset + len); 3113 3114 end = start + skb_shinfo(skb)->frags[i].size; 3115 if ((copy = end - offset) > 0) { 3116 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3117 3118 if (copy > len) 3119 copy = len; 3120 sg[elt].page_link = 0; 3121 sg_set_page(&sg[elt], frag->page, copy, 3122 frag->page_offset+offset-start); 3123 elt++; 3124 if (!(len -= copy)) 3125 return elt; 3126 offset += copy; 3127 } 3128 start = end; 3129 } 3130 3131 skb_walk_frags(skb, frag_iter) { 3132 int end; 3133 3134 WARN_ON(start > offset + len); 3135 3136 end = start + frag_iter->len; 3137 if ((copy = end - offset) > 0) { 3138 if (copy > len) 3139 copy = len; 3140 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 3141 copy); 3142 if ((len -= copy) == 0) 3143 return elt; 3144 offset += copy; 3145 } 3146 start = end; 3147 } 3148 BUG_ON(len); 3149 return elt; 3150} 3151 3152int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3153{ 3154 int nsg = __skb_to_sgvec(skb, sg, offset, len); 3155 3156 sg_mark_end(&sg[nsg - 1]); 3157 3158 return nsg; 3159} 3160EXPORT_SYMBOL_GPL(skb_to_sgvec); 3161 3162/** 3163 * skb_cow_data - Check that a socket buffer's data buffers are writable 3164 * @skb: The socket buffer to check. 3165 * @tailbits: Amount of trailing space to be added 3166 * @trailer: Returned pointer to the skb where the @tailbits space begins 3167 * 3168 * Make sure that the data buffers attached to a socket buffer are 3169 * writable. If they are not, private copies are made of the data buffers 3170 * and the socket buffer is set to use these instead. 3171 * 3172 * If @tailbits is given, make sure that there is space to write @tailbits 3173 * bytes of data beyond current end of socket buffer. @trailer will be 3174 * set to point to the skb in which this space begins. 3175 * 3176 * The number of scatterlist elements required to completely map the 3177 * COW'd and extended socket buffer will be returned. 3178 */ 3179int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3180{ 3181 int copyflag; 3182 int elt; 3183 struct sk_buff *skb1, **skb_p; 3184 3185 /* If skb is cloned or its head is paged, reallocate 3186 * head pulling out all the pages (pages are considered not writable 3187 * at the moment even if they are anonymous). 3188 */ 3189 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3190 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3191 return -ENOMEM; 3192 3193 /* Easy case. Most of packets will go this way. */ 3194 if (!skb_has_frags(skb)) { 3195 /* A little of trouble, not enough of space for trailer. 3196 * This should not happen, when stack is tuned to generate 3197 * good frames. OK, on miss we reallocate and reserve even more 3198 * space, 128 bytes is fair. */ 3199 3200 if (skb_tailroom(skb) < tailbits && 3201 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3202 return -ENOMEM; 3203 3204 /* Voila! */ 3205 *trailer = skb; 3206 return 1; 3207 } 3208 3209 /* Misery. We are in troubles, going to mincer fragments... */ 3210 3211 elt = 1; 3212 skb_p = &skb_shinfo(skb)->frag_list; 3213 copyflag = 0; 3214 3215 while ((skb1 = *skb_p) != NULL) { 3216 int ntail = 0; 3217 3218 /* The fragment is partially pulled by someone, 3219 * this can happen on input. Copy it and everything 3220 * after it. */ 3221 3222 if (skb_shared(skb1)) 3223 copyflag = 1; 3224 3225 /* If the skb is the last, worry about trailer. */ 3226 3227 if (skb1->next == NULL && tailbits) { 3228 if (skb_shinfo(skb1)->nr_frags || 3229 skb_has_frags(skb1) || 3230 skb_tailroom(skb1) < tailbits) 3231 ntail = tailbits + 128; 3232 } 3233 3234 if (copyflag || 3235 skb_cloned(skb1) || 3236 ntail || 3237 skb_shinfo(skb1)->nr_frags || 3238 skb_has_frags(skb1)) { 3239 struct sk_buff *skb2; 3240 3241 /* Fuck, we are miserable poor guys... */ 3242 if (ntail == 0) 3243 skb2 = skb_copy(skb1, GFP_ATOMIC); 3244 else 3245 skb2 = skb_copy_expand(skb1, 3246 skb_headroom(skb1), 3247 ntail, 3248 GFP_ATOMIC); 3249 if (unlikely(skb2 == NULL)) 3250 return -ENOMEM; 3251 3252 if (skb1->sk) 3253 skb_set_owner_w(skb2, skb1->sk); 3254 3255 /* Looking around. Are we still alive? 3256 * OK, link new skb, drop old one */ 3257 3258 skb2->next = skb1->next; 3259 *skb_p = skb2; 3260 kfree_skb(skb1); 3261 skb1 = skb2; 3262 } 3263 elt++; 3264 *trailer = skb1; 3265 skb_p = &skb1->next; 3266 } 3267 3268 return elt; 3269} 3270EXPORT_SYMBOL_GPL(skb_cow_data); 3271 3272static void sock_rmem_free(struct sk_buff *skb) 3273{ 3274 struct sock *sk = skb->sk; 3275 3276 atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3277} 3278 3279/* 3280 * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3281 */ 3282int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3283{ 3284 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 3285 (unsigned)sk->sk_rcvbuf) 3286 return -ENOMEM; 3287 3288 skb_orphan(skb); 3289 skb->sk = sk; 3290 skb->destructor = sock_rmem_free; 3291 atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3292 3293 skb_queue_tail(&sk->sk_error_queue, skb); 3294 if (!sock_flag(sk, SOCK_DEAD)) 3295 sk->sk_data_ready(sk, skb->len); 3296 return 0; 3297} 3298EXPORT_SYMBOL(sock_queue_err_skb); 3299 3300void skb_tstamp_tx(struct sk_buff *orig_skb, 3301 struct skb_shared_hwtstamps *hwtstamps) 3302{ 3303 struct sock *sk = orig_skb->sk; 3304 struct sock_exterr_skb *serr; 3305 struct sk_buff *skb; 3306 int err; 3307 3308 if (!sk) 3309 return; 3310 3311 skb = skb_clone(orig_skb, GFP_ATOMIC); 3312 if (!skb) 3313 return; 3314 3315 if (hwtstamps) { 3316 *skb_hwtstamps(skb) = 3317 *hwtstamps; 3318 } else { 3319 /* 3320 * no hardware time stamps available, 3321 * so keep the skb_shared_tx and only 3322 * store software time stamp 3323 */ 3324 skb->tstamp = ktime_get_real(); 3325 } 3326 3327 serr = SKB_EXT_ERR(skb); 3328 memset(serr, 0, sizeof(*serr)); 3329 serr->ee.ee_errno = ENOMSG; 3330 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3331 3332 err = sock_queue_err_skb(sk, skb); 3333 3334 if (err) 3335 kfree_skb(skb); 3336} 3337EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3338 3339 3340/** 3341 * skb_partial_csum_set - set up and verify partial csum values for packet 3342 * @skb: the skb to set 3343 * @start: the number of bytes after skb->data to start checksumming. 3344 * @off: the offset from start to place the checksum. 3345 * 3346 * For untrusted partially-checksummed packets, we need to make sure the values 3347 * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3348 * 3349 * This function checks and sets those values and skb->ip_summed: if this 3350 * returns false you should drop the packet. 3351 */ 3352bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3353{ 3354 if (unlikely(start > skb_headlen(skb)) || 3355 unlikely((int)start + off > skb_headlen(skb) - 2)) { 3356 if (net_ratelimit()) 3357 printk(KERN_WARNING 3358 "bad partial csum: csum=%u/%u len=%u\n", 3359 start, off, skb_headlen(skb)); 3360 return false; 3361 } 3362 skb->ip_summed = CHECKSUM_PARTIAL; 3363 skb->csum_start = skb_headroom(skb) + start; 3364 skb->csum_offset = off; 3365 return true; 3366} 3367EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3368 3369void __skb_warn_lro_forwarding(const struct sk_buff *skb) 3370{ 3371 if (net_ratelimit()) 3372 pr_warning("%s: received packets cannot be forwarded" 3373 " while LRO is enabled\n", skb->dev->name); 3374} 3375EXPORT_SYMBOL(__skb_warn_lro_forwarding); 3376 3377/* 3378 * Allocate and add an object to packet pool. 3379 */ 3380static void * __init 3381skb_tcph_pool_add(tcph_pool_t *tcph_pool) 3382{ 3383 struct sk_buff *skb; 3384 3385 spin_lock_bh(&tcph_pool->lock); 3386 if (tcph_pool == NULL) 3387 goto err; 3388 3389 /* No need to allocate more objects */ 3390 if (tcph_pool->curr_obj == tcph_pool->max_obj) 3391 goto err; 3392 3393 /* Allocate a new skb and add it to the tcph_pool */ 3394 skb = dev_alloc_skb(tcph_pool->obj_size); 3395 if (skb == NULL) { 3396 printk("%s: skb alloc of len %d failed\n", __FUNCTION__, 3397 tcph_pool->obj_size); 3398 goto err; 3399 } 3400 3401 /* Add to tcph_pool */ 3402 skb->next = (struct sk_buff *)tcph_pool->head; 3403 tcph_pool->head = skb; 3404 tcph_pool->curr_obj++; 3405 3406 /* Use bit flag to indicate skb from fast tcph_pool */ 3407 skb->tcpf_hdrbuf = 1; 3408 3409 spin_unlock_bh(&tcph_pool->lock); 3410 3411 return skb; 3412 3413err: 3414 spin_unlock_bh(&tcph_pool->lock); 3415 return NULL; 3416} 3417 3418/* 3419 * Initialize the tcp header pkt pool with specified number of skbs. 3420 */ 3421static int32 __init 3422skb_tcph_pool_init(uint numobj, uint size) 3423{ 3424 tcph_pool = kzalloc(sizeof(tcph_pool_t), GFP_ATOMIC); 3425 3426 if (tcph_pool == NULL) 3427 return -1; 3428 3429 tcph_pool->max_obj = numobj; 3430 tcph_pool->obj_size = size; 3431 3432 spin_lock_init(&tcph_pool->lock); 3433 3434 while (numobj--) { 3435 if (skb_tcph_pool_add(tcph_pool) == NULL) 3436 return -1; 3437 } 3438 3439 return 0; 3440} 3441 3442static inline struct sk_buff * 3443skb_tcph_pool_alloc(tcph_pool_t *tcph_pool, uint len) 3444{ 3445 struct sk_buff *skb; 3446 3447 spin_lock_bh(&tcph_pool->lock); 3448 3449 if ((len > tcph_pool->obj_size) || (tcph_pool->head == NULL)) { 3450 spin_unlock_bh(&tcph_pool->lock); 3451 return NULL; 3452 } 3453 3454 /* Get an object from tcph_pool */ 3455 skb = (struct sk_buff *)tcph_pool->head; 3456 tcph_pool->head = (void *)skb->next; 3457 3458 tcph_pool->curr_obj--; 3459 3460 pref_range(skb, skb + 1); 3461 3462 /* Init skb struct */ 3463 memset(skb, 0, offsetof(struct sk_buff, tail)); 3464 memset(((u8 *)&skb->users)+sizeof(atomic_t), 0, 3465 (sizeof(struct sk_buff) - (sizeof(atomic_t) + offsetof(struct sk_buff, users)))); 3466 skb->data = skb->head; 3467 skb_reset_tail_pointer(skb); 3468 atomic_set(&skb->users, 1); 3469#ifdef NET_SKBUFF_DATA_USES_OFFSET 3470 skb->mac_header = ~0U; 3471#endif 3472 3473 /* make sure we initialize shinfo sequentially */ 3474 memset(skb_shinfo(skb), 0, offsetof(struct skb_shared_info, dataref)); 3475 atomic_set(&skb_shinfo(skb)->dataref, 1); 3476 3477 /* Use bit flag to indicate skb from fast tcph_pool */ 3478 skb->tcpf_hdrbuf = 1; 3479 3480 spin_unlock_bh(&tcph_pool->lock); 3481 3482 return skb; 3483} 3484 3485static inline void 3486skb_tcph_pool_free(tcph_pool_t *tcph_pool, struct sk_buff *skb) 3487{ 3488 spin_lock_bh(&tcph_pool->lock); 3489 3490 skb_release_head_state(skb); 3491 3492 if (!skb->cloned || 3493 !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 3494 &skb_shinfo(skb)->dataref)) { 3495 if (skb_shinfo(skb)->nr_frags) { 3496 int i; 3497 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 3498 put_page(skb_shinfo(skb)->frags[i].page); 3499 } 3500 3501 if (skb_has_frags(skb)) 3502 skb_drop_fraglist(skb); 3503 } 3504 3505 /* Add object back to the tcph_pool */ 3506 skb->next = (struct sk_buff *)tcph_pool->head; 3507 tcph_pool->head = (void *)skb; 3508 3509 tcph_pool->curr_obj++; 3510 spin_unlock_bh(&tcph_pool->lock); 3511} 3512 3513#define TCPH_POOL_NUM_OBJ 128 3514#define TCPH_POOL_OBJ_SZ 320 3515 3516static int __init 3517skb_init_tcph_pool(void) 3518{ 3519 skb_tcph_pool_init(TCPH_POOL_NUM_OBJ, TCPH_POOL_OBJ_SZ); 3520 return 0; 3521} 3522 3523late_initcall(skb_init_tcph_pool); /* init level 7 */ 3524