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 len_diff, 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 len_diff = skb_network_header_len(nskb) - skb_network_header_len(skb); 3941 __copy_skb_header(nskb, skb); 3942 3943 skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 3944 nskb->transport_header += len_diff; 3945 skb_copy_from_linear_data_offset(skb, -tnl_hlen, 3946 nskb->data - tnl_hlen, 3947 offset + tnl_hlen); 3948 3949 if (skb_needs_linearize(nskb, features) && 3950 __skb_linearize(nskb)) 3951 goto err_linearize; 3952 3953 } while (list_skb); 3954 3955 skb->truesize = skb->truesize - delta_truesize; 3956 skb->data_len = skb->data_len - delta_len; 3957 skb->len = skb->len - delta_len; 3958 3959 skb_gso_reset(skb); 3960 3961 skb->prev = tail; 3962 3963 if (skb_needs_linearize(skb, features) && 3964 __skb_linearize(skb)) 3965 goto err_linearize; 3966 3967 skb_get(skb); 3968 3969 return skb; 3970 3971 err_linearize: 3972 kfree_skb_list(skb->next); 3973 skb->next = NULL; 3974 return ERR_PTR(-ENOMEM); 3975 } 3976 EXPORT_SYMBOL_GPL(skb_segment_list); 3977 3978 /** 3979 * skb_segment - Perform protocol segmentation on skb. 3980 * @head_skb: buffer to segment 3981 * @features: features for the output path (see dev->features) 3982 * 3983 * This function performs segmentation on the given skb. It returns 3984 * a pointer to the first in a list of new skbs for the segments. 3985 * In case of error it returns ERR_PTR(err). 3986 */ 3987 struct sk_buff *skb_segment(struct sk_buff *head_skb, 3988 netdev_features_t features) 3989 { 3990 struct sk_buff *segs = NULL; 3991 struct sk_buff *tail = NULL; 3992 struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3993 skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3994 unsigned int mss = skb_shinfo(head_skb)->gso_size; 3995 unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 3996 struct sk_buff *frag_skb = head_skb; 3997 unsigned int offset = doffset; 3998 unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3999 unsigned int partial_segs = 0; 4000 unsigned int headroom; 4001 unsigned int len = head_skb->len; 4002 __be16 proto; 4003 bool csum, sg; 4004 int nfrags = skb_shinfo(head_skb)->nr_frags; 4005 int err = -ENOMEM; 4006 int i = 0; 4007 int pos; 4008 4009 if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) && 4010 (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) { 4011 /* gso_size is untrusted, and we have a frag_list with a linear 4012 * non head_frag head. 4013 * 4014 * (we assume checking the first list_skb member suffices; 4015 * i.e if either of the list_skb members have non head_frag 4016 * head, then the first one has too). 4017 * 4018 * If head_skb's headlen does not fit requested gso_size, it 4019 * means that the frag_list members do NOT terminate on exact 4020 * gso_size boundaries. Hence we cannot perform skb_frag_t page 4021 * sharing. Therefore we must fallback to copying the frag_list 4022 * skbs; we do so by disabling SG. 4023 */ 4024 if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) 4025 features &= ~NETIF_F_SG; 4026 } 4027 4028 __skb_push(head_skb, doffset); 4029 proto = skb_network_protocol(head_skb, NULL); 4030 if (unlikely(!proto)) 4031 return ERR_PTR(-EINVAL); 4032 4033 sg = !!(features & NETIF_F_SG); 4034 csum = !!can_checksum_protocol(features, proto); 4035 4036 if (sg && csum && (mss != GSO_BY_FRAGS)) { 4037 if (!(features & NETIF_F_GSO_PARTIAL)) { 4038 struct sk_buff *iter; 4039 unsigned int frag_len; 4040 4041 if (!list_skb || 4042 !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 4043 goto normal; 4044 4045 /* If we get here then all the required 4046 * GSO features except frag_list are supported. 4047 * Try to split the SKB to multiple GSO SKBs 4048 * with no frag_list. 4049 * Currently we can do that only when the buffers don't 4050 * have a linear part and all the buffers except 4051 * the last are of the same length. 4052 */ 4053 frag_len = list_skb->len; 4054 skb_walk_frags(head_skb, iter) { 4055 if (frag_len != iter->len && iter->next) 4056 goto normal; 4057 if (skb_headlen(iter) && !iter->head_frag) 4058 goto normal; 4059 4060 len -= iter->len; 4061 } 4062 4063 if (len != frag_len) 4064 goto normal; 4065 } 4066 4067 /* GSO partial only requires that we trim off any excess that 4068 * doesn't fit into an MSS sized block, so take care of that 4069 * now. 4070 */ 4071 partial_segs = len / mss; 4072 if (partial_segs > 1) 4073 mss *= partial_segs; 4074 else 4075 partial_segs = 0; 4076 } 4077 4078 normal: 4079 headroom = skb_headroom(head_skb); 4080 pos = skb_headlen(head_skb); 4081 4082 do { 4083 struct sk_buff *nskb; 4084 skb_frag_t *nskb_frag; 4085 int hsize; 4086 int size; 4087 4088 if (unlikely(mss == GSO_BY_FRAGS)) { 4089 len = list_skb->len; 4090 } else { 4091 len = head_skb->len - offset; 4092 if (len > mss) 4093 len = mss; 4094 } 4095 4096 hsize = skb_headlen(head_skb) - offset; 4097 4098 if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 4099 (skb_headlen(list_skb) == len || sg)) { 4100 BUG_ON(skb_headlen(list_skb) > len); 4101 4102 i = 0; 4103 nfrags = skb_shinfo(list_skb)->nr_frags; 4104 frag = skb_shinfo(list_skb)->frags; 4105 frag_skb = list_skb; 4106 pos += skb_headlen(list_skb); 4107 4108 while (pos < offset + len) { 4109 BUG_ON(i >= nfrags); 4110 4111 size = skb_frag_size(frag); 4112 if (pos + size > offset + len) 4113 break; 4114 4115 i++; 4116 pos += size; 4117 frag++; 4118 } 4119 4120 nskb = skb_clone(list_skb, GFP_ATOMIC); 4121 list_skb = list_skb->next; 4122 4123 if (unlikely(!nskb)) 4124 goto err; 4125 4126 if (unlikely(pskb_trim(nskb, len))) { 4127 kfree_skb(nskb); 4128 goto err; 4129 } 4130 4131 hsize = skb_end_offset(nskb); 4132 if (skb_cow_head(nskb, doffset + headroom)) { 4133 kfree_skb(nskb); 4134 goto err; 4135 } 4136 4137 nskb->truesize += skb_end_offset(nskb) - hsize; 4138 skb_release_head_state(nskb); 4139 __skb_push(nskb, doffset); 4140 } else { 4141 if (hsize < 0) 4142 hsize = 0; 4143 if (hsize > len || !sg) 4144 hsize = len; 4145 4146 nskb = __alloc_skb(hsize + doffset + headroom, 4147 GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4148 NUMA_NO_NODE); 4149 4150 if (unlikely(!nskb)) 4151 goto err; 4152 4153 skb_reserve(nskb, headroom); 4154 __skb_put(nskb, doffset); 4155 } 4156 4157 if (segs) 4158 tail->next = nskb; 4159 else 4160 segs = nskb; 4161 tail = nskb; 4162 4163 __copy_skb_header(nskb, head_skb); 4164 4165 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4166 skb_reset_mac_len(nskb); 4167 4168 skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 4169 nskb->data - tnl_hlen, 4170 doffset + tnl_hlen); 4171 4172 if (nskb->len == len + doffset) 4173 goto perform_csum_check; 4174 4175 if (!sg) { 4176 if (!csum) { 4177 if (!nskb->remcsum_offload) 4178 nskb->ip_summed = CHECKSUM_NONE; 4179 SKB_GSO_CB(nskb)->csum = 4180 skb_copy_and_csum_bits(head_skb, offset, 4181 skb_put(nskb, 4182 len), 4183 len); 4184 SKB_GSO_CB(nskb)->csum_start = 4185 skb_headroom(nskb) + doffset; 4186 } else { 4187 skb_copy_bits(head_skb, offset, 4188 skb_put(nskb, len), 4189 len); 4190 } 4191 continue; 4192 } 4193 4194 nskb_frag = skb_shinfo(nskb)->frags; 4195 4196 skb_copy_from_linear_data_offset(head_skb, offset, 4197 skb_put(nskb, hsize), hsize); 4198 4199 skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 4200 SKBFL_SHARED_FRAG; 4201 4202 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4203 skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4204 goto err; 4205 4206 while (pos < offset + len) { 4207 if (i >= nfrags) { 4208 i = 0; 4209 nfrags = skb_shinfo(list_skb)->nr_frags; 4210 frag = skb_shinfo(list_skb)->frags; 4211 frag_skb = list_skb; 4212 if (!skb_headlen(list_skb)) { 4213 BUG_ON(!nfrags); 4214 } else { 4215 BUG_ON(!list_skb->head_frag); 4216 4217 /* to make room for head_frag. */ 4218 i--; 4219 frag--; 4220 } 4221 if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4222 skb_zerocopy_clone(nskb, frag_skb, 4223 GFP_ATOMIC)) 4224 goto err; 4225 4226 list_skb = list_skb->next; 4227 } 4228 4229 if (unlikely(skb_shinfo(nskb)->nr_frags >= 4230 MAX_SKB_FRAGS)) { 4231 net_warn_ratelimited( 4232 "skb_segment: too many frags: %u %u\n", 4233 pos, mss); 4234 err = -EINVAL; 4235 goto err; 4236 } 4237 4238 *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 4239 __skb_frag_ref(nskb_frag); 4240 size = skb_frag_size(nskb_frag); 4241 4242 if (pos < offset) { 4243 skb_frag_off_add(nskb_frag, offset - pos); 4244 skb_frag_size_sub(nskb_frag, offset - pos); 4245 } 4246 4247 skb_shinfo(nskb)->nr_frags++; 4248 4249 if (pos + size <= offset + len) { 4250 i++; 4251 frag++; 4252 pos += size; 4253 } else { 4254 skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 4255 goto skip_fraglist; 4256 } 4257 4258 nskb_frag++; 4259 } 4260 4261 skip_fraglist: 4262 nskb->data_len = len - hsize; 4263 nskb->len += nskb->data_len; 4264 nskb->truesize += nskb->data_len; 4265 4266 perform_csum_check: 4267 if (!csum) { 4268 if (skb_has_shared_frag(nskb) && 4269 __skb_linearize(nskb)) 4270 goto err; 4271 4272 if (!nskb->remcsum_offload) 4273 nskb->ip_summed = CHECKSUM_NONE; 4274 SKB_GSO_CB(nskb)->csum = 4275 skb_checksum(nskb, doffset, 4276 nskb->len - doffset, 0); 4277 SKB_GSO_CB(nskb)->csum_start = 4278 skb_headroom(nskb) + doffset; 4279 } 4280 } while ((offset += len) < head_skb->len); 4281 4282 /* Some callers want to get the end of the list. 4283 * Put it in segs->prev to avoid walking the list. 4284 * (see validate_xmit_skb_list() for example) 4285 */ 4286 segs->prev = tail; 4287 4288 if (partial_segs) { 4289 struct sk_buff *iter; 4290 int type = skb_shinfo(head_skb)->gso_type; 4291 unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4292 4293 /* Update type to add partial and then remove dodgy if set */ 4294 type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4295 type &= ~SKB_GSO_DODGY; 4296 4297 /* Update GSO info and prepare to start updating headers on 4298 * our way back down the stack of protocols. 4299 */ 4300 for (iter = segs; iter; iter = iter->next) { 4301 skb_shinfo(iter)->gso_size = gso_size; 4302 skb_shinfo(iter)->gso_segs = partial_segs; 4303 skb_shinfo(iter)->gso_type = type; 4304 SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 4305 } 4306 4307 if (tail->len - doffset <= gso_size) 4308 skb_shinfo(tail)->gso_size = 0; 4309 else if (tail != segs) 4310 skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4311 } 4312 4313 /* Following permits correct backpressure, for protocols 4314 * using skb_set_owner_w(). 4315 * Idea is to tranfert ownership from head_skb to last segment. 4316 */ 4317 if (head_skb->destructor == sock_wfree) { 4318 swap(tail->truesize, head_skb->truesize); 4319 swap(tail->destructor, head_skb->destructor); 4320 swap(tail->sk, head_skb->sk); 4321 } 4322 return segs; 4323 4324 err: 4325 kfree_skb_list(segs); 4326 return ERR_PTR(err); 4327 } 4328 EXPORT_SYMBOL_GPL(skb_segment); 4329 4330 #ifdef CONFIG_SKB_EXTENSIONS 4331 #define SKB_EXT_ALIGN_VALUE 8 4332 #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4333 4334 static const u8 skb_ext_type_len[] = { 4335 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4336 [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4337 #endif 4338 #ifdef CONFIG_XFRM 4339 [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 4340 #endif 4341 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 4342 [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 4343 #endif 4344 #if IS_ENABLED(CONFIG_MPTCP) 4345 [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 4346 #endif 4347 #if IS_ENABLED(CONFIG_MCTP_FLOWS) 4348 [SKB_EXT_MCTP] = SKB_EXT_CHUNKSIZEOF(struct mctp_flow), 4349 #endif 4350 }; 4351 4352 static __always_inline unsigned int skb_ext_total_length(void) 4353 { 4354 return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4355 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4356 skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4357 #endif 4358 #ifdef CONFIG_XFRM 4359 skb_ext_type_len[SKB_EXT_SEC_PATH] + 4360 #endif 4361 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 4362 skb_ext_type_len[TC_SKB_EXT] + 4363 #endif 4364 #if IS_ENABLED(CONFIG_MPTCP) 4365 skb_ext_type_len[SKB_EXT_MPTCP] + 4366 #endif 4367 #if IS_ENABLED(CONFIG_MCTP_FLOWS) 4368 skb_ext_type_len[SKB_EXT_MCTP] + 4369 #endif 4370 0; 4371 } 4372 4373 static void skb_extensions_init(void) 4374 { 4375 BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4376 BUILD_BUG_ON(skb_ext_total_length() > 255); 4377 4378 skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4379 SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4380 0, 4381 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4382 NULL); 4383 } 4384 #else 4385 static void skb_extensions_init(void) {} 4386 #endif 4387 4388 void __init skb_init(void) 4389 { 4390 skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 4391 sizeof(struct sk_buff), 4392 0, 4393 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4394 offsetof(struct sk_buff, cb), 4395 sizeof_field(struct sk_buff, cb), 4396 NULL); 4397 skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4398 sizeof(struct sk_buff_fclones), 4399 0, 4400 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4401 NULL); 4402 skb_extensions_init(); 4403 } 4404 4405 static int 4406 __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 4407 unsigned int recursion_level) 4408 { 4409 int start = skb_headlen(skb); 4410 int i, copy = start - offset; 4411 struct sk_buff *frag_iter; 4412 int elt = 0; 4413 4414 if (unlikely(recursion_level >= 24)) 4415 return -EMSGSIZE; 4416 4417 if (copy > 0) { 4418 if (copy > len) 4419 copy = len; 4420 sg_set_buf(sg, skb->data + offset, copy); 4421 elt++; 4422 if ((len -= copy) == 0) 4423 return elt; 4424 offset += copy; 4425 } 4426 4427 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 4428 int end; 4429 4430 WARN_ON(start > offset + len); 4431 4432 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4433 if ((copy = end - offset) > 0) { 4434 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 4435 if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 4436 return -EMSGSIZE; 4437 4438 if (copy > len) 4439 copy = len; 4440 sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4441 skb_frag_off(frag) + offset - start); 4442 elt++; 4443 if (!(len -= copy)) 4444 return elt; 4445 offset += copy; 4446 } 4447 start = end; 4448 } 4449 4450 skb_walk_frags(skb, frag_iter) { 4451 int end, ret; 4452 4453 WARN_ON(start > offset + len); 4454 4455 end = start + frag_iter->len; 4456 if ((copy = end - offset) > 0) { 4457 if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 4458 return -EMSGSIZE; 4459 4460 if (copy > len) 4461 copy = len; 4462 ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 4463 copy, recursion_level + 1); 4464 if (unlikely(ret < 0)) 4465 return ret; 4466 elt += ret; 4467 if ((len -= copy) == 0) 4468 return elt; 4469 offset += copy; 4470 } 4471 start = end; 4472 } 4473 BUG_ON(len); 4474 return elt; 4475 } 4476 4477 /** 4478 * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 4479 * @skb: Socket buffer containing the buffers to be mapped 4480 * @sg: The scatter-gather list to map into 4481 * @offset: The offset into the buffer's contents to start mapping 4482 * @len: Length of buffer space to be mapped 4483 * 4484 * Fill the specified scatter-gather list with mappings/pointers into a 4485 * region of the buffer space attached to a socket buffer. Returns either 4486 * the number of scatterlist items used, or -EMSGSIZE if the contents 4487 * could not fit. 4488 */ 4489 int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 4490 { 4491 int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 4492 4493 if (nsg <= 0) 4494 return nsg; 4495 4496 sg_mark_end(&sg[nsg - 1]); 4497 4498 return nsg; 4499 } 4500 EXPORT_SYMBOL_GPL(skb_to_sgvec); 4501 4502 /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 4503 * sglist without mark the sg which contain last skb data as the end. 4504 * So the caller can mannipulate sg list as will when padding new data after 4505 * the first call without calling sg_unmark_end to expend sg list. 4506 * 4507 * Scenario to use skb_to_sgvec_nomark: 4508 * 1. sg_init_table 4509 * 2. skb_to_sgvec_nomark(payload1) 4510 * 3. skb_to_sgvec_nomark(payload2) 4511 * 4512 * This is equivalent to: 4513 * 1. sg_init_table 4514 * 2. skb_to_sgvec(payload1) 4515 * 3. sg_unmark_end 4516 * 4. skb_to_sgvec(payload2) 4517 * 4518 * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 4519 * is more preferable. 4520 */ 4521 int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 4522 int offset, int len) 4523 { 4524 return __skb_to_sgvec(skb, sg, offset, len, 0); 4525 } 4526 EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 4527 4528 4529 4530 /** 4531 * skb_cow_data - Check that a socket buffer's data buffers are writable 4532 * @skb: The socket buffer to check. 4533 * @tailbits: Amount of trailing space to be added 4534 * @trailer: Returned pointer to the skb where the @tailbits space begins 4535 * 4536 * Make sure that the data buffers attached to a socket buffer are 4537 * writable. If they are not, private copies are made of the data buffers 4538 * and the socket buffer is set to use these instead. 4539 * 4540 * If @tailbits is given, make sure that there is space to write @tailbits 4541 * bytes of data beyond current end of socket buffer. @trailer will be 4542 * set to point to the skb in which this space begins. 4543 * 4544 * The number of scatterlist elements required to completely map the 4545 * COW'd and extended socket buffer will be returned. 4546 */ 4547 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4548 { 4549 int copyflag; 4550 int elt; 4551 struct sk_buff *skb1, **skb_p; 4552 4553 /* If skb is cloned or its head is paged, reallocate 4554 * head pulling out all the pages (pages are considered not writable 4555 * at the moment even if they are anonymous). 4556 */ 4557 if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4558 !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4559 return -ENOMEM; 4560 4561 /* Easy case. Most of packets will go this way. */ 4562 if (!skb_has_frag_list(skb)) { 4563 /* A little of trouble, not enough of space for trailer. 4564 * This should not happen, when stack is tuned to generate 4565 * good frames. OK, on miss we reallocate and reserve even more 4566 * space, 128 bytes is fair. */ 4567 4568 if (skb_tailroom(skb) < tailbits && 4569 pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4570 return -ENOMEM; 4571 4572 /* Voila! */ 4573 *trailer = skb; 4574 return 1; 4575 } 4576 4577 /* Misery. We are in troubles, going to mincer fragments... */ 4578 4579 elt = 1; 4580 skb_p = &skb_shinfo(skb)->frag_list; 4581 copyflag = 0; 4582 4583 while ((skb1 = *skb_p) != NULL) { 4584 int ntail = 0; 4585 4586 /* The fragment is partially pulled by someone, 4587 * this can happen on input. Copy it and everything 4588 * after it. */ 4589 4590 if (skb_shared(skb1)) 4591 copyflag = 1; 4592 4593 /* If the skb is the last, worry about trailer. */ 4594 4595 if (skb1->next == NULL && tailbits) { 4596 if (skb_shinfo(skb1)->nr_frags || 4597 skb_has_frag_list(skb1) || 4598 skb_tailroom(skb1) < tailbits) 4599 ntail = tailbits + 128; 4600 } 4601 4602 if (copyflag || 4603 skb_cloned(skb1) || 4604 ntail || 4605 skb_shinfo(skb1)->nr_frags || 4606 skb_has_frag_list(skb1)) { 4607 struct sk_buff *skb2; 4608 4609 /* Fuck, we are miserable poor guys... */ 4610 if (ntail == 0) 4611 skb2 = skb_copy(skb1, GFP_ATOMIC); 4612 else 4613 skb2 = skb_copy_expand(skb1, 4614 skb_headroom(skb1), 4615 ntail, 4616 GFP_ATOMIC); 4617 if (unlikely(skb2 == NULL)) 4618 return -ENOMEM; 4619 4620 if (skb1->sk) 4621 skb_set_owner_w(skb2, skb1->sk); 4622 4623 /* Looking around. Are we still alive? 4624 * OK, link new skb, drop old one */ 4625 4626 skb2->next = skb1->next; 4627 *skb_p = skb2; 4628 kfree_skb(skb1); 4629 skb1 = skb2; 4630 } 4631 elt++; 4632 *trailer = skb1; 4633 skb_p = &skb1->next; 4634 } 4635 4636 return elt; 4637 } 4638 EXPORT_SYMBOL_GPL(skb_cow_data); 4639 4640 static void sock_rmem_free(struct sk_buff *skb) 4641 { 4642 struct sock *sk = skb->sk; 4643 4644 atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4645 } 4646 4647 static void skb_set_err_queue(struct sk_buff *skb) 4648 { 4649 /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 4650 * So, it is safe to (mis)use it to mark skbs on the error queue. 4651 */ 4652 skb->pkt_type = PACKET_OUTGOING; 4653 BUILD_BUG_ON(PACKET_OUTGOING == 0); 4654 } 4655 4656 /* 4657 * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4658 */ 4659 int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4660 { 4661 if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4662 (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4663 return -ENOMEM; 4664 4665 skb_orphan(skb); 4666 skb->sk = sk; 4667 skb->destructor = sock_rmem_free; 4668 atomic_add(skb->truesize, &sk->sk_rmem_alloc); 4669 skb_set_err_queue(skb); 4670 4671 /* before exiting rcu section, make sure dst is refcounted */ 4672 skb_dst_force(skb); 4673 4674 skb_queue_tail(&sk->sk_error_queue, skb); 4675 if (!sock_flag(sk, SOCK_DEAD)) 4676 sk_error_report(sk); 4677 return 0; 4678 } 4679 EXPORT_SYMBOL(sock_queue_err_skb); 4680 4681 static bool is_icmp_err_skb(const struct sk_buff *skb) 4682 { 4683 return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 4684 SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 4685 } 4686 4687 struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4688 { 4689 struct sk_buff_head *q = &sk->sk_error_queue; 4690 struct sk_buff *skb, *skb_next = NULL; 4691 bool icmp_next = false; 4692 unsigned long flags; 4693 4694 spin_lock_irqsave(&q->lock, flags); 4695 skb = __skb_dequeue(q); 4696 if (skb && (skb_next = skb_peek(q))) { 4697 icmp_next = is_icmp_err_skb(skb_next); 4698 if (icmp_next) 4699 sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 4700 } 4701 spin_unlock_irqrestore(&q->lock, flags); 4702 4703 if (is_icmp_err_skb(skb) && !icmp_next) 4704 sk->sk_err = 0; 4705 4706 if (skb_next) 4707 sk_error_report(sk); 4708 4709 return skb; 4710 } 4711 EXPORT_SYMBOL(sock_dequeue_err_skb); 4712 4713 /** 4714 * skb_clone_sk - create clone of skb, and take reference to socket 4715 * @skb: the skb to clone 4716 * 4717 * This function creates a clone of a buffer that holds a reference on 4718 * sk_refcnt. Buffers created via this function are meant to be 4719 * returned using sock_queue_err_skb, or free via kfree_skb. 4720 * 4721 * When passing buffers allocated with this function to sock_queue_err_skb 4722 * it is necessary to wrap the call with sock_hold/sock_put in order to 4723 * prevent the socket from being released prior to being enqueued on 4724 * the sk_error_queue. 4725 */ 4726 struct sk_buff *skb_clone_sk(struct sk_buff *skb) 4727 { 4728 struct sock *sk = skb->sk; 4729 struct sk_buff *clone; 4730 4731 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 4732 return NULL; 4733 4734 clone = skb_clone(skb, GFP_ATOMIC); 4735 if (!clone) { 4736 sock_put(sk); 4737 return NULL; 4738 } 4739 4740 clone->sk = sk; 4741 clone->destructor = sock_efree; 4742 4743 return clone; 4744 } 4745 EXPORT_SYMBOL(skb_clone_sk); 4746 4747 static void __skb_complete_tx_timestamp(struct sk_buff *skb, 4748 struct sock *sk, 4749 int tstype, 4750 bool opt_stats) 4751 { 4752 struct sock_exterr_skb *serr; 4753 int err; 4754 4755 BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 4756 4757 serr = SKB_EXT_ERR(skb); 4758 memset(serr, 0, sizeof(*serr)); 4759 serr->ee.ee_errno = ENOMSG; 4760 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4761 serr->ee.ee_info = tstype; 4762 serr->opt_stats = opt_stats; 4763 serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 4764 if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 4765 serr->ee.ee_data = skb_shinfo(skb)->tskey; 4766 if (sk_is_tcp(sk)) 4767 serr->ee.ee_data -= atomic_read(&sk->sk_tskey); 4768 } 4769 4770 err = sock_queue_err_skb(sk, skb); 4771 4772 if (err) 4773 kfree_skb(skb); 4774 } 4775 4776 static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4777 { 4778 bool ret; 4779 4780 if (likely(sysctl_tstamp_allow_data || tsonly)) 4781 return true; 4782 4783 read_lock_bh(&sk->sk_callback_lock); 4784 ret = sk->sk_socket && sk->sk_socket->file && 4785 file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4786 read_unlock_bh(&sk->sk_callback_lock); 4787 return ret; 4788 } 4789 4790 void skb_complete_tx_timestamp(struct sk_buff *skb, 4791 struct skb_shared_hwtstamps *hwtstamps) 4792 { 4793 struct sock *sk = skb->sk; 4794 4795 if (!skb_may_tx_timestamp(sk, false)) 4796 goto err; 4797 4798 /* Take a reference to prevent skb_orphan() from freeing the socket, 4799 * but only if the socket refcount is not zero. 4800 */ 4801 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 4802 *skb_hwtstamps(skb) = *hwtstamps; 4803 __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 4804 sock_put(sk); 4805 return; 4806 } 4807 4808 err: 4809 kfree_skb(skb); 4810 } 4811 EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 4812 4813 void __skb_tstamp_tx(struct sk_buff *orig_skb, 4814 const struct sk_buff *ack_skb, 4815 struct skb_shared_hwtstamps *hwtstamps, 4816 struct sock *sk, int tstype) 4817 { 4818 struct sk_buff *skb; 4819 bool tsonly, opt_stats = false; 4820 4821 if (!sk) 4822 return; 4823 4824 if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4825 skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4826 return; 4827 4828 tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 4829 if (!skb_may_tx_timestamp(sk, tsonly)) 4830 return; 4831 4832 if (tsonly) { 4833 #ifdef CONFIG_INET 4834 if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 4835 sk_is_tcp(sk)) { 4836 skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 4837 ack_skb); 4838 opt_stats = true; 4839 } else 4840 #endif 4841 skb = alloc_skb(0, GFP_ATOMIC); 4842 } else { 4843 skb = skb_clone(orig_skb, GFP_ATOMIC); 4844 } 4845 if (!skb) 4846 return; 4847 4848 if (tsonly) { 4849 skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4850 SKBTX_ANY_TSTAMP; 4851 skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 4852 } 4853 4854 if (hwtstamps) 4855 *skb_hwtstamps(skb) = *hwtstamps; 4856 else 4857 __net_timestamp(skb); 4858 4859 __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 4860 } 4861 EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4862 4863 void skb_tstamp_tx(struct sk_buff *orig_skb, 4864 struct skb_shared_hwtstamps *hwtstamps) 4865 { 4866 return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 4867 SCM_TSTAMP_SND); 4868 } 4869 EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4870 4871 void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 4872 { 4873 struct sock *sk = skb->sk; 4874 struct sock_exterr_skb *serr; 4875 int err = 1; 4876 4877 skb->wifi_acked_valid = 1; 4878 skb->wifi_acked = acked; 4879 4880 serr = SKB_EXT_ERR(skb); 4881 memset(serr, 0, sizeof(*serr)); 4882 serr->ee.ee_errno = ENOMSG; 4883 serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 4884 4885 /* Take a reference to prevent skb_orphan() from freeing the socket, 4886 * but only if the socket refcount is not zero. 4887 */ 4888 if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 4889 err = sock_queue_err_skb(sk, skb); 4890 sock_put(sk); 4891 } 4892 if (err) 4893 kfree_skb(skb); 4894 } 4895 EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 4896 4897 /** 4898 * skb_partial_csum_set - set up and verify partial csum values for packet 4899 * @skb: the skb to set 4900 * @start: the number of bytes after skb->data to start checksumming. 4901 * @off: the offset from start to place the checksum. 4902 * 4903 * For untrusted partially-checksummed packets, we need to make sure the values 4904 * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4905 * 4906 * This function checks and sets those values and skb->ip_summed: if this 4907 * returns false you should drop the packet. 4908 */ 4909 bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4910 { 4911 u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 4912 u32 csum_start = skb_headroom(skb) + (u32)start; 4913 4914 if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 4915 net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 4916 start, off, skb_headroom(skb), skb_headlen(skb)); 4917 return false; 4918 } 4919 skb->ip_summed = CHECKSUM_PARTIAL; 4920 skb->csum_start = csum_start; 4921 skb->csum_offset = off; 4922 skb_set_transport_header(skb, start); 4923 return true; 4924 } 4925 EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4926 4927 static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4928 unsigned int max) 4929 { 4930 if (skb_headlen(skb) >= len) 4931 return 0; 4932 4933 /* If we need to pullup then pullup to the max, so we 4934 * won't need to do it again. 4935 */ 4936 if (max > skb->len) 4937 max = skb->len; 4938 4939 if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4940 return -ENOMEM; 4941 4942 if (skb_headlen(skb) < len) 4943 return -EPROTO; 4944 4945 return 0; 4946 } 4947 4948 #define MAX_TCP_HDR_LEN (15 * 4) 4949 4950 static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4951 typeof(IPPROTO_IP) proto, 4952 unsigned int off) 4953 { 4954 int err; 4955 4956 switch (proto) { 4957 case IPPROTO_TCP: 4958 err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4959 off + MAX_TCP_HDR_LEN); 4960 if (!err && !skb_partial_csum_set(skb, off, 4961 offsetof(struct tcphdr, 4962 check))) 4963 err = -EPROTO; 4964 return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4965 4966 case IPPROTO_UDP: 4967 err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4968 off + sizeof(struct udphdr)); 4969 if (!err && !skb_partial_csum_set(skb, off, 4970 offsetof(struct udphdr, 4971 check))) 4972 err = -EPROTO; 4973 return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4974 } 4975 4976 return ERR_PTR(-EPROTO); 4977 } 4978 4979 /* This value should be large enough to cover a tagged ethernet header plus 4980 * maximally sized IP and TCP or UDP headers. 4981 */ 4982 #define MAX_IP_HDR_LEN 128 4983 4984 static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4985 { 4986 unsigned int off; 4987 bool fragment; 4988 __sum16 *csum; 4989 int err; 4990 4991 fragment = false; 4992 4993 err = skb_maybe_pull_tail(skb, 4994 sizeof(struct iphdr), 4995 MAX_IP_HDR_LEN); 4996 if (err < 0) 4997 goto out; 4998 4999 if (ip_is_fragment(ip_hdr(skb))) 5000 fragment = true; 5001 5002 off = ip_hdrlen(skb); 5003 5004 err = -EPROTO; 5005 5006 if (fragment) 5007 goto out; 5008 5009 csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5010 if (IS_ERR(csum)) 5011 return PTR_ERR(csum); 5012 5013 if (recalculate) 5014 *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5015 ip_hdr(skb)->daddr, 5016 skb->len - off, 5017 ip_hdr(skb)->protocol, 0); 5018 err = 0; 5019 5020 out: 5021 return err; 5022 } 5023 5024 /* This value should be large enough to cover a tagged ethernet header plus 5025 * an IPv6 header, all options, and a maximal TCP or UDP header. 5026 */ 5027 #define MAX_IPV6_HDR_LEN 256 5028 5029 #define OPT_HDR(type, skb, off) \ 5030 (type *)(skb_network_header(skb) + (off)) 5031 5032 static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5033 { 5034 int err; 5035 u8 nexthdr; 5036 unsigned int off; 5037 unsigned int len; 5038 bool fragment; 5039 bool done; 5040 __sum16 *csum; 5041 5042 fragment = false; 5043 done = false; 5044 5045 off = sizeof(struct ipv6hdr); 5046 5047 err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5048 if (err < 0) 5049 goto out; 5050 5051 nexthdr = ipv6_hdr(skb)->nexthdr; 5052 5053 len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5054 while (off <= len && !done) { 5055 switch (nexthdr) { 5056 case IPPROTO_DSTOPTS: 5057 case IPPROTO_HOPOPTS: 5058 case IPPROTO_ROUTING: { 5059 struct ipv6_opt_hdr *hp; 5060 5061 err = skb_maybe_pull_tail(skb, 5062 off + 5063 sizeof(struct ipv6_opt_hdr), 5064 MAX_IPV6_HDR_LEN); 5065 if (err < 0) 5066 goto out; 5067 5068 hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5069 nexthdr = hp->nexthdr; 5070 off += ipv6_optlen(hp); 5071 break; 5072 } 5073 case IPPROTO_AH: { 5074 struct ip_auth_hdr *hp; 5075 5076 err = skb_maybe_pull_tail(skb, 5077 off + 5078 sizeof(struct ip_auth_hdr), 5079 MAX_IPV6_HDR_LEN); 5080 if (err < 0) 5081 goto out; 5082 5083 hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5084 nexthdr = hp->nexthdr; 5085 off += ipv6_authlen(hp); 5086 break; 5087 } 5088 case IPPROTO_FRAGMENT: { 5089 struct frag_hdr *hp; 5090 5091 err = skb_maybe_pull_tail(skb, 5092 off + 5093 sizeof(struct frag_hdr), 5094 MAX_IPV6_HDR_LEN); 5095 if (err < 0) 5096 goto out; 5097 5098 hp = OPT_HDR(struct frag_hdr, skb, off); 5099 5100 if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5101 fragment = true; 5102 5103 nexthdr = hp->nexthdr; 5104 off += sizeof(struct frag_hdr); 5105 break; 5106 } 5107 default: 5108 done = true; 5109 break; 5110 } 5111 } 5112 5113 err = -EPROTO; 5114 5115 if (!done || fragment) 5116 goto out; 5117 5118 csum = skb_checksum_setup_ip(skb, nexthdr, off); 5119 if (IS_ERR(csum)) 5120 return PTR_ERR(csum); 5121 5122 if (recalculate) 5123 *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5124 &ipv6_hdr(skb)->daddr, 5125 skb->len - off, nexthdr, 0); 5126 err = 0; 5127 5128 out: 5129 return err; 5130 } 5131 5132 /** 5133 * skb_checksum_setup - set up partial checksum offset 5134 * @skb: the skb to set up 5135 * @recalculate: if true the pseudo-header checksum will be recalculated 5136 */ 5137 int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5138 { 5139 int err; 5140 5141 switch (skb->protocol) { 5142 case htons(ETH_P_IP): 5143 err = skb_checksum_setup_ipv4(skb, recalculate); 5144 break; 5145 5146 case htons(ETH_P_IPV6): 5147 err = skb_checksum_setup_ipv6(skb, recalculate); 5148 break; 5149 5150 default: 5151 err = -EPROTO; 5152 break; 5153 } 5154 5155 return err; 5156 } 5157 EXPORT_SYMBOL(skb_checksum_setup); 5158 5159 /** 5160 * skb_checksum_maybe_trim - maybe trims the given skb 5161 * @skb: the skb to check 5162 * @transport_len: the data length beyond the network header 5163 * 5164 * Checks whether the given skb has data beyond the given transport length. 5165 * If so, returns a cloned skb trimmed to this transport length. 5166 * Otherwise returns the provided skb. Returns NULL in error cases 5167 * (e.g. transport_len exceeds skb length or out-of-memory). 5168 * 5169 * Caller needs to set the skb transport header and free any returned skb if it 5170 * differs from the provided skb. 5171 */ 5172 static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 5173 unsigned int transport_len) 5174 { 5175 struct sk_buff *skb_chk; 5176 unsigned int len = skb_transport_offset(skb) + transport_len; 5177 int ret; 5178 5179 if (skb->len < len) 5180 return NULL; 5181 else if (skb->len == len) 5182 return skb; 5183 5184 skb_chk = skb_clone(skb, GFP_ATOMIC); 5185 if (!skb_chk) 5186 return NULL; 5187 5188 ret = pskb_trim_rcsum(skb_chk, len); 5189 if (ret) { 5190 kfree_skb(skb_chk); 5191 return NULL; 5192 } 5193 5194 return skb_chk; 5195 } 5196 5197 /** 5198 * skb_checksum_trimmed - validate checksum of an skb 5199 * @skb: the skb to check 5200 * @transport_len: the data length beyond the network header 5201 * @skb_chkf: checksum function to use 5202 * 5203 * Applies the given checksum function skb_chkf to the provided skb. 5204 * Returns a checked and maybe trimmed skb. Returns NULL on error. 5205 * 5206 * If the skb has data beyond the given transport length, then a 5207 * trimmed & cloned skb is checked and returned. 5208 * 5209 * Caller needs to set the skb transport header and free any returned skb if it 5210 * differs from the provided skb. 5211 */ 5212 struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 5213 unsigned int transport_len, 5214 __sum16(*skb_chkf)(struct sk_buff *skb)) 5215 { 5216 struct sk_buff *skb_chk; 5217 unsigned int offset = skb_transport_offset(skb); 5218 __sum16 ret; 5219 5220 skb_chk = skb_checksum_maybe_trim(skb, transport_len); 5221 if (!skb_chk) 5222 goto err; 5223 5224 if (!pskb_may_pull(skb_chk, offset)) 5225 goto err; 5226 5227 skb_pull_rcsum(skb_chk, offset); 5228 ret = skb_chkf(skb_chk); 5229 skb_push_rcsum(skb_chk, offset); 5230 5231 if (ret) 5232 goto err; 5233 5234 return skb_chk; 5235 5236 err: 5237 if (skb_chk && skb_chk != skb) 5238 kfree_skb(skb_chk); 5239 5240 return NULL; 5241 5242 } 5243 EXPORT_SYMBOL(skb_checksum_trimmed); 5244 5245 void __skb_warn_lro_forwarding(const struct sk_buff *skb) 5246 { 5247 net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5248 skb->dev->name); 5249 } 5250 EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5251 5252 void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5253 { 5254 if (head_stolen) { 5255 skb_release_head_state(skb); 5256 kmem_cache_free(skbuff_head_cache, skb); 5257 } else { 5258 __kfree_skb(skb); 5259 } 5260 } 5261 EXPORT_SYMBOL(kfree_skb_partial); 5262 5263 /** 5264 * skb_try_coalesce - try to merge skb to prior one 5265 * @to: prior buffer 5266 * @from: buffer to add 5267 * @fragstolen: pointer to boolean 5268 * @delta_truesize: how much more was allocated than was requested 5269 */ 5270 bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5271 bool *fragstolen, int *delta_truesize) 5272 { 5273 struct skb_shared_info *to_shinfo, *from_shinfo; 5274 int i, delta, len = from->len; 5275 5276 *fragstolen = false; 5277 5278 if (skb_cloned(to)) 5279 return false; 5280 5281 /* In general, avoid mixing slab allocated and page_pool allocated 5282 * pages within the same SKB. However when @to is not pp_recycle and 5283 * @from is cloned, we can transition frag pages from page_pool to 5284 * reference counted. 5285 * 5286 * On the other hand, don't allow coalescing two pp_recycle SKBs if 5287 * @from is cloned, in case the SKB is using page_pool fragment 5288 * references (PP_FLAG_PAGE_FRAG). Since we only take full page 5289 * references for cloned SKBs at the moment that would result in 5290 * inconsistent reference counts. 5291 */ 5292 if (to->pp_recycle != (from->pp_recycle && !skb_cloned(from))) 5293 return false; 5294 5295 if (len <= skb_tailroom(to)) { 5296 if (len) 5297 BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5298 *delta_truesize = 0; 5299 return true; 5300 } 5301 5302 to_shinfo = skb_shinfo(to); 5303 from_shinfo = skb_shinfo(from); 5304 if (to_shinfo->frag_list || from_shinfo->frag_list) 5305 return false; 5306 if (skb_zcopy(to) || skb_zcopy(from)) 5307 return false; 5308 5309 if (skb_headlen(from) != 0) { 5310 struct page *page; 5311 unsigned int offset; 5312 5313 if (to_shinfo->nr_frags + 5314 from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5315 return false; 5316 5317 if (skb_head_is_locked(from)) 5318 return false; 5319 5320 delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5321 5322 page = virt_to_head_page(from->head); 5323 offset = from->data - (unsigned char *)page_address(page); 5324 5325 skb_fill_page_desc(to, to_shinfo->nr_frags, 5326 page, offset, skb_headlen(from)); 5327 *fragstolen = true; 5328 } else { 5329 if (to_shinfo->nr_frags + 5330 from_shinfo->nr_frags > MAX_SKB_FRAGS) 5331 return false; 5332 5333 delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5334 } 5335 5336 WARN_ON_ONCE(delta < len); 5337 5338 memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5339 from_shinfo->frags, 5340 from_shinfo->nr_frags * sizeof(skb_frag_t)); 5341 to_shinfo->nr_frags += from_shinfo->nr_frags; 5342 5343 if (!skb_cloned(from)) 5344 from_shinfo->nr_frags = 0; 5345 5346 /* if the skb is not cloned this does nothing 5347 * since we set nr_frags to 0. 5348 */ 5349 for (i = 0; i < from_shinfo->nr_frags; i++) 5350 __skb_frag_ref(&from_shinfo->frags[i]); 5351 5352 to->truesize += delta; 5353 to->len += len; 5354 to->data_len += len; 5355 5356 *delta_truesize = delta; 5357 return true; 5358 } 5359 EXPORT_SYMBOL(skb_try_coalesce); 5360 5361 /** 5362 * skb_scrub_packet - scrub an skb 5363 * 5364 * @skb: buffer to clean 5365 * @xnet: packet is crossing netns 5366 * 5367 * skb_scrub_packet can be used after encapsulating or decapsulting a packet 5368 * into/from a tunnel. Some information have to be cleared during these 5369 * operations. 5370 * skb_scrub_packet can also be used to clean a skb before injecting it in 5371 * another namespace (@xnet == true). We have to clear all information in the 5372 * skb that could impact namespace isolation. 5373 */ 5374 void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5375 { 5376 skb->pkt_type = PACKET_HOST; 5377 skb->skb_iif = 0; 5378 skb->ignore_df = 0; 5379 skb_dst_drop(skb); 5380 skb_ext_reset(skb); 5381 nf_reset_ct(skb); 5382 nf_reset_trace(skb); 5383 5384 #ifdef CONFIG_NET_SWITCHDEV 5385 skb->offload_fwd_mark = 0; 5386 skb->offload_l3_fwd_mark = 0; 5387 #endif 5388 5389 if (!xnet) 5390 return; 5391 5392 ipvs_reset(skb); 5393 skb->mark = 0; 5394 skb_clear_tstamp(skb); 5395 } 5396 EXPORT_SYMBOL_GPL(skb_scrub_packet); 5397 5398 /** 5399 * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5400 * 5401 * @skb: GSO skb 5402 * 5403 * skb_gso_transport_seglen is used to determine the real size of the 5404 * individual segments, including Layer4 headers (TCP/UDP). 5405 * 5406 * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5407 */ 5408 static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5409 { 5410 const struct skb_shared_info *shinfo = skb_shinfo(skb); 5411 unsigned int thlen = 0; 5412 5413 if (skb->encapsulation) { 5414 thlen = skb_inner_transport_header(skb) - 5415 skb_transport_header(skb); 5416 5417 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5418 thlen += inner_tcp_hdrlen(skb); 5419 } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5420 thlen = tcp_hdrlen(skb); 5421 } else if (unlikely(skb_is_gso_sctp(skb))) { 5422 thlen = sizeof(struct sctphdr); 5423 } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5424 thlen = sizeof(struct udphdr); 5425 } 5426 /* UFO sets gso_size to the size of the fragmentation 5427 * payload, i.e. the size of the L4 (UDP) header is already 5428 * accounted for. 5429 */ 5430 return thlen + shinfo->gso_size; 5431 } 5432 5433 /** 5434 * skb_gso_network_seglen - Return length of individual segments of a gso packet 5435 * 5436 * @skb: GSO skb 5437 * 5438 * skb_gso_network_seglen is used to determine the real size of the 5439 * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5440 * 5441 * The MAC/L2 header is not accounted for. 5442 */ 5443 static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5444 { 5445 unsigned int hdr_len = skb_transport_header(skb) - 5446 skb_network_header(skb); 5447 5448 return hdr_len + skb_gso_transport_seglen(skb); 5449 } 5450 5451 /** 5452 * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5453 * 5454 * @skb: GSO skb 5455 * 5456 * skb_gso_mac_seglen is used to determine the real size of the 5457 * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5458 * headers (TCP/UDP). 5459 */ 5460 static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5461 { 5462 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5463 5464 return hdr_len + skb_gso_transport_seglen(skb); 5465 } 5466 5467 /** 5468 * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 5469 * 5470 * There are a couple of instances where we have a GSO skb, and we 5471 * want to determine what size it would be after it is segmented. 5472 * 5473 * We might want to check: 5474 * - L3+L4+payload size (e.g. IP forwarding) 5475 * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 5476 * 5477 * This is a helper to do that correctly considering GSO_BY_FRAGS. 5478 * 5479 * @skb: GSO skb 5480 * 5481 * @seg_len: The segmented length (from skb_gso_*_seglen). In the 5482 * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 5483 * 5484 * @max_len: The maximum permissible length. 5485 * 5486 * Returns true if the segmented length <= max length. 5487 */ 5488 static inline bool skb_gso_size_check(const struct sk_buff *skb, 5489 unsigned int seg_len, 5490 unsigned int max_len) { 5491 const struct skb_shared_info *shinfo = skb_shinfo(skb); 5492 const struct sk_buff *iter; 5493 5494 if (shinfo->gso_size != GSO_BY_FRAGS) 5495 return seg_len <= max_len; 5496 5497 /* Undo this so we can re-use header sizes */ 5498 seg_len -= GSO_BY_FRAGS; 5499 5500 skb_walk_frags(skb, iter) { 5501 if (seg_len + skb_headlen(iter) > max_len) 5502 return false; 5503 } 5504 5505 return true; 5506 } 5507 5508 /** 5509 * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5510 * 5511 * @skb: GSO skb 5512 * @mtu: MTU to validate against 5513 * 5514 * skb_gso_validate_network_len validates if a given skb will fit a 5515 * wanted MTU once split. It considers L3 headers, L4 headers, and the 5516 * payload. 5517 */ 5518 bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5519 { 5520 return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5521 } 5522 EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5523 5524 /** 5525 * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 5526 * 5527 * @skb: GSO skb 5528 * @len: length to validate against 5529 * 5530 * skb_gso_validate_mac_len validates if a given skb will fit a wanted 5531 * length once split, including L2, L3 and L4 headers and the payload. 5532 */ 5533 bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 5534 { 5535 return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 5536 } 5537 EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 5538 5539 static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 5540 { 5541 int mac_len, meta_len; 5542 void *meta; 5543 5544 if (skb_cow(skb, skb_headroom(skb)) < 0) { 5545 kfree_skb(skb); 5546 return NULL; 5547 } 5548 5549 mac_len = skb->data - skb_mac_header(skb); 5550 if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 5551 memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 5552 mac_len - VLAN_HLEN - ETH_TLEN); 5553 } 5554 5555 meta_len = skb_metadata_len(skb); 5556 if (meta_len) { 5557 meta = skb_metadata_end(skb) - meta_len; 5558 memmove(meta + VLAN_HLEN, meta, meta_len); 5559 } 5560 5561 skb->mac_header += VLAN_HLEN; 5562 return skb; 5563 } 5564 5565 struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 5566 { 5567 struct vlan_hdr *vhdr; 5568 u16 vlan_tci; 5569 5570 if (unlikely(skb_vlan_tag_present(skb))) { 5571 /* vlan_tci is already set-up so leave this for another time */ 5572 return skb; 5573 } 5574 5575 skb = skb_share_check(skb, GFP_ATOMIC); 5576 if (unlikely(!skb)) 5577 goto err_free; 5578 /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 5579 if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 5580 goto err_free; 5581 5582 vhdr = (struct vlan_hdr *)skb->data; 5583 vlan_tci = ntohs(vhdr->h_vlan_TCI); 5584 __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 5585 5586 skb_pull_rcsum(skb, VLAN_HLEN); 5587 vlan_set_encap_proto(skb, vhdr); 5588 5589 skb = skb_reorder_vlan_header(skb); 5590 if (unlikely(!skb)) 5591 goto err_free; 5592 5593 skb_reset_network_header(skb); 5594 if (!skb_transport_header_was_set(skb)) 5595 skb_reset_transport_header(skb); 5596 skb_reset_mac_len(skb); 5597 5598 return skb; 5599 5600 err_free: 5601 kfree_skb(skb); 5602 return NULL; 5603 } 5604 EXPORT_SYMBOL(skb_vlan_untag); 5605 5606 int skb_ensure_writable(struct sk_buff *skb, int write_len) 5607 { 5608 if (!pskb_may_pull(skb, write_len)) 5609 return -ENOMEM; 5610 5611 if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5612 return 0; 5613 5614 return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5615 } 5616 EXPORT_SYMBOL(skb_ensure_writable); 5617 5618 /* remove VLAN header from packet and update csum accordingly. 5619 * expects a non skb_vlan_tag_present skb with a vlan tag payload 5620 */ 5621 int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 5622 { 5623 struct vlan_hdr *vhdr; 5624 int offset = skb->data - skb_mac_header(skb); 5625 int err; 5626 5627 if (WARN_ONCE(offset, 5628 "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5629 offset)) { 5630 return -EINVAL; 5631 } 5632 5633 err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 5634 if (unlikely(err)) 5635 return err; 5636 5637 skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 5638 5639 vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 5640 *vlan_tci = ntohs(vhdr->h_vlan_TCI); 5641 5642 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 5643 __skb_pull(skb, VLAN_HLEN); 5644 5645 vlan_set_encap_proto(skb, vhdr); 5646 skb->mac_header += VLAN_HLEN; 5647 5648 if (skb_network_offset(skb) < ETH_HLEN) 5649 skb_set_network_header(skb, ETH_HLEN); 5650 5651 skb_reset_mac_len(skb); 5652 5653 return err; 5654 } 5655 EXPORT_SYMBOL(__skb_vlan_pop); 5656 5657 /* Pop a vlan tag either from hwaccel or from payload. 5658 * Expects skb->data at mac header. 5659 */ 5660 int skb_vlan_pop(struct sk_buff *skb) 5661 { 5662 u16 vlan_tci; 5663 __be16 vlan_proto; 5664 int err; 5665 5666 if (likely(skb_vlan_tag_present(skb))) { 5667 __vlan_hwaccel_clear_tag(skb); 5668 } else { 5669 if (unlikely(!eth_type_vlan(skb->protocol))) 5670 return 0; 5671 5672 err = __skb_vlan_pop(skb, &vlan_tci); 5673 if (err) 5674 return err; 5675 } 5676 /* move next vlan tag to hw accel tag */ 5677 if (likely(!eth_type_vlan(skb->protocol))) 5678 return 0; 5679 5680 vlan_proto = skb->protocol; 5681 err = __skb_vlan_pop(skb, &vlan_tci); 5682 if (unlikely(err)) 5683 return err; 5684 5685 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 5686 return 0; 5687 } 5688 EXPORT_SYMBOL(skb_vlan_pop); 5689 5690 /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5691 * Expects skb->data at mac header. 5692 */ 5693 int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 5694 { 5695 if (skb_vlan_tag_present(skb)) { 5696 int offset = skb->data - skb_mac_header(skb); 5697 int err; 5698 5699 if (WARN_ONCE(offset, 5700 "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5701 offset)) { 5702 return -EINVAL; 5703 } 5704 5705 err = __vlan_insert_tag(skb, skb->vlan_proto, 5706 skb_vlan_tag_get(skb)); 5707 if (err) 5708 return err; 5709 5710 skb->protocol = skb->vlan_proto; 5711 skb->mac_len += VLAN_HLEN; 5712 5713 skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 5714 } 5715 __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 5716 return 0; 5717 } 5718 EXPORT_SYMBOL(skb_vlan_push); 5719 5720 /** 5721 * skb_eth_pop() - Drop the Ethernet header at the head of a packet 5722 * 5723 * @skb: Socket buffer to modify 5724 * 5725 * Drop the Ethernet header of @skb. 5726 * 5727 * Expects that skb->data points to the mac header and that no VLAN tags are 5728 * present. 5729 * 5730 * Returns 0 on success, -errno otherwise. 5731 */ 5732 int skb_eth_pop(struct sk_buff *skb) 5733 { 5734 if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 5735 skb_network_offset(skb) < ETH_HLEN) 5736 return -EPROTO; 5737 5738 skb_pull_rcsum(skb, ETH_HLEN); 5739 skb_reset_mac_header(skb); 5740 skb_reset_mac_len(skb); 5741 5742 return 0; 5743 } 5744 EXPORT_SYMBOL(skb_eth_pop); 5745 5746 /** 5747 * skb_eth_push() - Add a new Ethernet header at the head of a packet 5748 * 5749 * @skb: Socket buffer to modify 5750 * @dst: Destination MAC address of the new header 5751 * @src: Source MAC address of the new header 5752 * 5753 * Prepend @skb with a new Ethernet header. 5754 * 5755 * Expects that skb->data points to the mac header, which must be empty. 5756 * 5757 * Returns 0 on success, -errno otherwise. 5758 */ 5759 int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 5760 const unsigned char *src) 5761 { 5762 struct ethhdr *eth; 5763 int err; 5764 5765 if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 5766 return -EPROTO; 5767 5768 err = skb_cow_head(skb, sizeof(*eth)); 5769 if (err < 0) 5770 return err; 5771 5772 skb_push(skb, sizeof(*eth)); 5773 skb_reset_mac_header(skb); 5774 skb_reset_mac_len(skb); 5775 5776 eth = eth_hdr(skb); 5777 ether_addr_copy(eth->h_dest, dst); 5778 ether_addr_copy(eth->h_source, src); 5779 eth->h_proto = skb->protocol; 5780 5781 skb_postpush_rcsum(skb, eth, sizeof(*eth)); 5782 5783 return 0; 5784 } 5785 EXPORT_SYMBOL(skb_eth_push); 5786 5787 /* Update the ethertype of hdr and the skb csum value if required. */ 5788 static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 5789 __be16 ethertype) 5790 { 5791 if (skb->ip_summed == CHECKSUM_COMPLETE) { 5792 __be16 diff[] = { ~hdr->h_proto, ethertype }; 5793 5794 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 5795 } 5796 5797 hdr->h_proto = ethertype; 5798 } 5799 5800 /** 5801 * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 5802 * the packet 5803 * 5804 * @skb: buffer 5805 * @mpls_lse: MPLS label stack entry to push 5806 * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 5807 * @mac_len: length of the MAC header 5808 * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 5809 * ethernet 5810 * 5811 * Expects skb->data at mac header. 5812 * 5813 * Returns 0 on success, -errno otherwise. 5814 */ 5815 int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 5816 int mac_len, bool ethernet) 5817 { 5818 struct mpls_shim_hdr *lse; 5819 int err; 5820 5821 if (unlikely(!eth_p_mpls(mpls_proto))) 5822 return -EINVAL; 5823 5824 /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 5825 if (skb->encapsulation) 5826 return -EINVAL; 5827 5828 err = skb_cow_head(skb, MPLS_HLEN); 5829 if (unlikely(err)) 5830 return err; 5831 5832 if (!skb->inner_protocol) { 5833 skb_set_inner_network_header(skb, skb_network_offset(skb)); 5834 skb_set_inner_protocol(skb, skb->protocol); 5835 } 5836 5837 skb_push(skb, MPLS_HLEN); 5838 memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 5839 mac_len); 5840 skb_reset_mac_header(skb); 5841 skb_set_network_header(skb, mac_len); 5842 skb_reset_mac_len(skb); 5843 5844 lse = mpls_hdr(skb); 5845 lse->label_stack_entry = mpls_lse; 5846 skb_postpush_rcsum(skb, lse, MPLS_HLEN); 5847 5848 if (ethernet && mac_len >= ETH_HLEN) 5849 skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 5850 skb->protocol = mpls_proto; 5851 5852 return 0; 5853 } 5854 EXPORT_SYMBOL_GPL(skb_mpls_push); 5855 5856 /** 5857 * skb_mpls_pop() - pop the outermost MPLS header 5858 * 5859 * @skb: buffer 5860 * @next_proto: ethertype of header after popped MPLS header 5861 * @mac_len: length of the MAC header 5862 * @ethernet: flag to indicate if the packet is ethernet 5863 * 5864 * Expects skb->data at mac header. 5865 * 5866 * Returns 0 on success, -errno otherwise. 5867 */ 5868 int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 5869 bool ethernet) 5870 { 5871 int err; 5872 5873 if (unlikely(!eth_p_mpls(skb->protocol))) 5874 return 0; 5875 5876 err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 5877 if (unlikely(err)) 5878 return err; 5879 5880 skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 5881 memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 5882 mac_len); 5883 5884 __skb_pull(skb, MPLS_HLEN); 5885 skb_reset_mac_header(skb); 5886 skb_set_network_header(skb, mac_len); 5887 5888 if (ethernet && mac_len >= ETH_HLEN) { 5889 struct ethhdr *hdr; 5890 5891 /* use mpls_hdr() to get ethertype to account for VLANs. */ 5892 hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 5893 skb_mod_eth_type(skb, hdr, next_proto); 5894 } 5895 skb->protocol = next_proto; 5896 5897 return 0; 5898 } 5899 EXPORT_SYMBOL_GPL(skb_mpls_pop); 5900 5901 /** 5902 * skb_mpls_update_lse() - modify outermost MPLS header and update csum 5903 * 5904 * @skb: buffer 5905 * @mpls_lse: new MPLS label stack entry to update to 5906 * 5907 * Expects skb->data at mac header. 5908 * 5909 * Returns 0 on success, -errno otherwise. 5910 */ 5911 int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 5912 { 5913 int err; 5914 5915 if (unlikely(!eth_p_mpls(skb->protocol))) 5916 return -EINVAL; 5917 5918 err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 5919 if (unlikely(err)) 5920 return err; 5921 5922 if (skb->ip_summed == CHECKSUM_COMPLETE) { 5923 __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 5924 5925 skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 5926 } 5927 5928 mpls_hdr(skb)->label_stack_entry = mpls_lse; 5929 5930 return 0; 5931 } 5932 EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 5933 5934 /** 5935 * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 5936 * 5937 * @skb: buffer 5938 * 5939 * Expects skb->data at mac header. 5940 * 5941 * Returns 0 on success, -errno otherwise. 5942 */ 5943 int skb_mpls_dec_ttl(struct sk_buff *skb) 5944 { 5945 u32 lse; 5946 u8 ttl; 5947 5948 if (unlikely(!eth_p_mpls(skb->protocol))) 5949 return -EINVAL; 5950 5951 if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 5952 return -ENOMEM; 5953 5954 lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 5955 ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 5956 if (!--ttl) 5957 return -EINVAL; 5958 5959 lse &= ~MPLS_LS_TTL_MASK; 5960 lse |= ttl << MPLS_LS_TTL_SHIFT; 5961 5962 return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 5963 } 5964 EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 5965 5966 /** 5967 * alloc_skb_with_frags - allocate skb with page frags 5968 * 5969 * @header_len: size of linear part 5970 * @data_len: needed length in frags 5971 * @max_page_order: max page order desired. 5972 * @errcode: pointer to error code if any 5973 * @gfp_mask: allocation mask 5974 * 5975 * This can be used to allocate a paged skb, given a maximal order for frags. 5976 */ 5977 struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 5978 unsigned long data_len, 5979 int max_page_order, 5980 int *errcode, 5981 gfp_t gfp_mask) 5982 { 5983 int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 5984 unsigned long chunk; 5985 struct sk_buff *skb; 5986 struct page *page; 5987 int i; 5988 5989 *errcode = -EMSGSIZE; 5990 /* Note this test could be relaxed, if we succeed to allocate 5991 * high order pages... 5992 */ 5993 if (npages > MAX_SKB_FRAGS) 5994 return NULL; 5995 5996 *errcode = -ENOBUFS; 5997 skb = alloc_skb(header_len, gfp_mask); 5998 if (!skb) 5999 return NULL; 6000 6001 skb->truesize += npages << PAGE_SHIFT; 6002 6003 for (i = 0; npages > 0; i++) { 6004 int order = max_page_order; 6005 6006 while (order) { 6007 if (npages >= 1 << order) { 6008 page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 6009 __GFP_COMP | 6010 __GFP_NOWARN, 6011 order); 6012 if (page) 6013 goto fill_page; 6014 /* Do not retry other high order allocations */ 6015 order = 1; 6016 max_page_order = 0; 6017 } 6018 order--; 6019 } 6020 page = alloc_page(gfp_mask); 6021 if (!page) 6022 goto failure; 6023 fill_page: 6024 chunk = min_t(unsigned long, data_len, 6025 PAGE_SIZE << order); 6026 skb_fill_page_desc(skb, i, page, 0, chunk); 6027 data_len -= chunk; 6028 npages -= 1 << order; 6029 } 6030 return skb; 6031 6032 failure: 6033 kfree_skb(skb); 6034 return NULL; 6035 } 6036 EXPORT_SYMBOL(alloc_skb_with_frags); 6037 6038 /* carve out the first off bytes from skb when off < headlen */ 6039 static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 6040 const int headlen, gfp_t gfp_mask) 6041 { 6042 int i; 6043 int size = skb_end_offset(skb); 6044 int new_hlen = headlen - off; 6045 u8 *data; 6046 6047 size = SKB_DATA_ALIGN(size); 6048 6049 if (skb_pfmemalloc(skb)) 6050 gfp_mask |= __GFP_MEMALLOC; 6051 data = kmalloc_reserve(size + 6052 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 6053 gfp_mask, NUMA_NO_NODE, NULL); 6054 if (!data) 6055 return -ENOMEM; 6056 6057 size = SKB_WITH_OVERHEAD(ksize(data)); 6058 6059 /* Copy real data, and all frags */ 6060 skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 6061 skb->len -= off; 6062 6063 memcpy((struct skb_shared_info *)(data + size), 6064 skb_shinfo(skb), 6065 offsetof(struct skb_shared_info, 6066 frags[skb_shinfo(skb)->nr_frags])); 6067 if (skb_cloned(skb)) { 6068 /* drop the old head gracefully */ 6069 if (skb_orphan_frags(skb, gfp_mask)) { 6070 kfree(data); 6071 return -ENOMEM; 6072 } 6073 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 6074 skb_frag_ref(skb, i); 6075 if (skb_has_frag_list(skb)) 6076 skb_clone_fraglist(skb); 6077 skb_release_data(skb); 6078 } else { 6079 /* we can reuse existing recount- all we did was 6080 * relocate values 6081 */ 6082 skb_free_head(skb); 6083 } 6084 6085 skb->head = data; 6086 skb->data = data; 6087 skb->head_frag = 0; 6088 skb_set_end_offset(skb, size); 6089 skb_set_tail_pointer(skb, skb_headlen(skb)); 6090 skb_headers_offset_update(skb, 0); 6091 skb->cloned = 0; 6092 skb->hdr_len = 0; 6093 skb->nohdr = 0; 6094 atomic_set(&skb_shinfo(skb)->dataref, 1); 6095 6096 return 0; 6097 } 6098 6099 static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 6100 6101 /* carve out the first eat bytes from skb's frag_list. May recurse into 6102 * pskb_carve() 6103 */ 6104 static int pskb_carve_frag_list(struct sk_buff *skb, 6105 struct skb_shared_info *shinfo, int eat, 6106 gfp_t gfp_mask) 6107 { 6108 struct sk_buff *list = shinfo->frag_list; 6109 struct sk_buff *clone = NULL; 6110 struct sk_buff *insp = NULL; 6111 6112 do { 6113 if (!list) { 6114 pr_err("Not enough bytes to eat. Want %d\n", eat); 6115 return -EFAULT; 6116 } 6117 if (list->len <= eat) { 6118 /* Eaten as whole. */ 6119 eat -= list->len; 6120 list = list->next; 6121 insp = list; 6122 } else { 6123 /* Eaten partially. */ 6124 if (skb_shared(list)) { 6125 clone = skb_clone(list, gfp_mask); 6126 if (!clone) 6127 return -ENOMEM; 6128 insp = list->next; 6129 list = clone; 6130 } else { 6131 /* This may be pulled without problems. */ 6132 insp = list; 6133 } 6134 if (pskb_carve(list, eat, gfp_mask) < 0) { 6135 kfree_skb(clone); 6136 return -ENOMEM; 6137 } 6138 break; 6139 } 6140 } while (eat); 6141 6142 /* Free pulled out fragments. */ 6143 while ((list = shinfo->frag_list) != insp) { 6144 shinfo->frag_list = list->next; 6145 consume_skb(list); 6146 } 6147 /* And insert new clone at head. */ 6148 if (clone) { 6149 clone->next = list; 6150 shinfo->frag_list = clone; 6151 } 6152 return 0; 6153 } 6154 6155 /* carve off first len bytes from skb. Split line (off) is in the 6156 * non-linear part of skb 6157 */ 6158 static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 6159 int pos, gfp_t gfp_mask) 6160 { 6161 int i, k = 0; 6162 int size = skb_end_offset(skb); 6163 u8 *data; 6164 const int nfrags = skb_shinfo(skb)->nr_frags; 6165 struct skb_shared_info *shinfo; 6166 6167 size = SKB_DATA_ALIGN(size); 6168 6169 if (skb_pfmemalloc(skb)) 6170 gfp_mask |= __GFP_MEMALLOC; 6171 data = kmalloc_reserve(size + 6172 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 6173 gfp_mask, NUMA_NO_NODE, NULL); 6174 if (!data) 6175 return -ENOMEM; 6176 6177 size = SKB_WITH_OVERHEAD(ksize(data)); 6178 6179 memcpy((struct skb_shared_info *)(data + size), 6180 skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 6181 if (skb_orphan_frags(skb, gfp_mask)) { 6182 kfree(data); 6183 return -ENOMEM; 6184 } 6185 shinfo = (struct skb_shared_info *)(data + size); 6186 for (i = 0; i < nfrags; i++) { 6187 int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 6188 6189 if (pos + fsize > off) { 6190 shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 6191 6192 if (pos < off) { 6193 /* Split frag. 6194 * We have two variants in this case: 6195 * 1. Move all the frag to the second 6196 * part, if it is possible. F.e. 6197 * this approach is mandatory for TUX, 6198 * where splitting is expensive. 6199 * 2. Split is accurately. We make this. 6200 */ 6201 skb_frag_off_add(&shinfo->frags[0], off - pos); 6202 skb_frag_size_sub(&shinfo->frags[0], off - pos); 6203 } 6204 skb_frag_ref(skb, i); 6205 k++; 6206 } 6207 pos += fsize; 6208 } 6209 shinfo->nr_frags = k; 6210 if (skb_has_frag_list(skb)) 6211 skb_clone_fraglist(skb); 6212 6213 /* split line is in frag list */ 6214 if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6215 /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6216 if (skb_has_frag_list(skb)) 6217 kfree_skb_list(skb_shinfo(skb)->frag_list); 6218 kfree(data); 6219 return -ENOMEM; 6220 } 6221 skb_release_data(skb); 6222 6223 skb->head = data; 6224 skb->head_frag = 0; 6225 skb->data = data; 6226 skb_set_end_offset(skb, size); 6227 skb_reset_tail_pointer(skb); 6228 skb_headers_offset_update(skb, 0); 6229 skb->cloned = 0; 6230 skb->hdr_len = 0; 6231 skb->nohdr = 0; 6232 skb->len -= off; 6233 skb->data_len = skb->len; 6234 atomic_set(&skb_shinfo(skb)->dataref, 1); 6235 return 0; 6236 } 6237 6238 /* remove len bytes from the beginning of the skb */ 6239 static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 6240 { 6241 int headlen = skb_headlen(skb); 6242 6243 if (len < headlen) 6244 return pskb_carve_inside_header(skb, len, headlen, gfp); 6245 else 6246 return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 6247 } 6248 6249 /* Extract to_copy bytes starting at off from skb, and return this in 6250 * a new skb 6251 */ 6252 struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 6253 int to_copy, gfp_t gfp) 6254 { 6255 struct sk_buff *clone = skb_clone(skb, gfp); 6256 6257 if (!clone) 6258 return NULL; 6259 6260 if (pskb_carve(clone, off, gfp) < 0 || 6261 pskb_trim(clone, to_copy)) { 6262 kfree_skb(clone); 6263 return NULL; 6264 } 6265 return clone; 6266 } 6267 EXPORT_SYMBOL(pskb_extract); 6268 6269 /** 6270 * skb_condense - try to get rid of fragments/frag_list if possible 6271 * @skb: buffer 6272 * 6273 * Can be used to save memory before skb is added to a busy queue. 6274 * If packet has bytes in frags and enough tail room in skb->head, 6275 * pull all of them, so that we can free the frags right now and adjust 6276 * truesize. 6277 * Notes: 6278 * We do not reallocate skb->head thus can not fail. 6279 * Caller must re-evaluate skb->truesize if needed. 6280 */ 6281 void skb_condense(struct sk_buff *skb) 6282 { 6283 if (skb->data_len) { 6284 if (skb->data_len > skb->end - skb->tail || 6285 skb_cloned(skb)) 6286 return; 6287 6288 /* Nice, we can free page frag(s) right now */ 6289 __pskb_pull_tail(skb, skb->data_len); 6290 } 6291 /* At this point, skb->truesize might be over estimated, 6292 * because skb had a fragment, and fragments do not tell 6293 * their truesize. 6294 * When we pulled its content into skb->head, fragment 6295 * was freed, but __pskb_pull_tail() could not possibly 6296 * adjust skb->truesize, not knowing the frag truesize. 6297 */ 6298 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6299 } 6300 6301 #ifdef CONFIG_SKB_EXTENSIONS 6302 static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6303 { 6304 return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6305 } 6306 6307 /** 6308 * __skb_ext_alloc - allocate a new skb extensions storage 6309 * 6310 * @flags: See kmalloc(). 6311 * 6312 * Returns the newly allocated pointer. The pointer can later attached to a 6313 * skb via __skb_ext_set(). 6314 * Note: caller must handle the skb_ext as an opaque data. 6315 */ 6316 struct skb_ext *__skb_ext_alloc(gfp_t flags) 6317 { 6318 struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6319 6320 if (new) { 6321 memset(new->offset, 0, sizeof(new->offset)); 6322 refcount_set(&new->refcnt, 1); 6323 } 6324 6325 return new; 6326 } 6327 6328 static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 6329 unsigned int old_active) 6330 { 6331 struct skb_ext *new; 6332 6333 if (refcount_read(&old->refcnt) == 1) 6334 return old; 6335 6336 new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6337 if (!new) 6338 return NULL; 6339 6340 memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6341 refcount_set(&new->refcnt, 1); 6342 6343 #ifdef CONFIG_XFRM 6344 if (old_active & (1 << SKB_EXT_SEC_PATH)) { 6345 struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 6346 unsigned int i; 6347 6348 for (i = 0; i < sp->len; i++) 6349 xfrm_state_hold(sp->xvec[i]); 6350 } 6351 #endif 6352 __skb_ext_put(old); 6353 return new; 6354 } 6355 6356 /** 6357 * __skb_ext_set - attach the specified extension storage to this skb 6358 * @skb: buffer 6359 * @id: extension id 6360 * @ext: extension storage previously allocated via __skb_ext_alloc() 6361 * 6362 * Existing extensions, if any, are cleared. 6363 * 6364 * Returns the pointer to the extension. 6365 */ 6366 void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 6367 struct skb_ext *ext) 6368 { 6369 unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 6370 6371 skb_ext_put(skb); 6372 newlen = newoff + skb_ext_type_len[id]; 6373 ext->chunks = newlen; 6374 ext->offset[id] = newoff; 6375 skb->extensions = ext; 6376 skb->active_extensions = 1 << id; 6377 return skb_ext_get_ptr(ext, id); 6378 } 6379 6380 /** 6381 * skb_ext_add - allocate space for given extension, COW if needed 6382 * @skb: buffer 6383 * @id: extension to allocate space for 6384 * 6385 * Allocates enough space for the given extension. 6386 * If the extension is already present, a pointer to that extension 6387 * is returned. 6388 * 6389 * If the skb was cloned, COW applies and the returned memory can be 6390 * modified without changing the extension space of clones buffers. 6391 * 6392 * Returns pointer to the extension or NULL on allocation failure. 6393 */ 6394 void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6395 { 6396 struct skb_ext *new, *old = NULL; 6397 unsigned int newlen, newoff; 6398 6399 if (skb->active_extensions) { 6400 old = skb->extensions; 6401 6402 new = skb_ext_maybe_cow(old, skb->active_extensions); 6403 if (!new) 6404 return NULL; 6405 6406 if (__skb_ext_exist(new, id)) 6407 goto set_active; 6408 6409 newoff = new->chunks; 6410 } else { 6411 newoff = SKB_EXT_CHUNKSIZEOF(*new); 6412 6413 new = __skb_ext_alloc(GFP_ATOMIC); 6414 if (!new) 6415 return NULL; 6416 } 6417 6418 newlen = newoff + skb_ext_type_len[id]; 6419 new->chunks = newlen; 6420 new->offset[id] = newoff; 6421 set_active: 6422 skb->slow_gro = 1; 6423 skb->extensions = new; 6424 skb->active_extensions |= 1 << id; 6425 return skb_ext_get_ptr(new, id); 6426 } 6427 EXPORT_SYMBOL(skb_ext_add); 6428 6429 #ifdef CONFIG_XFRM 6430 static void skb_ext_put_sp(struct sec_path *sp) 6431 { 6432 unsigned int i; 6433 6434 for (i = 0; i < sp->len; i++) 6435 xfrm_state_put(sp->xvec[i]); 6436 } 6437 #endif 6438 6439 #ifdef CONFIG_MCTP_FLOWS 6440 static void skb_ext_put_mctp(struct mctp_flow *flow) 6441 { 6442 if (flow->key) 6443 mctp_key_unref(flow->key); 6444 } 6445 #endif 6446 6447 void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6448 { 6449 struct skb_ext *ext = skb->extensions; 6450 6451 skb->active_extensions &= ~(1 << id); 6452 if (skb->active_extensions == 0) { 6453 skb->extensions = NULL; 6454 __skb_ext_put(ext); 6455 #ifdef CONFIG_XFRM 6456 } else if (id == SKB_EXT_SEC_PATH && 6457 refcount_read(&ext->refcnt) == 1) { 6458 struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 6459 6460 skb_ext_put_sp(sp); 6461 sp->len = 0; 6462 #endif 6463 } 6464 } 6465 EXPORT_SYMBOL(__skb_ext_del); 6466 6467 void __skb_ext_put(struct skb_ext *ext) 6468 { 6469 /* If this is last clone, nothing can increment 6470 * it after check passes. Avoids one atomic op. 6471 */ 6472 if (refcount_read(&ext->refcnt) == 1) 6473 goto free_now; 6474 6475 if (!refcount_dec_and_test(&ext->refcnt)) 6476 return; 6477 free_now: 6478 #ifdef CONFIG_XFRM 6479 if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 6480 skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 6481 #endif 6482 #ifdef CONFIG_MCTP_FLOWS 6483 if (__skb_ext_exist(ext, SKB_EXT_MCTP)) 6484 skb_ext_put_mctp(skb_ext_get_ptr(ext, SKB_EXT_MCTP)); 6485 #endif 6486 6487 kmem_cache_free(skbuff_ext_cache, ext); 6488 } 6489 EXPORT_SYMBOL(__skb_ext_put); 6490 #endif /* CONFIG_SKB_EXTENSIONS */ 6491