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