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