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