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