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/mm.h> 45 #include <linux/interrupt.h> 46 #include <linux/in.h> 47 #include <linux/inet.h> 48 #include <linux/slab.h> 49 #include <linux/tcp.h> 50 #include <linux/udp.h> 51 #include <linux/sctp.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 <linux/uaccess.h> 75 #include <trace/events/skb.h> 76 #include <linux/highmem.h> 77 #include <linux/capability.h> 78 #include <linux/user_namespace.h> 79 80 struct kmem_cache *skbuff_head_cache __read_mostly; 81 static struct kmem_cache *skbuff_fclone_cache __read_mostly; 82 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 83 EXPORT_SYMBOL(sysctl_max_skb_frags); 84 85 /** 86 * skb_panic - private function for out-of-line support 87 * @skb: buffer 88 * @sz: size 89 * @addr: address 90 * @msg: skb_over_panic or skb_under_panic 91 * 92 * Out-of-line support for skb_put() and skb_push(). 93 * Called via the wrapper skb_over_panic() or skb_under_panic(). 94 * Keep out of line to prevent kernel bloat. 95 * __builtin_return_address is not used because it is not always reliable. 96 */ 97 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 98 const char msg[]) 99 { 100 pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 101 msg, addr, skb->len, sz, skb->head, skb->data, 102 (unsigned long)skb->tail, (unsigned long)skb->end, 103 skb->dev ? skb->dev->name : "<NULL>"); 104 BUG(); 105 } 106 107 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 108 { 109 skb_panic(skb, sz, addr, __func__); 110 } 111 112 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 113 { 114 skb_panic(skb, sz, addr, __func__); 115 } 116 117 /* 118 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 119 * the caller if emergency pfmemalloc reserves are being used. If it is and 120 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 121 * may be used. Otherwise, the packet data may be discarded until enough 122 * memory is free 123 */ 124 #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 125 __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 126 127 static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 128 unsigned long ip, bool *pfmemalloc) 129 { 130 void *obj; 131 bool ret_pfmemalloc = false; 132 133 /* 134 * Try a regular allocation, when that fails and we're not entitled 135 * to the reserves, fail. 136 */ 137 obj = kmalloc_node_track_caller(size, 138 flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 139 node); 140 if (obj || !(gfp_pfmemalloc_allowed(flags))) 141 goto out; 142 143 /* Try again but now we are using pfmemalloc reserves */ 144 ret_pfmemalloc = true; 145 obj = kmalloc_node_track_caller(size, flags, node); 146 147 out: 148 if (pfmemalloc) 149 *pfmemalloc = ret_pfmemalloc; 150 151 return obj; 152 } 153 154 /* Allocate a new skbuff. We do this ourselves so we can fill in a few 155 * 'private' fields and also do memory statistics to find all the 156 * [BEEP] leaks. 157 * 158 */ 159 160 /** 161 * __alloc_skb - allocate a network buffer 162 * @size: size to allocate 163 * @gfp_mask: allocation mask 164 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 165 * instead of head cache and allocate a cloned (child) skb. 166 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 167 * allocations in case the data is required for writeback 168 * @node: numa node to allocate memory on 169 * 170 * Allocate a new &sk_buff. The returned buffer has no headroom and a 171 * tail room of at least size bytes. The object has a reference count 172 * of one. The return is the buffer. On a failure the return is %NULL. 173 * 174 * Buffers may only be allocated from interrupts using a @gfp_mask of 175 * %GFP_ATOMIC. 176 */ 177 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 178 int flags, int node) 179 { 180 struct kmem_cache *cache; 181 struct skb_shared_info *shinfo; 182 struct sk_buff *skb; 183 u8 *data; 184 bool pfmemalloc; 185 186 cache = (flags & SKB_ALLOC_FCLONE) 187 ? skbuff_fclone_cache : skbuff_head_cache; 188 189 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 190 gfp_mask |= __GFP_MEMALLOC; 191 192 /* Get the HEAD */ 193 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 194 if (!skb) 195 goto out; 196 prefetchw(skb); 197 198 /* We do our best to align skb_shared_info on a separate cache 199 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 200 * aligned memory blocks, unless SLUB/SLAB debug is enabled. 201 * Both skb->head and skb_shared_info are cache line aligned. 202 */ 203 size = SKB_DATA_ALIGN(size); 204 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 205 data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 206 if (!data) 207 goto nodata; 208 /* kmalloc(size) might give us more room than requested. 209 * Put skb_shared_info exactly at the end of allocated zone, 210 * to allow max possible filling before reallocation. 211 */ 212 size = SKB_WITH_OVERHEAD(ksize(data)); 213 prefetchw(data + size); 214 215 /* 216 * Only clear those fields we need to clear, not those that we will 217 * actually initialise below. Hence, don't put any more fields after 218 * the tail pointer in struct sk_buff! 219 */ 220 memset(skb, 0, offsetof(struct sk_buff, tail)); 221 /* Account for allocated memory : skb + skb->head */ 222 skb->truesize = SKB_TRUESIZE(size); 223 skb->pfmemalloc = pfmemalloc; 224 refcount_set(&skb->users, 1); 225 skb->head = data; 226 skb->data = data; 227 skb_reset_tail_pointer(skb); 228 skb->end = skb->tail + size; 229 skb->mac_header = (typeof(skb->mac_header))~0U; 230 skb->transport_header = (typeof(skb->transport_header))~0U; 231 232 /* make sure we initialize shinfo sequentially */ 233 shinfo = skb_shinfo(skb); 234 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 235 atomic_set(&shinfo->dataref, 1); 236 237 if (flags & SKB_ALLOC_FCLONE) { 238 struct sk_buff_fclones *fclones; 239 240 fclones = container_of(skb, struct sk_buff_fclones, skb1); 241 242 skb->fclone = SKB_FCLONE_ORIG; 243 refcount_set(&fclones->fclone_ref, 1); 244 245 fclones->skb2.fclone = SKB_FCLONE_CLONE; 246 } 247 out: 248 return skb; 249 nodata: 250 kmem_cache_free(cache, skb); 251 skb = NULL; 252 goto out; 253 } 254 EXPORT_SYMBOL(__alloc_skb); 255 256 /** 257 * __build_skb - build a network buffer 258 * @data: data buffer provided by caller 259 * @frag_size: size of data, or 0 if head was kmalloced 260 * 261 * Allocate a new &sk_buff. Caller provides space holding head and 262 * skb_shared_info. @data must have been allocated by kmalloc() only if 263 * @frag_size is 0, otherwise data should come from the page allocator 264 * or vmalloc() 265 * The return is the new skb buffer. 266 * On a failure the return is %NULL, and @data is not freed. 267 * Notes : 268 * Before IO, driver allocates only data buffer where NIC put incoming frame 269 * Driver should add room at head (NET_SKB_PAD) and 270 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 271 * After IO, driver calls build_skb(), to allocate sk_buff and populate it 272 * before giving packet to stack. 273 * RX rings only contains data buffers, not full skbs. 274 */ 275 struct sk_buff *__build_skb(void *data, unsigned int frag_size) 276 { 277 struct skb_shared_info *shinfo; 278 struct sk_buff *skb; 279 unsigned int size = frag_size ? : ksize(data); 280 281 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 282 if (!skb) 283 return NULL; 284 285 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 286 287 memset(skb, 0, offsetof(struct sk_buff, tail)); 288 skb->truesize = SKB_TRUESIZE(size); 289 refcount_set(&skb->users, 1); 290 skb->head = data; 291 skb->data = data; 292 skb_reset_tail_pointer(skb); 293 skb->end = skb->tail + size; 294 skb->mac_header = (typeof(skb->mac_header))~0U; 295 skb->transport_header = (typeof(skb->transport_header))~0U; 296 297 /* make sure we initialize shinfo sequentially */ 298 shinfo = skb_shinfo(skb); 299 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 300 atomic_set(&shinfo->dataref, 1); 301 302 return skb; 303 } 304 305 /* build_skb() is wrapper over __build_skb(), that specifically 306 * takes care of skb->head and skb->pfmemalloc 307 * This means that if @frag_size is not zero, then @data must be backed 308 * by a page fragment, not kmalloc() or vmalloc() 309 */ 310 struct sk_buff *build_skb(void *data, unsigned int frag_size) 311 { 312 struct sk_buff *skb = __build_skb(data, frag_size); 313 314 if (skb && frag_size) { 315 skb->head_frag = 1; 316 if (page_is_pfmemalloc(virt_to_head_page(data))) 317 skb->pfmemalloc = 1; 318 } 319 return skb; 320 } 321 EXPORT_SYMBOL(build_skb); 322 323 #define NAPI_SKB_CACHE_SIZE 64 324 325 struct napi_alloc_cache { 326 struct page_frag_cache page; 327 unsigned int skb_count; 328 void *skb_cache[NAPI_SKB_CACHE_SIZE]; 329 }; 330 331 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 332 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 333 334 static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 335 { 336 struct page_frag_cache *nc; 337 unsigned long flags; 338 void *data; 339 340 local_irq_save(flags); 341 nc = this_cpu_ptr(&netdev_alloc_cache); 342 data = page_frag_alloc(nc, fragsz, gfp_mask); 343 local_irq_restore(flags); 344 return data; 345 } 346 347 /** 348 * netdev_alloc_frag - allocate a page fragment 349 * @fragsz: fragment size 350 * 351 * Allocates a frag from a page for receive buffer. 352 * Uses GFP_ATOMIC allocations. 353 */ 354 void *netdev_alloc_frag(unsigned int fragsz) 355 { 356 return __netdev_alloc_frag(fragsz, GFP_ATOMIC); 357 } 358 EXPORT_SYMBOL(netdev_alloc_frag); 359 360 static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 361 { 362 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 363 364 return page_frag_alloc(&nc->page, fragsz, gfp_mask); 365 } 366 367 void *napi_alloc_frag(unsigned int fragsz) 368 { 369 return __napi_alloc_frag(fragsz, GFP_ATOMIC); 370 } 371 EXPORT_SYMBOL(napi_alloc_frag); 372 373 /** 374 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 375 * @dev: network device to receive on 376 * @len: length to allocate 377 * @gfp_mask: get_free_pages mask, passed to alloc_skb 378 * 379 * Allocate a new &sk_buff and assign it a usage count of one. The 380 * buffer has NET_SKB_PAD headroom built in. Users should allocate 381 * the headroom they think they need without accounting for the 382 * built in space. The built in space is used for optimisations. 383 * 384 * %NULL is returned if there is no free memory. 385 */ 386 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 387 gfp_t gfp_mask) 388 { 389 struct page_frag_cache *nc; 390 unsigned long flags; 391 struct sk_buff *skb; 392 bool pfmemalloc; 393 void *data; 394 395 len += NET_SKB_PAD; 396 397 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 398 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 399 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 400 if (!skb) 401 goto skb_fail; 402 goto skb_success; 403 } 404 405 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 406 len = SKB_DATA_ALIGN(len); 407 408 if (sk_memalloc_socks()) 409 gfp_mask |= __GFP_MEMALLOC; 410 411 local_irq_save(flags); 412 413 nc = this_cpu_ptr(&netdev_alloc_cache); 414 data = page_frag_alloc(nc, len, gfp_mask); 415 pfmemalloc = nc->pfmemalloc; 416 417 local_irq_restore(flags); 418 419 if (unlikely(!data)) 420 return NULL; 421 422 skb = __build_skb(data, len); 423 if (unlikely(!skb)) { 424 skb_free_frag(data); 425 return NULL; 426 } 427 428 /* use OR instead of assignment to avoid clearing of bits in mask */ 429 if (pfmemalloc) 430 skb->pfmemalloc = 1; 431 skb->head_frag = 1; 432 433 skb_success: 434 skb_reserve(skb, NET_SKB_PAD); 435 skb->dev = dev; 436 437 skb_fail: 438 return skb; 439 } 440 EXPORT_SYMBOL(__netdev_alloc_skb); 441 442 /** 443 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 444 * @napi: napi instance this buffer was allocated for 445 * @len: length to allocate 446 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 447 * 448 * Allocate a new sk_buff for use in NAPI receive. This buffer will 449 * attempt to allocate the head from a special reserved region used 450 * only for NAPI Rx allocation. By doing this we can save several 451 * CPU cycles by avoiding having to disable and re-enable IRQs. 452 * 453 * %NULL is returned if there is no free memory. 454 */ 455 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 456 gfp_t gfp_mask) 457 { 458 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 459 struct sk_buff *skb; 460 void *data; 461 462 len += NET_SKB_PAD + NET_IP_ALIGN; 463 464 if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 465 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 466 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 467 if (!skb) 468 goto skb_fail; 469 goto skb_success; 470 } 471 472 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 473 len = SKB_DATA_ALIGN(len); 474 475 if (sk_memalloc_socks()) 476 gfp_mask |= __GFP_MEMALLOC; 477 478 data = page_frag_alloc(&nc->page, len, gfp_mask); 479 if (unlikely(!data)) 480 return NULL; 481 482 skb = __build_skb(data, len); 483 if (unlikely(!skb)) { 484 skb_free_frag(data); 485 return NULL; 486 } 487 488 /* use OR instead of assignment to avoid clearing of bits in mask */ 489 if (nc->page.pfmemalloc) 490 skb->pfmemalloc = 1; 491 skb->head_frag = 1; 492 493 skb_success: 494 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 495 skb->dev = napi->dev; 496 497 skb_fail: 498 return skb; 499 } 500 EXPORT_SYMBOL(__napi_alloc_skb); 501 502 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 503 int size, unsigned int truesize) 504 { 505 skb_fill_page_desc(skb, i, page, off, size); 506 skb->len += size; 507 skb->data_len += size; 508 skb->truesize += truesize; 509 } 510 EXPORT_SYMBOL(skb_add_rx_frag); 511 512 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 513 unsigned int truesize) 514 { 515 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 516 517 skb_frag_size_add(frag, size); 518 skb->len += size; 519 skb->data_len += size; 520 skb->truesize += truesize; 521 } 522 EXPORT_SYMBOL(skb_coalesce_rx_frag); 523 524 static void skb_drop_list(struct sk_buff **listp) 525 { 526 kfree_skb_list(*listp); 527 *listp = NULL; 528 } 529 530 static inline void skb_drop_fraglist(struct sk_buff *skb) 531 { 532 skb_drop_list(&skb_shinfo(skb)->frag_list); 533 } 534 535 static void skb_clone_fraglist(struct sk_buff *skb) 536 { 537 struct sk_buff *list; 538 539 skb_walk_frags(skb, list) 540 skb_get(list); 541 } 542 543 static void skb_free_head(struct sk_buff *skb) 544 { 545 unsigned char *head = skb->head; 546 547 if (skb->head_frag) 548 skb_free_frag(head); 549 else 550 kfree(head); 551 } 552 553 static void skb_release_data(struct sk_buff *skb) 554 { 555 struct skb_shared_info *shinfo = skb_shinfo(skb); 556 int i; 557 558 if (skb->cloned && 559 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 560 &shinfo->dataref)) 561 return; 562 563 for (i = 0; i < shinfo->nr_frags; i++) 564 __skb_frag_unref(&shinfo->frags[i]); 565 566 if (shinfo->frag_list) 567 kfree_skb_list(shinfo->frag_list); 568 569 skb_zcopy_clear(skb, true); 570 skb_free_head(skb); 571 } 572 573 /* 574 * Free an skbuff by memory without cleaning the state. 575 */ 576 static void kfree_skbmem(struct sk_buff *skb) 577 { 578 struct sk_buff_fclones *fclones; 579 580 switch (skb->fclone) { 581 case SKB_FCLONE_UNAVAILABLE: 582 kmem_cache_free(skbuff_head_cache, skb); 583 return; 584 585 case SKB_FCLONE_ORIG: 586 fclones = container_of(skb, struct sk_buff_fclones, skb1); 587 588 /* We usually free the clone (TX completion) before original skb 589 * This test would have no chance to be true for the clone, 590 * while here, branch prediction will be good. 591 */ 592 if (refcount_read(&fclones->fclone_ref) == 1) 593 goto fastpath; 594 break; 595 596 default: /* SKB_FCLONE_CLONE */ 597 fclones = container_of(skb, struct sk_buff_fclones, skb2); 598 break; 599 } 600 if (!refcount_dec_and_test(&fclones->fclone_ref)) 601 return; 602 fastpath: 603 kmem_cache_free(skbuff_fclone_cache, fclones); 604 } 605 606 void skb_release_head_state(struct sk_buff *skb) 607 { 608 skb_dst_drop(skb); 609 secpath_reset(skb); 610 if (skb->destructor) { 611 WARN_ON(in_irq()); 612 skb->destructor(skb); 613 } 614 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 615 nf_conntrack_put(skb_nfct(skb)); 616 #endif 617 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 618 nf_bridge_put(skb->nf_bridge); 619 #endif 620 } 621 622 /* Free everything but the sk_buff shell. */ 623 static void skb_release_all(struct sk_buff *skb) 624 { 625 skb_release_head_state(skb); 626 if (likely(skb->head)) 627 skb_release_data(skb); 628 } 629 630 /** 631 * __kfree_skb - private function 632 * @skb: buffer 633 * 634 * Free an sk_buff. Release anything attached to the buffer. 635 * Clean the state. This is an internal helper function. Users should 636 * always call kfree_skb 637 */ 638 639 void __kfree_skb(struct sk_buff *skb) 640 { 641 skb_release_all(skb); 642 kfree_skbmem(skb); 643 } 644 EXPORT_SYMBOL(__kfree_skb); 645 646 /** 647 * kfree_skb - free an sk_buff 648 * @skb: buffer to free 649 * 650 * Drop a reference to the buffer and free it if the usage count has 651 * hit zero. 652 */ 653 void kfree_skb(struct sk_buff *skb) 654 { 655 if (!skb_unref(skb)) 656 return; 657 658 trace_kfree_skb(skb, __builtin_return_address(0)); 659 __kfree_skb(skb); 660 } 661 EXPORT_SYMBOL(kfree_skb); 662 663 void kfree_skb_list(struct sk_buff *segs) 664 { 665 while (segs) { 666 struct sk_buff *next = segs->next; 667 668 kfree_skb(segs); 669 segs = next; 670 } 671 } 672 EXPORT_SYMBOL(kfree_skb_list); 673 674 /** 675 * skb_tx_error - report an sk_buff xmit error 676 * @skb: buffer that triggered an error 677 * 678 * Report xmit error if a device callback is tracking this skb. 679 * skb must be freed afterwards. 680 */ 681 void skb_tx_error(struct sk_buff *skb) 682 { 683 skb_zcopy_clear(skb, true); 684 } 685 EXPORT_SYMBOL(skb_tx_error); 686 687 /** 688 * consume_skb - free an skbuff 689 * @skb: buffer to free 690 * 691 * Drop a ref to the buffer and free it if the usage count has hit zero 692 * Functions identically to kfree_skb, but kfree_skb assumes that the frame 693 * is being dropped after a failure and notes that 694 */ 695 void consume_skb(struct sk_buff *skb) 696 { 697 if (!skb_unref(skb)) 698 return; 699 700 trace_consume_skb(skb); 701 __kfree_skb(skb); 702 } 703 EXPORT_SYMBOL(consume_skb); 704 705 /** 706 * consume_stateless_skb - free an skbuff, assuming it is stateless 707 * @skb: buffer to free 708 * 709 * Alike consume_skb(), but this variant assumes that this is the last 710 * skb reference and all the head states have been already dropped 711 */ 712 void __consume_stateless_skb(struct sk_buff *skb) 713 { 714 trace_consume_skb(skb); 715 skb_release_data(skb); 716 kfree_skbmem(skb); 717 } 718 719 void __kfree_skb_flush(void) 720 { 721 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 722 723 /* flush skb_cache if containing objects */ 724 if (nc->skb_count) { 725 kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, 726 nc->skb_cache); 727 nc->skb_count = 0; 728 } 729 } 730 731 static inline void _kfree_skb_defer(struct sk_buff *skb) 732 { 733 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 734 735 /* drop skb->head and call any destructors for packet */ 736 skb_release_all(skb); 737 738 /* record skb to CPU local list */ 739 nc->skb_cache[nc->skb_count++] = skb; 740 741 #ifdef CONFIG_SLUB 742 /* SLUB writes into objects when freeing */ 743 prefetchw(skb); 744 #endif 745 746 /* flush skb_cache if it is filled */ 747 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 748 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, 749 nc->skb_cache); 750 nc->skb_count = 0; 751 } 752 } 753 void __kfree_skb_defer(struct sk_buff *skb) 754 { 755 _kfree_skb_defer(skb); 756 } 757 758 void napi_consume_skb(struct sk_buff *skb, int budget) 759 { 760 if (unlikely(!skb)) 761 return; 762 763 /* Zero budget indicate non-NAPI context called us, like netpoll */ 764 if (unlikely(!budget)) { 765 dev_consume_skb_any(skb); 766 return; 767 } 768 769 if (!skb_unref(skb)) 770 return; 771 772 /* if reaching here SKB is ready to free */ 773 trace_consume_skb(skb); 774 775 /* if SKB is a clone, don't handle this case */ 776 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 777 __kfree_skb(skb); 778 return; 779 } 780 781 _kfree_skb_defer(skb); 782 } 783 EXPORT_SYMBOL(napi_consume_skb); 784 785 /* Make sure a field is enclosed inside headers_start/headers_end section */ 786 #define CHECK_SKB_FIELD(field) \ 787 BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 788 offsetof(struct sk_buff, headers_start)); \ 789 BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 790 offsetof(struct sk_buff, headers_end)); \ 791 792 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 793 { 794 new->tstamp = old->tstamp; 795 /* We do not copy old->sk */ 796 new->dev = old->dev; 797 memcpy(new->cb, old->cb, sizeof(old->cb)); 798 skb_dst_copy(new, old); 799 #ifdef CONFIG_XFRM 800 new->sp = secpath_get(old->sp); 801 #endif 802 __nf_copy(new, old, false); 803 804 /* Note : this field could be in headers_start/headers_end section 805 * It is not yet because we do not want to have a 16 bit hole 806 */ 807 new->queue_mapping = old->queue_mapping; 808 809 memcpy(&new->headers_start, &old->headers_start, 810 offsetof(struct sk_buff, headers_end) - 811 offsetof(struct sk_buff, headers_start)); 812 CHECK_SKB_FIELD(protocol); 813 CHECK_SKB_FIELD(csum); 814 CHECK_SKB_FIELD(hash); 815 CHECK_SKB_FIELD(priority); 816 CHECK_SKB_FIELD(skb_iif); 817 CHECK_SKB_FIELD(vlan_proto); 818 CHECK_SKB_FIELD(vlan_tci); 819 CHECK_SKB_FIELD(transport_header); 820 CHECK_SKB_FIELD(network_header); 821 CHECK_SKB_FIELD(mac_header); 822 CHECK_SKB_FIELD(inner_protocol); 823 CHECK_SKB_FIELD(inner_transport_header); 824 CHECK_SKB_FIELD(inner_network_header); 825 CHECK_SKB_FIELD(inner_mac_header); 826 CHECK_SKB_FIELD(mark); 827 #ifdef CONFIG_NETWORK_SECMARK 828 CHECK_SKB_FIELD(secmark); 829 #endif 830 #ifdef CONFIG_NET_RX_BUSY_POLL 831 CHECK_SKB_FIELD(napi_id); 832 #endif 833 #ifdef CONFIG_XPS 834 CHECK_SKB_FIELD(sender_cpu); 835 #endif 836 #ifdef CONFIG_NET_SCHED 837 CHECK_SKB_FIELD(tc_index); 838 #endif 839 840 } 841 842 /* 843 * You should not add any new code to this function. Add it to 844 * __copy_skb_header above instead. 845 */ 846 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 847 { 848 #define C(x) n->x = skb->x 849 850 n->next = n->prev = NULL; 851 n->sk = NULL; 852 __copy_skb_header(n, skb); 853 854 C(len); 855 C(data_len); 856 C(mac_len); 857 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 858 n->cloned = 1; 859 n->nohdr = 0; 860 n->destructor = NULL; 861 C(tail); 862 C(end); 863 C(head); 864 C(head_frag); 865 C(data); 866 C(truesize); 867 refcount_set(&n->users, 1); 868 869 atomic_inc(&(skb_shinfo(skb)->dataref)); 870 skb->cloned = 1; 871 872 return n; 873 #undef C 874 } 875 876 /** 877 * skb_morph - morph one skb into another 878 * @dst: the skb to receive the contents 879 * @src: the skb to supply the contents 880 * 881 * This is identical to skb_clone except that the target skb is 882 * supplied by the user. 883 * 884 * The target skb is returned upon exit. 885 */ 886 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 887 { 888 skb_release_all(dst); 889 return __skb_clone(dst, src); 890 } 891 EXPORT_SYMBOL_GPL(skb_morph); 892 893 static int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 894 { 895 unsigned long max_pg, num_pg, new_pg, old_pg; 896 struct user_struct *user; 897 898 if (capable(CAP_IPC_LOCK) || !size) 899 return 0; 900 901 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 902 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 903 user = mmp->user ? : current_user(); 904 905 do { 906 old_pg = atomic_long_read(&user->locked_vm); 907 new_pg = old_pg + num_pg; 908 if (new_pg > max_pg) 909 return -ENOBUFS; 910 } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 911 old_pg); 912 913 if (!mmp->user) { 914 mmp->user = get_uid(user); 915 mmp->num_pg = num_pg; 916 } else { 917 mmp->num_pg += num_pg; 918 } 919 920 return 0; 921 } 922 923 static void mm_unaccount_pinned_pages(struct mmpin *mmp) 924 { 925 if (mmp->user) { 926 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 927 free_uid(mmp->user); 928 } 929 } 930 931 struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size) 932 { 933 struct ubuf_info *uarg; 934 struct sk_buff *skb; 935 936 WARN_ON_ONCE(!in_task()); 937 938 if (!sock_flag(sk, SOCK_ZEROCOPY)) 939 return NULL; 940 941 skb = sock_omalloc(sk, 0, GFP_KERNEL); 942 if (!skb) 943 return NULL; 944 945 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 946 uarg = (void *)skb->cb; 947 uarg->mmp.user = NULL; 948 949 if (mm_account_pinned_pages(&uarg->mmp, size)) { 950 kfree_skb(skb); 951 return NULL; 952 } 953 954 uarg->callback = sock_zerocopy_callback; 955 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 956 uarg->len = 1; 957 uarg->bytelen = size; 958 uarg->zerocopy = 1; 959 refcount_set(&uarg->refcnt, 1); 960 sock_hold(sk); 961 962 return uarg; 963 } 964 EXPORT_SYMBOL_GPL(sock_zerocopy_alloc); 965 966 static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 967 { 968 return container_of((void *)uarg, struct sk_buff, cb); 969 } 970 971 struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size, 972 struct ubuf_info *uarg) 973 { 974 if (uarg) { 975 const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 976 u32 bytelen, next; 977 978 /* realloc only when socket is locked (TCP, UDP cork), 979 * so uarg->len and sk_zckey access is serialized 980 */ 981 if (!sock_owned_by_user(sk)) { 982 WARN_ON_ONCE(1); 983 return NULL; 984 } 985 986 bytelen = uarg->bytelen + size; 987 if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 988 /* TCP can create new skb to attach new uarg */ 989 if (sk->sk_type == SOCK_STREAM) 990 goto new_alloc; 991 return NULL; 992 } 993 994 next = (u32)atomic_read(&sk->sk_zckey); 995 if ((u32)(uarg->id + uarg->len) == next) { 996 if (mm_account_pinned_pages(&uarg->mmp, size)) 997 return NULL; 998 uarg->len++; 999 uarg->bytelen = bytelen; 1000 atomic_set(&sk->sk_zckey, ++next); 1001 sock_zerocopy_get(uarg); 1002 return uarg; 1003 } 1004 } 1005 1006 new_alloc: 1007 return sock_zerocopy_alloc(sk, size); 1008 } 1009 EXPORT_SYMBOL_GPL(sock_zerocopy_realloc); 1010 1011 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 1012 { 1013 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 1014 u32 old_lo, old_hi; 1015 u64 sum_len; 1016 1017 old_lo = serr->ee.ee_info; 1018 old_hi = serr->ee.ee_data; 1019 sum_len = old_hi - old_lo + 1ULL + len; 1020 1021 if (sum_len >= (1ULL << 32)) 1022 return false; 1023 1024 if (lo != old_hi + 1) 1025 return false; 1026 1027 serr->ee.ee_data += len; 1028 return true; 1029 } 1030 1031 void sock_zerocopy_callback(struct ubuf_info *uarg, bool success) 1032 { 1033 struct sk_buff *tail, *skb = skb_from_uarg(uarg); 1034 struct sock_exterr_skb *serr; 1035 struct sock *sk = skb->sk; 1036 struct sk_buff_head *q; 1037 unsigned long flags; 1038 u32 lo, hi; 1039 u16 len; 1040 1041 mm_unaccount_pinned_pages(&uarg->mmp); 1042 1043 /* if !len, there was only 1 call, and it was aborted 1044 * so do not queue a completion notification 1045 */ 1046 if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 1047 goto release; 1048 1049 len = uarg->len; 1050 lo = uarg->id; 1051 hi = uarg->id + len - 1; 1052 1053 serr = SKB_EXT_ERR(skb); 1054 memset(serr, 0, sizeof(*serr)); 1055 serr->ee.ee_errno = 0; 1056 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 1057 serr->ee.ee_data = hi; 1058 serr->ee.ee_info = lo; 1059 if (!success) 1060 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 1061 1062 q = &sk->sk_error_queue; 1063 spin_lock_irqsave(&q->lock, flags); 1064 tail = skb_peek_tail(q); 1065 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 1066 !skb_zerocopy_notify_extend(tail, lo, len)) { 1067 __skb_queue_tail(q, skb); 1068 skb = NULL; 1069 } 1070 spin_unlock_irqrestore(&q->lock, flags); 1071 1072 sk->sk_error_report(sk); 1073 1074 release: 1075 consume_skb(skb); 1076 sock_put(sk); 1077 } 1078 EXPORT_SYMBOL_GPL(sock_zerocopy_callback); 1079 1080 void sock_zerocopy_put(struct ubuf_info *uarg) 1081 { 1082 if (uarg && refcount_dec_and_test(&uarg->refcnt)) { 1083 if (uarg->callback) 1084 uarg->callback(uarg, uarg->zerocopy); 1085 else 1086 consume_skb(skb_from_uarg(uarg)); 1087 } 1088 } 1089 EXPORT_SYMBOL_GPL(sock_zerocopy_put); 1090 1091 void sock_zerocopy_put_abort(struct ubuf_info *uarg) 1092 { 1093 if (uarg) { 1094 struct sock *sk = skb_from_uarg(uarg)->sk; 1095 1096 atomic_dec(&sk->sk_zckey); 1097 uarg->len--; 1098 1099 sock_zerocopy_put(uarg); 1100 } 1101 } 1102 EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort); 1103 1104 extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb, 1105 struct iov_iter *from, size_t length); 1106 1107 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 1108 struct msghdr *msg, int len, 1109 struct ubuf_info *uarg) 1110 { 1111 struct ubuf_info *orig_uarg = skb_zcopy(skb); 1112 struct iov_iter orig_iter = msg->msg_iter; 1113 int err, orig_len = skb->len; 1114 1115 /* An skb can only point to one uarg. This edge case happens when 1116 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 1117 */ 1118 if (orig_uarg && uarg != orig_uarg) 1119 return -EEXIST; 1120 1121 err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 1122 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 1123 struct sock *save_sk = skb->sk; 1124 1125 /* Streams do not free skb on error. Reset to prev state. */ 1126 msg->msg_iter = orig_iter; 1127 skb->sk = sk; 1128 ___pskb_trim(skb, orig_len); 1129 skb->sk = save_sk; 1130 return err; 1131 } 1132 1133 skb_zcopy_set(skb, uarg); 1134 return skb->len - orig_len; 1135 } 1136 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 1137 1138 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 1139 gfp_t gfp_mask) 1140 { 1141 if (skb_zcopy(orig)) { 1142 if (skb_zcopy(nskb)) { 1143 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 1144 if (!gfp_mask) { 1145 WARN_ON_ONCE(1); 1146 return -ENOMEM; 1147 } 1148 if (skb_uarg(nskb) == skb_uarg(orig)) 1149 return 0; 1150 if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 1151 return -EIO; 1152 } 1153 skb_zcopy_set(nskb, skb_uarg(orig)); 1154 } 1155 return 0; 1156 } 1157 1158 /** 1159 * skb_copy_ubufs - copy userspace skb frags buffers to kernel 1160 * @skb: the skb to modify 1161 * @gfp_mask: allocation priority 1162 * 1163 * This must be called on SKBTX_DEV_ZEROCOPY skb. 1164 * It will copy all frags into kernel and drop the reference 1165 * to userspace pages. 1166 * 1167 * If this function is called from an interrupt gfp_mask() must be 1168 * %GFP_ATOMIC. 1169 * 1170 * Returns 0 on success or a negative error code on failure 1171 * to allocate kernel memory to copy to. 1172 */ 1173 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1174 { 1175 int num_frags = skb_shinfo(skb)->nr_frags; 1176 struct page *page, *head = NULL; 1177 int i, new_frags; 1178 u32 d_off; 1179 1180 if (!num_frags) 1181 return 0; 1182 1183 if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 1184 return -EINVAL; 1185 1186 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 1187 for (i = 0; i < new_frags; i++) { 1188 page = alloc_page(gfp_mask); 1189 if (!page) { 1190 while (head) { 1191 struct page *next = (struct page *)page_private(head); 1192 put_page(head); 1193 head = next; 1194 } 1195 return -ENOMEM; 1196 } 1197 set_page_private(page, (unsigned long)head); 1198 head = page; 1199 } 1200 1201 page = head; 1202 d_off = 0; 1203 for (i = 0; i < num_frags; i++) { 1204 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 1205 u32 p_off, p_len, copied; 1206 struct page *p; 1207 u8 *vaddr; 1208 1209 skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f), 1210 p, p_off, p_len, copied) { 1211 u32 copy, done = 0; 1212 vaddr = kmap_atomic(p); 1213 1214 while (done < p_len) { 1215 if (d_off == PAGE_SIZE) { 1216 d_off = 0; 1217 page = (struct page *)page_private(page); 1218 } 1219 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 1220 memcpy(page_address(page) + d_off, 1221 vaddr + p_off + done, copy); 1222 done += copy; 1223 d_off += copy; 1224 } 1225 kunmap_atomic(vaddr); 1226 } 1227 } 1228 1229 /* skb frags release userspace buffers */ 1230 for (i = 0; i < num_frags; i++) 1231 skb_frag_unref(skb, i); 1232 1233 /* skb frags point to kernel buffers */ 1234 for (i = 0; i < new_frags - 1; i++) { 1235 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 1236 head = (struct page *)page_private(head); 1237 } 1238 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 1239 skb_shinfo(skb)->nr_frags = new_frags; 1240 1241 skb_zcopy_clear(skb, false); 1242 return 0; 1243 } 1244 EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1245 1246 /** 1247 * skb_clone - duplicate an sk_buff 1248 * @skb: buffer to clone 1249 * @gfp_mask: allocation priority 1250 * 1251 * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1252 * copies share the same packet data but not structure. The new 1253 * buffer has a reference count of 1. If the allocation fails the 1254 * function returns %NULL otherwise the new buffer is returned. 1255 * 1256 * If this function is called from an interrupt gfp_mask() must be 1257 * %GFP_ATOMIC. 1258 */ 1259 1260 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1261 { 1262 struct sk_buff_fclones *fclones = container_of(skb, 1263 struct sk_buff_fclones, 1264 skb1); 1265 struct sk_buff *n; 1266 1267 if (skb_orphan_frags(skb, gfp_mask)) 1268 return NULL; 1269 1270 if (skb->fclone == SKB_FCLONE_ORIG && 1271 refcount_read(&fclones->fclone_ref) == 1) { 1272 n = &fclones->skb2; 1273 refcount_set(&fclones->fclone_ref, 2); 1274 } else { 1275 if (skb_pfmemalloc(skb)) 1276 gfp_mask |= __GFP_MEMALLOC; 1277 1278 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1279 if (!n) 1280 return NULL; 1281 1282 n->fclone = SKB_FCLONE_UNAVAILABLE; 1283 } 1284 1285 return __skb_clone(n, skb); 1286 } 1287 EXPORT_SYMBOL(skb_clone); 1288 1289 static void skb_headers_offset_update(struct sk_buff *skb, int off) 1290 { 1291 /* Only adjust this if it actually is csum_start rather than csum */ 1292 if (skb->ip_summed == CHECKSUM_PARTIAL) 1293 skb->csum_start += off; 1294 /* {transport,network,mac}_header and tail are relative to skb->head */ 1295 skb->transport_header += off; 1296 skb->network_header += off; 1297 if (skb_mac_header_was_set(skb)) 1298 skb->mac_header += off; 1299 skb->inner_transport_header += off; 1300 skb->inner_network_header += off; 1301 skb->inner_mac_header += off; 1302 } 1303 1304 static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1305 { 1306 __copy_skb_header(new, old); 1307 1308 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 1309 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 1310 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 1311 } 1312 1313 static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1314 { 1315 if (skb_pfmemalloc(skb)) 1316 return SKB_ALLOC_RX; 1317 return 0; 1318 } 1319 1320 /** 1321 * skb_copy - create private copy of an sk_buff 1322 * @skb: buffer to copy 1323 * @gfp_mask: allocation priority 1324 * 1325 * Make a copy of both an &sk_buff and its data. This is used when the 1326 * caller wishes to modify the data and needs a private copy of the 1327 * data to alter. Returns %NULL on failure or the pointer to the buffer 1328 * on success. The returned buffer has a reference count of 1. 1329 * 1330 * As by-product this function converts non-linear &sk_buff to linear 1331 * one, so that &sk_buff becomes completely private and caller is allowed 1332 * to modify all the data of returned buffer. This means that this 1333 * function is not recommended for use in circumstances when only 1334 * header is going to be modified. Use pskb_copy() instead. 1335 */ 1336 1337 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 1338 { 1339 int headerlen = skb_headroom(skb); 1340 unsigned int size = skb_end_offset(skb) + skb->data_len; 1341 struct sk_buff *n = __alloc_skb(size, gfp_mask, 1342 skb_alloc_rx_flag(skb), NUMA_NO_NODE); 1343 1344 if (!n) 1345 return NULL; 1346 1347 /* Set the data pointer */ 1348 skb_reserve(n, headerlen); 1349 /* Set the tail pointer and length */ 1350 skb_put(n, skb->len); 1351 1352 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 1353 1354 copy_skb_header(n, skb); 1355 return n; 1356 } 1357 EXPORT_SYMBOL(skb_copy); 1358 1359 /** 1360 * __pskb_copy_fclone - create copy of an sk_buff with private head. 1361 * @skb: buffer to copy 1362 * @headroom: headroom of new skb 1363 * @gfp_mask: allocation priority 1364 * @fclone: if true allocate the copy of the skb from the fclone 1365 * cache instead of the head cache; it is recommended to set this 1366 * to true for the cases where the copy will likely be cloned 1367 * 1368 * Make a copy of both an &sk_buff and part of its data, located 1369 * in header. Fragmented data remain shared. This is used when 1370 * the caller wishes to modify only header of &sk_buff and needs 1371 * private copy of the header to alter. Returns %NULL on failure 1372 * or the pointer to the buffer on success. 1373 * The returned buffer has a reference count of 1. 1374 */ 1375 1376 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1377 gfp_t gfp_mask, bool fclone) 1378 { 1379 unsigned int size = skb_headlen(skb) + headroom; 1380 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1381 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 1382 1383 if (!n) 1384 goto out; 1385 1386 /* Set the data pointer */ 1387 skb_reserve(n, headroom); 1388 /* Set the tail pointer and length */ 1389 skb_put(n, skb_headlen(skb)); 1390 /* Copy the bytes */ 1391 skb_copy_from_linear_data(skb, n->data, n->len); 1392 1393 n->truesize += skb->data_len; 1394 n->data_len = skb->data_len; 1395 n->len = skb->len; 1396 1397 if (skb_shinfo(skb)->nr_frags) { 1398 int i; 1399 1400 if (skb_orphan_frags(skb, gfp_mask) || 1401 skb_zerocopy_clone(n, skb, gfp_mask)) { 1402 kfree_skb(n); 1403 n = NULL; 1404 goto out; 1405 } 1406 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1407 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1408 skb_frag_ref(skb, i); 1409 } 1410 skb_shinfo(n)->nr_frags = i; 1411 } 1412 1413 if (skb_has_frag_list(skb)) { 1414 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 1415 skb_clone_fraglist(n); 1416 } 1417 1418 copy_skb_header(n, skb); 1419 out: 1420 return n; 1421 } 1422 EXPORT_SYMBOL(__pskb_copy_fclone); 1423 1424 /** 1425 * pskb_expand_head - reallocate header of &sk_buff 1426 * @skb: buffer to reallocate 1427 * @nhead: room to add at head 1428 * @ntail: room to add at tail 1429 * @gfp_mask: allocation priority 1430 * 1431 * Expands (or creates identical copy, if @nhead and @ntail are zero) 1432 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 1433 * reference count of 1. Returns zero in the case of success or error, 1434 * if expansion failed. In the last case, &sk_buff is not changed. 1435 * 1436 * All the pointers pointing into skb header may change and must be 1437 * reloaded after call to this function. 1438 */ 1439 1440 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1441 gfp_t gfp_mask) 1442 { 1443 int i, osize = skb_end_offset(skb); 1444 int size = osize + nhead + ntail; 1445 long off; 1446 u8 *data; 1447 1448 BUG_ON(nhead < 0); 1449 1450 BUG_ON(skb_shared(skb)); 1451 1452 size = SKB_DATA_ALIGN(size); 1453 1454 if (skb_pfmemalloc(skb)) 1455 gfp_mask |= __GFP_MEMALLOC; 1456 data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1457 gfp_mask, NUMA_NO_NODE, NULL); 1458 if (!data) 1459 goto nodata; 1460 size = SKB_WITH_OVERHEAD(ksize(data)); 1461 1462 /* Copy only real data... and, alas, header. This should be 1463 * optimized for the cases when header is void. 1464 */ 1465 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 1466 1467 memcpy((struct skb_shared_info *)(data + size), 1468 skb_shinfo(skb), 1469 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 1470 1471 /* 1472 * if shinfo is shared we must drop the old head gracefully, but if it 1473 * is not we can just drop the old head and let the existing refcount 1474 * be since all we did is relocate the values 1475 */ 1476 if (skb_cloned(skb)) { 1477 if (skb_orphan_frags(skb, gfp_mask)) 1478 goto nofrags; 1479 if (skb_zcopy(skb)) 1480 refcount_inc(&skb_uarg(skb)->refcnt); 1481 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1482 skb_frag_ref(skb, i); 1483 1484 if (skb_has_frag_list(skb)) 1485 skb_clone_fraglist(skb); 1486 1487 skb_release_data(skb); 1488 } else { 1489 skb_free_head(skb); 1490 } 1491 off = (data + nhead) - skb->head; 1492 1493 skb->head = data; 1494 skb->head_frag = 0; 1495 skb->data += off; 1496 #ifdef NET_SKBUFF_DATA_USES_OFFSET 1497 skb->end = size; 1498 off = nhead; 1499 #else 1500 skb->end = skb->head + size; 1501 #endif 1502 skb->tail += off; 1503 skb_headers_offset_update(skb, nhead); 1504 skb->cloned = 0; 1505 skb->hdr_len = 0; 1506 skb->nohdr = 0; 1507 atomic_set(&skb_shinfo(skb)->dataref, 1); 1508 1509 skb_metadata_clear(skb); 1510 1511 /* It is not generally safe to change skb->truesize. 1512 * For the moment, we really care of rx path, or 1513 * when skb is orphaned (not attached to a socket). 1514 */ 1515 if (!skb->sk || skb->destructor == sock_edemux) 1516 skb->truesize += size - osize; 1517 1518 return 0; 1519 1520 nofrags: 1521 kfree(data); 1522 nodata: 1523 return -ENOMEM; 1524 } 1525 EXPORT_SYMBOL(pskb_expand_head); 1526 1527 /* Make private copy of skb with writable head and some headroom */ 1528 1529 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 1530 { 1531 struct sk_buff *skb2; 1532 int delta = headroom - skb_headroom(skb); 1533 1534 if (delta <= 0) 1535 skb2 = pskb_copy(skb, GFP_ATOMIC); 1536 else { 1537 skb2 = skb_clone(skb, GFP_ATOMIC); 1538 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 1539 GFP_ATOMIC)) { 1540 kfree_skb(skb2); 1541 skb2 = NULL; 1542 } 1543 } 1544 return skb2; 1545 } 1546 EXPORT_SYMBOL(skb_realloc_headroom); 1547 1548 /** 1549 * skb_copy_expand - copy and expand sk_buff 1550 * @skb: buffer to copy 1551 * @newheadroom: new free bytes at head 1552 * @newtailroom: new free bytes at tail 1553 * @gfp_mask: allocation priority 1554 * 1555 * Make a copy of both an &sk_buff and its data and while doing so 1556 * allocate additional space. 1557 * 1558 * This is used when the caller wishes to modify the data and needs a 1559 * private copy of the data to alter as well as more space for new fields. 1560 * Returns %NULL on failure or the pointer to the buffer 1561 * on success. The returned buffer has a reference count of 1. 1562 * 1563 * You must pass %GFP_ATOMIC as the allocation priority if this function 1564 * is called from an interrupt. 1565 */ 1566 struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 1567 int newheadroom, int newtailroom, 1568 gfp_t gfp_mask) 1569 { 1570 /* 1571 * Allocate the copy buffer 1572 */ 1573 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1574 gfp_mask, skb_alloc_rx_flag(skb), 1575 NUMA_NO_NODE); 1576 int oldheadroom = skb_headroom(skb); 1577 int head_copy_len, head_copy_off; 1578 1579 if (!n) 1580 return NULL; 1581 1582 skb_reserve(n, newheadroom); 1583 1584 /* Set the tail pointer and length */ 1585 skb_put(n, skb->len); 1586 1587 head_copy_len = oldheadroom; 1588 head_copy_off = 0; 1589 if (newheadroom <= head_copy_len) 1590 head_copy_len = newheadroom; 1591 else 1592 head_copy_off = newheadroom - head_copy_len; 1593 1594 /* Copy the linear header and data. */ 1595 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 1596 skb->len + head_copy_len)); 1597 1598 copy_skb_header(n, skb); 1599 1600 skb_headers_offset_update(n, newheadroom - oldheadroom); 1601 1602 return n; 1603 } 1604 EXPORT_SYMBOL(skb_copy_expand); 1605 1606 /** 1607 * __skb_pad - zero pad the tail of an skb 1608 * @skb: buffer to pad 1609 * @pad: space to pad 1610 * @free_on_error: free buffer on error 1611 * 1612 * Ensure that a buffer is followed by a padding area that is zero 1613 * filled. Used by network drivers which may DMA or transfer data 1614 * beyond the buffer end onto the wire. 1615 * 1616 * May return error in out of memory cases. The skb is freed on error 1617 * if @free_on_error is true. 1618 */ 1619 1620 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 1621 { 1622 int err; 1623 int ntail; 1624 1625 /* If the skbuff is non linear tailroom is always zero.. */ 1626 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 1627 memset(skb->data+skb->len, 0, pad); 1628 return 0; 1629 } 1630 1631 ntail = skb->data_len + pad - (skb->end - skb->tail); 1632 if (likely(skb_cloned(skb) || ntail > 0)) { 1633 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 1634 if (unlikely(err)) 1635 goto free_skb; 1636 } 1637 1638 /* FIXME: The use of this function with non-linear skb's really needs 1639 * to be audited. 1640 */ 1641 err = skb_linearize(skb); 1642 if (unlikely(err)) 1643 goto free_skb; 1644 1645 memset(skb->data + skb->len, 0, pad); 1646 return 0; 1647 1648 free_skb: 1649 if (free_on_error) 1650 kfree_skb(skb); 1651 return err; 1652 } 1653 EXPORT_SYMBOL(__skb_pad); 1654 1655 /** 1656 * pskb_put - add data to the tail of a potentially fragmented buffer 1657 * @skb: start of the buffer to use 1658 * @tail: tail fragment of the buffer to use 1659 * @len: amount of data to add 1660 * 1661 * This function extends the used data area of the potentially 1662 * fragmented buffer. @tail must be the last fragment of @skb -- or 1663 * @skb itself. If this would exceed the total buffer size the kernel 1664 * will panic. A pointer to the first byte of the extra data is 1665 * returned. 1666 */ 1667 1668 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 1669 { 1670 if (tail != skb) { 1671 skb->data_len += len; 1672 skb->len += len; 1673 } 1674 return skb_put(tail, len); 1675 } 1676 EXPORT_SYMBOL_GPL(pskb_put); 1677 1678 /** 1679 * skb_put - add data to a buffer 1680 * @skb: buffer to use 1681 * @len: amount of data to add 1682 * 1683 * This function extends the used data area of the buffer. If this would 1684 * exceed the total buffer size the kernel will panic. A pointer to the 1685 * first byte of the extra data is returned. 1686 */ 1687 void *skb_put(struct sk_buff *skb, unsigned int len) 1688 { 1689 void *tmp = skb_tail_pointer(skb); 1690 SKB_LINEAR_ASSERT(skb); 1691 skb->tail += len; 1692 skb->len += len; 1693 if (unlikely(skb->tail > skb->end)) 1694 skb_over_panic(skb, len, __builtin_return_address(0)); 1695 return tmp; 1696 } 1697 EXPORT_SYMBOL(skb_put); 1698 1699 /** 1700 * skb_push - add data to the start of a buffer 1701 * @skb: buffer to use 1702 * @len: amount of data to add 1703 * 1704 * This function extends the used data area of the buffer at the buffer 1705 * start. If this would exceed the total buffer headroom the kernel will 1706 * panic. A pointer to the first byte of the extra data is returned. 1707 */ 1708 void *skb_push(struct sk_buff *skb, unsigned int len) 1709 { 1710 skb->data -= len; 1711 skb->len += len; 1712 if (unlikely(skb->data<skb->head)) 1713 skb_under_panic(skb, len, __builtin_return_address(0)); 1714 return skb->data; 1715 } 1716 EXPORT_SYMBOL(skb_push); 1717 1718 /** 1719 * skb_pull - remove data from the start of a buffer 1720 * @skb: buffer to use 1721 * @len: amount of data to remove 1722 * 1723 * This function removes data from the start of a buffer, returning 1724 * the memory to the headroom. A pointer to the next data in the buffer 1725 * is returned. Once the data has been pulled future pushes will overwrite 1726 * the old data. 1727 */ 1728 void *skb_pull(struct sk_buff *skb, unsigned int len) 1729 { 1730 return skb_pull_inline(skb, len); 1731 } 1732 EXPORT_SYMBOL(skb_pull); 1733 1734 /** 1735 * skb_trim - remove end from a buffer 1736 * @skb: buffer to alter 1737 * @len: new length 1738 * 1739 * Cut the length of a buffer down by removing data from the tail. If 1740 * the buffer is already under the length specified it is not modified. 1741 * The skb must be linear. 1742 */ 1743 void skb_trim(struct sk_buff *skb, unsigned int len) 1744 { 1745 if (skb->len > len) 1746 __skb_trim(skb, len); 1747 } 1748 EXPORT_SYMBOL(skb_trim); 1749 1750 /* Trims skb to length len. It can change skb pointers. 1751 */ 1752 1753 int ___pskb_trim(struct sk_buff *skb, unsigned int len) 1754 { 1755 struct sk_buff **fragp; 1756 struct sk_buff *frag; 1757 int offset = skb_headlen(skb); 1758 int nfrags = skb_shinfo(skb)->nr_frags; 1759 int i; 1760 int err; 1761 1762 if (skb_cloned(skb) && 1763 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 1764 return err; 1765 1766 i = 0; 1767 if (offset >= len) 1768 goto drop_pages; 1769 1770 for (; i < nfrags; i++) { 1771 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 1772 1773 if (end < len) { 1774 offset = end; 1775 continue; 1776 } 1777 1778 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 1779 1780 drop_pages: 1781 skb_shinfo(skb)->nr_frags = i; 1782 1783 for (; i < nfrags; i++) 1784 skb_frag_unref(skb, i); 1785 1786 if (skb_has_frag_list(skb)) 1787 skb_drop_fraglist(skb); 1788 goto done; 1789 } 1790 1791 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 1792 fragp = &frag->next) { 1793 int end = offset + frag->len; 1794 1795 if (skb_shared(frag)) { 1796 struct sk_buff *nfrag; 1797 1798 nfrag = skb_clone(frag, GFP_ATOMIC); 1799 if (unlikely(!nfrag)) 1800 return -ENOMEM; 1801 1802 nfrag->next = frag->next; 1803 consume_skb(frag); 1804 frag = nfrag; 1805 *fragp = frag; 1806 } 1807 1808 if (end < len) { 1809 offset = end; 1810 continue; 1811 } 1812 1813 if (end > len && 1814 unlikely((err = pskb_trim(frag, len - offset)))) 1815 return err; 1816 1817 if (frag->next) 1818 skb_drop_list(&frag->next); 1819 break; 1820 } 1821 1822 done: 1823 if (len > skb_headlen(skb)) { 1824 skb->data_len -= skb->len - len; 1825 skb->len = len; 1826 } else { 1827 skb->len = len; 1828 skb->data_len = 0; 1829 skb_set_tail_pointer(skb, len); 1830 } 1831 1832 if (!skb->sk || skb->destructor == sock_edemux) 1833 skb_condense(skb); 1834 return 0; 1835 } 1836 EXPORT_SYMBOL(___pskb_trim); 1837 1838 /** 1839 * __pskb_pull_tail - advance tail of skb header 1840 * @skb: buffer to reallocate 1841 * @delta: number of bytes to advance tail 1842 * 1843 * The function makes a sense only on a fragmented &sk_buff, 1844 * it expands header moving its tail forward and copying necessary 1845 * data from fragmented part. 1846 * 1847 * &sk_buff MUST have reference count of 1. 1848 * 1849 * Returns %NULL (and &sk_buff does not change) if pull failed 1850 * or value of new tail of skb in the case of success. 1851 * 1852 * All the pointers pointing into skb header may change and must be 1853 * reloaded after call to this function. 1854 */ 1855 1856 /* Moves tail of skb head forward, copying data from fragmented part, 1857 * when it is necessary. 1858 * 1. It may fail due to malloc failure. 1859 * 2. It may change skb pointers. 1860 * 1861 * It is pretty complicated. Luckily, it is called only in exceptional cases. 1862 */ 1863 void *__pskb_pull_tail(struct sk_buff *skb, int delta) 1864 { 1865 /* If skb has not enough free space at tail, get new one 1866 * plus 128 bytes for future expansions. If we have enough 1867 * room at tail, reallocate without expansion only if skb is cloned. 1868 */ 1869 int i, k, eat = (skb->tail + delta) - skb->end; 1870 1871 if (eat > 0 || skb_cloned(skb)) { 1872 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 1873 GFP_ATOMIC)) 1874 return NULL; 1875 } 1876 1877 BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 1878 skb_tail_pointer(skb), delta)); 1879 1880 /* Optimization: no fragments, no reasons to preestimate 1881 * size of pulled pages. Superb. 1882 */ 1883 if (!skb_has_frag_list(skb)) 1884 goto pull_pages; 1885 1886 /* Estimate size of pulled pages. */ 1887 eat = delta; 1888 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1889 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 1890 1891 if (size >= eat) 1892 goto pull_pages; 1893 eat -= size; 1894 } 1895 1896 /* If we need update frag list, we are in troubles. 1897 * Certainly, it is possible to add an offset to skb data, 1898 * but taking into account that pulling is expected to 1899 * be very rare operation, it is worth to fight against 1900 * further bloating skb head and crucify ourselves here instead. 1901 * Pure masohism, indeed. 8)8) 1902 */ 1903 if (eat) { 1904 struct sk_buff *list = skb_shinfo(skb)->frag_list; 1905 struct sk_buff *clone = NULL; 1906 struct sk_buff *insp = NULL; 1907 1908 do { 1909 BUG_ON(!list); 1910 1911 if (list->len <= eat) { 1912 /* Eaten as whole. */ 1913 eat -= list->len; 1914 list = list->next; 1915 insp = list; 1916 } else { 1917 /* Eaten partially. */ 1918 1919 if (skb_shared(list)) { 1920 /* Sucks! We need to fork list. :-( */ 1921 clone = skb_clone(list, GFP_ATOMIC); 1922 if (!clone) 1923 return NULL; 1924 insp = list->next; 1925 list = clone; 1926 } else { 1927 /* This may be pulled without 1928 * problems. */ 1929 insp = list; 1930 } 1931 if (!pskb_pull(list, eat)) { 1932 kfree_skb(clone); 1933 return NULL; 1934 } 1935 break; 1936 } 1937 } while (eat); 1938 1939 /* Free pulled out fragments. */ 1940 while ((list = skb_shinfo(skb)->frag_list) != insp) { 1941 skb_shinfo(skb)->frag_list = list->next; 1942 kfree_skb(list); 1943 } 1944 /* And insert new clone at head. */ 1945 if (clone) { 1946 clone->next = list; 1947 skb_shinfo(skb)->frag_list = clone; 1948 } 1949 } 1950 /* Success! Now we may commit changes to skb data. */ 1951 1952 pull_pages: 1953 eat = delta; 1954 k = 0; 1955 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1956 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 1957 1958 if (size <= eat) { 1959 skb_frag_unref(skb, i); 1960 eat -= size; 1961 } else { 1962 skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 1963 if (eat) { 1964 skb_shinfo(skb)->frags[k].page_offset += eat; 1965 skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 1966 if (!i) 1967 goto end; 1968 eat = 0; 1969 } 1970 k++; 1971 } 1972 } 1973 skb_shinfo(skb)->nr_frags = k; 1974 1975 end: 1976 skb->tail += delta; 1977 skb->data_len -= delta; 1978 1979 if (!skb->data_len) 1980 skb_zcopy_clear(skb, false); 1981 1982 return skb_tail_pointer(skb); 1983 } 1984 EXPORT_SYMBOL(__pskb_pull_tail); 1985 1986 /** 1987 * skb_copy_bits - copy bits from skb to kernel buffer 1988 * @skb: source skb 1989 * @offset: offset in source 1990 * @to: destination buffer 1991 * @len: number of bytes to copy 1992 * 1993 * Copy the specified number of bytes from the source skb to the 1994 * destination buffer. 1995 * 1996 * CAUTION ! : 1997 * If its prototype is ever changed, 1998 * check arch/{*}/net/{*}.S files, 1999 * since it is called from BPF assembly code. 2000 */ 2001 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 2002 { 2003 int start = skb_headlen(skb); 2004 struct sk_buff *frag_iter; 2005 int i, copy; 2006 2007 if (offset > (int)skb->len - len) 2008 goto fault; 2009 2010 /* Copy header. */ 2011 if ((copy = start - offset) > 0) { 2012 if (copy > len) 2013 copy = len; 2014 skb_copy_from_linear_data_offset(skb, offset, to, copy); 2015 if ((len -= copy) == 0) 2016 return 0; 2017 offset += copy; 2018 to += copy; 2019 } 2020 2021 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2022 int end; 2023 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 2024 2025 WARN_ON(start > offset + len); 2026 2027 end = start + skb_frag_size(f); 2028 if ((copy = end - offset) > 0) { 2029 u32 p_off, p_len, copied; 2030 struct page *p; 2031 u8 *vaddr; 2032 2033 if (copy > len) 2034 copy = len; 2035 2036 skb_frag_foreach_page(f, 2037 f->page_offset + offset - start, 2038 copy, p, p_off, p_len, copied) { 2039 vaddr = kmap_atomic(p); 2040 memcpy(to + copied, vaddr + p_off, p_len); 2041 kunmap_atomic(vaddr); 2042 } 2043 2044 if ((len -= copy) == 0) 2045 return 0; 2046 offset += copy; 2047 to += copy; 2048 } 2049 start = end; 2050 } 2051 2052 skb_walk_frags(skb, frag_iter) { 2053 int end; 2054 2055 WARN_ON(start > offset + len); 2056 2057 end = start + frag_iter->len; 2058 if ((copy = end - offset) > 0) { 2059 if (copy > len) 2060 copy = len; 2061 if (skb_copy_bits(frag_iter, offset - start, to, copy)) 2062 goto fault; 2063 if ((len -= copy) == 0) 2064 return 0; 2065 offset += copy; 2066 to += copy; 2067 } 2068 start = end; 2069 } 2070 2071 if (!len) 2072 return 0; 2073 2074 fault: 2075 return -EFAULT; 2076 } 2077 EXPORT_SYMBOL(skb_copy_bits); 2078 2079 /* 2080 * Callback from splice_to_pipe(), if we need to release some pages 2081 * at the end of the spd in case we error'ed out in filling the pipe. 2082 */ 2083 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 2084 { 2085 put_page(spd->pages[i]); 2086 } 2087 2088 static struct page *linear_to_page(struct page *page, unsigned int *len, 2089 unsigned int *offset, 2090 struct sock *sk) 2091 { 2092 struct page_frag *pfrag = sk_page_frag(sk); 2093 2094 if (!sk_page_frag_refill(sk, pfrag)) 2095 return NULL; 2096 2097 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 2098 2099 memcpy(page_address(pfrag->page) + pfrag->offset, 2100 page_address(page) + *offset, *len); 2101 *offset = pfrag->offset; 2102 pfrag->offset += *len; 2103 2104 return pfrag->page; 2105 } 2106 2107 static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 2108 struct page *page, 2109 unsigned int offset) 2110 { 2111 return spd->nr_pages && 2112 spd->pages[spd->nr_pages - 1] == page && 2113 (spd->partial[spd->nr_pages - 1].offset + 2114 spd->partial[spd->nr_pages - 1].len == offset); 2115 } 2116 2117 /* 2118 * Fill page/offset/length into spd, if it can hold more pages. 2119 */ 2120 static bool spd_fill_page(struct splice_pipe_desc *spd, 2121 struct pipe_inode_info *pipe, struct page *page, 2122 unsigned int *len, unsigned int offset, 2123 bool linear, 2124 struct sock *sk) 2125 { 2126 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2127 return true; 2128 2129 if (linear) { 2130 page = linear_to_page(page, len, &offset, sk); 2131 if (!page) 2132 return true; 2133 } 2134 if (spd_can_coalesce(spd, page, offset)) { 2135 spd->partial[spd->nr_pages - 1].len += *len; 2136 return false; 2137 } 2138 get_page(page); 2139 spd->pages[spd->nr_pages] = page; 2140 spd->partial[spd->nr_pages].len = *len; 2141 spd->partial[spd->nr_pages].offset = offset; 2142 spd->nr_pages++; 2143 2144 return false; 2145 } 2146 2147 static bool __splice_segment(struct page *page, unsigned int poff, 2148 unsigned int plen, unsigned int *off, 2149 unsigned int *len, 2150 struct splice_pipe_desc *spd, bool linear, 2151 struct sock *sk, 2152 struct pipe_inode_info *pipe) 2153 { 2154 if (!*len) 2155 return true; 2156 2157 /* skip this segment if already processed */ 2158 if (*off >= plen) { 2159 *off -= plen; 2160 return false; 2161 } 2162 2163 /* ignore any bits we already processed */ 2164 poff += *off; 2165 plen -= *off; 2166 *off = 0; 2167 2168 do { 2169 unsigned int flen = min(*len, plen); 2170 2171 if (spd_fill_page(spd, pipe, page, &flen, poff, 2172 linear, sk)) 2173 return true; 2174 poff += flen; 2175 plen -= flen; 2176 *len -= flen; 2177 } while (*len && plen); 2178 2179 return false; 2180 } 2181 2182 /* 2183 * Map linear and fragment data from the skb to spd. It reports true if the 2184 * pipe is full or if we already spliced the requested length. 2185 */ 2186 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 2187 unsigned int *offset, unsigned int *len, 2188 struct splice_pipe_desc *spd, struct sock *sk) 2189 { 2190 int seg; 2191 struct sk_buff *iter; 2192 2193 /* map the linear part : 2194 * If skb->head_frag is set, this 'linear' part is backed by a 2195 * fragment, and if the head is not shared with any clones then 2196 * we can avoid a copy since we own the head portion of this page. 2197 */ 2198 if (__splice_segment(virt_to_page(skb->data), 2199 (unsigned long) skb->data & (PAGE_SIZE - 1), 2200 skb_headlen(skb), 2201 offset, len, spd, 2202 skb_head_is_locked(skb), 2203 sk, pipe)) 2204 return true; 2205 2206 /* 2207 * then map the fragments 2208 */ 2209 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 2210 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 2211 2212 if (__splice_segment(skb_frag_page(f), 2213 f->page_offset, skb_frag_size(f), 2214 offset, len, spd, false, sk, pipe)) 2215 return true; 2216 } 2217 2218 skb_walk_frags(skb, iter) { 2219 if (*offset >= iter->len) { 2220 *offset -= iter->len; 2221 continue; 2222 } 2223 /* __skb_splice_bits() only fails if the output has no room 2224 * left, so no point in going over the frag_list for the error 2225 * case. 2226 */ 2227 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2228 return true; 2229 } 2230 2231 return false; 2232 } 2233 2234 /* 2235 * Map data from the skb to a pipe. Should handle both the linear part, 2236 * the fragments, and the frag list. 2237 */ 2238 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 2239 struct pipe_inode_info *pipe, unsigned int tlen, 2240 unsigned int flags) 2241 { 2242 struct partial_page partial[MAX_SKB_FRAGS]; 2243 struct page *pages[MAX_SKB_FRAGS]; 2244 struct splice_pipe_desc spd = { 2245 .pages = pages, 2246 .partial = partial, 2247 .nr_pages_max = MAX_SKB_FRAGS, 2248 .ops = &nosteal_pipe_buf_ops, 2249 .spd_release = sock_spd_release, 2250 }; 2251 int ret = 0; 2252 2253 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 2254 2255 if (spd.nr_pages) 2256 ret = splice_to_pipe(pipe, &spd); 2257 2258 return ret; 2259 } 2260 EXPORT_SYMBOL_GPL(skb_splice_bits); 2261 2262 /* Send skb data on a socket. Socket must be locked. */ 2263 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 2264 int len) 2265 { 2266 unsigned int orig_len = len; 2267 struct sk_buff *head = skb; 2268 unsigned short fragidx; 2269 int slen, ret; 2270 2271 do_frag_list: 2272 2273 /* Deal with head data */ 2274 while (offset < skb_headlen(skb) && len) { 2275 struct kvec kv; 2276 struct msghdr msg; 2277 2278 slen = min_t(int, len, skb_headlen(skb) - offset); 2279 kv.iov_base = skb->data + offset; 2280 kv.iov_len = slen; 2281 memset(&msg, 0, sizeof(msg)); 2282 2283 ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen); 2284 if (ret <= 0) 2285 goto error; 2286 2287 offset += ret; 2288 len -= ret; 2289 } 2290 2291 /* All the data was skb head? */ 2292 if (!len) 2293 goto out; 2294 2295 /* Make offset relative to start of frags */ 2296 offset -= skb_headlen(skb); 2297 2298 /* Find where we are in frag list */ 2299 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 2300 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 2301 2302 if (offset < frag->size) 2303 break; 2304 2305 offset -= frag->size; 2306 } 2307 2308 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 2309 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 2310 2311 slen = min_t(size_t, len, frag->size - offset); 2312 2313 while (slen) { 2314 ret = kernel_sendpage_locked(sk, frag->page.p, 2315 frag->page_offset + offset, 2316 slen, MSG_DONTWAIT); 2317 if (ret <= 0) 2318 goto error; 2319 2320 len -= ret; 2321 offset += ret; 2322 slen -= ret; 2323 } 2324 2325 offset = 0; 2326 } 2327 2328 if (len) { 2329 /* Process any frag lists */ 2330 2331 if (skb == head) { 2332 if (skb_has_frag_list(skb)) { 2333 skb = skb_shinfo(skb)->frag_list; 2334 goto do_frag_list; 2335 } 2336 } else if (skb->next) { 2337 skb = skb->next; 2338 goto do_frag_list; 2339 } 2340 } 2341 2342 out: 2343 return orig_len - len; 2344 2345 error: 2346 return orig_len == len ? ret : orig_len - len; 2347 } 2348 EXPORT_SYMBOL_GPL(skb_send_sock_locked); 2349 2350 /* Send skb data on a socket. */ 2351 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 2352 { 2353 int ret = 0; 2354 2355 lock_sock(sk); 2356 ret = skb_send_sock_locked(sk, skb, offset, len); 2357 release_sock(sk); 2358 2359 return ret; 2360 } 2361 EXPORT_SYMBOL_GPL(skb_send_sock); 2362 2363 /** 2364 * skb_store_bits - store bits from kernel buffer to skb 2365 * @skb: destination buffer 2366 * @offset: offset in destination 2367 * @from: source buffer 2368 * @len: number of bytes to copy 2369 * 2370 * Copy the specified number of bytes from the source buffer to the 2371 * destination skb. This function handles all the messy bits of 2372 * traversing fragment lists and such. 2373 */ 2374 2375 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2376 { 2377 int start = skb_headlen(skb); 2378 struct sk_buff *frag_iter; 2379 int i, copy; 2380 2381 if (offset > (int)skb->len - len) 2382 goto fault; 2383 2384 if ((copy = start - offset) > 0) { 2385 if (copy > len) 2386 copy = len; 2387 skb_copy_to_linear_data_offset(skb, offset, from, copy); 2388 if ((len -= copy) == 0) 2389 return 0; 2390 offset += copy; 2391 from += copy; 2392 } 2393 2394 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2395 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2396 int end; 2397 2398 WARN_ON(start > offset + len); 2399 2400 end = start + skb_frag_size(frag); 2401 if ((copy = end - offset) > 0) { 2402 u32 p_off, p_len, copied; 2403 struct page *p; 2404 u8 *vaddr; 2405 2406 if (copy > len) 2407 copy = len; 2408 2409 skb_frag_foreach_page(frag, 2410 frag->page_offset + offset - start, 2411 copy, p, p_off, p_len, copied) { 2412 vaddr = kmap_atomic(p); 2413 memcpy(vaddr + p_off, from + copied, p_len); 2414 kunmap_atomic(vaddr); 2415 } 2416 2417 if ((len -= copy) == 0) 2418 return 0; 2419 offset += copy; 2420 from += copy; 2421 } 2422 start = end; 2423 } 2424 2425 skb_walk_frags(skb, frag_iter) { 2426 int end; 2427 2428 WARN_ON(start > offset + len); 2429 2430 end = start + frag_iter->len; 2431 if ((copy = end - offset) > 0) { 2432 if (copy > len) 2433 copy = len; 2434 if (skb_store_bits(frag_iter, offset - start, 2435 from, copy)) 2436 goto fault; 2437 if ((len -= copy) == 0) 2438 return 0; 2439 offset += copy; 2440 from += copy; 2441 } 2442 start = end; 2443 } 2444 if (!len) 2445 return 0; 2446 2447 fault: 2448 return -EFAULT; 2449 } 2450 EXPORT_SYMBOL(skb_store_bits); 2451 2452 /* Checksum skb data. */ 2453 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 2454 __wsum csum, const struct skb_checksum_ops *ops) 2455 { 2456 int start = skb_headlen(skb); 2457 int i, copy = start - offset; 2458 struct sk_buff *frag_iter; 2459 int pos = 0; 2460 2461 /* Checksum header. */ 2462 if (copy > 0) { 2463 if (copy > len) 2464 copy = len; 2465 csum = ops->update(skb->data + offset, copy, csum); 2466 if ((len -= copy) == 0) 2467 return csum; 2468 offset += copy; 2469 pos = copy; 2470 } 2471 2472 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2473 int end; 2474 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2475 2476 WARN_ON(start > offset + len); 2477 2478 end = start + skb_frag_size(frag); 2479 if ((copy = end - offset) > 0) { 2480 u32 p_off, p_len, copied; 2481 struct page *p; 2482 __wsum csum2; 2483 u8 *vaddr; 2484 2485 if (copy > len) 2486 copy = len; 2487 2488 skb_frag_foreach_page(frag, 2489 frag->page_offset + offset - start, 2490 copy, p, p_off, p_len, copied) { 2491 vaddr = kmap_atomic(p); 2492 csum2 = ops->update(vaddr + p_off, p_len, 0); 2493 kunmap_atomic(vaddr); 2494 csum = ops->combine(csum, csum2, pos, p_len); 2495 pos += p_len; 2496 } 2497 2498 if (!(len -= copy)) 2499 return csum; 2500 offset += copy; 2501 } 2502 start = end; 2503 } 2504 2505 skb_walk_frags(skb, frag_iter) { 2506 int end; 2507 2508 WARN_ON(start > offset + len); 2509 2510 end = start + frag_iter->len; 2511 if ((copy = end - offset) > 0) { 2512 __wsum csum2; 2513 if (copy > len) 2514 copy = len; 2515 csum2 = __skb_checksum(frag_iter, offset - start, 2516 copy, 0, ops); 2517 csum = ops->combine(csum, csum2, pos, copy); 2518 if ((len -= copy) == 0) 2519 return csum; 2520 offset += copy; 2521 pos += copy; 2522 } 2523 start = end; 2524 } 2525 BUG_ON(len); 2526 2527 return csum; 2528 } 2529 EXPORT_SYMBOL(__skb_checksum); 2530 2531 __wsum skb_checksum(const struct sk_buff *skb, int offset, 2532 int len, __wsum csum) 2533 { 2534 const struct skb_checksum_ops ops = { 2535 .update = csum_partial_ext, 2536 .combine = csum_block_add_ext, 2537 }; 2538 2539 return __skb_checksum(skb, offset, len, csum, &ops); 2540 } 2541 EXPORT_SYMBOL(skb_checksum); 2542 2543 /* Both of above in one bottle. */ 2544 2545 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 2546 u8 *to, int len, __wsum csum) 2547 { 2548 int start = skb_headlen(skb); 2549 int i, copy = start - offset; 2550 struct sk_buff *frag_iter; 2551 int pos = 0; 2552 2553 /* Copy header. */ 2554 if (copy > 0) { 2555 if (copy > len) 2556 copy = len; 2557 csum = csum_partial_copy_nocheck(skb->data + offset, to, 2558 copy, csum); 2559 if ((len -= copy) == 0) 2560 return csum; 2561 offset += copy; 2562 to += copy; 2563 pos = copy; 2564 } 2565 2566 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2567 int end; 2568 2569 WARN_ON(start > offset + len); 2570 2571 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 2572 if ((copy = end - offset) > 0) { 2573 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2574 u32 p_off, p_len, copied; 2575 struct page *p; 2576 __wsum csum2; 2577 u8 *vaddr; 2578 2579 if (copy > len) 2580 copy = len; 2581 2582 skb_frag_foreach_page(frag, 2583 frag->page_offset + offset - start, 2584 copy, p, p_off, p_len, copied) { 2585 vaddr = kmap_atomic(p); 2586 csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2587 to + copied, 2588 p_len, 0); 2589 kunmap_atomic(vaddr); 2590 csum = csum_block_add(csum, csum2, pos); 2591 pos += p_len; 2592 } 2593 2594 if (!(len -= copy)) 2595 return csum; 2596 offset += copy; 2597 to += copy; 2598 } 2599 start = end; 2600 } 2601 2602 skb_walk_frags(skb, frag_iter) { 2603 __wsum csum2; 2604 int end; 2605 2606 WARN_ON(start > offset + len); 2607 2608 end = start + frag_iter->len; 2609 if ((copy = end - offset) > 0) { 2610 if (copy > len) 2611 copy = len; 2612 csum2 = skb_copy_and_csum_bits(frag_iter, 2613 offset - start, 2614 to, copy, 0); 2615 csum = csum_block_add(csum, csum2, pos); 2616 if ((len -= copy) == 0) 2617 return csum; 2618 offset += copy; 2619 to += copy; 2620 pos += copy; 2621 } 2622 start = end; 2623 } 2624 BUG_ON(len); 2625 return csum; 2626 } 2627 EXPORT_SYMBOL(skb_copy_and_csum_bits); 2628 2629 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 2630 { 2631 net_warn_ratelimited( 2632 "%s: attempt to compute crc32c without libcrc32c.ko\n", 2633 __func__); 2634 return 0; 2635 } 2636 2637 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 2638 int offset, int len) 2639 { 2640 net_warn_ratelimited( 2641 "%s: attempt to compute crc32c without libcrc32c.ko\n", 2642 __func__); 2643 return 0; 2644 } 2645 2646 static const struct skb_checksum_ops default_crc32c_ops = { 2647 .update = warn_crc32c_csum_update, 2648 .combine = warn_crc32c_csum_combine, 2649 }; 2650 2651 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 2652 &default_crc32c_ops; 2653 EXPORT_SYMBOL(crc32c_csum_stub); 2654 2655 /** 2656 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2657 * @from: source buffer 2658 * 2659 * Calculates the amount of linear headroom needed in the 'to' skb passed 2660 * into skb_zerocopy(). 2661 */ 2662 unsigned int 2663 skb_zerocopy_headlen(const struct sk_buff *from) 2664 { 2665 unsigned int hlen = 0; 2666 2667 if (!from->head_frag || 2668 skb_headlen(from) < L1_CACHE_BYTES || 2669 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2670 hlen = skb_headlen(from); 2671 2672 if (skb_has_frag_list(from)) 2673 hlen = from->len; 2674 2675 return hlen; 2676 } 2677 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2678 2679 /** 2680 * skb_zerocopy - Zero copy skb to skb 2681 * @to: destination buffer 2682 * @from: source buffer 2683 * @len: number of bytes to copy from source buffer 2684 * @hlen: size of linear headroom in destination buffer 2685 * 2686 * Copies up to `len` bytes from `from` to `to` by creating references 2687 * to the frags in the source buffer. 2688 * 2689 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2690 * headroom in the `to` buffer. 2691 * 2692 * Return value: 2693 * 0: everything is OK 2694 * -ENOMEM: couldn't orphan frags of @from due to lack of memory 2695 * -EFAULT: skb_copy_bits() found some problem with skb geometry 2696 */ 2697 int 2698 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2699 { 2700 int i, j = 0; 2701 int plen = 0; /* length of skb->head fragment */ 2702 int ret; 2703 struct page *page; 2704 unsigned int offset; 2705 2706 BUG_ON(!from->head_frag && !hlen); 2707 2708 /* dont bother with small payloads */ 2709 if (len <= skb_tailroom(to)) 2710 return skb_copy_bits(from, 0, skb_put(to, len), len); 2711 2712 if (hlen) { 2713 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 2714 if (unlikely(ret)) 2715 return ret; 2716 len -= hlen; 2717 } else { 2718 plen = min_t(int, skb_headlen(from), len); 2719 if (plen) { 2720 page = virt_to_head_page(from->head); 2721 offset = from->data - (unsigned char *)page_address(page); 2722 __skb_fill_page_desc(to, 0, page, offset, plen); 2723 get_page(page); 2724 j = 1; 2725 len -= plen; 2726 } 2727 } 2728 2729 to->truesize += len + plen; 2730 to->len += len + plen; 2731 to->data_len += len + plen; 2732 2733 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 2734 skb_tx_error(from); 2735 return -ENOMEM; 2736 } 2737 skb_zerocopy_clone(to, from, GFP_ATOMIC); 2738 2739 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2740 if (!len) 2741 break; 2742 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2743 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2744 len -= skb_shinfo(to)->frags[j].size; 2745 skb_frag_ref(to, j); 2746 j++; 2747 } 2748 skb_shinfo(to)->nr_frags = j; 2749 2750 return 0; 2751 } 2752 EXPORT_SYMBOL_GPL(skb_zerocopy); 2753 2754 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 2755 { 2756 __wsum csum; 2757 long csstart; 2758 2759 if (skb->ip_summed == CHECKSUM_PARTIAL) 2760 csstart = skb_checksum_start_offset(skb); 2761 else 2762 csstart = skb_headlen(skb); 2763 2764 BUG_ON(csstart > skb_headlen(skb)); 2765 2766 skb_copy_from_linear_data(skb, to, csstart); 2767 2768 csum = 0; 2769 if (csstart != skb->len) 2770 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 2771 skb->len - csstart, 0); 2772 2773 if (skb->ip_summed == CHECKSUM_PARTIAL) { 2774 long csstuff = csstart + skb->csum_offset; 2775 2776 *((__sum16 *)(to + csstuff)) = csum_fold(csum); 2777 } 2778 } 2779 EXPORT_SYMBOL(skb_copy_and_csum_dev); 2780 2781 /** 2782 * skb_dequeue - remove from the head of the queue 2783 * @list: list to dequeue from 2784 * 2785 * Remove the head of the list. The list lock is taken so the function 2786 * may be used safely with other locking list functions. The head item is 2787 * returned or %NULL if the list is empty. 2788 */ 2789 2790 struct sk_buff *skb_dequeue(struct sk_buff_head *list) 2791 { 2792 unsigned long flags; 2793 struct sk_buff *result; 2794 2795 spin_lock_irqsave(&list->lock, flags); 2796 result = __skb_dequeue(list); 2797 spin_unlock_irqrestore(&list->lock, flags); 2798 return result; 2799 } 2800 EXPORT_SYMBOL(skb_dequeue); 2801 2802 /** 2803 * skb_dequeue_tail - remove from the tail of the queue 2804 * @list: list to dequeue from 2805 * 2806 * Remove the tail of the list. The list lock is taken so the function 2807 * may be used safely with other locking list functions. The tail item is 2808 * returned or %NULL if the list is empty. 2809 */ 2810 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 2811 { 2812 unsigned long flags; 2813 struct sk_buff *result; 2814 2815 spin_lock_irqsave(&list->lock, flags); 2816 result = __skb_dequeue_tail(list); 2817 spin_unlock_irqrestore(&list->lock, flags); 2818 return result; 2819 } 2820 EXPORT_SYMBOL(skb_dequeue_tail); 2821 2822 /** 2823 * skb_queue_purge - empty a list 2824 * @list: list to empty 2825 * 2826 * Delete all buffers on an &sk_buff list. Each buffer is removed from 2827 * the list and one reference dropped. This function takes the list 2828 * lock and is atomic with respect to other list locking functions. 2829 */ 2830 void skb_queue_purge(struct sk_buff_head *list) 2831 { 2832 struct sk_buff *skb; 2833 while ((skb = skb_dequeue(list)) != NULL) 2834 kfree_skb(skb); 2835 } 2836 EXPORT_SYMBOL(skb_queue_purge); 2837 2838 /** 2839 * skb_rbtree_purge - empty a skb rbtree 2840 * @root: root of the rbtree to empty 2841 * 2842 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 2843 * the list and one reference dropped. This function does not take 2844 * any lock. Synchronization should be handled by the caller (e.g., TCP 2845 * out-of-order queue is protected by the socket lock). 2846 */ 2847 void skb_rbtree_purge(struct rb_root *root) 2848 { 2849 struct rb_node *p = rb_first(root); 2850 2851 while (p) { 2852 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 2853 2854 p = rb_next(p); 2855 rb_erase(&skb->rbnode, root); 2856 kfree_skb(skb); 2857 } 2858 } 2859 2860 /** 2861 * skb_queue_head - queue a buffer at the list head 2862 * @list: list to use 2863 * @newsk: buffer to queue 2864 * 2865 * Queue a buffer at the start of the list. This function takes the 2866 * list lock and can be used safely with other locking &sk_buff functions 2867 * safely. 2868 * 2869 * A buffer cannot be placed on two lists at the same time. 2870 */ 2871 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 2872 { 2873 unsigned long flags; 2874 2875 spin_lock_irqsave(&list->lock, flags); 2876 __skb_queue_head(list, newsk); 2877 spin_unlock_irqrestore(&list->lock, flags); 2878 } 2879 EXPORT_SYMBOL(skb_queue_head); 2880 2881 /** 2882 * skb_queue_tail - queue a buffer at the list tail 2883 * @list: list to use 2884 * @newsk: buffer to queue 2885 * 2886 * Queue a buffer at the tail of the list. This function takes the 2887 * list lock and can be used safely with other locking &sk_buff functions 2888 * safely. 2889 * 2890 * A buffer cannot be placed on two lists at the same time. 2891 */ 2892 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 2893 { 2894 unsigned long flags; 2895 2896 spin_lock_irqsave(&list->lock, flags); 2897 __skb_queue_tail(list, newsk); 2898 spin_unlock_irqrestore(&list->lock, flags); 2899 } 2900 EXPORT_SYMBOL(skb_queue_tail); 2901 2902 /** 2903 * skb_unlink - remove a buffer from a list 2904 * @skb: buffer to remove 2905 * @list: list to use 2906 * 2907 * Remove a packet from a list. The list locks are taken and this 2908 * function is atomic with respect to other list locked calls 2909 * 2910 * You must know what list the SKB is on. 2911 */ 2912 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 2913 { 2914 unsigned long flags; 2915 2916 spin_lock_irqsave(&list->lock, flags); 2917 __skb_unlink(skb, list); 2918 spin_unlock_irqrestore(&list->lock, flags); 2919 } 2920 EXPORT_SYMBOL(skb_unlink); 2921 2922 /** 2923 * skb_append - append a buffer 2924 * @old: buffer to insert after 2925 * @newsk: buffer to insert 2926 * @list: list to use 2927 * 2928 * Place a packet after a given packet in a list. The list locks are taken 2929 * and this function is atomic with respect to other list locked calls. 2930 * A buffer cannot be placed on two lists at the same time. 2931 */ 2932 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 2933 { 2934 unsigned long flags; 2935 2936 spin_lock_irqsave(&list->lock, flags); 2937 __skb_queue_after(list, old, newsk); 2938 spin_unlock_irqrestore(&list->lock, flags); 2939 } 2940 EXPORT_SYMBOL(skb_append); 2941 2942 /** 2943 * skb_insert - insert a buffer 2944 * @old: buffer to insert before 2945 * @newsk: buffer to insert 2946 * @list: list to use 2947 * 2948 * Place a packet before a given packet in a list. The list locks are 2949 * taken and this function is atomic with respect to other list locked 2950 * calls. 2951 * 2952 * A buffer cannot be placed on two lists at the same time. 2953 */ 2954 void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 2955 { 2956 unsigned long flags; 2957 2958 spin_lock_irqsave(&list->lock, flags); 2959 __skb_insert(newsk, old->prev, old, list); 2960 spin_unlock_irqrestore(&list->lock, flags); 2961 } 2962 EXPORT_SYMBOL(skb_insert); 2963 2964 static inline void skb_split_inside_header(struct sk_buff *skb, 2965 struct sk_buff* skb1, 2966 const u32 len, const int pos) 2967 { 2968 int i; 2969 2970 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2971 pos - len); 2972 /* And move data appendix as is. */ 2973 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 2974 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 2975 2976 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 2977 skb_shinfo(skb)->nr_frags = 0; 2978 skb1->data_len = skb->data_len; 2979 skb1->len += skb1->data_len; 2980 skb->data_len = 0; 2981 skb->len = len; 2982 skb_set_tail_pointer(skb, len); 2983 } 2984 2985 static inline void skb_split_no_header(struct sk_buff *skb, 2986 struct sk_buff* skb1, 2987 const u32 len, int pos) 2988 { 2989 int i, k = 0; 2990 const int nfrags = skb_shinfo(skb)->nr_frags; 2991 2992 skb_shinfo(skb)->nr_frags = 0; 2993 skb1->len = skb1->data_len = skb->len - len; 2994 skb->len = len; 2995 skb->data_len = len - pos; 2996 2997 for (i = 0; i < nfrags; i++) { 2998 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 2999 3000 if (pos + size > len) { 3001 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 3002 3003 if (pos < len) { 3004 /* Split frag. 3005 * We have two variants in this case: 3006 * 1. Move all the frag to the second 3007 * part, if it is possible. F.e. 3008 * this approach is mandatory for TUX, 3009 * where splitting is expensive. 3010 * 2. Split is accurately. We make this. 3011 */ 3012 skb_frag_ref(skb, i); 3013 skb_shinfo(skb1)->frags[0].page_offset += len - pos; 3014 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 3015 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 3016 skb_shinfo(skb)->nr_frags++; 3017 } 3018 k++; 3019 } else 3020 skb_shinfo(skb)->nr_frags++; 3021 pos += size; 3022 } 3023 skb_shinfo(skb1)->nr_frags = k; 3024 } 3025 3026 /** 3027 * skb_split - Split fragmented skb to two parts at length len. 3028 * @skb: the buffer to split 3029 * @skb1: the buffer to receive the second part 3030 * @len: new length for skb 3031 */ 3032 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 3033 { 3034 int pos = skb_headlen(skb); 3035 3036 skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & 3037 SKBTX_SHARED_FRAG; 3038 skb_zerocopy_clone(skb1, skb, 0); 3039 if (len < pos) /* Split line is inside header. */ 3040 skb_split_inside_header(skb, skb1, len, pos); 3041 else /* Second chunk has no header, nothing to copy. */ 3042 skb_split_no_header(skb, skb1, len, pos); 3043 } 3044 EXPORT_SYMBOL(skb_split); 3045 3046 /* Shifting from/to a cloned skb is a no-go. 3047 * 3048 * Caller cannot keep skb_shinfo related pointers past calling here! 3049 */ 3050 static int skb_prepare_for_shift(struct sk_buff *skb) 3051 { 3052 return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3053 } 3054 3055 /** 3056 * skb_shift - Shifts paged data partially from skb to another 3057 * @tgt: buffer into which tail data gets added 3058 * @skb: buffer from which the paged data comes from 3059 * @shiftlen: shift up to this many bytes 3060 * 3061 * Attempts to shift up to shiftlen worth of bytes, which may be less than 3062 * the length of the skb, from skb to tgt. Returns number bytes shifted. 3063 * It's up to caller to free skb if everything was shifted. 3064 * 3065 * If @tgt runs out of frags, the whole operation is aborted. 3066 * 3067 * Skb cannot include anything else but paged data while tgt is allowed 3068 * to have non-paged data as well. 3069 * 3070 * TODO: full sized shift could be optimized but that would need 3071 * specialized skb free'er to handle frags without up-to-date nr_frags. 3072 */ 3073 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3074 { 3075 int from, to, merge, todo; 3076 struct skb_frag_struct *fragfrom, *fragto; 3077 3078 BUG_ON(shiftlen > skb->len); 3079 3080 if (skb_headlen(skb)) 3081 return 0; 3082 if (skb_zcopy(tgt) || skb_zcopy(skb)) 3083 return 0; 3084 3085 todo = shiftlen; 3086 from = 0; 3087 to = skb_shinfo(tgt)->nr_frags; 3088 fragfrom = &skb_shinfo(skb)->frags[from]; 3089 3090 /* Actual merge is delayed until the point when we know we can 3091 * commit all, so that we don't have to undo partial changes 3092 */ 3093 if (!to || 3094 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3095 fragfrom->page_offset)) { 3096 merge = -1; 3097 } else { 3098 merge = to - 1; 3099 3100 todo -= skb_frag_size(fragfrom); 3101 if (todo < 0) { 3102 if (skb_prepare_for_shift(skb) || 3103 skb_prepare_for_shift(tgt)) 3104 return 0; 3105 3106 /* All previous frag pointers might be stale! */ 3107 fragfrom = &skb_shinfo(skb)->frags[from]; 3108 fragto = &skb_shinfo(tgt)->frags[merge]; 3109 3110 skb_frag_size_add(fragto, shiftlen); 3111 skb_frag_size_sub(fragfrom, shiftlen); 3112 fragfrom->page_offset += shiftlen; 3113 3114 goto onlymerged; 3115 } 3116 3117 from++; 3118 } 3119 3120 /* Skip full, not-fitting skb to avoid expensive operations */ 3121 if ((shiftlen == skb->len) && 3122 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3123 return 0; 3124 3125 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3126 return 0; 3127 3128 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3129 if (to == MAX_SKB_FRAGS) 3130 return 0; 3131 3132 fragfrom = &skb_shinfo(skb)->frags[from]; 3133 fragto = &skb_shinfo(tgt)->frags[to]; 3134 3135 if (todo >= skb_frag_size(fragfrom)) { 3136 *fragto = *fragfrom; 3137 todo -= skb_frag_size(fragfrom); 3138 from++; 3139 to++; 3140 3141 } else { 3142 __skb_frag_ref(fragfrom); 3143 fragto->page = fragfrom->page; 3144 fragto->page_offset = fragfrom->page_offset; 3145 skb_frag_size_set(fragto, todo); 3146 3147 fragfrom->page_offset += todo; 3148 skb_frag_size_sub(fragfrom, todo); 3149 todo = 0; 3150 3151 to++; 3152 break; 3153 } 3154 } 3155 3156 /* Ready to "commit" this state change to tgt */ 3157 skb_shinfo(tgt)->nr_frags = to; 3158 3159 if (merge >= 0) { 3160 fragfrom = &skb_shinfo(skb)->frags[0]; 3161 fragto = &skb_shinfo(tgt)->frags[merge]; 3162 3163 skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 3164 __skb_frag_unref(fragfrom); 3165 } 3166 3167 /* Reposition in the original skb */ 3168 to = 0; 3169 while (from < skb_shinfo(skb)->nr_frags) 3170 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3171 skb_shinfo(skb)->nr_frags = to; 3172 3173 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3174 3175 onlymerged: 3176 /* Most likely the tgt won't ever need its checksum anymore, skb on 3177 * the other hand might need it if it needs to be resent 3178 */ 3179 tgt->ip_summed = CHECKSUM_PARTIAL; 3180 skb->ip_summed = CHECKSUM_PARTIAL; 3181 3182 /* Yak, is it really working this way? Some helper please? */ 3183 skb->len -= shiftlen; 3184 skb->data_len -= shiftlen; 3185 skb->truesize -= shiftlen; 3186 tgt->len += shiftlen; 3187 tgt->data_len += shiftlen; 3188 tgt->truesize += shiftlen; 3189 3190 return shiftlen; 3191 } 3192 3193 /** 3194 * skb_prepare_seq_read - Prepare a sequential read of skb data 3195 * @skb: the buffer to read 3196 * @from: lower offset of data to be read 3197 * @to: upper offset of data to be read 3198 * @st: state variable 3199 * 3200 * Initializes the specified state variable. Must be called before 3201 * invoking skb_seq_read() for the first time. 3202 */ 3203 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3204 unsigned int to, struct skb_seq_state *st) 3205 { 3206 st->lower_offset = from; 3207 st->upper_offset = to; 3208 st->root_skb = st->cur_skb = skb; 3209 st->frag_idx = st->stepped_offset = 0; 3210 st->frag_data = NULL; 3211 } 3212 EXPORT_SYMBOL(skb_prepare_seq_read); 3213 3214 /** 3215 * skb_seq_read - Sequentially read skb data 3216 * @consumed: number of bytes consumed by the caller so far 3217 * @data: destination pointer for data to be returned 3218 * @st: state variable 3219 * 3220 * Reads a block of skb data at @consumed relative to the 3221 * lower offset specified to skb_prepare_seq_read(). Assigns 3222 * the head of the data block to @data and returns the length 3223 * of the block or 0 if the end of the skb data or the upper 3224 * offset has been reached. 3225 * 3226 * The caller is not required to consume all of the data 3227 * returned, i.e. @consumed is typically set to the number 3228 * of bytes already consumed and the next call to 3229 * skb_seq_read() will return the remaining part of the block. 3230 * 3231 * Note 1: The size of each block of data returned can be arbitrary, 3232 * this limitation is the cost for zerocopy sequential 3233 * reads of potentially non linear data. 3234 * 3235 * Note 2: Fragment lists within fragments are not implemented 3236 * at the moment, state->root_skb could be replaced with 3237 * a stack for this purpose. 3238 */ 3239 unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3240 struct skb_seq_state *st) 3241 { 3242 unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3243 skb_frag_t *frag; 3244 3245 if (unlikely(abs_offset >= st->upper_offset)) { 3246 if (st->frag_data) { 3247 kunmap_atomic(st->frag_data); 3248 st->frag_data = NULL; 3249 } 3250 return 0; 3251 } 3252 3253 next_skb: 3254 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3255 3256 if (abs_offset < block_limit && !st->frag_data) { 3257 *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3258 return block_limit - abs_offset; 3259 } 3260 3261 if (st->frag_idx == 0 && !st->frag_data) 3262 st->stepped_offset += skb_headlen(st->cur_skb); 3263 3264 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 3265 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 3266 block_limit = skb_frag_size(frag) + st->stepped_offset; 3267 3268 if (abs_offset < block_limit) { 3269 if (!st->frag_data) 3270 st->frag_data = kmap_atomic(skb_frag_page(frag)); 3271 3272 *data = (u8 *) st->frag_data + frag->page_offset + 3273 (abs_offset - st->stepped_offset); 3274 3275 return block_limit - abs_offset; 3276 } 3277 3278 if (st->frag_data) { 3279 kunmap_atomic(st->frag_data); 3280 st->frag_data = NULL; 3281 } 3282 3283 st->frag_idx++; 3284 st->stepped_offset += skb_frag_size(frag); 3285 } 3286 3287 if (st->frag_data) { 3288 kunmap_atomic(st->frag_data); 3289 st->frag_data = NULL; 3290 } 3291 3292 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3293 st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 3294 st->frag_idx = 0; 3295 goto next_skb; 3296 } else if (st->cur_skb->next) { 3297 st->cur_skb = st->cur_skb->next; 3298 st->frag_idx = 0; 3299 goto next_skb; 3300 } 3301 3302 return 0; 3303 } 3304 EXPORT_SYMBOL(skb_seq_read); 3305 3306 /** 3307 * skb_abort_seq_read - Abort a sequential read of skb data 3308 * @st: state variable 3309 * 3310 * Must be called if skb_seq_read() was not called until it 3311 * returned 0. 3312 */ 3313 void skb_abort_seq_read(struct skb_seq_state *st) 3314 { 3315 if (st->frag_data) 3316 kunmap_atomic(st->frag_data); 3317 } 3318 EXPORT_SYMBOL(skb_abort_seq_read); 3319 3320 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 3321 3322 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 3323 struct ts_config *conf, 3324 struct ts_state *state) 3325 { 3326 return skb_seq_read(offset, text, TS_SKB_CB(state)); 3327 } 3328 3329 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 3330 { 3331 skb_abort_seq_read(TS_SKB_CB(state)); 3332 } 3333 3334 /** 3335 * skb_find_text - Find a text pattern in skb data 3336 * @skb: the buffer to look in 3337 * @from: search offset 3338 * @to: search limit 3339 * @config: textsearch configuration 3340 * 3341 * Finds a pattern in the skb data according to the specified 3342 * textsearch configuration. Use textsearch_next() to retrieve 3343 * subsequent occurrences of the pattern. Returns the offset 3344 * to the first occurrence or UINT_MAX if no match was found. 3345 */ 3346 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3347 unsigned int to, struct ts_config *config) 3348 { 3349 struct ts_state state; 3350 unsigned int ret; 3351 3352 config->get_next_block = skb_ts_get_next_block; 3353 config->finish = skb_ts_finish; 3354 3355 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 3356 3357 ret = textsearch_find(config, &state); 3358 return (ret <= to - from ? ret : UINT_MAX); 3359 } 3360 EXPORT_SYMBOL(skb_find_text); 3361 3362 /** 3363 * skb_append_datato_frags - append the user data to a skb 3364 * @sk: sock structure 3365 * @skb: skb structure to be appended with user data. 3366 * @getfrag: call back function to be used for getting the user data 3367 * @from: pointer to user message iov 3368 * @length: length of the iov message 3369 * 3370 * Description: This procedure append the user data in the fragment part 3371 * of the skb if any page alloc fails user this procedure returns -ENOMEM 3372 */ 3373 int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 3374 int (*getfrag)(void *from, char *to, int offset, 3375 int len, int odd, struct sk_buff *skb), 3376 void *from, int length) 3377 { 3378 int frg_cnt = skb_shinfo(skb)->nr_frags; 3379 int copy; 3380 int offset = 0; 3381 int ret; 3382 struct page_frag *pfrag = ¤t->task_frag; 3383 3384 do { 3385 /* Return error if we don't have space for new frag */ 3386 if (frg_cnt >= MAX_SKB_FRAGS) 3387 return -EMSGSIZE; 3388 3389 if (!sk_page_frag_refill(sk, pfrag)) 3390 return -ENOMEM; 3391 3392 /* copy the user data to page */ 3393 copy = min_t(int, length, pfrag->size - pfrag->offset); 3394 3395 ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 3396 offset, copy, 0, skb); 3397 if (ret < 0) 3398 return -EFAULT; 3399 3400 /* copy was successful so update the size parameters */ 3401 skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 3402 copy); 3403 frg_cnt++; 3404 pfrag->offset += copy; 3405 get_page(pfrag->page); 3406 3407 skb->truesize += copy; 3408 refcount_add(copy, &sk->sk_wmem_alloc); 3409 skb->len += copy; 3410 skb->data_len += copy; 3411 offset += copy; 3412 length -= copy; 3413 3414 } while (length > 0); 3415 3416 return 0; 3417 } 3418 EXPORT_SYMBOL(skb_append_datato_frags); 3419 3420 int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3421 int offset, size_t size) 3422 { 3423 int i = skb_shinfo(skb)->nr_frags; 3424 3425 if (skb_can_coalesce(skb, i, page, offset)) { 3426 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3427 } else if (i < MAX_SKB_FRAGS) { 3428 get_page(page); 3429 skb_fill_page_desc(skb, i, page, offset, size); 3430 } else { 3431 return -EMSGSIZE; 3432 } 3433 3434 return 0; 3435 } 3436 EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3437 3438 /** 3439 * skb_pull_rcsum - pull skb and update receive checksum 3440 * @skb: buffer to update 3441 * @len: length of data pulled 3442 * 3443 * This function performs an skb_pull on the packet and updates 3444 * the CHECKSUM_COMPLETE checksum. It should be used on 3445 * receive path processing instead of skb_pull unless you know 3446 * that the checksum difference is zero (e.g., a valid IP header) 3447 * or you are setting ip_summed to CHECKSUM_NONE. 3448 */ 3449 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3450 { 3451 unsigned char *data = skb->data; 3452 3453 BUG_ON(len > skb->len); 3454 __skb_pull(skb, len); 3455 skb_postpull_rcsum(skb, data, len); 3456 return skb->data; 3457 } 3458 EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3459 3460 /** 3461 * skb_segment - Perform protocol segmentation on skb. 3462 * @head_skb: buffer to segment 3463 * @features: features for the output path (see dev->features) 3464 * 3465 * This function performs segmentation on the given skb. It returns 3466 * a pointer to the first in a list of new skbs for the segments. 3467 * In case of error it returns ERR_PTR(err). 3468 */ 3469 struct sk_buff *skb_segment(struct sk_buff *head_skb, 3470 netdev_features_t features) 3471 { 3472 struct sk_buff *segs = NULL; 3473 struct sk_buff *tail = NULL; 3474 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3475 skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3476 unsigned int mss = skb_shinfo(head_skb)->gso_size; 3477 unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 3478 struct sk_buff *frag_skb = head_skb; 3479 unsigned int offset = doffset; 3480 unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3481 unsigned int partial_segs = 0; 3482 unsigned int headroom; 3483 unsigned int len = head_skb->len; 3484 __be16 proto; 3485 bool csum, sg; 3486 int nfrags = skb_shinfo(head_skb)->nr_frags; 3487 int err = -ENOMEM; 3488 int i = 0; 3489 int pos; 3490 int dummy; 3491 3492 __skb_push(head_skb, doffset); 3493 proto = skb_network_protocol(head_skb, &dummy); 3494 if (unlikely(!proto)) 3495 return ERR_PTR(-EINVAL); 3496 3497 sg = !!(features & NETIF_F_SG); 3498 csum = !!can_checksum_protocol(features, proto); 3499 3500 if (sg && csum && (mss != GSO_BY_FRAGS)) { 3501 if (!(features & NETIF_F_GSO_PARTIAL)) { 3502 struct sk_buff *iter; 3503 unsigned int frag_len; 3504 3505 if (!list_skb || 3506 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 3507 goto normal; 3508 3509 /* If we get here then all the required 3510 * GSO features except frag_list are supported. 3511 * Try to split the SKB to multiple GSO SKBs 3512 * with no frag_list. 3513 * Currently we can do that only when the buffers don't 3514 * have a linear part and all the buffers except 3515 * the last are of the same length. 3516 */ 3517 frag_len = list_skb->len; 3518 skb_walk_frags(head_skb, iter) { 3519 if (frag_len != iter->len && iter->next) 3520 goto normal; 3521 if (skb_headlen(iter) && !iter->head_frag) 3522 goto normal; 3523 3524 len -= iter->len; 3525 } 3526 3527 if (len != frag_len) 3528 goto normal; 3529 } 3530 3531 /* GSO partial only requires that we trim off any excess that 3532 * doesn't fit into an MSS sized block, so take care of that 3533 * now. 3534 */ 3535 partial_segs = len / mss; 3536 if (partial_segs > 1) 3537 mss *= partial_segs; 3538 else 3539 partial_segs = 0; 3540 } 3541 3542 normal: 3543 headroom = skb_headroom(head_skb); 3544 pos = skb_headlen(head_skb); 3545 3546 do { 3547 struct sk_buff *nskb; 3548 skb_frag_t *nskb_frag; 3549 int hsize; 3550 int size; 3551 3552 if (unlikely(mss == GSO_BY_FRAGS)) { 3553 len = list_skb->len; 3554 } else { 3555 len = head_skb->len - offset; 3556 if (len > mss) 3557 len = mss; 3558 } 3559 3560 hsize = skb_headlen(head_skb) - offset; 3561 if (hsize < 0) 3562 hsize = 0; 3563 if (hsize > len || !sg) 3564 hsize = len; 3565 3566 if (!hsize && i >= nfrags && skb_headlen(list_skb) && 3567 (skb_headlen(list_skb) == len || sg)) { 3568 BUG_ON(skb_headlen(list_skb) > len); 3569 3570 i = 0; 3571 nfrags = skb_shinfo(list_skb)->nr_frags; 3572 frag = skb_shinfo(list_skb)->frags; 3573 frag_skb = list_skb; 3574 pos += skb_headlen(list_skb); 3575 3576 while (pos < offset + len) { 3577 BUG_ON(i >= nfrags); 3578 3579 size = skb_frag_size(frag); 3580 if (pos + size > offset + len) 3581 break; 3582 3583 i++; 3584 pos += size; 3585 frag++; 3586 } 3587 3588 nskb = skb_clone(list_skb, GFP_ATOMIC); 3589 list_skb = list_skb->next; 3590 3591 if (unlikely(!nskb)) 3592 goto err; 3593 3594 if (unlikely(pskb_trim(nskb, len))) { 3595 kfree_skb(nskb); 3596 goto err; 3597 } 3598 3599 hsize = skb_end_offset(nskb); 3600 if (skb_cow_head(nskb, doffset + headroom)) { 3601 kfree_skb(nskb); 3602 goto err; 3603 } 3604 3605 nskb->truesize += skb_end_offset(nskb) - hsize; 3606 skb_release_head_state(nskb); 3607 __skb_push(nskb, doffset); 3608 } else { 3609 nskb = __alloc_skb(hsize + doffset + headroom, 3610 GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3611 NUMA_NO_NODE); 3612 3613 if (unlikely(!nskb)) 3614 goto err; 3615 3616 skb_reserve(nskb, headroom); 3617 __skb_put(nskb, doffset); 3618 } 3619 3620 if (segs) 3621 tail->next = nskb; 3622 else 3623 segs = nskb; 3624 tail = nskb; 3625 3626 __copy_skb_header(nskb, head_skb); 3627 3628 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3629 skb_reset_mac_len(nskb); 3630 3631 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 3632 nskb->data - tnl_hlen, 3633 doffset + tnl_hlen); 3634 3635 if (nskb->len == len + doffset) 3636 goto perform_csum_check; 3637 3638 if (!sg) { 3639 if (!nskb->remcsum_offload) 3640 nskb->ip_summed = CHECKSUM_NONE; 3641 SKB_GSO_CB(nskb)->csum = 3642 skb_copy_and_csum_bits(head_skb, offset, 3643 skb_put(nskb, len), 3644 len, 0); 3645 SKB_GSO_CB(nskb)->csum_start = 3646 skb_headroom(nskb) + doffset; 3647 continue; 3648 } 3649 3650 nskb_frag = skb_shinfo(nskb)->frags; 3651 3652 skb_copy_from_linear_data_offset(head_skb, offset, 3653 skb_put(nskb, hsize), hsize); 3654 3655 skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & 3656 SKBTX_SHARED_FRAG; 3657 if (skb_zerocopy_clone(nskb, head_skb, GFP_ATOMIC)) 3658 goto err; 3659 3660 while (pos < offset + len) { 3661 if (i >= nfrags) { 3662 BUG_ON(skb_headlen(list_skb)); 3663 3664 i = 0; 3665 nfrags = skb_shinfo(list_skb)->nr_frags; 3666 frag = skb_shinfo(list_skb)->frags; 3667 frag_skb = list_skb; 3668 3669 BUG_ON(!nfrags); 3670 3671 list_skb = list_skb->next; 3672 } 3673 3674 if (unlikely(skb_shinfo(nskb)->nr_frags >= 3675 MAX_SKB_FRAGS)) { 3676 net_warn_ratelimited( 3677 "skb_segment: too many frags: %u %u\n", 3678 pos, mss); 3679 goto err; 3680 } 3681 3682 if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 3683 goto err; 3684 3685 *nskb_frag = *frag; 3686 __skb_frag_ref(nskb_frag); 3687 size = skb_frag_size(nskb_frag); 3688 3689 if (pos < offset) { 3690 nskb_frag->page_offset += offset - pos; 3691 skb_frag_size_sub(nskb_frag, offset - pos); 3692 } 3693 3694 skb_shinfo(nskb)->nr_frags++; 3695 3696 if (pos + size <= offset + len) { 3697 i++; 3698 frag++; 3699 pos += size; 3700 } else { 3701 skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 3702 goto skip_fraglist; 3703 } 3704 3705 nskb_frag++; 3706 } 3707 3708 skip_fraglist: 3709 nskb->data_len = len - hsize; 3710 nskb->len += nskb->data_len; 3711 nskb->truesize += nskb->data_len; 3712 3713 perform_csum_check: 3714 if (!csum) { 3715 if (skb_has_shared_frag(nskb)) { 3716 err = __skb_linearize(nskb); 3717 if (err) 3718 goto err; 3719 } 3720 if (!nskb->remcsum_offload) 3721 nskb->ip_summed = CHECKSUM_NONE; 3722 SKB_GSO_CB(nskb)->csum = 3723 skb_checksum(nskb, doffset, 3724 nskb->len - doffset, 0); 3725 SKB_GSO_CB(nskb)->csum_start = 3726 skb_headroom(nskb) + doffset; 3727 } 3728 } while ((offset += len) < head_skb->len); 3729 3730 /* Some callers want to get the end of the list. 3731 * Put it in segs->prev to avoid walking the list. 3732 * (see validate_xmit_skb_list() for example) 3733 */ 3734 segs->prev = tail; 3735 3736 if (partial_segs) { 3737 struct sk_buff *iter; 3738 int type = skb_shinfo(head_skb)->gso_type; 3739 unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 3740 3741 /* Update type to add partial and then remove dodgy if set */ 3742 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 3743 type &= ~SKB_GSO_DODGY; 3744 3745 /* Update GSO info and prepare to start updating headers on 3746 * our way back down the stack of protocols. 3747 */ 3748 for (iter = segs; iter; iter = iter->next) { 3749 skb_shinfo(iter)->gso_size = gso_size; 3750 skb_shinfo(iter)->gso_segs = partial_segs; 3751 skb_shinfo(iter)->gso_type = type; 3752 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 3753 } 3754 3755 if (tail->len - doffset <= gso_size) 3756 skb_shinfo(tail)->gso_size = 0; 3757 else if (tail != segs) 3758 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 3759 } 3760 3761 /* Following permits correct backpressure, for protocols 3762 * using skb_set_owner_w(). 3763 * Idea is to tranfert ownership from head_skb to last segment. 3764 */ 3765 if (head_skb->destructor == sock_wfree) { 3766 swap(tail->truesize, head_skb->truesize); 3767 swap(tail->destructor, head_skb->destructor); 3768 swap(tail->sk, head_skb->sk); 3769 } 3770 return segs; 3771 3772 err: 3773 kfree_skb_list(segs); 3774 return ERR_PTR(err); 3775 } 3776 EXPORT_SYMBOL_GPL(skb_segment); 3777 3778 int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 3779 { 3780 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 3781 unsigned int offset = skb_gro_offset(skb); 3782 unsigned int headlen = skb_headlen(skb); 3783 unsigned int len = skb_gro_len(skb); 3784 struct sk_buff *lp, *p = *head; 3785 unsigned int delta_truesize; 3786 3787 if (unlikely(p->len + len >= 65536)) 3788 return -E2BIG; 3789 3790 lp = NAPI_GRO_CB(p)->last; 3791 pinfo = skb_shinfo(lp); 3792 3793 if (headlen <= offset) { 3794 skb_frag_t *frag; 3795 skb_frag_t *frag2; 3796 int i = skbinfo->nr_frags; 3797 int nr_frags = pinfo->nr_frags + i; 3798 3799 if (nr_frags > MAX_SKB_FRAGS) 3800 goto merge; 3801 3802 offset -= headlen; 3803 pinfo->nr_frags = nr_frags; 3804 skbinfo->nr_frags = 0; 3805 3806 frag = pinfo->frags + nr_frags; 3807 frag2 = skbinfo->frags + i; 3808 do { 3809 *--frag = *--frag2; 3810 } while (--i); 3811 3812 frag->page_offset += offset; 3813 skb_frag_size_sub(frag, offset); 3814 3815 /* all fragments truesize : remove (head size + sk_buff) */ 3816 delta_truesize = skb->truesize - 3817 SKB_TRUESIZE(skb_end_offset(skb)); 3818 3819 skb->truesize -= skb->data_len; 3820 skb->len -= skb->data_len; 3821 skb->data_len = 0; 3822 3823 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 3824 goto done; 3825 } else if (skb->head_frag) { 3826 int nr_frags = pinfo->nr_frags; 3827 skb_frag_t *frag = pinfo->frags + nr_frags; 3828 struct page *page = virt_to_head_page(skb->head); 3829 unsigned int first_size = headlen - offset; 3830 unsigned int first_offset; 3831 3832 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 3833 goto merge; 3834 3835 first_offset = skb->data - 3836 (unsigned char *)page_address(page) + 3837 offset; 3838 3839 pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3840 3841 frag->page.p = page; 3842 frag->page_offset = first_offset; 3843 skb_frag_size_set(frag, first_size); 3844 3845 memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3846 /* We dont need to clear skbinfo->nr_frags here */ 3847 3848 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3849 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3850 goto done; 3851 } 3852 3853 merge: 3854 delta_truesize = skb->truesize; 3855 if (offset > headlen) { 3856 unsigned int eat = offset - headlen; 3857 3858 skbinfo->frags[0].page_offset += eat; 3859 skb_frag_size_sub(&skbinfo->frags[0], eat); 3860 skb->data_len -= eat; 3861 skb->len -= eat; 3862 offset = headlen; 3863 } 3864 3865 __skb_pull(skb, offset); 3866 3867 if (NAPI_GRO_CB(p)->last == p) 3868 skb_shinfo(p)->frag_list = skb; 3869 else 3870 NAPI_GRO_CB(p)->last->next = skb; 3871 NAPI_GRO_CB(p)->last = skb; 3872 __skb_header_release(skb); 3873 lp = p; 3874 3875 done: 3876 NAPI_GRO_CB(p)->count++; 3877 p->data_len += len; 3878 p->truesize += delta_truesize; 3879 p->len += len; 3880 if (lp != p) { 3881 lp->data_len += len; 3882 lp->truesize += delta_truesize; 3883 lp->len += len; 3884 } 3885 NAPI_GRO_CB(skb)->same_flow = 1; 3886 return 0; 3887 } 3888 EXPORT_SYMBOL_GPL(skb_gro_receive); 3889 3890 void __init skb_init(void) 3891 { 3892 skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 3893 sizeof(struct sk_buff), 3894 0, 3895 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 3896 NULL); 3897 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3898 sizeof(struct sk_buff_fclones), 3899 0, 3900 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 3901 NULL); 3902 } 3903 3904 static int 3905 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 3906 unsigned int recursion_level) 3907 { 3908 int start = skb_headlen(skb); 3909 int i, copy = start - offset; 3910 struct sk_buff *frag_iter; 3911 int elt = 0; 3912 3913 if (unlikely(recursion_level >= 24)) 3914 return -EMSGSIZE; 3915 3916 if (copy > 0) { 3917 if (copy > len) 3918 copy = len; 3919 sg_set_buf(sg, skb->data + offset, copy); 3920 elt++; 3921 if ((len -= copy) == 0) 3922 return elt; 3923 offset += copy; 3924 } 3925 3926 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3927 int end; 3928 3929 WARN_ON(start > offset + len); 3930 3931 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3932 if ((copy = end - offset) > 0) { 3933 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3934 if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 3935 return -EMSGSIZE; 3936 3937 if (copy > len) 3938 copy = len; 3939 sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3940 frag->page_offset+offset-start); 3941 elt++; 3942 if (!(len -= copy)) 3943 return elt; 3944 offset += copy; 3945 } 3946 start = end; 3947 } 3948 3949 skb_walk_frags(skb, frag_iter) { 3950 int end, ret; 3951 3952 WARN_ON(start > offset + len); 3953 3954 end = start + frag_iter->len; 3955 if ((copy = end - offset) > 0) { 3956 if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 3957 return -EMSGSIZE; 3958 3959 if (copy > len) 3960 copy = len; 3961 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 3962 copy, recursion_level + 1); 3963 if (unlikely(ret < 0)) 3964 return ret; 3965 elt += ret; 3966 if ((len -= copy) == 0) 3967 return elt; 3968 offset += copy; 3969 } 3970 start = end; 3971 } 3972 BUG_ON(len); 3973 return elt; 3974 } 3975 3976 /** 3977 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3978 * @skb: Socket buffer containing the buffers to be mapped 3979 * @sg: The scatter-gather list to map into 3980 * @offset: The offset into the buffer's contents to start mapping 3981 * @len: Length of buffer space to be mapped 3982 * 3983 * Fill the specified scatter-gather list with mappings/pointers into a 3984 * region of the buffer space attached to a socket buffer. Returns either 3985 * the number of scatterlist items used, or -EMSGSIZE if the contents 3986 * could not fit. 3987 */ 3988 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3989 { 3990 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 3991 3992 if (nsg <= 0) 3993 return nsg; 3994 3995 sg_mark_end(&sg[nsg - 1]); 3996 3997 return nsg; 3998 } 3999 EXPORT_SYMBOL_GPL(skb_to_sgvec); 4000 4001 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 4002 * sglist without mark the sg which contain last skb data as the end. 4003 * So the caller can mannipulate sg list as will when padding new data after 4004 * the first call without calling sg_unmark_end to expend sg list. 4005 * 4006 * Scenario to use skb_to_sgvec_nomark: 4007 * 1. sg_init_table 4008 * 2. skb_to_sgvec_nomark(payload1) 4009 * 3. skb_to_sgvec_nomark(payload2) 4010 * 4011 * This is equivalent to: 4012 * 1. sg_init_table 4013 * 2. skb_to_sgvec(payload1) 4014 * 3. sg_unmark_end 4015 * 4. skb_to_sgvec(payload2) 4016 * 4017 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 4018 * is more preferable. 4019 */ 4020 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 4021 int offset, int len) 4022 { 4023 return __skb_to_sgvec(skb, sg, offset, len, 0); 4024 } 4025 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 4026 4027 4028 4029 /** 4030 * skb_cow_data - Check that a socket buffer's data buffers are writable 4031 * @skb: The socket buffer to check. 4032 * @tailbits: Amount of trailing space to be added 4033 * @trailer: Returned pointer to the skb where the @tailbits space begins 4034 * 4035 * Make sure that the data buffers attached to a socket buffer are 4036 * writable. If they are not, private copies are made of the data buffers 4037 * and the socket buffer is set to use these instead. 4038 * 4039 * If @tailbits is given, make sure that there is space to write @tailbits 4040 * bytes of data beyond current end of socket buffer. @trailer will be 4041 * set to point to the skb in which this space begins. 4042 * 4043 * The number of scatterlist elements required to completely map the 4044 * COW'd and extended socket buffer will be returned. 4045 */ 4046 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4047 { 4048 int copyflag; 4049 int elt; 4050 struct sk_buff *skb1, **skb_p; 4051 4052 /* If skb is cloned or its head is paged, reallocate 4053 * head pulling out all the pages (pages are considered not writable 4054 * at the moment even if they are anonymous). 4055 */ 4056 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4057 __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 4058 return -ENOMEM; 4059 4060 /* Easy case. Most of packets will go this way. */ 4061 if (!skb_has_frag_list(skb)) { 4062 /* A little of trouble, not enough of space for trailer. 4063 * This should not happen, when stack is tuned to generate 4064 * good frames. OK, on miss we reallocate and reserve even more 4065 * space, 128 bytes is fair. */ 4066 4067 if (skb_tailroom(skb) < tailbits && 4068 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4069 return -ENOMEM; 4070 4071 /* Voila! */ 4072 *trailer = skb; 4073 return 1; 4074 } 4075 4076 /* Misery. We are in troubles, going to mincer fragments... */ 4077 4078 elt = 1; 4079 skb_p = &skb_shinfo(skb)->frag_list; 4080 copyflag = 0; 4081 4082 while ((skb1 = *skb_p) != NULL) { 4083 int ntail = 0; 4084 4085 /* The fragment is partially pulled by someone, 4086 * this can happen on input. Copy it and everything 4087 * after it. */ 4088 4089 if (skb_shared(skb1)) 4090 copyflag = 1; 4091 4092 /* If the skb is the last, worry about trailer. */ 4093 4094 if (skb1->next == NULL && tailbits) { 4095 if (skb_shinfo(skb1)->nr_frags || 4096 skb_has_frag_list(skb1) || 4097 skb_tailroom(skb1) < tailbits) 4098 ntail = tailbits + 128; 4099 } 4100 4101 if (copyflag || 4102 skb_cloned(skb1) || 4103 ntail || 4104 skb_shinfo(skb1)->nr_frags || 4105 skb_has_frag_list(skb1)) { 4106 struct sk_buff *skb2; 4107 4108 /* Fuck, we are miserable poor guys... */ 4109 if (ntail == 0) 4110 skb2 = skb_copy(skb1, GFP_ATOMIC); 4111 else 4112 skb2 = skb_copy_expand(skb1, 4113 skb_headroom(skb1), 4114 ntail, 4115 GFP_ATOMIC); 4116 if (unlikely(skb2 == NULL)) 4117 return -ENOMEM; 4118 4119 if (skb1->sk) 4120 skb_set_owner_w(skb2, skb1->sk); 4121 4122 /* Looking around. Are we still alive? 4123 * OK, link new skb, drop old one */ 4124 4125 skb2->next = skb1->next; 4126 *skb_p = skb2; 4127 kfree_skb(skb1); 4128 skb1 = skb2; 4129 } 4130 elt++; 4131 *trailer = skb1; 4132 skb_p = &skb1->next; 4133 } 4134 4135 return elt; 4136 } 4137 EXPORT_SYMBOL_GPL(skb_cow_data); 4138 4139 static void sock_rmem_free(struct sk_buff *skb) 4140 { 4141 struct sock *sk = skb->sk; 4142 4143 atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4144 } 4145 4146 static void skb_set_err_queue(struct sk_buff *skb) 4147 { 4148 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 4149 * So, it is safe to (mis)use it to mark skbs on the error queue. 4150 */ 4151 skb->pkt_type = PACKET_OUTGOING; 4152 BUILD_BUG_ON(PACKET_OUTGOING == 0); 4153 } 4154 4155 /* 4156 * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4157 */ 4158 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4159 { 4160 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4161 (unsigned int)sk->sk_rcvbuf) 4162 return -ENOMEM; 4163 4164 skb_orphan(skb); 4165 skb->sk = sk; 4166 skb->destructor = sock_rmem_free; 4167 atomic_add(skb->truesize, &sk->sk_rmem_alloc); 4168 skb_set_err_queue(skb); 4169 4170 /* before exiting rcu section, make sure dst is refcounted */ 4171 skb_dst_force(skb); 4172 4173 skb_queue_tail(&sk->sk_error_queue, skb); 4174 if (!sock_flag(sk, SOCK_DEAD)) 4175 sk->sk_data_ready(sk); 4176 return 0; 4177 } 4178 EXPORT_SYMBOL(sock_queue_err_skb); 4179 4180 static bool is_icmp_err_skb(const struct sk_buff *skb) 4181 { 4182 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 4183 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 4184 } 4185 4186 struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4187 { 4188 struct sk_buff_head *q = &sk->sk_error_queue; 4189 struct sk_buff *skb, *skb_next = NULL; 4190 bool icmp_next = false; 4191 unsigned long flags; 4192 4193 spin_lock_irqsave(&q->lock, flags); 4194 skb = __skb_dequeue(q); 4195 if (skb && (skb_next = skb_peek(q))) { 4196 icmp_next = is_icmp_err_skb(skb_next); 4197 if (icmp_next) 4198 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin; 4199 } 4200 spin_unlock_irqrestore(&q->lock, flags); 4201 4202 if (is_icmp_err_skb(skb) && !icmp_next) 4203 sk->sk_err = 0; 4204 4205 if (skb_next) 4206 sk->sk_error_report(sk); 4207 4208 return skb; 4209 } 4210 EXPORT_SYMBOL(sock_dequeue_err_skb); 4211 4212 /** 4213 * skb_clone_sk - create clone of skb, and take reference to socket 4214 * @skb: the skb to clone 4215 * 4216 * This function creates a clone of a buffer that holds a reference on 4217 * sk_refcnt. Buffers created via this function are meant to be 4218 * returned using sock_queue_err_skb, or free via kfree_skb. 4219 * 4220 * When passing buffers allocated with this function to sock_queue_err_skb 4221 * it is necessary to wrap the call with sock_hold/sock_put in order to 4222 * prevent the socket from being released prior to being enqueued on 4223 * the sk_error_queue. 4224 */ 4225 struct sk_buff *skb_clone_sk(struct sk_buff *skb) 4226 { 4227 struct sock *sk = skb->sk; 4228 struct sk_buff *clone; 4229 4230 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 4231 return NULL; 4232 4233 clone = skb_clone(skb, GFP_ATOMIC); 4234 if (!clone) { 4235 sock_put(sk); 4236 return NULL; 4237 } 4238 4239 clone->sk = sk; 4240 clone->destructor = sock_efree; 4241 4242 return clone; 4243 } 4244 EXPORT_SYMBOL(skb_clone_sk); 4245 4246 static void __skb_complete_tx_timestamp(struct sk_buff *skb, 4247 struct sock *sk, 4248 int tstype, 4249 bool opt_stats) 4250 { 4251 struct sock_exterr_skb *serr; 4252 int err; 4253 4254 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 4255 4256 serr = SKB_EXT_ERR(skb); 4257 memset(serr, 0, sizeof(*serr)); 4258 serr->ee.ee_errno = ENOMSG; 4259 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4260 serr->ee.ee_info = tstype; 4261 serr->opt_stats = opt_stats; 4262 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 4263 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 4264 serr->ee.ee_data = skb_shinfo(skb)->tskey; 4265 if (sk->sk_protocol == IPPROTO_TCP && 4266 sk->sk_type == SOCK_STREAM) 4267 serr->ee.ee_data -= sk->sk_tskey; 4268 } 4269 4270 err = sock_queue_err_skb(sk, skb); 4271 4272 if (err) 4273 kfree_skb(skb); 4274 } 4275 4276 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4277 { 4278 bool ret; 4279 4280 if (likely(sysctl_tstamp_allow_data || tsonly)) 4281 return true; 4282 4283 read_lock_bh(&sk->sk_callback_lock); 4284 ret = sk->sk_socket && sk->sk_socket->file && 4285 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4286 read_unlock_bh(&sk->sk_callback_lock); 4287 return ret; 4288 } 4289 4290 void skb_complete_tx_timestamp(struct sk_buff *skb, 4291 struct skb_shared_hwtstamps *hwtstamps) 4292 { 4293 struct sock *sk = skb->sk; 4294 4295 if (!skb_may_tx_timestamp(sk, false)) 4296 return; 4297 4298 /* Take a reference to prevent skb_orphan() from freeing the socket, 4299 * but only if the socket refcount is not zero. 4300 */ 4301 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 4302 *skb_hwtstamps(skb) = *hwtstamps; 4303 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 4304 sock_put(sk); 4305 } 4306 } 4307 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 4308 4309 void __skb_tstamp_tx(struct sk_buff *orig_skb, 4310 struct skb_shared_hwtstamps *hwtstamps, 4311 struct sock *sk, int tstype) 4312 { 4313 struct sk_buff *skb; 4314 bool tsonly, opt_stats = false; 4315 4316 if (!sk) 4317 return; 4318 4319 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4320 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4321 return; 4322 4323 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 4324 if (!skb_may_tx_timestamp(sk, tsonly)) 4325 return; 4326 4327 if (tsonly) { 4328 #ifdef CONFIG_INET 4329 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 4330 sk->sk_protocol == IPPROTO_TCP && 4331 sk->sk_type == SOCK_STREAM) { 4332 skb = tcp_get_timestamping_opt_stats(sk); 4333 opt_stats = true; 4334 } else 4335 #endif 4336 skb = alloc_skb(0, GFP_ATOMIC); 4337 } else { 4338 skb = skb_clone(orig_skb, GFP_ATOMIC); 4339 } 4340 if (!skb) 4341 return; 4342 4343 if (tsonly) { 4344 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4345 SKBTX_ANY_TSTAMP; 4346 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 4347 } 4348 4349 if (hwtstamps) 4350 *skb_hwtstamps(skb) = *hwtstamps; 4351 else 4352 skb->tstamp = ktime_get_real(); 4353 4354 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 4355 } 4356 EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4357 4358 void skb_tstamp_tx(struct sk_buff *orig_skb, 4359 struct skb_shared_hwtstamps *hwtstamps) 4360 { 4361 return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 4362 SCM_TSTAMP_SND); 4363 } 4364 EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4365 4366 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 4367 { 4368 struct sock *sk = skb->sk; 4369 struct sock_exterr_skb *serr; 4370 int err = 1; 4371 4372 skb->wifi_acked_valid = 1; 4373 skb->wifi_acked = acked; 4374 4375 serr = SKB_EXT_ERR(skb); 4376 memset(serr, 0, sizeof(*serr)); 4377 serr->ee.ee_errno = ENOMSG; 4378 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 4379 4380 /* Take a reference to prevent skb_orphan() from freeing the socket, 4381 * but only if the socket refcount is not zero. 4382 */ 4383 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 4384 err = sock_queue_err_skb(sk, skb); 4385 sock_put(sk); 4386 } 4387 if (err) 4388 kfree_skb(skb); 4389 } 4390 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 4391 4392 /** 4393 * skb_partial_csum_set - set up and verify partial csum values for packet 4394 * @skb: the skb to set 4395 * @start: the number of bytes after skb->data to start checksumming. 4396 * @off: the offset from start to place the checksum. 4397 * 4398 * For untrusted partially-checksummed packets, we need to make sure the values 4399 * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4400 * 4401 * This function checks and sets those values and skb->ip_summed: if this 4402 * returns false you should drop the packet. 4403 */ 4404 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4405 { 4406 if (unlikely(start > skb_headlen(skb)) || 4407 unlikely((int)start + off > skb_headlen(skb) - 2)) { 4408 net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 4409 start, off, skb_headlen(skb)); 4410 return false; 4411 } 4412 skb->ip_summed = CHECKSUM_PARTIAL; 4413 skb->csum_start = skb_headroom(skb) + start; 4414 skb->csum_offset = off; 4415 skb_set_transport_header(skb, start); 4416 return true; 4417 } 4418 EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4419 4420 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4421 unsigned int max) 4422 { 4423 if (skb_headlen(skb) >= len) 4424 return 0; 4425 4426 /* If we need to pullup then pullup to the max, so we 4427 * won't need to do it again. 4428 */ 4429 if (max > skb->len) 4430 max = skb->len; 4431 4432 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4433 return -ENOMEM; 4434 4435 if (skb_headlen(skb) < len) 4436 return -EPROTO; 4437 4438 return 0; 4439 } 4440 4441 #define MAX_TCP_HDR_LEN (15 * 4) 4442 4443 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4444 typeof(IPPROTO_IP) proto, 4445 unsigned int off) 4446 { 4447 switch (proto) { 4448 int err; 4449 4450 case IPPROTO_TCP: 4451 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4452 off + MAX_TCP_HDR_LEN); 4453 if (!err && !skb_partial_csum_set(skb, off, 4454 offsetof(struct tcphdr, 4455 check))) 4456 err = -EPROTO; 4457 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4458 4459 case IPPROTO_UDP: 4460 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4461 off + sizeof(struct udphdr)); 4462 if (!err && !skb_partial_csum_set(skb, off, 4463 offsetof(struct udphdr, 4464 check))) 4465 err = -EPROTO; 4466 return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4467 } 4468 4469 return ERR_PTR(-EPROTO); 4470 } 4471 4472 /* This value should be large enough to cover a tagged ethernet header plus 4473 * maximally sized IP and TCP or UDP headers. 4474 */ 4475 #define MAX_IP_HDR_LEN 128 4476 4477 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4478 { 4479 unsigned int off; 4480 bool fragment; 4481 __sum16 *csum; 4482 int err; 4483 4484 fragment = false; 4485 4486 err = skb_maybe_pull_tail(skb, 4487 sizeof(struct iphdr), 4488 MAX_IP_HDR_LEN); 4489 if (err < 0) 4490 goto out; 4491 4492 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 4493 fragment = true; 4494 4495 off = ip_hdrlen(skb); 4496 4497 err = -EPROTO; 4498 4499 if (fragment) 4500 goto out; 4501 4502 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 4503 if (IS_ERR(csum)) 4504 return PTR_ERR(csum); 4505 4506 if (recalculate) 4507 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 4508 ip_hdr(skb)->daddr, 4509 skb->len - off, 4510 ip_hdr(skb)->protocol, 0); 4511 err = 0; 4512 4513 out: 4514 return err; 4515 } 4516 4517 /* This value should be large enough to cover a tagged ethernet header plus 4518 * an IPv6 header, all options, and a maximal TCP or UDP header. 4519 */ 4520 #define MAX_IPV6_HDR_LEN 256 4521 4522 #define OPT_HDR(type, skb, off) \ 4523 (type *)(skb_network_header(skb) + (off)) 4524 4525 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 4526 { 4527 int err; 4528 u8 nexthdr; 4529 unsigned int off; 4530 unsigned int len; 4531 bool fragment; 4532 bool done; 4533 __sum16 *csum; 4534 4535 fragment = false; 4536 done = false; 4537 4538 off = sizeof(struct ipv6hdr); 4539 4540 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 4541 if (err < 0) 4542 goto out; 4543 4544 nexthdr = ipv6_hdr(skb)->nexthdr; 4545 4546 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 4547 while (off <= len && !done) { 4548 switch (nexthdr) { 4549 case IPPROTO_DSTOPTS: 4550 case IPPROTO_HOPOPTS: 4551 case IPPROTO_ROUTING: { 4552 struct ipv6_opt_hdr *hp; 4553 4554 err = skb_maybe_pull_tail(skb, 4555 off + 4556 sizeof(struct ipv6_opt_hdr), 4557 MAX_IPV6_HDR_LEN); 4558 if (err < 0) 4559 goto out; 4560 4561 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4562 nexthdr = hp->nexthdr; 4563 off += ipv6_optlen(hp); 4564 break; 4565 } 4566 case IPPROTO_AH: { 4567 struct ip_auth_hdr *hp; 4568 4569 err = skb_maybe_pull_tail(skb, 4570 off + 4571 sizeof(struct ip_auth_hdr), 4572 MAX_IPV6_HDR_LEN); 4573 if (err < 0) 4574 goto out; 4575 4576 hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4577 nexthdr = hp->nexthdr; 4578 off += ipv6_authlen(hp); 4579 break; 4580 } 4581 case IPPROTO_FRAGMENT: { 4582 struct frag_hdr *hp; 4583 4584 err = skb_maybe_pull_tail(skb, 4585 off + 4586 sizeof(struct frag_hdr), 4587 MAX_IPV6_HDR_LEN); 4588 if (err < 0) 4589 goto out; 4590 4591 hp = OPT_HDR(struct frag_hdr, skb, off); 4592 4593 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4594 fragment = true; 4595 4596 nexthdr = hp->nexthdr; 4597 off += sizeof(struct frag_hdr); 4598 break; 4599 } 4600 default: 4601 done = true; 4602 break; 4603 } 4604 } 4605 4606 err = -EPROTO; 4607 4608 if (!done || fragment) 4609 goto out; 4610 4611 csum = skb_checksum_setup_ip(skb, nexthdr, off); 4612 if (IS_ERR(csum)) 4613 return PTR_ERR(csum); 4614 4615 if (recalculate) 4616 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4617 &ipv6_hdr(skb)->daddr, 4618 skb->len - off, nexthdr, 0); 4619 err = 0; 4620 4621 out: 4622 return err; 4623 } 4624 4625 /** 4626 * skb_checksum_setup - set up partial checksum offset 4627 * @skb: the skb to set up 4628 * @recalculate: if true the pseudo-header checksum will be recalculated 4629 */ 4630 int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4631 { 4632 int err; 4633 4634 switch (skb->protocol) { 4635 case htons(ETH_P_IP): 4636 err = skb_checksum_setup_ipv4(skb, recalculate); 4637 break; 4638 4639 case htons(ETH_P_IPV6): 4640 err = skb_checksum_setup_ipv6(skb, recalculate); 4641 break; 4642 4643 default: 4644 err = -EPROTO; 4645 break; 4646 } 4647 4648 return err; 4649 } 4650 EXPORT_SYMBOL(skb_checksum_setup); 4651 4652 /** 4653 * skb_checksum_maybe_trim - maybe trims the given skb 4654 * @skb: the skb to check 4655 * @transport_len: the data length beyond the network header 4656 * 4657 * Checks whether the given skb has data beyond the given transport length. 4658 * If so, returns a cloned skb trimmed to this transport length. 4659 * Otherwise returns the provided skb. Returns NULL in error cases 4660 * (e.g. transport_len exceeds skb length or out-of-memory). 4661 * 4662 * Caller needs to set the skb transport header and free any returned skb if it 4663 * differs from the provided skb. 4664 */ 4665 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 4666 unsigned int transport_len) 4667 { 4668 struct sk_buff *skb_chk; 4669 unsigned int len = skb_transport_offset(skb) + transport_len; 4670 int ret; 4671 4672 if (skb->len < len) 4673 return NULL; 4674 else if (skb->len == len) 4675 return skb; 4676 4677 skb_chk = skb_clone(skb, GFP_ATOMIC); 4678 if (!skb_chk) 4679 return NULL; 4680 4681 ret = pskb_trim_rcsum(skb_chk, len); 4682 if (ret) { 4683 kfree_skb(skb_chk); 4684 return NULL; 4685 } 4686 4687 return skb_chk; 4688 } 4689 4690 /** 4691 * skb_checksum_trimmed - validate checksum of an skb 4692 * @skb: the skb to check 4693 * @transport_len: the data length beyond the network header 4694 * @skb_chkf: checksum function to use 4695 * 4696 * Applies the given checksum function skb_chkf to the provided skb. 4697 * Returns a checked and maybe trimmed skb. Returns NULL on error. 4698 * 4699 * If the skb has data beyond the given transport length, then a 4700 * trimmed & cloned skb is checked and returned. 4701 * 4702 * Caller needs to set the skb transport header and free any returned skb if it 4703 * differs from the provided skb. 4704 */ 4705 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 4706 unsigned int transport_len, 4707 __sum16(*skb_chkf)(struct sk_buff *skb)) 4708 { 4709 struct sk_buff *skb_chk; 4710 unsigned int offset = skb_transport_offset(skb); 4711 __sum16 ret; 4712 4713 skb_chk = skb_checksum_maybe_trim(skb, transport_len); 4714 if (!skb_chk) 4715 goto err; 4716 4717 if (!pskb_may_pull(skb_chk, offset)) 4718 goto err; 4719 4720 skb_pull_rcsum(skb_chk, offset); 4721 ret = skb_chkf(skb_chk); 4722 skb_push_rcsum(skb_chk, offset); 4723 4724 if (ret) 4725 goto err; 4726 4727 return skb_chk; 4728 4729 err: 4730 if (skb_chk && skb_chk != skb) 4731 kfree_skb(skb_chk); 4732 4733 return NULL; 4734 4735 } 4736 EXPORT_SYMBOL(skb_checksum_trimmed); 4737 4738 void __skb_warn_lro_forwarding(const struct sk_buff *skb) 4739 { 4740 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4741 skb->dev->name); 4742 } 4743 EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4744 4745 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4746 { 4747 if (head_stolen) { 4748 skb_release_head_state(skb); 4749 kmem_cache_free(skbuff_head_cache, skb); 4750 } else { 4751 __kfree_skb(skb); 4752 } 4753 } 4754 EXPORT_SYMBOL(kfree_skb_partial); 4755 4756 /** 4757 * skb_try_coalesce - try to merge skb to prior one 4758 * @to: prior buffer 4759 * @from: buffer to add 4760 * @fragstolen: pointer to boolean 4761 * @delta_truesize: how much more was allocated than was requested 4762 */ 4763 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4764 bool *fragstolen, int *delta_truesize) 4765 { 4766 struct skb_shared_info *to_shinfo, *from_shinfo; 4767 int i, delta, len = from->len; 4768 4769 *fragstolen = false; 4770 4771 if (skb_cloned(to)) 4772 return false; 4773 4774 if (len <= skb_tailroom(to)) { 4775 if (len) 4776 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4777 *delta_truesize = 0; 4778 return true; 4779 } 4780 4781 to_shinfo = skb_shinfo(to); 4782 from_shinfo = skb_shinfo(from); 4783 if (to_shinfo->frag_list || from_shinfo->frag_list) 4784 return false; 4785 if (skb_zcopy(to) || skb_zcopy(from)) 4786 return false; 4787 4788 if (skb_headlen(from) != 0) { 4789 struct page *page; 4790 unsigned int offset; 4791 4792 if (to_shinfo->nr_frags + 4793 from_shinfo->nr_frags >= MAX_SKB_FRAGS) 4794 return false; 4795 4796 if (skb_head_is_locked(from)) 4797 return false; 4798 4799 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4800 4801 page = virt_to_head_page(from->head); 4802 offset = from->data - (unsigned char *)page_address(page); 4803 4804 skb_fill_page_desc(to, to_shinfo->nr_frags, 4805 page, offset, skb_headlen(from)); 4806 *fragstolen = true; 4807 } else { 4808 if (to_shinfo->nr_frags + 4809 from_shinfo->nr_frags > MAX_SKB_FRAGS) 4810 return false; 4811 4812 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4813 } 4814 4815 WARN_ON_ONCE(delta < len); 4816 4817 memcpy(to_shinfo->frags + to_shinfo->nr_frags, 4818 from_shinfo->frags, 4819 from_shinfo->nr_frags * sizeof(skb_frag_t)); 4820 to_shinfo->nr_frags += from_shinfo->nr_frags; 4821 4822 if (!skb_cloned(from)) 4823 from_shinfo->nr_frags = 0; 4824 4825 /* if the skb is not cloned this does nothing 4826 * since we set nr_frags to 0. 4827 */ 4828 for (i = 0; i < from_shinfo->nr_frags; i++) 4829 __skb_frag_ref(&from_shinfo->frags[i]); 4830 4831 to->truesize += delta; 4832 to->len += len; 4833 to->data_len += len; 4834 4835 *delta_truesize = delta; 4836 return true; 4837 } 4838 EXPORT_SYMBOL(skb_try_coalesce); 4839 4840 /** 4841 * skb_scrub_packet - scrub an skb 4842 * 4843 * @skb: buffer to clean 4844 * @xnet: packet is crossing netns 4845 * 4846 * skb_scrub_packet can be used after encapsulating or decapsulting a packet 4847 * into/from a tunnel. Some information have to be cleared during these 4848 * operations. 4849 * skb_scrub_packet can also be used to clean a skb before injecting it in 4850 * another namespace (@xnet == true). We have to clear all information in the 4851 * skb that could impact namespace isolation. 4852 */ 4853 void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4854 { 4855 skb->tstamp = 0; 4856 skb->pkt_type = PACKET_HOST; 4857 skb->skb_iif = 0; 4858 skb->ignore_df = 0; 4859 skb_dst_drop(skb); 4860 secpath_reset(skb); 4861 nf_reset(skb); 4862 nf_reset_trace(skb); 4863 4864 if (!xnet) 4865 return; 4866 4867 ipvs_reset(skb); 4868 skb_orphan(skb); 4869 skb->mark = 0; 4870 } 4871 EXPORT_SYMBOL_GPL(skb_scrub_packet); 4872 4873 /** 4874 * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4875 * 4876 * @skb: GSO skb 4877 * 4878 * skb_gso_transport_seglen is used to determine the real size of the 4879 * individual segments, including Layer4 headers (TCP/UDP). 4880 * 4881 * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4882 */ 4883 unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4884 { 4885 const struct skb_shared_info *shinfo = skb_shinfo(skb); 4886 unsigned int thlen = 0; 4887 4888 if (skb->encapsulation) { 4889 thlen = skb_inner_transport_header(skb) - 4890 skb_transport_header(skb); 4891 4892 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4893 thlen += inner_tcp_hdrlen(skb); 4894 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4895 thlen = tcp_hdrlen(skb); 4896 } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) { 4897 thlen = sizeof(struct sctphdr); 4898 } 4899 /* UFO sets gso_size to the size of the fragmentation 4900 * payload, i.e. the size of the L4 (UDP) header is already 4901 * accounted for. 4902 */ 4903 return thlen + shinfo->gso_size; 4904 } 4905 EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 4906 4907 /** 4908 * skb_gso_validate_mtu - Return in case such skb fits a given MTU 4909 * 4910 * @skb: GSO skb 4911 * @mtu: MTU to validate against 4912 * 4913 * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU 4914 * once split. 4915 */ 4916 bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu) 4917 { 4918 const struct skb_shared_info *shinfo = skb_shinfo(skb); 4919 const struct sk_buff *iter; 4920 unsigned int hlen; 4921 4922 hlen = skb_gso_network_seglen(skb); 4923 4924 if (shinfo->gso_size != GSO_BY_FRAGS) 4925 return hlen <= mtu; 4926 4927 /* Undo this so we can re-use header sizes */ 4928 hlen -= GSO_BY_FRAGS; 4929 4930 skb_walk_frags(skb, iter) { 4931 if (hlen + skb_headlen(iter) > mtu) 4932 return false; 4933 } 4934 4935 return true; 4936 } 4937 EXPORT_SYMBOL_GPL(skb_gso_validate_mtu); 4938 4939 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 4940 { 4941 if (skb_cow(skb, skb_headroom(skb)) < 0) { 4942 kfree_skb(skb); 4943 return NULL; 4944 } 4945 4946 memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN, 4947 2 * ETH_ALEN); 4948 skb->mac_header += VLAN_HLEN; 4949 return skb; 4950 } 4951 4952 struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 4953 { 4954 struct vlan_hdr *vhdr; 4955 u16 vlan_tci; 4956 4957 if (unlikely(skb_vlan_tag_present(skb))) { 4958 /* vlan_tci is already set-up so leave this for another time */ 4959 return skb; 4960 } 4961 4962 skb = skb_share_check(skb, GFP_ATOMIC); 4963 if (unlikely(!skb)) 4964 goto err_free; 4965 4966 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 4967 goto err_free; 4968 4969 vhdr = (struct vlan_hdr *)skb->data; 4970 vlan_tci = ntohs(vhdr->h_vlan_TCI); 4971 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 4972 4973 skb_pull_rcsum(skb, VLAN_HLEN); 4974 vlan_set_encap_proto(skb, vhdr); 4975 4976 skb = skb_reorder_vlan_header(skb); 4977 if (unlikely(!skb)) 4978 goto err_free; 4979 4980 skb_reset_network_header(skb); 4981 skb_reset_transport_header(skb); 4982 skb_reset_mac_len(skb); 4983 4984 return skb; 4985 4986 err_free: 4987 kfree_skb(skb); 4988 return NULL; 4989 } 4990 EXPORT_SYMBOL(skb_vlan_untag); 4991 4992 int skb_ensure_writable(struct sk_buff *skb, int write_len) 4993 { 4994 if (!pskb_may_pull(skb, write_len)) 4995 return -ENOMEM; 4996 4997 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4998 return 0; 4999 5000 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5001 } 5002 EXPORT_SYMBOL(skb_ensure_writable); 5003 5004 /* remove VLAN header from packet and update csum accordingly. 5005 * expects a non skb_vlan_tag_present skb with a vlan tag payload 5006 */ 5007 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 5008 { 5009 struct vlan_hdr *vhdr; 5010 int offset = skb->data - skb_mac_header(skb); 5011 int err; 5012 5013 if (WARN_ONCE(offset, 5014 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5015 offset)) { 5016 return -EINVAL; 5017 } 5018 5019 err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 5020 if (unlikely(err)) 5021 return err; 5022 5023 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 5024 5025 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 5026 *vlan_tci = ntohs(vhdr->h_vlan_TCI); 5027 5028 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 5029 __skb_pull(skb, VLAN_HLEN); 5030 5031 vlan_set_encap_proto(skb, vhdr); 5032 skb->mac_header += VLAN_HLEN; 5033 5034 if (skb_network_offset(skb) < ETH_HLEN) 5035 skb_set_network_header(skb, ETH_HLEN); 5036 5037 skb_reset_mac_len(skb); 5038 5039 return err; 5040 } 5041 EXPORT_SYMBOL(__skb_vlan_pop); 5042 5043 /* Pop a vlan tag either from hwaccel or from payload. 5044 * Expects skb->data at mac header. 5045 */ 5046 int skb_vlan_pop(struct sk_buff *skb) 5047 { 5048 u16 vlan_tci; 5049 __be16 vlan_proto; 5050 int err; 5051 5052 if (likely(skb_vlan_tag_present(skb))) { 5053 skb->vlan_tci = 0; 5054 } else { 5055 if (unlikely(!eth_type_vlan(skb->protocol))) 5056 return 0; 5057 5058 err = __skb_vlan_pop(skb, &vlan_tci); 5059 if (err) 5060 return err; 5061 } 5062 /* move next vlan tag to hw accel tag */ 5063 if (likely(!eth_type_vlan(skb->protocol))) 5064 return 0; 5065 5066 vlan_proto = skb->protocol; 5067 err = __skb_vlan_pop(skb, &vlan_tci); 5068 if (unlikely(err)) 5069 return err; 5070 5071 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 5072 return 0; 5073 } 5074 EXPORT_SYMBOL(skb_vlan_pop); 5075 5076 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5077 * Expects skb->data at mac header. 5078 */ 5079 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 5080 { 5081 if (skb_vlan_tag_present(skb)) { 5082 int offset = skb->data - skb_mac_header(skb); 5083 int err; 5084 5085 if (WARN_ONCE(offset, 5086 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5087 offset)) { 5088 return -EINVAL; 5089 } 5090 5091 err = __vlan_insert_tag(skb, skb->vlan_proto, 5092 skb_vlan_tag_get(skb)); 5093 if (err) 5094 return err; 5095 5096 skb->protocol = skb->vlan_proto; 5097 skb->mac_len += VLAN_HLEN; 5098 5099 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 5100 } 5101 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 5102 return 0; 5103 } 5104 EXPORT_SYMBOL(skb_vlan_push); 5105 5106 /** 5107 * alloc_skb_with_frags - allocate skb with page frags 5108 * 5109 * @header_len: size of linear part 5110 * @data_len: needed length in frags 5111 * @max_page_order: max page order desired. 5112 * @errcode: pointer to error code if any 5113 * @gfp_mask: allocation mask 5114 * 5115 * This can be used to allocate a paged skb, given a maximal order for frags. 5116 */ 5117 struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 5118 unsigned long data_len, 5119 int max_page_order, 5120 int *errcode, 5121 gfp_t gfp_mask) 5122 { 5123 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 5124 unsigned long chunk; 5125 struct sk_buff *skb; 5126 struct page *page; 5127 gfp_t gfp_head; 5128 int i; 5129 5130 *errcode = -EMSGSIZE; 5131 /* Note this test could be relaxed, if we succeed to allocate 5132 * high order pages... 5133 */ 5134 if (npages > MAX_SKB_FRAGS) 5135 return NULL; 5136 5137 gfp_head = gfp_mask; 5138 if (gfp_head & __GFP_DIRECT_RECLAIM) 5139 gfp_head |= __GFP_RETRY_MAYFAIL; 5140 5141 *errcode = -ENOBUFS; 5142 skb = alloc_skb(header_len, gfp_head); 5143 if (!skb) 5144 return NULL; 5145 5146 skb->truesize += npages << PAGE_SHIFT; 5147 5148 for (i = 0; npages > 0; i++) { 5149 int order = max_page_order; 5150 5151 while (order) { 5152 if (npages >= 1 << order) { 5153 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 5154 __GFP_COMP | 5155 __GFP_NOWARN | 5156 __GFP_NORETRY, 5157 order); 5158 if (page) 5159 goto fill_page; 5160 /* Do not retry other high order allocations */ 5161 order = 1; 5162 max_page_order = 0; 5163 } 5164 order--; 5165 } 5166 page = alloc_page(gfp_mask); 5167 if (!page) 5168 goto failure; 5169 fill_page: 5170 chunk = min_t(unsigned long, data_len, 5171 PAGE_SIZE << order); 5172 skb_fill_page_desc(skb, i, page, 0, chunk); 5173 data_len -= chunk; 5174 npages -= 1 << order; 5175 } 5176 return skb; 5177 5178 failure: 5179 kfree_skb(skb); 5180 return NULL; 5181 } 5182 EXPORT_SYMBOL(alloc_skb_with_frags); 5183 5184 /* carve out the first off bytes from skb when off < headlen */ 5185 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 5186 const int headlen, gfp_t gfp_mask) 5187 { 5188 int i; 5189 int size = skb_end_offset(skb); 5190 int new_hlen = headlen - off; 5191 u8 *data; 5192 5193 size = SKB_DATA_ALIGN(size); 5194 5195 if (skb_pfmemalloc(skb)) 5196 gfp_mask |= __GFP_MEMALLOC; 5197 data = kmalloc_reserve(size + 5198 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 5199 gfp_mask, NUMA_NO_NODE, NULL); 5200 if (!data) 5201 return -ENOMEM; 5202 5203 size = SKB_WITH_OVERHEAD(ksize(data)); 5204 5205 /* Copy real data, and all frags */ 5206 skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 5207 skb->len -= off; 5208 5209 memcpy((struct skb_shared_info *)(data + size), 5210 skb_shinfo(skb), 5211 offsetof(struct skb_shared_info, 5212 frags[skb_shinfo(skb)->nr_frags])); 5213 if (skb_cloned(skb)) { 5214 /* drop the old head gracefully */ 5215 if (skb_orphan_frags(skb, gfp_mask)) { 5216 kfree(data); 5217 return -ENOMEM; 5218 } 5219 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 5220 skb_frag_ref(skb, i); 5221 if (skb_has_frag_list(skb)) 5222 skb_clone_fraglist(skb); 5223 skb_release_data(skb); 5224 } else { 5225 /* we can reuse existing recount- all we did was 5226 * relocate values 5227 */ 5228 skb_free_head(skb); 5229 } 5230 5231 skb->head = data; 5232 skb->data = data; 5233 skb->head_frag = 0; 5234 #ifdef NET_SKBUFF_DATA_USES_OFFSET 5235 skb->end = size; 5236 #else 5237 skb->end = skb->head + size; 5238 #endif 5239 skb_set_tail_pointer(skb, skb_headlen(skb)); 5240 skb_headers_offset_update(skb, 0); 5241 skb->cloned = 0; 5242 skb->hdr_len = 0; 5243 skb->nohdr = 0; 5244 atomic_set(&skb_shinfo(skb)->dataref, 1); 5245 5246 return 0; 5247 } 5248 5249 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 5250 5251 /* carve out the first eat bytes from skb's frag_list. May recurse into 5252 * pskb_carve() 5253 */ 5254 static int pskb_carve_frag_list(struct sk_buff *skb, 5255 struct skb_shared_info *shinfo, int eat, 5256 gfp_t gfp_mask) 5257 { 5258 struct sk_buff *list = shinfo->frag_list; 5259 struct sk_buff *clone = NULL; 5260 struct sk_buff *insp = NULL; 5261 5262 do { 5263 if (!list) { 5264 pr_err("Not enough bytes to eat. Want %d\n", eat); 5265 return -EFAULT; 5266 } 5267 if (list->len <= eat) { 5268 /* Eaten as whole. */ 5269 eat -= list->len; 5270 list = list->next; 5271 insp = list; 5272 } else { 5273 /* Eaten partially. */ 5274 if (skb_shared(list)) { 5275 clone = skb_clone(list, gfp_mask); 5276 if (!clone) 5277 return -ENOMEM; 5278 insp = list->next; 5279 list = clone; 5280 } else { 5281 /* This may be pulled without problems. */ 5282 insp = list; 5283 } 5284 if (pskb_carve(list, eat, gfp_mask) < 0) { 5285 kfree_skb(clone); 5286 return -ENOMEM; 5287 } 5288 break; 5289 } 5290 } while (eat); 5291 5292 /* Free pulled out fragments. */ 5293 while ((list = shinfo->frag_list) != insp) { 5294 shinfo->frag_list = list->next; 5295 kfree_skb(list); 5296 } 5297 /* And insert new clone at head. */ 5298 if (clone) { 5299 clone->next = list; 5300 shinfo->frag_list = clone; 5301 } 5302 return 0; 5303 } 5304 5305 /* carve off first len bytes from skb. Split line (off) is in the 5306 * non-linear part of skb 5307 */ 5308 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 5309 int pos, gfp_t gfp_mask) 5310 { 5311 int i, k = 0; 5312 int size = skb_end_offset(skb); 5313 u8 *data; 5314 const int nfrags = skb_shinfo(skb)->nr_frags; 5315 struct skb_shared_info *shinfo; 5316 5317 size = SKB_DATA_ALIGN(size); 5318 5319 if (skb_pfmemalloc(skb)) 5320 gfp_mask |= __GFP_MEMALLOC; 5321 data = kmalloc_reserve(size + 5322 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 5323 gfp_mask, NUMA_NO_NODE, NULL); 5324 if (!data) 5325 return -ENOMEM; 5326 5327 size = SKB_WITH_OVERHEAD(ksize(data)); 5328 5329 memcpy((struct skb_shared_info *)(data + size), 5330 skb_shinfo(skb), offsetof(struct skb_shared_info, 5331 frags[skb_shinfo(skb)->nr_frags])); 5332 if (skb_orphan_frags(skb, gfp_mask)) { 5333 kfree(data); 5334 return -ENOMEM; 5335 } 5336 shinfo = (struct skb_shared_info *)(data + size); 5337 for (i = 0; i < nfrags; i++) { 5338 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 5339 5340 if (pos + fsize > off) { 5341 shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 5342 5343 if (pos < off) { 5344 /* Split frag. 5345 * We have two variants in this case: 5346 * 1. Move all the frag to the second 5347 * part, if it is possible. F.e. 5348 * this approach is mandatory for TUX, 5349 * where splitting is expensive. 5350 * 2. Split is accurately. We make this. 5351 */ 5352 shinfo->frags[0].page_offset += off - pos; 5353 skb_frag_size_sub(&shinfo->frags[0], off - pos); 5354 } 5355 skb_frag_ref(skb, i); 5356 k++; 5357 } 5358 pos += fsize; 5359 } 5360 shinfo->nr_frags = k; 5361 if (skb_has_frag_list(skb)) 5362 skb_clone_fraglist(skb); 5363 5364 if (k == 0) { 5365 /* split line is in frag list */ 5366 pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask); 5367 } 5368 skb_release_data(skb); 5369 5370 skb->head = data; 5371 skb->head_frag = 0; 5372 skb->data = data; 5373 #ifdef NET_SKBUFF_DATA_USES_OFFSET 5374 skb->end = size; 5375 #else 5376 skb->end = skb->head + size; 5377 #endif 5378 skb_reset_tail_pointer(skb); 5379 skb_headers_offset_update(skb, 0); 5380 skb->cloned = 0; 5381 skb->hdr_len = 0; 5382 skb->nohdr = 0; 5383 skb->len -= off; 5384 skb->data_len = skb->len; 5385 atomic_set(&skb_shinfo(skb)->dataref, 1); 5386 return 0; 5387 } 5388 5389 /* remove len bytes from the beginning of the skb */ 5390 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 5391 { 5392 int headlen = skb_headlen(skb); 5393 5394 if (len < headlen) 5395 return pskb_carve_inside_header(skb, len, headlen, gfp); 5396 else 5397 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 5398 } 5399 5400 /* Extract to_copy bytes starting at off from skb, and return this in 5401 * a new skb 5402 */ 5403 struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 5404 int to_copy, gfp_t gfp) 5405 { 5406 struct sk_buff *clone = skb_clone(skb, gfp); 5407 5408 if (!clone) 5409 return NULL; 5410 5411 if (pskb_carve(clone, off, gfp) < 0 || 5412 pskb_trim(clone, to_copy)) { 5413 kfree_skb(clone); 5414 return NULL; 5415 } 5416 return clone; 5417 } 5418 EXPORT_SYMBOL(pskb_extract); 5419 5420 /** 5421 * skb_condense - try to get rid of fragments/frag_list if possible 5422 * @skb: buffer 5423 * 5424 * Can be used to save memory before skb is added to a busy queue. 5425 * If packet has bytes in frags and enough tail room in skb->head, 5426 * pull all of them, so that we can free the frags right now and adjust 5427 * truesize. 5428 * Notes: 5429 * We do not reallocate skb->head thus can not fail. 5430 * Caller must re-evaluate skb->truesize if needed. 5431 */ 5432 void skb_condense(struct sk_buff *skb) 5433 { 5434 if (skb->data_len) { 5435 if (skb->data_len > skb->end - skb->tail || 5436 skb_cloned(skb)) 5437 return; 5438 5439 /* Nice, we can free page frag(s) right now */ 5440 __pskb_pull_tail(skb, skb->data_len); 5441 } 5442 /* At this point, skb->truesize might be over estimated, 5443 * because skb had a fragment, and fragments do not tell 5444 * their truesize. 5445 * When we pulled its content into skb->head, fragment 5446 * was freed, but __pskb_pull_tail() could not possibly 5447 * adjust skb->truesize, not knowing the frag truesize. 5448 */ 5449 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 5450 } 5451