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; 82df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 83df5042f4SFlorian Westphal static struct kmem_cache *skbuff_ext_cache __ro_after_init; 84df5042f4SFlorian Westphal #endif 855f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 865f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /** 89f05de73bSJean Sacren * skb_panic - private function for out-of-line support 901da177e4SLinus Torvalds * @skb: buffer 911da177e4SLinus Torvalds * @sz: size 92f05de73bSJean Sacren * @addr: address 9399d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 941da177e4SLinus Torvalds * 95f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 96f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 97f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 98f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 991da177e4SLinus Torvalds */ 100f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 10199d5851eSJames Hogan const char msg[]) 1021da177e4SLinus Torvalds { 103e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 10499d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1054305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 10626095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1071da177e4SLinus Torvalds BUG(); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 110f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1111da177e4SLinus Torvalds { 112f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 115f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 116f05de73bSJean Sacren { 117f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 118f05de73bSJean Sacren } 119c93bdd0eSMel Gorman 120c93bdd0eSMel Gorman /* 121c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 122c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 123c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 124c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 125c93bdd0eSMel Gorman * memory is free 126c93bdd0eSMel Gorman */ 127c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 128c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 12961c5e88aSstephen hemminger 13061c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 13161c5e88aSstephen hemminger unsigned long ip, bool *pfmemalloc) 132c93bdd0eSMel Gorman { 133c93bdd0eSMel Gorman void *obj; 134c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 135c93bdd0eSMel Gorman 136c93bdd0eSMel Gorman /* 137c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 138c93bdd0eSMel Gorman * to the reserves, fail. 139c93bdd0eSMel Gorman */ 140c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 141c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 142c93bdd0eSMel Gorman node); 143c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 144c93bdd0eSMel Gorman goto out; 145c93bdd0eSMel Gorman 146c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 147c93bdd0eSMel Gorman ret_pfmemalloc = true; 148c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 149c93bdd0eSMel Gorman 150c93bdd0eSMel Gorman out: 151c93bdd0eSMel Gorman if (pfmemalloc) 152c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 153c93bdd0eSMel Gorman 154c93bdd0eSMel Gorman return obj; 155c93bdd0eSMel Gorman } 156c93bdd0eSMel Gorman 1571da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1581da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1591da177e4SLinus Torvalds * [BEEP] leaks. 1601da177e4SLinus Torvalds * 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /** 164d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1651da177e4SLinus Torvalds * @size: size to allocate 1661da177e4SLinus Torvalds * @gfp_mask: allocation mask 167c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 168c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 169c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 170c93bdd0eSMel Gorman * allocations in case the data is required for writeback 171b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1721da177e4SLinus Torvalds * 1731da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 17494b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 17594b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 1761da177e4SLinus Torvalds * 1771da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1781da177e4SLinus Torvalds * %GFP_ATOMIC. 1791da177e4SLinus Torvalds */ 180dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 181c93bdd0eSMel Gorman int flags, int node) 1821da177e4SLinus Torvalds { 183e18b890bSChristoph Lameter struct kmem_cache *cache; 1844947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1851da177e4SLinus Torvalds struct sk_buff *skb; 1861da177e4SLinus Torvalds u8 *data; 187c93bdd0eSMel Gorman bool pfmemalloc; 1881da177e4SLinus Torvalds 189c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 190c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 191c93bdd0eSMel Gorman 192c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 193c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1948798b3fbSHerbert Xu 1951da177e4SLinus Torvalds /* Get the HEAD */ 196b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1971da177e4SLinus Torvalds if (!skb) 1981da177e4SLinus Torvalds goto out; 199ec7d2f2cSEric Dumazet prefetchw(skb); 2001da177e4SLinus Torvalds 20187fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 20287fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 20387fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 20487fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 20587fb4b7bSEric Dumazet */ 206bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 20787fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 208c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2091da177e4SLinus Torvalds if (!data) 2101da177e4SLinus Torvalds goto nodata; 21187fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 21287fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 21387fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 21487fb4b7bSEric Dumazet */ 21587fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 216ec7d2f2cSEric Dumazet prefetchw(data + size); 2171da177e4SLinus Torvalds 218ca0605a7SArnaldo Carvalho de Melo /* 219c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 220c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 221c8005785SJohannes Berg * the tail pointer in struct sk_buff! 222ca0605a7SArnaldo Carvalho de Melo */ 223ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 22487fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 22587fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 226c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 22763354797SReshetova, Elena refcount_set(&skb->users, 1); 2281da177e4SLinus Torvalds skb->head = data; 2291da177e4SLinus Torvalds skb->data = data; 23027a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2314305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 23235d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 23335d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 23419633e12SStephen Hemminger 2354947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2364947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 237ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2384947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 2394947d3efSBenjamin LaHaise 240c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 241d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 2421da177e4SLinus Torvalds 243d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 244d0bf4a9eSEric Dumazet 245d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 2462638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 1); 247d179cd12SDavid S. Miller 2486ffe75ebSEric Dumazet fclones->skb2.fclone = SKB_FCLONE_CLONE; 249d179cd12SDavid S. Miller } 2501da177e4SLinus Torvalds out: 2511da177e4SLinus Torvalds return skb; 2521da177e4SLinus Torvalds nodata: 2538798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2541da177e4SLinus Torvalds skb = NULL; 2551da177e4SLinus Torvalds goto out; 2561da177e4SLinus Torvalds } 257b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds /** 2602ea2f62cSEric Dumazet * __build_skb - build a network buffer 261b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2622ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 263b2b5ce9dSEric Dumazet * 264b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 265deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 2662ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 2672ea2f62cSEric Dumazet * or vmalloc() 268b2b5ce9dSEric Dumazet * The return is the new skb buffer. 269b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 270b2b5ce9dSEric Dumazet * Notes : 271b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 272b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 273b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 274b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 275b2b5ce9dSEric Dumazet * before giving packet to stack. 276b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 277b2b5ce9dSEric Dumazet */ 2782ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 279b2b5ce9dSEric Dumazet { 280b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 281b2b5ce9dSEric Dumazet struct sk_buff *skb; 282d3836f21SEric Dumazet unsigned int size = frag_size ? : ksize(data); 283b2b5ce9dSEric Dumazet 284b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 285b2b5ce9dSEric Dumazet if (!skb) 286b2b5ce9dSEric Dumazet return NULL; 287b2b5ce9dSEric Dumazet 288d3836f21SEric Dumazet size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 289b2b5ce9dSEric Dumazet 290b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 291b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 29263354797SReshetova, Elena refcount_set(&skb->users, 1); 293b2b5ce9dSEric Dumazet skb->head = data; 294b2b5ce9dSEric Dumazet skb->data = data; 295b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 296b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 29735d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 29835d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 299b2b5ce9dSEric Dumazet 300b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 301b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 302b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 303b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 304b2b5ce9dSEric Dumazet 305b2b5ce9dSEric Dumazet return skb; 306b2b5ce9dSEric Dumazet } 3072ea2f62cSEric Dumazet 3082ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3092ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3102ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3112ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3122ea2f62cSEric Dumazet */ 3132ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3142ea2f62cSEric Dumazet { 3152ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3162ea2f62cSEric Dumazet 3172ea2f62cSEric Dumazet if (skb && frag_size) { 3182ea2f62cSEric Dumazet skb->head_frag = 1; 3192f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3202ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3212ea2f62cSEric Dumazet } 3222ea2f62cSEric Dumazet return skb; 3232ea2f62cSEric Dumazet } 324b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 325b2b5ce9dSEric Dumazet 326795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE 64 327795bb1c0SJesper Dangaard Brouer 328795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache { 329795bb1c0SJesper Dangaard Brouer struct page_frag_cache page; 330e0d7924aSAlexey Dobriyan unsigned int skb_count; 331795bb1c0SJesper Dangaard Brouer void *skb_cache[NAPI_SKB_CACHE_SIZE]; 332795bb1c0SJesper Dangaard Brouer }; 333795bb1c0SJesper Dangaard Brouer 334b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 335795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 336ffde7328SAlexander Duyck 337ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 338ffde7328SAlexander Duyck { 339b63ae8caSAlexander Duyck struct page_frag_cache *nc; 340ffde7328SAlexander Duyck unsigned long flags; 341ffde7328SAlexander Duyck void *data; 342ffde7328SAlexander Duyck 343ffde7328SAlexander Duyck local_irq_save(flags); 3449451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 3458c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, fragsz, gfp_mask); 3466f532612SEric Dumazet local_irq_restore(flags); 3476f532612SEric Dumazet return data; 3486f532612SEric Dumazet } 349c93bdd0eSMel Gorman 350c93bdd0eSMel Gorman /** 351c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 352c93bdd0eSMel Gorman * @fragsz: fragment size 353c93bdd0eSMel Gorman * 354c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 355c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 356c93bdd0eSMel Gorman */ 357c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 358c93bdd0eSMel Gorman { 359453f85d4SMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC); 360c93bdd0eSMel Gorman } 3616f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3626f532612SEric Dumazet 363ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 364ffde7328SAlexander Duyck { 365795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3669451980aSAlexander Duyck 3678c2dd3e4SAlexander Duyck return page_frag_alloc(&nc->page, fragsz, gfp_mask); 368ffde7328SAlexander Duyck } 369ffde7328SAlexander Duyck 370ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 371ffde7328SAlexander Duyck { 372453f85d4SMel Gorman return __napi_alloc_frag(fragsz, GFP_ATOMIC); 373ffde7328SAlexander Duyck } 374ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 375ffde7328SAlexander Duyck 3766f532612SEric Dumazet /** 377fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 378fd11a83dSAlexander Duyck * @dev: network device to receive on 379d7499160SMasanari Iida * @len: length to allocate 380fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 381fd11a83dSAlexander Duyck * 382fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 383fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 384fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 385fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 386fd11a83dSAlexander Duyck * 387fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 388fd11a83dSAlexander Duyck */ 3899451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 3909451980aSAlexander Duyck gfp_t gfp_mask) 391fd11a83dSAlexander Duyck { 392b63ae8caSAlexander Duyck struct page_frag_cache *nc; 3939451980aSAlexander Duyck unsigned long flags; 394fd11a83dSAlexander Duyck struct sk_buff *skb; 3959451980aSAlexander Duyck bool pfmemalloc; 3969451980aSAlexander Duyck void *data; 397fd11a83dSAlexander Duyck 3989451980aSAlexander Duyck len += NET_SKB_PAD; 399fd11a83dSAlexander Duyck 4009451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 401d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 402a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 403a080e7bdSAlexander Duyck if (!skb) 404a080e7bdSAlexander Duyck goto skb_fail; 405a080e7bdSAlexander Duyck goto skb_success; 406a080e7bdSAlexander Duyck } 4079451980aSAlexander Duyck 4089451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4099451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4109451980aSAlexander Duyck 4119451980aSAlexander Duyck if (sk_memalloc_socks()) 4129451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4139451980aSAlexander Duyck 4149451980aSAlexander Duyck local_irq_save(flags); 4159451980aSAlexander Duyck 4169451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4178c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 4189451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 4199451980aSAlexander Duyck 4209451980aSAlexander Duyck local_irq_restore(flags); 4219451980aSAlexander Duyck 4229451980aSAlexander Duyck if (unlikely(!data)) 4239451980aSAlexander Duyck return NULL; 4249451980aSAlexander Duyck 4259451980aSAlexander Duyck skb = __build_skb(data, len); 4269451980aSAlexander Duyck if (unlikely(!skb)) { 427181edb2bSAlexander Duyck skb_free_frag(data); 4289451980aSAlexander Duyck return NULL; 4299451980aSAlexander Duyck } 4309451980aSAlexander Duyck 4319451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 4329451980aSAlexander Duyck if (pfmemalloc) 4339451980aSAlexander Duyck skb->pfmemalloc = 1; 4349451980aSAlexander Duyck skb->head_frag = 1; 4359451980aSAlexander Duyck 436a080e7bdSAlexander Duyck skb_success: 4378af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4387b2e497aSChristoph Hellwig skb->dev = dev; 439fd11a83dSAlexander Duyck 440a080e7bdSAlexander Duyck skb_fail: 4418af27456SChristoph Hellwig return skb; 4428af27456SChristoph Hellwig } 443b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4441da177e4SLinus Torvalds 445fd11a83dSAlexander Duyck /** 446fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 447fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 448d7499160SMasanari Iida * @len: length to allocate 449fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 450fd11a83dSAlexander Duyck * 451fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 452fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 453fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 454fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 455fd11a83dSAlexander Duyck * 456fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 457fd11a83dSAlexander Duyck */ 4589451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 4599451980aSAlexander Duyck gfp_t gfp_mask) 460fd11a83dSAlexander Duyck { 461795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 462fd11a83dSAlexander Duyck struct sk_buff *skb; 4639451980aSAlexander Duyck void *data; 464fd11a83dSAlexander Duyck 4659451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 466fd11a83dSAlexander Duyck 4679451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 468d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 469a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 470a080e7bdSAlexander Duyck if (!skb) 471a080e7bdSAlexander Duyck goto skb_fail; 472a080e7bdSAlexander Duyck goto skb_success; 473a080e7bdSAlexander Duyck } 4749451980aSAlexander Duyck 4759451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4769451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4779451980aSAlexander Duyck 4789451980aSAlexander Duyck if (sk_memalloc_socks()) 4799451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4809451980aSAlexander Duyck 4818c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 4829451980aSAlexander Duyck if (unlikely(!data)) 4839451980aSAlexander Duyck return NULL; 4849451980aSAlexander Duyck 4859451980aSAlexander Duyck skb = __build_skb(data, len); 4869451980aSAlexander Duyck if (unlikely(!skb)) { 487181edb2bSAlexander Duyck skb_free_frag(data); 4889451980aSAlexander Duyck return NULL; 4899451980aSAlexander Duyck } 4909451980aSAlexander Duyck 4919451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 492795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 4939451980aSAlexander Duyck skb->pfmemalloc = 1; 4949451980aSAlexander Duyck skb->head_frag = 1; 4959451980aSAlexander Duyck 496a080e7bdSAlexander Duyck skb_success: 497fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 498fd11a83dSAlexander Duyck skb->dev = napi->dev; 499fd11a83dSAlexander Duyck 500a080e7bdSAlexander Duyck skb_fail: 501fd11a83dSAlexander Duyck return skb; 502fd11a83dSAlexander Duyck } 503fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 504fd11a83dSAlexander Duyck 505654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 50650269e19SEric Dumazet int size, unsigned int truesize) 507654bed16SPeter Zijlstra { 508654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 509654bed16SPeter Zijlstra skb->len += size; 510654bed16SPeter Zijlstra skb->data_len += size; 51150269e19SEric Dumazet skb->truesize += truesize; 512654bed16SPeter Zijlstra } 513654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 514654bed16SPeter Zijlstra 515f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 516f8e617e1SJason Wang unsigned int truesize) 517f8e617e1SJason Wang { 518f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 519f8e617e1SJason Wang 520f8e617e1SJason Wang skb_frag_size_add(frag, size); 521f8e617e1SJason Wang skb->len += size; 522f8e617e1SJason Wang skb->data_len += size; 523f8e617e1SJason Wang skb->truesize += truesize; 524f8e617e1SJason Wang } 525f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 526f8e617e1SJason Wang 52727b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5281da177e4SLinus Torvalds { 529bd8a7036SEric Dumazet kfree_skb_list(*listp); 53027b437c8SHerbert Xu *listp = NULL; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 53327b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 53427b437c8SHerbert Xu { 53527b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 53627b437c8SHerbert Xu } 53727b437c8SHerbert Xu 5381da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds struct sk_buff *list; 5411da177e4SLinus Torvalds 542fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5431da177e4SLinus Torvalds skb_get(list); 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 546d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 547d3836f21SEric Dumazet { 548181edb2bSAlexander Duyck unsigned char *head = skb->head; 549181edb2bSAlexander Duyck 550d3836f21SEric Dumazet if (skb->head_frag) 551181edb2bSAlexander Duyck skb_free_frag(head); 552d3836f21SEric Dumazet else 553181edb2bSAlexander Duyck kfree(head); 554d3836f21SEric Dumazet } 555d3836f21SEric Dumazet 5565bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5571da177e4SLinus Torvalds { 558ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5591da177e4SLinus Torvalds int i; 560ff04a771SEric Dumazet 561ff04a771SEric Dumazet if (skb->cloned && 562ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 563ff04a771SEric Dumazet &shinfo->dataref)) 564ff04a771SEric Dumazet return; 565ff04a771SEric Dumazet 566ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 567ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 5681da177e4SLinus Torvalds 569ff04a771SEric Dumazet if (shinfo->frag_list) 570ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 5711da177e4SLinus Torvalds 5721f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 573d3836f21SEric Dumazet skb_free_head(skb); 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds /* 5771da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 5781da177e4SLinus Torvalds */ 5792d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 5801da177e4SLinus Torvalds { 581d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 582d179cd12SDavid S. Miller 583d179cd12SDavid S. Miller switch (skb->fclone) { 584d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 5851da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 5866ffe75ebSEric Dumazet return; 587d179cd12SDavid S. Miller 588d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 589d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 5906ffe75ebSEric Dumazet 5916ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 5926ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 5936ffe75ebSEric Dumazet * while here, branch prediction will be good. 5946ffe75ebSEric Dumazet */ 5952638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 5966ffe75ebSEric Dumazet goto fastpath; 597d179cd12SDavid S. Miller break; 598d179cd12SDavid S. Miller 5996ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 600d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 601d179cd12SDavid S. Miller break; 6023ff50b79SStephen Hemminger } 6032638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 6046ffe75ebSEric Dumazet return; 6056ffe75ebSEric Dumazet fastpath: 6066ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6090a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 6101da177e4SLinus Torvalds { 611adf30907SEric Dumazet skb_dst_drop(skb); 6121da177e4SLinus Torvalds if (skb->destructor) { 6139c2b3328SStephen Hemminger WARN_ON(in_irq()); 6141da177e4SLinus Torvalds skb->destructor(skb); 6151da177e4SLinus Torvalds } 616a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 617cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 6182fc72c7bSKOVACS Krisztian #endif 619df5042f4SFlorian Westphal skb_ext_put(skb); 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); 799df5042f4SFlorian Westphal __skb_ext_copy(new, old); 800b1937227SEric Dumazet __nf_copy(new, old, false); 8016aa895b0SPatrick McHardy 802b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 803b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 804b1937227SEric Dumazet */ 805b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 80606021292SEliezer Tamir 807b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 808b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 809b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 810b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 811b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 812b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 813b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 814b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 815b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 816b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 817b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 818b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 819b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 820b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 821b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 822b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 823b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 824b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 825b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 826b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 827b1937227SEric Dumazet #endif 828e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 829b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 83006021292SEliezer Tamir #endif 8312bd82484SEric Dumazet #ifdef CONFIG_XPS 8322bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 8332bd82484SEric Dumazet #endif 834b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 835b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 836b1937227SEric Dumazet #endif 837b1937227SEric Dumazet 838dec18810SHerbert Xu } 839dec18810SHerbert Xu 84082c49a35SHerbert Xu /* 84182c49a35SHerbert Xu * You should not add any new code to this function. Add it to 84282c49a35SHerbert Xu * __copy_skb_header above instead. 84382c49a35SHerbert Xu */ 844e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds #define C(x) n->x = skb->x 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds n->next = n->prev = NULL; 8491da177e4SLinus Torvalds n->sk = NULL; 850dec18810SHerbert Xu __copy_skb_header(n, skb); 851dec18810SHerbert Xu 8521da177e4SLinus Torvalds C(len); 8531da177e4SLinus Torvalds C(data_len); 8543e6b3b2eSAlexey Dobriyan C(mac_len); 855334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 85602f1c89dSPaul Moore n->cloned = 1; 8571da177e4SLinus Torvalds n->nohdr = 0; 858b13dda9fSEric Dumazet n->peeked = 0; 859e78bfb07SStefano Brivio C(pfmemalloc); 8601da177e4SLinus Torvalds n->destructor = NULL; 8611da177e4SLinus Torvalds C(tail); 8621da177e4SLinus Torvalds C(end); 86302f1c89dSPaul Moore C(head); 864d3836f21SEric Dumazet C(head_frag); 86502f1c89dSPaul Moore C(data); 86602f1c89dSPaul Moore C(truesize); 86763354797SReshetova, Elena refcount_set(&n->users, 1); 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 8701da177e4SLinus Torvalds skb->cloned = 1; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds return n; 873e0053ec0SHerbert Xu #undef C 874e0053ec0SHerbert Xu } 875e0053ec0SHerbert Xu 876e0053ec0SHerbert Xu /** 877e0053ec0SHerbert Xu * skb_morph - morph one skb into another 878e0053ec0SHerbert Xu * @dst: the skb to receive the contents 879e0053ec0SHerbert Xu * @src: the skb to supply the contents 880e0053ec0SHerbert Xu * 881e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 882e0053ec0SHerbert Xu * supplied by the user. 883e0053ec0SHerbert Xu * 884e0053ec0SHerbert Xu * The target skb is returned upon exit. 885e0053ec0SHerbert Xu */ 886e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 887e0053ec0SHerbert Xu { 8882d4baff8SHerbert Xu skb_release_all(dst); 889e0053ec0SHerbert Xu return __skb_clone(dst, src); 890e0053ec0SHerbert Xu } 891e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 892e0053ec0SHerbert Xu 8936f89dbceSSowmini Varadhan int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 894a91dbff5SWillem de Bruijn { 895a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 896a91dbff5SWillem de Bruijn struct user_struct *user; 897a91dbff5SWillem de Bruijn 898a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 899a91dbff5SWillem de Bruijn return 0; 900a91dbff5SWillem de Bruijn 901a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 902a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 903a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 904a91dbff5SWillem de Bruijn 905a91dbff5SWillem de Bruijn do { 906a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 907a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 908a91dbff5SWillem de Bruijn if (new_pg > max_pg) 909a91dbff5SWillem de Bruijn return -ENOBUFS; 910a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 911a91dbff5SWillem de Bruijn old_pg); 912a91dbff5SWillem de Bruijn 913a91dbff5SWillem de Bruijn if (!mmp->user) { 914a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 915a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 916a91dbff5SWillem de Bruijn } else { 917a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 918a91dbff5SWillem de Bruijn } 919a91dbff5SWillem de Bruijn 920a91dbff5SWillem de Bruijn return 0; 921a91dbff5SWillem de Bruijn } 9226f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_account_pinned_pages); 923a91dbff5SWillem de Bruijn 9246f89dbceSSowmini Varadhan void mm_unaccount_pinned_pages(struct mmpin *mmp) 925a91dbff5SWillem de Bruijn { 926a91dbff5SWillem de Bruijn if (mmp->user) { 927a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 928a91dbff5SWillem de Bruijn free_uid(mmp->user); 929a91dbff5SWillem de Bruijn } 930a91dbff5SWillem de Bruijn } 9316f89dbceSSowmini Varadhan EXPORT_SYMBOL_GPL(mm_unaccount_pinned_pages); 932a91dbff5SWillem de Bruijn 93352267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size) 93452267790SWillem de Bruijn { 93552267790SWillem de Bruijn struct ubuf_info *uarg; 93652267790SWillem de Bruijn struct sk_buff *skb; 93752267790SWillem de Bruijn 93852267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 93952267790SWillem de Bruijn 94052267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 94152267790SWillem de Bruijn if (!skb) 94252267790SWillem de Bruijn return NULL; 94352267790SWillem de Bruijn 94452267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 94552267790SWillem de Bruijn uarg = (void *)skb->cb; 946a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 947a91dbff5SWillem de Bruijn 948a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 949a91dbff5SWillem de Bruijn kfree_skb(skb); 950a91dbff5SWillem de Bruijn return NULL; 951a91dbff5SWillem de Bruijn } 95252267790SWillem de Bruijn 95352267790SWillem de Bruijn uarg->callback = sock_zerocopy_callback; 9544ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 9554ab6c99dSWillem de Bruijn uarg->len = 1; 9564ab6c99dSWillem de Bruijn uarg->bytelen = size; 95752267790SWillem de Bruijn uarg->zerocopy = 1; 958c1d1b437SEric Dumazet refcount_set(&uarg->refcnt, 1); 95952267790SWillem de Bruijn sock_hold(sk); 96052267790SWillem de Bruijn 96152267790SWillem de Bruijn return uarg; 96252267790SWillem de Bruijn } 96352267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc); 96452267790SWillem de Bruijn 96552267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 96652267790SWillem de Bruijn { 96752267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 96852267790SWillem de Bruijn } 96952267790SWillem de Bruijn 9704ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size, 9714ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 9724ab6c99dSWillem de Bruijn { 9734ab6c99dSWillem de Bruijn if (uarg) { 9744ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 9754ab6c99dSWillem de Bruijn u32 bytelen, next; 9764ab6c99dSWillem de Bruijn 9774ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 9784ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 9794ab6c99dSWillem de Bruijn */ 9804ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 9814ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 9824ab6c99dSWillem de Bruijn return NULL; 9834ab6c99dSWillem de Bruijn } 9844ab6c99dSWillem de Bruijn 9854ab6c99dSWillem de Bruijn bytelen = uarg->bytelen + size; 9864ab6c99dSWillem de Bruijn if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 9874ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 9884ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 9894ab6c99dSWillem de Bruijn goto new_alloc; 9904ab6c99dSWillem de Bruijn return NULL; 9914ab6c99dSWillem de Bruijn } 9924ab6c99dSWillem de Bruijn 9934ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 9944ab6c99dSWillem de Bruijn if ((u32)(uarg->id + uarg->len) == next) { 995a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) 996a91dbff5SWillem de Bruijn return NULL; 9974ab6c99dSWillem de Bruijn uarg->len++; 9984ab6c99dSWillem de Bruijn uarg->bytelen = bytelen; 9994ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1000db5bce32SEric Dumazet sock_zerocopy_get(uarg); 10014ab6c99dSWillem de Bruijn return uarg; 10024ab6c99dSWillem de Bruijn } 10034ab6c99dSWillem de Bruijn } 10044ab6c99dSWillem de Bruijn 10054ab6c99dSWillem de Bruijn new_alloc: 10064ab6c99dSWillem de Bruijn return sock_zerocopy_alloc(sk, size); 10074ab6c99dSWillem de Bruijn } 10084ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc); 10094ab6c99dSWillem de Bruijn 10104ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 10114ab6c99dSWillem de Bruijn { 10124ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 10134ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 10144ab6c99dSWillem de Bruijn u64 sum_len; 10154ab6c99dSWillem de Bruijn 10164ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 10174ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 10184ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 10194ab6c99dSWillem de Bruijn 10204ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 10214ab6c99dSWillem de Bruijn return false; 10224ab6c99dSWillem de Bruijn 10234ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 10244ab6c99dSWillem de Bruijn return false; 10254ab6c99dSWillem de Bruijn 10264ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 10274ab6c99dSWillem de Bruijn return true; 10284ab6c99dSWillem de Bruijn } 10294ab6c99dSWillem de Bruijn 103052267790SWillem de Bruijn void sock_zerocopy_callback(struct ubuf_info *uarg, bool success) 103152267790SWillem de Bruijn { 10324ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 103352267790SWillem de Bruijn struct sock_exterr_skb *serr; 103452267790SWillem de Bruijn struct sock *sk = skb->sk; 10354ab6c99dSWillem de Bruijn struct sk_buff_head *q; 10364ab6c99dSWillem de Bruijn unsigned long flags; 10374ab6c99dSWillem de Bruijn u32 lo, hi; 10384ab6c99dSWillem de Bruijn u16 len; 103952267790SWillem de Bruijn 1040ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1041ccaffff1SWillem de Bruijn 10424ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 10434ab6c99dSWillem de Bruijn * so do not queue a completion notification 10444ab6c99dSWillem de Bruijn */ 10454ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 104652267790SWillem de Bruijn goto release; 104752267790SWillem de Bruijn 10484ab6c99dSWillem de Bruijn len = uarg->len; 10494ab6c99dSWillem de Bruijn lo = uarg->id; 10504ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 10514ab6c99dSWillem de Bruijn 105252267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 105352267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 105452267790SWillem de Bruijn serr->ee.ee_errno = 0; 105552267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 10564ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 10574ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 105852267790SWillem de Bruijn if (!success) 105952267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 106052267790SWillem de Bruijn 10614ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 10624ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 10634ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 10644ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 10654ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 10664ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 106752267790SWillem de Bruijn skb = NULL; 10684ab6c99dSWillem de Bruijn } 10694ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 107052267790SWillem de Bruijn 107152267790SWillem de Bruijn sk->sk_error_report(sk); 107252267790SWillem de Bruijn 107352267790SWillem de Bruijn release: 107452267790SWillem de Bruijn consume_skb(skb); 107552267790SWillem de Bruijn sock_put(sk); 107652267790SWillem de Bruijn } 107752267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback); 107852267790SWillem de Bruijn 107952267790SWillem de Bruijn void sock_zerocopy_put(struct ubuf_info *uarg) 108052267790SWillem de Bruijn { 1081c1d1b437SEric Dumazet if (uarg && refcount_dec_and_test(&uarg->refcnt)) { 108252267790SWillem de Bruijn if (uarg->callback) 108352267790SWillem de Bruijn uarg->callback(uarg, uarg->zerocopy); 108452267790SWillem de Bruijn else 108552267790SWillem de Bruijn consume_skb(skb_from_uarg(uarg)); 108652267790SWillem de Bruijn } 108752267790SWillem de Bruijn } 108852267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put); 108952267790SWillem de Bruijn 109052900d22SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg, bool have_uref) 109152267790SWillem de Bruijn { 109252267790SWillem de Bruijn if (uarg) { 109352267790SWillem de Bruijn struct sock *sk = skb_from_uarg(uarg)->sk; 109452267790SWillem de Bruijn 109552267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 10964ab6c99dSWillem de Bruijn uarg->len--; 109752267790SWillem de Bruijn 109852900d22SWillem de Bruijn if (have_uref) 109952267790SWillem de Bruijn sock_zerocopy_put(uarg); 110052267790SWillem de Bruijn } 110152267790SWillem de Bruijn } 110252267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort); 110352267790SWillem de Bruijn 110452267790SWillem de Bruijn extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb, 110552267790SWillem de Bruijn struct iov_iter *from, size_t length); 110652267790SWillem de Bruijn 1107b5947e5dSWillem de Bruijn int skb_zerocopy_iter_dgram(struct sk_buff *skb, struct msghdr *msg, int len) 1108b5947e5dSWillem de Bruijn { 1109b5947e5dSWillem de Bruijn return __zerocopy_sg_from_iter(skb->sk, skb, &msg->msg_iter, len); 1110b5947e5dSWillem de Bruijn } 1111b5947e5dSWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_dgram); 1112b5947e5dSWillem de Bruijn 111352267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 111452267790SWillem de Bruijn struct msghdr *msg, int len, 111552267790SWillem de Bruijn struct ubuf_info *uarg) 111652267790SWillem de Bruijn { 11174ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 111852267790SWillem de Bruijn struct iov_iter orig_iter = msg->msg_iter; 111952267790SWillem de Bruijn int err, orig_len = skb->len; 112052267790SWillem de Bruijn 11214ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 11224ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 11234ab6c99dSWillem de Bruijn */ 11244ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 11254ab6c99dSWillem de Bruijn return -EEXIST; 11264ab6c99dSWillem de Bruijn 112752267790SWillem de Bruijn err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 112852267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 112954d43117SWillem de Bruijn struct sock *save_sk = skb->sk; 113054d43117SWillem de Bruijn 113152267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 113252267790SWillem de Bruijn msg->msg_iter = orig_iter; 113354d43117SWillem de Bruijn skb->sk = sk; 113452267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 113554d43117SWillem de Bruijn skb->sk = save_sk; 113652267790SWillem de Bruijn return err; 113752267790SWillem de Bruijn } 113852267790SWillem de Bruijn 113952900d22SWillem de Bruijn skb_zcopy_set(skb, uarg, NULL); 114052267790SWillem de Bruijn return skb->len - orig_len; 114152267790SWillem de Bruijn } 114252267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 114352267790SWillem de Bruijn 11441f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 114552267790SWillem de Bruijn gfp_t gfp_mask) 114652267790SWillem de Bruijn { 114752267790SWillem de Bruijn if (skb_zcopy(orig)) { 114852267790SWillem de Bruijn if (skb_zcopy(nskb)) { 114952267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 115052267790SWillem de Bruijn if (!gfp_mask) { 115152267790SWillem de Bruijn WARN_ON_ONCE(1); 115252267790SWillem de Bruijn return -ENOMEM; 115352267790SWillem de Bruijn } 115452267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 115552267790SWillem de Bruijn return 0; 115652267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 115752267790SWillem de Bruijn return -EIO; 115852267790SWillem de Bruijn } 115952900d22SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig), NULL); 116052267790SWillem de Bruijn } 116152267790SWillem de Bruijn return 0; 116252267790SWillem de Bruijn } 116352267790SWillem de Bruijn 11642c53040fSBen Hutchings /** 11652c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 116648c83012SMichael S. Tsirkin * @skb: the skb to modify 116748c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 116848c83012SMichael S. Tsirkin * 116948c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 117048c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 117148c83012SMichael S. Tsirkin * to userspace pages. 117248c83012SMichael S. Tsirkin * 117348c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 117448c83012SMichael S. Tsirkin * %GFP_ATOMIC. 117548c83012SMichael S. Tsirkin * 117648c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 117748c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 117848c83012SMichael S. Tsirkin */ 117948c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1180a6686f2fSShirley Ma { 1181a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1182a6686f2fSShirley Ma struct page *page, *head = NULL; 11833ece7826SWillem de Bruijn int i, new_frags; 11843ece7826SWillem de Bruijn u32 d_off; 1185a6686f2fSShirley Ma 11863ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 11873ece7826SWillem de Bruijn return -EINVAL; 11883ece7826SWillem de Bruijn 1189f72c4ac6SWillem de Bruijn if (!num_frags) 1190f72c4ac6SWillem de Bruijn goto release; 1191f72c4ac6SWillem de Bruijn 11923ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 11933ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 119402756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1195a6686f2fSShirley Ma if (!page) { 1196a6686f2fSShirley Ma while (head) { 119740dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1198a6686f2fSShirley Ma put_page(head); 1199a6686f2fSShirley Ma head = next; 1200a6686f2fSShirley Ma } 1201a6686f2fSShirley Ma return -ENOMEM; 1202a6686f2fSShirley Ma } 12033ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 12043ece7826SWillem de Bruijn head = page; 12053ece7826SWillem de Bruijn } 12063ece7826SWillem de Bruijn 12073ece7826SWillem de Bruijn page = head; 12083ece7826SWillem de Bruijn d_off = 0; 12093ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 12103ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 12113ece7826SWillem de Bruijn u32 p_off, p_len, copied; 12123ece7826SWillem de Bruijn struct page *p; 12133ece7826SWillem de Bruijn u8 *vaddr; 1214c613c209SWillem de Bruijn 1215c613c209SWillem de Bruijn skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f), 1216c613c209SWillem de Bruijn p, p_off, p_len, copied) { 12173ece7826SWillem de Bruijn u32 copy, done = 0; 1218c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 12193ece7826SWillem de Bruijn 12203ece7826SWillem de Bruijn while (done < p_len) { 12213ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 12223ece7826SWillem de Bruijn d_off = 0; 12233ece7826SWillem de Bruijn page = (struct page *)page_private(page); 12243ece7826SWillem de Bruijn } 12253ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 12263ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 12273ece7826SWillem de Bruijn vaddr + p_off + done, copy); 12283ece7826SWillem de Bruijn done += copy; 12293ece7826SWillem de Bruijn d_off += copy; 12303ece7826SWillem de Bruijn } 123151c56b00SEric Dumazet kunmap_atomic(vaddr); 1232c613c209SWillem de Bruijn } 1233a6686f2fSShirley Ma } 1234a6686f2fSShirley Ma 1235a6686f2fSShirley Ma /* skb frags release userspace buffers */ 123602756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1237a8605c60SIan Campbell skb_frag_unref(skb, i); 1238a6686f2fSShirley Ma 1239a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 12403ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 12413ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 124240dadff2SSunghan Suh head = (struct page *)page_private(head); 1243a6686f2fSShirley Ma } 12443ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 12453ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 124648c83012SMichael S. Tsirkin 1247b90ddd56SWillem de Bruijn release: 12481f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1249a6686f2fSShirley Ma return 0; 1250a6686f2fSShirley Ma } 1251dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1252a6686f2fSShirley Ma 1253e0053ec0SHerbert Xu /** 1254e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1255e0053ec0SHerbert Xu * @skb: buffer to clone 1256e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1257e0053ec0SHerbert Xu * 1258e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1259e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1260e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1261e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1262e0053ec0SHerbert Xu * 1263e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1264e0053ec0SHerbert Xu * %GFP_ATOMIC. 1265e0053ec0SHerbert Xu */ 1266e0053ec0SHerbert Xu 1267e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1268e0053ec0SHerbert Xu { 1269d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1270d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1271d0bf4a9eSEric Dumazet skb1); 12726ffe75ebSEric Dumazet struct sk_buff *n; 1273e0053ec0SHerbert Xu 127470008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1275a6686f2fSShirley Ma return NULL; 1276a6686f2fSShirley Ma 1277e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 12782638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 12796ffe75ebSEric Dumazet n = &fclones->skb2; 12802638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1281e0053ec0SHerbert Xu } else { 1282c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1283c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1284c93bdd0eSMel Gorman 1285e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1286e0053ec0SHerbert Xu if (!n) 1287e0053ec0SHerbert Xu return NULL; 1288fe55f6d5SVegard Nossum 1289e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1290e0053ec0SHerbert Xu } 1291e0053ec0SHerbert Xu 1292e0053ec0SHerbert Xu return __skb_clone(n, skb); 12931da177e4SLinus Torvalds } 1294b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 12951da177e4SLinus Torvalds 1296b0768a86SToshiaki Makita void skb_headers_offset_update(struct sk_buff *skb, int off) 1297f5b17294SPravin B Shelar { 1298030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1299030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1300030737bcSEric Dumazet skb->csum_start += off; 1301f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1302f5b17294SPravin B Shelar skb->transport_header += off; 1303f5b17294SPravin B Shelar skb->network_header += off; 1304f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1305f5b17294SPravin B Shelar skb->mac_header += off; 1306f5b17294SPravin B Shelar skb->inner_transport_header += off; 1307f5b17294SPravin B Shelar skb->inner_network_header += off; 1308aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1309f5b17294SPravin B Shelar } 1310b0768a86SToshiaki Makita EXPORT_SYMBOL(skb_headers_offset_update); 1311f5b17294SPravin B Shelar 131208303c18SIlya Lesokhin void skb_copy_header(struct sk_buff *new, const struct sk_buff *old) 13131da177e4SLinus Torvalds { 1314dec18810SHerbert Xu __copy_skb_header(new, old); 1315dec18810SHerbert Xu 13167967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 13177967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 13187967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 13191da177e4SLinus Torvalds } 132008303c18SIlya Lesokhin EXPORT_SYMBOL(skb_copy_header); 13211da177e4SLinus Torvalds 1322c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1323c93bdd0eSMel Gorman { 1324c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1325c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1326c93bdd0eSMel Gorman return 0; 1327c93bdd0eSMel Gorman } 1328c93bdd0eSMel Gorman 13291da177e4SLinus Torvalds /** 13301da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 13311da177e4SLinus Torvalds * @skb: buffer to copy 13321da177e4SLinus Torvalds * @gfp_mask: allocation priority 13331da177e4SLinus Torvalds * 13341da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 13351da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 13361da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 13371da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 13381da177e4SLinus Torvalds * 13391da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 13401da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 13411da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 13421da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 13431da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 13441da177e4SLinus Torvalds */ 13451da177e4SLinus Torvalds 1346dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 13471da177e4SLinus Torvalds { 13486602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1349ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1350c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1351c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 13526602cebbSEric Dumazet 13531da177e4SLinus Torvalds if (!n) 13541da177e4SLinus Torvalds return NULL; 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds /* Set the data pointer */ 13571da177e4SLinus Torvalds skb_reserve(n, headerlen); 13581da177e4SLinus Torvalds /* Set the tail pointer and length */ 13591da177e4SLinus Torvalds skb_put(n, skb->len); 13601da177e4SLinus Torvalds 13619f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)); 13621da177e4SLinus Torvalds 136308303c18SIlya Lesokhin skb_copy_header(n, skb); 13641da177e4SLinus Torvalds return n; 13651da177e4SLinus Torvalds } 1366b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /** 1369bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 13701da177e4SLinus Torvalds * @skb: buffer to copy 1371117632e6SEric Dumazet * @headroom: headroom of new skb 13721da177e4SLinus Torvalds * @gfp_mask: allocation priority 1373bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1374bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1375bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 13761da177e4SLinus Torvalds * 13771da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 13781da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 13791da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 13801da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 13811da177e4SLinus Torvalds * or the pointer to the buffer on success. 13821da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 13831da177e4SLinus Torvalds */ 13841da177e4SLinus Torvalds 1385bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1386bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 13871da177e4SLinus Torvalds { 1388117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1389bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1390bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 13916602cebbSEric Dumazet 13921da177e4SLinus Torvalds if (!n) 13931da177e4SLinus Torvalds goto out; 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds /* Set the data pointer */ 1396117632e6SEric Dumazet skb_reserve(n, headroom); 13971da177e4SLinus Torvalds /* Set the tail pointer and length */ 13981da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 13991da177e4SLinus Torvalds /* Copy the bytes */ 1400d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 14011da177e4SLinus Torvalds 140225f484a6SHerbert Xu n->truesize += skb->data_len; 14031da177e4SLinus Torvalds n->data_len = skb->data_len; 14041da177e4SLinus Torvalds n->len = skb->len; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 14071da177e4SLinus Torvalds int i; 14081da177e4SLinus Torvalds 14091f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 14101f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 14111511022cSDan Carpenter kfree_skb(n); 14121511022cSDan Carpenter n = NULL; 1413a6686f2fSShirley Ma goto out; 1414a6686f2fSShirley Ma } 14151da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14161da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1417ea2ab693SIan Campbell skb_frag_ref(skb, i); 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 142221dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 14231da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 14241da177e4SLinus Torvalds skb_clone_fraglist(n); 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 142708303c18SIlya Lesokhin skb_copy_header(n, skb); 14281da177e4SLinus Torvalds out: 14291da177e4SLinus Torvalds return n; 14301da177e4SLinus Torvalds } 1431bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds /** 14341da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 14351da177e4SLinus Torvalds * @skb: buffer to reallocate 14361da177e4SLinus Torvalds * @nhead: room to add at head 14371da177e4SLinus Torvalds * @ntail: room to add at tail 14381da177e4SLinus Torvalds * @gfp_mask: allocation priority 14391da177e4SLinus Torvalds * 1440bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1441bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 14421da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 14431da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 14441da177e4SLinus Torvalds * 14451da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 14461da177e4SLinus Torvalds * reloaded after call to this function. 14471da177e4SLinus Torvalds */ 14481da177e4SLinus Torvalds 144986a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1450dd0fc66fSAl Viro gfp_t gfp_mask) 14511da177e4SLinus Torvalds { 1452158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1453158f323bSEric Dumazet int size = osize + nhead + ntail; 14541da177e4SLinus Torvalds long off; 1455158f323bSEric Dumazet u8 *data; 14561da177e4SLinus Torvalds 14574edd87adSHerbert Xu BUG_ON(nhead < 0); 14584edd87adSHerbert Xu 14599f77fad3STim Hansen BUG_ON(skb_shared(skb)); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 14621da177e4SLinus Torvalds 1463c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1464c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1465c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1466c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 14671da177e4SLinus Torvalds if (!data) 14681da177e4SLinus Torvalds goto nodata; 146987151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 14726602cebbSEric Dumazet * optimized for the cases when header is void. 14736602cebbSEric Dumazet */ 14746602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 14756602cebbSEric Dumazet 14766602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 14776602cebbSEric Dumazet skb_shinfo(skb), 1478fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 14791da177e4SLinus Torvalds 14803e24591aSAlexander Duyck /* 14813e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 14823e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 14833e24591aSAlexander Duyck * be since all we did is relocate the values 14843e24591aSAlexander Duyck */ 14853e24591aSAlexander Duyck if (skb_cloned(skb)) { 148670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1487a6686f2fSShirley Ma goto nofrags; 14881f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 1489c1d1b437SEric Dumazet refcount_inc(&skb_uarg(skb)->refcnt); 14901da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1491ea2ab693SIan Campbell skb_frag_ref(skb, i); 14921da177e4SLinus Torvalds 149321dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 14941da177e4SLinus Torvalds skb_clone_fraglist(skb); 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds skb_release_data(skb); 14973e24591aSAlexander Duyck } else { 14983e24591aSAlexander Duyck skb_free_head(skb); 14991fd63041SEric Dumazet } 15001da177e4SLinus Torvalds off = (data + nhead) - skb->head; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds skb->head = data; 1503d3836f21SEric Dumazet skb->head_frag = 0; 15041da177e4SLinus Torvalds skb->data += off; 15054305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 15064305b541SArnaldo Carvalho de Melo skb->end = size; 150756eb8882SPatrick McHardy off = nhead; 15084305b541SArnaldo Carvalho de Melo #else 15094305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 151056eb8882SPatrick McHardy #endif 151127a884dcSArnaldo Carvalho de Melo skb->tail += off; 1512b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 15131da177e4SLinus Torvalds skb->cloned = 0; 1514334a8132SPatrick McHardy skb->hdr_len = 0; 15151da177e4SLinus Torvalds skb->nohdr = 0; 15161da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1517158f323bSEric Dumazet 1518de8f3a83SDaniel Borkmann skb_metadata_clear(skb); 1519de8f3a83SDaniel Borkmann 1520158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1521158f323bSEric Dumazet * For the moment, we really care of rx path, or 1522158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1523158f323bSEric Dumazet */ 1524158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1525158f323bSEric Dumazet skb->truesize += size - osize; 1526158f323bSEric Dumazet 15271da177e4SLinus Torvalds return 0; 15281da177e4SLinus Torvalds 1529a6686f2fSShirley Ma nofrags: 1530a6686f2fSShirley Ma kfree(data); 15311da177e4SLinus Torvalds nodata: 15321da177e4SLinus Torvalds return -ENOMEM; 15331da177e4SLinus Torvalds } 1534b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 15391da177e4SLinus Torvalds { 15401da177e4SLinus Torvalds struct sk_buff *skb2; 15411da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds if (delta <= 0) 15441da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 15451da177e4SLinus Torvalds else { 15461da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 15471da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 15481da177e4SLinus Torvalds GFP_ATOMIC)) { 15491da177e4SLinus Torvalds kfree_skb(skb2); 15501da177e4SLinus Torvalds skb2 = NULL; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds return skb2; 15541da177e4SLinus Torvalds } 1555b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /** 15581da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 15591da177e4SLinus Torvalds * @skb: buffer to copy 15601da177e4SLinus Torvalds * @newheadroom: new free bytes at head 15611da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 15621da177e4SLinus Torvalds * @gfp_mask: allocation priority 15631da177e4SLinus Torvalds * 15641da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 15651da177e4SLinus Torvalds * allocate additional space. 15661da177e4SLinus Torvalds * 15671da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 15681da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 15691da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 15701da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 15711da177e4SLinus Torvalds * 15721da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 15731da177e4SLinus Torvalds * is called from an interrupt. 15741da177e4SLinus Torvalds */ 15751da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 157686a76cafSVictor Fusco int newheadroom, int newtailroom, 1577dd0fc66fSAl Viro gfp_t gfp_mask) 15781da177e4SLinus Torvalds { 15791da177e4SLinus Torvalds /* 15801da177e4SLinus Torvalds * Allocate the copy buffer 15811da177e4SLinus Torvalds */ 1582c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1583c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1584c93bdd0eSMel Gorman NUMA_NO_NODE); 1585efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 15861da177e4SLinus Torvalds int head_copy_len, head_copy_off; 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds if (!n) 15891da177e4SLinus Torvalds return NULL; 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds skb_reserve(n, newheadroom); 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* Set the tail pointer and length */ 15941da177e4SLinus Torvalds skb_put(n, skb->len); 15951da177e4SLinus Torvalds 1596efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 15971da177e4SLinus Torvalds head_copy_off = 0; 15981da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 15991da177e4SLinus Torvalds head_copy_len = newheadroom; 16001da177e4SLinus Torvalds else 16011da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds /* Copy the linear header and data. */ 16049f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 16059f77fad3STim Hansen skb->len + head_copy_len)); 16061da177e4SLinus Torvalds 160708303c18SIlya Lesokhin skb_copy_header(n, skb); 16081da177e4SLinus Torvalds 1609030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1610efd1e8d5SPatrick McHardy 16111da177e4SLinus Torvalds return n; 16121da177e4SLinus Torvalds } 1613b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds /** 1616cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 16171da177e4SLinus Torvalds * @skb: buffer to pad 16181da177e4SLinus Torvalds * @pad: space to pad 1619cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 16201da177e4SLinus Torvalds * 16211da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 16221da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 16231da177e4SLinus Torvalds * beyond the buffer end onto the wire. 16241da177e4SLinus Torvalds * 1625cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1626cd0a137aSFlorian Fainelli * if @free_on_error is true. 16271da177e4SLinus Torvalds */ 16281da177e4SLinus Torvalds 1629cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 16301da177e4SLinus Torvalds { 16315b057c6bSHerbert Xu int err; 16325b057c6bSHerbert Xu int ntail; 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 16355b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 16361da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 16375b057c6bSHerbert Xu return 0; 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16404305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 16415b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 16425b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 16435b057c6bSHerbert Xu if (unlikely(err)) 16445b057c6bSHerbert Xu goto free_skb; 16455b057c6bSHerbert Xu } 16465b057c6bSHerbert Xu 16475b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 16485b057c6bSHerbert Xu * to be audited. 16495b057c6bSHerbert Xu */ 16505b057c6bSHerbert Xu err = skb_linearize(skb); 16515b057c6bSHerbert Xu if (unlikely(err)) 16525b057c6bSHerbert Xu goto free_skb; 16535b057c6bSHerbert Xu 16545b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 16555b057c6bSHerbert Xu return 0; 16565b057c6bSHerbert Xu 16575b057c6bSHerbert Xu free_skb: 1658cd0a137aSFlorian Fainelli if (free_on_error) 16591da177e4SLinus Torvalds kfree_skb(skb); 16605b057c6bSHerbert Xu return err; 16611da177e4SLinus Torvalds } 1662cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 16631da177e4SLinus Torvalds 16640dde3e16SIlpo Järvinen /** 16650c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 16660c7ddf36SMathias Krause * @skb: start of the buffer to use 16670c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 16680c7ddf36SMathias Krause * @len: amount of data to add 16690c7ddf36SMathias Krause * 16700c7ddf36SMathias Krause * This function extends the used data area of the potentially 16710c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 16720c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 16730c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 16740c7ddf36SMathias Krause * returned. 16750c7ddf36SMathias Krause */ 16760c7ddf36SMathias Krause 16774df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 16780c7ddf36SMathias Krause { 16790c7ddf36SMathias Krause if (tail != skb) { 16800c7ddf36SMathias Krause skb->data_len += len; 16810c7ddf36SMathias Krause skb->len += len; 16820c7ddf36SMathias Krause } 16830c7ddf36SMathias Krause return skb_put(tail, len); 16840c7ddf36SMathias Krause } 16850c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 16860c7ddf36SMathias Krause 16870c7ddf36SMathias Krause /** 16880dde3e16SIlpo Järvinen * skb_put - add data to a buffer 16890dde3e16SIlpo Järvinen * @skb: buffer to use 16900dde3e16SIlpo Järvinen * @len: amount of data to add 16910dde3e16SIlpo Järvinen * 16920dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 16930dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 16940dde3e16SIlpo Järvinen * first byte of the extra data is returned. 16950dde3e16SIlpo Järvinen */ 16964df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 16970dde3e16SIlpo Järvinen { 16984df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 16990dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 17000dde3e16SIlpo Järvinen skb->tail += len; 17010dde3e16SIlpo Järvinen skb->len += len; 17020dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 17030dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 17040dde3e16SIlpo Järvinen return tmp; 17050dde3e16SIlpo Järvinen } 17060dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 17070dde3e16SIlpo Järvinen 17086be8ac2fSIlpo Järvinen /** 1709c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1710c2aa270aSIlpo Järvinen * @skb: buffer to use 1711c2aa270aSIlpo Järvinen * @len: amount of data to add 1712c2aa270aSIlpo Järvinen * 1713c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1714c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1715c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1716c2aa270aSIlpo Järvinen */ 1717d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 1718c2aa270aSIlpo Järvinen { 1719c2aa270aSIlpo Järvinen skb->data -= len; 1720c2aa270aSIlpo Järvinen skb->len += len; 1721c2aa270aSIlpo Järvinen if (unlikely(skb->data < skb->head)) 1722c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1723c2aa270aSIlpo Järvinen return skb->data; 1724c2aa270aSIlpo Järvinen } 1725c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1726c2aa270aSIlpo Järvinen 1727c2aa270aSIlpo Järvinen /** 17286be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 17296be8ac2fSIlpo Järvinen * @skb: buffer to use 17306be8ac2fSIlpo Järvinen * @len: amount of data to remove 17316be8ac2fSIlpo Järvinen * 17326be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 17336be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 17346be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 17356be8ac2fSIlpo Järvinen * the old data. 17366be8ac2fSIlpo Järvinen */ 1737af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 17386be8ac2fSIlpo Järvinen { 173947d29646SDavid S. Miller return skb_pull_inline(skb, len); 17406be8ac2fSIlpo Järvinen } 17416be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 17426be8ac2fSIlpo Järvinen 1743419ae74eSIlpo Järvinen /** 1744419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1745419ae74eSIlpo Järvinen * @skb: buffer to alter 1746419ae74eSIlpo Järvinen * @len: new length 1747419ae74eSIlpo Järvinen * 1748419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1749419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1750419ae74eSIlpo Järvinen * The skb must be linear. 1751419ae74eSIlpo Järvinen */ 1752419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1753419ae74eSIlpo Järvinen { 1754419ae74eSIlpo Järvinen if (skb->len > len) 1755419ae74eSIlpo Järvinen __skb_trim(skb, len); 1756419ae74eSIlpo Järvinen } 1757419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1758419ae74eSIlpo Järvinen 17593cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 17601da177e4SLinus Torvalds */ 17611da177e4SLinus Torvalds 17623cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 17631da177e4SLinus Torvalds { 176427b437c8SHerbert Xu struct sk_buff **fragp; 176527b437c8SHerbert Xu struct sk_buff *frag; 17661da177e4SLinus Torvalds int offset = skb_headlen(skb); 17671da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 17681da177e4SLinus Torvalds int i; 176927b437c8SHerbert Xu int err; 177027b437c8SHerbert Xu 177127b437c8SHerbert Xu if (skb_cloned(skb) && 177227b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 177327b437c8SHerbert Xu return err; 17741da177e4SLinus Torvalds 1775f4d26fb3SHerbert Xu i = 0; 1776f4d26fb3SHerbert Xu if (offset >= len) 1777f4d26fb3SHerbert Xu goto drop_pages; 1778f4d26fb3SHerbert Xu 1779f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 17809e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 178127b437c8SHerbert Xu 178227b437c8SHerbert Xu if (end < len) { 17831da177e4SLinus Torvalds offset = end; 178427b437c8SHerbert Xu continue; 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17879e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 178827b437c8SHerbert Xu 1789f4d26fb3SHerbert Xu drop_pages: 179027b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 179127b437c8SHerbert Xu 179227b437c8SHerbert Xu for (; i < nfrags; i++) 1793ea2ab693SIan Campbell skb_frag_unref(skb, i); 179427b437c8SHerbert Xu 179521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 179627b437c8SHerbert Xu skb_drop_fraglist(skb); 1797f4d26fb3SHerbert Xu goto done; 179827b437c8SHerbert Xu } 179927b437c8SHerbert Xu 180027b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 180127b437c8SHerbert Xu fragp = &frag->next) { 180227b437c8SHerbert Xu int end = offset + frag->len; 180327b437c8SHerbert Xu 180427b437c8SHerbert Xu if (skb_shared(frag)) { 180527b437c8SHerbert Xu struct sk_buff *nfrag; 180627b437c8SHerbert Xu 180727b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 180827b437c8SHerbert Xu if (unlikely(!nfrag)) 180927b437c8SHerbert Xu return -ENOMEM; 181027b437c8SHerbert Xu 181127b437c8SHerbert Xu nfrag->next = frag->next; 181285bb2a60SEric Dumazet consume_skb(frag); 181327b437c8SHerbert Xu frag = nfrag; 181427b437c8SHerbert Xu *fragp = frag; 181527b437c8SHerbert Xu } 181627b437c8SHerbert Xu 181727b437c8SHerbert Xu if (end < len) { 181827b437c8SHerbert Xu offset = end; 181927b437c8SHerbert Xu continue; 182027b437c8SHerbert Xu } 182127b437c8SHerbert Xu 182227b437c8SHerbert Xu if (end > len && 182327b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 182427b437c8SHerbert Xu return err; 182527b437c8SHerbert Xu 182627b437c8SHerbert Xu if (frag->next) 182727b437c8SHerbert Xu skb_drop_list(&frag->next); 182827b437c8SHerbert Xu break; 182927b437c8SHerbert Xu } 183027b437c8SHerbert Xu 1831f4d26fb3SHerbert Xu done: 183227b437c8SHerbert Xu if (len > skb_headlen(skb)) { 18331da177e4SLinus Torvalds skb->data_len -= skb->len - len; 18341da177e4SLinus Torvalds skb->len = len; 18351da177e4SLinus Torvalds } else { 18361da177e4SLinus Torvalds skb->len = len; 18371da177e4SLinus Torvalds skb->data_len = 0; 183827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 1841c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1842c21b48ccSEric Dumazet skb_condense(skb); 18431da177e4SLinus Torvalds return 0; 18441da177e4SLinus Torvalds } 1845b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 18461da177e4SLinus Torvalds 184788078d98SEric Dumazet /* Note : use pskb_trim_rcsum() instead of calling this directly 184888078d98SEric Dumazet */ 184988078d98SEric Dumazet int pskb_trim_rcsum_slow(struct sk_buff *skb, unsigned int len) 185088078d98SEric Dumazet { 185188078d98SEric Dumazet if (skb->ip_summed == CHECKSUM_COMPLETE) { 185288078d98SEric Dumazet int delta = skb->len - len; 185388078d98SEric Dumazet 1854d55bef50SDimitris Michailidis skb->csum = csum_block_sub(skb->csum, 1855d55bef50SDimitris Michailidis skb_checksum(skb, len, delta, 0), 1856d55bef50SDimitris Michailidis len); 185788078d98SEric Dumazet } 185888078d98SEric Dumazet return __pskb_trim(skb, len); 185988078d98SEric Dumazet } 186088078d98SEric Dumazet EXPORT_SYMBOL(pskb_trim_rcsum_slow); 186188078d98SEric Dumazet 18621da177e4SLinus Torvalds /** 18631da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 18641da177e4SLinus Torvalds * @skb: buffer to reallocate 18651da177e4SLinus Torvalds * @delta: number of bytes to advance tail 18661da177e4SLinus Torvalds * 18671da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 18681da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 18691da177e4SLinus Torvalds * data from fragmented part. 18701da177e4SLinus Torvalds * 18711da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 18721da177e4SLinus Torvalds * 18731da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 18741da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 18751da177e4SLinus Torvalds * 18761da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 18771da177e4SLinus Torvalds * reloaded after call to this function. 18781da177e4SLinus Torvalds */ 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 18811da177e4SLinus Torvalds * when it is necessary. 18821da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 18831da177e4SLinus Torvalds * 2. It may change skb pointers. 18841da177e4SLinus Torvalds * 18851da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 18861da177e4SLinus Torvalds */ 1887af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 18881da177e4SLinus Torvalds { 18891da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 18901da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 18911da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 18921da177e4SLinus Torvalds */ 18934305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 18961da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 18971da177e4SLinus Torvalds GFP_ATOMIC)) 18981da177e4SLinus Torvalds return NULL; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19019f77fad3STim Hansen BUG_ON(skb_copy_bits(skb, skb_headlen(skb), 19029f77fad3STim Hansen skb_tail_pointer(skb), delta)); 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 19051da177e4SLinus Torvalds * size of pulled pages. Superb. 19061da177e4SLinus Torvalds */ 190721dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 19081da177e4SLinus Torvalds goto pull_pages; 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 19111da177e4SLinus Torvalds eat = delta; 19121da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19139e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 19149e903e08SEric Dumazet 19159e903e08SEric Dumazet if (size >= eat) 19161da177e4SLinus Torvalds goto pull_pages; 19179e903e08SEric Dumazet eat -= size; 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 192109001b03SWenhua Shi * Certainly, it is possible to add an offset to skb data, 19221da177e4SLinus Torvalds * but taking into account that pulling is expected to 19231da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 19241da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 19251da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 19261da177e4SLinus Torvalds */ 19271da177e4SLinus Torvalds if (eat) { 19281da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 19291da177e4SLinus Torvalds struct sk_buff *clone = NULL; 19301da177e4SLinus Torvalds struct sk_buff *insp = NULL; 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds do { 19331da177e4SLinus Torvalds if (list->len <= eat) { 19341da177e4SLinus Torvalds /* Eaten as whole. */ 19351da177e4SLinus Torvalds eat -= list->len; 19361da177e4SLinus Torvalds list = list->next; 19371da177e4SLinus Torvalds insp = list; 19381da177e4SLinus Torvalds } else { 19391da177e4SLinus Torvalds /* Eaten partially. */ 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds if (skb_shared(list)) { 19421da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 19431da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 19441da177e4SLinus Torvalds if (!clone) 19451da177e4SLinus Torvalds return NULL; 19461da177e4SLinus Torvalds insp = list->next; 19471da177e4SLinus Torvalds list = clone; 19481da177e4SLinus Torvalds } else { 19491da177e4SLinus Torvalds /* This may be pulled without 19501da177e4SLinus Torvalds * problems. */ 19511da177e4SLinus Torvalds insp = list; 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 19541da177e4SLinus Torvalds kfree_skb(clone); 19551da177e4SLinus Torvalds return NULL; 19561da177e4SLinus Torvalds } 19571da177e4SLinus Torvalds break; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds } while (eat); 19601da177e4SLinus Torvalds 19611da177e4SLinus Torvalds /* Free pulled out fragments. */ 19621da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 19631da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 19641da177e4SLinus Torvalds kfree_skb(list); 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds /* And insert new clone at head. */ 19671da177e4SLinus Torvalds if (clone) { 19681da177e4SLinus Torvalds clone->next = list; 19691da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds pull_pages: 19751da177e4SLinus Torvalds eat = delta; 19761da177e4SLinus Torvalds k = 0; 19771da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19789e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 19799e903e08SEric Dumazet 19809e903e08SEric Dumazet if (size <= eat) { 1981ea2ab693SIan Campbell skb_frag_unref(skb, i); 19829e903e08SEric Dumazet eat -= size; 19831da177e4SLinus Torvalds } else { 19841da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 19851da177e4SLinus Torvalds if (eat) { 19861da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 19879e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 19883ccc6c6fSlinzhang if (!i) 19893ccc6c6fSlinzhang goto end; 19901da177e4SLinus Torvalds eat = 0; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds k++; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 19961da177e4SLinus Torvalds 19973ccc6c6fSlinzhang end: 19981da177e4SLinus Torvalds skb->tail += delta; 19991da177e4SLinus Torvalds skb->data_len -= delta; 20001da177e4SLinus Torvalds 20011f8b977aSWillem de Bruijn if (!skb->data_len) 20021f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 20031f8b977aSWillem de Bruijn 200427a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 20051da177e4SLinus Torvalds } 2006b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 20071da177e4SLinus Torvalds 200822019b17SEric Dumazet /** 200922019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 201022019b17SEric Dumazet * @skb: source skb 201122019b17SEric Dumazet * @offset: offset in source 201222019b17SEric Dumazet * @to: destination buffer 201322019b17SEric Dumazet * @len: number of bytes to copy 201422019b17SEric Dumazet * 201522019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 201622019b17SEric Dumazet * destination buffer. 201722019b17SEric Dumazet * 201822019b17SEric Dumazet * CAUTION ! : 201922019b17SEric Dumazet * If its prototype is ever changed, 202022019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 202122019b17SEric Dumazet * since it is called from BPF assembly code. 202222019b17SEric Dumazet */ 20231da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 20241da177e4SLinus Torvalds { 20251a028e50SDavid S. Miller int start = skb_headlen(skb); 2026fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2027fbb398a8SDavid S. Miller int i, copy; 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds if (offset > (int)skb->len - len) 20301da177e4SLinus Torvalds goto fault; 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds /* Copy header. */ 20331a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 20341da177e4SLinus Torvalds if (copy > len) 20351da177e4SLinus Torvalds copy = len; 2036d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 20371da177e4SLinus Torvalds if ((len -= copy) == 0) 20381da177e4SLinus Torvalds return 0; 20391da177e4SLinus Torvalds offset += copy; 20401da177e4SLinus Torvalds to += copy; 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20441a028e50SDavid S. Miller int end; 204551c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 20461da177e4SLinus Torvalds 2047547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20481a028e50SDavid S. Miller 204951c56b00SEric Dumazet end = start + skb_frag_size(f); 20501da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2051c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2052c613c209SWillem de Bruijn struct page *p; 20531da177e4SLinus Torvalds u8 *vaddr; 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds if (copy > len) 20561da177e4SLinus Torvalds copy = len; 20571da177e4SLinus Torvalds 2058c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2059c613c209SWillem de Bruijn f->page_offset + offset - start, 2060c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2061c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2062c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 206351c56b00SEric Dumazet kunmap_atomic(vaddr); 2064c613c209SWillem de Bruijn } 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds if ((len -= copy) == 0) 20671da177e4SLinus Torvalds return 0; 20681da177e4SLinus Torvalds offset += copy; 20691da177e4SLinus Torvalds to += copy; 20701da177e4SLinus Torvalds } 20711a028e50SDavid S. Miller start = end; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds 2074fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20751a028e50SDavid S. Miller int end; 20761da177e4SLinus Torvalds 2077547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20781a028e50SDavid S. Miller 2079fbb398a8SDavid S. Miller end = start + frag_iter->len; 20801da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20811da177e4SLinus Torvalds if (copy > len) 20821da177e4SLinus Torvalds copy = len; 2083fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 20841da177e4SLinus Torvalds goto fault; 20851da177e4SLinus Torvalds if ((len -= copy) == 0) 20861da177e4SLinus Torvalds return 0; 20871da177e4SLinus Torvalds offset += copy; 20881da177e4SLinus Torvalds to += copy; 20891da177e4SLinus Torvalds } 20901a028e50SDavid S. Miller start = end; 20911da177e4SLinus Torvalds } 2092a6686f2fSShirley Ma 20931da177e4SLinus Torvalds if (!len) 20941da177e4SLinus Torvalds return 0; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds fault: 20971da177e4SLinus Torvalds return -EFAULT; 20981da177e4SLinus Torvalds } 2099b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 21001da177e4SLinus Torvalds 21019c55e01cSJens Axboe /* 21029c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 21039c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 21049c55e01cSJens Axboe */ 21059c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 21069c55e01cSJens Axboe { 21078b9d3728SJarek Poplawski put_page(spd->pages[i]); 21088b9d3728SJarek Poplawski } 21099c55e01cSJens Axboe 2110a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 21114fb66994SJarek Poplawski unsigned int *offset, 211218aafc62SEric Dumazet struct sock *sk) 21138b9d3728SJarek Poplawski { 21145640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 21158b9d3728SJarek Poplawski 21165640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 21178b9d3728SJarek Poplawski return NULL; 21184fb66994SJarek Poplawski 21195640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 21204fb66994SJarek Poplawski 21215640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 21225640f768SEric Dumazet page_address(page) + *offset, *len); 21235640f768SEric Dumazet *offset = pfrag->offset; 21245640f768SEric Dumazet pfrag->offset += *len; 21254fb66994SJarek Poplawski 21265640f768SEric Dumazet return pfrag->page; 21279c55e01cSJens Axboe } 21289c55e01cSJens Axboe 212941c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 213041c73a0dSEric Dumazet struct page *page, 213141c73a0dSEric Dumazet unsigned int offset) 213241c73a0dSEric Dumazet { 213341c73a0dSEric Dumazet return spd->nr_pages && 213441c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 213541c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 213641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 213741c73a0dSEric Dumazet } 213841c73a0dSEric Dumazet 21399c55e01cSJens Axboe /* 21409c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 21419c55e01cSJens Axboe */ 2142a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 214335f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 21444fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 214518aafc62SEric Dumazet bool linear, 21467a67e56fSJarek Poplawski struct sock *sk) 21479c55e01cSJens Axboe { 214841c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2149a108d5f3SDavid S. Miller return true; 21509c55e01cSJens Axboe 21518b9d3728SJarek Poplawski if (linear) { 215218aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 21538b9d3728SJarek Poplawski if (!page) 2154a108d5f3SDavid S. Miller return true; 215541c73a0dSEric Dumazet } 215641c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 215741c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2158a108d5f3SDavid S. Miller return false; 215941c73a0dSEric Dumazet } 21608b9d3728SJarek Poplawski get_page(page); 21619c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 21624fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 21639c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 21649c55e01cSJens Axboe spd->nr_pages++; 21658b9d3728SJarek Poplawski 2166a108d5f3SDavid S. Miller return false; 21679c55e01cSJens Axboe } 21689c55e01cSJens Axboe 2169a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 21702870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 217118aafc62SEric Dumazet unsigned int *len, 2172d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 217335f3d14dSJens Axboe struct sock *sk, 217435f3d14dSJens Axboe struct pipe_inode_info *pipe) 21759c55e01cSJens Axboe { 21762870c43dSOctavian Purdila if (!*len) 2177a108d5f3SDavid S. Miller return true; 21789c55e01cSJens Axboe 21792870c43dSOctavian Purdila /* skip this segment if already processed */ 21802870c43dSOctavian Purdila if (*off >= plen) { 21812870c43dSOctavian Purdila *off -= plen; 2182a108d5f3SDavid S. Miller return false; 21832870c43dSOctavian Purdila } 21842870c43dSOctavian Purdila 21852870c43dSOctavian Purdila /* ignore any bits we already processed */ 21869ca1b22dSEric Dumazet poff += *off; 21879ca1b22dSEric Dumazet plen -= *off; 21882870c43dSOctavian Purdila *off = 0; 21892870c43dSOctavian Purdila 219018aafc62SEric Dumazet do { 219118aafc62SEric Dumazet unsigned int flen = min(*len, plen); 21922870c43dSOctavian Purdila 219318aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 219418aafc62SEric Dumazet linear, sk)) 2195a108d5f3SDavid S. Miller return true; 219618aafc62SEric Dumazet poff += flen; 219718aafc62SEric Dumazet plen -= flen; 21982870c43dSOctavian Purdila *len -= flen; 219918aafc62SEric Dumazet } while (*len && plen); 22002870c43dSOctavian Purdila 2201a108d5f3SDavid S. Miller return false; 2202db43a282SOctavian Purdila } 22039c55e01cSJens Axboe 22049c55e01cSJens Axboe /* 2205a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 22062870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 22079c55e01cSJens Axboe */ 2208a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 220935f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 221035f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 22112870c43dSOctavian Purdila { 22122870c43dSOctavian Purdila int seg; 2213fa9835e5STom Herbert struct sk_buff *iter; 22149c55e01cSJens Axboe 22151d0c0b32SEric Dumazet /* map the linear part : 22162996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 22172996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 22182996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 22199c55e01cSJens Axboe */ 22202870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 22212870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 22222870c43dSOctavian Purdila skb_headlen(skb), 222318aafc62SEric Dumazet offset, len, spd, 22243a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 22251d0c0b32SEric Dumazet sk, pipe)) 2226a108d5f3SDavid S. Miller return true; 22279c55e01cSJens Axboe 22289c55e01cSJens Axboe /* 22299c55e01cSJens Axboe * then map the fragments 22309c55e01cSJens Axboe */ 22319c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 22329c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 22339c55e01cSJens Axboe 2234ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 22359e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 223618aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2237a108d5f3SDavid S. Miller return true; 22389c55e01cSJens Axboe } 22399c55e01cSJens Axboe 2240fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2241fa9835e5STom Herbert if (*offset >= iter->len) { 2242fa9835e5STom Herbert *offset -= iter->len; 2243fa9835e5STom Herbert continue; 2244fa9835e5STom Herbert } 2245fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2246fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2247fa9835e5STom Herbert * case. 2248fa9835e5STom Herbert */ 2249fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2250fa9835e5STom Herbert return true; 2251fa9835e5STom Herbert } 2252fa9835e5STom Herbert 2253a108d5f3SDavid S. Miller return false; 22549c55e01cSJens Axboe } 22559c55e01cSJens Axboe 22569c55e01cSJens Axboe /* 22579c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2258fa9835e5STom Herbert * the fragments, and the frag list. 22599c55e01cSJens Axboe */ 2260a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 22619c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 226225869262SAl Viro unsigned int flags) 22639c55e01cSJens Axboe { 226441c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 226541c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 22669c55e01cSJens Axboe struct splice_pipe_desc spd = { 22679c55e01cSJens Axboe .pages = pages, 22689c55e01cSJens Axboe .partial = partial, 2269047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 227028a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 22719c55e01cSJens Axboe .spd_release = sock_spd_release, 22729c55e01cSJens Axboe }; 227335f3d14dSJens Axboe int ret = 0; 227435f3d14dSJens Axboe 2275fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 22769c55e01cSJens Axboe 2277a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 227825869262SAl Viro ret = splice_to_pipe(pipe, &spd); 22799c55e01cSJens Axboe 228035f3d14dSJens Axboe return ret; 22819c55e01cSJens Axboe } 22822b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 22839c55e01cSJens Axboe 228420bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */ 228520bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 228620bf50deSTom Herbert int len) 228720bf50deSTom Herbert { 228820bf50deSTom Herbert unsigned int orig_len = len; 228920bf50deSTom Herbert struct sk_buff *head = skb; 229020bf50deSTom Herbert unsigned short fragidx; 229120bf50deSTom Herbert int slen, ret; 229220bf50deSTom Herbert 229320bf50deSTom Herbert do_frag_list: 229420bf50deSTom Herbert 229520bf50deSTom Herbert /* Deal with head data */ 229620bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 229720bf50deSTom Herbert struct kvec kv; 229820bf50deSTom Herbert struct msghdr msg; 229920bf50deSTom Herbert 230020bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 230120bf50deSTom Herbert kv.iov_base = skb->data + offset; 2302db5980d8SJohn Fastabend kv.iov_len = slen; 230320bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 230420bf50deSTom Herbert 230520bf50deSTom Herbert ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen); 230620bf50deSTom Herbert if (ret <= 0) 230720bf50deSTom Herbert goto error; 230820bf50deSTom Herbert 230920bf50deSTom Herbert offset += ret; 231020bf50deSTom Herbert len -= ret; 231120bf50deSTom Herbert } 231220bf50deSTom Herbert 231320bf50deSTom Herbert /* All the data was skb head? */ 231420bf50deSTom Herbert if (!len) 231520bf50deSTom Herbert goto out; 231620bf50deSTom Herbert 231720bf50deSTom Herbert /* Make offset relative to start of frags */ 231820bf50deSTom Herbert offset -= skb_headlen(skb); 231920bf50deSTom Herbert 232020bf50deSTom Herbert /* Find where we are in frag list */ 232120bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 232220bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 232320bf50deSTom Herbert 232420bf50deSTom Herbert if (offset < frag->size) 232520bf50deSTom Herbert break; 232620bf50deSTom Herbert 232720bf50deSTom Herbert offset -= frag->size; 232820bf50deSTom Herbert } 232920bf50deSTom Herbert 233020bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 233120bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 233220bf50deSTom Herbert 233320bf50deSTom Herbert slen = min_t(size_t, len, frag->size - offset); 233420bf50deSTom Herbert 233520bf50deSTom Herbert while (slen) { 233620bf50deSTom Herbert ret = kernel_sendpage_locked(sk, frag->page.p, 233720bf50deSTom Herbert frag->page_offset + offset, 233820bf50deSTom Herbert slen, MSG_DONTWAIT); 233920bf50deSTom Herbert if (ret <= 0) 234020bf50deSTom Herbert goto error; 234120bf50deSTom Herbert 234220bf50deSTom Herbert len -= ret; 234320bf50deSTom Herbert offset += ret; 234420bf50deSTom Herbert slen -= ret; 234520bf50deSTom Herbert } 234620bf50deSTom Herbert 234720bf50deSTom Herbert offset = 0; 234820bf50deSTom Herbert } 234920bf50deSTom Herbert 235020bf50deSTom Herbert if (len) { 235120bf50deSTom Herbert /* Process any frag lists */ 235220bf50deSTom Herbert 235320bf50deSTom Herbert if (skb == head) { 235420bf50deSTom Herbert if (skb_has_frag_list(skb)) { 235520bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 235620bf50deSTom Herbert goto do_frag_list; 235720bf50deSTom Herbert } 235820bf50deSTom Herbert } else if (skb->next) { 235920bf50deSTom Herbert skb = skb->next; 236020bf50deSTom Herbert goto do_frag_list; 236120bf50deSTom Herbert } 236220bf50deSTom Herbert } 236320bf50deSTom Herbert 236420bf50deSTom Herbert out: 236520bf50deSTom Herbert return orig_len - len; 236620bf50deSTom Herbert 236720bf50deSTom Herbert error: 236820bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 236920bf50deSTom Herbert } 237020bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 237120bf50deSTom Herbert 2372357b40a1SHerbert Xu /** 2373357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2374357b40a1SHerbert Xu * @skb: destination buffer 2375357b40a1SHerbert Xu * @offset: offset in destination 2376357b40a1SHerbert Xu * @from: source buffer 2377357b40a1SHerbert Xu * @len: number of bytes to copy 2378357b40a1SHerbert Xu * 2379357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2380357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2381357b40a1SHerbert Xu * traversing fragment lists and such. 2382357b40a1SHerbert Xu */ 2383357b40a1SHerbert Xu 23840c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2385357b40a1SHerbert Xu { 23861a028e50SDavid S. Miller int start = skb_headlen(skb); 2387fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2388fbb398a8SDavid S. Miller int i, copy; 2389357b40a1SHerbert Xu 2390357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2391357b40a1SHerbert Xu goto fault; 2392357b40a1SHerbert Xu 23931a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2394357b40a1SHerbert Xu if (copy > len) 2395357b40a1SHerbert Xu copy = len; 239627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2397357b40a1SHerbert Xu if ((len -= copy) == 0) 2398357b40a1SHerbert Xu return 0; 2399357b40a1SHerbert Xu offset += copy; 2400357b40a1SHerbert Xu from += copy; 2401357b40a1SHerbert Xu } 2402357b40a1SHerbert Xu 2403357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2404357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 24051a028e50SDavid S. Miller int end; 2406357b40a1SHerbert Xu 2407547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24081a028e50SDavid S. Miller 24099e903e08SEric Dumazet end = start + skb_frag_size(frag); 2410357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2411c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2412c613c209SWillem de Bruijn struct page *p; 2413357b40a1SHerbert Xu u8 *vaddr; 2414357b40a1SHerbert Xu 2415357b40a1SHerbert Xu if (copy > len) 2416357b40a1SHerbert Xu copy = len; 2417357b40a1SHerbert Xu 2418c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2419c613c209SWillem de Bruijn frag->page_offset + offset - start, 2420c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2421c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2422c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 242351c56b00SEric Dumazet kunmap_atomic(vaddr); 2424c613c209SWillem de Bruijn } 2425357b40a1SHerbert Xu 2426357b40a1SHerbert Xu if ((len -= copy) == 0) 2427357b40a1SHerbert Xu return 0; 2428357b40a1SHerbert Xu offset += copy; 2429357b40a1SHerbert Xu from += copy; 2430357b40a1SHerbert Xu } 24311a028e50SDavid S. Miller start = end; 2432357b40a1SHerbert Xu } 2433357b40a1SHerbert Xu 2434fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 24351a028e50SDavid S. Miller int end; 2436357b40a1SHerbert Xu 2437547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24381a028e50SDavid S. Miller 2439fbb398a8SDavid S. Miller end = start + frag_iter->len; 2440357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2441357b40a1SHerbert Xu if (copy > len) 2442357b40a1SHerbert Xu copy = len; 2443fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 24441a028e50SDavid S. Miller from, copy)) 2445357b40a1SHerbert Xu goto fault; 2446357b40a1SHerbert Xu if ((len -= copy) == 0) 2447357b40a1SHerbert Xu return 0; 2448357b40a1SHerbert Xu offset += copy; 2449357b40a1SHerbert Xu from += copy; 2450357b40a1SHerbert Xu } 24511a028e50SDavid S. Miller start = end; 2452357b40a1SHerbert Xu } 2453357b40a1SHerbert Xu if (!len) 2454357b40a1SHerbert Xu return 0; 2455357b40a1SHerbert Xu 2456357b40a1SHerbert Xu fault: 2457357b40a1SHerbert Xu return -EFAULT; 2458357b40a1SHerbert Xu } 2459357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2460357b40a1SHerbert Xu 24611da177e4SLinus Torvalds /* Checksum skb data. */ 24622817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 24632817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 24641da177e4SLinus Torvalds { 24651a028e50SDavid S. Miller int start = skb_headlen(skb); 24661a028e50SDavid S. Miller int i, copy = start - offset; 2467fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 24681da177e4SLinus Torvalds int pos = 0; 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds /* Checksum header. */ 24711da177e4SLinus Torvalds if (copy > 0) { 24721da177e4SLinus Torvalds if (copy > len) 24731da177e4SLinus Torvalds copy = len; 24742817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 24751da177e4SLinus Torvalds if ((len -= copy) == 0) 24761da177e4SLinus Torvalds return csum; 24771da177e4SLinus Torvalds offset += copy; 24781da177e4SLinus Torvalds pos = copy; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds 24811da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 24821a028e50SDavid S. Miller int end; 248351c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 24841da177e4SLinus Torvalds 2485547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24861a028e50SDavid S. Miller 248751c56b00SEric Dumazet end = start + skb_frag_size(frag); 24881da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2489c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2490c613c209SWillem de Bruijn struct page *p; 249144bb9363SAl Viro __wsum csum2; 24921da177e4SLinus Torvalds u8 *vaddr; 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds if (copy > len) 24951da177e4SLinus Torvalds copy = len; 2496c613c209SWillem de Bruijn 2497c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2498c613c209SWillem de Bruijn frag->page_offset + offset - start, 2499c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2500c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2501c613c209SWillem de Bruijn csum2 = ops->update(vaddr + p_off, p_len, 0); 250251c56b00SEric Dumazet kunmap_atomic(vaddr); 2503c613c209SWillem de Bruijn csum = ops->combine(csum, csum2, pos, p_len); 2504c613c209SWillem de Bruijn pos += p_len; 2505c613c209SWillem de Bruijn } 2506c613c209SWillem de Bruijn 25071da177e4SLinus Torvalds if (!(len -= copy)) 25081da177e4SLinus Torvalds return csum; 25091da177e4SLinus Torvalds offset += copy; 25101da177e4SLinus Torvalds } 25111a028e50SDavid S. Miller start = end; 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 2514fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 25151a028e50SDavid S. Miller int end; 25161da177e4SLinus Torvalds 2517547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25181a028e50SDavid S. Miller 2519fbb398a8SDavid S. Miller end = start + frag_iter->len; 25201da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 25215f92a738SAl Viro __wsum csum2; 25221da177e4SLinus Torvalds if (copy > len) 25231da177e4SLinus Torvalds copy = len; 25242817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 25252817a336SDaniel Borkmann copy, 0, ops); 25262817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 25271da177e4SLinus Torvalds if ((len -= copy) == 0) 25281da177e4SLinus Torvalds return csum; 25291da177e4SLinus Torvalds offset += copy; 25301da177e4SLinus Torvalds pos += copy; 25311da177e4SLinus Torvalds } 25321a028e50SDavid S. Miller start = end; 25331da177e4SLinus Torvalds } 253409a62660SKris Katterjohn BUG_ON(len); 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds return csum; 25371da177e4SLinus Torvalds } 25382817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 25392817a336SDaniel Borkmann 25402817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 25412817a336SDaniel Borkmann int len, __wsum csum) 25422817a336SDaniel Borkmann { 25432817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2544cea80ea8SDaniel Borkmann .update = csum_partial_ext, 25452817a336SDaniel Borkmann .combine = csum_block_add_ext, 25462817a336SDaniel Borkmann }; 25472817a336SDaniel Borkmann 25482817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 25492817a336SDaniel Borkmann } 2550b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds /* Both of above in one bottle. */ 25531da177e4SLinus Torvalds 255481d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 255581d77662SAl Viro u8 *to, int len, __wsum csum) 25561da177e4SLinus Torvalds { 25571a028e50SDavid S. Miller int start = skb_headlen(skb); 25581a028e50SDavid S. Miller int i, copy = start - offset; 2559fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 25601da177e4SLinus Torvalds int pos = 0; 25611da177e4SLinus Torvalds 25621da177e4SLinus Torvalds /* Copy header. */ 25631da177e4SLinus Torvalds if (copy > 0) { 25641da177e4SLinus Torvalds if (copy > len) 25651da177e4SLinus Torvalds copy = len; 25661da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 25671da177e4SLinus Torvalds copy, csum); 25681da177e4SLinus Torvalds if ((len -= copy) == 0) 25691da177e4SLinus Torvalds return csum; 25701da177e4SLinus Torvalds offset += copy; 25711da177e4SLinus Torvalds to += copy; 25721da177e4SLinus Torvalds pos = copy; 25731da177e4SLinus Torvalds } 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 25761a028e50SDavid S. Miller int end; 25771da177e4SLinus Torvalds 2578547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25791a028e50SDavid S. Miller 25809e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 25811da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2582c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2583c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2584c613c209SWillem de Bruijn struct page *p; 25855084205fSAl Viro __wsum csum2; 25861da177e4SLinus Torvalds u8 *vaddr; 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds if (copy > len) 25891da177e4SLinus Torvalds copy = len; 2590c613c209SWillem de Bruijn 2591c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2592c613c209SWillem de Bruijn frag->page_offset + offset - start, 2593c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2594c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2595c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2596c613c209SWillem de Bruijn to + copied, 2597c613c209SWillem de Bruijn p_len, 0); 259851c56b00SEric Dumazet kunmap_atomic(vaddr); 25991da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 2600c613c209SWillem de Bruijn pos += p_len; 2601c613c209SWillem de Bruijn } 2602c613c209SWillem de Bruijn 26031da177e4SLinus Torvalds if (!(len -= copy)) 26041da177e4SLinus Torvalds return csum; 26051da177e4SLinus Torvalds offset += copy; 26061da177e4SLinus Torvalds to += copy; 26071da177e4SLinus Torvalds } 26081a028e50SDavid S. Miller start = end; 26091da177e4SLinus Torvalds } 26101da177e4SLinus Torvalds 2611fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 261281d77662SAl Viro __wsum csum2; 26131a028e50SDavid S. Miller int end; 26141da177e4SLinus Torvalds 2615547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26161a028e50SDavid S. Miller 2617fbb398a8SDavid S. Miller end = start + frag_iter->len; 26181da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 26191da177e4SLinus Torvalds if (copy > len) 26201da177e4SLinus Torvalds copy = len; 2621fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 26221a028e50SDavid S. Miller offset - start, 26231da177e4SLinus Torvalds to, copy, 0); 26241da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 26251da177e4SLinus Torvalds if ((len -= copy) == 0) 26261da177e4SLinus Torvalds return csum; 26271da177e4SLinus Torvalds offset += copy; 26281da177e4SLinus Torvalds to += copy; 26291da177e4SLinus Torvalds pos += copy; 26301da177e4SLinus Torvalds } 26311a028e50SDavid S. Miller start = end; 26321da177e4SLinus Torvalds } 263309a62660SKris Katterjohn BUG_ON(len); 26341da177e4SLinus Torvalds return csum; 26351da177e4SLinus Torvalds } 2636b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 26371da177e4SLinus Torvalds 263849f8e832SCong Wang __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len) 263949f8e832SCong Wang { 264049f8e832SCong Wang __sum16 sum; 264149f8e832SCong Wang 264249f8e832SCong Wang sum = csum_fold(skb_checksum(skb, 0, len, skb->csum)); 264314641931SCong Wang /* See comments in __skb_checksum_complete(). */ 264449f8e832SCong Wang if (likely(!sum)) { 264549f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 264649f8e832SCong Wang !skb->csum_complete_sw) 26477fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 264849f8e832SCong Wang } 264949f8e832SCong Wang if (!skb_shared(skb)) 265049f8e832SCong Wang skb->csum_valid = !sum; 265149f8e832SCong Wang return sum; 265249f8e832SCong Wang } 265349f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete_head); 265449f8e832SCong Wang 265514641931SCong Wang /* This function assumes skb->csum already holds pseudo header's checksum, 265614641931SCong Wang * which has been changed from the hardware checksum, for example, by 265714641931SCong Wang * __skb_checksum_validate_complete(). And, the original skb->csum must 265814641931SCong Wang * have been validated unsuccessfully for CHECKSUM_COMPLETE case. 265914641931SCong Wang * 266014641931SCong Wang * It returns non-zero if the recomputed checksum is still invalid, otherwise 266114641931SCong Wang * zero. The new checksum is stored back into skb->csum unless the skb is 266214641931SCong Wang * shared. 266314641931SCong Wang */ 266449f8e832SCong Wang __sum16 __skb_checksum_complete(struct sk_buff *skb) 266549f8e832SCong Wang { 266649f8e832SCong Wang __wsum csum; 266749f8e832SCong Wang __sum16 sum; 266849f8e832SCong Wang 266949f8e832SCong Wang csum = skb_checksum(skb, 0, skb->len, 0); 267049f8e832SCong Wang 267149f8e832SCong Wang sum = csum_fold(csum_add(skb->csum, csum)); 267214641931SCong Wang /* This check is inverted, because we already knew the hardware 267314641931SCong Wang * checksum is invalid before calling this function. So, if the 267414641931SCong Wang * re-computed checksum is valid instead, then we have a mismatch 267514641931SCong Wang * between the original skb->csum and skb_checksum(). This means either 267614641931SCong Wang * the original hardware checksum is incorrect or we screw up skb->csum 267714641931SCong Wang * when moving skb->data around. 267814641931SCong Wang */ 267949f8e832SCong Wang if (likely(!sum)) { 268049f8e832SCong Wang if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && 268149f8e832SCong Wang !skb->csum_complete_sw) 26827fe50ac8SCong Wang netdev_rx_csum_fault(skb->dev, skb); 268349f8e832SCong Wang } 268449f8e832SCong Wang 268549f8e832SCong Wang if (!skb_shared(skb)) { 268649f8e832SCong Wang /* Save full packet checksum */ 268749f8e832SCong Wang skb->csum = csum; 268849f8e832SCong Wang skb->ip_summed = CHECKSUM_COMPLETE; 268949f8e832SCong Wang skb->csum_complete_sw = 1; 269049f8e832SCong Wang skb->csum_valid = !sum; 269149f8e832SCong Wang } 269249f8e832SCong Wang 269349f8e832SCong Wang return sum; 269449f8e832SCong Wang } 269549f8e832SCong Wang EXPORT_SYMBOL(__skb_checksum_complete); 269649f8e832SCong Wang 26979617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 26989617813dSDavide Caratti { 26999617813dSDavide Caratti net_warn_ratelimited( 27009617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 27019617813dSDavide Caratti __func__); 27029617813dSDavide Caratti return 0; 27039617813dSDavide Caratti } 27049617813dSDavide Caratti 27059617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 27069617813dSDavide Caratti int offset, int len) 27079617813dSDavide Caratti { 27089617813dSDavide Caratti net_warn_ratelimited( 27099617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 27109617813dSDavide Caratti __func__); 27119617813dSDavide Caratti return 0; 27129617813dSDavide Caratti } 27139617813dSDavide Caratti 27149617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 27159617813dSDavide Caratti .update = warn_crc32c_csum_update, 27169617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 27179617813dSDavide Caratti }; 27189617813dSDavide Caratti 27199617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 27209617813dSDavide Caratti &default_crc32c_ops; 27219617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 27229617813dSDavide Caratti 2723af2806f8SThomas Graf /** 2724af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2725af2806f8SThomas Graf * @from: source buffer 2726af2806f8SThomas Graf * 2727af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2728af2806f8SThomas Graf * into skb_zerocopy(). 2729af2806f8SThomas Graf */ 2730af2806f8SThomas Graf unsigned int 2731af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2732af2806f8SThomas Graf { 2733af2806f8SThomas Graf unsigned int hlen = 0; 2734af2806f8SThomas Graf 2735af2806f8SThomas Graf if (!from->head_frag || 2736af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2737af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2738af2806f8SThomas Graf hlen = skb_headlen(from); 2739af2806f8SThomas Graf 2740af2806f8SThomas Graf if (skb_has_frag_list(from)) 2741af2806f8SThomas Graf hlen = from->len; 2742af2806f8SThomas Graf 2743af2806f8SThomas Graf return hlen; 2744af2806f8SThomas Graf } 2745af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2746af2806f8SThomas Graf 2747af2806f8SThomas Graf /** 2748af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2749af2806f8SThomas Graf * @to: destination buffer 27507fceb4deSMasanari Iida * @from: source buffer 2751af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2752af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2753af2806f8SThomas Graf * 2754af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2755af2806f8SThomas Graf * to the frags in the source buffer. 2756af2806f8SThomas Graf * 2757af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2758af2806f8SThomas Graf * headroom in the `to` buffer. 275936d5fe6aSZoltan Kiss * 276036d5fe6aSZoltan Kiss * Return value: 276136d5fe6aSZoltan Kiss * 0: everything is OK 276236d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 276336d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2764af2806f8SThomas Graf */ 276536d5fe6aSZoltan Kiss int 276636d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2767af2806f8SThomas Graf { 2768af2806f8SThomas Graf int i, j = 0; 2769af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 277036d5fe6aSZoltan Kiss int ret; 2771af2806f8SThomas Graf struct page *page; 2772af2806f8SThomas Graf unsigned int offset; 2773af2806f8SThomas Graf 2774af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2775af2806f8SThomas Graf 2776af2806f8SThomas Graf /* dont bother with small payloads */ 277736d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 277836d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2779af2806f8SThomas Graf 2780af2806f8SThomas Graf if (hlen) { 278136d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 278236d5fe6aSZoltan Kiss if (unlikely(ret)) 278336d5fe6aSZoltan Kiss return ret; 2784af2806f8SThomas Graf len -= hlen; 2785af2806f8SThomas Graf } else { 2786af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2787af2806f8SThomas Graf if (plen) { 2788af2806f8SThomas Graf page = virt_to_head_page(from->head); 2789af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2790af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2791af2806f8SThomas Graf get_page(page); 2792af2806f8SThomas Graf j = 1; 2793af2806f8SThomas Graf len -= plen; 2794af2806f8SThomas Graf } 2795af2806f8SThomas Graf } 2796af2806f8SThomas Graf 2797af2806f8SThomas Graf to->truesize += len + plen; 2798af2806f8SThomas Graf to->len += len + plen; 2799af2806f8SThomas Graf to->data_len += len + plen; 2800af2806f8SThomas Graf 280136d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 280236d5fe6aSZoltan Kiss skb_tx_error(from); 280336d5fe6aSZoltan Kiss return -ENOMEM; 280436d5fe6aSZoltan Kiss } 28051f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 280636d5fe6aSZoltan Kiss 2807af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2808af2806f8SThomas Graf if (!len) 2809af2806f8SThomas Graf break; 2810af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2811af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2812af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2813af2806f8SThomas Graf skb_frag_ref(to, j); 2814af2806f8SThomas Graf j++; 2815af2806f8SThomas Graf } 2816af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 281736d5fe6aSZoltan Kiss 281836d5fe6aSZoltan Kiss return 0; 2819af2806f8SThomas Graf } 2820af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2821af2806f8SThomas Graf 28221da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 28231da177e4SLinus Torvalds { 2824d3bc23e7SAl Viro __wsum csum; 28251da177e4SLinus Torvalds long csstart; 28261da177e4SLinus Torvalds 282784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 282855508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 28291da177e4SLinus Torvalds else 28301da177e4SLinus Torvalds csstart = skb_headlen(skb); 28311da177e4SLinus Torvalds 283209a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 28331da177e4SLinus Torvalds 2834d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 28351da177e4SLinus Torvalds 28361da177e4SLinus Torvalds csum = 0; 28371da177e4SLinus Torvalds if (csstart != skb->len) 28381da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 28391da177e4SLinus Torvalds skb->len - csstart, 0); 28401da177e4SLinus Torvalds 284184fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2842ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 28431da177e4SLinus Torvalds 2844d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 28451da177e4SLinus Torvalds } 28461da177e4SLinus Torvalds } 2847b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 28481da177e4SLinus Torvalds 28491da177e4SLinus Torvalds /** 28501da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 28511da177e4SLinus Torvalds * @list: list to dequeue from 28521da177e4SLinus Torvalds * 28531da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 28541da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 28551da177e4SLinus Torvalds * returned or %NULL if the list is empty. 28561da177e4SLinus Torvalds */ 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 28591da177e4SLinus Torvalds { 28601da177e4SLinus Torvalds unsigned long flags; 28611da177e4SLinus Torvalds struct sk_buff *result; 28621da177e4SLinus Torvalds 28631da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28641da177e4SLinus Torvalds result = __skb_dequeue(list); 28651da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 28661da177e4SLinus Torvalds return result; 28671da177e4SLinus Torvalds } 2868b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds /** 28711da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 28721da177e4SLinus Torvalds * @list: list to dequeue from 28731da177e4SLinus Torvalds * 28741da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 28751da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 28761da177e4SLinus Torvalds * returned or %NULL if the list is empty. 28771da177e4SLinus Torvalds */ 28781da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 28791da177e4SLinus Torvalds { 28801da177e4SLinus Torvalds unsigned long flags; 28811da177e4SLinus Torvalds struct sk_buff *result; 28821da177e4SLinus Torvalds 28831da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28841da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 28851da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 28861da177e4SLinus Torvalds return result; 28871da177e4SLinus Torvalds } 2888b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 28891da177e4SLinus Torvalds 28901da177e4SLinus Torvalds /** 28911da177e4SLinus Torvalds * skb_queue_purge - empty a list 28921da177e4SLinus Torvalds * @list: list to empty 28931da177e4SLinus Torvalds * 28941da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 28951da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 28961da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 28971da177e4SLinus Torvalds */ 28981da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 28991da177e4SLinus Torvalds { 29001da177e4SLinus Torvalds struct sk_buff *skb; 29011da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 29021da177e4SLinus Torvalds kfree_skb(skb); 29031da177e4SLinus Torvalds } 2904b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 29051da177e4SLinus Torvalds 29061da177e4SLinus Torvalds /** 29079f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 29089f5afeaeSYaogong Wang * @root: root of the rbtree to empty 2909385114deSPeter Oskolkov * Return value: the sum of truesizes of all purged skbs. 29109f5afeaeSYaogong Wang * 29119f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 29129f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 29139f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 29149f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 29159f5afeaeSYaogong Wang */ 2916385114deSPeter Oskolkov unsigned int skb_rbtree_purge(struct rb_root *root) 29179f5afeaeSYaogong Wang { 29187c90584cSEric Dumazet struct rb_node *p = rb_first(root); 2919385114deSPeter Oskolkov unsigned int sum = 0; 29209f5afeaeSYaogong Wang 29217c90584cSEric Dumazet while (p) { 29227c90584cSEric Dumazet struct sk_buff *skb = rb_entry(p, struct sk_buff, rbnode); 29237c90584cSEric Dumazet 29247c90584cSEric Dumazet p = rb_next(p); 29257c90584cSEric Dumazet rb_erase(&skb->rbnode, root); 2926385114deSPeter Oskolkov sum += skb->truesize; 29279f5afeaeSYaogong Wang kfree_skb(skb); 29287c90584cSEric Dumazet } 2929385114deSPeter Oskolkov return sum; 29309f5afeaeSYaogong Wang } 29319f5afeaeSYaogong Wang 29329f5afeaeSYaogong Wang /** 29331da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 29341da177e4SLinus Torvalds * @list: list to use 29351da177e4SLinus Torvalds * @newsk: buffer to queue 29361da177e4SLinus Torvalds * 29371da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 29381da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 29391da177e4SLinus Torvalds * safely. 29401da177e4SLinus Torvalds * 29411da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 29421da177e4SLinus Torvalds */ 29431da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds unsigned long flags; 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 29481da177e4SLinus Torvalds __skb_queue_head(list, newsk); 29491da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29501da177e4SLinus Torvalds } 2951b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 29521da177e4SLinus Torvalds 29531da177e4SLinus Torvalds /** 29541da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 29551da177e4SLinus Torvalds * @list: list to use 29561da177e4SLinus Torvalds * @newsk: buffer to queue 29571da177e4SLinus Torvalds * 29581da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 29591da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 29601da177e4SLinus Torvalds * safely. 29611da177e4SLinus Torvalds * 29621da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 29631da177e4SLinus Torvalds */ 29641da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 29651da177e4SLinus Torvalds { 29661da177e4SLinus Torvalds unsigned long flags; 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 29691da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 29701da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29711da177e4SLinus Torvalds } 2972b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 29738728b834SDavid S. Miller 29741da177e4SLinus Torvalds /** 29751da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 29761da177e4SLinus Torvalds * @skb: buffer to remove 29778728b834SDavid S. Miller * @list: list to use 29781da177e4SLinus Torvalds * 29798728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 29808728b834SDavid S. Miller * function is atomic with respect to other list locked calls 29811da177e4SLinus Torvalds * 29828728b834SDavid S. Miller * You must know what list the SKB is on. 29831da177e4SLinus Torvalds */ 29848728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 29851da177e4SLinus Torvalds { 29861da177e4SLinus Torvalds unsigned long flags; 29871da177e4SLinus Torvalds 29881da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 29898728b834SDavid S. Miller __skb_unlink(skb, list); 29901da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29911da177e4SLinus Torvalds } 2992b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds /** 29951da177e4SLinus Torvalds * skb_append - append a buffer 29961da177e4SLinus Torvalds * @old: buffer to insert after 29971da177e4SLinus Torvalds * @newsk: buffer to insert 29988728b834SDavid S. Miller * @list: list to use 29991da177e4SLinus Torvalds * 30001da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 30011da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 30021da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 30031da177e4SLinus Torvalds */ 30048728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 30051da177e4SLinus Torvalds { 30061da177e4SLinus Torvalds unsigned long flags; 30071da177e4SLinus Torvalds 30088728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 30097de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 30108728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 30111da177e4SLinus Torvalds } 3012b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 30131da177e4SLinus Torvalds 30141da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 30151da177e4SLinus Torvalds struct sk_buff* skb1, 30161da177e4SLinus Torvalds const u32 len, const int pos) 30171da177e4SLinus Torvalds { 30181da177e4SLinus Torvalds int i; 30191da177e4SLinus Torvalds 3020d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 3021d626f62bSArnaldo Carvalho de Melo pos - len); 30221da177e4SLinus Torvalds /* And move data appendix as is. */ 30231da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 30241da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 30251da177e4SLinus Torvalds 30261da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 30271da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 30281da177e4SLinus Torvalds skb1->data_len = skb->data_len; 30291da177e4SLinus Torvalds skb1->len += skb1->data_len; 30301da177e4SLinus Torvalds skb->data_len = 0; 30311da177e4SLinus Torvalds skb->len = len; 303227a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 30331da177e4SLinus Torvalds } 30341da177e4SLinus Torvalds 30351da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 30361da177e4SLinus Torvalds struct sk_buff* skb1, 30371da177e4SLinus Torvalds const u32 len, int pos) 30381da177e4SLinus Torvalds { 30391da177e4SLinus Torvalds int i, k = 0; 30401da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 30431da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 30441da177e4SLinus Torvalds skb->len = len; 30451da177e4SLinus Torvalds skb->data_len = len - pos; 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 30489e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 30491da177e4SLinus Torvalds 30501da177e4SLinus Torvalds if (pos + size > len) { 30511da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds if (pos < len) { 30541da177e4SLinus Torvalds /* Split frag. 30551da177e4SLinus Torvalds * We have two variants in this case: 30561da177e4SLinus Torvalds * 1. Move all the frag to the second 30571da177e4SLinus Torvalds * part, if it is possible. F.e. 30581da177e4SLinus Torvalds * this approach is mandatory for TUX, 30591da177e4SLinus Torvalds * where splitting is expensive. 30601da177e4SLinus Torvalds * 2. Split is accurately. We make this. 30611da177e4SLinus Torvalds */ 3062ea2ab693SIan Campbell skb_frag_ref(skb, i); 30631da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 30649e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 30659e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 30661da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 30671da177e4SLinus Torvalds } 30681da177e4SLinus Torvalds k++; 30691da177e4SLinus Torvalds } else 30701da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 30711da177e4SLinus Torvalds pos += size; 30721da177e4SLinus Torvalds } 30731da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 30741da177e4SLinus Torvalds } 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds /** 30771da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 30781da177e4SLinus Torvalds * @skb: the buffer to split 30791da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 30801da177e4SLinus Torvalds * @len: new length for skb 30811da177e4SLinus Torvalds */ 30821da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 30831da177e4SLinus Torvalds { 30841da177e4SLinus Torvalds int pos = skb_headlen(skb); 30851da177e4SLinus Torvalds 3086fff88030SWillem de Bruijn skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & 3087fff88030SWillem de Bruijn SKBTX_SHARED_FRAG; 30881f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 30891da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 30901da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 30911da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 30921da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 30931da177e4SLinus Torvalds } 3094b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 30951da177e4SLinus Torvalds 30969f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 30979f782db3SIlpo Järvinen * 30989f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 30999f782db3SIlpo Järvinen */ 3100832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3101832d11c5SIlpo Järvinen { 31020ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3103832d11c5SIlpo Järvinen } 3104832d11c5SIlpo Järvinen 3105832d11c5SIlpo Järvinen /** 3106832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3107832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3108832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3109832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3110832d11c5SIlpo Järvinen * 3111832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 311220e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3113832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3114832d11c5SIlpo Järvinen * 3115832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3116832d11c5SIlpo Järvinen * 3117832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3118832d11c5SIlpo Järvinen * to have non-paged data as well. 3119832d11c5SIlpo Järvinen * 3120832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3121832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3122832d11c5SIlpo Järvinen */ 3123832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3124832d11c5SIlpo Järvinen { 3125832d11c5SIlpo Järvinen int from, to, merge, todo; 3126832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 3127832d11c5SIlpo Järvinen 3128832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3129f8071cdeSEric Dumazet 3130f8071cdeSEric Dumazet if (skb_headlen(skb)) 3131f8071cdeSEric Dumazet return 0; 31321f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 31331f8b977aSWillem de Bruijn return 0; 3134832d11c5SIlpo Järvinen 3135832d11c5SIlpo Järvinen todo = shiftlen; 3136832d11c5SIlpo Järvinen from = 0; 3137832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3138832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3139832d11c5SIlpo Järvinen 3140832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3141832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3142832d11c5SIlpo Järvinen */ 3143832d11c5SIlpo Järvinen if (!to || 3144ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3145ea2ab693SIan Campbell fragfrom->page_offset)) { 3146832d11c5SIlpo Järvinen merge = -1; 3147832d11c5SIlpo Järvinen } else { 3148832d11c5SIlpo Järvinen merge = to - 1; 3149832d11c5SIlpo Järvinen 31509e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3151832d11c5SIlpo Järvinen if (todo < 0) { 3152832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3153832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3154832d11c5SIlpo Järvinen return 0; 3155832d11c5SIlpo Järvinen 31569f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 31579f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3158832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3159832d11c5SIlpo Järvinen 31609e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 31619e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3162832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 3163832d11c5SIlpo Järvinen 3164832d11c5SIlpo Järvinen goto onlymerged; 3165832d11c5SIlpo Järvinen } 3166832d11c5SIlpo Järvinen 3167832d11c5SIlpo Järvinen from++; 3168832d11c5SIlpo Järvinen } 3169832d11c5SIlpo Järvinen 3170832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3171832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3172832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3173832d11c5SIlpo Järvinen return 0; 3174832d11c5SIlpo Järvinen 3175832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3176832d11c5SIlpo Järvinen return 0; 3177832d11c5SIlpo Järvinen 3178832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3179832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3180832d11c5SIlpo Järvinen return 0; 3181832d11c5SIlpo Järvinen 3182832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3183832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3184832d11c5SIlpo Järvinen 31859e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3186832d11c5SIlpo Järvinen *fragto = *fragfrom; 31879e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3188832d11c5SIlpo Järvinen from++; 3189832d11c5SIlpo Järvinen to++; 3190832d11c5SIlpo Järvinen 3191832d11c5SIlpo Järvinen } else { 3192ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3193832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 3194832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 31959e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3196832d11c5SIlpo Järvinen 3197832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 31989e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3199832d11c5SIlpo Järvinen todo = 0; 3200832d11c5SIlpo Järvinen 3201832d11c5SIlpo Järvinen to++; 3202832d11c5SIlpo Järvinen break; 3203832d11c5SIlpo Järvinen } 3204832d11c5SIlpo Järvinen } 3205832d11c5SIlpo Järvinen 3206832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3207832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3208832d11c5SIlpo Järvinen 3209832d11c5SIlpo Järvinen if (merge >= 0) { 3210832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3211832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3212832d11c5SIlpo Järvinen 32139e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 3214ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 3215832d11c5SIlpo Järvinen } 3216832d11c5SIlpo Järvinen 3217832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3218832d11c5SIlpo Järvinen to = 0; 3219832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3220832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3221832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3222832d11c5SIlpo Järvinen 3223832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3224832d11c5SIlpo Järvinen 3225832d11c5SIlpo Järvinen onlymerged: 3226832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3227832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3228832d11c5SIlpo Järvinen */ 3229832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3230832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3231832d11c5SIlpo Järvinen 3232832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 3233832d11c5SIlpo Järvinen skb->len -= shiftlen; 3234832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 3235832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 3236832d11c5SIlpo Järvinen tgt->len += shiftlen; 3237832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 3238832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 3239832d11c5SIlpo Järvinen 3240832d11c5SIlpo Järvinen return shiftlen; 3241832d11c5SIlpo Järvinen } 3242832d11c5SIlpo Järvinen 3243677e90edSThomas Graf /** 3244677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3245677e90edSThomas Graf * @skb: the buffer to read 3246677e90edSThomas Graf * @from: lower offset of data to be read 3247677e90edSThomas Graf * @to: upper offset of data to be read 3248677e90edSThomas Graf * @st: state variable 3249677e90edSThomas Graf * 3250677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3251677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3252677e90edSThomas Graf */ 3253677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3254677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3255677e90edSThomas Graf { 3256677e90edSThomas Graf st->lower_offset = from; 3257677e90edSThomas Graf st->upper_offset = to; 3258677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3259677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3260677e90edSThomas Graf st->frag_data = NULL; 3261677e90edSThomas Graf } 3262b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3263677e90edSThomas Graf 3264677e90edSThomas Graf /** 3265677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3266677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3267677e90edSThomas Graf * @data: destination pointer for data to be returned 3268677e90edSThomas Graf * @st: state variable 3269677e90edSThomas Graf * 3270bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3271677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3272bc32383cSMathias Krause * the head of the data block to @data and returns the length 3273677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3274677e90edSThomas Graf * offset has been reached. 3275677e90edSThomas Graf * 3276677e90edSThomas Graf * The caller is not required to consume all of the data 3277bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3278677e90edSThomas Graf * of bytes already consumed and the next call to 3279677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3280677e90edSThomas Graf * 328125985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3282e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3283677e90edSThomas Graf * reads of potentially non linear data. 3284677e90edSThomas Graf * 3285bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3286677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3287677e90edSThomas Graf * a stack for this purpose. 3288677e90edSThomas Graf */ 3289677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3290677e90edSThomas Graf struct skb_seq_state *st) 3291677e90edSThomas Graf { 3292677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3293677e90edSThomas Graf skb_frag_t *frag; 3294677e90edSThomas Graf 3295aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3296aeb193eaSWedson Almeida Filho if (st->frag_data) { 3297aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3298aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3299aeb193eaSWedson Almeida Filho } 3300677e90edSThomas Graf return 0; 3301aeb193eaSWedson Almeida Filho } 3302677e90edSThomas Graf 3303677e90edSThomas Graf next_skb: 330495e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3305677e90edSThomas Graf 3306995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 330795e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3308677e90edSThomas Graf return block_limit - abs_offset; 3309677e90edSThomas Graf } 3310677e90edSThomas Graf 3311677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3312677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3313677e90edSThomas Graf 3314677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 3315677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 33169e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 3317677e90edSThomas Graf 3318677e90edSThomas Graf if (abs_offset < block_limit) { 3319677e90edSThomas Graf if (!st->frag_data) 332051c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 3321677e90edSThomas Graf 3322677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 3323677e90edSThomas Graf (abs_offset - st->stepped_offset); 3324677e90edSThomas Graf 3325677e90edSThomas Graf return block_limit - abs_offset; 3326677e90edSThomas Graf } 3327677e90edSThomas Graf 3328677e90edSThomas Graf if (st->frag_data) { 332951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3330677e90edSThomas Graf st->frag_data = NULL; 3331677e90edSThomas Graf } 3332677e90edSThomas Graf 3333677e90edSThomas Graf st->frag_idx++; 33349e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 3335677e90edSThomas Graf } 3336677e90edSThomas Graf 33375b5a60daSOlaf Kirch if (st->frag_data) { 333851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 33395b5a60daSOlaf Kirch st->frag_data = NULL; 33405b5a60daSOlaf Kirch } 33415b5a60daSOlaf Kirch 334221dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3343677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 334495e3b24cSHerbert Xu st->frag_idx = 0; 3345677e90edSThomas Graf goto next_skb; 334671b3346dSShyam Iyer } else if (st->cur_skb->next) { 334771b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 334871b3346dSShyam Iyer st->frag_idx = 0; 3349677e90edSThomas Graf goto next_skb; 3350677e90edSThomas Graf } 3351677e90edSThomas Graf 3352677e90edSThomas Graf return 0; 3353677e90edSThomas Graf } 3354b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3355677e90edSThomas Graf 3356677e90edSThomas Graf /** 3357677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3358677e90edSThomas Graf * @st: state variable 3359677e90edSThomas Graf * 3360677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3361677e90edSThomas Graf * returned 0. 3362677e90edSThomas Graf */ 3363677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3364677e90edSThomas Graf { 3365677e90edSThomas Graf if (st->frag_data) 336651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3367677e90edSThomas Graf } 3368b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3369677e90edSThomas Graf 33703fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 33713fc7e8a6SThomas Graf 33723fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 33733fc7e8a6SThomas Graf struct ts_config *conf, 33743fc7e8a6SThomas Graf struct ts_state *state) 33753fc7e8a6SThomas Graf { 33763fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 33773fc7e8a6SThomas Graf } 33783fc7e8a6SThomas Graf 33793fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 33803fc7e8a6SThomas Graf { 33813fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 33823fc7e8a6SThomas Graf } 33833fc7e8a6SThomas Graf 33843fc7e8a6SThomas Graf /** 33853fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 33863fc7e8a6SThomas Graf * @skb: the buffer to look in 33873fc7e8a6SThomas Graf * @from: search offset 33883fc7e8a6SThomas Graf * @to: search limit 33893fc7e8a6SThomas Graf * @config: textsearch configuration 33903fc7e8a6SThomas Graf * 33913fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 33923fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 33933fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 33943fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 33953fc7e8a6SThomas Graf */ 33963fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3397059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 33983fc7e8a6SThomas Graf { 3399059a2440SBojan Prtvar struct ts_state state; 3400f72b948dSPhil Oester unsigned int ret; 3401f72b948dSPhil Oester 34023fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 34033fc7e8a6SThomas Graf config->finish = skb_ts_finish; 34043fc7e8a6SThomas Graf 3405059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 34063fc7e8a6SThomas Graf 3407059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3408f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 34093fc7e8a6SThomas Graf } 3410b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 34113fc7e8a6SThomas Graf 3412be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3413be12a1feSHannes Frederic Sowa int offset, size_t size) 3414be12a1feSHannes Frederic Sowa { 3415be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3416be12a1feSHannes Frederic Sowa 3417be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3418be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3419be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3420be12a1feSHannes Frederic Sowa get_page(page); 3421be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3422be12a1feSHannes Frederic Sowa } else { 3423be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3424be12a1feSHannes Frederic Sowa } 3425be12a1feSHannes Frederic Sowa 3426be12a1feSHannes Frederic Sowa return 0; 3427be12a1feSHannes Frederic Sowa } 3428be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3429be12a1feSHannes Frederic Sowa 3430cbb042f9SHerbert Xu /** 3431cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3432cbb042f9SHerbert Xu * @skb: buffer to update 3433cbb042f9SHerbert Xu * @len: length of data pulled 3434cbb042f9SHerbert Xu * 3435cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3436fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 343784fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 343884fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 343984fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3440cbb042f9SHerbert Xu */ 3441af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3442cbb042f9SHerbert Xu { 344331b33dfbSPravin B Shelar unsigned char *data = skb->data; 344431b33dfbSPravin B Shelar 3445cbb042f9SHerbert Xu BUG_ON(len > skb->len); 344631b33dfbSPravin B Shelar __skb_pull(skb, len); 344731b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 344831b33dfbSPravin B Shelar return skb->data; 3449cbb042f9SHerbert Xu } 3450f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3451f94691acSArnaldo Carvalho de Melo 345213acc94eSYonghong Song static inline skb_frag_t skb_head_frag_to_page_desc(struct sk_buff *frag_skb) 345313acc94eSYonghong Song { 345413acc94eSYonghong Song skb_frag_t head_frag; 345513acc94eSYonghong Song struct page *page; 345613acc94eSYonghong Song 345713acc94eSYonghong Song page = virt_to_head_page(frag_skb->head); 345813acc94eSYonghong Song head_frag.page.p = page; 345913acc94eSYonghong Song head_frag.page_offset = frag_skb->data - 346013acc94eSYonghong Song (unsigned char *)page_address(page); 346113acc94eSYonghong Song head_frag.size = skb_headlen(frag_skb); 346213acc94eSYonghong Song return head_frag; 346313acc94eSYonghong Song } 346413acc94eSYonghong Song 3465f4c50d99SHerbert Xu /** 3466f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3467df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3468576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3469f4c50d99SHerbert Xu * 3470f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 34714c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 34724c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3473f4c50d99SHerbert Xu */ 3474df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3475df5771ffSMichael S. Tsirkin netdev_features_t features) 3476f4c50d99SHerbert Xu { 3477f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3478f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 34791a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3480df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3481df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3482df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 34831fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3484f4c50d99SHerbert Xu unsigned int offset = doffset; 3485df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3486802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3487f4c50d99SHerbert Xu unsigned int headroom; 3488802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3489ec5f0615SPravin B Shelar __be16 proto; 349036c98382SAlexander Duyck bool csum, sg; 3491df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3492f4c50d99SHerbert Xu int err = -ENOMEM; 3493f4c50d99SHerbert Xu int i = 0; 3494f4c50d99SHerbert Xu int pos; 349553d6471cSVlad Yasevich int dummy; 3496f4c50d99SHerbert Xu 34975882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 349853d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3499ec5f0615SPravin B Shelar if (unlikely(!proto)) 3500ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3501ec5f0615SPravin B Shelar 350236c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 3503f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 35047e2b10c1STom Herbert 350507b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 350607b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 350707b26c94SSteffen Klassert struct sk_buff *iter; 350843170c4eSIlan Tayari unsigned int frag_len; 350907b26c94SSteffen Klassert 351007b26c94SSteffen Klassert if (!list_skb || 351107b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 351207b26c94SSteffen Klassert goto normal; 351307b26c94SSteffen Klassert 351443170c4eSIlan Tayari /* If we get here then all the required 351543170c4eSIlan Tayari * GSO features except frag_list are supported. 351643170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 351743170c4eSIlan Tayari * with no frag_list. 351843170c4eSIlan Tayari * Currently we can do that only when the buffers don't 351943170c4eSIlan Tayari * have a linear part and all the buffers except 352043170c4eSIlan Tayari * the last are of the same length. 352107b26c94SSteffen Klassert */ 352243170c4eSIlan Tayari frag_len = list_skb->len; 352307b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 352443170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 352543170c4eSIlan Tayari goto normal; 3526eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 352707b26c94SSteffen Klassert goto normal; 352807b26c94SSteffen Klassert 352907b26c94SSteffen Klassert len -= iter->len; 353007b26c94SSteffen Klassert } 353143170c4eSIlan Tayari 353243170c4eSIlan Tayari if (len != frag_len) 353343170c4eSIlan Tayari goto normal; 353407b26c94SSteffen Klassert } 353507b26c94SSteffen Klassert 3536802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 3537802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 3538802ab55aSAlexander Duyck * now. 3539802ab55aSAlexander Duyck */ 3540802ab55aSAlexander Duyck partial_segs = len / mss; 3541d7fb5a80SAlexander Duyck if (partial_segs > 1) 3542802ab55aSAlexander Duyck mss *= partial_segs; 3543d7fb5a80SAlexander Duyck else 3544d7fb5a80SAlexander Duyck partial_segs = 0; 3545802ab55aSAlexander Duyck } 3546802ab55aSAlexander Duyck 354707b26c94SSteffen Klassert normal: 3548df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3549df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3550f4c50d99SHerbert Xu 3551f4c50d99SHerbert Xu do { 3552f4c50d99SHerbert Xu struct sk_buff *nskb; 35538cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3554c8884eddSHerbert Xu int hsize; 3555f4c50d99SHerbert Xu int size; 3556f4c50d99SHerbert Xu 35573953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 35583953c46cSMarcelo Ricardo Leitner len = list_skb->len; 35593953c46cSMarcelo Ricardo Leitner } else { 3560df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3561f4c50d99SHerbert Xu if (len > mss) 3562f4c50d99SHerbert Xu len = mss; 35633953c46cSMarcelo Ricardo Leitner } 3564f4c50d99SHerbert Xu 3565df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3566f4c50d99SHerbert Xu if (hsize < 0) 3567f4c50d99SHerbert Xu hsize = 0; 3568c8884eddSHerbert Xu if (hsize > len || !sg) 3569c8884eddSHerbert Xu hsize = len; 3570f4c50d99SHerbert Xu 35711a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 35721a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 35731a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 357489319d38SHerbert Xu 35759d8506ccSHerbert Xu i = 0; 35761a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 35771a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 35781fd819ecSMichael S. Tsirkin frag_skb = list_skb; 35791a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 35809d8506ccSHerbert Xu 35819d8506ccSHerbert Xu while (pos < offset + len) { 35829d8506ccSHerbert Xu BUG_ON(i >= nfrags); 35839d8506ccSHerbert Xu 35844e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 35859d8506ccSHerbert Xu if (pos + size > offset + len) 35869d8506ccSHerbert Xu break; 35879d8506ccSHerbert Xu 35889d8506ccSHerbert Xu i++; 35899d8506ccSHerbert Xu pos += size; 35904e1beba1SMichael S. Tsirkin frag++; 35919d8506ccSHerbert Xu } 35929d8506ccSHerbert Xu 35931a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 35941a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 359589319d38SHerbert Xu 3596f4c50d99SHerbert Xu if (unlikely(!nskb)) 3597f4c50d99SHerbert Xu goto err; 3598f4c50d99SHerbert Xu 35999d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 36009d8506ccSHerbert Xu kfree_skb(nskb); 36019d8506ccSHerbert Xu goto err; 36029d8506ccSHerbert Xu } 36039d8506ccSHerbert Xu 3604ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 360589319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 360689319d38SHerbert Xu kfree_skb(nskb); 360789319d38SHerbert Xu goto err; 360889319d38SHerbert Xu } 360989319d38SHerbert Xu 3610ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 361189319d38SHerbert Xu skb_release_head_state(nskb); 361289319d38SHerbert Xu __skb_push(nskb, doffset); 361389319d38SHerbert Xu } else { 3614c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3615df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3616c93bdd0eSMel Gorman NUMA_NO_NODE); 361789319d38SHerbert Xu 361889319d38SHerbert Xu if (unlikely(!nskb)) 361989319d38SHerbert Xu goto err; 362089319d38SHerbert Xu 362189319d38SHerbert Xu skb_reserve(nskb, headroom); 362289319d38SHerbert Xu __skb_put(nskb, doffset); 362389319d38SHerbert Xu } 362489319d38SHerbert Xu 3625f4c50d99SHerbert Xu if (segs) 3626f4c50d99SHerbert Xu tail->next = nskb; 3627f4c50d99SHerbert Xu else 3628f4c50d99SHerbert Xu segs = nskb; 3629f4c50d99SHerbert Xu tail = nskb; 3630f4c50d99SHerbert Xu 3631df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3632f4c50d99SHerbert Xu 3633030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3634fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 363568c33163SPravin B Shelar 3636df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 363768c33163SPravin B Shelar nskb->data - tnl_hlen, 363868c33163SPravin B Shelar doffset + tnl_hlen); 363989319d38SHerbert Xu 36409d8506ccSHerbert Xu if (nskb->len == len + doffset) 36411cdbcb79SSimon Horman goto perform_csum_check; 364289319d38SHerbert Xu 36437fbeffedSAlexander Duyck if (!sg) { 36447fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 36456f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 364676443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 364776443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 3648f4c50d99SHerbert Xu skb_put(nskb, len), 3649f4c50d99SHerbert Xu len, 0); 36507e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3651de843723STom Herbert skb_headroom(nskb) + doffset; 3652f4c50d99SHerbert Xu continue; 3653f4c50d99SHerbert Xu } 3654f4c50d99SHerbert Xu 36558cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3656f4c50d99SHerbert Xu 3657df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3658d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3659f4c50d99SHerbert Xu 3660fff88030SWillem de Bruijn skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & 3661df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3662cef401deSEric Dumazet 3663bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3664bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, GFP_ATOMIC)) 3665bf5c25d6SWillem de Bruijn goto err; 3666bf5c25d6SWillem de Bruijn 36679d8506ccSHerbert Xu while (pos < offset + len) { 36689d8506ccSHerbert Xu if (i >= nfrags) { 36699d8506ccSHerbert Xu i = 0; 36701a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 36711a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 36721fd819ecSMichael S. Tsirkin frag_skb = list_skb; 367313acc94eSYonghong Song if (!skb_headlen(list_skb)) { 36749d8506ccSHerbert Xu BUG_ON(!nfrags); 367513acc94eSYonghong Song } else { 367613acc94eSYonghong Song BUG_ON(!list_skb->head_frag); 36779d8506ccSHerbert Xu 367813acc94eSYonghong Song /* to make room for head_frag. */ 367913acc94eSYonghong Song i--; 368013acc94eSYonghong Song frag--; 368113acc94eSYonghong Song } 3682bf5c25d6SWillem de Bruijn if (skb_orphan_frags(frag_skb, GFP_ATOMIC) || 3683bf5c25d6SWillem de Bruijn skb_zerocopy_clone(nskb, frag_skb, 3684bf5c25d6SWillem de Bruijn GFP_ATOMIC)) 3685bf5c25d6SWillem de Bruijn goto err; 3686bf5c25d6SWillem de Bruijn 36871a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 36889d8506ccSHerbert Xu } 36899d8506ccSHerbert Xu 36909d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 36919d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 36929d8506ccSHerbert Xu net_warn_ratelimited( 36939d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 36949d8506ccSHerbert Xu pos, mss); 3695ff907a11SEric Dumazet err = -EINVAL; 36969d8506ccSHerbert Xu goto err; 36979d8506ccSHerbert Xu } 36989d8506ccSHerbert Xu 369913acc94eSYonghong Song *nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag; 37008cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 37018cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3702f4c50d99SHerbert Xu 3703f4c50d99SHerbert Xu if (pos < offset) { 37048cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 37058cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3706f4c50d99SHerbert Xu } 3707f4c50d99SHerbert Xu 370889319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3709f4c50d99SHerbert Xu 3710f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3711f4c50d99SHerbert Xu i++; 37124e1beba1SMichael S. Tsirkin frag++; 3713f4c50d99SHerbert Xu pos += size; 3714f4c50d99SHerbert Xu } else { 37158cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 371689319d38SHerbert Xu goto skip_fraglist; 3717f4c50d99SHerbert Xu } 3718f4c50d99SHerbert Xu 37198cb19905SMichael S. Tsirkin nskb_frag++; 3720f4c50d99SHerbert Xu } 3721f4c50d99SHerbert Xu 372289319d38SHerbert Xu skip_fraglist: 3723f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3724f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3725f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3726ec5f0615SPravin B Shelar 37271cdbcb79SSimon Horman perform_csum_check: 37287fbeffedSAlexander Duyck if (!csum) { 3729ff907a11SEric Dumazet if (skb_has_shared_frag(nskb) && 3730ff907a11SEric Dumazet __skb_linearize(nskb)) 3731ddff00d4SAlexander Duyck goto err; 3732ff907a11SEric Dumazet 37337fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 3734ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 373576443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 373676443456SAlexander Duyck skb_checksum(nskb, doffset, 373776443456SAlexander Duyck nskb->len - doffset, 0); 37387e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 37397e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3740ec5f0615SPravin B Shelar } 3741df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3742f4c50d99SHerbert Xu 3743bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3744bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3745bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3746bec3cfdcSEric Dumazet */ 3747bec3cfdcSEric Dumazet segs->prev = tail; 3748432c856fSToshiaki Makita 3749802ab55aSAlexander Duyck if (partial_segs) { 375007b26c94SSteffen Klassert struct sk_buff *iter; 3751802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 375207b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 3753802ab55aSAlexander Duyck 3754802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 375507b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 3756802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 3757802ab55aSAlexander Duyck 3758802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 3759802ab55aSAlexander Duyck * our way back down the stack of protocols. 3760802ab55aSAlexander Duyck */ 376107b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 376207b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 376307b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 376407b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 376507b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 376607b26c94SSteffen Klassert } 376707b26c94SSteffen Klassert 376807b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 376907b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 377007b26c94SSteffen Klassert else if (tail != segs) 377107b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 3772802ab55aSAlexander Duyck } 3773802ab55aSAlexander Duyck 3774432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3775432c856fSToshiaki Makita * using skb_set_owner_w(). 3776432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3777432c856fSToshiaki Makita */ 3778432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3779432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3780432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3781432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3782432c856fSToshiaki Makita } 3783f4c50d99SHerbert Xu return segs; 3784f4c50d99SHerbert Xu 3785f4c50d99SHerbert Xu err: 3786289dccbeSEric Dumazet kfree_skb_list(segs); 3787f4c50d99SHerbert Xu return ERR_PTR(err); 3788f4c50d99SHerbert Xu } 3789f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3790f4c50d99SHerbert Xu 3791d4546c25SDavid Miller int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb) 379271d93b39SHerbert Xu { 37938a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 379467147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 379567147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 37968a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 3797715dc1f3SEric Dumazet unsigned int delta_truesize; 3798d4546c25SDavid Miller struct sk_buff *lp; 379971d93b39SHerbert Xu 38008a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 380171d93b39SHerbert Xu return -E2BIG; 380271d93b39SHerbert Xu 380329e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 38048a29111cSEric Dumazet pinfo = skb_shinfo(lp); 38058a29111cSEric Dumazet 38068a29111cSEric Dumazet if (headlen <= offset) { 380742da6994SHerbert Xu skb_frag_t *frag; 380866e92fcfSHerbert Xu skb_frag_t *frag2; 38099aaa156cSHerbert Xu int i = skbinfo->nr_frags; 38109aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 381142da6994SHerbert Xu 381266e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 38138a29111cSEric Dumazet goto merge; 381481705ad1SHerbert Xu 38158a29111cSEric Dumazet offset -= headlen; 38169aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 38179aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3818f5572068SHerbert Xu 38199aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 38209aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 382166e92fcfSHerbert Xu do { 382266e92fcfSHerbert Xu *--frag = *--frag2; 382366e92fcfSHerbert Xu } while (--i); 382466e92fcfSHerbert Xu 382566e92fcfSHerbert Xu frag->page_offset += offset; 38269e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 382766e92fcfSHerbert Xu 3828715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3829ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3830ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3831715dc1f3SEric Dumazet 3832f5572068SHerbert Xu skb->truesize -= skb->data_len; 3833f5572068SHerbert Xu skb->len -= skb->data_len; 3834f5572068SHerbert Xu skb->data_len = 0; 3835f5572068SHerbert Xu 3836715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 38375d38a079SHerbert Xu goto done; 3838d7e8883cSEric Dumazet } else if (skb->head_frag) { 3839d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3840d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3841d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3842d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3843d7e8883cSEric Dumazet unsigned int first_offset; 3844d7e8883cSEric Dumazet 3845d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 38468a29111cSEric Dumazet goto merge; 3847d7e8883cSEric Dumazet 3848d7e8883cSEric Dumazet first_offset = skb->data - 3849d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3850d7e8883cSEric Dumazet offset; 3851d7e8883cSEric Dumazet 3852d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3853d7e8883cSEric Dumazet 3854d7e8883cSEric Dumazet frag->page.p = page; 3855d7e8883cSEric Dumazet frag->page_offset = first_offset; 3856d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3857d7e8883cSEric Dumazet 3858d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3859d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3860d7e8883cSEric Dumazet 3861715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3862d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3863d7e8883cSEric Dumazet goto done; 38648a29111cSEric Dumazet } 386571d93b39SHerbert Xu 386671d93b39SHerbert Xu merge: 3867715dc1f3SEric Dumazet delta_truesize = skb->truesize; 386867147ba9SHerbert Xu if (offset > headlen) { 3869d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3870d1dc7abfSMichal Schmidt 3871d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 38729e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3873d1dc7abfSMichal Schmidt skb->data_len -= eat; 3874d1dc7abfSMichal Schmidt skb->len -= eat; 387567147ba9SHerbert Xu offset = headlen; 387656035022SHerbert Xu } 387756035022SHerbert Xu 387867147ba9SHerbert Xu __skb_pull(skb, offset); 387956035022SHerbert Xu 388029e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 38818a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 38828a29111cSEric Dumazet else 3883c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3884c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3885f4a775d1SEric Dumazet __skb_header_release(skb); 38868a29111cSEric Dumazet lp = p; 388771d93b39SHerbert Xu 38885d38a079SHerbert Xu done: 38895d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 389037fe4732SHerbert Xu p->data_len += len; 3891715dc1f3SEric Dumazet p->truesize += delta_truesize; 389237fe4732SHerbert Xu p->len += len; 38938a29111cSEric Dumazet if (lp != p) { 38948a29111cSEric Dumazet lp->data_len += len; 38958a29111cSEric Dumazet lp->truesize += delta_truesize; 38968a29111cSEric Dumazet lp->len += len; 38978a29111cSEric Dumazet } 389871d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 389971d93b39SHerbert Xu return 0; 390071d93b39SHerbert Xu } 390157c05650SMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gro_receive); 390271d93b39SHerbert Xu 3903df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 3904df5042f4SFlorian Westphal #define SKB_EXT_ALIGN_VALUE 8 3905df5042f4SFlorian Westphal #define SKB_EXT_CHUNKSIZEOF(x) (ALIGN((sizeof(x)), SKB_EXT_ALIGN_VALUE) / SKB_EXT_ALIGN_VALUE) 3906df5042f4SFlorian Westphal 3907df5042f4SFlorian Westphal static const u8 skb_ext_type_len[] = { 3908df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 3909df5042f4SFlorian Westphal [SKB_EXT_BRIDGE_NF] = SKB_EXT_CHUNKSIZEOF(struct nf_bridge_info), 3910df5042f4SFlorian Westphal #endif 39114165079bSFlorian Westphal #ifdef CONFIG_XFRM 39124165079bSFlorian Westphal [SKB_EXT_SEC_PATH] = SKB_EXT_CHUNKSIZEOF(struct sec_path), 39134165079bSFlorian Westphal #endif 3914df5042f4SFlorian Westphal }; 3915df5042f4SFlorian Westphal 3916df5042f4SFlorian Westphal static __always_inline unsigned int skb_ext_total_length(void) 3917df5042f4SFlorian Westphal { 3918df5042f4SFlorian Westphal return SKB_EXT_CHUNKSIZEOF(struct skb_ext) + 3919df5042f4SFlorian Westphal #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 3920df5042f4SFlorian Westphal skb_ext_type_len[SKB_EXT_BRIDGE_NF] + 3921df5042f4SFlorian Westphal #endif 39224165079bSFlorian Westphal #ifdef CONFIG_XFRM 39234165079bSFlorian Westphal skb_ext_type_len[SKB_EXT_SEC_PATH] + 39244165079bSFlorian Westphal #endif 3925df5042f4SFlorian Westphal 0; 3926df5042f4SFlorian Westphal } 3927df5042f4SFlorian Westphal 3928df5042f4SFlorian Westphal static void skb_extensions_init(void) 3929df5042f4SFlorian Westphal { 3930df5042f4SFlorian Westphal BUILD_BUG_ON(SKB_EXT_NUM >= 8); 3931df5042f4SFlorian Westphal BUILD_BUG_ON(skb_ext_total_length() > 255); 3932df5042f4SFlorian Westphal 3933df5042f4SFlorian Westphal skbuff_ext_cache = kmem_cache_create("skbuff_ext_cache", 3934df5042f4SFlorian Westphal SKB_EXT_ALIGN_VALUE * skb_ext_total_length(), 3935df5042f4SFlorian Westphal 0, 3936df5042f4SFlorian Westphal SLAB_HWCACHE_ALIGN|SLAB_PANIC, 3937df5042f4SFlorian Westphal NULL); 3938df5042f4SFlorian Westphal } 3939df5042f4SFlorian Westphal #else 3940df5042f4SFlorian Westphal static void skb_extensions_init(void) {} 3941df5042f4SFlorian Westphal #endif 3942df5042f4SFlorian Westphal 39431da177e4SLinus Torvalds void __init skb_init(void) 39441da177e4SLinus Torvalds { 394579a8a642SKees Cook skbuff_head_cache = kmem_cache_create_usercopy("skbuff_head_cache", 39461da177e4SLinus Torvalds sizeof(struct sk_buff), 39471da177e4SLinus Torvalds 0, 3948e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 394979a8a642SKees Cook offsetof(struct sk_buff, cb), 395079a8a642SKees Cook sizeof_field(struct sk_buff, cb), 395120c2df83SPaul Mundt NULL); 3952d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3953d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3954d179cd12SDavid S. Miller 0, 3955e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 395620c2df83SPaul Mundt NULL); 3957df5042f4SFlorian Westphal skb_extensions_init(); 39581da177e4SLinus Torvalds } 39591da177e4SLinus Torvalds 396051c739d1SDavid S. Miller static int 396148a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 396248a1df65SJason A. Donenfeld unsigned int recursion_level) 3963716ea3a7SDavid Howells { 39641a028e50SDavid S. Miller int start = skb_headlen(skb); 39651a028e50SDavid S. Miller int i, copy = start - offset; 3966fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3967716ea3a7SDavid Howells int elt = 0; 3968716ea3a7SDavid Howells 396948a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 397048a1df65SJason A. Donenfeld return -EMSGSIZE; 397148a1df65SJason A. Donenfeld 3972716ea3a7SDavid Howells if (copy > 0) { 3973716ea3a7SDavid Howells if (copy > len) 3974716ea3a7SDavid Howells copy = len; 3975642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3976716ea3a7SDavid Howells elt++; 3977716ea3a7SDavid Howells if ((len -= copy) == 0) 3978716ea3a7SDavid Howells return elt; 3979716ea3a7SDavid Howells offset += copy; 3980716ea3a7SDavid Howells } 3981716ea3a7SDavid Howells 3982716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 39831a028e50SDavid S. Miller int end; 3984716ea3a7SDavid Howells 3985547b792cSIlpo Järvinen WARN_ON(start > offset + len); 39861a028e50SDavid S. Miller 39879e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3988716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3989716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 399048a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 399148a1df65SJason A. Donenfeld return -EMSGSIZE; 3992716ea3a7SDavid Howells 3993716ea3a7SDavid Howells if (copy > len) 3994716ea3a7SDavid Howells copy = len; 3995ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3996642f1490SJens Axboe frag->page_offset+offset-start); 3997716ea3a7SDavid Howells elt++; 3998716ea3a7SDavid Howells if (!(len -= copy)) 3999716ea3a7SDavid Howells return elt; 4000716ea3a7SDavid Howells offset += copy; 4001716ea3a7SDavid Howells } 40021a028e50SDavid S. Miller start = end; 4003716ea3a7SDavid Howells } 4004716ea3a7SDavid Howells 4005fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 400648a1df65SJason A. Donenfeld int end, ret; 4007716ea3a7SDavid Howells 4008547b792cSIlpo Järvinen WARN_ON(start > offset + len); 40091a028e50SDavid S. Miller 4010fbb398a8SDavid S. Miller end = start + frag_iter->len; 4011716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 401248a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 401348a1df65SJason A. Donenfeld return -EMSGSIZE; 401448a1df65SJason A. Donenfeld 4015716ea3a7SDavid Howells if (copy > len) 4016716ea3a7SDavid Howells copy = len; 401748a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 401848a1df65SJason A. Donenfeld copy, recursion_level + 1); 401948a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 402048a1df65SJason A. Donenfeld return ret; 402148a1df65SJason A. Donenfeld elt += ret; 4022716ea3a7SDavid Howells if ((len -= copy) == 0) 4023716ea3a7SDavid Howells return elt; 4024716ea3a7SDavid Howells offset += copy; 4025716ea3a7SDavid Howells } 40261a028e50SDavid S. Miller start = end; 4027716ea3a7SDavid Howells } 4028716ea3a7SDavid Howells BUG_ON(len); 4029716ea3a7SDavid Howells return elt; 4030716ea3a7SDavid Howells } 4031716ea3a7SDavid Howells 403248a1df65SJason A. Donenfeld /** 403348a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 403448a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 403548a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 403648a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 403748a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 403848a1df65SJason A. Donenfeld * 403948a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 404048a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 404148a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 404248a1df65SJason A. Donenfeld * could not fit. 404348a1df65SJason A. Donenfeld */ 404448a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 404548a1df65SJason A. Donenfeld { 404648a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 404748a1df65SJason A. Donenfeld 404848a1df65SJason A. Donenfeld if (nsg <= 0) 404948a1df65SJason A. Donenfeld return nsg; 405048a1df65SJason A. Donenfeld 405148a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 405248a1df65SJason A. Donenfeld 405348a1df65SJason A. Donenfeld return nsg; 405448a1df65SJason A. Donenfeld } 405548a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 405648a1df65SJason A. Donenfeld 405725a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 405825a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 405925a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 406025a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 406125a91d8dSFan Du * 406225a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 406325a91d8dSFan Du * 1. sg_init_table 406425a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 406525a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 406625a91d8dSFan Du * 406725a91d8dSFan Du * This is equivalent to: 406825a91d8dSFan Du * 1. sg_init_table 406925a91d8dSFan Du * 2. skb_to_sgvec(payload1) 407025a91d8dSFan Du * 3. sg_unmark_end 407125a91d8dSFan Du * 4. skb_to_sgvec(payload2) 407225a91d8dSFan Du * 407325a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 407425a91d8dSFan Du * is more preferable. 407525a91d8dSFan Du */ 407625a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 407725a91d8dSFan Du int offset, int len) 407825a91d8dSFan Du { 407948a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 408025a91d8dSFan Du } 408125a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 408225a91d8dSFan Du 408351c739d1SDavid S. Miller 408451c739d1SDavid S. Miller 4085716ea3a7SDavid Howells /** 4086716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4087716ea3a7SDavid Howells * @skb: The socket buffer to check. 4088716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4089716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4090716ea3a7SDavid Howells * 4091716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4092716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4093716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4094716ea3a7SDavid Howells * 4095716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4096716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4097716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4098716ea3a7SDavid Howells * 4099716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4100716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4101716ea3a7SDavid Howells */ 4102716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4103716ea3a7SDavid Howells { 4104716ea3a7SDavid Howells int copyflag; 4105716ea3a7SDavid Howells int elt; 4106716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4107716ea3a7SDavid Howells 4108716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4109716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4110716ea3a7SDavid Howells * at the moment even if they are anonymous). 4111716ea3a7SDavid Howells */ 4112716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4113716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 4114716ea3a7SDavid Howells return -ENOMEM; 4115716ea3a7SDavid Howells 4116716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 411721dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4118716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4119716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4120716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4121716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4122716ea3a7SDavid Howells 4123716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4124716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4125716ea3a7SDavid Howells return -ENOMEM; 4126716ea3a7SDavid Howells 4127716ea3a7SDavid Howells /* Voila! */ 4128716ea3a7SDavid Howells *trailer = skb; 4129716ea3a7SDavid Howells return 1; 4130716ea3a7SDavid Howells } 4131716ea3a7SDavid Howells 4132716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4133716ea3a7SDavid Howells 4134716ea3a7SDavid Howells elt = 1; 4135716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4136716ea3a7SDavid Howells copyflag = 0; 4137716ea3a7SDavid Howells 4138716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4139716ea3a7SDavid Howells int ntail = 0; 4140716ea3a7SDavid Howells 4141716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4142716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4143716ea3a7SDavid Howells * after it. */ 4144716ea3a7SDavid Howells 4145716ea3a7SDavid Howells if (skb_shared(skb1)) 4146716ea3a7SDavid Howells copyflag = 1; 4147716ea3a7SDavid Howells 4148716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4149716ea3a7SDavid Howells 4150716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4151716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 415221dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4153716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4154716ea3a7SDavid Howells ntail = tailbits + 128; 4155716ea3a7SDavid Howells } 4156716ea3a7SDavid Howells 4157716ea3a7SDavid Howells if (copyflag || 4158716ea3a7SDavid Howells skb_cloned(skb1) || 4159716ea3a7SDavid Howells ntail || 4160716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 416121dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4162716ea3a7SDavid Howells struct sk_buff *skb2; 4163716ea3a7SDavid Howells 4164716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4165716ea3a7SDavid Howells if (ntail == 0) 4166716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4167716ea3a7SDavid Howells else 4168716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4169716ea3a7SDavid Howells skb_headroom(skb1), 4170716ea3a7SDavid Howells ntail, 4171716ea3a7SDavid Howells GFP_ATOMIC); 4172716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4173716ea3a7SDavid Howells return -ENOMEM; 4174716ea3a7SDavid Howells 4175716ea3a7SDavid Howells if (skb1->sk) 4176716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4177716ea3a7SDavid Howells 4178716ea3a7SDavid Howells /* Looking around. Are we still alive? 4179716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4180716ea3a7SDavid Howells 4181716ea3a7SDavid Howells skb2->next = skb1->next; 4182716ea3a7SDavid Howells *skb_p = skb2; 4183716ea3a7SDavid Howells kfree_skb(skb1); 4184716ea3a7SDavid Howells skb1 = skb2; 4185716ea3a7SDavid Howells } 4186716ea3a7SDavid Howells elt++; 4187716ea3a7SDavid Howells *trailer = skb1; 4188716ea3a7SDavid Howells skb_p = &skb1->next; 4189716ea3a7SDavid Howells } 4190716ea3a7SDavid Howells 4191716ea3a7SDavid Howells return elt; 4192716ea3a7SDavid Howells } 4193b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4194716ea3a7SDavid Howells 4195b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4196b1faf566SEric Dumazet { 4197b1faf566SEric Dumazet struct sock *sk = skb->sk; 4198b1faf566SEric Dumazet 4199b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4200b1faf566SEric Dumazet } 4201b1faf566SEric Dumazet 42028605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 42038605330aSSoheil Hassas Yeganeh { 42048605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 42058605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 42068605330aSSoheil Hassas Yeganeh */ 42078605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 42088605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 42098605330aSSoheil Hassas Yeganeh } 42108605330aSSoheil Hassas Yeganeh 4211b1faf566SEric Dumazet /* 4212b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4213b1faf566SEric Dumazet */ 4214b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4215b1faf566SEric Dumazet { 4216b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 421795c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 4218b1faf566SEric Dumazet return -ENOMEM; 4219b1faf566SEric Dumazet 4220b1faf566SEric Dumazet skb_orphan(skb); 4221b1faf566SEric Dumazet skb->sk = sk; 4222b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4223b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 42248605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4225b1faf566SEric Dumazet 4226abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4227abb57ea4SEric Dumazet skb_dst_force(skb); 4228abb57ea4SEric Dumazet 4229b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4230b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 42316e5d58fdSVinicius Costa Gomes sk->sk_error_report(sk); 4232b1faf566SEric Dumazet return 0; 4233b1faf566SEric Dumazet } 4234b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4235b1faf566SEric Dumazet 423683a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 423783a1a1a7SSoheil Hassas Yeganeh { 423883a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 423983a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 424083a1a1a7SSoheil Hassas Yeganeh } 424183a1a1a7SSoheil Hassas Yeganeh 4242364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4243364a9e93SWillem de Bruijn { 4244364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 424583a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 424683a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4247997d5c3fSEric Dumazet unsigned long flags; 4248364a9e93SWillem de Bruijn 4249997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4250364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 425138b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 425283a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 425338b25793SSoheil Hassas Yeganeh if (icmp_next) 425438b25793SSoheil Hassas Yeganeh sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin; 425538b25793SSoheil Hassas Yeganeh } 4256997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4257364a9e93SWillem de Bruijn 425883a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 425983a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 426083a1a1a7SSoheil Hassas Yeganeh 426183a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4262364a9e93SWillem de Bruijn sk->sk_error_report(sk); 4263364a9e93SWillem de Bruijn 4264364a9e93SWillem de Bruijn return skb; 4265364a9e93SWillem de Bruijn } 4266364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4267364a9e93SWillem de Bruijn 4268cab41c47SAlexander Duyck /** 4269cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4270cab41c47SAlexander Duyck * @skb: the skb to clone 4271cab41c47SAlexander Duyck * 4272cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4273cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4274cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4275cab41c47SAlexander Duyck * 4276cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4277cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4278cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4279cab41c47SAlexander Duyck * the sk_error_queue. 4280cab41c47SAlexander Duyck */ 428162bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 428262bccb8cSAlexander Duyck { 428362bccb8cSAlexander Duyck struct sock *sk = skb->sk; 428462bccb8cSAlexander Duyck struct sk_buff *clone; 428562bccb8cSAlexander Duyck 428641c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 428762bccb8cSAlexander Duyck return NULL; 428862bccb8cSAlexander Duyck 428962bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 429062bccb8cSAlexander Duyck if (!clone) { 429162bccb8cSAlexander Duyck sock_put(sk); 429262bccb8cSAlexander Duyck return NULL; 429362bccb8cSAlexander Duyck } 429462bccb8cSAlexander Duyck 429562bccb8cSAlexander Duyck clone->sk = sk; 429662bccb8cSAlexander Duyck clone->destructor = sock_efree; 429762bccb8cSAlexander Duyck 429862bccb8cSAlexander Duyck return clone; 429962bccb8cSAlexander Duyck } 430062bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 430162bccb8cSAlexander Duyck 430237846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 430337846ef0SAlexander Duyck struct sock *sk, 43044ef1b286SSoheil Hassas Yeganeh int tstype, 43054ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4306ac45f602SPatrick Ohly { 4307ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4308ac45f602SPatrick Ohly int err; 4309ac45f602SPatrick Ohly 43104ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 43114ef1b286SSoheil Hassas Yeganeh 4312ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4313ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4314ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4315ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4316e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 43174ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 43181862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 43194ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 432009c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 4321ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 4322ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 43234ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 43244ed2d765SWillem de Bruijn } 432529030374SEric Dumazet 4326ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 432729030374SEric Dumazet 4328ac45f602SPatrick Ohly if (err) 4329ac45f602SPatrick Ohly kfree_skb(skb); 4330ac45f602SPatrick Ohly } 433137846ef0SAlexander Duyck 4332b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4333b245be1fSWillem de Bruijn { 4334b245be1fSWillem de Bruijn bool ret; 4335b245be1fSWillem de Bruijn 4336b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 4337b245be1fSWillem de Bruijn return true; 4338b245be1fSWillem de Bruijn 4339b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4340b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4341b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4342b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4343b245be1fSWillem de Bruijn return ret; 4344b245be1fSWillem de Bruijn } 4345b245be1fSWillem de Bruijn 434637846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 434737846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 434837846ef0SAlexander Duyck { 434937846ef0SAlexander Duyck struct sock *sk = skb->sk; 435037846ef0SAlexander Duyck 4351b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 435235b99dffSWillem de Bruijn goto err; 4353b245be1fSWillem de Bruijn 43549ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 43559ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 43569ac25fc0SEric Dumazet */ 435741c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 435837846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 43594ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 436037846ef0SAlexander Duyck sock_put(sk); 436135b99dffSWillem de Bruijn return; 436237846ef0SAlexander Duyck } 436335b99dffSWillem de Bruijn 436435b99dffSWillem de Bruijn err: 436535b99dffSWillem de Bruijn kfree_skb(skb); 43669ac25fc0SEric Dumazet } 436737846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 436837846ef0SAlexander Duyck 436937846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 437037846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 437137846ef0SAlexander Duyck struct sock *sk, int tstype) 437237846ef0SAlexander Duyck { 437337846ef0SAlexander Duyck struct sk_buff *skb; 43744ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 437537846ef0SAlexander Duyck 43763a8dd971SWillem de Bruijn if (!sk) 43773a8dd971SWillem de Bruijn return; 43783a8dd971SWillem de Bruijn 4379b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4380b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4381b50a5c70SMiroslav Lichvar return; 4382b50a5c70SMiroslav Lichvar 43833a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 43843a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 438537846ef0SAlexander Duyck return; 438637846ef0SAlexander Duyck 43871c885808SFrancis Yan if (tsonly) { 43881c885808SFrancis Yan #ifdef CONFIG_INET 43891c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 43901c885808SFrancis Yan sk->sk_protocol == IPPROTO_TCP && 43914ef1b286SSoheil Hassas Yeganeh sk->sk_type == SOCK_STREAM) { 43921c885808SFrancis Yan skb = tcp_get_timestamping_opt_stats(sk); 43934ef1b286SSoheil Hassas Yeganeh opt_stats = true; 43944ef1b286SSoheil Hassas Yeganeh } else 43951c885808SFrancis Yan #endif 43961c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 43971c885808SFrancis Yan } else { 439837846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 43991c885808SFrancis Yan } 440037846ef0SAlexander Duyck if (!skb) 440137846ef0SAlexander Duyck return; 440237846ef0SAlexander Duyck 440349ca0d8bSWillem de Bruijn if (tsonly) { 4404fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4405fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 440649ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 440749ca0d8bSWillem de Bruijn } 440849ca0d8bSWillem de Bruijn 440949ca0d8bSWillem de Bruijn if (hwtstamps) 441049ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 441149ca0d8bSWillem de Bruijn else 441249ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 441349ca0d8bSWillem de Bruijn 44144ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 441537846ef0SAlexander Duyck } 4416e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4417e7fd2885SWillem de Bruijn 4418e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4419e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4420e7fd2885SWillem de Bruijn { 4421e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 4422e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 4423e7fd2885SWillem de Bruijn } 4424ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4425ac45f602SPatrick Ohly 44266e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 44276e3e939fSJohannes Berg { 44286e3e939fSJohannes Berg struct sock *sk = skb->sk; 44296e3e939fSJohannes Berg struct sock_exterr_skb *serr; 4430dd4f1072SEric Dumazet int err = 1; 44316e3e939fSJohannes Berg 44326e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 44336e3e939fSJohannes Berg skb->wifi_acked = acked; 44346e3e939fSJohannes Berg 44356e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 44366e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 44376e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 44386e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 44396e3e939fSJohannes Berg 4440dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4441dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4442dd4f1072SEric Dumazet */ 444341c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 44446e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4445dd4f1072SEric Dumazet sock_put(sk); 4446dd4f1072SEric Dumazet } 44476e3e939fSJohannes Berg if (err) 44486e3e939fSJohannes Berg kfree_skb(skb); 44496e3e939fSJohannes Berg } 44506e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 44516e3e939fSJohannes Berg 4452f35d9d8aSRusty Russell /** 4453f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4454f35d9d8aSRusty Russell * @skb: the skb to set 4455f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4456f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4457f35d9d8aSRusty Russell * 4458f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4459f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4460f35d9d8aSRusty Russell * 4461f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4462f35d9d8aSRusty Russell * returns false you should drop the packet. 4463f35d9d8aSRusty Russell */ 4464f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4465f35d9d8aSRusty Russell { 446652b5d6f5SEric Dumazet u32 csum_end = (u32)start + (u32)off + sizeof(__sum16); 446752b5d6f5SEric Dumazet u32 csum_start = skb_headroom(skb) + (u32)start; 446852b5d6f5SEric Dumazet 446952b5d6f5SEric Dumazet if (unlikely(csum_start > U16_MAX || csum_end > skb_headlen(skb))) { 447052b5d6f5SEric Dumazet net_warn_ratelimited("bad partial csum: csum=%u/%u headroom=%u headlen=%u\n", 447152b5d6f5SEric Dumazet start, off, skb_headroom(skb), skb_headlen(skb)); 4472f35d9d8aSRusty Russell return false; 4473f35d9d8aSRusty Russell } 4474f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 447552b5d6f5SEric Dumazet skb->csum_start = csum_start; 4476f35d9d8aSRusty Russell skb->csum_offset = off; 4477e5d5decaSJason Wang skb_set_transport_header(skb, start); 4478f35d9d8aSRusty Russell return true; 4479f35d9d8aSRusty Russell } 4480b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4481f35d9d8aSRusty Russell 4482ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4483ed1f50c3SPaul Durrant unsigned int max) 4484ed1f50c3SPaul Durrant { 4485ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4486ed1f50c3SPaul Durrant return 0; 4487ed1f50c3SPaul Durrant 4488ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4489ed1f50c3SPaul Durrant * won't need to do it again. 4490ed1f50c3SPaul Durrant */ 4491ed1f50c3SPaul Durrant if (max > skb->len) 4492ed1f50c3SPaul Durrant max = skb->len; 4493ed1f50c3SPaul Durrant 4494ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4495ed1f50c3SPaul Durrant return -ENOMEM; 4496ed1f50c3SPaul Durrant 4497ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4498ed1f50c3SPaul Durrant return -EPROTO; 4499ed1f50c3SPaul Durrant 4500ed1f50c3SPaul Durrant return 0; 4501ed1f50c3SPaul Durrant } 4502ed1f50c3SPaul Durrant 4503f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4504f9708b43SJan Beulich 4505f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4506f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4507f9708b43SJan Beulich unsigned int off) 4508f9708b43SJan Beulich { 4509f9708b43SJan Beulich switch (proto) { 4510f9708b43SJan Beulich int err; 4511f9708b43SJan Beulich 4512f9708b43SJan Beulich case IPPROTO_TCP: 4513f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4514f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 4515f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4516f9708b43SJan Beulich offsetof(struct tcphdr, 4517f9708b43SJan Beulich check))) 4518f9708b43SJan Beulich err = -EPROTO; 4519f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4520f9708b43SJan Beulich 4521f9708b43SJan Beulich case IPPROTO_UDP: 4522f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4523f9708b43SJan Beulich off + sizeof(struct udphdr)); 4524f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4525f9708b43SJan Beulich offsetof(struct udphdr, 4526f9708b43SJan Beulich check))) 4527f9708b43SJan Beulich err = -EPROTO; 4528f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4529f9708b43SJan Beulich } 4530f9708b43SJan Beulich 4531f9708b43SJan Beulich return ERR_PTR(-EPROTO); 4532f9708b43SJan Beulich } 4533f9708b43SJan Beulich 4534ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4535ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 4536ed1f50c3SPaul Durrant */ 4537ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 4538ed1f50c3SPaul Durrant 4539f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4540ed1f50c3SPaul Durrant { 4541ed1f50c3SPaul Durrant unsigned int off; 4542ed1f50c3SPaul Durrant bool fragment; 4543f9708b43SJan Beulich __sum16 *csum; 4544ed1f50c3SPaul Durrant int err; 4545ed1f50c3SPaul Durrant 4546ed1f50c3SPaul Durrant fragment = false; 4547ed1f50c3SPaul Durrant 4548ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4549ed1f50c3SPaul Durrant sizeof(struct iphdr), 4550ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 4551ed1f50c3SPaul Durrant if (err < 0) 4552ed1f50c3SPaul Durrant goto out; 4553ed1f50c3SPaul Durrant 4554ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 4555ed1f50c3SPaul Durrant fragment = true; 4556ed1f50c3SPaul Durrant 4557ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 4558ed1f50c3SPaul Durrant 4559ed1f50c3SPaul Durrant err = -EPROTO; 4560ed1f50c3SPaul Durrant 4561ed1f50c3SPaul Durrant if (fragment) 4562ed1f50c3SPaul Durrant goto out; 4563ed1f50c3SPaul Durrant 4564f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 4565f9708b43SJan Beulich if (IS_ERR(csum)) 4566f9708b43SJan Beulich return PTR_ERR(csum); 4567ed1f50c3SPaul Durrant 4568ed1f50c3SPaul Durrant if (recalculate) 4569f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 4570ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 4571ed1f50c3SPaul Durrant skb->len - off, 4572f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 4573ed1f50c3SPaul Durrant err = 0; 4574ed1f50c3SPaul Durrant 4575ed1f50c3SPaul Durrant out: 4576ed1f50c3SPaul Durrant return err; 4577ed1f50c3SPaul Durrant } 4578ed1f50c3SPaul Durrant 4579ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4580ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 4581ed1f50c3SPaul Durrant */ 4582ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 4583ed1f50c3SPaul Durrant 4584ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 4585ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 4586ed1f50c3SPaul Durrant 4587ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 4588ed1f50c3SPaul Durrant { 4589ed1f50c3SPaul Durrant int err; 4590ed1f50c3SPaul Durrant u8 nexthdr; 4591ed1f50c3SPaul Durrant unsigned int off; 4592ed1f50c3SPaul Durrant unsigned int len; 4593ed1f50c3SPaul Durrant bool fragment; 4594ed1f50c3SPaul Durrant bool done; 4595f9708b43SJan Beulich __sum16 *csum; 4596ed1f50c3SPaul Durrant 4597ed1f50c3SPaul Durrant fragment = false; 4598ed1f50c3SPaul Durrant done = false; 4599ed1f50c3SPaul Durrant 4600ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 4601ed1f50c3SPaul Durrant 4602ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 4603ed1f50c3SPaul Durrant if (err < 0) 4604ed1f50c3SPaul Durrant goto out; 4605ed1f50c3SPaul Durrant 4606ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 4607ed1f50c3SPaul Durrant 4608ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 4609ed1f50c3SPaul Durrant while (off <= len && !done) { 4610ed1f50c3SPaul Durrant switch (nexthdr) { 4611ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 4612ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 4613ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 4614ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 4615ed1f50c3SPaul Durrant 4616ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4617ed1f50c3SPaul Durrant off + 4618ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 4619ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4620ed1f50c3SPaul Durrant if (err < 0) 4621ed1f50c3SPaul Durrant goto out; 4622ed1f50c3SPaul Durrant 4623ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4624ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4625ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 4626ed1f50c3SPaul Durrant break; 4627ed1f50c3SPaul Durrant } 4628ed1f50c3SPaul Durrant case IPPROTO_AH: { 4629ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 4630ed1f50c3SPaul Durrant 4631ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4632ed1f50c3SPaul Durrant off + 4633ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 4634ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4635ed1f50c3SPaul Durrant if (err < 0) 4636ed1f50c3SPaul Durrant goto out; 4637ed1f50c3SPaul Durrant 4638ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4639ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4640ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 4641ed1f50c3SPaul Durrant break; 4642ed1f50c3SPaul Durrant } 4643ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 4644ed1f50c3SPaul Durrant struct frag_hdr *hp; 4645ed1f50c3SPaul Durrant 4646ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4647ed1f50c3SPaul Durrant off + 4648ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 4649ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4650ed1f50c3SPaul Durrant if (err < 0) 4651ed1f50c3SPaul Durrant goto out; 4652ed1f50c3SPaul Durrant 4653ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 4654ed1f50c3SPaul Durrant 4655ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4656ed1f50c3SPaul Durrant fragment = true; 4657ed1f50c3SPaul Durrant 4658ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4659ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 4660ed1f50c3SPaul Durrant break; 4661ed1f50c3SPaul Durrant } 4662ed1f50c3SPaul Durrant default: 4663ed1f50c3SPaul Durrant done = true; 4664ed1f50c3SPaul Durrant break; 4665ed1f50c3SPaul Durrant } 4666ed1f50c3SPaul Durrant } 4667ed1f50c3SPaul Durrant 4668ed1f50c3SPaul Durrant err = -EPROTO; 4669ed1f50c3SPaul Durrant 4670ed1f50c3SPaul Durrant if (!done || fragment) 4671ed1f50c3SPaul Durrant goto out; 4672ed1f50c3SPaul Durrant 4673f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 4674f9708b43SJan Beulich if (IS_ERR(csum)) 4675f9708b43SJan Beulich return PTR_ERR(csum); 4676ed1f50c3SPaul Durrant 4677ed1f50c3SPaul Durrant if (recalculate) 4678f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4679ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 4680f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4681ed1f50c3SPaul Durrant err = 0; 4682ed1f50c3SPaul Durrant 4683ed1f50c3SPaul Durrant out: 4684ed1f50c3SPaul Durrant return err; 4685ed1f50c3SPaul Durrant } 4686ed1f50c3SPaul Durrant 4687ed1f50c3SPaul Durrant /** 4688ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4689ed1f50c3SPaul Durrant * @skb: the skb to set up 4690ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4691ed1f50c3SPaul Durrant */ 4692ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4693ed1f50c3SPaul Durrant { 4694ed1f50c3SPaul Durrant int err; 4695ed1f50c3SPaul Durrant 4696ed1f50c3SPaul Durrant switch (skb->protocol) { 4697ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4698f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4699ed1f50c3SPaul Durrant break; 4700ed1f50c3SPaul Durrant 4701ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4702ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4703ed1f50c3SPaul Durrant break; 4704ed1f50c3SPaul Durrant 4705ed1f50c3SPaul Durrant default: 4706ed1f50c3SPaul Durrant err = -EPROTO; 4707ed1f50c3SPaul Durrant break; 4708ed1f50c3SPaul Durrant } 4709ed1f50c3SPaul Durrant 4710ed1f50c3SPaul Durrant return err; 4711ed1f50c3SPaul Durrant } 4712ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4713ed1f50c3SPaul Durrant 47149afd85c9SLinus Lüssing /** 47159afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 47169afd85c9SLinus Lüssing * @skb: the skb to check 47179afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 47189afd85c9SLinus Lüssing * 47199afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 47209afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 47219afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 47229afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 47239afd85c9SLinus Lüssing * 4724a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4725a516993fSLinus Lüssing * differs from the provided skb. 47269afd85c9SLinus Lüssing */ 47279afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 47289afd85c9SLinus Lüssing unsigned int transport_len) 47299afd85c9SLinus Lüssing { 47309afd85c9SLinus Lüssing struct sk_buff *skb_chk; 47319afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 47329afd85c9SLinus Lüssing int ret; 47339afd85c9SLinus Lüssing 4734a516993fSLinus Lüssing if (skb->len < len) 47359afd85c9SLinus Lüssing return NULL; 4736a516993fSLinus Lüssing else if (skb->len == len) 47379afd85c9SLinus Lüssing return skb; 47389afd85c9SLinus Lüssing 47399afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 47409afd85c9SLinus Lüssing if (!skb_chk) 47419afd85c9SLinus Lüssing return NULL; 47429afd85c9SLinus Lüssing 47439afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 47449afd85c9SLinus Lüssing if (ret) { 47459afd85c9SLinus Lüssing kfree_skb(skb_chk); 47469afd85c9SLinus Lüssing return NULL; 47479afd85c9SLinus Lüssing } 47489afd85c9SLinus Lüssing 47499afd85c9SLinus Lüssing return skb_chk; 47509afd85c9SLinus Lüssing } 47519afd85c9SLinus Lüssing 47529afd85c9SLinus Lüssing /** 47539afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 47549afd85c9SLinus Lüssing * @skb: the skb to check 47559afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 47569afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 47579afd85c9SLinus Lüssing * 47589afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 47599afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 47609afd85c9SLinus Lüssing * 47619afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 47629afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 47639afd85c9SLinus Lüssing * 4764a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4765a516993fSLinus Lüssing * differs from the provided skb. 47669afd85c9SLinus Lüssing */ 47679afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 47689afd85c9SLinus Lüssing unsigned int transport_len, 47699afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 47709afd85c9SLinus Lüssing { 47719afd85c9SLinus Lüssing struct sk_buff *skb_chk; 47729afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 4773fcba67c9SLinus Lüssing __sum16 ret; 47749afd85c9SLinus Lüssing 47759afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 47769afd85c9SLinus Lüssing if (!skb_chk) 4777a516993fSLinus Lüssing goto err; 47789afd85c9SLinus Lüssing 4779a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 4780a516993fSLinus Lüssing goto err; 47819afd85c9SLinus Lüssing 47829b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 47839afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 47849b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 47859afd85c9SLinus Lüssing 4786a516993fSLinus Lüssing if (ret) 4787a516993fSLinus Lüssing goto err; 47889afd85c9SLinus Lüssing 47899afd85c9SLinus Lüssing return skb_chk; 4790a516993fSLinus Lüssing 4791a516993fSLinus Lüssing err: 4792a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 4793a516993fSLinus Lüssing kfree_skb(skb_chk); 4794a516993fSLinus Lüssing 4795a516993fSLinus Lüssing return NULL; 4796a516993fSLinus Lüssing 47979afd85c9SLinus Lüssing } 47989afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 47999afd85c9SLinus Lüssing 48004497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 48014497b076SBen Hutchings { 4802e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4803e87cc472SJoe Perches skb->dev->name); 48044497b076SBen Hutchings } 48054497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4806bad43ca8SEric Dumazet 4807bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4808bad43ca8SEric Dumazet { 48093d861f66SEric Dumazet if (head_stolen) { 48103d861f66SEric Dumazet skb_release_head_state(skb); 4811bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 48123d861f66SEric Dumazet } else { 4813bad43ca8SEric Dumazet __kfree_skb(skb); 4814bad43ca8SEric Dumazet } 48153d861f66SEric Dumazet } 4816bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4817bad43ca8SEric Dumazet 4818bad43ca8SEric Dumazet /** 4819bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4820bad43ca8SEric Dumazet * @to: prior buffer 4821bad43ca8SEric Dumazet * @from: buffer to add 4822bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4823c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4824bad43ca8SEric Dumazet */ 4825bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4826bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4827bad43ca8SEric Dumazet { 4828c818fa9eSEric Dumazet struct skb_shared_info *to_shinfo, *from_shinfo; 4829bad43ca8SEric Dumazet int i, delta, len = from->len; 4830bad43ca8SEric Dumazet 4831bad43ca8SEric Dumazet *fragstolen = false; 4832bad43ca8SEric Dumazet 4833bad43ca8SEric Dumazet if (skb_cloned(to)) 4834bad43ca8SEric Dumazet return false; 4835bad43ca8SEric Dumazet 4836bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4837e93a0435SEric Dumazet if (len) 4838bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4839bad43ca8SEric Dumazet *delta_truesize = 0; 4840bad43ca8SEric Dumazet return true; 4841bad43ca8SEric Dumazet } 4842bad43ca8SEric Dumazet 4843c818fa9eSEric Dumazet to_shinfo = skb_shinfo(to); 4844c818fa9eSEric Dumazet from_shinfo = skb_shinfo(from); 4845c818fa9eSEric Dumazet if (to_shinfo->frag_list || from_shinfo->frag_list) 4846bad43ca8SEric Dumazet return false; 48471f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 48481f8b977aSWillem de Bruijn return false; 4849bad43ca8SEric Dumazet 4850bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4851bad43ca8SEric Dumazet struct page *page; 4852bad43ca8SEric Dumazet unsigned int offset; 4853bad43ca8SEric Dumazet 4854c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 4855c818fa9eSEric Dumazet from_shinfo->nr_frags >= MAX_SKB_FRAGS) 4856bad43ca8SEric Dumazet return false; 4857bad43ca8SEric Dumazet 4858bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4859bad43ca8SEric Dumazet return false; 4860bad43ca8SEric Dumazet 4861bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4862bad43ca8SEric Dumazet 4863bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4864bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4865bad43ca8SEric Dumazet 4866c818fa9eSEric Dumazet skb_fill_page_desc(to, to_shinfo->nr_frags, 4867bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4868bad43ca8SEric Dumazet *fragstolen = true; 4869bad43ca8SEric Dumazet } else { 4870c818fa9eSEric Dumazet if (to_shinfo->nr_frags + 4871c818fa9eSEric Dumazet from_shinfo->nr_frags > MAX_SKB_FRAGS) 4872bad43ca8SEric Dumazet return false; 4873bad43ca8SEric Dumazet 4874f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4875bad43ca8SEric Dumazet } 4876bad43ca8SEric Dumazet 4877bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4878bad43ca8SEric Dumazet 4879c818fa9eSEric Dumazet memcpy(to_shinfo->frags + to_shinfo->nr_frags, 4880c818fa9eSEric Dumazet from_shinfo->frags, 4881c818fa9eSEric Dumazet from_shinfo->nr_frags * sizeof(skb_frag_t)); 4882c818fa9eSEric Dumazet to_shinfo->nr_frags += from_shinfo->nr_frags; 4883bad43ca8SEric Dumazet 4884bad43ca8SEric Dumazet if (!skb_cloned(from)) 4885c818fa9eSEric Dumazet from_shinfo->nr_frags = 0; 4886bad43ca8SEric Dumazet 48878ea853fdSLi RongQing /* if the skb is not cloned this does nothing 48888ea853fdSLi RongQing * since we set nr_frags to 0. 48898ea853fdSLi RongQing */ 4890c818fa9eSEric Dumazet for (i = 0; i < from_shinfo->nr_frags; i++) 4891c818fa9eSEric Dumazet __skb_frag_ref(&from_shinfo->frags[i]); 4892bad43ca8SEric Dumazet 4893bad43ca8SEric Dumazet to->truesize += delta; 4894bad43ca8SEric Dumazet to->len += len; 4895bad43ca8SEric Dumazet to->data_len += len; 4896bad43ca8SEric Dumazet 4897bad43ca8SEric Dumazet *delta_truesize = delta; 4898bad43ca8SEric Dumazet return true; 4899bad43ca8SEric Dumazet } 4900bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4901621e84d6SNicolas Dichtel 4902621e84d6SNicolas Dichtel /** 49038b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4904621e84d6SNicolas Dichtel * 4905621e84d6SNicolas Dichtel * @skb: buffer to clean 49068b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4907621e84d6SNicolas Dichtel * 49088b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 49098b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 49108b27f277SNicolas Dichtel * operations. 49118b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 49128b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 49138b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4914621e84d6SNicolas Dichtel */ 49158b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4916621e84d6SNicolas Dichtel { 4917621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4918621e84d6SNicolas Dichtel skb->skb_iif = 0; 491960ff7467SWANG Cong skb->ignore_df = 0; 4920621e84d6SNicolas Dichtel skb_dst_drop(skb); 4921621e84d6SNicolas Dichtel secpath_reset(skb); 4922621e84d6SNicolas Dichtel nf_reset(skb); 4923621e84d6SNicolas Dichtel nf_reset_trace(skb); 4924213dd74aSHerbert Xu 49256f9a5069SPetr Machata #ifdef CONFIG_NET_SWITCHDEV 49266f9a5069SPetr Machata skb->offload_fwd_mark = 0; 4927875e8939SIdo Schimmel skb->offload_l3_fwd_mark = 0; 49286f9a5069SPetr Machata #endif 49296f9a5069SPetr Machata 4930213dd74aSHerbert Xu if (!xnet) 4931213dd74aSHerbert Xu return; 4932213dd74aSHerbert Xu 49332b5ec1a5SYe Yin ipvs_reset(skb); 4934213dd74aSHerbert Xu skb->mark = 0; 4935c47d8c2fSJesus Sanchez-Palencia skb->tstamp = 0; 4936621e84d6SNicolas Dichtel } 4937621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4938de960aa9SFlorian Westphal 4939de960aa9SFlorian Westphal /** 4940de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4941de960aa9SFlorian Westphal * 4942de960aa9SFlorian Westphal * @skb: GSO skb 4943de960aa9SFlorian Westphal * 4944de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4945de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4946de960aa9SFlorian Westphal * 4947de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4948de960aa9SFlorian Westphal */ 4949a4a77718SDaniel Axtens static unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4950de960aa9SFlorian Westphal { 4951de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4952f993bc25SFlorian Westphal unsigned int thlen = 0; 4953f993bc25SFlorian Westphal 4954f993bc25SFlorian Westphal if (skb->encapsulation) { 4955f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4956f993bc25SFlorian Westphal skb_transport_header(skb); 4957de960aa9SFlorian Westphal 4958de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4959f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4960f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4961f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 49621dd27cdeSDaniel Axtens } else if (unlikely(skb_is_gso_sctp(skb))) { 496390017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 4964ee80d1ebSWillem de Bruijn } else if (shinfo->gso_type & SKB_GSO_UDP_L4) { 4965ee80d1ebSWillem de Bruijn thlen = sizeof(struct udphdr); 4966f993bc25SFlorian Westphal } 49676d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 49686d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 49696d39d589SFlorian Westphal * accounted for. 49706d39d589SFlorian Westphal */ 4971f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4972de960aa9SFlorian Westphal } 4973a4a77718SDaniel Axtens 4974a4a77718SDaniel Axtens /** 4975a4a77718SDaniel Axtens * skb_gso_network_seglen - Return length of individual segments of a gso packet 4976a4a77718SDaniel Axtens * 4977a4a77718SDaniel Axtens * @skb: GSO skb 4978a4a77718SDaniel Axtens * 4979a4a77718SDaniel Axtens * skb_gso_network_seglen is used to determine the real size of the 4980a4a77718SDaniel Axtens * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP). 4981a4a77718SDaniel Axtens * 4982a4a77718SDaniel Axtens * The MAC/L2 header is not accounted for. 4983a4a77718SDaniel Axtens */ 4984a4a77718SDaniel Axtens static unsigned int skb_gso_network_seglen(const struct sk_buff *skb) 4985a4a77718SDaniel Axtens { 4986a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - 4987a4a77718SDaniel Axtens skb_network_header(skb); 4988a4a77718SDaniel Axtens 4989a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 4990a4a77718SDaniel Axtens } 4991a4a77718SDaniel Axtens 4992a4a77718SDaniel Axtens /** 4993a4a77718SDaniel Axtens * skb_gso_mac_seglen - Return length of individual segments of a gso packet 4994a4a77718SDaniel Axtens * 4995a4a77718SDaniel Axtens * @skb: GSO skb 4996a4a77718SDaniel Axtens * 4997a4a77718SDaniel Axtens * skb_gso_mac_seglen is used to determine the real size of the 4998a4a77718SDaniel Axtens * individual segments, including MAC/L2, Layer3 (IP, IPv6) and L4 4999a4a77718SDaniel Axtens * headers (TCP/UDP). 5000a4a77718SDaniel Axtens */ 5001a4a77718SDaniel Axtens static unsigned int skb_gso_mac_seglen(const struct sk_buff *skb) 5002a4a77718SDaniel Axtens { 5003a4a77718SDaniel Axtens unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb); 5004a4a77718SDaniel Axtens 5005a4a77718SDaniel Axtens return hdr_len + skb_gso_transport_seglen(skb); 5006a4a77718SDaniel Axtens } 50070d5501c1SVlad Yasevich 5008ae7ef81eSMarcelo Ricardo Leitner /** 50092b16f048SDaniel Axtens * skb_gso_size_check - check the skb size, considering GSO_BY_FRAGS 50102b16f048SDaniel Axtens * 50112b16f048SDaniel Axtens * There are a couple of instances where we have a GSO skb, and we 50122b16f048SDaniel Axtens * want to determine what size it would be after it is segmented. 50132b16f048SDaniel Axtens * 50142b16f048SDaniel Axtens * We might want to check: 50152b16f048SDaniel Axtens * - L3+L4+payload size (e.g. IP forwarding) 50162b16f048SDaniel Axtens * - L2+L3+L4+payload size (e.g. sanity check before passing to driver) 50172b16f048SDaniel Axtens * 50182b16f048SDaniel Axtens * This is a helper to do that correctly considering GSO_BY_FRAGS. 50192b16f048SDaniel Axtens * 502049682bfaSMathieu Malaterre * @skb: GSO skb 502149682bfaSMathieu Malaterre * 50222b16f048SDaniel Axtens * @seg_len: The segmented length (from skb_gso_*_seglen). In the 50232b16f048SDaniel Axtens * GSO_BY_FRAGS case this will be [header sizes + GSO_BY_FRAGS]. 50242b16f048SDaniel Axtens * 50252b16f048SDaniel Axtens * @max_len: The maximum permissible length. 50262b16f048SDaniel Axtens * 50272b16f048SDaniel Axtens * Returns true if the segmented length <= max length. 50282b16f048SDaniel Axtens */ 50292b16f048SDaniel Axtens static inline bool skb_gso_size_check(const struct sk_buff *skb, 50302b16f048SDaniel Axtens unsigned int seg_len, 50312b16f048SDaniel Axtens unsigned int max_len) { 50322b16f048SDaniel Axtens const struct skb_shared_info *shinfo = skb_shinfo(skb); 50332b16f048SDaniel Axtens const struct sk_buff *iter; 50342b16f048SDaniel Axtens 50352b16f048SDaniel Axtens if (shinfo->gso_size != GSO_BY_FRAGS) 50362b16f048SDaniel Axtens return seg_len <= max_len; 50372b16f048SDaniel Axtens 50382b16f048SDaniel Axtens /* Undo this so we can re-use header sizes */ 50392b16f048SDaniel Axtens seg_len -= GSO_BY_FRAGS; 50402b16f048SDaniel Axtens 50412b16f048SDaniel Axtens skb_walk_frags(skb, iter) { 50422b16f048SDaniel Axtens if (seg_len + skb_headlen(iter) > max_len) 50432b16f048SDaniel Axtens return false; 50442b16f048SDaniel Axtens } 50452b16f048SDaniel Axtens 50462b16f048SDaniel Axtens return true; 50472b16f048SDaniel Axtens } 50482b16f048SDaniel Axtens 50492b16f048SDaniel Axtens /** 5050779b7931SDaniel Axtens * skb_gso_validate_network_len - Will a split GSO skb fit into a given MTU? 5051ae7ef81eSMarcelo Ricardo Leitner * 5052ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 505376f21b99SDavid S. Miller * @mtu: MTU to validate against 5054ae7ef81eSMarcelo Ricardo Leitner * 5055779b7931SDaniel Axtens * skb_gso_validate_network_len validates if a given skb will fit a 5056779b7931SDaniel Axtens * wanted MTU once split. It considers L3 headers, L4 headers, and the 5057779b7931SDaniel Axtens * payload. 5058ae7ef81eSMarcelo Ricardo Leitner */ 5059779b7931SDaniel Axtens bool skb_gso_validate_network_len(const struct sk_buff *skb, unsigned int mtu) 5060ae7ef81eSMarcelo Ricardo Leitner { 50612b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_network_seglen(skb), mtu); 5062ae7ef81eSMarcelo Ricardo Leitner } 5063779b7931SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_network_len); 5064ae7ef81eSMarcelo Ricardo Leitner 50652b16f048SDaniel Axtens /** 50662b16f048SDaniel Axtens * skb_gso_validate_mac_len - Will a split GSO skb fit in a given length? 50672b16f048SDaniel Axtens * 50682b16f048SDaniel Axtens * @skb: GSO skb 50692b16f048SDaniel Axtens * @len: length to validate against 50702b16f048SDaniel Axtens * 50712b16f048SDaniel Axtens * skb_gso_validate_mac_len validates if a given skb will fit a wanted 50722b16f048SDaniel Axtens * length once split, including L2, L3 and L4 headers and the payload. 50732b16f048SDaniel Axtens */ 50742b16f048SDaniel Axtens bool skb_gso_validate_mac_len(const struct sk_buff *skb, unsigned int len) 50752b16f048SDaniel Axtens { 50762b16f048SDaniel Axtens return skb_gso_size_check(skb, skb_gso_mac_seglen(skb), len); 50772b16f048SDaniel Axtens } 50782b16f048SDaniel Axtens EXPORT_SYMBOL_GPL(skb_gso_validate_mac_len); 50792b16f048SDaniel Axtens 50800d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 50810d5501c1SVlad Yasevich { 50824bbb3e0eSToshiaki Makita int mac_len; 50834bbb3e0eSToshiaki Makita 50840d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 50850d5501c1SVlad Yasevich kfree_skb(skb); 50860d5501c1SVlad Yasevich return NULL; 50870d5501c1SVlad Yasevich } 50880d5501c1SVlad Yasevich 50894bbb3e0eSToshiaki Makita mac_len = skb->data - skb_mac_header(skb); 5090ae474573SToshiaki Makita if (likely(mac_len > VLAN_HLEN + ETH_TLEN)) { 50914bbb3e0eSToshiaki Makita memmove(skb_mac_header(skb) + VLAN_HLEN, skb_mac_header(skb), 50924bbb3e0eSToshiaki Makita mac_len - VLAN_HLEN - ETH_TLEN); 5093ae474573SToshiaki Makita } 50940d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 50950d5501c1SVlad Yasevich return skb; 50960d5501c1SVlad Yasevich } 50970d5501c1SVlad Yasevich 50980d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 50990d5501c1SVlad Yasevich { 51000d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 51010d5501c1SVlad Yasevich u16 vlan_tci; 51020d5501c1SVlad Yasevich 5103df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 51040d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 51050d5501c1SVlad Yasevich return skb; 51060d5501c1SVlad Yasevich } 51070d5501c1SVlad Yasevich 51080d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 51090d5501c1SVlad Yasevich if (unlikely(!skb)) 51100d5501c1SVlad Yasevich goto err_free; 51110d5501c1SVlad Yasevich 51120d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 51130d5501c1SVlad Yasevich goto err_free; 51140d5501c1SVlad Yasevich 51150d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 51160d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 51170d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 51180d5501c1SVlad Yasevich 51190d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 51200d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 51210d5501c1SVlad Yasevich 51220d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 51230d5501c1SVlad Yasevich if (unlikely(!skb)) 51240d5501c1SVlad Yasevich goto err_free; 51250d5501c1SVlad Yasevich 51260d5501c1SVlad Yasevich skb_reset_network_header(skb); 51270d5501c1SVlad Yasevich skb_reset_transport_header(skb); 51280d5501c1SVlad Yasevich skb_reset_mac_len(skb); 51290d5501c1SVlad Yasevich 51300d5501c1SVlad Yasevich return skb; 51310d5501c1SVlad Yasevich 51320d5501c1SVlad Yasevich err_free: 51330d5501c1SVlad Yasevich kfree_skb(skb); 51340d5501c1SVlad Yasevich return NULL; 51350d5501c1SVlad Yasevich } 51360d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 51372e4e4410SEric Dumazet 5138e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 5139e2195121SJiri Pirko { 5140e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 5141e2195121SJiri Pirko return -ENOMEM; 5142e2195121SJiri Pirko 5143e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 5144e2195121SJiri Pirko return 0; 5145e2195121SJiri Pirko 5146e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 5147e2195121SJiri Pirko } 5148e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5149e2195121SJiri Pirko 5150bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5151bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5152bfca4c52SShmulik Ladkani */ 5153bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 515493515d53SJiri Pirko { 515593515d53SJiri Pirko struct vlan_hdr *vhdr; 5156b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 515793515d53SJiri Pirko int err; 515893515d53SJiri Pirko 5159b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5160b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5161b6a79208SShmulik Ladkani offset)) { 5162b6a79208SShmulik Ladkani return -EINVAL; 5163b6a79208SShmulik Ladkani } 5164b6a79208SShmulik Ladkani 516593515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 516693515d53SJiri Pirko if (unlikely(err)) 5167b6a79208SShmulik Ladkani return err; 516893515d53SJiri Pirko 516993515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 517093515d53SJiri Pirko 517193515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 517293515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 517393515d53SJiri Pirko 517493515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 517593515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 517693515d53SJiri Pirko 517793515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 517893515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 517993515d53SJiri Pirko 518093515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 518193515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 518293515d53SJiri Pirko 518393515d53SJiri Pirko skb_reset_mac_len(skb); 518493515d53SJiri Pirko 518593515d53SJiri Pirko return err; 518693515d53SJiri Pirko } 5187bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 518893515d53SJiri Pirko 5189b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5190b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5191b6a79208SShmulik Ladkani */ 519293515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 519393515d53SJiri Pirko { 519493515d53SJiri Pirko u16 vlan_tci; 519593515d53SJiri Pirko __be16 vlan_proto; 519693515d53SJiri Pirko int err; 519793515d53SJiri Pirko 5198df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 5199b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 520093515d53SJiri Pirko } else { 5201ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 520293515d53SJiri Pirko return 0; 520393515d53SJiri Pirko 520493515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 520593515d53SJiri Pirko if (err) 520693515d53SJiri Pirko return err; 520793515d53SJiri Pirko } 520893515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5209ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 521093515d53SJiri Pirko return 0; 521193515d53SJiri Pirko 521293515d53SJiri Pirko vlan_proto = skb->protocol; 521393515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 521493515d53SJiri Pirko if (unlikely(err)) 521593515d53SJiri Pirko return err; 521693515d53SJiri Pirko 521793515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 521893515d53SJiri Pirko return 0; 521993515d53SJiri Pirko } 522093515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 522193515d53SJiri Pirko 5222b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5223b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5224b6a79208SShmulik Ladkani */ 522593515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 522693515d53SJiri Pirko { 5227df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5228b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 522993515d53SJiri Pirko int err; 523093515d53SJiri Pirko 5231b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5232b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5233b6a79208SShmulik Ladkani offset)) { 5234b6a79208SShmulik Ladkani return -EINVAL; 5235b6a79208SShmulik Ladkani } 5236b6a79208SShmulik Ladkani 523793515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5238df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5239b6a79208SShmulik Ladkani if (err) 524093515d53SJiri Pirko return err; 52419241e2dfSDaniel Borkmann 524293515d53SJiri Pirko skb->protocol = skb->vlan_proto; 524393515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 524493515d53SJiri Pirko 52456b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 524693515d53SJiri Pirko } 524793515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 524893515d53SJiri Pirko return 0; 524993515d53SJiri Pirko } 525093515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 525193515d53SJiri Pirko 52522e4e4410SEric Dumazet /** 52532e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 52542e4e4410SEric Dumazet * 5255de3f0d0eSMasanari Iida * @header_len: size of linear part 5256de3f0d0eSMasanari Iida * @data_len: needed length in frags 5257de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 5258de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 5259de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 52602e4e4410SEric Dumazet * 52612e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 52622e4e4410SEric Dumazet */ 52632e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 52642e4e4410SEric Dumazet unsigned long data_len, 52652e4e4410SEric Dumazet int max_page_order, 52662e4e4410SEric Dumazet int *errcode, 52672e4e4410SEric Dumazet gfp_t gfp_mask) 52682e4e4410SEric Dumazet { 52692e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 52702e4e4410SEric Dumazet unsigned long chunk; 52712e4e4410SEric Dumazet struct sk_buff *skb; 52722e4e4410SEric Dumazet struct page *page; 52732e4e4410SEric Dumazet int i; 52742e4e4410SEric Dumazet 52752e4e4410SEric Dumazet *errcode = -EMSGSIZE; 52762e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 52772e4e4410SEric Dumazet * high order pages... 52782e4e4410SEric Dumazet */ 52792e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 52802e4e4410SEric Dumazet return NULL; 52812e4e4410SEric Dumazet 52822e4e4410SEric Dumazet *errcode = -ENOBUFS; 5283f8c468e8SDavid Rientjes skb = alloc_skb(header_len, gfp_mask); 52842e4e4410SEric Dumazet if (!skb) 52852e4e4410SEric Dumazet return NULL; 52862e4e4410SEric Dumazet 52872e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 52882e4e4410SEric Dumazet 52892e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 52902e4e4410SEric Dumazet int order = max_page_order; 52912e4e4410SEric Dumazet 52922e4e4410SEric Dumazet while (order) { 52932e4e4410SEric Dumazet if (npages >= 1 << order) { 5294d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 52952e4e4410SEric Dumazet __GFP_COMP | 5296d14b56f5SMichal Hocko __GFP_NOWARN, 52972e4e4410SEric Dumazet order); 52982e4e4410SEric Dumazet if (page) 52992e4e4410SEric Dumazet goto fill_page; 53002e4e4410SEric Dumazet /* Do not retry other high order allocations */ 53012e4e4410SEric Dumazet order = 1; 53022e4e4410SEric Dumazet max_page_order = 0; 53032e4e4410SEric Dumazet } 53042e4e4410SEric Dumazet order--; 53052e4e4410SEric Dumazet } 53062e4e4410SEric Dumazet page = alloc_page(gfp_mask); 53072e4e4410SEric Dumazet if (!page) 53082e4e4410SEric Dumazet goto failure; 53092e4e4410SEric Dumazet fill_page: 53102e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 53112e4e4410SEric Dumazet PAGE_SIZE << order); 53122e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 53132e4e4410SEric Dumazet data_len -= chunk; 53142e4e4410SEric Dumazet npages -= 1 << order; 53152e4e4410SEric Dumazet } 53162e4e4410SEric Dumazet return skb; 53172e4e4410SEric Dumazet 53182e4e4410SEric Dumazet failure: 53192e4e4410SEric Dumazet kfree_skb(skb); 53202e4e4410SEric Dumazet return NULL; 53212e4e4410SEric Dumazet } 53222e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 53236fa01ccdSSowmini Varadhan 53246fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 53256fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 53266fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 53276fa01ccdSSowmini Varadhan { 53286fa01ccdSSowmini Varadhan int i; 53296fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 53306fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 53316fa01ccdSSowmini Varadhan u8 *data; 53326fa01ccdSSowmini Varadhan 53336fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 53346fa01ccdSSowmini Varadhan 53356fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 53366fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 53376fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 53386fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 53396fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 53406fa01ccdSSowmini Varadhan if (!data) 53416fa01ccdSSowmini Varadhan return -ENOMEM; 53426fa01ccdSSowmini Varadhan 53436fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 53446fa01ccdSSowmini Varadhan 53456fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 53466fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 53476fa01ccdSSowmini Varadhan skb->len -= off; 53486fa01ccdSSowmini Varadhan 53496fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 53506fa01ccdSSowmini Varadhan skb_shinfo(skb), 53516fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 53526fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 53536fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 53546fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 53556fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 53566fa01ccdSSowmini Varadhan kfree(data); 53576fa01ccdSSowmini Varadhan return -ENOMEM; 53586fa01ccdSSowmini Varadhan } 53596fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 53606fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 53616fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 53626fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 53636fa01ccdSSowmini Varadhan skb_release_data(skb); 53646fa01ccdSSowmini Varadhan } else { 53656fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 53666fa01ccdSSowmini Varadhan * relocate values 53676fa01ccdSSowmini Varadhan */ 53686fa01ccdSSowmini Varadhan skb_free_head(skb); 53696fa01ccdSSowmini Varadhan } 53706fa01ccdSSowmini Varadhan 53716fa01ccdSSowmini Varadhan skb->head = data; 53726fa01ccdSSowmini Varadhan skb->data = data; 53736fa01ccdSSowmini Varadhan skb->head_frag = 0; 53746fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 53756fa01ccdSSowmini Varadhan skb->end = size; 53766fa01ccdSSowmini Varadhan #else 53776fa01ccdSSowmini Varadhan skb->end = skb->head + size; 53786fa01ccdSSowmini Varadhan #endif 53796fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 53806fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 53816fa01ccdSSowmini Varadhan skb->cloned = 0; 53826fa01ccdSSowmini Varadhan skb->hdr_len = 0; 53836fa01ccdSSowmini Varadhan skb->nohdr = 0; 53846fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 53856fa01ccdSSowmini Varadhan 53866fa01ccdSSowmini Varadhan return 0; 53876fa01ccdSSowmini Varadhan } 53886fa01ccdSSowmini Varadhan 53896fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 53906fa01ccdSSowmini Varadhan 53916fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 53926fa01ccdSSowmini Varadhan * pskb_carve() 53936fa01ccdSSowmini Varadhan */ 53946fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 53956fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 53966fa01ccdSSowmini Varadhan gfp_t gfp_mask) 53976fa01ccdSSowmini Varadhan { 53986fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 53996fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 54006fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 54016fa01ccdSSowmini Varadhan 54026fa01ccdSSowmini Varadhan do { 54036fa01ccdSSowmini Varadhan if (!list) { 54046fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 54056fa01ccdSSowmini Varadhan return -EFAULT; 54066fa01ccdSSowmini Varadhan } 54076fa01ccdSSowmini Varadhan if (list->len <= eat) { 54086fa01ccdSSowmini Varadhan /* Eaten as whole. */ 54096fa01ccdSSowmini Varadhan eat -= list->len; 54106fa01ccdSSowmini Varadhan list = list->next; 54116fa01ccdSSowmini Varadhan insp = list; 54126fa01ccdSSowmini Varadhan } else { 54136fa01ccdSSowmini Varadhan /* Eaten partially. */ 54146fa01ccdSSowmini Varadhan if (skb_shared(list)) { 54156fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 54166fa01ccdSSowmini Varadhan if (!clone) 54176fa01ccdSSowmini Varadhan return -ENOMEM; 54186fa01ccdSSowmini Varadhan insp = list->next; 54196fa01ccdSSowmini Varadhan list = clone; 54206fa01ccdSSowmini Varadhan } else { 54216fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 54226fa01ccdSSowmini Varadhan insp = list; 54236fa01ccdSSowmini Varadhan } 54246fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 54256fa01ccdSSowmini Varadhan kfree_skb(clone); 54266fa01ccdSSowmini Varadhan return -ENOMEM; 54276fa01ccdSSowmini Varadhan } 54286fa01ccdSSowmini Varadhan break; 54296fa01ccdSSowmini Varadhan } 54306fa01ccdSSowmini Varadhan } while (eat); 54316fa01ccdSSowmini Varadhan 54326fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 54336fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 54346fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 54356fa01ccdSSowmini Varadhan kfree_skb(list); 54366fa01ccdSSowmini Varadhan } 54376fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 54386fa01ccdSSowmini Varadhan if (clone) { 54396fa01ccdSSowmini Varadhan clone->next = list; 54406fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 54416fa01ccdSSowmini Varadhan } 54426fa01ccdSSowmini Varadhan return 0; 54436fa01ccdSSowmini Varadhan } 54446fa01ccdSSowmini Varadhan 54456fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 54466fa01ccdSSowmini Varadhan * non-linear part of skb 54476fa01ccdSSowmini Varadhan */ 54486fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 54496fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 54506fa01ccdSSowmini Varadhan { 54516fa01ccdSSowmini Varadhan int i, k = 0; 54526fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 54536fa01ccdSSowmini Varadhan u8 *data; 54546fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 54556fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 54566fa01ccdSSowmini Varadhan 54576fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 54586fa01ccdSSowmini Varadhan 54596fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 54606fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 54616fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 54626fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 54636fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 54646fa01ccdSSowmini Varadhan if (!data) 54656fa01ccdSSowmini Varadhan return -ENOMEM; 54666fa01ccdSSowmini Varadhan 54676fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 54686fa01ccdSSowmini Varadhan 54696fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 54706fa01ccdSSowmini Varadhan skb_shinfo(skb), offsetof(struct skb_shared_info, 54716fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 54726fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 54736fa01ccdSSowmini Varadhan kfree(data); 54746fa01ccdSSowmini Varadhan return -ENOMEM; 54756fa01ccdSSowmini Varadhan } 54766fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 54776fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 54786fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 54796fa01ccdSSowmini Varadhan 54806fa01ccdSSowmini Varadhan if (pos + fsize > off) { 54816fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 54826fa01ccdSSowmini Varadhan 54836fa01ccdSSowmini Varadhan if (pos < off) { 54846fa01ccdSSowmini Varadhan /* Split frag. 54856fa01ccdSSowmini Varadhan * We have two variants in this case: 54866fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 54876fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 54886fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 54896fa01ccdSSowmini Varadhan * where splitting is expensive. 54906fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 54916fa01ccdSSowmini Varadhan */ 54926fa01ccdSSowmini Varadhan shinfo->frags[0].page_offset += off - pos; 54936fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 54946fa01ccdSSowmini Varadhan } 54956fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 54966fa01ccdSSowmini Varadhan k++; 54976fa01ccdSSowmini Varadhan } 54986fa01ccdSSowmini Varadhan pos += fsize; 54996fa01ccdSSowmini Varadhan } 55006fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 55016fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 55026fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 55036fa01ccdSSowmini Varadhan 55046fa01ccdSSowmini Varadhan if (k == 0) { 55056fa01ccdSSowmini Varadhan /* split line is in frag list */ 55066fa01ccdSSowmini Varadhan pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask); 55076fa01ccdSSowmini Varadhan } 55086fa01ccdSSowmini Varadhan skb_release_data(skb); 55096fa01ccdSSowmini Varadhan 55106fa01ccdSSowmini Varadhan skb->head = data; 55116fa01ccdSSowmini Varadhan skb->head_frag = 0; 55126fa01ccdSSowmini Varadhan skb->data = data; 55136fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 55146fa01ccdSSowmini Varadhan skb->end = size; 55156fa01ccdSSowmini Varadhan #else 55166fa01ccdSSowmini Varadhan skb->end = skb->head + size; 55176fa01ccdSSowmini Varadhan #endif 55186fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 55196fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 55206fa01ccdSSowmini Varadhan skb->cloned = 0; 55216fa01ccdSSowmini Varadhan skb->hdr_len = 0; 55226fa01ccdSSowmini Varadhan skb->nohdr = 0; 55236fa01ccdSSowmini Varadhan skb->len -= off; 55246fa01ccdSSowmini Varadhan skb->data_len = skb->len; 55256fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 55266fa01ccdSSowmini Varadhan return 0; 55276fa01ccdSSowmini Varadhan } 55286fa01ccdSSowmini Varadhan 55296fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 55306fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 55316fa01ccdSSowmini Varadhan { 55326fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 55336fa01ccdSSowmini Varadhan 55346fa01ccdSSowmini Varadhan if (len < headlen) 55356fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 55366fa01ccdSSowmini Varadhan else 55376fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 55386fa01ccdSSowmini Varadhan } 55396fa01ccdSSowmini Varadhan 55406fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 55416fa01ccdSSowmini Varadhan * a new skb 55426fa01ccdSSowmini Varadhan */ 55436fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 55446fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 55456fa01ccdSSowmini Varadhan { 55466fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 55476fa01ccdSSowmini Varadhan 55486fa01ccdSSowmini Varadhan if (!clone) 55496fa01ccdSSowmini Varadhan return NULL; 55506fa01ccdSSowmini Varadhan 55516fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 55526fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 55536fa01ccdSSowmini Varadhan kfree_skb(clone); 55546fa01ccdSSowmini Varadhan return NULL; 55556fa01ccdSSowmini Varadhan } 55566fa01ccdSSowmini Varadhan return clone; 55576fa01ccdSSowmini Varadhan } 55586fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 5559c8c8b127SEric Dumazet 5560c8c8b127SEric Dumazet /** 5561c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 5562c8c8b127SEric Dumazet * @skb: buffer 5563c8c8b127SEric Dumazet * 5564c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 5565c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 5566c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 5567c8c8b127SEric Dumazet * truesize. 5568c8c8b127SEric Dumazet * Notes: 5569c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 5570c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 5571c8c8b127SEric Dumazet */ 5572c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 5573c8c8b127SEric Dumazet { 55743174fed9SEric Dumazet if (skb->data_len) { 55753174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 5576c8c8b127SEric Dumazet skb_cloned(skb)) 5577c8c8b127SEric Dumazet return; 5578c8c8b127SEric Dumazet 5579c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 5580c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 55813174fed9SEric Dumazet } 55823174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 55833174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 55843174fed9SEric Dumazet * their truesize. 55853174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 55863174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 55873174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 5588c8c8b127SEric Dumazet */ 5589c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 5590c8c8b127SEric Dumazet } 5591df5042f4SFlorian Westphal 5592df5042f4SFlorian Westphal #ifdef CONFIG_SKB_EXTENSIONS 5593df5042f4SFlorian Westphal static void *skb_ext_get_ptr(struct skb_ext *ext, enum skb_ext_id id) 5594df5042f4SFlorian Westphal { 5595df5042f4SFlorian Westphal return (void *)ext + (ext->offset[id] * SKB_EXT_ALIGN_VALUE); 5596df5042f4SFlorian Westphal } 5597df5042f4SFlorian Westphal 5598df5042f4SFlorian Westphal static struct skb_ext *skb_ext_alloc(void) 5599df5042f4SFlorian Westphal { 5600df5042f4SFlorian Westphal struct skb_ext *new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 5601df5042f4SFlorian Westphal 5602df5042f4SFlorian Westphal if (new) { 5603df5042f4SFlorian Westphal memset(new->offset, 0, sizeof(new->offset)); 5604df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 5605df5042f4SFlorian Westphal } 5606df5042f4SFlorian Westphal 5607df5042f4SFlorian Westphal return new; 5608df5042f4SFlorian Westphal } 5609df5042f4SFlorian Westphal 56104165079bSFlorian Westphal static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old, 56114165079bSFlorian Westphal unsigned int old_active) 5612df5042f4SFlorian Westphal { 5613df5042f4SFlorian Westphal struct skb_ext *new; 5614df5042f4SFlorian Westphal 5615df5042f4SFlorian Westphal if (refcount_read(&old->refcnt) == 1) 5616df5042f4SFlorian Westphal return old; 5617df5042f4SFlorian Westphal 5618df5042f4SFlorian Westphal new = kmem_cache_alloc(skbuff_ext_cache, GFP_ATOMIC); 5619df5042f4SFlorian Westphal if (!new) 5620df5042f4SFlorian Westphal return NULL; 5621df5042f4SFlorian Westphal 5622df5042f4SFlorian Westphal memcpy(new, old, old->chunks * SKB_EXT_ALIGN_VALUE); 5623df5042f4SFlorian Westphal refcount_set(&new->refcnt, 1); 5624df5042f4SFlorian Westphal 56254165079bSFlorian Westphal #ifdef CONFIG_XFRM 56264165079bSFlorian Westphal if (old_active & (1 << SKB_EXT_SEC_PATH)) { 56274165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(old, SKB_EXT_SEC_PATH); 56284165079bSFlorian Westphal unsigned int i; 56294165079bSFlorian Westphal 56304165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 56314165079bSFlorian Westphal xfrm_state_hold(sp->xvec[i]); 56324165079bSFlorian Westphal } 56334165079bSFlorian Westphal #endif 5634df5042f4SFlorian Westphal __skb_ext_put(old); 5635df5042f4SFlorian Westphal return new; 5636df5042f4SFlorian Westphal } 5637df5042f4SFlorian Westphal 5638df5042f4SFlorian Westphal /** 5639df5042f4SFlorian Westphal * skb_ext_add - allocate space for given extension, COW if needed 5640df5042f4SFlorian Westphal * @skb: buffer 5641df5042f4SFlorian Westphal * @id: extension to allocate space for 5642df5042f4SFlorian Westphal * 5643df5042f4SFlorian Westphal * Allocates enough space for the given extension. 5644df5042f4SFlorian Westphal * If the extension is already present, a pointer to that extension 5645df5042f4SFlorian Westphal * is returned. 5646df5042f4SFlorian Westphal * 5647df5042f4SFlorian Westphal * If the skb was cloned, COW applies and the returned memory can be 5648df5042f4SFlorian Westphal * modified without changing the extension space of clones buffers. 5649df5042f4SFlorian Westphal * 5650df5042f4SFlorian Westphal * Returns pointer to the extension or NULL on allocation failure. 5651df5042f4SFlorian Westphal */ 5652df5042f4SFlorian Westphal void *skb_ext_add(struct sk_buff *skb, enum skb_ext_id id) 5653df5042f4SFlorian Westphal { 5654df5042f4SFlorian Westphal struct skb_ext *new, *old = NULL; 5655df5042f4SFlorian Westphal unsigned int newlen, newoff; 5656df5042f4SFlorian Westphal 5657df5042f4SFlorian Westphal if (skb->active_extensions) { 5658df5042f4SFlorian Westphal old = skb->extensions; 5659df5042f4SFlorian Westphal 56604165079bSFlorian Westphal new = skb_ext_maybe_cow(old, skb->active_extensions); 5661df5042f4SFlorian Westphal if (!new) 5662df5042f4SFlorian Westphal return NULL; 5663df5042f4SFlorian Westphal 5664682ec859SPaolo Abeni if (__skb_ext_exist(new, id)) 5665df5042f4SFlorian Westphal goto set_active; 5666df5042f4SFlorian Westphal 5667e94e50bdSPaolo Abeni newoff = new->chunks; 5668df5042f4SFlorian Westphal } else { 5669df5042f4SFlorian Westphal newoff = SKB_EXT_CHUNKSIZEOF(*new); 5670df5042f4SFlorian Westphal 5671df5042f4SFlorian Westphal new = skb_ext_alloc(); 5672df5042f4SFlorian Westphal if (!new) 5673df5042f4SFlorian Westphal return NULL; 5674df5042f4SFlorian Westphal } 5675df5042f4SFlorian Westphal 5676df5042f4SFlorian Westphal newlen = newoff + skb_ext_type_len[id]; 5677df5042f4SFlorian Westphal new->chunks = newlen; 5678df5042f4SFlorian Westphal new->offset[id] = newoff; 5679df5042f4SFlorian Westphal set_active: 5680682ec859SPaolo Abeni skb->extensions = new; 5681df5042f4SFlorian Westphal skb->active_extensions |= 1 << id; 5682df5042f4SFlorian Westphal return skb_ext_get_ptr(new, id); 5683df5042f4SFlorian Westphal } 5684df5042f4SFlorian Westphal EXPORT_SYMBOL(skb_ext_add); 5685df5042f4SFlorian Westphal 56864165079bSFlorian Westphal #ifdef CONFIG_XFRM 56874165079bSFlorian Westphal static void skb_ext_put_sp(struct sec_path *sp) 56884165079bSFlorian Westphal { 56894165079bSFlorian Westphal unsigned int i; 56904165079bSFlorian Westphal 56914165079bSFlorian Westphal for (i = 0; i < sp->len; i++) 56924165079bSFlorian Westphal xfrm_state_put(sp->xvec[i]); 56934165079bSFlorian Westphal } 56944165079bSFlorian Westphal #endif 56954165079bSFlorian Westphal 5696df5042f4SFlorian Westphal void __skb_ext_del(struct sk_buff *skb, enum skb_ext_id id) 5697df5042f4SFlorian Westphal { 5698df5042f4SFlorian Westphal struct skb_ext *ext = skb->extensions; 5699df5042f4SFlorian Westphal 5700df5042f4SFlorian Westphal skb->active_extensions &= ~(1 << id); 5701df5042f4SFlorian Westphal if (skb->active_extensions == 0) { 5702df5042f4SFlorian Westphal skb->extensions = NULL; 5703df5042f4SFlorian Westphal __skb_ext_put(ext); 57044165079bSFlorian Westphal #ifdef CONFIG_XFRM 57054165079bSFlorian Westphal } else if (id == SKB_EXT_SEC_PATH && 57064165079bSFlorian Westphal refcount_read(&ext->refcnt) == 1) { 57074165079bSFlorian Westphal struct sec_path *sp = skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH); 57084165079bSFlorian Westphal 57094165079bSFlorian Westphal skb_ext_put_sp(sp); 57104165079bSFlorian Westphal sp->len = 0; 57114165079bSFlorian Westphal #endif 5712df5042f4SFlorian Westphal } 5713df5042f4SFlorian Westphal } 5714df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_del); 5715df5042f4SFlorian Westphal 5716df5042f4SFlorian Westphal void __skb_ext_put(struct skb_ext *ext) 5717df5042f4SFlorian Westphal { 5718df5042f4SFlorian Westphal /* If this is last clone, nothing can increment 5719df5042f4SFlorian Westphal * it after check passes. Avoids one atomic op. 5720df5042f4SFlorian Westphal */ 5721df5042f4SFlorian Westphal if (refcount_read(&ext->refcnt) == 1) 5722df5042f4SFlorian Westphal goto free_now; 5723df5042f4SFlorian Westphal 5724df5042f4SFlorian Westphal if (!refcount_dec_and_test(&ext->refcnt)) 5725df5042f4SFlorian Westphal return; 5726df5042f4SFlorian Westphal free_now: 57274165079bSFlorian Westphal #ifdef CONFIG_XFRM 57284165079bSFlorian Westphal if (__skb_ext_exist(ext, SKB_EXT_SEC_PATH)) 57294165079bSFlorian Westphal skb_ext_put_sp(skb_ext_get_ptr(ext, SKB_EXT_SEC_PATH)); 57304165079bSFlorian Westphal #endif 57314165079bSFlorian Westphal 5732df5042f4SFlorian Westphal kmem_cache_free(skbuff_ext_cache, ext); 5733df5042f4SFlorian Westphal } 5734df5042f4SFlorian Westphal EXPORT_SYMBOL(__skb_ext_put); 5735df5042f4SFlorian Westphal #endif /* CONFIG_SKB_EXTENSIONS */ 5736