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