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