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