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