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