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