11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Routines having to do with the 'struct sk_buff' memory handlers. 31da177e4SLinus Torvalds * 4113aa838SAlan Cox * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> 51da177e4SLinus Torvalds * Florian La Roche <rzsfl@rz.uni-sb.de> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Fixes: 81da177e4SLinus Torvalds * Alan Cox : Fixed the worst of the load 91da177e4SLinus Torvalds * balancer bugs. 101da177e4SLinus Torvalds * Dave Platt : Interrupt stacking fix. 111da177e4SLinus Torvalds * Richard Kooijman : Timestamp fixes. 121da177e4SLinus Torvalds * Alan Cox : Changed buffer format. 131da177e4SLinus Torvalds * Alan Cox : destructor hook for AF_UNIX etc. 141da177e4SLinus Torvalds * Linus Torvalds : Better skb_clone. 151da177e4SLinus Torvalds * Alan Cox : Added skb_copy. 161da177e4SLinus Torvalds * Alan Cox : Added all the changed routines Linus 171da177e4SLinus Torvalds * only put in the headers 181da177e4SLinus Torvalds * Ray VanTassle : Fixed --skb->lock in free 191da177e4SLinus Torvalds * Alan Cox : skb_copy copy arp field 201da177e4SLinus Torvalds * Andi Kleen : slabified it. 211da177e4SLinus Torvalds * Robert Olsson : Removed skb_head_pool 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * NOTE: 241da177e4SLinus Torvalds * The __skb_ routines should be called with interrupts 251da177e4SLinus Torvalds * disabled, or you better be *real* sure that the operation is atomic 261da177e4SLinus Torvalds * with respect to whatever list is being frobbed (e.g. via lock_sock() 271da177e4SLinus Torvalds * or via disabling bottom half handlers, etc). 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 301da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 311da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 321da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 331da177e4SLinus Torvalds */ 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /* 361da177e4SLinus Torvalds * The functions in this file will not compile correctly with gcc 2.4.x 371da177e4SLinus Torvalds */ 381da177e4SLinus Torvalds 39e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 40e005d193SJoe Perches 411da177e4SLinus Torvalds #include <linux/module.h> 421da177e4SLinus Torvalds #include <linux/types.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 441da177e4SLinus Torvalds #include <linux/mm.h> 451da177e4SLinus Torvalds #include <linux/interrupt.h> 461da177e4SLinus Torvalds #include <linux/in.h> 471da177e4SLinus Torvalds #include <linux/inet.h> 481da177e4SLinus Torvalds #include <linux/slab.h> 49de960aa9SFlorian Westphal #include <linux/tcp.h> 50de960aa9SFlorian Westphal #include <linux/udp.h> 5190017accSMarcelo Ricardo Leitner #include <linux/sctp.h> 521da177e4SLinus Torvalds #include <linux/netdevice.h> 531da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 541da177e4SLinus Torvalds #include <net/pkt_sched.h> 551da177e4SLinus Torvalds #endif 561da177e4SLinus Torvalds #include <linux/string.h> 571da177e4SLinus Torvalds #include <linux/skbuff.h> 589c55e01cSJens Axboe #include <linux/splice.h> 591da177e4SLinus Torvalds #include <linux/cache.h> 601da177e4SLinus Torvalds #include <linux/rtnetlink.h> 611da177e4SLinus Torvalds #include <linux/init.h> 62716ea3a7SDavid Howells #include <linux/scatterlist.h> 63ac45f602SPatrick Ohly #include <linux/errqueue.h> 64268bb0ceSLinus Torvalds #include <linux/prefetch.h> 650d5501c1SVlad Yasevich #include <linux/if_vlan.h> 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds #include <net/protocol.h> 681da177e4SLinus Torvalds #include <net/dst.h> 691da177e4SLinus Torvalds #include <net/sock.h> 701da177e4SLinus Torvalds #include <net/checksum.h> 71ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 721da177e4SLinus Torvalds #include <net/xfrm.h> 731da177e4SLinus Torvalds 747c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 75ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7651c56b00SEric Dumazet #include <linux/highmem.h> 77b245be1fSWillem de Bruijn #include <linux/capability.h> 78b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 79a1f8e7f7SAl Viro 8008009a76SAlexey Dobriyan struct kmem_cache *skbuff_head_cache __ro_after_init; 8108009a76SAlexey Dobriyan static struct kmem_cache *skbuff_fclone_cache __ro_after_init; 825f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 835f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /** 86f05de73bSJean Sacren * skb_panic - private function for out-of-line support 871da177e4SLinus Torvalds * @skb: buffer 881da177e4SLinus Torvalds * @sz: size 89f05de73bSJean Sacren * @addr: address 9099d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 911da177e4SLinus Torvalds * 92f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 93f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 94f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 95f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 961da177e4SLinus Torvalds */ 97f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 9899d5851eSJames Hogan const char msg[]) 991da177e4SLinus Torvalds { 100e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 10199d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1024305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 10326095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1041da177e4SLinus Torvalds BUG(); 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 107f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1081da177e4SLinus Torvalds { 109f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 112f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 113f05de73bSJean Sacren { 114f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 115f05de73bSJean Sacren } 116c93bdd0eSMel Gorman 117c93bdd0eSMel Gorman /* 118c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 119c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 120c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 121c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 122c93bdd0eSMel Gorman * memory is free 123c93bdd0eSMel Gorman */ 124c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 125c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 12661c5e88aSstephen hemminger 12761c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 12861c5e88aSstephen hemminger unsigned long ip, bool *pfmemalloc) 129c93bdd0eSMel Gorman { 130c93bdd0eSMel Gorman void *obj; 131c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 132c93bdd0eSMel Gorman 133c93bdd0eSMel Gorman /* 134c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 135c93bdd0eSMel Gorman * to the reserves, fail. 136c93bdd0eSMel Gorman */ 137c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 138c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 139c93bdd0eSMel Gorman node); 140c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 141c93bdd0eSMel Gorman goto out; 142c93bdd0eSMel Gorman 143c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 144c93bdd0eSMel Gorman ret_pfmemalloc = true; 145c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 146c93bdd0eSMel Gorman 147c93bdd0eSMel Gorman out: 148c93bdd0eSMel Gorman if (pfmemalloc) 149c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 150c93bdd0eSMel Gorman 151c93bdd0eSMel Gorman return obj; 152c93bdd0eSMel Gorman } 153c93bdd0eSMel Gorman 1541da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1551da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1561da177e4SLinus Torvalds * [BEEP] leaks. 1571da177e4SLinus Torvalds * 1581da177e4SLinus Torvalds */ 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /** 161d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1621da177e4SLinus Torvalds * @size: size to allocate 1631da177e4SLinus Torvalds * @gfp_mask: allocation mask 164c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 165c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 166c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 167c93bdd0eSMel Gorman * allocations in case the data is required for writeback 168b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1691da177e4SLinus Torvalds * 1701da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 17194b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 17294b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 1731da177e4SLinus Torvalds * 1741da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1751da177e4SLinus Torvalds * %GFP_ATOMIC. 1761da177e4SLinus Torvalds */ 177dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 178c93bdd0eSMel Gorman int flags, int node) 1791da177e4SLinus Torvalds { 180e18b890bSChristoph Lameter struct kmem_cache *cache; 1814947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1821da177e4SLinus Torvalds struct sk_buff *skb; 1831da177e4SLinus Torvalds u8 *data; 184c93bdd0eSMel Gorman bool pfmemalloc; 1851da177e4SLinus Torvalds 186c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 187c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 188c93bdd0eSMel Gorman 189c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 190c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1918798b3fbSHerbert Xu 1921da177e4SLinus Torvalds /* Get the HEAD */ 193b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1941da177e4SLinus Torvalds if (!skb) 1951da177e4SLinus Torvalds goto out; 196ec7d2f2cSEric Dumazet prefetchw(skb); 1971da177e4SLinus Torvalds 19887fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 19987fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 20087fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 20187fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 20287fb4b7bSEric Dumazet */ 203bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 20487fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 205c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2061da177e4SLinus Torvalds if (!data) 2071da177e4SLinus Torvalds goto nodata; 20887fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 20987fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 21087fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 21187fb4b7bSEric Dumazet */ 21287fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 213ec7d2f2cSEric Dumazet prefetchw(data + size); 2141da177e4SLinus Torvalds 215ca0605a7SArnaldo Carvalho de Melo /* 216c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 217c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 218c8005785SJohannes Berg * the tail pointer in struct sk_buff! 219ca0605a7SArnaldo Carvalho de Melo */ 220ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 22187fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 22287fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 223c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 22463354797SReshetova, Elena refcount_set(&skb->users, 1); 2251da177e4SLinus Torvalds skb->head = data; 2261da177e4SLinus Torvalds skb->data = data; 22727a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2284305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 22935d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 23035d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 23119633e12SStephen Hemminger 2324947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2334947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 234ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2354947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 2364947d3efSBenjamin LaHaise 237c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 238d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 2391da177e4SLinus Torvalds 240d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 241d0bf4a9eSEric Dumazet 242d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 2432638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 1); 244d179cd12SDavid S. Miller 2456ffe75ebSEric Dumazet fclones->skb2.fclone = SKB_FCLONE_CLONE; 246d179cd12SDavid S. Miller } 2471da177e4SLinus Torvalds out: 2481da177e4SLinus Torvalds return skb; 2491da177e4SLinus Torvalds nodata: 2508798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2511da177e4SLinus Torvalds skb = NULL; 2521da177e4SLinus Torvalds goto out; 2531da177e4SLinus Torvalds } 254b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /** 2572ea2f62cSEric Dumazet * __build_skb - build a network buffer 258b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2592ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 260b2b5ce9dSEric Dumazet * 261b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 262deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 2632ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 2642ea2f62cSEric Dumazet * or vmalloc() 265b2b5ce9dSEric Dumazet * The return is the new skb buffer. 266b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 267b2b5ce9dSEric Dumazet * Notes : 268b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 269b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 270b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 271b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 272b2b5ce9dSEric Dumazet * before giving packet to stack. 273b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 274b2b5ce9dSEric Dumazet */ 2752ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 276b2b5ce9dSEric Dumazet { 277b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 278b2b5ce9dSEric Dumazet struct sk_buff *skb; 279d3836f21SEric Dumazet unsigned int size = frag_size ? : ksize(data); 280b2b5ce9dSEric Dumazet 281b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 282b2b5ce9dSEric Dumazet if (!skb) 283b2b5ce9dSEric Dumazet return NULL; 284b2b5ce9dSEric Dumazet 285d3836f21SEric Dumazet size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 286b2b5ce9dSEric Dumazet 287b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 288b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 28963354797SReshetova, Elena refcount_set(&skb->users, 1); 290b2b5ce9dSEric Dumazet skb->head = data; 291b2b5ce9dSEric Dumazet skb->data = data; 292b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 293b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 29435d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 29535d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 296b2b5ce9dSEric Dumazet 297b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 298b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 299b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 300b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 301b2b5ce9dSEric Dumazet 302b2b5ce9dSEric Dumazet return skb; 303b2b5ce9dSEric Dumazet } 3042ea2f62cSEric Dumazet 3052ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3062ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3072ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3082ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3092ea2f62cSEric Dumazet */ 3102ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3112ea2f62cSEric Dumazet { 3122ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3132ea2f62cSEric Dumazet 3142ea2f62cSEric Dumazet if (skb && frag_size) { 3152ea2f62cSEric Dumazet skb->head_frag = 1; 3162f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3172ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3182ea2f62cSEric Dumazet } 3192ea2f62cSEric Dumazet return skb; 3202ea2f62cSEric Dumazet } 321b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 322b2b5ce9dSEric Dumazet 323795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE 64 324795bb1c0SJesper Dangaard Brouer 325795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache { 326795bb1c0SJesper Dangaard Brouer struct page_frag_cache page; 327e0d7924aSAlexey Dobriyan unsigned int skb_count; 328795bb1c0SJesper Dangaard Brouer void *skb_cache[NAPI_SKB_CACHE_SIZE]; 329795bb1c0SJesper Dangaard Brouer }; 330795bb1c0SJesper Dangaard Brouer 331b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 332795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 333ffde7328SAlexander Duyck 334ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 335ffde7328SAlexander Duyck { 336b63ae8caSAlexander Duyck struct page_frag_cache *nc; 337ffde7328SAlexander Duyck unsigned long flags; 338ffde7328SAlexander Duyck void *data; 339ffde7328SAlexander Duyck 340ffde7328SAlexander Duyck local_irq_save(flags); 3419451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 3428c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, fragsz, gfp_mask); 3436f532612SEric Dumazet local_irq_restore(flags); 3446f532612SEric Dumazet return data; 3456f532612SEric Dumazet } 346c93bdd0eSMel Gorman 347c93bdd0eSMel Gorman /** 348c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 349c93bdd0eSMel Gorman * @fragsz: fragment size 350c93bdd0eSMel Gorman * 351c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 352c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 353c93bdd0eSMel Gorman */ 354c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 355c93bdd0eSMel Gorman { 356453f85d4SMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC); 357c93bdd0eSMel Gorman } 3586f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3596f532612SEric Dumazet 360ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 361ffde7328SAlexander Duyck { 362795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3639451980aSAlexander Duyck 3648c2dd3e4SAlexander Duyck return page_frag_alloc(&nc->page, fragsz, gfp_mask); 365ffde7328SAlexander Duyck } 366ffde7328SAlexander Duyck 367ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 368ffde7328SAlexander Duyck { 369453f85d4SMel Gorman return __napi_alloc_frag(fragsz, GFP_ATOMIC); 370ffde7328SAlexander Duyck } 371ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 372ffde7328SAlexander Duyck 3736f532612SEric Dumazet /** 374fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 375fd11a83dSAlexander Duyck * @dev: network device to receive on 376d7499160SMasanari Iida * @len: length to allocate 377fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 378fd11a83dSAlexander Duyck * 379fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 380fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 381fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 382fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 383fd11a83dSAlexander Duyck * 384fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 385fd11a83dSAlexander Duyck */ 3869451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 3879451980aSAlexander Duyck gfp_t gfp_mask) 388fd11a83dSAlexander Duyck { 389b63ae8caSAlexander Duyck struct page_frag_cache *nc; 3909451980aSAlexander Duyck unsigned long flags; 391fd11a83dSAlexander Duyck struct sk_buff *skb; 3929451980aSAlexander Duyck bool pfmemalloc; 3939451980aSAlexander Duyck void *data; 394fd11a83dSAlexander Duyck 3959451980aSAlexander Duyck len += NET_SKB_PAD; 396fd11a83dSAlexander Duyck 3979451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 398d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 399a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 400a080e7bdSAlexander Duyck if (!skb) 401a080e7bdSAlexander Duyck goto skb_fail; 402a080e7bdSAlexander Duyck goto skb_success; 403a080e7bdSAlexander Duyck } 4049451980aSAlexander Duyck 4059451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4069451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4079451980aSAlexander Duyck 4089451980aSAlexander Duyck if (sk_memalloc_socks()) 4099451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4109451980aSAlexander Duyck 4119451980aSAlexander Duyck local_irq_save(flags); 4129451980aSAlexander Duyck 4139451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4148c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 4159451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 4169451980aSAlexander Duyck 4179451980aSAlexander Duyck local_irq_restore(flags); 4189451980aSAlexander Duyck 4199451980aSAlexander Duyck if (unlikely(!data)) 4209451980aSAlexander Duyck return NULL; 4219451980aSAlexander Duyck 4229451980aSAlexander Duyck skb = __build_skb(data, len); 4239451980aSAlexander Duyck if (unlikely(!skb)) { 424181edb2bSAlexander Duyck skb_free_frag(data); 4259451980aSAlexander Duyck return NULL; 4269451980aSAlexander Duyck } 4279451980aSAlexander Duyck 4289451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 4299451980aSAlexander Duyck if (pfmemalloc) 4309451980aSAlexander Duyck skb->pfmemalloc = 1; 4319451980aSAlexander Duyck skb->head_frag = 1; 4329451980aSAlexander Duyck 433a080e7bdSAlexander Duyck skb_success: 4348af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4357b2e497aSChristoph Hellwig skb->dev = dev; 436fd11a83dSAlexander Duyck 437a080e7bdSAlexander Duyck skb_fail: 4388af27456SChristoph Hellwig return skb; 4398af27456SChristoph Hellwig } 440b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4411da177e4SLinus Torvalds 442fd11a83dSAlexander Duyck /** 443fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 444fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 445d7499160SMasanari Iida * @len: length to allocate 446fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 447fd11a83dSAlexander Duyck * 448fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 449fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 450fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 451fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 452fd11a83dSAlexander Duyck * 453fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 454fd11a83dSAlexander Duyck */ 4559451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 4569451980aSAlexander Duyck gfp_t gfp_mask) 457fd11a83dSAlexander Duyck { 458795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 459fd11a83dSAlexander Duyck struct sk_buff *skb; 4609451980aSAlexander Duyck void *data; 461fd11a83dSAlexander Duyck 4629451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 463fd11a83dSAlexander Duyck 4649451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 465d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 466a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 467a080e7bdSAlexander Duyck if (!skb) 468a080e7bdSAlexander Duyck goto skb_fail; 469a080e7bdSAlexander Duyck goto skb_success; 470a080e7bdSAlexander Duyck } 4719451980aSAlexander Duyck 4729451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4739451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4749451980aSAlexander Duyck 4759451980aSAlexander Duyck if (sk_memalloc_socks()) 4769451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4779451980aSAlexander Duyck 4788c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 4799451980aSAlexander Duyck if (unlikely(!data)) 4809451980aSAlexander Duyck return NULL; 4819451980aSAlexander Duyck 4829451980aSAlexander Duyck skb = __build_skb(data, len); 4839451980aSAlexander Duyck if (unlikely(!skb)) { 484181edb2bSAlexander Duyck skb_free_frag(data); 4859451980aSAlexander Duyck return NULL; 4869451980aSAlexander Duyck } 4879451980aSAlexander Duyck 4889451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 489795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 4909451980aSAlexander Duyck skb->pfmemalloc = 1; 4919451980aSAlexander Duyck skb->head_frag = 1; 4929451980aSAlexander Duyck 493a080e7bdSAlexander Duyck skb_success: 494fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 495fd11a83dSAlexander Duyck skb->dev = napi->dev; 496fd11a83dSAlexander Duyck 497a080e7bdSAlexander Duyck skb_fail: 498fd11a83dSAlexander Duyck return skb; 499fd11a83dSAlexander Duyck } 500fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 501fd11a83dSAlexander Duyck 502654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 50350269e19SEric Dumazet int size, unsigned int truesize) 504654bed16SPeter Zijlstra { 505654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 506654bed16SPeter Zijlstra skb->len += size; 507654bed16SPeter Zijlstra skb->data_len += size; 50850269e19SEric Dumazet skb->truesize += truesize; 509654bed16SPeter Zijlstra } 510654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 511654bed16SPeter Zijlstra 512f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 513f8e617e1SJason Wang unsigned int truesize) 514f8e617e1SJason Wang { 515f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 516f8e617e1SJason Wang 517f8e617e1SJason Wang skb_frag_size_add(frag, size); 518f8e617e1SJason Wang skb->len += size; 519f8e617e1SJason Wang skb->data_len += size; 520f8e617e1SJason Wang skb->truesize += truesize; 521f8e617e1SJason Wang } 522f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 523f8e617e1SJason Wang 52427b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5251da177e4SLinus Torvalds { 526bd8a7036SEric Dumazet kfree_skb_list(*listp); 52727b437c8SHerbert Xu *listp = NULL; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 53027b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 53127b437c8SHerbert Xu { 53227b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 53327b437c8SHerbert Xu } 53427b437c8SHerbert Xu 5351da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5361da177e4SLinus Torvalds { 5371da177e4SLinus Torvalds struct sk_buff *list; 5381da177e4SLinus Torvalds 539fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5401da177e4SLinus Torvalds skb_get(list); 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds 543d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 544d3836f21SEric Dumazet { 545181edb2bSAlexander Duyck unsigned char *head = skb->head; 546181edb2bSAlexander Duyck 547d3836f21SEric Dumazet if (skb->head_frag) 548181edb2bSAlexander Duyck skb_free_frag(head); 549d3836f21SEric Dumazet else 550181edb2bSAlexander Duyck kfree(head); 551d3836f21SEric Dumazet } 552d3836f21SEric Dumazet 5535bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5541da177e4SLinus Torvalds { 555ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5561da177e4SLinus Torvalds int i; 557ff04a771SEric Dumazet 558ff04a771SEric Dumazet if (skb->cloned && 559ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 560ff04a771SEric Dumazet &shinfo->dataref)) 561ff04a771SEric Dumazet return; 562ff04a771SEric Dumazet 563ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 564ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 5651da177e4SLinus Torvalds 566ff04a771SEric Dumazet if (shinfo->frag_list) 567ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 5681da177e4SLinus Torvalds 5691f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 570d3836f21SEric Dumazet skb_free_head(skb); 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /* 5741da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 5751da177e4SLinus Torvalds */ 5762d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 5771da177e4SLinus Torvalds { 578d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 579d179cd12SDavid S. Miller 580d179cd12SDavid S. Miller switch (skb->fclone) { 581d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 5821da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 5836ffe75ebSEric Dumazet return; 584d179cd12SDavid S. Miller 585d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 586d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 5876ffe75ebSEric Dumazet 5886ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 5896ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 5906ffe75ebSEric Dumazet * while here, branch prediction will be good. 5916ffe75ebSEric Dumazet */ 5922638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 5936ffe75ebSEric Dumazet goto fastpath; 594d179cd12SDavid S. Miller break; 595d179cd12SDavid S. Miller 5966ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 597d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 598d179cd12SDavid S. Miller break; 5993ff50b79SStephen Hemminger } 6002638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 6016ffe75ebSEric Dumazet return; 6026ffe75ebSEric Dumazet fastpath: 6036ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds 6060a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 6071da177e4SLinus Torvalds { 608adf30907SEric Dumazet skb_dst_drop(skb); 6090a463c78SPaolo Abeni secpath_reset(skb); 6101da177e4SLinus Torvalds if (skb->destructor) { 6119c2b3328SStephen Hemminger WARN_ON(in_irq()); 6121da177e4SLinus Torvalds skb->destructor(skb); 6131da177e4SLinus Torvalds } 614a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 615cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 6162fc72c7bSKOVACS Krisztian #endif 6171109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 6181da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 6191da177e4SLinus Torvalds #endif 62004a4bb55SLennert Buytenhek } 62104a4bb55SLennert Buytenhek 62204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 62304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 62404a4bb55SLennert Buytenhek { 62504a4bb55SLennert Buytenhek skb_release_head_state(skb); 626a28b1b90SFlorian Westphal if (likely(skb->head)) 6272d4baff8SHerbert Xu skb_release_data(skb); 6282d4baff8SHerbert Xu } 6291da177e4SLinus Torvalds 6302d4baff8SHerbert Xu /** 6312d4baff8SHerbert Xu * __kfree_skb - private function 6322d4baff8SHerbert Xu * @skb: buffer 6332d4baff8SHerbert Xu * 6342d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6352d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6362d4baff8SHerbert Xu * always call kfree_skb 6372d4baff8SHerbert Xu */ 6382d4baff8SHerbert Xu 6392d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6402d4baff8SHerbert Xu { 6412d4baff8SHerbert Xu skb_release_all(skb); 6421da177e4SLinus Torvalds kfree_skbmem(skb); 6431da177e4SLinus Torvalds } 644b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /** 647231d06aeSJörn Engel * kfree_skb - free an sk_buff 648231d06aeSJörn Engel * @skb: buffer to free 649231d06aeSJörn Engel * 650231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 651231d06aeSJörn Engel * hit zero. 652231d06aeSJörn Engel */ 653231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 654231d06aeSJörn Engel { 6553889a803SPaolo Abeni if (!skb_unref(skb)) 656231d06aeSJörn Engel return; 6573889a803SPaolo Abeni 658ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 659231d06aeSJörn Engel __kfree_skb(skb); 660231d06aeSJörn Engel } 661b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 662231d06aeSJörn Engel 663bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 664bd8a7036SEric Dumazet { 665bd8a7036SEric Dumazet while (segs) { 666bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 667bd8a7036SEric Dumazet 668bd8a7036SEric Dumazet kfree_skb(segs); 669bd8a7036SEric Dumazet segs = next; 670bd8a7036SEric Dumazet } 671bd8a7036SEric Dumazet } 672bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 673bd8a7036SEric Dumazet 674d1a203eaSStephen Hemminger /** 67525121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 67625121173SMichael S. Tsirkin * @skb: buffer that triggered an error 67725121173SMichael S. Tsirkin * 67825121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 67925121173SMichael S. Tsirkin * skb must be freed afterwards. 68025121173SMichael S. Tsirkin */ 68125121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 68225121173SMichael S. Tsirkin { 6831f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 68425121173SMichael S. Tsirkin } 68525121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 68625121173SMichael S. Tsirkin 68725121173SMichael S. Tsirkin /** 688ead2ceb0SNeil Horman * consume_skb - free an skbuff 689ead2ceb0SNeil Horman * @skb: buffer to free 690ead2ceb0SNeil Horman * 691ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 692ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 693ead2ceb0SNeil Horman * is being dropped after a failure and notes that 694ead2ceb0SNeil Horman */ 695ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 696ead2ceb0SNeil Horman { 6973889a803SPaolo Abeni if (!skb_unref(skb)) 698ead2ceb0SNeil Horman return; 6993889a803SPaolo Abeni 70007dc22e7SKoki Sanagi trace_consume_skb(skb); 701ead2ceb0SNeil Horman __kfree_skb(skb); 702ead2ceb0SNeil Horman } 703ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 704ead2ceb0SNeil Horman 7050a463c78SPaolo Abeni /** 7060a463c78SPaolo Abeni * consume_stateless_skb - free an skbuff, assuming it is stateless 7070a463c78SPaolo Abeni * @skb: buffer to free 7080a463c78SPaolo Abeni * 709ca2c1418SPaolo Abeni * Alike consume_skb(), but this variant assumes that this is the last 710ca2c1418SPaolo Abeni * skb reference and all the head states have been already dropped 7110a463c78SPaolo Abeni */ 712ca2c1418SPaolo Abeni void __consume_stateless_skb(struct sk_buff *skb) 7130a463c78SPaolo Abeni { 7140a463c78SPaolo Abeni trace_consume_skb(skb); 7150a463c78SPaolo Abeni skb_release_data(skb); 7160a463c78SPaolo Abeni kfree_skbmem(skb); 7170a463c78SPaolo Abeni } 7180a463c78SPaolo Abeni 719795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void) 720795bb1c0SJesper Dangaard Brouer { 721795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 722795bb1c0SJesper Dangaard Brouer 723795bb1c0SJesper Dangaard Brouer /* flush skb_cache if containing objects */ 724795bb1c0SJesper Dangaard Brouer if (nc->skb_count) { 725795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, 726795bb1c0SJesper Dangaard Brouer nc->skb_cache); 727795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 728795bb1c0SJesper Dangaard Brouer } 729795bb1c0SJesper Dangaard Brouer } 730795bb1c0SJesper Dangaard Brouer 73115fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb) 732795bb1c0SJesper Dangaard Brouer { 733795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 734795bb1c0SJesper Dangaard Brouer 735795bb1c0SJesper Dangaard Brouer /* drop skb->head and call any destructors for packet */ 736795bb1c0SJesper Dangaard Brouer skb_release_all(skb); 737795bb1c0SJesper Dangaard Brouer 738795bb1c0SJesper Dangaard Brouer /* record skb to CPU local list */ 739795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 740795bb1c0SJesper Dangaard Brouer 741795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB 742795bb1c0SJesper Dangaard Brouer /* SLUB writes into objects when freeing */ 743795bb1c0SJesper Dangaard Brouer prefetchw(skb); 744795bb1c0SJesper Dangaard Brouer #endif 745795bb1c0SJesper Dangaard Brouer 746795bb1c0SJesper Dangaard Brouer /* flush skb_cache if it is filled */ 747795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 748795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, 749795bb1c0SJesper Dangaard Brouer nc->skb_cache); 750795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 751795bb1c0SJesper Dangaard Brouer } 752795bb1c0SJesper Dangaard Brouer } 75315fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 75415fad714SJesper Dangaard Brouer { 75515fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 75615fad714SJesper Dangaard Brouer } 757795bb1c0SJesper Dangaard Brouer 758795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 759795bb1c0SJesper Dangaard Brouer { 760795bb1c0SJesper Dangaard Brouer if (unlikely(!skb)) 761795bb1c0SJesper Dangaard Brouer return; 762795bb1c0SJesper Dangaard Brouer 763885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 764795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 765885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 766795bb1c0SJesper Dangaard Brouer return; 767795bb1c0SJesper Dangaard Brouer } 768795bb1c0SJesper Dangaard Brouer 7697608894eSPaolo Abeni if (!skb_unref(skb)) 770795bb1c0SJesper Dangaard Brouer return; 7717608894eSPaolo Abeni 772795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 773795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 774795bb1c0SJesper Dangaard Brouer 775795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 776abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 777795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 778795bb1c0SJesper Dangaard Brouer return; 779795bb1c0SJesper Dangaard Brouer } 780795bb1c0SJesper Dangaard Brouer 78115fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 782795bb1c0SJesper Dangaard Brouer } 783795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 784795bb1c0SJesper Dangaard Brouer 785b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 786b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 787b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 788b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 789b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 790b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 791b1937227SEric Dumazet 792dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 793dec18810SHerbert Xu { 794dec18810SHerbert Xu new->tstamp = old->tstamp; 795b1937227SEric Dumazet /* We do not copy old->sk */ 796dec18810SHerbert Xu new->dev = old->dev; 797b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 7987fee226aSEric Dumazet skb_dst_copy(new, old); 799def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 800dec18810SHerbert Xu new->sp = secpath_get(old->sp); 801dec18810SHerbert Xu #endif 802b1937227SEric Dumazet __nf_copy(new, old, false); 8036aa895b0SPatrick McHardy 804b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 805b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 806b1937227SEric Dumazet */ 807b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 80806021292SEliezer Tamir 809b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 810b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 811b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 812b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 813b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 814b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 815b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 816b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 817b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 818b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 819b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 820b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 821b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 822b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 823b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 824b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 825b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 826b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 827b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 828b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 829b1937227SEric Dumazet #endif 830e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 831b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 83206021292SEliezer Tamir #endif 8332bd82484SEric Dumazet #ifdef CONFIG_XPS 8342bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 8352bd82484SEric Dumazet #endif 836b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 837b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 838b1937227SEric Dumazet #endif 839b1937227SEric Dumazet 840dec18810SHerbert Xu } 841dec18810SHerbert Xu 84282c49a35SHerbert Xu /* 84382c49a35SHerbert Xu * You should not add any new code to this function. Add it to 84482c49a35SHerbert Xu * __copy_skb_header above instead. 84582c49a35SHerbert Xu */ 846e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds #define C(x) n->x = skb->x 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds n->next = n->prev = NULL; 8511da177e4SLinus Torvalds n->sk = NULL; 852dec18810SHerbert Xu __copy_skb_header(n, skb); 853dec18810SHerbert Xu 8541da177e4SLinus Torvalds C(len); 8551da177e4SLinus Torvalds C(data_len); 8563e6b3b2eSAlexey Dobriyan C(mac_len); 857334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 85802f1c89dSPaul Moore n->cloned = 1; 8591da177e4SLinus Torvalds n->nohdr = 0; 860b13dda9fSEric Dumazet n->peeked = 0; 861e78bfb07SStefano Brivio C(pfmemalloc); 8621da177e4SLinus Torvalds n->destructor = NULL; 8631da177e4SLinus Torvalds C(tail); 8641da177e4SLinus Torvalds C(end); 86502f1c89dSPaul Moore C(head); 866d3836f21SEric Dumazet C(head_frag); 86702f1c89dSPaul Moore C(data); 86802f1c89dSPaul Moore C(truesize); 86963354797SReshetova, Elena refcount_set(&n->users, 1); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 8721da177e4SLinus Torvalds skb->cloned = 1; 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds return n; 875e0053ec0SHerbert Xu #undef C 876e0053ec0SHerbert Xu } 877e0053ec0SHerbert Xu 878e0053ec0SHerbert Xu /** 879e0053ec0SHerbert Xu * skb_morph - morph one skb into another 880e0053ec0SHerbert Xu * @dst: the skb to receive the contents 881e0053ec0SHerbert Xu * @src: the skb to supply the contents 882e0053ec0SHerbert Xu * 883e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 884e0053ec0SHerbert Xu * supplied by the user. 885e0053ec0SHerbert Xu * 886e0053ec0SHerbert Xu * The target skb is returned upon exit. 887e0053ec0SHerbert Xu */ 888e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 889e0053ec0SHerbert Xu { 8902d4baff8SHerbert Xu skb_release_all(dst); 891e0053ec0SHerbert Xu return __skb_clone(dst, src); 892e0053ec0SHerbert Xu } 893e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 894e0053ec0SHerbert Xu 8956f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 896a91dbff5SWillem de Bruijn { 897a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 898a91dbff5SWillem de Bruijn struct user_struct *user; 899a91dbff5SWillem de Bruijn 900a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 901a91dbff5SWillem de Bruijn return 0; 902a91dbff5SWillem de Bruijn 903a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 904a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 905a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 906a91dbff5SWillem de Bruijn 907a91dbff5SWillem de Bruijn do { 908a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 909a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 910a91dbff5SWillem de Bruijn if (new_pg > max_pg) 911a91dbff5SWillem de Bruijn return -ENOBUFS; 912a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 913a91dbff5SWillem de Bruijn old_pg); 914a91dbff5SWillem de Bruijn 915a91dbff5SWillem de Bruijn if (!mmp->user) { 916a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 917a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 918a91dbff5SWillem de Bruijn } else { 919a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 920a91dbff5SWillem de Bruijn } 921a91dbff5SWillem de Bruijn 922a91dbff5SWillem de Bruijn return 0; 923a91dbff5SWillem de Bruijn } 9246f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 925a91dbff5SWillem de Bruijn 9266f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 927a91dbff5SWillem de Bruijn { 928a91dbff5SWillem de Bruijn if (mmp->user) { 929a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 930a91dbff5SWillem de Bruijn free_uid(mmp->user); 931a91dbff5SWillem de Bruijn } 932a91dbff5SWillem de Bruijn } 9336f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 934a91dbff5SWillem de Bruijn 93552267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size) 93652267790SWillem de Bruijn { 93752267790SWillem de Bruijn struct ubuf_info *uarg; 93852267790SWillem de Bruijn struct sk_buff *skb; 93952267790SWillem de Bruijn 94052267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 94152267790SWillem de Bruijn 94252267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 94352267790SWillem de Bruijn if (!skb) 94452267790SWillem de Bruijn return NULL; 94552267790SWillem de Bruijn 94652267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 94752267790SWillem de Bruijn uarg = (void *)skb->cb; 948a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 949a91dbff5SWillem de Bruijn 950a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 951a91dbff5SWillem de Bruijn kfree_skb(skb); 952a91dbff5SWillem de Bruijn return NULL; 953a91dbff5SWillem de Bruijn } 95452267790SWillem de Bruijn 95552267790SWillem de Bruijn uarg->callback = sock_zerocopy_callback; 9564ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 9574ab6c99dSWillem de Bruijn uarg->len = 1; 9584ab6c99dSWillem de Bruijn uarg->bytelen = size; 95952267790SWillem de Bruijn uarg->zerocopy = 1; 960c1d1b437SEric Dumazet refcount_set(&uarg->refcnt, 1); 96152267790SWillem de Bruijn sock_hold(sk); 96252267790SWillem de Bruijn 96352267790SWillem de Bruijn return uarg; 96452267790SWillem de Bruijn } 96552267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc); 96652267790SWillem de Bruijn 96752267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 96852267790SWillem de Bruijn { 96952267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 97052267790SWillem de Bruijn } 97152267790SWillem de Bruijn 9724ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size, 9734ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 9744ab6c99dSWillem de Bruijn { 9754ab6c99dSWillem de Bruijn if (uarg) { 9764ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 9774ab6c99dSWillem de Bruijn u32 bytelen, next; 9784ab6c99dSWillem de Bruijn 9794ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 9804ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 9814ab6c99dSWillem de Bruijn */ 9824ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 9834ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 9844ab6c99dSWillem de Bruijn return NULL; 9854ab6c99dSWillem de Bruijn } 9864ab6c99dSWillem de Bruijn 9874ab6c99dSWillem de Bruijn bytelen = uarg->bytelen + size; 9884ab6c99dSWillem de Bruijn if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 9894ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 9904ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 9914ab6c99dSWillem de Bruijn goto new_alloc; 9924ab6c99dSWillem de Bruijn return NULL; 9934ab6c99dSWillem de Bruijn } 9944ab6c99dSWillem de Bruijn 9954ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 9964ab6c99dSWillem de Bruijn if ((u32)(uarg->id + uarg->len) == next) { 997a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) 998a91dbff5SWillem de Bruijn return NULL; 9994ab6c99dSWillem de Bruijn uarg->len++; 10004ab6c99dSWillem de Bruijn uarg->bytelen = bytelen; 10014ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1002db5bce32SEric Dumazet sock_zerocopy_get(uarg); 10034ab6c99dSWillem de Bruijn return uarg; 10044ab6c99dSWillem de Bruijn } 10054ab6c99dSWillem de Bruijn } 10064ab6c99dSWillem de Bruijn 10074ab6c99dSWillem de Bruijn new_alloc: 10084ab6c99dSWillem de Bruijn return sock_zerocopy_alloc(sk, size); 10094ab6c99dSWillem de Bruijn } 10104ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc); 10114ab6c99dSWillem de Bruijn 10124ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 10134ab6c99dSWillem de Bruijn { 10144ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 10154ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 10164ab6c99dSWillem de Bruijn u64 sum_len; 10174ab6c99dSWillem de Bruijn 10184ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 10194ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 10204ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 10214ab6c99dSWillem de Bruijn 10224ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 10234ab6c99dSWillem de Bruijn return false; 10244ab6c99dSWillem de Bruijn 10254ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 10264ab6c99dSWillem de Bruijn return false; 10274ab6c99dSWillem de Bruijn 10284ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 10294ab6c99dSWillem de Bruijn return true; 10304ab6c99dSWillem de Bruijn } 10314ab6c99dSWillem de Bruijn 103252267790SWillem de Bruijn void sock_zerocopy_callback(struct ubuf_info *uarg, bool success) 103352267790SWillem de Bruijn { 10344ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 103552267790SWillem de Bruijn struct sock_exterr_skb *serr; 103652267790SWillem de Bruijn struct sock *sk = skb->sk; 10374ab6c99dSWillem de Bruijn struct sk_buff_head *q; 10384ab6c99dSWillem de Bruijn unsigned long flags; 10394ab6c99dSWillem de Bruijn u32 lo, hi; 10404ab6c99dSWillem de Bruijn u16 len; 104152267790SWillem de Bruijn 1042ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1043ccaffff1SWillem de Bruijn 10444ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 10454ab6c99dSWillem de Bruijn * so do not queue a completion notification 10464ab6c99dSWillem de Bruijn */ 10474ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 104852267790SWillem de Bruijn goto release; 104952267790SWillem de Bruijn 10504ab6c99dSWillem de Bruijn len = uarg->len; 10514ab6c99dSWillem de Bruijn lo = uarg->id; 10524ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 10534ab6c99dSWillem de Bruijn 105452267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 105552267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 105652267790SWillem de Bruijn serr->ee.ee_errno = 0; 105752267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 10584ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 10594ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 106052267790SWillem de Bruijn if (!success) 106152267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 106252267790SWillem de Bruijn 10634ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 10644ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 10654ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 10664ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 10674ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 10684ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 106952267790SWillem de Bruijn skb = NULL; 10704ab6c99dSWillem de Bruijn } 10714ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 107252267790SWillem de Bruijn 107352267790SWillem de Bruijn sk->sk_error_report(sk); 107452267790SWillem de Bruijn 107552267790SWillem de Bruijn release: 107652267790SWillem de Bruijn consume_skb(skb); 107752267790SWillem de Bruijn sock_put(sk); 107852267790SWillem de Bruijn } 107952267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback); 108052267790SWillem de Bruijn 108152267790SWillem de Bruijn void sock_zerocopy_put(struct ubuf_info *uarg) 108252267790SWillem de Bruijn { 1083c1d1b437SEric Dumazet if (uarg && refcount_dec_and_test(&uarg->refcnt)) { 108452267790SWillem de Bruijn if (uarg->callback) 108552267790SWillem de Bruijn uarg->callback(uarg, uarg->zerocopy); 108652267790SWillem de Bruijn else 108752267790SWillem de Bruijn consume_skb(skb_from_uarg(uarg)); 108852267790SWillem de Bruijn } 108952267790SWillem de Bruijn } 109052267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put); 109152267790SWillem de Bruijn 109252267790SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg) 109352267790SWillem de Bruijn { 109452267790SWillem de Bruijn if (uarg) { 109552267790SWillem de Bruijn struct sock *sk = skb_from_uarg(uarg)->sk; 109652267790SWillem de Bruijn 109752267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 10984ab6c99dSWillem de Bruijn uarg->len--; 109952267790SWillem de Bruijn 110052267790SWillem de Bruijn sock_zerocopy_put(uarg); 110152267790SWillem de Bruijn } 110252267790SWillem de Bruijn } 110352267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort); 110452267790SWillem de Bruijn 110552267790SWillem de Bruijn extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb, 110652267790SWillem de Bruijn struct iov_iter *from, size_t length); 110752267790SWillem de Bruijn 110852267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 110952267790SWillem de Bruijn struct msghdr *msg, int len, 111052267790SWillem de Bruijn struct ubuf_info *uarg) 111152267790SWillem de Bruijn { 11124ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 111352267790SWillem de Bruijn struct iov_iter orig_iter = msg->msg_iter; 111452267790SWillem de Bruijn int err, orig_len = skb->len; 111552267790SWillem de Bruijn 11164ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 11174ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 11184ab6c99dSWillem de Bruijn */ 11194ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 11204ab6c99dSWillem de Bruijn return -EEXIST; 11214ab6c99dSWillem de Bruijn 112252267790SWillem de Bruijn err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 112352267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 112454d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 112554d43117SWillem de Bruijn 112652267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 112752267790SWillem de Bruijn msg->msg_iter = orig_iter; 112854d43117SWillem de Bruijn skb->sk = sk; 112952267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 113054d43117SWillem de Bruijn skb->sk = save_sk; 113152267790SWillem de Bruijn return err; 113252267790SWillem de Bruijn } 113352267790SWillem de Bruijn 113452267790SWillem de Bruijn skb_zcopy_set(skb, uarg); 113552267790SWillem de Bruijn return skb->len - orig_len; 113652267790SWillem de Bruijn } 113752267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 113852267790SWillem de Bruijn 11391f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 114052267790SWillem de Bruijn gfp_t gfp_mask) 114152267790SWillem de Bruijn { 114252267790SWillem de Bruijn if (skb_zcopy(orig)) { 114352267790SWillem de Bruijn if (skb_zcopy(nskb)) { 114452267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 114552267790SWillem de Bruijn if (!gfp_mask) { 114652267790SWillem de Bruijn WARN_ON_ONCE(1); 114752267790SWillem de Bruijn return -ENOMEM; 114852267790SWillem de Bruijn } 114952267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 115052267790SWillem de Bruijn return 0; 115152267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 115252267790SWillem de Bruijn return -EIO; 115352267790SWillem de Bruijn } 115452267790SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig)); 115552267790SWillem de Bruijn } 115652267790SWillem de Bruijn return 0; 115752267790SWillem de Bruijn } 115852267790SWillem de Bruijn 11592c53040fSBen Hutchings /** 11602c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 116148c83012SMichael S. Tsirkin * @skb: the skb to modify 116248c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 116348c83012SMichael S. Tsirkin * 116448c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 116548c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 116648c83012SMichael S. Tsirkin * to userspace pages. 116748c83012SMichael S. Tsirkin * 116848c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 116948c83012SMichael S. Tsirkin * %GFP_ATOMIC. 117048c83012SMichael S. Tsirkin * 117148c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 117248c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 117348c83012SMichael S. Tsirkin */ 117448c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1175a6686f2fSShirley Ma { 1176a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1177a6686f2fSShirley Ma struct page *page, *head = NULL; 11783ece7826SWillem de Bruijn int i, new_frags; 11793ece7826SWillem de Bruijn u32 d_off; 1180a6686f2fSShirley Ma 11813ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 11823ece7826SWillem de Bruijn return -EINVAL; 11833ece7826SWillem de Bruijn 1184f72c4ac6SWillem de Bruijn if (!num_frags) 1185f72c4ac6SWillem de Bruijn goto release; 1186f72c4ac6SWillem de Bruijn 11873ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 11883ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 118902756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1190a6686f2fSShirley Ma if (!page) { 1191a6686f2fSShirley Ma while (head) { 119240dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1193a6686f2fSShirley Ma put_page(head); 1194a6686f2fSShirley Ma head = next; 1195a6686f2fSShirley Ma } 1196a6686f2fSShirley Ma return -ENOMEM; 1197a6686f2fSShirley Ma } 11983ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 11993ece7826SWillem de Bruijn head = page; 12003ece7826SWillem de Bruijn } 12013ece7826SWillem de Bruijn 12023ece7826SWillem de Bruijn page = head; 12033ece7826SWillem de Bruijn d_off = 0; 12043ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 12053ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 12063ece7826SWillem de Bruijn u32 p_off, p_len, copied; 12073ece7826SWillem de Bruijn struct page *p; 12083ece7826SWillem de Bruijn u8 *vaddr; 1209c613c209SWillem de Bruijn 1210c613c209SWillem de Bruijn skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f), 1211c613c209SWillem de Bruijn p, p_off, p_len, copied) { 12123ece7826SWillem de Bruijn u32 copy, done = 0; 1213c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 12143ece7826SWillem de Bruijn 12153ece7826SWillem de Bruijn while (done < p_len) { 12163ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 12173ece7826SWillem de Bruijn d_off = 0; 12183ece7826SWillem de Bruijn page = (struct page *)page_private(page); 12193ece7826SWillem de Bruijn } 12203ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 12213ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 12223ece7826SWillem de Bruijn vaddr + p_off + done, copy); 12233ece7826SWillem de Bruijn done += copy; 12243ece7826SWillem de Bruijn d_off += copy; 12253ece7826SWillem de Bruijn } 122651c56b00SEric Dumazet kunmap_atomic(vaddr); 1227c613c209SWillem de Bruijn } 1228a6686f2fSShirley Ma } 1229a6686f2fSShirley Ma 1230a6686f2fSShirley Ma /* skb frags release userspace buffers */ 123102756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1232a8605c60SIan Campbell skb_frag_unref(skb, i); 1233a6686f2fSShirley Ma 1234a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 12353ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 12363ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 123740dadff2SSunghan Suh head = (struct page *)page_private(head); 1238a6686f2fSShirley Ma } 12393ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 12403ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 124148c83012SMichael S. Tsirkin 1242b90ddd56SWillem de Bruijn release: 12431f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1244a6686f2fSShirley Ma return 0; 1245a6686f2fSShirley Ma } 1246dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1247a6686f2fSShirley Ma 1248e0053ec0SHerbert Xu /** 1249e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1250e0053ec0SHerbert Xu * @skb: buffer to clone 1251e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1252e0053ec0SHerbert Xu * 1253e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1254e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1255e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1256e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1257e0053ec0SHerbert Xu * 1258e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1259e0053ec0SHerbert Xu * %GFP_ATOMIC. 1260e0053ec0SHerbert Xu */ 1261e0053ec0SHerbert Xu 1262e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1263e0053ec0SHerbert Xu { 1264d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1265d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1266d0bf4a9eSEric Dumazet skb1); 12676ffe75ebSEric Dumazet struct sk_buff *n; 1268e0053ec0SHerbert Xu 126970008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1270a6686f2fSShirley Ma return NULL; 1271a6686f2fSShirley Ma 1272e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 12732638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 12746ffe75ebSEric Dumazet n = &fclones->skb2; 12752638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1276e0053ec0SHerbert Xu } else { 1277c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1278c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1279c93bdd0eSMel Gorman 1280e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1281e0053ec0SHerbert Xu if (!n) 1282e0053ec0SHerbert Xu return NULL; 1283fe55f6d5SVegard Nossum 1284e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1285e0053ec0SHerbert Xu } 1286e0053ec0SHerbert Xu 1287e0053ec0SHerbert Xu return __skb_clone(n, skb); 12881da177e4SLinus Torvalds } 1289b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 12901da177e4SLinus Torvalds 1291b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1292f5b17294SPravin B Shelar { 1293030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1294030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1295030737bcSEric Dumazet skb->csum_start += off; 1296f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1297f5b17294SPravin B Shelar skb->transport_header += off; 1298f5b17294SPravin B Shelar skb->network_header += off; 1299f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1300f5b17294SPravin B Shelar skb->mac_header += off; 1301f5b17294SPravin B Shelar skb->inner_transport_header += off; 1302f5b17294SPravin B Shelar skb->inner_network_header += off; 1303aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1304f5b17294SPravin B Shelar } 1305b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1306f5b17294SPravin B Shelar 130708303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 13081da177e4SLinus Torvalds { 1309dec18810SHerbert Xu __copy_skb_header(new, old); 1310dec18810SHerbert Xu 13117967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 13127967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 13137967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 13141da177e4SLinus Torvalds } 131508303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 13161da177e4SLinus Torvalds 1317c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1318c93bdd0eSMel Gorman { 1319c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1320c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1321c93bdd0eSMel Gorman return 0; 1322c93bdd0eSMel Gorman } 1323c93bdd0eSMel Gorman 13241da177e4SLinus Torvalds /** 13251da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 13261da177e4SLinus Torvalds * @skb: buffer to copy 13271da177e4SLinus Torvalds * @gfp_mask: allocation priority 13281da177e4SLinus Torvalds * 13291da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 13301da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 13311da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 13321da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 13331da177e4SLinus Torvalds * 13341da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 13351da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 13361da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 13371da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 13381da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 13391da177e4SLinus Torvalds */ 13401da177e4SLinus Torvalds 1341dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 13421da177e4SLinus Torvalds { 13436602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1344ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1345c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1346c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 13476602cebbSEric Dumazet 13481da177e4SLinus Torvalds if (!n) 13491da177e4SLinus Torvalds return NULL; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /* Set the data pointer */ 13521da177e4SLinus Torvalds skb_reserve(n, headerlen); 13531da177e4SLinus Torvalds /* Set the tail pointer and length */ 13541da177e4SLinus Torvalds skb_put(n, skb->len); 13551da177e4SLinus Torvalds 13569f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 13571da177e4SLinus Torvalds 135808303c18SIlya Lesokhin skb_copy_header(n, skb); 13591da177e4SLinus Torvalds return n; 13601da177e4SLinus Torvalds } 1361b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds /** 1364bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 13651da177e4SLinus Torvalds * @skb: buffer to copy 1366117632e6SEric Dumazet * @headroom: headroom of new skb 13671da177e4SLinus Torvalds * @gfp_mask: allocation priority 1368bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1369bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1370bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 13711da177e4SLinus Torvalds * 13721da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 13731da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 13741da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 13751da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 13761da177e4SLinus Torvalds * or the pointer to the buffer on success. 13771da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 13781da177e4SLinus Torvalds */ 13791da177e4SLinus Torvalds 1380bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1381bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 13821da177e4SLinus Torvalds { 1383117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1384bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1385bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 13866602cebbSEric Dumazet 13871da177e4SLinus Torvalds if (!n) 13881da177e4SLinus Torvalds goto out; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds /* Set the data pointer */ 1391117632e6SEric Dumazet skb_reserve(n, headroom); 13921da177e4SLinus Torvalds /* Set the tail pointer and length */ 13931da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 13941da177e4SLinus Torvalds /* Copy the bytes */ 1395d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 13961da177e4SLinus Torvalds 139725f484a6SHerbert Xu n->truesize += skb->data_len; 13981da177e4SLinus Torvalds n->data_len = skb->data_len; 13991da177e4SLinus Torvalds n->len = skb->len; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 14021da177e4SLinus Torvalds int i; 14031da177e4SLinus Torvalds 14041f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 14051f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 14061511022cSDan Carpenter kfree_skb(n); 14071511022cSDan Carpenter n = NULL; 1408a6686f2fSShirley Ma goto out; 1409a6686f2fSShirley Ma } 14101da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14111da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1412ea2ab693SIan Campbell skb_frag_ref(skb, i); 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 141721dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 14181da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 14191da177e4SLinus Torvalds skb_clone_fraglist(n); 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 142208303c18SIlya Lesokhin skb_copy_header(n, skb); 14231da177e4SLinus Torvalds out: 14241da177e4SLinus Torvalds return n; 14251da177e4SLinus Torvalds } 1426bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds /** 14291da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 14301da177e4SLinus Torvalds * @skb: buffer to reallocate 14311da177e4SLinus Torvalds * @nhead: room to add at head 14321da177e4SLinus Torvalds * @ntail: room to add at tail 14331da177e4SLinus Torvalds * @gfp_mask: allocation priority 14341da177e4SLinus Torvalds * 1435bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1436bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 14371da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 14381da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 14391da177e4SLinus Torvalds * 14401da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 14411da177e4SLinus Torvalds * reloaded after call to this function. 14421da177e4SLinus Torvalds */ 14431da177e4SLinus Torvalds 144486a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1445dd0fc66fSAl Viro gfp_t gfp_mask) 14461da177e4SLinus Torvalds { 1447158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1448158f323bSEric Dumazet int size = osize + nhead + ntail; 14491da177e4SLinus Torvalds long off; 1450158f323bSEric Dumazet u8 *data; 14511da177e4SLinus Torvalds 14524edd87adSHerbert Xu BUG_ON(nhead < 0); 14534edd87adSHerbert Xu 14549f77fad3STim Hansen BUG_ON(skb_shared(skb)); 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 14571da177e4SLinus Torvalds 1458c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1459c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1460c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1461c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 14621da177e4SLinus Torvalds if (!data) 14631da177e4SLinus Torvalds goto nodata; 146487151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 14676602cebbSEric Dumazet * optimized for the cases when header is void. 14686602cebbSEric Dumazet */ 14696602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 14706602cebbSEric Dumazet 14716602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 14726602cebbSEric Dumazet skb_shinfo(skb), 1473fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 14741da177e4SLinus Torvalds 14753e24591aSAlexander Duyck /* 14763e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 14773e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 14783e24591aSAlexander Duyck * be since all we did is relocate the values 14793e24591aSAlexander Duyck */ 14803e24591aSAlexander Duyck if (skb_cloned(skb)) { 148170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1482a6686f2fSShirley Ma goto nofrags; 14831f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1484c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 14851da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1486ea2ab693SIan Campbell skb_frag_ref(skb, i); 14871da177e4SLinus Torvalds 148821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 14891da177e4SLinus Torvalds skb_clone_fraglist(skb); 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds skb_release_data(skb); 14923e24591aSAlexander Duyck } else { 14933e24591aSAlexander Duyck skb_free_head(skb); 14941fd63041SEric Dumazet } 14951da177e4SLinus Torvalds off = (data + nhead) - skb->head; 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds skb->head = data; 1498d3836f21SEric Dumazet skb->head_frag = 0; 14991da177e4SLinus Torvalds skb->data += off; 15004305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 15014305b541SArnaldo Carvalho de Melo skb->end = size; 150256eb8882SPatrick McHardy off = nhead; 15034305b541SArnaldo Carvalho de Melo #else 15044305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 150556eb8882SPatrick McHardy #endif 150627a884dcSArnaldo Carvalho de Melo skb->tail += off; 1507b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 15081da177e4SLinus Torvalds skb->cloned = 0; 1509334a8132SPatrick McHardy skb->hdr_len = 0; 15101da177e4SLinus Torvalds skb->nohdr = 0; 15111da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1512158f323bSEric Dumazet 1513de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1514de8f3a83SDaniel Borkmann 1515158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1516158f323bSEric Dumazet * For the moment, we really care of rx path, or 1517158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1518158f323bSEric Dumazet */ 1519158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1520158f323bSEric Dumazet skb->truesize += size - osize; 1521158f323bSEric Dumazet 15221da177e4SLinus Torvalds return 0; 15231da177e4SLinus Torvalds 1524a6686f2fSShirley Ma nofrags: 1525a6686f2fSShirley Ma kfree(data); 15261da177e4SLinus Torvalds nodata: 15271da177e4SLinus Torvalds return -ENOMEM; 15281da177e4SLinus Torvalds } 1529b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 15341da177e4SLinus Torvalds { 15351da177e4SLinus Torvalds struct sk_buff *skb2; 15361da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds if (delta <= 0) 15391da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 15401da177e4SLinus Torvalds else { 15411da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 15421da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 15431da177e4SLinus Torvalds GFP_ATOMIC)) { 15441da177e4SLinus Torvalds kfree_skb(skb2); 15451da177e4SLinus Torvalds skb2 = NULL; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds return skb2; 15491da177e4SLinus Torvalds } 1550b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds /** 15531da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 15541da177e4SLinus Torvalds * @skb: buffer to copy 15551da177e4SLinus Torvalds * @newheadroom: new free bytes at head 15561da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 15571da177e4SLinus Torvalds * @gfp_mask: allocation priority 15581da177e4SLinus Torvalds * 15591da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 15601da177e4SLinus Torvalds * allocate additional space. 15611da177e4SLinus Torvalds * 15621da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 15631da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 15641da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 15651da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 15661da177e4SLinus Torvalds * 15671da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 15681da177e4SLinus Torvalds * is called from an interrupt. 15691da177e4SLinus Torvalds */ 15701da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 157186a76cafSVictor Fusco int newheadroom, int newtailroom, 1572dd0fc66fSAl Viro gfp_t gfp_mask) 15731da177e4SLinus Torvalds { 15741da177e4SLinus Torvalds /* 15751da177e4SLinus Torvalds * Allocate the copy buffer 15761da177e4SLinus Torvalds */ 1577c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1578c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1579c93bdd0eSMel Gorman NUMA_NO_NODE); 1580efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 15811da177e4SLinus Torvalds int head_copy_len, head_copy_off; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds if (!n) 15841da177e4SLinus Torvalds return NULL; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds skb_reserve(n, newheadroom); 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds /* Set the tail pointer and length */ 15891da177e4SLinus Torvalds skb_put(n, skb->len); 15901da177e4SLinus Torvalds 1591efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 15921da177e4SLinus Torvalds head_copy_off = 0; 15931da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 15941da177e4SLinus Torvalds head_copy_len = newheadroom; 15951da177e4SLinus Torvalds else 15961da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds /* Copy the linear header and data. */ 15999f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 16009f77fad3STim Hansen skb->len + head_copy_len)); 16011da177e4SLinus Torvalds 160208303c18SIlya Lesokhin skb_copy_header(n, skb); 16031da177e4SLinus Torvalds 1604030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1605efd1e8d5SPatrick McHardy 16061da177e4SLinus Torvalds return n; 16071da177e4SLinus Torvalds } 1608b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds /** 1611cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 16121da177e4SLinus Torvalds * @skb: buffer to pad 16131da177e4SLinus Torvalds * @pad: space to pad 1614cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 16151da177e4SLinus Torvalds * 16161da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 16171da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 16181da177e4SLinus Torvalds * beyond the buffer end onto the wire. 16191da177e4SLinus Torvalds * 1620cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1621cd0a137aSFlorian Fainelli * if @free_on_error is true. 16221da177e4SLinus Torvalds */ 16231da177e4SLinus Torvalds 1624cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 16251da177e4SLinus Torvalds { 16265b057c6bSHerbert Xu int err; 16275b057c6bSHerbert Xu int ntail; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 16305b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 16311da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 16325b057c6bSHerbert Xu return 0; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16354305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 16365b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 16375b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 16385b057c6bSHerbert Xu if (unlikely(err)) 16395b057c6bSHerbert Xu goto free_skb; 16405b057c6bSHerbert Xu } 16415b057c6bSHerbert Xu 16425b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 16435b057c6bSHerbert Xu * to be audited. 16445b057c6bSHerbert Xu */ 16455b057c6bSHerbert Xu err = skb_linearize(skb); 16465b057c6bSHerbert Xu if (unlikely(err)) 16475b057c6bSHerbert Xu goto free_skb; 16485b057c6bSHerbert Xu 16495b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 16505b057c6bSHerbert Xu return 0; 16515b057c6bSHerbert Xu 16525b057c6bSHerbert Xu free_skb: 1653cd0a137aSFlorian Fainelli if (free_on_error) 16541da177e4SLinus Torvalds kfree_skb(skb); 16555b057c6bSHerbert Xu return err; 16561da177e4SLinus Torvalds } 1657cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 16581da177e4SLinus Torvalds 16590dde3e16SIlpo Järvinen /** 16600c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 16610c7ddf36SMathias Krause * @skb: start of the buffer to use 16620c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 16630c7ddf36SMathias Krause * @len: amount of data to add 16640c7ddf36SMathias Krause * 16650c7ddf36SMathias Krause * This function extends the used data area of the potentially 16660c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 16670c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 16680c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 16690c7ddf36SMathias Krause * returned. 16700c7ddf36SMathias Krause */ 16710c7ddf36SMathias Krause 16724df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 16730c7ddf36SMathias Krause { 16740c7ddf36SMathias Krause if (tail != skb) { 16750c7ddf36SMathias Krause skb->data_len += len; 16760c7ddf36SMathias Krause skb->len += len; 16770c7ddf36SMathias Krause } 16780c7ddf36SMathias Krause return skb_put(tail, len); 16790c7ddf36SMathias Krause } 16800c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 16810c7ddf36SMathias Krause 16820c7ddf36SMathias Krause /** 16830dde3e16SIlpo Järvinen * skb_put - add data to a buffer 16840dde3e16SIlpo Järvinen * @skb: buffer to use 16850dde3e16SIlpo Järvinen * @len: amount of data to add 16860dde3e16SIlpo Järvinen * 16870dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 16880dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 16890dde3e16SIlpo Järvinen * first byte of the extra data is returned. 16900dde3e16SIlpo Järvinen */ 16914df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 16920dde3e16SIlpo Järvinen { 16934df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 16940dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 16950dde3e16SIlpo Järvinen skb->tail += len; 16960dde3e16SIlpo Järvinen skb->len += len; 16970dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 16980dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 16990dde3e16SIlpo Järvinen return tmp; 17000dde3e16SIlpo Järvinen } 17010dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 17020dde3e16SIlpo Järvinen 17036be8ac2fSIlpo Järvinen /** 1704c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1705c2aa270aSIlpo Järvinen * @skb: buffer to use 1706c2aa270aSIlpo Järvinen * @len: amount of data to add 1707c2aa270aSIlpo Järvinen * 1708c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1709c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1710c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1711c2aa270aSIlpo Järvinen */ 1712d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 1713c2aa270aSIlpo Järvinen { 1714c2aa270aSIlpo Järvinen skb->data -= len; 1715c2aa270aSIlpo Järvinen skb->len += len; 1716c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 1717c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1718c2aa270aSIlpo Järvinen return skb->data; 1719c2aa270aSIlpo Järvinen } 1720c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1721c2aa270aSIlpo Järvinen 1722c2aa270aSIlpo Järvinen /** 17236be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 17246be8ac2fSIlpo Järvinen * @skb: buffer to use 17256be8ac2fSIlpo Järvinen * @len: amount of data to remove 17266be8ac2fSIlpo Järvinen * 17276be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 17286be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 17296be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 17306be8ac2fSIlpo Järvinen * the old data. 17316be8ac2fSIlpo Järvinen */ 1732af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 17336be8ac2fSIlpo Järvinen { 173447d29646SDavid S. Miller return skb_pull_inline(skb, len); 17356be8ac2fSIlpo Järvinen } 17366be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 17376be8ac2fSIlpo Järvinen 1738419ae74eSIlpo Järvinen /** 1739419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1740419ae74eSIlpo Järvinen * @skb: buffer to alter 1741419ae74eSIlpo Järvinen * @len: new length 1742419ae74eSIlpo Järvinen * 1743419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1744419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1745419ae74eSIlpo Järvinen * The skb must be linear. 1746419ae74eSIlpo Järvinen */ 1747419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1748419ae74eSIlpo Järvinen { 1749419ae74eSIlpo Järvinen if (skb->len > len) 1750419ae74eSIlpo Järvinen __skb_trim(skb, len); 1751419ae74eSIlpo Järvinen } 1752419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1753419ae74eSIlpo Järvinen 17543cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 17551da177e4SLinus Torvalds */ 17561da177e4SLinus Torvalds 17573cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 17581da177e4SLinus Torvalds { 175927b437c8SHerbert Xu struct sk_buff **fragp; 176027b437c8SHerbert Xu struct sk_buff *frag; 17611da177e4SLinus Torvalds int offset = skb_headlen(skb); 17621da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 17631da177e4SLinus Torvalds int i; 176427b437c8SHerbert Xu int err; 176527b437c8SHerbert Xu 176627b437c8SHerbert Xu if (skb_cloned(skb) && 176727b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 176827b437c8SHerbert Xu return err; 17691da177e4SLinus Torvalds 1770f4d26fb3SHerbert Xu i = 0; 1771f4d26fb3SHerbert Xu if (offset >= len) 1772f4d26fb3SHerbert Xu goto drop_pages; 1773f4d26fb3SHerbert Xu 1774f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 17759e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 177627b437c8SHerbert Xu 177727b437c8SHerbert Xu if (end < len) { 17781da177e4SLinus Torvalds offset = end; 177927b437c8SHerbert Xu continue; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17829e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 178327b437c8SHerbert Xu 1784f4d26fb3SHerbert Xu drop_pages: 178527b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 178627b437c8SHerbert Xu 178727b437c8SHerbert Xu for (; i < nfrags; i++) 1788ea2ab693SIan Campbell skb_frag_unref(skb, i); 178927b437c8SHerbert Xu 179021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 179127b437c8SHerbert Xu skb_drop_fraglist(skb); 1792f4d26fb3SHerbert Xu goto done; 179327b437c8SHerbert Xu } 179427b437c8SHerbert Xu 179527b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 179627b437c8SHerbert Xu fragp = &frag->next) { 179727b437c8SHerbert Xu int end = offset + frag->len; 179827b437c8SHerbert Xu 179927b437c8SHerbert Xu if (skb_shared(frag)) { 180027b437c8SHerbert Xu struct sk_buff *nfrag; 180127b437c8SHerbert Xu 180227b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 180327b437c8SHerbert Xu if (unlikely(!nfrag)) 180427b437c8SHerbert Xu return -ENOMEM; 180527b437c8SHerbert Xu 180627b437c8SHerbert Xu nfrag->next = frag->next; 180785bb2a60SEric Dumazet consume_skb(frag); 180827b437c8SHerbert Xu frag = nfrag; 180927b437c8SHerbert Xu *fragp = frag; 181027b437c8SHerbert Xu } 181127b437c8SHerbert Xu 181227b437c8SHerbert Xu if (end < len) { 181327b437c8SHerbert Xu offset = end; 181427b437c8SHerbert Xu continue; 181527b437c8SHerbert Xu } 181627b437c8SHerbert Xu 181727b437c8SHerbert Xu if (end > len && 181827b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 181927b437c8SHerbert Xu return err; 182027b437c8SHerbert Xu 182127b437c8SHerbert Xu if (frag->next) 182227b437c8SHerbert Xu skb_drop_list(&frag->next); 182327b437c8SHerbert Xu break; 182427b437c8SHerbert Xu } 182527b437c8SHerbert Xu 1826f4d26fb3SHerbert Xu done: 182727b437c8SHerbert Xu if (len > skb_headlen(skb)) { 18281da177e4SLinus Torvalds skb->data_len -= skb->len - len; 18291da177e4SLinus Torvalds skb->len = len; 18301da177e4SLinus Torvalds } else { 18311da177e4SLinus Torvalds skb->len = len; 18321da177e4SLinus Torvalds skb->data_len = 0; 183327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 18341da177e4SLinus Torvalds } 18351da177e4SLinus Torvalds 1836c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1837c21b48ccSEric Dumazet skb_condense(skb); 18381da177e4SLinus Torvalds return 0; 18391da177e4SLinus Torvalds } 1840b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 18411da177e4SLinus Torvalds 184288078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 184388078d98SEric Dumazet */ 184488078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 184588078d98SEric Dumazet { 184688078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 184788078d98SEric Dumazet int delta = skb->len - len; 184888078d98SEric Dumazet 1849d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 1850d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 1851d55bef50SDimitris Michailidis len); 185288078d98SEric Dumazet } 185388078d98SEric Dumazet return __pskb_trim(skb, len); 185488078d98SEric Dumazet } 185588078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 185688078d98SEric Dumazet 18571da177e4SLinus Torvalds /** 18581da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 18591da177e4SLinus Torvalds * @skb: buffer to reallocate 18601da177e4SLinus Torvalds * @delta: number of bytes to advance tail 18611da177e4SLinus Torvalds * 18621da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 18631da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 18641da177e4SLinus Torvalds * data from fragmented part. 18651da177e4SLinus Torvalds * 18661da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 18671da177e4SLinus Torvalds * 18681da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 18691da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 18701da177e4SLinus Torvalds * 18711da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 18721da177e4SLinus Torvalds * reloaded after call to this function. 18731da177e4SLinus Torvalds */ 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 18761da177e4SLinus Torvalds * when it is necessary. 18771da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 18781da177e4SLinus Torvalds * 2. It may change skb pointers. 18791da177e4SLinus Torvalds * 18801da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 18811da177e4SLinus Torvalds */ 1882af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 18831da177e4SLinus Torvalds { 18841da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 18851da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 18861da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 18871da177e4SLinus Torvalds */ 18884305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 18911da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 18921da177e4SLinus Torvalds GFP_ATOMIC)) 18931da177e4SLinus Torvalds return NULL; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds 18969f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 18979f77fad3STim Hansen skb_tail_pointer(skb), delta)); 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 19001da177e4SLinus Torvalds * size of pulled pages. Superb. 19011da177e4SLinus Torvalds */ 190221dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 19031da177e4SLinus Torvalds goto pull_pages; 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 19061da177e4SLinus Torvalds eat = delta; 19071da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19089e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 19099e903e08SEric Dumazet 19109e903e08SEric Dumazet if (size >= eat) 19111da177e4SLinus Torvalds goto pull_pages; 19129e903e08SEric Dumazet eat -= size; 19131da177e4SLinus Torvalds } 19141da177e4SLinus Torvalds 19151da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 191609001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 19171da177e4SLinus Torvalds * but taking into account that pulling is expected to 19181da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 19191da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 19201da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 19211da177e4SLinus Torvalds */ 19221da177e4SLinus Torvalds if (eat) { 19231da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 19241da177e4SLinus Torvalds struct sk_buff *clone = NULL; 19251da177e4SLinus Torvalds struct sk_buff *insp = NULL; 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds do { 192809a62660SKris Katterjohn BUG_ON(!list); 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds if (list->len <= eat) { 19311da177e4SLinus Torvalds /* Eaten as whole. */ 19321da177e4SLinus Torvalds eat -= list->len; 19331da177e4SLinus Torvalds list = list->next; 19341da177e4SLinus Torvalds insp = list; 19351da177e4SLinus Torvalds } else { 19361da177e4SLinus Torvalds /* Eaten partially. */ 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds if (skb_shared(list)) { 19391da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 19401da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 19411da177e4SLinus Torvalds if (!clone) 19421da177e4SLinus Torvalds return NULL; 19431da177e4SLinus Torvalds insp = list->next; 19441da177e4SLinus Torvalds list = clone; 19451da177e4SLinus Torvalds } else { 19461da177e4SLinus Torvalds /* This may be pulled without 19471da177e4SLinus Torvalds * problems. */ 19481da177e4SLinus Torvalds insp = list; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 19511da177e4SLinus Torvalds kfree_skb(clone); 19521da177e4SLinus Torvalds return NULL; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds break; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds } while (eat); 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds /* Free pulled out fragments. */ 19591da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 19601da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 19611da177e4SLinus Torvalds kfree_skb(list); 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds /* And insert new clone at head. */ 19641da177e4SLinus Torvalds if (clone) { 19651da177e4SLinus Torvalds clone->next = list; 19661da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds pull_pages: 19721da177e4SLinus Torvalds eat = delta; 19731da177e4SLinus Torvalds k = 0; 19741da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19759e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 19769e903e08SEric Dumazet 19779e903e08SEric Dumazet if (size <= eat) { 1978ea2ab693SIan Campbell skb_frag_unref(skb, i); 19799e903e08SEric Dumazet eat -= size; 19801da177e4SLinus Torvalds } else { 19811da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 19821da177e4SLinus Torvalds if (eat) { 19831da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 19849e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 19853ccc6c6fSlinzhang if (!i) 19863ccc6c6fSlinzhang goto end; 19871da177e4SLinus Torvalds eat = 0; 19881da177e4SLinus Torvalds } 19891da177e4SLinus Torvalds k++; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 19931da177e4SLinus Torvalds 19943ccc6c6fSlinzhang end: 19951da177e4SLinus Torvalds skb->tail += delta; 19961da177e4SLinus Torvalds skb->data_len -= delta; 19971da177e4SLinus Torvalds 19981f8b977aSWillem de Bruijn if (!skb->data_len) 19991f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 20001f8b977aSWillem de Bruijn 200127a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 20021da177e4SLinus Torvalds } 2003b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 20041da177e4SLinus Torvalds 200522019b17SEric Dumazet /** 200622019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 200722019b17SEric Dumazet * @skb: source skb 200822019b17SEric Dumazet * @offset: offset in source 200922019b17SEric Dumazet * @to: destination buffer 201022019b17SEric Dumazet * @len: number of bytes to copy 201122019b17SEric Dumazet * 201222019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 201322019b17SEric Dumazet * destination buffer. 201422019b17SEric Dumazet * 201522019b17SEric Dumazet * CAUTION ! : 201622019b17SEric Dumazet * If its prototype is ever changed, 201722019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 201822019b17SEric Dumazet * since it is called from BPF assembly code. 201922019b17SEric Dumazet */ 20201da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 20211da177e4SLinus Torvalds { 20221a028e50SDavid S. Miller int start = skb_headlen(skb); 2023fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2024fbb398a8SDavid S. Miller int i, copy; 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds if (offset > (int)skb->len - len) 20271da177e4SLinus Torvalds goto fault; 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds /* Copy header. */ 20301a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 20311da177e4SLinus Torvalds if (copy > len) 20321da177e4SLinus Torvalds copy = len; 2033d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 20341da177e4SLinus Torvalds if ((len -= copy) == 0) 20351da177e4SLinus Torvalds return 0; 20361da177e4SLinus Torvalds offset += copy; 20371da177e4SLinus Torvalds to += copy; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20411a028e50SDavid S. Miller int end; 204251c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 20431da177e4SLinus Torvalds 2044547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20451a028e50SDavid S. Miller 204651c56b00SEric Dumazet end = start + skb_frag_size(f); 20471da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2048c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2049c613c209SWillem de Bruijn struct page *p; 20501da177e4SLinus Torvalds u8 *vaddr; 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds if (copy > len) 20531da177e4SLinus Torvalds copy = len; 20541da177e4SLinus Torvalds 2055c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2056c613c209SWillem de Bruijn f->page_offset + offset - start, 2057c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2058c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2059c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 206051c56b00SEric Dumazet kunmap_atomic(vaddr); 2061c613c209SWillem de Bruijn } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds if ((len -= copy) == 0) 20641da177e4SLinus Torvalds return 0; 20651da177e4SLinus Torvalds offset += copy; 20661da177e4SLinus Torvalds to += copy; 20671da177e4SLinus Torvalds } 20681a028e50SDavid S. Miller start = end; 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 2071fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20721a028e50SDavid S. Miller int end; 20731da177e4SLinus Torvalds 2074547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20751a028e50SDavid S. Miller 2076fbb398a8SDavid S. Miller end = start + frag_iter->len; 20771da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20781da177e4SLinus Torvalds if (copy > len) 20791da177e4SLinus Torvalds copy = len; 2080fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 20811da177e4SLinus Torvalds goto fault; 20821da177e4SLinus Torvalds if ((len -= copy) == 0) 20831da177e4SLinus Torvalds return 0; 20841da177e4SLinus Torvalds offset += copy; 20851da177e4SLinus Torvalds to += copy; 20861da177e4SLinus Torvalds } 20871a028e50SDavid S. Miller start = end; 20881da177e4SLinus Torvalds } 2089a6686f2fSShirley Ma 20901da177e4SLinus Torvalds if (!len) 20911da177e4SLinus Torvalds return 0; 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds fault: 20941da177e4SLinus Torvalds return -EFAULT; 20951da177e4SLinus Torvalds } 2096b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 20971da177e4SLinus Torvalds 20989c55e01cSJens Axboe /* 20999c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 21009c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 21019c55e01cSJens Axboe */ 21029c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 21039c55e01cSJens Axboe { 21048b9d3728SJarek Poplawski put_page(spd->pages[i]); 21058b9d3728SJarek Poplawski } 21069c55e01cSJens Axboe 2107a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 21084fb66994SJarek Poplawski unsigned int *offset, 210918aafc62SEric Dumazet struct sock *sk) 21108b9d3728SJarek Poplawski { 21115640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 21128b9d3728SJarek Poplawski 21135640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 21148b9d3728SJarek Poplawski return NULL; 21154fb66994SJarek Poplawski 21165640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 21174fb66994SJarek Poplawski 21185640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 21195640f768SEric Dumazet page_address(page) + *offset, *len); 21205640f768SEric Dumazet *offset = pfrag->offset; 21215640f768SEric Dumazet pfrag->offset += *len; 21224fb66994SJarek Poplawski 21235640f768SEric Dumazet return pfrag->page; 21249c55e01cSJens Axboe } 21259c55e01cSJens Axboe 212641c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 212741c73a0dSEric Dumazet struct page *page, 212841c73a0dSEric Dumazet unsigned int offset) 212941c73a0dSEric Dumazet { 213041c73a0dSEric Dumazet return spd->nr_pages && 213141c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 213241c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 213341c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 213441c73a0dSEric Dumazet } 213541c73a0dSEric Dumazet 21369c55e01cSJens Axboe /* 21379c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 21389c55e01cSJens Axboe */ 2139a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 214035f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 21414fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 214218aafc62SEric Dumazet bool linear, 21437a67e56fSJarek Poplawski struct sock *sk) 21449c55e01cSJens Axboe { 214541c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2146a108d5f3SDavid S. Miller return true; 21479c55e01cSJens Axboe 21488b9d3728SJarek Poplawski if (linear) { 214918aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 21508b9d3728SJarek Poplawski if (!page) 2151a108d5f3SDavid S. Miller return true; 215241c73a0dSEric Dumazet } 215341c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 215441c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2155a108d5f3SDavid S. Miller return false; 215641c73a0dSEric Dumazet } 21578b9d3728SJarek Poplawski get_page(page); 21589c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 21594fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 21609c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 21619c55e01cSJens Axboe spd->nr_pages++; 21628b9d3728SJarek Poplawski 2163a108d5f3SDavid S. Miller return false; 21649c55e01cSJens Axboe } 21659c55e01cSJens Axboe 2166a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 21672870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 216818aafc62SEric Dumazet unsigned int *len, 2169d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 217035f3d14dSJens Axboe struct sock *sk, 217135f3d14dSJens Axboe struct pipe_inode_info *pipe) 21729c55e01cSJens Axboe { 21732870c43dSOctavian Purdila if (!*len) 2174a108d5f3SDavid S. Miller return true; 21759c55e01cSJens Axboe 21762870c43dSOctavian Purdila /* skip this segment if already processed */ 21772870c43dSOctavian Purdila if (*off >= plen) { 21782870c43dSOctavian Purdila *off -= plen; 2179a108d5f3SDavid S. Miller return false; 21802870c43dSOctavian Purdila } 21812870c43dSOctavian Purdila 21822870c43dSOctavian Purdila /* ignore any bits we already processed */ 21839ca1b22dSEric Dumazet poff += *off; 21849ca1b22dSEric Dumazet plen -= *off; 21852870c43dSOctavian Purdila *off = 0; 21862870c43dSOctavian Purdila 218718aafc62SEric Dumazet do { 218818aafc62SEric Dumazet unsigned int flen = min(*len, plen); 21892870c43dSOctavian Purdila 219018aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 219118aafc62SEric Dumazet linear, sk)) 2192a108d5f3SDavid S. Miller return true; 219318aafc62SEric Dumazet poff += flen; 219418aafc62SEric Dumazet plen -= flen; 21952870c43dSOctavian Purdila *len -= flen; 219618aafc62SEric Dumazet } while (*len && plen); 21972870c43dSOctavian Purdila 2198a108d5f3SDavid S. Miller return false; 2199db43a282SOctavian Purdila } 22009c55e01cSJens Axboe 22019c55e01cSJens Axboe /* 2202a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 22032870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 22049c55e01cSJens Axboe */ 2205a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 220635f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 220735f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 22082870c43dSOctavian Purdila { 22092870c43dSOctavian Purdila int seg; 2210fa9835e5STom Herbert struct sk_buff *iter; 22119c55e01cSJens Axboe 22121d0c0b32SEric Dumazet /* map the linear part : 22132996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 22142996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 22152996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 22169c55e01cSJens Axboe */ 22172870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 22182870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 22192870c43dSOctavian Purdila skb_headlen(skb), 222018aafc62SEric Dumazet offset, len, spd, 22213a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 22221d0c0b32SEric Dumazet sk, pipe)) 2223a108d5f3SDavid S. Miller return true; 22249c55e01cSJens Axboe 22259c55e01cSJens Axboe /* 22269c55e01cSJens Axboe * then map the fragments 22279c55e01cSJens Axboe */ 22289c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 22299c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 22309c55e01cSJens Axboe 2231ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 22329e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 223318aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2234a108d5f3SDavid S. Miller return true; 22359c55e01cSJens Axboe } 22369c55e01cSJens Axboe 2237fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2238fa9835e5STom Herbert if (*offset >= iter->len) { 2239fa9835e5STom Herbert *offset -= iter->len; 2240fa9835e5STom Herbert continue; 2241fa9835e5STom Herbert } 2242fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2243fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2244fa9835e5STom Herbert * case. 2245fa9835e5STom Herbert */ 2246fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2247fa9835e5STom Herbert return true; 2248fa9835e5STom Herbert } 2249fa9835e5STom Herbert 2250a108d5f3SDavid S. Miller return false; 22519c55e01cSJens Axboe } 22529c55e01cSJens Axboe 22539c55e01cSJens Axboe /* 22549c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2255fa9835e5STom Herbert * the fragments, and the frag list. 22569c55e01cSJens Axboe */ 2257a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 22589c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 225925869262SAl Viro unsigned int flags) 22609c55e01cSJens Axboe { 226141c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 226241c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 22639c55e01cSJens Axboe struct splice_pipe_desc spd = { 22649c55e01cSJens Axboe .pages = pages, 22659c55e01cSJens Axboe .partial = partial, 2266047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 226728a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 22689c55e01cSJens Axboe .spd_release = sock_spd_release, 22699c55e01cSJens Axboe }; 227035f3d14dSJens Axboe int ret = 0; 227135f3d14dSJens Axboe 2272fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 22739c55e01cSJens Axboe 2274a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 227525869262SAl Viro ret = splice_to_pipe(pipe, &spd); 22769c55e01cSJens Axboe 227735f3d14dSJens Axboe return ret; 22789c55e01cSJens Axboe } 22792b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 22809c55e01cSJens Axboe 228120bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */ 228220bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 228320bf50deSTom Herbert int len) 228420bf50deSTom Herbert { 228520bf50deSTom Herbert unsigned int orig_len = len; 228620bf50deSTom Herbert struct sk_buff *head = skb; 228720bf50deSTom Herbert unsigned short fragidx; 228820bf50deSTom Herbert int slen, ret; 228920bf50deSTom Herbert 229020bf50deSTom Herbert do_frag_list: 229120bf50deSTom Herbert 229220bf50deSTom Herbert /* Deal with head data */ 229320bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 229420bf50deSTom Herbert struct kvec kv; 229520bf50deSTom Herbert struct msghdr msg; 229620bf50deSTom Herbert 229720bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 229820bf50deSTom Herbert kv.iov_base = skb->data + offset; 2299db5980d8SJohn Fastabend kv.iov_len = slen; 230020bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 230120bf50deSTom Herbert 230220bf50deSTom Herbert ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen); 230320bf50deSTom Herbert if (ret <= 0) 230420bf50deSTom Herbert goto error; 230520bf50deSTom Herbert 230620bf50deSTom Herbert offset += ret; 230720bf50deSTom Herbert len -= ret; 230820bf50deSTom Herbert } 230920bf50deSTom Herbert 231020bf50deSTom Herbert /* All the data was skb head? */ 231120bf50deSTom Herbert if (!len) 231220bf50deSTom Herbert goto out; 231320bf50deSTom Herbert 231420bf50deSTom Herbert /* Make offset relative to start of frags */ 231520bf50deSTom Herbert offset -= skb_headlen(skb); 231620bf50deSTom Herbert 231720bf50deSTom Herbert /* Find where we are in frag list */ 231820bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 231920bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 232020bf50deSTom Herbert 232120bf50deSTom Herbert if (offset < frag->size) 232220bf50deSTom Herbert break; 232320bf50deSTom Herbert 232420bf50deSTom Herbert offset -= frag->size; 232520bf50deSTom Herbert } 232620bf50deSTom Herbert 232720bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 232820bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 232920bf50deSTom Herbert 233020bf50deSTom Herbert slen = min_t(size_t, len, frag->size - offset); 233120bf50deSTom Herbert 233220bf50deSTom Herbert while (slen) { 233320bf50deSTom Herbert ret = kernel_sendpage_locked(sk, frag->page.p, 233420bf50deSTom Herbert frag->page_offset + offset, 233520bf50deSTom Herbert slen, MSG_DONTWAIT); 233620bf50deSTom Herbert if (ret <= 0) 233720bf50deSTom Herbert goto error; 233820bf50deSTom Herbert 233920bf50deSTom Herbert len -= ret; 234020bf50deSTom Herbert offset += ret; 234120bf50deSTom Herbert slen -= ret; 234220bf50deSTom Herbert } 234320bf50deSTom Herbert 234420bf50deSTom Herbert offset = 0; 234520bf50deSTom Herbert } 234620bf50deSTom Herbert 234720bf50deSTom Herbert if (len) { 234820bf50deSTom Herbert /* Process any frag lists */ 234920bf50deSTom Herbert 235020bf50deSTom Herbert if (skb == head) { 235120bf50deSTom Herbert if (skb_has_frag_list(skb)) { 235220bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 235320bf50deSTom Herbert goto do_frag_list; 235420bf50deSTom Herbert } 235520bf50deSTom Herbert } else if (skb->next) { 235620bf50deSTom Herbert skb = skb->next; 235720bf50deSTom Herbert goto do_frag_list; 235820bf50deSTom Herbert } 235920bf50deSTom Herbert } 236020bf50deSTom Herbert 236120bf50deSTom Herbert out: 236220bf50deSTom Herbert return orig_len - len; 236320bf50deSTom Herbert 236420bf50deSTom Herbert error: 236520bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 236620bf50deSTom Herbert } 236720bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 236820bf50deSTom Herbert 236920bf50deSTom Herbert /* Send skb data on a socket. */ 237020bf50deSTom Herbert int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 237120bf50deSTom Herbert { 237220bf50deSTom Herbert int ret = 0; 237320bf50deSTom Herbert 237420bf50deSTom Herbert lock_sock(sk); 237520bf50deSTom Herbert ret = skb_send_sock_locked(sk, skb, offset, len); 237620bf50deSTom Herbert release_sock(sk); 237720bf50deSTom Herbert 237820bf50deSTom Herbert return ret; 237920bf50deSTom Herbert } 238020bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock); 238120bf50deSTom Herbert 2382357b40a1SHerbert Xu /** 2383357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2384357b40a1SHerbert Xu * @skb: destination buffer 2385357b40a1SHerbert Xu * @offset: offset in destination 2386357b40a1SHerbert Xu * @from: source buffer 2387357b40a1SHerbert Xu * @len: number of bytes to copy 2388357b40a1SHerbert Xu * 2389357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2390357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2391357b40a1SHerbert Xu * traversing fragment lists and such. 2392357b40a1SHerbert Xu */ 2393357b40a1SHerbert Xu 23940c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2395357b40a1SHerbert Xu { 23961a028e50SDavid S. Miller int start = skb_headlen(skb); 2397fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2398fbb398a8SDavid S. Miller int i, copy; 2399357b40a1SHerbert Xu 2400357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2401357b40a1SHerbert Xu goto fault; 2402357b40a1SHerbert Xu 24031a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2404357b40a1SHerbert Xu if (copy > len) 2405357b40a1SHerbert Xu copy = len; 240627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2407357b40a1SHerbert Xu if ((len -= copy) == 0) 2408357b40a1SHerbert Xu return 0; 2409357b40a1SHerbert Xu offset += copy; 2410357b40a1SHerbert Xu from += copy; 2411357b40a1SHerbert Xu } 2412357b40a1SHerbert Xu 2413357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2414357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 24151a028e50SDavid S. Miller int end; 2416357b40a1SHerbert Xu 2417547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24181a028e50SDavid S. Miller 24199e903e08SEric Dumazet end = start + skb_frag_size(frag); 2420357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2421c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2422c613c209SWillem de Bruijn struct page *p; 2423357b40a1SHerbert Xu u8 *vaddr; 2424357b40a1SHerbert Xu 2425357b40a1SHerbert Xu if (copy > len) 2426357b40a1SHerbert Xu copy = len; 2427357b40a1SHerbert Xu 2428c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2429c613c209SWillem de Bruijn frag->page_offset + offset - start, 2430c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2431c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2432c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 243351c56b00SEric Dumazet kunmap_atomic(vaddr); 2434c613c209SWillem de Bruijn } 2435357b40a1SHerbert Xu 2436357b40a1SHerbert Xu if ((len -= copy) == 0) 2437357b40a1SHerbert Xu return 0; 2438357b40a1SHerbert Xu offset += copy; 2439357b40a1SHerbert Xu from += copy; 2440357b40a1SHerbert Xu } 24411a028e50SDavid S. Miller start = end; 2442357b40a1SHerbert Xu } 2443357b40a1SHerbert Xu 2444fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 24451a028e50SDavid S. Miller int end; 2446357b40a1SHerbert Xu 2447547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24481a028e50SDavid S. Miller 2449fbb398a8SDavid S. Miller end = start + frag_iter->len; 2450357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2451357b40a1SHerbert Xu if (copy > len) 2452357b40a1SHerbert Xu copy = len; 2453fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 24541a028e50SDavid S. Miller from, copy)) 2455357b40a1SHerbert Xu goto fault; 2456357b40a1SHerbert Xu if ((len -= copy) == 0) 2457357b40a1SHerbert Xu return 0; 2458357b40a1SHerbert Xu offset += copy; 2459357b40a1SHerbert Xu from += copy; 2460357b40a1SHerbert Xu } 24611a028e50SDavid S. Miller start = end; 2462357b40a1SHerbert Xu } 2463357b40a1SHerbert Xu if (!len) 2464357b40a1SHerbert Xu return 0; 2465357b40a1SHerbert Xu 2466357b40a1SHerbert Xu fault: 2467357b40a1SHerbert Xu return -EFAULT; 2468357b40a1SHerbert Xu } 2469357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2470357b40a1SHerbert Xu 24711da177e4SLinus Torvalds /* Checksum skb data. */ 24722817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 24732817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 24741da177e4SLinus Torvalds { 24751a028e50SDavid S. Miller int start = skb_headlen(skb); 24761a028e50SDavid S. Miller int i, copy = start - offset; 2477fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 24781da177e4SLinus Torvalds int pos = 0; 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds /* Checksum header. */ 24811da177e4SLinus Torvalds if (copy > 0) { 24821da177e4SLinus Torvalds if (copy > len) 24831da177e4SLinus Torvalds copy = len; 24842817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 24851da177e4SLinus Torvalds if ((len -= copy) == 0) 24861da177e4SLinus Torvalds return csum; 24871da177e4SLinus Torvalds offset += copy; 24881da177e4SLinus Torvalds pos = copy; 24891da177e4SLinus Torvalds } 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 24921a028e50SDavid S. Miller int end; 249351c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 24941da177e4SLinus Torvalds 2495547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24961a028e50SDavid S. Miller 249751c56b00SEric Dumazet end = start + skb_frag_size(frag); 24981da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2499c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2500c613c209SWillem de Bruijn struct page *p; 250144bb9363SAl Viro __wsum csum2; 25021da177e4SLinus Torvalds u8 *vaddr; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds if (copy > len) 25051da177e4SLinus Torvalds copy = len; 2506c613c209SWillem de Bruijn 2507c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2508c613c209SWillem de Bruijn frag->page_offset + offset - start, 2509c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2510c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2511c613c209SWillem de Bruijn csum2 = ops->update(vaddr + p_off, p_len, 0); 251251c56b00SEric Dumazet kunmap_atomic(vaddr); 2513c613c209SWillem de Bruijn csum = ops->combine(csum, csum2, pos, p_len); 2514c613c209SWillem de Bruijn pos += p_len; 2515c613c209SWillem de Bruijn } 2516c613c209SWillem de Bruijn 25171da177e4SLinus Torvalds if (!(len -= copy)) 25181da177e4SLinus Torvalds return csum; 25191da177e4SLinus Torvalds offset += copy; 25201da177e4SLinus Torvalds } 25211a028e50SDavid S. Miller start = end; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 2524fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 25251a028e50SDavid S. Miller int end; 25261da177e4SLinus Torvalds 2527547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25281a028e50SDavid S. Miller 2529fbb398a8SDavid S. Miller end = start + frag_iter->len; 25301da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 25315f92a738SAl Viro __wsum csum2; 25321da177e4SLinus Torvalds if (copy > len) 25331da177e4SLinus Torvalds copy = len; 25342817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 25352817a336SDaniel Borkmann copy, 0, ops); 25362817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 25371da177e4SLinus Torvalds if ((len -= copy) == 0) 25381da177e4SLinus Torvalds return csum; 25391da177e4SLinus Torvalds offset += copy; 25401da177e4SLinus Torvalds pos += copy; 25411da177e4SLinus Torvalds } 25421a028e50SDavid S. Miller start = end; 25431da177e4SLinus Torvalds } 254409a62660SKris Katterjohn BUG_ON(len); 25451da177e4SLinus Torvalds 25461da177e4SLinus Torvalds return csum; 25471da177e4SLinus Torvalds } 25482817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 25492817a336SDaniel Borkmann 25502817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 25512817a336SDaniel Borkmann int len, __wsum csum) 25522817a336SDaniel Borkmann { 25532817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2554cea80ea8SDaniel Borkmann .update = csum_partial_ext, 25552817a336SDaniel Borkmann .combine = csum_block_add_ext, 25562817a336SDaniel Borkmann }; 25572817a336SDaniel Borkmann 25582817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 25592817a336SDaniel Borkmann } 2560b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 25611da177e4SLinus Torvalds 25621da177e4SLinus Torvalds /* Both of above in one bottle. */ 25631da177e4SLinus Torvalds 256481d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 256581d77662SAl Viro u8 *to, int len, __wsum csum) 25661da177e4SLinus Torvalds { 25671a028e50SDavid S. Miller int start = skb_headlen(skb); 25681a028e50SDavid S. Miller int i, copy = start - offset; 2569fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 25701da177e4SLinus Torvalds int pos = 0; 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds /* Copy header. */ 25731da177e4SLinus Torvalds if (copy > 0) { 25741da177e4SLinus Torvalds if (copy > len) 25751da177e4SLinus Torvalds copy = len; 25761da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 25771da177e4SLinus Torvalds copy, csum); 25781da177e4SLinus Torvalds if ((len -= copy) == 0) 25791da177e4SLinus Torvalds return csum; 25801da177e4SLinus Torvalds offset += copy; 25811da177e4SLinus Torvalds to += copy; 25821da177e4SLinus Torvalds pos = copy; 25831da177e4SLinus Torvalds } 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 25861a028e50SDavid S. Miller int end; 25871da177e4SLinus Torvalds 2588547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25891a028e50SDavid S. Miller 25909e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 25911da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2592c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2593c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2594c613c209SWillem de Bruijn struct page *p; 25955084205fSAl Viro __wsum csum2; 25961da177e4SLinus Torvalds u8 *vaddr; 25971da177e4SLinus Torvalds 25981da177e4SLinus Torvalds if (copy > len) 25991da177e4SLinus Torvalds copy = len; 2600c613c209SWillem de Bruijn 2601c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2602c613c209SWillem de Bruijn frag->page_offset + offset - start, 2603c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2604c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2605c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2606c613c209SWillem de Bruijn to + copied, 2607c613c209SWillem de Bruijn p_len, 0); 260851c56b00SEric Dumazet kunmap_atomic(vaddr); 26091da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 2610c613c209SWillem de Bruijn pos += p_len; 2611c613c209SWillem de Bruijn } 2612c613c209SWillem de Bruijn 26131da177e4SLinus Torvalds if (!(len -= copy)) 26141da177e4SLinus Torvalds return csum; 26151da177e4SLinus Torvalds offset += copy; 26161da177e4SLinus Torvalds to += copy; 26171da177e4SLinus Torvalds } 26181a028e50SDavid S. Miller start = end; 26191da177e4SLinus Torvalds } 26201da177e4SLinus Torvalds 2621fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 262281d77662SAl Viro __wsum csum2; 26231a028e50SDavid S. Miller int end; 26241da177e4SLinus Torvalds 2625547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26261a028e50SDavid S. Miller 2627fbb398a8SDavid S. Miller end = start + frag_iter->len; 26281da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 26291da177e4SLinus Torvalds if (copy > len) 26301da177e4SLinus Torvalds copy = len; 2631fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 26321a028e50SDavid S. Miller offset - start, 26331da177e4SLinus Torvalds to, copy, 0); 26341da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 26351da177e4SLinus Torvalds if ((len -= copy) == 0) 26361da177e4SLinus Torvalds return csum; 26371da177e4SLinus Torvalds offset += copy; 26381da177e4SLinus Torvalds to += copy; 26391da177e4SLinus Torvalds pos += copy; 26401da177e4SLinus Torvalds } 26411a028e50SDavid S. Miller start = end; 26421da177e4SLinus Torvalds } 264309a62660SKris Katterjohn BUG_ON(len); 26441da177e4SLinus Torvalds return csum; 26451da177e4SLinus Torvalds } 2646b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 26471da177e4SLinus Torvalds 26489617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 26499617813dSDavide Caratti { 26509617813dSDavide Caratti net_warn_ratelimited( 26519617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 26529617813dSDavide Caratti __func__); 26539617813dSDavide Caratti return 0; 26549617813dSDavide Caratti } 26559617813dSDavide Caratti 26569617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 26579617813dSDavide Caratti int offset, int len) 26589617813dSDavide Caratti { 26599617813dSDavide Caratti net_warn_ratelimited( 26609617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 26619617813dSDavide Caratti __func__); 26629617813dSDavide Caratti return 0; 26639617813dSDavide Caratti } 26649617813dSDavide Caratti 26659617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 26669617813dSDavide Caratti .update = warn_crc32c_csum_update, 26679617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 26689617813dSDavide Caratti }; 26699617813dSDavide Caratti 26709617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 26719617813dSDavide Caratti &default_crc32c_ops; 26729617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 26739617813dSDavide Caratti 2674af2806f8SThomas Graf /** 2675af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2676af2806f8SThomas Graf * @from: source buffer 2677af2806f8SThomas Graf * 2678af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2679af2806f8SThomas Graf * into skb_zerocopy(). 2680af2806f8SThomas Graf */ 2681af2806f8SThomas Graf unsigned int 2682af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2683af2806f8SThomas Graf { 2684af2806f8SThomas Graf unsigned int hlen = 0; 2685af2806f8SThomas Graf 2686af2806f8SThomas Graf if (!from->head_frag || 2687af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2688af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2689af2806f8SThomas Graf hlen = skb_headlen(from); 2690af2806f8SThomas Graf 2691af2806f8SThomas Graf if (skb_has_frag_list(from)) 2692af2806f8SThomas Graf hlen = from->len; 2693af2806f8SThomas Graf 2694af2806f8SThomas Graf return hlen; 2695af2806f8SThomas Graf } 2696af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2697af2806f8SThomas Graf 2698af2806f8SThomas Graf /** 2699af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2700af2806f8SThomas Graf * @to: destination buffer 27017fceb4deSMasanari Iida * @from: source buffer 2702af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2703af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2704af2806f8SThomas Graf * 2705af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2706af2806f8SThomas Graf * to the frags in the source buffer. 2707af2806f8SThomas Graf * 2708af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2709af2806f8SThomas Graf * headroom in the `to` buffer. 271036d5fe6aSZoltan Kiss * 271136d5fe6aSZoltan Kiss * Return value: 271236d5fe6aSZoltan Kiss * 0: everything is OK 271336d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 271436d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2715af2806f8SThomas Graf */ 271636d5fe6aSZoltan Kiss int 271736d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2718af2806f8SThomas Graf { 2719af2806f8SThomas Graf int i, j = 0; 2720af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 272136d5fe6aSZoltan Kiss int ret; 2722af2806f8SThomas Graf struct page *page; 2723af2806f8SThomas Graf unsigned int offset; 2724af2806f8SThomas Graf 2725af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2726af2806f8SThomas Graf 2727af2806f8SThomas Graf /* dont bother with small payloads */ 272836d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 272936d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2730af2806f8SThomas Graf 2731af2806f8SThomas Graf if (hlen) { 273236d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 273336d5fe6aSZoltan Kiss if (unlikely(ret)) 273436d5fe6aSZoltan Kiss return ret; 2735af2806f8SThomas Graf len -= hlen; 2736af2806f8SThomas Graf } else { 2737af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2738af2806f8SThomas Graf if (plen) { 2739af2806f8SThomas Graf page = virt_to_head_page(from->head); 2740af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2741af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2742af2806f8SThomas Graf get_page(page); 2743af2806f8SThomas Graf j = 1; 2744af2806f8SThomas Graf len -= plen; 2745af2806f8SThomas Graf } 2746af2806f8SThomas Graf } 2747af2806f8SThomas Graf 2748af2806f8SThomas Graf to->truesize += len + plen; 2749af2806f8SThomas Graf to->len += len + plen; 2750af2806f8SThomas Graf to->data_len += len + plen; 2751af2806f8SThomas Graf 275236d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 275336d5fe6aSZoltan Kiss skb_tx_error(from); 275436d5fe6aSZoltan Kiss return -ENOMEM; 275536d5fe6aSZoltan Kiss } 27561f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 275736d5fe6aSZoltan Kiss 2758af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2759af2806f8SThomas Graf if (!len) 2760af2806f8SThomas Graf break; 2761af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2762af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2763af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2764af2806f8SThomas Graf skb_frag_ref(to, j); 2765af2806f8SThomas Graf j++; 2766af2806f8SThomas Graf } 2767af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 276836d5fe6aSZoltan Kiss 276936d5fe6aSZoltan Kiss return 0; 2770af2806f8SThomas Graf } 2771af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2772af2806f8SThomas Graf 27731da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 27741da177e4SLinus Torvalds { 2775d3bc23e7SAl Viro __wsum csum; 27761da177e4SLinus Torvalds long csstart; 27771da177e4SLinus Torvalds 277884fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 277955508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 27801da177e4SLinus Torvalds else 27811da177e4SLinus Torvalds csstart = skb_headlen(skb); 27821da177e4SLinus Torvalds 278309a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 27841da177e4SLinus Torvalds 2785d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds csum = 0; 27881da177e4SLinus Torvalds if (csstart != skb->len) 27891da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 27901da177e4SLinus Torvalds skb->len - csstart, 0); 27911da177e4SLinus Torvalds 279284fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2793ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 27941da177e4SLinus Torvalds 2795d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds } 2798b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds /** 28011da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 28021da177e4SLinus Torvalds * @list: list to dequeue from 28031da177e4SLinus Torvalds * 28041da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 28051da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 28061da177e4SLinus Torvalds * returned or %NULL if the list is empty. 28071da177e4SLinus Torvalds */ 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 28101da177e4SLinus Torvalds { 28111da177e4SLinus Torvalds unsigned long flags; 28121da177e4SLinus Torvalds struct sk_buff *result; 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28151da177e4SLinus Torvalds result = __skb_dequeue(list); 28161da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 28171da177e4SLinus Torvalds return result; 28181da177e4SLinus Torvalds } 2819b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds /** 28221da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 28231da177e4SLinus Torvalds * @list: list to dequeue from 28241da177e4SLinus Torvalds * 28251da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 28261da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 28271da177e4SLinus Torvalds * returned or %NULL if the list is empty. 28281da177e4SLinus Torvalds */ 28291da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 28301da177e4SLinus Torvalds { 28311da177e4SLinus Torvalds unsigned long flags; 28321da177e4SLinus Torvalds struct sk_buff *result; 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28351da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 28361da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 28371da177e4SLinus Torvalds return result; 28381da177e4SLinus Torvalds } 2839b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 28401da177e4SLinus Torvalds 28411da177e4SLinus Torvalds /** 28421da177e4SLinus Torvalds * skb_queue_purge - empty a list 28431da177e4SLinus Torvalds * @list: list to empty 28441da177e4SLinus Torvalds * 28451da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 28461da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 28471da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 28481da177e4SLinus Torvalds */ 28491da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 28501da177e4SLinus Torvalds { 28511da177e4SLinus Torvalds struct sk_buff *skb; 28521da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 28531da177e4SLinus Torvalds kfree_skb(skb); 28541da177e4SLinus Torvalds } 2855b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 28561da177e4SLinus Torvalds 28571da177e4SLinus Torvalds /** 28589f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 28599f5afeaeSYaogong Wang * @root: root of the rbtree to empty 2860385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 28619f5afeaeSYaogong Wang * 28629f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 28639f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 28649f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 28659f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 28669f5afeaeSYaogong Wang */ 2867385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 28689f5afeaeSYaogong Wang { 28697c90584cSEric Dumazet struct rb_node *p = rb_first(root); 2870385114deSPeter Oskolkov unsigned int sum = 0; 28719f5afeaeSYaogong Wang 28727c90584cSEric Dumazet while (p) { 28737c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 28747c90584cSEric Dumazet 28757c90584cSEric Dumazet p = rb_next(p); 28767c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 2877385114deSPeter Oskolkov sum += skb->truesize; 28789f5afeaeSYaogong Wang kfree_skb(skb); 28797c90584cSEric Dumazet } 2880385114deSPeter Oskolkov return sum; 28819f5afeaeSYaogong Wang } 28829f5afeaeSYaogong Wang 28839f5afeaeSYaogong Wang /** 28841da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 28851da177e4SLinus Torvalds * @list: list to use 28861da177e4SLinus Torvalds * @newsk: buffer to queue 28871da177e4SLinus Torvalds * 28881da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 28891da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 28901da177e4SLinus Torvalds * safely. 28911da177e4SLinus Torvalds * 28921da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 28931da177e4SLinus Torvalds */ 28941da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 28951da177e4SLinus Torvalds { 28961da177e4SLinus Torvalds unsigned long flags; 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28991da177e4SLinus Torvalds __skb_queue_head(list, newsk); 29001da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29011da177e4SLinus Torvalds } 2902b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds /** 29051da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 29061da177e4SLinus Torvalds * @list: list to use 29071da177e4SLinus Torvalds * @newsk: buffer to queue 29081da177e4SLinus Torvalds * 29091da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 29101da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 29111da177e4SLinus Torvalds * safely. 29121da177e4SLinus Torvalds * 29131da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 29141da177e4SLinus Torvalds */ 29151da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 29161da177e4SLinus Torvalds { 29171da177e4SLinus Torvalds unsigned long flags; 29181da177e4SLinus Torvalds 29191da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 29201da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 29211da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29221da177e4SLinus Torvalds } 2923b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 29248728b834SDavid S. Miller 29251da177e4SLinus Torvalds /** 29261da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 29271da177e4SLinus Torvalds * @skb: buffer to remove 29288728b834SDavid S. Miller * @list: list to use 29291da177e4SLinus Torvalds * 29308728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 29318728b834SDavid S. Miller * function is atomic with respect to other list locked calls 29321da177e4SLinus Torvalds * 29338728b834SDavid S. Miller * You must know what list the SKB is on. 29341da177e4SLinus Torvalds */ 29358728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 29361da177e4SLinus Torvalds { 29371da177e4SLinus Torvalds unsigned long flags; 29381da177e4SLinus Torvalds 29391da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 29408728b834SDavid S. Miller __skb_unlink(skb, list); 29411da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29421da177e4SLinus Torvalds } 2943b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 29441da177e4SLinus Torvalds 29451da177e4SLinus Torvalds /** 29461da177e4SLinus Torvalds * skb_append - append a buffer 29471da177e4SLinus Torvalds * @old: buffer to insert after 29481da177e4SLinus Torvalds * @newsk: buffer to insert 29498728b834SDavid S. Miller * @list: list to use 29501da177e4SLinus Torvalds * 29511da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 29521da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 29531da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 29541da177e4SLinus Torvalds */ 29558728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 29561da177e4SLinus Torvalds { 29571da177e4SLinus Torvalds unsigned long flags; 29581da177e4SLinus Torvalds 29598728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 29607de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 29618728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 29621da177e4SLinus Torvalds } 2963b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 29641da177e4SLinus Torvalds 29651da177e4SLinus Torvalds /** 29661da177e4SLinus Torvalds * skb_insert - insert a buffer 29671da177e4SLinus Torvalds * @old: buffer to insert before 29681da177e4SLinus Torvalds * @newsk: buffer to insert 29698728b834SDavid S. Miller * @list: list to use 29701da177e4SLinus Torvalds * 29718728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 29728728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 29738728b834SDavid S. Miller * calls. 29748728b834SDavid S. Miller * 29751da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 29761da177e4SLinus Torvalds */ 29778728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 29781da177e4SLinus Torvalds { 29791da177e4SLinus Torvalds unsigned long flags; 29801da177e4SLinus Torvalds 29818728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 29828728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 29838728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 29841da177e4SLinus Torvalds } 2985b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 29881da177e4SLinus Torvalds struct sk_buff* skb1, 29891da177e4SLinus Torvalds const u32 len, const int pos) 29901da177e4SLinus Torvalds { 29911da177e4SLinus Torvalds int i; 29921da177e4SLinus Torvalds 2993d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2994d626f62bSArnaldo Carvalho de Melo pos - len); 29951da177e4SLinus Torvalds /* And move data appendix as is. */ 29961da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 29971da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 30001da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 30011da177e4SLinus Torvalds skb1->data_len = skb->data_len; 30021da177e4SLinus Torvalds skb1->len += skb1->data_len; 30031da177e4SLinus Torvalds skb->data_len = 0; 30041da177e4SLinus Torvalds skb->len = len; 300527a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 30061da177e4SLinus Torvalds } 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 30091da177e4SLinus Torvalds struct sk_buff* skb1, 30101da177e4SLinus Torvalds const u32 len, int pos) 30111da177e4SLinus Torvalds { 30121da177e4SLinus Torvalds int i, k = 0; 30131da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 30141da177e4SLinus Torvalds 30151da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 30161da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 30171da177e4SLinus Torvalds skb->len = len; 30181da177e4SLinus Torvalds skb->data_len = len - pos; 30191da177e4SLinus Torvalds 30201da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 30219e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 30221da177e4SLinus Torvalds 30231da177e4SLinus Torvalds if (pos + size > len) { 30241da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 30251da177e4SLinus Torvalds 30261da177e4SLinus Torvalds if (pos < len) { 30271da177e4SLinus Torvalds /* Split frag. 30281da177e4SLinus Torvalds * We have two variants in this case: 30291da177e4SLinus Torvalds * 1. Move all the frag to the second 30301da177e4SLinus Torvalds * part, if it is possible. F.e. 30311da177e4SLinus Torvalds * this approach is mandatory for TUX, 30321da177e4SLinus Torvalds * where splitting is expensive. 30331da177e4SLinus Torvalds * 2. Split is accurately. We make this. 30341da177e4SLinus Torvalds */ 3035ea2ab693SIan Campbell skb_frag_ref(skb, i); 30361da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 30379e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 30389e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 30391da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 30401da177e4SLinus Torvalds } 30411da177e4SLinus Torvalds k++; 30421da177e4SLinus Torvalds } else 30431da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 30441da177e4SLinus Torvalds pos += size; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 30471da177e4SLinus Torvalds } 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds /** 30501da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 30511da177e4SLinus Torvalds * @skb: the buffer to split 30521da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 30531da177e4SLinus Torvalds * @len: new length for skb 30541da177e4SLinus Torvalds */ 30551da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 30561da177e4SLinus Torvalds { 30571da177e4SLinus Torvalds int pos = skb_headlen(skb); 30581da177e4SLinus Torvalds 3059fff88030SWillem de Bruijn skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & 3060fff88030SWillem de Bruijn SKBTX_SHARED_FRAG; 30611f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 30621da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 30631da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 30641da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 30651da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 30661da177e4SLinus Torvalds } 3067b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 30681da177e4SLinus Torvalds 30699f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 30709f782db3SIlpo Järvinen * 30719f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 30729f782db3SIlpo Järvinen */ 3073832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3074832d11c5SIlpo Järvinen { 30750ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3076832d11c5SIlpo Järvinen } 3077832d11c5SIlpo Järvinen 3078832d11c5SIlpo Järvinen /** 3079832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3080832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3081832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3082832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3083832d11c5SIlpo Järvinen * 3084832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 308520e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3086832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3087832d11c5SIlpo Järvinen * 3088832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3089832d11c5SIlpo Järvinen * 3090832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3091832d11c5SIlpo Järvinen * to have non-paged data as well. 3092832d11c5SIlpo Järvinen * 3093832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3094832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3095832d11c5SIlpo Järvinen */ 3096832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3097832d11c5SIlpo Järvinen { 3098832d11c5SIlpo Järvinen int from, to, merge, todo; 3099832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 3100832d11c5SIlpo Järvinen 3101832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3102f8071cdeSEric Dumazet 3103f8071cdeSEric Dumazet if (skb_headlen(skb)) 3104f8071cdeSEric Dumazet return 0; 31051f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 31061f8b977aSWillem de Bruijn return 0; 3107832d11c5SIlpo Järvinen 3108832d11c5SIlpo Järvinen todo = shiftlen; 3109832d11c5SIlpo Järvinen from = 0; 3110832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3111832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3112832d11c5SIlpo Järvinen 3113832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3114832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3115832d11c5SIlpo Järvinen */ 3116832d11c5SIlpo Järvinen if (!to || 3117ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3118ea2ab693SIan Campbell fragfrom->page_offset)) { 3119832d11c5SIlpo Järvinen merge = -1; 3120832d11c5SIlpo Järvinen } else { 3121832d11c5SIlpo Järvinen merge = to - 1; 3122832d11c5SIlpo Järvinen 31239e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3124832d11c5SIlpo Järvinen if (todo < 0) { 3125832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3126832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3127832d11c5SIlpo Järvinen return 0; 3128832d11c5SIlpo Järvinen 31299f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 31309f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3131832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3132832d11c5SIlpo Järvinen 31339e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 31349e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3135832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 3136832d11c5SIlpo Järvinen 3137832d11c5SIlpo Järvinen goto onlymerged; 3138832d11c5SIlpo Järvinen } 3139832d11c5SIlpo Järvinen 3140832d11c5SIlpo Järvinen from++; 3141832d11c5SIlpo Järvinen } 3142832d11c5SIlpo Järvinen 3143832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3144832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3145832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3146832d11c5SIlpo Järvinen return 0; 3147832d11c5SIlpo Järvinen 3148832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3149832d11c5SIlpo Järvinen return 0; 3150832d11c5SIlpo Järvinen 3151832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3152832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3153832d11c5SIlpo Järvinen return 0; 3154832d11c5SIlpo Järvinen 3155832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3156832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3157832d11c5SIlpo Järvinen 31589e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3159832d11c5SIlpo Järvinen *fragto = *fragfrom; 31609e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3161832d11c5SIlpo Järvinen from++; 3162832d11c5SIlpo Järvinen to++; 3163832d11c5SIlpo Järvinen 3164832d11c5SIlpo Järvinen } else { 3165ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3166832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 3167832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 31689e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3169832d11c5SIlpo Järvinen 3170832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 31719e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3172832d11c5SIlpo Järvinen todo = 0; 3173832d11c5SIlpo Järvinen 3174832d11c5SIlpo Järvinen to++; 3175832d11c5SIlpo Järvinen break; 3176832d11c5SIlpo Järvinen } 3177832d11c5SIlpo Järvinen } 3178832d11c5SIlpo Järvinen 3179832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3180832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3181832d11c5SIlpo Järvinen 3182832d11c5SIlpo Järvinen if (merge >= 0) { 3183832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3184832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3185832d11c5SIlpo Järvinen 31869e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 3187ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 3188832d11c5SIlpo Järvinen } 3189832d11c5SIlpo Järvinen 3190832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3191832d11c5SIlpo Järvinen to = 0; 3192832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3193832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3194832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3195832d11c5SIlpo Järvinen 3196832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3197832d11c5SIlpo Järvinen 3198832d11c5SIlpo Järvinen onlymerged: 3199832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3200832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3201832d11c5SIlpo Järvinen */ 3202832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3203832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3204832d11c5SIlpo Järvinen 3205832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 3206832d11c5SIlpo Järvinen skb->len -= shiftlen; 3207832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 3208832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 3209832d11c5SIlpo Järvinen tgt->len += shiftlen; 3210832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 3211832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 3212832d11c5SIlpo Järvinen 3213832d11c5SIlpo Järvinen return shiftlen; 3214832d11c5SIlpo Järvinen } 3215832d11c5SIlpo Järvinen 3216677e90edSThomas Graf /** 3217677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3218677e90edSThomas Graf * @skb: the buffer to read 3219677e90edSThomas Graf * @from: lower offset of data to be read 3220677e90edSThomas Graf * @to: upper offset of data to be read 3221677e90edSThomas Graf * @st: state variable 3222677e90edSThomas Graf * 3223677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3224677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3225677e90edSThomas Graf */ 3226677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3227677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3228677e90edSThomas Graf { 3229677e90edSThomas Graf st->lower_offset = from; 3230677e90edSThomas Graf st->upper_offset = to; 3231677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3232677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3233677e90edSThomas Graf st->frag_data = NULL; 3234677e90edSThomas Graf } 3235b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3236677e90edSThomas Graf 3237677e90edSThomas Graf /** 3238677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3239677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3240677e90edSThomas Graf * @data: destination pointer for data to be returned 3241677e90edSThomas Graf * @st: state variable 3242677e90edSThomas Graf * 3243bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3244677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3245bc32383cSMathias Krause * the head of the data block to @data and returns the length 3246677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3247677e90edSThomas Graf * offset has been reached. 3248677e90edSThomas Graf * 3249677e90edSThomas Graf * The caller is not required to consume all of the data 3250bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3251677e90edSThomas Graf * of bytes already consumed and the next call to 3252677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3253677e90edSThomas Graf * 325425985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3255e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3256677e90edSThomas Graf * reads of potentially non linear data. 3257677e90edSThomas Graf * 3258bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3259677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3260677e90edSThomas Graf * a stack for this purpose. 3261677e90edSThomas Graf */ 3262677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3263677e90edSThomas Graf struct skb_seq_state *st) 3264677e90edSThomas Graf { 3265677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3266677e90edSThomas Graf skb_frag_t *frag; 3267677e90edSThomas Graf 3268aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3269aeb193eaSWedson Almeida Filho if (st->frag_data) { 3270aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3271aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3272aeb193eaSWedson Almeida Filho } 3273677e90edSThomas Graf return 0; 3274aeb193eaSWedson Almeida Filho } 3275677e90edSThomas Graf 3276677e90edSThomas Graf next_skb: 327795e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3278677e90edSThomas Graf 3279995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 328095e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3281677e90edSThomas Graf return block_limit - abs_offset; 3282677e90edSThomas Graf } 3283677e90edSThomas Graf 3284677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3285677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3286677e90edSThomas Graf 3287677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 3288677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 32899e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 3290677e90edSThomas Graf 3291677e90edSThomas Graf if (abs_offset < block_limit) { 3292677e90edSThomas Graf if (!st->frag_data) 329351c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 3294677e90edSThomas Graf 3295677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 3296677e90edSThomas Graf (abs_offset - st->stepped_offset); 3297677e90edSThomas Graf 3298677e90edSThomas Graf return block_limit - abs_offset; 3299677e90edSThomas Graf } 3300677e90edSThomas Graf 3301677e90edSThomas Graf if (st->frag_data) { 330251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3303677e90edSThomas Graf st->frag_data = NULL; 3304677e90edSThomas Graf } 3305677e90edSThomas Graf 3306677e90edSThomas Graf st->frag_idx++; 33079e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 3308677e90edSThomas Graf } 3309677e90edSThomas Graf 33105b5a60daSOlaf Kirch if (st->frag_data) { 331151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 33125b5a60daSOlaf Kirch st->frag_data = NULL; 33135b5a60daSOlaf Kirch } 33145b5a60daSOlaf Kirch 331521dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3316677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 331795e3b24cSHerbert Xu st->frag_idx = 0; 3318677e90edSThomas Graf goto next_skb; 331971b3346dSShyam Iyer } else if (st->cur_skb->next) { 332071b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 332171b3346dSShyam Iyer st->frag_idx = 0; 3322677e90edSThomas Graf goto next_skb; 3323677e90edSThomas Graf } 3324677e90edSThomas Graf 3325677e90edSThomas Graf return 0; 3326677e90edSThomas Graf } 3327b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3328677e90edSThomas Graf 3329677e90edSThomas Graf /** 3330677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3331677e90edSThomas Graf * @st: state variable 3332677e90edSThomas Graf * 3333677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3334677e90edSThomas Graf * returned 0. 3335677e90edSThomas Graf */ 3336677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3337677e90edSThomas Graf { 3338677e90edSThomas Graf if (st->frag_data) 333951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3340677e90edSThomas Graf } 3341b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3342677e90edSThomas Graf 33433fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 33443fc7e8a6SThomas Graf 33453fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 33463fc7e8a6SThomas Graf struct ts_config *conf, 33473fc7e8a6SThomas Graf struct ts_state *state) 33483fc7e8a6SThomas Graf { 33493fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 33503fc7e8a6SThomas Graf } 33513fc7e8a6SThomas Graf 33523fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 33533fc7e8a6SThomas Graf { 33543fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 33553fc7e8a6SThomas Graf } 33563fc7e8a6SThomas Graf 33573fc7e8a6SThomas Graf /** 33583fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 33593fc7e8a6SThomas Graf * @skb: the buffer to look in 33603fc7e8a6SThomas Graf * @from: search offset 33613fc7e8a6SThomas Graf * @to: search limit 33623fc7e8a6SThomas Graf * @config: textsearch configuration 33633fc7e8a6SThomas Graf * 33643fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 33653fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 33663fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 33673fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 33683fc7e8a6SThomas Graf */ 33693fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3370059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 33713fc7e8a6SThomas Graf { 3372059a2440SBojan Prtvar struct ts_state state; 3373f72b948dSPhil Oester unsigned int ret; 3374f72b948dSPhil Oester 33753fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 33763fc7e8a6SThomas Graf config->finish = skb_ts_finish; 33773fc7e8a6SThomas Graf 3378059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 33793fc7e8a6SThomas Graf 3380059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3381f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 33823fc7e8a6SThomas Graf } 3383b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 33843fc7e8a6SThomas Graf 3385be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3386be12a1feSHannes Frederic Sowa int offset, size_t size) 3387be12a1feSHannes Frederic Sowa { 3388be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3389be12a1feSHannes Frederic Sowa 3390be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3391be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3392be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3393be12a1feSHannes Frederic Sowa get_page(page); 3394be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3395be12a1feSHannes Frederic Sowa } else { 3396be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3397be12a1feSHannes Frederic Sowa } 3398be12a1feSHannes Frederic Sowa 3399be12a1feSHannes Frederic Sowa return 0; 3400be12a1feSHannes Frederic Sowa } 3401be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3402be12a1feSHannes Frederic Sowa 3403cbb042f9SHerbert Xu /** 3404cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3405cbb042f9SHerbert Xu * @skb: buffer to update 3406cbb042f9SHerbert Xu * @len: length of data pulled 3407cbb042f9SHerbert Xu * 3408cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3409fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 341084fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 341184fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 341284fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3413cbb042f9SHerbert Xu */ 3414af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3415cbb042f9SHerbert Xu { 341631b33dfbSPravin B Shelar unsigned char *data = skb->data; 341731b33dfbSPravin B Shelar 3418cbb042f9SHerbert Xu BUG_ON(len > skb->len); 341931b33dfbSPravin B Shelar __skb_pull(skb, len); 342031b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 342131b33dfbSPravin B Shelar return skb->data; 3422cbb042f9SHerbert Xu } 3423f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3424f94691acSArnaldo Carvalho de Melo 342513acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 342613acc94eSYonghong Song { 342713acc94eSYonghong Song skb_frag_t head_frag; 342813acc94eSYonghong Song struct page *page; 342913acc94eSYonghong Song 343013acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 343113acc94eSYonghong Song head_frag.page.p = page; 343213acc94eSYonghong Song head_frag.page_offset = frag_skb->data - 343313acc94eSYonghong Song (unsigned char *)page_address(page); 343413acc94eSYonghong Song head_frag.size = skb_headlen(frag_skb); 343513acc94eSYonghong Song return head_frag; 343613acc94eSYonghong Song } 343713acc94eSYonghong Song 3438f4c50d99SHerbert Xu /** 3439f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3440df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3441576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3442f4c50d99SHerbert Xu * 3443f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 34444c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 34454c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3446f4c50d99SHerbert Xu */ 3447df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3448df5771ffSMichael S. Tsirkin netdev_features_t features) 3449f4c50d99SHerbert Xu { 3450f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3451f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 34521a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3453df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3454df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3455df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 34561fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3457f4c50d99SHerbert Xu unsigned int offset = doffset; 3458df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3459802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3460f4c50d99SHerbert Xu unsigned int headroom; 3461802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3462ec5f0615SPravin B Shelar __be16 proto; 346336c98382SAlexander Duyck bool csum, sg; 3464df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3465f4c50d99SHerbert Xu int err = -ENOMEM; 3466f4c50d99SHerbert Xu int i = 0; 3467f4c50d99SHerbert Xu int pos; 346853d6471cSVlad Yasevich int dummy; 3469f4c50d99SHerbert Xu 34705882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 347153d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3472ec5f0615SPravin B Shelar if (unlikely(!proto)) 3473ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3474ec5f0615SPravin B Shelar 347536c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 3476f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 34777e2b10c1STom Herbert 347807b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 347907b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 348007b26c94SSteffen Klassert struct sk_buff *iter; 348143170c4eSIlan Tayari unsigned int frag_len; 348207b26c94SSteffen Klassert 348307b26c94SSteffen Klassert if (!list_skb || 348407b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 348507b26c94SSteffen Klassert goto normal; 348607b26c94SSteffen Klassert 348743170c4eSIlan Tayari /* If we get here then all the required 348843170c4eSIlan Tayari * GSO features except frag_list are supported. 348943170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 349043170c4eSIlan Tayari * with no frag_list. 349143170c4eSIlan Tayari * Currently we can do that only when the buffers don't 349243170c4eSIlan Tayari * have a linear part and all the buffers except 349343170c4eSIlan Tayari * the last are of the same length. 349407b26c94SSteffen Klassert */ 349543170c4eSIlan Tayari frag_len = list_skb->len; 349607b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 349743170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 349843170c4eSIlan Tayari goto normal; 3499eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 350007b26c94SSteffen Klassert goto normal; 350107b26c94SSteffen Klassert 350207b26c94SSteffen Klassert len -= iter->len; 350307b26c94SSteffen Klassert } 350443170c4eSIlan Tayari 350543170c4eSIlan Tayari if (len != frag_len) 350643170c4eSIlan Tayari goto normal; 350707b26c94SSteffen Klassert } 350807b26c94SSteffen Klassert 3509802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 3510802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 3511802ab55aSAlexander Duyck * now. 3512802ab55aSAlexander Duyck */ 3513802ab55aSAlexander Duyck partial_segs = len / mss; 3514d7fb5a80SAlexander Duyck if (partial_segs > 1) 3515802ab55aSAlexander Duyck mss *= partial_segs; 3516d7fb5a80SAlexander Duyck else 3517d7fb5a80SAlexander Duyck partial_segs = 0; 3518802ab55aSAlexander Duyck } 3519802ab55aSAlexander Duyck 352007b26c94SSteffen Klassert normal: 3521df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3522df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3523f4c50d99SHerbert Xu 3524f4c50d99SHerbert Xu do { 3525f4c50d99SHerbert Xu struct sk_buff *nskb; 35268cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3527c8884eddSHerbert Xu int hsize; 3528f4c50d99SHerbert Xu int size; 3529f4c50d99SHerbert Xu 35303953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 35313953c46cSMarcelo Ricardo Leitner len = list_skb->len; 35323953c46cSMarcelo Ricardo Leitner } else { 3533df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3534f4c50d99SHerbert Xu if (len > mss) 3535f4c50d99SHerbert Xu len = mss; 35363953c46cSMarcelo Ricardo Leitner } 3537f4c50d99SHerbert Xu 3538df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3539f4c50d99SHerbert Xu if (hsize < 0) 3540f4c50d99SHerbert Xu hsize = 0; 3541c8884eddSHerbert Xu if (hsize > len || !sg) 3542c8884eddSHerbert Xu hsize = len; 3543f4c50d99SHerbert Xu 35441a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 35451a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 35461a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 354789319d38SHerbert Xu 35489d8506ccSHerbert Xu i = 0; 35491a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 35501a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 35511fd819ecSMichael S. Tsirkin frag_skb = list_skb; 35521a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 35539d8506ccSHerbert Xu 35549d8506ccSHerbert Xu while (pos < offset + len) { 35559d8506ccSHerbert Xu BUG_ON(i >= nfrags); 35569d8506ccSHerbert Xu 35574e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 35589d8506ccSHerbert Xu if (pos + size > offset + len) 35599d8506ccSHerbert Xu break; 35609d8506ccSHerbert Xu 35619d8506ccSHerbert Xu i++; 35629d8506ccSHerbert Xu pos += size; 35634e1beba1SMichael S. Tsirkin frag++; 35649d8506ccSHerbert Xu } 35659d8506ccSHerbert Xu 35661a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 35671a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 356889319d38SHerbert Xu 3569f4c50d99SHerbert Xu if (unlikely(!nskb)) 3570f4c50d99SHerbert Xu goto err; 3571f4c50d99SHerbert Xu 35729d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 35739d8506ccSHerbert Xu kfree_skb(nskb); 35749d8506ccSHerbert Xu goto err; 35759d8506ccSHerbert Xu } 35769d8506ccSHerbert Xu 3577ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 357889319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 357989319d38SHerbert Xu kfree_skb(nskb); 358089319d38SHerbert Xu goto err; 358189319d38SHerbert Xu } 358289319d38SHerbert Xu 3583ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 358489319d38SHerbert Xu skb_release_head_state(nskb); 358589319d38SHerbert Xu __skb_push(nskb, doffset); 358689319d38SHerbert Xu } else { 3587c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3588df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3589c93bdd0eSMel Gorman NUMA_NO_NODE); 359089319d38SHerbert Xu 359189319d38SHerbert Xu if (unlikely(!nskb)) 359289319d38SHerbert Xu goto err; 359389319d38SHerbert Xu 359489319d38SHerbert Xu skb_reserve(nskb, headroom); 359589319d38SHerbert Xu __skb_put(nskb, doffset); 359689319d38SHerbert Xu } 359789319d38SHerbert Xu 3598f4c50d99SHerbert Xu if (segs) 3599f4c50d99SHerbert Xu tail->next = nskb; 3600f4c50d99SHerbert Xu else 3601f4c50d99SHerbert Xu segs = nskb; 3602f4c50d99SHerbert Xu tail = nskb; 3603f4c50d99SHerbert Xu 3604df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3605f4c50d99SHerbert Xu 3606030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3607fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 360868c33163SPravin B Shelar 3609df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 361068c33163SPravin B Shelar nskb->data - tnl_hlen, 361168c33163SPravin B Shelar doffset + tnl_hlen); 361289319d38SHerbert Xu 36139d8506ccSHerbert Xu if (nskb->len == len + doffset) 36141cdbcb79SSimon Horman goto perform_csum_check; 361589319d38SHerbert Xu 36167fbeffedSAlexander Duyck if (!sg) { 36177fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 36186f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 361976443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 362076443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 3621f4c50d99SHerbert Xu skb_put(nskb, len), 3622f4c50d99SHerbert Xu len, 0); 36237e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3624de843723STom Herbert skb_headroom(nskb) + doffset; 3625f4c50d99SHerbert Xu continue; 3626f4c50d99SHerbert Xu } 3627f4c50d99SHerbert Xu 36288cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3629f4c50d99SHerbert Xu 3630df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3631d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3632f4c50d99SHerbert Xu 3633fff88030SWillem de Bruijn skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & 3634df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3635cef401deSEric Dumazet 3636bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3637bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 3638bf5c25d6SWillem de Bruijn goto err; 3639bf5c25d6SWillem de Bruijn 36409d8506ccSHerbert Xu while (pos < offset + len) { 36419d8506ccSHerbert Xu if (i >= nfrags) { 36429d8506ccSHerbert Xu i = 0; 36431a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 36441a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 36451fd819ecSMichael S. Tsirkin frag_skb = list_skb; 364613acc94eSYonghong Song if (!skb_headlen(list_skb)) { 36479d8506ccSHerbert Xu BUG_ON(!nfrags); 364813acc94eSYonghong Song } else { 364913acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 36509d8506ccSHerbert Xu 365113acc94eSYonghong Song /* to make room for head_frag. */ 365213acc94eSYonghong Song i--; 365313acc94eSYonghong Song frag--; 365413acc94eSYonghong Song } 3655bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3656bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 3657bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 3658bf5c25d6SWillem de Bruijn goto err; 3659bf5c25d6SWillem de Bruijn 36601a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 36619d8506ccSHerbert Xu } 36629d8506ccSHerbert Xu 36639d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 36649d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 36659d8506ccSHerbert Xu net_warn_ratelimited( 36669d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 36679d8506ccSHerbert Xu pos, mss); 3668ff907a11SEric Dumazet err = -EINVAL; 36699d8506ccSHerbert Xu goto err; 36709d8506ccSHerbert Xu } 36719d8506ccSHerbert Xu 367213acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 36738cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 36748cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3675f4c50d99SHerbert Xu 3676f4c50d99SHerbert Xu if (pos < offset) { 36778cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 36788cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3679f4c50d99SHerbert Xu } 3680f4c50d99SHerbert Xu 368189319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3682f4c50d99SHerbert Xu 3683f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3684f4c50d99SHerbert Xu i++; 36854e1beba1SMichael S. Tsirkin frag++; 3686f4c50d99SHerbert Xu pos += size; 3687f4c50d99SHerbert Xu } else { 36888cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 368989319d38SHerbert Xu goto skip_fraglist; 3690f4c50d99SHerbert Xu } 3691f4c50d99SHerbert Xu 36928cb19905SMichael S. Tsirkin nskb_frag++; 3693f4c50d99SHerbert Xu } 3694f4c50d99SHerbert Xu 369589319d38SHerbert Xu skip_fraglist: 3696f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3697f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3698f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3699ec5f0615SPravin B Shelar 37001cdbcb79SSimon Horman perform_csum_check: 37017fbeffedSAlexander Duyck if (!csum) { 3702ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 3703ff907a11SEric Dumazet __skb_linearize(nskb)) 3704ddff00d4SAlexander Duyck goto err; 3705ff907a11SEric Dumazet 37067fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 3707ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 370876443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 370976443456SAlexander Duyck skb_checksum(nskb, doffset, 371076443456SAlexander Duyck nskb->len - doffset, 0); 37117e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 37127e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3713ec5f0615SPravin B Shelar } 3714df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3715f4c50d99SHerbert Xu 3716bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3717bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3718bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3719bec3cfdcSEric Dumazet */ 3720bec3cfdcSEric Dumazet segs->prev = tail; 3721432c856fSToshiaki Makita 3722802ab55aSAlexander Duyck if (partial_segs) { 372307b26c94SSteffen Klassert struct sk_buff *iter; 3724802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 372507b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 3726802ab55aSAlexander Duyck 3727802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 372807b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 3729802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 3730802ab55aSAlexander Duyck 3731802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 3732802ab55aSAlexander Duyck * our way back down the stack of protocols. 3733802ab55aSAlexander Duyck */ 373407b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 373507b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 373607b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 373707b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 373807b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 373907b26c94SSteffen Klassert } 374007b26c94SSteffen Klassert 374107b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 374207b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 374307b26c94SSteffen Klassert else if (tail != segs) 374407b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 3745802ab55aSAlexander Duyck } 3746802ab55aSAlexander Duyck 3747432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3748432c856fSToshiaki Makita * using skb_set_owner_w(). 3749432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3750432c856fSToshiaki Makita */ 3751432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3752432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3753432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3754432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3755432c856fSToshiaki Makita } 3756f4c50d99SHerbert Xu return segs; 3757f4c50d99SHerbert Xu 3758f4c50d99SHerbert Xu err: 3759289dccbeSEric Dumazet kfree_skb_list(segs); 3760f4c50d99SHerbert Xu return ERR_PTR(err); 3761f4c50d99SHerbert Xu } 3762f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3763f4c50d99SHerbert Xu 3764d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb) 376571d93b39SHerbert Xu { 37668a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 376767147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 376867147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 37698a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 3770715dc1f3SEric Dumazet unsigned int delta_truesize; 3771d4546c25SDavid Miller struct sk_buff *lp; 377271d93b39SHerbert Xu 37738a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 377471d93b39SHerbert Xu return -E2BIG; 377571d93b39SHerbert Xu 377629e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 37778a29111cSEric Dumazet pinfo = skb_shinfo(lp); 37788a29111cSEric Dumazet 37798a29111cSEric Dumazet if (headlen <= offset) { 378042da6994SHerbert Xu skb_frag_t *frag; 378166e92fcfSHerbert Xu skb_frag_t *frag2; 37829aaa156cSHerbert Xu int i = skbinfo->nr_frags; 37839aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 378442da6994SHerbert Xu 378566e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 37868a29111cSEric Dumazet goto merge; 378781705ad1SHerbert Xu 37888a29111cSEric Dumazet offset -= headlen; 37899aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 37909aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3791f5572068SHerbert Xu 37929aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 37939aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 379466e92fcfSHerbert Xu do { 379566e92fcfSHerbert Xu *--frag = *--frag2; 379666e92fcfSHerbert Xu } while (--i); 379766e92fcfSHerbert Xu 379866e92fcfSHerbert Xu frag->page_offset += offset; 37999e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 380066e92fcfSHerbert Xu 3801715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3802ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3803ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3804715dc1f3SEric Dumazet 3805f5572068SHerbert Xu skb->truesize -= skb->data_len; 3806f5572068SHerbert Xu skb->len -= skb->data_len; 3807f5572068SHerbert Xu skb->data_len = 0; 3808f5572068SHerbert Xu 3809715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 38105d38a079SHerbert Xu goto done; 3811d7e8883cSEric Dumazet } else if (skb->head_frag) { 3812d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3813d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3814d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3815d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3816d7e8883cSEric Dumazet unsigned int first_offset; 3817d7e8883cSEric Dumazet 3818d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 38198a29111cSEric Dumazet goto merge; 3820d7e8883cSEric Dumazet 3821d7e8883cSEric Dumazet first_offset = skb->data - 3822d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3823d7e8883cSEric Dumazet offset; 3824d7e8883cSEric Dumazet 3825d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3826d7e8883cSEric Dumazet 3827d7e8883cSEric Dumazet frag->page.p = page; 3828d7e8883cSEric Dumazet frag->page_offset = first_offset; 3829d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3830d7e8883cSEric Dumazet 3831d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3832d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3833d7e8883cSEric Dumazet 3834715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3835d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3836d7e8883cSEric Dumazet goto done; 38378a29111cSEric Dumazet } 383871d93b39SHerbert Xu 383971d93b39SHerbert Xu merge: 3840715dc1f3SEric Dumazet delta_truesize = skb->truesize; 384167147ba9SHerbert Xu if (offset > headlen) { 3842d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3843d1dc7abfSMichal Schmidt 3844d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 38459e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3846d1dc7abfSMichal Schmidt skb->data_len -= eat; 3847d1dc7abfSMichal Schmidt skb->len -= eat; 384867147ba9SHerbert Xu offset = headlen; 384956035022SHerbert Xu } 385056035022SHerbert Xu 385167147ba9SHerbert Xu __skb_pull(skb, offset); 385256035022SHerbert Xu 385329e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 38548a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 38558a29111cSEric Dumazet else 3856c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3857c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3858f4a775d1SEric Dumazet __skb_header_release(skb); 38598a29111cSEric Dumazet lp = p; 386071d93b39SHerbert Xu 38615d38a079SHerbert Xu done: 38625d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 386337fe4732SHerbert Xu p->data_len += len; 3864715dc1f3SEric Dumazet p->truesize += delta_truesize; 386537fe4732SHerbert Xu p->len += len; 38668a29111cSEric Dumazet if (lp != p) { 38678a29111cSEric Dumazet lp->data_len += len; 38688a29111cSEric Dumazet lp->truesize += delta_truesize; 38698a29111cSEric Dumazet lp->len += len; 38708a29111cSEric Dumazet } 387171d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 387271d93b39SHerbert Xu return 0; 387371d93b39SHerbert Xu } 387457c05650SMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gro_receive); 387571d93b39SHerbert Xu 38761da177e4SLinus Torvalds void __init skb_init(void) 38771da177e4SLinus Torvalds { 387879a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 38791da177e4SLinus Torvalds sizeof(struct sk_buff), 38801da177e4SLinus Torvalds 0, 3881e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 388279a8a642SKees Cook offsetof(struct sk_buff, cb), 388379a8a642SKees Cook sizeof_field(struct sk_buff, cb), 388420c2df83SPaul Mundt NULL); 3885d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3886d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3887d179cd12SDavid S. Miller 0, 3888e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 388920c2df83SPaul Mundt NULL); 38901da177e4SLinus Torvalds } 38911da177e4SLinus Torvalds 389251c739d1SDavid S. Miller static int 389348a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 389448a1df65SJason A. Donenfeld unsigned int recursion_level) 3895716ea3a7SDavid Howells { 38961a028e50SDavid S. Miller int start = skb_headlen(skb); 38971a028e50SDavid S. Miller int i, copy = start - offset; 3898fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3899716ea3a7SDavid Howells int elt = 0; 3900716ea3a7SDavid Howells 390148a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 390248a1df65SJason A. Donenfeld return -EMSGSIZE; 390348a1df65SJason A. Donenfeld 3904716ea3a7SDavid Howells if (copy > 0) { 3905716ea3a7SDavid Howells if (copy > len) 3906716ea3a7SDavid Howells copy = len; 3907642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3908716ea3a7SDavid Howells elt++; 3909716ea3a7SDavid Howells if ((len -= copy) == 0) 3910716ea3a7SDavid Howells return elt; 3911716ea3a7SDavid Howells offset += copy; 3912716ea3a7SDavid Howells } 3913716ea3a7SDavid Howells 3914716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 39151a028e50SDavid S. Miller int end; 3916716ea3a7SDavid Howells 3917547b792cSIlpo Järvinen WARN_ON(start > offset + len); 39181a028e50SDavid S. Miller 39199e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3920716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3921716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 392248a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 392348a1df65SJason A. Donenfeld return -EMSGSIZE; 3924716ea3a7SDavid Howells 3925716ea3a7SDavid Howells if (copy > len) 3926716ea3a7SDavid Howells copy = len; 3927ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3928642f1490SJens Axboe frag->page_offset+offset-start); 3929716ea3a7SDavid Howells elt++; 3930716ea3a7SDavid Howells if (!(len -= copy)) 3931716ea3a7SDavid Howells return elt; 3932716ea3a7SDavid Howells offset += copy; 3933716ea3a7SDavid Howells } 39341a028e50SDavid S. Miller start = end; 3935716ea3a7SDavid Howells } 3936716ea3a7SDavid Howells 3937fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 393848a1df65SJason A. Donenfeld int end, ret; 3939716ea3a7SDavid Howells 3940547b792cSIlpo Järvinen WARN_ON(start > offset + len); 39411a028e50SDavid S. Miller 3942fbb398a8SDavid S. Miller end = start + frag_iter->len; 3943716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 394448a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 394548a1df65SJason A. Donenfeld return -EMSGSIZE; 394648a1df65SJason A. Donenfeld 3947716ea3a7SDavid Howells if (copy > len) 3948716ea3a7SDavid Howells copy = len; 394948a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 395048a1df65SJason A. Donenfeld copy, recursion_level + 1); 395148a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 395248a1df65SJason A. Donenfeld return ret; 395348a1df65SJason A. Donenfeld elt += ret; 3954716ea3a7SDavid Howells if ((len -= copy) == 0) 3955716ea3a7SDavid Howells return elt; 3956716ea3a7SDavid Howells offset += copy; 3957716ea3a7SDavid Howells } 39581a028e50SDavid S. Miller start = end; 3959716ea3a7SDavid Howells } 3960716ea3a7SDavid Howells BUG_ON(len); 3961716ea3a7SDavid Howells return elt; 3962716ea3a7SDavid Howells } 3963716ea3a7SDavid Howells 396448a1df65SJason A. Donenfeld /** 396548a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 396648a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 396748a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 396848a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 396948a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 397048a1df65SJason A. Donenfeld * 397148a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 397248a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 397348a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 397448a1df65SJason A. Donenfeld * could not fit. 397548a1df65SJason A. Donenfeld */ 397648a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 397748a1df65SJason A. Donenfeld { 397848a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 397948a1df65SJason A. Donenfeld 398048a1df65SJason A. Donenfeld if (nsg <= 0) 398148a1df65SJason A. Donenfeld return nsg; 398248a1df65SJason A. Donenfeld 398348a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 398448a1df65SJason A. Donenfeld 398548a1df65SJason A. Donenfeld return nsg; 398648a1df65SJason A. Donenfeld } 398748a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 398848a1df65SJason A. Donenfeld 398925a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 399025a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 399125a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 399225a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 399325a91d8dSFan Du * 399425a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 399525a91d8dSFan Du * 1. sg_init_table 399625a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 399725a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 399825a91d8dSFan Du * 399925a91d8dSFan Du * This is equivalent to: 400025a91d8dSFan Du * 1. sg_init_table 400125a91d8dSFan Du * 2. skb_to_sgvec(payload1) 400225a91d8dSFan Du * 3. sg_unmark_end 400325a91d8dSFan Du * 4. skb_to_sgvec(payload2) 400425a91d8dSFan Du * 400525a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 400625a91d8dSFan Du * is more preferable. 400725a91d8dSFan Du */ 400825a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 400925a91d8dSFan Du int offset, int len) 401025a91d8dSFan Du { 401148a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 401225a91d8dSFan Du } 401325a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 401425a91d8dSFan Du 401551c739d1SDavid S. Miller 401651c739d1SDavid S. Miller 4017716ea3a7SDavid Howells /** 4018716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4019716ea3a7SDavid Howells * @skb: The socket buffer to check. 4020716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4021716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4022716ea3a7SDavid Howells * 4023716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4024716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4025716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4026716ea3a7SDavid Howells * 4027716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4028716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4029716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4030716ea3a7SDavid Howells * 4031716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4032716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4033716ea3a7SDavid Howells */ 4034716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4035716ea3a7SDavid Howells { 4036716ea3a7SDavid Howells int copyflag; 4037716ea3a7SDavid Howells int elt; 4038716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4039716ea3a7SDavid Howells 4040716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4041716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4042716ea3a7SDavid Howells * at the moment even if they are anonymous). 4043716ea3a7SDavid Howells */ 4044716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4045716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 4046716ea3a7SDavid Howells return -ENOMEM; 4047716ea3a7SDavid Howells 4048716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 404921dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4050716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4051716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4052716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4053716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4054716ea3a7SDavid Howells 4055716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4056716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4057716ea3a7SDavid Howells return -ENOMEM; 4058716ea3a7SDavid Howells 4059716ea3a7SDavid Howells /* Voila! */ 4060716ea3a7SDavid Howells *trailer = skb; 4061716ea3a7SDavid Howells return 1; 4062716ea3a7SDavid Howells } 4063716ea3a7SDavid Howells 4064716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4065716ea3a7SDavid Howells 4066716ea3a7SDavid Howells elt = 1; 4067716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4068716ea3a7SDavid Howells copyflag = 0; 4069716ea3a7SDavid Howells 4070716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4071716ea3a7SDavid Howells int ntail = 0; 4072716ea3a7SDavid Howells 4073716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4074716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4075716ea3a7SDavid Howells * after it. */ 4076716ea3a7SDavid Howells 4077716ea3a7SDavid Howells if (skb_shared(skb1)) 4078716ea3a7SDavid Howells copyflag = 1; 4079716ea3a7SDavid Howells 4080716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4081716ea3a7SDavid Howells 4082716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4083716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 408421dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4085716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4086716ea3a7SDavid Howells ntail = tailbits + 128; 4087716ea3a7SDavid Howells } 4088716ea3a7SDavid Howells 4089716ea3a7SDavid Howells if (copyflag || 4090716ea3a7SDavid Howells skb_cloned(skb1) || 4091716ea3a7SDavid Howells ntail || 4092716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 409321dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4094716ea3a7SDavid Howells struct sk_buff *skb2; 4095716ea3a7SDavid Howells 4096716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4097716ea3a7SDavid Howells if (ntail == 0) 4098716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4099716ea3a7SDavid Howells else 4100716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4101716ea3a7SDavid Howells skb_headroom(skb1), 4102716ea3a7SDavid Howells ntail, 4103716ea3a7SDavid Howells GFP_ATOMIC); 4104716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4105716ea3a7SDavid Howells return -ENOMEM; 4106716ea3a7SDavid Howells 4107716ea3a7SDavid Howells if (skb1->sk) 4108716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4109716ea3a7SDavid Howells 4110716ea3a7SDavid Howells /* Looking around. Are we still alive? 4111716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4112716ea3a7SDavid Howells 4113716ea3a7SDavid Howells skb2->next = skb1->next; 4114716ea3a7SDavid Howells *skb_p = skb2; 4115716ea3a7SDavid Howells kfree_skb(skb1); 4116716ea3a7SDavid Howells skb1 = skb2; 4117716ea3a7SDavid Howells } 4118716ea3a7SDavid Howells elt++; 4119716ea3a7SDavid Howells *trailer = skb1; 4120716ea3a7SDavid Howells skb_p = &skb1->next; 4121716ea3a7SDavid Howells } 4122716ea3a7SDavid Howells 4123716ea3a7SDavid Howells return elt; 4124716ea3a7SDavid Howells } 4125b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4126716ea3a7SDavid Howells 4127b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4128b1faf566SEric Dumazet { 4129b1faf566SEric Dumazet struct sock *sk = skb->sk; 4130b1faf566SEric Dumazet 4131b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4132b1faf566SEric Dumazet } 4133b1faf566SEric Dumazet 41348605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 41358605330aSSoheil Hassas Yeganeh { 41368605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 41378605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 41388605330aSSoheil Hassas Yeganeh */ 41398605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 41408605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 41418605330aSSoheil Hassas Yeganeh } 41428605330aSSoheil Hassas Yeganeh 4143b1faf566SEric Dumazet /* 4144b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4145b1faf566SEric Dumazet */ 4146b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4147b1faf566SEric Dumazet { 4148b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 414995c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 4150b1faf566SEric Dumazet return -ENOMEM; 4151b1faf566SEric Dumazet 4152b1faf566SEric Dumazet skb_orphan(skb); 4153b1faf566SEric Dumazet skb->sk = sk; 4154b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4155b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 41568605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4157b1faf566SEric Dumazet 4158abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4159abb57ea4SEric Dumazet skb_dst_force(skb); 4160abb57ea4SEric Dumazet 4161b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4162b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 41636e5d58fdSVinicius Costa Gomes sk->sk_error_report(sk); 4164b1faf566SEric Dumazet return 0; 4165b1faf566SEric Dumazet } 4166b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4167b1faf566SEric Dumazet 416883a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 416983a1a1a7SSoheil Hassas Yeganeh { 417083a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 417183a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 417283a1a1a7SSoheil Hassas Yeganeh } 417383a1a1a7SSoheil Hassas Yeganeh 4174364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4175364a9e93SWillem de Bruijn { 4176364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 417783a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 417883a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4179997d5c3fSEric Dumazet unsigned long flags; 4180364a9e93SWillem de Bruijn 4181997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4182364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 418338b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 418483a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 418538b25793SSoheil Hassas Yeganeh if (icmp_next) 418638b25793SSoheil Hassas Yeganeh sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin; 418738b25793SSoheil Hassas Yeganeh } 4188997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4189364a9e93SWillem de Bruijn 419083a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 419183a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 419283a1a1a7SSoheil Hassas Yeganeh 419383a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4194364a9e93SWillem de Bruijn sk->sk_error_report(sk); 4195364a9e93SWillem de Bruijn 4196364a9e93SWillem de Bruijn return skb; 4197364a9e93SWillem de Bruijn } 4198364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4199364a9e93SWillem de Bruijn 4200cab41c47SAlexander Duyck /** 4201cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4202cab41c47SAlexander Duyck * @skb: the skb to clone 4203cab41c47SAlexander Duyck * 4204cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4205cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4206cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4207cab41c47SAlexander Duyck * 4208cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4209cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4210cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4211cab41c47SAlexander Duyck * the sk_error_queue. 4212cab41c47SAlexander Duyck */ 421362bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 421462bccb8cSAlexander Duyck { 421562bccb8cSAlexander Duyck struct sock *sk = skb->sk; 421662bccb8cSAlexander Duyck struct sk_buff *clone; 421762bccb8cSAlexander Duyck 421841c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 421962bccb8cSAlexander Duyck return NULL; 422062bccb8cSAlexander Duyck 422162bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 422262bccb8cSAlexander Duyck if (!clone) { 422362bccb8cSAlexander Duyck sock_put(sk); 422462bccb8cSAlexander Duyck return NULL; 422562bccb8cSAlexander Duyck } 422662bccb8cSAlexander Duyck 422762bccb8cSAlexander Duyck clone->sk = sk; 422862bccb8cSAlexander Duyck clone->destructor = sock_efree; 422962bccb8cSAlexander Duyck 423062bccb8cSAlexander Duyck return clone; 423162bccb8cSAlexander Duyck } 423262bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 423362bccb8cSAlexander Duyck 423437846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 423537846ef0SAlexander Duyck struct sock *sk, 42364ef1b286SSoheil Hassas Yeganeh int tstype, 42374ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4238ac45f602SPatrick Ohly { 4239ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4240ac45f602SPatrick Ohly int err; 4241ac45f602SPatrick Ohly 42424ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 42434ef1b286SSoheil Hassas Yeganeh 4244ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4245ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4246ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4247ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4248e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 42494ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 42501862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 42514ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 425209c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 4253ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 4254ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 42554ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 42564ed2d765SWillem de Bruijn } 425729030374SEric Dumazet 4258ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 425929030374SEric Dumazet 4260ac45f602SPatrick Ohly if (err) 4261ac45f602SPatrick Ohly kfree_skb(skb); 4262ac45f602SPatrick Ohly } 426337846ef0SAlexander Duyck 4264b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4265b245be1fSWillem de Bruijn { 4266b245be1fSWillem de Bruijn bool ret; 4267b245be1fSWillem de Bruijn 4268b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 4269b245be1fSWillem de Bruijn return true; 4270b245be1fSWillem de Bruijn 4271b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4272b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4273b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4274b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4275b245be1fSWillem de Bruijn return ret; 4276b245be1fSWillem de Bruijn } 4277b245be1fSWillem de Bruijn 427837846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 427937846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 428037846ef0SAlexander Duyck { 428137846ef0SAlexander Duyck struct sock *sk = skb->sk; 428237846ef0SAlexander Duyck 4283b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 428435b99dffSWillem de Bruijn goto err; 4285b245be1fSWillem de Bruijn 42869ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 42879ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 42889ac25fc0SEric Dumazet */ 428941c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 429037846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 42914ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 429237846ef0SAlexander Duyck sock_put(sk); 429335b99dffSWillem de Bruijn return; 429437846ef0SAlexander Duyck } 429535b99dffSWillem de Bruijn 429635b99dffSWillem de Bruijn err: 429735b99dffSWillem de Bruijn kfree_skb(skb); 42989ac25fc0SEric Dumazet } 429937846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 430037846ef0SAlexander Duyck 430137846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 430237846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 430337846ef0SAlexander Duyck struct sock *sk, int tstype) 430437846ef0SAlexander Duyck { 430537846ef0SAlexander Duyck struct sk_buff *skb; 43064ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 430737846ef0SAlexander Duyck 43083a8dd971SWillem de Bruijn if (!sk) 43093a8dd971SWillem de Bruijn return; 43103a8dd971SWillem de Bruijn 4311b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4312b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4313b50a5c70SMiroslav Lichvar return; 4314b50a5c70SMiroslav Lichvar 43153a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 43163a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 431737846ef0SAlexander Duyck return; 431837846ef0SAlexander Duyck 43191c885808SFrancis Yan if (tsonly) { 43201c885808SFrancis Yan #ifdef CONFIG_INET 43211c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 43221c885808SFrancis Yan sk->sk_protocol == IPPROTO_TCP && 43234ef1b286SSoheil Hassas Yeganeh sk->sk_type == SOCK_STREAM) { 43241c885808SFrancis Yan skb = tcp_get_timestamping_opt_stats(sk); 43254ef1b286SSoheil Hassas Yeganeh opt_stats = true; 43264ef1b286SSoheil Hassas Yeganeh } else 43271c885808SFrancis Yan #endif 43281c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 43291c885808SFrancis Yan } else { 433037846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 43311c885808SFrancis Yan } 433237846ef0SAlexander Duyck if (!skb) 433337846ef0SAlexander Duyck return; 433437846ef0SAlexander Duyck 433549ca0d8bSWillem de Bruijn if (tsonly) { 4336fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4337fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 433849ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 433949ca0d8bSWillem de Bruijn } 434049ca0d8bSWillem de Bruijn 434149ca0d8bSWillem de Bruijn if (hwtstamps) 434249ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 434349ca0d8bSWillem de Bruijn else 434449ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 434549ca0d8bSWillem de Bruijn 43464ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 434737846ef0SAlexander Duyck } 4348e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4349e7fd2885SWillem de Bruijn 4350e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4351e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4352e7fd2885SWillem de Bruijn { 4353e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 4354e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 4355e7fd2885SWillem de Bruijn } 4356ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4357ac45f602SPatrick Ohly 43586e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 43596e3e939fSJohannes Berg { 43606e3e939fSJohannes Berg struct sock *sk = skb->sk; 43616e3e939fSJohannes Berg struct sock_exterr_skb *serr; 4362dd4f1072SEric Dumazet int err = 1; 43636e3e939fSJohannes Berg 43646e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 43656e3e939fSJohannes Berg skb->wifi_acked = acked; 43666e3e939fSJohannes Berg 43676e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 43686e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 43696e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 43706e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 43716e3e939fSJohannes Berg 4372dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4373dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4374dd4f1072SEric Dumazet */ 437541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 43766e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4377dd4f1072SEric Dumazet sock_put(sk); 4378dd4f1072SEric Dumazet } 43796e3e939fSJohannes Berg if (err) 43806e3e939fSJohannes Berg kfree_skb(skb); 43816e3e939fSJohannes Berg } 43826e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 43836e3e939fSJohannes Berg 4384f35d9d8aSRusty Russell /** 4385f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4386f35d9d8aSRusty Russell * @skb: the skb to set 4387f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4388f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4389f35d9d8aSRusty Russell * 4390f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4391f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4392f35d9d8aSRusty Russell * 4393f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4394f35d9d8aSRusty Russell * returns false you should drop the packet. 4395f35d9d8aSRusty Russell */ 4396f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4397f35d9d8aSRusty Russell { 439852b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 439952b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 440052b5d6f5SEric Dumazet 440152b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 440252b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 440352b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 4404f35d9d8aSRusty Russell return false; 4405f35d9d8aSRusty Russell } 4406f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 440752b5d6f5SEric Dumazet skb->csum_start = csum_start; 4408f35d9d8aSRusty Russell skb->csum_offset = off; 4409e5d5decaSJason Wang skb_set_transport_header(skb, start); 4410f35d9d8aSRusty Russell return true; 4411f35d9d8aSRusty Russell } 4412b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4413f35d9d8aSRusty Russell 4414ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4415ed1f50c3SPaul Durrant unsigned int max) 4416ed1f50c3SPaul Durrant { 4417ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4418ed1f50c3SPaul Durrant return 0; 4419ed1f50c3SPaul Durrant 4420ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4421ed1f50c3SPaul Durrant * won't need to do it again. 4422ed1f50c3SPaul Durrant */ 4423ed1f50c3SPaul Durrant if (max > skb->len) 4424ed1f50c3SPaul Durrant max = skb->len; 4425ed1f50c3SPaul Durrant 4426ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4427ed1f50c3SPaul Durrant return -ENOMEM; 4428ed1f50c3SPaul Durrant 4429ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4430ed1f50c3SPaul Durrant return -EPROTO; 4431ed1f50c3SPaul Durrant 4432ed1f50c3SPaul Durrant return 0; 4433ed1f50c3SPaul Durrant } 4434ed1f50c3SPaul Durrant 4435f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4436f9708b43SJan Beulich 4437f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4438f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4439f9708b43SJan Beulich unsigned int off) 4440f9708b43SJan Beulich { 4441f9708b43SJan Beulich switch (proto) { 4442f9708b43SJan Beulich int err; 4443f9708b43SJan Beulich 4444f9708b43SJan Beulich case IPPROTO_TCP: 4445f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4446f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 4447f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4448f9708b43SJan Beulich offsetof(struct tcphdr, 4449f9708b43SJan Beulich check))) 4450f9708b43SJan Beulich err = -EPROTO; 4451f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4452f9708b43SJan Beulich 4453f9708b43SJan Beulich case IPPROTO_UDP: 4454f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4455f9708b43SJan Beulich off + sizeof(struct udphdr)); 4456f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4457f9708b43SJan Beulich offsetof(struct udphdr, 4458f9708b43SJan Beulich check))) 4459f9708b43SJan Beulich err = -EPROTO; 4460f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4461f9708b43SJan Beulich } 4462f9708b43SJan Beulich 4463f9708b43SJan Beulich return ERR_PTR(-EPROTO); 4464f9708b43SJan Beulich } 4465f9708b43SJan Beulich 4466ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4467ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 4468ed1f50c3SPaul Durrant */ 4469ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 4470ed1f50c3SPaul Durrant 4471f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4472ed1f50c3SPaul Durrant { 4473ed1f50c3SPaul Durrant unsigned int off; 4474ed1f50c3SPaul Durrant bool fragment; 4475f9708b43SJan Beulich __sum16 *csum; 4476ed1f50c3SPaul Durrant int err; 4477ed1f50c3SPaul Durrant 4478ed1f50c3SPaul Durrant fragment = false; 4479ed1f50c3SPaul Durrant 4480ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4481ed1f50c3SPaul Durrant sizeof(struct iphdr), 4482ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 4483ed1f50c3SPaul Durrant if (err < 0) 4484ed1f50c3SPaul Durrant goto out; 4485ed1f50c3SPaul Durrant 4486ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 4487ed1f50c3SPaul Durrant fragment = true; 4488ed1f50c3SPaul Durrant 4489ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 4490ed1f50c3SPaul Durrant 4491ed1f50c3SPaul Durrant err = -EPROTO; 4492ed1f50c3SPaul Durrant 4493ed1f50c3SPaul Durrant if (fragment) 4494ed1f50c3SPaul Durrant goto out; 4495ed1f50c3SPaul Durrant 4496f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 4497f9708b43SJan Beulich if (IS_ERR(csum)) 4498f9708b43SJan Beulich return PTR_ERR(csum); 4499ed1f50c3SPaul Durrant 4500ed1f50c3SPaul Durrant if (recalculate) 4501f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 4502ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 4503ed1f50c3SPaul Durrant skb->len - off, 4504f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 4505ed1f50c3SPaul Durrant err = 0; 4506ed1f50c3SPaul Durrant 4507ed1f50c3SPaul Durrant out: 4508ed1f50c3SPaul Durrant return err; 4509ed1f50c3SPaul Durrant } 4510ed1f50c3SPaul Durrant 4511ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4512ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 4513ed1f50c3SPaul Durrant */ 4514ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 4515ed1f50c3SPaul Durrant 4516ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 4517ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 4518ed1f50c3SPaul Durrant 4519ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 4520ed1f50c3SPaul Durrant { 4521ed1f50c3SPaul Durrant int err; 4522ed1f50c3SPaul Durrant u8 nexthdr; 4523ed1f50c3SPaul Durrant unsigned int off; 4524ed1f50c3SPaul Durrant unsigned int len; 4525ed1f50c3SPaul Durrant bool fragment; 4526ed1f50c3SPaul Durrant bool done; 4527f9708b43SJan Beulich __sum16 *csum; 4528ed1f50c3SPaul Durrant 4529ed1f50c3SPaul Durrant fragment = false; 4530ed1f50c3SPaul Durrant done = false; 4531ed1f50c3SPaul Durrant 4532ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 4533ed1f50c3SPaul Durrant 4534ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 4535ed1f50c3SPaul Durrant if (err < 0) 4536ed1f50c3SPaul Durrant goto out; 4537ed1f50c3SPaul Durrant 4538ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 4539ed1f50c3SPaul Durrant 4540ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 4541ed1f50c3SPaul Durrant while (off <= len && !done) { 4542ed1f50c3SPaul Durrant switch (nexthdr) { 4543ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 4544ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 4545ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 4546ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 4547ed1f50c3SPaul Durrant 4548ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4549ed1f50c3SPaul Durrant off + 4550ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 4551ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4552ed1f50c3SPaul Durrant if (err < 0) 4553ed1f50c3SPaul Durrant goto out; 4554ed1f50c3SPaul Durrant 4555ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4556ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4557ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 4558ed1f50c3SPaul Durrant break; 4559ed1f50c3SPaul Durrant } 4560ed1f50c3SPaul Durrant case IPPROTO_AH: { 4561ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 4562ed1f50c3SPaul Durrant 4563ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4564ed1f50c3SPaul Durrant off + 4565ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 4566ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4567ed1f50c3SPaul Durrant if (err < 0) 4568ed1f50c3SPaul Durrant goto out; 4569ed1f50c3SPaul Durrant 4570ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4571ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4572ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 4573ed1f50c3SPaul Durrant break; 4574ed1f50c3SPaul Durrant } 4575ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 4576ed1f50c3SPaul Durrant struct frag_hdr *hp; 4577ed1f50c3SPaul Durrant 4578ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4579ed1f50c3SPaul Durrant off + 4580ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 4581ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4582ed1f50c3SPaul Durrant if (err < 0) 4583ed1f50c3SPaul Durrant goto out; 4584ed1f50c3SPaul Durrant 4585ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 4586ed1f50c3SPaul Durrant 4587ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4588ed1f50c3SPaul Durrant fragment = true; 4589ed1f50c3SPaul Durrant 4590ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4591ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 4592ed1f50c3SPaul Durrant break; 4593ed1f50c3SPaul Durrant } 4594ed1f50c3SPaul Durrant default: 4595ed1f50c3SPaul Durrant done = true; 4596ed1f50c3SPaul Durrant break; 4597ed1f50c3SPaul Durrant } 4598ed1f50c3SPaul Durrant } 4599ed1f50c3SPaul Durrant 4600ed1f50c3SPaul Durrant err = -EPROTO; 4601ed1f50c3SPaul Durrant 4602ed1f50c3SPaul Durrant if (!done || fragment) 4603ed1f50c3SPaul Durrant goto out; 4604ed1f50c3SPaul Durrant 4605f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 4606f9708b43SJan Beulich if (IS_ERR(csum)) 4607f9708b43SJan Beulich return PTR_ERR(csum); 4608ed1f50c3SPaul Durrant 4609ed1f50c3SPaul Durrant if (recalculate) 4610f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4611ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 4612f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4613ed1f50c3SPaul Durrant err = 0; 4614ed1f50c3SPaul Durrant 4615ed1f50c3SPaul Durrant out: 4616ed1f50c3SPaul Durrant return err; 4617ed1f50c3SPaul Durrant } 4618ed1f50c3SPaul Durrant 4619ed1f50c3SPaul Durrant /** 4620ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4621ed1f50c3SPaul Durrant * @skb: the skb to set up 4622ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4623ed1f50c3SPaul Durrant */ 4624ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4625ed1f50c3SPaul Durrant { 4626ed1f50c3SPaul Durrant int err; 4627ed1f50c3SPaul Durrant 4628ed1f50c3SPaul Durrant switch (skb->protocol) { 4629ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4630f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4631ed1f50c3SPaul Durrant break; 4632ed1f50c3SPaul Durrant 4633ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4634ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4635ed1f50c3SPaul Durrant break; 4636ed1f50c3SPaul Durrant 4637ed1f50c3SPaul Durrant default: 4638ed1f50c3SPaul Durrant err = -EPROTO; 4639ed1f50c3SPaul Durrant break; 4640ed1f50c3SPaul Durrant } 4641ed1f50c3SPaul Durrant 4642ed1f50c3SPaul Durrant return err; 4643ed1f50c3SPaul Durrant } 4644ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4645ed1f50c3SPaul Durrant 46469afd85c9SLinus Lüssing /** 46479afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 46489afd85c9SLinus Lüssing * @skb: the skb to check 46499afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 46509afd85c9SLinus Lüssing * 46519afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 46529afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 46539afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 46549afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 46559afd85c9SLinus Lüssing * 4656a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4657a516993fSLinus Lüssing * differs from the provided skb. 46589afd85c9SLinus Lüssing */ 46599afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 46609afd85c9SLinus Lüssing unsigned int transport_len) 46619afd85c9SLinus Lüssing { 46629afd85c9SLinus Lüssing struct sk_buff *skb_chk; 46639afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 46649afd85c9SLinus Lüssing int ret; 46659afd85c9SLinus Lüssing 4666a516993fSLinus Lüssing if (skb->len < len) 46679afd85c9SLinus Lüssing return NULL; 4668a516993fSLinus Lüssing else if (skb->len == len) 46699afd85c9SLinus Lüssing return skb; 46709afd85c9SLinus Lüssing 46719afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 46729afd85c9SLinus Lüssing if (!skb_chk) 46739afd85c9SLinus Lüssing return NULL; 46749afd85c9SLinus Lüssing 46759afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 46769afd85c9SLinus Lüssing if (ret) { 46779afd85c9SLinus Lüssing kfree_skb(skb_chk); 46789afd85c9SLinus Lüssing return NULL; 46799afd85c9SLinus Lüssing } 46809afd85c9SLinus Lüssing 46819afd85c9SLinus Lüssing return skb_chk; 46829afd85c9SLinus Lüssing } 46839afd85c9SLinus Lüssing 46849afd85c9SLinus Lüssing /** 46859afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 46869afd85c9SLinus Lüssing * @skb: the skb to check 46879afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 46889afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 46899afd85c9SLinus Lüssing * 46909afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 46919afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 46929afd85c9SLinus Lüssing * 46939afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 46949afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 46959afd85c9SLinus Lüssing * 4696a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4697a516993fSLinus Lüssing * differs from the provided skb. 46989afd85c9SLinus Lüssing */ 46999afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 47009afd85c9SLinus Lüssing unsigned int transport_len, 47019afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 47029afd85c9SLinus Lüssing { 47039afd85c9SLinus Lüssing struct sk_buff *skb_chk; 47049afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 4705fcba67c9SLinus Lüssing __sum16 ret; 47069afd85c9SLinus Lüssing 47079afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 47089afd85c9SLinus Lüssing if (!skb_chk) 4709a516993fSLinus Lüssing goto err; 47109afd85c9SLinus Lüssing 4711a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 4712a516993fSLinus Lüssing goto err; 47139afd85c9SLinus Lüssing 47149b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 47159afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 47169b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 47179afd85c9SLinus Lüssing 4718a516993fSLinus Lüssing if (ret) 4719a516993fSLinus Lüssing goto err; 47209afd85c9SLinus Lüssing 47219afd85c9SLinus Lüssing return skb_chk; 4722a516993fSLinus Lüssing 4723a516993fSLinus Lüssing err: 4724a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 4725a516993fSLinus Lüssing kfree_skb(skb_chk); 4726a516993fSLinus Lüssing 4727a516993fSLinus Lüssing return NULL; 4728a516993fSLinus Lüssing 47299afd85c9SLinus Lüssing } 47309afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 47319afd85c9SLinus Lüssing 47324497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 47334497b076SBen Hutchings { 4734e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4735e87cc472SJoe Perches skb->dev->name); 47364497b076SBen Hutchings } 47374497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4738bad43ca8SEric Dumazet 4739bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4740bad43ca8SEric Dumazet { 47413d861f66SEric Dumazet if (head_stolen) { 47423d861f66SEric Dumazet skb_release_head_state(skb); 4743bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 47443d861f66SEric Dumazet } else { 4745bad43ca8SEric Dumazet __kfree_skb(skb); 4746bad43ca8SEric Dumazet } 47473d861f66SEric Dumazet } 4748bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4749bad43ca8SEric Dumazet 4750bad43ca8SEric Dumazet /** 4751bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4752bad43ca8SEric Dumazet * @to: prior buffer 4753bad43ca8SEric Dumazet * @from: buffer to add 4754bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4755c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4756bad43ca8SEric Dumazet */ 4757bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4758bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4759bad43ca8SEric Dumazet { 4760c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 4761bad43ca8SEric Dumazet int i, delta, len = from->len; 4762bad43ca8SEric Dumazet 4763bad43ca8SEric Dumazet *fragstolen = false; 4764bad43ca8SEric Dumazet 4765bad43ca8SEric Dumazet if (skb_cloned(to)) 4766bad43ca8SEric Dumazet return false; 4767bad43ca8SEric Dumazet 4768bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4769e93a0435SEric Dumazet if (len) 4770bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4771bad43ca8SEric Dumazet *delta_truesize = 0; 4772bad43ca8SEric Dumazet return true; 4773bad43ca8SEric Dumazet } 4774bad43ca8SEric Dumazet 4775c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 4776c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 4777c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 4778bad43ca8SEric Dumazet return false; 47791f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 47801f8b977aSWillem de Bruijn return false; 4781bad43ca8SEric Dumazet 4782bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4783bad43ca8SEric Dumazet struct page *page; 4784bad43ca8SEric Dumazet unsigned int offset; 4785bad43ca8SEric Dumazet 4786c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 4787c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 4788bad43ca8SEric Dumazet return false; 4789bad43ca8SEric Dumazet 4790bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4791bad43ca8SEric Dumazet return false; 4792bad43ca8SEric Dumazet 4793bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4794bad43ca8SEric Dumazet 4795bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4796bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4797bad43ca8SEric Dumazet 4798c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 4799bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4800bad43ca8SEric Dumazet *fragstolen = true; 4801bad43ca8SEric Dumazet } else { 4802c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 4803c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 4804bad43ca8SEric Dumazet return false; 4805bad43ca8SEric Dumazet 4806f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4807bad43ca8SEric Dumazet } 4808bad43ca8SEric Dumazet 4809bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4810bad43ca8SEric Dumazet 4811c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 4812c818fa9eSEric Dumazet from_shinfo->frags, 4813c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 4814c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 4815bad43ca8SEric Dumazet 4816bad43ca8SEric Dumazet if (!skb_cloned(from)) 4817c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 4818bad43ca8SEric Dumazet 48198ea853fdSLi RongQing /* if the skb is not cloned this does nothing 48208ea853fdSLi RongQing * since we set nr_frags to 0. 48218ea853fdSLi RongQing */ 4822c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 4823c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 4824bad43ca8SEric Dumazet 4825bad43ca8SEric Dumazet to->truesize += delta; 4826bad43ca8SEric Dumazet to->len += len; 4827bad43ca8SEric Dumazet to->data_len += len; 4828bad43ca8SEric Dumazet 4829bad43ca8SEric Dumazet *delta_truesize = delta; 4830bad43ca8SEric Dumazet return true; 4831bad43ca8SEric Dumazet } 4832bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4833621e84d6SNicolas Dichtel 4834621e84d6SNicolas Dichtel /** 48358b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4836621e84d6SNicolas Dichtel * 4837621e84d6SNicolas Dichtel * @skb: buffer to clean 48388b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4839621e84d6SNicolas Dichtel * 48408b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 48418b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 48428b27f277SNicolas Dichtel * operations. 48438b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 48448b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 48458b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4846621e84d6SNicolas Dichtel */ 48478b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4848621e84d6SNicolas Dichtel { 4849621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4850621e84d6SNicolas Dichtel skb->skb_iif = 0; 485160ff7467SWANG Cong skb->ignore_df = 0; 4852621e84d6SNicolas Dichtel skb_dst_drop(skb); 4853621e84d6SNicolas Dichtel secpath_reset(skb); 4854621e84d6SNicolas Dichtel nf_reset(skb); 4855621e84d6SNicolas Dichtel nf_reset_trace(skb); 4856213dd74aSHerbert Xu 4857213dd74aSHerbert Xu if (!xnet) 4858213dd74aSHerbert Xu return; 4859213dd74aSHerbert Xu 48602b5ec1a5SYe Yin ipvs_reset(skb); 4861213dd74aSHerbert Xu skb->mark = 0; 4862c47d8c2fSJesus Sanchez-Palencia skb->tstamp = 0; 4863621e84d6SNicolas Dichtel } 4864621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4865de960aa9SFlorian Westphal 4866de960aa9SFlorian Westphal /** 4867de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4868de960aa9SFlorian Westphal * 4869de960aa9SFlorian Westphal * @skb: GSO skb 4870de960aa9SFlorian Westphal * 4871de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4872de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4873de960aa9SFlorian Westphal * 4874de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4875de960aa9SFlorian Westphal */ 4876a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4877de960aa9SFlorian Westphal { 4878de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4879f993bc25SFlorian Westphal unsigned int thlen = 0; 4880f993bc25SFlorian Westphal 4881f993bc25SFlorian Westphal if (skb->encapsulation) { 4882f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4883f993bc25SFlorian Westphal skb_transport_header(skb); 4884de960aa9SFlorian Westphal 4885de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4886f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4887f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4888f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 48891dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 489090017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 4891ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 4892ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 4893f993bc25SFlorian Westphal } 48946d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 48956d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 48966d39d589SFlorian Westphal * accounted for. 48976d39d589SFlorian Westphal */ 4898f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4899de960aa9SFlorian Westphal } 4900a4a77718SDaniel Axtens 4901a4a77718SDaniel Axtens /** 4902a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 4903a4a77718SDaniel Axtens * 4904a4a77718SDaniel Axtens * @skb: GSO skb 4905a4a77718SDaniel Axtens * 4906a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 4907a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 4908a4a77718SDaniel Axtens * 4909a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 4910a4a77718SDaniel Axtens */ 4911a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 4912a4a77718SDaniel Axtens { 4913a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 4914a4a77718SDaniel Axtens skb_network_header(skb); 4915a4a77718SDaniel Axtens 4916a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 4917a4a77718SDaniel Axtens } 4918a4a77718SDaniel Axtens 4919a4a77718SDaniel Axtens /** 4920a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 4921a4a77718SDaniel Axtens * 4922a4a77718SDaniel Axtens * @skb: GSO skb 4923a4a77718SDaniel Axtens * 4924a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 4925a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 4926a4a77718SDaniel Axtens * headers (TCP/UDP). 4927a4a77718SDaniel Axtens */ 4928a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 4929a4a77718SDaniel Axtens { 4930a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 4931a4a77718SDaniel Axtens 4932a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 4933a4a77718SDaniel Axtens } 49340d5501c1SVlad Yasevich 4935ae7ef81eSMarcelo Ricardo Leitner /** 49362b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 49372b16f048SDaniel Axtens * 49382b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 49392b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 49402b16f048SDaniel Axtens * 49412b16f048SDaniel Axtens * We might want to check: 49422b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 49432b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 49442b16f048SDaniel Axtens * 49452b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 49462b16f048SDaniel Axtens * 494749682bfaSMathieu Malaterre * @skb: GSO skb 494849682bfaSMathieu Malaterre * 49492b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 49502b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 49512b16f048SDaniel Axtens * 49522b16f048SDaniel Axtens * @max_len: The maximum permissible length. 49532b16f048SDaniel Axtens * 49542b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 49552b16f048SDaniel Axtens */ 49562b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 49572b16f048SDaniel Axtens unsigned int seg_len, 49582b16f048SDaniel Axtens unsigned int max_len) { 49592b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 49602b16f048SDaniel Axtens const struct sk_buff *iter; 49612b16f048SDaniel Axtens 49622b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 49632b16f048SDaniel Axtens return seg_len <= max_len; 49642b16f048SDaniel Axtens 49652b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 49662b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 49672b16f048SDaniel Axtens 49682b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 49692b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 49702b16f048SDaniel Axtens return false; 49712b16f048SDaniel Axtens } 49722b16f048SDaniel Axtens 49732b16f048SDaniel Axtens return true; 49742b16f048SDaniel Axtens } 49752b16f048SDaniel Axtens 49762b16f048SDaniel Axtens /** 4977779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 4978ae7ef81eSMarcelo Ricardo Leitner * 4979ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 498076f21b99SDavid S. Miller * @mtu: MTU to validate against 4981ae7ef81eSMarcelo Ricardo Leitner * 4982779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 4983779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 4984779b7931SDaniel Axtens * payload. 4985ae7ef81eSMarcelo Ricardo Leitner */ 4986779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 4987ae7ef81eSMarcelo Ricardo Leitner { 49882b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 4989ae7ef81eSMarcelo Ricardo Leitner } 4990779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 4991ae7ef81eSMarcelo Ricardo Leitner 49922b16f048SDaniel Axtens /** 49932b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 49942b16f048SDaniel Axtens * 49952b16f048SDaniel Axtens * @skb: GSO skb 49962b16f048SDaniel Axtens * @len: length to validate against 49972b16f048SDaniel Axtens * 49982b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 49992b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 50002b16f048SDaniel Axtens */ 50012b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 50022b16f048SDaniel Axtens { 50032b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 50042b16f048SDaniel Axtens } 50052b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 50062b16f048SDaniel Axtens 50070d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 50080d5501c1SVlad Yasevich { 50094bbb3e0eSToshiaki Makita int mac_len; 50104bbb3e0eSToshiaki Makita 50110d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 50120d5501c1SVlad Yasevich kfree_skb(skb); 50130d5501c1SVlad Yasevich return NULL; 50140d5501c1SVlad Yasevich } 50150d5501c1SVlad Yasevich 50164bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5017ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 50184bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 50194bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5020ae474573SToshiaki Makita } 50210d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 50220d5501c1SVlad Yasevich return skb; 50230d5501c1SVlad Yasevich } 50240d5501c1SVlad Yasevich 50250d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 50260d5501c1SVlad Yasevich { 50270d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 50280d5501c1SVlad Yasevich u16 vlan_tci; 50290d5501c1SVlad Yasevich 5030df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 50310d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 50320d5501c1SVlad Yasevich return skb; 50330d5501c1SVlad Yasevich } 50340d5501c1SVlad Yasevich 50350d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 50360d5501c1SVlad Yasevich if (unlikely(!skb)) 50370d5501c1SVlad Yasevich goto err_free; 50380d5501c1SVlad Yasevich 50390d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 50400d5501c1SVlad Yasevich goto err_free; 50410d5501c1SVlad Yasevich 50420d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 50430d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 50440d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 50450d5501c1SVlad Yasevich 50460d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 50470d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 50480d5501c1SVlad Yasevich 50490d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 50500d5501c1SVlad Yasevich if (unlikely(!skb)) 50510d5501c1SVlad Yasevich goto err_free; 50520d5501c1SVlad Yasevich 50530d5501c1SVlad Yasevich skb_reset_network_header(skb); 50540d5501c1SVlad Yasevich skb_reset_transport_header(skb); 50550d5501c1SVlad Yasevich skb_reset_mac_len(skb); 50560d5501c1SVlad Yasevich 50570d5501c1SVlad Yasevich return skb; 50580d5501c1SVlad Yasevich 50590d5501c1SVlad Yasevich err_free: 50600d5501c1SVlad Yasevich kfree_skb(skb); 50610d5501c1SVlad Yasevich return NULL; 50620d5501c1SVlad Yasevich } 50630d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 50642e4e4410SEric Dumazet 5065e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 5066e2195121SJiri Pirko { 5067e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5068e2195121SJiri Pirko return -ENOMEM; 5069e2195121SJiri Pirko 5070e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5071e2195121SJiri Pirko return 0; 5072e2195121SJiri Pirko 5073e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5074e2195121SJiri Pirko } 5075e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5076e2195121SJiri Pirko 5077bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5078bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5079bfca4c52SShmulik Ladkani */ 5080bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 508193515d53SJiri Pirko { 508293515d53SJiri Pirko struct vlan_hdr *vhdr; 5083b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 508493515d53SJiri Pirko int err; 508593515d53SJiri Pirko 5086b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5087b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5088b6a79208SShmulik Ladkani offset)) { 5089b6a79208SShmulik Ladkani return -EINVAL; 5090b6a79208SShmulik Ladkani } 5091b6a79208SShmulik Ladkani 509293515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 509393515d53SJiri Pirko if (unlikely(err)) 5094b6a79208SShmulik Ladkani return err; 509593515d53SJiri Pirko 509693515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 509793515d53SJiri Pirko 509893515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 509993515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 510093515d53SJiri Pirko 510193515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 510293515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 510393515d53SJiri Pirko 510493515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 510593515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 510693515d53SJiri Pirko 510793515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 510893515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 510993515d53SJiri Pirko 511093515d53SJiri Pirko skb_reset_mac_len(skb); 511193515d53SJiri Pirko 511293515d53SJiri Pirko return err; 511393515d53SJiri Pirko } 5114bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 511593515d53SJiri Pirko 5116b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5117b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5118b6a79208SShmulik Ladkani */ 511993515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 512093515d53SJiri Pirko { 512193515d53SJiri Pirko u16 vlan_tci; 512293515d53SJiri Pirko __be16 vlan_proto; 512393515d53SJiri Pirko int err; 512493515d53SJiri Pirko 5125df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 512693515d53SJiri Pirko skb->vlan_tci = 0; 512793515d53SJiri Pirko } else { 5128ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 512993515d53SJiri Pirko return 0; 513093515d53SJiri Pirko 513193515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 513293515d53SJiri Pirko if (err) 513393515d53SJiri Pirko return err; 513493515d53SJiri Pirko } 513593515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5136ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 513793515d53SJiri Pirko return 0; 513893515d53SJiri Pirko 513993515d53SJiri Pirko vlan_proto = skb->protocol; 514093515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 514193515d53SJiri Pirko if (unlikely(err)) 514293515d53SJiri Pirko return err; 514393515d53SJiri Pirko 514493515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 514593515d53SJiri Pirko return 0; 514693515d53SJiri Pirko } 514793515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 514893515d53SJiri Pirko 5149b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5150b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5151b6a79208SShmulik Ladkani */ 515293515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 515393515d53SJiri Pirko { 5154df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5155b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 515693515d53SJiri Pirko int err; 515793515d53SJiri Pirko 5158b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5159b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5160b6a79208SShmulik Ladkani offset)) { 5161b6a79208SShmulik Ladkani return -EINVAL; 5162b6a79208SShmulik Ladkani } 5163b6a79208SShmulik Ladkani 516493515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5165df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5166b6a79208SShmulik Ladkani if (err) 516793515d53SJiri Pirko return err; 51689241e2dfSDaniel Borkmann 516993515d53SJiri Pirko skb->protocol = skb->vlan_proto; 517093515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 517193515d53SJiri Pirko 51726b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 517393515d53SJiri Pirko } 517493515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 517593515d53SJiri Pirko return 0; 517693515d53SJiri Pirko } 517793515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 517893515d53SJiri Pirko 51792e4e4410SEric Dumazet /** 51802e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 51812e4e4410SEric Dumazet * 5182de3f0d0eSMasanari Iida * @header_len: size of linear part 5183de3f0d0eSMasanari Iida * @data_len: needed length in frags 5184de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 5185de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 5186de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 51872e4e4410SEric Dumazet * 51882e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 51892e4e4410SEric Dumazet */ 51902e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 51912e4e4410SEric Dumazet unsigned long data_len, 51922e4e4410SEric Dumazet int max_page_order, 51932e4e4410SEric Dumazet int *errcode, 51942e4e4410SEric Dumazet gfp_t gfp_mask) 51952e4e4410SEric Dumazet { 51962e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 51972e4e4410SEric Dumazet unsigned long chunk; 51982e4e4410SEric Dumazet struct sk_buff *skb; 51992e4e4410SEric Dumazet struct page *page; 52002e4e4410SEric Dumazet gfp_t gfp_head; 52012e4e4410SEric Dumazet int i; 52022e4e4410SEric Dumazet 52032e4e4410SEric Dumazet *errcode = -EMSGSIZE; 52042e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 52052e4e4410SEric Dumazet * high order pages... 52062e4e4410SEric Dumazet */ 52072e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 52082e4e4410SEric Dumazet return NULL; 52092e4e4410SEric Dumazet 52102e4e4410SEric Dumazet gfp_head = gfp_mask; 5211d0164adcSMel Gorman if (gfp_head & __GFP_DIRECT_RECLAIM) 5212dcda9b04SMichal Hocko gfp_head |= __GFP_RETRY_MAYFAIL; 52132e4e4410SEric Dumazet 52142e4e4410SEric Dumazet *errcode = -ENOBUFS; 52152e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 52162e4e4410SEric Dumazet if (!skb) 52172e4e4410SEric Dumazet return NULL; 52182e4e4410SEric Dumazet 52192e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 52202e4e4410SEric Dumazet 52212e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 52222e4e4410SEric Dumazet int order = max_page_order; 52232e4e4410SEric Dumazet 52242e4e4410SEric Dumazet while (order) { 52252e4e4410SEric Dumazet if (npages >= 1 << order) { 5226d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 52272e4e4410SEric Dumazet __GFP_COMP | 5228d14b56f5SMichal Hocko __GFP_NOWARN, 52292e4e4410SEric Dumazet order); 52302e4e4410SEric Dumazet if (page) 52312e4e4410SEric Dumazet goto fill_page; 52322e4e4410SEric Dumazet /* Do not retry other high order allocations */ 52332e4e4410SEric Dumazet order = 1; 52342e4e4410SEric Dumazet max_page_order = 0; 52352e4e4410SEric Dumazet } 52362e4e4410SEric Dumazet order--; 52372e4e4410SEric Dumazet } 52382e4e4410SEric Dumazet page = alloc_page(gfp_mask); 52392e4e4410SEric Dumazet if (!page) 52402e4e4410SEric Dumazet goto failure; 52412e4e4410SEric Dumazet fill_page: 52422e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 52432e4e4410SEric Dumazet PAGE_SIZE << order); 52442e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 52452e4e4410SEric Dumazet data_len -= chunk; 52462e4e4410SEric Dumazet npages -= 1 << order; 52472e4e4410SEric Dumazet } 52482e4e4410SEric Dumazet return skb; 52492e4e4410SEric Dumazet 52502e4e4410SEric Dumazet failure: 52512e4e4410SEric Dumazet kfree_skb(skb); 52522e4e4410SEric Dumazet return NULL; 52532e4e4410SEric Dumazet } 52542e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 52556fa01ccdSSowmini Varadhan 52566fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 52576fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 52586fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 52596fa01ccdSSowmini Varadhan { 52606fa01ccdSSowmini Varadhan int i; 52616fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 52626fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 52636fa01ccdSSowmini Varadhan u8 *data; 52646fa01ccdSSowmini Varadhan 52656fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 52666fa01ccdSSowmini Varadhan 52676fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 52686fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 52696fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 52706fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 52716fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 52726fa01ccdSSowmini Varadhan if (!data) 52736fa01ccdSSowmini Varadhan return -ENOMEM; 52746fa01ccdSSowmini Varadhan 52756fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 52766fa01ccdSSowmini Varadhan 52776fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 52786fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 52796fa01ccdSSowmini Varadhan skb->len -= off; 52806fa01ccdSSowmini Varadhan 52816fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 52826fa01ccdSSowmini Varadhan skb_shinfo(skb), 52836fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 52846fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 52856fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 52866fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 52876fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 52886fa01ccdSSowmini Varadhan kfree(data); 52896fa01ccdSSowmini Varadhan return -ENOMEM; 52906fa01ccdSSowmini Varadhan } 52916fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 52926fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 52936fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 52946fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 52956fa01ccdSSowmini Varadhan skb_release_data(skb); 52966fa01ccdSSowmini Varadhan } else { 52976fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 52986fa01ccdSSowmini Varadhan * relocate values 52996fa01ccdSSowmini Varadhan */ 53006fa01ccdSSowmini Varadhan skb_free_head(skb); 53016fa01ccdSSowmini Varadhan } 53026fa01ccdSSowmini Varadhan 53036fa01ccdSSowmini Varadhan skb->head = data; 53046fa01ccdSSowmini Varadhan skb->data = data; 53056fa01ccdSSowmini Varadhan skb->head_frag = 0; 53066fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 53076fa01ccdSSowmini Varadhan skb->end = size; 53086fa01ccdSSowmini Varadhan #else 53096fa01ccdSSowmini Varadhan skb->end = skb->head + size; 53106fa01ccdSSowmini Varadhan #endif 53116fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 53126fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 53136fa01ccdSSowmini Varadhan skb->cloned = 0; 53146fa01ccdSSowmini Varadhan skb->hdr_len = 0; 53156fa01ccdSSowmini Varadhan skb->nohdr = 0; 53166fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 53176fa01ccdSSowmini Varadhan 53186fa01ccdSSowmini Varadhan return 0; 53196fa01ccdSSowmini Varadhan } 53206fa01ccdSSowmini Varadhan 53216fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 53226fa01ccdSSowmini Varadhan 53236fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 53246fa01ccdSSowmini Varadhan * pskb_carve() 53256fa01ccdSSowmini Varadhan */ 53266fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 53276fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 53286fa01ccdSSowmini Varadhan gfp_t gfp_mask) 53296fa01ccdSSowmini Varadhan { 53306fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 53316fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 53326fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 53336fa01ccdSSowmini Varadhan 53346fa01ccdSSowmini Varadhan do { 53356fa01ccdSSowmini Varadhan if (!list) { 53366fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 53376fa01ccdSSowmini Varadhan return -EFAULT; 53386fa01ccdSSowmini Varadhan } 53396fa01ccdSSowmini Varadhan if (list->len <= eat) { 53406fa01ccdSSowmini Varadhan /* Eaten as whole. */ 53416fa01ccdSSowmini Varadhan eat -= list->len; 53426fa01ccdSSowmini Varadhan list = list->next; 53436fa01ccdSSowmini Varadhan insp = list; 53446fa01ccdSSowmini Varadhan } else { 53456fa01ccdSSowmini Varadhan /* Eaten partially. */ 53466fa01ccdSSowmini Varadhan if (skb_shared(list)) { 53476fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 53486fa01ccdSSowmini Varadhan if (!clone) 53496fa01ccdSSowmini Varadhan return -ENOMEM; 53506fa01ccdSSowmini Varadhan insp = list->next; 53516fa01ccdSSowmini Varadhan list = clone; 53526fa01ccdSSowmini Varadhan } else { 53536fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 53546fa01ccdSSowmini Varadhan insp = list; 53556fa01ccdSSowmini Varadhan } 53566fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 53576fa01ccdSSowmini Varadhan kfree_skb(clone); 53586fa01ccdSSowmini Varadhan return -ENOMEM; 53596fa01ccdSSowmini Varadhan } 53606fa01ccdSSowmini Varadhan break; 53616fa01ccdSSowmini Varadhan } 53626fa01ccdSSowmini Varadhan } while (eat); 53636fa01ccdSSowmini Varadhan 53646fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 53656fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 53666fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 53676fa01ccdSSowmini Varadhan kfree_skb(list); 53686fa01ccdSSowmini Varadhan } 53696fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 53706fa01ccdSSowmini Varadhan if (clone) { 53716fa01ccdSSowmini Varadhan clone->next = list; 53726fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 53736fa01ccdSSowmini Varadhan } 53746fa01ccdSSowmini Varadhan return 0; 53756fa01ccdSSowmini Varadhan } 53766fa01ccdSSowmini Varadhan 53776fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 53786fa01ccdSSowmini Varadhan * non-linear part of skb 53796fa01ccdSSowmini Varadhan */ 53806fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 53816fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 53826fa01ccdSSowmini Varadhan { 53836fa01ccdSSowmini Varadhan int i, k = 0; 53846fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 53856fa01ccdSSowmini Varadhan u8 *data; 53866fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 53876fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 53886fa01ccdSSowmini Varadhan 53896fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 53906fa01ccdSSowmini Varadhan 53916fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 53926fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 53936fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 53946fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 53956fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 53966fa01ccdSSowmini Varadhan if (!data) 53976fa01ccdSSowmini Varadhan return -ENOMEM; 53986fa01ccdSSowmini Varadhan 53996fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 54006fa01ccdSSowmini Varadhan 54016fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 54026fa01ccdSSowmini Varadhan skb_shinfo(skb), offsetof(struct skb_shared_info, 54036fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 54046fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 54056fa01ccdSSowmini Varadhan kfree(data); 54066fa01ccdSSowmini Varadhan return -ENOMEM; 54076fa01ccdSSowmini Varadhan } 54086fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 54096fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 54106fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 54116fa01ccdSSowmini Varadhan 54126fa01ccdSSowmini Varadhan if (pos + fsize > off) { 54136fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 54146fa01ccdSSowmini Varadhan 54156fa01ccdSSowmini Varadhan if (pos < off) { 54166fa01ccdSSowmini Varadhan /* Split frag. 54176fa01ccdSSowmini Varadhan * We have two variants in this case: 54186fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 54196fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 54206fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 54216fa01ccdSSowmini Varadhan * where splitting is expensive. 54226fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 54236fa01ccdSSowmini Varadhan */ 54246fa01ccdSSowmini Varadhan shinfo->frags[0].page_offset += off - pos; 54256fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 54266fa01ccdSSowmini Varadhan } 54276fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 54286fa01ccdSSowmini Varadhan k++; 54296fa01ccdSSowmini Varadhan } 54306fa01ccdSSowmini Varadhan pos += fsize; 54316fa01ccdSSowmini Varadhan } 54326fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 54336fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 54346fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 54356fa01ccdSSowmini Varadhan 54366fa01ccdSSowmini Varadhan if (k == 0) { 54376fa01ccdSSowmini Varadhan /* split line is in frag list */ 54386fa01ccdSSowmini Varadhan pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask); 54396fa01ccdSSowmini Varadhan } 54406fa01ccdSSowmini Varadhan skb_release_data(skb); 54416fa01ccdSSowmini Varadhan 54426fa01ccdSSowmini Varadhan skb->head = data; 54436fa01ccdSSowmini Varadhan skb->head_frag = 0; 54446fa01ccdSSowmini Varadhan skb->data = data; 54456fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 54466fa01ccdSSowmini Varadhan skb->end = size; 54476fa01ccdSSowmini Varadhan #else 54486fa01ccdSSowmini Varadhan skb->end = skb->head + size; 54496fa01ccdSSowmini Varadhan #endif 54506fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 54516fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 54526fa01ccdSSowmini Varadhan skb->cloned = 0; 54536fa01ccdSSowmini Varadhan skb->hdr_len = 0; 54546fa01ccdSSowmini Varadhan skb->nohdr = 0; 54556fa01ccdSSowmini Varadhan skb->len -= off; 54566fa01ccdSSowmini Varadhan skb->data_len = skb->len; 54576fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 54586fa01ccdSSowmini Varadhan return 0; 54596fa01ccdSSowmini Varadhan } 54606fa01ccdSSowmini Varadhan 54616fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 54626fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 54636fa01ccdSSowmini Varadhan { 54646fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 54656fa01ccdSSowmini Varadhan 54666fa01ccdSSowmini Varadhan if (len < headlen) 54676fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 54686fa01ccdSSowmini Varadhan else 54696fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 54706fa01ccdSSowmini Varadhan } 54716fa01ccdSSowmini Varadhan 54726fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 54736fa01ccdSSowmini Varadhan * a new skb 54746fa01ccdSSowmini Varadhan */ 54756fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 54766fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 54776fa01ccdSSowmini Varadhan { 54786fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 54796fa01ccdSSowmini Varadhan 54806fa01ccdSSowmini Varadhan if (!clone) 54816fa01ccdSSowmini Varadhan return NULL; 54826fa01ccdSSowmini Varadhan 54836fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 54846fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 54856fa01ccdSSowmini Varadhan kfree_skb(clone); 54866fa01ccdSSowmini Varadhan return NULL; 54876fa01ccdSSowmini Varadhan } 54886fa01ccdSSowmini Varadhan return clone; 54896fa01ccdSSowmini Varadhan } 54906fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 5491c8c8b127SEric Dumazet 5492c8c8b127SEric Dumazet /** 5493c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 5494c8c8b127SEric Dumazet * @skb: buffer 5495c8c8b127SEric Dumazet * 5496c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 5497c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 5498c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 5499c8c8b127SEric Dumazet * truesize. 5500c8c8b127SEric Dumazet * Notes: 5501c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 5502c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 5503c8c8b127SEric Dumazet */ 5504c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 5505c8c8b127SEric Dumazet { 55063174fed9SEric Dumazet if (skb->data_len) { 55073174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 5508c8c8b127SEric Dumazet skb_cloned(skb)) 5509c8c8b127SEric Dumazet return; 5510c8c8b127SEric Dumazet 5511c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 5512c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 55133174fed9SEric Dumazet } 55143174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 55153174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 55163174fed9SEric Dumazet * their truesize. 55173174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 55183174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 55193174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 5520c8c8b127SEric Dumazet */ 5521c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 5522c8c8b127SEric Dumazet } 5523