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