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