12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 41da177e4SLinus Torvalds * 5113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 61da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Fixes: 91da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 101da177e4SLinus Torvalds * balancer bugs. 111da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 121da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 131da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 141da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 151da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 161da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 171da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 181da177e4SLinus Torvalds * only put in the headers 191da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 201da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 211da177e4SLinus Torvalds * Andi Kleen : slabified it. 221da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * NOTE: 251da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 261da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 271da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 281da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 291da177e4SLinus Torvalds */ 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* 321da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 35e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 36e005d193SJoe Perches 371da177e4SLinus Torvalds #include <linux/module.h> 381da177e4SLinus Torvalds #include <linux/types.h> 391da177e4SLinus Torvalds #include <linux/kernel.h> 401da177e4SLinus Torvalds #include <linux/mm.h> 411da177e4SLinus Torvalds #include <linux/interrupt.h> 421da177e4SLinus Torvalds #include <linux/in.h> 431da177e4SLinus Torvalds #include <linux/inet.h> 441da177e4SLinus Torvalds #include <linux/slab.h> 45de960aa9SFlorian Westphal #include <linux/tcp.h> 46de960aa9SFlorian Westphal #include <linux/udp.h> 4790017accSMarcelo Ricardo Leitner #include <linux/sctp.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 501da177e4SLinus Torvalds #include <net/pkt_sched.h> 511da177e4SLinus Torvalds #endif 521da177e4SLinus Torvalds #include <linux/string.h> 531da177e4SLinus Torvalds #include <linux/skbuff.h> 549c55e01cSJens Axboe #include <linux/splice.h> 551da177e4SLinus Torvalds #include <linux/cache.h> 561da177e4SLinus Torvalds #include <linux/rtnetlink.h> 571da177e4SLinus Torvalds #include <linux/init.h> 58716ea3a7SDavid Howells #include <linux/scatterlist.h> 59ac45f602SPatrick Ohly #include <linux/errqueue.h> 60268bb0ceSLinus Torvalds #include <linux/prefetch.h> 610d5501c1SVlad Yasevich #include <linux/if_vlan.h> 622a2ea508SJohn Hurley #include <linux/mpls.h> 63183f47fcSSebastian Andrzej Siewior #include <linux/kcov.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #include <net/protocol.h> 661da177e4SLinus Torvalds #include <net/dst.h> 671da177e4SLinus Torvalds #include <net/sock.h> 681da177e4SLinus Torvalds #include <net/checksum.h> 69ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 701da177e4SLinus Torvalds #include <net/xfrm.h> 718822e270SJohn Hurley #include <net/mpls.h> 723ee17bc7SMat Martineau #include <net/mptcp.h> 736a5bcd84SIlias Apalodimas #include <net/page_pool.h> 741da177e4SLinus Torvalds 757c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 76ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7751c56b00SEric Dumazet #include <linux/highmem.h> 78b245be1fSWillem de Bruijn #include <linux/capability.h> 79b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 802544af03SMatteo Croce #include <linux/indirect_call_wrapper.h> 81a1f8e7f7SAl Viro 827b7ed885SBart Van Assche #include "datagram.h" 837b7ed885SBart Van Assche 8408009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init; 8508009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 86df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 87df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 88df5042f4SFlorian Westphal #endif 895f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 905f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /** 93f05de73bSJean Sacren * skb_panic - private function for out-of-line support 941da177e4SLinus Torvalds * @skb: buffer 951da177e4SLinus Torvalds * @sz: size 96f05de73bSJean Sacren * @addr: address 9799d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 981da177e4SLinus Torvalds * 99f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 100f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 101f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 102f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 1031da177e4SLinus Torvalds */ 104f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 10599d5851eSJames Hogan const char msg[]) 1061da177e4SLinus Torvalds { 10741a46913SJesper Dangaard Brouer pr_emerg("%s: text:%px len:%d put:%d head:%px data:%px tail:%#lx end:%#lx dev:%s\n", 10899d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1094305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 11026095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1111da177e4SLinus Torvalds BUG(); 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 114f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1151da177e4SLinus Torvalds { 116f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 119f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 120f05de73bSJean Sacren { 121f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 122f05de73bSJean Sacren } 123c93bdd0eSMel Gorman 12450fad4b5SAlexander Lobakin #define NAPI_SKB_CACHE_SIZE 64 125f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_BULK 16 126f450d539SAlexander Lobakin #define NAPI_SKB_CACHE_HALF (NAPI_SKB_CACHE_SIZE / 2) 12750fad4b5SAlexander Lobakin 12850fad4b5SAlexander Lobakin struct napi_alloc_cache { 12950fad4b5SAlexander Lobakin struct page_frag_cache page; 13050fad4b5SAlexander Lobakin unsigned int skb_count; 13150fad4b5SAlexander Lobakin void *skb_cache[NAPI_SKB_CACHE_SIZE]; 13250fad4b5SAlexander Lobakin }; 13350fad4b5SAlexander Lobakin 13450fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 13550fad4b5SAlexander Lobakin static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 13650fad4b5SAlexander Lobakin 13750fad4b5SAlexander Lobakin static void *__alloc_frag_align(unsigned int fragsz, gfp_t gfp_mask, 13850fad4b5SAlexander Lobakin unsigned int align_mask) 13950fad4b5SAlexander Lobakin { 14050fad4b5SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 14150fad4b5SAlexander Lobakin 14250fad4b5SAlexander Lobakin return page_frag_alloc_align(&nc->page, fragsz, gfp_mask, align_mask); 14350fad4b5SAlexander Lobakin } 14450fad4b5SAlexander Lobakin 14550fad4b5SAlexander Lobakin void *__napi_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 14650fad4b5SAlexander Lobakin { 14750fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 14850fad4b5SAlexander Lobakin 14950fad4b5SAlexander Lobakin return __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask); 15050fad4b5SAlexander Lobakin } 15150fad4b5SAlexander Lobakin EXPORT_SYMBOL(__napi_alloc_frag_align); 15250fad4b5SAlexander Lobakin 15350fad4b5SAlexander Lobakin void *__netdev_alloc_frag_align(unsigned int fragsz, unsigned int align_mask) 15450fad4b5SAlexander Lobakin { 15550fad4b5SAlexander Lobakin struct page_frag_cache *nc; 15650fad4b5SAlexander Lobakin void *data; 15750fad4b5SAlexander Lobakin 15850fad4b5SAlexander Lobakin fragsz = SKB_DATA_ALIGN(fragsz); 15950fad4b5SAlexander Lobakin if (in_irq() || irqs_disabled()) { 16050fad4b5SAlexander Lobakin nc = this_cpu_ptr(&netdev_alloc_cache); 16150fad4b5SAlexander Lobakin data = page_frag_alloc_align(nc, fragsz, GFP_ATOMIC, align_mask); 16250fad4b5SAlexander Lobakin } else { 16350fad4b5SAlexander Lobakin local_bh_disable(); 16450fad4b5SAlexander Lobakin data = __alloc_frag_align(fragsz, GFP_ATOMIC, align_mask); 16550fad4b5SAlexander Lobakin local_bh_enable(); 16650fad4b5SAlexander Lobakin } 16750fad4b5SAlexander Lobakin return data; 16850fad4b5SAlexander Lobakin } 16950fad4b5SAlexander Lobakin EXPORT_SYMBOL(__netdev_alloc_frag_align); 17050fad4b5SAlexander Lobakin 171f450d539SAlexander Lobakin static struct sk_buff *napi_skb_cache_get(void) 172f450d539SAlexander Lobakin { 173f450d539SAlexander Lobakin struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 174f450d539SAlexander Lobakin struct sk_buff *skb; 175f450d539SAlexander Lobakin 176f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 177f450d539SAlexander Lobakin nc->skb_count = kmem_cache_alloc_bulk(skbuff_head_cache, 178f450d539SAlexander Lobakin GFP_ATOMIC, 179f450d539SAlexander Lobakin NAPI_SKB_CACHE_BULK, 180f450d539SAlexander Lobakin nc->skb_cache); 181f450d539SAlexander Lobakin if (unlikely(!nc->skb_count)) 182f450d539SAlexander Lobakin return NULL; 183f450d539SAlexander Lobakin 184f450d539SAlexander Lobakin skb = nc->skb_cache[--nc->skb_count]; 185f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, skb); 186f450d539SAlexander Lobakin 187f450d539SAlexander Lobakin return skb; 188f450d539SAlexander Lobakin } 189f450d539SAlexander Lobakin 190ba0509b6SJesper Dangaard Brouer /* Caller must provide SKB that is memset cleared */ 191483126b3SAlexander Lobakin static void __build_skb_around(struct sk_buff *skb, void *data, 192483126b3SAlexander Lobakin unsigned int frag_size) 193ba0509b6SJesper Dangaard Brouer { 194ba0509b6SJesper Dangaard Brouer struct skb_shared_info *shinfo; 195ba0509b6SJesper Dangaard Brouer unsigned int size = frag_size ? : ksize(data); 196ba0509b6SJesper Dangaard Brouer 197ba0509b6SJesper Dangaard Brouer size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 198ba0509b6SJesper Dangaard Brouer 199ba0509b6SJesper Dangaard Brouer /* Assumes caller memset cleared SKB */ 200ba0509b6SJesper Dangaard Brouer skb->truesize = SKB_TRUESIZE(size); 201ba0509b6SJesper Dangaard Brouer refcount_set(&skb->users, 1); 202ba0509b6SJesper Dangaard Brouer skb->head = data; 203ba0509b6SJesper Dangaard Brouer skb->data = data; 204ba0509b6SJesper Dangaard Brouer skb_reset_tail_pointer(skb); 205ba0509b6SJesper Dangaard Brouer skb->end = skb->tail + size; 206ba0509b6SJesper Dangaard Brouer skb->mac_header = (typeof(skb->mac_header))~0U; 207ba0509b6SJesper Dangaard Brouer skb->transport_header = (typeof(skb->transport_header))~0U; 208ba0509b6SJesper Dangaard Brouer 209ba0509b6SJesper Dangaard Brouer /* make sure we initialize shinfo sequentially */ 210ba0509b6SJesper Dangaard Brouer shinfo = skb_shinfo(skb); 211ba0509b6SJesper Dangaard Brouer memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 212ba0509b6SJesper Dangaard Brouer atomic_set(&shinfo->dataref, 1); 213ba0509b6SJesper Dangaard Brouer 2146370cc3bSAleksandr Nogikh skb_set_kcov_handle(skb, kcov_common_handle()); 215ba0509b6SJesper Dangaard Brouer } 216ba0509b6SJesper Dangaard Brouer 2171da177e4SLinus Torvalds /** 2182ea2f62cSEric Dumazet * __build_skb - build a network buffer 219b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2202ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 221b2b5ce9dSEric Dumazet * 222b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 223deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 2242ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 2252ea2f62cSEric Dumazet * or vmalloc() 226b2b5ce9dSEric Dumazet * The return is the new skb buffer. 227b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 228b2b5ce9dSEric Dumazet * Notes : 229b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 230b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 231b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 232b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 233b2b5ce9dSEric Dumazet * before giving packet to stack. 234b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 235b2b5ce9dSEric Dumazet */ 2362ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 237b2b5ce9dSEric Dumazet { 238b2b5ce9dSEric Dumazet struct sk_buff *skb; 239b2b5ce9dSEric Dumazet 240b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 241ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 242b2b5ce9dSEric Dumazet return NULL; 243b2b5ce9dSEric Dumazet 244b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 245483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 246b2b5ce9dSEric Dumazet 247483126b3SAlexander Lobakin return skb; 248b2b5ce9dSEric Dumazet } 2492ea2f62cSEric Dumazet 2502ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 2512ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 2522ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 2532ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 2542ea2f62cSEric Dumazet */ 2552ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 2562ea2f62cSEric Dumazet { 2572ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 2582ea2f62cSEric Dumazet 2592ea2f62cSEric Dumazet if (skb && frag_size) { 2602ea2f62cSEric Dumazet skb->head_frag = 1; 2612f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 2622ea2f62cSEric Dumazet skb->pfmemalloc = 1; 2632ea2f62cSEric Dumazet } 2642ea2f62cSEric Dumazet return skb; 2652ea2f62cSEric Dumazet } 266b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 267b2b5ce9dSEric Dumazet 268ba0509b6SJesper Dangaard Brouer /** 269ba0509b6SJesper Dangaard Brouer * build_skb_around - build a network buffer around provided skb 270ba0509b6SJesper Dangaard Brouer * @skb: sk_buff provide by caller, must be memset cleared 271ba0509b6SJesper Dangaard Brouer * @data: data buffer provided by caller 272ba0509b6SJesper Dangaard Brouer * @frag_size: size of data, or 0 if head was kmalloced 273ba0509b6SJesper Dangaard Brouer */ 274ba0509b6SJesper Dangaard Brouer struct sk_buff *build_skb_around(struct sk_buff *skb, 275ba0509b6SJesper Dangaard Brouer void *data, unsigned int frag_size) 276ba0509b6SJesper Dangaard Brouer { 277ba0509b6SJesper Dangaard Brouer if (unlikely(!skb)) 278ba0509b6SJesper Dangaard Brouer return NULL; 279ba0509b6SJesper Dangaard Brouer 280483126b3SAlexander Lobakin __build_skb_around(skb, data, frag_size); 281ba0509b6SJesper Dangaard Brouer 282483126b3SAlexander Lobakin if (frag_size) { 283ba0509b6SJesper Dangaard Brouer skb->head_frag = 1; 284ba0509b6SJesper Dangaard Brouer if (page_is_pfmemalloc(virt_to_head_page(data))) 285ba0509b6SJesper Dangaard Brouer skb->pfmemalloc = 1; 286ba0509b6SJesper Dangaard Brouer } 287ba0509b6SJesper Dangaard Brouer return skb; 288ba0509b6SJesper Dangaard Brouer } 289ba0509b6SJesper Dangaard Brouer EXPORT_SYMBOL(build_skb_around); 290ba0509b6SJesper Dangaard Brouer 291f450d539SAlexander Lobakin /** 292f450d539SAlexander Lobakin * __napi_build_skb - build a network buffer 293f450d539SAlexander Lobakin * @data: data buffer provided by caller 294f450d539SAlexander Lobakin * @frag_size: size of data, or 0 if head was kmalloced 295f450d539SAlexander Lobakin * 296f450d539SAlexander Lobakin * Version of __build_skb() that uses NAPI percpu caches to obtain 297f450d539SAlexander Lobakin * skbuff_head instead of inplace allocation. 298f450d539SAlexander Lobakin * 299f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 300f450d539SAlexander Lobakin */ 301f450d539SAlexander Lobakin static struct sk_buff *__napi_build_skb(void *data, unsigned int frag_size) 302f450d539SAlexander Lobakin { 303f450d539SAlexander Lobakin struct sk_buff *skb; 304f450d539SAlexander Lobakin 305f450d539SAlexander Lobakin skb = napi_skb_cache_get(); 306f450d539SAlexander Lobakin if (unlikely(!skb)) 307f450d539SAlexander Lobakin return NULL; 308f450d539SAlexander Lobakin 309f450d539SAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 310f450d539SAlexander Lobakin __build_skb_around(skb, data, frag_size); 311f450d539SAlexander Lobakin 312f450d539SAlexander Lobakin return skb; 313f450d539SAlexander Lobakin } 314f450d539SAlexander Lobakin 315f450d539SAlexander Lobakin /** 316f450d539SAlexander Lobakin * napi_build_skb - build a network buffer 317f450d539SAlexander Lobakin * @data: data buffer provided by caller 318f450d539SAlexander Lobakin * @frag_size: size of data, or 0 if head was kmalloced 319f450d539SAlexander Lobakin * 320f450d539SAlexander Lobakin * Version of __napi_build_skb() that takes care of skb->head_frag 321f450d539SAlexander Lobakin * and skb->pfmemalloc when the data is a page or page fragment. 322f450d539SAlexander Lobakin * 323f450d539SAlexander Lobakin * Returns a new &sk_buff on success, %NULL on allocation failure. 324f450d539SAlexander Lobakin */ 325f450d539SAlexander Lobakin struct sk_buff *napi_build_skb(void *data, unsigned int frag_size) 326f450d539SAlexander Lobakin { 327f450d539SAlexander Lobakin struct sk_buff *skb = __napi_build_skb(data, frag_size); 328f450d539SAlexander Lobakin 329f450d539SAlexander Lobakin if (likely(skb) && frag_size) { 330f450d539SAlexander Lobakin skb->head_frag = 1; 331f450d539SAlexander Lobakin skb_propagate_pfmemalloc(virt_to_head_page(data), skb); 332f450d539SAlexander Lobakin } 333f450d539SAlexander Lobakin 334f450d539SAlexander Lobakin return skb; 335f450d539SAlexander Lobakin } 336f450d539SAlexander Lobakin EXPORT_SYMBOL(napi_build_skb); 337f450d539SAlexander Lobakin 3385381b23dSAlexander Lobakin /* 3395381b23dSAlexander Lobakin * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 3405381b23dSAlexander Lobakin * the caller if emergency pfmemalloc reserves are being used. If it is and 3415381b23dSAlexander Lobakin * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 3425381b23dSAlexander Lobakin * may be used. Otherwise, the packet data may be discarded until enough 3435381b23dSAlexander Lobakin * memory is free 3445381b23dSAlexander Lobakin */ 345ef28095fSAlexander Lobakin static void *kmalloc_reserve(size_t size, gfp_t flags, int node, 346ef28095fSAlexander Lobakin bool *pfmemalloc) 3475381b23dSAlexander Lobakin { 3485381b23dSAlexander Lobakin void *obj; 3495381b23dSAlexander Lobakin bool ret_pfmemalloc = false; 3505381b23dSAlexander Lobakin 3515381b23dSAlexander Lobakin /* 3525381b23dSAlexander Lobakin * Try a regular allocation, when that fails and we're not entitled 3535381b23dSAlexander Lobakin * to the reserves, fail. 3545381b23dSAlexander Lobakin */ 3555381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, 3565381b23dSAlexander Lobakin flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 3575381b23dSAlexander Lobakin node); 3585381b23dSAlexander Lobakin if (obj || !(gfp_pfmemalloc_allowed(flags))) 3595381b23dSAlexander Lobakin goto out; 3605381b23dSAlexander Lobakin 3615381b23dSAlexander Lobakin /* Try again but now we are using pfmemalloc reserves */ 3625381b23dSAlexander Lobakin ret_pfmemalloc = true; 3635381b23dSAlexander Lobakin obj = kmalloc_node_track_caller(size, flags, node); 3645381b23dSAlexander Lobakin 3655381b23dSAlexander Lobakin out: 3665381b23dSAlexander Lobakin if (pfmemalloc) 3675381b23dSAlexander Lobakin *pfmemalloc = ret_pfmemalloc; 3685381b23dSAlexander Lobakin 3695381b23dSAlexander Lobakin return obj; 3705381b23dSAlexander Lobakin } 3715381b23dSAlexander Lobakin 3725381b23dSAlexander Lobakin /* Allocate a new skbuff. We do this ourselves so we can fill in a few 3735381b23dSAlexander Lobakin * 'private' fields and also do memory statistics to find all the 3745381b23dSAlexander Lobakin * [BEEP] leaks. 3755381b23dSAlexander Lobakin * 3765381b23dSAlexander Lobakin */ 3775381b23dSAlexander Lobakin 3785381b23dSAlexander Lobakin /** 3795381b23dSAlexander Lobakin * __alloc_skb - allocate a network buffer 3805381b23dSAlexander Lobakin * @size: size to allocate 3815381b23dSAlexander Lobakin * @gfp_mask: allocation mask 3825381b23dSAlexander Lobakin * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 3835381b23dSAlexander Lobakin * instead of head cache and allocate a cloned (child) skb. 3845381b23dSAlexander Lobakin * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 3855381b23dSAlexander Lobakin * allocations in case the data is required for writeback 3865381b23dSAlexander Lobakin * @node: numa node to allocate memory on 3875381b23dSAlexander Lobakin * 3885381b23dSAlexander Lobakin * Allocate a new &sk_buff. The returned buffer has no headroom and a 3895381b23dSAlexander Lobakin * tail room of at least size bytes. The object has a reference count 3905381b23dSAlexander Lobakin * of one. The return is the buffer. On a failure the return is %NULL. 3915381b23dSAlexander Lobakin * 3925381b23dSAlexander Lobakin * Buffers may only be allocated from interrupts using a @gfp_mask of 3935381b23dSAlexander Lobakin * %GFP_ATOMIC. 3945381b23dSAlexander Lobakin */ 3955381b23dSAlexander Lobakin struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 3965381b23dSAlexander Lobakin int flags, int node) 3975381b23dSAlexander Lobakin { 3985381b23dSAlexander Lobakin struct kmem_cache *cache; 3995381b23dSAlexander Lobakin struct sk_buff *skb; 4005381b23dSAlexander Lobakin u8 *data; 4015381b23dSAlexander Lobakin bool pfmemalloc; 4025381b23dSAlexander Lobakin 4035381b23dSAlexander Lobakin cache = (flags & SKB_ALLOC_FCLONE) 4045381b23dSAlexander Lobakin ? skbuff_fclone_cache : skbuff_head_cache; 4055381b23dSAlexander Lobakin 4065381b23dSAlexander Lobakin if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 4075381b23dSAlexander Lobakin gfp_mask |= __GFP_MEMALLOC; 4085381b23dSAlexander Lobakin 4095381b23dSAlexander Lobakin /* Get the HEAD */ 410d13612b5SAlexander Lobakin if ((flags & (SKB_ALLOC_FCLONE | SKB_ALLOC_NAPI)) == SKB_ALLOC_NAPI && 411d13612b5SAlexander Lobakin likely(node == NUMA_NO_NODE || node == numa_mem_id())) 412d13612b5SAlexander Lobakin skb = napi_skb_cache_get(); 413d13612b5SAlexander Lobakin else 414d13612b5SAlexander Lobakin skb = kmem_cache_alloc_node(cache, gfp_mask & ~GFP_DMA, node); 415df1ae022SAlexander Lobakin if (unlikely(!skb)) 416df1ae022SAlexander Lobakin return NULL; 4175381b23dSAlexander Lobakin prefetchw(skb); 4185381b23dSAlexander Lobakin 4195381b23dSAlexander Lobakin /* We do our best to align skb_shared_info on a separate cache 4205381b23dSAlexander Lobakin * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 4215381b23dSAlexander Lobakin * aligned memory blocks, unless SLUB/SLAB debug is enabled. 4225381b23dSAlexander Lobakin * Both skb->head and skb_shared_info are cache line aligned. 4235381b23dSAlexander Lobakin */ 4245381b23dSAlexander Lobakin size = SKB_DATA_ALIGN(size); 4255381b23dSAlexander Lobakin size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4265381b23dSAlexander Lobakin data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 427df1ae022SAlexander Lobakin if (unlikely(!data)) 4285381b23dSAlexander Lobakin goto nodata; 4295381b23dSAlexander Lobakin /* kmalloc(size) might give us more room than requested. 4305381b23dSAlexander Lobakin * Put skb_shared_info exactly at the end of allocated zone, 4315381b23dSAlexander Lobakin * to allow max possible filling before reallocation. 4325381b23dSAlexander Lobakin */ 4335381b23dSAlexander Lobakin size = SKB_WITH_OVERHEAD(ksize(data)); 4345381b23dSAlexander Lobakin prefetchw(data + size); 4355381b23dSAlexander Lobakin 4365381b23dSAlexander Lobakin /* 4375381b23dSAlexander Lobakin * Only clear those fields we need to clear, not those that we will 4385381b23dSAlexander Lobakin * actually initialise below. Hence, don't put any more fields after 4395381b23dSAlexander Lobakin * the tail pointer in struct sk_buff! 4405381b23dSAlexander Lobakin */ 4415381b23dSAlexander Lobakin memset(skb, 0, offsetof(struct sk_buff, tail)); 442f9d6725bSAlexander Lobakin __build_skb_around(skb, data, 0); 4435381b23dSAlexander Lobakin skb->pfmemalloc = pfmemalloc; 4445381b23dSAlexander Lobakin 4455381b23dSAlexander Lobakin if (flags & SKB_ALLOC_FCLONE) { 4465381b23dSAlexander Lobakin struct sk_buff_fclones *fclones; 4475381b23dSAlexander Lobakin 4485381b23dSAlexander Lobakin fclones = container_of(skb, struct sk_buff_fclones, skb1); 4495381b23dSAlexander Lobakin 4505381b23dSAlexander Lobakin skb->fclone = SKB_FCLONE_ORIG; 4515381b23dSAlexander Lobakin refcount_set(&fclones->fclone_ref, 1); 4525381b23dSAlexander Lobakin 4535381b23dSAlexander Lobakin fclones->skb2.fclone = SKB_FCLONE_CLONE; 4545381b23dSAlexander Lobakin } 4555381b23dSAlexander Lobakin 4565381b23dSAlexander Lobakin return skb; 457df1ae022SAlexander Lobakin 4585381b23dSAlexander Lobakin nodata: 4595381b23dSAlexander Lobakin kmem_cache_free(cache, skb); 460df1ae022SAlexander Lobakin return NULL; 4615381b23dSAlexander Lobakin } 4625381b23dSAlexander Lobakin EXPORT_SYMBOL(__alloc_skb); 4635381b23dSAlexander Lobakin 4647ba7aeabSSebastian Andrzej Siewior /** 465fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 466fd11a83dSAlexander Duyck * @dev: network device to receive on 467d7499160SMasanari Iida * @len: length to allocate 468fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 469fd11a83dSAlexander Duyck * 470fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 471fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 472fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 473fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 474fd11a83dSAlexander Duyck * 475fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 476fd11a83dSAlexander Duyck */ 4779451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 4789451980aSAlexander Duyck gfp_t gfp_mask) 479fd11a83dSAlexander Duyck { 480b63ae8caSAlexander Duyck struct page_frag_cache *nc; 481fd11a83dSAlexander Duyck struct sk_buff *skb; 4829451980aSAlexander Duyck bool pfmemalloc; 4839451980aSAlexander Duyck void *data; 484fd11a83dSAlexander Duyck 4859451980aSAlexander Duyck len += NET_SKB_PAD; 486fd11a83dSAlexander Duyck 48766c55602SAlexander Lobakin /* If requested length is either too small or too big, 48866c55602SAlexander Lobakin * we use kmalloc() for skb->head allocation. 48966c55602SAlexander Lobakin */ 49066c55602SAlexander Lobakin if (len <= SKB_WITH_OVERHEAD(1024) || 49166c55602SAlexander Lobakin len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 492d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 493a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 494a080e7bdSAlexander Duyck if (!skb) 495a080e7bdSAlexander Duyck goto skb_fail; 496a080e7bdSAlexander Duyck goto skb_success; 497a080e7bdSAlexander Duyck } 4989451980aSAlexander Duyck 4999451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5009451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5019451980aSAlexander Duyck 5029451980aSAlexander Duyck if (sk_memalloc_socks()) 5039451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5049451980aSAlexander Duyck 50592dcabd7SSebastian Andrzej Siewior if (in_irq() || irqs_disabled()) { 5069451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 5078c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 5089451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 50992dcabd7SSebastian Andrzej Siewior } else { 51092dcabd7SSebastian Andrzej Siewior local_bh_disable(); 51192dcabd7SSebastian Andrzej Siewior nc = this_cpu_ptr(&napi_alloc_cache.page); 51292dcabd7SSebastian Andrzej Siewior data = page_frag_alloc(nc, len, gfp_mask); 51392dcabd7SSebastian Andrzej Siewior pfmemalloc = nc->pfmemalloc; 51492dcabd7SSebastian Andrzej Siewior local_bh_enable(); 51592dcabd7SSebastian Andrzej Siewior } 5169451980aSAlexander Duyck 5179451980aSAlexander Duyck if (unlikely(!data)) 5189451980aSAlexander Duyck return NULL; 5199451980aSAlexander Duyck 5209451980aSAlexander Duyck skb = __build_skb(data, len); 5219451980aSAlexander Duyck if (unlikely(!skb)) { 522181edb2bSAlexander Duyck skb_free_frag(data); 5239451980aSAlexander Duyck return NULL; 5249451980aSAlexander Duyck } 5259451980aSAlexander Duyck 5269451980aSAlexander Duyck if (pfmemalloc) 5279451980aSAlexander Duyck skb->pfmemalloc = 1; 5289451980aSAlexander Duyck skb->head_frag = 1; 5299451980aSAlexander Duyck 530a080e7bdSAlexander Duyck skb_success: 5318af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 5327b2e497aSChristoph Hellwig skb->dev = dev; 533fd11a83dSAlexander Duyck 534a080e7bdSAlexander Duyck skb_fail: 5358af27456SChristoph Hellwig return skb; 5368af27456SChristoph Hellwig } 537b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 5381da177e4SLinus Torvalds 539fd11a83dSAlexander Duyck /** 540fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 541fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 542d7499160SMasanari Iida * @len: length to allocate 543fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 544fd11a83dSAlexander Duyck * 545fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 546fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 547fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 548fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 549fd11a83dSAlexander Duyck * 550fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 551fd11a83dSAlexander Duyck */ 5529451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 5539451980aSAlexander Duyck gfp_t gfp_mask) 554fd11a83dSAlexander Duyck { 5553226b158SEric Dumazet struct napi_alloc_cache *nc; 556fd11a83dSAlexander Duyck struct sk_buff *skb; 5579451980aSAlexander Duyck void *data; 558fd11a83dSAlexander Duyck 5599451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 560fd11a83dSAlexander Duyck 5613226b158SEric Dumazet /* If requested length is either too small or too big, 5623226b158SEric Dumazet * we use kmalloc() for skb->head allocation. 5633226b158SEric Dumazet */ 5643226b158SEric Dumazet if (len <= SKB_WITH_OVERHEAD(1024) || 5653226b158SEric Dumazet len > SKB_WITH_OVERHEAD(PAGE_SIZE) || 566d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 567cfb8ec65SAlexander Lobakin skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX | SKB_ALLOC_NAPI, 568cfb8ec65SAlexander Lobakin NUMA_NO_NODE); 569a080e7bdSAlexander Duyck if (!skb) 570a080e7bdSAlexander Duyck goto skb_fail; 571a080e7bdSAlexander Duyck goto skb_success; 572a080e7bdSAlexander Duyck } 5739451980aSAlexander Duyck 5743226b158SEric Dumazet nc = this_cpu_ptr(&napi_alloc_cache); 5759451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5769451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5779451980aSAlexander Duyck 5789451980aSAlexander Duyck if (sk_memalloc_socks()) 5799451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5809451980aSAlexander Duyck 5818c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 5829451980aSAlexander Duyck if (unlikely(!data)) 5839451980aSAlexander Duyck return NULL; 5849451980aSAlexander Duyck 585cfb8ec65SAlexander Lobakin skb = __napi_build_skb(data, len); 5869451980aSAlexander Duyck if (unlikely(!skb)) { 587181edb2bSAlexander Duyck skb_free_frag(data); 5889451980aSAlexander Duyck return NULL; 5899451980aSAlexander Duyck } 5909451980aSAlexander Duyck 591795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 5929451980aSAlexander Duyck skb->pfmemalloc = 1; 5939451980aSAlexander Duyck skb->head_frag = 1; 5949451980aSAlexander Duyck 595a080e7bdSAlexander Duyck skb_success: 596fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 597fd11a83dSAlexander Duyck skb->dev = napi->dev; 598fd11a83dSAlexander Duyck 599a080e7bdSAlexander Duyck skb_fail: 600fd11a83dSAlexander Duyck return skb; 601fd11a83dSAlexander Duyck } 602fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 603fd11a83dSAlexander Duyck 604654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 60550269e19SEric Dumazet int size, unsigned int truesize) 606654bed16SPeter Zijlstra { 607654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 608654bed16SPeter Zijlstra skb->len += size; 609654bed16SPeter Zijlstra skb->data_len += size; 61050269e19SEric Dumazet skb->truesize += truesize; 611654bed16SPeter Zijlstra } 612654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 613654bed16SPeter Zijlstra 614f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 615f8e617e1SJason Wang unsigned int truesize) 616f8e617e1SJason Wang { 617f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 618f8e617e1SJason Wang 619f8e617e1SJason Wang skb_frag_size_add(frag, size); 620f8e617e1SJason Wang skb->len += size; 621f8e617e1SJason Wang skb->data_len += size; 622f8e617e1SJason Wang skb->truesize += truesize; 623f8e617e1SJason Wang } 624f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 625f8e617e1SJason Wang 62627b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 6271da177e4SLinus Torvalds { 628bd8a7036SEric Dumazet kfree_skb_list(*listp); 62927b437c8SHerbert Xu *listp = NULL; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 63227b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 63327b437c8SHerbert Xu { 63427b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 63527b437c8SHerbert Xu } 63627b437c8SHerbert Xu 6371da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds struct sk_buff *list; 6401da177e4SLinus Torvalds 641fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 6421da177e4SLinus Torvalds skb_get(list); 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 645d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 646d3836f21SEric Dumazet { 647181edb2bSAlexander Duyck unsigned char *head = skb->head; 648181edb2bSAlexander Duyck 6496a5bcd84SIlias Apalodimas if (skb->head_frag) { 6506a5bcd84SIlias Apalodimas if (skb_pp_recycle(skb, head)) 6516a5bcd84SIlias Apalodimas return; 652181edb2bSAlexander Duyck skb_free_frag(head); 6536a5bcd84SIlias Apalodimas } else { 654181edb2bSAlexander Duyck kfree(head); 655d3836f21SEric Dumazet } 6566a5bcd84SIlias Apalodimas } 657d3836f21SEric Dumazet 6585bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 6591da177e4SLinus Torvalds { 660ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 6611da177e4SLinus Torvalds int i; 662ff04a771SEric Dumazet 663ff04a771SEric Dumazet if (skb->cloned && 664ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 665ff04a771SEric Dumazet &shinfo->dataref)) 6662cc3aeb5SIlias Apalodimas goto exit; 667ff04a771SEric Dumazet 66870c43167SJonathan Lemon skb_zcopy_clear(skb, true); 66970c43167SJonathan Lemon 670ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 6716a5bcd84SIlias Apalodimas __skb_frag_unref(&shinfo->frags[i], skb->pp_recycle); 6721da177e4SLinus Torvalds 673ff04a771SEric Dumazet if (shinfo->frag_list) 674ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6751da177e4SLinus Torvalds 676d3836f21SEric Dumazet skb_free_head(skb); 6772cc3aeb5SIlias Apalodimas exit: 6782cc3aeb5SIlias Apalodimas /* When we clone an SKB we copy the reycling bit. The pp_recycle 6792cc3aeb5SIlias Apalodimas * bit is only set on the head though, so in order to avoid races 6802cc3aeb5SIlias Apalodimas * while trying to recycle fragments on __skb_frag_unref() we need 6812cc3aeb5SIlias Apalodimas * to make one SKB responsible for triggering the recycle path. 6822cc3aeb5SIlias Apalodimas * So disable the recycling bit if an SKB is cloned and we have 6832cc3aeb5SIlias Apalodimas * additional references to to the fragmented part of the SKB. 6842cc3aeb5SIlias Apalodimas * Eventually the last SKB will have the recycling bit set and it's 6852cc3aeb5SIlias Apalodimas * dataref set to 0, which will trigger the recycling 6862cc3aeb5SIlias Apalodimas */ 6872cc3aeb5SIlias Apalodimas skb->pp_recycle = 0; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6921da177e4SLinus Torvalds */ 6932d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6941da177e4SLinus Torvalds { 695d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 696d179cd12SDavid S. Miller 697d179cd12SDavid S. Miller switch (skb->fclone) { 698d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6991da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 7006ffe75ebSEric Dumazet return; 701d179cd12SDavid S. Miller 702d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 703d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 7046ffe75ebSEric Dumazet 7056ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 7066ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 7076ffe75ebSEric Dumazet * while here, branch prediction will be good. 7086ffe75ebSEric Dumazet */ 7092638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 7106ffe75ebSEric Dumazet goto fastpath; 711d179cd12SDavid S. Miller break; 712d179cd12SDavid S. Miller 7136ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 714d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 715d179cd12SDavid S. Miller break; 7163ff50b79SStephen Hemminger } 7172638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 7186ffe75ebSEric Dumazet return; 7196ffe75ebSEric Dumazet fastpath: 7206ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 7230a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 7241da177e4SLinus Torvalds { 725adf30907SEric Dumazet skb_dst_drop(skb); 7261da177e4SLinus Torvalds if (skb->destructor) { 7279c2b3328SStephen Hemminger WARN_ON(in_irq()); 7281da177e4SLinus Torvalds skb->destructor(skb); 7291da177e4SLinus Torvalds } 730a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 731cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 7322fc72c7bSKOVACS Krisztian #endif 733df5042f4SFlorian Westphal skb_ext_put(skb); 73404a4bb55SLennert Buytenhek } 73504a4bb55SLennert Buytenhek 73604a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 73704a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 73804a4bb55SLennert Buytenhek { 73904a4bb55SLennert Buytenhek skb_release_head_state(skb); 740a28b1b90SFlorian Westphal if (likely(skb->head)) 7412d4baff8SHerbert Xu skb_release_data(skb); 7422d4baff8SHerbert Xu } 7431da177e4SLinus Torvalds 7442d4baff8SHerbert Xu /** 7452d4baff8SHerbert Xu * __kfree_skb - private function 7462d4baff8SHerbert Xu * @skb: buffer 7472d4baff8SHerbert Xu * 7482d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 7492d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 7502d4baff8SHerbert Xu * always call kfree_skb 7512d4baff8SHerbert Xu */ 7522d4baff8SHerbert Xu 7532d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 7542d4baff8SHerbert Xu { 7552d4baff8SHerbert Xu skb_release_all(skb); 7561da177e4SLinus Torvalds kfree_skbmem(skb); 7571da177e4SLinus Torvalds } 758b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /** 761231d06aeSJörn Engel * kfree_skb - free an sk_buff 762231d06aeSJörn Engel * @skb: buffer to free 763231d06aeSJörn Engel * 764231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 765231d06aeSJörn Engel * hit zero. 766231d06aeSJörn Engel */ 767231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 768231d06aeSJörn Engel { 7693889a803SPaolo Abeni if (!skb_unref(skb)) 770231d06aeSJörn Engel return; 7713889a803SPaolo Abeni 772ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 773231d06aeSJörn Engel __kfree_skb(skb); 774231d06aeSJörn Engel } 775b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 776231d06aeSJörn Engel 777bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 778bd8a7036SEric Dumazet { 779bd8a7036SEric Dumazet while (segs) { 780bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 781bd8a7036SEric Dumazet 782bd8a7036SEric Dumazet kfree_skb(segs); 783bd8a7036SEric Dumazet segs = next; 784bd8a7036SEric Dumazet } 785bd8a7036SEric Dumazet } 786bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 787bd8a7036SEric Dumazet 7886413139dSWillem de Bruijn /* Dump skb information and contents. 7896413139dSWillem de Bruijn * 7906413139dSWillem de Bruijn * Must only be called from net_ratelimit()-ed paths. 7916413139dSWillem de Bruijn * 792302af7c6SVladimir Oltean * Dumps whole packets if full_pkt, only headers otherwise. 7936413139dSWillem de Bruijn */ 7946413139dSWillem de Bruijn void skb_dump(const char *level, const struct sk_buff *skb, bool full_pkt) 7956413139dSWillem de Bruijn { 7966413139dSWillem de Bruijn struct skb_shared_info *sh = skb_shinfo(skb); 7976413139dSWillem de Bruijn struct net_device *dev = skb->dev; 7986413139dSWillem de Bruijn struct sock *sk = skb->sk; 7996413139dSWillem de Bruijn struct sk_buff *list_skb; 8006413139dSWillem de Bruijn bool has_mac, has_trans; 8016413139dSWillem de Bruijn int headroom, tailroom; 8026413139dSWillem de Bruijn int i, len, seg_len; 8036413139dSWillem de Bruijn 8046413139dSWillem de Bruijn if (full_pkt) 8056413139dSWillem de Bruijn len = skb->len; 8066413139dSWillem de Bruijn else 8076413139dSWillem de Bruijn len = min_t(int, skb->len, MAX_HEADER + 128); 8086413139dSWillem de Bruijn 8096413139dSWillem de Bruijn headroom = skb_headroom(skb); 8106413139dSWillem de Bruijn tailroom = skb_tailroom(skb); 8116413139dSWillem de Bruijn 8126413139dSWillem de Bruijn has_mac = skb_mac_header_was_set(skb); 8136413139dSWillem de Bruijn has_trans = skb_transport_header_was_set(skb); 8146413139dSWillem de Bruijn 8156413139dSWillem de Bruijn printk("%sskb len=%u headroom=%u headlen=%u tailroom=%u\n" 8166413139dSWillem de Bruijn "mac=(%d,%d) net=(%d,%d) trans=%d\n" 8176413139dSWillem de Bruijn "shinfo(txflags=%u nr_frags=%u gso(size=%hu type=%u segs=%hu))\n" 8186413139dSWillem de Bruijn "csum(0x%x ip_summed=%u complete_sw=%u valid=%u level=%u)\n" 8196413139dSWillem de Bruijn "hash(0x%x sw=%u l4=%u) proto=0x%04x pkttype=%u iif=%d\n", 8206413139dSWillem de Bruijn level, skb->len, headroom, skb_headlen(skb), tailroom, 8216413139dSWillem de Bruijn has_mac ? skb->mac_header : -1, 8226413139dSWillem de Bruijn has_mac ? skb_mac_header_len(skb) : -1, 8236413139dSWillem de Bruijn skb->network_header, 8246413139dSWillem de Bruijn has_trans ? skb_network_header_len(skb) : -1, 8256413139dSWillem de Bruijn has_trans ? skb->transport_header : -1, 8266413139dSWillem de Bruijn sh->tx_flags, sh->nr_frags, 8276413139dSWillem de Bruijn sh->gso_size, sh->gso_type, sh->gso_segs, 8286413139dSWillem de Bruijn skb->csum, skb->ip_summed, skb->csum_complete_sw, 8296413139dSWillem de Bruijn skb->csum_valid, skb->csum_level, 8306413139dSWillem de Bruijn skb->hash, skb->sw_hash, skb->l4_hash, 8316413139dSWillem de Bruijn ntohs(skb->protocol), skb->pkt_type, skb->skb_iif); 8326413139dSWillem de Bruijn 8336413139dSWillem de Bruijn if (dev) 8346413139dSWillem de Bruijn printk("%sdev name=%s feat=0x%pNF\n", 8356413139dSWillem de Bruijn level, dev->name, &dev->features); 8366413139dSWillem de Bruijn if (sk) 837db8051f3SQian Cai printk("%ssk family=%hu type=%u proto=%u\n", 8386413139dSWillem de Bruijn level, sk->sk_family, sk->sk_type, sk->sk_protocol); 8396413139dSWillem de Bruijn 8406413139dSWillem de Bruijn if (full_pkt && headroom) 8416413139dSWillem de Bruijn print_hex_dump(level, "skb headroom: ", DUMP_PREFIX_OFFSET, 8426413139dSWillem de Bruijn 16, 1, skb->head, headroom, false); 8436413139dSWillem de Bruijn 8446413139dSWillem de Bruijn seg_len = min_t(int, skb_headlen(skb), len); 8456413139dSWillem de Bruijn if (seg_len) 8466413139dSWillem de Bruijn print_hex_dump(level, "skb linear: ", DUMP_PREFIX_OFFSET, 8476413139dSWillem de Bruijn 16, 1, skb->data, seg_len, false); 8486413139dSWillem de Bruijn len -= seg_len; 8496413139dSWillem de Bruijn 8506413139dSWillem de Bruijn if (full_pkt && tailroom) 8516413139dSWillem de Bruijn print_hex_dump(level, "skb tailroom: ", DUMP_PREFIX_OFFSET, 8526413139dSWillem de Bruijn 16, 1, skb_tail_pointer(skb), tailroom, false); 8536413139dSWillem de Bruijn 8546413139dSWillem de Bruijn for (i = 0; len && i < skb_shinfo(skb)->nr_frags; i++) { 8556413139dSWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 8566413139dSWillem de Bruijn u32 p_off, p_len, copied; 8576413139dSWillem de Bruijn struct page *p; 8586413139dSWillem de Bruijn u8 *vaddr; 8596413139dSWillem de Bruijn 860b54c9d5bSJonathan Lemon skb_frag_foreach_page(frag, skb_frag_off(frag), 8616413139dSWillem de Bruijn skb_frag_size(frag), p, p_off, p_len, 8626413139dSWillem de Bruijn copied) { 8636413139dSWillem de Bruijn seg_len = min_t(int, p_len, len); 8646413139dSWillem de Bruijn vaddr = kmap_atomic(p); 8656413139dSWillem de Bruijn print_hex_dump(level, "skb frag: ", 8666413139dSWillem de Bruijn DUMP_PREFIX_OFFSET, 8676413139dSWillem de Bruijn 16, 1, vaddr + p_off, seg_len, false); 8686413139dSWillem de Bruijn kunmap_atomic(vaddr); 8696413139dSWillem de Bruijn len -= seg_len; 8706413139dSWillem de Bruijn if (!len) 8716413139dSWillem de Bruijn break; 8726413139dSWillem de Bruijn } 8736413139dSWillem de Bruijn } 8746413139dSWillem de Bruijn 8756413139dSWillem de Bruijn if (full_pkt && skb_has_frag_list(skb)) { 8766413139dSWillem de Bruijn printk("skb fraglist:\n"); 8776413139dSWillem de Bruijn skb_walk_frags(skb, list_skb) 8786413139dSWillem de Bruijn skb_dump(level, list_skb, true); 8796413139dSWillem de Bruijn } 8806413139dSWillem de Bruijn } 8816413139dSWillem de Bruijn EXPORT_SYMBOL(skb_dump); 8826413139dSWillem de Bruijn 883d1a203eaSStephen Hemminger /** 88425121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 88525121173SMichael S. Tsirkin * @skb: buffer that triggered an error 88625121173SMichael S. Tsirkin * 88725121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 88825121173SMichael S. Tsirkin * skb must be freed afterwards. 88925121173SMichael S. Tsirkin */ 89025121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 89125121173SMichael S. Tsirkin { 8921f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 89325121173SMichael S. Tsirkin } 89425121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 89525121173SMichael S. Tsirkin 896be769db2SHerbert Xu #ifdef CONFIG_TRACEPOINTS 89725121173SMichael S. Tsirkin /** 898ead2ceb0SNeil Horman * consume_skb - free an skbuff 899ead2ceb0SNeil Horman * @skb: buffer to free 900ead2ceb0SNeil Horman * 901ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 902ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 903ead2ceb0SNeil Horman * is being dropped after a failure and notes that 904ead2ceb0SNeil Horman */ 905ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 906ead2ceb0SNeil Horman { 9073889a803SPaolo Abeni if (!skb_unref(skb)) 908ead2ceb0SNeil Horman return; 9093889a803SPaolo Abeni 91007dc22e7SKoki Sanagi trace_consume_skb(skb); 911ead2ceb0SNeil Horman __kfree_skb(skb); 912ead2ceb0SNeil Horman } 913ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 914be769db2SHerbert Xu #endif 915ead2ceb0SNeil Horman 9160a463c78SPaolo Abeni /** 917c1639be9SMauro Carvalho Chehab * __consume_stateless_skb - free an skbuff, assuming it is stateless 9180a463c78SPaolo Abeni * @skb: buffer to free 9190a463c78SPaolo Abeni * 920ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 921ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 9220a463c78SPaolo Abeni */ 923ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 9240a463c78SPaolo Abeni { 9250a463c78SPaolo Abeni trace_consume_skb(skb); 9260a463c78SPaolo Abeni skb_release_data(skb); 9270a463c78SPaolo Abeni kfree_skbmem(skb); 9280a463c78SPaolo Abeni } 9290a463c78SPaolo Abeni 930f450d539SAlexander Lobakin static void napi_skb_cache_put(struct sk_buff *skb) 931795bb1c0SJesper Dangaard Brouer { 932795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 933f450d539SAlexander Lobakin u32 i; 934795bb1c0SJesper Dangaard Brouer 935f450d539SAlexander Lobakin kasan_poison_object_data(skbuff_head_cache, skb); 936795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 937795bb1c0SJesper Dangaard Brouer 938795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 939f450d539SAlexander Lobakin for (i = NAPI_SKB_CACHE_HALF; i < NAPI_SKB_CACHE_SIZE; i++) 940f450d539SAlexander Lobakin kasan_unpoison_object_data(skbuff_head_cache, 941f450d539SAlexander Lobakin nc->skb_cache[i]); 942f450d539SAlexander Lobakin 943f450d539SAlexander Lobakin kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_HALF, 944f450d539SAlexander Lobakin nc->skb_cache + NAPI_SKB_CACHE_HALF); 945f450d539SAlexander Lobakin nc->skb_count = NAPI_SKB_CACHE_HALF; 946795bb1c0SJesper Dangaard Brouer } 947795bb1c0SJesper Dangaard Brouer } 948f450d539SAlexander Lobakin 94915fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 95015fad714SJesper Dangaard Brouer { 9519243adfcSAlexander Lobakin skb_release_all(skb); 9529243adfcSAlexander Lobakin napi_skb_cache_put(skb); 9539243adfcSAlexander Lobakin } 9549243adfcSAlexander Lobakin 9559243adfcSAlexander Lobakin void napi_skb_free_stolen_head(struct sk_buff *skb) 9569243adfcSAlexander Lobakin { 9579efb4b5bSPaolo Abeni if (unlikely(skb->slow_gro)) { 9588550ff8dSPaul Blakey nf_reset_ct(skb); 9599243adfcSAlexander Lobakin skb_dst_drop(skb); 9609243adfcSAlexander Lobakin skb_ext_put(skb); 961*5e10da53SPaolo Abeni skb_orphan(skb); 9629efb4b5bSPaolo Abeni skb->slow_gro = 0; 9639efb4b5bSPaolo Abeni } 964f450d539SAlexander Lobakin napi_skb_cache_put(skb); 96515fad714SJesper Dangaard Brouer } 966795bb1c0SJesper Dangaard Brouer 967795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 968795bb1c0SJesper Dangaard Brouer { 969885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 970795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 971885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 972795bb1c0SJesper Dangaard Brouer return; 973795bb1c0SJesper Dangaard Brouer } 974795bb1c0SJesper Dangaard Brouer 9756454eca8SYunsheng Lin lockdep_assert_in_softirq(); 9766454eca8SYunsheng Lin 9777608894eSPaolo Abeni if (!skb_unref(skb)) 978795bb1c0SJesper Dangaard Brouer return; 9797608894eSPaolo Abeni 980795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 981795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 982795bb1c0SJesper Dangaard Brouer 983795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 984abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 985795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 986795bb1c0SJesper Dangaard Brouer return; 987795bb1c0SJesper Dangaard Brouer } 988795bb1c0SJesper Dangaard Brouer 9899243adfcSAlexander Lobakin skb_release_all(skb); 990f450d539SAlexander Lobakin napi_skb_cache_put(skb); 991795bb1c0SJesper Dangaard Brouer } 992795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 993795bb1c0SJesper Dangaard Brouer 994b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 995b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 996b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 997b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 998b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 999b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 1000b1937227SEric Dumazet 1001dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 1002dec18810SHerbert Xu { 1003dec18810SHerbert Xu new->tstamp = old->tstamp; 1004b1937227SEric Dumazet /* We do not copy old->sk */ 1005dec18810SHerbert Xu new->dev = old->dev; 1006b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 10077fee226aSEric Dumazet skb_dst_copy(new, old); 1008df5042f4SFlorian Westphal __skb_ext_copy(new, old); 1009b1937227SEric Dumazet __nf_copy(new, old, false); 10106aa895b0SPatrick McHardy 1011b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 1012b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 1013b1937227SEric Dumazet */ 1014b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 101506021292SEliezer Tamir 1016b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 1017b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 1018b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 1019b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 1020b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 1021b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 1022b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 1023b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 1024b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 1025b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 1026b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 1027b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 1028b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 1029b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 1030b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 1031b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 1032b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 1033b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 1034b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 1035b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 1036b1937227SEric Dumazet #endif 1037e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 1038b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 103906021292SEliezer Tamir #endif 10402bd82484SEric Dumazet #ifdef CONFIG_XPS 10412bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 10422bd82484SEric Dumazet #endif 1043b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 1044b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 1045b1937227SEric Dumazet #endif 1046b1937227SEric Dumazet 1047dec18810SHerbert Xu } 1048dec18810SHerbert Xu 104982c49a35SHerbert Xu /* 105082c49a35SHerbert Xu * You should not add any new code to this function. Add it to 105182c49a35SHerbert Xu * __copy_skb_header above instead. 105282c49a35SHerbert Xu */ 1053e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 10541da177e4SLinus Torvalds { 10551da177e4SLinus Torvalds #define C(x) n->x = skb->x 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds n->next = n->prev = NULL; 10581da177e4SLinus Torvalds n->sk = NULL; 1059dec18810SHerbert Xu __copy_skb_header(n, skb); 1060dec18810SHerbert Xu 10611da177e4SLinus Torvalds C(len); 10621da177e4SLinus Torvalds C(data_len); 10633e6b3b2eSAlexey Dobriyan C(mac_len); 1064334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 106502f1c89dSPaul Moore n->cloned = 1; 10661da177e4SLinus Torvalds n->nohdr = 0; 1067b13dda9fSEric Dumazet n->peeked = 0; 1068e78bfb07SStefano Brivio C(pfmemalloc); 10696a5bcd84SIlias Apalodimas C(pp_recycle); 10701da177e4SLinus Torvalds n->destructor = NULL; 10711da177e4SLinus Torvalds C(tail); 10721da177e4SLinus Torvalds C(end); 107302f1c89dSPaul Moore C(head); 1074d3836f21SEric Dumazet C(head_frag); 107502f1c89dSPaul Moore C(data); 107602f1c89dSPaul Moore C(truesize); 107763354797SReshetova, Elena refcount_set(&n->users, 1); 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 10801da177e4SLinus Torvalds skb->cloned = 1; 10811da177e4SLinus Torvalds 10821da177e4SLinus Torvalds return n; 1083e0053ec0SHerbert Xu #undef C 1084e0053ec0SHerbert Xu } 1085e0053ec0SHerbert Xu 1086e0053ec0SHerbert Xu /** 1087da29e4b4SJakub Kicinski * alloc_skb_for_msg() - allocate sk_buff to wrap frag list forming a msg 1088da29e4b4SJakub Kicinski * @first: first sk_buff of the msg 1089da29e4b4SJakub Kicinski */ 1090da29e4b4SJakub Kicinski struct sk_buff *alloc_skb_for_msg(struct sk_buff *first) 1091da29e4b4SJakub Kicinski { 1092da29e4b4SJakub Kicinski struct sk_buff *n; 1093da29e4b4SJakub Kicinski 1094da29e4b4SJakub Kicinski n = alloc_skb(0, GFP_ATOMIC); 1095da29e4b4SJakub Kicinski if (!n) 1096da29e4b4SJakub Kicinski return NULL; 1097da29e4b4SJakub Kicinski 1098da29e4b4SJakub Kicinski n->len = first->len; 1099da29e4b4SJakub Kicinski n->data_len = first->len; 1100da29e4b4SJakub Kicinski n->truesize = first->truesize; 1101da29e4b4SJakub Kicinski 1102da29e4b4SJakub Kicinski skb_shinfo(n)->frag_list = first; 1103da29e4b4SJakub Kicinski 1104da29e4b4SJakub Kicinski __copy_skb_header(n, first); 1105da29e4b4SJakub Kicinski n->destructor = NULL; 1106da29e4b4SJakub Kicinski 1107da29e4b4SJakub Kicinski return n; 1108da29e4b4SJakub Kicinski } 1109da29e4b4SJakub Kicinski EXPORT_SYMBOL_GPL(alloc_skb_for_msg); 1110da29e4b4SJakub Kicinski 1111da29e4b4SJakub Kicinski /** 1112e0053ec0SHerbert Xu * skb_morph - morph one skb into another 1113e0053ec0SHerbert Xu * @dst: the skb to receive the contents 1114e0053ec0SHerbert Xu * @src: the skb to supply the contents 1115e0053ec0SHerbert Xu * 1116e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 1117e0053ec0SHerbert Xu * supplied by the user. 1118e0053ec0SHerbert Xu * 1119e0053ec0SHerbert Xu * The target skb is returned upon exit. 1120e0053ec0SHerbert Xu */ 1121e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 1122e0053ec0SHerbert Xu { 11232d4baff8SHerbert Xu skb_release_all(dst); 1124e0053ec0SHerbert Xu return __skb_clone(dst, src); 1125e0053ec0SHerbert Xu } 1126e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 1127e0053ec0SHerbert Xu 11286f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 1129a91dbff5SWillem de Bruijn { 1130a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 1131a91dbff5SWillem de Bruijn struct user_struct *user; 1132a91dbff5SWillem de Bruijn 1133a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 1134a91dbff5SWillem de Bruijn return 0; 1135a91dbff5SWillem de Bruijn 1136a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 1137a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 1138a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 1139a91dbff5SWillem de Bruijn 1140a91dbff5SWillem de Bruijn do { 1141a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 1142a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 1143a91dbff5SWillem de Bruijn if (new_pg > max_pg) 1144a91dbff5SWillem de Bruijn return -ENOBUFS; 1145a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 1146a91dbff5SWillem de Bruijn old_pg); 1147a91dbff5SWillem de Bruijn 1148a91dbff5SWillem de Bruijn if (!mmp->user) { 1149a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 1150a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 1151a91dbff5SWillem de Bruijn } else { 1152a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 1153a91dbff5SWillem de Bruijn } 1154a91dbff5SWillem de Bruijn 1155a91dbff5SWillem de Bruijn return 0; 1156a91dbff5SWillem de Bruijn } 11576f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 1158a91dbff5SWillem de Bruijn 11596f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 1160a91dbff5SWillem de Bruijn { 1161a91dbff5SWillem de Bruijn if (mmp->user) { 1162a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 1163a91dbff5SWillem de Bruijn free_uid(mmp->user); 1164a91dbff5SWillem de Bruijn } 1165a91dbff5SWillem de Bruijn } 11666f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 1167a91dbff5SWillem de Bruijn 11688c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_alloc(struct sock *sk, size_t size) 116952267790SWillem de Bruijn { 117052267790SWillem de Bruijn struct ubuf_info *uarg; 117152267790SWillem de Bruijn struct sk_buff *skb; 117252267790SWillem de Bruijn 117352267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 117452267790SWillem de Bruijn 117552267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 117652267790SWillem de Bruijn if (!skb) 117752267790SWillem de Bruijn return NULL; 117852267790SWillem de Bruijn 117952267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 118052267790SWillem de Bruijn uarg = (void *)skb->cb; 1181a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 1182a91dbff5SWillem de Bruijn 1183a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 1184a91dbff5SWillem de Bruijn kfree_skb(skb); 1185a91dbff5SWillem de Bruijn return NULL; 1186a91dbff5SWillem de Bruijn } 118752267790SWillem de Bruijn 11888c793822SJonathan Lemon uarg->callback = msg_zerocopy_callback; 11894ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 11904ab6c99dSWillem de Bruijn uarg->len = 1; 11914ab6c99dSWillem de Bruijn uarg->bytelen = size; 119252267790SWillem de Bruijn uarg->zerocopy = 1; 119304c2d33eSJonathan Lemon uarg->flags = SKBFL_ZEROCOPY_FRAG; 1194c1d1b437SEric Dumazet refcount_set(&uarg->refcnt, 1); 119552267790SWillem de Bruijn sock_hold(sk); 119652267790SWillem de Bruijn 119752267790SWillem de Bruijn return uarg; 119852267790SWillem de Bruijn } 11998c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_alloc); 120052267790SWillem de Bruijn 120152267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 120252267790SWillem de Bruijn { 120352267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 120452267790SWillem de Bruijn } 120552267790SWillem de Bruijn 12068c793822SJonathan Lemon struct ubuf_info *msg_zerocopy_realloc(struct sock *sk, size_t size, 12074ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 12084ab6c99dSWillem de Bruijn { 12094ab6c99dSWillem de Bruijn if (uarg) { 12104ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 12114ab6c99dSWillem de Bruijn u32 bytelen, next; 12124ab6c99dSWillem de Bruijn 12134ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 12144ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 12154ab6c99dSWillem de Bruijn */ 12164ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 12174ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 12184ab6c99dSWillem de Bruijn return NULL; 12194ab6c99dSWillem de Bruijn } 12204ab6c99dSWillem de Bruijn 12214ab6c99dSWillem de Bruijn bytelen = uarg->bytelen + size; 12224ab6c99dSWillem de Bruijn if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 12234ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 12244ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 12254ab6c99dSWillem de Bruijn goto new_alloc; 12264ab6c99dSWillem de Bruijn return NULL; 12274ab6c99dSWillem de Bruijn } 12284ab6c99dSWillem de Bruijn 12294ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 12304ab6c99dSWillem de Bruijn if ((u32)(uarg->id + uarg->len) == next) { 1231a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) 1232a91dbff5SWillem de Bruijn return NULL; 12334ab6c99dSWillem de Bruijn uarg->len++; 12344ab6c99dSWillem de Bruijn uarg->bytelen = bytelen; 12354ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1236100f6d8eSWillem de Bruijn 1237100f6d8eSWillem de Bruijn /* no extra ref when appending to datagram (MSG_MORE) */ 1238100f6d8eSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 12398e044917SJonathan Lemon net_zcopy_get(uarg); 1240100f6d8eSWillem de Bruijn 12414ab6c99dSWillem de Bruijn return uarg; 12424ab6c99dSWillem de Bruijn } 12434ab6c99dSWillem de Bruijn } 12444ab6c99dSWillem de Bruijn 12454ab6c99dSWillem de Bruijn new_alloc: 12468c793822SJonathan Lemon return msg_zerocopy_alloc(sk, size); 12474ab6c99dSWillem de Bruijn } 12488c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_realloc); 12494ab6c99dSWillem de Bruijn 12504ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 12514ab6c99dSWillem de Bruijn { 12524ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 12534ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 12544ab6c99dSWillem de Bruijn u64 sum_len; 12554ab6c99dSWillem de Bruijn 12564ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 12574ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 12584ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 12594ab6c99dSWillem de Bruijn 12604ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 12614ab6c99dSWillem de Bruijn return false; 12624ab6c99dSWillem de Bruijn 12634ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 12644ab6c99dSWillem de Bruijn return false; 12654ab6c99dSWillem de Bruijn 12664ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 12674ab6c99dSWillem de Bruijn return true; 12684ab6c99dSWillem de Bruijn } 12694ab6c99dSWillem de Bruijn 12708c793822SJonathan Lemon static void __msg_zerocopy_callback(struct ubuf_info *uarg) 127152267790SWillem de Bruijn { 12724ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 127352267790SWillem de Bruijn struct sock_exterr_skb *serr; 127452267790SWillem de Bruijn struct sock *sk = skb->sk; 12754ab6c99dSWillem de Bruijn struct sk_buff_head *q; 12764ab6c99dSWillem de Bruijn unsigned long flags; 12773bdd5ee0SWillem de Bruijn bool is_zerocopy; 12784ab6c99dSWillem de Bruijn u32 lo, hi; 12794ab6c99dSWillem de Bruijn u16 len; 128052267790SWillem de Bruijn 1281ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1282ccaffff1SWillem de Bruijn 12834ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 12844ab6c99dSWillem de Bruijn * so do not queue a completion notification 12854ab6c99dSWillem de Bruijn */ 12864ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 128752267790SWillem de Bruijn goto release; 128852267790SWillem de Bruijn 12894ab6c99dSWillem de Bruijn len = uarg->len; 12904ab6c99dSWillem de Bruijn lo = uarg->id; 12914ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 12923bdd5ee0SWillem de Bruijn is_zerocopy = uarg->zerocopy; 12934ab6c99dSWillem de Bruijn 129452267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 129552267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 129652267790SWillem de Bruijn serr->ee.ee_errno = 0; 129752267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 12984ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 12994ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 13003bdd5ee0SWillem de Bruijn if (!is_zerocopy) 130152267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 130252267790SWillem de Bruijn 13034ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 13044ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 13054ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 13064ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 13074ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 13084ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 130952267790SWillem de Bruijn skb = NULL; 13104ab6c99dSWillem de Bruijn } 13114ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 131252267790SWillem de Bruijn 1313e3ae2365SAlexander Aring sk_error_report(sk); 131452267790SWillem de Bruijn 131552267790SWillem de Bruijn release: 131652267790SWillem de Bruijn consume_skb(skb); 131752267790SWillem de Bruijn sock_put(sk); 131852267790SWillem de Bruijn } 131975518851SJonathan Lemon 13208c793822SJonathan Lemon void msg_zerocopy_callback(struct sk_buff *skb, struct ubuf_info *uarg, 132136177832SJonathan Lemon bool success) 132275518851SJonathan Lemon { 132375518851SJonathan Lemon uarg->zerocopy = uarg->zerocopy & success; 132475518851SJonathan Lemon 132575518851SJonathan Lemon if (refcount_dec_and_test(&uarg->refcnt)) 13268c793822SJonathan Lemon __msg_zerocopy_callback(uarg); 132775518851SJonathan Lemon } 13288c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_callback); 132952267790SWillem de Bruijn 13308c793822SJonathan Lemon void msg_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 133152267790SWillem de Bruijn { 133252267790SWillem de Bruijn struct sock *sk = skb_from_uarg(uarg)->sk; 133352267790SWillem de Bruijn 133452267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 13354ab6c99dSWillem de Bruijn uarg->len--; 133652267790SWillem de Bruijn 133752900d22SWillem de Bruijn if (have_uref) 13388c793822SJonathan Lemon msg_zerocopy_callback(NULL, uarg, true); 133952267790SWillem de Bruijn } 13408c793822SJonathan Lemon EXPORT_SYMBOL_GPL(msg_zerocopy_put_abort); 134152267790SWillem de Bruijn 1342b5947e5dSWillem de Bruijn int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len) 1343b5947e5dSWillem de Bruijn { 1344b5947e5dSWillem de Bruijn return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len); 1345b5947e5dSWillem de Bruijn } 1346b5947e5dSWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram); 1347b5947e5dSWillem de Bruijn 134852267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 134952267790SWillem de Bruijn struct msghdr *msg, int len, 135052267790SWillem de Bruijn struct ubuf_info *uarg) 135152267790SWillem de Bruijn { 13524ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 135352267790SWillem de Bruijn struct iov_iter orig_iter = msg->msg_iter; 135452267790SWillem de Bruijn int err, orig_len = skb->len; 135552267790SWillem de Bruijn 13564ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 13574ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 13584ab6c99dSWillem de Bruijn */ 13594ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 13604ab6c99dSWillem de Bruijn return -EEXIST; 13614ab6c99dSWillem de Bruijn 136252267790SWillem de Bruijn err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 136352267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 136454d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 136554d43117SWillem de Bruijn 136652267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 136752267790SWillem de Bruijn msg->msg_iter = orig_iter; 136854d43117SWillem de Bruijn skb->sk = sk; 136952267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 137054d43117SWillem de Bruijn skb->sk = save_sk; 137152267790SWillem de Bruijn return err; 137252267790SWillem de Bruijn } 137352267790SWillem de Bruijn 137452900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 137552267790SWillem de Bruijn return skb->len - orig_len; 137652267790SWillem de Bruijn } 137752267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 137852267790SWillem de Bruijn 13791f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 138052267790SWillem de Bruijn gfp_t gfp_mask) 138152267790SWillem de Bruijn { 138252267790SWillem de Bruijn if (skb_zcopy(orig)) { 138352267790SWillem de Bruijn if (skb_zcopy(nskb)) { 138452267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 138552267790SWillem de Bruijn if (!gfp_mask) { 138652267790SWillem de Bruijn WARN_ON_ONCE(1); 138752267790SWillem de Bruijn return -ENOMEM; 138852267790SWillem de Bruijn } 138952267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 139052267790SWillem de Bruijn return 0; 139152267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 139252267790SWillem de Bruijn return -EIO; 139352267790SWillem de Bruijn } 139452900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 139552267790SWillem de Bruijn } 139652267790SWillem de Bruijn return 0; 139752267790SWillem de Bruijn } 139852267790SWillem de Bruijn 13992c53040fSBen Hutchings /** 14002c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 140148c83012SMichael S. Tsirkin * @skb: the skb to modify 140248c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 140348c83012SMichael S. Tsirkin * 140406b4feb3SJonathan Lemon * This must be called on skb with SKBFL_ZEROCOPY_ENABLE. 140548c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 140648c83012SMichael S. Tsirkin * to userspace pages. 140748c83012SMichael S. Tsirkin * 140848c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 140948c83012SMichael S. Tsirkin * %GFP_ATOMIC. 141048c83012SMichael S. Tsirkin * 141148c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 141248c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 141348c83012SMichael S. Tsirkin */ 141448c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1415a6686f2fSShirley Ma { 1416a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1417a6686f2fSShirley Ma struct page *page, *head = NULL; 14183ece7826SWillem de Bruijn int i, new_frags; 14193ece7826SWillem de Bruijn u32 d_off; 1420a6686f2fSShirley Ma 14213ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 14223ece7826SWillem de Bruijn return -EINVAL; 14233ece7826SWillem de Bruijn 1424f72c4ac6SWillem de Bruijn if (!num_frags) 1425f72c4ac6SWillem de Bruijn goto release; 1426f72c4ac6SWillem de Bruijn 14273ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 14283ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 142902756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1430a6686f2fSShirley Ma if (!page) { 1431a6686f2fSShirley Ma while (head) { 143240dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1433a6686f2fSShirley Ma put_page(head); 1434a6686f2fSShirley Ma head = next; 1435a6686f2fSShirley Ma } 1436a6686f2fSShirley Ma return -ENOMEM; 1437a6686f2fSShirley Ma } 14383ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 14393ece7826SWillem de Bruijn head = page; 14403ece7826SWillem de Bruijn } 14413ece7826SWillem de Bruijn 14423ece7826SWillem de Bruijn page = head; 14433ece7826SWillem de Bruijn d_off = 0; 14443ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 14453ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 14463ece7826SWillem de Bruijn u32 p_off, p_len, copied; 14473ece7826SWillem de Bruijn struct page *p; 14483ece7826SWillem de Bruijn u8 *vaddr; 1449c613c209SWillem de Bruijn 1450b54c9d5bSJonathan Lemon skb_frag_foreach_page(f, skb_frag_off(f), skb_frag_size(f), 1451c613c209SWillem de Bruijn p, p_off, p_len, copied) { 14523ece7826SWillem de Bruijn u32 copy, done = 0; 1453c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 14543ece7826SWillem de Bruijn 14553ece7826SWillem de Bruijn while (done < p_len) { 14563ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 14573ece7826SWillem de Bruijn d_off = 0; 14583ece7826SWillem de Bruijn page = (struct page *)page_private(page); 14593ece7826SWillem de Bruijn } 14603ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 14613ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 14623ece7826SWillem de Bruijn vaddr + p_off + done, copy); 14633ece7826SWillem de Bruijn done += copy; 14643ece7826SWillem de Bruijn d_off += copy; 14653ece7826SWillem de Bruijn } 146651c56b00SEric Dumazet kunmap_atomic(vaddr); 1467c613c209SWillem de Bruijn } 1468a6686f2fSShirley Ma } 1469a6686f2fSShirley Ma 1470a6686f2fSShirley Ma /* skb frags release userspace buffers */ 147102756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1472a8605c60SIan Campbell skb_frag_unref(skb, i); 1473a6686f2fSShirley Ma 1474a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 14753ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 14763ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 147740dadff2SSunghan Suh head = (struct page *)page_private(head); 1478a6686f2fSShirley Ma } 14793ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 14803ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 148148c83012SMichael S. Tsirkin 1482b90ddd56SWillem de Bruijn release: 14831f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1484a6686f2fSShirley Ma return 0; 1485a6686f2fSShirley Ma } 1486dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1487a6686f2fSShirley Ma 1488e0053ec0SHerbert Xu /** 1489e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1490e0053ec0SHerbert Xu * @skb: buffer to clone 1491e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1492e0053ec0SHerbert Xu * 1493e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1494e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1495e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1496e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1497e0053ec0SHerbert Xu * 1498e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1499e0053ec0SHerbert Xu * %GFP_ATOMIC. 1500e0053ec0SHerbert Xu */ 1501e0053ec0SHerbert Xu 1502e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1503e0053ec0SHerbert Xu { 1504d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1505d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1506d0bf4a9eSEric Dumazet skb1); 15076ffe75ebSEric Dumazet struct sk_buff *n; 1508e0053ec0SHerbert Xu 150970008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1510a6686f2fSShirley Ma return NULL; 1511a6686f2fSShirley Ma 1512e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 15132638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 15146ffe75ebSEric Dumazet n = &fclones->skb2; 15152638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1516e0053ec0SHerbert Xu } else { 1517c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1518c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1519c93bdd0eSMel Gorman 1520e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1521e0053ec0SHerbert Xu if (!n) 1522e0053ec0SHerbert Xu return NULL; 1523fe55f6d5SVegard Nossum 1524e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1525e0053ec0SHerbert Xu } 1526e0053ec0SHerbert Xu 1527e0053ec0SHerbert Xu return __skb_clone(n, skb); 15281da177e4SLinus Torvalds } 1529b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 15301da177e4SLinus Torvalds 1531b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1532f5b17294SPravin B Shelar { 1533030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1534030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1535030737bcSEric Dumazet skb->csum_start += off; 1536f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1537f5b17294SPravin B Shelar skb->transport_header += off; 1538f5b17294SPravin B Shelar skb->network_header += off; 1539f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1540f5b17294SPravin B Shelar skb->mac_header += off; 1541f5b17294SPravin B Shelar skb->inner_transport_header += off; 1542f5b17294SPravin B Shelar skb->inner_network_header += off; 1543aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1544f5b17294SPravin B Shelar } 1545b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1546f5b17294SPravin B Shelar 154708303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 15481da177e4SLinus Torvalds { 1549dec18810SHerbert Xu __copy_skb_header(new, old); 1550dec18810SHerbert Xu 15517967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 15527967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 15537967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 15541da177e4SLinus Torvalds } 155508303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 15561da177e4SLinus Torvalds 1557c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1558c93bdd0eSMel Gorman { 1559c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1560c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1561c93bdd0eSMel Gorman return 0; 1562c93bdd0eSMel Gorman } 1563c93bdd0eSMel Gorman 15641da177e4SLinus Torvalds /** 15651da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 15661da177e4SLinus Torvalds * @skb: buffer to copy 15671da177e4SLinus Torvalds * @gfp_mask: allocation priority 15681da177e4SLinus Torvalds * 15691da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 15701da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 15711da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 15721da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 15731da177e4SLinus Torvalds * 15741da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 15751da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 15761da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 15771da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 15781da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 15791da177e4SLinus Torvalds */ 15801da177e4SLinus Torvalds 1581dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 15821da177e4SLinus Torvalds { 15836602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1584ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1585c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1586c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 15876602cebbSEric Dumazet 15881da177e4SLinus Torvalds if (!n) 15891da177e4SLinus Torvalds return NULL; 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds /* Set the data pointer */ 15921da177e4SLinus Torvalds skb_reserve(n, headerlen); 15931da177e4SLinus Torvalds /* Set the tail pointer and length */ 15941da177e4SLinus Torvalds skb_put(n, skb->len); 15951da177e4SLinus Torvalds 15969f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 15971da177e4SLinus Torvalds 159808303c18SIlya Lesokhin skb_copy_header(n, skb); 15991da177e4SLinus Torvalds return n; 16001da177e4SLinus Torvalds } 1601b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds /** 1604bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 16051da177e4SLinus Torvalds * @skb: buffer to copy 1606117632e6SEric Dumazet * @headroom: headroom of new skb 16071da177e4SLinus Torvalds * @gfp_mask: allocation priority 1608bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1609bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1610bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 16111da177e4SLinus Torvalds * 16121da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 16131da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 16141da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 16151da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 16161da177e4SLinus Torvalds * or the pointer to the buffer on success. 16171da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 16181da177e4SLinus Torvalds */ 16191da177e4SLinus Torvalds 1620bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1621bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 16221da177e4SLinus Torvalds { 1623117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1624bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1625bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 16266602cebbSEric Dumazet 16271da177e4SLinus Torvalds if (!n) 16281da177e4SLinus Torvalds goto out; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds /* Set the data pointer */ 1631117632e6SEric Dumazet skb_reserve(n, headroom); 16321da177e4SLinus Torvalds /* Set the tail pointer and length */ 16331da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 16341da177e4SLinus Torvalds /* Copy the bytes */ 1635d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 16361da177e4SLinus Torvalds 163725f484a6SHerbert Xu n->truesize += skb->data_len; 16381da177e4SLinus Torvalds n->data_len = skb->data_len; 16391da177e4SLinus Torvalds n->len = skb->len; 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 16421da177e4SLinus Torvalds int i; 16431da177e4SLinus Torvalds 16441f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 16451f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 16461511022cSDan Carpenter kfree_skb(n); 16471511022cSDan Carpenter n = NULL; 1648a6686f2fSShirley Ma goto out; 1649a6686f2fSShirley Ma } 16501da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16511da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1652ea2ab693SIan Campbell skb_frag_ref(skb, i); 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 165721dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 16581da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 16591da177e4SLinus Torvalds skb_clone_fraglist(n); 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds 166208303c18SIlya Lesokhin skb_copy_header(n, skb); 16631da177e4SLinus Torvalds out: 16641da177e4SLinus Torvalds return n; 16651da177e4SLinus Torvalds } 1666bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds /** 16691da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 16701da177e4SLinus Torvalds * @skb: buffer to reallocate 16711da177e4SLinus Torvalds * @nhead: room to add at head 16721da177e4SLinus Torvalds * @ntail: room to add at tail 16731da177e4SLinus Torvalds * @gfp_mask: allocation priority 16741da177e4SLinus Torvalds * 1675bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1676bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 16771da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 16781da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 16791da177e4SLinus Torvalds * 16801da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 16811da177e4SLinus Torvalds * reloaded after call to this function. 16821da177e4SLinus Torvalds */ 16831da177e4SLinus Torvalds 168486a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1685dd0fc66fSAl Viro gfp_t gfp_mask) 16861da177e4SLinus Torvalds { 1687158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1688158f323bSEric Dumazet int size = osize + nhead + ntail; 16891da177e4SLinus Torvalds long off; 1690158f323bSEric Dumazet u8 *data; 16911da177e4SLinus Torvalds 16924edd87adSHerbert Xu BUG_ON(nhead < 0); 16934edd87adSHerbert Xu 16949f77fad3STim Hansen BUG_ON(skb_shared(skb)); 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 16971da177e4SLinus Torvalds 1698c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1699c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1700c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1701c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 17021da177e4SLinus Torvalds if (!data) 17031da177e4SLinus Torvalds goto nodata; 170487151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 17076602cebbSEric Dumazet * optimized for the cases when header is void. 17086602cebbSEric Dumazet */ 17096602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 17106602cebbSEric Dumazet 17116602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 17126602cebbSEric Dumazet skb_shinfo(skb), 1713fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 17141da177e4SLinus Torvalds 17153e24591aSAlexander Duyck /* 17163e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 17173e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 17183e24591aSAlexander Duyck * be since all we did is relocate the values 17193e24591aSAlexander Duyck */ 17203e24591aSAlexander Duyck if (skb_cloned(skb)) { 172170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1722a6686f2fSShirley Ma goto nofrags; 17231f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1724c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 17251da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1726ea2ab693SIan Campbell skb_frag_ref(skb, i); 17271da177e4SLinus Torvalds 172821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 17291da177e4SLinus Torvalds skb_clone_fraglist(skb); 17301da177e4SLinus Torvalds 17311da177e4SLinus Torvalds skb_release_data(skb); 17323e24591aSAlexander Duyck } else { 17333e24591aSAlexander Duyck skb_free_head(skb); 17341fd63041SEric Dumazet } 17351da177e4SLinus Torvalds off = (data + nhead) - skb->head; 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds skb->head = data; 1738d3836f21SEric Dumazet skb->head_frag = 0; 17391da177e4SLinus Torvalds skb->data += off; 17404305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 17414305b541SArnaldo Carvalho de Melo skb->end = size; 174256eb8882SPatrick McHardy off = nhead; 17434305b541SArnaldo Carvalho de Melo #else 17444305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 174556eb8882SPatrick McHardy #endif 174627a884dcSArnaldo Carvalho de Melo skb->tail += off; 1747b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 17481da177e4SLinus Torvalds skb->cloned = 0; 1749334a8132SPatrick McHardy skb->hdr_len = 0; 17501da177e4SLinus Torvalds skb->nohdr = 0; 17511da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1752158f323bSEric Dumazet 1753de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1754de8f3a83SDaniel Borkmann 1755158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1756158f323bSEric Dumazet * For the moment, we really care of rx path, or 1757158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1758158f323bSEric Dumazet */ 1759158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1760158f323bSEric Dumazet skb->truesize += size - osize; 1761158f323bSEric Dumazet 17621da177e4SLinus Torvalds return 0; 17631da177e4SLinus Torvalds 1764a6686f2fSShirley Ma nofrags: 1765a6686f2fSShirley Ma kfree(data); 17661da177e4SLinus Torvalds nodata: 17671da177e4SLinus Torvalds return -ENOMEM; 17681da177e4SLinus Torvalds } 1769b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 17741da177e4SLinus Torvalds { 17751da177e4SLinus Torvalds struct sk_buff *skb2; 17761da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds if (delta <= 0) 17791da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 17801da177e4SLinus Torvalds else { 17811da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 17821da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 17831da177e4SLinus Torvalds GFP_ATOMIC)) { 17841da177e4SLinus Torvalds kfree_skb(skb2); 17851da177e4SLinus Torvalds skb2 = NULL; 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds return skb2; 17891da177e4SLinus Torvalds } 1790b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds /** 17931da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 17941da177e4SLinus Torvalds * @skb: buffer to copy 17951da177e4SLinus Torvalds * @newheadroom: new free bytes at head 17961da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 17971da177e4SLinus Torvalds * @gfp_mask: allocation priority 17981da177e4SLinus Torvalds * 17991da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 18001da177e4SLinus Torvalds * allocate additional space. 18011da177e4SLinus Torvalds * 18021da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 18031da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 18041da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 18051da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 18061da177e4SLinus Torvalds * 18071da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 18081da177e4SLinus Torvalds * is called from an interrupt. 18091da177e4SLinus Torvalds */ 18101da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 181186a76cafSVictor Fusco int newheadroom, int newtailroom, 1812dd0fc66fSAl Viro gfp_t gfp_mask) 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds /* 18151da177e4SLinus Torvalds * Allocate the copy buffer 18161da177e4SLinus Torvalds */ 1817c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1818c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1819c93bdd0eSMel Gorman NUMA_NO_NODE); 1820efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 18211da177e4SLinus Torvalds int head_copy_len, head_copy_off; 18221da177e4SLinus Torvalds 18231da177e4SLinus Torvalds if (!n) 18241da177e4SLinus Torvalds return NULL; 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds skb_reserve(n, newheadroom); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds /* Set the tail pointer and length */ 18291da177e4SLinus Torvalds skb_put(n, skb->len); 18301da177e4SLinus Torvalds 1831efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 18321da177e4SLinus Torvalds head_copy_off = 0; 18331da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 18341da177e4SLinus Torvalds head_copy_len = newheadroom; 18351da177e4SLinus Torvalds else 18361da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds /* Copy the linear header and data. */ 18399f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 18409f77fad3STim Hansen skb->len + head_copy_len)); 18411da177e4SLinus Torvalds 184208303c18SIlya Lesokhin skb_copy_header(n, skb); 18431da177e4SLinus Torvalds 1844030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1845efd1e8d5SPatrick McHardy 18461da177e4SLinus Torvalds return n; 18471da177e4SLinus Torvalds } 1848b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds /** 1851cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 18521da177e4SLinus Torvalds * @skb: buffer to pad 18531da177e4SLinus Torvalds * @pad: space to pad 1854cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 18551da177e4SLinus Torvalds * 18561da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 18571da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 18581da177e4SLinus Torvalds * beyond the buffer end onto the wire. 18591da177e4SLinus Torvalds * 1860cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1861cd0a137aSFlorian Fainelli * if @free_on_error is true. 18621da177e4SLinus Torvalds */ 18631da177e4SLinus Torvalds 1864cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 18651da177e4SLinus Torvalds { 18665b057c6bSHerbert Xu int err; 18675b057c6bSHerbert Xu int ntail; 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 18705b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 18711da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 18725b057c6bSHerbert Xu return 0; 18731da177e4SLinus Torvalds } 18741da177e4SLinus Torvalds 18754305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 18765b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 18775b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 18785b057c6bSHerbert Xu if (unlikely(err)) 18795b057c6bSHerbert Xu goto free_skb; 18805b057c6bSHerbert Xu } 18815b057c6bSHerbert Xu 18825b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 18835b057c6bSHerbert Xu * to be audited. 18845b057c6bSHerbert Xu */ 18855b057c6bSHerbert Xu err = skb_linearize(skb); 18865b057c6bSHerbert Xu if (unlikely(err)) 18875b057c6bSHerbert Xu goto free_skb; 18885b057c6bSHerbert Xu 18895b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 18905b057c6bSHerbert Xu return 0; 18915b057c6bSHerbert Xu 18925b057c6bSHerbert Xu free_skb: 1893cd0a137aSFlorian Fainelli if (free_on_error) 18941da177e4SLinus Torvalds kfree_skb(skb); 18955b057c6bSHerbert Xu return err; 18961da177e4SLinus Torvalds } 1897cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 18981da177e4SLinus Torvalds 18990dde3e16SIlpo Järvinen /** 19000c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 19010c7ddf36SMathias Krause * @skb: start of the buffer to use 19020c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 19030c7ddf36SMathias Krause * @len: amount of data to add 19040c7ddf36SMathias Krause * 19050c7ddf36SMathias Krause * This function extends the used data area of the potentially 19060c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 19070c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 19080c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 19090c7ddf36SMathias Krause * returned. 19100c7ddf36SMathias Krause */ 19110c7ddf36SMathias Krause 19124df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 19130c7ddf36SMathias Krause { 19140c7ddf36SMathias Krause if (tail != skb) { 19150c7ddf36SMathias Krause skb->data_len += len; 19160c7ddf36SMathias Krause skb->len += len; 19170c7ddf36SMathias Krause } 19180c7ddf36SMathias Krause return skb_put(tail, len); 19190c7ddf36SMathias Krause } 19200c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 19210c7ddf36SMathias Krause 19220c7ddf36SMathias Krause /** 19230dde3e16SIlpo Järvinen * skb_put - add data to a buffer 19240dde3e16SIlpo Järvinen * @skb: buffer to use 19250dde3e16SIlpo Järvinen * @len: amount of data to add 19260dde3e16SIlpo Järvinen * 19270dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 19280dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 19290dde3e16SIlpo Järvinen * first byte of the extra data is returned. 19300dde3e16SIlpo Järvinen */ 19314df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 19320dde3e16SIlpo Järvinen { 19334df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 19340dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 19350dde3e16SIlpo Järvinen skb->tail += len; 19360dde3e16SIlpo Järvinen skb->len += len; 19370dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 19380dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 19390dde3e16SIlpo Järvinen return tmp; 19400dde3e16SIlpo Järvinen } 19410dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 19420dde3e16SIlpo Järvinen 19436be8ac2fSIlpo Järvinen /** 1944c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1945c2aa270aSIlpo Järvinen * @skb: buffer to use 1946c2aa270aSIlpo Järvinen * @len: amount of data to add 1947c2aa270aSIlpo Järvinen * 1948c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1949c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1950c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1951c2aa270aSIlpo Järvinen */ 1952d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 1953c2aa270aSIlpo Järvinen { 1954c2aa270aSIlpo Järvinen skb->data -= len; 1955c2aa270aSIlpo Järvinen skb->len += len; 1956c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 1957c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1958c2aa270aSIlpo Järvinen return skb->data; 1959c2aa270aSIlpo Järvinen } 1960c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1961c2aa270aSIlpo Järvinen 1962c2aa270aSIlpo Järvinen /** 19636be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 19646be8ac2fSIlpo Järvinen * @skb: buffer to use 19656be8ac2fSIlpo Järvinen * @len: amount of data to remove 19666be8ac2fSIlpo Järvinen * 19676be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 19686be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 19696be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 19706be8ac2fSIlpo Järvinen * the old data. 19716be8ac2fSIlpo Järvinen */ 1972af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 19736be8ac2fSIlpo Järvinen { 197447d29646SDavid S. Miller return skb_pull_inline(skb, len); 19756be8ac2fSIlpo Järvinen } 19766be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 19776be8ac2fSIlpo Järvinen 1978419ae74eSIlpo Järvinen /** 1979419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1980419ae74eSIlpo Järvinen * @skb: buffer to alter 1981419ae74eSIlpo Järvinen * @len: new length 1982419ae74eSIlpo Järvinen * 1983419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1984419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1985419ae74eSIlpo Järvinen * The skb must be linear. 1986419ae74eSIlpo Järvinen */ 1987419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1988419ae74eSIlpo Järvinen { 1989419ae74eSIlpo Järvinen if (skb->len > len) 1990419ae74eSIlpo Järvinen __skb_trim(skb, len); 1991419ae74eSIlpo Järvinen } 1992419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1993419ae74eSIlpo Järvinen 19943cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 19951da177e4SLinus Torvalds */ 19961da177e4SLinus Torvalds 19973cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 19981da177e4SLinus Torvalds { 199927b437c8SHerbert Xu struct sk_buff **fragp; 200027b437c8SHerbert Xu struct sk_buff *frag; 20011da177e4SLinus Torvalds int offset = skb_headlen(skb); 20021da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 20031da177e4SLinus Torvalds int i; 200427b437c8SHerbert Xu int err; 200527b437c8SHerbert Xu 200627b437c8SHerbert Xu if (skb_cloned(skb) && 200727b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 200827b437c8SHerbert Xu return err; 20091da177e4SLinus Torvalds 2010f4d26fb3SHerbert Xu i = 0; 2011f4d26fb3SHerbert Xu if (offset >= len) 2012f4d26fb3SHerbert Xu goto drop_pages; 2013f4d26fb3SHerbert Xu 2014f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 20159e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 201627b437c8SHerbert Xu 201727b437c8SHerbert Xu if (end < len) { 20181da177e4SLinus Torvalds offset = end; 201927b437c8SHerbert Xu continue; 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds 20229e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 202327b437c8SHerbert Xu 2024f4d26fb3SHerbert Xu drop_pages: 202527b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 202627b437c8SHerbert Xu 202727b437c8SHerbert Xu for (; i < nfrags; i++) 2028ea2ab693SIan Campbell skb_frag_unref(skb, i); 202927b437c8SHerbert Xu 203021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 203127b437c8SHerbert Xu skb_drop_fraglist(skb); 2032f4d26fb3SHerbert Xu goto done; 203327b437c8SHerbert Xu } 203427b437c8SHerbert Xu 203527b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 203627b437c8SHerbert Xu fragp = &frag->next) { 203727b437c8SHerbert Xu int end = offset + frag->len; 203827b437c8SHerbert Xu 203927b437c8SHerbert Xu if (skb_shared(frag)) { 204027b437c8SHerbert Xu struct sk_buff *nfrag; 204127b437c8SHerbert Xu 204227b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 204327b437c8SHerbert Xu if (unlikely(!nfrag)) 204427b437c8SHerbert Xu return -ENOMEM; 204527b437c8SHerbert Xu 204627b437c8SHerbert Xu nfrag->next = frag->next; 204785bb2a60SEric Dumazet consume_skb(frag); 204827b437c8SHerbert Xu frag = nfrag; 204927b437c8SHerbert Xu *fragp = frag; 205027b437c8SHerbert Xu } 205127b437c8SHerbert Xu 205227b437c8SHerbert Xu if (end < len) { 205327b437c8SHerbert Xu offset = end; 205427b437c8SHerbert Xu continue; 205527b437c8SHerbert Xu } 205627b437c8SHerbert Xu 205727b437c8SHerbert Xu if (end > len && 205827b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 205927b437c8SHerbert Xu return err; 206027b437c8SHerbert Xu 206127b437c8SHerbert Xu if (frag->next) 206227b437c8SHerbert Xu skb_drop_list(&frag->next); 206327b437c8SHerbert Xu break; 206427b437c8SHerbert Xu } 206527b437c8SHerbert Xu 2066f4d26fb3SHerbert Xu done: 206727b437c8SHerbert Xu if (len > skb_headlen(skb)) { 20681da177e4SLinus Torvalds skb->data_len -= skb->len - len; 20691da177e4SLinus Torvalds skb->len = len; 20701da177e4SLinus Torvalds } else { 20711da177e4SLinus Torvalds skb->len = len; 20721da177e4SLinus Torvalds skb->data_len = 0; 207327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 20741da177e4SLinus Torvalds } 20751da177e4SLinus Torvalds 2076c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 2077c21b48ccSEric Dumazet skb_condense(skb); 20781da177e4SLinus Torvalds return 0; 20791da177e4SLinus Torvalds } 2080b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 20811da177e4SLinus Torvalds 208288078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 208388078d98SEric Dumazet */ 208488078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 208588078d98SEric Dumazet { 208688078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 208788078d98SEric Dumazet int delta = skb->len - len; 208888078d98SEric Dumazet 2089d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 2090d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 2091d55bef50SDimitris Michailidis len); 209254970a2fSVasily Averin } else if (skb->ip_summed == CHECKSUM_PARTIAL) { 209354970a2fSVasily Averin int hdlen = (len > skb_headlen(skb)) ? skb_headlen(skb) : len; 209454970a2fSVasily Averin int offset = skb_checksum_start_offset(skb) + skb->csum_offset; 209554970a2fSVasily Averin 209654970a2fSVasily Averin if (offset + sizeof(__sum16) > hdlen) 209754970a2fSVasily Averin return -EINVAL; 209888078d98SEric Dumazet } 209988078d98SEric Dumazet return __pskb_trim(skb, len); 210088078d98SEric Dumazet } 210188078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 210288078d98SEric Dumazet 21031da177e4SLinus Torvalds /** 21041da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 21051da177e4SLinus Torvalds * @skb: buffer to reallocate 21061da177e4SLinus Torvalds * @delta: number of bytes to advance tail 21071da177e4SLinus Torvalds * 21081da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 21091da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 21101da177e4SLinus Torvalds * data from fragmented part. 21111da177e4SLinus Torvalds * 21121da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 21131da177e4SLinus Torvalds * 21141da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 21151da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 21161da177e4SLinus Torvalds * 21171da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 21181da177e4SLinus Torvalds * reloaded after call to this function. 21191da177e4SLinus Torvalds */ 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 21221da177e4SLinus Torvalds * when it is necessary. 21231da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 21241da177e4SLinus Torvalds * 2. It may change skb pointers. 21251da177e4SLinus Torvalds * 21261da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 21271da177e4SLinus Torvalds */ 2128af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 21291da177e4SLinus Torvalds { 21301da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 21311da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 21321da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 21331da177e4SLinus Torvalds */ 21344305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 21351da177e4SLinus Torvalds 21361da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 21371da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 21381da177e4SLinus Torvalds GFP_ATOMIC)) 21391da177e4SLinus Torvalds return NULL; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21429f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 21439f77fad3STim Hansen skb_tail_pointer(skb), delta)); 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 21461da177e4SLinus Torvalds * size of pulled pages. Superb. 21471da177e4SLinus Torvalds */ 214821dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 21491da177e4SLinus Torvalds goto pull_pages; 21501da177e4SLinus Torvalds 21511da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 21521da177e4SLinus Torvalds eat = delta; 21531da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21549e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 21559e903e08SEric Dumazet 21569e903e08SEric Dumazet if (size >= eat) 21571da177e4SLinus Torvalds goto pull_pages; 21589e903e08SEric Dumazet eat -= size; 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 216209001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 21631da177e4SLinus Torvalds * but taking into account that pulling is expected to 21641da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 21651da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 21661da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 21671da177e4SLinus Torvalds */ 21681da177e4SLinus Torvalds if (eat) { 21691da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 21701da177e4SLinus Torvalds struct sk_buff *clone = NULL; 21711da177e4SLinus Torvalds struct sk_buff *insp = NULL; 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds do { 21741da177e4SLinus Torvalds if (list->len <= eat) { 21751da177e4SLinus Torvalds /* Eaten as whole. */ 21761da177e4SLinus Torvalds eat -= list->len; 21771da177e4SLinus Torvalds list = list->next; 21781da177e4SLinus Torvalds insp = list; 21791da177e4SLinus Torvalds } else { 21801da177e4SLinus Torvalds /* Eaten partially. */ 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds if (skb_shared(list)) { 21831da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 21841da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 21851da177e4SLinus Torvalds if (!clone) 21861da177e4SLinus Torvalds return NULL; 21871da177e4SLinus Torvalds insp = list->next; 21881da177e4SLinus Torvalds list = clone; 21891da177e4SLinus Torvalds } else { 21901da177e4SLinus Torvalds /* This may be pulled without 21911da177e4SLinus Torvalds * problems. */ 21921da177e4SLinus Torvalds insp = list; 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 21951da177e4SLinus Torvalds kfree_skb(clone); 21961da177e4SLinus Torvalds return NULL; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds break; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds } while (eat); 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds /* Free pulled out fragments. */ 22031da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 22041da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 22051da177e4SLinus Torvalds kfree_skb(list); 22061da177e4SLinus Torvalds } 22071da177e4SLinus Torvalds /* And insert new clone at head. */ 22081da177e4SLinus Torvalds if (clone) { 22091da177e4SLinus Torvalds clone->next = list; 22101da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 22111da177e4SLinus Torvalds } 22121da177e4SLinus Torvalds } 22131da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds pull_pages: 22161da177e4SLinus Torvalds eat = delta; 22171da177e4SLinus Torvalds k = 0; 22181da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 22199e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 22209e903e08SEric Dumazet 22219e903e08SEric Dumazet if (size <= eat) { 2222ea2ab693SIan Campbell skb_frag_unref(skb, i); 22239e903e08SEric Dumazet eat -= size; 22241da177e4SLinus Torvalds } else { 2225b54c9d5bSJonathan Lemon skb_frag_t *frag = &skb_shinfo(skb)->frags[k]; 2226b54c9d5bSJonathan Lemon 2227b54c9d5bSJonathan Lemon *frag = skb_shinfo(skb)->frags[i]; 22281da177e4SLinus Torvalds if (eat) { 2229b54c9d5bSJonathan Lemon skb_frag_off_add(frag, eat); 2230b54c9d5bSJonathan Lemon skb_frag_size_sub(frag, eat); 22313ccc6c6fSlinzhang if (!i) 22323ccc6c6fSlinzhang goto end; 22331da177e4SLinus Torvalds eat = 0; 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds k++; 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 22391da177e4SLinus Torvalds 22403ccc6c6fSlinzhang end: 22411da177e4SLinus Torvalds skb->tail += delta; 22421da177e4SLinus Torvalds skb->data_len -= delta; 22431da177e4SLinus Torvalds 22441f8b977aSWillem de Bruijn if (!skb->data_len) 22451f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 22461f8b977aSWillem de Bruijn 224727a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 22481da177e4SLinus Torvalds } 2249b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 22501da177e4SLinus Torvalds 225122019b17SEric Dumazet /** 225222019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 225322019b17SEric Dumazet * @skb: source skb 225422019b17SEric Dumazet * @offset: offset in source 225522019b17SEric Dumazet * @to: destination buffer 225622019b17SEric Dumazet * @len: number of bytes to copy 225722019b17SEric Dumazet * 225822019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 225922019b17SEric Dumazet * destination buffer. 226022019b17SEric Dumazet * 226122019b17SEric Dumazet * CAUTION ! : 226222019b17SEric Dumazet * If its prototype is ever changed, 226322019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 226422019b17SEric Dumazet * since it is called from BPF assembly code. 226522019b17SEric Dumazet */ 22661da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 22671da177e4SLinus Torvalds { 22681a028e50SDavid S. Miller int start = skb_headlen(skb); 2269fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2270fbb398a8SDavid S. Miller int i, copy; 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds if (offset > (int)skb->len - len) 22731da177e4SLinus Torvalds goto fault; 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds /* Copy header. */ 22761a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 22771da177e4SLinus Torvalds if (copy > len) 22781da177e4SLinus Torvalds copy = len; 2279d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 22801da177e4SLinus Torvalds if ((len -= copy) == 0) 22811da177e4SLinus Torvalds return 0; 22821da177e4SLinus Torvalds offset += copy; 22831da177e4SLinus Torvalds to += copy; 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 22871a028e50SDavid S. Miller int end; 228851c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 22891da177e4SLinus Torvalds 2290547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22911a028e50SDavid S. Miller 229251c56b00SEric Dumazet end = start + skb_frag_size(f); 22931da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2294c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2295c613c209SWillem de Bruijn struct page *p; 22961da177e4SLinus Torvalds u8 *vaddr; 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds if (copy > len) 22991da177e4SLinus Torvalds copy = len; 23001da177e4SLinus Torvalds 2301c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2302b54c9d5bSJonathan Lemon skb_frag_off(f) + offset - start, 2303c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2304c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2305c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 230651c56b00SEric Dumazet kunmap_atomic(vaddr); 2307c613c209SWillem de Bruijn } 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds if ((len -= copy) == 0) 23101da177e4SLinus Torvalds return 0; 23111da177e4SLinus Torvalds offset += copy; 23121da177e4SLinus Torvalds to += copy; 23131da177e4SLinus Torvalds } 23141a028e50SDavid S. Miller start = end; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 2317fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 23181a028e50SDavid S. Miller int end; 23191da177e4SLinus Torvalds 2320547b792cSIlpo Järvinen WARN_ON(start > offset + len); 23211a028e50SDavid S. Miller 2322fbb398a8SDavid S. Miller end = start + frag_iter->len; 23231da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 23241da177e4SLinus Torvalds if (copy > len) 23251da177e4SLinus Torvalds copy = len; 2326fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 23271da177e4SLinus Torvalds goto fault; 23281da177e4SLinus Torvalds if ((len -= copy) == 0) 23291da177e4SLinus Torvalds return 0; 23301da177e4SLinus Torvalds offset += copy; 23311da177e4SLinus Torvalds to += copy; 23321da177e4SLinus Torvalds } 23331a028e50SDavid S. Miller start = end; 23341da177e4SLinus Torvalds } 2335a6686f2fSShirley Ma 23361da177e4SLinus Torvalds if (!len) 23371da177e4SLinus Torvalds return 0; 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds fault: 23401da177e4SLinus Torvalds return -EFAULT; 23411da177e4SLinus Torvalds } 2342b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 23431da177e4SLinus Torvalds 23449c55e01cSJens Axboe /* 23459c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 23469c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 23479c55e01cSJens Axboe */ 23489c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 23499c55e01cSJens Axboe { 23508b9d3728SJarek Poplawski put_page(spd->pages[i]); 23518b9d3728SJarek Poplawski } 23529c55e01cSJens Axboe 2353a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 23544fb66994SJarek Poplawski unsigned int *offset, 235518aafc62SEric Dumazet struct sock *sk) 23568b9d3728SJarek Poplawski { 23575640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 23588b9d3728SJarek Poplawski 23595640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 23608b9d3728SJarek Poplawski return NULL; 23614fb66994SJarek Poplawski 23625640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 23634fb66994SJarek Poplawski 23645640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 23655640f768SEric Dumazet page_address(page) + *offset, *len); 23665640f768SEric Dumazet *offset = pfrag->offset; 23675640f768SEric Dumazet pfrag->offset += *len; 23684fb66994SJarek Poplawski 23695640f768SEric Dumazet return pfrag->page; 23709c55e01cSJens Axboe } 23719c55e01cSJens Axboe 237241c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 237341c73a0dSEric Dumazet struct page *page, 237441c73a0dSEric Dumazet unsigned int offset) 237541c73a0dSEric Dumazet { 237641c73a0dSEric Dumazet return spd->nr_pages && 237741c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 237841c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 237941c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 238041c73a0dSEric Dumazet } 238141c73a0dSEric Dumazet 23829c55e01cSJens Axboe /* 23839c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 23849c55e01cSJens Axboe */ 2385a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 238635f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 23874fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 238818aafc62SEric Dumazet bool linear, 23897a67e56fSJarek Poplawski struct sock *sk) 23909c55e01cSJens Axboe { 239141c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2392a108d5f3SDavid S. Miller return true; 23939c55e01cSJens Axboe 23948b9d3728SJarek Poplawski if (linear) { 239518aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 23968b9d3728SJarek Poplawski if (!page) 2397a108d5f3SDavid S. Miller return true; 239841c73a0dSEric Dumazet } 239941c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 240041c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2401a108d5f3SDavid S. Miller return false; 240241c73a0dSEric Dumazet } 24038b9d3728SJarek Poplawski get_page(page); 24049c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 24054fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 24069c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 24079c55e01cSJens Axboe spd->nr_pages++; 24088b9d3728SJarek Poplawski 2409a108d5f3SDavid S. Miller return false; 24109c55e01cSJens Axboe } 24119c55e01cSJens Axboe 2412a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 24132870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 241418aafc62SEric Dumazet unsigned int *len, 2415d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 241635f3d14dSJens Axboe struct sock *sk, 241735f3d14dSJens Axboe struct pipe_inode_info *pipe) 24189c55e01cSJens Axboe { 24192870c43dSOctavian Purdila if (!*len) 2420a108d5f3SDavid S. Miller return true; 24219c55e01cSJens Axboe 24222870c43dSOctavian Purdila /* skip this segment if already processed */ 24232870c43dSOctavian Purdila if (*off >= plen) { 24242870c43dSOctavian Purdila *off -= plen; 2425a108d5f3SDavid S. Miller return false; 24262870c43dSOctavian Purdila } 24272870c43dSOctavian Purdila 24282870c43dSOctavian Purdila /* ignore any bits we already processed */ 24299ca1b22dSEric Dumazet poff += *off; 24309ca1b22dSEric Dumazet plen -= *off; 24312870c43dSOctavian Purdila *off = 0; 24322870c43dSOctavian Purdila 243318aafc62SEric Dumazet do { 243418aafc62SEric Dumazet unsigned int flen = min(*len, plen); 24352870c43dSOctavian Purdila 243618aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 243718aafc62SEric Dumazet linear, sk)) 2438a108d5f3SDavid S. Miller return true; 243918aafc62SEric Dumazet poff += flen; 244018aafc62SEric Dumazet plen -= flen; 24412870c43dSOctavian Purdila *len -= flen; 244218aafc62SEric Dumazet } while (*len && plen); 24432870c43dSOctavian Purdila 2444a108d5f3SDavid S. Miller return false; 2445db43a282SOctavian Purdila } 24469c55e01cSJens Axboe 24479c55e01cSJens Axboe /* 2448a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 24492870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 24509c55e01cSJens Axboe */ 2451a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 245235f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 245335f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 24542870c43dSOctavian Purdila { 24552870c43dSOctavian Purdila int seg; 2456fa9835e5STom Herbert struct sk_buff *iter; 24579c55e01cSJens Axboe 24581d0c0b32SEric Dumazet /* map the linear part : 24592996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 24602996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 24612996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 24629c55e01cSJens Axboe */ 24632870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 24642870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 24652870c43dSOctavian Purdila skb_headlen(skb), 246618aafc62SEric Dumazet offset, len, spd, 24673a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 24681d0c0b32SEric Dumazet sk, pipe)) 2469a108d5f3SDavid S. Miller return true; 24709c55e01cSJens Axboe 24719c55e01cSJens Axboe /* 24729c55e01cSJens Axboe * then map the fragments 24739c55e01cSJens Axboe */ 24749c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 24759c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 24769c55e01cSJens Axboe 2477ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 2478b54c9d5bSJonathan Lemon skb_frag_off(f), skb_frag_size(f), 247918aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2480a108d5f3SDavid S. Miller return true; 24819c55e01cSJens Axboe } 24829c55e01cSJens Axboe 2483fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2484fa9835e5STom Herbert if (*offset >= iter->len) { 2485fa9835e5STom Herbert *offset -= iter->len; 2486fa9835e5STom Herbert continue; 2487fa9835e5STom Herbert } 2488fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2489fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2490fa9835e5STom Herbert * case. 2491fa9835e5STom Herbert */ 2492fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2493fa9835e5STom Herbert return true; 2494fa9835e5STom Herbert } 2495fa9835e5STom Herbert 2496a108d5f3SDavid S. Miller return false; 24979c55e01cSJens Axboe } 24989c55e01cSJens Axboe 24999c55e01cSJens Axboe /* 25009c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2501fa9835e5STom Herbert * the fragments, and the frag list. 25029c55e01cSJens Axboe */ 2503a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 25049c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 250525869262SAl Viro unsigned int flags) 25069c55e01cSJens Axboe { 250741c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 250841c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 25099c55e01cSJens Axboe struct splice_pipe_desc spd = { 25109c55e01cSJens Axboe .pages = pages, 25119c55e01cSJens Axboe .partial = partial, 2512047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 251328a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 25149c55e01cSJens Axboe .spd_release = sock_spd_release, 25159c55e01cSJens Axboe }; 251635f3d14dSJens Axboe int ret = 0; 251735f3d14dSJens Axboe 2518fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 25199c55e01cSJens Axboe 2520a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 252125869262SAl Viro ret = splice_to_pipe(pipe, &spd); 25229c55e01cSJens Axboe 252335f3d14dSJens Axboe return ret; 25249c55e01cSJens Axboe } 25252b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 25269c55e01cSJens Axboe 25270739cd28SCong Wang static int sendmsg_unlocked(struct sock *sk, struct msghdr *msg, 25280739cd28SCong Wang struct kvec *vec, size_t num, size_t size) 25290739cd28SCong Wang { 25300739cd28SCong Wang struct socket *sock = sk->sk_socket; 25310739cd28SCong Wang 25320739cd28SCong Wang if (!sock) 25330739cd28SCong Wang return -EINVAL; 25340739cd28SCong Wang return kernel_sendmsg(sock, msg, vec, num, size); 25350739cd28SCong Wang } 25360739cd28SCong Wang 25370739cd28SCong Wang static int sendpage_unlocked(struct sock *sk, struct page *page, int offset, 25380739cd28SCong Wang size_t size, int flags) 25390739cd28SCong Wang { 25400739cd28SCong Wang struct socket *sock = sk->sk_socket; 25410739cd28SCong Wang 25420739cd28SCong Wang if (!sock) 25430739cd28SCong Wang return -EINVAL; 25440739cd28SCong Wang return kernel_sendpage(sock, page, offset, size, flags); 25450739cd28SCong Wang } 25460739cd28SCong Wang 25470739cd28SCong Wang typedef int (*sendmsg_func)(struct sock *sk, struct msghdr *msg, 25480739cd28SCong Wang struct kvec *vec, size_t num, size_t size); 25490739cd28SCong Wang typedef int (*sendpage_func)(struct sock *sk, struct page *page, int offset, 25500739cd28SCong Wang size_t size, int flags); 25510739cd28SCong Wang static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, 25520739cd28SCong Wang int len, sendmsg_func sendmsg, sendpage_func sendpage) 255320bf50deSTom Herbert { 255420bf50deSTom Herbert unsigned int orig_len = len; 255520bf50deSTom Herbert struct sk_buff *head = skb; 255620bf50deSTom Herbert unsigned short fragidx; 255720bf50deSTom Herbert int slen, ret; 255820bf50deSTom Herbert 255920bf50deSTom Herbert do_frag_list: 256020bf50deSTom Herbert 256120bf50deSTom Herbert /* Deal with head data */ 256220bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 256320bf50deSTom Herbert struct kvec kv; 256420bf50deSTom Herbert struct msghdr msg; 256520bf50deSTom Herbert 256620bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 256720bf50deSTom Herbert kv.iov_base = skb->data + offset; 2568db5980d8SJohn Fastabend kv.iov_len = slen; 256920bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 2570bd95e678SJohn Fastabend msg.msg_flags = MSG_DONTWAIT; 257120bf50deSTom Herbert 25720739cd28SCong Wang ret = INDIRECT_CALL_2(sendmsg, kernel_sendmsg_locked, 25730739cd28SCong Wang sendmsg_unlocked, sk, &msg, &kv, 1, slen); 257420bf50deSTom Herbert if (ret <= 0) 257520bf50deSTom Herbert goto error; 257620bf50deSTom Herbert 257720bf50deSTom Herbert offset += ret; 257820bf50deSTom Herbert len -= ret; 257920bf50deSTom Herbert } 258020bf50deSTom Herbert 258120bf50deSTom Herbert /* All the data was skb head? */ 258220bf50deSTom Herbert if (!len) 258320bf50deSTom Herbert goto out; 258420bf50deSTom Herbert 258520bf50deSTom Herbert /* Make offset relative to start of frags */ 258620bf50deSTom Herbert offset -= skb_headlen(skb); 258720bf50deSTom Herbert 258820bf50deSTom Herbert /* Find where we are in frag list */ 258920bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 259020bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 259120bf50deSTom Herbert 2592d8e18a51SMatthew Wilcox (Oracle) if (offset < skb_frag_size(frag)) 259320bf50deSTom Herbert break; 259420bf50deSTom Herbert 2595d8e18a51SMatthew Wilcox (Oracle) offset -= skb_frag_size(frag); 259620bf50deSTom Herbert } 259720bf50deSTom Herbert 259820bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 259920bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 260020bf50deSTom Herbert 2601d8e18a51SMatthew Wilcox (Oracle) slen = min_t(size_t, len, skb_frag_size(frag) - offset); 260220bf50deSTom Herbert 260320bf50deSTom Herbert while (slen) { 26040739cd28SCong Wang ret = INDIRECT_CALL_2(sendpage, kernel_sendpage_locked, 26050739cd28SCong Wang sendpage_unlocked, sk, 26060739cd28SCong Wang skb_frag_page(frag), 2607b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset, 260820bf50deSTom Herbert slen, MSG_DONTWAIT); 260920bf50deSTom Herbert if (ret <= 0) 261020bf50deSTom Herbert goto error; 261120bf50deSTom Herbert 261220bf50deSTom Herbert len -= ret; 261320bf50deSTom Herbert offset += ret; 261420bf50deSTom Herbert slen -= ret; 261520bf50deSTom Herbert } 261620bf50deSTom Herbert 261720bf50deSTom Herbert offset = 0; 261820bf50deSTom Herbert } 261920bf50deSTom Herbert 262020bf50deSTom Herbert if (len) { 262120bf50deSTom Herbert /* Process any frag lists */ 262220bf50deSTom Herbert 262320bf50deSTom Herbert if (skb == head) { 262420bf50deSTom Herbert if (skb_has_frag_list(skb)) { 262520bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 262620bf50deSTom Herbert goto do_frag_list; 262720bf50deSTom Herbert } 262820bf50deSTom Herbert } else if (skb->next) { 262920bf50deSTom Herbert skb = skb->next; 263020bf50deSTom Herbert goto do_frag_list; 263120bf50deSTom Herbert } 263220bf50deSTom Herbert } 263320bf50deSTom Herbert 263420bf50deSTom Herbert out: 263520bf50deSTom Herbert return orig_len - len; 263620bf50deSTom Herbert 263720bf50deSTom Herbert error: 263820bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 263920bf50deSTom Herbert } 26400739cd28SCong Wang 26410739cd28SCong Wang /* Send skb data on a socket. Socket must be locked. */ 26420739cd28SCong Wang int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 26430739cd28SCong Wang int len) 26440739cd28SCong Wang { 26450739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, kernel_sendmsg_locked, 26460739cd28SCong Wang kernel_sendpage_locked); 26470739cd28SCong Wang } 264820bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 264920bf50deSTom Herbert 26500739cd28SCong Wang /* Send skb data on a socket. Socket must be unlocked. */ 26510739cd28SCong Wang int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 26520739cd28SCong Wang { 26530739cd28SCong Wang return __skb_send_sock(sk, skb, offset, len, sendmsg_unlocked, 26540739cd28SCong Wang sendpage_unlocked); 26550739cd28SCong Wang } 26560739cd28SCong Wang 2657357b40a1SHerbert Xu /** 2658357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2659357b40a1SHerbert Xu * @skb: destination buffer 2660357b40a1SHerbert Xu * @offset: offset in destination 2661357b40a1SHerbert Xu * @from: source buffer 2662357b40a1SHerbert Xu * @len: number of bytes to copy 2663357b40a1SHerbert Xu * 2664357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2665357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2666357b40a1SHerbert Xu * traversing fragment lists and such. 2667357b40a1SHerbert Xu */ 2668357b40a1SHerbert Xu 26690c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2670357b40a1SHerbert Xu { 26711a028e50SDavid S. Miller int start = skb_headlen(skb); 2672fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2673fbb398a8SDavid S. Miller int i, copy; 2674357b40a1SHerbert Xu 2675357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2676357b40a1SHerbert Xu goto fault; 2677357b40a1SHerbert Xu 26781a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2679357b40a1SHerbert Xu if (copy > len) 2680357b40a1SHerbert Xu copy = len; 268127d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2682357b40a1SHerbert Xu if ((len -= copy) == 0) 2683357b40a1SHerbert Xu return 0; 2684357b40a1SHerbert Xu offset += copy; 2685357b40a1SHerbert Xu from += copy; 2686357b40a1SHerbert Xu } 2687357b40a1SHerbert Xu 2688357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2689357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 26901a028e50SDavid S. Miller int end; 2691357b40a1SHerbert Xu 2692547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26931a028e50SDavid S. Miller 26949e903e08SEric Dumazet end = start + skb_frag_size(frag); 2695357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2696c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2697c613c209SWillem de Bruijn struct page *p; 2698357b40a1SHerbert Xu u8 *vaddr; 2699357b40a1SHerbert Xu 2700357b40a1SHerbert Xu if (copy > len) 2701357b40a1SHerbert Xu copy = len; 2702357b40a1SHerbert Xu 2703c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2704b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2705c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2706c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2707c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 270851c56b00SEric Dumazet kunmap_atomic(vaddr); 2709c613c209SWillem de Bruijn } 2710357b40a1SHerbert Xu 2711357b40a1SHerbert Xu if ((len -= copy) == 0) 2712357b40a1SHerbert Xu return 0; 2713357b40a1SHerbert Xu offset += copy; 2714357b40a1SHerbert Xu from += copy; 2715357b40a1SHerbert Xu } 27161a028e50SDavid S. Miller start = end; 2717357b40a1SHerbert Xu } 2718357b40a1SHerbert Xu 2719fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 27201a028e50SDavid S. Miller int end; 2721357b40a1SHerbert Xu 2722547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27231a028e50SDavid S. Miller 2724fbb398a8SDavid S. Miller end = start + frag_iter->len; 2725357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2726357b40a1SHerbert Xu if (copy > len) 2727357b40a1SHerbert Xu copy = len; 2728fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 27291a028e50SDavid S. Miller from, copy)) 2730357b40a1SHerbert Xu goto fault; 2731357b40a1SHerbert Xu if ((len -= copy) == 0) 2732357b40a1SHerbert Xu return 0; 2733357b40a1SHerbert Xu offset += copy; 2734357b40a1SHerbert Xu from += copy; 2735357b40a1SHerbert Xu } 27361a028e50SDavid S. Miller start = end; 2737357b40a1SHerbert Xu } 2738357b40a1SHerbert Xu if (!len) 2739357b40a1SHerbert Xu return 0; 2740357b40a1SHerbert Xu 2741357b40a1SHerbert Xu fault: 2742357b40a1SHerbert Xu return -EFAULT; 2743357b40a1SHerbert Xu } 2744357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2745357b40a1SHerbert Xu 27461da177e4SLinus Torvalds /* Checksum skb data. */ 27472817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 27482817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 27491da177e4SLinus Torvalds { 27501a028e50SDavid S. Miller int start = skb_headlen(skb); 27511a028e50SDavid S. Miller int i, copy = start - offset; 2752fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 27531da177e4SLinus Torvalds int pos = 0; 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds /* Checksum header. */ 27561da177e4SLinus Torvalds if (copy > 0) { 27571da177e4SLinus Torvalds if (copy > len) 27581da177e4SLinus Torvalds copy = len; 27592544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->update, csum_partial_ext, 27602544af03SMatteo Croce skb->data + offset, copy, csum); 27611da177e4SLinus Torvalds if ((len -= copy) == 0) 27621da177e4SLinus Torvalds return csum; 27631da177e4SLinus Torvalds offset += copy; 27641da177e4SLinus Torvalds pos = copy; 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 27681a028e50SDavid S. Miller int end; 276951c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 27701da177e4SLinus Torvalds 2771547b792cSIlpo Järvinen WARN_ON(start > offset + len); 27721a028e50SDavid S. Miller 277351c56b00SEric Dumazet end = start + skb_frag_size(frag); 27741da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2775c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2776c613c209SWillem de Bruijn struct page *p; 277744bb9363SAl Viro __wsum csum2; 27781da177e4SLinus Torvalds u8 *vaddr; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds if (copy > len) 27811da177e4SLinus Torvalds copy = len; 2782c613c209SWillem de Bruijn 2783c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2784b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2785c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2786c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 27872544af03SMatteo Croce csum2 = INDIRECT_CALL_1(ops->update, 27882544af03SMatteo Croce csum_partial_ext, 27892544af03SMatteo Croce vaddr + p_off, p_len, 0); 279051c56b00SEric Dumazet kunmap_atomic(vaddr); 27912544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, 27922544af03SMatteo Croce csum_block_add_ext, csum, 27932544af03SMatteo Croce csum2, pos, p_len); 2794c613c209SWillem de Bruijn pos += p_len; 2795c613c209SWillem de Bruijn } 2796c613c209SWillem de Bruijn 27971da177e4SLinus Torvalds if (!(len -= copy)) 27981da177e4SLinus Torvalds return csum; 27991da177e4SLinus Torvalds offset += copy; 28001da177e4SLinus Torvalds } 28011a028e50SDavid S. Miller start = end; 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 2804fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 28051a028e50SDavid S. Miller int end; 28061da177e4SLinus Torvalds 2807547b792cSIlpo Järvinen WARN_ON(start > offset + len); 28081a028e50SDavid S. Miller 2809fbb398a8SDavid S. Miller end = start + frag_iter->len; 28101da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 28115f92a738SAl Viro __wsum csum2; 28121da177e4SLinus Torvalds if (copy > len) 28131da177e4SLinus Torvalds copy = len; 28142817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 28152817a336SDaniel Borkmann copy, 0, ops); 28162544af03SMatteo Croce csum = INDIRECT_CALL_1(ops->combine, csum_block_add_ext, 28172544af03SMatteo Croce csum, csum2, pos, copy); 28181da177e4SLinus Torvalds if ((len -= copy) == 0) 28191da177e4SLinus Torvalds return csum; 28201da177e4SLinus Torvalds offset += copy; 28211da177e4SLinus Torvalds pos += copy; 28221da177e4SLinus Torvalds } 28231a028e50SDavid S. Miller start = end; 28241da177e4SLinus Torvalds } 282509a62660SKris Katterjohn BUG_ON(len); 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds return csum; 28281da177e4SLinus Torvalds } 28292817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 28302817a336SDaniel Borkmann 28312817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 28322817a336SDaniel Borkmann int len, __wsum csum) 28332817a336SDaniel Borkmann { 28342817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2835cea80ea8SDaniel Borkmann .update = csum_partial_ext, 28362817a336SDaniel Borkmann .combine = csum_block_add_ext, 28372817a336SDaniel Borkmann }; 28382817a336SDaniel Borkmann 28392817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 28402817a336SDaniel Borkmann } 2841b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds /* Both of above in one bottle. */ 28441da177e4SLinus Torvalds 284581d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 28468d5930dfSAl Viro u8 *to, int len) 28471da177e4SLinus Torvalds { 28481a028e50SDavid S. Miller int start = skb_headlen(skb); 28491a028e50SDavid S. Miller int i, copy = start - offset; 2850fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 28511da177e4SLinus Torvalds int pos = 0; 28528d5930dfSAl Viro __wsum csum = 0; 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds /* Copy header. */ 28551da177e4SLinus Torvalds if (copy > 0) { 28561da177e4SLinus Torvalds if (copy > len) 28571da177e4SLinus Torvalds copy = len; 28581da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 2859cc44c17bSAl Viro copy); 28601da177e4SLinus Torvalds if ((len -= copy) == 0) 28611da177e4SLinus Torvalds return csum; 28621da177e4SLinus Torvalds offset += copy; 28631da177e4SLinus Torvalds to += copy; 28641da177e4SLinus Torvalds pos = copy; 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 28681a028e50SDavid S. Miller int end; 28691da177e4SLinus Torvalds 2870547b792cSIlpo Järvinen WARN_ON(start > offset + len); 28711a028e50SDavid S. Miller 28729e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 28731da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2874c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2875c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2876c613c209SWillem de Bruijn struct page *p; 28775084205fSAl Viro __wsum csum2; 28781da177e4SLinus Torvalds u8 *vaddr; 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds if (copy > len) 28811da177e4SLinus Torvalds copy = len; 2882c613c209SWillem de Bruijn 2883c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2884b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start, 2885c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2886c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2887c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2888c613c209SWillem de Bruijn to + copied, 2889cc44c17bSAl Viro p_len); 289051c56b00SEric Dumazet kunmap_atomic(vaddr); 28911da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 2892c613c209SWillem de Bruijn pos += p_len; 2893c613c209SWillem de Bruijn } 2894c613c209SWillem de Bruijn 28951da177e4SLinus Torvalds if (!(len -= copy)) 28961da177e4SLinus Torvalds return csum; 28971da177e4SLinus Torvalds offset += copy; 28981da177e4SLinus Torvalds to += copy; 28991da177e4SLinus Torvalds } 29001a028e50SDavid S. Miller start = end; 29011da177e4SLinus Torvalds } 29021da177e4SLinus Torvalds 2903fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 290481d77662SAl Viro __wsum csum2; 29051a028e50SDavid S. Miller int end; 29061da177e4SLinus Torvalds 2907547b792cSIlpo Järvinen WARN_ON(start > offset + len); 29081a028e50SDavid S. Miller 2909fbb398a8SDavid S. Miller end = start + frag_iter->len; 29101da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 29111da177e4SLinus Torvalds if (copy > len) 29121da177e4SLinus Torvalds copy = len; 2913fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 29141a028e50SDavid S. Miller offset - start, 29158d5930dfSAl Viro to, copy); 29161da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 29171da177e4SLinus Torvalds if ((len -= copy) == 0) 29181da177e4SLinus Torvalds return csum; 29191da177e4SLinus Torvalds offset += copy; 29201da177e4SLinus Torvalds to += copy; 29211da177e4SLinus Torvalds pos += copy; 29221da177e4SLinus Torvalds } 29231a028e50SDavid S. Miller start = end; 29241da177e4SLinus Torvalds } 292509a62660SKris Katterjohn BUG_ON(len); 29261da177e4SLinus Torvalds return csum; 29271da177e4SLinus Torvalds } 2928b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 29291da177e4SLinus Torvalds 293049f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 293149f8e832SCong Wang { 293249f8e832SCong Wang __sum16 sum; 293349f8e832SCong Wang 293449f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 293514641931SCong Wang /* See comments in __skb_checksum_complete(). */ 293649f8e832SCong Wang if (likely(!sum)) { 293749f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 293849f8e832SCong Wang !skb->csum_complete_sw) 29397fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 294049f8e832SCong Wang } 294149f8e832SCong Wang if (!skb_shared(skb)) 294249f8e832SCong Wang skb->csum_valid = !sum; 294349f8e832SCong Wang return sum; 294449f8e832SCong Wang } 294549f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 294649f8e832SCong Wang 294714641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 294814641931SCong Wang * which has been changed from the hardware checksum, for example, by 294914641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 295014641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 295114641931SCong Wang * 295214641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 295314641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 295414641931SCong Wang * shared. 295514641931SCong Wang */ 295649f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 295749f8e832SCong Wang { 295849f8e832SCong Wang __wsum csum; 295949f8e832SCong Wang __sum16 sum; 296049f8e832SCong Wang 296149f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 296249f8e832SCong Wang 296349f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 296414641931SCong Wang /* This check is inverted, because we already knew the hardware 296514641931SCong Wang * checksum is invalid before calling this function. So, if the 296614641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 296714641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 296814641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 296914641931SCong Wang * when moving skb->data around. 297014641931SCong Wang */ 297149f8e832SCong Wang if (likely(!sum)) { 297249f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 297349f8e832SCong Wang !skb->csum_complete_sw) 29747fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 297549f8e832SCong Wang } 297649f8e832SCong Wang 297749f8e832SCong Wang if (!skb_shared(skb)) { 297849f8e832SCong Wang /* Save full packet checksum */ 297949f8e832SCong Wang skb->csum = csum; 298049f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 298149f8e832SCong Wang skb->csum_complete_sw = 1; 298249f8e832SCong Wang skb->csum_valid = !sum; 298349f8e832SCong Wang } 298449f8e832SCong Wang 298549f8e832SCong Wang return sum; 298649f8e832SCong Wang } 298749f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 298849f8e832SCong Wang 29899617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 29909617813dSDavide Caratti { 29919617813dSDavide Caratti net_warn_ratelimited( 29929617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 29939617813dSDavide Caratti __func__); 29949617813dSDavide Caratti return 0; 29959617813dSDavide Caratti } 29969617813dSDavide Caratti 29979617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 29989617813dSDavide Caratti int offset, int len) 29999617813dSDavide Caratti { 30009617813dSDavide Caratti net_warn_ratelimited( 30019617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 30029617813dSDavide Caratti __func__); 30039617813dSDavide Caratti return 0; 30049617813dSDavide Caratti } 30059617813dSDavide Caratti 30069617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 30079617813dSDavide Caratti .update = warn_crc32c_csum_update, 30089617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 30099617813dSDavide Caratti }; 30109617813dSDavide Caratti 30119617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 30129617813dSDavide Caratti &default_crc32c_ops; 30139617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 30149617813dSDavide Caratti 3015af2806f8SThomas Graf /** 3016af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 3017af2806f8SThomas Graf * @from: source buffer 3018af2806f8SThomas Graf * 3019af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 3020af2806f8SThomas Graf * into skb_zerocopy(). 3021af2806f8SThomas Graf */ 3022af2806f8SThomas Graf unsigned int 3023af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 3024af2806f8SThomas Graf { 3025af2806f8SThomas Graf unsigned int hlen = 0; 3026af2806f8SThomas Graf 3027af2806f8SThomas Graf if (!from->head_frag || 3028af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 3029a17ad096SPravin B Shelar skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) { 3030af2806f8SThomas Graf hlen = skb_headlen(from); 3031a17ad096SPravin B Shelar if (!hlen) 3032a17ad096SPravin B Shelar hlen = from->len; 3033a17ad096SPravin B Shelar } 3034af2806f8SThomas Graf 3035af2806f8SThomas Graf if (skb_has_frag_list(from)) 3036af2806f8SThomas Graf hlen = from->len; 3037af2806f8SThomas Graf 3038af2806f8SThomas Graf return hlen; 3039af2806f8SThomas Graf } 3040af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 3041af2806f8SThomas Graf 3042af2806f8SThomas Graf /** 3043af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 3044af2806f8SThomas Graf * @to: destination buffer 30457fceb4deSMasanari Iida * @from: source buffer 3046af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 3047af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 3048af2806f8SThomas Graf * 3049af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 3050af2806f8SThomas Graf * to the frags in the source buffer. 3051af2806f8SThomas Graf * 3052af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 3053af2806f8SThomas Graf * headroom in the `to` buffer. 305436d5fe6aSZoltan Kiss * 305536d5fe6aSZoltan Kiss * Return value: 305636d5fe6aSZoltan Kiss * 0: everything is OK 305736d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 305836d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 3059af2806f8SThomas Graf */ 306036d5fe6aSZoltan Kiss int 306136d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 3062af2806f8SThomas Graf { 3063af2806f8SThomas Graf int i, j = 0; 3064af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 306536d5fe6aSZoltan Kiss int ret; 3066af2806f8SThomas Graf struct page *page; 3067af2806f8SThomas Graf unsigned int offset; 3068af2806f8SThomas Graf 3069af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 3070af2806f8SThomas Graf 3071af2806f8SThomas Graf /* dont bother with small payloads */ 307236d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 307336d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 3074af2806f8SThomas Graf 3075af2806f8SThomas Graf if (hlen) { 307636d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 307736d5fe6aSZoltan Kiss if (unlikely(ret)) 307836d5fe6aSZoltan Kiss return ret; 3079af2806f8SThomas Graf len -= hlen; 3080af2806f8SThomas Graf } else { 3081af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 3082af2806f8SThomas Graf if (plen) { 3083af2806f8SThomas Graf page = virt_to_head_page(from->head); 3084af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 3085af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 3086af2806f8SThomas Graf get_page(page); 3087af2806f8SThomas Graf j = 1; 3088af2806f8SThomas Graf len -= plen; 3089af2806f8SThomas Graf } 3090af2806f8SThomas Graf } 3091af2806f8SThomas Graf 3092af2806f8SThomas Graf to->truesize += len + plen; 3093af2806f8SThomas Graf to->len += len + plen; 3094af2806f8SThomas Graf to->data_len += len + plen; 3095af2806f8SThomas Graf 309636d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 309736d5fe6aSZoltan Kiss skb_tx_error(from); 309836d5fe6aSZoltan Kiss return -ENOMEM; 309936d5fe6aSZoltan Kiss } 31001f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 310136d5fe6aSZoltan Kiss 3102af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 3103d8e18a51SMatthew Wilcox (Oracle) int size; 3104d8e18a51SMatthew Wilcox (Oracle) 3105af2806f8SThomas Graf if (!len) 3106af2806f8SThomas Graf break; 3107af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 3108d8e18a51SMatthew Wilcox (Oracle) size = min_t(int, skb_frag_size(&skb_shinfo(to)->frags[j]), 3109d8e18a51SMatthew Wilcox (Oracle) len); 3110d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&skb_shinfo(to)->frags[j], size); 3111d8e18a51SMatthew Wilcox (Oracle) len -= size; 3112af2806f8SThomas Graf skb_frag_ref(to, j); 3113af2806f8SThomas Graf j++; 3114af2806f8SThomas Graf } 3115af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 311636d5fe6aSZoltan Kiss 311736d5fe6aSZoltan Kiss return 0; 3118af2806f8SThomas Graf } 3119af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 3120af2806f8SThomas Graf 31211da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 31221da177e4SLinus Torvalds { 3123d3bc23e7SAl Viro __wsum csum; 31241da177e4SLinus Torvalds long csstart; 31251da177e4SLinus Torvalds 312684fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 312755508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 31281da177e4SLinus Torvalds else 31291da177e4SLinus Torvalds csstart = skb_headlen(skb); 31301da177e4SLinus Torvalds 313109a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 31321da177e4SLinus Torvalds 3133d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 31341da177e4SLinus Torvalds 31351da177e4SLinus Torvalds csum = 0; 31361da177e4SLinus Torvalds if (csstart != skb->len) 31371da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 31388d5930dfSAl Viro skb->len - csstart); 31391da177e4SLinus Torvalds 314084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 3141ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 31421da177e4SLinus Torvalds 3143d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 31441da177e4SLinus Torvalds } 31451da177e4SLinus Torvalds } 3146b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds /** 31491da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 31501da177e4SLinus Torvalds * @list: list to dequeue from 31511da177e4SLinus Torvalds * 31521da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 31531da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 31541da177e4SLinus Torvalds * returned or %NULL if the list is empty. 31551da177e4SLinus Torvalds */ 31561da177e4SLinus Torvalds 31571da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 31581da177e4SLinus Torvalds { 31591da177e4SLinus Torvalds unsigned long flags; 31601da177e4SLinus Torvalds struct sk_buff *result; 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31631da177e4SLinus Torvalds result = __skb_dequeue(list); 31641da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31651da177e4SLinus Torvalds return result; 31661da177e4SLinus Torvalds } 3167b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds /** 31701da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 31711da177e4SLinus Torvalds * @list: list to dequeue from 31721da177e4SLinus Torvalds * 31731da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 31741da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 31751da177e4SLinus Torvalds * returned or %NULL if the list is empty. 31761da177e4SLinus Torvalds */ 31771da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 31781da177e4SLinus Torvalds { 31791da177e4SLinus Torvalds unsigned long flags; 31801da177e4SLinus Torvalds struct sk_buff *result; 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 31831da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 31841da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 31851da177e4SLinus Torvalds return result; 31861da177e4SLinus Torvalds } 3187b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 31881da177e4SLinus Torvalds 31891da177e4SLinus Torvalds /** 31901da177e4SLinus Torvalds * skb_queue_purge - empty a list 31911da177e4SLinus Torvalds * @list: list to empty 31921da177e4SLinus Torvalds * 31931da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 31941da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 31951da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 31961da177e4SLinus Torvalds */ 31971da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 31981da177e4SLinus Torvalds { 31991da177e4SLinus Torvalds struct sk_buff *skb; 32001da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 32011da177e4SLinus Torvalds kfree_skb(skb); 32021da177e4SLinus Torvalds } 3203b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds /** 32069f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 32079f5afeaeSYaogong Wang * @root: root of the rbtree to empty 3208385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 32099f5afeaeSYaogong Wang * 32109f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 32119f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 32129f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 32139f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 32149f5afeaeSYaogong Wang */ 3215385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 32169f5afeaeSYaogong Wang { 32177c90584cSEric Dumazet struct rb_node *p = rb_first(root); 3218385114deSPeter Oskolkov unsigned int sum = 0; 32199f5afeaeSYaogong Wang 32207c90584cSEric Dumazet while (p) { 32217c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 32227c90584cSEric Dumazet 32237c90584cSEric Dumazet p = rb_next(p); 32247c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 3225385114deSPeter Oskolkov sum += skb->truesize; 32269f5afeaeSYaogong Wang kfree_skb(skb); 32277c90584cSEric Dumazet } 3228385114deSPeter Oskolkov return sum; 32299f5afeaeSYaogong Wang } 32309f5afeaeSYaogong Wang 32319f5afeaeSYaogong Wang /** 32321da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 32331da177e4SLinus Torvalds * @list: list to use 32341da177e4SLinus Torvalds * @newsk: buffer to queue 32351da177e4SLinus Torvalds * 32361da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 32371da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 32381da177e4SLinus Torvalds * safely. 32391da177e4SLinus Torvalds * 32401da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 32411da177e4SLinus Torvalds */ 32421da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 32431da177e4SLinus Torvalds { 32441da177e4SLinus Torvalds unsigned long flags; 32451da177e4SLinus Torvalds 32461da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 32471da177e4SLinus Torvalds __skb_queue_head(list, newsk); 32481da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 32491da177e4SLinus Torvalds } 3250b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 32511da177e4SLinus Torvalds 32521da177e4SLinus Torvalds /** 32531da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 32541da177e4SLinus Torvalds * @list: list to use 32551da177e4SLinus Torvalds * @newsk: buffer to queue 32561da177e4SLinus Torvalds * 32571da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 32581da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 32591da177e4SLinus Torvalds * safely. 32601da177e4SLinus Torvalds * 32611da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 32621da177e4SLinus Torvalds */ 32631da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 32641da177e4SLinus Torvalds { 32651da177e4SLinus Torvalds unsigned long flags; 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 32681da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 32691da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 32701da177e4SLinus Torvalds } 3271b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 32728728b834SDavid S. Miller 32731da177e4SLinus Torvalds /** 32741da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 32751da177e4SLinus Torvalds * @skb: buffer to remove 32768728b834SDavid S. Miller * @list: list to use 32771da177e4SLinus Torvalds * 32788728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 32798728b834SDavid S. Miller * function is atomic with respect to other list locked calls 32801da177e4SLinus Torvalds * 32818728b834SDavid S. Miller * You must know what list the SKB is on. 32821da177e4SLinus Torvalds */ 32838728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 32841da177e4SLinus Torvalds { 32851da177e4SLinus Torvalds unsigned long flags; 32861da177e4SLinus Torvalds 32871da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 32888728b834SDavid S. Miller __skb_unlink(skb, list); 32891da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 32901da177e4SLinus Torvalds } 3291b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 32921da177e4SLinus Torvalds 32931da177e4SLinus Torvalds /** 32941da177e4SLinus Torvalds * skb_append - append a buffer 32951da177e4SLinus Torvalds * @old: buffer to insert after 32961da177e4SLinus Torvalds * @newsk: buffer to insert 32978728b834SDavid S. Miller * @list: list to use 32981da177e4SLinus Torvalds * 32991da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 33001da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 33011da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 33021da177e4SLinus Torvalds */ 33038728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 33041da177e4SLinus Torvalds { 33051da177e4SLinus Torvalds unsigned long flags; 33061da177e4SLinus Torvalds 33078728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 33087de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 33098728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 33101da177e4SLinus Torvalds } 3311b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 33141da177e4SLinus Torvalds struct sk_buff* skb1, 33151da177e4SLinus Torvalds const u32 len, const int pos) 33161da177e4SLinus Torvalds { 33171da177e4SLinus Torvalds int i; 33181da177e4SLinus Torvalds 3319d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3320d626f62bSArnaldo Carvalho de Melo pos - len); 33211da177e4SLinus Torvalds /* And move data appendix as is. */ 33221da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 33231da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 33241da177e4SLinus Torvalds 33251da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 33261da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 33271da177e4SLinus Torvalds skb1->data_len = skb->data_len; 33281da177e4SLinus Torvalds skb1->len += skb1->data_len; 33291da177e4SLinus Torvalds skb->data_len = 0; 33301da177e4SLinus Torvalds skb->len = len; 333127a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 33321da177e4SLinus Torvalds } 33331da177e4SLinus Torvalds 33341da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 33351da177e4SLinus Torvalds struct sk_buff* skb1, 33361da177e4SLinus Torvalds const u32 len, int pos) 33371da177e4SLinus Torvalds { 33381da177e4SLinus Torvalds int i, k = 0; 33391da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 33401da177e4SLinus Torvalds 33411da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 33421da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 33431da177e4SLinus Torvalds skb->len = len; 33441da177e4SLinus Torvalds skb->data_len = len - pos; 33451da177e4SLinus Torvalds 33461da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 33479e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 33481da177e4SLinus Torvalds 33491da177e4SLinus Torvalds if (pos + size > len) { 33501da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 33511da177e4SLinus Torvalds 33521da177e4SLinus Torvalds if (pos < len) { 33531da177e4SLinus Torvalds /* Split frag. 33541da177e4SLinus Torvalds * We have two variants in this case: 33551da177e4SLinus Torvalds * 1. Move all the frag to the second 33561da177e4SLinus Torvalds * part, if it is possible. F.e. 33571da177e4SLinus Torvalds * this approach is mandatory for TUX, 33581da177e4SLinus Torvalds * where splitting is expensive. 33591da177e4SLinus Torvalds * 2. Split is accurately. We make this. 33601da177e4SLinus Torvalds */ 3361ea2ab693SIan Campbell skb_frag_ref(skb, i); 3362b54c9d5bSJonathan Lemon skb_frag_off_add(&skb_shinfo(skb1)->frags[0], len - pos); 33639e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 33649e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 33651da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds k++; 33681da177e4SLinus Torvalds } else 33691da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 33701da177e4SLinus Torvalds pos += size; 33711da177e4SLinus Torvalds } 33721da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 33731da177e4SLinus Torvalds } 33741da177e4SLinus Torvalds 33751da177e4SLinus Torvalds /** 33761da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 33771da177e4SLinus Torvalds * @skb: the buffer to split 33781da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 33791da177e4SLinus Torvalds * @len: new length for skb 33801da177e4SLinus Torvalds */ 33811da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 33821da177e4SLinus Torvalds { 33831da177e4SLinus Torvalds int pos = skb_headlen(skb); 33841da177e4SLinus Torvalds 338506b4feb3SJonathan Lemon skb_shinfo(skb1)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG; 33861f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 33871da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 33881da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 33891da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 33901da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 33911da177e4SLinus Torvalds } 3392b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 33931da177e4SLinus Torvalds 33949f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 33959f782db3SIlpo Järvinen * 33969f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 33979f782db3SIlpo Järvinen */ 3398832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3399832d11c5SIlpo Järvinen { 3400097b9146SMarco Elver int ret = 0; 3401097b9146SMarco Elver 3402097b9146SMarco Elver if (skb_cloned(skb)) { 3403097b9146SMarco Elver /* Save and restore truesize: pskb_expand_head() may reallocate 3404097b9146SMarco Elver * memory where ksize(kmalloc(S)) != ksize(kmalloc(S)), but we 3405097b9146SMarco Elver * cannot change truesize at this point. 3406097b9146SMarco Elver */ 3407097b9146SMarco Elver unsigned int save_truesize = skb->truesize; 3408097b9146SMarco Elver 3409097b9146SMarco Elver ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3410097b9146SMarco Elver skb->truesize = save_truesize; 3411097b9146SMarco Elver } 3412097b9146SMarco Elver return ret; 3413832d11c5SIlpo Järvinen } 3414832d11c5SIlpo Järvinen 3415832d11c5SIlpo Järvinen /** 3416832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3417832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3418832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3419832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3420832d11c5SIlpo Järvinen * 3421832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 342220e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3423832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3424832d11c5SIlpo Järvinen * 3425832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3426832d11c5SIlpo Järvinen * 3427832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3428832d11c5SIlpo Järvinen * to have non-paged data as well. 3429832d11c5SIlpo Järvinen * 3430832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3431832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3432832d11c5SIlpo Järvinen */ 3433832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3434832d11c5SIlpo Järvinen { 3435832d11c5SIlpo Järvinen int from, to, merge, todo; 3436d8e18a51SMatthew Wilcox (Oracle) skb_frag_t *fragfrom, *fragto; 3437832d11c5SIlpo Järvinen 3438832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3439f8071cdeSEric Dumazet 3440f8071cdeSEric Dumazet if (skb_headlen(skb)) 3441f8071cdeSEric Dumazet return 0; 34421f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 34431f8b977aSWillem de Bruijn return 0; 3444832d11c5SIlpo Järvinen 3445832d11c5SIlpo Järvinen todo = shiftlen; 3446832d11c5SIlpo Järvinen from = 0; 3447832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3448832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3449832d11c5SIlpo Järvinen 3450832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3451832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3452832d11c5SIlpo Järvinen */ 3453832d11c5SIlpo Järvinen if (!to || 3454ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3455b54c9d5bSJonathan Lemon skb_frag_off(fragfrom))) { 3456832d11c5SIlpo Järvinen merge = -1; 3457832d11c5SIlpo Järvinen } else { 3458832d11c5SIlpo Järvinen merge = to - 1; 3459832d11c5SIlpo Järvinen 34609e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3461832d11c5SIlpo Järvinen if (todo < 0) { 3462832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3463832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3464832d11c5SIlpo Järvinen return 0; 3465832d11c5SIlpo Järvinen 34669f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 34679f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3468832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3469832d11c5SIlpo Järvinen 34709e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 34719e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3472b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, shiftlen); 3473832d11c5SIlpo Järvinen 3474832d11c5SIlpo Järvinen goto onlymerged; 3475832d11c5SIlpo Järvinen } 3476832d11c5SIlpo Järvinen 3477832d11c5SIlpo Järvinen from++; 3478832d11c5SIlpo Järvinen } 3479832d11c5SIlpo Järvinen 3480832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3481832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3482832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3483832d11c5SIlpo Järvinen return 0; 3484832d11c5SIlpo Järvinen 3485832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3486832d11c5SIlpo Järvinen return 0; 3487832d11c5SIlpo Järvinen 3488832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3489832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3490832d11c5SIlpo Järvinen return 0; 3491832d11c5SIlpo Järvinen 3492832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3493832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3494832d11c5SIlpo Järvinen 34959e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3496832d11c5SIlpo Järvinen *fragto = *fragfrom; 34979e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3498832d11c5SIlpo Järvinen from++; 3499832d11c5SIlpo Järvinen to++; 3500832d11c5SIlpo Järvinen 3501832d11c5SIlpo Järvinen } else { 3502ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3503b54c9d5bSJonathan Lemon skb_frag_page_copy(fragto, fragfrom); 3504b54c9d5bSJonathan Lemon skb_frag_off_copy(fragto, fragfrom); 35059e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3506832d11c5SIlpo Järvinen 3507b54c9d5bSJonathan Lemon skb_frag_off_add(fragfrom, todo); 35089e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3509832d11c5SIlpo Järvinen todo = 0; 3510832d11c5SIlpo Järvinen 3511832d11c5SIlpo Järvinen to++; 3512832d11c5SIlpo Järvinen break; 3513832d11c5SIlpo Järvinen } 3514832d11c5SIlpo Järvinen } 3515832d11c5SIlpo Järvinen 3516832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3517832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3518832d11c5SIlpo Järvinen 3519832d11c5SIlpo Järvinen if (merge >= 0) { 3520832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3521832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3522832d11c5SIlpo Järvinen 35239e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 35246a5bcd84SIlias Apalodimas __skb_frag_unref(fragfrom, skb->pp_recycle); 3525832d11c5SIlpo Järvinen } 3526832d11c5SIlpo Järvinen 3527832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3528832d11c5SIlpo Järvinen to = 0; 3529832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3530832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3531832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3532832d11c5SIlpo Järvinen 3533832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3534832d11c5SIlpo Järvinen 3535832d11c5SIlpo Järvinen onlymerged: 3536832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3537832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3538832d11c5SIlpo Järvinen */ 3539832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3540832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3541832d11c5SIlpo Järvinen 3542832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 3543832d11c5SIlpo Järvinen skb->len -= shiftlen; 3544832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 3545832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 3546832d11c5SIlpo Järvinen tgt->len += shiftlen; 3547832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 3548832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 3549832d11c5SIlpo Järvinen 3550832d11c5SIlpo Järvinen return shiftlen; 3551832d11c5SIlpo Järvinen } 3552832d11c5SIlpo Järvinen 3553677e90edSThomas Graf /** 3554677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3555677e90edSThomas Graf * @skb: the buffer to read 3556677e90edSThomas Graf * @from: lower offset of data to be read 3557677e90edSThomas Graf * @to: upper offset of data to be read 3558677e90edSThomas Graf * @st: state variable 3559677e90edSThomas Graf * 3560677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3561677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3562677e90edSThomas Graf */ 3563677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3564677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3565677e90edSThomas Graf { 3566677e90edSThomas Graf st->lower_offset = from; 3567677e90edSThomas Graf st->upper_offset = to; 3568677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3569677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3570677e90edSThomas Graf st->frag_data = NULL; 357197550f6fSWillem de Bruijn st->frag_off = 0; 3572677e90edSThomas Graf } 3573b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3574677e90edSThomas Graf 3575677e90edSThomas Graf /** 3576677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3577677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3578677e90edSThomas Graf * @data: destination pointer for data to be returned 3579677e90edSThomas Graf * @st: state variable 3580677e90edSThomas Graf * 3581bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3582677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3583bc32383cSMathias Krause * the head of the data block to @data and returns the length 3584677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3585677e90edSThomas Graf * offset has been reached. 3586677e90edSThomas Graf * 3587677e90edSThomas Graf * The caller is not required to consume all of the data 3588bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3589677e90edSThomas Graf * of bytes already consumed and the next call to 3590677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3591677e90edSThomas Graf * 359225985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3593e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3594677e90edSThomas Graf * reads of potentially non linear data. 3595677e90edSThomas Graf * 3596bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3597677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3598677e90edSThomas Graf * a stack for this purpose. 3599677e90edSThomas Graf */ 3600677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3601677e90edSThomas Graf struct skb_seq_state *st) 3602677e90edSThomas Graf { 3603677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3604677e90edSThomas Graf skb_frag_t *frag; 3605677e90edSThomas Graf 3606aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3607aeb193eaSWedson Almeida Filho if (st->frag_data) { 3608aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3609aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3610aeb193eaSWedson Almeida Filho } 3611677e90edSThomas Graf return 0; 3612aeb193eaSWedson Almeida Filho } 3613677e90edSThomas Graf 3614677e90edSThomas Graf next_skb: 361595e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3616677e90edSThomas Graf 3617995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 361895e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3619677e90edSThomas Graf return block_limit - abs_offset; 3620677e90edSThomas Graf } 3621677e90edSThomas Graf 3622677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3623677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3624677e90edSThomas Graf 3625677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 362697550f6fSWillem de Bruijn unsigned int pg_idx, pg_off, pg_sz; 3627677e90edSThomas Graf 362897550f6fSWillem de Bruijn frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 362997550f6fSWillem de Bruijn 363097550f6fSWillem de Bruijn pg_idx = 0; 363197550f6fSWillem de Bruijn pg_off = skb_frag_off(frag); 363297550f6fSWillem de Bruijn pg_sz = skb_frag_size(frag); 363397550f6fSWillem de Bruijn 363497550f6fSWillem de Bruijn if (skb_frag_must_loop(skb_frag_page(frag))) { 363597550f6fSWillem de Bruijn pg_idx = (pg_off + st->frag_off) >> PAGE_SHIFT; 363697550f6fSWillem de Bruijn pg_off = offset_in_page(pg_off + st->frag_off); 363797550f6fSWillem de Bruijn pg_sz = min_t(unsigned int, pg_sz - st->frag_off, 363897550f6fSWillem de Bruijn PAGE_SIZE - pg_off); 363997550f6fSWillem de Bruijn } 364097550f6fSWillem de Bruijn 364197550f6fSWillem de Bruijn block_limit = pg_sz + st->stepped_offset; 3642677e90edSThomas Graf if (abs_offset < block_limit) { 3643677e90edSThomas Graf if (!st->frag_data) 364497550f6fSWillem de Bruijn st->frag_data = kmap_atomic(skb_frag_page(frag) + pg_idx); 3645677e90edSThomas Graf 364697550f6fSWillem de Bruijn *data = (u8 *)st->frag_data + pg_off + 3647677e90edSThomas Graf (abs_offset - st->stepped_offset); 3648677e90edSThomas Graf 3649677e90edSThomas Graf return block_limit - abs_offset; 3650677e90edSThomas Graf } 3651677e90edSThomas Graf 3652677e90edSThomas Graf if (st->frag_data) { 365351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3654677e90edSThomas Graf st->frag_data = NULL; 3655677e90edSThomas Graf } 3656677e90edSThomas Graf 365797550f6fSWillem de Bruijn st->stepped_offset += pg_sz; 365897550f6fSWillem de Bruijn st->frag_off += pg_sz; 365997550f6fSWillem de Bruijn if (st->frag_off == skb_frag_size(frag)) { 366097550f6fSWillem de Bruijn st->frag_off = 0; 3661677e90edSThomas Graf st->frag_idx++; 366297550f6fSWillem de Bruijn } 3663677e90edSThomas Graf } 3664677e90edSThomas Graf 36655b5a60daSOlaf Kirch if (st->frag_data) { 366651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 36675b5a60daSOlaf Kirch st->frag_data = NULL; 36685b5a60daSOlaf Kirch } 36695b5a60daSOlaf Kirch 367021dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3671677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 367295e3b24cSHerbert Xu st->frag_idx = 0; 3673677e90edSThomas Graf goto next_skb; 367471b3346dSShyam Iyer } else if (st->cur_skb->next) { 367571b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 367671b3346dSShyam Iyer st->frag_idx = 0; 3677677e90edSThomas Graf goto next_skb; 3678677e90edSThomas Graf } 3679677e90edSThomas Graf 3680677e90edSThomas Graf return 0; 3681677e90edSThomas Graf } 3682b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3683677e90edSThomas Graf 3684677e90edSThomas Graf /** 3685677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3686677e90edSThomas Graf * @st: state variable 3687677e90edSThomas Graf * 3688677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3689677e90edSThomas Graf * returned 0. 3690677e90edSThomas Graf */ 3691677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3692677e90edSThomas Graf { 3693677e90edSThomas Graf if (st->frag_data) 369451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3695677e90edSThomas Graf } 3696b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3697677e90edSThomas Graf 36983fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 36993fc7e8a6SThomas Graf 37003fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 37013fc7e8a6SThomas Graf struct ts_config *conf, 37023fc7e8a6SThomas Graf struct ts_state *state) 37033fc7e8a6SThomas Graf { 37043fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 37053fc7e8a6SThomas Graf } 37063fc7e8a6SThomas Graf 37073fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 37083fc7e8a6SThomas Graf { 37093fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 37103fc7e8a6SThomas Graf } 37113fc7e8a6SThomas Graf 37123fc7e8a6SThomas Graf /** 37133fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 37143fc7e8a6SThomas Graf * @skb: the buffer to look in 37153fc7e8a6SThomas Graf * @from: search offset 37163fc7e8a6SThomas Graf * @to: search limit 37173fc7e8a6SThomas Graf * @config: textsearch configuration 37183fc7e8a6SThomas Graf * 37193fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 37203fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 37213fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 37223fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 37233fc7e8a6SThomas Graf */ 37243fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3725059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 37263fc7e8a6SThomas Graf { 3727059a2440SBojan Prtvar struct ts_state state; 3728f72b948dSPhil Oester unsigned int ret; 3729f72b948dSPhil Oester 3730b228c9b0SWillem de Bruijn BUILD_BUG_ON(sizeof(struct skb_seq_state) > sizeof(state.cb)); 3731b228c9b0SWillem de Bruijn 37323fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 37333fc7e8a6SThomas Graf config->finish = skb_ts_finish; 37343fc7e8a6SThomas Graf 3735059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 37363fc7e8a6SThomas Graf 3737059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3738f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 37393fc7e8a6SThomas Graf } 3740b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 37413fc7e8a6SThomas Graf 3742be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3743be12a1feSHannes Frederic Sowa int offset, size_t size) 3744be12a1feSHannes Frederic Sowa { 3745be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3746be12a1feSHannes Frederic Sowa 3747be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3748be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3749be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3750be12a1feSHannes Frederic Sowa get_page(page); 3751be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3752be12a1feSHannes Frederic Sowa } else { 3753be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3754be12a1feSHannes Frederic Sowa } 3755be12a1feSHannes Frederic Sowa 3756be12a1feSHannes Frederic Sowa return 0; 3757be12a1feSHannes Frederic Sowa } 3758be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3759be12a1feSHannes Frederic Sowa 3760cbb042f9SHerbert Xu /** 3761cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3762cbb042f9SHerbert Xu * @skb: buffer to update 3763cbb042f9SHerbert Xu * @len: length of data pulled 3764cbb042f9SHerbert Xu * 3765cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3766fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 376784fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 376884fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 376984fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3770cbb042f9SHerbert Xu */ 3771af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3772cbb042f9SHerbert Xu { 377331b33dfbSPravin B Shelar unsigned char *data = skb->data; 377431b33dfbSPravin B Shelar 3775cbb042f9SHerbert Xu BUG_ON(len > skb->len); 377631b33dfbSPravin B Shelar __skb_pull(skb, len); 377731b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 377831b33dfbSPravin B Shelar return skb->data; 3779cbb042f9SHerbert Xu } 3780f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3781f94691acSArnaldo Carvalho de Melo 378213acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 378313acc94eSYonghong Song { 378413acc94eSYonghong Song skb_frag_t head_frag; 378513acc94eSYonghong Song struct page *page; 378613acc94eSYonghong Song 378713acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 3788d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(&head_frag, page); 3789b54c9d5bSJonathan Lemon skb_frag_off_set(&head_frag, frag_skb->data - 3790b54c9d5bSJonathan Lemon (unsigned char *)page_address(page)); 3791d8e18a51SMatthew Wilcox (Oracle) skb_frag_size_set(&head_frag, skb_headlen(frag_skb)); 379213acc94eSYonghong Song return head_frag; 379313acc94eSYonghong Song } 379413acc94eSYonghong Song 37953a1296a3SSteffen Klassert struct sk_buff *skb_segment_list(struct sk_buff *skb, 37963a1296a3SSteffen Klassert netdev_features_t features, 37973a1296a3SSteffen Klassert unsigned int offset) 37983a1296a3SSteffen Klassert { 37993a1296a3SSteffen Klassert struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; 38003a1296a3SSteffen Klassert unsigned int tnl_hlen = skb_tnl_header_len(skb); 38013a1296a3SSteffen Klassert unsigned int delta_truesize = 0; 38023a1296a3SSteffen Klassert unsigned int delta_len = 0; 38033a1296a3SSteffen Klassert struct sk_buff *tail = NULL; 380453475c5dSDongseok Yi struct sk_buff *nskb, *tmp; 380553475c5dSDongseok Yi int err; 38063a1296a3SSteffen Klassert 38073a1296a3SSteffen Klassert skb_push(skb, -skb_network_offset(skb) + offset); 38083a1296a3SSteffen Klassert 38093a1296a3SSteffen Klassert skb_shinfo(skb)->frag_list = NULL; 38103a1296a3SSteffen Klassert 38113a1296a3SSteffen Klassert do { 38123a1296a3SSteffen Klassert nskb = list_skb; 38133a1296a3SSteffen Klassert list_skb = list_skb->next; 38143a1296a3SSteffen Klassert 381553475c5dSDongseok Yi err = 0; 381653475c5dSDongseok Yi if (skb_shared(nskb)) { 381753475c5dSDongseok Yi tmp = skb_clone(nskb, GFP_ATOMIC); 381853475c5dSDongseok Yi if (tmp) { 381953475c5dSDongseok Yi consume_skb(nskb); 382053475c5dSDongseok Yi nskb = tmp; 382153475c5dSDongseok Yi err = skb_unclone(nskb, GFP_ATOMIC); 382253475c5dSDongseok Yi } else { 382353475c5dSDongseok Yi err = -ENOMEM; 382453475c5dSDongseok Yi } 382553475c5dSDongseok Yi } 382653475c5dSDongseok Yi 38273a1296a3SSteffen Klassert if (!tail) 38283a1296a3SSteffen Klassert skb->next = nskb; 38293a1296a3SSteffen Klassert else 38303a1296a3SSteffen Klassert tail->next = nskb; 38313a1296a3SSteffen Klassert 383253475c5dSDongseok Yi if (unlikely(err)) { 383353475c5dSDongseok Yi nskb->next = list_skb; 383453475c5dSDongseok Yi goto err_linearize; 383553475c5dSDongseok Yi } 383653475c5dSDongseok Yi 38373a1296a3SSteffen Klassert tail = nskb; 38383a1296a3SSteffen Klassert 38393a1296a3SSteffen Klassert delta_len += nskb->len; 38403a1296a3SSteffen Klassert delta_truesize += nskb->truesize; 38413a1296a3SSteffen Klassert 38423a1296a3SSteffen Klassert skb_push(nskb, -skb_network_offset(nskb) + offset); 38433a1296a3SSteffen Klassert 3844cf673ed0SFlorian Westphal skb_release_head_state(nskb); 38453a1296a3SSteffen Klassert __copy_skb_header(nskb, skb); 38463a1296a3SSteffen Klassert 38473a1296a3SSteffen Klassert skb_headers_offset_update(nskb, skb_headroom(nskb) - skb_headroom(skb)); 38483a1296a3SSteffen Klassert skb_copy_from_linear_data_offset(skb, -tnl_hlen, 38493a1296a3SSteffen Klassert nskb->data - tnl_hlen, 38503a1296a3SSteffen Klassert offset + tnl_hlen); 38513a1296a3SSteffen Klassert 38523a1296a3SSteffen Klassert if (skb_needs_linearize(nskb, features) && 38533a1296a3SSteffen Klassert __skb_linearize(nskb)) 38543a1296a3SSteffen Klassert goto err_linearize; 38553a1296a3SSteffen Klassert 38563a1296a3SSteffen Klassert } while (list_skb); 38573a1296a3SSteffen Klassert 38583a1296a3SSteffen Klassert skb->truesize = skb->truesize - delta_truesize; 38593a1296a3SSteffen Klassert skb->data_len = skb->data_len - delta_len; 38603a1296a3SSteffen Klassert skb->len = skb->len - delta_len; 38613a1296a3SSteffen Klassert 38623a1296a3SSteffen Klassert skb_gso_reset(skb); 38633a1296a3SSteffen Klassert 38643a1296a3SSteffen Klassert skb->prev = tail; 38653a1296a3SSteffen Klassert 38663a1296a3SSteffen Klassert if (skb_needs_linearize(skb, features) && 38673a1296a3SSteffen Klassert __skb_linearize(skb)) 38683a1296a3SSteffen Klassert goto err_linearize; 38693a1296a3SSteffen Klassert 38703a1296a3SSteffen Klassert skb_get(skb); 38713a1296a3SSteffen Klassert 38723a1296a3SSteffen Klassert return skb; 38733a1296a3SSteffen Klassert 38743a1296a3SSteffen Klassert err_linearize: 38753a1296a3SSteffen Klassert kfree_skb_list(skb->next); 38763a1296a3SSteffen Klassert skb->next = NULL; 38773a1296a3SSteffen Klassert return ERR_PTR(-ENOMEM); 38783a1296a3SSteffen Klassert } 38793a1296a3SSteffen Klassert EXPORT_SYMBOL_GPL(skb_segment_list); 38803a1296a3SSteffen Klassert 38813a1296a3SSteffen Klassert int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb) 38823a1296a3SSteffen Klassert { 38833a1296a3SSteffen Klassert if (unlikely(p->len + skb->len >= 65536)) 38843a1296a3SSteffen Klassert return -E2BIG; 38853a1296a3SSteffen Klassert 38863a1296a3SSteffen Klassert if (NAPI_GRO_CB(p)->last == p) 38873a1296a3SSteffen Klassert skb_shinfo(p)->frag_list = skb; 38883a1296a3SSteffen Klassert else 38893a1296a3SSteffen Klassert NAPI_GRO_CB(p)->last->next = skb; 38903a1296a3SSteffen Klassert 38913a1296a3SSteffen Klassert skb_pull(skb, skb_gro_offset(skb)); 38923a1296a3SSteffen Klassert 38933a1296a3SSteffen Klassert NAPI_GRO_CB(p)->last = skb; 38943a1296a3SSteffen Klassert NAPI_GRO_CB(p)->count++; 38953a1296a3SSteffen Klassert p->data_len += skb->len; 3896*5e10da53SPaolo Abeni 3897*5e10da53SPaolo Abeni /* sk owenrship - if any - completely transferred to the aggregated packet */ 3898*5e10da53SPaolo Abeni skb->destructor = NULL; 38993a1296a3SSteffen Klassert p->truesize += skb->truesize; 39003a1296a3SSteffen Klassert p->len += skb->len; 39013a1296a3SSteffen Klassert 39023a1296a3SSteffen Klassert NAPI_GRO_CB(skb)->same_flow = 1; 39033a1296a3SSteffen Klassert 39043a1296a3SSteffen Klassert return 0; 39053a1296a3SSteffen Klassert } 39063a1296a3SSteffen Klassert 3907f4c50d99SHerbert Xu /** 3908f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3909df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3910576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3911f4c50d99SHerbert Xu * 3912f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 39134c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 39144c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3915f4c50d99SHerbert Xu */ 3916df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3917df5771ffSMichael S. Tsirkin netdev_features_t features) 3918f4c50d99SHerbert Xu { 3919f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3920f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 39211a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3922df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3923df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3924df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 39251fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3926f4c50d99SHerbert Xu unsigned int offset = doffset; 3927df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3928802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3929f4c50d99SHerbert Xu unsigned int headroom; 3930802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3931ec5f0615SPravin B Shelar __be16 proto; 393236c98382SAlexander Duyck bool csum, sg; 3933df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3934f4c50d99SHerbert Xu int err = -ENOMEM; 3935f4c50d99SHerbert Xu int i = 0; 3936f4c50d99SHerbert Xu int pos; 3937f4c50d99SHerbert Xu 39383dcbdb13SShmulik Ladkani if (list_skb && !list_skb->head_frag && skb_headlen(list_skb) && 39393dcbdb13SShmulik Ladkani (skb_shinfo(head_skb)->gso_type & SKB_GSO_DODGY)) { 39403dcbdb13SShmulik Ladkani /* gso_size is untrusted, and we have a frag_list with a linear 39413dcbdb13SShmulik Ladkani * non head_frag head. 39423dcbdb13SShmulik Ladkani * 39433dcbdb13SShmulik Ladkani * (we assume checking the first list_skb member suffices; 39443dcbdb13SShmulik Ladkani * i.e if either of the list_skb members have non head_frag 39453dcbdb13SShmulik Ladkani * head, then the first one has too). 39463dcbdb13SShmulik Ladkani * 39473dcbdb13SShmulik Ladkani * If head_skb's headlen does not fit requested gso_size, it 39483dcbdb13SShmulik Ladkani * means that the frag_list members do NOT terminate on exact 39493dcbdb13SShmulik Ladkani * gso_size boundaries. Hence we cannot perform skb_frag_t page 39503dcbdb13SShmulik Ladkani * sharing. Therefore we must fallback to copying the frag_list 39513dcbdb13SShmulik Ladkani * skbs; we do so by disabling SG. 39523dcbdb13SShmulik Ladkani */ 39533dcbdb13SShmulik Ladkani if (mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) 39543dcbdb13SShmulik Ladkani features &= ~NETIF_F_SG; 39553dcbdb13SShmulik Ladkani } 39563dcbdb13SShmulik Ladkani 39575882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 39582f631133SMiaohe Lin proto = skb_network_protocol(head_skb, NULL); 3959ec5f0615SPravin B Shelar if (unlikely(!proto)) 3960ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3961ec5f0615SPravin B Shelar 396236c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 3963f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 39647e2b10c1STom Herbert 396507b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 396607b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 396707b26c94SSteffen Klassert struct sk_buff *iter; 396843170c4eSIlan Tayari unsigned int frag_len; 396907b26c94SSteffen Klassert 397007b26c94SSteffen Klassert if (!list_skb || 397107b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 397207b26c94SSteffen Klassert goto normal; 397307b26c94SSteffen Klassert 397443170c4eSIlan Tayari /* If we get here then all the required 397543170c4eSIlan Tayari * GSO features except frag_list are supported. 397643170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 397743170c4eSIlan Tayari * with no frag_list. 397843170c4eSIlan Tayari * Currently we can do that only when the buffers don't 397943170c4eSIlan Tayari * have a linear part and all the buffers except 398043170c4eSIlan Tayari * the last are of the same length. 398107b26c94SSteffen Klassert */ 398243170c4eSIlan Tayari frag_len = list_skb->len; 398307b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 398443170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 398543170c4eSIlan Tayari goto normal; 3986eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 398707b26c94SSteffen Klassert goto normal; 398807b26c94SSteffen Klassert 398907b26c94SSteffen Klassert len -= iter->len; 399007b26c94SSteffen Klassert } 399143170c4eSIlan Tayari 399243170c4eSIlan Tayari if (len != frag_len) 399343170c4eSIlan Tayari goto normal; 399407b26c94SSteffen Klassert } 399507b26c94SSteffen Klassert 3996802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 3997802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 3998802ab55aSAlexander Duyck * now. 3999802ab55aSAlexander Duyck */ 4000802ab55aSAlexander Duyck partial_segs = len / mss; 4001d7fb5a80SAlexander Duyck if (partial_segs > 1) 4002802ab55aSAlexander Duyck mss *= partial_segs; 4003d7fb5a80SAlexander Duyck else 4004d7fb5a80SAlexander Duyck partial_segs = 0; 4005802ab55aSAlexander Duyck } 4006802ab55aSAlexander Duyck 400707b26c94SSteffen Klassert normal: 4008df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 4009df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 4010f4c50d99SHerbert Xu 4011f4c50d99SHerbert Xu do { 4012f4c50d99SHerbert Xu struct sk_buff *nskb; 40138cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 4014c8884eddSHerbert Xu int hsize; 4015f4c50d99SHerbert Xu int size; 4016f4c50d99SHerbert Xu 40173953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 40183953c46cSMarcelo Ricardo Leitner len = list_skb->len; 40193953c46cSMarcelo Ricardo Leitner } else { 4020df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 4021f4c50d99SHerbert Xu if (len > mss) 4022f4c50d99SHerbert Xu len = mss; 40233953c46cSMarcelo Ricardo Leitner } 4024f4c50d99SHerbert Xu 4025df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 4026f4c50d99SHerbert Xu 4027dbd50f23SXin Long if (hsize <= 0 && i >= nfrags && skb_headlen(list_skb) && 40281a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 40291a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 403089319d38SHerbert Xu 40319d8506ccSHerbert Xu i = 0; 40321a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 40331a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 40341fd819ecSMichael S. Tsirkin frag_skb = list_skb; 40351a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 40369d8506ccSHerbert Xu 40379d8506ccSHerbert Xu while (pos < offset + len) { 40389d8506ccSHerbert Xu BUG_ON(i >= nfrags); 40399d8506ccSHerbert Xu 40404e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 40419d8506ccSHerbert Xu if (pos + size > offset + len) 40429d8506ccSHerbert Xu break; 40439d8506ccSHerbert Xu 40449d8506ccSHerbert Xu i++; 40459d8506ccSHerbert Xu pos += size; 40464e1beba1SMichael S. Tsirkin frag++; 40479d8506ccSHerbert Xu } 40489d8506ccSHerbert Xu 40491a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 40501a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 405189319d38SHerbert Xu 4052f4c50d99SHerbert Xu if (unlikely(!nskb)) 4053f4c50d99SHerbert Xu goto err; 4054f4c50d99SHerbert Xu 40559d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 40569d8506ccSHerbert Xu kfree_skb(nskb); 40579d8506ccSHerbert Xu goto err; 40589d8506ccSHerbert Xu } 40599d8506ccSHerbert Xu 4060ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 406189319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 406289319d38SHerbert Xu kfree_skb(nskb); 406389319d38SHerbert Xu goto err; 406489319d38SHerbert Xu } 406589319d38SHerbert Xu 4066ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 406789319d38SHerbert Xu skb_release_head_state(nskb); 406889319d38SHerbert Xu __skb_push(nskb, doffset); 406989319d38SHerbert Xu } else { 407000b229f7SPaolo Abeni if (hsize < 0) 407100b229f7SPaolo Abeni hsize = 0; 4072dbd50f23SXin Long if (hsize > len || !sg) 4073dbd50f23SXin Long hsize = len; 4074dbd50f23SXin Long 4075c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 4076df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 4077c93bdd0eSMel Gorman NUMA_NO_NODE); 407889319d38SHerbert Xu 407989319d38SHerbert Xu if (unlikely(!nskb)) 408089319d38SHerbert Xu goto err; 408189319d38SHerbert Xu 408289319d38SHerbert Xu skb_reserve(nskb, headroom); 408389319d38SHerbert Xu __skb_put(nskb, doffset); 408489319d38SHerbert Xu } 408589319d38SHerbert Xu 4086f4c50d99SHerbert Xu if (segs) 4087f4c50d99SHerbert Xu tail->next = nskb; 4088f4c50d99SHerbert Xu else 4089f4c50d99SHerbert Xu segs = nskb; 4090f4c50d99SHerbert Xu tail = nskb; 4091f4c50d99SHerbert Xu 4092df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 4093f4c50d99SHerbert Xu 4094030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 4095fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 409668c33163SPravin B Shelar 4097df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 409868c33163SPravin B Shelar nskb->data - tnl_hlen, 409968c33163SPravin B Shelar doffset + tnl_hlen); 410089319d38SHerbert Xu 41019d8506ccSHerbert Xu if (nskb->len == len + doffset) 41021cdbcb79SSimon Horman goto perform_csum_check; 410389319d38SHerbert Xu 41047fbeffedSAlexander Duyck if (!sg) { 41051454c9faSYadu Kishore if (!csum) { 41067fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 41076f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 410876443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 410976443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 41101454c9faSYadu Kishore skb_put(nskb, 41111454c9faSYadu Kishore len), 41128d5930dfSAl Viro len); 41137e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 4114de843723STom Herbert skb_headroom(nskb) + doffset; 41151454c9faSYadu Kishore } else { 41161454c9faSYadu Kishore skb_copy_bits(head_skb, offset, 41171454c9faSYadu Kishore skb_put(nskb, len), 41181454c9faSYadu Kishore len); 41191454c9faSYadu Kishore } 4120f4c50d99SHerbert Xu continue; 4121f4c50d99SHerbert Xu } 4122f4c50d99SHerbert Xu 41238cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 4124f4c50d99SHerbert Xu 4125df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 4126d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 4127f4c50d99SHerbert Xu 412806b4feb3SJonathan Lemon skb_shinfo(nskb)->flags |= skb_shinfo(head_skb)->flags & 412906b4feb3SJonathan Lemon SKBFL_SHARED_FRAG; 4130cef401deSEric Dumazet 4131bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4132bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 4133bf5c25d6SWillem de Bruijn goto err; 4134bf5c25d6SWillem de Bruijn 41359d8506ccSHerbert Xu while (pos < offset + len) { 41369d8506ccSHerbert Xu if (i >= nfrags) { 41379d8506ccSHerbert Xu i = 0; 41381a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 41391a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 41401fd819ecSMichael S. Tsirkin frag_skb = list_skb; 414113acc94eSYonghong Song if (!skb_headlen(list_skb)) { 41429d8506ccSHerbert Xu BUG_ON(!nfrags); 414313acc94eSYonghong Song } else { 414413acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 41459d8506ccSHerbert Xu 414613acc94eSYonghong Song /* to make room for head_frag. */ 414713acc94eSYonghong Song i--; 414813acc94eSYonghong Song frag--; 414913acc94eSYonghong Song } 4150bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 4151bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 4152bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 4153bf5c25d6SWillem de Bruijn goto err; 4154bf5c25d6SWillem de Bruijn 41551a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 41569d8506ccSHerbert Xu } 41579d8506ccSHerbert Xu 41589d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 41599d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 41609d8506ccSHerbert Xu net_warn_ratelimited( 41619d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 41629d8506ccSHerbert Xu pos, mss); 4163ff907a11SEric Dumazet err = -EINVAL; 41649d8506ccSHerbert Xu goto err; 41659d8506ccSHerbert Xu } 41669d8506ccSHerbert Xu 416713acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 41688cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 41698cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 4170f4c50d99SHerbert Xu 4171f4c50d99SHerbert Xu if (pos < offset) { 4172b54c9d5bSJonathan Lemon skb_frag_off_add(nskb_frag, offset - pos); 41738cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 4174f4c50d99SHerbert Xu } 4175f4c50d99SHerbert Xu 417689319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 4177f4c50d99SHerbert Xu 4178f4c50d99SHerbert Xu if (pos + size <= offset + len) { 4179f4c50d99SHerbert Xu i++; 41804e1beba1SMichael S. Tsirkin frag++; 4181f4c50d99SHerbert Xu pos += size; 4182f4c50d99SHerbert Xu } else { 41838cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 418489319d38SHerbert Xu goto skip_fraglist; 4185f4c50d99SHerbert Xu } 4186f4c50d99SHerbert Xu 41878cb19905SMichael S. Tsirkin nskb_frag++; 4188f4c50d99SHerbert Xu } 4189f4c50d99SHerbert Xu 419089319d38SHerbert Xu skip_fraglist: 4191f4c50d99SHerbert Xu nskb->data_len = len - hsize; 4192f4c50d99SHerbert Xu nskb->len += nskb->data_len; 4193f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 4194ec5f0615SPravin B Shelar 41951cdbcb79SSimon Horman perform_csum_check: 41967fbeffedSAlexander Duyck if (!csum) { 4197ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 4198ff907a11SEric Dumazet __skb_linearize(nskb)) 4199ddff00d4SAlexander Duyck goto err; 4200ff907a11SEric Dumazet 42017fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 4202ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 420376443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 420476443456SAlexander Duyck skb_checksum(nskb, doffset, 420576443456SAlexander Duyck nskb->len - doffset, 0); 42067e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 42077e2b10c1STom Herbert skb_headroom(nskb) + doffset; 4208ec5f0615SPravin B Shelar } 4209df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 4210f4c50d99SHerbert Xu 4211bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 4212bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 4213bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 4214bec3cfdcSEric Dumazet */ 4215bec3cfdcSEric Dumazet segs->prev = tail; 4216432c856fSToshiaki Makita 4217802ab55aSAlexander Duyck if (partial_segs) { 421807b26c94SSteffen Klassert struct sk_buff *iter; 4219802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 422007b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 4221802ab55aSAlexander Duyck 4222802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 422307b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 4224802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 4225802ab55aSAlexander Duyck 4226802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 4227802ab55aSAlexander Duyck * our way back down the stack of protocols. 4228802ab55aSAlexander Duyck */ 422907b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 423007b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 423107b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 423207b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 423307b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 423407b26c94SSteffen Klassert } 423507b26c94SSteffen Klassert 423607b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 423707b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 423807b26c94SSteffen Klassert else if (tail != segs) 423907b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 4240802ab55aSAlexander Duyck } 4241802ab55aSAlexander Duyck 4242432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 4243432c856fSToshiaki Makita * using skb_set_owner_w(). 4244432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 4245432c856fSToshiaki Makita */ 4246432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 4247432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 4248432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 4249432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 4250432c856fSToshiaki Makita } 4251f4c50d99SHerbert Xu return segs; 4252f4c50d99SHerbert Xu 4253f4c50d99SHerbert Xu err: 4254289dccbeSEric Dumazet kfree_skb_list(segs); 4255f4c50d99SHerbert Xu return ERR_PTR(err); 4256f4c50d99SHerbert Xu } 4257f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 4258f4c50d99SHerbert Xu 4259d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb) 426071d93b39SHerbert Xu { 42618a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 426267147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 426367147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 42648a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 4265715dc1f3SEric Dumazet unsigned int delta_truesize; 4266*5e10da53SPaolo Abeni unsigned int new_truesize; 4267d4546c25SDavid Miller struct sk_buff *lp; 426871d93b39SHerbert Xu 42690ab03f35SSteffen Klassert if (unlikely(p->len + len >= 65536 || NAPI_GRO_CB(skb)->flush)) 427071d93b39SHerbert Xu return -E2BIG; 427171d93b39SHerbert Xu 427229e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 42738a29111cSEric Dumazet pinfo = skb_shinfo(lp); 42748a29111cSEric Dumazet 42758a29111cSEric Dumazet if (headlen <= offset) { 427642da6994SHerbert Xu skb_frag_t *frag; 427766e92fcfSHerbert Xu skb_frag_t *frag2; 42789aaa156cSHerbert Xu int i = skbinfo->nr_frags; 42799aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 428042da6994SHerbert Xu 428166e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 42828a29111cSEric Dumazet goto merge; 428381705ad1SHerbert Xu 42848a29111cSEric Dumazet offset -= headlen; 42859aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 42869aaa156cSHerbert Xu skbinfo->nr_frags = 0; 4287f5572068SHerbert Xu 42889aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 42899aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 429066e92fcfSHerbert Xu do { 429166e92fcfSHerbert Xu *--frag = *--frag2; 429266e92fcfSHerbert Xu } while (--i); 429366e92fcfSHerbert Xu 4294b54c9d5bSJonathan Lemon skb_frag_off_add(frag, offset); 42959e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 429666e92fcfSHerbert Xu 4297715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 4298*5e10da53SPaolo Abeni new_truesize = SKB_TRUESIZE(skb_end_offset(skb)); 4299*5e10da53SPaolo Abeni delta_truesize = skb->truesize - new_truesize; 4300715dc1f3SEric Dumazet 4301*5e10da53SPaolo Abeni skb->truesize = new_truesize; 4302f5572068SHerbert Xu skb->len -= skb->data_len; 4303f5572068SHerbert Xu skb->data_len = 0; 4304f5572068SHerbert Xu 4305715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 43065d38a079SHerbert Xu goto done; 4307d7e8883cSEric Dumazet } else if (skb->head_frag) { 4308d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 4309d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 4310d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 4311d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 4312d7e8883cSEric Dumazet unsigned int first_offset; 4313d7e8883cSEric Dumazet 4314d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 43158a29111cSEric Dumazet goto merge; 4316d7e8883cSEric Dumazet 4317d7e8883cSEric Dumazet first_offset = skb->data - 4318d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 4319d7e8883cSEric Dumazet offset; 4320d7e8883cSEric Dumazet 4321d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 4322d7e8883cSEric Dumazet 4323d8e18a51SMatthew Wilcox (Oracle) __skb_frag_set_page(frag, page); 4324b54c9d5bSJonathan Lemon skb_frag_off_set(frag, first_offset); 4325d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 4326d7e8883cSEric Dumazet 4327d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 4328d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 4329d7e8883cSEric Dumazet 4330*5e10da53SPaolo Abeni new_truesize = SKB_TRUESIZE(sizeof(struct sk_buff)); 4331*5e10da53SPaolo Abeni delta_truesize = skb->truesize - new_truesize; 4332*5e10da53SPaolo Abeni skb->truesize = new_truesize; 4333d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 4334d7e8883cSEric Dumazet goto done; 43358a29111cSEric Dumazet } 433671d93b39SHerbert Xu 433771d93b39SHerbert Xu merge: 4338*5e10da53SPaolo Abeni /* sk owenrship - if any - completely transferred to the aggregated packet */ 4339*5e10da53SPaolo Abeni skb->destructor = NULL; 4340715dc1f3SEric Dumazet delta_truesize = skb->truesize; 434167147ba9SHerbert Xu if (offset > headlen) { 4342d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 4343d1dc7abfSMichal Schmidt 4344b54c9d5bSJonathan Lemon skb_frag_off_add(&skbinfo->frags[0], eat); 43459e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 4346d1dc7abfSMichal Schmidt skb->data_len -= eat; 4347d1dc7abfSMichal Schmidt skb->len -= eat; 434867147ba9SHerbert Xu offset = headlen; 434956035022SHerbert Xu } 435056035022SHerbert Xu 435167147ba9SHerbert Xu __skb_pull(skb, offset); 435256035022SHerbert Xu 435329e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 43548a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 43558a29111cSEric Dumazet else 4356c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 4357c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 4358f4a775d1SEric Dumazet __skb_header_release(skb); 43598a29111cSEric Dumazet lp = p; 436071d93b39SHerbert Xu 43615d38a079SHerbert Xu done: 43625d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 436337fe4732SHerbert Xu p->data_len += len; 4364715dc1f3SEric Dumazet p->truesize += delta_truesize; 436537fe4732SHerbert Xu p->len += len; 43668a29111cSEric Dumazet if (lp != p) { 43678a29111cSEric Dumazet lp->data_len += len; 43688a29111cSEric Dumazet lp->truesize += delta_truesize; 43698a29111cSEric Dumazet lp->len += len; 43708a29111cSEric Dumazet } 437171d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 437271d93b39SHerbert Xu return 0; 437371d93b39SHerbert Xu } 437471d93b39SHerbert Xu 4375df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 4376df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 4377df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 4378df5042f4SFlorian Westphal 4379df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 4380df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4381df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 4382df5042f4SFlorian Westphal #endif 43834165079bSFlorian Westphal #ifdef CONFIG_XFRM 43844165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 43854165079bSFlorian Westphal #endif 438695a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 438795a7233cSPaul Blakey [TC_SKB_EXT] = SKB_EXT_CHUNKSIZEOF(struct tc_skb_ext), 438895a7233cSPaul Blakey #endif 43893ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 43903ee17bc7SMat Martineau [SKB_EXT_MPTCP] = SKB_EXT_CHUNKSIZEOF(struct mptcp_ext), 43913ee17bc7SMat Martineau #endif 4392df5042f4SFlorian Westphal }; 4393df5042f4SFlorian Westphal 4394df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 4395df5042f4SFlorian Westphal { 4396df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 4397df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 4398df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 4399df5042f4SFlorian Westphal #endif 44004165079bSFlorian Westphal #ifdef CONFIG_XFRM 44014165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 44024165079bSFlorian Westphal #endif 440395a7233cSPaul Blakey #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) 440495a7233cSPaul Blakey skb_ext_type_len[TC_SKB_EXT] + 440595a7233cSPaul Blakey #endif 44063ee17bc7SMat Martineau #if IS_ENABLED(CONFIG_MPTCP) 44073ee17bc7SMat Martineau skb_ext_type_len[SKB_EXT_MPTCP] + 44083ee17bc7SMat Martineau #endif 4409df5042f4SFlorian Westphal 0; 4410df5042f4SFlorian Westphal } 4411df5042f4SFlorian Westphal 4412df5042f4SFlorian Westphal static void skb_extensions_init(void) 4413df5042f4SFlorian Westphal { 4414df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 4415df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 4416df5042f4SFlorian Westphal 4417df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 4418df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 4419df5042f4SFlorian Westphal 0, 4420df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 4421df5042f4SFlorian Westphal NULL); 4422df5042f4SFlorian Westphal } 4423df5042f4SFlorian Westphal #else 4424df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 4425df5042f4SFlorian Westphal #endif 4426df5042f4SFlorian Westphal 44271da177e4SLinus Torvalds void __init skb_init(void) 44281da177e4SLinus Torvalds { 442979a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 44301da177e4SLinus Torvalds sizeof(struct sk_buff), 44311da177e4SLinus Torvalds 0, 4432e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 443379a8a642SKees Cook offsetof(struct sk_buff, cb), 443479a8a642SKees Cook sizeof_field(struct sk_buff, cb), 443520c2df83SPaul Mundt NULL); 4436d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 4437d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 4438d179cd12SDavid S. Miller 0, 4439e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 444020c2df83SPaul Mundt NULL); 4441df5042f4SFlorian Westphal skb_extensions_init(); 44421da177e4SLinus Torvalds } 44431da177e4SLinus Torvalds 444451c739d1SDavid S. Miller static int 444548a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 444648a1df65SJason A. Donenfeld unsigned int recursion_level) 4447716ea3a7SDavid Howells { 44481a028e50SDavid S. Miller int start = skb_headlen(skb); 44491a028e50SDavid S. Miller int i, copy = start - offset; 4450fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 4451716ea3a7SDavid Howells int elt = 0; 4452716ea3a7SDavid Howells 445348a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 445448a1df65SJason A. Donenfeld return -EMSGSIZE; 445548a1df65SJason A. Donenfeld 4456716ea3a7SDavid Howells if (copy > 0) { 4457716ea3a7SDavid Howells if (copy > len) 4458716ea3a7SDavid Howells copy = len; 4459642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 4460716ea3a7SDavid Howells elt++; 4461716ea3a7SDavid Howells if ((len -= copy) == 0) 4462716ea3a7SDavid Howells return elt; 4463716ea3a7SDavid Howells offset += copy; 4464716ea3a7SDavid Howells } 4465716ea3a7SDavid Howells 4466716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 44671a028e50SDavid S. Miller int end; 4468716ea3a7SDavid Howells 4469547b792cSIlpo Järvinen WARN_ON(start > offset + len); 44701a028e50SDavid S. Miller 44719e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 4472716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 4473716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 447448a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 447548a1df65SJason A. Donenfeld return -EMSGSIZE; 4476716ea3a7SDavid Howells 4477716ea3a7SDavid Howells if (copy > len) 4478716ea3a7SDavid Howells copy = len; 4479ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 4480b54c9d5bSJonathan Lemon skb_frag_off(frag) + offset - start); 4481716ea3a7SDavid Howells elt++; 4482716ea3a7SDavid Howells if (!(len -= copy)) 4483716ea3a7SDavid Howells return elt; 4484716ea3a7SDavid Howells offset += copy; 4485716ea3a7SDavid Howells } 44861a028e50SDavid S. Miller start = end; 4487716ea3a7SDavid Howells } 4488716ea3a7SDavid Howells 4489fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 449048a1df65SJason A. Donenfeld int end, ret; 4491716ea3a7SDavid Howells 4492547b792cSIlpo Järvinen WARN_ON(start > offset + len); 44931a028e50SDavid S. Miller 4494fbb398a8SDavid S. Miller end = start + frag_iter->len; 4495716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 449648a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 449748a1df65SJason A. Donenfeld return -EMSGSIZE; 449848a1df65SJason A. Donenfeld 4499716ea3a7SDavid Howells if (copy > len) 4500716ea3a7SDavid Howells copy = len; 450148a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 450248a1df65SJason A. Donenfeld copy, recursion_level + 1); 450348a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 450448a1df65SJason A. Donenfeld return ret; 450548a1df65SJason A. Donenfeld elt += ret; 4506716ea3a7SDavid Howells if ((len -= copy) == 0) 4507716ea3a7SDavid Howells return elt; 4508716ea3a7SDavid Howells offset += copy; 4509716ea3a7SDavid Howells } 45101a028e50SDavid S. Miller start = end; 4511716ea3a7SDavid Howells } 4512716ea3a7SDavid Howells BUG_ON(len); 4513716ea3a7SDavid Howells return elt; 4514716ea3a7SDavid Howells } 4515716ea3a7SDavid Howells 451648a1df65SJason A. Donenfeld /** 451748a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 451848a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 451948a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 452048a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 452148a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 452248a1df65SJason A. Donenfeld * 452348a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 452448a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 452548a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 452648a1df65SJason A. Donenfeld * could not fit. 452748a1df65SJason A. Donenfeld */ 452848a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 452948a1df65SJason A. Donenfeld { 453048a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 453148a1df65SJason A. Donenfeld 453248a1df65SJason A. Donenfeld if (nsg <= 0) 453348a1df65SJason A. Donenfeld return nsg; 453448a1df65SJason A. Donenfeld 453548a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 453648a1df65SJason A. Donenfeld 453748a1df65SJason A. Donenfeld return nsg; 453848a1df65SJason A. Donenfeld } 453948a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 454048a1df65SJason A. Donenfeld 454125a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 454225a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 454325a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 454425a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 454525a91d8dSFan Du * 454625a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 454725a91d8dSFan Du * 1. sg_init_table 454825a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 454925a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 455025a91d8dSFan Du * 455125a91d8dSFan Du * This is equivalent to: 455225a91d8dSFan Du * 1. sg_init_table 455325a91d8dSFan Du * 2. skb_to_sgvec(payload1) 455425a91d8dSFan Du * 3. sg_unmark_end 455525a91d8dSFan Du * 4. skb_to_sgvec(payload2) 455625a91d8dSFan Du * 455725a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 455825a91d8dSFan Du * is more preferable. 455925a91d8dSFan Du */ 456025a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 456125a91d8dSFan Du int offset, int len) 456225a91d8dSFan Du { 456348a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 456425a91d8dSFan Du } 456525a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 456625a91d8dSFan Du 456751c739d1SDavid S. Miller 456851c739d1SDavid S. Miller 4569716ea3a7SDavid Howells /** 4570716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4571716ea3a7SDavid Howells * @skb: The socket buffer to check. 4572716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4573716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4574716ea3a7SDavid Howells * 4575716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4576716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4577716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4578716ea3a7SDavid Howells * 4579716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4580716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4581716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4582716ea3a7SDavid Howells * 4583716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4584716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4585716ea3a7SDavid Howells */ 4586716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4587716ea3a7SDavid Howells { 4588716ea3a7SDavid Howells int copyflag; 4589716ea3a7SDavid Howells int elt; 4590716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4591716ea3a7SDavid Howells 4592716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4593716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4594716ea3a7SDavid Howells * at the moment even if they are anonymous). 4595716ea3a7SDavid Howells */ 4596716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4597c15fc199SMiaohe Lin !__pskb_pull_tail(skb, __skb_pagelen(skb))) 4598716ea3a7SDavid Howells return -ENOMEM; 4599716ea3a7SDavid Howells 4600716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 460121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4602716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4603716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4604716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4605716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4606716ea3a7SDavid Howells 4607716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4608716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4609716ea3a7SDavid Howells return -ENOMEM; 4610716ea3a7SDavid Howells 4611716ea3a7SDavid Howells /* Voila! */ 4612716ea3a7SDavid Howells *trailer = skb; 4613716ea3a7SDavid Howells return 1; 4614716ea3a7SDavid Howells } 4615716ea3a7SDavid Howells 4616716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4617716ea3a7SDavid Howells 4618716ea3a7SDavid Howells elt = 1; 4619716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4620716ea3a7SDavid Howells copyflag = 0; 4621716ea3a7SDavid Howells 4622716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4623716ea3a7SDavid Howells int ntail = 0; 4624716ea3a7SDavid Howells 4625716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4626716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4627716ea3a7SDavid Howells * after it. */ 4628716ea3a7SDavid Howells 4629716ea3a7SDavid Howells if (skb_shared(skb1)) 4630716ea3a7SDavid Howells copyflag = 1; 4631716ea3a7SDavid Howells 4632716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4633716ea3a7SDavid Howells 4634716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4635716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 463621dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4637716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4638716ea3a7SDavid Howells ntail = tailbits + 128; 4639716ea3a7SDavid Howells } 4640716ea3a7SDavid Howells 4641716ea3a7SDavid Howells if (copyflag || 4642716ea3a7SDavid Howells skb_cloned(skb1) || 4643716ea3a7SDavid Howells ntail || 4644716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 464521dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4646716ea3a7SDavid Howells struct sk_buff *skb2; 4647716ea3a7SDavid Howells 4648716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4649716ea3a7SDavid Howells if (ntail == 0) 4650716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4651716ea3a7SDavid Howells else 4652716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4653716ea3a7SDavid Howells skb_headroom(skb1), 4654716ea3a7SDavid Howells ntail, 4655716ea3a7SDavid Howells GFP_ATOMIC); 4656716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4657716ea3a7SDavid Howells return -ENOMEM; 4658716ea3a7SDavid Howells 4659716ea3a7SDavid Howells if (skb1->sk) 4660716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4661716ea3a7SDavid Howells 4662716ea3a7SDavid Howells /* Looking around. Are we still alive? 4663716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4664716ea3a7SDavid Howells 4665716ea3a7SDavid Howells skb2->next = skb1->next; 4666716ea3a7SDavid Howells *skb_p = skb2; 4667716ea3a7SDavid Howells kfree_skb(skb1); 4668716ea3a7SDavid Howells skb1 = skb2; 4669716ea3a7SDavid Howells } 4670716ea3a7SDavid Howells elt++; 4671716ea3a7SDavid Howells *trailer = skb1; 4672716ea3a7SDavid Howells skb_p = &skb1->next; 4673716ea3a7SDavid Howells } 4674716ea3a7SDavid Howells 4675716ea3a7SDavid Howells return elt; 4676716ea3a7SDavid Howells } 4677b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4678716ea3a7SDavid Howells 4679b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4680b1faf566SEric Dumazet { 4681b1faf566SEric Dumazet struct sock *sk = skb->sk; 4682b1faf566SEric Dumazet 4683b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4684b1faf566SEric Dumazet } 4685b1faf566SEric Dumazet 46868605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 46878605330aSSoheil Hassas Yeganeh { 46888605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 46898605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 46908605330aSSoheil Hassas Yeganeh */ 46918605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 46928605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 46938605330aSSoheil Hassas Yeganeh } 46948605330aSSoheil Hassas Yeganeh 4695b1faf566SEric Dumazet /* 4696b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4697b1faf566SEric Dumazet */ 4698b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4699b1faf566SEric Dumazet { 4700b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 4701ebb3b78dSEric Dumazet (unsigned int)READ_ONCE(sk->sk_rcvbuf)) 4702b1faf566SEric Dumazet return -ENOMEM; 4703b1faf566SEric Dumazet 4704b1faf566SEric Dumazet skb_orphan(skb); 4705b1faf566SEric Dumazet skb->sk = sk; 4706b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4707b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 47088605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4709b1faf566SEric Dumazet 4710abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4711abb57ea4SEric Dumazet skb_dst_force(skb); 4712abb57ea4SEric Dumazet 4713b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4714b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 4715e3ae2365SAlexander Aring sk_error_report(sk); 4716b1faf566SEric Dumazet return 0; 4717b1faf566SEric Dumazet } 4718b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4719b1faf566SEric Dumazet 472083a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 472183a1a1a7SSoheil Hassas Yeganeh { 472283a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 472383a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 472483a1a1a7SSoheil Hassas Yeganeh } 472583a1a1a7SSoheil Hassas Yeganeh 4726364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4727364a9e93SWillem de Bruijn { 4728364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 472983a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 473083a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4731997d5c3fSEric Dumazet unsigned long flags; 4732364a9e93SWillem de Bruijn 4733997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4734364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 473538b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 473683a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 473738b25793SSoheil Hassas Yeganeh if (icmp_next) 4738985f7337SWillem de Bruijn sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 473938b25793SSoheil Hassas Yeganeh } 4740997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4741364a9e93SWillem de Bruijn 474283a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 474383a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 474483a1a1a7SSoheil Hassas Yeganeh 474583a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4746e3ae2365SAlexander Aring sk_error_report(sk); 4747364a9e93SWillem de Bruijn 4748364a9e93SWillem de Bruijn return skb; 4749364a9e93SWillem de Bruijn } 4750364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4751364a9e93SWillem de Bruijn 4752cab41c47SAlexander Duyck /** 4753cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4754cab41c47SAlexander Duyck * @skb: the skb to clone 4755cab41c47SAlexander Duyck * 4756cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4757cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4758cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4759cab41c47SAlexander Duyck * 4760cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4761cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4762cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4763cab41c47SAlexander Duyck * the sk_error_queue. 4764cab41c47SAlexander Duyck */ 476562bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 476662bccb8cSAlexander Duyck { 476762bccb8cSAlexander Duyck struct sock *sk = skb->sk; 476862bccb8cSAlexander Duyck struct sk_buff *clone; 476962bccb8cSAlexander Duyck 477041c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 477162bccb8cSAlexander Duyck return NULL; 477262bccb8cSAlexander Duyck 477362bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 477462bccb8cSAlexander Duyck if (!clone) { 477562bccb8cSAlexander Duyck sock_put(sk); 477662bccb8cSAlexander Duyck return NULL; 477762bccb8cSAlexander Duyck } 477862bccb8cSAlexander Duyck 477962bccb8cSAlexander Duyck clone->sk = sk; 478062bccb8cSAlexander Duyck clone->destructor = sock_efree; 478162bccb8cSAlexander Duyck 478262bccb8cSAlexander Duyck return clone; 478362bccb8cSAlexander Duyck } 478462bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 478562bccb8cSAlexander Duyck 478637846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 478737846ef0SAlexander Duyck struct sock *sk, 47884ef1b286SSoheil Hassas Yeganeh int tstype, 47894ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4790ac45f602SPatrick Ohly { 4791ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4792ac45f602SPatrick Ohly int err; 4793ac45f602SPatrick Ohly 47944ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 47954ef1b286SSoheil Hassas Yeganeh 4796ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4797ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4798ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4799ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4800e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 48014ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 48021862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 48034ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 480409c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 4805ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 4806ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 48074ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 48084ed2d765SWillem de Bruijn } 480929030374SEric Dumazet 4810ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 481129030374SEric Dumazet 4812ac45f602SPatrick Ohly if (err) 4813ac45f602SPatrick Ohly kfree_skb(skb); 4814ac45f602SPatrick Ohly } 481537846ef0SAlexander Duyck 4816b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4817b245be1fSWillem de Bruijn { 4818b245be1fSWillem de Bruijn bool ret; 4819b245be1fSWillem de Bruijn 4820b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 4821b245be1fSWillem de Bruijn return true; 4822b245be1fSWillem de Bruijn 4823b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4824b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4825b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4826b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4827b245be1fSWillem de Bruijn return ret; 4828b245be1fSWillem de Bruijn } 4829b245be1fSWillem de Bruijn 483037846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 483137846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 483237846ef0SAlexander Duyck { 483337846ef0SAlexander Duyck struct sock *sk = skb->sk; 483437846ef0SAlexander Duyck 4835b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 483635b99dffSWillem de Bruijn goto err; 4837b245be1fSWillem de Bruijn 48389ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 48399ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 48409ac25fc0SEric Dumazet */ 484141c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 484237846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 48434ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 484437846ef0SAlexander Duyck sock_put(sk); 484535b99dffSWillem de Bruijn return; 484637846ef0SAlexander Duyck } 484735b99dffSWillem de Bruijn 484835b99dffSWillem de Bruijn err: 484935b99dffSWillem de Bruijn kfree_skb(skb); 48509ac25fc0SEric Dumazet } 485137846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 485237846ef0SAlexander Duyck 485337846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 4854e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, 485537846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 485637846ef0SAlexander Duyck struct sock *sk, int tstype) 485737846ef0SAlexander Duyck { 485837846ef0SAlexander Duyck struct sk_buff *skb; 48594ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 486037846ef0SAlexander Duyck 48613a8dd971SWillem de Bruijn if (!sk) 48623a8dd971SWillem de Bruijn return; 48633a8dd971SWillem de Bruijn 4864b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4865b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4866b50a5c70SMiroslav Lichvar return; 4867b50a5c70SMiroslav Lichvar 48683a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 48693a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 487037846ef0SAlexander Duyck return; 487137846ef0SAlexander Duyck 48721c885808SFrancis Yan if (tsonly) { 48731c885808SFrancis Yan #ifdef CONFIG_INET 48741c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 48751c885808SFrancis Yan sk->sk_protocol == IPPROTO_TCP && 48764ef1b286SSoheil Hassas Yeganeh sk->sk_type == SOCK_STREAM) { 4877e7ed11eeSYousuk Seung skb = tcp_get_timestamping_opt_stats(sk, orig_skb, 4878e7ed11eeSYousuk Seung ack_skb); 48794ef1b286SSoheil Hassas Yeganeh opt_stats = true; 48804ef1b286SSoheil Hassas Yeganeh } else 48811c885808SFrancis Yan #endif 48821c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 48831c885808SFrancis Yan } else { 488437846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 48851c885808SFrancis Yan } 488637846ef0SAlexander Duyck if (!skb) 488737846ef0SAlexander Duyck return; 488837846ef0SAlexander Duyck 488949ca0d8bSWillem de Bruijn if (tsonly) { 4890fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4891fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 489249ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 489349ca0d8bSWillem de Bruijn } 489449ca0d8bSWillem de Bruijn 489549ca0d8bSWillem de Bruijn if (hwtstamps) 489649ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 489749ca0d8bSWillem de Bruijn else 489849ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 489949ca0d8bSWillem de Bruijn 49004ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 490137846ef0SAlexander Duyck } 4902e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4903e7fd2885SWillem de Bruijn 4904e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4905e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4906e7fd2885SWillem de Bruijn { 4907e7ed11eeSYousuk Seung return __skb_tstamp_tx(orig_skb, NULL, hwtstamps, orig_skb->sk, 4908e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 4909e7fd2885SWillem de Bruijn } 4910ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4911ac45f602SPatrick Ohly 49126e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 49136e3e939fSJohannes Berg { 49146e3e939fSJohannes Berg struct sock *sk = skb->sk; 49156e3e939fSJohannes Berg struct sock_exterr_skb *serr; 4916dd4f1072SEric Dumazet int err = 1; 49176e3e939fSJohannes Berg 49186e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 49196e3e939fSJohannes Berg skb->wifi_acked = acked; 49206e3e939fSJohannes Berg 49216e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 49226e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 49236e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 49246e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 49256e3e939fSJohannes Berg 4926dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4927dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4928dd4f1072SEric Dumazet */ 492941c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 49306e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4931dd4f1072SEric Dumazet sock_put(sk); 4932dd4f1072SEric Dumazet } 49336e3e939fSJohannes Berg if (err) 49346e3e939fSJohannes Berg kfree_skb(skb); 49356e3e939fSJohannes Berg } 49366e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 49376e3e939fSJohannes Berg 4938f35d9d8aSRusty Russell /** 4939f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4940f35d9d8aSRusty Russell * @skb: the skb to set 4941f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4942f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4943f35d9d8aSRusty Russell * 4944f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4945f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4946f35d9d8aSRusty Russell * 4947f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4948f35d9d8aSRusty Russell * returns false you should drop the packet. 4949f35d9d8aSRusty Russell */ 4950f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4951f35d9d8aSRusty Russell { 495252b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 495352b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 495452b5d6f5SEric Dumazet 495552b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 495652b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 495752b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 4958f35d9d8aSRusty Russell return false; 4959f35d9d8aSRusty Russell } 4960f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 496152b5d6f5SEric Dumazet skb->csum_start = csum_start; 4962f35d9d8aSRusty Russell skb->csum_offset = off; 4963e5d5decaSJason Wang skb_set_transport_header(skb, start); 4964f35d9d8aSRusty Russell return true; 4965f35d9d8aSRusty Russell } 4966b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4967f35d9d8aSRusty Russell 4968ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4969ed1f50c3SPaul Durrant unsigned int max) 4970ed1f50c3SPaul Durrant { 4971ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4972ed1f50c3SPaul Durrant return 0; 4973ed1f50c3SPaul Durrant 4974ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4975ed1f50c3SPaul Durrant * won't need to do it again. 4976ed1f50c3SPaul Durrant */ 4977ed1f50c3SPaul Durrant if (max > skb->len) 4978ed1f50c3SPaul Durrant max = skb->len; 4979ed1f50c3SPaul Durrant 4980ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4981ed1f50c3SPaul Durrant return -ENOMEM; 4982ed1f50c3SPaul Durrant 4983ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4984ed1f50c3SPaul Durrant return -EPROTO; 4985ed1f50c3SPaul Durrant 4986ed1f50c3SPaul Durrant return 0; 4987ed1f50c3SPaul Durrant } 4988ed1f50c3SPaul Durrant 4989f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4990f9708b43SJan Beulich 4991f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4992f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4993f9708b43SJan Beulich unsigned int off) 4994f9708b43SJan Beulich { 4995f9708b43SJan Beulich int err; 4996f9708b43SJan Beulich 4997161d1792SKees Cook switch (proto) { 4998f9708b43SJan Beulich case IPPROTO_TCP: 4999f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 5000f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 5001f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5002f9708b43SJan Beulich offsetof(struct tcphdr, 5003f9708b43SJan Beulich check))) 5004f9708b43SJan Beulich err = -EPROTO; 5005f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 5006f9708b43SJan Beulich 5007f9708b43SJan Beulich case IPPROTO_UDP: 5008f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 5009f9708b43SJan Beulich off + sizeof(struct udphdr)); 5010f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 5011f9708b43SJan Beulich offsetof(struct udphdr, 5012f9708b43SJan Beulich check))) 5013f9708b43SJan Beulich err = -EPROTO; 5014f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 5015f9708b43SJan Beulich } 5016f9708b43SJan Beulich 5017f9708b43SJan Beulich return ERR_PTR(-EPROTO); 5018f9708b43SJan Beulich } 5019f9708b43SJan Beulich 5020ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5021ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 5022ed1f50c3SPaul Durrant */ 5023ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 5024ed1f50c3SPaul Durrant 5025f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 5026ed1f50c3SPaul Durrant { 5027ed1f50c3SPaul Durrant unsigned int off; 5028ed1f50c3SPaul Durrant bool fragment; 5029f9708b43SJan Beulich __sum16 *csum; 5030ed1f50c3SPaul Durrant int err; 5031ed1f50c3SPaul Durrant 5032ed1f50c3SPaul Durrant fragment = false; 5033ed1f50c3SPaul Durrant 5034ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5035ed1f50c3SPaul Durrant sizeof(struct iphdr), 5036ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 5037ed1f50c3SPaul Durrant if (err < 0) 5038ed1f50c3SPaul Durrant goto out; 5039ed1f50c3SPaul Durrant 504011f920d2SMiaohe Lin if (ip_is_fragment(ip_hdr(skb))) 5041ed1f50c3SPaul Durrant fragment = true; 5042ed1f50c3SPaul Durrant 5043ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 5044ed1f50c3SPaul Durrant 5045ed1f50c3SPaul Durrant err = -EPROTO; 5046ed1f50c3SPaul Durrant 5047ed1f50c3SPaul Durrant if (fragment) 5048ed1f50c3SPaul Durrant goto out; 5049ed1f50c3SPaul Durrant 5050f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 5051f9708b43SJan Beulich if (IS_ERR(csum)) 5052f9708b43SJan Beulich return PTR_ERR(csum); 5053ed1f50c3SPaul Durrant 5054ed1f50c3SPaul Durrant if (recalculate) 5055f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 5056ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 5057ed1f50c3SPaul Durrant skb->len - off, 5058f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 5059ed1f50c3SPaul Durrant err = 0; 5060ed1f50c3SPaul Durrant 5061ed1f50c3SPaul Durrant out: 5062ed1f50c3SPaul Durrant return err; 5063ed1f50c3SPaul Durrant } 5064ed1f50c3SPaul Durrant 5065ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 5066ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 5067ed1f50c3SPaul Durrant */ 5068ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 5069ed1f50c3SPaul Durrant 5070ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 5071ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 5072ed1f50c3SPaul Durrant 5073ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 5074ed1f50c3SPaul Durrant { 5075ed1f50c3SPaul Durrant int err; 5076ed1f50c3SPaul Durrant u8 nexthdr; 5077ed1f50c3SPaul Durrant unsigned int off; 5078ed1f50c3SPaul Durrant unsigned int len; 5079ed1f50c3SPaul Durrant bool fragment; 5080ed1f50c3SPaul Durrant bool done; 5081f9708b43SJan Beulich __sum16 *csum; 5082ed1f50c3SPaul Durrant 5083ed1f50c3SPaul Durrant fragment = false; 5084ed1f50c3SPaul Durrant done = false; 5085ed1f50c3SPaul Durrant 5086ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 5087ed1f50c3SPaul Durrant 5088ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 5089ed1f50c3SPaul Durrant if (err < 0) 5090ed1f50c3SPaul Durrant goto out; 5091ed1f50c3SPaul Durrant 5092ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 5093ed1f50c3SPaul Durrant 5094ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 5095ed1f50c3SPaul Durrant while (off <= len && !done) { 5096ed1f50c3SPaul Durrant switch (nexthdr) { 5097ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 5098ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 5099ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 5100ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 5101ed1f50c3SPaul Durrant 5102ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5103ed1f50c3SPaul Durrant off + 5104ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 5105ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5106ed1f50c3SPaul Durrant if (err < 0) 5107ed1f50c3SPaul Durrant goto out; 5108ed1f50c3SPaul Durrant 5109ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 5110ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5111ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 5112ed1f50c3SPaul Durrant break; 5113ed1f50c3SPaul Durrant } 5114ed1f50c3SPaul Durrant case IPPROTO_AH: { 5115ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 5116ed1f50c3SPaul Durrant 5117ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5118ed1f50c3SPaul Durrant off + 5119ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 5120ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5121ed1f50c3SPaul Durrant if (err < 0) 5122ed1f50c3SPaul Durrant goto out; 5123ed1f50c3SPaul Durrant 5124ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 5125ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5126ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 5127ed1f50c3SPaul Durrant break; 5128ed1f50c3SPaul Durrant } 5129ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 5130ed1f50c3SPaul Durrant struct frag_hdr *hp; 5131ed1f50c3SPaul Durrant 5132ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 5133ed1f50c3SPaul Durrant off + 5134ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 5135ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 5136ed1f50c3SPaul Durrant if (err < 0) 5137ed1f50c3SPaul Durrant goto out; 5138ed1f50c3SPaul Durrant 5139ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 5140ed1f50c3SPaul Durrant 5141ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 5142ed1f50c3SPaul Durrant fragment = true; 5143ed1f50c3SPaul Durrant 5144ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 5145ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 5146ed1f50c3SPaul Durrant break; 5147ed1f50c3SPaul Durrant } 5148ed1f50c3SPaul Durrant default: 5149ed1f50c3SPaul Durrant done = true; 5150ed1f50c3SPaul Durrant break; 5151ed1f50c3SPaul Durrant } 5152ed1f50c3SPaul Durrant } 5153ed1f50c3SPaul Durrant 5154ed1f50c3SPaul Durrant err = -EPROTO; 5155ed1f50c3SPaul Durrant 5156ed1f50c3SPaul Durrant if (!done || fragment) 5157ed1f50c3SPaul Durrant goto out; 5158ed1f50c3SPaul Durrant 5159f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 5160f9708b43SJan Beulich if (IS_ERR(csum)) 5161f9708b43SJan Beulich return PTR_ERR(csum); 5162ed1f50c3SPaul Durrant 5163ed1f50c3SPaul Durrant if (recalculate) 5164f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 5165ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 5166f9708b43SJan Beulich skb->len - off, nexthdr, 0); 5167ed1f50c3SPaul Durrant err = 0; 5168ed1f50c3SPaul Durrant 5169ed1f50c3SPaul Durrant out: 5170ed1f50c3SPaul Durrant return err; 5171ed1f50c3SPaul Durrant } 5172ed1f50c3SPaul Durrant 5173ed1f50c3SPaul Durrant /** 5174ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 5175ed1f50c3SPaul Durrant * @skb: the skb to set up 5176ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 5177ed1f50c3SPaul Durrant */ 5178ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 5179ed1f50c3SPaul Durrant { 5180ed1f50c3SPaul Durrant int err; 5181ed1f50c3SPaul Durrant 5182ed1f50c3SPaul Durrant switch (skb->protocol) { 5183ed1f50c3SPaul Durrant case htons(ETH_P_IP): 5184f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 5185ed1f50c3SPaul Durrant break; 5186ed1f50c3SPaul Durrant 5187ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 5188ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 5189ed1f50c3SPaul Durrant break; 5190ed1f50c3SPaul Durrant 5191ed1f50c3SPaul Durrant default: 5192ed1f50c3SPaul Durrant err = -EPROTO; 5193ed1f50c3SPaul Durrant break; 5194ed1f50c3SPaul Durrant } 5195ed1f50c3SPaul Durrant 5196ed1f50c3SPaul Durrant return err; 5197ed1f50c3SPaul Durrant } 5198ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 5199ed1f50c3SPaul Durrant 52009afd85c9SLinus Lüssing /** 52019afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 52029afd85c9SLinus Lüssing * @skb: the skb to check 52039afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 52049afd85c9SLinus Lüssing * 52059afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 52069afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 52079afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 52089afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 52099afd85c9SLinus Lüssing * 5210a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5211a516993fSLinus Lüssing * differs from the provided skb. 52129afd85c9SLinus Lüssing */ 52139afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 52149afd85c9SLinus Lüssing unsigned int transport_len) 52159afd85c9SLinus Lüssing { 52169afd85c9SLinus Lüssing struct sk_buff *skb_chk; 52179afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 52189afd85c9SLinus Lüssing int ret; 52199afd85c9SLinus Lüssing 5220a516993fSLinus Lüssing if (skb->len < len) 52219afd85c9SLinus Lüssing return NULL; 5222a516993fSLinus Lüssing else if (skb->len == len) 52239afd85c9SLinus Lüssing return skb; 52249afd85c9SLinus Lüssing 52259afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 52269afd85c9SLinus Lüssing if (!skb_chk) 52279afd85c9SLinus Lüssing return NULL; 52289afd85c9SLinus Lüssing 52299afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 52309afd85c9SLinus Lüssing if (ret) { 52319afd85c9SLinus Lüssing kfree_skb(skb_chk); 52329afd85c9SLinus Lüssing return NULL; 52339afd85c9SLinus Lüssing } 52349afd85c9SLinus Lüssing 52359afd85c9SLinus Lüssing return skb_chk; 52369afd85c9SLinus Lüssing } 52379afd85c9SLinus Lüssing 52389afd85c9SLinus Lüssing /** 52399afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 52409afd85c9SLinus Lüssing * @skb: the skb to check 52419afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 52429afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 52439afd85c9SLinus Lüssing * 52449afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 52459afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 52469afd85c9SLinus Lüssing * 52479afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 52489afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 52499afd85c9SLinus Lüssing * 5250a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 5251a516993fSLinus Lüssing * differs from the provided skb. 52529afd85c9SLinus Lüssing */ 52539afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 52549afd85c9SLinus Lüssing unsigned int transport_len, 52559afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 52569afd85c9SLinus Lüssing { 52579afd85c9SLinus Lüssing struct sk_buff *skb_chk; 52589afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 5259fcba67c9SLinus Lüssing __sum16 ret; 52609afd85c9SLinus Lüssing 52619afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 52629afd85c9SLinus Lüssing if (!skb_chk) 5263a516993fSLinus Lüssing goto err; 52649afd85c9SLinus Lüssing 5265a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 5266a516993fSLinus Lüssing goto err; 52679afd85c9SLinus Lüssing 52689b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 52699afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 52709b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 52719afd85c9SLinus Lüssing 5272a516993fSLinus Lüssing if (ret) 5273a516993fSLinus Lüssing goto err; 52749afd85c9SLinus Lüssing 52759afd85c9SLinus Lüssing return skb_chk; 5276a516993fSLinus Lüssing 5277a516993fSLinus Lüssing err: 5278a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 5279a516993fSLinus Lüssing kfree_skb(skb_chk); 5280a516993fSLinus Lüssing 5281a516993fSLinus Lüssing return NULL; 5282a516993fSLinus Lüssing 52839afd85c9SLinus Lüssing } 52849afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 52859afd85c9SLinus Lüssing 52864497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 52874497b076SBen Hutchings { 5288e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 5289e87cc472SJoe Perches skb->dev->name); 52904497b076SBen Hutchings } 52914497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 5292bad43ca8SEric Dumazet 5293bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 5294bad43ca8SEric Dumazet { 52953d861f66SEric Dumazet if (head_stolen) { 52963d861f66SEric Dumazet skb_release_head_state(skb); 5297bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 52983d861f66SEric Dumazet } else { 5299bad43ca8SEric Dumazet __kfree_skb(skb); 5300bad43ca8SEric Dumazet } 53013d861f66SEric Dumazet } 5302bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 5303bad43ca8SEric Dumazet 5304bad43ca8SEric Dumazet /** 5305bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 5306bad43ca8SEric Dumazet * @to: prior buffer 5307bad43ca8SEric Dumazet * @from: buffer to add 5308bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 5309c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 5310bad43ca8SEric Dumazet */ 5311bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 5312bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 5313bad43ca8SEric Dumazet { 5314c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 5315bad43ca8SEric Dumazet int i, delta, len = from->len; 5316bad43ca8SEric Dumazet 5317bad43ca8SEric Dumazet *fragstolen = false; 5318bad43ca8SEric Dumazet 5319bad43ca8SEric Dumazet if (skb_cloned(to)) 5320bad43ca8SEric Dumazet return false; 5321bad43ca8SEric Dumazet 53226a5bcd84SIlias Apalodimas /* The page pool signature of struct page will eventually figure out 53236a5bcd84SIlias Apalodimas * which pages can be recycled or not but for now let's prohibit slab 53246a5bcd84SIlias Apalodimas * allocated and page_pool allocated SKBs from being coalesced. 53256a5bcd84SIlias Apalodimas */ 53266a5bcd84SIlias Apalodimas if (to->pp_recycle != from->pp_recycle) 53276a5bcd84SIlias Apalodimas return false; 53286a5bcd84SIlias Apalodimas 5329bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 5330e93a0435SEric Dumazet if (len) 5331bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 5332bad43ca8SEric Dumazet *delta_truesize = 0; 5333bad43ca8SEric Dumazet return true; 5334bad43ca8SEric Dumazet } 5335bad43ca8SEric Dumazet 5336c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 5337c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 5338c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 5339bad43ca8SEric Dumazet return false; 53401f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 53411f8b977aSWillem de Bruijn return false; 5342bad43ca8SEric Dumazet 5343bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 5344bad43ca8SEric Dumazet struct page *page; 5345bad43ca8SEric Dumazet unsigned int offset; 5346bad43ca8SEric Dumazet 5347c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5348c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 5349bad43ca8SEric Dumazet return false; 5350bad43ca8SEric Dumazet 5351bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 5352bad43ca8SEric Dumazet return false; 5353bad43ca8SEric Dumazet 5354bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 5355bad43ca8SEric Dumazet 5356bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 5357bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 5358bad43ca8SEric Dumazet 5359c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 5360bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 5361bad43ca8SEric Dumazet *fragstolen = true; 5362bad43ca8SEric Dumazet } else { 5363c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 5364c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 5365bad43ca8SEric Dumazet return false; 5366bad43ca8SEric Dumazet 5367f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 5368bad43ca8SEric Dumazet } 5369bad43ca8SEric Dumazet 5370bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 5371bad43ca8SEric Dumazet 5372c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 5373c818fa9eSEric Dumazet from_shinfo->frags, 5374c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 5375c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 5376bad43ca8SEric Dumazet 5377bad43ca8SEric Dumazet if (!skb_cloned(from)) 5378c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 5379bad43ca8SEric Dumazet 53808ea853fdSLi RongQing /* if the skb is not cloned this does nothing 53818ea853fdSLi RongQing * since we set nr_frags to 0. 53828ea853fdSLi RongQing */ 5383c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 5384c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 5385bad43ca8SEric Dumazet 5386bad43ca8SEric Dumazet to->truesize += delta; 5387bad43ca8SEric Dumazet to->len += len; 5388bad43ca8SEric Dumazet to->data_len += len; 5389bad43ca8SEric Dumazet 5390bad43ca8SEric Dumazet *delta_truesize = delta; 5391bad43ca8SEric Dumazet return true; 5392bad43ca8SEric Dumazet } 5393bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 5394621e84d6SNicolas Dichtel 5395621e84d6SNicolas Dichtel /** 53968b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 5397621e84d6SNicolas Dichtel * 5398621e84d6SNicolas Dichtel * @skb: buffer to clean 53998b27f277SNicolas Dichtel * @xnet: packet is crossing netns 5400621e84d6SNicolas Dichtel * 54018b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 54028b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 54038b27f277SNicolas Dichtel * operations. 54048b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 54058b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 54068b27f277SNicolas Dichtel * skb that could impact namespace isolation. 5407621e84d6SNicolas Dichtel */ 54088b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 5409621e84d6SNicolas Dichtel { 5410621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 5411621e84d6SNicolas Dichtel skb->skb_iif = 0; 541260ff7467SWANG Cong skb->ignore_df = 0; 5413621e84d6SNicolas Dichtel skb_dst_drop(skb); 5414174e2381SFlorian Westphal skb_ext_reset(skb); 5415895b5c9fSFlorian Westphal nf_reset_ct(skb); 5416621e84d6SNicolas Dichtel nf_reset_trace(skb); 5417213dd74aSHerbert Xu 54186f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 54196f9a5069SPetr Machata skb->offload_fwd_mark = 0; 5420875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 54216f9a5069SPetr Machata #endif 54226f9a5069SPetr Machata 5423213dd74aSHerbert Xu if (!xnet) 5424213dd74aSHerbert Xu return; 5425213dd74aSHerbert Xu 54262b5ec1a5SYe Yin ipvs_reset(skb); 5427213dd74aSHerbert Xu skb->mark = 0; 5428c47d8c2fSJesus Sanchez-Palencia skb->tstamp = 0; 5429621e84d6SNicolas Dichtel } 5430621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 5431de960aa9SFlorian Westphal 5432de960aa9SFlorian Westphal /** 5433de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 5434de960aa9SFlorian Westphal * 5435de960aa9SFlorian Westphal * @skb: GSO skb 5436de960aa9SFlorian Westphal * 5437de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 5438de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 5439de960aa9SFlorian Westphal * 5440de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 5441de960aa9SFlorian Westphal */ 5442a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 5443de960aa9SFlorian Westphal { 5444de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 5445f993bc25SFlorian Westphal unsigned int thlen = 0; 5446f993bc25SFlorian Westphal 5447f993bc25SFlorian Westphal if (skb->encapsulation) { 5448f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 5449f993bc25SFlorian Westphal skb_transport_header(skb); 5450de960aa9SFlorian Westphal 5451de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 5452f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 5453f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 5454f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 54551dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 545690017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 5457ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 5458ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 5459f993bc25SFlorian Westphal } 54606d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 54616d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 54626d39d589SFlorian Westphal * accounted for. 54636d39d589SFlorian Westphal */ 5464f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 5465de960aa9SFlorian Westphal } 5466a4a77718SDaniel Axtens 5467a4a77718SDaniel Axtens /** 5468a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 5469a4a77718SDaniel Axtens * 5470a4a77718SDaniel Axtens * @skb: GSO skb 5471a4a77718SDaniel Axtens * 5472a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 5473a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 5474a4a77718SDaniel Axtens * 5475a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 5476a4a77718SDaniel Axtens */ 5477a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 5478a4a77718SDaniel Axtens { 5479a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 5480a4a77718SDaniel Axtens skb_network_header(skb); 5481a4a77718SDaniel Axtens 5482a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5483a4a77718SDaniel Axtens } 5484a4a77718SDaniel Axtens 5485a4a77718SDaniel Axtens /** 5486a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 5487a4a77718SDaniel Axtens * 5488a4a77718SDaniel Axtens * @skb: GSO skb 5489a4a77718SDaniel Axtens * 5490a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 5491a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 5492a4a77718SDaniel Axtens * headers (TCP/UDP). 5493a4a77718SDaniel Axtens */ 5494a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5495a4a77718SDaniel Axtens { 5496a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5497a4a77718SDaniel Axtens 5498a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5499a4a77718SDaniel Axtens } 55000d5501c1SVlad Yasevich 5501ae7ef81eSMarcelo Ricardo Leitner /** 55022b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 55032b16f048SDaniel Axtens * 55042b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 55052b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 55062b16f048SDaniel Axtens * 55072b16f048SDaniel Axtens * We might want to check: 55082b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 55092b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 55102b16f048SDaniel Axtens * 55112b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 55122b16f048SDaniel Axtens * 551349682bfaSMathieu Malaterre * @skb: GSO skb 551449682bfaSMathieu Malaterre * 55152b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 55162b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 55172b16f048SDaniel Axtens * 55182b16f048SDaniel Axtens * @max_len: The maximum permissible length. 55192b16f048SDaniel Axtens * 55202b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 55212b16f048SDaniel Axtens */ 55222b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 55232b16f048SDaniel Axtens unsigned int seg_len, 55242b16f048SDaniel Axtens unsigned int max_len) { 55252b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 55262b16f048SDaniel Axtens const struct sk_buff *iter; 55272b16f048SDaniel Axtens 55282b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 55292b16f048SDaniel Axtens return seg_len <= max_len; 55302b16f048SDaniel Axtens 55312b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 55322b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 55332b16f048SDaniel Axtens 55342b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 55352b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 55362b16f048SDaniel Axtens return false; 55372b16f048SDaniel Axtens } 55382b16f048SDaniel Axtens 55392b16f048SDaniel Axtens return true; 55402b16f048SDaniel Axtens } 55412b16f048SDaniel Axtens 55422b16f048SDaniel Axtens /** 5543779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5544ae7ef81eSMarcelo Ricardo Leitner * 5545ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 554676f21b99SDavid S. Miller * @mtu: MTU to validate against 5547ae7ef81eSMarcelo Ricardo Leitner * 5548779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5549779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5550779b7931SDaniel Axtens * payload. 5551ae7ef81eSMarcelo Ricardo Leitner */ 5552779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5553ae7ef81eSMarcelo Ricardo Leitner { 55542b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5555ae7ef81eSMarcelo Ricardo Leitner } 5556779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5557ae7ef81eSMarcelo Ricardo Leitner 55582b16f048SDaniel Axtens /** 55592b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 55602b16f048SDaniel Axtens * 55612b16f048SDaniel Axtens * @skb: GSO skb 55622b16f048SDaniel Axtens * @len: length to validate against 55632b16f048SDaniel Axtens * 55642b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 55652b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 55662b16f048SDaniel Axtens */ 55672b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 55682b16f048SDaniel Axtens { 55692b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 55702b16f048SDaniel Axtens } 55712b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 55722b16f048SDaniel Axtens 55730d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 55740d5501c1SVlad Yasevich { 5575d85e8be2SYuya Kusakabe int mac_len, meta_len; 5576d85e8be2SYuya Kusakabe void *meta; 55774bbb3e0eSToshiaki Makita 55780d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 55790d5501c1SVlad Yasevich kfree_skb(skb); 55800d5501c1SVlad Yasevich return NULL; 55810d5501c1SVlad Yasevich } 55820d5501c1SVlad Yasevich 55834bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5584ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 55854bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 55864bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5587ae474573SToshiaki Makita } 5588d85e8be2SYuya Kusakabe 5589d85e8be2SYuya Kusakabe meta_len = skb_metadata_len(skb); 5590d85e8be2SYuya Kusakabe if (meta_len) { 5591d85e8be2SYuya Kusakabe meta = skb_metadata_end(skb) - meta_len; 5592d85e8be2SYuya Kusakabe memmove(meta + VLAN_HLEN, meta, meta_len); 5593d85e8be2SYuya Kusakabe } 5594d85e8be2SYuya Kusakabe 55950d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 55960d5501c1SVlad Yasevich return skb; 55970d5501c1SVlad Yasevich } 55980d5501c1SVlad Yasevich 55990d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 56000d5501c1SVlad Yasevich { 56010d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 56020d5501c1SVlad Yasevich u16 vlan_tci; 56030d5501c1SVlad Yasevich 5604df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 56050d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 56060d5501c1SVlad Yasevich return skb; 56070d5501c1SVlad Yasevich } 56080d5501c1SVlad Yasevich 56090d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 56100d5501c1SVlad Yasevich if (unlikely(!skb)) 56110d5501c1SVlad Yasevich goto err_free; 561255eff0ebSMiaohe Lin /* We may access the two bytes after vlan_hdr in vlan_set_encap_proto(). */ 561355eff0ebSMiaohe Lin if (unlikely(!pskb_may_pull(skb, VLAN_HLEN + sizeof(unsigned short)))) 56140d5501c1SVlad Yasevich goto err_free; 56150d5501c1SVlad Yasevich 56160d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 56170d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 56180d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 56190d5501c1SVlad Yasevich 56200d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 56210d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 56220d5501c1SVlad Yasevich 56230d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 56240d5501c1SVlad Yasevich if (unlikely(!skb)) 56250d5501c1SVlad Yasevich goto err_free; 56260d5501c1SVlad Yasevich 56270d5501c1SVlad Yasevich skb_reset_network_header(skb); 56288be33ecfSAlexander Lobakin if (!skb_transport_header_was_set(skb)) 56290d5501c1SVlad Yasevich skb_reset_transport_header(skb); 56300d5501c1SVlad Yasevich skb_reset_mac_len(skb); 56310d5501c1SVlad Yasevich 56320d5501c1SVlad Yasevich return skb; 56330d5501c1SVlad Yasevich 56340d5501c1SVlad Yasevich err_free: 56350d5501c1SVlad Yasevich kfree_skb(skb); 56360d5501c1SVlad Yasevich return NULL; 56370d5501c1SVlad Yasevich } 56380d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 56392e4e4410SEric Dumazet 5640e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 5641e2195121SJiri Pirko { 5642e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5643e2195121SJiri Pirko return -ENOMEM; 5644e2195121SJiri Pirko 5645e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5646e2195121SJiri Pirko return 0; 5647e2195121SJiri Pirko 5648e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5649e2195121SJiri Pirko } 5650e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5651e2195121SJiri Pirko 5652bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5653bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5654bfca4c52SShmulik Ladkani */ 5655bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 565693515d53SJiri Pirko { 565793515d53SJiri Pirko struct vlan_hdr *vhdr; 5658b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 565993515d53SJiri Pirko int err; 566093515d53SJiri Pirko 5661b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5662b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5663b6a79208SShmulik Ladkani offset)) { 5664b6a79208SShmulik Ladkani return -EINVAL; 5665b6a79208SShmulik Ladkani } 5666b6a79208SShmulik Ladkani 566793515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 566893515d53SJiri Pirko if (unlikely(err)) 5669b6a79208SShmulik Ladkani return err; 567093515d53SJiri Pirko 567193515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 567293515d53SJiri Pirko 567393515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 567493515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 567593515d53SJiri Pirko 567693515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 567793515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 567893515d53SJiri Pirko 567993515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 568093515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 568193515d53SJiri Pirko 568293515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 568393515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 568493515d53SJiri Pirko 568593515d53SJiri Pirko skb_reset_mac_len(skb); 568693515d53SJiri Pirko 568793515d53SJiri Pirko return err; 568893515d53SJiri Pirko } 5689bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 569093515d53SJiri Pirko 5691b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5692b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5693b6a79208SShmulik Ladkani */ 569493515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 569593515d53SJiri Pirko { 569693515d53SJiri Pirko u16 vlan_tci; 569793515d53SJiri Pirko __be16 vlan_proto; 569893515d53SJiri Pirko int err; 569993515d53SJiri Pirko 5700df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5701b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 570293515d53SJiri Pirko } else { 5703ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 570493515d53SJiri Pirko return 0; 570593515d53SJiri Pirko 570693515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 570793515d53SJiri Pirko if (err) 570893515d53SJiri Pirko return err; 570993515d53SJiri Pirko } 571093515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5711ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 571293515d53SJiri Pirko return 0; 571393515d53SJiri Pirko 571493515d53SJiri Pirko vlan_proto = skb->protocol; 571593515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 571693515d53SJiri Pirko if (unlikely(err)) 571793515d53SJiri Pirko return err; 571893515d53SJiri Pirko 571993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 572093515d53SJiri Pirko return 0; 572193515d53SJiri Pirko } 572293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 572393515d53SJiri Pirko 5724b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5725b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5726b6a79208SShmulik Ladkani */ 572793515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 572893515d53SJiri Pirko { 5729df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5730b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 573193515d53SJiri Pirko int err; 573293515d53SJiri Pirko 5733b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5734b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5735b6a79208SShmulik Ladkani offset)) { 5736b6a79208SShmulik Ladkani return -EINVAL; 5737b6a79208SShmulik Ladkani } 5738b6a79208SShmulik Ladkani 573993515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5740df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5741b6a79208SShmulik Ladkani if (err) 574293515d53SJiri Pirko return err; 57439241e2dfSDaniel Borkmann 574493515d53SJiri Pirko skb->protocol = skb->vlan_proto; 574593515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 574693515d53SJiri Pirko 57476b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 574893515d53SJiri Pirko } 574993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 575093515d53SJiri Pirko return 0; 575193515d53SJiri Pirko } 575293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 575393515d53SJiri Pirko 575419fbcb36SGuillaume Nault /** 575519fbcb36SGuillaume Nault * skb_eth_pop() - Drop the Ethernet header at the head of a packet 575619fbcb36SGuillaume Nault * 575719fbcb36SGuillaume Nault * @skb: Socket buffer to modify 575819fbcb36SGuillaume Nault * 575919fbcb36SGuillaume Nault * Drop the Ethernet header of @skb. 576019fbcb36SGuillaume Nault * 576119fbcb36SGuillaume Nault * Expects that skb->data points to the mac header and that no VLAN tags are 576219fbcb36SGuillaume Nault * present. 576319fbcb36SGuillaume Nault * 576419fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 576519fbcb36SGuillaume Nault */ 576619fbcb36SGuillaume Nault int skb_eth_pop(struct sk_buff *skb) 576719fbcb36SGuillaume Nault { 576819fbcb36SGuillaume Nault if (!pskb_may_pull(skb, ETH_HLEN) || skb_vlan_tagged(skb) || 576919fbcb36SGuillaume Nault skb_network_offset(skb) < ETH_HLEN) 577019fbcb36SGuillaume Nault return -EPROTO; 577119fbcb36SGuillaume Nault 577219fbcb36SGuillaume Nault skb_pull_rcsum(skb, ETH_HLEN); 577319fbcb36SGuillaume Nault skb_reset_mac_header(skb); 577419fbcb36SGuillaume Nault skb_reset_mac_len(skb); 577519fbcb36SGuillaume Nault 577619fbcb36SGuillaume Nault return 0; 577719fbcb36SGuillaume Nault } 577819fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_pop); 577919fbcb36SGuillaume Nault 578019fbcb36SGuillaume Nault /** 578119fbcb36SGuillaume Nault * skb_eth_push() - Add a new Ethernet header at the head of a packet 578219fbcb36SGuillaume Nault * 578319fbcb36SGuillaume Nault * @skb: Socket buffer to modify 578419fbcb36SGuillaume Nault * @dst: Destination MAC address of the new header 578519fbcb36SGuillaume Nault * @src: Source MAC address of the new header 578619fbcb36SGuillaume Nault * 578719fbcb36SGuillaume Nault * Prepend @skb with a new Ethernet header. 578819fbcb36SGuillaume Nault * 578919fbcb36SGuillaume Nault * Expects that skb->data points to the mac header, which must be empty. 579019fbcb36SGuillaume Nault * 579119fbcb36SGuillaume Nault * Returns 0 on success, -errno otherwise. 579219fbcb36SGuillaume Nault */ 579319fbcb36SGuillaume Nault int skb_eth_push(struct sk_buff *skb, const unsigned char *dst, 579419fbcb36SGuillaume Nault const unsigned char *src) 579519fbcb36SGuillaume Nault { 579619fbcb36SGuillaume Nault struct ethhdr *eth; 579719fbcb36SGuillaume Nault int err; 579819fbcb36SGuillaume Nault 579919fbcb36SGuillaume Nault if (skb_network_offset(skb) || skb_vlan_tag_present(skb)) 580019fbcb36SGuillaume Nault return -EPROTO; 580119fbcb36SGuillaume Nault 580219fbcb36SGuillaume Nault err = skb_cow_head(skb, sizeof(*eth)); 580319fbcb36SGuillaume Nault if (err < 0) 580419fbcb36SGuillaume Nault return err; 580519fbcb36SGuillaume Nault 580619fbcb36SGuillaume Nault skb_push(skb, sizeof(*eth)); 580719fbcb36SGuillaume Nault skb_reset_mac_header(skb); 580819fbcb36SGuillaume Nault skb_reset_mac_len(skb); 580919fbcb36SGuillaume Nault 581019fbcb36SGuillaume Nault eth = eth_hdr(skb); 581119fbcb36SGuillaume Nault ether_addr_copy(eth->h_dest, dst); 581219fbcb36SGuillaume Nault ether_addr_copy(eth->h_source, src); 581319fbcb36SGuillaume Nault eth->h_proto = skb->protocol; 581419fbcb36SGuillaume Nault 581519fbcb36SGuillaume Nault skb_postpush_rcsum(skb, eth, sizeof(*eth)); 581619fbcb36SGuillaume Nault 581719fbcb36SGuillaume Nault return 0; 581819fbcb36SGuillaume Nault } 581919fbcb36SGuillaume Nault EXPORT_SYMBOL(skb_eth_push); 582019fbcb36SGuillaume Nault 58218822e270SJohn Hurley /* Update the ethertype of hdr and the skb csum value if required. */ 58228822e270SJohn Hurley static void skb_mod_eth_type(struct sk_buff *skb, struct ethhdr *hdr, 58238822e270SJohn Hurley __be16 ethertype) 58248822e270SJohn Hurley { 58258822e270SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 58268822e270SJohn Hurley __be16 diff[] = { ~hdr->h_proto, ethertype }; 58278822e270SJohn Hurley 58288822e270SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 58298822e270SJohn Hurley } 58308822e270SJohn Hurley 58318822e270SJohn Hurley hdr->h_proto = ethertype; 58328822e270SJohn Hurley } 58338822e270SJohn Hurley 58348822e270SJohn Hurley /** 5835e7dbfed1SMartin Varghese * skb_mpls_push() - push a new MPLS header after mac_len bytes from start of 5836e7dbfed1SMartin Varghese * the packet 58378822e270SJohn Hurley * 58388822e270SJohn Hurley * @skb: buffer 58398822e270SJohn Hurley * @mpls_lse: MPLS label stack entry to push 58408822e270SJohn Hurley * @mpls_proto: ethertype of the new MPLS header (expects 0x8847 or 0x8848) 5841fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 5842e7dbfed1SMartin Varghese * @ethernet: flag to indicate if the resulting packet after skb_mpls_push is 5843e7dbfed1SMartin Varghese * ethernet 58448822e270SJohn Hurley * 58458822e270SJohn Hurley * Expects skb->data at mac header. 58468822e270SJohn Hurley * 58478822e270SJohn Hurley * Returns 0 on success, -errno otherwise. 58488822e270SJohn Hurley */ 5849fa4e0f88SDavide Caratti int skb_mpls_push(struct sk_buff *skb, __be32 mpls_lse, __be16 mpls_proto, 5850d04ac224SMartin Varghese int mac_len, bool ethernet) 58518822e270SJohn Hurley { 58528822e270SJohn Hurley struct mpls_shim_hdr *lse; 58538822e270SJohn Hurley int err; 58548822e270SJohn Hurley 58558822e270SJohn Hurley if (unlikely(!eth_p_mpls(mpls_proto))) 58568822e270SJohn Hurley return -EINVAL; 58578822e270SJohn Hurley 58588822e270SJohn Hurley /* Networking stack does not allow simultaneous Tunnel and MPLS GSO. */ 58598822e270SJohn Hurley if (skb->encapsulation) 58608822e270SJohn Hurley return -EINVAL; 58618822e270SJohn Hurley 58628822e270SJohn Hurley err = skb_cow_head(skb, MPLS_HLEN); 58638822e270SJohn Hurley if (unlikely(err)) 58648822e270SJohn Hurley return err; 58658822e270SJohn Hurley 58668822e270SJohn Hurley if (!skb->inner_protocol) { 5867e7dbfed1SMartin Varghese skb_set_inner_network_header(skb, skb_network_offset(skb)); 58688822e270SJohn Hurley skb_set_inner_protocol(skb, skb->protocol); 58698822e270SJohn Hurley } 58708822e270SJohn Hurley 58718822e270SJohn Hurley skb_push(skb, MPLS_HLEN); 58728822e270SJohn Hurley memmove(skb_mac_header(skb) - MPLS_HLEN, skb_mac_header(skb), 5873fa4e0f88SDavide Caratti mac_len); 58748822e270SJohn Hurley skb_reset_mac_header(skb); 5875fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5876e7dbfed1SMartin Varghese skb_reset_mac_len(skb); 58778822e270SJohn Hurley 58788822e270SJohn Hurley lse = mpls_hdr(skb); 58798822e270SJohn Hurley lse->label_stack_entry = mpls_lse; 58808822e270SJohn Hurley skb_postpush_rcsum(skb, lse, MPLS_HLEN); 58818822e270SJohn Hurley 58824296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) 58838822e270SJohn Hurley skb_mod_eth_type(skb, eth_hdr(skb), mpls_proto); 58848822e270SJohn Hurley skb->protocol = mpls_proto; 58858822e270SJohn Hurley 58868822e270SJohn Hurley return 0; 58878822e270SJohn Hurley } 58888822e270SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_push); 58898822e270SJohn Hurley 58902e4e4410SEric Dumazet /** 5891ed246ceeSJohn Hurley * skb_mpls_pop() - pop the outermost MPLS header 5892ed246ceeSJohn Hurley * 5893ed246ceeSJohn Hurley * @skb: buffer 5894ed246ceeSJohn Hurley * @next_proto: ethertype of header after popped MPLS header 5895fa4e0f88SDavide Caratti * @mac_len: length of the MAC header 589676f99f98SMartin Varghese * @ethernet: flag to indicate if the packet is ethernet 5897ed246ceeSJohn Hurley * 5898ed246ceeSJohn Hurley * Expects skb->data at mac header. 5899ed246ceeSJohn Hurley * 5900ed246ceeSJohn Hurley * Returns 0 on success, -errno otherwise. 5901ed246ceeSJohn Hurley */ 5902040b5cfbSMartin Varghese int skb_mpls_pop(struct sk_buff *skb, __be16 next_proto, int mac_len, 5903040b5cfbSMartin Varghese bool ethernet) 5904ed246ceeSJohn Hurley { 5905ed246ceeSJohn Hurley int err; 5906ed246ceeSJohn Hurley 5907ed246ceeSJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5908dedc5a08SDavide Caratti return 0; 5909ed246ceeSJohn Hurley 5910fa4e0f88SDavide Caratti err = skb_ensure_writable(skb, mac_len + MPLS_HLEN); 5911ed246ceeSJohn Hurley if (unlikely(err)) 5912ed246ceeSJohn Hurley return err; 5913ed246ceeSJohn Hurley 5914ed246ceeSJohn Hurley skb_postpull_rcsum(skb, mpls_hdr(skb), MPLS_HLEN); 5915ed246ceeSJohn Hurley memmove(skb_mac_header(skb) + MPLS_HLEN, skb_mac_header(skb), 5916fa4e0f88SDavide Caratti mac_len); 5917ed246ceeSJohn Hurley 5918ed246ceeSJohn Hurley __skb_pull(skb, MPLS_HLEN); 5919ed246ceeSJohn Hurley skb_reset_mac_header(skb); 5920fa4e0f88SDavide Caratti skb_set_network_header(skb, mac_len); 5921ed246ceeSJohn Hurley 59224296adc3SGuillaume Nault if (ethernet && mac_len >= ETH_HLEN) { 5923ed246ceeSJohn Hurley struct ethhdr *hdr; 5924ed246ceeSJohn Hurley 5925ed246ceeSJohn Hurley /* use mpls_hdr() to get ethertype to account for VLANs. */ 5926ed246ceeSJohn Hurley hdr = (struct ethhdr *)((void *)mpls_hdr(skb) - ETH_HLEN); 5927ed246ceeSJohn Hurley skb_mod_eth_type(skb, hdr, next_proto); 5928ed246ceeSJohn Hurley } 5929ed246ceeSJohn Hurley skb->protocol = next_proto; 5930ed246ceeSJohn Hurley 5931ed246ceeSJohn Hurley return 0; 5932ed246ceeSJohn Hurley } 5933ed246ceeSJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_pop); 5934ed246ceeSJohn Hurley 5935ed246ceeSJohn Hurley /** 5936d27cf5c5SJohn Hurley * skb_mpls_update_lse() - modify outermost MPLS header and update csum 5937d27cf5c5SJohn Hurley * 5938d27cf5c5SJohn Hurley * @skb: buffer 5939d27cf5c5SJohn Hurley * @mpls_lse: new MPLS label stack entry to update to 5940d27cf5c5SJohn Hurley * 5941d27cf5c5SJohn Hurley * Expects skb->data at mac header. 5942d27cf5c5SJohn Hurley * 5943d27cf5c5SJohn Hurley * Returns 0 on success, -errno otherwise. 5944d27cf5c5SJohn Hurley */ 5945d27cf5c5SJohn Hurley int skb_mpls_update_lse(struct sk_buff *skb, __be32 mpls_lse) 5946d27cf5c5SJohn Hurley { 5947d27cf5c5SJohn Hurley int err; 5948d27cf5c5SJohn Hurley 5949d27cf5c5SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 5950d27cf5c5SJohn Hurley return -EINVAL; 5951d27cf5c5SJohn Hurley 5952d27cf5c5SJohn Hurley err = skb_ensure_writable(skb, skb->mac_len + MPLS_HLEN); 5953d27cf5c5SJohn Hurley if (unlikely(err)) 5954d27cf5c5SJohn Hurley return err; 5955d27cf5c5SJohn Hurley 5956d27cf5c5SJohn Hurley if (skb->ip_summed == CHECKSUM_COMPLETE) { 5957d27cf5c5SJohn Hurley __be32 diff[] = { ~mpls_hdr(skb)->label_stack_entry, mpls_lse }; 5958d27cf5c5SJohn Hurley 5959d27cf5c5SJohn Hurley skb->csum = csum_partial((char *)diff, sizeof(diff), skb->csum); 5960d27cf5c5SJohn Hurley } 5961d27cf5c5SJohn Hurley 5962d27cf5c5SJohn Hurley mpls_hdr(skb)->label_stack_entry = mpls_lse; 5963d27cf5c5SJohn Hurley 5964d27cf5c5SJohn Hurley return 0; 5965d27cf5c5SJohn Hurley } 5966d27cf5c5SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_update_lse); 5967d27cf5c5SJohn Hurley 5968d27cf5c5SJohn Hurley /** 59692a2ea508SJohn Hurley * skb_mpls_dec_ttl() - decrement the TTL of the outermost MPLS header 59702a2ea508SJohn Hurley * 59712a2ea508SJohn Hurley * @skb: buffer 59722a2ea508SJohn Hurley * 59732a2ea508SJohn Hurley * Expects skb->data at mac header. 59742a2ea508SJohn Hurley * 59752a2ea508SJohn Hurley * Returns 0 on success, -errno otherwise. 59762a2ea508SJohn Hurley */ 59772a2ea508SJohn Hurley int skb_mpls_dec_ttl(struct sk_buff *skb) 59782a2ea508SJohn Hurley { 59792a2ea508SJohn Hurley u32 lse; 59802a2ea508SJohn Hurley u8 ttl; 59812a2ea508SJohn Hurley 59822a2ea508SJohn Hurley if (unlikely(!eth_p_mpls(skb->protocol))) 59832a2ea508SJohn Hurley return -EINVAL; 59842a2ea508SJohn Hurley 598513de4ed9SDavide Caratti if (!pskb_may_pull(skb, skb_network_offset(skb) + MPLS_HLEN)) 598613de4ed9SDavide Caratti return -ENOMEM; 598713de4ed9SDavide Caratti 59882a2ea508SJohn Hurley lse = be32_to_cpu(mpls_hdr(skb)->label_stack_entry); 59892a2ea508SJohn Hurley ttl = (lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT; 59902a2ea508SJohn Hurley if (!--ttl) 59912a2ea508SJohn Hurley return -EINVAL; 59922a2ea508SJohn Hurley 59932a2ea508SJohn Hurley lse &= ~MPLS_LS_TTL_MASK; 59942a2ea508SJohn Hurley lse |= ttl << MPLS_LS_TTL_SHIFT; 59952a2ea508SJohn Hurley 59962a2ea508SJohn Hurley return skb_mpls_update_lse(skb, cpu_to_be32(lse)); 59972a2ea508SJohn Hurley } 59982a2ea508SJohn Hurley EXPORT_SYMBOL_GPL(skb_mpls_dec_ttl); 59992a2ea508SJohn Hurley 60002a2ea508SJohn Hurley /** 60012e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 60022e4e4410SEric Dumazet * 6003de3f0d0eSMasanari Iida * @header_len: size of linear part 6004de3f0d0eSMasanari Iida * @data_len: needed length in frags 6005de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 6006de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 6007de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 60082e4e4410SEric Dumazet * 60092e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 60102e4e4410SEric Dumazet */ 60112e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 60122e4e4410SEric Dumazet unsigned long data_len, 60132e4e4410SEric Dumazet int max_page_order, 60142e4e4410SEric Dumazet int *errcode, 60152e4e4410SEric Dumazet gfp_t gfp_mask) 60162e4e4410SEric Dumazet { 60172e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 60182e4e4410SEric Dumazet unsigned long chunk; 60192e4e4410SEric Dumazet struct sk_buff *skb; 60202e4e4410SEric Dumazet struct page *page; 60212e4e4410SEric Dumazet int i; 60222e4e4410SEric Dumazet 60232e4e4410SEric Dumazet *errcode = -EMSGSIZE; 60242e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 60252e4e4410SEric Dumazet * high order pages... 60262e4e4410SEric Dumazet */ 60272e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 60282e4e4410SEric Dumazet return NULL; 60292e4e4410SEric Dumazet 60302e4e4410SEric Dumazet *errcode = -ENOBUFS; 6031f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 60322e4e4410SEric Dumazet if (!skb) 60332e4e4410SEric Dumazet return NULL; 60342e4e4410SEric Dumazet 60352e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 60362e4e4410SEric Dumazet 60372e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 60382e4e4410SEric Dumazet int order = max_page_order; 60392e4e4410SEric Dumazet 60402e4e4410SEric Dumazet while (order) { 60412e4e4410SEric Dumazet if (npages >= 1 << order) { 6042d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 60432e4e4410SEric Dumazet __GFP_COMP | 6044d14b56f5SMichal Hocko __GFP_NOWARN, 60452e4e4410SEric Dumazet order); 60462e4e4410SEric Dumazet if (page) 60472e4e4410SEric Dumazet goto fill_page; 60482e4e4410SEric Dumazet /* Do not retry other high order allocations */ 60492e4e4410SEric Dumazet order = 1; 60502e4e4410SEric Dumazet max_page_order = 0; 60512e4e4410SEric Dumazet } 60522e4e4410SEric Dumazet order--; 60532e4e4410SEric Dumazet } 60542e4e4410SEric Dumazet page = alloc_page(gfp_mask); 60552e4e4410SEric Dumazet if (!page) 60562e4e4410SEric Dumazet goto failure; 60572e4e4410SEric Dumazet fill_page: 60582e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 60592e4e4410SEric Dumazet PAGE_SIZE << order); 60602e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 60612e4e4410SEric Dumazet data_len -= chunk; 60622e4e4410SEric Dumazet npages -= 1 << order; 60632e4e4410SEric Dumazet } 60642e4e4410SEric Dumazet return skb; 60652e4e4410SEric Dumazet 60662e4e4410SEric Dumazet failure: 60672e4e4410SEric Dumazet kfree_skb(skb); 60682e4e4410SEric Dumazet return NULL; 60692e4e4410SEric Dumazet } 60702e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 60716fa01ccdSSowmini Varadhan 60726fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 60736fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 60746fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 60756fa01ccdSSowmini Varadhan { 60766fa01ccdSSowmini Varadhan int i; 60776fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 60786fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 60796fa01ccdSSowmini Varadhan u8 *data; 60806fa01ccdSSowmini Varadhan 60816fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 60826fa01ccdSSowmini Varadhan 60836fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 60846fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 60856fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 60866fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 60876fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 60886fa01ccdSSowmini Varadhan if (!data) 60896fa01ccdSSowmini Varadhan return -ENOMEM; 60906fa01ccdSSowmini Varadhan 60916fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 60926fa01ccdSSowmini Varadhan 60936fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 60946fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 60956fa01ccdSSowmini Varadhan skb->len -= off; 60966fa01ccdSSowmini Varadhan 60976fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 60986fa01ccdSSowmini Varadhan skb_shinfo(skb), 60996fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 61006fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 61016fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 61026fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 61036fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 61046fa01ccdSSowmini Varadhan kfree(data); 61056fa01ccdSSowmini Varadhan return -ENOMEM; 61066fa01ccdSSowmini Varadhan } 61076fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 61086fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 61096fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 61106fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 61116fa01ccdSSowmini Varadhan skb_release_data(skb); 61126fa01ccdSSowmini Varadhan } else { 61136fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 61146fa01ccdSSowmini Varadhan * relocate values 61156fa01ccdSSowmini Varadhan */ 61166fa01ccdSSowmini Varadhan skb_free_head(skb); 61176fa01ccdSSowmini Varadhan } 61186fa01ccdSSowmini Varadhan 61196fa01ccdSSowmini Varadhan skb->head = data; 61206fa01ccdSSowmini Varadhan skb->data = data; 61216fa01ccdSSowmini Varadhan skb->head_frag = 0; 61226fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 61236fa01ccdSSowmini Varadhan skb->end = size; 61246fa01ccdSSowmini Varadhan #else 61256fa01ccdSSowmini Varadhan skb->end = skb->head + size; 61266fa01ccdSSowmini Varadhan #endif 61276fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 61286fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 61296fa01ccdSSowmini Varadhan skb->cloned = 0; 61306fa01ccdSSowmini Varadhan skb->hdr_len = 0; 61316fa01ccdSSowmini Varadhan skb->nohdr = 0; 61326fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 61336fa01ccdSSowmini Varadhan 61346fa01ccdSSowmini Varadhan return 0; 61356fa01ccdSSowmini Varadhan } 61366fa01ccdSSowmini Varadhan 61376fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 61386fa01ccdSSowmini Varadhan 61396fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 61406fa01ccdSSowmini Varadhan * pskb_carve() 61416fa01ccdSSowmini Varadhan */ 61426fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 61436fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 61446fa01ccdSSowmini Varadhan gfp_t gfp_mask) 61456fa01ccdSSowmini Varadhan { 61466fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 61476fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 61486fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 61496fa01ccdSSowmini Varadhan 61506fa01ccdSSowmini Varadhan do { 61516fa01ccdSSowmini Varadhan if (!list) { 61526fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 61536fa01ccdSSowmini Varadhan return -EFAULT; 61546fa01ccdSSowmini Varadhan } 61556fa01ccdSSowmini Varadhan if (list->len <= eat) { 61566fa01ccdSSowmini Varadhan /* Eaten as whole. */ 61576fa01ccdSSowmini Varadhan eat -= list->len; 61586fa01ccdSSowmini Varadhan list = list->next; 61596fa01ccdSSowmini Varadhan insp = list; 61606fa01ccdSSowmini Varadhan } else { 61616fa01ccdSSowmini Varadhan /* Eaten partially. */ 61626fa01ccdSSowmini Varadhan if (skb_shared(list)) { 61636fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 61646fa01ccdSSowmini Varadhan if (!clone) 61656fa01ccdSSowmini Varadhan return -ENOMEM; 61666fa01ccdSSowmini Varadhan insp = list->next; 61676fa01ccdSSowmini Varadhan list = clone; 61686fa01ccdSSowmini Varadhan } else { 61696fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 61706fa01ccdSSowmini Varadhan insp = list; 61716fa01ccdSSowmini Varadhan } 61726fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 61736fa01ccdSSowmini Varadhan kfree_skb(clone); 61746fa01ccdSSowmini Varadhan return -ENOMEM; 61756fa01ccdSSowmini Varadhan } 61766fa01ccdSSowmini Varadhan break; 61776fa01ccdSSowmini Varadhan } 61786fa01ccdSSowmini Varadhan } while (eat); 61796fa01ccdSSowmini Varadhan 61806fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 61816fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 61826fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 61836fa01ccdSSowmini Varadhan kfree_skb(list); 61846fa01ccdSSowmini Varadhan } 61856fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 61866fa01ccdSSowmini Varadhan if (clone) { 61876fa01ccdSSowmini Varadhan clone->next = list; 61886fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 61896fa01ccdSSowmini Varadhan } 61906fa01ccdSSowmini Varadhan return 0; 61916fa01ccdSSowmini Varadhan } 61926fa01ccdSSowmini Varadhan 61936fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 61946fa01ccdSSowmini Varadhan * non-linear part of skb 61956fa01ccdSSowmini Varadhan */ 61966fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 61976fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 61986fa01ccdSSowmini Varadhan { 61996fa01ccdSSowmini Varadhan int i, k = 0; 62006fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 62016fa01ccdSSowmini Varadhan u8 *data; 62026fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 62036fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 62046fa01ccdSSowmini Varadhan 62056fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 62066fa01ccdSSowmini Varadhan 62076fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 62086fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 62096fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 62106fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 62116fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 62126fa01ccdSSowmini Varadhan if (!data) 62136fa01ccdSSowmini Varadhan return -ENOMEM; 62146fa01ccdSSowmini Varadhan 62156fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 62166fa01ccdSSowmini Varadhan 62176fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 6218e3ec1e8cSMiaohe Lin skb_shinfo(skb), offsetof(struct skb_shared_info, frags[0])); 62196fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 62206fa01ccdSSowmini Varadhan kfree(data); 62216fa01ccdSSowmini Varadhan return -ENOMEM; 62226fa01ccdSSowmini Varadhan } 62236fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 62246fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 62256fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 62266fa01ccdSSowmini Varadhan 62276fa01ccdSSowmini Varadhan if (pos + fsize > off) { 62286fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 62296fa01ccdSSowmini Varadhan 62306fa01ccdSSowmini Varadhan if (pos < off) { 62316fa01ccdSSowmini Varadhan /* Split frag. 62326fa01ccdSSowmini Varadhan * We have two variants in this case: 62336fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 62346fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 62356fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 62366fa01ccdSSowmini Varadhan * where splitting is expensive. 62376fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 62386fa01ccdSSowmini Varadhan */ 6239b54c9d5bSJonathan Lemon skb_frag_off_add(&shinfo->frags[0], off - pos); 62406fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 62416fa01ccdSSowmini Varadhan } 62426fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 62436fa01ccdSSowmini Varadhan k++; 62446fa01ccdSSowmini Varadhan } 62456fa01ccdSSowmini Varadhan pos += fsize; 62466fa01ccdSSowmini Varadhan } 62476fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 62486fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 62496fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 62506fa01ccdSSowmini Varadhan 62516fa01ccdSSowmini Varadhan /* split line is in frag list */ 6252eabe8618SMiaohe Lin if (k == 0 && pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask)) { 6253eabe8618SMiaohe Lin /* skb_frag_unref() is not needed here as shinfo->nr_frags = 0. */ 6254eabe8618SMiaohe Lin if (skb_has_frag_list(skb)) 6255eabe8618SMiaohe Lin kfree_skb_list(skb_shinfo(skb)->frag_list); 6256eabe8618SMiaohe Lin kfree(data); 6257eabe8618SMiaohe Lin return -ENOMEM; 62586fa01ccdSSowmini Varadhan } 62596fa01ccdSSowmini Varadhan skb_release_data(skb); 62606fa01ccdSSowmini Varadhan 62616fa01ccdSSowmini Varadhan skb->head = data; 62626fa01ccdSSowmini Varadhan skb->head_frag = 0; 62636fa01ccdSSowmini Varadhan skb->data = data; 62646fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 62656fa01ccdSSowmini Varadhan skb->end = size; 62666fa01ccdSSowmini Varadhan #else 62676fa01ccdSSowmini Varadhan skb->end = skb->head + size; 62686fa01ccdSSowmini Varadhan #endif 62696fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 62706fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 62716fa01ccdSSowmini Varadhan skb->cloned = 0; 62726fa01ccdSSowmini Varadhan skb->hdr_len = 0; 62736fa01ccdSSowmini Varadhan skb->nohdr = 0; 62746fa01ccdSSowmini Varadhan skb->len -= off; 62756fa01ccdSSowmini Varadhan skb->data_len = skb->len; 62766fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 62776fa01ccdSSowmini Varadhan return 0; 62786fa01ccdSSowmini Varadhan } 62796fa01ccdSSowmini Varadhan 62806fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 62816fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 62826fa01ccdSSowmini Varadhan { 62836fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 62846fa01ccdSSowmini Varadhan 62856fa01ccdSSowmini Varadhan if (len < headlen) 62866fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 62876fa01ccdSSowmini Varadhan else 62886fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 62896fa01ccdSSowmini Varadhan } 62906fa01ccdSSowmini Varadhan 62916fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 62926fa01ccdSSowmini Varadhan * a new skb 62936fa01ccdSSowmini Varadhan */ 62946fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 62956fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 62966fa01ccdSSowmini Varadhan { 62976fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 62986fa01ccdSSowmini Varadhan 62996fa01ccdSSowmini Varadhan if (!clone) 63006fa01ccdSSowmini Varadhan return NULL; 63016fa01ccdSSowmini Varadhan 63026fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 63036fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 63046fa01ccdSSowmini Varadhan kfree_skb(clone); 63056fa01ccdSSowmini Varadhan return NULL; 63066fa01ccdSSowmini Varadhan } 63076fa01ccdSSowmini Varadhan return clone; 63086fa01ccdSSowmini Varadhan } 63096fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 6310c8c8b127SEric Dumazet 6311c8c8b127SEric Dumazet /** 6312c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 6313c8c8b127SEric Dumazet * @skb: buffer 6314c8c8b127SEric Dumazet * 6315c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 6316c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 6317c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 6318c8c8b127SEric Dumazet * truesize. 6319c8c8b127SEric Dumazet * Notes: 6320c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 6321c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 6322c8c8b127SEric Dumazet */ 6323c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 6324c8c8b127SEric Dumazet { 63253174fed9SEric Dumazet if (skb->data_len) { 63263174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 6327c8c8b127SEric Dumazet skb_cloned(skb)) 6328c8c8b127SEric Dumazet return; 6329c8c8b127SEric Dumazet 6330c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 6331c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 63323174fed9SEric Dumazet } 63333174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 63343174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 63353174fed9SEric Dumazet * their truesize. 63363174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 63373174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 63383174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 6339c8c8b127SEric Dumazet */ 6340c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 6341c8c8b127SEric Dumazet } 6342df5042f4SFlorian Westphal 6343df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 6344df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 6345df5042f4SFlorian Westphal { 6346df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 6347df5042f4SFlorian Westphal } 6348df5042f4SFlorian Westphal 63498b69a803SPaolo Abeni /** 63508b69a803SPaolo Abeni * __skb_ext_alloc - allocate a new skb extensions storage 63518b69a803SPaolo Abeni * 63524930f483SFlorian Westphal * @flags: See kmalloc(). 63534930f483SFlorian Westphal * 63548b69a803SPaolo Abeni * Returns the newly allocated pointer. The pointer can later attached to a 63558b69a803SPaolo Abeni * skb via __skb_ext_set(). 63568b69a803SPaolo Abeni * Note: caller must handle the skb_ext as an opaque data. 63578b69a803SPaolo Abeni */ 63584930f483SFlorian Westphal struct skb_ext *__skb_ext_alloc(gfp_t flags) 6359df5042f4SFlorian Westphal { 63604930f483SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, flags); 6361df5042f4SFlorian Westphal 6362df5042f4SFlorian Westphal if (new) { 6363df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 6364df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6365df5042f4SFlorian Westphal } 6366df5042f4SFlorian Westphal 6367df5042f4SFlorian Westphal return new; 6368df5042f4SFlorian Westphal } 6369df5042f4SFlorian Westphal 63704165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 63714165079bSFlorian Westphal unsigned int old_active) 6372df5042f4SFlorian Westphal { 6373df5042f4SFlorian Westphal struct skb_ext *new; 6374df5042f4SFlorian Westphal 6375df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 6376df5042f4SFlorian Westphal return old; 6377df5042f4SFlorian Westphal 6378df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 6379df5042f4SFlorian Westphal if (!new) 6380df5042f4SFlorian Westphal return NULL; 6381df5042f4SFlorian Westphal 6382df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 6383df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 6384df5042f4SFlorian Westphal 63854165079bSFlorian Westphal #ifdef CONFIG_XFRM 63864165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 63874165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 63884165079bSFlorian Westphal unsigned int i; 63894165079bSFlorian Westphal 63904165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 63914165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 63924165079bSFlorian Westphal } 63934165079bSFlorian Westphal #endif 6394df5042f4SFlorian Westphal __skb_ext_put(old); 6395df5042f4SFlorian Westphal return new; 6396df5042f4SFlorian Westphal } 6397df5042f4SFlorian Westphal 6398df5042f4SFlorian Westphal /** 63998b69a803SPaolo Abeni * __skb_ext_set - attach the specified extension storage to this skb 64008b69a803SPaolo Abeni * @skb: buffer 64018b69a803SPaolo Abeni * @id: extension id 64028b69a803SPaolo Abeni * @ext: extension storage previously allocated via __skb_ext_alloc() 64038b69a803SPaolo Abeni * 64048b69a803SPaolo Abeni * Existing extensions, if any, are cleared. 64058b69a803SPaolo Abeni * 64068b69a803SPaolo Abeni * Returns the pointer to the extension. 64078b69a803SPaolo Abeni */ 64088b69a803SPaolo Abeni void *__skb_ext_set(struct sk_buff *skb, enum skb_ext_id id, 64098b69a803SPaolo Abeni struct skb_ext *ext) 64108b69a803SPaolo Abeni { 64118b69a803SPaolo Abeni unsigned int newlen, newoff = SKB_EXT_CHUNKSIZEOF(*ext); 64128b69a803SPaolo Abeni 64138b69a803SPaolo Abeni skb_ext_put(skb); 64148b69a803SPaolo Abeni newlen = newoff + skb_ext_type_len[id]; 64158b69a803SPaolo Abeni ext->chunks = newlen; 64168b69a803SPaolo Abeni ext->offset[id] = newoff; 64178b69a803SPaolo Abeni skb->extensions = ext; 64188b69a803SPaolo Abeni skb->active_extensions = 1 << id; 64198b69a803SPaolo Abeni return skb_ext_get_ptr(ext, id); 64208b69a803SPaolo Abeni } 64218b69a803SPaolo Abeni 64228b69a803SPaolo Abeni /** 6423df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 6424df5042f4SFlorian Westphal * @skb: buffer 6425df5042f4SFlorian Westphal * @id: extension to allocate space for 6426df5042f4SFlorian Westphal * 6427df5042f4SFlorian Westphal * Allocates enough space for the given extension. 6428df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 6429df5042f4SFlorian Westphal * is returned. 6430df5042f4SFlorian Westphal * 6431df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 6432df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 6433df5042f4SFlorian Westphal * 6434df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 6435df5042f4SFlorian Westphal */ 6436df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 6437df5042f4SFlorian Westphal { 6438df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 6439df5042f4SFlorian Westphal unsigned int newlen, newoff; 6440df5042f4SFlorian Westphal 6441df5042f4SFlorian Westphal if (skb->active_extensions) { 6442df5042f4SFlorian Westphal old = skb->extensions; 6443df5042f4SFlorian Westphal 64444165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 6445df5042f4SFlorian Westphal if (!new) 6446df5042f4SFlorian Westphal return NULL; 6447df5042f4SFlorian Westphal 6448682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 6449df5042f4SFlorian Westphal goto set_active; 6450df5042f4SFlorian Westphal 6451e94e50bdSPaolo Abeni newoff = new->chunks; 6452df5042f4SFlorian Westphal } else { 6453df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 6454df5042f4SFlorian Westphal 64554930f483SFlorian Westphal new = __skb_ext_alloc(GFP_ATOMIC); 6456df5042f4SFlorian Westphal if (!new) 6457df5042f4SFlorian Westphal return NULL; 6458df5042f4SFlorian Westphal } 6459df5042f4SFlorian Westphal 6460df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 6461df5042f4SFlorian Westphal new->chunks = newlen; 6462df5042f4SFlorian Westphal new->offset[id] = newoff; 6463df5042f4SFlorian Westphal set_active: 6464b0999f38SPaolo Abeni skb->slow_gro = 1; 6465682ec859SPaolo Abeni skb->extensions = new; 6466df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 6467df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 6468df5042f4SFlorian Westphal } 6469df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 6470df5042f4SFlorian Westphal 64714165079bSFlorian Westphal #ifdef CONFIG_XFRM 64724165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 64734165079bSFlorian Westphal { 64744165079bSFlorian Westphal unsigned int i; 64754165079bSFlorian Westphal 64764165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 64774165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 64784165079bSFlorian Westphal } 64794165079bSFlorian Westphal #endif 64804165079bSFlorian Westphal 6481df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 6482df5042f4SFlorian Westphal { 6483df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 6484df5042f4SFlorian Westphal 6485df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 6486df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 6487df5042f4SFlorian Westphal skb->extensions = NULL; 6488df5042f4SFlorian Westphal __skb_ext_put(ext); 64894165079bSFlorian Westphal #ifdef CONFIG_XFRM 64904165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 64914165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 64924165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 64934165079bSFlorian Westphal 64944165079bSFlorian Westphal skb_ext_put_sp(sp); 64954165079bSFlorian Westphal sp->len = 0; 64964165079bSFlorian Westphal #endif 6497df5042f4SFlorian Westphal } 6498df5042f4SFlorian Westphal } 6499df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 6500df5042f4SFlorian Westphal 6501df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 6502df5042f4SFlorian Westphal { 6503df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 6504df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 6505df5042f4SFlorian Westphal */ 6506df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 6507df5042f4SFlorian Westphal goto free_now; 6508df5042f4SFlorian Westphal 6509df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 6510df5042f4SFlorian Westphal return; 6511df5042f4SFlorian Westphal free_now: 65124165079bSFlorian Westphal #ifdef CONFIG_XFRM 65134165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 65144165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 65154165079bSFlorian Westphal #endif 65164165079bSFlorian Westphal 6517df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 6518df5042f4SFlorian Westphal } 6519df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 6520df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 6521