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