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 83 #include "dev.h" 84 #include "sock_destructor.h" 85 86 struct kmem_cache *skbuff_head_cache __ro_after_init; 87 static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 88 #ifdef CONFIG_SKB_EXTENSIONS 89 static struct kmem_cache *skbuff_ext_cache __ro_after_init; 90 #endif 91 int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 92 EXPORT_SYMBOL(sysctl_max_skb_frags); 93 94 #undef FN 95 #define FN(reason) [SKB_DROP_REASON_##reason] = #reason, 96 const char * const drop_reasons[] = { 97 [SKB_CONSUMED] = "CONSUMED", 98 DEFINE_DROP_REASON(FN, FN) 99 }; 100 EXPORT_SYMBOL(drop_reasons); 101 102 /** 103 * skb_panic - private function for out-of-line support 104 * @skb: buffer 105 * @sz: size 106 * @addr: address 107 * @msg: skb_over_panic or skb_under_panic 108 * 109 * Out-of-line support for skb_put() and skb_push(). 110 * Called via the wrapper skb_over_panic() or skb_under_panic(). 111 * Keep out of line to prevent kernel bloat. 112 * __builtin_return_address is not used because it is not always reliable. 113 */ 114 static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 115 const char msg[]) 116 { 117 pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 118 msg, addr, skb->len, sz, skb->head, skb->data, 119 (unsigned long)skb->tail, (unsigned long)skb->end, 120 skb->dev ? skb->dev->name : "<NULL>"); 121 BUG(); 122 } 123 124 static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 125 { 126 skb_panic(skb, sz, addr, __func__); 127 } 128 129 static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 130 { 131 skb_panic(skb, sz, addr, __func__); 132 } 133 134 #define NAPI_SKB_CACHE_SIZE 64 135 #define NAPI_SKB_CACHE_BULK 16 136 #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 137 138 #if PAGE_SIZE == SZ_4K 139 140 #define NAPI_HAS_SMALL_PAGE_FRAG 1 141 #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) ((nc).pfmemalloc) 142 143 /* specialized page frag allocator using a single order 0 page 144 * and slicing it into 1K sized fragment. Constrained to systems 145 * with a very limited amount of 1K fragments fitting a single 146 * page - to avoid excessive truesize underestimation 147 */ 148 149 struct page_frag_1k { 150 void *va; 151 u16 offset; 152 bool pfmemalloc; 153 }; 154 155 static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp) 156 { 157 struct page *page; 158 int offset; 159 160 offset = nc->offset - SZ_1K; 161 if (likely(offset >= 0)) 162 goto use_frag; 163 164 page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 165 if (!page) 166 return NULL; 167 168 nc->va = page_address(page); 169 nc->pfmemalloc = page_is_pfmemalloc(page); 170 offset = PAGE_SIZE - SZ_1K; 171 page_ref_add(page, offset / SZ_1K); 172 173 use_frag: 174 nc->offset = offset; 175 return nc->va + offset; 176 } 177 #else 178 179 /* the small page is actually unused in this build; add dummy helpers 180 * to please the compiler and avoid later preprocessor's conditionals 181 */ 182 #define NAPI_HAS_SMALL_PAGE_FRAG 0 183 #define NAPI_SMALL_PAGE_PFMEMALLOC(nc) false 184 185 struct page_frag_1k { 186 }; 187 188 static void *page_frag_alloc_1k(struct page_frag_1k *nc, gfp_t gfp_mask) 189 { 190 return NULL; 191 } 192 193 #endif 194 195 struct napi_alloc_cache { 196 struct page_frag_cache page; 197 struct page_frag_1k page_small; 198 unsigned int skb_count; 199 void *skb_cache[NAPI_SKB_CACHE_SIZE]; 200 }; 201 202 static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 203 static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 204 205 /* Double check that napi_get_frags() allocates skbs with 206 * skb->head being backed by slab, not a page fragment. 207 * This is to make sure bug fixed in 3226b158e67c 208 * ("net: avoid 32 x truesize under-estimation for tiny skbs") 209 * does not accidentally come back. 210 */ 211 void napi_get_frags_check(struct napi_struct *napi) 212 { 213 struct sk_buff *skb; 214 215 local_bh_disable(); 216 skb = napi_get_frags(napi); 217 WARN_ON_ONCE(!NAPI_HAS_SMALL_PAGE_FRAG && skb && skb->head_frag); 218 napi_free_frags(napi); 219 local_bh_enable(); 220 } 221 222 void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 223 { 224 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 225 226 fragsz = SKB_DATA_ALIGN(fragsz); 227 228 return page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 229 } 230 EXPORT_SYMBOL(__napi_alloc_frag_align); 231 232 void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 233 { 234 void *data; 235 236 fragsz = SKB_DATA_ALIGN(fragsz); 237 if (in_hardirq() || irqs_disabled()) { 238 struct page_frag_cache *nc = this_cpu_ptr(&netdev_alloc_cache); 239 240 data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 241 } else { 242 struct napi_alloc_cache *nc; 243 244 local_bh_disable(); 245 nc = this_cpu_ptr(&napi_alloc_cache); 246 data = page_frag_alloc_align(&nc->page, fragsz, GFP_ATOMIC, align_mask); 247 local_bh_enable(); 248 } 249 return data; 250 } 251 EXPORT_SYMBOL(__netdev_alloc_frag_align); 252 253 static struct sk_buff *napi_skb_cache_get(void) 254 { 255 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 256 struct sk_buff *skb; 257 258 if (unlikely(!nc->skb_count)) { 259 nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, 260 GFP_ATOMIC, 261 NAPI_SKB_CACHE_BULK, 262 nc->skb_cache); 263 if (unlikely(!nc->skb_count)) 264 return NULL; 265 } 266 267 skb = nc->skb_cache[--nc->skb_count]; 268 kasan_unpoison_object_data(skbuff_head_cache, skb); 269 270 return skb; 271 } 272 273 static inline void __finalize_skb_around(struct sk_buff *skb, void *data, 274 unsigned int size) 275 { 276 struct skb_shared_info *shinfo; 277 278 size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 279 280 /* Assumes caller memset cleared SKB */ 281 skb->truesize = SKB_TRUESIZE(size); 282 refcount_set(&skb->users, 1); 283 skb->head = data; 284 skb->data = data; 285 skb_reset_tail_pointer(skb); 286 skb_set_end_offset(skb, size); 287 skb->mac_header = (typeof(skb->mac_header))~0U; 288 skb->transport_header = (typeof(skb->transport_header))~0U; 289 skb->alloc_cpu = raw_smp_processor_id(); 290 /* make sure we initialize shinfo sequentially */ 291 shinfo = skb_shinfo(skb); 292 memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 293 atomic_set(&shinfo->dataref, 1); 294 295 skb_set_kcov_handle(skb, kcov_common_handle()); 296 } 297 298 static inline void *__slab_build_skb(struct sk_buff *skb, void *data, 299 unsigned int *size) 300 { 301 void *resized; 302 303 /* Must find the allocation size (and grow it to match). */ 304 *size = ksize(data); 305 /* krealloc() will immediately return "data" when 306 * "ksize(data)" is requested: it is the existing upper 307 * bounds. As a result, GFP_ATOMIC will be ignored. Note 308 * that this "new" pointer needs to be passed back to the 309 * caller for use so the __alloc_size hinting will be 310 * tracked correctly. 311 */ 312 resized = krealloc(data, *size, GFP_ATOMIC); 313 WARN_ON_ONCE(resized != data); 314 return resized; 315 } 316 317 /* build_skb() variant which can operate on slab buffers. 318 * Note that this should be used sparingly as slab buffers 319 * cannot be combined efficiently by GRO! 320 */ 321 struct sk_buff *slab_build_skb(void *data) 322 { 323 struct sk_buff *skb; 324 unsigned int size; 325 326 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 327 if (unlikely(!skb)) 328 return NULL; 329 330 memset(skb, 0, offsetof(struct sk_buff, tail)); 331 data = __slab_build_skb(skb, data, &size); 332 __finalize_skb_around(skb, data, size); 333 334 return skb; 335 } 336 EXPORT_SYMBOL(slab_build_skb); 337 338 /* Caller must provide SKB that is memset cleared */ 339 static void __build_skb_around(struct sk_buff *skb, void *data, 340 unsigned int frag_size) 341 { 342 unsigned int size = frag_size; 343 344 /* frag_size == 0 is considered deprecated now. Callers 345 * using slab buffer should use slab_build_skb() instead. 346 */ 347 if (WARN_ONCE(size == 0, "Use slab_build_skb() instead")) 348 data = __slab_build_skb(skb, data, &size); 349 350 __finalize_skb_around(skb, data, size); 351 } 352 353 /** 354 * __build_skb - build a network buffer 355 * @data: data buffer provided by caller 356 * @frag_size: size of data (must not be 0) 357 * 358 * Allocate a new &sk_buff. Caller provides space holding head and 359 * skb_shared_info. @data must have been allocated from the page 360 * allocator or vmalloc(). (A @frag_size of 0 to indicate a kmalloc() 361 * allocation is deprecated, and callers should use slab_build_skb() 362 * instead.) 363 * The return is the new skb buffer. 364 * On a failure the return is %NULL, and @data is not freed. 365 * Notes : 366 * Before IO, driver allocates only data buffer where NIC put incoming frame 367 * Driver should add room at head (NET_SKB_PAD) and 368 * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 369 * After IO, driver calls build_skb(), to allocate sk_buff and populate it 370 * before giving packet to stack. 371 * RX rings only contains data buffers, not full skbs. 372 */ 373 struct sk_buff *__build_skb(void *data, unsigned int frag_size) 374 { 375 struct sk_buff *skb; 376 377 skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 378 if (unlikely(!skb)) 379 return NULL; 380 381 memset(skb, 0, offsetof(struct sk_buff, tail)); 382 __build_skb_around(skb, data, frag_size); 383 384 return skb; 385 } 386 387 /* build_skb() is wrapper over __build_skb(), that specifically 388 * takes care of skb->head and skb->pfmemalloc 389 */ 390 struct sk_buff *build_skb(void *data, unsigned int frag_size) 391 { 392 struct sk_buff *skb = __build_skb(data, frag_size); 393 394 if (skb && frag_size) { 395 skb->head_frag = 1; 396 if (page_is_pfmemalloc(virt_to_head_page(data))) 397 skb->pfmemalloc = 1; 398 } 399 return skb; 400 } 401 EXPORT_SYMBOL(build_skb); 402 403 /** 404 * build_skb_around - build a network buffer around provided skb 405 * @skb: sk_buff provide by caller, must be memset cleared 406 * @data: data buffer provided by caller 407 * @frag_size: size of data 408 */ 409 struct sk_buff *build_skb_around(struct sk_buff *skb, 410 void *data, unsigned int frag_size) 411 { 412 if (unlikely(!skb)) 413 return NULL; 414 415 __build_skb_around(skb, data, frag_size); 416 417 if (frag_size) { 418 skb->head_frag = 1; 419 if (page_is_pfmemalloc(virt_to_head_page(data))) 420 skb->pfmemalloc = 1; 421 } 422 return skb; 423 } 424 EXPORT_SYMBOL(build_skb_around); 425 426 /** 427 * __napi_build_skb - build a network buffer 428 * @data: data buffer provided by caller 429 * @frag_size: size of data 430 * 431 * Version of __build_skb() that uses NAPI percpu caches to obtain 432 * skbuff_head instead of inplace allocation. 433 * 434 * Returns a new &sk_buff on success, %NULL on allocation failure. 435 */ 436 static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 437 { 438 struct sk_buff *skb; 439 440 skb = napi_skb_cache_get(); 441 if (unlikely(!skb)) 442 return NULL; 443 444 memset(skb, 0, offsetof(struct sk_buff, tail)); 445 __build_skb_around(skb, data, frag_size); 446 447 return skb; 448 } 449 450 /** 451 * napi_build_skb - build a network buffer 452 * @data: data buffer provided by caller 453 * @frag_size: size of data 454 * 455 * Version of __napi_build_skb() that takes care of skb->head_frag 456 * and skb->pfmemalloc when the data is a page or page fragment. 457 * 458 * Returns a new &sk_buff on success, %NULL on allocation failure. 459 */ 460 struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 461 { 462 struct sk_buff *skb = __napi_build_skb(data, frag_size); 463 464 if (likely(skb) && frag_size) { 465 skb->head_frag = 1; 466 skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 467 } 468 469 return skb; 470 } 471 EXPORT_SYMBOL(napi_build_skb); 472 473 /* 474 * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 475 * the caller if emergency pfmemalloc reserves are being used. If it is and 476 * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 477 * may be used. Otherwise, the packet data may be discarded until enough 478 * memory is free 479 */ 480 static void *kmalloc_reserve(size_t size, gfp_t flags, int node, 481 bool *pfmemalloc) 482 { 483 void *obj; 484 bool ret_pfmemalloc = false; 485 486 /* 487 * Try a regular allocation, when that fails and we're not entitled 488 * to the reserves, fail. 489 */ 490 obj = kmalloc_node_track_caller(size, 491 flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 492 node); 493 if (obj || !(gfp_pfmemalloc_allowed(flags))) 494 goto out; 495 496 /* Try again but now we are using pfmemalloc reserves */ 497 ret_pfmemalloc = true; 498 obj = kmalloc_node_track_caller(size, flags, node); 499 500 out: 501 if (pfmemalloc) 502 *pfmemalloc = ret_pfmemalloc; 503 504 return obj; 505 } 506 507 /* Allocate a new skbuff. We do this ourselves so we can fill in a few 508 * 'private' fields and also do memory statistics to find all the 509 * [BEEP] leaks. 510 * 511 */ 512 513 /** 514 * __alloc_skb - allocate a network buffer 515 * @size: size to allocate 516 * @gfp_mask: allocation mask 517 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 518 * instead of head cache and allocate a cloned (child) skb. 519 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 520 * allocations in case the data is required for writeback 521 * @node: numa node to allocate memory on 522 * 523 * Allocate a new &sk_buff. The returned buffer has no headroom and a 524 * tail room of at least size bytes. The object has a reference count 525 * of one. The return is the buffer. On a failure the return is %NULL. 526 * 527 * Buffers may only be allocated from interrupts using a @gfp_mask of 528 * %GFP_ATOMIC. 529 */ 530 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 531 int flags, int node) 532 { 533 struct kmem_cache *cache; 534 struct sk_buff *skb; 535 unsigned int osize; 536 bool pfmemalloc; 537 u8 *data; 538 539 cache = (flags & SKB_ALLOC_FCLONE) 540 ? skbuff_fclone_cache : skbuff_head_cache; 541 542 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 543 gfp_mask |= __GFP_MEMALLOC; 544 545 /* Get the HEAD */ 546 if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 547 likely(node == NUMA_NO_NODE || node == numa_mem_id())) 548 skb = napi_skb_cache_get(); 549 else 550 skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 551 if (unlikely(!skb)) 552 return NULL; 553 prefetchw(skb); 554 555 /* We do our best to align skb_shared_info on a separate cache 556 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 557 * aligned memory blocks, unless SLUB/SLAB debug is enabled. 558 * Both skb->head and skb_shared_info are cache line aligned. 559 */ 560 size = SKB_DATA_ALIGN(size); 561 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 562 osize = kmalloc_size_roundup(size); 563 data = kmalloc_reserve(osize, gfp_mask, node, &pfmemalloc); 564 if (unlikely(!data)) 565 goto nodata; 566 /* kmalloc_size_roundup() might give us more room than requested. 567 * Put skb_shared_info exactly at the end of allocated zone, 568 * to allow max possible filling before reallocation. 569 */ 570 size = SKB_WITH_OVERHEAD(osize); 571 prefetchw(data + size); 572 573 /* 574 * Only clear those fields we need to clear, not those that we will 575 * actually initialise below. Hence, don't put any more fields after 576 * the tail pointer in struct sk_buff! 577 */ 578 memset(skb, 0, offsetof(struct sk_buff, tail)); 579 __build_skb_around(skb, data, osize); 580 skb->pfmemalloc = pfmemalloc; 581 582 if (flags & SKB_ALLOC_FCLONE) { 583 struct sk_buff_fclones *fclones; 584 585 fclones = container_of(skb, struct sk_buff_fclones, skb1); 586 587 skb->fclone = SKB_FCLONE_ORIG; 588 refcount_set(&fclones->fclone_ref, 1); 589 } 590 591 return skb; 592 593 nodata: 594 kmem_cache_free(cache, skb); 595 return NULL; 596 } 597 EXPORT_SYMBOL(__alloc_skb); 598 599 /** 600 * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 601 * @dev: network device to receive on 602 * @len: length to allocate 603 * @gfp_mask: get_free_pages mask, passed to alloc_skb 604 * 605 * Allocate a new &sk_buff and assign it a usage count of one. The 606 * buffer has NET_SKB_PAD headroom built in. Users should allocate 607 * the headroom they think they need without accounting for the 608 * built in space. The built in space is used for optimisations. 609 * 610 * %NULL is returned if there is no free memory. 611 */ 612 struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 613 gfp_t gfp_mask) 614 { 615 struct page_frag_cache *nc; 616 struct sk_buff *skb; 617 bool pfmemalloc; 618 void *data; 619 620 len += NET_SKB_PAD; 621 622 /* If requested length is either too small or too big, 623 * we use kmalloc() for skb->head allocation. 624 */ 625 if (len <= SKB_WITH_OVERHEAD(1024) || 626 len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 627 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 628 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 629 if (!skb) 630 goto skb_fail; 631 goto skb_success; 632 } 633 634 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 635 len = SKB_DATA_ALIGN(len); 636 637 if (sk_memalloc_socks()) 638 gfp_mask |= __GFP_MEMALLOC; 639 640 if (in_hardirq() || irqs_disabled()) { 641 nc = this_cpu_ptr(&netdev_alloc_cache); 642 data = page_frag_alloc(nc, len, gfp_mask); 643 pfmemalloc = nc->pfmemalloc; 644 } else { 645 local_bh_disable(); 646 nc = this_cpu_ptr(&napi_alloc_cache.page); 647 data = page_frag_alloc(nc, len, gfp_mask); 648 pfmemalloc = nc->pfmemalloc; 649 local_bh_enable(); 650 } 651 652 if (unlikely(!data)) 653 return NULL; 654 655 skb = __build_skb(data, len); 656 if (unlikely(!skb)) { 657 skb_free_frag(data); 658 return NULL; 659 } 660 661 if (pfmemalloc) 662 skb->pfmemalloc = 1; 663 skb->head_frag = 1; 664 665 skb_success: 666 skb_reserve(skb, NET_SKB_PAD); 667 skb->dev = dev; 668 669 skb_fail: 670 return skb; 671 } 672 EXPORT_SYMBOL(__netdev_alloc_skb); 673 674 /** 675 * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 676 * @napi: napi instance this buffer was allocated for 677 * @len: length to allocate 678 * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 679 * 680 * Allocate a new sk_buff for use in NAPI receive. This buffer will 681 * attempt to allocate the head from a special reserved region used 682 * only for NAPI Rx allocation. By doing this we can save several 683 * CPU cycles by avoiding having to disable and re-enable IRQs. 684 * 685 * %NULL is returned if there is no free memory. 686 */ 687 struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 688 gfp_t gfp_mask) 689 { 690 struct napi_alloc_cache *nc; 691 struct sk_buff *skb; 692 bool pfmemalloc; 693 void *data; 694 695 DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 696 len += NET_SKB_PAD + NET_IP_ALIGN; 697 698 /* If requested length is either too small or too big, 699 * we use kmalloc() for skb->head allocation. 700 * When the small frag allocator is available, prefer it over kmalloc 701 * for small fragments 702 */ 703 if ((!NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) || 704 len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 705 (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 706 skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 707 NUMA_NO_NODE); 708 if (!skb) 709 goto skb_fail; 710 goto skb_success; 711 } 712 713 nc = this_cpu_ptr(&napi_alloc_cache); 714 715 if (sk_memalloc_socks()) 716 gfp_mask |= __GFP_MEMALLOC; 717 718 if (NAPI_HAS_SMALL_PAGE_FRAG && len <= SKB_WITH_OVERHEAD(1024)) { 719 /* we are artificially inflating the allocation size, but 720 * that is not as bad as it may look like, as: 721 * - 'len' less than GRO_MAX_HEAD makes little sense 722 * - On most systems, larger 'len' values lead to fragment 723 * size above 512 bytes 724 * - kmalloc would use the kmalloc-1k slab for such values 725 * - Builds with smaller GRO_MAX_HEAD will very likely do 726 * little networking, as that implies no WiFi and no 727 * tunnels support, and 32 bits arches. 728 */ 729 len = SZ_1K; 730 731 data = page_frag_alloc_1k(&nc->page_small, gfp_mask); 732 pfmemalloc = NAPI_SMALL_PAGE_PFMEMALLOC(nc->page_small); 733 } else { 734 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 735 len = SKB_DATA_ALIGN(len); 736 737 data = page_frag_alloc(&nc->page, len, gfp_mask); 738 pfmemalloc = nc->page.pfmemalloc; 739 } 740 741 if (unlikely(!data)) 742 return NULL; 743 744 skb = __napi_build_skb(data, len); 745 if (unlikely(!skb)) { 746 skb_free_frag(data); 747 return NULL; 748 } 749 750 if (pfmemalloc) 751 skb->pfmemalloc = 1; 752 skb->head_frag = 1; 753 754 skb_success: 755 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 756 skb->dev = napi->dev; 757 758 skb_fail: 759 return skb; 760 } 761 EXPORT_SYMBOL(__napi_alloc_skb); 762 763 void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 764 int size, unsigned int truesize) 765 { 766 skb_fill_page_desc(skb, i, page, off, size); 767 skb->len += size; 768 skb->data_len += size; 769 skb->truesize += truesize; 770 } 771 EXPORT_SYMBOL(skb_add_rx_frag); 772 773 void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 774 unsigned int truesize) 775 { 776 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 777 778 skb_frag_size_add(frag, size); 779 skb->len += size; 780 skb->data_len += size; 781 skb->truesize += truesize; 782 } 783 EXPORT_SYMBOL(skb_coalesce_rx_frag); 784 785 static void skb_drop_list(struct sk_buff **listp) 786 { 787 kfree_skb_list(*listp); 788 *listp = NULL; 789 } 790 791 static inline void skb_drop_fraglist(struct sk_buff *skb) 792 { 793 skb_drop_list(&skb_shinfo(skb)->frag_list); 794 } 795 796 static void skb_clone_fraglist(struct sk_buff *skb) 797 { 798 struct sk_buff *list; 799 800 skb_walk_frags(skb, list) 801 skb_get(list); 802 } 803 804 static bool skb_pp_recycle(struct sk_buff *skb, void *data) 805 { 806 if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle) 807 return false; 808 return page_pool_return_skb_page(virt_to_page(data)); 809 } 810 811 static void skb_free_head(struct sk_buff *skb) 812 { 813 unsigned char *head = skb->head; 814 815 if (skb->head_frag) { 816 if (skb_pp_recycle(skb, head)) 817 return; 818 skb_free_frag(head); 819 } else { 820 kfree(head); 821 } 822 } 823 824 static void skb_release_data(struct sk_buff *skb, enum skb_drop_reason reason) 825 { 826 struct skb_shared_info *shinfo = skb_shinfo(skb); 827 int i; 828 829 if (skb->cloned && 830 atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 831 &shinfo->dataref)) 832 goto exit; 833 834 if (skb_zcopy(skb)) { 835 bool skip_unref = shinfo->flags & SKBFL_MANAGED_FRAG_REFS; 836 837 skb_zcopy_clear(skb, true); 838 if (skip_unref) 839 goto free_head; 840 } 841 842 for (i = 0; i < shinfo->nr_frags; i++) 843 __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle); 844 845 free_head: 846 if (shinfo->frag_list) 847 kfree_skb_list_reason(shinfo->frag_list, reason); 848 849 skb_free_head(skb); 850 exit: 851 /* When we clone an SKB we copy the reycling bit. The pp_recycle 852 * bit is only set on the head though, so in order to avoid races 853 * while trying to recycle fragments on __skb_frag_unref() we need 854 * to make one SKB responsible for triggering the recycle path. 855 * So disable the recycling bit if an SKB is cloned and we have 856 * additional references to the fragmented part of the SKB. 857 * Eventually the last SKB will have the recycling bit set and it's 858 * dataref set to 0, which will trigger the recycling 859 */ 860 skb->pp_recycle = 0; 861 } 862 863 /* 864 * Free an skbuff by memory without cleaning the state. 865 */ 866 static void kfree_skbmem(struct sk_buff *skb) 867 { 868 struct sk_buff_fclones *fclones; 869 870 switch (skb->fclone) { 871 case SKB_FCLONE_UNAVAILABLE: 872 kmem_cache_free(skbuff_head_cache, skb); 873 return; 874 875 case SKB_FCLONE_ORIG: 876 fclones = container_of(skb, struct sk_buff_fclones, skb1); 877 878 /* We usually free the clone (TX completion) before original skb 879 * This test would have no chance to be true for the clone, 880 * while here, branch prediction will be good. 881 */ 882 if (refcount_read(&fclones->fclone_ref) == 1) 883 goto fastpath; 884 break; 885 886 default: /* SKB_FCLONE_CLONE */ 887 fclones = container_of(skb, struct sk_buff_fclones, skb2); 888 break; 889 } 890 if (!refcount_dec_and_test(&fclones->fclone_ref)) 891 return; 892 fastpath: 893 kmem_cache_free(skbuff_fclone_cache, fclones); 894 } 895 896 void skb_release_head_state(struct sk_buff *skb) 897 { 898 skb_dst_drop(skb); 899 if (skb->destructor) { 900 DEBUG_NET_WARN_ON_ONCE(in_hardirq()); 901 skb->destructor(skb); 902 } 903 #if IS_ENABLED(CONFIG_NF_CONNTRACK) 904 nf_conntrack_put(skb_nfct(skb)); 905 #endif 906 skb_ext_put(skb); 907 } 908 909 /* Free everything but the sk_buff shell. */ 910 static void skb_release_all(struct sk_buff *skb, enum skb_drop_reason reason) 911 { 912 skb_release_head_state(skb); 913 if (likely(skb->head)) 914 skb_release_data(skb, reason); 915 } 916 917 /** 918 * __kfree_skb - private function 919 * @skb: buffer 920 * 921 * Free an sk_buff. Release anything attached to the buffer. 922 * Clean the state. This is an internal helper function. Users should 923 * always call kfree_skb 924 */ 925 926 void __kfree_skb(struct sk_buff *skb) 927 { 928 skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED); 929 kfree_skbmem(skb); 930 } 931 EXPORT_SYMBOL(__kfree_skb); 932 933 static __always_inline 934 bool __kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 935 { 936 if (unlikely(!skb_unref(skb))) 937 return false; 938 939 DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX); 940 941 if (reason == SKB_CONSUMED) 942 trace_consume_skb(skb); 943 else 944 trace_kfree_skb(skb, __builtin_return_address(0), reason); 945 return true; 946 } 947 948 /** 949 * kfree_skb_reason - free an sk_buff with special reason 950 * @skb: buffer to free 951 * @reason: reason why this skb is dropped 952 * 953 * Drop a reference to the buffer and free it if the usage count has 954 * hit zero. Meanwhile, pass the drop reason to 'kfree_skb' 955 * tracepoint. 956 */ 957 void __fix_address 958 kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) 959 { 960 if (__kfree_skb_reason(skb, reason)) 961 __kfree_skb(skb); 962 } 963 EXPORT_SYMBOL(kfree_skb_reason); 964 965 #define KFREE_SKB_BULK_SIZE 16 966 967 struct skb_free_array { 968 unsigned int skb_count; 969 void *skb_array[KFREE_SKB_BULK_SIZE]; 970 }; 971 972 static void kfree_skb_add_bulk(struct sk_buff *skb, 973 struct skb_free_array *sa, 974 enum skb_drop_reason reason) 975 { 976 /* if SKB is a clone, don't handle this case */ 977 if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 978 __kfree_skb(skb); 979 return; 980 } 981 982 skb_release_all(skb, reason); 983 sa->skb_array[sa->skb_count++] = skb; 984 985 if (unlikely(sa->skb_count == KFREE_SKB_BULK_SIZE)) { 986 kmem_cache_free_bulk(skbuff_head_cache, KFREE_SKB_BULK_SIZE, 987 sa->skb_array); 988 sa->skb_count = 0; 989 } 990 } 991 992 void __fix_address 993 kfree_skb_list_reason(struct sk_buff *segs, enum skb_drop_reason reason) 994 { 995 struct skb_free_array sa; 996 997 sa.skb_count = 0; 998 999 while (segs) { 1000 struct sk_buff *next = segs->next; 1001 1002 skb_mark_not_on_list(segs); 1003 1004 if (__kfree_skb_reason(segs, reason)) 1005 kfree_skb_add_bulk(segs, &sa, reason); 1006 1007 segs = next; 1008 } 1009 1010 if (sa.skb_count) 1011 kmem_cache_free_bulk(skbuff_head_cache, sa.skb_count, 1012 sa.skb_array); 1013 } 1014 EXPORT_SYMBOL(kfree_skb_list_reason); 1015 1016 /* Dump skb information and contents. 1017 * 1018 * Must only be called from net_ratelimit()-ed paths. 1019 * 1020 * Dumps whole packets if full_pkt, only headers otherwise. 1021 */ 1022 void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 1023 { 1024 struct skb_shared_info *sh = skb_shinfo(skb); 1025 struct net_device *dev = skb->dev; 1026 struct sock *sk = skb->sk; 1027 struct sk_buff *list_skb; 1028 bool has_mac, has_trans; 1029 int headroom, tailroom; 1030 int i, len, seg_len; 1031 1032 if (full_pkt) 1033 len = skb->len; 1034 else 1035 len = min_t(int, skb->len, MAX_HEADER + 128); 1036 1037 headroom = skb_headroom(skb); 1038 tailroom = skb_tailroom(skb); 1039 1040 has_mac = skb_mac_header_was_set(skb); 1041 has_trans = skb_transport_header_was_set(skb); 1042 1043 printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 1044 "mac=(%d,%d) net=(%d,%d) trans=%d\n" 1045 "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 1046 "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 1047 "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 1048 level, skb->len, headroom, skb_headlen(skb), tailroom, 1049 has_mac ? skb->mac_header : -1, 1050 has_mac ? skb_mac_header_len(skb) : -1, 1051 skb->network_header, 1052 has_trans ? skb_network_header_len(skb) : -1, 1053 has_trans ? skb->transport_header : -1, 1054 sh->tx_flags, sh->nr_frags, 1055 sh->gso_size, sh->gso_type, sh->gso_segs, 1056 skb->csum, skb->ip_summed, skb->csum_complete_sw, 1057 skb->csum_valid, skb->csum_level, 1058 skb->hash, skb->sw_hash, skb->l4_hash, 1059 ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 1060 1061 if (dev) 1062 printk("%sdev name=%s feat=%pNF\n", 1063 level, dev->name, &dev->features); 1064 if (sk) 1065 printk("%ssk family=%hu type=%u proto=%u\n", 1066 level, sk->sk_family, sk->sk_type, sk->sk_protocol); 1067 1068 if (full_pkt && headroom) 1069 print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 1070 16, 1, skb->head, headroom, false); 1071 1072 seg_len = min_t(int, skb_headlen(skb), len); 1073 if (seg_len) 1074 print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 1075 16, 1, skb->data, seg_len, false); 1076 len -= seg_len; 1077 1078 if (full_pkt && tailroom) 1079 print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 1080 16, 1, skb_tail_pointer(skb), tailroom, false); 1081 1082 for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 1083 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1084 u32 p_off, p_len, copied; 1085 struct page *p; 1086 u8 *vaddr; 1087 1088 skb_frag_foreach_page(frag, skb_frag_off(frag), 1089 skb_frag_size(frag), p, p_off, p_len, 1090 copied) { 1091 seg_len = min_t(int, p_len, len); 1092 vaddr = kmap_atomic(p); 1093 print_hex_dump(level, "skb frag: ", 1094 DUMP_PREFIX_OFFSET, 1095 16, 1, vaddr + p_off, seg_len, false); 1096 kunmap_atomic(vaddr); 1097 len -= seg_len; 1098 if (!len) 1099 break; 1100 } 1101 } 1102 1103 if (full_pkt && skb_has_frag_list(skb)) { 1104 printk("skb fraglist:\n"); 1105 skb_walk_frags(skb, list_skb) 1106 skb_dump(level, list_skb, true); 1107 } 1108 } 1109 EXPORT_SYMBOL(skb_dump); 1110 1111 /** 1112 * skb_tx_error - report an sk_buff xmit error 1113 * @skb: buffer that triggered an error 1114 * 1115 * Report xmit error if a device callback is tracking this skb. 1116 * skb must be freed afterwards. 1117 */ 1118 void skb_tx_error(struct sk_buff *skb) 1119 { 1120 if (skb) { 1121 skb_zcopy_downgrade_managed(skb); 1122 skb_zcopy_clear(skb, true); 1123 } 1124 } 1125 EXPORT_SYMBOL(skb_tx_error); 1126 1127 #ifdef CONFIG_TRACEPOINTS 1128 /** 1129 * consume_skb - free an skbuff 1130 * @skb: buffer to free 1131 * 1132 * Drop a ref to the buffer and free it if the usage count has hit zero 1133 * Functions identically to kfree_skb, but kfree_skb assumes that the frame 1134 * is being dropped after a failure and notes that 1135 */ 1136 void consume_skb(struct sk_buff *skb) 1137 { 1138 if (!skb_unref(skb)) 1139 return; 1140 1141 trace_consume_skb(skb); 1142 __kfree_skb(skb); 1143 } 1144 EXPORT_SYMBOL(consume_skb); 1145 #endif 1146 1147 /** 1148 * __consume_stateless_skb - free an skbuff, assuming it is stateless 1149 * @skb: buffer to free 1150 * 1151 * Alike consume_skb(), but this variant assumes that this is the last 1152 * skb reference and all the head states have been already dropped 1153 */ 1154 void __consume_stateless_skb(struct sk_buff *skb) 1155 { 1156 trace_consume_skb(skb); 1157 skb_release_data(skb, SKB_CONSUMED); 1158 kfree_skbmem(skb); 1159 } 1160 1161 static void napi_skb_cache_put(struct sk_buff *skb) 1162 { 1163 struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 1164 u32 i; 1165 1166 kasan_poison_object_data(skbuff_head_cache, skb); 1167 nc->skb_cache[nc->skb_count++] = skb; 1168 1169 if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 1170 for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 1171 kasan_unpoison_object_data(skbuff_head_cache, 1172 nc->skb_cache[i]); 1173 1174 kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, 1175 nc->skb_cache + NAPI_SKB_CACHE_HALF); 1176 nc->skb_count = NAPI_SKB_CACHE_HALF; 1177 } 1178 } 1179 1180 void __kfree_skb_defer(struct sk_buff *skb) 1181 { 1182 skb_release_all(skb, SKB_DROP_REASON_NOT_SPECIFIED); 1183 napi_skb_cache_put(skb); 1184 } 1185 1186 void napi_skb_free_stolen_head(struct sk_buff *skb) 1187 { 1188 if (unlikely(skb->slow_gro)) { 1189 nf_reset_ct(skb); 1190 skb_dst_drop(skb); 1191 skb_ext_put(skb); 1192 skb_orphan(skb); 1193 skb->slow_gro = 0; 1194 } 1195 napi_skb_cache_put(skb); 1196 } 1197 1198 void napi_consume_skb(struct sk_buff *skb, int budget) 1199 { 1200 /* Zero budget indicate non-NAPI context called us, like netpoll */ 1201 if (unlikely(!budget)) { 1202 dev_consume_skb_any(skb); 1203 return; 1204 } 1205 1206 DEBUG_NET_WARN_ON_ONCE(!in_softirq()); 1207 1208 if (!skb_unref(skb)) 1209 return; 1210 1211 /* if reaching here SKB is ready to free */ 1212 trace_consume_skb(skb); 1213 1214 /* if SKB is a clone, don't handle this case */ 1215 if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 1216 __kfree_skb(skb); 1217 return; 1218 } 1219 1220 skb_release_all(skb, SKB_CONSUMED); 1221 napi_skb_cache_put(skb); 1222 } 1223 EXPORT_SYMBOL(napi_consume_skb); 1224 1225 /* Make sure a field is contained by headers group */ 1226 #define CHECK_SKB_FIELD(field) \ 1227 BUILD_BUG_ON(offsetof(struct sk_buff, field) != \ 1228 offsetof(struct sk_buff, headers.field)); \ 1229 1230 static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1231 { 1232 new->tstamp = old->tstamp; 1233 /* We do not copy old->sk */ 1234 new->dev = old->dev; 1235 memcpy(new->cb, old->cb, sizeof(old->cb)); 1236 skb_dst_copy(new, old); 1237 __skb_ext_copy(new, old); 1238 __nf_copy(new, old, false); 1239 1240 /* Note : this field could be in the headers group. 1241 * It is not yet because we do not want to have a 16 bit hole 1242 */ 1243 new->queue_mapping = old->queue_mapping; 1244 1245 memcpy(&new->headers, &old->headers, sizeof(new->headers)); 1246 CHECK_SKB_FIELD(protocol); 1247 CHECK_SKB_FIELD(csum); 1248 CHECK_SKB_FIELD(hash); 1249 CHECK_SKB_FIELD(priority); 1250 CHECK_SKB_FIELD(skb_iif); 1251 CHECK_SKB_FIELD(vlan_proto); 1252 CHECK_SKB_FIELD(vlan_tci); 1253 CHECK_SKB_FIELD(transport_header); 1254 CHECK_SKB_FIELD(network_header); 1255 CHECK_SKB_FIELD(mac_header); 1256 CHECK_SKB_FIELD(inner_protocol); 1257 CHECK_SKB_FIELD(inner_transport_header); 1258 CHECK_SKB_FIELD(inner_network_header); 1259 CHECK_SKB_FIELD(inner_mac_header); 1260 CHECK_SKB_FIELD(mark); 1261 #ifdef CONFIG_NETWORK_SECMARK 1262 CHECK_SKB_FIELD(secmark); 1263 #endif 1264 #ifdef CONFIG_NET_RX_BUSY_POLL 1265 CHECK_SKB_FIELD(napi_id); 1266 #endif 1267 CHECK_SKB_FIELD(alloc_cpu); 1268 #ifdef CONFIG_XPS 1269 CHECK_SKB_FIELD(sender_cpu); 1270 #endif 1271 #ifdef CONFIG_NET_SCHED 1272 CHECK_SKB_FIELD(tc_index); 1273 #endif 1274 1275 } 1276 1277 /* 1278 * You should not add any new code to this function. Add it to 1279 * __copy_skb_header above instead. 1280 */ 1281 static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 1282 { 1283 #define C(x) n->x = skb->x 1284 1285 n->next = n->prev = NULL; 1286 n->sk = NULL; 1287 __copy_skb_header(n, skb); 1288 1289 C(len); 1290 C(data_len); 1291 C(mac_len); 1292 n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 1293 n->cloned = 1; 1294 n->nohdr = 0; 1295 n->peeked = 0; 1296 C(pfmemalloc); 1297 C(pp_recycle); 1298 n->destructor = NULL; 1299 C(tail); 1300 C(end); 1301 C(head); 1302 C(head_frag); 1303 C(data); 1304 C(truesize); 1305 refcount_set(&n->users, 1); 1306 1307 atomic_inc(&(skb_shinfo(skb)->dataref)); 1308 skb->cloned = 1; 1309 1310 return n; 1311 #undef C 1312 } 1313 1314 /** 1315 * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1316 * @first: first sk_buff of the msg 1317 */ 1318 struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1319 { 1320 struct sk_buff *n; 1321 1322 n = alloc_skb(0, GFP_ATOMIC); 1323 if (!n) 1324 return NULL; 1325 1326 n->len = first->len; 1327 n->data_len = first->len; 1328 n->truesize = first->truesize; 1329 1330 skb_shinfo(n)->frag_list = first; 1331 1332 __copy_skb_header(n, first); 1333 n->destructor = NULL; 1334 1335 return n; 1336 } 1337 EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1338 1339 /** 1340 * skb_morph - morph one skb into another 1341 * @dst: the skb to receive the contents 1342 * @src: the skb to supply the contents 1343 * 1344 * This is identical to skb_clone except that the target skb is 1345 * supplied by the user. 1346 * 1347 * The target skb is returned upon exit. 1348 */ 1349 struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1350 { 1351 skb_release_all(dst, SKB_CONSUMED); 1352 return __skb_clone(dst, src); 1353 } 1354 EXPORT_SYMBOL_GPL(skb_morph); 1355 1356 int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1357 { 1358 unsigned long max_pg, num_pg, new_pg, old_pg; 1359 struct user_struct *user; 1360 1361 if (capable(CAP_IPC_LOCK) || !size) 1362 return 0; 1363 1364 num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1365 max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1366 user = mmp->user ? : current_user(); 1367 1368 old_pg = atomic_long_read(&user->locked_vm); 1369 do { 1370 new_pg = old_pg + num_pg; 1371 if (new_pg > max_pg) 1372 return -ENOBUFS; 1373 } while (!atomic_long_try_cmpxchg(&user->locked_vm, &old_pg, new_pg)); 1374 1375 if (!mmp->user) { 1376 mmp->user = get_uid(user); 1377 mmp->num_pg = num_pg; 1378 } else { 1379 mmp->num_pg += num_pg; 1380 } 1381 1382 return 0; 1383 } 1384 EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1385 1386 void mm_unaccount_pinned_pages(struct mmpin *mmp) 1387 { 1388 if (mmp->user) { 1389 atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1390 free_uid(mmp->user); 1391 } 1392 } 1393 EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1394 1395 static struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 1396 { 1397 struct ubuf_info_msgzc *uarg; 1398 struct sk_buff *skb; 1399 1400 WARN_ON_ONCE(!in_task()); 1401 1402 skb = sock_omalloc(sk, 0, GFP_KERNEL); 1403 if (!skb) 1404 return NULL; 1405 1406 BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 1407 uarg = (void *)skb->cb; 1408 uarg->mmp.user = NULL; 1409 1410 if (mm_account_pinned_pages(&uarg->mmp, size)) { 1411 kfree_skb(skb); 1412 return NULL; 1413 } 1414 1415 uarg->ubuf.callback = msg_zerocopy_callback; 1416 uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 1417 uarg->len = 1; 1418 uarg->bytelen = size; 1419 uarg->zerocopy = 1; 1420 uarg->ubuf.flags = SKBFL_ZEROCOPY_FRAG | SKBFL_DONT_ORPHAN; 1421 refcount_set(&uarg->ubuf.refcnt, 1); 1422 sock_hold(sk); 1423 1424 return &uarg->ubuf; 1425 } 1426 1427 static inline struct sk_buff *skb_from_uarg(struct ubuf_info_msgzc *uarg) 1428 { 1429 return container_of((void *)uarg, struct sk_buff, cb); 1430 } 1431 1432 struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 1433 struct ubuf_info *uarg) 1434 { 1435 if (uarg) { 1436 struct ubuf_info_msgzc *uarg_zc; 1437 const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 1438 u32 bytelen, next; 1439 1440 /* there might be non MSG_ZEROCOPY users */ 1441 if (uarg->callback != msg_zerocopy_callback) 1442 return NULL; 1443 1444 /* realloc only when socket is locked (TCP, UDP cork), 1445 * so uarg->len and sk_zckey access is serialized 1446 */ 1447 if (!sock_owned_by_user(sk)) { 1448 WARN_ON_ONCE(1); 1449 return NULL; 1450 } 1451 1452 uarg_zc = uarg_to_msgzc(uarg); 1453 bytelen = uarg_zc->bytelen + size; 1454 if (uarg_zc->len == USHRT_MAX - 1 || bytelen > byte_limit) { 1455 /* TCP can create new skb to attach new uarg */ 1456 if (sk->sk_type == SOCK_STREAM) 1457 goto new_alloc; 1458 return NULL; 1459 } 1460 1461 next = (u32)atomic_read(&sk->sk_zckey); 1462 if ((u32)(uarg_zc->id + uarg_zc->len) == next) { 1463 if (mm_account_pinned_pages(&uarg_zc->mmp, size)) 1464 return NULL; 1465 uarg_zc->len++; 1466 uarg_zc->bytelen = bytelen; 1467 atomic_set(&sk->sk_zckey, ++next); 1468 1469 /* no extra ref when appending to datagram (MSG_MORE) */ 1470 if (sk->sk_type == SOCK_STREAM) 1471 net_zcopy_get(uarg); 1472 1473 return uarg; 1474 } 1475 } 1476 1477 new_alloc: 1478 return msg_zerocopy_alloc(sk, size); 1479 } 1480 EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 1481 1482 static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 1483 { 1484 struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 1485 u32 old_lo, old_hi; 1486 u64 sum_len; 1487 1488 old_lo = serr->ee.ee_info; 1489 old_hi = serr->ee.ee_data; 1490 sum_len = old_hi - old_lo + 1ULL + len; 1491 1492 if (sum_len >= (1ULL << 32)) 1493 return false; 1494 1495 if (lo != old_hi + 1) 1496 return false; 1497 1498 serr->ee.ee_data += len; 1499 return true; 1500 } 1501 1502 static void __msg_zerocopy_callback(struct ubuf_info_msgzc *uarg) 1503 { 1504 struct sk_buff *tail, *skb = skb_from_uarg(uarg); 1505 struct sock_exterr_skb *serr; 1506 struct sock *sk = skb->sk; 1507 struct sk_buff_head *q; 1508 unsigned long flags; 1509 bool is_zerocopy; 1510 u32 lo, hi; 1511 u16 len; 1512 1513 mm_unaccount_pinned_pages(&uarg->mmp); 1514 1515 /* if !len, there was only 1 call, and it was aborted 1516 * so do not queue a completion notification 1517 */ 1518 if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 1519 goto release; 1520 1521 len = uarg->len; 1522 lo = uarg->id; 1523 hi = uarg->id + len - 1; 1524 is_zerocopy = uarg->zerocopy; 1525 1526 serr = SKB_EXT_ERR(skb); 1527 memset(serr, 0, sizeof(*serr)); 1528 serr->ee.ee_errno = 0; 1529 serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 1530 serr->ee.ee_data = hi; 1531 serr->ee.ee_info = lo; 1532 if (!is_zerocopy) 1533 serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 1534 1535 q = &sk->sk_error_queue; 1536 spin_lock_irqsave(&q->lock, flags); 1537 tail = skb_peek_tail(q); 1538 if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 1539 !skb_zerocopy_notify_extend(tail, lo, len)) { 1540 __skb_queue_tail(q, skb); 1541 skb = NULL; 1542 } 1543 spin_unlock_irqrestore(&q->lock, flags); 1544 1545 sk_error_report(sk); 1546 1547 release: 1548 consume_skb(skb); 1549 sock_put(sk); 1550 } 1551 1552 void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 1553 bool success) 1554 { 1555 struct ubuf_info_msgzc *uarg_zc = uarg_to_msgzc(uarg); 1556 1557 uarg_zc->zerocopy = uarg_zc->zerocopy & success; 1558 1559 if (refcount_dec_and_test(&uarg->refcnt)) 1560 __msg_zerocopy_callback(uarg_zc); 1561 } 1562 EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 1563 1564 void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 1565 { 1566 struct sock *sk = skb_from_uarg(uarg_to_msgzc(uarg))->sk; 1567 1568 atomic_dec(&sk->sk_zckey); 1569 uarg_to_msgzc(uarg)->len--; 1570 1571 if (have_uref) 1572 msg_zerocopy_callback(NULL, uarg, true); 1573 } 1574 EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 1575 1576 int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 1577 struct msghdr *msg, int len, 1578 struct ubuf_info *uarg) 1579 { 1580 struct ubuf_info *orig_uarg = skb_zcopy(skb); 1581 int err, orig_len = skb->len; 1582 1583 /* An skb can only point to one uarg. This edge case happens when 1584 * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 1585 */ 1586 if (orig_uarg && uarg != orig_uarg) 1587 return -EEXIST; 1588 1589 err = __zerocopy_sg_from_iter(msg, sk, skb, &msg->msg_iter, len); 1590 if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 1591 struct sock *save_sk = skb->sk; 1592 1593 /* Streams do not free skb on error. Reset to prev state. */ 1594 iov_iter_revert(&msg->msg_iter, skb->len - orig_len); 1595 skb->sk = sk; 1596 ___pskb_trim(skb, orig_len); 1597 skb->sk = save_sk; 1598 return err; 1599 } 1600 1601 skb_zcopy_set(skb, uarg, NULL); 1602 return skb->len - orig_len; 1603 } 1604 EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 1605 1606 void __skb_zcopy_downgrade_managed(struct sk_buff *skb) 1607 { 1608 int i; 1609 1610 skb_shinfo(skb)->flags &= ~SKBFL_MANAGED_FRAG_REFS; 1611 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1612 skb_frag_ref(skb, i); 1613 } 1614 EXPORT_SYMBOL_GPL(__skb_zcopy_downgrade_managed); 1615 1616 static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 1617 gfp_t gfp_mask) 1618 { 1619 if (skb_zcopy(orig)) { 1620 if (skb_zcopy(nskb)) { 1621 /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 1622 if (!gfp_mask) { 1623 WARN_ON_ONCE(1); 1624 return -ENOMEM; 1625 } 1626 if (skb_uarg(nskb) == skb_uarg(orig)) 1627 return 0; 1628 if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 1629 return -EIO; 1630 } 1631 skb_zcopy_set(nskb, skb_uarg(orig), NULL); 1632 } 1633 return 0; 1634 } 1635 1636 /** 1637 * skb_copy_ubufs - copy userspace skb frags buffers to kernel 1638 * @skb: the skb to modify 1639 * @gfp_mask: allocation priority 1640 * 1641 * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 1642 * It will copy all frags into kernel and drop the reference 1643 * to userspace pages. 1644 * 1645 * If this function is called from an interrupt gfp_mask() must be 1646 * %GFP_ATOMIC. 1647 * 1648 * Returns 0 on success or a negative error code on failure 1649 * to allocate kernel memory to copy to. 1650 */ 1651 int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1652 { 1653 int num_frags = skb_shinfo(skb)->nr_frags; 1654 struct page *page, *head = NULL; 1655 int i, new_frags; 1656 u32 d_off; 1657 1658 if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 1659 return -EINVAL; 1660 1661 if (!num_frags) 1662 goto release; 1663 1664 new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 1665 for (i = 0; i < new_frags; i++) { 1666 page = alloc_page(gfp_mask); 1667 if (!page) { 1668 while (head) { 1669 struct page *next = (struct page *)page_private(head); 1670 put_page(head); 1671 head = next; 1672 } 1673 return -ENOMEM; 1674 } 1675 set_page_private(page, (unsigned long)head); 1676 head = page; 1677 } 1678 1679 page = head; 1680 d_off = 0; 1681 for (i = 0; i < num_frags; i++) { 1682 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 1683 u32 p_off, p_len, copied; 1684 struct page *p; 1685 u8 *vaddr; 1686 1687 skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1688 p, p_off, p_len, copied) { 1689 u32 copy, done = 0; 1690 vaddr = kmap_atomic(p); 1691 1692 while (done < p_len) { 1693 if (d_off == PAGE_SIZE) { 1694 d_off = 0; 1695 page = (struct page *)page_private(page); 1696 } 1697 copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 1698 memcpy(page_address(page) + d_off, 1699 vaddr + p_off + done, copy); 1700 done += copy; 1701 d_off += copy; 1702 } 1703 kunmap_atomic(vaddr); 1704 } 1705 } 1706 1707 /* skb frags release userspace buffers */ 1708 for (i = 0; i < num_frags; i++) 1709 skb_frag_unref(skb, i); 1710 1711 /* skb frags point to kernel buffers */ 1712 for (i = 0; i < new_frags - 1; i++) { 1713 __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 1714 head = (struct page *)page_private(head); 1715 } 1716 __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 1717 skb_shinfo(skb)->nr_frags = new_frags; 1718 1719 release: 1720 skb_zcopy_clear(skb, false); 1721 return 0; 1722 } 1723 EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1724 1725 /** 1726 * skb_clone - duplicate an sk_buff 1727 * @skb: buffer to clone 1728 * @gfp_mask: allocation priority 1729 * 1730 * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1731 * copies share the same packet data but not structure. The new 1732 * buffer has a reference count of 1. If the allocation fails the 1733 * function returns %NULL otherwise the new buffer is returned. 1734 * 1735 * If this function is called from an interrupt gfp_mask() must be 1736 * %GFP_ATOMIC. 1737 */ 1738 1739 struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1740 { 1741 struct sk_buff_fclones *fclones = container_of(skb, 1742 struct sk_buff_fclones, 1743 skb1); 1744 struct sk_buff *n; 1745 1746 if (skb_orphan_frags(skb, gfp_mask)) 1747 return NULL; 1748 1749 if (skb->fclone == SKB_FCLONE_ORIG && 1750 refcount_read(&fclones->fclone_ref) == 1) { 1751 n = &fclones->skb2; 1752 refcount_set(&fclones->fclone_ref, 2); 1753 n->fclone = SKB_FCLONE_CLONE; 1754 } else { 1755 if (skb_pfmemalloc(skb)) 1756 gfp_mask |= __GFP_MEMALLOC; 1757 1758 n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1759 if (!n) 1760 return NULL; 1761 1762 n->fclone = SKB_FCLONE_UNAVAILABLE; 1763 } 1764 1765 return __skb_clone(n, skb); 1766 } 1767 EXPORT_SYMBOL(skb_clone); 1768 1769 void skb_headers_offset_update(struct sk_buff *skb, int off) 1770 { 1771 /* Only adjust this if it actually is csum_start rather than csum */ 1772 if (skb->ip_summed == CHECKSUM_PARTIAL) 1773 skb->csum_start += off; 1774 /* {transport,network,mac}_header and tail are relative to skb->head */ 1775 skb->transport_header += off; 1776 skb->network_header += off; 1777 if (skb_mac_header_was_set(skb)) 1778 skb->mac_header += off; 1779 skb->inner_transport_header += off; 1780 skb->inner_network_header += off; 1781 skb->inner_mac_header += off; 1782 } 1783 EXPORT_SYMBOL(skb_headers_offset_update); 1784 1785 void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 1786 { 1787 __copy_skb_header(new, old); 1788 1789 skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 1790 skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 1791 skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 1792 } 1793 EXPORT_SYMBOL(skb_copy_header); 1794 1795 static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1796 { 1797 if (skb_pfmemalloc(skb)) 1798 return SKB_ALLOC_RX; 1799 return 0; 1800 } 1801 1802 /** 1803 * skb_copy - create private copy of an sk_buff 1804 * @skb: buffer to copy 1805 * @gfp_mask: allocation priority 1806 * 1807 * Make a copy of both an &sk_buff and its data. This is used when the 1808 * caller wishes to modify the data and needs a private copy of the 1809 * data to alter. Returns %NULL on failure or the pointer to the buffer 1810 * on success. The returned buffer has a reference count of 1. 1811 * 1812 * As by-product this function converts non-linear &sk_buff to linear 1813 * one, so that &sk_buff becomes completely private and caller is allowed 1814 * to modify all the data of returned buffer. This means that this 1815 * function is not recommended for use in circumstances when only 1816 * header is going to be modified. Use pskb_copy() instead. 1817 */ 1818 1819 struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 1820 { 1821 int headerlen = skb_headroom(skb); 1822 unsigned int size = skb_end_offset(skb) + skb->data_len; 1823 struct sk_buff *n = __alloc_skb(size, gfp_mask, 1824 skb_alloc_rx_flag(skb), NUMA_NO_NODE); 1825 1826 if (!n) 1827 return NULL; 1828 1829 /* Set the data pointer */ 1830 skb_reserve(n, headerlen); 1831 /* Set the tail pointer and length */ 1832 skb_put(n, skb->len); 1833 1834 BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 1835 1836 skb_copy_header(n, skb); 1837 return n; 1838 } 1839 EXPORT_SYMBOL(skb_copy); 1840 1841 /** 1842 * __pskb_copy_fclone - create copy of an sk_buff with private head. 1843 * @skb: buffer to copy 1844 * @headroom: headroom of new skb 1845 * @gfp_mask: allocation priority 1846 * @fclone: if true allocate the copy of the skb from the fclone 1847 * cache instead of the head cache; it is recommended to set this 1848 * to true for the cases where the copy will likely be cloned 1849 * 1850 * Make a copy of both an &sk_buff and part of its data, located 1851 * in header. Fragmented data remain shared. This is used when 1852 * the caller wishes to modify only header of &sk_buff and needs 1853 * private copy of the header to alter. Returns %NULL on failure 1854 * or the pointer to the buffer on success. 1855 * The returned buffer has a reference count of 1. 1856 */ 1857 1858 struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1859 gfp_t gfp_mask, bool fclone) 1860 { 1861 unsigned int size = skb_headlen(skb) + headroom; 1862 int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1863 struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 1864 1865 if (!n) 1866 goto out; 1867 1868 /* Set the data pointer */ 1869 skb_reserve(n, headroom); 1870 /* Set the tail pointer and length */ 1871 skb_put(n, skb_headlen(skb)); 1872 /* Copy the bytes */ 1873 skb_copy_from_linear_data(skb, n->data, n->len); 1874 1875 n->truesize += skb->data_len; 1876 n->data_len = skb->data_len; 1877 n->len = skb->len; 1878 1879 if (skb_shinfo(skb)->nr_frags) { 1880 int i; 1881 1882 if (skb_orphan_frags(skb, gfp_mask) || 1883 skb_zerocopy_clone(n, skb, gfp_mask)) { 1884 kfree_skb(n); 1885 n = NULL; 1886 goto out; 1887 } 1888 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1889 skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1890 skb_frag_ref(skb, i); 1891 } 1892 skb_shinfo(n)->nr_frags = i; 1893 } 1894 1895 if (skb_has_frag_list(skb)) { 1896 skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 1897 skb_clone_fraglist(n); 1898 } 1899 1900 skb_copy_header(n, skb); 1901 out: 1902 return n; 1903 } 1904 EXPORT_SYMBOL(__pskb_copy_fclone); 1905 1906 /** 1907 * pskb_expand_head - reallocate header of &sk_buff 1908 * @skb: buffer to reallocate 1909 * @nhead: room to add at head 1910 * @ntail: room to add at tail 1911 * @gfp_mask: allocation priority 1912 * 1913 * Expands (or creates identical copy, if @nhead and @ntail are zero) 1914 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 1915 * reference count of 1. Returns zero in the case of success or error, 1916 * if expansion failed. In the last case, &sk_buff is not changed. 1917 * 1918 * All the pointers pointing into skb header may change and must be 1919 * reloaded after call to this function. 1920 */ 1921 1922 int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1923 gfp_t gfp_mask) 1924 { 1925 unsigned int osize = skb_end_offset(skb); 1926 unsigned int size = osize + nhead + ntail; 1927 long off; 1928 u8 *data; 1929 int i; 1930 1931 BUG_ON(nhead < 0); 1932 1933 BUG_ON(skb_shared(skb)); 1934 1935 skb_zcopy_downgrade_managed(skb); 1936 1937 if (skb_pfmemalloc(skb)) 1938 gfp_mask |= __GFP_MEMALLOC; 1939 1940 size = SKB_DATA_ALIGN(size); 1941 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 1942 size = kmalloc_size_roundup(size); 1943 data = kmalloc_reserve(size, gfp_mask, NUMA_NO_NODE, NULL); 1944 if (!data) 1945 goto nodata; 1946 size = SKB_WITH_OVERHEAD(size); 1947 1948 /* Copy only real data... and, alas, header. This should be 1949 * optimized for the cases when header is void. 1950 */ 1951 memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 1952 1953 memcpy((struct skb_shared_info *)(data + size), 1954 skb_shinfo(skb), 1955 offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 1956 1957 /* 1958 * if shinfo is shared we must drop the old head gracefully, but if it 1959 * is not we can just drop the old head and let the existing refcount 1960 * be since all we did is relocate the values 1961 */ 1962 if (skb_cloned(skb)) { 1963 if (skb_orphan_frags(skb, gfp_mask)) 1964 goto nofrags; 1965 if (skb_zcopy(skb)) 1966 refcount_inc(&skb_uarg(skb)->refcnt); 1967 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1968 skb_frag_ref(skb, i); 1969 1970 if (skb_has_frag_list(skb)) 1971 skb_clone_fraglist(skb); 1972 1973 skb_release_data(skb, SKB_CONSUMED); 1974 } else { 1975 skb_free_head(skb); 1976 } 1977 off = (data + nhead) - skb->head; 1978 1979 skb->head = data; 1980 skb->head_frag = 0; 1981 skb->data += off; 1982 1983 skb_set_end_offset(skb, size); 1984 #ifdef NET_SKBUFF_DATA_USES_OFFSET 1985 off = nhead; 1986 #endif 1987 skb->tail += off; 1988 skb_headers_offset_update(skb, nhead); 1989 skb->cloned = 0; 1990 skb->hdr_len = 0; 1991 skb->nohdr = 0; 1992 atomic_set(&skb_shinfo(skb)->dataref, 1); 1993 1994 skb_metadata_clear(skb); 1995 1996 /* It is not generally safe to change skb->truesize. 1997 * For the moment, we really care of rx path, or 1998 * when skb is orphaned (not attached to a socket). 1999 */ 2000 if (!skb->sk || skb->destructor == sock_edemux) 2001 skb->truesize += size - osize; 2002 2003 return 0; 2004 2005 nofrags: 2006 kfree(data); 2007 nodata: 2008 return -ENOMEM; 2009 } 2010 EXPORT_SYMBOL(pskb_expand_head); 2011 2012 /* Make private copy of skb with writable head and some headroom */ 2013 2014 struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 2015 { 2016 struct sk_buff *skb2; 2017 int delta = headroom - skb_headroom(skb); 2018 2019 if (delta <= 0) 2020 skb2 = pskb_copy(skb, GFP_ATOMIC); 2021 else { 2022 skb2 = skb_clone(skb, GFP_ATOMIC); 2023 if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 2024 GFP_ATOMIC)) { 2025 kfree_skb(skb2); 2026 skb2 = NULL; 2027 } 2028 } 2029 return skb2; 2030 } 2031 EXPORT_SYMBOL(skb_realloc_headroom); 2032 2033 int __skb_unclone_keeptruesize(struct sk_buff *skb, gfp_t pri) 2034 { 2035 unsigned int saved_end_offset, saved_truesize; 2036 struct skb_shared_info *shinfo; 2037 int res; 2038 2039 saved_end_offset = skb_end_offset(skb); 2040 saved_truesize = skb->truesize; 2041 2042 res = pskb_expand_head(skb, 0, 0, pri); 2043 if (res) 2044 return res; 2045 2046 skb->truesize = saved_truesize; 2047 2048 if (likely(skb_end_offset(skb) == saved_end_offset)) 2049 return 0; 2050 2051 shinfo = skb_shinfo(skb); 2052 2053 /* We are about to change back skb->end, 2054 * we need to move skb_shinfo() to its new location. 2055 */ 2056 memmove(skb->head + saved_end_offset, 2057 shinfo, 2058 offsetof(struct skb_shared_info, frags[shinfo->nr_frags])); 2059 2060 skb_set_end_offset(skb, saved_end_offset); 2061 2062 return 0; 2063 } 2064 2065 /** 2066 * skb_expand_head - reallocate header of &sk_buff 2067 * @skb: buffer to reallocate 2068 * @headroom: needed headroom 2069 * 2070 * Unlike skb_realloc_headroom, this one does not allocate a new skb 2071 * if possible; copies skb->sk to new skb as needed 2072 * and frees original skb in case of failures. 2073 * 2074 * It expect increased headroom and generates warning otherwise. 2075 */ 2076 2077 struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) 2078 { 2079 int delta = headroom - skb_headroom(skb); 2080 int osize = skb_end_offset(skb); 2081 struct sock *sk = skb->sk; 2082 2083 if (WARN_ONCE(delta <= 0, 2084 "%s is expecting an increase in the headroom", __func__)) 2085 return skb; 2086 2087 delta = SKB_DATA_ALIGN(delta); 2088 /* pskb_expand_head() might crash, if skb is shared. */ 2089 if (skb_shared(skb) || !is_skb_wmem(skb)) { 2090 struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); 2091 2092 if (unlikely(!nskb)) 2093 goto fail; 2094 2095 if (sk) 2096 skb_set_owner_w(nskb, sk); 2097 consume_skb(skb); 2098 skb = nskb; 2099 } 2100 if (pskb_expand_head(skb, delta, 0, GFP_ATOMIC)) 2101 goto fail; 2102 2103 if (sk && is_skb_wmem(skb)) { 2104 delta = skb_end_offset(skb) - osize; 2105 refcount_add(delta, &sk->sk_wmem_alloc); 2106 skb->truesize += delta; 2107 } 2108 return skb; 2109 2110 fail: 2111 kfree_skb(skb); 2112 return NULL; 2113 } 2114 EXPORT_SYMBOL(skb_expand_head); 2115 2116 /** 2117 * skb_copy_expand - copy and expand sk_buff 2118 * @skb: buffer to copy 2119 * @newheadroom: new free bytes at head 2120 * @newtailroom: new free bytes at tail 2121 * @gfp_mask: allocation priority 2122 * 2123 * Make a copy of both an &sk_buff and its data and while doing so 2124 * allocate additional space. 2125 * 2126 * This is used when the caller wishes to modify the data and needs a 2127 * private copy of the data to alter as well as more space for new fields. 2128 * Returns %NULL on failure or the pointer to the buffer 2129 * on success. The returned buffer has a reference count of 1. 2130 * 2131 * You must pass %GFP_ATOMIC as the allocation priority if this function 2132 * is called from an interrupt. 2133 */ 2134 struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 2135 int newheadroom, int newtailroom, 2136 gfp_t gfp_mask) 2137 { 2138 /* 2139 * Allocate the copy buffer 2140 */ 2141 struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 2142 gfp_mask, skb_alloc_rx_flag(skb), 2143 NUMA_NO_NODE); 2144 int oldheadroom = skb_headroom(skb); 2145 int head_copy_len, head_copy_off; 2146 2147 if (!n) 2148 return NULL; 2149 2150 skb_reserve(n, newheadroom); 2151 2152 /* Set the tail pointer and length */ 2153 skb_put(n, skb->len); 2154 2155 head_copy_len = oldheadroom; 2156 head_copy_off = 0; 2157 if (newheadroom <= head_copy_len) 2158 head_copy_len = newheadroom; 2159 else 2160 head_copy_off = newheadroom - head_copy_len; 2161 2162 /* Copy the linear header and data. */ 2163 BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 2164 skb->len + head_copy_len)); 2165 2166 skb_copy_header(n, skb); 2167 2168 skb_headers_offset_update(n, newheadroom - oldheadroom); 2169 2170 return n; 2171 } 2172 EXPORT_SYMBOL(skb_copy_expand); 2173 2174 /** 2175 * __skb_pad - zero pad the tail of an skb 2176 * @skb: buffer to pad 2177 * @pad: space to pad 2178 * @free_on_error: free buffer on error 2179 * 2180 * Ensure that a buffer is followed by a padding area that is zero 2181 * filled. Used by network drivers which may DMA or transfer data 2182 * beyond the buffer end onto the wire. 2183 * 2184 * May return error in out of memory cases. The skb is freed on error 2185 * if @free_on_error is true. 2186 */ 2187 2188 int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 2189 { 2190 int err; 2191 int ntail; 2192 2193 /* If the skbuff is non linear tailroom is always zero.. */ 2194 if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 2195 memset(skb->data+skb->len, 0, pad); 2196 return 0; 2197 } 2198 2199 ntail = skb->data_len + pad - (skb->end - skb->tail); 2200 if (likely(skb_cloned(skb) || ntail > 0)) { 2201 err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 2202 if (unlikely(err)) 2203 goto free_skb; 2204 } 2205 2206 /* FIXME: The use of this function with non-linear skb's really needs 2207 * to be audited. 2208 */ 2209 err = skb_linearize(skb); 2210 if (unlikely(err)) 2211 goto free_skb; 2212 2213 memset(skb->data + skb->len, 0, pad); 2214 return 0; 2215 2216 free_skb: 2217 if (free_on_error) 2218 kfree_skb(skb); 2219 return err; 2220 } 2221 EXPORT_SYMBOL(__skb_pad); 2222 2223 /** 2224 * pskb_put - add data to the tail of a potentially fragmented buffer 2225 * @skb: start of the buffer to use 2226 * @tail: tail fragment of the buffer to use 2227 * @len: amount of data to add 2228 * 2229 * This function extends the used data area of the potentially 2230 * fragmented buffer. @tail must be the last fragment of @skb -- or 2231 * @skb itself. If this would exceed the total buffer size the kernel 2232 * will panic. A pointer to the first byte of the extra data is 2233 * returned. 2234 */ 2235 2236 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 2237 { 2238 if (tail != skb) { 2239 skb->data_len += len; 2240 skb->len += len; 2241 } 2242 return skb_put(tail, len); 2243 } 2244 EXPORT_SYMBOL_GPL(pskb_put); 2245 2246 /** 2247 * skb_put - add data to a buffer 2248 * @skb: buffer to use 2249 * @len: amount of data to add 2250 * 2251 * This function extends the used data area of the buffer. If this would 2252 * exceed the total buffer size the kernel will panic. A pointer to the 2253 * first byte of the extra data is returned. 2254 */ 2255 void *skb_put(struct sk_buff *skb, unsigned int len) 2256 { 2257 void *tmp = skb_tail_pointer(skb); 2258 SKB_LINEAR_ASSERT(skb); 2259 skb->tail += len; 2260 skb->len += len; 2261 if (unlikely(skb->tail > skb->end)) 2262 skb_over_panic(skb, len, __builtin_return_address(0)); 2263 return tmp; 2264 } 2265 EXPORT_SYMBOL(skb_put); 2266 2267 /** 2268 * skb_push - add data to the start of a buffer 2269 * @skb: buffer to use 2270 * @len: amount of data to add 2271 * 2272 * This function extends the used data area of the buffer at the buffer 2273 * start. If this would exceed the total buffer headroom the kernel will 2274 * panic. A pointer to the first byte of the extra data is returned. 2275 */ 2276 void *skb_push(struct sk_buff *skb, unsigned int len) 2277 { 2278 skb->data -= len; 2279 skb->len += len; 2280 if (unlikely(skb->data < skb->head)) 2281 skb_under_panic(skb, len, __builtin_return_address(0)); 2282 return skb->data; 2283 } 2284 EXPORT_SYMBOL(skb_push); 2285 2286 /** 2287 * skb_pull - remove data from the start of a buffer 2288 * @skb: buffer to use 2289 * @len: amount of data to remove 2290 * 2291 * This function removes data from the start of a buffer, returning 2292 * the memory to the headroom. A pointer to the next data in the buffer 2293 * is returned. Once the data has been pulled future pushes will overwrite 2294 * the old data. 2295 */ 2296 void *skb_pull(struct sk_buff *skb, unsigned int len) 2297 { 2298 return skb_pull_inline(skb, len); 2299 } 2300 EXPORT_SYMBOL(skb_pull); 2301 2302 /** 2303 * skb_pull_data - remove data from the start of a buffer returning its 2304 * original position. 2305 * @skb: buffer to use 2306 * @len: amount of data to remove 2307 * 2308 * This function removes data from the start of a buffer, returning 2309 * the memory to the headroom. A pointer to the original data in the buffer 2310 * is returned after checking if there is enough data to pull. Once the 2311 * data has been pulled future pushes will overwrite the old data. 2312 */ 2313 void *skb_pull_data(struct sk_buff *skb, size_t len) 2314 { 2315 void *data = skb->data; 2316 2317 if (skb->len < len) 2318 return NULL; 2319 2320 skb_pull(skb, len); 2321 2322 return data; 2323 } 2324 EXPORT_SYMBOL(skb_pull_data); 2325 2326 /** 2327 * skb_trim - remove end from a buffer 2328 * @skb: buffer to alter 2329 * @len: new length 2330 * 2331 * Cut the length of a buffer down by removing data from the tail. If 2332 * the buffer is already under the length specified it is not modified. 2333 * The skb must be linear. 2334 */ 2335 void skb_trim(struct sk_buff *skb, unsigned int len) 2336 { 2337 if (skb->len > len) 2338 __skb_trim(skb, len); 2339 } 2340 EXPORT_SYMBOL(skb_trim); 2341 2342 /* Trims skb to length len. It can change skb pointers. 2343 */ 2344 2345 int ___pskb_trim(struct sk_buff *skb, unsigned int len) 2346 { 2347 struct sk_buff **fragp; 2348 struct sk_buff *frag; 2349 int offset = skb_headlen(skb); 2350 int nfrags = skb_shinfo(skb)->nr_frags; 2351 int i; 2352 int err; 2353 2354 if (skb_cloned(skb) && 2355 unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 2356 return err; 2357 2358 i = 0; 2359 if (offset >= len) 2360 goto drop_pages; 2361 2362 for (; i < nfrags; i++) { 2363 int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 2364 2365 if (end < len) { 2366 offset = end; 2367 continue; 2368 } 2369 2370 skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 2371 2372 drop_pages: 2373 skb_shinfo(skb)->nr_frags = i; 2374 2375 for (; i < nfrags; i++) 2376 skb_frag_unref(skb, i); 2377 2378 if (skb_has_frag_list(skb)) 2379 skb_drop_fraglist(skb); 2380 goto done; 2381 } 2382 2383 for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 2384 fragp = &frag->next) { 2385 int end = offset + frag->len; 2386 2387 if (skb_shared(frag)) { 2388 struct sk_buff *nfrag; 2389 2390 nfrag = skb_clone(frag, GFP_ATOMIC); 2391 if (unlikely(!nfrag)) 2392 return -ENOMEM; 2393 2394 nfrag->next = frag->next; 2395 consume_skb(frag); 2396 frag = nfrag; 2397 *fragp = frag; 2398 } 2399 2400 if (end < len) { 2401 offset = end; 2402 continue; 2403 } 2404 2405 if (end > len && 2406 unlikely((err = pskb_trim(frag, len - offset)))) 2407 return err; 2408 2409 if (frag->next) 2410 skb_drop_list(&frag->next); 2411 break; 2412 } 2413 2414 done: 2415 if (len > skb_headlen(skb)) { 2416 skb->data_len -= skb->len - len; 2417 skb->len = len; 2418 } else { 2419 skb->len = len; 2420 skb->data_len = 0; 2421 skb_set_tail_pointer(skb, len); 2422 } 2423 2424 if (!skb->sk || skb->destructor == sock_edemux) 2425 skb_condense(skb); 2426 return 0; 2427 } 2428 EXPORT_SYMBOL(___pskb_trim); 2429 2430 /* Note : use pskb_trim_rcsum() instead of calling this directly 2431 */ 2432 int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 2433 { 2434 if (skb->ip_summed == CHECKSUM_COMPLETE) { 2435 int delta = skb->len - len; 2436 2437 skb->csum = csum_block_sub(skb->csum, 2438 skb_checksum(skb, len, delta, 0), 2439 len); 2440 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 2441 int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 2442 int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 2443 2444 if (offset + sizeof(__sum16) > hdlen) 2445 return -EINVAL; 2446 } 2447 return __pskb_trim(skb, len); 2448 } 2449 EXPORT_SYMBOL(pskb_trim_rcsum_slow); 2450 2451 /** 2452 * __pskb_pull_tail - advance tail of skb header 2453 * @skb: buffer to reallocate 2454 * @delta: number of bytes to advance tail 2455 * 2456 * The function makes a sense only on a fragmented &sk_buff, 2457 * it expands header moving its tail forward and copying necessary 2458 * data from fragmented part. 2459 * 2460 * &sk_buff MUST have reference count of 1. 2461 * 2462 * Returns %NULL (and &sk_buff does not change) if pull failed 2463 * or value of new tail of skb in the case of success. 2464 * 2465 * All the pointers pointing into skb header may change and must be 2466 * reloaded after call to this function. 2467 */ 2468 2469 /* Moves tail of skb head forward, copying data from fragmented part, 2470 * when it is necessary. 2471 * 1. It may fail due to malloc failure. 2472 * 2. It may change skb pointers. 2473 * 2474 * It is pretty complicated. Luckily, it is called only in exceptional cases. 2475 */ 2476 void *__pskb_pull_tail(struct sk_buff *skb, int delta) 2477 { 2478 /* If skb has not enough free space at tail, get new one 2479 * plus 128 bytes for future expansions. If we have enough 2480 * room at tail, reallocate without expansion only if skb is cloned. 2481 */ 2482 int i, k, eat = (skb->tail + delta) - skb->end; 2483 2484 if (eat > 0 || skb_cloned(skb)) { 2485 if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 2486 GFP_ATOMIC)) 2487 return NULL; 2488 } 2489 2490 BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 2491 skb_tail_pointer(skb), delta)); 2492 2493 /* Optimization: no fragments, no reasons to preestimate 2494 * size of pulled pages. Superb. 2495 */ 2496 if (!skb_has_frag_list(skb)) 2497 goto pull_pages; 2498 2499 /* Estimate size of pulled pages. */ 2500 eat = delta; 2501 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2502 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 2503 2504 if (size >= eat) 2505 goto pull_pages; 2506 eat -= size; 2507 } 2508 2509 /* If we need update frag list, we are in troubles. 2510 * Certainly, it is possible to add an offset to skb data, 2511 * but taking into account that pulling is expected to 2512 * be very rare operation, it is worth to fight against 2513 * further bloating skb head and crucify ourselves here instead. 2514 * Pure masohism, indeed. 8)8) 2515 */ 2516 if (eat) { 2517 struct sk_buff *list = skb_shinfo(skb)->frag_list; 2518 struct sk_buff *clone = NULL; 2519 struct sk_buff *insp = NULL; 2520 2521 do { 2522 if (list->len <= eat) { 2523 /* Eaten as whole. */ 2524 eat -= list->len; 2525 list = list->next; 2526 insp = list; 2527 } else { 2528 /* Eaten partially. */ 2529 if (skb_is_gso(skb) && !list->head_frag && 2530 skb_headlen(list)) 2531 skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY; 2532 2533 if (skb_shared(list)) { 2534 /* Sucks! We need to fork list. :-( */ 2535 clone = skb_clone(list, GFP_ATOMIC); 2536 if (!clone) 2537 return NULL; 2538 insp = list->next; 2539 list = clone; 2540 } else { 2541 /* This may be pulled without 2542 * problems. */ 2543 insp = list; 2544 } 2545 if (!pskb_pull(list, eat)) { 2546 kfree_skb(clone); 2547 return NULL; 2548 } 2549 break; 2550 } 2551 } while (eat); 2552 2553 /* Free pulled out fragments. */ 2554 while ((list = skb_shinfo(skb)->frag_list) != insp) { 2555 skb_shinfo(skb)->frag_list = list->next; 2556 consume_skb(list); 2557 } 2558 /* And insert new clone at head. */ 2559 if (clone) { 2560 clone->next = list; 2561 skb_shinfo(skb)->frag_list = clone; 2562 } 2563 } 2564 /* Success! Now we may commit changes to skb data. */ 2565 2566 pull_pages: 2567 eat = delta; 2568 k = 0; 2569 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2570 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 2571 2572 if (size <= eat) { 2573 skb_frag_unref(skb, i); 2574 eat -= size; 2575 } else { 2576 skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2577 2578 *frag = skb_shinfo(skb)->frags[i]; 2579 if (eat) { 2580 skb_frag_off_add(frag, eat); 2581 skb_frag_size_sub(frag, eat); 2582 if (!i) 2583 goto end; 2584 eat = 0; 2585 } 2586 k++; 2587 } 2588 } 2589 skb_shinfo(skb)->nr_frags = k; 2590 2591 end: 2592 skb->tail += delta; 2593 skb->data_len -= delta; 2594 2595 if (!skb->data_len) 2596 skb_zcopy_clear(skb, false); 2597 2598 return skb_tail_pointer(skb); 2599 } 2600 EXPORT_SYMBOL(__pskb_pull_tail); 2601 2602 /** 2603 * skb_copy_bits - copy bits from skb to kernel buffer 2604 * @skb: source skb 2605 * @offset: offset in source 2606 * @to: destination buffer 2607 * @len: number of bytes to copy 2608 * 2609 * Copy the specified number of bytes from the source skb to the 2610 * destination buffer. 2611 * 2612 * CAUTION ! : 2613 * If its prototype is ever changed, 2614 * check arch/{*}/net/{*}.S files, 2615 * since it is called from BPF assembly code. 2616 */ 2617 int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 2618 { 2619 int start = skb_headlen(skb); 2620 struct sk_buff *frag_iter; 2621 int i, copy; 2622 2623 if (offset > (int)skb->len - len) 2624 goto fault; 2625 2626 /* Copy header. */ 2627 if ((copy = start - offset) > 0) { 2628 if (copy > len) 2629 copy = len; 2630 skb_copy_from_linear_data_offset(skb, offset, to, copy); 2631 if ((len -= copy) == 0) 2632 return 0; 2633 offset += copy; 2634 to += copy; 2635 } 2636 2637 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2638 int end; 2639 skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 2640 2641 WARN_ON(start > offset + len); 2642 2643 end = start + skb_frag_size(f); 2644 if ((copy = end - offset) > 0) { 2645 u32 p_off, p_len, copied; 2646 struct page *p; 2647 u8 *vaddr; 2648 2649 if (copy > len) 2650 copy = len; 2651 2652 skb_frag_foreach_page(f, 2653 skb_frag_off(f) + offset - start, 2654 copy, p, p_off, p_len, copied) { 2655 vaddr = kmap_atomic(p); 2656 memcpy(to + copied, vaddr + p_off, p_len); 2657 kunmap_atomic(vaddr); 2658 } 2659 2660 if ((len -= copy) == 0) 2661 return 0; 2662 offset += copy; 2663 to += copy; 2664 } 2665 start = end; 2666 } 2667 2668 skb_walk_frags(skb, frag_iter) { 2669 int end; 2670 2671 WARN_ON(start > offset + len); 2672 2673 end = start + frag_iter->len; 2674 if ((copy = end - offset) > 0) { 2675 if (copy > len) 2676 copy = len; 2677 if (skb_copy_bits(frag_iter, offset - start, to, copy)) 2678 goto fault; 2679 if ((len -= copy) == 0) 2680 return 0; 2681 offset += copy; 2682 to += copy; 2683 } 2684 start = end; 2685 } 2686 2687 if (!len) 2688 return 0; 2689 2690 fault: 2691 return -EFAULT; 2692 } 2693 EXPORT_SYMBOL(skb_copy_bits); 2694 2695 /* 2696 * Callback from splice_to_pipe(), if we need to release some pages 2697 * at the end of the spd in case we error'ed out in filling the pipe. 2698 */ 2699 static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 2700 { 2701 put_page(spd->pages[i]); 2702 } 2703 2704 static struct page *linear_to_page(struct page *page, unsigned int *len, 2705 unsigned int *offset, 2706 struct sock *sk) 2707 { 2708 struct page_frag *pfrag = sk_page_frag(sk); 2709 2710 if (!sk_page_frag_refill(sk, pfrag)) 2711 return NULL; 2712 2713 *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 2714 2715 memcpy(page_address(pfrag->page) + pfrag->offset, 2716 page_address(page) + *offset, *len); 2717 *offset = pfrag->offset; 2718 pfrag->offset += *len; 2719 2720 return pfrag->page; 2721 } 2722 2723 static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 2724 struct page *page, 2725 unsigned int offset) 2726 { 2727 return spd->nr_pages && 2728 spd->pages[spd->nr_pages - 1] == page && 2729 (spd->partial[spd->nr_pages - 1].offset + 2730 spd->partial[spd->nr_pages - 1].len == offset); 2731 } 2732 2733 /* 2734 * Fill page/offset/length into spd, if it can hold more pages. 2735 */ 2736 static bool spd_fill_page(struct splice_pipe_desc *spd, 2737 struct pipe_inode_info *pipe, struct page *page, 2738 unsigned int *len, unsigned int offset, 2739 bool linear, 2740 struct sock *sk) 2741 { 2742 if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2743 return true; 2744 2745 if (linear) { 2746 page = linear_to_page(page, len, &offset, sk); 2747 if (!page) 2748 return true; 2749 } 2750 if (spd_can_coalesce(spd, page, offset)) { 2751 spd->partial[spd->nr_pages - 1].len += *len; 2752 return false; 2753 } 2754 get_page(page); 2755 spd->pages[spd->nr_pages] = page; 2756 spd->partial[spd->nr_pages].len = *len; 2757 spd->partial[spd->nr_pages].offset = offset; 2758 spd->nr_pages++; 2759 2760 return false; 2761 } 2762 2763 static bool __splice_segment(struct page *page, unsigned int poff, 2764 unsigned int plen, unsigned int *off, 2765 unsigned int *len, 2766 struct splice_pipe_desc *spd, bool linear, 2767 struct sock *sk, 2768 struct pipe_inode_info *pipe) 2769 { 2770 if (!*len) 2771 return true; 2772 2773 /* skip this segment if already processed */ 2774 if (*off >= plen) { 2775 *off -= plen; 2776 return false; 2777 } 2778 2779 /* ignore any bits we already processed */ 2780 poff += *off; 2781 plen -= *off; 2782 *off = 0; 2783 2784 do { 2785 unsigned int flen = min(*len, plen); 2786 2787 if (spd_fill_page(spd, pipe, page, &flen, poff, 2788 linear, sk)) 2789 return true; 2790 poff += flen; 2791 plen -= flen; 2792 *len -= flen; 2793 } while (*len && plen); 2794 2795 return false; 2796 } 2797 2798 /* 2799 * Map linear and fragment data from the skb to spd. It reports true if the 2800 * pipe is full or if we already spliced the requested length. 2801 */ 2802 static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 2803 unsigned int *offset, unsigned int *len, 2804 struct splice_pipe_desc *spd, struct sock *sk) 2805 { 2806 int seg; 2807 struct sk_buff *iter; 2808 2809 /* map the linear part : 2810 * If skb->head_frag is set, this 'linear' part is backed by a 2811 * fragment, and if the head is not shared with any clones then 2812 * we can avoid a copy since we own the head portion of this page. 2813 */ 2814 if (__splice_segment(virt_to_page(skb->data), 2815 (unsigned long) skb->data & (PAGE_SIZE - 1), 2816 skb_headlen(skb), 2817 offset, len, spd, 2818 skb_head_is_locked(skb), 2819 sk, pipe)) 2820 return true; 2821 2822 /* 2823 * then map the fragments 2824 */ 2825 for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 2826 const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 2827 2828 if (__splice_segment(skb_frag_page(f), 2829 skb_frag_off(f), skb_frag_size(f), 2830 offset, len, spd, false, sk, pipe)) 2831 return true; 2832 } 2833 2834 skb_walk_frags(skb, iter) { 2835 if (*offset >= iter->len) { 2836 *offset -= iter->len; 2837 continue; 2838 } 2839 /* __skb_splice_bits() only fails if the output has no room 2840 * left, so no point in going over the frag_list for the error 2841 * case. 2842 */ 2843 if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2844 return true; 2845 } 2846 2847 return false; 2848 } 2849 2850 /* 2851 * Map data from the skb to a pipe. Should handle both the linear part, 2852 * the fragments, and the frag list. 2853 */ 2854 int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 2855 struct pipe_inode_info *pipe, unsigned int tlen, 2856 unsigned int flags) 2857 { 2858 struct partial_page partial[MAX_SKB_FRAGS]; 2859 struct page *pages[MAX_SKB_FRAGS]; 2860 struct splice_pipe_desc spd = { 2861 .pages = pages, 2862 .partial = partial, 2863 .nr_pages_max = MAX_SKB_FRAGS, 2864 .ops = &nosteal_pipe_buf_ops, 2865 .spd_release = sock_spd_release, 2866 }; 2867 int ret = 0; 2868 2869 __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 2870 2871 if (spd.nr_pages) 2872 ret = splice_to_pipe(pipe, &spd); 2873 2874 return ret; 2875 } 2876 EXPORT_SYMBOL_GPL(skb_splice_bits); 2877 2878 static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 2879 struct kvec *vec, size_t num, size_t size) 2880 { 2881 struct socket *sock = sk->sk_socket; 2882 2883 if (!sock) 2884 return -EINVAL; 2885 return kernel_sendmsg(sock, msg, vec, num, size); 2886 } 2887 2888 static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 2889 size_t size, int flags) 2890 { 2891 struct socket *sock = sk->sk_socket; 2892 2893 if (!sock) 2894 return -EINVAL; 2895 return kernel_sendpage(sock, page, offset, size, flags); 2896 } 2897 2898 typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 2899 struct kvec *vec, size_t num, size_t size); 2900 typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 2901 size_t size, int flags); 2902 static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 2903 int len, sendmsg_func sendmsg, sendpage_func sendpage) 2904 { 2905 unsigned int orig_len = len; 2906 struct sk_buff *head = skb; 2907 unsigned short fragidx; 2908 int slen, ret; 2909 2910 do_frag_list: 2911 2912 /* Deal with head data */ 2913 while (offset < skb_headlen(skb) && len) { 2914 struct kvec kv; 2915 struct msghdr msg; 2916 2917 slen = min_t(int, len, skb_headlen(skb) - offset); 2918 kv.iov_base = skb->data + offset; 2919 kv.iov_len = slen; 2920 memset(&msg, 0, sizeof(msg)); 2921 msg.msg_flags = MSG_DONTWAIT; 2922 2923 ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 2924 sendmsg_unlocked, sk, &msg, &kv, 1, slen); 2925 if (ret <= 0) 2926 goto error; 2927 2928 offset += ret; 2929 len -= ret; 2930 } 2931 2932 /* All the data was skb head? */ 2933 if (!len) 2934 goto out; 2935 2936 /* Make offset relative to start of frags */ 2937 offset -= skb_headlen(skb); 2938 2939 /* Find where we are in frag list */ 2940 for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 2941 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 2942 2943 if (offset < skb_frag_size(frag)) 2944 break; 2945 2946 offset -= skb_frag_size(frag); 2947 } 2948 2949 for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 2950 skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 2951 2952 slen = min_t(size_t, len, skb_frag_size(frag) - offset); 2953 2954 while (slen) { 2955 ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 2956 sendpage_unlocked, sk, 2957 skb_frag_page(frag), 2958 skb_frag_off(frag) + offset, 2959 slen, MSG_DONTWAIT); 2960 if (ret <= 0) 2961 goto error; 2962 2963 len -= ret; 2964 offset += ret; 2965 slen -= ret; 2966 } 2967 2968 offset = 0; 2969 } 2970 2971 if (len) { 2972 /* Process any frag lists */ 2973 2974 if (skb == head) { 2975 if (skb_has_frag_list(skb)) { 2976 skb = skb_shinfo(skb)->frag_list; 2977 goto do_frag_list; 2978 } 2979 } else if (skb->next) { 2980 skb = skb->next; 2981 goto do_frag_list; 2982 } 2983 } 2984 2985 out: 2986 return orig_len - len; 2987 2988 error: 2989 return orig_len == len ? ret : orig_len - len; 2990 } 2991 2992 /* Send skb data on a socket. Socket must be locked. */ 2993 int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 2994 int len) 2995 { 2996 return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 2997 kernel_sendpage_locked); 2998 } 2999 EXPORT_SYMBOL_GPL(skb_send_sock_locked); 3000 3001 /* Send skb data on a socket. Socket must be unlocked. */ 3002 int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 3003 { 3004 return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 3005 sendpage_unlocked); 3006 } 3007 3008 /** 3009 * skb_store_bits - store bits from kernel buffer to skb 3010 * @skb: destination buffer 3011 * @offset: offset in destination 3012 * @from: source buffer 3013 * @len: number of bytes to copy 3014 * 3015 * Copy the specified number of bytes from the source buffer to the 3016 * destination skb. This function handles all the messy bits of 3017 * traversing fragment lists and such. 3018 */ 3019 3020 int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 3021 { 3022 int start = skb_headlen(skb); 3023 struct sk_buff *frag_iter; 3024 int i, copy; 3025 3026 if (offset > (int)skb->len - len) 3027 goto fault; 3028 3029 if ((copy = start - offset) > 0) { 3030 if (copy > len) 3031 copy = len; 3032 skb_copy_to_linear_data_offset(skb, offset, from, copy); 3033 if ((len -= copy) == 0) 3034 return 0; 3035 offset += copy; 3036 from += copy; 3037 } 3038 3039 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3040 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3041 int end; 3042 3043 WARN_ON(start > offset + len); 3044 3045 end = start + skb_frag_size(frag); 3046 if ((copy = end - offset) > 0) { 3047 u32 p_off, p_len, copied; 3048 struct page *p; 3049 u8 *vaddr; 3050 3051 if (copy > len) 3052 copy = len; 3053 3054 skb_frag_foreach_page(frag, 3055 skb_frag_off(frag) + offset - start, 3056 copy, p, p_off, p_len, copied) { 3057 vaddr = kmap_atomic(p); 3058 memcpy(vaddr + p_off, from + copied, p_len); 3059 kunmap_atomic(vaddr); 3060 } 3061 3062 if ((len -= copy) == 0) 3063 return 0; 3064 offset += copy; 3065 from += copy; 3066 } 3067 start = end; 3068 } 3069 3070 skb_walk_frags(skb, frag_iter) { 3071 int end; 3072 3073 WARN_ON(start > offset + len); 3074 3075 end = start + frag_iter->len; 3076 if ((copy = end - offset) > 0) { 3077 if (copy > len) 3078 copy = len; 3079 if (skb_store_bits(frag_iter, offset - start, 3080 from, copy)) 3081 goto fault; 3082 if ((len -= copy) == 0) 3083 return 0; 3084 offset += copy; 3085 from += copy; 3086 } 3087 start = end; 3088 } 3089 if (!len) 3090 return 0; 3091 3092 fault: 3093 return -EFAULT; 3094 } 3095 EXPORT_SYMBOL(skb_store_bits); 3096 3097 /* Checksum skb data. */ 3098 __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 3099 __wsum csum, const struct skb_checksum_ops *ops) 3100 { 3101 int start = skb_headlen(skb); 3102 int i, copy = start - offset; 3103 struct sk_buff *frag_iter; 3104 int pos = 0; 3105 3106 /* Checksum header. */ 3107 if (copy > 0) { 3108 if (copy > len) 3109 copy = len; 3110 csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 3111 skb->data + offset, copy, csum); 3112 if ((len -= copy) == 0) 3113 return csum; 3114 offset += copy; 3115 pos = copy; 3116 } 3117 3118 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3119 int end; 3120 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3121 3122 WARN_ON(start > offset + len); 3123 3124 end = start + skb_frag_size(frag); 3125 if ((copy = end - offset) > 0) { 3126 u32 p_off, p_len, copied; 3127 struct page *p; 3128 __wsum csum2; 3129 u8 *vaddr; 3130 3131 if (copy > len) 3132 copy = len; 3133 3134 skb_frag_foreach_page(frag, 3135 skb_frag_off(frag) + offset - start, 3136 copy, p, p_off, p_len, copied) { 3137 vaddr = kmap_atomic(p); 3138 csum2 = INDIRECT_CALL_1(ops->update, 3139 csum_partial_ext, 3140 vaddr + p_off, p_len, 0); 3141 kunmap_atomic(vaddr); 3142 csum = INDIRECT_CALL_1(ops->combine, 3143 csum_block_add_ext, csum, 3144 csum2, pos, p_len); 3145 pos += p_len; 3146 } 3147 3148 if (!(len -= copy)) 3149 return csum; 3150 offset += copy; 3151 } 3152 start = end; 3153 } 3154 3155 skb_walk_frags(skb, frag_iter) { 3156 int end; 3157 3158 WARN_ON(start > offset + len); 3159 3160 end = start + frag_iter->len; 3161 if ((copy = end - offset) > 0) { 3162 __wsum csum2; 3163 if (copy > len) 3164 copy = len; 3165 csum2 = __skb_checksum(frag_iter, offset - start, 3166 copy, 0, ops); 3167 csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 3168 csum, csum2, pos, copy); 3169 if ((len -= copy) == 0) 3170 return csum; 3171 offset += copy; 3172 pos += copy; 3173 } 3174 start = end; 3175 } 3176 BUG_ON(len); 3177 3178 return csum; 3179 } 3180 EXPORT_SYMBOL(__skb_checksum); 3181 3182 __wsum skb_checksum(const struct sk_buff *skb, int offset, 3183 int len, __wsum csum) 3184 { 3185 const struct skb_checksum_ops ops = { 3186 .update = csum_partial_ext, 3187 .combine = csum_block_add_ext, 3188 }; 3189 3190 return __skb_checksum(skb, offset, len, csum, &ops); 3191 } 3192 EXPORT_SYMBOL(skb_checksum); 3193 3194 /* Both of above in one bottle. */ 3195 3196 __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 3197 u8 *to, int len) 3198 { 3199 int start = skb_headlen(skb); 3200 int i, copy = start - offset; 3201 struct sk_buff *frag_iter; 3202 int pos = 0; 3203 __wsum csum = 0; 3204 3205 /* Copy header. */ 3206 if (copy > 0) { 3207 if (copy > len) 3208 copy = len; 3209 csum = csum_partial_copy_nocheck(skb->data + offset, to, 3210 copy); 3211 if ((len -= copy) == 0) 3212 return csum; 3213 offset += copy; 3214 to += copy; 3215 pos = copy; 3216 } 3217 3218 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 3219 int end; 3220 3221 WARN_ON(start > offset + len); 3222 3223 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3224 if ((copy = end - offset) > 0) { 3225 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3226 u32 p_off, p_len, copied; 3227 struct page *p; 3228 __wsum csum2; 3229 u8 *vaddr; 3230 3231 if (copy > len) 3232 copy = len; 3233 3234 skb_frag_foreach_page(frag, 3235 skb_frag_off(frag) + offset - start, 3236 copy, p, p_off, p_len, copied) { 3237 vaddr = kmap_atomic(p); 3238 csum2 = csum_partial_copy_nocheck(vaddr + p_off, 3239 to + copied, 3240 p_len); 3241 kunmap_atomic(vaddr); 3242 csum = csum_block_add(csum, csum2, pos); 3243 pos += p_len; 3244 } 3245 3246 if (!(len -= copy)) 3247 return csum; 3248 offset += copy; 3249 to += copy; 3250 } 3251 start = end; 3252 } 3253 3254 skb_walk_frags(skb, frag_iter) { 3255 __wsum csum2; 3256 int end; 3257 3258 WARN_ON(start > offset + len); 3259 3260 end = start + frag_iter->len; 3261 if ((copy = end - offset) > 0) { 3262 if (copy > len) 3263 copy = len; 3264 csum2 = skb_copy_and_csum_bits(frag_iter, 3265 offset - start, 3266 to, copy); 3267 csum = csum_block_add(csum, csum2, pos); 3268 if ((len -= copy) == 0) 3269 return csum; 3270 offset += copy; 3271 to += copy; 3272 pos += copy; 3273 } 3274 start = end; 3275 } 3276 BUG_ON(len); 3277 return csum; 3278 } 3279 EXPORT_SYMBOL(skb_copy_and_csum_bits); 3280 3281 __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 3282 { 3283 __sum16 sum; 3284 3285 sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 3286 /* See comments in __skb_checksum_complete(). */ 3287 if (likely(!sum)) { 3288 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 3289 !skb->csum_complete_sw) 3290 netdev_rx_csum_fault(skb->dev, skb); 3291 } 3292 if (!skb_shared(skb)) 3293 skb->csum_valid = !sum; 3294 return sum; 3295 } 3296 EXPORT_SYMBOL(__skb_checksum_complete_head); 3297 3298 /* This function assumes skb->csum already holds pseudo header's checksum, 3299 * which has been changed from the hardware checksum, for example, by 3300 * __skb_checksum_validate_complete(). And, the original skb->csum must 3301 * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 3302 * 3303 * It returns non-zero if the recomputed checksum is still invalid, otherwise 3304 * zero. The new checksum is stored back into skb->csum unless the skb is 3305 * shared. 3306 */ 3307 __sum16 __skb_checksum_complete(struct sk_buff *skb) 3308 { 3309 __wsum csum; 3310 __sum16 sum; 3311 3312 csum = skb_checksum(skb, 0, skb->len, 0); 3313 3314 sum = csum_fold(csum_add(skb->csum, csum)); 3315 /* This check is inverted, because we already knew the hardware 3316 * checksum is invalid before calling this function. So, if the 3317 * re-computed checksum is valid instead, then we have a mismatch 3318 * between the original skb->csum and skb_checksum(). This means either 3319 * the original hardware checksum is incorrect or we screw up skb->csum 3320 * when moving skb->data around. 3321 */ 3322 if (likely(!sum)) { 3323 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 3324 !skb->csum_complete_sw) 3325 netdev_rx_csum_fault(skb->dev, skb); 3326 } 3327 3328 if (!skb_shared(skb)) { 3329 /* Save full packet checksum */ 3330 skb->csum = csum; 3331 skb->ip_summed = CHECKSUM_COMPLETE; 3332 skb->csum_complete_sw = 1; 3333 skb->csum_valid = !sum; 3334 } 3335 3336 return sum; 3337 } 3338 EXPORT_SYMBOL(__skb_checksum_complete); 3339 3340 static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 3341 { 3342 net_warn_ratelimited( 3343 "%s: attempt to compute crc32c without libcrc32c.ko\n", 3344 __func__); 3345 return 0; 3346 } 3347 3348 static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 3349 int offset, int len) 3350 { 3351 net_warn_ratelimited( 3352 "%s: attempt to compute crc32c without libcrc32c.ko\n", 3353 __func__); 3354 return 0; 3355 } 3356 3357 static const struct skb_checksum_ops default_crc32c_ops = { 3358 .update = warn_crc32c_csum_update, 3359 .combine = warn_crc32c_csum_combine, 3360 }; 3361 3362 const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 3363 &default_crc32c_ops; 3364 EXPORT_SYMBOL(crc32c_csum_stub); 3365 3366 /** 3367 * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3368 * @from: source buffer 3369 * 3370 * Calculates the amount of linear headroom needed in the 'to' skb passed 3371 * into skb_zerocopy(). 3372 */ 3373 unsigned int 3374 skb_zerocopy_headlen(const struct sk_buff *from) 3375 { 3376 unsigned int hlen = 0; 3377 3378 if (!from->head_frag || 3379 skb_headlen(from) < L1_CACHE_BYTES || 3380 skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3381 hlen = skb_headlen(from); 3382 if (!hlen) 3383 hlen = from->len; 3384 } 3385 3386 if (skb_has_frag_list(from)) 3387 hlen = from->len; 3388 3389 return hlen; 3390 } 3391 EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3392 3393 /** 3394 * skb_zerocopy - Zero copy skb to skb 3395 * @to: destination buffer 3396 * @from: source buffer 3397 * @len: number of bytes to copy from source buffer 3398 * @hlen: size of linear headroom in destination buffer 3399 * 3400 * Copies up to `len` bytes from `from` to `to` by creating references 3401 * to the frags in the source buffer. 3402 * 3403 * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3404 * headroom in the `to` buffer. 3405 * 3406 * Return value: 3407 * 0: everything is OK 3408 * -ENOMEM: couldn't orphan frags of @from due to lack of memory 3409 * -EFAULT: skb_copy_bits() found some problem with skb geometry 3410 */ 3411 int 3412 skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3413 { 3414 int i, j = 0; 3415 int plen = 0; /* length of skb->head fragment */ 3416 int ret; 3417 struct page *page; 3418 unsigned int offset; 3419 3420 BUG_ON(!from->head_frag && !hlen); 3421 3422 /* dont bother with small payloads */ 3423 if (len <= skb_tailroom(to)) 3424 return skb_copy_bits(from, 0, skb_put(to, len), len); 3425 3426 if (hlen) { 3427 ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 3428 if (unlikely(ret)) 3429 return ret; 3430 len -= hlen; 3431 } else { 3432 plen = min_t(int, skb_headlen(from), len); 3433 if (plen) { 3434 page = virt_to_head_page(from->head); 3435 offset = from->data - (unsigned char *)page_address(page); 3436 __skb_fill_page_desc(to, 0, page, offset, plen); 3437 get_page(page); 3438 j = 1; 3439 len -= plen; 3440 } 3441 } 3442 3443 skb_len_add(to, len + plen); 3444 3445 if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 3446 skb_tx_error(from); 3447 return -ENOMEM; 3448 } 3449 skb_zerocopy_clone(to, from, GFP_ATOMIC); 3450 3451 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3452 int size; 3453 3454 if (!len) 3455 break; 3456 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3457 size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3458 len); 3459 skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3460 len -= size; 3461 skb_frag_ref(to, j); 3462 j++; 3463 } 3464 skb_shinfo(to)->nr_frags = j; 3465 3466 return 0; 3467 } 3468 EXPORT_SYMBOL_GPL(skb_zerocopy); 3469 3470 void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 3471 { 3472 __wsum csum; 3473 long csstart; 3474 3475 if (skb->ip_summed == CHECKSUM_PARTIAL) 3476 csstart = skb_checksum_start_offset(skb); 3477 else 3478 csstart = skb_headlen(skb); 3479 3480 BUG_ON(csstart > skb_headlen(skb)); 3481 3482 skb_copy_from_linear_data(skb, to, csstart); 3483 3484 csum = 0; 3485 if (csstart != skb->len) 3486 csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 3487 skb->len - csstart); 3488 3489 if (skb->ip_summed == CHECKSUM_PARTIAL) { 3490 long csstuff = csstart + skb->csum_offset; 3491 3492 *((__sum16 *)(to + csstuff)) = csum_fold(csum); 3493 } 3494 } 3495 EXPORT_SYMBOL(skb_copy_and_csum_dev); 3496 3497 /** 3498 * skb_dequeue - remove from the head of the queue 3499 * @list: list to dequeue from 3500 * 3501 * Remove the head of the list. The list lock is taken so the function 3502 * may be used safely with other locking list functions. The head item is 3503 * returned or %NULL if the list is empty. 3504 */ 3505 3506 struct sk_buff *skb_dequeue(struct sk_buff_head *list) 3507 { 3508 unsigned long flags; 3509 struct sk_buff *result; 3510 3511 spin_lock_irqsave(&list->lock, flags); 3512 result = __skb_dequeue(list); 3513 spin_unlock_irqrestore(&list->lock, flags); 3514 return result; 3515 } 3516 EXPORT_SYMBOL(skb_dequeue); 3517 3518 /** 3519 * skb_dequeue_tail - remove from the tail of the queue 3520 * @list: list to dequeue from 3521 * 3522 * Remove the tail of the list. The list lock is taken so the function 3523 * may be used safely with other locking list functions. The tail item is 3524 * returned or %NULL if the list is empty. 3525 */ 3526 struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 3527 { 3528 unsigned long flags; 3529 struct sk_buff *result; 3530 3531 spin_lock_irqsave(&list->lock, flags); 3532 result = __skb_dequeue_tail(list); 3533 spin_unlock_irqrestore(&list->lock, flags); 3534 return result; 3535 } 3536 EXPORT_SYMBOL(skb_dequeue_tail); 3537 3538 /** 3539 * skb_queue_purge - empty a list 3540 * @list: list to empty 3541 * 3542 * Delete all buffers on an &sk_buff list. Each buffer is removed from 3543 * the list and one reference dropped. This function takes the list 3544 * lock and is atomic with respect to other list locking functions. 3545 */ 3546 void skb_queue_purge(struct sk_buff_head *list) 3547 { 3548 struct sk_buff *skb; 3549 while ((skb = skb_dequeue(list)) != NULL) 3550 kfree_skb(skb); 3551 } 3552 EXPORT_SYMBOL(skb_queue_purge); 3553 3554 /** 3555 * skb_rbtree_purge - empty a skb rbtree 3556 * @root: root of the rbtree to empty 3557 * Return value: the sum of truesizes of all purged skbs. 3558 * 3559 * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 3560 * the list and one reference dropped. This function does not take 3561 * any lock. Synchronization should be handled by the caller (e.g., TCP 3562 * out-of-order queue is protected by the socket lock). 3563 */ 3564 unsigned int skb_rbtree_purge(struct rb_root *root) 3565 { 3566 struct rb_node *p = rb_first(root); 3567 unsigned int sum = 0; 3568 3569 while (p) { 3570 struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 3571 3572 p = rb_next(p); 3573 rb_erase(&skb->rbnode, root); 3574 sum += skb->truesize; 3575 kfree_skb(skb); 3576 } 3577 return sum; 3578 } 3579 3580 /** 3581 * skb_queue_head - queue a buffer at the list head 3582 * @list: list to use 3583 * @newsk: buffer to queue 3584 * 3585 * Queue a buffer at the start of the list. This function takes the 3586 * list lock and can be used safely with other locking &sk_buff functions 3587 * safely. 3588 * 3589 * A buffer cannot be placed on two lists at the same time. 3590 */ 3591 void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 3592 { 3593 unsigned long flags; 3594 3595 spin_lock_irqsave(&list->lock, flags); 3596 __skb_queue_head(list, newsk); 3597 spin_unlock_irqrestore(&list->lock, flags); 3598 } 3599 EXPORT_SYMBOL(skb_queue_head); 3600 3601 /** 3602 * skb_queue_tail - queue a buffer at the list tail 3603 * @list: list to use 3604 * @newsk: buffer to queue 3605 * 3606 * Queue a buffer at the tail of the list. This function takes the 3607 * list lock and can be used safely with other locking &sk_buff functions 3608 * safely. 3609 * 3610 * A buffer cannot be placed on two lists at the same time. 3611 */ 3612 void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 3613 { 3614 unsigned long flags; 3615 3616 spin_lock_irqsave(&list->lock, flags); 3617 __skb_queue_tail(list, newsk); 3618 spin_unlock_irqrestore(&list->lock, flags); 3619 } 3620 EXPORT_SYMBOL(skb_queue_tail); 3621 3622 /** 3623 * skb_unlink - remove a buffer from a list 3624 * @skb: buffer to remove 3625 * @list: list to use 3626 * 3627 * Remove a packet from a list. The list locks are taken and this 3628 * function is atomic with respect to other list locked calls 3629 * 3630 * You must know what list the SKB is on. 3631 */ 3632 void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 3633 { 3634 unsigned long flags; 3635 3636 spin_lock_irqsave(&list->lock, flags); 3637 __skb_unlink(skb, list); 3638 spin_unlock_irqrestore(&list->lock, flags); 3639 } 3640 EXPORT_SYMBOL(skb_unlink); 3641 3642 /** 3643 * skb_append - append a buffer 3644 * @old: buffer to insert after 3645 * @newsk: buffer to insert 3646 * @list: list to use 3647 * 3648 * Place a packet after a given packet in a list. The list locks are taken 3649 * and this function is atomic with respect to other list locked calls. 3650 * A buffer cannot be placed on two lists at the same time. 3651 */ 3652 void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 3653 { 3654 unsigned long flags; 3655 3656 spin_lock_irqsave(&list->lock, flags); 3657 __skb_queue_after(list, old, newsk); 3658 spin_unlock_irqrestore(&list->lock, flags); 3659 } 3660 EXPORT_SYMBOL(skb_append); 3661 3662 static inline void skb_split_inside_header(struct sk_buff *skb, 3663 struct sk_buff* skb1, 3664 const u32 len, const int pos) 3665 { 3666 int i; 3667 3668 skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3669 pos - len); 3670 /* And move data appendix as is. */ 3671 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 3672 skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 3673 3674 skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 3675 skb_shinfo(skb)->nr_frags = 0; 3676 skb1->data_len = skb->data_len; 3677 skb1->len += skb1->data_len; 3678 skb->data_len = 0; 3679 skb->len = len; 3680 skb_set_tail_pointer(skb, len); 3681 } 3682 3683 static inline void skb_split_no_header(struct sk_buff *skb, 3684 struct sk_buff* skb1, 3685 const u32 len, int pos) 3686 { 3687 int i, k = 0; 3688 const int nfrags = skb_shinfo(skb)->nr_frags; 3689 3690 skb_shinfo(skb)->nr_frags = 0; 3691 skb1->len = skb1->data_len = skb->len - len; 3692 skb->len = len; 3693 skb->data_len = len - pos; 3694 3695 for (i = 0; i < nfrags; i++) { 3696 int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 3697 3698 if (pos + size > len) { 3699 skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 3700 3701 if (pos < len) { 3702 /* Split frag. 3703 * We have two variants in this case: 3704 * 1. Move all the frag to the second 3705 * part, if it is possible. F.e. 3706 * this approach is mandatory for TUX, 3707 * where splitting is expensive. 3708 * 2. Split is accurately. We make this. 3709 */ 3710 skb_frag_ref(skb, i); 3711 skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 3712 skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 3713 skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 3714 skb_shinfo(skb)->nr_frags++; 3715 } 3716 k++; 3717 } else 3718 skb_shinfo(skb)->nr_frags++; 3719 pos += size; 3720 } 3721 skb_shinfo(skb1)->nr_frags = k; 3722 } 3723 3724 /** 3725 * skb_split - Split fragmented skb to two parts at length len. 3726 * @skb: the buffer to split 3727 * @skb1: the buffer to receive the second part 3728 * @len: new length for skb 3729 */ 3730 void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 3731 { 3732 int pos = skb_headlen(skb); 3733 const int zc_flags = SKBFL_SHARED_FRAG | SKBFL_PURE_ZEROCOPY; 3734 3735 skb_zcopy_downgrade_managed(skb); 3736 3737 skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & zc_flags; 3738 skb_zerocopy_clone(skb1, skb, 0); 3739 if (len < pos) /* Split line is inside header. */ 3740 skb_split_inside_header(skb, skb1, len, pos); 3741 else /* Second chunk has no header, nothing to copy. */ 3742 skb_split_no_header(skb, skb1, len, pos); 3743 } 3744 EXPORT_SYMBOL(skb_split); 3745 3746 /* Shifting from/to a cloned skb is a no-go. 3747 * 3748 * Caller cannot keep skb_shinfo related pointers past calling here! 3749 */ 3750 static int skb_prepare_for_shift(struct sk_buff *skb) 3751 { 3752 return skb_unclone_keeptruesize(skb, GFP_ATOMIC); 3753 } 3754 3755 /** 3756 * skb_shift - Shifts paged data partially from skb to another 3757 * @tgt: buffer into which tail data gets added 3758 * @skb: buffer from which the paged data comes from 3759 * @shiftlen: shift up to this many bytes 3760 * 3761 * Attempts to shift up to shiftlen worth of bytes, which may be less than 3762 * the length of the skb, from skb to tgt. Returns number bytes shifted. 3763 * It's up to caller to free skb if everything was shifted. 3764 * 3765 * If @tgt runs out of frags, the whole operation is aborted. 3766 * 3767 * Skb cannot include anything else but paged data while tgt is allowed 3768 * to have non-paged data as well. 3769 * 3770 * TODO: full sized shift could be optimized but that would need 3771 * specialized skb free'er to handle frags without up-to-date nr_frags. 3772 */ 3773 int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3774 { 3775 int from, to, merge, todo; 3776 skb_frag_t *fragfrom, *fragto; 3777 3778 BUG_ON(shiftlen > skb->len); 3779 3780 if (skb_headlen(skb)) 3781 return 0; 3782 if (skb_zcopy(tgt) || skb_zcopy(skb)) 3783 return 0; 3784 3785 todo = shiftlen; 3786 from = 0; 3787 to = skb_shinfo(tgt)->nr_frags; 3788 fragfrom = &skb_shinfo(skb)->frags[from]; 3789 3790 /* Actual merge is delayed until the point when we know we can 3791 * commit all, so that we don't have to undo partial changes 3792 */ 3793 if (!to || 3794 !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3795 skb_frag_off(fragfrom))) { 3796 merge = -1; 3797 } else { 3798 merge = to - 1; 3799 3800 todo -= skb_frag_size(fragfrom); 3801 if (todo < 0) { 3802 if (skb_prepare_for_shift(skb) || 3803 skb_prepare_for_shift(tgt)) 3804 return 0; 3805 3806 /* All previous frag pointers might be stale! */ 3807 fragfrom = &skb_shinfo(skb)->frags[from]; 3808 fragto = &skb_shinfo(tgt)->frags[merge]; 3809 3810 skb_frag_size_add(fragto, shiftlen); 3811 skb_frag_size_sub(fragfrom, shiftlen); 3812 skb_frag_off_add(fragfrom, shiftlen); 3813 3814 goto onlymerged; 3815 } 3816 3817 from++; 3818 } 3819 3820 /* Skip full, not-fitting skb to avoid expensive operations */ 3821 if ((shiftlen == skb->len) && 3822 (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3823 return 0; 3824 3825 if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3826 return 0; 3827 3828 while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3829 if (to == MAX_SKB_FRAGS) 3830 return 0; 3831 3832 fragfrom = &skb_shinfo(skb)->frags[from]; 3833 fragto = &skb_shinfo(tgt)->frags[to]; 3834 3835 if (todo >= skb_frag_size(fragfrom)) { 3836 *fragto = *fragfrom; 3837 todo -= skb_frag_size(fragfrom); 3838 from++; 3839 to++; 3840 3841 } else { 3842 __skb_frag_ref(fragfrom); 3843 skb_frag_page_copy(fragto, fragfrom); 3844 skb_frag_off_copy(fragto, fragfrom); 3845 skb_frag_size_set(fragto, todo); 3846 3847 skb_frag_off_add(fragfrom, todo); 3848 skb_frag_size_sub(fragfrom, todo); 3849 todo = 0; 3850 3851 to++; 3852 break; 3853 } 3854 } 3855 3856 /* Ready to "commit" this state change to tgt */ 3857 skb_shinfo(tgt)->nr_frags = to; 3858 3859 if (merge >= 0) { 3860 fragfrom = &skb_shinfo(skb)->frags[0]; 3861 fragto = &skb_shinfo(tgt)->frags[merge]; 3862 3863 skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 3864 __skb_frag_unref(fragfrom, skb->pp_recycle); 3865 } 3866 3867 /* Reposition in the original skb */ 3868 to = 0; 3869 while (from < skb_shinfo(skb)->nr_frags) 3870 skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3871 skb_shinfo(skb)->nr_frags = to; 3872 3873 BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3874 3875 onlymerged: 3876 /* Most likely the tgt won't ever need its checksum anymore, skb on 3877 * the other hand might need it if it needs to be resent 3878 */ 3879 tgt->ip_summed = CHECKSUM_PARTIAL; 3880 skb->ip_summed = CHECKSUM_PARTIAL; 3881 3882 skb_len_add(skb, -shiftlen); 3883 skb_len_add(tgt, shiftlen); 3884 3885 return shiftlen; 3886 } 3887 3888 /** 3889 * skb_prepare_seq_read - Prepare a sequential read of skb data 3890 * @skb: the buffer to read 3891 * @from: lower offset of data to be read 3892 * @to: upper offset of data to be read 3893 * @st: state variable 3894 * 3895 * Initializes the specified state variable. Must be called before 3896 * invoking skb_seq_read() for the first time. 3897 */ 3898 void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3899 unsigned int to, struct skb_seq_state *st) 3900 { 3901 st->lower_offset = from; 3902 st->upper_offset = to; 3903 st->root_skb = st->cur_skb = skb; 3904 st->frag_idx = st->stepped_offset = 0; 3905 st->frag_data = NULL; 3906 st->frag_off = 0; 3907 } 3908 EXPORT_SYMBOL(skb_prepare_seq_read); 3909 3910 /** 3911 * skb_seq_read - Sequentially read skb data 3912 * @consumed: number of bytes consumed by the caller so far 3913 * @data: destination pointer for data to be returned 3914 * @st: state variable 3915 * 3916 * Reads a block of skb data at @consumed relative to the 3917 * lower offset specified to skb_prepare_seq_read(). Assigns 3918 * the head of the data block to @data and returns the length 3919 * of the block or 0 if the end of the skb data or the upper 3920 * offset has been reached. 3921 * 3922 * The caller is not required to consume all of the data 3923 * returned, i.e. @consumed is typically set to the number 3924 * of bytes already consumed and the next call to 3925 * skb_seq_read() will return the remaining part of the block. 3926 * 3927 * Note 1: The size of each block of data returned can be arbitrary, 3928 * this limitation is the cost for zerocopy sequential 3929 * reads of potentially non linear data. 3930 * 3931 * Note 2: Fragment lists within fragments are not implemented 3932 * at the moment, state->root_skb could be replaced with 3933 * a stack for this purpose. 3934 */ 3935 unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3936 struct skb_seq_state *st) 3937 { 3938 unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3939 skb_frag_t *frag; 3940 3941 if (unlikely(abs_offset >= st->upper_offset)) { 3942 if (st->frag_data) { 3943 kunmap_atomic(st->frag_data); 3944 st->frag_data = NULL; 3945 } 3946 return 0; 3947 } 3948 3949 next_skb: 3950 block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3951 3952 if (abs_offset < block_limit && !st->frag_data) { 3953 *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3954 return block_limit - abs_offset; 3955 } 3956 3957 if (st->frag_idx == 0 && !st->frag_data) 3958 st->stepped_offset += skb_headlen(st->cur_skb); 3959 3960 while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 3961 unsigned int pg_idx, pg_off, pg_sz; 3962 3963 frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 3964 3965 pg_idx = 0; 3966 pg_off = skb_frag_off(frag); 3967 pg_sz = skb_frag_size(frag); 3968 3969 if (skb_frag_must_loop(skb_frag_page(frag))) { 3970 pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 3971 pg_off = offset_in_page(pg_off + st->frag_off); 3972 pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 3973 PAGE_SIZE - pg_off); 3974 } 3975 3976 block_limit = pg_sz + st->stepped_offset; 3977 if (abs_offset < block_limit) { 3978 if (!st->frag_data) 3979 st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 3980 3981 *data = (u8 *)st->frag_data + pg_off + 3982 (abs_offset - st->stepped_offset); 3983 3984 return block_limit - abs_offset; 3985 } 3986 3987 if (st->frag_data) { 3988 kunmap_atomic(st->frag_data); 3989 st->frag_data = NULL; 3990 } 3991 3992 st->stepped_offset += pg_sz; 3993 st->frag_off += pg_sz; 3994 if (st->frag_off == skb_frag_size(frag)) { 3995 st->frag_off = 0; 3996 st->frag_idx++; 3997 } 3998 } 3999 4000 if (st->frag_data) { 4001 kunmap_atomic(st->frag_data); 4002 st->frag_data = NULL; 4003 } 4004 4005 if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 4006 st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 4007 st->frag_idx = 0; 4008 goto next_skb; 4009 } else if (st->cur_skb->next) { 4010 st->cur_skb = st->cur_skb->next; 4011 st->frag_idx = 0; 4012 goto next_skb; 4013 } 4014 4015 return 0; 4016 } 4017 EXPORT_SYMBOL(skb_seq_read); 4018 4019 /** 4020 * skb_abort_seq_read - Abort a sequential read of skb data 4021 * @st: state variable 4022 * 4023 * Must be called if skb_seq_read() was not called until it 4024 * returned 0. 4025 */ 4026 void skb_abort_seq_read(struct skb_seq_state *st) 4027 { 4028 if (st->frag_data) 4029 kunmap_atomic(st->frag_data); 4030 } 4031 EXPORT_SYMBOL(skb_abort_seq_read); 4032 4033 #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 4034 4035 static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 4036 struct ts_config *conf, 4037 struct ts_state *state) 4038 { 4039 return skb_seq_read(offset, text, TS_SKB_CB(state)); 4040 } 4041 4042 static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 4043 { 4044 skb_abort_seq_read(TS_SKB_CB(state)); 4045 } 4046 4047 /** 4048 * skb_find_text - Find a text pattern in skb data 4049 * @skb: the buffer to look in 4050 * @from: search offset 4051 * @to: search limit 4052 * @config: textsearch configuration 4053 * 4054 * Finds a pattern in the skb data according to the specified 4055 * textsearch configuration. Use textsearch_next() to retrieve 4056 * subsequent occurrences of the pattern. Returns the offset 4057 * to the first occurrence or UINT_MAX if no match was found. 4058 */ 4059 unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 4060 unsigned int to, struct ts_config *config) 4061 { 4062 struct ts_state state; 4063 unsigned int ret; 4064 4065 BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 4066 4067 config->get_next_block = skb_ts_get_next_block; 4068 config->finish = skb_ts_finish; 4069 4070 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 4071 4072 ret = textsearch_find(config, &state); 4073 return (ret <= to - from ? ret : UINT_MAX); 4074 } 4075 EXPORT_SYMBOL(skb_find_text); 4076 4077 int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 4078 int offset, size_t size) 4079 { 4080 int i = skb_shinfo(skb)->nr_frags; 4081 4082 if (skb_can_coalesce(skb, i, page, offset)) { 4083 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 4084 } else if (i < MAX_SKB_FRAGS) { 4085 skb_zcopy_downgrade_managed(skb); 4086 get_page(page); 4087 skb_fill_page_desc_noacc(skb, i, page, offset, size); 4088 } else { 4089 return -EMSGSIZE; 4090 } 4091 4092 return 0; 4093 } 4094 EXPORT_SYMBOL_GPL(skb_append_pagefrags); 4095 4096 /** 4097 * skb_pull_rcsum - pull skb and update receive checksum 4098 * @skb: buffer to update 4099 * @len: length of data pulled 4100 * 4101 * This function performs an skb_pull on the packet and updates 4102 * the CHECKSUM_COMPLETE checksum. It should be used on 4103 * receive path processing instead of skb_pull unless you know 4104 * that the checksum difference is zero (e.g., a valid IP header) 4105 * or you are setting ip_summed to CHECKSUM_NONE. 4106 */ 4107 void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 4108 { 4109 unsigned char *data = skb->data; 4110 4111 BUG_ON(len > skb->len); 4112 __skb_pull(skb, len); 4113 skb_postpull_rcsum(skb, data, len); 4114 return skb->data; 4115 } 4116 EXPORT_SYMBOL_GPL(skb_pull_rcsum); 4117 4118 static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 4119 { 4120 skb_frag_t head_frag; 4121 struct page *page; 4122 4123 page = virt_to_head_page(frag_skb->head); 4124 __skb_frag_set_page(&head_frag, page); 4125 skb_frag_off_set(&head_frag, frag_skb->data - 4126 (unsigned char *)page_address(page)); 4127 skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 4128 return head_frag; 4129 } 4130 4131 struct sk_buff *skb_segment_list(struct sk_buff *skb, 4132 netdev_features_t features, 4133 unsigned int offset) 4134 { 4135 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 4136 unsigned int tnl_hlen = skb_tnl_header_len(skb); 4137 unsigned int delta_truesize = 0; 4138 unsigned int delta_len = 0; 4139 struct sk_buff *tail = NULL; 4140 struct sk_buff *nskb, *tmp; 4141 int len_diff, err; 4142 4143 skb_push(skb, -skb_network_offset(skb) + offset); 4144 4145 skb_shinfo(skb)->frag_list = NULL; 4146 4147 do { 4148 nskb = list_skb; 4149 list_skb = list_skb->next; 4150 4151 err = 0; 4152 delta_truesize += nskb->truesize; 4153 if (skb_shared(nskb)) { 4154 tmp = skb_clone(nskb, GFP_ATOMIC); 4155 if (tmp) { 4156 consume_skb(nskb); 4157 nskb = tmp; 4158 err = skb_unclone(nskb, GFP_ATOMIC); 4159 } else { 4160 err = -ENOMEM; 4161 } 4162 } 4163 4164 if (!tail) 4165 skb->next = nskb; 4166 else 4167 tail->next = nskb; 4168 4169 if (unlikely(err)) { 4170 nskb->next = list_skb; 4171 goto err_linearize; 4172 } 4173 4174 tail = nskb; 4175 4176 delta_len += nskb->len; 4177 4178 skb_push(nskb, -skb_network_offset(nskb) + offset); 4179 4180 skb_release_head_state(nskb); 4181 len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 4182 __copy_skb_header(nskb, skb); 4183 4184 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 4185 nskb->transport_header += len_diff; 4186 skb_copy_from_linear_data_offset(skb, -tnl_hlen, 4187 nskb->data - tnl_hlen, 4188 offset + tnl_hlen); 4189 4190 if (skb_needs_linearize(nskb, features) && 4191 __skb_linearize(nskb)) 4192 goto err_linearize; 4193 4194 } while (list_skb); 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