1 /* 2 * Routines having to do with the 'struct sk_buff' memory handlers. 3 * 4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 5 * Florian La Roche <rzsfl@rz.uni-sb.de> 6 * 7 * Fixes: 8 * Alan Cox : Fixed the worst of the load 9 * balancer bugs. 10 * Dave Platt : Interrupt stacking fix. 11 * Richard Kooijman : Timestamp fixes. 12 * Alan Cox : Changed buffer format. 13 * Alan Cox : destructor hook for AF_UNIX etc. 14 * Linus Torvalds : Better skb_clone. 15 * Alan Cox : Added skb_copy. 16 * Alan Cox : Added all the changed routines Linus 17 * only put in the headers 18 * Ray VanTassle : Fixed --skb->lock in free 19 * Alan Cox : skb_copy copy arp field 20 * Andi Kleen : slabified it. 21 * Robert Olsson : Removed skb_head_pool 22 * 23 * NOTE: 24 * The __skb_ routines should be called with interrupts 25 * disabled, or you better be *real* sure that the operation is atomic 26 * with respect to whatever list is being frobbed (e.g. via lock_sock() 27 * or via disabling bottom half handlers, etc). 28 * 29 * This program is free software; you can redistribute it and/or 30 * modify it under the terms of the GNU General Public License 31 * as published by the Free Software Foundation; either version 32 * 2 of the License, or (at your option) any later version. 33 */ 34 35 /* 36 * The functions in this file will not compile correctly with gcc 2.4.x 37 */ 38 39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 40 41 #include <linux/module.h> 42 #include <linux/types.h> 43 #include <linux/kernel.h> 44 #include <linux/kmemcheck.h> 45 #include <linux/mm.h> 46 #include <linux/interrupt.h> 47 #include <linux/in.h> 48 #include <linux/inet.h> 49 #include <linux/slab.h> 50 #include <linux/tcp.h> 51 #include <linux/udp.h> 52 #include <linux/netdevice.h> 53 #ifdef CONFIG_NET_CLS_ACT 54 #include <net/pkt_sched.h> 55 #endif 56 #include <linux/string.h> 57 #include <linux/skbuff.h> 58 #include <linux/splice.h> 59 #include <linux/cache.h> 60 #include <linux/rtnetlink.h> 61 #include <linux/init.h> 62 #include <linux/scatterlist.h> 63 #include <linux/errqueue.h> 64 #include <linux/prefetch.h> 65 #include <linux/if_vlan.h> 66 67 #include <net/protocol.h> 68 #include <net/dst.h> 69 #include <net/sock.h> 70 #include <net/checksum.h> 71 #include <net/ip6_checksum.h> 72 #include <net/xfrm.h> 73 74 #include <asm/uaccess.h> 75 #include <trace/events/skb.h> 76 #include <linux/highmem.h> 77 78 struct kmem_cache *skbuff_head_cache __read_mostly; 79 static struct kmem_cache *skbuff_fclone_cache __read_mostly; 80 81 /** 82 * skb_panic - private function for out-of-line support 83 * @skb: buffer 84 * @sz: size 85 * @addr: address 86 * @msg: skb_over_panic or skb_under_panic 87 * 88 * Out-of-line support for skb_put() and skb_push(). 89 * Called via the wrapper skb_over_panic() or skb_under_panic(). 90 * Keep out of line to prevent kernel bloat. 91 * __builtin_return_address is not used because it is not always reliable. 92 */ 93 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 94 const char msg[]) 95 { 96 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 97 msg, addr, skb->len, sz, skb->head, skb->data, 98 (unsigned long)skb->tail, (unsigned long)skb->end, 99 skb->dev ? skb->dev->name : "<NULL>"); 100 BUG(); 101 } 102 103 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 104 { 105 skb_panic(skb, sz, addr, __func__); 106 } 107 108 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 109 { 110 skb_panic(skb, sz, addr, __func__); 111 } 112 113 /* 114 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 115 * the caller if emergency pfmemalloc reserves are being used. If it is and 116 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 117 * may be used. Otherwise, the packet data may be discarded until enough 118 * memory is free 119 */ 120 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 121 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 122 123 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 124 unsigned long ip, bool *pfmemalloc) 125 { 126 void *obj; 127 bool ret_pfmemalloc = false; 128 129 /* 130 * Try a regular allocation, when that fails and we're not entitled 131 * to the reserves, fail. 132 */ 133 obj = kmalloc_node_track_caller(size, 134 flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 135 node); 136 if (obj || !(gfp_pfmemalloc_allowed(flags))) 137 goto out; 138 139 /* Try again but now we are using pfmemalloc reserves */ 140 ret_pfmemalloc = true; 141 obj = kmalloc_node_track_caller(size, flags, node); 142 143 out: 144 if (pfmemalloc) 145 *pfmemalloc = ret_pfmemalloc; 146 147 return obj; 148 } 149 150 /* Allocate a new skbuff. We do this ourselves so we can fill in a few 151 * 'private' fields and also do memory statistics to find all the 152 * [BEEP] leaks. 153 * 154 */ 155 156 struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node) 157 { 158 struct sk_buff *skb; 159 160 /* Get the HEAD */ 161 skb = kmem_cache_alloc_node(skbuff_head_cache, 162 gfp_mask & ~__GFP_DMA, node); 163 if (!skb) 164 goto out; 165 166 /* 167 * Only clear those fields we need to clear, not those that we will 168 * actually initialise below. Hence, don't put any more fields after 169 * the tail pointer in struct sk_buff! 170 */ 171 memset(skb, 0, offsetof(struct sk_buff, tail)); 172 skb->head = NULL; 173 skb->truesize = sizeof(struct sk_buff); 174 atomic_set(&skb->users, 1); 175 176 skb->mac_header = (typeof(skb->mac_header))~0U; 177 out: 178 return skb; 179 } 180 181 /** 182 * __alloc_skb - allocate a network buffer 183 * @size: size to allocate 184 * @gfp_mask: allocation mask 185 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 186 * instead of head cache and allocate a cloned (child) skb. 187 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 188 * allocations in case the data is required for writeback 189 * @node: numa node to allocate memory on 190 * 191 * Allocate a new &sk_buff. The returned buffer has no headroom and a 192 * tail room of at least size bytes. The object has a reference count 193 * of one. The return is the buffer. On a failure the return is %NULL. 194 * 195 * Buffers may only be allocated from interrupts using a @gfp_mask of 196 * %GFP_ATOMIC. 197 */ 198 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 199 int flags, int node) 200 { 201 struct kmem_cache *cache; 202 struct skb_shared_info *shinfo; 203 struct sk_buff *skb; 204 u8 *data; 205 bool pfmemalloc; 206 207 cache = (flags & SKB_ALLOC_FCLONE) 208 ? skbuff_fclone_cache : skbuff_head_cache; 209 210 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 211 gfp_mask |= __GFP_MEMALLOC; 212 213 /* Get the HEAD */ 214 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 215 if (!skb) 216 goto out; 217 prefetchw(skb); 218 219 /* We do our best to align skb_shared_info on a separate cache 220 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 221 * aligned memory blocks, unless SLUB/SLAB debug is enabled. 222 * Both skb->head and skb_shared_info are cache line aligned. 223 */ 224 size = SKB_DATA_ALIGN(size); 225 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 226 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 227 if (!data) 228 goto nodata; 229 /* kmalloc(size) might give us more room than requested. 230 * Put skb_shared_info exactly at the end of allocated zone, 231 * to allow max possible filling before reallocation. 232 */ 233 size = SKB_WITH_OVERHEAD(ksize(data)); 234 prefetchw(data + size); 235 236 /* 237 * Only clear those fields we need to clear, not those that we will 238 * actually initialise below. Hence, don't put any more fields after 239 * the tail pointer in struct sk_buff! 240 */ 241 memset(skb, 0, offsetof(struct sk_buff, tail)); 242 /* Account for allocated memory : skb + skb->head */ 243 skb->truesize = SKB_TRUESIZE(size); 244 skb->pfmemalloc = pfmemalloc; 245 atomic_set(&skb->users, 1); 246 skb->head = data; 247 skb->data = data; 248 skb_reset_tail_pointer(skb); 249 skb->end = skb->tail + size; 250 skb->mac_header = (typeof(skb->mac_header))~0U; 251 skb->transport_header = (typeof(skb->transport_header))~0U; 252 253 /* make sure we initialize shinfo sequentially */ 254 shinfo = skb_shinfo(skb); 255 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 256 atomic_set(&shinfo->dataref, 1); 257 kmemcheck_annotate_variable(shinfo->destructor_arg); 258 259 if (flags & SKB_ALLOC_FCLONE) { 260 struct sk_buff_fclones *fclones; 261 262 fclones = container_of(skb, struct sk_buff_fclones, skb1); 263 264 kmemcheck_annotate_bitfield(&fclones->skb2, flags1); 265 skb->fclone = SKB_FCLONE_ORIG; 266 atomic_set(&fclones->fclone_ref, 1); 267 268 fclones->skb2.fclone = SKB_FCLONE_FREE; 269 fclones->skb2.pfmemalloc = pfmemalloc; 270 } 271 out: 272 return skb; 273 nodata: 274 kmem_cache_free(cache, skb); 275 skb = NULL; 276 goto out; 277 } 278 EXPORT_SYMBOL(__alloc_skb); 279 280 /** 281 * build_skb - build a network buffer 282 * @data: data buffer provided by caller 283 * @frag_size: size of fragment, or 0 if head was kmalloced 284 * 285 * Allocate a new &sk_buff. Caller provides space holding head and 286 * skb_shared_info. @data must have been allocated by kmalloc() only if 287 * @frag_size is 0, otherwise data should come from the page allocator. 288 * The return is the new skb buffer. 289 * On a failure the return is %NULL, and @data is not freed. 290 * Notes : 291 * Before IO, driver allocates only data buffer where NIC put incoming frame 292 * Driver should add room at head (NET_SKB_PAD) and 293 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 294 * After IO, driver calls build_skb(), to allocate sk_buff and populate it 295 * before giving packet to stack. 296 * RX rings only contains data buffers, not full skbs. 297 */ 298 struct sk_buff *build_skb(void *data, unsigned int frag_size) 299 { 300 struct skb_shared_info *shinfo; 301 struct sk_buff *skb; 302 unsigned int size = frag_size ? : ksize(data); 303 304 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 305 if (!skb) 306 return NULL; 307 308 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 309 310 memset(skb, 0, offsetof(struct sk_buff, tail)); 311 skb->truesize = SKB_TRUESIZE(size); 312 skb->head_frag = frag_size != 0; 313 atomic_set(&skb->users, 1); 314 skb->head = data; 315 skb->data = data; 316 skb_reset_tail_pointer(skb); 317 skb->end = skb->tail + size; 318 skb->mac_header = (typeof(skb->mac_header))~0U; 319 skb->transport_header = (typeof(skb->transport_header))~0U; 320 321 /* make sure we initialize shinfo sequentially */ 322 shinfo = skb_shinfo(skb); 323 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 324 atomic_set(&shinfo->dataref, 1); 325 kmemcheck_annotate_variable(shinfo->destructor_arg); 326 327 return skb; 328 } 329 EXPORT_SYMBOL(build_skb); 330 331 struct netdev_alloc_cache { 332 struct page_frag frag; 333 /* we maintain a pagecount bias, so that we dont dirty cache line 334 * containing page->_count every time we allocate a fragment. 335 */ 336 unsigned int pagecnt_bias; 337 }; 338 static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); 339 340 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 341 { 342 struct netdev_alloc_cache *nc; 343 void *data = NULL; 344 int order; 345 unsigned long flags; 346 347 local_irq_save(flags); 348 nc = this_cpu_ptr(&netdev_alloc_cache); 349 if (unlikely(!nc->frag.page)) { 350 refill: 351 for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) { 352 gfp_t gfp = gfp_mask; 353 354 if (order) 355 gfp |= __GFP_COMP | __GFP_NOWARN; 356 nc->frag.page = alloc_pages(gfp, order); 357 if (likely(nc->frag.page)) 358 break; 359 if (--order < 0) 360 goto end; 361 } 362 nc->frag.size = PAGE_SIZE << order; 363 /* Even if we own the page, we do not use atomic_set(). 364 * This would break get_page_unless_zero() users. 365 */ 366 atomic_add(NETDEV_PAGECNT_MAX_BIAS - 1, 367 &nc->frag.page->_count); 368 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; 369 nc->frag.offset = 0; 370 } 371 372 if (nc->frag.offset + fragsz > nc->frag.size) { 373 if (atomic_read(&nc->frag.page->_count) != nc->pagecnt_bias) { 374 if (!atomic_sub_and_test(nc->pagecnt_bias, 375 &nc->frag.page->_count)) 376 goto refill; 377 /* OK, page count is 0, we can safely set it */ 378 atomic_set(&nc->frag.page->_count, 379 NETDEV_PAGECNT_MAX_BIAS); 380 } else { 381 atomic_add(NETDEV_PAGECNT_MAX_BIAS - nc->pagecnt_bias, 382 &nc->frag.page->_count); 383 } 384 nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; 385 nc->frag.offset = 0; 386 } 387 388 data = page_address(nc->frag.page) + nc->frag.offset; 389 nc->frag.offset += fragsz; 390 nc->pagecnt_bias--; 391 end: 392 local_irq_restore(flags); 393 return data; 394 } 395 396 /** 397 * netdev_alloc_frag - allocate a page fragment 398 * @fragsz: fragment size 399 * 400 * Allocates a frag from a page for receive buffer. 401 * Uses GFP_ATOMIC allocations. 402 */ 403 void *netdev_alloc_frag(unsigned int fragsz) 404 { 405 return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 406 } 407 EXPORT_SYMBOL(netdev_alloc_frag); 408 409 /** 410 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 411 * @dev: network device to receive on 412 * @length: length to allocate 413 * @gfp_mask: get_free_pages mask, passed to alloc_skb 414 * 415 * Allocate a new &sk_buff and assign it a usage count of one. The 416 * buffer has unspecified headroom built in. Users should allocate 417 * the headroom they think they need without accounting for the 418 * built in space. The built in space is used for optimisations. 419 * 420 * %NULL is returned if there is no free memory. 421 */ 422 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 423 unsigned int length, gfp_t gfp_mask) 424 { 425 struct sk_buff *skb = NULL; 426 unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + 427 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 428 429 if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { 430 void *data; 431 432 if (sk_memalloc_socks()) 433 gfp_mask |= __GFP_MEMALLOC; 434 435 data = __netdev_alloc_frag(fragsz, gfp_mask); 436 437 if (likely(data)) { 438 skb = build_skb(data, fragsz); 439 if (unlikely(!skb)) 440 put_page(virt_to_head_page(data)); 441 } 442 } else { 443 skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 444 SKB_ALLOC_RX, NUMA_NO_NODE); 445 } 446 if (likely(skb)) { 447 skb_reserve(skb, NET_SKB_PAD); 448 skb->dev = dev; 449 } 450 return skb; 451 } 452 EXPORT_SYMBOL(__netdev_alloc_skb); 453 454 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 455 int size, unsigned int truesize) 456 { 457 skb_fill_page_desc(skb, i, page, off, size); 458 skb->len += size; 459 skb->data_len += size; 460 skb->truesize += truesize; 461 } 462 EXPORT_SYMBOL(skb_add_rx_frag); 463 464 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 465 unsigned int truesize) 466 { 467 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 468 469 skb_frag_size_add(frag, size); 470 skb->len += size; 471 skb->data_len += size; 472 skb->truesize += truesize; 473 } 474 EXPORT_SYMBOL(skb_coalesce_rx_frag); 475 476 static void skb_drop_list(struct sk_buff **listp) 477 { 478 kfree_skb_list(*listp); 479 *listp = NULL; 480 } 481 482 static inline void skb_drop_fraglist(struct sk_buff *skb) 483 { 484 skb_drop_list(&skb_shinfo(skb)->frag_list); 485 } 486 487 static void skb_clone_fraglist(struct sk_buff *skb) 488 { 489 struct sk_buff *list; 490 491 skb_walk_frags(skb, list) 492 skb_get(list); 493 } 494 495 static void skb_free_head(struct sk_buff *skb) 496 { 497 if (skb->head_frag) 498 put_page(virt_to_head_page(skb->head)); 499 else 500 kfree(skb->head); 501 } 502 503 static void skb_release_data(struct sk_buff *skb) 504 { 505 struct skb_shared_info *shinfo = skb_shinfo(skb); 506 int i; 507 508 if (skb->cloned && 509 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 510 &shinfo->dataref)) 511 return; 512 513 for (i = 0; i < shinfo->nr_frags; i++) 514 __skb_frag_unref(&shinfo->frags[i]); 515 516 /* 517 * If skb buf is from userspace, we need to notify the caller 518 * the lower device DMA has done; 519 */ 520 if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { 521 struct ubuf_info *uarg; 522 523 uarg = shinfo->destructor_arg; 524 if (uarg->callback) 525 uarg->callback(uarg, true); 526 } 527 528 if (shinfo->frag_list) 529 kfree_skb_list(shinfo->frag_list); 530 531 skb_free_head(skb); 532 } 533 534 /* 535 * Free an skbuff by memory without cleaning the state. 536 */ 537 static void kfree_skbmem(struct sk_buff *skb) 538 { 539 struct sk_buff_fclones *fclones; 540 541 switch (skb->fclone) { 542 case SKB_FCLONE_UNAVAILABLE: 543 kmem_cache_free(skbuff_head_cache, skb); 544 break; 545 546 case SKB_FCLONE_ORIG: 547 fclones = container_of(skb, struct sk_buff_fclones, skb1); 548 if (atomic_dec_and_test(&fclones->fclone_ref)) 549 kmem_cache_free(skbuff_fclone_cache, fclones); 550 break; 551 552 case SKB_FCLONE_CLONE: 553 fclones = container_of(skb, struct sk_buff_fclones, skb2); 554 555 /* Warning : We must perform the atomic_dec_and_test() before 556 * setting skb->fclone back to SKB_FCLONE_FREE, otherwise 557 * skb_clone() could set clone_ref to 2 before our decrement. 558 * Anyway, if we are going to free the structure, no need to 559 * rewrite skb->fclone. 560 */ 561 if (atomic_dec_and_test(&fclones->fclone_ref)) { 562 kmem_cache_free(skbuff_fclone_cache, fclones); 563 } else { 564 /* The clone portion is available for 565 * fast-cloning again. 566 */ 567 skb->fclone = SKB_FCLONE_FREE; 568 } 569 break; 570 } 571 } 572 573 static void skb_release_head_state(struct sk_buff *skb) 574 { 575 skb_dst_drop(skb); 576 #ifdef CONFIG_XFRM 577 secpath_put(skb->sp); 578 #endif 579 if (skb->destructor) { 580 WARN_ON(in_irq()); 581 skb->destructor(skb); 582 } 583 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 584 nf_conntrack_put(skb->nfct); 585 #endif 586 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 587 nf_bridge_put(skb->nf_bridge); 588 #endif 589 /* XXX: IS this still necessary? - JHS */ 590 #ifdef CONFIG_NET_SCHED 591 skb->tc_index = 0; 592 #ifdef CONFIG_NET_CLS_ACT 593 skb->tc_verd = 0; 594 #endif 595 #endif 596 } 597 598 /* Free everything but the sk_buff shell. */ 599 static void skb_release_all(struct sk_buff *skb) 600 { 601 skb_release_head_state(skb); 602 if (likely(skb->head)) 603 skb_release_data(skb); 604 } 605 606 /** 607 * __kfree_skb - private function 608 * @skb: buffer 609 * 610 * Free an sk_buff. Release anything attached to the buffer. 611 * Clean the state. This is an internal helper function. Users should 612 * always call kfree_skb 613 */ 614 615 void __kfree_skb(struct sk_buff *skb) 616 { 617 skb_release_all(skb); 618 kfree_skbmem(skb); 619 } 620 EXPORT_SYMBOL(__kfree_skb); 621 622 /** 623 * kfree_skb - free an sk_buff 624 * @skb: buffer to free 625 * 626 * Drop a reference to the buffer and free it if the usage count has 627 * hit zero. 628 */ 629 void kfree_skb(struct sk_buff *skb) 630 { 631 if (unlikely(!skb)) 632 return; 633 if (likely(atomic_read(&skb->users) == 1)) 634 smp_rmb(); 635 else if (likely(!atomic_dec_and_test(&skb->users))) 636 return; 637 trace_kfree_skb(skb, __builtin_return_address(0)); 638 __kfree_skb(skb); 639 } 640 EXPORT_SYMBOL(kfree_skb); 641 642 void kfree_skb_list(struct sk_buff *segs) 643 { 644 while (segs) { 645 struct sk_buff *next = segs->next; 646 647 kfree_skb(segs); 648 segs = next; 649 } 650 } 651 EXPORT_SYMBOL(kfree_skb_list); 652 653 /** 654 * skb_tx_error - report an sk_buff xmit error 655 * @skb: buffer that triggered an error 656 * 657 * Report xmit error if a device callback is tracking this skb. 658 * skb must be freed afterwards. 659 */ 660 void skb_tx_error(struct sk_buff *skb) 661 { 662 if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 663 struct ubuf_info *uarg; 664 665 uarg = skb_shinfo(skb)->destructor_arg; 666 if (uarg->callback) 667 uarg->callback(uarg, false); 668 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 669 } 670 } 671 EXPORT_SYMBOL(skb_tx_error); 672 673 /** 674 * consume_skb - free an skbuff 675 * @skb: buffer to free 676 * 677 * Drop a ref to the buffer and free it if the usage count has hit zero 678 * Functions identically to kfree_skb, but kfree_skb assumes that the frame 679 * is being dropped after a failure and notes that 680 */ 681 void consume_skb(struct sk_buff *skb) 682 { 683 if (unlikely(!skb)) 684 return; 685 if (likely(atomic_read(&skb->users) == 1)) 686 smp_rmb(); 687 else if (likely(!atomic_dec_and_test(&skb->users))) 688 return; 689 trace_consume_skb(skb); 690 __kfree_skb(skb); 691 } 692 EXPORT_SYMBOL(consume_skb); 693 694 /* Make sure a field is enclosed inside headers_start/headers_end section */ 695 #define CHECK_SKB_FIELD(field) \ 696 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 697 offsetof(struct sk_buff, headers_start)); \ 698 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 699 offsetof(struct sk_buff, headers_end)); \ 700 701 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 702 { 703 new->tstamp = old->tstamp; 704 /* We do not copy old->sk */ 705 new->dev = old->dev; 706 memcpy(new->cb, old->cb, sizeof(old->cb)); 707 skb_dst_copy(new, old); 708 #ifdef CONFIG_XFRM 709 new->sp = secpath_get(old->sp); 710 #endif 711 __nf_copy(new, old, false); 712 713 /* Note : this field could be in headers_start/headers_end section 714 * It is not yet because we do not want to have a 16 bit hole 715 */ 716 new->queue_mapping = old->queue_mapping; 717 718 memcpy(&new->headers_start, &old->headers_start, 719 offsetof(struct sk_buff, headers_end) - 720 offsetof(struct sk_buff, headers_start)); 721 CHECK_SKB_FIELD(protocol); 722 CHECK_SKB_FIELD(csum); 723 CHECK_SKB_FIELD(hash); 724 CHECK_SKB_FIELD(priority); 725 CHECK_SKB_FIELD(skb_iif); 726 CHECK_SKB_FIELD(vlan_proto); 727 CHECK_SKB_FIELD(vlan_tci); 728 CHECK_SKB_FIELD(transport_header); 729 CHECK_SKB_FIELD(network_header); 730 CHECK_SKB_FIELD(mac_header); 731 CHECK_SKB_FIELD(inner_protocol); 732 CHECK_SKB_FIELD(inner_transport_header); 733 CHECK_SKB_FIELD(inner_network_header); 734 CHECK_SKB_FIELD(inner_mac_header); 735 CHECK_SKB_FIELD(mark); 736 #ifdef CONFIG_NETWORK_SECMARK 737 CHECK_SKB_FIELD(secmark); 738 #endif 739 #ifdef CONFIG_NET_RX_BUSY_POLL 740 CHECK_SKB_FIELD(napi_id); 741 #endif 742 #ifdef CONFIG_NET_SCHED 743 CHECK_SKB_FIELD(tc_index); 744 #ifdef CONFIG_NET_CLS_ACT 745 CHECK_SKB_FIELD(tc_verd); 746 #endif 747 #endif 748 749 } 750 751 /* 752 * You should not add any new code to this function. Add it to 753 * __copy_skb_header above instead. 754 */ 755 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 756 { 757 #define C(x) n->x = skb->x 758 759 n->next = n->prev = NULL; 760 n->sk = NULL; 761 __copy_skb_header(n, skb); 762 763 C(len); 764 C(data_len); 765 C(mac_len); 766 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 767 n->cloned = 1; 768 n->nohdr = 0; 769 n->destructor = NULL; 770 C(tail); 771 C(end); 772 C(head); 773 C(head_frag); 774 C(data); 775 C(truesize); 776 atomic_set(&n->users, 1); 777 778 atomic_inc(&(skb_shinfo(skb)->dataref)); 779 skb->cloned = 1; 780 781 return n; 782 #undef C 783 } 784 785 /** 786 * skb_morph - morph one skb into another 787 * @dst: the skb to receive the contents 788 * @src: the skb to supply the contents 789 * 790 * This is identical to skb_clone except that the target skb is 791 * supplied by the user. 792 * 793 * The target skb is returned upon exit. 794 */ 795 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 796 { 797 skb_release_all(dst); 798 return __skb_clone(dst, src); 799 } 800 EXPORT_SYMBOL_GPL(skb_morph); 801 802 /** 803 * skb_copy_ubufs - copy userspace skb frags buffers to kernel 804 * @skb: the skb to modify 805 * @gfp_mask: allocation priority 806 * 807 * This must be called on SKBTX_DEV_ZEROCOPY skb. 808 * It will copy all frags into kernel and drop the reference 809 * to userspace pages. 810 * 811 * If this function is called from an interrupt gfp_mask() must be 812 * %GFP_ATOMIC. 813 * 814 * Returns 0 on success or a negative error code on failure 815 * to allocate kernel memory to copy to. 816 */ 817 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 818 { 819 int i; 820 int num_frags = skb_shinfo(skb)->nr_frags; 821 struct page *page, *head = NULL; 822 struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 823 824 for (i = 0; i < num_frags; i++) { 825 u8 *vaddr; 826 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 827 828 page = alloc_page(gfp_mask); 829 if (!page) { 830 while (head) { 831 struct page *next = (struct page *)page_private(head); 832 put_page(head); 833 head = next; 834 } 835 return -ENOMEM; 836 } 837 vaddr = kmap_atomic(skb_frag_page(f)); 838 memcpy(page_address(page), 839 vaddr + f->page_offset, skb_frag_size(f)); 840 kunmap_atomic(vaddr); 841 set_page_private(page, (unsigned long)head); 842 head = page; 843 } 844 845 /* skb frags release userspace buffers */ 846 for (i = 0; i < num_frags; i++) 847 skb_frag_unref(skb, i); 848 849 uarg->callback(uarg, false); 850 851 /* skb frags point to kernel buffers */ 852 for (i = num_frags - 1; i >= 0; i--) { 853 __skb_fill_page_desc(skb, i, head, 0, 854 skb_shinfo(skb)->frags[i].size); 855 head = (struct page *)page_private(head); 856 } 857 858 skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 859 return 0; 860 } 861 EXPORT_SYMBOL_GPL(skb_copy_ubufs); 862 863 /** 864 * skb_clone - duplicate an sk_buff 865 * @skb: buffer to clone 866 * @gfp_mask: allocation priority 867 * 868 * Duplicate an &sk_buff. The new one is not owned by a socket. Both 869 * copies share the same packet data but not structure. The new 870 * buffer has a reference count of 1. If the allocation fails the 871 * function returns %NULL otherwise the new buffer is returned. 872 * 873 * If this function is called from an interrupt gfp_mask() must be 874 * %GFP_ATOMIC. 875 */ 876 877 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 878 { 879 struct sk_buff_fclones *fclones = container_of(skb, 880 struct sk_buff_fclones, 881 skb1); 882 struct sk_buff *n = &fclones->skb2; 883 884 if (skb_orphan_frags(skb, gfp_mask)) 885 return NULL; 886 887 if (skb->fclone == SKB_FCLONE_ORIG && 888 n->fclone == SKB_FCLONE_FREE) { 889 n->fclone = SKB_FCLONE_CLONE; 890 /* As our fastclone was free, clone_ref must be 1 at this point. 891 * We could use atomic_inc() here, but it is faster 892 * to set the final value. 893 */ 894 atomic_set(&fclones->fclone_ref, 2); 895 } else { 896 if (skb_pfmemalloc(skb)) 897 gfp_mask |= __GFP_MEMALLOC; 898 899 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 900 if (!n) 901 return NULL; 902 903 kmemcheck_annotate_bitfield(n, flags1); 904 n->fclone = SKB_FCLONE_UNAVAILABLE; 905 } 906 907 return __skb_clone(n, skb); 908 } 909 EXPORT_SYMBOL(skb_clone); 910 911 static void skb_headers_offset_update(struct sk_buff *skb, int off) 912 { 913 /* Only adjust this if it actually is csum_start rather than csum */ 914 if (skb->ip_summed == CHECKSUM_PARTIAL) 915 skb->csum_start += off; 916 /* {transport,network,mac}_header and tail are relative to skb->head */ 917 skb->transport_header += off; 918 skb->network_header += off; 919 if (skb_mac_header_was_set(skb)) 920 skb->mac_header += off; 921 skb->inner_transport_header += off; 922 skb->inner_network_header += off; 923 skb->inner_mac_header += off; 924 } 925 926 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 927 { 928 __copy_skb_header(new, old); 929 930 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 931 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 932 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 933 } 934 935 static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 936 { 937 if (skb_pfmemalloc(skb)) 938 return SKB_ALLOC_RX; 939 return 0; 940 } 941 942 /** 943 * skb_copy - create private copy of an sk_buff 944 * @skb: buffer to copy 945 * @gfp_mask: allocation priority 946 * 947 * Make a copy of both an &sk_buff and its data. This is used when the 948 * caller wishes to modify the data and needs a private copy of the 949 * data to alter. Returns %NULL on failure or the pointer to the buffer 950 * on success. The returned buffer has a reference count of 1. 951 * 952 * As by-product this function converts non-linear &sk_buff to linear 953 * one, so that &sk_buff becomes completely private and caller is allowed 954 * to modify all the data of returned buffer. This means that this 955 * function is not recommended for use in circumstances when only 956 * header is going to be modified. Use pskb_copy() instead. 957 */ 958 959 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 960 { 961 int headerlen = skb_headroom(skb); 962 unsigned int size = skb_end_offset(skb) + skb->data_len; 963 struct sk_buff *n = __alloc_skb(size, gfp_mask, 964 skb_alloc_rx_flag(skb), NUMA_NO_NODE); 965 966 if (!n) 967 return NULL; 968 969 /* Set the data pointer */ 970 skb_reserve(n, headerlen); 971 /* Set the tail pointer and length */ 972 skb_put(n, skb->len); 973 974 if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 975 BUG(); 976 977 copy_skb_header(n, skb); 978 return n; 979 } 980 EXPORT_SYMBOL(skb_copy); 981 982 /** 983 * __pskb_copy_fclone - create copy of an sk_buff with private head. 984 * @skb: buffer to copy 985 * @headroom: headroom of new skb 986 * @gfp_mask: allocation priority 987 * @fclone: if true allocate the copy of the skb from the fclone 988 * cache instead of the head cache; it is recommended to set this 989 * to true for the cases where the copy will likely be cloned 990 * 991 * Make a copy of both an &sk_buff and part of its data, located 992 * in header. Fragmented data remain shared. This is used when 993 * the caller wishes to modify only header of &sk_buff and needs 994 * private copy of the header to alter. Returns %NULL on failure 995 * or the pointer to the buffer on success. 996 * The returned buffer has a reference count of 1. 997 */ 998 999 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1000 gfp_t gfp_mask, bool fclone) 1001 { 1002 unsigned int size = skb_headlen(skb) + headroom; 1003 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1004 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 1005 1006 if (!n) 1007 goto out; 1008 1009 /* Set the data pointer */ 1010 skb_reserve(n, headroom); 1011 /* Set the tail pointer and length */ 1012 skb_put(n, skb_headlen(skb)); 1013 /* Copy the bytes */ 1014 skb_copy_from_linear_data(skb, n->data, n->len); 1015 1016 n->truesize += skb->data_len; 1017 n->data_len = skb->data_len; 1018 n->len = skb->len; 1019 1020 if (skb_shinfo(skb)->nr_frags) { 1021 int i; 1022 1023 if (skb_orphan_frags(skb, gfp_mask)) { 1024 kfree_skb(n); 1025 n = NULL; 1026 goto out; 1027 } 1028 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1029 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1030 skb_frag_ref(skb, i); 1031 } 1032 skb_shinfo(n)->nr_frags = i; 1033 } 1034 1035 if (skb_has_frag_list(skb)) { 1036 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 1037 skb_clone_fraglist(n); 1038 } 1039 1040 copy_skb_header(n, skb); 1041 out: 1042 return n; 1043 } 1044 EXPORT_SYMBOL(__pskb_copy_fclone); 1045 1046 /** 1047 * pskb_expand_head - reallocate header of &sk_buff 1048 * @skb: buffer to reallocate 1049 * @nhead: room to add at head 1050 * @ntail: room to add at tail 1051 * @gfp_mask: allocation priority 1052 * 1053 * Expands (or creates identical copy, if @nhead and @ntail are zero) 1054 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 1055 * reference count of 1. Returns zero in the case of success or error, 1056 * if expansion failed. In the last case, &sk_buff is not changed. 1057 * 1058 * All the pointers pointing into skb header may change and must be 1059 * reloaded after call to this function. 1060 */ 1061 1062 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1063 gfp_t gfp_mask) 1064 { 1065 int i; 1066 u8 *data; 1067 int size = nhead + skb_end_offset(skb) + ntail; 1068 long off; 1069 1070 BUG_ON(nhead < 0); 1071 1072 if (skb_shared(skb)) 1073 BUG(); 1074 1075 size = SKB_DATA_ALIGN(size); 1076 1077 if (skb_pfmemalloc(skb)) 1078 gfp_mask |= __GFP_MEMALLOC; 1079 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1080 gfp_mask, NUMA_NO_NODE, NULL); 1081 if (!data) 1082 goto nodata; 1083 size = SKB_WITH_OVERHEAD(ksize(data)); 1084 1085 /* Copy only real data... and, alas, header. This should be 1086 * optimized for the cases when header is void. 1087 */ 1088 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 1089 1090 memcpy((struct skb_shared_info *)(data + size), 1091 skb_shinfo(skb), 1092 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 1093 1094 /* 1095 * if shinfo is shared we must drop the old head gracefully, but if it 1096 * is not we can just drop the old head and let the existing refcount 1097 * be since all we did is relocate the values 1098 */ 1099 if (skb_cloned(skb)) { 1100 /* copy this zero copy skb frags */ 1101 if (skb_orphan_frags(skb, gfp_mask)) 1102 goto nofrags; 1103 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1104 skb_frag_ref(skb, i); 1105 1106 if (skb_has_frag_list(skb)) 1107 skb_clone_fraglist(skb); 1108 1109 skb_release_data(skb); 1110 } else { 1111 skb_free_head(skb); 1112 } 1113 off = (data + nhead) - skb->head; 1114 1115 skb->head = data; 1116 skb->head_frag = 0; 1117 skb->data += off; 1118 #ifdef NET_SKBUFF_DATA_USES_OFFSET 1119 skb->end = size; 1120 off = nhead; 1121 #else 1122 skb->end = skb->head + size; 1123 #endif 1124 skb->tail += off; 1125 skb_headers_offset_update(skb, nhead); 1126 skb->cloned = 0; 1127 skb->hdr_len = 0; 1128 skb->nohdr = 0; 1129 atomic_set(&skb_shinfo(skb)->dataref, 1); 1130 return 0; 1131 1132 nofrags: 1133 kfree(data); 1134 nodata: 1135 return -ENOMEM; 1136 } 1137 EXPORT_SYMBOL(pskb_expand_head); 1138 1139 /* Make private copy of skb with writable head and some headroom */ 1140 1141 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 1142 { 1143 struct sk_buff *skb2; 1144 int delta = headroom - skb_headroom(skb); 1145 1146 if (delta <= 0) 1147 skb2 = pskb_copy(skb, GFP_ATOMIC); 1148 else { 1149 skb2 = skb_clone(skb, GFP_ATOMIC); 1150 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 1151 GFP_ATOMIC)) { 1152 kfree_skb(skb2); 1153 skb2 = NULL; 1154 } 1155 } 1156 return skb2; 1157 } 1158 EXPORT_SYMBOL(skb_realloc_headroom); 1159 1160 /** 1161 * skb_copy_expand - copy and expand sk_buff 1162 * @skb: buffer to copy 1163 * @newheadroom: new free bytes at head 1164 * @newtailroom: new free bytes at tail 1165 * @gfp_mask: allocation priority 1166 * 1167 * Make a copy of both an &sk_buff and its data and while doing so 1168 * allocate additional space. 1169 * 1170 * This is used when the caller wishes to modify the data and needs a 1171 * private copy of the data to alter as well as more space for new fields. 1172 * Returns %NULL on failure or the pointer to the buffer 1173 * on success. The returned buffer has a reference count of 1. 1174 * 1175 * You must pass %GFP_ATOMIC as the allocation priority if this function 1176 * is called from an interrupt. 1177 */ 1178 struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 1179 int newheadroom, int newtailroom, 1180 gfp_t gfp_mask) 1181 { 1182 /* 1183 * Allocate the copy buffer 1184 */ 1185 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1186 gfp_mask, skb_alloc_rx_flag(skb), 1187 NUMA_NO_NODE); 1188 int oldheadroom = skb_headroom(skb); 1189 int head_copy_len, head_copy_off; 1190 1191 if (!n) 1192 return NULL; 1193 1194 skb_reserve(n, newheadroom); 1195 1196 /* Set the tail pointer and length */ 1197 skb_put(n, skb->len); 1198 1199 head_copy_len = oldheadroom; 1200 head_copy_off = 0; 1201 if (newheadroom <= head_copy_len) 1202 head_copy_len = newheadroom; 1203 else 1204 head_copy_off = newheadroom - head_copy_len; 1205 1206 /* Copy the linear header and data. */ 1207 if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 1208 skb->len + head_copy_len)) 1209 BUG(); 1210 1211 copy_skb_header(n, skb); 1212 1213 skb_headers_offset_update(n, newheadroom - oldheadroom); 1214 1215 return n; 1216 } 1217 EXPORT_SYMBOL(skb_copy_expand); 1218 1219 /** 1220 * skb_pad - zero pad the tail of an skb 1221 * @skb: buffer to pad 1222 * @pad: space to pad 1223 * 1224 * Ensure that a buffer is followed by a padding area that is zero 1225 * filled. Used by network drivers which may DMA or transfer data 1226 * beyond the buffer end onto the wire. 1227 * 1228 * May return error in out of memory cases. The skb is freed on error. 1229 */ 1230 1231 int skb_pad(struct sk_buff *skb, int pad) 1232 { 1233 int err; 1234 int ntail; 1235 1236 /* If the skbuff is non linear tailroom is always zero.. */ 1237 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 1238 memset(skb->data+skb->len, 0, pad); 1239 return 0; 1240 } 1241 1242 ntail = skb->data_len + pad - (skb->end - skb->tail); 1243 if (likely(skb_cloned(skb) || ntail > 0)) { 1244 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 1245 if (unlikely(err)) 1246 goto free_skb; 1247 } 1248 1249 /* FIXME: The use of this function with non-linear skb's really needs 1250 * to be audited. 1251 */ 1252 err = skb_linearize(skb); 1253 if (unlikely(err)) 1254 goto free_skb; 1255 1256 memset(skb->data + skb->len, 0, pad); 1257 return 0; 1258 1259 free_skb: 1260 kfree_skb(skb); 1261 return err; 1262 } 1263 EXPORT_SYMBOL(skb_pad); 1264 1265 /** 1266 * pskb_put - add data to the tail of a potentially fragmented buffer 1267 * @skb: start of the buffer to use 1268 * @tail: tail fragment of the buffer to use 1269 * @len: amount of data to add 1270 * 1271 * This function extends the used data area of the potentially 1272 * fragmented buffer. @tail must be the last fragment of @skb -- or 1273 * @skb itself. If this would exceed the total buffer size the kernel 1274 * will panic. A pointer to the first byte of the extra data is 1275 * returned. 1276 */ 1277 1278 unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 1279 { 1280 if (tail != skb) { 1281 skb->data_len += len; 1282 skb->len += len; 1283 } 1284 return skb_put(tail, len); 1285 } 1286 EXPORT_SYMBOL_GPL(pskb_put); 1287 1288 /** 1289 * skb_put - add data to a buffer 1290 * @skb: buffer to use 1291 * @len: amount of data to add 1292 * 1293 * This function extends the used data area of the buffer. If this would 1294 * exceed the total buffer size the kernel will panic. A pointer to the 1295 * first byte of the extra data is returned. 1296 */ 1297 unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 1298 { 1299 unsigned char *tmp = skb_tail_pointer(skb); 1300 SKB_LINEAR_ASSERT(skb); 1301 skb->tail += len; 1302 skb->len += len; 1303 if (unlikely(skb->tail > skb->end)) 1304 skb_over_panic(skb, len, __builtin_return_address(0)); 1305 return tmp; 1306 } 1307 EXPORT_SYMBOL(skb_put); 1308 1309 /** 1310 * skb_push - add data to the start of a buffer 1311 * @skb: buffer to use 1312 * @len: amount of data to add 1313 * 1314 * This function extends the used data area of the buffer at the buffer 1315 * start. If this would exceed the total buffer headroom the kernel will 1316 * panic. A pointer to the first byte of the extra data is returned. 1317 */ 1318 unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1319 { 1320 skb->data -= len; 1321 skb->len += len; 1322 if (unlikely(skb->data<skb->head)) 1323 skb_under_panic(skb, len, __builtin_return_address(0)); 1324 return skb->data; 1325 } 1326 EXPORT_SYMBOL(skb_push); 1327 1328 /** 1329 * skb_pull - remove data from the start of a buffer 1330 * @skb: buffer to use 1331 * @len: amount of data to remove 1332 * 1333 * This function removes data from the start of a buffer, returning 1334 * the memory to the headroom. A pointer to the next data in the buffer 1335 * is returned. Once the data has been pulled future pushes will overwrite 1336 * the old data. 1337 */ 1338 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 1339 { 1340 return skb_pull_inline(skb, len); 1341 } 1342 EXPORT_SYMBOL(skb_pull); 1343 1344 /** 1345 * skb_trim - remove end from a buffer 1346 * @skb: buffer to alter 1347 * @len: new length 1348 * 1349 * Cut the length of a buffer down by removing data from the tail. If 1350 * the buffer is already under the length specified it is not modified. 1351 * The skb must be linear. 1352 */ 1353 void skb_trim(struct sk_buff *skb, unsigned int len) 1354 { 1355 if (skb->len > len) 1356 __skb_trim(skb, len); 1357 } 1358 EXPORT_SYMBOL(skb_trim); 1359 1360 /* Trims skb to length len. It can change skb pointers. 1361 */ 1362 1363 int ___pskb_trim(struct sk_buff *skb, unsigned int len) 1364 { 1365 struct sk_buff **fragp; 1366 struct sk_buff *frag; 1367 int offset = skb_headlen(skb); 1368 int nfrags = skb_shinfo(skb)->nr_frags; 1369 int i; 1370 int err; 1371 1372 if (skb_cloned(skb) && 1373 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 1374 return err; 1375 1376 i = 0; 1377 if (offset >= len) 1378 goto drop_pages; 1379 1380 for (; i < nfrags; i++) { 1381 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 1382 1383 if (end < len) { 1384 offset = end; 1385 continue; 1386 } 1387 1388 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 1389 1390 drop_pages: 1391 skb_shinfo(skb)->nr_frags = i; 1392 1393 for (; i < nfrags; i++) 1394 skb_frag_unref(skb, i); 1395 1396 if (skb_has_frag_list(skb)) 1397 skb_drop_fraglist(skb); 1398 goto done; 1399 } 1400 1401 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 1402 fragp = &frag->next) { 1403 int end = offset + frag->len; 1404 1405 if (skb_shared(frag)) { 1406 struct sk_buff *nfrag; 1407 1408 nfrag = skb_clone(frag, GFP_ATOMIC); 1409 if (unlikely(!nfrag)) 1410 return -ENOMEM; 1411 1412 nfrag->next = frag->next; 1413 consume_skb(frag); 1414 frag = nfrag; 1415 *fragp = frag; 1416 } 1417 1418 if (end < len) { 1419 offset = end; 1420 continue; 1421 } 1422 1423 if (end > len && 1424 unlikely((err = pskb_trim(frag, len - offset)))) 1425 return err; 1426 1427 if (frag->next) 1428 skb_drop_list(&frag->next); 1429 break; 1430 } 1431 1432 done: 1433 if (len > skb_headlen(skb)) { 1434 skb->data_len -= skb->len - len; 1435 skb->len = len; 1436 } else { 1437 skb->len = len; 1438 skb->data_len = 0; 1439 skb_set_tail_pointer(skb, len); 1440 } 1441 1442 return 0; 1443 } 1444 EXPORT_SYMBOL(___pskb_trim); 1445 1446 /** 1447 * __pskb_pull_tail - advance tail of skb header 1448 * @skb: buffer to reallocate 1449 * @delta: number of bytes to advance tail 1450 * 1451 * The function makes a sense only on a fragmented &sk_buff, 1452 * it expands header moving its tail forward and copying necessary 1453 * data from fragmented part. 1454 * 1455 * &sk_buff MUST have reference count of 1. 1456 * 1457 * Returns %NULL (and &sk_buff does not change) if pull failed 1458 * or value of new tail of skb in the case of success. 1459 * 1460 * All the pointers pointing into skb header may change and must be 1461 * reloaded after call to this function. 1462 */ 1463 1464 /* Moves tail of skb head forward, copying data from fragmented part, 1465 * when it is necessary. 1466 * 1. It may fail due to malloc failure. 1467 * 2. It may change skb pointers. 1468 * 1469 * It is pretty complicated. Luckily, it is called only in exceptional cases. 1470 */ 1471 unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 1472 { 1473 /* If skb has not enough free space at tail, get new one 1474 * plus 128 bytes for future expansions. If we have enough 1475 * room at tail, reallocate without expansion only if skb is cloned. 1476 */ 1477 int i, k, eat = (skb->tail + delta) - skb->end; 1478 1479 if (eat > 0 || skb_cloned(skb)) { 1480 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 1481 GFP_ATOMIC)) 1482 return NULL; 1483 } 1484 1485 if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 1486 BUG(); 1487 1488 /* Optimization: no fragments, no reasons to preestimate 1489 * size of pulled pages. Superb. 1490 */ 1491 if (!skb_has_frag_list(skb)) 1492 goto pull_pages; 1493 1494 /* Estimate size of pulled pages. */ 1495 eat = delta; 1496 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1497 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 1498 1499 if (size >= eat) 1500 goto pull_pages; 1501 eat -= size; 1502 } 1503 1504 /* If we need update frag list, we are in troubles. 1505 * Certainly, it possible to add an offset to skb data, 1506 * but taking into account that pulling is expected to 1507 * be very rare operation, it is worth to fight against 1508 * further bloating skb head and crucify ourselves here instead. 1509 * Pure masohism, indeed. 8)8) 1510 */ 1511 if (eat) { 1512 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1513 struct sk_buff *clone = NULL; 1514 struct sk_buff *insp = NULL; 1515 1516 do { 1517 BUG_ON(!list); 1518 1519 if (list->len <= eat) { 1520 /* Eaten as whole. */ 1521 eat -= list->len; 1522 list = list->next; 1523 insp = list; 1524 } else { 1525 /* Eaten partially. */ 1526 1527 if (skb_shared(list)) { 1528 /* Sucks! We need to fork list. :-( */ 1529 clone = skb_clone(list, GFP_ATOMIC); 1530 if (!clone) 1531 return NULL; 1532 insp = list->next; 1533 list = clone; 1534 } else { 1535 /* This may be pulled without 1536 * problems. */ 1537 insp = list; 1538 } 1539 if (!pskb_pull(list, eat)) { 1540 kfree_skb(clone); 1541 return NULL; 1542 } 1543 break; 1544 } 1545 } while (eat); 1546 1547 /* Free pulled out fragments. */ 1548 while ((list = skb_shinfo(skb)->frag_list) != insp) { 1549 skb_shinfo(skb)->frag_list = list->next; 1550 kfree_skb(list); 1551 } 1552 /* And insert new clone at head. */ 1553 if (clone) { 1554 clone->next = list; 1555 skb_shinfo(skb)->frag_list = clone; 1556 } 1557 } 1558 /* Success! Now we may commit changes to skb data. */ 1559 1560 pull_pages: 1561 eat = delta; 1562 k = 0; 1563 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1564 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 1565 1566 if (size <= eat) { 1567 skb_frag_unref(skb, i); 1568 eat -= size; 1569 } else { 1570 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 1571 if (eat) { 1572 skb_shinfo(skb)->frags[k].page_offset += eat; 1573 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 1574 eat = 0; 1575 } 1576 k++; 1577 } 1578 } 1579 skb_shinfo(skb)->nr_frags = k; 1580 1581 skb->tail += delta; 1582 skb->data_len -= delta; 1583 1584 return skb_tail_pointer(skb); 1585 } 1586 EXPORT_SYMBOL(__pskb_pull_tail); 1587 1588 /** 1589 * skb_copy_bits - copy bits from skb to kernel buffer 1590 * @skb: source skb 1591 * @offset: offset in source 1592 * @to: destination buffer 1593 * @len: number of bytes to copy 1594 * 1595 * Copy the specified number of bytes from the source skb to the 1596 * destination buffer. 1597 * 1598 * CAUTION ! : 1599 * If its prototype is ever changed, 1600 * check arch/{*}/net/{*}.S files, 1601 * since it is called from BPF assembly code. 1602 */ 1603 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 1604 { 1605 int start = skb_headlen(skb); 1606 struct sk_buff *frag_iter; 1607 int i, copy; 1608 1609 if (offset > (int)skb->len - len) 1610 goto fault; 1611 1612 /* Copy header. */ 1613 if ((copy = start - offset) > 0) { 1614 if (copy > len) 1615 copy = len; 1616 skb_copy_from_linear_data_offset(skb, offset, to, copy); 1617 if ((len -= copy) == 0) 1618 return 0; 1619 offset += copy; 1620 to += copy; 1621 } 1622 1623 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1624 int end; 1625 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 1626 1627 WARN_ON(start > offset + len); 1628 1629 end = start + skb_frag_size(f); 1630 if ((copy = end - offset) > 0) { 1631 u8 *vaddr; 1632 1633 if (copy > len) 1634 copy = len; 1635 1636 vaddr = kmap_atomic(skb_frag_page(f)); 1637 memcpy(to, 1638 vaddr + f->page_offset + offset - start, 1639 copy); 1640 kunmap_atomic(vaddr); 1641 1642 if ((len -= copy) == 0) 1643 return 0; 1644 offset += copy; 1645 to += copy; 1646 } 1647 start = end; 1648 } 1649 1650 skb_walk_frags(skb, frag_iter) { 1651 int end; 1652 1653 WARN_ON(start > offset + len); 1654 1655 end = start + frag_iter->len; 1656 if ((copy = end - offset) > 0) { 1657 if (copy > len) 1658 copy = len; 1659 if (skb_copy_bits(frag_iter, offset - start, to, copy)) 1660 goto fault; 1661 if ((len -= copy) == 0) 1662 return 0; 1663 offset += copy; 1664 to += copy; 1665 } 1666 start = end; 1667 } 1668 1669 if (!len) 1670 return 0; 1671 1672 fault: 1673 return -EFAULT; 1674 } 1675 EXPORT_SYMBOL(skb_copy_bits); 1676 1677 /* 1678 * Callback from splice_to_pipe(), if we need to release some pages 1679 * at the end of the spd in case we error'ed out in filling the pipe. 1680 */ 1681 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 1682 { 1683 put_page(spd->pages[i]); 1684 } 1685 1686 static struct page *linear_to_page(struct page *page, unsigned int *len, 1687 unsigned int *offset, 1688 struct sock *sk) 1689 { 1690 struct page_frag *pfrag = sk_page_frag(sk); 1691 1692 if (!sk_page_frag_refill(sk, pfrag)) 1693 return NULL; 1694 1695 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 1696 1697 memcpy(page_address(pfrag->page) + pfrag->offset, 1698 page_address(page) + *offset, *len); 1699 *offset = pfrag->offset; 1700 pfrag->offset += *len; 1701 1702 return pfrag->page; 1703 } 1704 1705 static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 1706 struct page *page, 1707 unsigned int offset) 1708 { 1709 return spd->nr_pages && 1710 spd->pages[spd->nr_pages - 1] == page && 1711 (spd->partial[spd->nr_pages - 1].offset + 1712 spd->partial[spd->nr_pages - 1].len == offset); 1713 } 1714 1715 /* 1716 * Fill page/offset/length into spd, if it can hold more pages. 1717 */ 1718 static bool spd_fill_page(struct splice_pipe_desc *spd, 1719 struct pipe_inode_info *pipe, struct page *page, 1720 unsigned int *len, unsigned int offset, 1721 bool linear, 1722 struct sock *sk) 1723 { 1724 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1725 return true; 1726 1727 if (linear) { 1728 page = linear_to_page(page, len, &offset, sk); 1729 if (!page) 1730 return true; 1731 } 1732 if (spd_can_coalesce(spd, page, offset)) { 1733 spd->partial[spd->nr_pages - 1].len += *len; 1734 return false; 1735 } 1736 get_page(page); 1737 spd->pages[spd->nr_pages] = page; 1738 spd->partial[spd->nr_pages].len = *len; 1739 spd->partial[spd->nr_pages].offset = offset; 1740 spd->nr_pages++; 1741 1742 return false; 1743 } 1744 1745 static bool __splice_segment(struct page *page, unsigned int poff, 1746 unsigned int plen, unsigned int *off, 1747 unsigned int *len, 1748 struct splice_pipe_desc *spd, bool linear, 1749 struct sock *sk, 1750 struct pipe_inode_info *pipe) 1751 { 1752 if (!*len) 1753 return true; 1754 1755 /* skip this segment if already processed */ 1756 if (*off >= plen) { 1757 *off -= plen; 1758 return false; 1759 } 1760 1761 /* ignore any bits we already processed */ 1762 poff += *off; 1763 plen -= *off; 1764 *off = 0; 1765 1766 do { 1767 unsigned int flen = min(*len, plen); 1768 1769 if (spd_fill_page(spd, pipe, page, &flen, poff, 1770 linear, sk)) 1771 return true; 1772 poff += flen; 1773 plen -= flen; 1774 *len -= flen; 1775 } while (*len && plen); 1776 1777 return false; 1778 } 1779 1780 /* 1781 * Map linear and fragment data from the skb to spd. It reports true if the 1782 * pipe is full or if we already spliced the requested length. 1783 */ 1784 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 1785 unsigned int *offset, unsigned int *len, 1786 struct splice_pipe_desc *spd, struct sock *sk) 1787 { 1788 int seg; 1789 1790 /* map the linear part : 1791 * If skb->head_frag is set, this 'linear' part is backed by a 1792 * fragment, and if the head is not shared with any clones then 1793 * we can avoid a copy since we own the head portion of this page. 1794 */ 1795 if (__splice_segment(virt_to_page(skb->data), 1796 (unsigned long) skb->data & (PAGE_SIZE - 1), 1797 skb_headlen(skb), 1798 offset, len, spd, 1799 skb_head_is_locked(skb), 1800 sk, pipe)) 1801 return true; 1802 1803 /* 1804 * then map the fragments 1805 */ 1806 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 1807 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 1808 1809 if (__splice_segment(skb_frag_page(f), 1810 f->page_offset, skb_frag_size(f), 1811 offset, len, spd, false, sk, pipe)) 1812 return true; 1813 } 1814 1815 return false; 1816 } 1817 1818 /* 1819 * Map data from the skb to a pipe. Should handle both the linear part, 1820 * the fragments, and the frag list. It does NOT handle frag lists within 1821 * the frag list, if such a thing exists. We'd probably need to recurse to 1822 * handle that cleanly. 1823 */ 1824 int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 1825 struct pipe_inode_info *pipe, unsigned int tlen, 1826 unsigned int flags) 1827 { 1828 struct partial_page partial[MAX_SKB_FRAGS]; 1829 struct page *pages[MAX_SKB_FRAGS]; 1830 struct splice_pipe_desc spd = { 1831 .pages = pages, 1832 .partial = partial, 1833 .nr_pages_max = MAX_SKB_FRAGS, 1834 .flags = flags, 1835 .ops = &nosteal_pipe_buf_ops, 1836 .spd_release = sock_spd_release, 1837 }; 1838 struct sk_buff *frag_iter; 1839 struct sock *sk = skb->sk; 1840 int ret = 0; 1841 1842 /* 1843 * __skb_splice_bits() only fails if the output has no room left, 1844 * so no point in going over the frag_list for the error case. 1845 */ 1846 if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 1847 goto done; 1848 else if (!tlen) 1849 goto done; 1850 1851 /* 1852 * now see if we have a frag_list to map 1853 */ 1854 skb_walk_frags(skb, frag_iter) { 1855 if (!tlen) 1856 break; 1857 if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1858 break; 1859 } 1860 1861 done: 1862 if (spd.nr_pages) { 1863 /* 1864 * Drop the socket lock, otherwise we have reverse 1865 * locking dependencies between sk_lock and i_mutex 1866 * here as compared to sendfile(). We enter here 1867 * with the socket lock held, and splice_to_pipe() will 1868 * grab the pipe inode lock. For sendfile() emulation, 1869 * we call into ->sendpage() with the i_mutex lock held 1870 * and networking will grab the socket lock. 1871 */ 1872 release_sock(sk); 1873 ret = splice_to_pipe(pipe, &spd); 1874 lock_sock(sk); 1875 } 1876 1877 return ret; 1878 } 1879 1880 /** 1881 * skb_store_bits - store bits from kernel buffer to skb 1882 * @skb: destination buffer 1883 * @offset: offset in destination 1884 * @from: source buffer 1885 * @len: number of bytes to copy 1886 * 1887 * Copy the specified number of bytes from the source buffer to the 1888 * destination skb. This function handles all the messy bits of 1889 * traversing fragment lists and such. 1890 */ 1891 1892 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1893 { 1894 int start = skb_headlen(skb); 1895 struct sk_buff *frag_iter; 1896 int i, copy; 1897 1898 if (offset > (int)skb->len - len) 1899 goto fault; 1900 1901 if ((copy = start - offset) > 0) { 1902 if (copy > len) 1903 copy = len; 1904 skb_copy_to_linear_data_offset(skb, offset, from, copy); 1905 if ((len -= copy) == 0) 1906 return 0; 1907 offset += copy; 1908 from += copy; 1909 } 1910 1911 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1912 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1913 int end; 1914 1915 WARN_ON(start > offset + len); 1916 1917 end = start + skb_frag_size(frag); 1918 if ((copy = end - offset) > 0) { 1919 u8 *vaddr; 1920 1921 if (copy > len) 1922 copy = len; 1923 1924 vaddr = kmap_atomic(skb_frag_page(frag)); 1925 memcpy(vaddr + frag->page_offset + offset - start, 1926 from, copy); 1927 kunmap_atomic(vaddr); 1928 1929 if ((len -= copy) == 0) 1930 return 0; 1931 offset += copy; 1932 from += copy; 1933 } 1934 start = end; 1935 } 1936 1937 skb_walk_frags(skb, frag_iter) { 1938 int end; 1939 1940 WARN_ON(start > offset + len); 1941 1942 end = start + frag_iter->len; 1943 if ((copy = end - offset) > 0) { 1944 if (copy > len) 1945 copy = len; 1946 if (skb_store_bits(frag_iter, offset - start, 1947 from, copy)) 1948 goto fault; 1949 if ((len -= copy) == 0) 1950 return 0; 1951 offset += copy; 1952 from += copy; 1953 } 1954 start = end; 1955 } 1956 if (!len) 1957 return 0; 1958 1959 fault: 1960 return -EFAULT; 1961 } 1962 EXPORT_SYMBOL(skb_store_bits); 1963 1964 /* Checksum skb data. */ 1965 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 1966 __wsum csum, const struct skb_checksum_ops *ops) 1967 { 1968 int start = skb_headlen(skb); 1969 int i, copy = start - offset; 1970 struct sk_buff *frag_iter; 1971 int pos = 0; 1972 1973 /* Checksum header. */ 1974 if (copy > 0) { 1975 if (copy > len) 1976 copy = len; 1977 csum = ops->update(skb->data + offset, copy, csum); 1978 if ((len -= copy) == 0) 1979 return csum; 1980 offset += copy; 1981 pos = copy; 1982 } 1983 1984 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1985 int end; 1986 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1987 1988 WARN_ON(start > offset + len); 1989 1990 end = start + skb_frag_size(frag); 1991 if ((copy = end - offset) > 0) { 1992 __wsum csum2; 1993 u8 *vaddr; 1994 1995 if (copy > len) 1996 copy = len; 1997 vaddr = kmap_atomic(skb_frag_page(frag)); 1998 csum2 = ops->update(vaddr + frag->page_offset + 1999 offset - start, copy, 0); 2000 kunmap_atomic(vaddr); 2001 csum = ops->combine(csum, csum2, pos, copy); 2002 if (!(len -= copy)) 2003 return csum; 2004 offset += copy; 2005 pos += copy; 2006 } 2007 start = end; 2008 } 2009 2010 skb_walk_frags(skb, frag_iter) { 2011 int end; 2012 2013 WARN_ON(start > offset + len); 2014 2015 end = start + frag_iter->len; 2016 if ((copy = end - offset) > 0) { 2017 __wsum csum2; 2018 if (copy > len) 2019 copy = len; 2020 csum2 = __skb_checksum(frag_iter, offset - start, 2021 copy, 0, ops); 2022 csum = ops->combine(csum, csum2, pos, copy); 2023 if ((len -= copy) == 0) 2024 return csum; 2025 offset += copy; 2026 pos += copy; 2027 } 2028 start = end; 2029 } 2030 BUG_ON(len); 2031 2032 return csum; 2033 } 2034 EXPORT_SYMBOL(__skb_checksum); 2035 2036 __wsum skb_checksum(const struct sk_buff *skb, int offset, 2037 int len, __wsum csum) 2038 { 2039 const struct skb_checksum_ops ops = { 2040 .update = csum_partial_ext, 2041 .combine = csum_block_add_ext, 2042 }; 2043 2044 return __skb_checksum(skb, offset, len, csum, &ops); 2045 } 2046 EXPORT_SYMBOL(skb_checksum); 2047 2048 /* Both of above in one bottle. */ 2049 2050 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 2051 u8 *to, int len, __wsum csum) 2052 { 2053 int start = skb_headlen(skb); 2054 int i, copy = start - offset; 2055 struct sk_buff *frag_iter; 2056 int pos = 0; 2057 2058 /* Copy header. */ 2059 if (copy > 0) { 2060 if (copy > len) 2061 copy = len; 2062 csum = csum_partial_copy_nocheck(skb->data + offset, to, 2063 copy, csum); 2064 if ((len -= copy) == 0) 2065 return csum; 2066 offset += copy; 2067 to += copy; 2068 pos = copy; 2069 } 2070 2071 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2072 int end; 2073 2074 WARN_ON(start > offset + len); 2075 2076 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 2077 if ((copy = end - offset) > 0) { 2078 __wsum csum2; 2079 u8 *vaddr; 2080 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2081 2082 if (copy > len) 2083 copy = len; 2084 vaddr = kmap_atomic(skb_frag_page(frag)); 2085 csum2 = csum_partial_copy_nocheck(vaddr + 2086 frag->page_offset + 2087 offset - start, to, 2088 copy, 0); 2089 kunmap_atomic(vaddr); 2090 csum = csum_block_add(csum, csum2, pos); 2091 if (!(len -= copy)) 2092 return csum; 2093 offset += copy; 2094 to += copy; 2095 pos += copy; 2096 } 2097 start = end; 2098 } 2099 2100 skb_walk_frags(skb, frag_iter) { 2101 __wsum csum2; 2102 int end; 2103 2104 WARN_ON(start > offset + len); 2105 2106 end = start + frag_iter->len; 2107 if ((copy = end - offset) > 0) { 2108 if (copy > len) 2109 copy = len; 2110 csum2 = skb_copy_and_csum_bits(frag_iter, 2111 offset - start, 2112 to, copy, 0); 2113 csum = csum_block_add(csum, csum2, pos); 2114 if ((len -= copy) == 0) 2115 return csum; 2116 offset += copy; 2117 to += copy; 2118 pos += copy; 2119 } 2120 start = end; 2121 } 2122 BUG_ON(len); 2123 return csum; 2124 } 2125 EXPORT_SYMBOL(skb_copy_and_csum_bits); 2126 2127 /** 2128 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2129 * @from: source buffer 2130 * 2131 * Calculates the amount of linear headroom needed in the 'to' skb passed 2132 * into skb_zerocopy(). 2133 */ 2134 unsigned int 2135 skb_zerocopy_headlen(const struct sk_buff *from) 2136 { 2137 unsigned int hlen = 0; 2138 2139 if (!from->head_frag || 2140 skb_headlen(from) < L1_CACHE_BYTES || 2141 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2142 hlen = skb_headlen(from); 2143 2144 if (skb_has_frag_list(from)) 2145 hlen = from->len; 2146 2147 return hlen; 2148 } 2149 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2150 2151 /** 2152 * skb_zerocopy - Zero copy skb to skb 2153 * @to: destination buffer 2154 * @from: source buffer 2155 * @len: number of bytes to copy from source buffer 2156 * @hlen: size of linear headroom in destination buffer 2157 * 2158 * Copies up to `len` bytes from `from` to `to` by creating references 2159 * to the frags in the source buffer. 2160 * 2161 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2162 * headroom in the `to` buffer. 2163 * 2164 * Return value: 2165 * 0: everything is OK 2166 * -ENOMEM: couldn't orphan frags of @from due to lack of memory 2167 * -EFAULT: skb_copy_bits() found some problem with skb geometry 2168 */ 2169 int 2170 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2171 { 2172 int i, j = 0; 2173 int plen = 0; /* length of skb->head fragment */ 2174 int ret; 2175 struct page *page; 2176 unsigned int offset; 2177 2178 BUG_ON(!from->head_frag && !hlen); 2179 2180 /* dont bother with small payloads */ 2181 if (len <= skb_tailroom(to)) 2182 return skb_copy_bits(from, 0, skb_put(to, len), len); 2183 2184 if (hlen) { 2185 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 2186 if (unlikely(ret)) 2187 return ret; 2188 len -= hlen; 2189 } else { 2190 plen = min_t(int, skb_headlen(from), len); 2191 if (plen) { 2192 page = virt_to_head_page(from->head); 2193 offset = from->data - (unsigned char *)page_address(page); 2194 __skb_fill_page_desc(to, 0, page, offset, plen); 2195 get_page(page); 2196 j = 1; 2197 len -= plen; 2198 } 2199 } 2200 2201 to->truesize += len + plen; 2202 to->len += len + plen; 2203 to->data_len += len + plen; 2204 2205 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 2206 skb_tx_error(from); 2207 return -ENOMEM; 2208 } 2209 2210 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2211 if (!len) 2212 break; 2213 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2214 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2215 len -= skb_shinfo(to)->frags[j].size; 2216 skb_frag_ref(to, j); 2217 j++; 2218 } 2219 skb_shinfo(to)->nr_frags = j; 2220 2221 return 0; 2222 } 2223 EXPORT_SYMBOL_GPL(skb_zerocopy); 2224 2225 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 2226 { 2227 __wsum csum; 2228 long csstart; 2229 2230 if (skb->ip_summed == CHECKSUM_PARTIAL) 2231 csstart = skb_checksum_start_offset(skb); 2232 else 2233 csstart = skb_headlen(skb); 2234 2235 BUG_ON(csstart > skb_headlen(skb)); 2236 2237 skb_copy_from_linear_data(skb, to, csstart); 2238 2239 csum = 0; 2240 if (csstart != skb->len) 2241 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 2242 skb->len - csstart, 0); 2243 2244 if (skb->ip_summed == CHECKSUM_PARTIAL) { 2245 long csstuff = csstart + skb->csum_offset; 2246 2247 *((__sum16 *)(to + csstuff)) = csum_fold(csum); 2248 } 2249 } 2250 EXPORT_SYMBOL(skb_copy_and_csum_dev); 2251 2252 /** 2253 * skb_dequeue - remove from the head of the queue 2254 * @list: list to dequeue from 2255 * 2256 * Remove the head of the list. The list lock is taken so the function 2257 * may be used safely with other locking list functions. The head item is 2258 * returned or %NULL if the list is empty. 2259 */ 2260 2261 struct sk_buff *skb_dequeue(struct sk_buff_head *list) 2262 { 2263 unsigned long flags; 2264 struct sk_buff *result; 2265 2266 spin_lock_irqsave(&list->lock, flags); 2267 result = __skb_dequeue(list); 2268 spin_unlock_irqrestore(&list->lock, flags); 2269 return result; 2270 } 2271 EXPORT_SYMBOL(skb_dequeue); 2272 2273 /** 2274 * skb_dequeue_tail - remove from the tail of the queue 2275 * @list: list to dequeue from 2276 * 2277 * Remove the tail of the list. The list lock is taken so the function 2278 * may be used safely with other locking list functions. The tail item is 2279 * returned or %NULL if the list is empty. 2280 */ 2281 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 2282 { 2283 unsigned long flags; 2284 struct sk_buff *result; 2285 2286 spin_lock_irqsave(&list->lock, flags); 2287 result = __skb_dequeue_tail(list); 2288 spin_unlock_irqrestore(&list->lock, flags); 2289 return result; 2290 } 2291 EXPORT_SYMBOL(skb_dequeue_tail); 2292 2293 /** 2294 * skb_queue_purge - empty a list 2295 * @list: list to empty 2296 * 2297 * Delete all buffers on an &sk_buff list. Each buffer is removed from 2298 * the list and one reference dropped. This function takes the list 2299 * lock and is atomic with respect to other list locking functions. 2300 */ 2301 void skb_queue_purge(struct sk_buff_head *list) 2302 { 2303 struct sk_buff *skb; 2304 while ((skb = skb_dequeue(list)) != NULL) 2305 kfree_skb(skb); 2306 } 2307 EXPORT_SYMBOL(skb_queue_purge); 2308 2309 /** 2310 * skb_queue_head - queue a buffer at the list head 2311 * @list: list to use 2312 * @newsk: buffer to queue 2313 * 2314 * Queue a buffer at the start of the list. This function takes the 2315 * list lock and can be used safely with other locking &sk_buff functions 2316 * safely. 2317 * 2318 * A buffer cannot be placed on two lists at the same time. 2319 */ 2320 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 2321 { 2322 unsigned long flags; 2323 2324 spin_lock_irqsave(&list->lock, flags); 2325 __skb_queue_head(list, newsk); 2326 spin_unlock_irqrestore(&list->lock, flags); 2327 } 2328 EXPORT_SYMBOL(skb_queue_head); 2329 2330 /** 2331 * skb_queue_tail - queue a buffer at the list tail 2332 * @list: list to use 2333 * @newsk: buffer to queue 2334 * 2335 * Queue a buffer at the tail of the list. This function takes the 2336 * list lock and can be used safely with other locking &sk_buff functions 2337 * safely. 2338 * 2339 * A buffer cannot be placed on two lists at the same time. 2340 */ 2341 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 2342 { 2343 unsigned long flags; 2344 2345 spin_lock_irqsave(&list->lock, flags); 2346 __skb_queue_tail(list, newsk); 2347 spin_unlock_irqrestore(&list->lock, flags); 2348 } 2349 EXPORT_SYMBOL(skb_queue_tail); 2350 2351 /** 2352 * skb_unlink - remove a buffer from a list 2353 * @skb: buffer to remove 2354 * @list: list to use 2355 * 2356 * Remove a packet from a list. The list locks are taken and this 2357 * function is atomic with respect to other list locked calls 2358 * 2359 * You must know what list the SKB is on. 2360 */ 2361 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 2362 { 2363 unsigned long flags; 2364 2365 spin_lock_irqsave(&list->lock, flags); 2366 __skb_unlink(skb, list); 2367 spin_unlock_irqrestore(&list->lock, flags); 2368 } 2369 EXPORT_SYMBOL(skb_unlink); 2370 2371 /** 2372 * skb_append - append a buffer 2373 * @old: buffer to insert after 2374 * @newsk: buffer to insert 2375 * @list: list to use 2376 * 2377 * Place a packet after a given packet in a list. The list locks are taken 2378 * and this function is atomic with respect to other list locked calls. 2379 * A buffer cannot be placed on two lists at the same time. 2380 */ 2381 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 2382 { 2383 unsigned long flags; 2384 2385 spin_lock_irqsave(&list->lock, flags); 2386 __skb_queue_after(list, old, newsk); 2387 spin_unlock_irqrestore(&list->lock, flags); 2388 } 2389 EXPORT_SYMBOL(skb_append); 2390 2391 /** 2392 * skb_insert - insert a buffer 2393 * @old: buffer to insert before 2394 * @newsk: buffer to insert 2395 * @list: list to use 2396 * 2397 * Place a packet before a given packet in a list. The list locks are 2398 * taken and this function is atomic with respect to other list locked 2399 * calls. 2400 * 2401 * A buffer cannot be placed on two lists at the same time. 2402 */ 2403 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 2404 { 2405 unsigned long flags; 2406 2407 spin_lock_irqsave(&list->lock, flags); 2408 __skb_insert(newsk, old->prev, old, list); 2409 spin_unlock_irqrestore(&list->lock, flags); 2410 } 2411 EXPORT_SYMBOL(skb_insert); 2412 2413 static inline void skb_split_inside_header(struct sk_buff *skb, 2414 struct sk_buff* skb1, 2415 const u32 len, const int pos) 2416 { 2417 int i; 2418 2419 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2420 pos - len); 2421 /* And move data appendix as is. */ 2422 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2423 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 2424 2425 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 2426 skb_shinfo(skb)->nr_frags = 0; 2427 skb1->data_len = skb->data_len; 2428 skb1->len += skb1->data_len; 2429 skb->data_len = 0; 2430 skb->len = len; 2431 skb_set_tail_pointer(skb, len); 2432 } 2433 2434 static inline void skb_split_no_header(struct sk_buff *skb, 2435 struct sk_buff* skb1, 2436 const u32 len, int pos) 2437 { 2438 int i, k = 0; 2439 const int nfrags = skb_shinfo(skb)->nr_frags; 2440 2441 skb_shinfo(skb)->nr_frags = 0; 2442 skb1->len = skb1->data_len = skb->len - len; 2443 skb->len = len; 2444 skb->data_len = len - pos; 2445 2446 for (i = 0; i < nfrags; i++) { 2447 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 2448 2449 if (pos + size > len) { 2450 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 2451 2452 if (pos < len) { 2453 /* Split frag. 2454 * We have two variants in this case: 2455 * 1. Move all the frag to the second 2456 * part, if it is possible. F.e. 2457 * this approach is mandatory for TUX, 2458 * where splitting is expensive. 2459 * 2. Split is accurately. We make this. 2460 */ 2461 skb_frag_ref(skb, i); 2462 skb_shinfo(skb1)->frags[0].page_offset += len - pos; 2463 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 2464 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 2465 skb_shinfo(skb)->nr_frags++; 2466 } 2467 k++; 2468 } else 2469 skb_shinfo(skb)->nr_frags++; 2470 pos += size; 2471 } 2472 skb_shinfo(skb1)->nr_frags = k; 2473 } 2474 2475 /** 2476 * skb_split - Split fragmented skb to two parts at length len. 2477 * @skb: the buffer to split 2478 * @skb1: the buffer to receive the second part 2479 * @len: new length for skb 2480 */ 2481 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 2482 { 2483 int pos = skb_headlen(skb); 2484 2485 skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 2486 if (len < pos) /* Split line is inside header. */ 2487 skb_split_inside_header(skb, skb1, len, pos); 2488 else /* Second chunk has no header, nothing to copy. */ 2489 skb_split_no_header(skb, skb1, len, pos); 2490 } 2491 EXPORT_SYMBOL(skb_split); 2492 2493 /* Shifting from/to a cloned skb is a no-go. 2494 * 2495 * Caller cannot keep skb_shinfo related pointers past calling here! 2496 */ 2497 static int skb_prepare_for_shift(struct sk_buff *skb) 2498 { 2499 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2500 } 2501 2502 /** 2503 * skb_shift - Shifts paged data partially from skb to another 2504 * @tgt: buffer into which tail data gets added 2505 * @skb: buffer from which the paged data comes from 2506 * @shiftlen: shift up to this many bytes 2507 * 2508 * Attempts to shift up to shiftlen worth of bytes, which may be less than 2509 * the length of the skb, from skb to tgt. Returns number bytes shifted. 2510 * It's up to caller to free skb if everything was shifted. 2511 * 2512 * If @tgt runs out of frags, the whole operation is aborted. 2513 * 2514 * Skb cannot include anything else but paged data while tgt is allowed 2515 * to have non-paged data as well. 2516 * 2517 * TODO: full sized shift could be optimized but that would need 2518 * specialized skb free'er to handle frags without up-to-date nr_frags. 2519 */ 2520 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2521 { 2522 int from, to, merge, todo; 2523 struct skb_frag_struct *fragfrom, *fragto; 2524 2525 BUG_ON(shiftlen > skb->len); 2526 BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2527 2528 todo = shiftlen; 2529 from = 0; 2530 to = skb_shinfo(tgt)->nr_frags; 2531 fragfrom = &skb_shinfo(skb)->frags[from]; 2532 2533 /* Actual merge is delayed until the point when we know we can 2534 * commit all, so that we don't have to undo partial changes 2535 */ 2536 if (!to || 2537 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2538 fragfrom->page_offset)) { 2539 merge = -1; 2540 } else { 2541 merge = to - 1; 2542 2543 todo -= skb_frag_size(fragfrom); 2544 if (todo < 0) { 2545 if (skb_prepare_for_shift(skb) || 2546 skb_prepare_for_shift(tgt)) 2547 return 0; 2548 2549 /* All previous frag pointers might be stale! */ 2550 fragfrom = &skb_shinfo(skb)->frags[from]; 2551 fragto = &skb_shinfo(tgt)->frags[merge]; 2552 2553 skb_frag_size_add(fragto, shiftlen); 2554 skb_frag_size_sub(fragfrom, shiftlen); 2555 fragfrom->page_offset += shiftlen; 2556 2557 goto onlymerged; 2558 } 2559 2560 from++; 2561 } 2562 2563 /* Skip full, not-fitting skb to avoid expensive operations */ 2564 if ((shiftlen == skb->len) && 2565 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2566 return 0; 2567 2568 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2569 return 0; 2570 2571 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2572 if (to == MAX_SKB_FRAGS) 2573 return 0; 2574 2575 fragfrom = &skb_shinfo(skb)->frags[from]; 2576 fragto = &skb_shinfo(tgt)->frags[to]; 2577 2578 if (todo >= skb_frag_size(fragfrom)) { 2579 *fragto = *fragfrom; 2580 todo -= skb_frag_size(fragfrom); 2581 from++; 2582 to++; 2583 2584 } else { 2585 __skb_frag_ref(fragfrom); 2586 fragto->page = fragfrom->page; 2587 fragto->page_offset = fragfrom->page_offset; 2588 skb_frag_size_set(fragto, todo); 2589 2590 fragfrom->page_offset += todo; 2591 skb_frag_size_sub(fragfrom, todo); 2592 todo = 0; 2593 2594 to++; 2595 break; 2596 } 2597 } 2598 2599 /* Ready to "commit" this state change to tgt */ 2600 skb_shinfo(tgt)->nr_frags = to; 2601 2602 if (merge >= 0) { 2603 fragfrom = &skb_shinfo(skb)->frags[0]; 2604 fragto = &skb_shinfo(tgt)->frags[merge]; 2605 2606 skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2607 __skb_frag_unref(fragfrom); 2608 } 2609 2610 /* Reposition in the original skb */ 2611 to = 0; 2612 while (from < skb_shinfo(skb)->nr_frags) 2613 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2614 skb_shinfo(skb)->nr_frags = to; 2615 2616 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2617 2618 onlymerged: 2619 /* Most likely the tgt won't ever need its checksum anymore, skb on 2620 * the other hand might need it if it needs to be resent 2621 */ 2622 tgt->ip_summed = CHECKSUM_PARTIAL; 2623 skb->ip_summed = CHECKSUM_PARTIAL; 2624 2625 /* Yak, is it really working this way? Some helper please? */ 2626 skb->len -= shiftlen; 2627 skb->data_len -= shiftlen; 2628 skb->truesize -= shiftlen; 2629 tgt->len += shiftlen; 2630 tgt->data_len += shiftlen; 2631 tgt->truesize += shiftlen; 2632 2633 return shiftlen; 2634 } 2635 2636 /** 2637 * skb_prepare_seq_read - Prepare a sequential read of skb data 2638 * @skb: the buffer to read 2639 * @from: lower offset of data to be read 2640 * @to: upper offset of data to be read 2641 * @st: state variable 2642 * 2643 * Initializes the specified state variable. Must be called before 2644 * invoking skb_seq_read() for the first time. 2645 */ 2646 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2647 unsigned int to, struct skb_seq_state *st) 2648 { 2649 st->lower_offset = from; 2650 st->upper_offset = to; 2651 st->root_skb = st->cur_skb = skb; 2652 st->frag_idx = st->stepped_offset = 0; 2653 st->frag_data = NULL; 2654 } 2655 EXPORT_SYMBOL(skb_prepare_seq_read); 2656 2657 /** 2658 * skb_seq_read - Sequentially read skb data 2659 * @consumed: number of bytes consumed by the caller so far 2660 * @data: destination pointer for data to be returned 2661 * @st: state variable 2662 * 2663 * Reads a block of skb data at @consumed relative to the 2664 * lower offset specified to skb_prepare_seq_read(). Assigns 2665 * the head of the data block to @data and returns the length 2666 * of the block or 0 if the end of the skb data or the upper 2667 * offset has been reached. 2668 * 2669 * The caller is not required to consume all of the data 2670 * returned, i.e. @consumed is typically set to the number 2671 * of bytes already consumed and the next call to 2672 * skb_seq_read() will return the remaining part of the block. 2673 * 2674 * Note 1: The size of each block of data returned can be arbitrary, 2675 * this limitation is the cost for zerocopy sequential 2676 * reads of potentially non linear data. 2677 * 2678 * Note 2: Fragment lists within fragments are not implemented 2679 * at the moment, state->root_skb could be replaced with 2680 * a stack for this purpose. 2681 */ 2682 unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2683 struct skb_seq_state *st) 2684 { 2685 unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2686 skb_frag_t *frag; 2687 2688 if (unlikely(abs_offset >= st->upper_offset)) { 2689 if (st->frag_data) { 2690 kunmap_atomic(st->frag_data); 2691 st->frag_data = NULL; 2692 } 2693 return 0; 2694 } 2695 2696 next_skb: 2697 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2698 2699 if (abs_offset < block_limit && !st->frag_data) { 2700 *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2701 return block_limit - abs_offset; 2702 } 2703 2704 if (st->frag_idx == 0 && !st->frag_data) 2705 st->stepped_offset += skb_headlen(st->cur_skb); 2706 2707 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2708 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 2709 block_limit = skb_frag_size(frag) + st->stepped_offset; 2710 2711 if (abs_offset < block_limit) { 2712 if (!st->frag_data) 2713 st->frag_data = kmap_atomic(skb_frag_page(frag)); 2714 2715 *data = (u8 *) st->frag_data + frag->page_offset + 2716 (abs_offset - st->stepped_offset); 2717 2718 return block_limit - abs_offset; 2719 } 2720 2721 if (st->frag_data) { 2722 kunmap_atomic(st->frag_data); 2723 st->frag_data = NULL; 2724 } 2725 2726 st->frag_idx++; 2727 st->stepped_offset += skb_frag_size(frag); 2728 } 2729 2730 if (st->frag_data) { 2731 kunmap_atomic(st->frag_data); 2732 st->frag_data = NULL; 2733 } 2734 2735 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2736 st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 2737 st->frag_idx = 0; 2738 goto next_skb; 2739 } else if (st->cur_skb->next) { 2740 st->cur_skb = st->cur_skb->next; 2741 st->frag_idx = 0; 2742 goto next_skb; 2743 } 2744 2745 return 0; 2746 } 2747 EXPORT_SYMBOL(skb_seq_read); 2748 2749 /** 2750 * skb_abort_seq_read - Abort a sequential read of skb data 2751 * @st: state variable 2752 * 2753 * Must be called if skb_seq_read() was not called until it 2754 * returned 0. 2755 */ 2756 void skb_abort_seq_read(struct skb_seq_state *st) 2757 { 2758 if (st->frag_data) 2759 kunmap_atomic(st->frag_data); 2760 } 2761 EXPORT_SYMBOL(skb_abort_seq_read); 2762 2763 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 2764 2765 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 2766 struct ts_config *conf, 2767 struct ts_state *state) 2768 { 2769 return skb_seq_read(offset, text, TS_SKB_CB(state)); 2770 } 2771 2772 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 2773 { 2774 skb_abort_seq_read(TS_SKB_CB(state)); 2775 } 2776 2777 /** 2778 * skb_find_text - Find a text pattern in skb data 2779 * @skb: the buffer to look in 2780 * @from: search offset 2781 * @to: search limit 2782 * @config: textsearch configuration 2783 * @state: uninitialized textsearch state variable 2784 * 2785 * Finds a pattern in the skb data according to the specified 2786 * textsearch configuration. Use textsearch_next() to retrieve 2787 * subsequent occurrences of the pattern. Returns the offset 2788 * to the first occurrence or UINT_MAX if no match was found. 2789 */ 2790 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2791 unsigned int to, struct ts_config *config, 2792 struct ts_state *state) 2793 { 2794 unsigned int ret; 2795 2796 config->get_next_block = skb_ts_get_next_block; 2797 config->finish = skb_ts_finish; 2798 2799 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 2800 2801 ret = textsearch_find(config, state); 2802 return (ret <= to - from ? ret : UINT_MAX); 2803 } 2804 EXPORT_SYMBOL(skb_find_text); 2805 2806 /** 2807 * skb_append_datato_frags - append the user data to a skb 2808 * @sk: sock structure 2809 * @skb: skb structure to be appended with user data. 2810 * @getfrag: call back function to be used for getting the user data 2811 * @from: pointer to user message iov 2812 * @length: length of the iov message 2813 * 2814 * Description: This procedure append the user data in the fragment part 2815 * of the skb if any page alloc fails user this procedure returns -ENOMEM 2816 */ 2817 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2818 int (*getfrag)(void *from, char *to, int offset, 2819 int len, int odd, struct sk_buff *skb), 2820 void *from, int length) 2821 { 2822 int frg_cnt = skb_shinfo(skb)->nr_frags; 2823 int copy; 2824 int offset = 0; 2825 int ret; 2826 struct page_frag *pfrag = ¤t->task_frag; 2827 2828 do { 2829 /* Return error if we don't have space for new frag */ 2830 if (frg_cnt >= MAX_SKB_FRAGS) 2831 return -EMSGSIZE; 2832 2833 if (!sk_page_frag_refill(sk, pfrag)) 2834 return -ENOMEM; 2835 2836 /* copy the user data to page */ 2837 copy = min_t(int, length, pfrag->size - pfrag->offset); 2838 2839 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 2840 offset, copy, 0, skb); 2841 if (ret < 0) 2842 return -EFAULT; 2843 2844 /* copy was successful so update the size parameters */ 2845 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 2846 copy); 2847 frg_cnt++; 2848 pfrag->offset += copy; 2849 get_page(pfrag->page); 2850 2851 skb->truesize += copy; 2852 atomic_add(copy, &sk->sk_wmem_alloc); 2853 skb->len += copy; 2854 skb->data_len += copy; 2855 offset += copy; 2856 length -= copy; 2857 2858 } while (length > 0); 2859 2860 return 0; 2861 } 2862 EXPORT_SYMBOL(skb_append_datato_frags); 2863 2864 /** 2865 * skb_pull_rcsum - pull skb and update receive checksum 2866 * @skb: buffer to update 2867 * @len: length of data pulled 2868 * 2869 * This function performs an skb_pull on the packet and updates 2870 * the CHECKSUM_COMPLETE checksum. It should be used on 2871 * receive path processing instead of skb_pull unless you know 2872 * that the checksum difference is zero (e.g., a valid IP header) 2873 * or you are setting ip_summed to CHECKSUM_NONE. 2874 */ 2875 unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2876 { 2877 BUG_ON(len > skb->len); 2878 skb->len -= len; 2879 BUG_ON(skb->len < skb->data_len); 2880 skb_postpull_rcsum(skb, skb->data, len); 2881 return skb->data += len; 2882 } 2883 EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2884 2885 /** 2886 * skb_segment - Perform protocol segmentation on skb. 2887 * @head_skb: buffer to segment 2888 * @features: features for the output path (see dev->features) 2889 * 2890 * This function performs segmentation on the given skb. It returns 2891 * a pointer to the first in a list of new skbs for the segments. 2892 * In case of error it returns ERR_PTR(err). 2893 */ 2894 struct sk_buff *skb_segment(struct sk_buff *head_skb, 2895 netdev_features_t features) 2896 { 2897 struct sk_buff *segs = NULL; 2898 struct sk_buff *tail = NULL; 2899 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 2900 skb_frag_t *frag = skb_shinfo(head_skb)->frags; 2901 unsigned int mss = skb_shinfo(head_skb)->gso_size; 2902 unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 2903 struct sk_buff *frag_skb = head_skb; 2904 unsigned int offset = doffset; 2905 unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 2906 unsigned int headroom; 2907 unsigned int len; 2908 __be16 proto; 2909 bool csum; 2910 int sg = !!(features & NETIF_F_SG); 2911 int nfrags = skb_shinfo(head_skb)->nr_frags; 2912 int err = -ENOMEM; 2913 int i = 0; 2914 int pos; 2915 int dummy; 2916 2917 __skb_push(head_skb, doffset); 2918 proto = skb_network_protocol(head_skb, &dummy); 2919 if (unlikely(!proto)) 2920 return ERR_PTR(-EINVAL); 2921 2922 csum = !head_skb->encap_hdr_csum && 2923 !!can_checksum_protocol(features, proto); 2924 2925 headroom = skb_headroom(head_skb); 2926 pos = skb_headlen(head_skb); 2927 2928 do { 2929 struct sk_buff *nskb; 2930 skb_frag_t *nskb_frag; 2931 int hsize; 2932 int size; 2933 2934 len = head_skb->len - offset; 2935 if (len > mss) 2936 len = mss; 2937 2938 hsize = skb_headlen(head_skb) - offset; 2939 if (hsize < 0) 2940 hsize = 0; 2941 if (hsize > len || !sg) 2942 hsize = len; 2943 2944 if (!hsize && i >= nfrags && skb_headlen(list_skb) && 2945 (skb_headlen(list_skb) == len || sg)) { 2946 BUG_ON(skb_headlen(list_skb) > len); 2947 2948 i = 0; 2949 nfrags = skb_shinfo(list_skb)->nr_frags; 2950 frag = skb_shinfo(list_skb)->frags; 2951 frag_skb = list_skb; 2952 pos += skb_headlen(list_skb); 2953 2954 while (pos < offset + len) { 2955 BUG_ON(i >= nfrags); 2956 2957 size = skb_frag_size(frag); 2958 if (pos + size > offset + len) 2959 break; 2960 2961 i++; 2962 pos += size; 2963 frag++; 2964 } 2965 2966 nskb = skb_clone(list_skb, GFP_ATOMIC); 2967 list_skb = list_skb->next; 2968 2969 if (unlikely(!nskb)) 2970 goto err; 2971 2972 if (unlikely(pskb_trim(nskb, len))) { 2973 kfree_skb(nskb); 2974 goto err; 2975 } 2976 2977 hsize = skb_end_offset(nskb); 2978 if (skb_cow_head(nskb, doffset + headroom)) { 2979 kfree_skb(nskb); 2980 goto err; 2981 } 2982 2983 nskb->truesize += skb_end_offset(nskb) - hsize; 2984 skb_release_head_state(nskb); 2985 __skb_push(nskb, doffset); 2986 } else { 2987 nskb = __alloc_skb(hsize + doffset + headroom, 2988 GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 2989 NUMA_NO_NODE); 2990 2991 if (unlikely(!nskb)) 2992 goto err; 2993 2994 skb_reserve(nskb, headroom); 2995 __skb_put(nskb, doffset); 2996 } 2997 2998 if (segs) 2999 tail->next = nskb; 3000 else 3001 segs = nskb; 3002 tail = nskb; 3003 3004 __copy_skb_header(nskb, head_skb); 3005 3006 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3007 skb_reset_mac_len(nskb); 3008 3009 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 3010 nskb->data - tnl_hlen, 3011 doffset + tnl_hlen); 3012 3013 if (nskb->len == len + doffset) 3014 goto perform_csum_check; 3015 3016 if (!sg) { 3017 nskb->ip_summed = CHECKSUM_NONE; 3018 nskb->csum = skb_copy_and_csum_bits(head_skb, offset, 3019 skb_put(nskb, len), 3020 len, 0); 3021 SKB_GSO_CB(nskb)->csum_start = 3022 skb_headroom(nskb) + doffset; 3023 continue; 3024 } 3025 3026 nskb_frag = skb_shinfo(nskb)->frags; 3027 3028 skb_copy_from_linear_data_offset(head_skb, offset, 3029 skb_put(nskb, hsize), hsize); 3030 3031 skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & 3032 SKBTX_SHARED_FRAG; 3033 3034 while (pos < offset + len) { 3035 if (i >= nfrags) { 3036 BUG_ON(skb_headlen(list_skb)); 3037 3038 i = 0; 3039 nfrags = skb_shinfo(list_skb)->nr_frags; 3040 frag = skb_shinfo(list_skb)->frags; 3041 frag_skb = list_skb; 3042 3043 BUG_ON(!nfrags); 3044 3045 list_skb = list_skb->next; 3046 } 3047 3048 if (unlikely(skb_shinfo(nskb)->nr_frags >= 3049 MAX_SKB_FRAGS)) { 3050 net_warn_ratelimited( 3051 "skb_segment: too many frags: %u %u\n", 3052 pos, mss); 3053 goto err; 3054 } 3055 3056 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 3057 goto err; 3058 3059 *nskb_frag = *frag; 3060 __skb_frag_ref(nskb_frag); 3061 size = skb_frag_size(nskb_frag); 3062 3063 if (pos < offset) { 3064 nskb_frag->page_offset += offset - pos; 3065 skb_frag_size_sub(nskb_frag, offset - pos); 3066 } 3067 3068 skb_shinfo(nskb)->nr_frags++; 3069 3070 if (pos + size <= offset + len) { 3071 i++; 3072 frag++; 3073 pos += size; 3074 } else { 3075 skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 3076 goto skip_fraglist; 3077 } 3078 3079 nskb_frag++; 3080 } 3081 3082 skip_fraglist: 3083 nskb->data_len = len - hsize; 3084 nskb->len += nskb->data_len; 3085 nskb->truesize += nskb->data_len; 3086 3087 perform_csum_check: 3088 if (!csum) { 3089 nskb->csum = skb_checksum(nskb, doffset, 3090 nskb->len - doffset, 0); 3091 nskb->ip_summed = CHECKSUM_NONE; 3092 SKB_GSO_CB(nskb)->csum_start = 3093 skb_headroom(nskb) + doffset; 3094 } 3095 } while ((offset += len) < head_skb->len); 3096 3097 /* Some callers want to get the end of the list. 3098 * Put it in segs->prev to avoid walking the list. 3099 * (see validate_xmit_skb_list() for example) 3100 */ 3101 segs->prev = tail; 3102 return segs; 3103 3104 err: 3105 kfree_skb_list(segs); 3106 return ERR_PTR(err); 3107 } 3108 EXPORT_SYMBOL_GPL(skb_segment); 3109 3110 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 3111 { 3112 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 3113 unsigned int offset = skb_gro_offset(skb); 3114 unsigned int headlen = skb_headlen(skb); 3115 struct sk_buff *nskb, *lp, *p = *head; 3116 unsigned int len = skb_gro_len(skb); 3117 unsigned int delta_truesize; 3118 unsigned int headroom; 3119 3120 if (unlikely(p->len + len >= 65536)) 3121 return -E2BIG; 3122 3123 lp = NAPI_GRO_CB(p)->last; 3124 pinfo = skb_shinfo(lp); 3125 3126 if (headlen <= offset) { 3127 skb_frag_t *frag; 3128 skb_frag_t *frag2; 3129 int i = skbinfo->nr_frags; 3130 int nr_frags = pinfo->nr_frags + i; 3131 3132 if (nr_frags > MAX_SKB_FRAGS) 3133 goto merge; 3134 3135 offset -= headlen; 3136 pinfo->nr_frags = nr_frags; 3137 skbinfo->nr_frags = 0; 3138 3139 frag = pinfo->frags + nr_frags; 3140 frag2 = skbinfo->frags + i; 3141 do { 3142 *--frag = *--frag2; 3143 } while (--i); 3144 3145 frag->page_offset += offset; 3146 skb_frag_size_sub(frag, offset); 3147 3148 /* all fragments truesize : remove (head size + sk_buff) */ 3149 delta_truesize = skb->truesize - 3150 SKB_TRUESIZE(skb_end_offset(skb)); 3151 3152 skb->truesize -= skb->data_len; 3153 skb->len -= skb->data_len; 3154 skb->data_len = 0; 3155 3156 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 3157 goto done; 3158 } else if (skb->head_frag) { 3159 int nr_frags = pinfo->nr_frags; 3160 skb_frag_t *frag = pinfo->frags + nr_frags; 3161 struct page *page = virt_to_head_page(skb->head); 3162 unsigned int first_size = headlen - offset; 3163 unsigned int first_offset; 3164 3165 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 3166 goto merge; 3167 3168 first_offset = skb->data - 3169 (unsigned char *)page_address(page) + 3170 offset; 3171 3172 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3173 3174 frag->page.p = page; 3175 frag->page_offset = first_offset; 3176 skb_frag_size_set(frag, first_size); 3177 3178 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3179 /* We dont need to clear skbinfo->nr_frags here */ 3180 3181 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3182 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3183 goto done; 3184 } 3185 /* switch back to head shinfo */ 3186 pinfo = skb_shinfo(p); 3187 3188 if (pinfo->frag_list) 3189 goto merge; 3190 if (skb_gro_len(p) != pinfo->gso_size) 3191 return -E2BIG; 3192 3193 headroom = skb_headroom(p); 3194 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 3195 if (unlikely(!nskb)) 3196 return -ENOMEM; 3197 3198 __copy_skb_header(nskb, p); 3199 nskb->mac_len = p->mac_len; 3200 3201 skb_reserve(nskb, headroom); 3202 __skb_put(nskb, skb_gro_offset(p)); 3203 3204 skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 3205 skb_set_network_header(nskb, skb_network_offset(p)); 3206 skb_set_transport_header(nskb, skb_transport_offset(p)); 3207 3208 __skb_pull(p, skb_gro_offset(p)); 3209 memcpy(skb_mac_header(nskb), skb_mac_header(p), 3210 p->data - skb_mac_header(p)); 3211 3212 skb_shinfo(nskb)->frag_list = p; 3213 skb_shinfo(nskb)->gso_size = pinfo->gso_size; 3214 pinfo->gso_size = 0; 3215 __skb_header_release(p); 3216 NAPI_GRO_CB(nskb)->last = p; 3217 3218 nskb->data_len += p->len; 3219 nskb->truesize += p->truesize; 3220 nskb->len += p->len; 3221 3222 *head = nskb; 3223 nskb->next = p->next; 3224 p->next = NULL; 3225 3226 p = nskb; 3227 3228 merge: 3229 delta_truesize = skb->truesize; 3230 if (offset > headlen) { 3231 unsigned int eat = offset - headlen; 3232 3233 skbinfo->frags[0].page_offset += eat; 3234 skb_frag_size_sub(&skbinfo->frags[0], eat); 3235 skb->data_len -= eat; 3236 skb->len -= eat; 3237 offset = headlen; 3238 } 3239 3240 __skb_pull(skb, offset); 3241 3242 if (NAPI_GRO_CB(p)->last == p) 3243 skb_shinfo(p)->frag_list = skb; 3244 else 3245 NAPI_GRO_CB(p)->last->next = skb; 3246 NAPI_GRO_CB(p)->last = skb; 3247 __skb_header_release(skb); 3248 lp = p; 3249 3250 done: 3251 NAPI_GRO_CB(p)->count++; 3252 p->data_len += len; 3253 p->truesize += delta_truesize; 3254 p->len += len; 3255 if (lp != p) { 3256 lp->data_len += len; 3257 lp->truesize += delta_truesize; 3258 lp->len += len; 3259 } 3260 NAPI_GRO_CB(skb)->same_flow = 1; 3261 return 0; 3262 } 3263 3264 void __init skb_init(void) 3265 { 3266 skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 3267 sizeof(struct sk_buff), 3268 0, 3269 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 3270 NULL); 3271 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3272 sizeof(struct sk_buff_fclones), 3273 0, 3274 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 3275 NULL); 3276 } 3277 3278 /** 3279 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3280 * @skb: Socket buffer containing the buffers to be mapped 3281 * @sg: The scatter-gather list to map into 3282 * @offset: The offset into the buffer's contents to start mapping 3283 * @len: Length of buffer space to be mapped 3284 * 3285 * Fill the specified scatter-gather list with mappings/pointers into a 3286 * region of the buffer space attached to a socket buffer. 3287 */ 3288 static int 3289 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3290 { 3291 int start = skb_headlen(skb); 3292 int i, copy = start - offset; 3293 struct sk_buff *frag_iter; 3294 int elt = 0; 3295 3296 if (copy > 0) { 3297 if (copy > len) 3298 copy = len; 3299 sg_set_buf(sg, skb->data + offset, copy); 3300 elt++; 3301 if ((len -= copy) == 0) 3302 return elt; 3303 offset += copy; 3304 } 3305 3306 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3307 int end; 3308 3309 WARN_ON(start > offset + len); 3310 3311 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3312 if ((copy = end - offset) > 0) { 3313 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3314 3315 if (copy > len) 3316 copy = len; 3317 sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3318 frag->page_offset+offset-start); 3319 elt++; 3320 if (!(len -= copy)) 3321 return elt; 3322 offset += copy; 3323 } 3324 start = end; 3325 } 3326 3327 skb_walk_frags(skb, frag_iter) { 3328 int end; 3329 3330 WARN_ON(start > offset + len); 3331 3332 end = start + frag_iter->len; 3333 if ((copy = end - offset) > 0) { 3334 if (copy > len) 3335 copy = len; 3336 elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 3337 copy); 3338 if ((len -= copy) == 0) 3339 return elt; 3340 offset += copy; 3341 } 3342 start = end; 3343 } 3344 BUG_ON(len); 3345 return elt; 3346 } 3347 3348 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 3349 * sglist without mark the sg which contain last skb data as the end. 3350 * So the caller can mannipulate sg list as will when padding new data after 3351 * the first call without calling sg_unmark_end to expend sg list. 3352 * 3353 * Scenario to use skb_to_sgvec_nomark: 3354 * 1. sg_init_table 3355 * 2. skb_to_sgvec_nomark(payload1) 3356 * 3. skb_to_sgvec_nomark(payload2) 3357 * 3358 * This is equivalent to: 3359 * 1. sg_init_table 3360 * 2. skb_to_sgvec(payload1) 3361 * 3. sg_unmark_end 3362 * 4. skb_to_sgvec(payload2) 3363 * 3364 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 3365 * is more preferable. 3366 */ 3367 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 3368 int offset, int len) 3369 { 3370 return __skb_to_sgvec(skb, sg, offset, len); 3371 } 3372 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 3373 3374 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3375 { 3376 int nsg = __skb_to_sgvec(skb, sg, offset, len); 3377 3378 sg_mark_end(&sg[nsg - 1]); 3379 3380 return nsg; 3381 } 3382 EXPORT_SYMBOL_GPL(skb_to_sgvec); 3383 3384 /** 3385 * skb_cow_data - Check that a socket buffer's data buffers are writable 3386 * @skb: The socket buffer to check. 3387 * @tailbits: Amount of trailing space to be added 3388 * @trailer: Returned pointer to the skb where the @tailbits space begins 3389 * 3390 * Make sure that the data buffers attached to a socket buffer are 3391 * writable. If they are not, private copies are made of the data buffers 3392 * and the socket buffer is set to use these instead. 3393 * 3394 * If @tailbits is given, make sure that there is space to write @tailbits 3395 * bytes of data beyond current end of socket buffer. @trailer will be 3396 * set to point to the skb in which this space begins. 3397 * 3398 * The number of scatterlist elements required to completely map the 3399 * COW'd and extended socket buffer will be returned. 3400 */ 3401 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3402 { 3403 int copyflag; 3404 int elt; 3405 struct sk_buff *skb1, **skb_p; 3406 3407 /* If skb is cloned or its head is paged, reallocate 3408 * head pulling out all the pages (pages are considered not writable 3409 * at the moment even if they are anonymous). 3410 */ 3411 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3412 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3413 return -ENOMEM; 3414 3415 /* Easy case. Most of packets will go this way. */ 3416 if (!skb_has_frag_list(skb)) { 3417 /* A little of trouble, not enough of space for trailer. 3418 * This should not happen, when stack is tuned to generate 3419 * good frames. OK, on miss we reallocate and reserve even more 3420 * space, 128 bytes is fair. */ 3421 3422 if (skb_tailroom(skb) < tailbits && 3423 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3424 return -ENOMEM; 3425 3426 /* Voila! */ 3427 *trailer = skb; 3428 return 1; 3429 } 3430 3431 /* Misery. We are in troubles, going to mincer fragments... */ 3432 3433 elt = 1; 3434 skb_p = &skb_shinfo(skb)->frag_list; 3435 copyflag = 0; 3436 3437 while ((skb1 = *skb_p) != NULL) { 3438 int ntail = 0; 3439 3440 /* The fragment is partially pulled by someone, 3441 * this can happen on input. Copy it and everything 3442 * after it. */ 3443 3444 if (skb_shared(skb1)) 3445 copyflag = 1; 3446 3447 /* If the skb is the last, worry about trailer. */ 3448 3449 if (skb1->next == NULL && tailbits) { 3450 if (skb_shinfo(skb1)->nr_frags || 3451 skb_has_frag_list(skb1) || 3452 skb_tailroom(skb1) < tailbits) 3453 ntail = tailbits + 128; 3454 } 3455 3456 if (copyflag || 3457 skb_cloned(skb1) || 3458 ntail || 3459 skb_shinfo(skb1)->nr_frags || 3460 skb_has_frag_list(skb1)) { 3461 struct sk_buff *skb2; 3462 3463 /* Fuck, we are miserable poor guys... */ 3464 if (ntail == 0) 3465 skb2 = skb_copy(skb1, GFP_ATOMIC); 3466 else 3467 skb2 = skb_copy_expand(skb1, 3468 skb_headroom(skb1), 3469 ntail, 3470 GFP_ATOMIC); 3471 if (unlikely(skb2 == NULL)) 3472 return -ENOMEM; 3473 3474 if (skb1->sk) 3475 skb_set_owner_w(skb2, skb1->sk); 3476 3477 /* Looking around. Are we still alive? 3478 * OK, link new skb, drop old one */ 3479 3480 skb2->next = skb1->next; 3481 *skb_p = skb2; 3482 kfree_skb(skb1); 3483 skb1 = skb2; 3484 } 3485 elt++; 3486 *trailer = skb1; 3487 skb_p = &skb1->next; 3488 } 3489 3490 return elt; 3491 } 3492 EXPORT_SYMBOL_GPL(skb_cow_data); 3493 3494 static void sock_rmem_free(struct sk_buff *skb) 3495 { 3496 struct sock *sk = skb->sk; 3497 3498 atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3499 } 3500 3501 /* 3502 * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3503 */ 3504 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3505 { 3506 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 3507 (unsigned int)sk->sk_rcvbuf) 3508 return -ENOMEM; 3509 3510 skb_orphan(skb); 3511 skb->sk = sk; 3512 skb->destructor = sock_rmem_free; 3513 atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3514 3515 /* before exiting rcu section, make sure dst is refcounted */ 3516 skb_dst_force(skb); 3517 3518 skb_queue_tail(&sk->sk_error_queue, skb); 3519 if (!sock_flag(sk, SOCK_DEAD)) 3520 sk->sk_data_ready(sk); 3521 return 0; 3522 } 3523 EXPORT_SYMBOL(sock_queue_err_skb); 3524 3525 struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3526 { 3527 struct sk_buff_head *q = &sk->sk_error_queue; 3528 struct sk_buff *skb, *skb_next; 3529 int err = 0; 3530 3531 spin_lock_bh(&q->lock); 3532 skb = __skb_dequeue(q); 3533 if (skb && (skb_next = skb_peek(q))) 3534 err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 3535 spin_unlock_bh(&q->lock); 3536 3537 sk->sk_err = err; 3538 if (err) 3539 sk->sk_error_report(sk); 3540 3541 return skb; 3542 } 3543 EXPORT_SYMBOL(sock_dequeue_err_skb); 3544 3545 /** 3546 * skb_clone_sk - create clone of skb, and take reference to socket 3547 * @skb: the skb to clone 3548 * 3549 * This function creates a clone of a buffer that holds a reference on 3550 * sk_refcnt. Buffers created via this function are meant to be 3551 * returned using sock_queue_err_skb, or free via kfree_skb. 3552 * 3553 * When passing buffers allocated with this function to sock_queue_err_skb 3554 * it is necessary to wrap the call with sock_hold/sock_put in order to 3555 * prevent the socket from being released prior to being enqueued on 3556 * the sk_error_queue. 3557 */ 3558 struct sk_buff *skb_clone_sk(struct sk_buff *skb) 3559 { 3560 struct sock *sk = skb->sk; 3561 struct sk_buff *clone; 3562 3563 if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) 3564 return NULL; 3565 3566 clone = skb_clone(skb, GFP_ATOMIC); 3567 if (!clone) { 3568 sock_put(sk); 3569 return NULL; 3570 } 3571 3572 clone->sk = sk; 3573 clone->destructor = sock_efree; 3574 3575 return clone; 3576 } 3577 EXPORT_SYMBOL(skb_clone_sk); 3578 3579 static void __skb_complete_tx_timestamp(struct sk_buff *skb, 3580 struct sock *sk, 3581 int tstype) 3582 { 3583 struct sock_exterr_skb *serr; 3584 int err; 3585 3586 serr = SKB_EXT_ERR(skb); 3587 memset(serr, 0, sizeof(*serr)); 3588 serr->ee.ee_errno = ENOMSG; 3589 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3590 serr->ee.ee_info = tstype; 3591 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 3592 serr->ee.ee_data = skb_shinfo(skb)->tskey; 3593 if (sk->sk_protocol == IPPROTO_TCP) 3594 serr->ee.ee_data -= sk->sk_tskey; 3595 } 3596 3597 err = sock_queue_err_skb(sk, skb); 3598 3599 if (err) 3600 kfree_skb(skb); 3601 } 3602 3603 void skb_complete_tx_timestamp(struct sk_buff *skb, 3604 struct skb_shared_hwtstamps *hwtstamps) 3605 { 3606 struct sock *sk = skb->sk; 3607 3608 /* take a reference to prevent skb_orphan() from freeing the socket */ 3609 sock_hold(sk); 3610 3611 *skb_hwtstamps(skb) = *hwtstamps; 3612 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); 3613 3614 sock_put(sk); 3615 } 3616 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 3617 3618 void __skb_tstamp_tx(struct sk_buff *orig_skb, 3619 struct skb_shared_hwtstamps *hwtstamps, 3620 struct sock *sk, int tstype) 3621 { 3622 struct sk_buff *skb; 3623 3624 if (!sk) 3625 return; 3626 3627 if (hwtstamps) 3628 *skb_hwtstamps(orig_skb) = *hwtstamps; 3629 else 3630 orig_skb->tstamp = ktime_get_real(); 3631 3632 skb = skb_clone(orig_skb, GFP_ATOMIC); 3633 if (!skb) 3634 return; 3635 3636 __skb_complete_tx_timestamp(skb, sk, tstype); 3637 } 3638 EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3639 3640 void skb_tstamp_tx(struct sk_buff *orig_skb, 3641 struct skb_shared_hwtstamps *hwtstamps) 3642 { 3643 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3644 SCM_TSTAMP_SND); 3645 } 3646 EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3647 3648 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 3649 { 3650 struct sock *sk = skb->sk; 3651 struct sock_exterr_skb *serr; 3652 int err; 3653 3654 skb->wifi_acked_valid = 1; 3655 skb->wifi_acked = acked; 3656 3657 serr = SKB_EXT_ERR(skb); 3658 memset(serr, 0, sizeof(*serr)); 3659 serr->ee.ee_errno = ENOMSG; 3660 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 3661 3662 /* take a reference to prevent skb_orphan() from freeing the socket */ 3663 sock_hold(sk); 3664 3665 err = sock_queue_err_skb(sk, skb); 3666 if (err) 3667 kfree_skb(skb); 3668 3669 sock_put(sk); 3670 } 3671 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 3672 3673 3674 /** 3675 * skb_partial_csum_set - set up and verify partial csum values for packet 3676 * @skb: the skb to set 3677 * @start: the number of bytes after skb->data to start checksumming. 3678 * @off: the offset from start to place the checksum. 3679 * 3680 * For untrusted partially-checksummed packets, we need to make sure the values 3681 * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3682 * 3683 * This function checks and sets those values and skb->ip_summed: if this 3684 * returns false you should drop the packet. 3685 */ 3686 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3687 { 3688 if (unlikely(start > skb_headlen(skb)) || 3689 unlikely((int)start + off > skb_headlen(skb) - 2)) { 3690 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 3691 start, off, skb_headlen(skb)); 3692 return false; 3693 } 3694 skb->ip_summed = CHECKSUM_PARTIAL; 3695 skb->csum_start = skb_headroom(skb) + start; 3696 skb->csum_offset = off; 3697 skb_set_transport_header(skb, start); 3698 return true; 3699 } 3700 EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3701 3702 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 3703 unsigned int max) 3704 { 3705 if (skb_headlen(skb) >= len) 3706 return 0; 3707 3708 /* If we need to pullup then pullup to the max, so we 3709 * won't need to do it again. 3710 */ 3711 if (max > skb->len) 3712 max = skb->len; 3713 3714 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 3715 return -ENOMEM; 3716 3717 if (skb_headlen(skb) < len) 3718 return -EPROTO; 3719 3720 return 0; 3721 } 3722 3723 #define MAX_TCP_HDR_LEN (15 * 4) 3724 3725 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 3726 typeof(IPPROTO_IP) proto, 3727 unsigned int off) 3728 { 3729 switch (proto) { 3730 int err; 3731 3732 case IPPROTO_TCP: 3733 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 3734 off + MAX_TCP_HDR_LEN); 3735 if (!err && !skb_partial_csum_set(skb, off, 3736 offsetof(struct tcphdr, 3737 check))) 3738 err = -EPROTO; 3739 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 3740 3741 case IPPROTO_UDP: 3742 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 3743 off + sizeof(struct udphdr)); 3744 if (!err && !skb_partial_csum_set(skb, off, 3745 offsetof(struct udphdr, 3746 check))) 3747 err = -EPROTO; 3748 return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 3749 } 3750 3751 return ERR_PTR(-EPROTO); 3752 } 3753 3754 /* This value should be large enough to cover a tagged ethernet header plus 3755 * maximally sized IP and TCP or UDP headers. 3756 */ 3757 #define MAX_IP_HDR_LEN 128 3758 3759 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 3760 { 3761 unsigned int off; 3762 bool fragment; 3763 __sum16 *csum; 3764 int err; 3765 3766 fragment = false; 3767 3768 err = skb_maybe_pull_tail(skb, 3769 sizeof(struct iphdr), 3770 MAX_IP_HDR_LEN); 3771 if (err < 0) 3772 goto out; 3773 3774 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 3775 fragment = true; 3776 3777 off = ip_hdrlen(skb); 3778 3779 err = -EPROTO; 3780 3781 if (fragment) 3782 goto out; 3783 3784 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 3785 if (IS_ERR(csum)) 3786 return PTR_ERR(csum); 3787 3788 if (recalculate) 3789 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 3790 ip_hdr(skb)->daddr, 3791 skb->len - off, 3792 ip_hdr(skb)->protocol, 0); 3793 err = 0; 3794 3795 out: 3796 return err; 3797 } 3798 3799 /* This value should be large enough to cover a tagged ethernet header plus 3800 * an IPv6 header, all options, and a maximal TCP or UDP header. 3801 */ 3802 #define MAX_IPV6_HDR_LEN 256 3803 3804 #define OPT_HDR(type, skb, off) \ 3805 (type *)(skb_network_header(skb) + (off)) 3806 3807 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 3808 { 3809 int err; 3810 u8 nexthdr; 3811 unsigned int off; 3812 unsigned int len; 3813 bool fragment; 3814 bool done; 3815 __sum16 *csum; 3816 3817 fragment = false; 3818 done = false; 3819 3820 off = sizeof(struct ipv6hdr); 3821 3822 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 3823 if (err < 0) 3824 goto out; 3825 3826 nexthdr = ipv6_hdr(skb)->nexthdr; 3827 3828 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 3829 while (off <= len && !done) { 3830 switch (nexthdr) { 3831 case IPPROTO_DSTOPTS: 3832 case IPPROTO_HOPOPTS: 3833 case IPPROTO_ROUTING: { 3834 struct ipv6_opt_hdr *hp; 3835 3836 err = skb_maybe_pull_tail(skb, 3837 off + 3838 sizeof(struct ipv6_opt_hdr), 3839 MAX_IPV6_HDR_LEN); 3840 if (err < 0) 3841 goto out; 3842 3843 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 3844 nexthdr = hp->nexthdr; 3845 off += ipv6_optlen(hp); 3846 break; 3847 } 3848 case IPPROTO_AH: { 3849 struct ip_auth_hdr *hp; 3850 3851 err = skb_maybe_pull_tail(skb, 3852 off + 3853 sizeof(struct ip_auth_hdr), 3854 MAX_IPV6_HDR_LEN); 3855 if (err < 0) 3856 goto out; 3857 3858 hp = OPT_HDR(struct ip_auth_hdr, skb, off); 3859 nexthdr = hp->nexthdr; 3860 off += ipv6_authlen(hp); 3861 break; 3862 } 3863 case IPPROTO_FRAGMENT: { 3864 struct frag_hdr *hp; 3865 3866 err = skb_maybe_pull_tail(skb, 3867 off + 3868 sizeof(struct frag_hdr), 3869 MAX_IPV6_HDR_LEN); 3870 if (err < 0) 3871 goto out; 3872 3873 hp = OPT_HDR(struct frag_hdr, skb, off); 3874 3875 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 3876 fragment = true; 3877 3878 nexthdr = hp->nexthdr; 3879 off += sizeof(struct frag_hdr); 3880 break; 3881 } 3882 default: 3883 done = true; 3884 break; 3885 } 3886 } 3887 3888 err = -EPROTO; 3889 3890 if (!done || fragment) 3891 goto out; 3892 3893 csum = skb_checksum_setup_ip(skb, nexthdr, off); 3894 if (IS_ERR(csum)) 3895 return PTR_ERR(csum); 3896 3897 if (recalculate) 3898 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 3899 &ipv6_hdr(skb)->daddr, 3900 skb->len - off, nexthdr, 0); 3901 err = 0; 3902 3903 out: 3904 return err; 3905 } 3906 3907 /** 3908 * skb_checksum_setup - set up partial checksum offset 3909 * @skb: the skb to set up 3910 * @recalculate: if true the pseudo-header checksum will be recalculated 3911 */ 3912 int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 3913 { 3914 int err; 3915 3916 switch (skb->protocol) { 3917 case htons(ETH_P_IP): 3918 err = skb_checksum_setup_ipv4(skb, recalculate); 3919 break; 3920 3921 case htons(ETH_P_IPV6): 3922 err = skb_checksum_setup_ipv6(skb, recalculate); 3923 break; 3924 3925 default: 3926 err = -EPROTO; 3927 break; 3928 } 3929 3930 return err; 3931 } 3932 EXPORT_SYMBOL(skb_checksum_setup); 3933 3934 void __skb_warn_lro_forwarding(const struct sk_buff *skb) 3935 { 3936 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 3937 skb->dev->name); 3938 } 3939 EXPORT_SYMBOL(__skb_warn_lro_forwarding); 3940 3941 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 3942 { 3943 if (head_stolen) { 3944 skb_release_head_state(skb); 3945 kmem_cache_free(skbuff_head_cache, skb); 3946 } else { 3947 __kfree_skb(skb); 3948 } 3949 } 3950 EXPORT_SYMBOL(kfree_skb_partial); 3951 3952 /** 3953 * skb_try_coalesce - try to merge skb to prior one 3954 * @to: prior buffer 3955 * @from: buffer to add 3956 * @fragstolen: pointer to boolean 3957 * @delta_truesize: how much more was allocated than was requested 3958 */ 3959 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 3960 bool *fragstolen, int *delta_truesize) 3961 { 3962 int i, delta, len = from->len; 3963 3964 *fragstolen = false; 3965 3966 if (skb_cloned(to)) 3967 return false; 3968 3969 if (len <= skb_tailroom(to)) { 3970 if (len) 3971 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 3972 *delta_truesize = 0; 3973 return true; 3974 } 3975 3976 if (skb_has_frag_list(to) || skb_has_frag_list(from)) 3977 return false; 3978 3979 if (skb_headlen(from) != 0) { 3980 struct page *page; 3981 unsigned int offset; 3982 3983 if (skb_shinfo(to)->nr_frags + 3984 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 3985 return false; 3986 3987 if (skb_head_is_locked(from)) 3988 return false; 3989 3990 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3991 3992 page = virt_to_head_page(from->head); 3993 offset = from->data - (unsigned char *)page_address(page); 3994 3995 skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 3996 page, offset, skb_headlen(from)); 3997 *fragstolen = true; 3998 } else { 3999 if (skb_shinfo(to)->nr_frags + 4000 skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4001 return false; 4002 4003 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4004 } 4005 4006 WARN_ON_ONCE(delta < len); 4007 4008 memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4009 skb_shinfo(from)->frags, 4010 skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4011 skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4012 4013 if (!skb_cloned(from)) 4014 skb_shinfo(from)->nr_frags = 0; 4015 4016 /* if the skb is not cloned this does nothing 4017 * since we set nr_frags to 0. 4018 */ 4019 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4020 skb_frag_ref(from, i); 4021 4022 to->truesize += delta; 4023 to->len += len; 4024 to->data_len += len; 4025 4026 *delta_truesize = delta; 4027 return true; 4028 } 4029 EXPORT_SYMBOL(skb_try_coalesce); 4030 4031 /** 4032 * skb_scrub_packet - scrub an skb 4033 * 4034 * @skb: buffer to clean 4035 * @xnet: packet is crossing netns 4036 * 4037 * skb_scrub_packet can be used after encapsulating or decapsulting a packet 4038 * into/from a tunnel. Some information have to be cleared during these 4039 * operations. 4040 * skb_scrub_packet can also be used to clean a skb before injecting it in 4041 * another namespace (@xnet == true). We have to clear all information in the 4042 * skb that could impact namespace isolation. 4043 */ 4044 void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4045 { 4046 if (xnet) 4047 skb_orphan(skb); 4048 skb->tstamp.tv64 = 0; 4049 skb->pkt_type = PACKET_HOST; 4050 skb->skb_iif = 0; 4051 skb->ignore_df = 0; 4052 skb_dst_drop(skb); 4053 skb->mark = 0; 4054 secpath_reset(skb); 4055 nf_reset(skb); 4056 nf_reset_trace(skb); 4057 } 4058 EXPORT_SYMBOL_GPL(skb_scrub_packet); 4059 4060 /** 4061 * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4062 * 4063 * @skb: GSO skb 4064 * 4065 * skb_gso_transport_seglen is used to determine the real size of the 4066 * individual segments, including Layer4 headers (TCP/UDP). 4067 * 4068 * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4069 */ 4070 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4071 { 4072 const struct skb_shared_info *shinfo = skb_shinfo(skb); 4073 unsigned int thlen = 0; 4074 4075 if (skb->encapsulation) { 4076 thlen = skb_inner_transport_header(skb) - 4077 skb_transport_header(skb); 4078 4079 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4080 thlen += inner_tcp_hdrlen(skb); 4081 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4082 thlen = tcp_hdrlen(skb); 4083 } 4084 /* UFO sets gso_size to the size of the fragmentation 4085 * payload, i.e. the size of the L4 (UDP) header is already 4086 * accounted for. 4087 */ 4088 return thlen + shinfo->gso_size; 4089 } 4090 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 4091 4092 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 4093 { 4094 if (skb_cow(skb, skb_headroom(skb)) < 0) { 4095 kfree_skb(skb); 4096 return NULL; 4097 } 4098 4099 memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN); 4100 skb->mac_header += VLAN_HLEN; 4101 return skb; 4102 } 4103 4104 struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 4105 { 4106 struct vlan_hdr *vhdr; 4107 u16 vlan_tci; 4108 4109 if (unlikely(vlan_tx_tag_present(skb))) { 4110 /* vlan_tci is already set-up so leave this for another time */ 4111 return skb; 4112 } 4113 4114 skb = skb_share_check(skb, GFP_ATOMIC); 4115 if (unlikely(!skb)) 4116 goto err_free; 4117 4118 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 4119 goto err_free; 4120 4121 vhdr = (struct vlan_hdr *)skb->data; 4122 vlan_tci = ntohs(vhdr->h_vlan_TCI); 4123 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 4124 4125 skb_pull_rcsum(skb, VLAN_HLEN); 4126 vlan_set_encap_proto(skb, vhdr); 4127 4128 skb = skb_reorder_vlan_header(skb); 4129 if (unlikely(!skb)) 4130 goto err_free; 4131 4132 skb_reset_network_header(skb); 4133 skb_reset_transport_header(skb); 4134 skb_reset_mac_len(skb); 4135 4136 return skb; 4137 4138 err_free: 4139 kfree_skb(skb); 4140 return NULL; 4141 } 4142 EXPORT_SYMBOL(skb_vlan_untag); 4143 4144 /** 4145 * alloc_skb_with_frags - allocate skb with page frags 4146 * 4147 * @header_len: size of linear part 4148 * @data_len: needed length in frags 4149 * @max_page_order: max page order desired. 4150 * @errcode: pointer to error code if any 4151 * @gfp_mask: allocation mask 4152 * 4153 * This can be used to allocate a paged skb, given a maximal order for frags. 4154 */ 4155 struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 4156 unsigned long data_len, 4157 int max_page_order, 4158 int *errcode, 4159 gfp_t gfp_mask) 4160 { 4161 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 4162 unsigned long chunk; 4163 struct sk_buff *skb; 4164 struct page *page; 4165 gfp_t gfp_head; 4166 int i; 4167 4168 *errcode = -EMSGSIZE; 4169 /* Note this test could be relaxed, if we succeed to allocate 4170 * high order pages... 4171 */ 4172 if (npages > MAX_SKB_FRAGS) 4173 return NULL; 4174 4175 gfp_head = gfp_mask; 4176 if (gfp_head & __GFP_WAIT) 4177 gfp_head |= __GFP_REPEAT; 4178 4179 *errcode = -ENOBUFS; 4180 skb = alloc_skb(header_len, gfp_head); 4181 if (!skb) 4182 return NULL; 4183 4184 skb->truesize += npages << PAGE_SHIFT; 4185 4186 for (i = 0; npages > 0; i++) { 4187 int order = max_page_order; 4188 4189 while (order) { 4190 if (npages >= 1 << order) { 4191 page = alloc_pages(gfp_mask | 4192 __GFP_COMP | 4193 __GFP_NOWARN | 4194 __GFP_NORETRY, 4195 order); 4196 if (page) 4197 goto fill_page; 4198 /* Do not retry other high order allocations */ 4199 order = 1; 4200 max_page_order = 0; 4201 } 4202 order--; 4203 } 4204 page = alloc_page(gfp_mask); 4205 if (!page) 4206 goto failure; 4207 fill_page: 4208 chunk = min_t(unsigned long, data_len, 4209 PAGE_SIZE << order); 4210 skb_fill_page_desc(skb, i, page, 0, chunk); 4211 data_len -= chunk; 4212 npages -= 1 << order; 4213 } 4214 return skb; 4215 4216 failure: 4217 kfree_skb(skb); 4218 return NULL; 4219 } 4220 EXPORT_SYMBOL(alloc_skb_with_frags); 4221