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