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