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