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