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