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