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