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