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