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