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