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