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