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