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