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