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