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> 44fe55f6d5SVegard Nossum #include <linux/kmemcheck.h> 451da177e4SLinus Torvalds #include <linux/mm.h> 461da177e4SLinus Torvalds #include <linux/interrupt.h> 471da177e4SLinus Torvalds #include <linux/in.h> 481da177e4SLinus Torvalds #include <linux/inet.h> 491da177e4SLinus Torvalds #include <linux/slab.h> 50de960aa9SFlorian Westphal #include <linux/tcp.h> 51de960aa9SFlorian Westphal #include <linux/udp.h> 5290017accSMarcelo Ricardo Leitner #include <linux/sctp.h> 531da177e4SLinus Torvalds #include <linux/netdevice.h> 541da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 551da177e4SLinus Torvalds #include <net/pkt_sched.h> 561da177e4SLinus Torvalds #endif 571da177e4SLinus Torvalds #include <linux/string.h> 581da177e4SLinus Torvalds #include <linux/skbuff.h> 599c55e01cSJens Axboe #include <linux/splice.h> 601da177e4SLinus Torvalds #include <linux/cache.h> 611da177e4SLinus Torvalds #include <linux/rtnetlink.h> 621da177e4SLinus Torvalds #include <linux/init.h> 63716ea3a7SDavid Howells #include <linux/scatterlist.h> 64ac45f602SPatrick Ohly #include <linux/errqueue.h> 65268bb0ceSLinus Torvalds #include <linux/prefetch.h> 660d5501c1SVlad Yasevich #include <linux/if_vlan.h> 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #include <net/protocol.h> 691da177e4SLinus Torvalds #include <net/dst.h> 701da177e4SLinus Torvalds #include <net/sock.h> 711da177e4SLinus Torvalds #include <net/checksum.h> 72ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 731da177e4SLinus Torvalds #include <net/xfrm.h> 741da177e4SLinus Torvalds 757c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 76ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7751c56b00SEric Dumazet #include <linux/highmem.h> 78b245be1fSWillem de Bruijn #include <linux/capability.h> 79b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 80a1f8e7f7SAl Viro 81d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly; 82e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 835f74f82eSHans Westgaard Ry int sysctl_max_skb_frags __read_mostly = MAX_SKB_FRAGS; 845f74f82eSHans Westgaard Ry EXPORT_SYMBOL(sysctl_max_skb_frags); 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /** 87f05de73bSJean Sacren * skb_panic - private function for out-of-line support 881da177e4SLinus Torvalds * @skb: buffer 891da177e4SLinus Torvalds * @sz: size 90f05de73bSJean Sacren * @addr: address 9199d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 921da177e4SLinus Torvalds * 93f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 94f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 95f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 96f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 971da177e4SLinus Torvalds */ 98f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 9999d5851eSJames Hogan const char msg[]) 1001da177e4SLinus Torvalds { 101e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 10299d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1034305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 10426095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1051da177e4SLinus Torvalds BUG(); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 108f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1091da177e4SLinus Torvalds { 110f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 113f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 114f05de73bSJean Sacren { 115f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 116f05de73bSJean Sacren } 117c93bdd0eSMel Gorman 118c93bdd0eSMel Gorman /* 119c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 120c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 121c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 122c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 123c93bdd0eSMel Gorman * memory is free 124c93bdd0eSMel Gorman */ 125c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 126c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 12761c5e88aSstephen hemminger 12861c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 12961c5e88aSstephen hemminger unsigned long ip, bool *pfmemalloc) 130c93bdd0eSMel Gorman { 131c93bdd0eSMel Gorman void *obj; 132c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 133c93bdd0eSMel Gorman 134c93bdd0eSMel Gorman /* 135c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 136c93bdd0eSMel Gorman * to the reserves, fail. 137c93bdd0eSMel Gorman */ 138c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 139c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 140c93bdd0eSMel Gorman node); 141c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 142c93bdd0eSMel Gorman goto out; 143c93bdd0eSMel Gorman 144c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 145c93bdd0eSMel Gorman ret_pfmemalloc = true; 146c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 147c93bdd0eSMel Gorman 148c93bdd0eSMel Gorman out: 149c93bdd0eSMel Gorman if (pfmemalloc) 150c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 151c93bdd0eSMel Gorman 152c93bdd0eSMel Gorman return obj; 153c93bdd0eSMel Gorman } 154c93bdd0eSMel Gorman 1551da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1561da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1571da177e4SLinus Torvalds * [BEEP] leaks. 1581da177e4SLinus Torvalds * 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /** 162d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1631da177e4SLinus Torvalds * @size: size to allocate 1641da177e4SLinus Torvalds * @gfp_mask: allocation mask 165c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 166c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 167c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 168c93bdd0eSMel Gorman * allocations in case the data is required for writeback 169b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1701da177e4SLinus Torvalds * 1711da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 17294b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 17394b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 1741da177e4SLinus Torvalds * 1751da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1761da177e4SLinus Torvalds * %GFP_ATOMIC. 1771da177e4SLinus Torvalds */ 178dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 179c93bdd0eSMel Gorman int flags, int node) 1801da177e4SLinus Torvalds { 181e18b890bSChristoph Lameter struct kmem_cache *cache; 1824947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 1831da177e4SLinus Torvalds struct sk_buff *skb; 1841da177e4SLinus Torvalds u8 *data; 185c93bdd0eSMel Gorman bool pfmemalloc; 1861da177e4SLinus Torvalds 187c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 188c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 189c93bdd0eSMel Gorman 190c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 191c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1928798b3fbSHerbert Xu 1931da177e4SLinus Torvalds /* Get the HEAD */ 194b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 1951da177e4SLinus Torvalds if (!skb) 1961da177e4SLinus Torvalds goto out; 197ec7d2f2cSEric Dumazet prefetchw(skb); 1981da177e4SLinus Torvalds 19987fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 20087fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 20187fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 20287fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 20387fb4b7bSEric Dumazet */ 204bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 20587fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 206c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2071da177e4SLinus Torvalds if (!data) 2081da177e4SLinus Torvalds goto nodata; 20987fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 21087fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 21187fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 21287fb4b7bSEric Dumazet */ 21387fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 214ec7d2f2cSEric Dumazet prefetchw(data + size); 2151da177e4SLinus Torvalds 216ca0605a7SArnaldo Carvalho de Melo /* 217c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 218c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 219c8005785SJohannes Berg * the tail pointer in struct sk_buff! 220ca0605a7SArnaldo Carvalho de Melo */ 221ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 22287fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 22387fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 224c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 22563354797SReshetova, Elena refcount_set(&skb->users, 1); 2261da177e4SLinus Torvalds skb->head = data; 2271da177e4SLinus Torvalds skb->data = data; 22827a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2294305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 23035d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 23135d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 23219633e12SStephen Hemminger 2334947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2344947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 235ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2364947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 237c2aa3665SEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 2384947d3efSBenjamin LaHaise 239c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 240d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 2411da177e4SLinus Torvalds 242d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 243d0bf4a9eSEric Dumazet 244d0bf4a9eSEric Dumazet kmemcheck_annotate_bitfield(&fclones->skb2, flags1); 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 kmemcheck_annotate_variable(shinfo->destructor_arg); 305b2b5ce9dSEric Dumazet 306b2b5ce9dSEric Dumazet return skb; 307b2b5ce9dSEric Dumazet } 3082ea2f62cSEric Dumazet 3092ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3102ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3112ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3122ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3132ea2f62cSEric Dumazet */ 3142ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3152ea2f62cSEric Dumazet { 3162ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3172ea2f62cSEric Dumazet 3182ea2f62cSEric Dumazet if (skb && frag_size) { 3192ea2f62cSEric Dumazet skb->head_frag = 1; 3202f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3212ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3222ea2f62cSEric Dumazet } 3232ea2f62cSEric Dumazet return skb; 3242ea2f62cSEric Dumazet } 325b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 326b2b5ce9dSEric Dumazet 327795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE 64 328795bb1c0SJesper Dangaard Brouer 329795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache { 330795bb1c0SJesper Dangaard Brouer struct page_frag_cache page; 331e0d7924aSAlexey Dobriyan unsigned int skb_count; 332795bb1c0SJesper Dangaard Brouer void *skb_cache[NAPI_SKB_CACHE_SIZE]; 333795bb1c0SJesper Dangaard Brouer }; 334795bb1c0SJesper Dangaard Brouer 335b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 336795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 337ffde7328SAlexander Duyck 338ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 339ffde7328SAlexander Duyck { 340b63ae8caSAlexander Duyck struct page_frag_cache *nc; 341ffde7328SAlexander Duyck unsigned long flags; 342ffde7328SAlexander Duyck void *data; 343ffde7328SAlexander Duyck 344ffde7328SAlexander Duyck local_irq_save(flags); 3459451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 3468c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, fragsz, gfp_mask); 3476f532612SEric Dumazet local_irq_restore(flags); 3486f532612SEric Dumazet return data; 3496f532612SEric Dumazet } 350c93bdd0eSMel Gorman 351c93bdd0eSMel Gorman /** 352c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 353c93bdd0eSMel Gorman * @fragsz: fragment size 354c93bdd0eSMel Gorman * 355c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 356c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 357c93bdd0eSMel Gorman */ 358c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 359c93bdd0eSMel Gorman { 360c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 361c93bdd0eSMel Gorman } 3626f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3636f532612SEric Dumazet 364ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 365ffde7328SAlexander Duyck { 366795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3679451980aSAlexander Duyck 3688c2dd3e4SAlexander Duyck return page_frag_alloc(&nc->page, fragsz, gfp_mask); 369ffde7328SAlexander Duyck } 370ffde7328SAlexander Duyck 371ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 372ffde7328SAlexander Duyck { 373ffde7328SAlexander Duyck return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 374ffde7328SAlexander Duyck } 375ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 376ffde7328SAlexander Duyck 3776f532612SEric Dumazet /** 378fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 379fd11a83dSAlexander Duyck * @dev: network device to receive on 380d7499160SMasanari Iida * @len: length to allocate 381fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 382fd11a83dSAlexander Duyck * 383fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 384fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 385fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 386fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 387fd11a83dSAlexander Duyck * 388fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 389fd11a83dSAlexander Duyck */ 3909451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 3919451980aSAlexander Duyck gfp_t gfp_mask) 392fd11a83dSAlexander Duyck { 393b63ae8caSAlexander Duyck struct page_frag_cache *nc; 3949451980aSAlexander Duyck unsigned long flags; 395fd11a83dSAlexander Duyck struct sk_buff *skb; 3969451980aSAlexander Duyck bool pfmemalloc; 3979451980aSAlexander Duyck void *data; 398fd11a83dSAlexander Duyck 3999451980aSAlexander Duyck len += NET_SKB_PAD; 400fd11a83dSAlexander Duyck 4019451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 402d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 403a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 404a080e7bdSAlexander Duyck if (!skb) 405a080e7bdSAlexander Duyck goto skb_fail; 406a080e7bdSAlexander Duyck goto skb_success; 407a080e7bdSAlexander Duyck } 4089451980aSAlexander Duyck 4099451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4109451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4119451980aSAlexander Duyck 4129451980aSAlexander Duyck if (sk_memalloc_socks()) 4139451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4149451980aSAlexander Duyck 4159451980aSAlexander Duyck local_irq_save(flags); 4169451980aSAlexander Duyck 4179451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4188c2dd3e4SAlexander Duyck data = page_frag_alloc(nc, len, gfp_mask); 4199451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 4209451980aSAlexander Duyck 4219451980aSAlexander Duyck local_irq_restore(flags); 4229451980aSAlexander Duyck 4239451980aSAlexander Duyck if (unlikely(!data)) 4249451980aSAlexander Duyck return NULL; 4259451980aSAlexander Duyck 4269451980aSAlexander Duyck skb = __build_skb(data, len); 4279451980aSAlexander Duyck if (unlikely(!skb)) { 428181edb2bSAlexander Duyck skb_free_frag(data); 4299451980aSAlexander Duyck return NULL; 4309451980aSAlexander Duyck } 4319451980aSAlexander Duyck 4329451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 4339451980aSAlexander Duyck if (pfmemalloc) 4349451980aSAlexander Duyck skb->pfmemalloc = 1; 4359451980aSAlexander Duyck skb->head_frag = 1; 4369451980aSAlexander Duyck 437a080e7bdSAlexander Duyck skb_success: 4388af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4397b2e497aSChristoph Hellwig skb->dev = dev; 440fd11a83dSAlexander Duyck 441a080e7bdSAlexander Duyck skb_fail: 4428af27456SChristoph Hellwig return skb; 4438af27456SChristoph Hellwig } 444b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4451da177e4SLinus Torvalds 446fd11a83dSAlexander Duyck /** 447fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 448fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 449d7499160SMasanari Iida * @len: length to allocate 450fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 451fd11a83dSAlexander Duyck * 452fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 453fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 454fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 455fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 456fd11a83dSAlexander Duyck * 457fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 458fd11a83dSAlexander Duyck */ 4599451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 4609451980aSAlexander Duyck gfp_t gfp_mask) 461fd11a83dSAlexander Duyck { 462795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 463fd11a83dSAlexander Duyck struct sk_buff *skb; 4649451980aSAlexander Duyck void *data; 465fd11a83dSAlexander Duyck 4669451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 467fd11a83dSAlexander Duyck 4689451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 469d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 470a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 471a080e7bdSAlexander Duyck if (!skb) 472a080e7bdSAlexander Duyck goto skb_fail; 473a080e7bdSAlexander Duyck goto skb_success; 474a080e7bdSAlexander Duyck } 4759451980aSAlexander Duyck 4769451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4779451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4789451980aSAlexander Duyck 4799451980aSAlexander Duyck if (sk_memalloc_socks()) 4809451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4819451980aSAlexander Duyck 4828c2dd3e4SAlexander Duyck data = page_frag_alloc(&nc->page, len, gfp_mask); 4839451980aSAlexander Duyck if (unlikely(!data)) 4849451980aSAlexander Duyck return NULL; 4859451980aSAlexander Duyck 4869451980aSAlexander Duyck skb = __build_skb(data, len); 4879451980aSAlexander Duyck if (unlikely(!skb)) { 488181edb2bSAlexander Duyck skb_free_frag(data); 4899451980aSAlexander Duyck return NULL; 4909451980aSAlexander Duyck } 4919451980aSAlexander Duyck 4929451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 493795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 4949451980aSAlexander Duyck skb->pfmemalloc = 1; 4959451980aSAlexander Duyck skb->head_frag = 1; 4969451980aSAlexander Duyck 497a080e7bdSAlexander Duyck skb_success: 498fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 499fd11a83dSAlexander Duyck skb->dev = napi->dev; 500fd11a83dSAlexander Duyck 501a080e7bdSAlexander Duyck skb_fail: 502fd11a83dSAlexander Duyck return skb; 503fd11a83dSAlexander Duyck } 504fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 505fd11a83dSAlexander Duyck 506654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 50750269e19SEric Dumazet int size, unsigned int truesize) 508654bed16SPeter Zijlstra { 509654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 510654bed16SPeter Zijlstra skb->len += size; 511654bed16SPeter Zijlstra skb->data_len += size; 51250269e19SEric Dumazet skb->truesize += truesize; 513654bed16SPeter Zijlstra } 514654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 515654bed16SPeter Zijlstra 516f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 517f8e617e1SJason Wang unsigned int truesize) 518f8e617e1SJason Wang { 519f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 520f8e617e1SJason Wang 521f8e617e1SJason Wang skb_frag_size_add(frag, size); 522f8e617e1SJason Wang skb->len += size; 523f8e617e1SJason Wang skb->data_len += size; 524f8e617e1SJason Wang skb->truesize += truesize; 525f8e617e1SJason Wang } 526f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 527f8e617e1SJason Wang 52827b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5291da177e4SLinus Torvalds { 530bd8a7036SEric Dumazet kfree_skb_list(*listp); 53127b437c8SHerbert Xu *listp = NULL; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 53427b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 53527b437c8SHerbert Xu { 53627b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 53727b437c8SHerbert Xu } 53827b437c8SHerbert Xu 5391da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5401da177e4SLinus Torvalds { 5411da177e4SLinus Torvalds struct sk_buff *list; 5421da177e4SLinus Torvalds 543fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5441da177e4SLinus Torvalds skb_get(list); 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 547d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 548d3836f21SEric Dumazet { 549181edb2bSAlexander Duyck unsigned char *head = skb->head; 550181edb2bSAlexander Duyck 551d3836f21SEric Dumazet if (skb->head_frag) 552181edb2bSAlexander Duyck skb_free_frag(head); 553d3836f21SEric Dumazet else 554181edb2bSAlexander Duyck kfree(head); 555d3836f21SEric Dumazet } 556d3836f21SEric Dumazet 5575bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5581da177e4SLinus Torvalds { 559ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5601da177e4SLinus Torvalds int i; 561ff04a771SEric Dumazet 562ff04a771SEric Dumazet if (skb->cloned && 563ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 564ff04a771SEric Dumazet &shinfo->dataref)) 565ff04a771SEric Dumazet return; 566ff04a771SEric Dumazet 567ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 568ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 5691da177e4SLinus Torvalds 570ff04a771SEric Dumazet if (shinfo->frag_list) 571ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 5721da177e4SLinus Torvalds 5731f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 574d3836f21SEric Dumazet skb_free_head(skb); 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* 5781da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 5791da177e4SLinus Torvalds */ 5802d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 5811da177e4SLinus Torvalds { 582d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 583d179cd12SDavid S. Miller 584d179cd12SDavid S. Miller switch (skb->fclone) { 585d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 5861da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 5876ffe75ebSEric Dumazet return; 588d179cd12SDavid S. Miller 589d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 590d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 5916ffe75ebSEric Dumazet 5926ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 5936ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 5946ffe75ebSEric Dumazet * while here, branch prediction will be good. 5956ffe75ebSEric Dumazet */ 5962638595aSReshetova, Elena if (refcount_read(&fclones->fclone_ref) == 1) 5976ffe75ebSEric Dumazet goto fastpath; 598d179cd12SDavid S. Miller break; 599d179cd12SDavid S. Miller 6006ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 601d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 602d179cd12SDavid S. Miller break; 6033ff50b79SStephen Hemminger } 6042638595aSReshetova, Elena if (!refcount_dec_and_test(&fclones->fclone_ref)) 6056ffe75ebSEric Dumazet return; 6066ffe75ebSEric Dumazet fastpath: 6076ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6081da177e4SLinus Torvalds } 6091da177e4SLinus Torvalds 6100a463c78SPaolo Abeni void skb_release_head_state(struct sk_buff *skb) 6111da177e4SLinus Torvalds { 612adf30907SEric Dumazet skb_dst_drop(skb); 6130a463c78SPaolo Abeni secpath_reset(skb); 6141da177e4SLinus Torvalds if (skb->destructor) { 6159c2b3328SStephen Hemminger WARN_ON(in_irq()); 6161da177e4SLinus Torvalds skb->destructor(skb); 6171da177e4SLinus Torvalds } 618a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 619cb9c6836SFlorian Westphal nf_conntrack_put(skb_nfct(skb)); 6202fc72c7bSKOVACS Krisztian #endif 6211109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 6221da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 6231da177e4SLinus Torvalds #endif 62404a4bb55SLennert Buytenhek } 62504a4bb55SLennert Buytenhek 62604a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 62704a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 62804a4bb55SLennert Buytenhek { 62904a4bb55SLennert Buytenhek skb_release_head_state(skb); 630a28b1b90SFlorian Westphal if (likely(skb->head)) 6312d4baff8SHerbert Xu skb_release_data(skb); 6322d4baff8SHerbert Xu } 6331da177e4SLinus Torvalds 6342d4baff8SHerbert Xu /** 6352d4baff8SHerbert Xu * __kfree_skb - private function 6362d4baff8SHerbert Xu * @skb: buffer 6372d4baff8SHerbert Xu * 6382d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6392d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6402d4baff8SHerbert Xu * always call kfree_skb 6412d4baff8SHerbert Xu */ 6422d4baff8SHerbert Xu 6432d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6442d4baff8SHerbert Xu { 6452d4baff8SHerbert Xu skb_release_all(skb); 6461da177e4SLinus Torvalds kfree_skbmem(skb); 6471da177e4SLinus Torvalds } 648b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /** 651231d06aeSJörn Engel * kfree_skb - free an sk_buff 652231d06aeSJörn Engel * @skb: buffer to free 653231d06aeSJörn Engel * 654231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 655231d06aeSJörn Engel * hit zero. 656231d06aeSJörn Engel */ 657231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 658231d06aeSJörn Engel { 6593889a803SPaolo Abeni if (!skb_unref(skb)) 660231d06aeSJörn Engel return; 6613889a803SPaolo Abeni 662ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 663231d06aeSJörn Engel __kfree_skb(skb); 664231d06aeSJörn Engel } 665b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 666231d06aeSJörn Engel 667bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 668bd8a7036SEric Dumazet { 669bd8a7036SEric Dumazet while (segs) { 670bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 671bd8a7036SEric Dumazet 672bd8a7036SEric Dumazet kfree_skb(segs); 673bd8a7036SEric Dumazet segs = next; 674bd8a7036SEric Dumazet } 675bd8a7036SEric Dumazet } 676bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 677bd8a7036SEric Dumazet 678d1a203eaSStephen Hemminger /** 67925121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 68025121173SMichael S. Tsirkin * @skb: buffer that triggered an error 68125121173SMichael S. Tsirkin * 68225121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 68325121173SMichael S. Tsirkin * skb must be freed afterwards. 68425121173SMichael S. Tsirkin */ 68525121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 68625121173SMichael S. Tsirkin { 6871f8b977aSWillem de Bruijn skb_zcopy_clear(skb, true); 68825121173SMichael S. Tsirkin } 68925121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 69025121173SMichael S. Tsirkin 69125121173SMichael S. Tsirkin /** 692ead2ceb0SNeil Horman * consume_skb - free an skbuff 693ead2ceb0SNeil Horman * @skb: buffer to free 694ead2ceb0SNeil Horman * 695ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 696ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 697ead2ceb0SNeil Horman * is being dropped after a failure and notes that 698ead2ceb0SNeil Horman */ 699ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 700ead2ceb0SNeil Horman { 7013889a803SPaolo Abeni if (!skb_unref(skb)) 702ead2ceb0SNeil Horman return; 7033889a803SPaolo Abeni 70407dc22e7SKoki Sanagi trace_consume_skb(skb); 705ead2ceb0SNeil Horman __kfree_skb(skb); 706ead2ceb0SNeil Horman } 707ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 708ead2ceb0SNeil Horman 7090a463c78SPaolo Abeni /** 7100a463c78SPaolo Abeni * consume_stateless_skb - free an skbuff, assuming it is stateless 7110a463c78SPaolo Abeni * @skb: buffer to free 7120a463c78SPaolo Abeni * 7130a463c78SPaolo Abeni * Works like consume_skb(), but this variant assumes that all the head 7140a463c78SPaolo Abeni * states have been already dropped. 7150a463c78SPaolo Abeni */ 7160a463c78SPaolo Abeni void consume_stateless_skb(struct sk_buff *skb) 7170a463c78SPaolo Abeni { 7180a463c78SPaolo Abeni if (!skb_unref(skb)) 7190a463c78SPaolo Abeni return; 7200a463c78SPaolo Abeni 7210a463c78SPaolo Abeni trace_consume_skb(skb); 7220a463c78SPaolo Abeni skb_release_data(skb); 7230a463c78SPaolo Abeni kfree_skbmem(skb); 7240a463c78SPaolo Abeni } 7250a463c78SPaolo Abeni 726795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void) 727795bb1c0SJesper Dangaard Brouer { 728795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 729795bb1c0SJesper Dangaard Brouer 730795bb1c0SJesper Dangaard Brouer /* flush skb_cache if containing objects */ 731795bb1c0SJesper Dangaard Brouer if (nc->skb_count) { 732795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, 733795bb1c0SJesper Dangaard Brouer nc->skb_cache); 734795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 735795bb1c0SJesper Dangaard Brouer } 736795bb1c0SJesper Dangaard Brouer } 737795bb1c0SJesper Dangaard Brouer 73815fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb) 739795bb1c0SJesper Dangaard Brouer { 740795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 741795bb1c0SJesper Dangaard Brouer 742795bb1c0SJesper Dangaard Brouer /* drop skb->head and call any destructors for packet */ 743795bb1c0SJesper Dangaard Brouer skb_release_all(skb); 744795bb1c0SJesper Dangaard Brouer 745795bb1c0SJesper Dangaard Brouer /* record skb to CPU local list */ 746795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 747795bb1c0SJesper Dangaard Brouer 748795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB 749795bb1c0SJesper Dangaard Brouer /* SLUB writes into objects when freeing */ 750795bb1c0SJesper Dangaard Brouer prefetchw(skb); 751795bb1c0SJesper Dangaard Brouer #endif 752795bb1c0SJesper Dangaard Brouer 753795bb1c0SJesper Dangaard Brouer /* flush skb_cache if it is filled */ 754795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 755795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, 756795bb1c0SJesper Dangaard Brouer nc->skb_cache); 757795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 758795bb1c0SJesper Dangaard Brouer } 759795bb1c0SJesper Dangaard Brouer } 76015fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 76115fad714SJesper Dangaard Brouer { 76215fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 76315fad714SJesper Dangaard Brouer } 764795bb1c0SJesper Dangaard Brouer 765795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 766795bb1c0SJesper Dangaard Brouer { 767795bb1c0SJesper Dangaard Brouer if (unlikely(!skb)) 768795bb1c0SJesper Dangaard Brouer return; 769795bb1c0SJesper Dangaard Brouer 770885eb0a5SJesper Dangaard Brouer /* Zero budget indicate non-NAPI context called us, like netpoll */ 771795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 772885eb0a5SJesper Dangaard Brouer dev_consume_skb_any(skb); 773795bb1c0SJesper Dangaard Brouer return; 774795bb1c0SJesper Dangaard Brouer } 775795bb1c0SJesper Dangaard Brouer 7767608894eSPaolo Abeni if (!skb_unref(skb)) 777795bb1c0SJesper Dangaard Brouer return; 7787608894eSPaolo Abeni 779795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 780795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 781795bb1c0SJesper Dangaard Brouer 782795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 783abbdb5a7SEric Dumazet if (skb->fclone != SKB_FCLONE_UNAVAILABLE) { 784795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 785795bb1c0SJesper Dangaard Brouer return; 786795bb1c0SJesper Dangaard Brouer } 787795bb1c0SJesper Dangaard Brouer 78815fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 789795bb1c0SJesper Dangaard Brouer } 790795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 791795bb1c0SJesper Dangaard Brouer 792b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 793b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 794b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 795b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 796b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 797b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 798b1937227SEric Dumazet 799dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 800dec18810SHerbert Xu { 801dec18810SHerbert Xu new->tstamp = old->tstamp; 802b1937227SEric Dumazet /* We do not copy old->sk */ 803dec18810SHerbert Xu new->dev = old->dev; 804b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 8057fee226aSEric Dumazet skb_dst_copy(new, old); 806def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 807dec18810SHerbert Xu new->sp = secpath_get(old->sp); 808dec18810SHerbert Xu #endif 809b1937227SEric Dumazet __nf_copy(new, old, false); 8106aa895b0SPatrick McHardy 811b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 812b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 813b1937227SEric Dumazet */ 814b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 81506021292SEliezer Tamir 816b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 817b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 818b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 819b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 820b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 821b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 822b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 823b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 824b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 825b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 826b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 827b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 828b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 829b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 830b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 831b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 832b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 833b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 834b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 835b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 836b1937227SEric Dumazet #endif 837e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 838b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 83906021292SEliezer Tamir #endif 8402bd82484SEric Dumazet #ifdef CONFIG_XPS 8412bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 8422bd82484SEric Dumazet #endif 843b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 844b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 845b1937227SEric Dumazet #endif 846b1937227SEric Dumazet 847dec18810SHerbert Xu } 848dec18810SHerbert Xu 84982c49a35SHerbert Xu /* 85082c49a35SHerbert Xu * You should not add any new code to this function. Add it to 85182c49a35SHerbert Xu * __copy_skb_header above instead. 85282c49a35SHerbert Xu */ 853e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds #define C(x) n->x = skb->x 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds n->next = n->prev = NULL; 8581da177e4SLinus Torvalds n->sk = NULL; 859dec18810SHerbert Xu __copy_skb_header(n, skb); 860dec18810SHerbert Xu 8611da177e4SLinus Torvalds C(len); 8621da177e4SLinus Torvalds C(data_len); 8633e6b3b2eSAlexey Dobriyan C(mac_len); 864334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 86502f1c89dSPaul Moore n->cloned = 1; 8661da177e4SLinus Torvalds n->nohdr = 0; 8671da177e4SLinus Torvalds n->destructor = NULL; 8681da177e4SLinus Torvalds C(tail); 8691da177e4SLinus Torvalds C(end); 87002f1c89dSPaul Moore C(head); 871d3836f21SEric Dumazet C(head_frag); 87202f1c89dSPaul Moore C(data); 87302f1c89dSPaul Moore C(truesize); 87463354797SReshetova, Elena refcount_set(&n->users, 1); 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 8771da177e4SLinus Torvalds skb->cloned = 1; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds return n; 880e0053ec0SHerbert Xu #undef C 881e0053ec0SHerbert Xu } 882e0053ec0SHerbert Xu 883e0053ec0SHerbert Xu /** 884e0053ec0SHerbert Xu * skb_morph - morph one skb into another 885e0053ec0SHerbert Xu * @dst: the skb to receive the contents 886e0053ec0SHerbert Xu * @src: the skb to supply the contents 887e0053ec0SHerbert Xu * 888e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 889e0053ec0SHerbert Xu * supplied by the user. 890e0053ec0SHerbert Xu * 891e0053ec0SHerbert Xu * The target skb is returned upon exit. 892e0053ec0SHerbert Xu */ 893e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 894e0053ec0SHerbert Xu { 8952d4baff8SHerbert Xu skb_release_all(dst); 896e0053ec0SHerbert Xu return __skb_clone(dst, src); 897e0053ec0SHerbert Xu } 898e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 899e0053ec0SHerbert Xu 900a91dbff5SWillem de Bruijn static int mm_account_pinned_pages(struct mmpin *mmp, size_t size) 901a91dbff5SWillem de Bruijn { 902a91dbff5SWillem de Bruijn unsigned long max_pg, num_pg, new_pg, old_pg; 903a91dbff5SWillem de Bruijn struct user_struct *user; 904a91dbff5SWillem de Bruijn 905a91dbff5SWillem de Bruijn if (capable(CAP_IPC_LOCK) || !size) 906a91dbff5SWillem de Bruijn return 0; 907a91dbff5SWillem de Bruijn 908a91dbff5SWillem de Bruijn num_pg = (size >> PAGE_SHIFT) + 2; /* worst case */ 909a91dbff5SWillem de Bruijn max_pg = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; 910a91dbff5SWillem de Bruijn user = mmp->user ? : current_user(); 911a91dbff5SWillem de Bruijn 912a91dbff5SWillem de Bruijn do { 913a91dbff5SWillem de Bruijn old_pg = atomic_long_read(&user->locked_vm); 914a91dbff5SWillem de Bruijn new_pg = old_pg + num_pg; 915a91dbff5SWillem de Bruijn if (new_pg > max_pg) 916a91dbff5SWillem de Bruijn return -ENOBUFS; 917a91dbff5SWillem de Bruijn } while (atomic_long_cmpxchg(&user->locked_vm, old_pg, new_pg) != 918a91dbff5SWillem de Bruijn old_pg); 919a91dbff5SWillem de Bruijn 920a91dbff5SWillem de Bruijn if (!mmp->user) { 921a91dbff5SWillem de Bruijn mmp->user = get_uid(user); 922a91dbff5SWillem de Bruijn mmp->num_pg = num_pg; 923a91dbff5SWillem de Bruijn } else { 924a91dbff5SWillem de Bruijn mmp->num_pg += num_pg; 925a91dbff5SWillem de Bruijn } 926a91dbff5SWillem de Bruijn 927a91dbff5SWillem de Bruijn return 0; 928a91dbff5SWillem de Bruijn } 929a91dbff5SWillem de Bruijn 930a91dbff5SWillem de Bruijn static void mm_unaccount_pinned_pages(struct mmpin *mmp) 931a91dbff5SWillem de Bruijn { 932a91dbff5SWillem de Bruijn if (mmp->user) { 933a91dbff5SWillem de Bruijn atomic_long_sub(mmp->num_pg, &mmp->user->locked_vm); 934a91dbff5SWillem de Bruijn free_uid(mmp->user); 935a91dbff5SWillem de Bruijn } 936a91dbff5SWillem de Bruijn } 937a91dbff5SWillem de Bruijn 93852267790SWillem de Bruijn struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size) 93952267790SWillem de Bruijn { 94052267790SWillem de Bruijn struct ubuf_info *uarg; 94152267790SWillem de Bruijn struct sk_buff *skb; 94252267790SWillem de Bruijn 94352267790SWillem de Bruijn WARN_ON_ONCE(!in_task()); 94452267790SWillem de Bruijn 94576851d12SWillem de Bruijn if (!sock_flag(sk, SOCK_ZEROCOPY)) 94676851d12SWillem de Bruijn return NULL; 94776851d12SWillem de Bruijn 94852267790SWillem de Bruijn skb = sock_omalloc(sk, 0, GFP_KERNEL); 94952267790SWillem de Bruijn if (!skb) 95052267790SWillem de Bruijn return NULL; 95152267790SWillem de Bruijn 95252267790SWillem de Bruijn BUILD_BUG_ON(sizeof(*uarg) > sizeof(skb->cb)); 95352267790SWillem de Bruijn uarg = (void *)skb->cb; 954a91dbff5SWillem de Bruijn uarg->mmp.user = NULL; 955a91dbff5SWillem de Bruijn 956a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) { 957a91dbff5SWillem de Bruijn kfree_skb(skb); 958a91dbff5SWillem de Bruijn return NULL; 959a91dbff5SWillem de Bruijn } 96052267790SWillem de Bruijn 96152267790SWillem de Bruijn uarg->callback = sock_zerocopy_callback; 9624ab6c99dSWillem de Bruijn uarg->id = ((u32)atomic_inc_return(&sk->sk_zckey)) - 1; 9634ab6c99dSWillem de Bruijn uarg->len = 1; 9644ab6c99dSWillem de Bruijn uarg->bytelen = size; 96552267790SWillem de Bruijn uarg->zerocopy = 1; 966db5bce32SEric Dumazet atomic_set(&uarg->refcnt, 1); 96752267790SWillem de Bruijn sock_hold(sk); 96852267790SWillem de Bruijn 96952267790SWillem de Bruijn return uarg; 97052267790SWillem de Bruijn } 97152267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_alloc); 97252267790SWillem de Bruijn 97352267790SWillem de Bruijn static inline struct sk_buff *skb_from_uarg(struct ubuf_info *uarg) 97452267790SWillem de Bruijn { 97552267790SWillem de Bruijn return container_of((void *)uarg, struct sk_buff, cb); 97652267790SWillem de Bruijn } 97752267790SWillem de Bruijn 9784ab6c99dSWillem de Bruijn struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size, 9794ab6c99dSWillem de Bruijn struct ubuf_info *uarg) 9804ab6c99dSWillem de Bruijn { 9814ab6c99dSWillem de Bruijn if (uarg) { 9824ab6c99dSWillem de Bruijn const u32 byte_limit = 1 << 19; /* limit to a few TSO */ 9834ab6c99dSWillem de Bruijn u32 bytelen, next; 9844ab6c99dSWillem de Bruijn 9854ab6c99dSWillem de Bruijn /* realloc only when socket is locked (TCP, UDP cork), 9864ab6c99dSWillem de Bruijn * so uarg->len and sk_zckey access is serialized 9874ab6c99dSWillem de Bruijn */ 9884ab6c99dSWillem de Bruijn if (!sock_owned_by_user(sk)) { 9894ab6c99dSWillem de Bruijn WARN_ON_ONCE(1); 9904ab6c99dSWillem de Bruijn return NULL; 9914ab6c99dSWillem de Bruijn } 9924ab6c99dSWillem de Bruijn 9934ab6c99dSWillem de Bruijn bytelen = uarg->bytelen + size; 9944ab6c99dSWillem de Bruijn if (uarg->len == USHRT_MAX - 1 || bytelen > byte_limit) { 9954ab6c99dSWillem de Bruijn /* TCP can create new skb to attach new uarg */ 9964ab6c99dSWillem de Bruijn if (sk->sk_type == SOCK_STREAM) 9974ab6c99dSWillem de Bruijn goto new_alloc; 9984ab6c99dSWillem de Bruijn return NULL; 9994ab6c99dSWillem de Bruijn } 10004ab6c99dSWillem de Bruijn 10014ab6c99dSWillem de Bruijn next = (u32)atomic_read(&sk->sk_zckey); 10024ab6c99dSWillem de Bruijn if ((u32)(uarg->id + uarg->len) == next) { 1003a91dbff5SWillem de Bruijn if (mm_account_pinned_pages(&uarg->mmp, size)) 1004a91dbff5SWillem de Bruijn return NULL; 10054ab6c99dSWillem de Bruijn uarg->len++; 10064ab6c99dSWillem de Bruijn uarg->bytelen = bytelen; 10074ab6c99dSWillem de Bruijn atomic_set(&sk->sk_zckey, ++next); 1008db5bce32SEric Dumazet sock_zerocopy_get(uarg); 10094ab6c99dSWillem de Bruijn return uarg; 10104ab6c99dSWillem de Bruijn } 10114ab6c99dSWillem de Bruijn } 10124ab6c99dSWillem de Bruijn 10134ab6c99dSWillem de Bruijn new_alloc: 10144ab6c99dSWillem de Bruijn return sock_zerocopy_alloc(sk, size); 10154ab6c99dSWillem de Bruijn } 10164ab6c99dSWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_realloc); 10174ab6c99dSWillem de Bruijn 10184ab6c99dSWillem de Bruijn static bool skb_zerocopy_notify_extend(struct sk_buff *skb, u32 lo, u16 len) 10194ab6c99dSWillem de Bruijn { 10204ab6c99dSWillem de Bruijn struct sock_exterr_skb *serr = SKB_EXT_ERR(skb); 10214ab6c99dSWillem de Bruijn u32 old_lo, old_hi; 10224ab6c99dSWillem de Bruijn u64 sum_len; 10234ab6c99dSWillem de Bruijn 10244ab6c99dSWillem de Bruijn old_lo = serr->ee.ee_info; 10254ab6c99dSWillem de Bruijn old_hi = serr->ee.ee_data; 10264ab6c99dSWillem de Bruijn sum_len = old_hi - old_lo + 1ULL + len; 10274ab6c99dSWillem de Bruijn 10284ab6c99dSWillem de Bruijn if (sum_len >= (1ULL << 32)) 10294ab6c99dSWillem de Bruijn return false; 10304ab6c99dSWillem de Bruijn 10314ab6c99dSWillem de Bruijn if (lo != old_hi + 1) 10324ab6c99dSWillem de Bruijn return false; 10334ab6c99dSWillem de Bruijn 10344ab6c99dSWillem de Bruijn serr->ee.ee_data += len; 10354ab6c99dSWillem de Bruijn return true; 10364ab6c99dSWillem de Bruijn } 10374ab6c99dSWillem de Bruijn 103852267790SWillem de Bruijn void sock_zerocopy_callback(struct ubuf_info *uarg, bool success) 103952267790SWillem de Bruijn { 10404ab6c99dSWillem de Bruijn struct sk_buff *tail, *skb = skb_from_uarg(uarg); 104152267790SWillem de Bruijn struct sock_exterr_skb *serr; 104252267790SWillem de Bruijn struct sock *sk = skb->sk; 10434ab6c99dSWillem de Bruijn struct sk_buff_head *q; 10444ab6c99dSWillem de Bruijn unsigned long flags; 10454ab6c99dSWillem de Bruijn u32 lo, hi; 10464ab6c99dSWillem de Bruijn u16 len; 104752267790SWillem de Bruijn 1048ccaffff1SWillem de Bruijn mm_unaccount_pinned_pages(&uarg->mmp); 1049ccaffff1SWillem de Bruijn 10504ab6c99dSWillem de Bruijn /* if !len, there was only 1 call, and it was aborted 10514ab6c99dSWillem de Bruijn * so do not queue a completion notification 10524ab6c99dSWillem de Bruijn */ 10534ab6c99dSWillem de Bruijn if (!uarg->len || sock_flag(sk, SOCK_DEAD)) 105452267790SWillem de Bruijn goto release; 105552267790SWillem de Bruijn 10564ab6c99dSWillem de Bruijn len = uarg->len; 10574ab6c99dSWillem de Bruijn lo = uarg->id; 10584ab6c99dSWillem de Bruijn hi = uarg->id + len - 1; 10594ab6c99dSWillem de Bruijn 106052267790SWillem de Bruijn serr = SKB_EXT_ERR(skb); 106152267790SWillem de Bruijn memset(serr, 0, sizeof(*serr)); 106252267790SWillem de Bruijn serr->ee.ee_errno = 0; 106352267790SWillem de Bruijn serr->ee.ee_origin = SO_EE_ORIGIN_ZEROCOPY; 10644ab6c99dSWillem de Bruijn serr->ee.ee_data = hi; 10654ab6c99dSWillem de Bruijn serr->ee.ee_info = lo; 106652267790SWillem de Bruijn if (!success) 106752267790SWillem de Bruijn serr->ee.ee_code |= SO_EE_CODE_ZEROCOPY_COPIED; 106852267790SWillem de Bruijn 10694ab6c99dSWillem de Bruijn q = &sk->sk_error_queue; 10704ab6c99dSWillem de Bruijn spin_lock_irqsave(&q->lock, flags); 10714ab6c99dSWillem de Bruijn tail = skb_peek_tail(q); 10724ab6c99dSWillem de Bruijn if (!tail || SKB_EXT_ERR(tail)->ee.ee_origin != SO_EE_ORIGIN_ZEROCOPY || 10734ab6c99dSWillem de Bruijn !skb_zerocopy_notify_extend(tail, lo, len)) { 10744ab6c99dSWillem de Bruijn __skb_queue_tail(q, skb); 107552267790SWillem de Bruijn skb = NULL; 10764ab6c99dSWillem de Bruijn } 10774ab6c99dSWillem de Bruijn spin_unlock_irqrestore(&q->lock, flags); 107852267790SWillem de Bruijn 107952267790SWillem de Bruijn sk->sk_error_report(sk); 108052267790SWillem de Bruijn 108152267790SWillem de Bruijn release: 108252267790SWillem de Bruijn consume_skb(skb); 108352267790SWillem de Bruijn sock_put(sk); 108452267790SWillem de Bruijn } 108552267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_callback); 108652267790SWillem de Bruijn 108752267790SWillem de Bruijn void sock_zerocopy_put(struct ubuf_info *uarg) 108852267790SWillem de Bruijn { 108952267790SWillem de Bruijn if (uarg && atomic_dec_and_test(&uarg->refcnt)) { 109052267790SWillem de Bruijn if (uarg->callback) 109152267790SWillem de Bruijn uarg->callback(uarg, uarg->zerocopy); 109252267790SWillem de Bruijn else 109352267790SWillem de Bruijn consume_skb(skb_from_uarg(uarg)); 109452267790SWillem de Bruijn } 109552267790SWillem de Bruijn } 109652267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put); 109752267790SWillem de Bruijn 109852267790SWillem de Bruijn void sock_zerocopy_put_abort(struct ubuf_info *uarg) 109952267790SWillem de Bruijn { 110052267790SWillem de Bruijn if (uarg) { 110152267790SWillem de Bruijn struct sock *sk = skb_from_uarg(uarg)->sk; 110252267790SWillem de Bruijn 110352267790SWillem de Bruijn atomic_dec(&sk->sk_zckey); 11044ab6c99dSWillem de Bruijn uarg->len--; 110552267790SWillem de Bruijn 110652267790SWillem de Bruijn sock_zerocopy_put(uarg); 110752267790SWillem de Bruijn } 110852267790SWillem de Bruijn } 110952267790SWillem de Bruijn EXPORT_SYMBOL_GPL(sock_zerocopy_put_abort); 111052267790SWillem de Bruijn 111152267790SWillem de Bruijn extern int __zerocopy_sg_from_iter(struct sock *sk, struct sk_buff *skb, 111252267790SWillem de Bruijn struct iov_iter *from, size_t length); 111352267790SWillem de Bruijn 111452267790SWillem de Bruijn int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb, 111552267790SWillem de Bruijn struct msghdr *msg, int len, 111652267790SWillem de Bruijn struct ubuf_info *uarg) 111752267790SWillem de Bruijn { 11184ab6c99dSWillem de Bruijn struct ubuf_info *orig_uarg = skb_zcopy(skb); 111952267790SWillem de Bruijn struct iov_iter orig_iter = msg->msg_iter; 112052267790SWillem de Bruijn int err, orig_len = skb->len; 112152267790SWillem de Bruijn 11224ab6c99dSWillem de Bruijn /* An skb can only point to one uarg. This edge case happens when 11234ab6c99dSWillem de Bruijn * TCP appends to an skb, but zerocopy_realloc triggered a new alloc. 11244ab6c99dSWillem de Bruijn */ 11254ab6c99dSWillem de Bruijn if (orig_uarg && uarg != orig_uarg) 11264ab6c99dSWillem de Bruijn return -EEXIST; 11274ab6c99dSWillem de Bruijn 112852267790SWillem de Bruijn err = __zerocopy_sg_from_iter(sk, skb, &msg->msg_iter, len); 112952267790SWillem de Bruijn if (err == -EFAULT || (err == -EMSGSIZE && skb->len == orig_len)) { 113052267790SWillem de Bruijn /* Streams do not free skb on error. Reset to prev state. */ 113152267790SWillem de Bruijn msg->msg_iter = orig_iter; 113252267790SWillem de Bruijn ___pskb_trim(skb, orig_len); 113352267790SWillem de Bruijn return err; 113452267790SWillem de Bruijn } 113552267790SWillem de Bruijn 113652267790SWillem de Bruijn skb_zcopy_set(skb, uarg); 113752267790SWillem de Bruijn return skb->len - orig_len; 113852267790SWillem de Bruijn } 113952267790SWillem de Bruijn EXPORT_SYMBOL_GPL(skb_zerocopy_iter_stream); 114052267790SWillem de Bruijn 11411f8b977aSWillem de Bruijn static int skb_zerocopy_clone(struct sk_buff *nskb, struct sk_buff *orig, 114252267790SWillem de Bruijn gfp_t gfp_mask) 114352267790SWillem de Bruijn { 114452267790SWillem de Bruijn if (skb_zcopy(orig)) { 114552267790SWillem de Bruijn if (skb_zcopy(nskb)) { 114652267790SWillem de Bruijn /* !gfp_mask callers are verified to !skb_zcopy(nskb) */ 114752267790SWillem de Bruijn if (!gfp_mask) { 114852267790SWillem de Bruijn WARN_ON_ONCE(1); 114952267790SWillem de Bruijn return -ENOMEM; 115052267790SWillem de Bruijn } 115152267790SWillem de Bruijn if (skb_uarg(nskb) == skb_uarg(orig)) 115252267790SWillem de Bruijn return 0; 115352267790SWillem de Bruijn if (skb_copy_ubufs(nskb, GFP_ATOMIC)) 115452267790SWillem de Bruijn return -EIO; 115552267790SWillem de Bruijn } 115652267790SWillem de Bruijn skb_zcopy_set(nskb, skb_uarg(orig)); 115752267790SWillem de Bruijn } 115852267790SWillem de Bruijn return 0; 115952267790SWillem de Bruijn } 116052267790SWillem de Bruijn 11612c53040fSBen Hutchings /** 11622c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 116348c83012SMichael S. Tsirkin * @skb: the skb to modify 116448c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 116548c83012SMichael S. Tsirkin * 116648c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 116748c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 116848c83012SMichael S. Tsirkin * to userspace pages. 116948c83012SMichael S. Tsirkin * 117048c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 117148c83012SMichael S. Tsirkin * %GFP_ATOMIC. 117248c83012SMichael S. Tsirkin * 117348c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 117448c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 117548c83012SMichael S. Tsirkin */ 117648c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 1177a6686f2fSShirley Ma { 1178a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 1179a6686f2fSShirley Ma struct page *page, *head = NULL; 11803ece7826SWillem de Bruijn int i, new_frags; 11813ece7826SWillem de Bruijn u32 d_off; 1182a6686f2fSShirley Ma 11833ece7826SWillem de Bruijn if (!num_frags) 11843ece7826SWillem de Bruijn return 0; 1185a6686f2fSShirley Ma 11863ece7826SWillem de Bruijn if (skb_shared(skb) || skb_unclone(skb, gfp_mask)) 11873ece7826SWillem de Bruijn return -EINVAL; 11883ece7826SWillem de Bruijn 11893ece7826SWillem de Bruijn new_frags = (__skb_pagelen(skb) + PAGE_SIZE - 1) >> PAGE_SHIFT; 11903ece7826SWillem de Bruijn for (i = 0; i < new_frags; i++) { 119102756ed4SKrishna Kumar page = alloc_page(gfp_mask); 1192a6686f2fSShirley Ma if (!page) { 1193a6686f2fSShirley Ma while (head) { 119440dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 1195a6686f2fSShirley Ma put_page(head); 1196a6686f2fSShirley Ma head = next; 1197a6686f2fSShirley Ma } 1198a6686f2fSShirley Ma return -ENOMEM; 1199a6686f2fSShirley Ma } 12003ece7826SWillem de Bruijn set_page_private(page, (unsigned long)head); 12013ece7826SWillem de Bruijn head = page; 12023ece7826SWillem de Bruijn } 12033ece7826SWillem de Bruijn 12043ece7826SWillem de Bruijn page = head; 12053ece7826SWillem de Bruijn d_off = 0; 12063ece7826SWillem de Bruijn for (i = 0; i < num_frags; i++) { 12073ece7826SWillem de Bruijn skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 12083ece7826SWillem de Bruijn u32 p_off, p_len, copied; 12093ece7826SWillem de Bruijn struct page *p; 12103ece7826SWillem de Bruijn u8 *vaddr; 1211c613c209SWillem de Bruijn 1212c613c209SWillem de Bruijn skb_frag_foreach_page(f, f->page_offset, skb_frag_size(f), 1213c613c209SWillem de Bruijn p, p_off, p_len, copied) { 12143ece7826SWillem de Bruijn u32 copy, done = 0; 1215c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 12163ece7826SWillem de Bruijn 12173ece7826SWillem de Bruijn while (done < p_len) { 12183ece7826SWillem de Bruijn if (d_off == PAGE_SIZE) { 12193ece7826SWillem de Bruijn d_off = 0; 12203ece7826SWillem de Bruijn page = (struct page *)page_private(page); 12213ece7826SWillem de Bruijn } 12223ece7826SWillem de Bruijn copy = min_t(u32, PAGE_SIZE - d_off, p_len - done); 12233ece7826SWillem de Bruijn memcpy(page_address(page) + d_off, 12243ece7826SWillem de Bruijn vaddr + p_off + done, copy); 12253ece7826SWillem de Bruijn done += copy; 12263ece7826SWillem de Bruijn d_off += copy; 12273ece7826SWillem de Bruijn } 122851c56b00SEric Dumazet kunmap_atomic(vaddr); 1229c613c209SWillem de Bruijn } 1230a6686f2fSShirley Ma } 1231a6686f2fSShirley Ma 1232a6686f2fSShirley Ma /* skb frags release userspace buffers */ 123302756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 1234a8605c60SIan Campbell skb_frag_unref(skb, i); 1235a6686f2fSShirley Ma 1236a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 12373ece7826SWillem de Bruijn for (i = 0; i < new_frags - 1; i++) { 12383ece7826SWillem de Bruijn __skb_fill_page_desc(skb, i, head, 0, PAGE_SIZE); 123940dadff2SSunghan Suh head = (struct page *)page_private(head); 1240a6686f2fSShirley Ma } 12413ece7826SWillem de Bruijn __skb_fill_page_desc(skb, new_frags - 1, head, 0, d_off); 12423ece7826SWillem de Bruijn skb_shinfo(skb)->nr_frags = new_frags; 124348c83012SMichael S. Tsirkin 12441f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 1245a6686f2fSShirley Ma return 0; 1246a6686f2fSShirley Ma } 1247dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 1248a6686f2fSShirley Ma 1249e0053ec0SHerbert Xu /** 1250e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 1251e0053ec0SHerbert Xu * @skb: buffer to clone 1252e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1253e0053ec0SHerbert Xu * 1254e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1255e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1256e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1257e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1258e0053ec0SHerbert Xu * 1259e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1260e0053ec0SHerbert Xu * %GFP_ATOMIC. 1261e0053ec0SHerbert Xu */ 1262e0053ec0SHerbert Xu 1263e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1264e0053ec0SHerbert Xu { 1265d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1266d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1267d0bf4a9eSEric Dumazet skb1); 12686ffe75ebSEric Dumazet struct sk_buff *n; 1269e0053ec0SHerbert Xu 127070008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1271a6686f2fSShirley Ma return NULL; 1272a6686f2fSShirley Ma 1273e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 12742638595aSReshetova, Elena refcount_read(&fclones->fclone_ref) == 1) { 12756ffe75ebSEric Dumazet n = &fclones->skb2; 12762638595aSReshetova, Elena refcount_set(&fclones->fclone_ref, 2); 1277e0053ec0SHerbert Xu } else { 1278c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1279c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1280c93bdd0eSMel Gorman 1281e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1282e0053ec0SHerbert Xu if (!n) 1283e0053ec0SHerbert Xu return NULL; 1284fe55f6d5SVegard Nossum 1285fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 1286e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1287e0053ec0SHerbert Xu } 1288e0053ec0SHerbert Xu 1289e0053ec0SHerbert Xu return __skb_clone(n, skb); 12901da177e4SLinus Torvalds } 1291b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 12921da177e4SLinus Torvalds 1293f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 1294f5b17294SPravin B Shelar { 1295030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1296030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1297030737bcSEric Dumazet skb->csum_start += off; 1298f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1299f5b17294SPravin B Shelar skb->transport_header += off; 1300f5b17294SPravin B Shelar skb->network_header += off; 1301f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1302f5b17294SPravin B Shelar skb->mac_header += off; 1303f5b17294SPravin B Shelar skb->inner_transport_header += off; 1304f5b17294SPravin B Shelar skb->inner_network_header += off; 1305aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1306f5b17294SPravin B Shelar } 1307f5b17294SPravin B Shelar 13081da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 13091da177e4SLinus Torvalds { 1310dec18810SHerbert Xu __copy_skb_header(new, old); 1311dec18810SHerbert Xu 13127967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 13137967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 13147967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 1317c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1318c93bdd0eSMel Gorman { 1319c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1320c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1321c93bdd0eSMel Gorman return 0; 1322c93bdd0eSMel Gorman } 1323c93bdd0eSMel Gorman 13241da177e4SLinus Torvalds /** 13251da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 13261da177e4SLinus Torvalds * @skb: buffer to copy 13271da177e4SLinus Torvalds * @gfp_mask: allocation priority 13281da177e4SLinus Torvalds * 13291da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 13301da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 13311da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 13321da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 13331da177e4SLinus Torvalds * 13341da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 13351da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 13361da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 13371da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 13381da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 13391da177e4SLinus Torvalds */ 13401da177e4SLinus Torvalds 1341dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 13421da177e4SLinus Torvalds { 13436602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1344ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1345c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1346c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 13476602cebbSEric Dumazet 13481da177e4SLinus Torvalds if (!n) 13491da177e4SLinus Torvalds return NULL; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /* Set the data pointer */ 13521da177e4SLinus Torvalds skb_reserve(n, headerlen); 13531da177e4SLinus Torvalds /* Set the tail pointer and length */ 13541da177e4SLinus Torvalds skb_put(n, skb->len); 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 13571da177e4SLinus Torvalds BUG(); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds copy_skb_header(n, skb); 13601da177e4SLinus Torvalds return n; 13611da177e4SLinus Torvalds } 1362b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds /** 1365bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 13661da177e4SLinus Torvalds * @skb: buffer to copy 1367117632e6SEric Dumazet * @headroom: headroom of new skb 13681da177e4SLinus Torvalds * @gfp_mask: allocation priority 1369bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1370bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1371bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 13721da177e4SLinus Torvalds * 13731da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 13741da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 13751da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 13761da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 13771da177e4SLinus Torvalds * or the pointer to the buffer on success. 13781da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 13791da177e4SLinus Torvalds */ 13801da177e4SLinus Torvalds 1381bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1382bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 13831da177e4SLinus Torvalds { 1384117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1385bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1386bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 13876602cebbSEric Dumazet 13881da177e4SLinus Torvalds if (!n) 13891da177e4SLinus Torvalds goto out; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* Set the data pointer */ 1392117632e6SEric Dumazet skb_reserve(n, headroom); 13931da177e4SLinus Torvalds /* Set the tail pointer and length */ 13941da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 13951da177e4SLinus Torvalds /* Copy the bytes */ 1396d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 13971da177e4SLinus Torvalds 139825f484a6SHerbert Xu n->truesize += skb->data_len; 13991da177e4SLinus Torvalds n->data_len = skb->data_len; 14001da177e4SLinus Torvalds n->len = skb->len; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 14031da177e4SLinus Torvalds int i; 14041da177e4SLinus Torvalds 14051f8b977aSWillem de Bruijn if (skb_orphan_frags(skb, gfp_mask) || 14061f8b977aSWillem de Bruijn skb_zerocopy_clone(n, skb, gfp_mask)) { 14071511022cSDan Carpenter kfree_skb(n); 14081511022cSDan Carpenter n = NULL; 1409a6686f2fSShirley Ma goto out; 1410a6686f2fSShirley Ma } 14111da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14121da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1413ea2ab693SIan Campbell skb_frag_ref(skb, i); 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 141821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 14191da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 14201da177e4SLinus Torvalds skb_clone_fraglist(n); 14211da177e4SLinus Torvalds } 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds copy_skb_header(n, skb); 14241da177e4SLinus Torvalds out: 14251da177e4SLinus Torvalds return n; 14261da177e4SLinus Torvalds } 1427bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds /** 14301da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 14311da177e4SLinus Torvalds * @skb: buffer to reallocate 14321da177e4SLinus Torvalds * @nhead: room to add at head 14331da177e4SLinus Torvalds * @ntail: room to add at tail 14341da177e4SLinus Torvalds * @gfp_mask: allocation priority 14351da177e4SLinus Torvalds * 1436bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1437bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 14381da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 14391da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 14401da177e4SLinus Torvalds * 14411da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 14421da177e4SLinus Torvalds * reloaded after call to this function. 14431da177e4SLinus Torvalds */ 14441da177e4SLinus Torvalds 144586a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1446dd0fc66fSAl Viro gfp_t gfp_mask) 14471da177e4SLinus Torvalds { 1448158f323bSEric Dumazet int i, osize = skb_end_offset(skb); 1449158f323bSEric Dumazet int size = osize + nhead + ntail; 14501da177e4SLinus Torvalds long off; 1451158f323bSEric Dumazet u8 *data; 14521da177e4SLinus Torvalds 14534edd87adSHerbert Xu BUG_ON(nhead < 0); 14544edd87adSHerbert Xu 14551da177e4SLinus Torvalds if (skb_shared(skb)) 14561da177e4SLinus Torvalds BUG(); 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 14591da177e4SLinus Torvalds 1460c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1461c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1462c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1463c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 14641da177e4SLinus Torvalds if (!data) 14651da177e4SLinus Torvalds goto nodata; 146687151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 14696602cebbSEric Dumazet * optimized for the cases when header is void. 14706602cebbSEric Dumazet */ 14716602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 14726602cebbSEric Dumazet 14736602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 14746602cebbSEric Dumazet skb_shinfo(skb), 1475fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 14761da177e4SLinus Torvalds 14773e24591aSAlexander Duyck /* 14783e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 14793e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 14803e24591aSAlexander Duyck * be since all we did is relocate the values 14813e24591aSAlexander Duyck */ 14823e24591aSAlexander Duyck if (skb_cloned(skb)) { 148370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1484a6686f2fSShirley Ma goto nofrags; 14851f8b977aSWillem de Bruijn if (skb_zcopy(skb)) 14861f8b977aSWillem de Bruijn atomic_inc(&skb_uarg(skb)->refcnt); 14871da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1488ea2ab693SIan Campbell skb_frag_ref(skb, i); 14891da177e4SLinus Torvalds 149021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 14911da177e4SLinus Torvalds skb_clone_fraglist(skb); 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds skb_release_data(skb); 14943e24591aSAlexander Duyck } else { 14953e24591aSAlexander Duyck skb_free_head(skb); 14961fd63041SEric Dumazet } 14971da177e4SLinus Torvalds off = (data + nhead) - skb->head; 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds skb->head = data; 1500d3836f21SEric Dumazet skb->head_frag = 0; 15011da177e4SLinus Torvalds skb->data += off; 15024305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 15034305b541SArnaldo Carvalho de Melo skb->end = size; 150456eb8882SPatrick McHardy off = nhead; 15054305b541SArnaldo Carvalho de Melo #else 15064305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 150756eb8882SPatrick McHardy #endif 150827a884dcSArnaldo Carvalho de Melo skb->tail += off; 1509b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 15101da177e4SLinus Torvalds skb->cloned = 0; 1511334a8132SPatrick McHardy skb->hdr_len = 0; 15121da177e4SLinus Torvalds skb->nohdr = 0; 15131da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 1514158f323bSEric Dumazet 1515158f323bSEric Dumazet /* It is not generally safe to change skb->truesize. 1516158f323bSEric Dumazet * For the moment, we really care of rx path, or 1517158f323bSEric Dumazet * when skb is orphaned (not attached to a socket). 1518158f323bSEric Dumazet */ 1519158f323bSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1520158f323bSEric Dumazet skb->truesize += size - osize; 1521158f323bSEric Dumazet 15221da177e4SLinus Torvalds return 0; 15231da177e4SLinus Torvalds 1524a6686f2fSShirley Ma nofrags: 1525a6686f2fSShirley Ma kfree(data); 15261da177e4SLinus Torvalds nodata: 15271da177e4SLinus Torvalds return -ENOMEM; 15281da177e4SLinus Torvalds } 1529b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 15341da177e4SLinus Torvalds { 15351da177e4SLinus Torvalds struct sk_buff *skb2; 15361da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds if (delta <= 0) 15391da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 15401da177e4SLinus Torvalds else { 15411da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 15421da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 15431da177e4SLinus Torvalds GFP_ATOMIC)) { 15441da177e4SLinus Torvalds kfree_skb(skb2); 15451da177e4SLinus Torvalds skb2 = NULL; 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds return skb2; 15491da177e4SLinus Torvalds } 1550b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds /** 15531da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 15541da177e4SLinus Torvalds * @skb: buffer to copy 15551da177e4SLinus Torvalds * @newheadroom: new free bytes at head 15561da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 15571da177e4SLinus Torvalds * @gfp_mask: allocation priority 15581da177e4SLinus Torvalds * 15591da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 15601da177e4SLinus Torvalds * allocate additional space. 15611da177e4SLinus Torvalds * 15621da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 15631da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 15641da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 15651da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 15661da177e4SLinus Torvalds * 15671da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 15681da177e4SLinus Torvalds * is called from an interrupt. 15691da177e4SLinus Torvalds */ 15701da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 157186a76cafSVictor Fusco int newheadroom, int newtailroom, 1572dd0fc66fSAl Viro gfp_t gfp_mask) 15731da177e4SLinus Torvalds { 15741da177e4SLinus Torvalds /* 15751da177e4SLinus Torvalds * Allocate the copy buffer 15761da177e4SLinus Torvalds */ 1577c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1578c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1579c93bdd0eSMel Gorman NUMA_NO_NODE); 1580efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 15811da177e4SLinus Torvalds int head_copy_len, head_copy_off; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds if (!n) 15841da177e4SLinus Torvalds return NULL; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds skb_reserve(n, newheadroom); 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds /* Set the tail pointer and length */ 15891da177e4SLinus Torvalds skb_put(n, skb->len); 15901da177e4SLinus Torvalds 1591efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 15921da177e4SLinus Torvalds head_copy_off = 0; 15931da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 15941da177e4SLinus Torvalds head_copy_len = newheadroom; 15951da177e4SLinus Torvalds else 15961da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds /* Copy the linear header and data. */ 15991da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 16001da177e4SLinus Torvalds skb->len + head_copy_len)) 16011da177e4SLinus Torvalds BUG(); 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds copy_skb_header(n, skb); 16041da177e4SLinus Torvalds 1605030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1606efd1e8d5SPatrick McHardy 16071da177e4SLinus Torvalds return n; 16081da177e4SLinus Torvalds } 1609b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds /** 1612cd0a137aSFlorian Fainelli * __skb_pad - zero pad the tail of an skb 16131da177e4SLinus Torvalds * @skb: buffer to pad 16141da177e4SLinus Torvalds * @pad: space to pad 1615cd0a137aSFlorian Fainelli * @free_on_error: free buffer on error 16161da177e4SLinus Torvalds * 16171da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 16181da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 16191da177e4SLinus Torvalds * beyond the buffer end onto the wire. 16201da177e4SLinus Torvalds * 1621cd0a137aSFlorian Fainelli * May return error in out of memory cases. The skb is freed on error 1622cd0a137aSFlorian Fainelli * if @free_on_error is true. 16231da177e4SLinus Torvalds */ 16241da177e4SLinus Torvalds 1625cd0a137aSFlorian Fainelli int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error) 16261da177e4SLinus Torvalds { 16275b057c6bSHerbert Xu int err; 16285b057c6bSHerbert Xu int ntail; 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 16315b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 16321da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 16335b057c6bSHerbert Xu return 0; 16341da177e4SLinus Torvalds } 16351da177e4SLinus Torvalds 16364305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 16375b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 16385b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 16395b057c6bSHerbert Xu if (unlikely(err)) 16405b057c6bSHerbert Xu goto free_skb; 16415b057c6bSHerbert Xu } 16425b057c6bSHerbert Xu 16435b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 16445b057c6bSHerbert Xu * to be audited. 16455b057c6bSHerbert Xu */ 16465b057c6bSHerbert Xu err = skb_linearize(skb); 16475b057c6bSHerbert Xu if (unlikely(err)) 16485b057c6bSHerbert Xu goto free_skb; 16495b057c6bSHerbert Xu 16505b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 16515b057c6bSHerbert Xu return 0; 16525b057c6bSHerbert Xu 16535b057c6bSHerbert Xu free_skb: 1654cd0a137aSFlorian Fainelli if (free_on_error) 16551da177e4SLinus Torvalds kfree_skb(skb); 16565b057c6bSHerbert Xu return err; 16571da177e4SLinus Torvalds } 1658cd0a137aSFlorian Fainelli EXPORT_SYMBOL(__skb_pad); 16591da177e4SLinus Torvalds 16600dde3e16SIlpo Järvinen /** 16610c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 16620c7ddf36SMathias Krause * @skb: start of the buffer to use 16630c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 16640c7ddf36SMathias Krause * @len: amount of data to add 16650c7ddf36SMathias Krause * 16660c7ddf36SMathias Krause * This function extends the used data area of the potentially 16670c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 16680c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 16690c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 16700c7ddf36SMathias Krause * returned. 16710c7ddf36SMathias Krause */ 16720c7ddf36SMathias Krause 16734df864c1SJohannes Berg void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 16740c7ddf36SMathias Krause { 16750c7ddf36SMathias Krause if (tail != skb) { 16760c7ddf36SMathias Krause skb->data_len += len; 16770c7ddf36SMathias Krause skb->len += len; 16780c7ddf36SMathias Krause } 16790c7ddf36SMathias Krause return skb_put(tail, len); 16800c7ddf36SMathias Krause } 16810c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 16820c7ddf36SMathias Krause 16830c7ddf36SMathias Krause /** 16840dde3e16SIlpo Järvinen * skb_put - add data to a buffer 16850dde3e16SIlpo Järvinen * @skb: buffer to use 16860dde3e16SIlpo Järvinen * @len: amount of data to add 16870dde3e16SIlpo Järvinen * 16880dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 16890dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 16900dde3e16SIlpo Järvinen * first byte of the extra data is returned. 16910dde3e16SIlpo Järvinen */ 16924df864c1SJohannes Berg void *skb_put(struct sk_buff *skb, unsigned int len) 16930dde3e16SIlpo Järvinen { 16944df864c1SJohannes Berg void *tmp = skb_tail_pointer(skb); 16950dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 16960dde3e16SIlpo Järvinen skb->tail += len; 16970dde3e16SIlpo Järvinen skb->len += len; 16980dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 16990dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 17000dde3e16SIlpo Järvinen return tmp; 17010dde3e16SIlpo Järvinen } 17020dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 17030dde3e16SIlpo Järvinen 17046be8ac2fSIlpo Järvinen /** 1705c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1706c2aa270aSIlpo Järvinen * @skb: buffer to use 1707c2aa270aSIlpo Järvinen * @len: amount of data to add 1708c2aa270aSIlpo Järvinen * 1709c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1710c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1711c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1712c2aa270aSIlpo Järvinen */ 1713d58ff351SJohannes Berg void *skb_push(struct sk_buff *skb, unsigned int len) 1714c2aa270aSIlpo Järvinen { 1715c2aa270aSIlpo Järvinen skb->data -= len; 1716c2aa270aSIlpo Järvinen skb->len += len; 1717c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1718c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1719c2aa270aSIlpo Järvinen return skb->data; 1720c2aa270aSIlpo Järvinen } 1721c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1722c2aa270aSIlpo Järvinen 1723c2aa270aSIlpo Järvinen /** 17246be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 17256be8ac2fSIlpo Järvinen * @skb: buffer to use 17266be8ac2fSIlpo Järvinen * @len: amount of data to remove 17276be8ac2fSIlpo Järvinen * 17286be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 17296be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 17306be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 17316be8ac2fSIlpo Järvinen * the old data. 17326be8ac2fSIlpo Järvinen */ 1733af72868bSJohannes Berg void *skb_pull(struct sk_buff *skb, unsigned int len) 17346be8ac2fSIlpo Järvinen { 173547d29646SDavid S. Miller return skb_pull_inline(skb, len); 17366be8ac2fSIlpo Järvinen } 17376be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 17386be8ac2fSIlpo Järvinen 1739419ae74eSIlpo Järvinen /** 1740419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1741419ae74eSIlpo Järvinen * @skb: buffer to alter 1742419ae74eSIlpo Järvinen * @len: new length 1743419ae74eSIlpo Järvinen * 1744419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1745419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1746419ae74eSIlpo Järvinen * The skb must be linear. 1747419ae74eSIlpo Järvinen */ 1748419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1749419ae74eSIlpo Järvinen { 1750419ae74eSIlpo Järvinen if (skb->len > len) 1751419ae74eSIlpo Järvinen __skb_trim(skb, len); 1752419ae74eSIlpo Järvinen } 1753419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1754419ae74eSIlpo Järvinen 17553cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 17561da177e4SLinus Torvalds */ 17571da177e4SLinus Torvalds 17583cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 17591da177e4SLinus Torvalds { 176027b437c8SHerbert Xu struct sk_buff **fragp; 176127b437c8SHerbert Xu struct sk_buff *frag; 17621da177e4SLinus Torvalds int offset = skb_headlen(skb); 17631da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 17641da177e4SLinus Torvalds int i; 176527b437c8SHerbert Xu int err; 176627b437c8SHerbert Xu 176727b437c8SHerbert Xu if (skb_cloned(skb) && 176827b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 176927b437c8SHerbert Xu return err; 17701da177e4SLinus Torvalds 1771f4d26fb3SHerbert Xu i = 0; 1772f4d26fb3SHerbert Xu if (offset >= len) 1773f4d26fb3SHerbert Xu goto drop_pages; 1774f4d26fb3SHerbert Xu 1775f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 17769e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 177727b437c8SHerbert Xu 177827b437c8SHerbert Xu if (end < len) { 17791da177e4SLinus Torvalds offset = end; 178027b437c8SHerbert Xu continue; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17839e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 178427b437c8SHerbert Xu 1785f4d26fb3SHerbert Xu drop_pages: 178627b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 178727b437c8SHerbert Xu 178827b437c8SHerbert Xu for (; i < nfrags; i++) 1789ea2ab693SIan Campbell skb_frag_unref(skb, i); 179027b437c8SHerbert Xu 179121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 179227b437c8SHerbert Xu skb_drop_fraglist(skb); 1793f4d26fb3SHerbert Xu goto done; 179427b437c8SHerbert Xu } 179527b437c8SHerbert Xu 179627b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 179727b437c8SHerbert Xu fragp = &frag->next) { 179827b437c8SHerbert Xu int end = offset + frag->len; 179927b437c8SHerbert Xu 180027b437c8SHerbert Xu if (skb_shared(frag)) { 180127b437c8SHerbert Xu struct sk_buff *nfrag; 180227b437c8SHerbert Xu 180327b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 180427b437c8SHerbert Xu if (unlikely(!nfrag)) 180527b437c8SHerbert Xu return -ENOMEM; 180627b437c8SHerbert Xu 180727b437c8SHerbert Xu nfrag->next = frag->next; 180885bb2a60SEric Dumazet consume_skb(frag); 180927b437c8SHerbert Xu frag = nfrag; 181027b437c8SHerbert Xu *fragp = frag; 181127b437c8SHerbert Xu } 181227b437c8SHerbert Xu 181327b437c8SHerbert Xu if (end < len) { 181427b437c8SHerbert Xu offset = end; 181527b437c8SHerbert Xu continue; 181627b437c8SHerbert Xu } 181727b437c8SHerbert Xu 181827b437c8SHerbert Xu if (end > len && 181927b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 182027b437c8SHerbert Xu return err; 182127b437c8SHerbert Xu 182227b437c8SHerbert Xu if (frag->next) 182327b437c8SHerbert Xu skb_drop_list(&frag->next); 182427b437c8SHerbert Xu break; 182527b437c8SHerbert Xu } 182627b437c8SHerbert Xu 1827f4d26fb3SHerbert Xu done: 182827b437c8SHerbert Xu if (len > skb_headlen(skb)) { 18291da177e4SLinus Torvalds skb->data_len -= skb->len - len; 18301da177e4SLinus Torvalds skb->len = len; 18311da177e4SLinus Torvalds } else { 18321da177e4SLinus Torvalds skb->len = len; 18331da177e4SLinus Torvalds skb->data_len = 0; 183427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds 1837c21b48ccSEric Dumazet if (!skb->sk || skb->destructor == sock_edemux) 1838c21b48ccSEric Dumazet skb_condense(skb); 18391da177e4SLinus Torvalds return 0; 18401da177e4SLinus Torvalds } 1841b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds /** 18441da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 18451da177e4SLinus Torvalds * @skb: buffer to reallocate 18461da177e4SLinus Torvalds * @delta: number of bytes to advance tail 18471da177e4SLinus Torvalds * 18481da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 18491da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 18501da177e4SLinus Torvalds * data from fragmented part. 18511da177e4SLinus Torvalds * 18521da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 18531da177e4SLinus Torvalds * 18541da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 18551da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 18561da177e4SLinus Torvalds * 18571da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 18581da177e4SLinus Torvalds * reloaded after call to this function. 18591da177e4SLinus Torvalds */ 18601da177e4SLinus Torvalds 18611da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 18621da177e4SLinus Torvalds * when it is necessary. 18631da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 18641da177e4SLinus Torvalds * 2. It may change skb pointers. 18651da177e4SLinus Torvalds * 18661da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 18671da177e4SLinus Torvalds */ 1868af72868bSJohannes Berg void *__pskb_pull_tail(struct sk_buff *skb, int delta) 18691da177e4SLinus Torvalds { 18701da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 18711da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 18721da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 18731da177e4SLinus Torvalds */ 18744305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 18771da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 18781da177e4SLinus Torvalds GFP_ATOMIC)) 18791da177e4SLinus Torvalds return NULL; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 188227a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 18831da177e4SLinus Torvalds BUG(); 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 18861da177e4SLinus Torvalds * size of pulled pages. Superb. 18871da177e4SLinus Torvalds */ 188821dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 18891da177e4SLinus Torvalds goto pull_pages; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 18921da177e4SLinus Torvalds eat = delta; 18931da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 18949e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 18959e903e08SEric Dumazet 18969e903e08SEric Dumazet if (size >= eat) 18971da177e4SLinus Torvalds goto pull_pages; 18989e903e08SEric Dumazet eat -= size; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 19021da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 19031da177e4SLinus Torvalds * but taking into account that pulling is expected to 19041da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 19051da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 19061da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 19071da177e4SLinus Torvalds */ 19081da177e4SLinus Torvalds if (eat) { 19091da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 19101da177e4SLinus Torvalds struct sk_buff *clone = NULL; 19111da177e4SLinus Torvalds struct sk_buff *insp = NULL; 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds do { 191409a62660SKris Katterjohn BUG_ON(!list); 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds if (list->len <= eat) { 19171da177e4SLinus Torvalds /* Eaten as whole. */ 19181da177e4SLinus Torvalds eat -= list->len; 19191da177e4SLinus Torvalds list = list->next; 19201da177e4SLinus Torvalds insp = list; 19211da177e4SLinus Torvalds } else { 19221da177e4SLinus Torvalds /* Eaten partially. */ 19231da177e4SLinus Torvalds 19241da177e4SLinus Torvalds if (skb_shared(list)) { 19251da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 19261da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 19271da177e4SLinus Torvalds if (!clone) 19281da177e4SLinus Torvalds return NULL; 19291da177e4SLinus Torvalds insp = list->next; 19301da177e4SLinus Torvalds list = clone; 19311da177e4SLinus Torvalds } else { 19321da177e4SLinus Torvalds /* This may be pulled without 19331da177e4SLinus Torvalds * problems. */ 19341da177e4SLinus Torvalds insp = list; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 19371da177e4SLinus Torvalds kfree_skb(clone); 19381da177e4SLinus Torvalds return NULL; 19391da177e4SLinus Torvalds } 19401da177e4SLinus Torvalds break; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds } while (eat); 19431da177e4SLinus Torvalds 19441da177e4SLinus Torvalds /* Free pulled out fragments. */ 19451da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 19461da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 19471da177e4SLinus Torvalds kfree_skb(list); 19481da177e4SLinus Torvalds } 19491da177e4SLinus Torvalds /* And insert new clone at head. */ 19501da177e4SLinus Torvalds if (clone) { 19511da177e4SLinus Torvalds clone->next = list; 19521da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds pull_pages: 19581da177e4SLinus Torvalds eat = delta; 19591da177e4SLinus Torvalds k = 0; 19601da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19619e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 19629e903e08SEric Dumazet 19639e903e08SEric Dumazet if (size <= eat) { 1964ea2ab693SIan Campbell skb_frag_unref(skb, i); 19659e903e08SEric Dumazet eat -= size; 19661da177e4SLinus Torvalds } else { 19671da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 19681da177e4SLinus Torvalds if (eat) { 19691da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 19709e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 19713ccc6c6fSlinzhang if (!i) 19723ccc6c6fSlinzhang goto end; 19731da177e4SLinus Torvalds eat = 0; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds k++; 19761da177e4SLinus Torvalds } 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 19791da177e4SLinus Torvalds 19803ccc6c6fSlinzhang end: 19811da177e4SLinus Torvalds skb->tail += delta; 19821da177e4SLinus Torvalds skb->data_len -= delta; 19831da177e4SLinus Torvalds 19841f8b977aSWillem de Bruijn if (!skb->data_len) 19851f8b977aSWillem de Bruijn skb_zcopy_clear(skb, false); 19861f8b977aSWillem de Bruijn 198727a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 19881da177e4SLinus Torvalds } 1989b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 19901da177e4SLinus Torvalds 199122019b17SEric Dumazet /** 199222019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 199322019b17SEric Dumazet * @skb: source skb 199422019b17SEric Dumazet * @offset: offset in source 199522019b17SEric Dumazet * @to: destination buffer 199622019b17SEric Dumazet * @len: number of bytes to copy 199722019b17SEric Dumazet * 199822019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 199922019b17SEric Dumazet * destination buffer. 200022019b17SEric Dumazet * 200122019b17SEric Dumazet * CAUTION ! : 200222019b17SEric Dumazet * If its prototype is ever changed, 200322019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 200422019b17SEric Dumazet * since it is called from BPF assembly code. 200522019b17SEric Dumazet */ 20061da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 20071da177e4SLinus Torvalds { 20081a028e50SDavid S. Miller int start = skb_headlen(skb); 2009fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2010fbb398a8SDavid S. Miller int i, copy; 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds if (offset > (int)skb->len - len) 20131da177e4SLinus Torvalds goto fault; 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds /* Copy header. */ 20161a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 20171da177e4SLinus Torvalds if (copy > len) 20181da177e4SLinus Torvalds copy = len; 2019d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 20201da177e4SLinus Torvalds if ((len -= copy) == 0) 20211da177e4SLinus Torvalds return 0; 20221da177e4SLinus Torvalds offset += copy; 20231da177e4SLinus Torvalds to += copy; 20241da177e4SLinus Torvalds } 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20271a028e50SDavid S. Miller int end; 202851c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 20291da177e4SLinus Torvalds 2030547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20311a028e50SDavid S. Miller 203251c56b00SEric Dumazet end = start + skb_frag_size(f); 20331da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2034c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2035c613c209SWillem de Bruijn struct page *p; 20361da177e4SLinus Torvalds u8 *vaddr; 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds if (copy > len) 20391da177e4SLinus Torvalds copy = len; 20401da177e4SLinus Torvalds 2041c613c209SWillem de Bruijn skb_frag_foreach_page(f, 2042c613c209SWillem de Bruijn f->page_offset + offset - start, 2043c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2044c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2045c613c209SWillem de Bruijn memcpy(to + copied, vaddr + p_off, p_len); 204651c56b00SEric Dumazet kunmap_atomic(vaddr); 2047c613c209SWillem de Bruijn } 20481da177e4SLinus Torvalds 20491da177e4SLinus Torvalds if ((len -= copy) == 0) 20501da177e4SLinus Torvalds return 0; 20511da177e4SLinus Torvalds offset += copy; 20521da177e4SLinus Torvalds to += copy; 20531da177e4SLinus Torvalds } 20541a028e50SDavid S. Miller start = end; 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 2057fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20581a028e50SDavid S. Miller int end; 20591da177e4SLinus Torvalds 2060547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20611a028e50SDavid S. Miller 2062fbb398a8SDavid S. Miller end = start + frag_iter->len; 20631da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20641da177e4SLinus Torvalds if (copy > len) 20651da177e4SLinus Torvalds copy = len; 2066fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 20671da177e4SLinus Torvalds goto fault; 20681da177e4SLinus Torvalds if ((len -= copy) == 0) 20691da177e4SLinus Torvalds return 0; 20701da177e4SLinus Torvalds offset += copy; 20711da177e4SLinus Torvalds to += copy; 20721da177e4SLinus Torvalds } 20731a028e50SDavid S. Miller start = end; 20741da177e4SLinus Torvalds } 2075a6686f2fSShirley Ma 20761da177e4SLinus Torvalds if (!len) 20771da177e4SLinus Torvalds return 0; 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds fault: 20801da177e4SLinus Torvalds return -EFAULT; 20811da177e4SLinus Torvalds } 2082b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 20831da177e4SLinus Torvalds 20849c55e01cSJens Axboe /* 20859c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 20869c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 20879c55e01cSJens Axboe */ 20889c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 20899c55e01cSJens Axboe { 20908b9d3728SJarek Poplawski put_page(spd->pages[i]); 20918b9d3728SJarek Poplawski } 20929c55e01cSJens Axboe 2093a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 20944fb66994SJarek Poplawski unsigned int *offset, 209518aafc62SEric Dumazet struct sock *sk) 20968b9d3728SJarek Poplawski { 20975640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 20988b9d3728SJarek Poplawski 20995640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 21008b9d3728SJarek Poplawski return NULL; 21014fb66994SJarek Poplawski 21025640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 21034fb66994SJarek Poplawski 21045640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 21055640f768SEric Dumazet page_address(page) + *offset, *len); 21065640f768SEric Dumazet *offset = pfrag->offset; 21075640f768SEric Dumazet pfrag->offset += *len; 21084fb66994SJarek Poplawski 21095640f768SEric Dumazet return pfrag->page; 21109c55e01cSJens Axboe } 21119c55e01cSJens Axboe 211241c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 211341c73a0dSEric Dumazet struct page *page, 211441c73a0dSEric Dumazet unsigned int offset) 211541c73a0dSEric Dumazet { 211641c73a0dSEric Dumazet return spd->nr_pages && 211741c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 211841c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 211941c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 212041c73a0dSEric Dumazet } 212141c73a0dSEric Dumazet 21229c55e01cSJens Axboe /* 21239c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 21249c55e01cSJens Axboe */ 2125a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 212635f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 21274fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 212818aafc62SEric Dumazet bool linear, 21297a67e56fSJarek Poplawski struct sock *sk) 21309c55e01cSJens Axboe { 213141c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 2132a108d5f3SDavid S. Miller return true; 21339c55e01cSJens Axboe 21348b9d3728SJarek Poplawski if (linear) { 213518aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 21368b9d3728SJarek Poplawski if (!page) 2137a108d5f3SDavid S. Miller return true; 213841c73a0dSEric Dumazet } 213941c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 214041c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 2141a108d5f3SDavid S. Miller return false; 214241c73a0dSEric Dumazet } 21438b9d3728SJarek Poplawski get_page(page); 21449c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 21454fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 21469c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 21479c55e01cSJens Axboe spd->nr_pages++; 21488b9d3728SJarek Poplawski 2149a108d5f3SDavid S. Miller return false; 21509c55e01cSJens Axboe } 21519c55e01cSJens Axboe 2152a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 21532870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 215418aafc62SEric Dumazet unsigned int *len, 2155d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 215635f3d14dSJens Axboe struct sock *sk, 215735f3d14dSJens Axboe struct pipe_inode_info *pipe) 21589c55e01cSJens Axboe { 21592870c43dSOctavian Purdila if (!*len) 2160a108d5f3SDavid S. Miller return true; 21619c55e01cSJens Axboe 21622870c43dSOctavian Purdila /* skip this segment if already processed */ 21632870c43dSOctavian Purdila if (*off >= plen) { 21642870c43dSOctavian Purdila *off -= plen; 2165a108d5f3SDavid S. Miller return false; 21662870c43dSOctavian Purdila } 21672870c43dSOctavian Purdila 21682870c43dSOctavian Purdila /* ignore any bits we already processed */ 21699ca1b22dSEric Dumazet poff += *off; 21709ca1b22dSEric Dumazet plen -= *off; 21712870c43dSOctavian Purdila *off = 0; 21722870c43dSOctavian Purdila 217318aafc62SEric Dumazet do { 217418aafc62SEric Dumazet unsigned int flen = min(*len, plen); 21752870c43dSOctavian Purdila 217618aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 217718aafc62SEric Dumazet linear, sk)) 2178a108d5f3SDavid S. Miller return true; 217918aafc62SEric Dumazet poff += flen; 218018aafc62SEric Dumazet plen -= flen; 21812870c43dSOctavian Purdila *len -= flen; 218218aafc62SEric Dumazet } while (*len && plen); 21832870c43dSOctavian Purdila 2184a108d5f3SDavid S. Miller return false; 2185db43a282SOctavian Purdila } 21869c55e01cSJens Axboe 21879c55e01cSJens Axboe /* 2188a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 21892870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 21909c55e01cSJens Axboe */ 2191a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 219235f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 219335f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 21942870c43dSOctavian Purdila { 21952870c43dSOctavian Purdila int seg; 2196fa9835e5STom Herbert struct sk_buff *iter; 21979c55e01cSJens Axboe 21981d0c0b32SEric Dumazet /* map the linear part : 21992996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 22002996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 22012996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 22029c55e01cSJens Axboe */ 22032870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 22042870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 22052870c43dSOctavian Purdila skb_headlen(skb), 220618aafc62SEric Dumazet offset, len, spd, 22073a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 22081d0c0b32SEric Dumazet sk, pipe)) 2209a108d5f3SDavid S. Miller return true; 22109c55e01cSJens Axboe 22119c55e01cSJens Axboe /* 22129c55e01cSJens Axboe * then map the fragments 22139c55e01cSJens Axboe */ 22149c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 22159c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 22169c55e01cSJens Axboe 2217ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 22189e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 221918aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 2220a108d5f3SDavid S. Miller return true; 22219c55e01cSJens Axboe } 22229c55e01cSJens Axboe 2223fa9835e5STom Herbert skb_walk_frags(skb, iter) { 2224fa9835e5STom Herbert if (*offset >= iter->len) { 2225fa9835e5STom Herbert *offset -= iter->len; 2226fa9835e5STom Herbert continue; 2227fa9835e5STom Herbert } 2228fa9835e5STom Herbert /* __skb_splice_bits() only fails if the output has no room 2229fa9835e5STom Herbert * left, so no point in going over the frag_list for the error 2230fa9835e5STom Herbert * case. 2231fa9835e5STom Herbert */ 2232fa9835e5STom Herbert if (__skb_splice_bits(iter, pipe, offset, len, spd, sk)) 2233fa9835e5STom Herbert return true; 2234fa9835e5STom Herbert } 2235fa9835e5STom Herbert 2236a108d5f3SDavid S. Miller return false; 22379c55e01cSJens Axboe } 22389c55e01cSJens Axboe 22399c55e01cSJens Axboe /* 22409c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 2241fa9835e5STom Herbert * the fragments, and the frag list. 22429c55e01cSJens Axboe */ 2243a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 22449c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 224525869262SAl Viro unsigned int flags) 22469c55e01cSJens Axboe { 224741c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 224841c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 22499c55e01cSJens Axboe struct splice_pipe_desc spd = { 22509c55e01cSJens Axboe .pages = pages, 22519c55e01cSJens Axboe .partial = partial, 2252047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 225328a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 22549c55e01cSJens Axboe .spd_release = sock_spd_release, 22559c55e01cSJens Axboe }; 225635f3d14dSJens Axboe int ret = 0; 225735f3d14dSJens Axboe 2258fa9835e5STom Herbert __skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk); 22599c55e01cSJens Axboe 2260a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 226125869262SAl Viro ret = splice_to_pipe(pipe, &spd); 22629c55e01cSJens Axboe 226335f3d14dSJens Axboe return ret; 22649c55e01cSJens Axboe } 22652b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 22669c55e01cSJens Axboe 226720bf50deSTom Herbert /* Send skb data on a socket. Socket must be locked. */ 226820bf50deSTom Herbert int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset, 226920bf50deSTom Herbert int len) 227020bf50deSTom Herbert { 227120bf50deSTom Herbert unsigned int orig_len = len; 227220bf50deSTom Herbert struct sk_buff *head = skb; 227320bf50deSTom Herbert unsigned short fragidx; 227420bf50deSTom Herbert int slen, ret; 227520bf50deSTom Herbert 227620bf50deSTom Herbert do_frag_list: 227720bf50deSTom Herbert 227820bf50deSTom Herbert /* Deal with head data */ 227920bf50deSTom Herbert while (offset < skb_headlen(skb) && len) { 228020bf50deSTom Herbert struct kvec kv; 228120bf50deSTom Herbert struct msghdr msg; 228220bf50deSTom Herbert 228320bf50deSTom Herbert slen = min_t(int, len, skb_headlen(skb) - offset); 228420bf50deSTom Herbert kv.iov_base = skb->data + offset; 2285db5980d8SJohn Fastabend kv.iov_len = slen; 228620bf50deSTom Herbert memset(&msg, 0, sizeof(msg)); 228720bf50deSTom Herbert 228820bf50deSTom Herbert ret = kernel_sendmsg_locked(sk, &msg, &kv, 1, slen); 228920bf50deSTom Herbert if (ret <= 0) 229020bf50deSTom Herbert goto error; 229120bf50deSTom Herbert 229220bf50deSTom Herbert offset += ret; 229320bf50deSTom Herbert len -= ret; 229420bf50deSTom Herbert } 229520bf50deSTom Herbert 229620bf50deSTom Herbert /* All the data was skb head? */ 229720bf50deSTom Herbert if (!len) 229820bf50deSTom Herbert goto out; 229920bf50deSTom Herbert 230020bf50deSTom Herbert /* Make offset relative to start of frags */ 230120bf50deSTom Herbert offset -= skb_headlen(skb); 230220bf50deSTom Herbert 230320bf50deSTom Herbert /* Find where we are in frag list */ 230420bf50deSTom Herbert for (fragidx = 0; fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 230520bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 230620bf50deSTom Herbert 230720bf50deSTom Herbert if (offset < frag->size) 230820bf50deSTom Herbert break; 230920bf50deSTom Herbert 231020bf50deSTom Herbert offset -= frag->size; 231120bf50deSTom Herbert } 231220bf50deSTom Herbert 231320bf50deSTom Herbert for (; len && fragidx < skb_shinfo(skb)->nr_frags; fragidx++) { 231420bf50deSTom Herbert skb_frag_t *frag = &skb_shinfo(skb)->frags[fragidx]; 231520bf50deSTom Herbert 231620bf50deSTom Herbert slen = min_t(size_t, len, frag->size - offset); 231720bf50deSTom Herbert 231820bf50deSTom Herbert while (slen) { 231920bf50deSTom Herbert ret = kernel_sendpage_locked(sk, frag->page.p, 232020bf50deSTom Herbert frag->page_offset + offset, 232120bf50deSTom Herbert slen, MSG_DONTWAIT); 232220bf50deSTom Herbert if (ret <= 0) 232320bf50deSTom Herbert goto error; 232420bf50deSTom Herbert 232520bf50deSTom Herbert len -= ret; 232620bf50deSTom Herbert offset += ret; 232720bf50deSTom Herbert slen -= ret; 232820bf50deSTom Herbert } 232920bf50deSTom Herbert 233020bf50deSTom Herbert offset = 0; 233120bf50deSTom Herbert } 233220bf50deSTom Herbert 233320bf50deSTom Herbert if (len) { 233420bf50deSTom Herbert /* Process any frag lists */ 233520bf50deSTom Herbert 233620bf50deSTom Herbert if (skb == head) { 233720bf50deSTom Herbert if (skb_has_frag_list(skb)) { 233820bf50deSTom Herbert skb = skb_shinfo(skb)->frag_list; 233920bf50deSTom Herbert goto do_frag_list; 234020bf50deSTom Herbert } 234120bf50deSTom Herbert } else if (skb->next) { 234220bf50deSTom Herbert skb = skb->next; 234320bf50deSTom Herbert goto do_frag_list; 234420bf50deSTom Herbert } 234520bf50deSTom Herbert } 234620bf50deSTom Herbert 234720bf50deSTom Herbert out: 234820bf50deSTom Herbert return orig_len - len; 234920bf50deSTom Herbert 235020bf50deSTom Herbert error: 235120bf50deSTom Herbert return orig_len == len ? ret : orig_len - len; 235220bf50deSTom Herbert } 235320bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock_locked); 235420bf50deSTom Herbert 235520bf50deSTom Herbert /* Send skb data on a socket. */ 235620bf50deSTom Herbert int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len) 235720bf50deSTom Herbert { 235820bf50deSTom Herbert int ret = 0; 235920bf50deSTom Herbert 236020bf50deSTom Herbert lock_sock(sk); 236120bf50deSTom Herbert ret = skb_send_sock_locked(sk, skb, offset, len); 236220bf50deSTom Herbert release_sock(sk); 236320bf50deSTom Herbert 236420bf50deSTom Herbert return ret; 236520bf50deSTom Herbert } 236620bf50deSTom Herbert EXPORT_SYMBOL_GPL(skb_send_sock); 236720bf50deSTom Herbert 2368357b40a1SHerbert Xu /** 2369357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2370357b40a1SHerbert Xu * @skb: destination buffer 2371357b40a1SHerbert Xu * @offset: offset in destination 2372357b40a1SHerbert Xu * @from: source buffer 2373357b40a1SHerbert Xu * @len: number of bytes to copy 2374357b40a1SHerbert Xu * 2375357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2376357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2377357b40a1SHerbert Xu * traversing fragment lists and such. 2378357b40a1SHerbert Xu */ 2379357b40a1SHerbert Xu 23800c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2381357b40a1SHerbert Xu { 23821a028e50SDavid S. Miller int start = skb_headlen(skb); 2383fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2384fbb398a8SDavid S. Miller int i, copy; 2385357b40a1SHerbert Xu 2386357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2387357b40a1SHerbert Xu goto fault; 2388357b40a1SHerbert Xu 23891a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2390357b40a1SHerbert Xu if (copy > len) 2391357b40a1SHerbert Xu copy = len; 239227d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2393357b40a1SHerbert Xu if ((len -= copy) == 0) 2394357b40a1SHerbert Xu return 0; 2395357b40a1SHerbert Xu offset += copy; 2396357b40a1SHerbert Xu from += copy; 2397357b40a1SHerbert Xu } 2398357b40a1SHerbert Xu 2399357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2400357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 24011a028e50SDavid S. Miller int end; 2402357b40a1SHerbert Xu 2403547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24041a028e50SDavid S. Miller 24059e903e08SEric Dumazet end = start + skb_frag_size(frag); 2406357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2407c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2408c613c209SWillem de Bruijn struct page *p; 2409357b40a1SHerbert Xu u8 *vaddr; 2410357b40a1SHerbert Xu 2411357b40a1SHerbert Xu if (copy > len) 2412357b40a1SHerbert Xu copy = len; 2413357b40a1SHerbert Xu 2414c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2415c613c209SWillem de Bruijn frag->page_offset + offset - start, 2416c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2417c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2418c613c209SWillem de Bruijn memcpy(vaddr + p_off, from + copied, p_len); 241951c56b00SEric Dumazet kunmap_atomic(vaddr); 2420c613c209SWillem de Bruijn } 2421357b40a1SHerbert Xu 2422357b40a1SHerbert Xu if ((len -= copy) == 0) 2423357b40a1SHerbert Xu return 0; 2424357b40a1SHerbert Xu offset += copy; 2425357b40a1SHerbert Xu from += copy; 2426357b40a1SHerbert Xu } 24271a028e50SDavid S. Miller start = end; 2428357b40a1SHerbert Xu } 2429357b40a1SHerbert Xu 2430fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 24311a028e50SDavid S. Miller int end; 2432357b40a1SHerbert Xu 2433547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24341a028e50SDavid S. Miller 2435fbb398a8SDavid S. Miller end = start + frag_iter->len; 2436357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2437357b40a1SHerbert Xu if (copy > len) 2438357b40a1SHerbert Xu copy = len; 2439fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 24401a028e50SDavid S. Miller from, copy)) 2441357b40a1SHerbert Xu goto fault; 2442357b40a1SHerbert Xu if ((len -= copy) == 0) 2443357b40a1SHerbert Xu return 0; 2444357b40a1SHerbert Xu offset += copy; 2445357b40a1SHerbert Xu from += copy; 2446357b40a1SHerbert Xu } 24471a028e50SDavid S. Miller start = end; 2448357b40a1SHerbert Xu } 2449357b40a1SHerbert Xu if (!len) 2450357b40a1SHerbert Xu return 0; 2451357b40a1SHerbert Xu 2452357b40a1SHerbert Xu fault: 2453357b40a1SHerbert Xu return -EFAULT; 2454357b40a1SHerbert Xu } 2455357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2456357b40a1SHerbert Xu 24571da177e4SLinus Torvalds /* Checksum skb data. */ 24582817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 24592817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 24601da177e4SLinus Torvalds { 24611a028e50SDavid S. Miller int start = skb_headlen(skb); 24621a028e50SDavid S. Miller int i, copy = start - offset; 2463fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 24641da177e4SLinus Torvalds int pos = 0; 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds /* Checksum header. */ 24671da177e4SLinus Torvalds if (copy > 0) { 24681da177e4SLinus Torvalds if (copy > len) 24691da177e4SLinus Torvalds copy = len; 24702817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 24711da177e4SLinus Torvalds if ((len -= copy) == 0) 24721da177e4SLinus Torvalds return csum; 24731da177e4SLinus Torvalds offset += copy; 24741da177e4SLinus Torvalds pos = copy; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 24781a028e50SDavid S. Miller int end; 247951c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 24801da177e4SLinus Torvalds 2481547b792cSIlpo Järvinen WARN_ON(start > offset + len); 24821a028e50SDavid S. Miller 248351c56b00SEric Dumazet end = start + skb_frag_size(frag); 24841da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2485c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2486c613c209SWillem de Bruijn struct page *p; 248744bb9363SAl Viro __wsum csum2; 24881da177e4SLinus Torvalds u8 *vaddr; 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds if (copy > len) 24911da177e4SLinus Torvalds copy = len; 2492c613c209SWillem de Bruijn 2493c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2494c613c209SWillem de Bruijn frag->page_offset + offset - start, 2495c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2496c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2497c613c209SWillem de Bruijn csum2 = ops->update(vaddr + p_off, p_len, 0); 249851c56b00SEric Dumazet kunmap_atomic(vaddr); 2499c613c209SWillem de Bruijn csum = ops->combine(csum, csum2, pos, p_len); 2500c613c209SWillem de Bruijn pos += p_len; 2501c613c209SWillem de Bruijn } 2502c613c209SWillem de Bruijn 25031da177e4SLinus Torvalds if (!(len -= copy)) 25041da177e4SLinus Torvalds return csum; 25051da177e4SLinus Torvalds offset += copy; 25061da177e4SLinus Torvalds } 25071a028e50SDavid S. Miller start = end; 25081da177e4SLinus Torvalds } 25091da177e4SLinus Torvalds 2510fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 25111a028e50SDavid S. Miller int end; 25121da177e4SLinus Torvalds 2513547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25141a028e50SDavid S. Miller 2515fbb398a8SDavid S. Miller end = start + frag_iter->len; 25161da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 25175f92a738SAl Viro __wsum csum2; 25181da177e4SLinus Torvalds if (copy > len) 25191da177e4SLinus Torvalds copy = len; 25202817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 25212817a336SDaniel Borkmann copy, 0, ops); 25222817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 25231da177e4SLinus Torvalds if ((len -= copy) == 0) 25241da177e4SLinus Torvalds return csum; 25251da177e4SLinus Torvalds offset += copy; 25261da177e4SLinus Torvalds pos += copy; 25271da177e4SLinus Torvalds } 25281a028e50SDavid S. Miller start = end; 25291da177e4SLinus Torvalds } 253009a62660SKris Katterjohn BUG_ON(len); 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds return csum; 25331da177e4SLinus Torvalds } 25342817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 25352817a336SDaniel Borkmann 25362817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 25372817a336SDaniel Borkmann int len, __wsum csum) 25382817a336SDaniel Borkmann { 25392817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2540cea80ea8SDaniel Borkmann .update = csum_partial_ext, 25412817a336SDaniel Borkmann .combine = csum_block_add_ext, 25422817a336SDaniel Borkmann }; 25432817a336SDaniel Borkmann 25442817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 25452817a336SDaniel Borkmann } 2546b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds /* Both of above in one bottle. */ 25491da177e4SLinus Torvalds 255081d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 255181d77662SAl Viro u8 *to, int len, __wsum csum) 25521da177e4SLinus Torvalds { 25531a028e50SDavid S. Miller int start = skb_headlen(skb); 25541a028e50SDavid S. Miller int i, copy = start - offset; 2555fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 25561da177e4SLinus Torvalds int pos = 0; 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds /* Copy header. */ 25591da177e4SLinus Torvalds if (copy > 0) { 25601da177e4SLinus Torvalds if (copy > len) 25611da177e4SLinus Torvalds copy = len; 25621da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 25631da177e4SLinus Torvalds copy, csum); 25641da177e4SLinus Torvalds if ((len -= copy) == 0) 25651da177e4SLinus Torvalds return csum; 25661da177e4SLinus Torvalds offset += copy; 25671da177e4SLinus Torvalds to += copy; 25681da177e4SLinus Torvalds pos = copy; 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 25721a028e50SDavid S. Miller int end; 25731da177e4SLinus Torvalds 2574547b792cSIlpo Järvinen WARN_ON(start > offset + len); 25751a028e50SDavid S. Miller 25769e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 25771da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 2578c613c209SWillem de Bruijn skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 2579c613c209SWillem de Bruijn u32 p_off, p_len, copied; 2580c613c209SWillem de Bruijn struct page *p; 25815084205fSAl Viro __wsum csum2; 25821da177e4SLinus Torvalds u8 *vaddr; 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds if (copy > len) 25851da177e4SLinus Torvalds copy = len; 2586c613c209SWillem de Bruijn 2587c613c209SWillem de Bruijn skb_frag_foreach_page(frag, 2588c613c209SWillem de Bruijn frag->page_offset + offset - start, 2589c613c209SWillem de Bruijn copy, p, p_off, p_len, copied) { 2590c613c209SWillem de Bruijn vaddr = kmap_atomic(p); 2591c613c209SWillem de Bruijn csum2 = csum_partial_copy_nocheck(vaddr + p_off, 2592c613c209SWillem de Bruijn to + copied, 2593c613c209SWillem de Bruijn p_len, 0); 259451c56b00SEric Dumazet kunmap_atomic(vaddr); 25951da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 2596c613c209SWillem de Bruijn pos += p_len; 2597c613c209SWillem de Bruijn } 2598c613c209SWillem de Bruijn 25991da177e4SLinus Torvalds if (!(len -= copy)) 26001da177e4SLinus Torvalds return csum; 26011da177e4SLinus Torvalds offset += copy; 26021da177e4SLinus Torvalds to += copy; 26031da177e4SLinus Torvalds } 26041a028e50SDavid S. Miller start = end; 26051da177e4SLinus Torvalds } 26061da177e4SLinus Torvalds 2607fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 260881d77662SAl Viro __wsum csum2; 26091a028e50SDavid S. Miller int end; 26101da177e4SLinus Torvalds 2611547b792cSIlpo Järvinen WARN_ON(start > offset + len); 26121a028e50SDavid S. Miller 2613fbb398a8SDavid S. Miller end = start + frag_iter->len; 26141da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 26151da177e4SLinus Torvalds if (copy > len) 26161da177e4SLinus Torvalds copy = len; 2617fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 26181a028e50SDavid S. Miller offset - start, 26191da177e4SLinus Torvalds to, copy, 0); 26201da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 26211da177e4SLinus Torvalds if ((len -= copy) == 0) 26221da177e4SLinus Torvalds return csum; 26231da177e4SLinus Torvalds offset += copy; 26241da177e4SLinus Torvalds to += copy; 26251da177e4SLinus Torvalds pos += copy; 26261da177e4SLinus Torvalds } 26271a028e50SDavid S. Miller start = end; 26281da177e4SLinus Torvalds } 262909a62660SKris Katterjohn BUG_ON(len); 26301da177e4SLinus Torvalds return csum; 26311da177e4SLinus Torvalds } 2632b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 26331da177e4SLinus Torvalds 26349617813dSDavide Caratti static __wsum warn_crc32c_csum_update(const void *buff, int len, __wsum sum) 26359617813dSDavide Caratti { 26369617813dSDavide Caratti net_warn_ratelimited( 26379617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 26389617813dSDavide Caratti __func__); 26399617813dSDavide Caratti return 0; 26409617813dSDavide Caratti } 26419617813dSDavide Caratti 26429617813dSDavide Caratti static __wsum warn_crc32c_csum_combine(__wsum csum, __wsum csum2, 26439617813dSDavide Caratti int offset, int len) 26449617813dSDavide Caratti { 26459617813dSDavide Caratti net_warn_ratelimited( 26469617813dSDavide Caratti "%s: attempt to compute crc32c without libcrc32c.ko\n", 26479617813dSDavide Caratti __func__); 26489617813dSDavide Caratti return 0; 26499617813dSDavide Caratti } 26509617813dSDavide Caratti 26519617813dSDavide Caratti static const struct skb_checksum_ops default_crc32c_ops = { 26529617813dSDavide Caratti .update = warn_crc32c_csum_update, 26539617813dSDavide Caratti .combine = warn_crc32c_csum_combine, 26549617813dSDavide Caratti }; 26559617813dSDavide Caratti 26569617813dSDavide Caratti const struct skb_checksum_ops *crc32c_csum_stub __read_mostly = 26579617813dSDavide Caratti &default_crc32c_ops; 26589617813dSDavide Caratti EXPORT_SYMBOL(crc32c_csum_stub); 26599617813dSDavide Caratti 2660af2806f8SThomas Graf /** 2661af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2662af2806f8SThomas Graf * @from: source buffer 2663af2806f8SThomas Graf * 2664af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2665af2806f8SThomas Graf * into skb_zerocopy(). 2666af2806f8SThomas Graf */ 2667af2806f8SThomas Graf unsigned int 2668af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2669af2806f8SThomas Graf { 2670af2806f8SThomas Graf unsigned int hlen = 0; 2671af2806f8SThomas Graf 2672af2806f8SThomas Graf if (!from->head_frag || 2673af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2674af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2675af2806f8SThomas Graf hlen = skb_headlen(from); 2676af2806f8SThomas Graf 2677af2806f8SThomas Graf if (skb_has_frag_list(from)) 2678af2806f8SThomas Graf hlen = from->len; 2679af2806f8SThomas Graf 2680af2806f8SThomas Graf return hlen; 2681af2806f8SThomas Graf } 2682af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2683af2806f8SThomas Graf 2684af2806f8SThomas Graf /** 2685af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2686af2806f8SThomas Graf * @to: destination buffer 26877fceb4deSMasanari Iida * @from: source buffer 2688af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2689af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2690af2806f8SThomas Graf * 2691af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2692af2806f8SThomas Graf * to the frags in the source buffer. 2693af2806f8SThomas Graf * 2694af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2695af2806f8SThomas Graf * headroom in the `to` buffer. 269636d5fe6aSZoltan Kiss * 269736d5fe6aSZoltan Kiss * Return value: 269836d5fe6aSZoltan Kiss * 0: everything is OK 269936d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 270036d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2701af2806f8SThomas Graf */ 270236d5fe6aSZoltan Kiss int 270336d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2704af2806f8SThomas Graf { 2705af2806f8SThomas Graf int i, j = 0; 2706af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 270736d5fe6aSZoltan Kiss int ret; 2708af2806f8SThomas Graf struct page *page; 2709af2806f8SThomas Graf unsigned int offset; 2710af2806f8SThomas Graf 2711af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2712af2806f8SThomas Graf 2713af2806f8SThomas Graf /* dont bother with small payloads */ 271436d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 271536d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2716af2806f8SThomas Graf 2717af2806f8SThomas Graf if (hlen) { 271836d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 271936d5fe6aSZoltan Kiss if (unlikely(ret)) 272036d5fe6aSZoltan Kiss return ret; 2721af2806f8SThomas Graf len -= hlen; 2722af2806f8SThomas Graf } else { 2723af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2724af2806f8SThomas Graf if (plen) { 2725af2806f8SThomas Graf page = virt_to_head_page(from->head); 2726af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2727af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2728af2806f8SThomas Graf get_page(page); 2729af2806f8SThomas Graf j = 1; 2730af2806f8SThomas Graf len -= plen; 2731af2806f8SThomas Graf } 2732af2806f8SThomas Graf } 2733af2806f8SThomas Graf 2734af2806f8SThomas Graf to->truesize += len + plen; 2735af2806f8SThomas Graf to->len += len + plen; 2736af2806f8SThomas Graf to->data_len += len + plen; 2737af2806f8SThomas Graf 273836d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 273936d5fe6aSZoltan Kiss skb_tx_error(from); 274036d5fe6aSZoltan Kiss return -ENOMEM; 274136d5fe6aSZoltan Kiss } 27421f8b977aSWillem de Bruijn skb_zerocopy_clone(to, from, GFP_ATOMIC); 274336d5fe6aSZoltan Kiss 2744af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2745af2806f8SThomas Graf if (!len) 2746af2806f8SThomas Graf break; 2747af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2748af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2749af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2750af2806f8SThomas Graf skb_frag_ref(to, j); 2751af2806f8SThomas Graf j++; 2752af2806f8SThomas Graf } 2753af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 275436d5fe6aSZoltan Kiss 275536d5fe6aSZoltan Kiss return 0; 2756af2806f8SThomas Graf } 2757af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2758af2806f8SThomas Graf 27591da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 27601da177e4SLinus Torvalds { 2761d3bc23e7SAl Viro __wsum csum; 27621da177e4SLinus Torvalds long csstart; 27631da177e4SLinus Torvalds 276484fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 276555508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 27661da177e4SLinus Torvalds else 27671da177e4SLinus Torvalds csstart = skb_headlen(skb); 27681da177e4SLinus Torvalds 276909a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 27701da177e4SLinus Torvalds 2771d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds csum = 0; 27741da177e4SLinus Torvalds if (csstart != skb->len) 27751da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 27761da177e4SLinus Torvalds skb->len - csstart, 0); 27771da177e4SLinus Torvalds 277884fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2779ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 27801da177e4SLinus Torvalds 2781d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 27821da177e4SLinus Torvalds } 27831da177e4SLinus Torvalds } 2784b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds /** 27871da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 27881da177e4SLinus Torvalds * @list: list to dequeue from 27891da177e4SLinus Torvalds * 27901da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 27911da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 27921da177e4SLinus Torvalds * returned or %NULL if the list is empty. 27931da177e4SLinus Torvalds */ 27941da177e4SLinus Torvalds 27951da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 27961da177e4SLinus Torvalds { 27971da177e4SLinus Torvalds unsigned long flags; 27981da177e4SLinus Torvalds struct sk_buff *result; 27991da177e4SLinus Torvalds 28001da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28011da177e4SLinus Torvalds result = __skb_dequeue(list); 28021da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 28031da177e4SLinus Torvalds return result; 28041da177e4SLinus Torvalds } 2805b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds /** 28081da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 28091da177e4SLinus Torvalds * @list: list to dequeue from 28101da177e4SLinus Torvalds * 28111da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 28121da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 28131da177e4SLinus Torvalds * returned or %NULL if the list is empty. 28141da177e4SLinus Torvalds */ 28151da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 28161da177e4SLinus Torvalds { 28171da177e4SLinus Torvalds unsigned long flags; 28181da177e4SLinus Torvalds struct sk_buff *result; 28191da177e4SLinus Torvalds 28201da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28211da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 28221da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 28231da177e4SLinus Torvalds return result; 28241da177e4SLinus Torvalds } 2825b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds /** 28281da177e4SLinus Torvalds * skb_queue_purge - empty a list 28291da177e4SLinus Torvalds * @list: list to empty 28301da177e4SLinus Torvalds * 28311da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 28321da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 28331da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 28341da177e4SLinus Torvalds */ 28351da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 28361da177e4SLinus Torvalds { 28371da177e4SLinus Torvalds struct sk_buff *skb; 28381da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 28391da177e4SLinus Torvalds kfree_skb(skb); 28401da177e4SLinus Torvalds } 2841b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds /** 28449f5afeaeSYaogong Wang * skb_rbtree_purge - empty a skb rbtree 28459f5afeaeSYaogong Wang * @root: root of the rbtree to empty 28469f5afeaeSYaogong Wang * 28479f5afeaeSYaogong Wang * Delete all buffers on an &sk_buff rbtree. Each buffer is removed from 28489f5afeaeSYaogong Wang * the list and one reference dropped. This function does not take 28499f5afeaeSYaogong Wang * any lock. Synchronization should be handled by the caller (e.g., TCP 28509f5afeaeSYaogong Wang * out-of-order queue is protected by the socket lock). 28519f5afeaeSYaogong Wang */ 28529f5afeaeSYaogong Wang void skb_rbtree_purge(struct rb_root *root) 28539f5afeaeSYaogong Wang { 28549f5afeaeSYaogong Wang struct sk_buff *skb, *next; 28559f5afeaeSYaogong Wang 28569f5afeaeSYaogong Wang rbtree_postorder_for_each_entry_safe(skb, next, root, rbnode) 28579f5afeaeSYaogong Wang kfree_skb(skb); 28589f5afeaeSYaogong Wang 28599f5afeaeSYaogong Wang *root = RB_ROOT; 28609f5afeaeSYaogong Wang } 28619f5afeaeSYaogong Wang 28629f5afeaeSYaogong Wang /** 28631da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 28641da177e4SLinus Torvalds * @list: list to use 28651da177e4SLinus Torvalds * @newsk: buffer to queue 28661da177e4SLinus Torvalds * 28671da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 28681da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 28691da177e4SLinus Torvalds * safely. 28701da177e4SLinus Torvalds * 28711da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 28721da177e4SLinus Torvalds */ 28731da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 28741da177e4SLinus Torvalds { 28751da177e4SLinus Torvalds unsigned long flags; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28781da177e4SLinus Torvalds __skb_queue_head(list, newsk); 28791da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 28801da177e4SLinus Torvalds } 2881b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 28821da177e4SLinus Torvalds 28831da177e4SLinus Torvalds /** 28841da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 28851da177e4SLinus Torvalds * @list: list to use 28861da177e4SLinus Torvalds * @newsk: buffer to queue 28871da177e4SLinus Torvalds * 28881da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 28891da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 28901da177e4SLinus Torvalds * safely. 28911da177e4SLinus Torvalds * 28921da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 28931da177e4SLinus Torvalds */ 28941da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 28951da177e4SLinus Torvalds { 28961da177e4SLinus Torvalds unsigned long flags; 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 28991da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 29001da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29011da177e4SLinus Torvalds } 2902b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 29038728b834SDavid S. Miller 29041da177e4SLinus Torvalds /** 29051da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 29061da177e4SLinus Torvalds * @skb: buffer to remove 29078728b834SDavid S. Miller * @list: list to use 29081da177e4SLinus Torvalds * 29098728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 29108728b834SDavid S. Miller * function is atomic with respect to other list locked calls 29111da177e4SLinus Torvalds * 29128728b834SDavid S. Miller * You must know what list the SKB is on. 29131da177e4SLinus Torvalds */ 29148728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 29151da177e4SLinus Torvalds { 29161da177e4SLinus Torvalds unsigned long flags; 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 29198728b834SDavid S. Miller __skb_unlink(skb, list); 29201da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 29211da177e4SLinus Torvalds } 2922b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 29231da177e4SLinus Torvalds 29241da177e4SLinus Torvalds /** 29251da177e4SLinus Torvalds * skb_append - append a buffer 29261da177e4SLinus Torvalds * @old: buffer to insert after 29271da177e4SLinus Torvalds * @newsk: buffer to insert 29288728b834SDavid S. Miller * @list: list to use 29291da177e4SLinus Torvalds * 29301da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 29311da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 29321da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 29331da177e4SLinus Torvalds */ 29348728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 29351da177e4SLinus Torvalds { 29361da177e4SLinus Torvalds unsigned long flags; 29371da177e4SLinus Torvalds 29388728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 29397de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 29408728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 29411da177e4SLinus Torvalds } 2942b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 29431da177e4SLinus Torvalds 29441da177e4SLinus Torvalds /** 29451da177e4SLinus Torvalds * skb_insert - insert a buffer 29461da177e4SLinus Torvalds * @old: buffer to insert before 29471da177e4SLinus Torvalds * @newsk: buffer to insert 29488728b834SDavid S. Miller * @list: list to use 29491da177e4SLinus Torvalds * 29508728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 29518728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 29528728b834SDavid S. Miller * calls. 29538728b834SDavid S. Miller * 29541da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 29551da177e4SLinus Torvalds */ 29568728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 29571da177e4SLinus Torvalds { 29581da177e4SLinus Torvalds unsigned long flags; 29591da177e4SLinus Torvalds 29608728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 29618728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 29628728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 29631da177e4SLinus Torvalds } 2964b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 29671da177e4SLinus Torvalds struct sk_buff* skb1, 29681da177e4SLinus Torvalds const u32 len, const int pos) 29691da177e4SLinus Torvalds { 29701da177e4SLinus Torvalds int i; 29711da177e4SLinus Torvalds 2972d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2973d626f62bSArnaldo Carvalho de Melo pos - len); 29741da177e4SLinus Torvalds /* And move data appendix as is. */ 29751da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 29761da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 29791da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 29801da177e4SLinus Torvalds skb1->data_len = skb->data_len; 29811da177e4SLinus Torvalds skb1->len += skb1->data_len; 29821da177e4SLinus Torvalds skb->data_len = 0; 29831da177e4SLinus Torvalds skb->len = len; 298427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 29851da177e4SLinus Torvalds } 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 29881da177e4SLinus Torvalds struct sk_buff* skb1, 29891da177e4SLinus Torvalds const u32 len, int pos) 29901da177e4SLinus Torvalds { 29911da177e4SLinus Torvalds int i, k = 0; 29921da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 29951da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 29961da177e4SLinus Torvalds skb->len = len; 29971da177e4SLinus Torvalds skb->data_len = len - pos; 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 30009e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds if (pos + size > len) { 30031da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 30041da177e4SLinus Torvalds 30051da177e4SLinus Torvalds if (pos < len) { 30061da177e4SLinus Torvalds /* Split frag. 30071da177e4SLinus Torvalds * We have two variants in this case: 30081da177e4SLinus Torvalds * 1. Move all the frag to the second 30091da177e4SLinus Torvalds * part, if it is possible. F.e. 30101da177e4SLinus Torvalds * this approach is mandatory for TUX, 30111da177e4SLinus Torvalds * where splitting is expensive. 30121da177e4SLinus Torvalds * 2. Split is accurately. We make this. 30131da177e4SLinus Torvalds */ 3014ea2ab693SIan Campbell skb_frag_ref(skb, i); 30151da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 30169e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 30179e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 30181da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds k++; 30211da177e4SLinus Torvalds } else 30221da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 30231da177e4SLinus Torvalds pos += size; 30241da177e4SLinus Torvalds } 30251da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 30261da177e4SLinus Torvalds } 30271da177e4SLinus Torvalds 30281da177e4SLinus Torvalds /** 30291da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 30301da177e4SLinus Torvalds * @skb: the buffer to split 30311da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 30321da177e4SLinus Torvalds * @len: new length for skb 30331da177e4SLinus Torvalds */ 30341da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 30351da177e4SLinus Torvalds { 30361da177e4SLinus Torvalds int pos = skb_headlen(skb); 30371da177e4SLinus Torvalds 3038fff88030SWillem de Bruijn skb_shinfo(skb1)->tx_flags |= skb_shinfo(skb)->tx_flags & 3039fff88030SWillem de Bruijn SKBTX_SHARED_FRAG; 30401f8b977aSWillem de Bruijn skb_zerocopy_clone(skb1, skb, 0); 30411da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 30421da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 30431da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 30441da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 30451da177e4SLinus Torvalds } 3046b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 30471da177e4SLinus Torvalds 30489f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 30499f782db3SIlpo Järvinen * 30509f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 30519f782db3SIlpo Järvinen */ 3052832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 3053832d11c5SIlpo Järvinen { 30540ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 3055832d11c5SIlpo Järvinen } 3056832d11c5SIlpo Järvinen 3057832d11c5SIlpo Järvinen /** 3058832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 3059832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 3060832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 3061832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 3062832d11c5SIlpo Järvinen * 3063832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 306420e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 3065832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 3066832d11c5SIlpo Järvinen * 3067832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 3068832d11c5SIlpo Järvinen * 3069832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 3070832d11c5SIlpo Järvinen * to have non-paged data as well. 3071832d11c5SIlpo Järvinen * 3072832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 3073832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 3074832d11c5SIlpo Järvinen */ 3075832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 3076832d11c5SIlpo Järvinen { 3077832d11c5SIlpo Järvinen int from, to, merge, todo; 3078832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 3079832d11c5SIlpo Järvinen 3080832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 3081f8071cdeSEric Dumazet 3082f8071cdeSEric Dumazet if (skb_headlen(skb)) 3083f8071cdeSEric Dumazet return 0; 30841f8b977aSWillem de Bruijn if (skb_zcopy(tgt) || skb_zcopy(skb)) 30851f8b977aSWillem de Bruijn return 0; 3086832d11c5SIlpo Järvinen 3087832d11c5SIlpo Järvinen todo = shiftlen; 3088832d11c5SIlpo Järvinen from = 0; 3089832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 3090832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3091832d11c5SIlpo Järvinen 3092832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 3093832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 3094832d11c5SIlpo Järvinen */ 3095832d11c5SIlpo Järvinen if (!to || 3096ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 3097ea2ab693SIan Campbell fragfrom->page_offset)) { 3098832d11c5SIlpo Järvinen merge = -1; 3099832d11c5SIlpo Järvinen } else { 3100832d11c5SIlpo Järvinen merge = to - 1; 3101832d11c5SIlpo Järvinen 31029e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3103832d11c5SIlpo Järvinen if (todo < 0) { 3104832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 3105832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 3106832d11c5SIlpo Järvinen return 0; 3107832d11c5SIlpo Järvinen 31089f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 31099f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3110832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3111832d11c5SIlpo Järvinen 31129e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 31139e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 3114832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 3115832d11c5SIlpo Järvinen 3116832d11c5SIlpo Järvinen goto onlymerged; 3117832d11c5SIlpo Järvinen } 3118832d11c5SIlpo Järvinen 3119832d11c5SIlpo Järvinen from++; 3120832d11c5SIlpo Järvinen } 3121832d11c5SIlpo Järvinen 3122832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 3123832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 3124832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 3125832d11c5SIlpo Järvinen return 0; 3126832d11c5SIlpo Järvinen 3127832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 3128832d11c5SIlpo Järvinen return 0; 3129832d11c5SIlpo Järvinen 3130832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 3131832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 3132832d11c5SIlpo Järvinen return 0; 3133832d11c5SIlpo Järvinen 3134832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 3135832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 3136832d11c5SIlpo Järvinen 31379e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 3138832d11c5SIlpo Järvinen *fragto = *fragfrom; 31399e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 3140832d11c5SIlpo Järvinen from++; 3141832d11c5SIlpo Järvinen to++; 3142832d11c5SIlpo Järvinen 3143832d11c5SIlpo Järvinen } else { 3144ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 3145832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 3146832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 31479e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 3148832d11c5SIlpo Järvinen 3149832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 31509e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 3151832d11c5SIlpo Järvinen todo = 0; 3152832d11c5SIlpo Järvinen 3153832d11c5SIlpo Järvinen to++; 3154832d11c5SIlpo Järvinen break; 3155832d11c5SIlpo Järvinen } 3156832d11c5SIlpo Järvinen } 3157832d11c5SIlpo Järvinen 3158832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 3159832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 3160832d11c5SIlpo Järvinen 3161832d11c5SIlpo Järvinen if (merge >= 0) { 3162832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 3163832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 3164832d11c5SIlpo Järvinen 31659e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 3166ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 3167832d11c5SIlpo Järvinen } 3168832d11c5SIlpo Järvinen 3169832d11c5SIlpo Järvinen /* Reposition in the original skb */ 3170832d11c5SIlpo Järvinen to = 0; 3171832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 3172832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 3173832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 3174832d11c5SIlpo Järvinen 3175832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 3176832d11c5SIlpo Järvinen 3177832d11c5SIlpo Järvinen onlymerged: 3178832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 3179832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 3180832d11c5SIlpo Järvinen */ 3181832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 3182832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 3183832d11c5SIlpo Järvinen 3184832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 3185832d11c5SIlpo Järvinen skb->len -= shiftlen; 3186832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 3187832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 3188832d11c5SIlpo Järvinen tgt->len += shiftlen; 3189832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 3190832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 3191832d11c5SIlpo Järvinen 3192832d11c5SIlpo Järvinen return shiftlen; 3193832d11c5SIlpo Järvinen } 3194832d11c5SIlpo Järvinen 3195677e90edSThomas Graf /** 3196677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 3197677e90edSThomas Graf * @skb: the buffer to read 3198677e90edSThomas Graf * @from: lower offset of data to be read 3199677e90edSThomas Graf * @to: upper offset of data to be read 3200677e90edSThomas Graf * @st: state variable 3201677e90edSThomas Graf * 3202677e90edSThomas Graf * Initializes the specified state variable. Must be called before 3203677e90edSThomas Graf * invoking skb_seq_read() for the first time. 3204677e90edSThomas Graf */ 3205677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 3206677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 3207677e90edSThomas Graf { 3208677e90edSThomas Graf st->lower_offset = from; 3209677e90edSThomas Graf st->upper_offset = to; 3210677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 3211677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 3212677e90edSThomas Graf st->frag_data = NULL; 3213677e90edSThomas Graf } 3214b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 3215677e90edSThomas Graf 3216677e90edSThomas Graf /** 3217677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 3218677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 3219677e90edSThomas Graf * @data: destination pointer for data to be returned 3220677e90edSThomas Graf * @st: state variable 3221677e90edSThomas Graf * 3222bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 3223677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 3224bc32383cSMathias Krause * the head of the data block to @data and returns the length 3225677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 3226677e90edSThomas Graf * offset has been reached. 3227677e90edSThomas Graf * 3228677e90edSThomas Graf * The caller is not required to consume all of the data 3229bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 3230677e90edSThomas Graf * of bytes already consumed and the next call to 3231677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 3232677e90edSThomas Graf * 323325985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 3234e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 3235677e90edSThomas Graf * reads of potentially non linear data. 3236677e90edSThomas Graf * 3237bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 3238677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 3239677e90edSThomas Graf * a stack for this purpose. 3240677e90edSThomas Graf */ 3241677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 3242677e90edSThomas Graf struct skb_seq_state *st) 3243677e90edSThomas Graf { 3244677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 3245677e90edSThomas Graf skb_frag_t *frag; 3246677e90edSThomas Graf 3247aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 3248aeb193eaSWedson Almeida Filho if (st->frag_data) { 3249aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 3250aeb193eaSWedson Almeida Filho st->frag_data = NULL; 3251aeb193eaSWedson Almeida Filho } 3252677e90edSThomas Graf return 0; 3253aeb193eaSWedson Almeida Filho } 3254677e90edSThomas Graf 3255677e90edSThomas Graf next_skb: 325695e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 3257677e90edSThomas Graf 3258995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 325995e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 3260677e90edSThomas Graf return block_limit - abs_offset; 3261677e90edSThomas Graf } 3262677e90edSThomas Graf 3263677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 3264677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 3265677e90edSThomas Graf 3266677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 3267677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 32689e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 3269677e90edSThomas Graf 3270677e90edSThomas Graf if (abs_offset < block_limit) { 3271677e90edSThomas Graf if (!st->frag_data) 327251c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 3273677e90edSThomas Graf 3274677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 3275677e90edSThomas Graf (abs_offset - st->stepped_offset); 3276677e90edSThomas Graf 3277677e90edSThomas Graf return block_limit - abs_offset; 3278677e90edSThomas Graf } 3279677e90edSThomas Graf 3280677e90edSThomas Graf if (st->frag_data) { 328151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3282677e90edSThomas Graf st->frag_data = NULL; 3283677e90edSThomas Graf } 3284677e90edSThomas Graf 3285677e90edSThomas Graf st->frag_idx++; 32869e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 3287677e90edSThomas Graf } 3288677e90edSThomas Graf 32895b5a60daSOlaf Kirch if (st->frag_data) { 329051c56b00SEric Dumazet kunmap_atomic(st->frag_data); 32915b5a60daSOlaf Kirch st->frag_data = NULL; 32925b5a60daSOlaf Kirch } 32935b5a60daSOlaf Kirch 329421dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 3295677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 329695e3b24cSHerbert Xu st->frag_idx = 0; 3297677e90edSThomas Graf goto next_skb; 329871b3346dSShyam Iyer } else if (st->cur_skb->next) { 329971b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 330071b3346dSShyam Iyer st->frag_idx = 0; 3301677e90edSThomas Graf goto next_skb; 3302677e90edSThomas Graf } 3303677e90edSThomas Graf 3304677e90edSThomas Graf return 0; 3305677e90edSThomas Graf } 3306b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 3307677e90edSThomas Graf 3308677e90edSThomas Graf /** 3309677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 3310677e90edSThomas Graf * @st: state variable 3311677e90edSThomas Graf * 3312677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 3313677e90edSThomas Graf * returned 0. 3314677e90edSThomas Graf */ 3315677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 3316677e90edSThomas Graf { 3317677e90edSThomas Graf if (st->frag_data) 331851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 3319677e90edSThomas Graf } 3320b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 3321677e90edSThomas Graf 33223fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 33233fc7e8a6SThomas Graf 33243fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 33253fc7e8a6SThomas Graf struct ts_config *conf, 33263fc7e8a6SThomas Graf struct ts_state *state) 33273fc7e8a6SThomas Graf { 33283fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 33293fc7e8a6SThomas Graf } 33303fc7e8a6SThomas Graf 33313fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 33323fc7e8a6SThomas Graf { 33333fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 33343fc7e8a6SThomas Graf } 33353fc7e8a6SThomas Graf 33363fc7e8a6SThomas Graf /** 33373fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 33383fc7e8a6SThomas Graf * @skb: the buffer to look in 33393fc7e8a6SThomas Graf * @from: search offset 33403fc7e8a6SThomas Graf * @to: search limit 33413fc7e8a6SThomas Graf * @config: textsearch configuration 33423fc7e8a6SThomas Graf * 33433fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 33443fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 33453fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 33463fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 33473fc7e8a6SThomas Graf */ 33483fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 3349059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 33503fc7e8a6SThomas Graf { 3351059a2440SBojan Prtvar struct ts_state state; 3352f72b948dSPhil Oester unsigned int ret; 3353f72b948dSPhil Oester 33543fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 33553fc7e8a6SThomas Graf config->finish = skb_ts_finish; 33563fc7e8a6SThomas Graf 3357059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 33583fc7e8a6SThomas Graf 3359059a2440SBojan Prtvar ret = textsearch_find(config, &state); 3360f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 33613fc7e8a6SThomas Graf } 3362b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 33633fc7e8a6SThomas Graf 3364e89e9cf5SAnanda Raju /** 33652c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 3366e89e9cf5SAnanda Raju * @sk: sock structure 3367e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 3368e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 3369e89e9cf5SAnanda Raju * @from: pointer to user message iov 3370e89e9cf5SAnanda Raju * @length: length of the iov message 3371e89e9cf5SAnanda Raju * 3372e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 3373e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 3374e89e9cf5SAnanda Raju */ 3375e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 3376dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 3377e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 3378e89e9cf5SAnanda Raju void *from, int length) 3379e89e9cf5SAnanda Raju { 3380b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 3381b2111724SEric Dumazet int copy; 3382e89e9cf5SAnanda Raju int offset = 0; 3383e89e9cf5SAnanda Raju int ret; 3384b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 3385e89e9cf5SAnanda Raju 3386e89e9cf5SAnanda Raju do { 3387e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 3388e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 3389b2111724SEric Dumazet return -EMSGSIZE; 3390e89e9cf5SAnanda Raju 3391b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 3392e89e9cf5SAnanda Raju return -ENOMEM; 3393e89e9cf5SAnanda Raju 3394e89e9cf5SAnanda Raju /* copy the user data to page */ 3395b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 3396e89e9cf5SAnanda Raju 3397b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 3398e89e9cf5SAnanda Raju offset, copy, 0, skb); 3399e89e9cf5SAnanda Raju if (ret < 0) 3400e89e9cf5SAnanda Raju return -EFAULT; 3401e89e9cf5SAnanda Raju 3402e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 3403b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 3404b2111724SEric Dumazet copy); 3405b2111724SEric Dumazet frg_cnt++; 3406b2111724SEric Dumazet pfrag->offset += copy; 3407b2111724SEric Dumazet get_page(pfrag->page); 3408b2111724SEric Dumazet 3409b2111724SEric Dumazet skb->truesize += copy; 341014afee4bSReshetova, Elena refcount_add(copy, &sk->sk_wmem_alloc); 3411e89e9cf5SAnanda Raju skb->len += copy; 3412e89e9cf5SAnanda Raju skb->data_len += copy; 3413e89e9cf5SAnanda Raju offset += copy; 3414e89e9cf5SAnanda Raju length -= copy; 3415e89e9cf5SAnanda Raju 3416e89e9cf5SAnanda Raju } while (length > 0); 3417e89e9cf5SAnanda Raju 3418e89e9cf5SAnanda Raju return 0; 3419e89e9cf5SAnanda Raju } 3420b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 3421e89e9cf5SAnanda Raju 3422be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3423be12a1feSHannes Frederic Sowa int offset, size_t size) 3424be12a1feSHannes Frederic Sowa { 3425be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3426be12a1feSHannes Frederic Sowa 3427be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3428be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3429be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3430be12a1feSHannes Frederic Sowa get_page(page); 3431be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3432be12a1feSHannes Frederic Sowa } else { 3433be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3434be12a1feSHannes Frederic Sowa } 3435be12a1feSHannes Frederic Sowa 3436be12a1feSHannes Frederic Sowa return 0; 3437be12a1feSHannes Frederic Sowa } 3438be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3439be12a1feSHannes Frederic Sowa 3440cbb042f9SHerbert Xu /** 3441cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3442cbb042f9SHerbert Xu * @skb: buffer to update 3443cbb042f9SHerbert Xu * @len: length of data pulled 3444cbb042f9SHerbert Xu * 3445cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3446fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 344784fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 344884fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 344984fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3450cbb042f9SHerbert Xu */ 3451af72868bSJohannes Berg void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3452cbb042f9SHerbert Xu { 345331b33dfbSPravin B Shelar unsigned char *data = skb->data; 345431b33dfbSPravin B Shelar 3455cbb042f9SHerbert Xu BUG_ON(len > skb->len); 345631b33dfbSPravin B Shelar __skb_pull(skb, len); 345731b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 345831b33dfbSPravin B Shelar return skb->data; 3459cbb042f9SHerbert Xu } 3460f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3461f94691acSArnaldo Carvalho de Melo 3462f4c50d99SHerbert Xu /** 3463f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3464df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3465576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3466f4c50d99SHerbert Xu * 3467f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 34684c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 34694c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3470f4c50d99SHerbert Xu */ 3471df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3472df5771ffSMichael S. Tsirkin netdev_features_t features) 3473f4c50d99SHerbert Xu { 3474f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3475f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 34761a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3477df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3478df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3479df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 34801fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3481f4c50d99SHerbert Xu unsigned int offset = doffset; 3482df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3483802ab55aSAlexander Duyck unsigned int partial_segs = 0; 3484f4c50d99SHerbert Xu unsigned int headroom; 3485802ab55aSAlexander Duyck unsigned int len = head_skb->len; 3486ec5f0615SPravin B Shelar __be16 proto; 348736c98382SAlexander Duyck bool csum, sg; 3488df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3489f4c50d99SHerbert Xu int err = -ENOMEM; 3490f4c50d99SHerbert Xu int i = 0; 3491f4c50d99SHerbert Xu int pos; 349253d6471cSVlad Yasevich int dummy; 3493f4c50d99SHerbert Xu 34945882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 349553d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3496ec5f0615SPravin B Shelar if (unlikely(!proto)) 3497ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3498ec5f0615SPravin B Shelar 349936c98382SAlexander Duyck sg = !!(features & NETIF_F_SG); 3500f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 35017e2b10c1STom Herbert 350207b26c94SSteffen Klassert if (sg && csum && (mss != GSO_BY_FRAGS)) { 350307b26c94SSteffen Klassert if (!(features & NETIF_F_GSO_PARTIAL)) { 350407b26c94SSteffen Klassert struct sk_buff *iter; 350543170c4eSIlan Tayari unsigned int frag_len; 350607b26c94SSteffen Klassert 350707b26c94SSteffen Klassert if (!list_skb || 350807b26c94SSteffen Klassert !net_gso_ok(features, skb_shinfo(head_skb)->gso_type)) 350907b26c94SSteffen Klassert goto normal; 351007b26c94SSteffen Klassert 351143170c4eSIlan Tayari /* If we get here then all the required 351243170c4eSIlan Tayari * GSO features except frag_list are supported. 351343170c4eSIlan Tayari * Try to split the SKB to multiple GSO SKBs 351443170c4eSIlan Tayari * with no frag_list. 351543170c4eSIlan Tayari * Currently we can do that only when the buffers don't 351643170c4eSIlan Tayari * have a linear part and all the buffers except 351743170c4eSIlan Tayari * the last are of the same length. 351807b26c94SSteffen Klassert */ 351943170c4eSIlan Tayari frag_len = list_skb->len; 352007b26c94SSteffen Klassert skb_walk_frags(head_skb, iter) { 352143170c4eSIlan Tayari if (frag_len != iter->len && iter->next) 352243170c4eSIlan Tayari goto normal; 3523eaffadbbSIlan Tayari if (skb_headlen(iter) && !iter->head_frag) 352407b26c94SSteffen Klassert goto normal; 352507b26c94SSteffen Klassert 352607b26c94SSteffen Klassert len -= iter->len; 352707b26c94SSteffen Klassert } 352843170c4eSIlan Tayari 352943170c4eSIlan Tayari if (len != frag_len) 353043170c4eSIlan Tayari goto normal; 353107b26c94SSteffen Klassert } 353207b26c94SSteffen Klassert 3533802ab55aSAlexander Duyck /* GSO partial only requires that we trim off any excess that 3534802ab55aSAlexander Duyck * doesn't fit into an MSS sized block, so take care of that 3535802ab55aSAlexander Duyck * now. 3536802ab55aSAlexander Duyck */ 3537802ab55aSAlexander Duyck partial_segs = len / mss; 3538d7fb5a80SAlexander Duyck if (partial_segs > 1) 3539802ab55aSAlexander Duyck mss *= partial_segs; 3540d7fb5a80SAlexander Duyck else 3541d7fb5a80SAlexander Duyck partial_segs = 0; 3542802ab55aSAlexander Duyck } 3543802ab55aSAlexander Duyck 354407b26c94SSteffen Klassert normal: 3545df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3546df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3547f4c50d99SHerbert Xu 3548f4c50d99SHerbert Xu do { 3549f4c50d99SHerbert Xu struct sk_buff *nskb; 35508cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3551c8884eddSHerbert Xu int hsize; 3552f4c50d99SHerbert Xu int size; 3553f4c50d99SHerbert Xu 35543953c46cSMarcelo Ricardo Leitner if (unlikely(mss == GSO_BY_FRAGS)) { 35553953c46cSMarcelo Ricardo Leitner len = list_skb->len; 35563953c46cSMarcelo Ricardo Leitner } else { 3557df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3558f4c50d99SHerbert Xu if (len > mss) 3559f4c50d99SHerbert Xu len = mss; 35603953c46cSMarcelo Ricardo Leitner } 3561f4c50d99SHerbert Xu 3562df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3563f4c50d99SHerbert Xu if (hsize < 0) 3564f4c50d99SHerbert Xu hsize = 0; 3565c8884eddSHerbert Xu if (hsize > len || !sg) 3566c8884eddSHerbert Xu hsize = len; 3567f4c50d99SHerbert Xu 35681a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 35691a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 35701a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 357189319d38SHerbert Xu 35729d8506ccSHerbert Xu i = 0; 35731a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 35741a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 35751fd819ecSMichael S. Tsirkin frag_skb = list_skb; 35761a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 35779d8506ccSHerbert Xu 35789d8506ccSHerbert Xu while (pos < offset + len) { 35799d8506ccSHerbert Xu BUG_ON(i >= nfrags); 35809d8506ccSHerbert Xu 35814e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 35829d8506ccSHerbert Xu if (pos + size > offset + len) 35839d8506ccSHerbert Xu break; 35849d8506ccSHerbert Xu 35859d8506ccSHerbert Xu i++; 35869d8506ccSHerbert Xu pos += size; 35874e1beba1SMichael S. Tsirkin frag++; 35889d8506ccSHerbert Xu } 35899d8506ccSHerbert Xu 35901a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 35911a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 359289319d38SHerbert Xu 3593f4c50d99SHerbert Xu if (unlikely(!nskb)) 3594f4c50d99SHerbert Xu goto err; 3595f4c50d99SHerbert Xu 35969d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 35979d8506ccSHerbert Xu kfree_skb(nskb); 35989d8506ccSHerbert Xu goto err; 35999d8506ccSHerbert Xu } 36009d8506ccSHerbert Xu 3601ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 360289319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 360389319d38SHerbert Xu kfree_skb(nskb); 360489319d38SHerbert Xu goto err; 360589319d38SHerbert Xu } 360689319d38SHerbert Xu 3607ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 360889319d38SHerbert Xu skb_release_head_state(nskb); 360989319d38SHerbert Xu __skb_push(nskb, doffset); 361089319d38SHerbert Xu } else { 3611c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3612df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3613c93bdd0eSMel Gorman NUMA_NO_NODE); 361489319d38SHerbert Xu 361589319d38SHerbert Xu if (unlikely(!nskb)) 361689319d38SHerbert Xu goto err; 361789319d38SHerbert Xu 361889319d38SHerbert Xu skb_reserve(nskb, headroom); 361989319d38SHerbert Xu __skb_put(nskb, doffset); 362089319d38SHerbert Xu } 362189319d38SHerbert Xu 3622f4c50d99SHerbert Xu if (segs) 3623f4c50d99SHerbert Xu tail->next = nskb; 3624f4c50d99SHerbert Xu else 3625f4c50d99SHerbert Xu segs = nskb; 3626f4c50d99SHerbert Xu tail = nskb; 3627f4c50d99SHerbert Xu 3628df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3629f4c50d99SHerbert Xu 3630030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3631fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 363268c33163SPravin B Shelar 3633df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 363468c33163SPravin B Shelar nskb->data - tnl_hlen, 363568c33163SPravin B Shelar doffset + tnl_hlen); 363689319d38SHerbert Xu 36379d8506ccSHerbert Xu if (nskb->len == len + doffset) 36381cdbcb79SSimon Horman goto perform_csum_check; 363989319d38SHerbert Xu 36407fbeffedSAlexander Duyck if (!sg) { 36417fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 36426f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 364376443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 364476443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 3645f4c50d99SHerbert Xu skb_put(nskb, len), 3646f4c50d99SHerbert Xu len, 0); 36477e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3648de843723STom Herbert skb_headroom(nskb) + doffset; 3649f4c50d99SHerbert Xu continue; 3650f4c50d99SHerbert Xu } 3651f4c50d99SHerbert Xu 36528cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3653f4c50d99SHerbert Xu 3654df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3655d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3656f4c50d99SHerbert Xu 3657fff88030SWillem de Bruijn skb_shinfo(nskb)->tx_flags |= skb_shinfo(head_skb)->tx_flags & 3658df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 36591f8b977aSWillem de Bruijn if (skb_zerocopy_clone(nskb, head_skb, GFP_ATOMIC)) 36601f8b977aSWillem de Bruijn goto err; 3661cef401deSEric Dumazet 36629d8506ccSHerbert Xu while (pos < offset + len) { 36639d8506ccSHerbert Xu if (i >= nfrags) { 36641a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 36659d8506ccSHerbert Xu 36669d8506ccSHerbert Xu i = 0; 36671a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 36681a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 36691fd819ecSMichael S. Tsirkin frag_skb = list_skb; 36709d8506ccSHerbert Xu 36719d8506ccSHerbert Xu BUG_ON(!nfrags); 36729d8506ccSHerbert Xu 36731a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 36749d8506ccSHerbert Xu } 36759d8506ccSHerbert Xu 36769d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 36779d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 36789d8506ccSHerbert Xu net_warn_ratelimited( 36799d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 36809d8506ccSHerbert Xu pos, mss); 36819d8506ccSHerbert Xu goto err; 36829d8506ccSHerbert Xu } 36839d8506ccSHerbert Xu 36841fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 36851fd819ecSMichael S. Tsirkin goto err; 36861fd819ecSMichael S. Tsirkin 36874e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 36888cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 36898cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3690f4c50d99SHerbert Xu 3691f4c50d99SHerbert Xu if (pos < offset) { 36928cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 36938cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3694f4c50d99SHerbert Xu } 3695f4c50d99SHerbert Xu 369689319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3697f4c50d99SHerbert Xu 3698f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3699f4c50d99SHerbert Xu i++; 37004e1beba1SMichael S. Tsirkin frag++; 3701f4c50d99SHerbert Xu pos += size; 3702f4c50d99SHerbert Xu } else { 37038cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 370489319d38SHerbert Xu goto skip_fraglist; 3705f4c50d99SHerbert Xu } 3706f4c50d99SHerbert Xu 37078cb19905SMichael S. Tsirkin nskb_frag++; 3708f4c50d99SHerbert Xu } 3709f4c50d99SHerbert Xu 371089319d38SHerbert Xu skip_fraglist: 3711f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3712f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3713f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3714ec5f0615SPravin B Shelar 37151cdbcb79SSimon Horman perform_csum_check: 37167fbeffedSAlexander Duyck if (!csum) { 3717ddff00d4SAlexander Duyck if (skb_has_shared_frag(nskb)) { 3718ddff00d4SAlexander Duyck err = __skb_linearize(nskb); 3719ddff00d4SAlexander Duyck if (err) 3720ddff00d4SAlexander Duyck goto err; 3721ddff00d4SAlexander Duyck } 37227fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 3723ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 372476443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 372576443456SAlexander Duyck skb_checksum(nskb, doffset, 372676443456SAlexander Duyck nskb->len - doffset, 0); 37277e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 37287e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3729ec5f0615SPravin B Shelar } 3730df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3731f4c50d99SHerbert Xu 3732bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3733bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3734bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3735bec3cfdcSEric Dumazet */ 3736bec3cfdcSEric Dumazet segs->prev = tail; 3737432c856fSToshiaki Makita 3738802ab55aSAlexander Duyck if (partial_segs) { 373907b26c94SSteffen Klassert struct sk_buff *iter; 3740802ab55aSAlexander Duyck int type = skb_shinfo(head_skb)->gso_type; 374107b26c94SSteffen Klassert unsigned short gso_size = skb_shinfo(head_skb)->gso_size; 3742802ab55aSAlexander Duyck 3743802ab55aSAlexander Duyck /* Update type to add partial and then remove dodgy if set */ 374407b26c94SSteffen Klassert type |= (features & NETIF_F_GSO_PARTIAL) / NETIF_F_GSO_PARTIAL * SKB_GSO_PARTIAL; 3745802ab55aSAlexander Duyck type &= ~SKB_GSO_DODGY; 3746802ab55aSAlexander Duyck 3747802ab55aSAlexander Duyck /* Update GSO info and prepare to start updating headers on 3748802ab55aSAlexander Duyck * our way back down the stack of protocols. 3749802ab55aSAlexander Duyck */ 375007b26c94SSteffen Klassert for (iter = segs; iter; iter = iter->next) { 375107b26c94SSteffen Klassert skb_shinfo(iter)->gso_size = gso_size; 375207b26c94SSteffen Klassert skb_shinfo(iter)->gso_segs = partial_segs; 375307b26c94SSteffen Klassert skb_shinfo(iter)->gso_type = type; 375407b26c94SSteffen Klassert SKB_GSO_CB(iter)->data_offset = skb_headroom(iter) + doffset; 375507b26c94SSteffen Klassert } 375607b26c94SSteffen Klassert 375707b26c94SSteffen Klassert if (tail->len - doffset <= gso_size) 375807b26c94SSteffen Klassert skb_shinfo(tail)->gso_size = 0; 375907b26c94SSteffen Klassert else if (tail != segs) 376007b26c94SSteffen Klassert skb_shinfo(tail)->gso_segs = DIV_ROUND_UP(tail->len - doffset, gso_size); 3761802ab55aSAlexander Duyck } 3762802ab55aSAlexander Duyck 3763432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3764432c856fSToshiaki Makita * using skb_set_owner_w(). 3765432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3766432c856fSToshiaki Makita */ 3767432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3768432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3769432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3770432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3771432c856fSToshiaki Makita } 3772f4c50d99SHerbert Xu return segs; 3773f4c50d99SHerbert Xu 3774f4c50d99SHerbert Xu err: 3775289dccbeSEric Dumazet kfree_skb_list(segs); 3776f4c50d99SHerbert Xu return ERR_PTR(err); 3777f4c50d99SHerbert Xu } 3778f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3779f4c50d99SHerbert Xu 378071d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 378171d93b39SHerbert Xu { 37828a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 378367147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 378467147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 37858a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 378658025e46SEric Dumazet struct sk_buff *lp, *p = *head; 3787715dc1f3SEric Dumazet unsigned int delta_truesize; 378871d93b39SHerbert Xu 37898a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 379071d93b39SHerbert Xu return -E2BIG; 379171d93b39SHerbert Xu 379229e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 37938a29111cSEric Dumazet pinfo = skb_shinfo(lp); 37948a29111cSEric Dumazet 37958a29111cSEric Dumazet if (headlen <= offset) { 379642da6994SHerbert Xu skb_frag_t *frag; 379766e92fcfSHerbert Xu skb_frag_t *frag2; 37989aaa156cSHerbert Xu int i = skbinfo->nr_frags; 37999aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 380042da6994SHerbert Xu 380166e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 38028a29111cSEric Dumazet goto merge; 380381705ad1SHerbert Xu 38048a29111cSEric Dumazet offset -= headlen; 38059aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 38069aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3807f5572068SHerbert Xu 38089aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 38099aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 381066e92fcfSHerbert Xu do { 381166e92fcfSHerbert Xu *--frag = *--frag2; 381266e92fcfSHerbert Xu } while (--i); 381366e92fcfSHerbert Xu 381466e92fcfSHerbert Xu frag->page_offset += offset; 38159e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 381666e92fcfSHerbert Xu 3817715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3818ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3819ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3820715dc1f3SEric Dumazet 3821f5572068SHerbert Xu skb->truesize -= skb->data_len; 3822f5572068SHerbert Xu skb->len -= skb->data_len; 3823f5572068SHerbert Xu skb->data_len = 0; 3824f5572068SHerbert Xu 3825715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 38265d38a079SHerbert Xu goto done; 3827d7e8883cSEric Dumazet } else if (skb->head_frag) { 3828d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3829d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3830d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3831d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3832d7e8883cSEric Dumazet unsigned int first_offset; 3833d7e8883cSEric Dumazet 3834d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 38358a29111cSEric Dumazet goto merge; 3836d7e8883cSEric Dumazet 3837d7e8883cSEric Dumazet first_offset = skb->data - 3838d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3839d7e8883cSEric Dumazet offset; 3840d7e8883cSEric Dumazet 3841d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3842d7e8883cSEric Dumazet 3843d7e8883cSEric Dumazet frag->page.p = page; 3844d7e8883cSEric Dumazet frag->page_offset = first_offset; 3845d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3846d7e8883cSEric Dumazet 3847d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3848d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3849d7e8883cSEric Dumazet 3850715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3851d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3852d7e8883cSEric Dumazet goto done; 38538a29111cSEric Dumazet } 385471d93b39SHerbert Xu 385571d93b39SHerbert Xu merge: 3856715dc1f3SEric Dumazet delta_truesize = skb->truesize; 385767147ba9SHerbert Xu if (offset > headlen) { 3858d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3859d1dc7abfSMichal Schmidt 3860d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 38619e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3862d1dc7abfSMichal Schmidt skb->data_len -= eat; 3863d1dc7abfSMichal Schmidt skb->len -= eat; 386467147ba9SHerbert Xu offset = headlen; 386556035022SHerbert Xu } 386656035022SHerbert Xu 386767147ba9SHerbert Xu __skb_pull(skb, offset); 386856035022SHerbert Xu 386929e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 38708a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 38718a29111cSEric Dumazet else 3872c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3873c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3874f4a775d1SEric Dumazet __skb_header_release(skb); 38758a29111cSEric Dumazet lp = p; 387671d93b39SHerbert Xu 38775d38a079SHerbert Xu done: 38785d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 387937fe4732SHerbert Xu p->data_len += len; 3880715dc1f3SEric Dumazet p->truesize += delta_truesize; 388137fe4732SHerbert Xu p->len += len; 38828a29111cSEric Dumazet if (lp != p) { 38838a29111cSEric Dumazet lp->data_len += len; 38848a29111cSEric Dumazet lp->truesize += delta_truesize; 38858a29111cSEric Dumazet lp->len += len; 38868a29111cSEric Dumazet } 388771d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 388871d93b39SHerbert Xu return 0; 388971d93b39SHerbert Xu } 389057c05650SMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gro_receive); 389171d93b39SHerbert Xu 38921da177e4SLinus Torvalds void __init skb_init(void) 38931da177e4SLinus Torvalds { 38941da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 38951da177e4SLinus Torvalds sizeof(struct sk_buff), 38961da177e4SLinus Torvalds 0, 3897e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 389820c2df83SPaul Mundt NULL); 3899d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3900d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3901d179cd12SDavid S. Miller 0, 3902e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 390320c2df83SPaul Mundt NULL); 39041da177e4SLinus Torvalds } 39051da177e4SLinus Torvalds 390651c739d1SDavid S. Miller static int 390748a1df65SJason A. Donenfeld __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len, 390848a1df65SJason A. Donenfeld unsigned int recursion_level) 3909716ea3a7SDavid Howells { 39101a028e50SDavid S. Miller int start = skb_headlen(skb); 39111a028e50SDavid S. Miller int i, copy = start - offset; 3912fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3913716ea3a7SDavid Howells int elt = 0; 3914716ea3a7SDavid Howells 391548a1df65SJason A. Donenfeld if (unlikely(recursion_level >= 24)) 391648a1df65SJason A. Donenfeld return -EMSGSIZE; 391748a1df65SJason A. Donenfeld 3918716ea3a7SDavid Howells if (copy > 0) { 3919716ea3a7SDavid Howells if (copy > len) 3920716ea3a7SDavid Howells copy = len; 3921642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3922716ea3a7SDavid Howells elt++; 3923716ea3a7SDavid Howells if ((len -= copy) == 0) 3924716ea3a7SDavid Howells return elt; 3925716ea3a7SDavid Howells offset += copy; 3926716ea3a7SDavid Howells } 3927716ea3a7SDavid Howells 3928716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 39291a028e50SDavid S. Miller int end; 3930716ea3a7SDavid Howells 3931547b792cSIlpo Järvinen WARN_ON(start > offset + len); 39321a028e50SDavid S. Miller 39339e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3934716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3935716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 393648a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 393748a1df65SJason A. Donenfeld return -EMSGSIZE; 3938716ea3a7SDavid Howells 3939716ea3a7SDavid Howells if (copy > len) 3940716ea3a7SDavid Howells copy = len; 3941ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3942642f1490SJens Axboe frag->page_offset+offset-start); 3943716ea3a7SDavid Howells elt++; 3944716ea3a7SDavid Howells if (!(len -= copy)) 3945716ea3a7SDavid Howells return elt; 3946716ea3a7SDavid Howells offset += copy; 3947716ea3a7SDavid Howells } 39481a028e50SDavid S. Miller start = end; 3949716ea3a7SDavid Howells } 3950716ea3a7SDavid Howells 3951fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 395248a1df65SJason A. Donenfeld int end, ret; 3953716ea3a7SDavid Howells 3954547b792cSIlpo Järvinen WARN_ON(start > offset + len); 39551a028e50SDavid S. Miller 3956fbb398a8SDavid S. Miller end = start + frag_iter->len; 3957716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 395848a1df65SJason A. Donenfeld if (unlikely(elt && sg_is_last(&sg[elt - 1]))) 395948a1df65SJason A. Donenfeld return -EMSGSIZE; 396048a1df65SJason A. Donenfeld 3961716ea3a7SDavid Howells if (copy > len) 3962716ea3a7SDavid Howells copy = len; 396348a1df65SJason A. Donenfeld ret = __skb_to_sgvec(frag_iter, sg+elt, offset - start, 396448a1df65SJason A. Donenfeld copy, recursion_level + 1); 396548a1df65SJason A. Donenfeld if (unlikely(ret < 0)) 396648a1df65SJason A. Donenfeld return ret; 396748a1df65SJason A. Donenfeld elt += ret; 3968716ea3a7SDavid Howells if ((len -= copy) == 0) 3969716ea3a7SDavid Howells return elt; 3970716ea3a7SDavid Howells offset += copy; 3971716ea3a7SDavid Howells } 39721a028e50SDavid S. Miller start = end; 3973716ea3a7SDavid Howells } 3974716ea3a7SDavid Howells BUG_ON(len); 3975716ea3a7SDavid Howells return elt; 3976716ea3a7SDavid Howells } 3977716ea3a7SDavid Howells 397848a1df65SJason A. Donenfeld /** 397948a1df65SJason A. Donenfeld * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 398048a1df65SJason A. Donenfeld * @skb: Socket buffer containing the buffers to be mapped 398148a1df65SJason A. Donenfeld * @sg: The scatter-gather list to map into 398248a1df65SJason A. Donenfeld * @offset: The offset into the buffer's contents to start mapping 398348a1df65SJason A. Donenfeld * @len: Length of buffer space to be mapped 398448a1df65SJason A. Donenfeld * 398548a1df65SJason A. Donenfeld * Fill the specified scatter-gather list with mappings/pointers into a 398648a1df65SJason A. Donenfeld * region of the buffer space attached to a socket buffer. Returns either 398748a1df65SJason A. Donenfeld * the number of scatterlist items used, or -EMSGSIZE if the contents 398848a1df65SJason A. Donenfeld * could not fit. 398948a1df65SJason A. Donenfeld */ 399048a1df65SJason A. Donenfeld int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 399148a1df65SJason A. Donenfeld { 399248a1df65SJason A. Donenfeld int nsg = __skb_to_sgvec(skb, sg, offset, len, 0); 399348a1df65SJason A. Donenfeld 399448a1df65SJason A. Donenfeld if (nsg <= 0) 399548a1df65SJason A. Donenfeld return nsg; 399648a1df65SJason A. Donenfeld 399748a1df65SJason A. Donenfeld sg_mark_end(&sg[nsg - 1]); 399848a1df65SJason A. Donenfeld 399948a1df65SJason A. Donenfeld return nsg; 400048a1df65SJason A. Donenfeld } 400148a1df65SJason A. Donenfeld EXPORT_SYMBOL_GPL(skb_to_sgvec); 400248a1df65SJason A. Donenfeld 400325a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 400425a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 400525a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 400625a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 400725a91d8dSFan Du * 400825a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 400925a91d8dSFan Du * 1. sg_init_table 401025a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 401125a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 401225a91d8dSFan Du * 401325a91d8dSFan Du * This is equivalent to: 401425a91d8dSFan Du * 1. sg_init_table 401525a91d8dSFan Du * 2. skb_to_sgvec(payload1) 401625a91d8dSFan Du * 3. sg_unmark_end 401725a91d8dSFan Du * 4. skb_to_sgvec(payload2) 401825a91d8dSFan Du * 401925a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 402025a91d8dSFan Du * is more preferable. 402125a91d8dSFan Du */ 402225a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 402325a91d8dSFan Du int offset, int len) 402425a91d8dSFan Du { 402548a1df65SJason A. Donenfeld return __skb_to_sgvec(skb, sg, offset, len, 0); 402625a91d8dSFan Du } 402725a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 402825a91d8dSFan Du 402951c739d1SDavid S. Miller 403051c739d1SDavid S. Miller 4031716ea3a7SDavid Howells /** 4032716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 4033716ea3a7SDavid Howells * @skb: The socket buffer to check. 4034716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 4035716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 4036716ea3a7SDavid Howells * 4037716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 4038716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 4039716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 4040716ea3a7SDavid Howells * 4041716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 4042716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 4043716ea3a7SDavid Howells * set to point to the skb in which this space begins. 4044716ea3a7SDavid Howells * 4045716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 4046716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 4047716ea3a7SDavid Howells */ 4048716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 4049716ea3a7SDavid Howells { 4050716ea3a7SDavid Howells int copyflag; 4051716ea3a7SDavid Howells int elt; 4052716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 4053716ea3a7SDavid Howells 4054716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 4055716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 4056716ea3a7SDavid Howells * at the moment even if they are anonymous). 4057716ea3a7SDavid Howells */ 4058716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 4059716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 4060716ea3a7SDavid Howells return -ENOMEM; 4061716ea3a7SDavid Howells 4062716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 406321dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 4064716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 4065716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 4066716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 4067716ea3a7SDavid Howells * space, 128 bytes is fair. */ 4068716ea3a7SDavid Howells 4069716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 4070716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 4071716ea3a7SDavid Howells return -ENOMEM; 4072716ea3a7SDavid Howells 4073716ea3a7SDavid Howells /* Voila! */ 4074716ea3a7SDavid Howells *trailer = skb; 4075716ea3a7SDavid Howells return 1; 4076716ea3a7SDavid Howells } 4077716ea3a7SDavid Howells 4078716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 4079716ea3a7SDavid Howells 4080716ea3a7SDavid Howells elt = 1; 4081716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 4082716ea3a7SDavid Howells copyflag = 0; 4083716ea3a7SDavid Howells 4084716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 4085716ea3a7SDavid Howells int ntail = 0; 4086716ea3a7SDavid Howells 4087716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 4088716ea3a7SDavid Howells * this can happen on input. Copy it and everything 4089716ea3a7SDavid Howells * after it. */ 4090716ea3a7SDavid Howells 4091716ea3a7SDavid Howells if (skb_shared(skb1)) 4092716ea3a7SDavid Howells copyflag = 1; 4093716ea3a7SDavid Howells 4094716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 4095716ea3a7SDavid Howells 4096716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 4097716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 409821dc3301SDavid S. Miller skb_has_frag_list(skb1) || 4099716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 4100716ea3a7SDavid Howells ntail = tailbits + 128; 4101716ea3a7SDavid Howells } 4102716ea3a7SDavid Howells 4103716ea3a7SDavid Howells if (copyflag || 4104716ea3a7SDavid Howells skb_cloned(skb1) || 4105716ea3a7SDavid Howells ntail || 4106716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 410721dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 4108716ea3a7SDavid Howells struct sk_buff *skb2; 4109716ea3a7SDavid Howells 4110716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 4111716ea3a7SDavid Howells if (ntail == 0) 4112716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 4113716ea3a7SDavid Howells else 4114716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 4115716ea3a7SDavid Howells skb_headroom(skb1), 4116716ea3a7SDavid Howells ntail, 4117716ea3a7SDavid Howells GFP_ATOMIC); 4118716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 4119716ea3a7SDavid Howells return -ENOMEM; 4120716ea3a7SDavid Howells 4121716ea3a7SDavid Howells if (skb1->sk) 4122716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 4123716ea3a7SDavid Howells 4124716ea3a7SDavid Howells /* Looking around. Are we still alive? 4125716ea3a7SDavid Howells * OK, link new skb, drop old one */ 4126716ea3a7SDavid Howells 4127716ea3a7SDavid Howells skb2->next = skb1->next; 4128716ea3a7SDavid Howells *skb_p = skb2; 4129716ea3a7SDavid Howells kfree_skb(skb1); 4130716ea3a7SDavid Howells skb1 = skb2; 4131716ea3a7SDavid Howells } 4132716ea3a7SDavid Howells elt++; 4133716ea3a7SDavid Howells *trailer = skb1; 4134716ea3a7SDavid Howells skb_p = &skb1->next; 4135716ea3a7SDavid Howells } 4136716ea3a7SDavid Howells 4137716ea3a7SDavid Howells return elt; 4138716ea3a7SDavid Howells } 4139b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 4140716ea3a7SDavid Howells 4141b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 4142b1faf566SEric Dumazet { 4143b1faf566SEric Dumazet struct sock *sk = skb->sk; 4144b1faf566SEric Dumazet 4145b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 4146b1faf566SEric Dumazet } 4147b1faf566SEric Dumazet 41488605330aSSoheil Hassas Yeganeh static void skb_set_err_queue(struct sk_buff *skb) 41498605330aSSoheil Hassas Yeganeh { 41508605330aSSoheil Hassas Yeganeh /* pkt_type of skbs received on local sockets is never PACKET_OUTGOING. 41518605330aSSoheil Hassas Yeganeh * So, it is safe to (mis)use it to mark skbs on the error queue. 41528605330aSSoheil Hassas Yeganeh */ 41538605330aSSoheil Hassas Yeganeh skb->pkt_type = PACKET_OUTGOING; 41548605330aSSoheil Hassas Yeganeh BUILD_BUG_ON(PACKET_OUTGOING == 0); 41558605330aSSoheil Hassas Yeganeh } 41568605330aSSoheil Hassas Yeganeh 4157b1faf566SEric Dumazet /* 4158b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 4159b1faf566SEric Dumazet */ 4160b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 4161b1faf566SEric Dumazet { 4162b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 416395c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 4164b1faf566SEric Dumazet return -ENOMEM; 4165b1faf566SEric Dumazet 4166b1faf566SEric Dumazet skb_orphan(skb); 4167b1faf566SEric Dumazet skb->sk = sk; 4168b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 4169b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 41708605330aSSoheil Hassas Yeganeh skb_set_err_queue(skb); 4171b1faf566SEric Dumazet 4172abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 4173abb57ea4SEric Dumazet skb_dst_force(skb); 4174abb57ea4SEric Dumazet 4175b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 4176b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 4177676d2369SDavid S. Miller sk->sk_data_ready(sk); 4178b1faf566SEric Dumazet return 0; 4179b1faf566SEric Dumazet } 4180b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 4181b1faf566SEric Dumazet 418283a1a1a7SSoheil Hassas Yeganeh static bool is_icmp_err_skb(const struct sk_buff *skb) 418383a1a1a7SSoheil Hassas Yeganeh { 418483a1a1a7SSoheil Hassas Yeganeh return skb && (SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP || 418583a1a1a7SSoheil Hassas Yeganeh SKB_EXT_ERR(skb)->ee.ee_origin == SO_EE_ORIGIN_ICMP6); 418683a1a1a7SSoheil Hassas Yeganeh } 418783a1a1a7SSoheil Hassas Yeganeh 4188364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 4189364a9e93SWillem de Bruijn { 4190364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 419183a1a1a7SSoheil Hassas Yeganeh struct sk_buff *skb, *skb_next = NULL; 419283a1a1a7SSoheil Hassas Yeganeh bool icmp_next = false; 4193997d5c3fSEric Dumazet unsigned long flags; 4194364a9e93SWillem de Bruijn 4195997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 4196364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 419738b25793SSoheil Hassas Yeganeh if (skb && (skb_next = skb_peek(q))) { 419883a1a1a7SSoheil Hassas Yeganeh icmp_next = is_icmp_err_skb(skb_next); 419938b25793SSoheil Hassas Yeganeh if (icmp_next) 420038b25793SSoheil Hassas Yeganeh sk->sk_err = SKB_EXT_ERR(skb_next)->ee.ee_origin; 420138b25793SSoheil Hassas Yeganeh } 4202997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 4203364a9e93SWillem de Bruijn 420483a1a1a7SSoheil Hassas Yeganeh if (is_icmp_err_skb(skb) && !icmp_next) 420583a1a1a7SSoheil Hassas Yeganeh sk->sk_err = 0; 420683a1a1a7SSoheil Hassas Yeganeh 420783a1a1a7SSoheil Hassas Yeganeh if (skb_next) 4208364a9e93SWillem de Bruijn sk->sk_error_report(sk); 4209364a9e93SWillem de Bruijn 4210364a9e93SWillem de Bruijn return skb; 4211364a9e93SWillem de Bruijn } 4212364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 4213364a9e93SWillem de Bruijn 4214cab41c47SAlexander Duyck /** 4215cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 4216cab41c47SAlexander Duyck * @skb: the skb to clone 4217cab41c47SAlexander Duyck * 4218cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 4219cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 4220cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 4221cab41c47SAlexander Duyck * 4222cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 4223cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 4224cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 4225cab41c47SAlexander Duyck * the sk_error_queue. 4226cab41c47SAlexander Duyck */ 422762bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 422862bccb8cSAlexander Duyck { 422962bccb8cSAlexander Duyck struct sock *sk = skb->sk; 423062bccb8cSAlexander Duyck struct sk_buff *clone; 423162bccb8cSAlexander Duyck 423241c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) 423362bccb8cSAlexander Duyck return NULL; 423462bccb8cSAlexander Duyck 423562bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 423662bccb8cSAlexander Duyck if (!clone) { 423762bccb8cSAlexander Duyck sock_put(sk); 423862bccb8cSAlexander Duyck return NULL; 423962bccb8cSAlexander Duyck } 424062bccb8cSAlexander Duyck 424162bccb8cSAlexander Duyck clone->sk = sk; 424262bccb8cSAlexander Duyck clone->destructor = sock_efree; 424362bccb8cSAlexander Duyck 424462bccb8cSAlexander Duyck return clone; 424562bccb8cSAlexander Duyck } 424662bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 424762bccb8cSAlexander Duyck 424837846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 424937846ef0SAlexander Duyck struct sock *sk, 42504ef1b286SSoheil Hassas Yeganeh int tstype, 42514ef1b286SSoheil Hassas Yeganeh bool opt_stats) 4252ac45f602SPatrick Ohly { 4253ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 4254ac45f602SPatrick Ohly int err; 4255ac45f602SPatrick Ohly 42564ef1b286SSoheil Hassas Yeganeh BUILD_BUG_ON(sizeof(struct sock_exterr_skb) > sizeof(skb->cb)); 42574ef1b286SSoheil Hassas Yeganeh 4258ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 4259ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 4260ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 4261ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 4262e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 42634ef1b286SSoheil Hassas Yeganeh serr->opt_stats = opt_stats; 42641862d620SWillem de Bruijn serr->header.h4.iif = skb->dev ? skb->dev->ifindex : 0; 42654ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 426609c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 4267ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 4268ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 42694ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 42704ed2d765SWillem de Bruijn } 427129030374SEric Dumazet 4272ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 427329030374SEric Dumazet 4274ac45f602SPatrick Ohly if (err) 4275ac45f602SPatrick Ohly kfree_skb(skb); 4276ac45f602SPatrick Ohly } 427737846ef0SAlexander Duyck 4278b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 4279b245be1fSWillem de Bruijn { 4280b245be1fSWillem de Bruijn bool ret; 4281b245be1fSWillem de Bruijn 4282b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 4283b245be1fSWillem de Bruijn return true; 4284b245be1fSWillem de Bruijn 4285b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 4286b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 4287b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 4288b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 4289b245be1fSWillem de Bruijn return ret; 4290b245be1fSWillem de Bruijn } 4291b245be1fSWillem de Bruijn 429237846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 429337846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 429437846ef0SAlexander Duyck { 429537846ef0SAlexander Duyck struct sock *sk = skb->sk; 429637846ef0SAlexander Duyck 4297b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 4298b245be1fSWillem de Bruijn return; 4299b245be1fSWillem de Bruijn 43009ac25fc0SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 43019ac25fc0SEric Dumazet * but only if the socket refcount is not zero. 43029ac25fc0SEric Dumazet */ 430341c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 430437846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 43054ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND, false); 430637846ef0SAlexander Duyck sock_put(sk); 430737846ef0SAlexander Duyck } 43089ac25fc0SEric Dumazet } 430937846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 431037846ef0SAlexander Duyck 431137846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 431237846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 431337846ef0SAlexander Duyck struct sock *sk, int tstype) 431437846ef0SAlexander Duyck { 431537846ef0SAlexander Duyck struct sk_buff *skb; 43164ef1b286SSoheil Hassas Yeganeh bool tsonly, opt_stats = false; 431737846ef0SAlexander Duyck 43183a8dd971SWillem de Bruijn if (!sk) 43193a8dd971SWillem de Bruijn return; 43203a8dd971SWillem de Bruijn 4321b50a5c70SMiroslav Lichvar if (!hwtstamps && !(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TX_SWHW) && 4322b50a5c70SMiroslav Lichvar skb_shinfo(orig_skb)->tx_flags & SKBTX_IN_PROGRESS) 4323b50a5c70SMiroslav Lichvar return; 4324b50a5c70SMiroslav Lichvar 43253a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 43263a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 432737846ef0SAlexander Duyck return; 432837846ef0SAlexander Duyck 43291c885808SFrancis Yan if (tsonly) { 43301c885808SFrancis Yan #ifdef CONFIG_INET 43311c885808SFrancis Yan if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_STATS) && 43321c885808SFrancis Yan sk->sk_protocol == IPPROTO_TCP && 43334ef1b286SSoheil Hassas Yeganeh sk->sk_type == SOCK_STREAM) { 43341c885808SFrancis Yan skb = tcp_get_timestamping_opt_stats(sk); 43354ef1b286SSoheil Hassas Yeganeh opt_stats = true; 43364ef1b286SSoheil Hassas Yeganeh } else 43371c885808SFrancis Yan #endif 43381c885808SFrancis Yan skb = alloc_skb(0, GFP_ATOMIC); 43391c885808SFrancis Yan } else { 434037846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 43411c885808SFrancis Yan } 434237846ef0SAlexander Duyck if (!skb) 434337846ef0SAlexander Duyck return; 434437846ef0SAlexander Duyck 434549ca0d8bSWillem de Bruijn if (tsonly) { 4346fff88030SWillem de Bruijn skb_shinfo(skb)->tx_flags |= skb_shinfo(orig_skb)->tx_flags & 4347fff88030SWillem de Bruijn SKBTX_ANY_TSTAMP; 434849ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 434949ca0d8bSWillem de Bruijn } 435049ca0d8bSWillem de Bruijn 435149ca0d8bSWillem de Bruijn if (hwtstamps) 435249ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 435349ca0d8bSWillem de Bruijn else 435449ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 435549ca0d8bSWillem de Bruijn 43564ef1b286SSoheil Hassas Yeganeh __skb_complete_tx_timestamp(skb, sk, tstype, opt_stats); 435737846ef0SAlexander Duyck } 4358e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 4359e7fd2885SWillem de Bruijn 4360e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 4361e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 4362e7fd2885SWillem de Bruijn { 4363e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 4364e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 4365e7fd2885SWillem de Bruijn } 4366ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 4367ac45f602SPatrick Ohly 43686e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 43696e3e939fSJohannes Berg { 43706e3e939fSJohannes Berg struct sock *sk = skb->sk; 43716e3e939fSJohannes Berg struct sock_exterr_skb *serr; 4372dd4f1072SEric Dumazet int err = 1; 43736e3e939fSJohannes Berg 43746e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 43756e3e939fSJohannes Berg skb->wifi_acked = acked; 43766e3e939fSJohannes Berg 43776e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 43786e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 43796e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 43806e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 43816e3e939fSJohannes Berg 4382dd4f1072SEric Dumazet /* Take a reference to prevent skb_orphan() from freeing the socket, 4383dd4f1072SEric Dumazet * but only if the socket refcount is not zero. 4384dd4f1072SEric Dumazet */ 438541c6d650SReshetova, Elena if (likely(refcount_inc_not_zero(&sk->sk_refcnt))) { 43866e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 4387dd4f1072SEric Dumazet sock_put(sk); 4388dd4f1072SEric Dumazet } 43896e3e939fSJohannes Berg if (err) 43906e3e939fSJohannes Berg kfree_skb(skb); 43916e3e939fSJohannes Berg } 43926e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 43936e3e939fSJohannes Berg 4394f35d9d8aSRusty Russell /** 4395f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 4396f35d9d8aSRusty Russell * @skb: the skb to set 4397f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 4398f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 4399f35d9d8aSRusty Russell * 4400f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 4401f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 4402f35d9d8aSRusty Russell * 4403f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 4404f35d9d8aSRusty Russell * returns false you should drop the packet. 4405f35d9d8aSRusty Russell */ 4406f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 4407f35d9d8aSRusty Russell { 44085ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 44095ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 4410e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 44115ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 4412f35d9d8aSRusty Russell return false; 4413f35d9d8aSRusty Russell } 4414f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 4415f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 4416f35d9d8aSRusty Russell skb->csum_offset = off; 4417e5d5decaSJason Wang skb_set_transport_header(skb, start); 4418f35d9d8aSRusty Russell return true; 4419f35d9d8aSRusty Russell } 4420b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 4421f35d9d8aSRusty Russell 4422ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 4423ed1f50c3SPaul Durrant unsigned int max) 4424ed1f50c3SPaul Durrant { 4425ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 4426ed1f50c3SPaul Durrant return 0; 4427ed1f50c3SPaul Durrant 4428ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 4429ed1f50c3SPaul Durrant * won't need to do it again. 4430ed1f50c3SPaul Durrant */ 4431ed1f50c3SPaul Durrant if (max > skb->len) 4432ed1f50c3SPaul Durrant max = skb->len; 4433ed1f50c3SPaul Durrant 4434ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 4435ed1f50c3SPaul Durrant return -ENOMEM; 4436ed1f50c3SPaul Durrant 4437ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 4438ed1f50c3SPaul Durrant return -EPROTO; 4439ed1f50c3SPaul Durrant 4440ed1f50c3SPaul Durrant return 0; 4441ed1f50c3SPaul Durrant } 4442ed1f50c3SPaul Durrant 4443f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 4444f9708b43SJan Beulich 4445f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 4446f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 4447f9708b43SJan Beulich unsigned int off) 4448f9708b43SJan Beulich { 4449f9708b43SJan Beulich switch (proto) { 4450f9708b43SJan Beulich int err; 4451f9708b43SJan Beulich 4452f9708b43SJan Beulich case IPPROTO_TCP: 4453f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 4454f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 4455f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4456f9708b43SJan Beulich offsetof(struct tcphdr, 4457f9708b43SJan Beulich check))) 4458f9708b43SJan Beulich err = -EPROTO; 4459f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 4460f9708b43SJan Beulich 4461f9708b43SJan Beulich case IPPROTO_UDP: 4462f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 4463f9708b43SJan Beulich off + sizeof(struct udphdr)); 4464f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 4465f9708b43SJan Beulich offsetof(struct udphdr, 4466f9708b43SJan Beulich check))) 4467f9708b43SJan Beulich err = -EPROTO; 4468f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 4469f9708b43SJan Beulich } 4470f9708b43SJan Beulich 4471f9708b43SJan Beulich return ERR_PTR(-EPROTO); 4472f9708b43SJan Beulich } 4473f9708b43SJan Beulich 4474ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4475ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 4476ed1f50c3SPaul Durrant */ 4477ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 4478ed1f50c3SPaul Durrant 4479f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 4480ed1f50c3SPaul Durrant { 4481ed1f50c3SPaul Durrant unsigned int off; 4482ed1f50c3SPaul Durrant bool fragment; 4483f9708b43SJan Beulich __sum16 *csum; 4484ed1f50c3SPaul Durrant int err; 4485ed1f50c3SPaul Durrant 4486ed1f50c3SPaul Durrant fragment = false; 4487ed1f50c3SPaul Durrant 4488ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4489ed1f50c3SPaul Durrant sizeof(struct iphdr), 4490ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 4491ed1f50c3SPaul Durrant if (err < 0) 4492ed1f50c3SPaul Durrant goto out; 4493ed1f50c3SPaul Durrant 4494ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 4495ed1f50c3SPaul Durrant fragment = true; 4496ed1f50c3SPaul Durrant 4497ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 4498ed1f50c3SPaul Durrant 4499ed1f50c3SPaul Durrant err = -EPROTO; 4500ed1f50c3SPaul Durrant 4501ed1f50c3SPaul Durrant if (fragment) 4502ed1f50c3SPaul Durrant goto out; 4503ed1f50c3SPaul Durrant 4504f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 4505f9708b43SJan Beulich if (IS_ERR(csum)) 4506f9708b43SJan Beulich return PTR_ERR(csum); 4507ed1f50c3SPaul Durrant 4508ed1f50c3SPaul Durrant if (recalculate) 4509f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 4510ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 4511ed1f50c3SPaul Durrant skb->len - off, 4512f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 4513ed1f50c3SPaul Durrant err = 0; 4514ed1f50c3SPaul Durrant 4515ed1f50c3SPaul Durrant out: 4516ed1f50c3SPaul Durrant return err; 4517ed1f50c3SPaul Durrant } 4518ed1f50c3SPaul Durrant 4519ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 4520ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 4521ed1f50c3SPaul Durrant */ 4522ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 4523ed1f50c3SPaul Durrant 4524ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 4525ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 4526ed1f50c3SPaul Durrant 4527ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 4528ed1f50c3SPaul Durrant { 4529ed1f50c3SPaul Durrant int err; 4530ed1f50c3SPaul Durrant u8 nexthdr; 4531ed1f50c3SPaul Durrant unsigned int off; 4532ed1f50c3SPaul Durrant unsigned int len; 4533ed1f50c3SPaul Durrant bool fragment; 4534ed1f50c3SPaul Durrant bool done; 4535f9708b43SJan Beulich __sum16 *csum; 4536ed1f50c3SPaul Durrant 4537ed1f50c3SPaul Durrant fragment = false; 4538ed1f50c3SPaul Durrant done = false; 4539ed1f50c3SPaul Durrant 4540ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 4541ed1f50c3SPaul Durrant 4542ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 4543ed1f50c3SPaul Durrant if (err < 0) 4544ed1f50c3SPaul Durrant goto out; 4545ed1f50c3SPaul Durrant 4546ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 4547ed1f50c3SPaul Durrant 4548ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 4549ed1f50c3SPaul Durrant while (off <= len && !done) { 4550ed1f50c3SPaul Durrant switch (nexthdr) { 4551ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 4552ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 4553ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 4554ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 4555ed1f50c3SPaul Durrant 4556ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4557ed1f50c3SPaul Durrant off + 4558ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 4559ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4560ed1f50c3SPaul Durrant if (err < 0) 4561ed1f50c3SPaul Durrant goto out; 4562ed1f50c3SPaul Durrant 4563ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4564ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4565ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 4566ed1f50c3SPaul Durrant break; 4567ed1f50c3SPaul Durrant } 4568ed1f50c3SPaul Durrant case IPPROTO_AH: { 4569ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 4570ed1f50c3SPaul Durrant 4571ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4572ed1f50c3SPaul Durrant off + 4573ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 4574ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4575ed1f50c3SPaul Durrant if (err < 0) 4576ed1f50c3SPaul Durrant goto out; 4577ed1f50c3SPaul Durrant 4578ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4579ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4580ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 4581ed1f50c3SPaul Durrant break; 4582ed1f50c3SPaul Durrant } 4583ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 4584ed1f50c3SPaul Durrant struct frag_hdr *hp; 4585ed1f50c3SPaul Durrant 4586ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4587ed1f50c3SPaul Durrant off + 4588ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 4589ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4590ed1f50c3SPaul Durrant if (err < 0) 4591ed1f50c3SPaul Durrant goto out; 4592ed1f50c3SPaul Durrant 4593ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 4594ed1f50c3SPaul Durrant 4595ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4596ed1f50c3SPaul Durrant fragment = true; 4597ed1f50c3SPaul Durrant 4598ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4599ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 4600ed1f50c3SPaul Durrant break; 4601ed1f50c3SPaul Durrant } 4602ed1f50c3SPaul Durrant default: 4603ed1f50c3SPaul Durrant done = true; 4604ed1f50c3SPaul Durrant break; 4605ed1f50c3SPaul Durrant } 4606ed1f50c3SPaul Durrant } 4607ed1f50c3SPaul Durrant 4608ed1f50c3SPaul Durrant err = -EPROTO; 4609ed1f50c3SPaul Durrant 4610ed1f50c3SPaul Durrant if (!done || fragment) 4611ed1f50c3SPaul Durrant goto out; 4612ed1f50c3SPaul Durrant 4613f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 4614f9708b43SJan Beulich if (IS_ERR(csum)) 4615f9708b43SJan Beulich return PTR_ERR(csum); 4616ed1f50c3SPaul Durrant 4617ed1f50c3SPaul Durrant if (recalculate) 4618f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4619ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 4620f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4621ed1f50c3SPaul Durrant err = 0; 4622ed1f50c3SPaul Durrant 4623ed1f50c3SPaul Durrant out: 4624ed1f50c3SPaul Durrant return err; 4625ed1f50c3SPaul Durrant } 4626ed1f50c3SPaul Durrant 4627ed1f50c3SPaul Durrant /** 4628ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4629ed1f50c3SPaul Durrant * @skb: the skb to set up 4630ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4631ed1f50c3SPaul Durrant */ 4632ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4633ed1f50c3SPaul Durrant { 4634ed1f50c3SPaul Durrant int err; 4635ed1f50c3SPaul Durrant 4636ed1f50c3SPaul Durrant switch (skb->protocol) { 4637ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4638f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4639ed1f50c3SPaul Durrant break; 4640ed1f50c3SPaul Durrant 4641ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4642ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4643ed1f50c3SPaul Durrant break; 4644ed1f50c3SPaul Durrant 4645ed1f50c3SPaul Durrant default: 4646ed1f50c3SPaul Durrant err = -EPROTO; 4647ed1f50c3SPaul Durrant break; 4648ed1f50c3SPaul Durrant } 4649ed1f50c3SPaul Durrant 4650ed1f50c3SPaul Durrant return err; 4651ed1f50c3SPaul Durrant } 4652ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4653ed1f50c3SPaul Durrant 46549afd85c9SLinus Lüssing /** 46559afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 46569afd85c9SLinus Lüssing * @skb: the skb to check 46579afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 46589afd85c9SLinus Lüssing * 46599afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 46609afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 46619afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 46629afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 46639afd85c9SLinus Lüssing * 4664a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4665a516993fSLinus Lüssing * differs from the provided skb. 46669afd85c9SLinus Lüssing */ 46679afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 46689afd85c9SLinus Lüssing unsigned int transport_len) 46699afd85c9SLinus Lüssing { 46709afd85c9SLinus Lüssing struct sk_buff *skb_chk; 46719afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 46729afd85c9SLinus Lüssing int ret; 46739afd85c9SLinus Lüssing 4674a516993fSLinus Lüssing if (skb->len < len) 46759afd85c9SLinus Lüssing return NULL; 4676a516993fSLinus Lüssing else if (skb->len == len) 46779afd85c9SLinus Lüssing return skb; 46789afd85c9SLinus Lüssing 46799afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 46809afd85c9SLinus Lüssing if (!skb_chk) 46819afd85c9SLinus Lüssing return NULL; 46829afd85c9SLinus Lüssing 46839afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 46849afd85c9SLinus Lüssing if (ret) { 46859afd85c9SLinus Lüssing kfree_skb(skb_chk); 46869afd85c9SLinus Lüssing return NULL; 46879afd85c9SLinus Lüssing } 46889afd85c9SLinus Lüssing 46899afd85c9SLinus Lüssing return skb_chk; 46909afd85c9SLinus Lüssing } 46919afd85c9SLinus Lüssing 46929afd85c9SLinus Lüssing /** 46939afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 46949afd85c9SLinus Lüssing * @skb: the skb to check 46959afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 46969afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 46979afd85c9SLinus Lüssing * 46989afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 46999afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 47009afd85c9SLinus Lüssing * 47019afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 47029afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 47039afd85c9SLinus Lüssing * 4704a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4705a516993fSLinus Lüssing * differs from the provided skb. 47069afd85c9SLinus Lüssing */ 47079afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 47089afd85c9SLinus Lüssing unsigned int transport_len, 47099afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 47109afd85c9SLinus Lüssing { 47119afd85c9SLinus Lüssing struct sk_buff *skb_chk; 47129afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 4713fcba67c9SLinus Lüssing __sum16 ret; 47149afd85c9SLinus Lüssing 47159afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 47169afd85c9SLinus Lüssing if (!skb_chk) 4717a516993fSLinus Lüssing goto err; 47189afd85c9SLinus Lüssing 4719a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 4720a516993fSLinus Lüssing goto err; 47219afd85c9SLinus Lüssing 47229b368814SLinus Lüssing skb_pull_rcsum(skb_chk, offset); 47239afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 47249b368814SLinus Lüssing skb_push_rcsum(skb_chk, offset); 47259afd85c9SLinus Lüssing 4726a516993fSLinus Lüssing if (ret) 4727a516993fSLinus Lüssing goto err; 47289afd85c9SLinus Lüssing 47299afd85c9SLinus Lüssing return skb_chk; 4730a516993fSLinus Lüssing 4731a516993fSLinus Lüssing err: 4732a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 4733a516993fSLinus Lüssing kfree_skb(skb_chk); 4734a516993fSLinus Lüssing 4735a516993fSLinus Lüssing return NULL; 4736a516993fSLinus Lüssing 47379afd85c9SLinus Lüssing } 47389afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 47399afd85c9SLinus Lüssing 47404497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 47414497b076SBen Hutchings { 4742e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4743e87cc472SJoe Perches skb->dev->name); 47444497b076SBen Hutchings } 47454497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4746bad43ca8SEric Dumazet 4747bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4748bad43ca8SEric Dumazet { 47493d861f66SEric Dumazet if (head_stolen) { 47503d861f66SEric Dumazet skb_release_head_state(skb); 4751bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 47523d861f66SEric Dumazet } else { 4753bad43ca8SEric Dumazet __kfree_skb(skb); 4754bad43ca8SEric Dumazet } 47553d861f66SEric Dumazet } 4756bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4757bad43ca8SEric Dumazet 4758bad43ca8SEric Dumazet /** 4759bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4760bad43ca8SEric Dumazet * @to: prior buffer 4761bad43ca8SEric Dumazet * @from: buffer to add 4762bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4763c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4764bad43ca8SEric Dumazet */ 4765bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4766bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4767bad43ca8SEric Dumazet { 4768bad43ca8SEric Dumazet int i, delta, len = from->len; 4769bad43ca8SEric Dumazet 4770bad43ca8SEric Dumazet *fragstolen = false; 4771bad43ca8SEric Dumazet 4772bad43ca8SEric Dumazet if (skb_cloned(to)) 4773bad43ca8SEric Dumazet return false; 4774bad43ca8SEric Dumazet 4775bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4776e93a0435SEric Dumazet if (len) 4777bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4778bad43ca8SEric Dumazet *delta_truesize = 0; 4779bad43ca8SEric Dumazet return true; 4780bad43ca8SEric Dumazet } 4781bad43ca8SEric Dumazet 4782bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 4783bad43ca8SEric Dumazet return false; 47841f8b977aSWillem de Bruijn if (skb_zcopy(to) || skb_zcopy(from)) 47851f8b977aSWillem de Bruijn return false; 4786bad43ca8SEric Dumazet 4787bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4788bad43ca8SEric Dumazet struct page *page; 4789bad43ca8SEric Dumazet unsigned int offset; 4790bad43ca8SEric Dumazet 4791bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4792bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 4793bad43ca8SEric Dumazet return false; 4794bad43ca8SEric Dumazet 4795bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4796bad43ca8SEric Dumazet return false; 4797bad43ca8SEric Dumazet 4798bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4799bad43ca8SEric Dumazet 4800bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4801bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4802bad43ca8SEric Dumazet 4803bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 4804bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4805bad43ca8SEric Dumazet *fragstolen = true; 4806bad43ca8SEric Dumazet } else { 4807bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4808bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4809bad43ca8SEric Dumazet return false; 4810bad43ca8SEric Dumazet 4811f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4812bad43ca8SEric Dumazet } 4813bad43ca8SEric Dumazet 4814bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4815bad43ca8SEric Dumazet 4816bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4817bad43ca8SEric Dumazet skb_shinfo(from)->frags, 4818bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4819bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4820bad43ca8SEric Dumazet 4821bad43ca8SEric Dumazet if (!skb_cloned(from)) 4822bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 4823bad43ca8SEric Dumazet 48248ea853fdSLi RongQing /* if the skb is not cloned this does nothing 48258ea853fdSLi RongQing * since we set nr_frags to 0. 48268ea853fdSLi RongQing */ 4827bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4828bad43ca8SEric Dumazet skb_frag_ref(from, i); 4829bad43ca8SEric Dumazet 4830bad43ca8SEric Dumazet to->truesize += delta; 4831bad43ca8SEric Dumazet to->len += len; 4832bad43ca8SEric Dumazet to->data_len += len; 4833bad43ca8SEric Dumazet 4834bad43ca8SEric Dumazet *delta_truesize = delta; 4835bad43ca8SEric Dumazet return true; 4836bad43ca8SEric Dumazet } 4837bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4838621e84d6SNicolas Dichtel 4839621e84d6SNicolas Dichtel /** 48408b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4841621e84d6SNicolas Dichtel * 4842621e84d6SNicolas Dichtel * @skb: buffer to clean 48438b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4844621e84d6SNicolas Dichtel * 48458b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 48468b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 48478b27f277SNicolas Dichtel * operations. 48488b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 48498b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 48508b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4851621e84d6SNicolas Dichtel */ 48528b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4853621e84d6SNicolas Dichtel { 48542456e855SThomas Gleixner skb->tstamp = 0; 4855621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4856621e84d6SNicolas Dichtel skb->skb_iif = 0; 485760ff7467SWANG Cong skb->ignore_df = 0; 4858621e84d6SNicolas Dichtel skb_dst_drop(skb); 4859621e84d6SNicolas Dichtel secpath_reset(skb); 4860621e84d6SNicolas Dichtel nf_reset(skb); 4861621e84d6SNicolas Dichtel nf_reset_trace(skb); 4862213dd74aSHerbert Xu 4863213dd74aSHerbert Xu if (!xnet) 4864213dd74aSHerbert Xu return; 4865213dd74aSHerbert Xu 4866213dd74aSHerbert Xu skb_orphan(skb); 4867213dd74aSHerbert Xu skb->mark = 0; 4868621e84d6SNicolas Dichtel } 4869621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4870de960aa9SFlorian Westphal 4871de960aa9SFlorian Westphal /** 4872de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4873de960aa9SFlorian Westphal * 4874de960aa9SFlorian Westphal * @skb: GSO skb 4875de960aa9SFlorian Westphal * 4876de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4877de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4878de960aa9SFlorian Westphal * 4879de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4880de960aa9SFlorian Westphal */ 4881de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4882de960aa9SFlorian Westphal { 4883de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4884f993bc25SFlorian Westphal unsigned int thlen = 0; 4885f993bc25SFlorian Westphal 4886f993bc25SFlorian Westphal if (skb->encapsulation) { 4887f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4888f993bc25SFlorian Westphal skb_transport_header(skb); 4889de960aa9SFlorian Westphal 4890de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4891f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4892f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4893f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 489490017accSMarcelo Ricardo Leitner } else if (unlikely(shinfo->gso_type & SKB_GSO_SCTP)) { 489590017accSMarcelo Ricardo Leitner thlen = sizeof(struct sctphdr); 4896f993bc25SFlorian Westphal } 48976d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 48986d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 48996d39d589SFlorian Westphal * accounted for. 49006d39d589SFlorian Westphal */ 4901f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4902de960aa9SFlorian Westphal } 4903de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 49040d5501c1SVlad Yasevich 4905ae7ef81eSMarcelo Ricardo Leitner /** 4906ae7ef81eSMarcelo Ricardo Leitner * skb_gso_validate_mtu - Return in case such skb fits a given MTU 4907ae7ef81eSMarcelo Ricardo Leitner * 4908ae7ef81eSMarcelo Ricardo Leitner * @skb: GSO skb 490976f21b99SDavid S. Miller * @mtu: MTU to validate against 4910ae7ef81eSMarcelo Ricardo Leitner * 4911ae7ef81eSMarcelo Ricardo Leitner * skb_gso_validate_mtu validates if a given skb will fit a wanted MTU 4912ae7ef81eSMarcelo Ricardo Leitner * once split. 4913ae7ef81eSMarcelo Ricardo Leitner */ 4914ae7ef81eSMarcelo Ricardo Leitner bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu) 4915ae7ef81eSMarcelo Ricardo Leitner { 4916ae7ef81eSMarcelo Ricardo Leitner const struct skb_shared_info *shinfo = skb_shinfo(skb); 4917ae7ef81eSMarcelo Ricardo Leitner const struct sk_buff *iter; 4918ae7ef81eSMarcelo Ricardo Leitner unsigned int hlen; 4919ae7ef81eSMarcelo Ricardo Leitner 4920ae7ef81eSMarcelo Ricardo Leitner hlen = skb_gso_network_seglen(skb); 4921ae7ef81eSMarcelo Ricardo Leitner 4922ae7ef81eSMarcelo Ricardo Leitner if (shinfo->gso_size != GSO_BY_FRAGS) 4923ae7ef81eSMarcelo Ricardo Leitner return hlen <= mtu; 4924ae7ef81eSMarcelo Ricardo Leitner 4925ae7ef81eSMarcelo Ricardo Leitner /* Undo this so we can re-use header sizes */ 4926ae7ef81eSMarcelo Ricardo Leitner hlen -= GSO_BY_FRAGS; 4927ae7ef81eSMarcelo Ricardo Leitner 4928ae7ef81eSMarcelo Ricardo Leitner skb_walk_frags(skb, iter) { 4929ae7ef81eSMarcelo Ricardo Leitner if (hlen + skb_headlen(iter) > mtu) 4930ae7ef81eSMarcelo Ricardo Leitner return false; 4931ae7ef81eSMarcelo Ricardo Leitner } 4932ae7ef81eSMarcelo Ricardo Leitner 4933ae7ef81eSMarcelo Ricardo Leitner return true; 4934ae7ef81eSMarcelo Ricardo Leitner } 4935ae7ef81eSMarcelo Ricardo Leitner EXPORT_SYMBOL_GPL(skb_gso_validate_mtu); 4936ae7ef81eSMarcelo Ricardo Leitner 49370d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 49380d5501c1SVlad Yasevich { 49390d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 49400d5501c1SVlad Yasevich kfree_skb(skb); 49410d5501c1SVlad Yasevich return NULL; 49420d5501c1SVlad Yasevich } 49430d5501c1SVlad Yasevich 4944f6548615SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN, 4945a6e18ff1SVlad Yasevich 2 * ETH_ALEN); 49460d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 49470d5501c1SVlad Yasevich return skb; 49480d5501c1SVlad Yasevich } 49490d5501c1SVlad Yasevich 49500d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 49510d5501c1SVlad Yasevich { 49520d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 49530d5501c1SVlad Yasevich u16 vlan_tci; 49540d5501c1SVlad Yasevich 4955df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 49560d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 49570d5501c1SVlad Yasevich return skb; 49580d5501c1SVlad Yasevich } 49590d5501c1SVlad Yasevich 49600d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 49610d5501c1SVlad Yasevich if (unlikely(!skb)) 49620d5501c1SVlad Yasevich goto err_free; 49630d5501c1SVlad Yasevich 49640d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 49650d5501c1SVlad Yasevich goto err_free; 49660d5501c1SVlad Yasevich 49670d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 49680d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 49690d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 49700d5501c1SVlad Yasevich 49710d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 49720d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 49730d5501c1SVlad Yasevich 49740d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 49750d5501c1SVlad Yasevich if (unlikely(!skb)) 49760d5501c1SVlad Yasevich goto err_free; 49770d5501c1SVlad Yasevich 49780d5501c1SVlad Yasevich skb_reset_network_header(skb); 49790d5501c1SVlad Yasevich skb_reset_transport_header(skb); 49800d5501c1SVlad Yasevich skb_reset_mac_len(skb); 49810d5501c1SVlad Yasevich 49820d5501c1SVlad Yasevich return skb; 49830d5501c1SVlad Yasevich 49840d5501c1SVlad Yasevich err_free: 49850d5501c1SVlad Yasevich kfree_skb(skb); 49860d5501c1SVlad Yasevich return NULL; 49870d5501c1SVlad Yasevich } 49880d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 49892e4e4410SEric Dumazet 4990e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 4991e2195121SJiri Pirko { 4992e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 4993e2195121SJiri Pirko return -ENOMEM; 4994e2195121SJiri Pirko 4995e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4996e2195121SJiri Pirko return 0; 4997e2195121SJiri Pirko 4998e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 4999e2195121SJiri Pirko } 5000e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 5001e2195121SJiri Pirko 5002bfca4c52SShmulik Ladkani /* remove VLAN header from packet and update csum accordingly. 5003bfca4c52SShmulik Ladkani * expects a non skb_vlan_tag_present skb with a vlan tag payload 5004bfca4c52SShmulik Ladkani */ 5005bfca4c52SShmulik Ladkani int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 500693515d53SJiri Pirko { 500793515d53SJiri Pirko struct vlan_hdr *vhdr; 5008b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 500993515d53SJiri Pirko int err; 501093515d53SJiri Pirko 5011b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5012b6a79208SShmulik Ladkani "__skb_vlan_pop got skb with skb->data not at mac header (offset %d)\n", 5013b6a79208SShmulik Ladkani offset)) { 5014b6a79208SShmulik Ladkani return -EINVAL; 5015b6a79208SShmulik Ladkani } 5016b6a79208SShmulik Ladkani 501793515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 501893515d53SJiri Pirko if (unlikely(err)) 5019b6a79208SShmulik Ladkani return err; 502093515d53SJiri Pirko 502193515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 502293515d53SJiri Pirko 502393515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 502493515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 502593515d53SJiri Pirko 502693515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 502793515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 502893515d53SJiri Pirko 502993515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 503093515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 503193515d53SJiri Pirko 503293515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 503393515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 503493515d53SJiri Pirko 503593515d53SJiri Pirko skb_reset_mac_len(skb); 503693515d53SJiri Pirko 503793515d53SJiri Pirko return err; 503893515d53SJiri Pirko } 5039bfca4c52SShmulik Ladkani EXPORT_SYMBOL(__skb_vlan_pop); 504093515d53SJiri Pirko 5041b6a79208SShmulik Ladkani /* Pop a vlan tag either from hwaccel or from payload. 5042b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5043b6a79208SShmulik Ladkani */ 504493515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 504593515d53SJiri Pirko { 504693515d53SJiri Pirko u16 vlan_tci; 504793515d53SJiri Pirko __be16 vlan_proto; 504893515d53SJiri Pirko int err; 504993515d53SJiri Pirko 5050df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 505193515d53SJiri Pirko skb->vlan_tci = 0; 505293515d53SJiri Pirko } else { 5053ecf4ee41SShmulik Ladkani if (unlikely(!eth_type_vlan(skb->protocol))) 505493515d53SJiri Pirko return 0; 505593515d53SJiri Pirko 505693515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 505793515d53SJiri Pirko if (err) 505893515d53SJiri Pirko return err; 505993515d53SJiri Pirko } 506093515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 5061ecf4ee41SShmulik Ladkani if (likely(!eth_type_vlan(skb->protocol))) 506293515d53SJiri Pirko return 0; 506393515d53SJiri Pirko 506493515d53SJiri Pirko vlan_proto = skb->protocol; 506593515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 506693515d53SJiri Pirko if (unlikely(err)) 506793515d53SJiri Pirko return err; 506893515d53SJiri Pirko 506993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 507093515d53SJiri Pirko return 0; 507193515d53SJiri Pirko } 507293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 507393515d53SJiri Pirko 5074b6a79208SShmulik Ladkani /* Push a vlan tag either into hwaccel or into payload (if hwaccel tag present). 5075b6a79208SShmulik Ladkani * Expects skb->data at mac header. 5076b6a79208SShmulik Ladkani */ 507793515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 507893515d53SJiri Pirko { 5079df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 5080b6a79208SShmulik Ladkani int offset = skb->data - skb_mac_header(skb); 508193515d53SJiri Pirko int err; 508293515d53SJiri Pirko 5083b6a79208SShmulik Ladkani if (WARN_ONCE(offset, 5084b6a79208SShmulik Ladkani "skb_vlan_push got skb with skb->data not at mac header (offset %d)\n", 5085b6a79208SShmulik Ladkani offset)) { 5086b6a79208SShmulik Ladkani return -EINVAL; 5087b6a79208SShmulik Ladkani } 5088b6a79208SShmulik Ladkani 508993515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 5090df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 5091b6a79208SShmulik Ladkani if (err) 509293515d53SJiri Pirko return err; 50939241e2dfSDaniel Borkmann 509493515d53SJiri Pirko skb->protocol = skb->vlan_proto; 509593515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 509693515d53SJiri Pirko 50976b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 509893515d53SJiri Pirko } 509993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 510093515d53SJiri Pirko return 0; 510193515d53SJiri Pirko } 510293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 510393515d53SJiri Pirko 51042e4e4410SEric Dumazet /** 51052e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 51062e4e4410SEric Dumazet * 5107de3f0d0eSMasanari Iida * @header_len: size of linear part 5108de3f0d0eSMasanari Iida * @data_len: needed length in frags 5109de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 5110de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 5111de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 51122e4e4410SEric Dumazet * 51132e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 51142e4e4410SEric Dumazet */ 51152e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 51162e4e4410SEric Dumazet unsigned long data_len, 51172e4e4410SEric Dumazet int max_page_order, 51182e4e4410SEric Dumazet int *errcode, 51192e4e4410SEric Dumazet gfp_t gfp_mask) 51202e4e4410SEric Dumazet { 51212e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 51222e4e4410SEric Dumazet unsigned long chunk; 51232e4e4410SEric Dumazet struct sk_buff *skb; 51242e4e4410SEric Dumazet struct page *page; 51252e4e4410SEric Dumazet gfp_t gfp_head; 51262e4e4410SEric Dumazet int i; 51272e4e4410SEric Dumazet 51282e4e4410SEric Dumazet *errcode = -EMSGSIZE; 51292e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 51302e4e4410SEric Dumazet * high order pages... 51312e4e4410SEric Dumazet */ 51322e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 51332e4e4410SEric Dumazet return NULL; 51342e4e4410SEric Dumazet 51352e4e4410SEric Dumazet gfp_head = gfp_mask; 5136d0164adcSMel Gorman if (gfp_head & __GFP_DIRECT_RECLAIM) 5137dcda9b04SMichal Hocko gfp_head |= __GFP_RETRY_MAYFAIL; 51382e4e4410SEric Dumazet 51392e4e4410SEric Dumazet *errcode = -ENOBUFS; 51402e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 51412e4e4410SEric Dumazet if (!skb) 51422e4e4410SEric Dumazet return NULL; 51432e4e4410SEric Dumazet 51442e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 51452e4e4410SEric Dumazet 51462e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 51472e4e4410SEric Dumazet int order = max_page_order; 51482e4e4410SEric Dumazet 51492e4e4410SEric Dumazet while (order) { 51502e4e4410SEric Dumazet if (npages >= 1 << order) { 5151d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 51522e4e4410SEric Dumazet __GFP_COMP | 51532e4e4410SEric Dumazet __GFP_NOWARN | 51542e4e4410SEric Dumazet __GFP_NORETRY, 51552e4e4410SEric Dumazet order); 51562e4e4410SEric Dumazet if (page) 51572e4e4410SEric Dumazet goto fill_page; 51582e4e4410SEric Dumazet /* Do not retry other high order allocations */ 51592e4e4410SEric Dumazet order = 1; 51602e4e4410SEric Dumazet max_page_order = 0; 51612e4e4410SEric Dumazet } 51622e4e4410SEric Dumazet order--; 51632e4e4410SEric Dumazet } 51642e4e4410SEric Dumazet page = alloc_page(gfp_mask); 51652e4e4410SEric Dumazet if (!page) 51662e4e4410SEric Dumazet goto failure; 51672e4e4410SEric Dumazet fill_page: 51682e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 51692e4e4410SEric Dumazet PAGE_SIZE << order); 51702e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 51712e4e4410SEric Dumazet data_len -= chunk; 51722e4e4410SEric Dumazet npages -= 1 << order; 51732e4e4410SEric Dumazet } 51742e4e4410SEric Dumazet return skb; 51752e4e4410SEric Dumazet 51762e4e4410SEric Dumazet failure: 51772e4e4410SEric Dumazet kfree_skb(skb); 51782e4e4410SEric Dumazet return NULL; 51792e4e4410SEric Dumazet } 51802e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 51816fa01ccdSSowmini Varadhan 51826fa01ccdSSowmini Varadhan /* carve out the first off bytes from skb when off < headlen */ 51836fa01ccdSSowmini Varadhan static int pskb_carve_inside_header(struct sk_buff *skb, const u32 off, 51846fa01ccdSSowmini Varadhan const int headlen, gfp_t gfp_mask) 51856fa01ccdSSowmini Varadhan { 51866fa01ccdSSowmini Varadhan int i; 51876fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 51886fa01ccdSSowmini Varadhan int new_hlen = headlen - off; 51896fa01ccdSSowmini Varadhan u8 *data; 51906fa01ccdSSowmini Varadhan 51916fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 51926fa01ccdSSowmini Varadhan 51936fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 51946fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 51956fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 51966fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 51976fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 51986fa01ccdSSowmini Varadhan if (!data) 51996fa01ccdSSowmini Varadhan return -ENOMEM; 52006fa01ccdSSowmini Varadhan 52016fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 52026fa01ccdSSowmini Varadhan 52036fa01ccdSSowmini Varadhan /* Copy real data, and all frags */ 52046fa01ccdSSowmini Varadhan skb_copy_from_linear_data_offset(skb, off, data, new_hlen); 52056fa01ccdSSowmini Varadhan skb->len -= off; 52066fa01ccdSSowmini Varadhan 52076fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 52086fa01ccdSSowmini Varadhan skb_shinfo(skb), 52096fa01ccdSSowmini Varadhan offsetof(struct skb_shared_info, 52106fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 52116fa01ccdSSowmini Varadhan if (skb_cloned(skb)) { 52126fa01ccdSSowmini Varadhan /* drop the old head gracefully */ 52136fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 52146fa01ccdSSowmini Varadhan kfree(data); 52156fa01ccdSSowmini Varadhan return -ENOMEM; 52166fa01ccdSSowmini Varadhan } 52176fa01ccdSSowmini Varadhan for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 52186fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 52196fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 52206fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 52216fa01ccdSSowmini Varadhan skb_release_data(skb); 52226fa01ccdSSowmini Varadhan } else { 52236fa01ccdSSowmini Varadhan /* we can reuse existing recount- all we did was 52246fa01ccdSSowmini Varadhan * relocate values 52256fa01ccdSSowmini Varadhan */ 52266fa01ccdSSowmini Varadhan skb_free_head(skb); 52276fa01ccdSSowmini Varadhan } 52286fa01ccdSSowmini Varadhan 52296fa01ccdSSowmini Varadhan skb->head = data; 52306fa01ccdSSowmini Varadhan skb->data = data; 52316fa01ccdSSowmini Varadhan skb->head_frag = 0; 52326fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 52336fa01ccdSSowmini Varadhan skb->end = size; 52346fa01ccdSSowmini Varadhan #else 52356fa01ccdSSowmini Varadhan skb->end = skb->head + size; 52366fa01ccdSSowmini Varadhan #endif 52376fa01ccdSSowmini Varadhan skb_set_tail_pointer(skb, skb_headlen(skb)); 52386fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 52396fa01ccdSSowmini Varadhan skb->cloned = 0; 52406fa01ccdSSowmini Varadhan skb->hdr_len = 0; 52416fa01ccdSSowmini Varadhan skb->nohdr = 0; 52426fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 52436fa01ccdSSowmini Varadhan 52446fa01ccdSSowmini Varadhan return 0; 52456fa01ccdSSowmini Varadhan } 52466fa01ccdSSowmini Varadhan 52476fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 off, gfp_t gfp); 52486fa01ccdSSowmini Varadhan 52496fa01ccdSSowmini Varadhan /* carve out the first eat bytes from skb's frag_list. May recurse into 52506fa01ccdSSowmini Varadhan * pskb_carve() 52516fa01ccdSSowmini Varadhan */ 52526fa01ccdSSowmini Varadhan static int pskb_carve_frag_list(struct sk_buff *skb, 52536fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo, int eat, 52546fa01ccdSSowmini Varadhan gfp_t gfp_mask) 52556fa01ccdSSowmini Varadhan { 52566fa01ccdSSowmini Varadhan struct sk_buff *list = shinfo->frag_list; 52576fa01ccdSSowmini Varadhan struct sk_buff *clone = NULL; 52586fa01ccdSSowmini Varadhan struct sk_buff *insp = NULL; 52596fa01ccdSSowmini Varadhan 52606fa01ccdSSowmini Varadhan do { 52616fa01ccdSSowmini Varadhan if (!list) { 52626fa01ccdSSowmini Varadhan pr_err("Not enough bytes to eat. Want %d\n", eat); 52636fa01ccdSSowmini Varadhan return -EFAULT; 52646fa01ccdSSowmini Varadhan } 52656fa01ccdSSowmini Varadhan if (list->len <= eat) { 52666fa01ccdSSowmini Varadhan /* Eaten as whole. */ 52676fa01ccdSSowmini Varadhan eat -= list->len; 52686fa01ccdSSowmini Varadhan list = list->next; 52696fa01ccdSSowmini Varadhan insp = list; 52706fa01ccdSSowmini Varadhan } else { 52716fa01ccdSSowmini Varadhan /* Eaten partially. */ 52726fa01ccdSSowmini Varadhan if (skb_shared(list)) { 52736fa01ccdSSowmini Varadhan clone = skb_clone(list, gfp_mask); 52746fa01ccdSSowmini Varadhan if (!clone) 52756fa01ccdSSowmini Varadhan return -ENOMEM; 52766fa01ccdSSowmini Varadhan insp = list->next; 52776fa01ccdSSowmini Varadhan list = clone; 52786fa01ccdSSowmini Varadhan } else { 52796fa01ccdSSowmini Varadhan /* This may be pulled without problems. */ 52806fa01ccdSSowmini Varadhan insp = list; 52816fa01ccdSSowmini Varadhan } 52826fa01ccdSSowmini Varadhan if (pskb_carve(list, eat, gfp_mask) < 0) { 52836fa01ccdSSowmini Varadhan kfree_skb(clone); 52846fa01ccdSSowmini Varadhan return -ENOMEM; 52856fa01ccdSSowmini Varadhan } 52866fa01ccdSSowmini Varadhan break; 52876fa01ccdSSowmini Varadhan } 52886fa01ccdSSowmini Varadhan } while (eat); 52896fa01ccdSSowmini Varadhan 52906fa01ccdSSowmini Varadhan /* Free pulled out fragments. */ 52916fa01ccdSSowmini Varadhan while ((list = shinfo->frag_list) != insp) { 52926fa01ccdSSowmini Varadhan shinfo->frag_list = list->next; 52936fa01ccdSSowmini Varadhan kfree_skb(list); 52946fa01ccdSSowmini Varadhan } 52956fa01ccdSSowmini Varadhan /* And insert new clone at head. */ 52966fa01ccdSSowmini Varadhan if (clone) { 52976fa01ccdSSowmini Varadhan clone->next = list; 52986fa01ccdSSowmini Varadhan shinfo->frag_list = clone; 52996fa01ccdSSowmini Varadhan } 53006fa01ccdSSowmini Varadhan return 0; 53016fa01ccdSSowmini Varadhan } 53026fa01ccdSSowmini Varadhan 53036fa01ccdSSowmini Varadhan /* carve off first len bytes from skb. Split line (off) is in the 53046fa01ccdSSowmini Varadhan * non-linear part of skb 53056fa01ccdSSowmini Varadhan */ 53066fa01ccdSSowmini Varadhan static int pskb_carve_inside_nonlinear(struct sk_buff *skb, const u32 off, 53076fa01ccdSSowmini Varadhan int pos, gfp_t gfp_mask) 53086fa01ccdSSowmini Varadhan { 53096fa01ccdSSowmini Varadhan int i, k = 0; 53106fa01ccdSSowmini Varadhan int size = skb_end_offset(skb); 53116fa01ccdSSowmini Varadhan u8 *data; 53126fa01ccdSSowmini Varadhan const int nfrags = skb_shinfo(skb)->nr_frags; 53136fa01ccdSSowmini Varadhan struct skb_shared_info *shinfo; 53146fa01ccdSSowmini Varadhan 53156fa01ccdSSowmini Varadhan size = SKB_DATA_ALIGN(size); 53166fa01ccdSSowmini Varadhan 53176fa01ccdSSowmini Varadhan if (skb_pfmemalloc(skb)) 53186fa01ccdSSowmini Varadhan gfp_mask |= __GFP_MEMALLOC; 53196fa01ccdSSowmini Varadhan data = kmalloc_reserve(size + 53206fa01ccdSSowmini Varadhan SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 53216fa01ccdSSowmini Varadhan gfp_mask, NUMA_NO_NODE, NULL); 53226fa01ccdSSowmini Varadhan if (!data) 53236fa01ccdSSowmini Varadhan return -ENOMEM; 53246fa01ccdSSowmini Varadhan 53256fa01ccdSSowmini Varadhan size = SKB_WITH_OVERHEAD(ksize(data)); 53266fa01ccdSSowmini Varadhan 53276fa01ccdSSowmini Varadhan memcpy((struct skb_shared_info *)(data + size), 53286fa01ccdSSowmini Varadhan skb_shinfo(skb), offsetof(struct skb_shared_info, 53296fa01ccdSSowmini Varadhan frags[skb_shinfo(skb)->nr_frags])); 53306fa01ccdSSowmini Varadhan if (skb_orphan_frags(skb, gfp_mask)) { 53316fa01ccdSSowmini Varadhan kfree(data); 53326fa01ccdSSowmini Varadhan return -ENOMEM; 53336fa01ccdSSowmini Varadhan } 53346fa01ccdSSowmini Varadhan shinfo = (struct skb_shared_info *)(data + size); 53356fa01ccdSSowmini Varadhan for (i = 0; i < nfrags; i++) { 53366fa01ccdSSowmini Varadhan int fsize = skb_frag_size(&skb_shinfo(skb)->frags[i]); 53376fa01ccdSSowmini Varadhan 53386fa01ccdSSowmini Varadhan if (pos + fsize > off) { 53396fa01ccdSSowmini Varadhan shinfo->frags[k] = skb_shinfo(skb)->frags[i]; 53406fa01ccdSSowmini Varadhan 53416fa01ccdSSowmini Varadhan if (pos < off) { 53426fa01ccdSSowmini Varadhan /* Split frag. 53436fa01ccdSSowmini Varadhan * We have two variants in this case: 53446fa01ccdSSowmini Varadhan * 1. Move all the frag to the second 53456fa01ccdSSowmini Varadhan * part, if it is possible. F.e. 53466fa01ccdSSowmini Varadhan * this approach is mandatory for TUX, 53476fa01ccdSSowmini Varadhan * where splitting is expensive. 53486fa01ccdSSowmini Varadhan * 2. Split is accurately. We make this. 53496fa01ccdSSowmini Varadhan */ 53506fa01ccdSSowmini Varadhan shinfo->frags[0].page_offset += off - pos; 53516fa01ccdSSowmini Varadhan skb_frag_size_sub(&shinfo->frags[0], off - pos); 53526fa01ccdSSowmini Varadhan } 53536fa01ccdSSowmini Varadhan skb_frag_ref(skb, i); 53546fa01ccdSSowmini Varadhan k++; 53556fa01ccdSSowmini Varadhan } 53566fa01ccdSSowmini Varadhan pos += fsize; 53576fa01ccdSSowmini Varadhan } 53586fa01ccdSSowmini Varadhan shinfo->nr_frags = k; 53596fa01ccdSSowmini Varadhan if (skb_has_frag_list(skb)) 53606fa01ccdSSowmini Varadhan skb_clone_fraglist(skb); 53616fa01ccdSSowmini Varadhan 53626fa01ccdSSowmini Varadhan if (k == 0) { 53636fa01ccdSSowmini Varadhan /* split line is in frag list */ 53646fa01ccdSSowmini Varadhan pskb_carve_frag_list(skb, shinfo, off - pos, gfp_mask); 53656fa01ccdSSowmini Varadhan } 53666fa01ccdSSowmini Varadhan skb_release_data(skb); 53676fa01ccdSSowmini Varadhan 53686fa01ccdSSowmini Varadhan skb->head = data; 53696fa01ccdSSowmini Varadhan skb->head_frag = 0; 53706fa01ccdSSowmini Varadhan skb->data = data; 53716fa01ccdSSowmini Varadhan #ifdef NET_SKBUFF_DATA_USES_OFFSET 53726fa01ccdSSowmini Varadhan skb->end = size; 53736fa01ccdSSowmini Varadhan #else 53746fa01ccdSSowmini Varadhan skb->end = skb->head + size; 53756fa01ccdSSowmini Varadhan #endif 53766fa01ccdSSowmini Varadhan skb_reset_tail_pointer(skb); 53776fa01ccdSSowmini Varadhan skb_headers_offset_update(skb, 0); 53786fa01ccdSSowmini Varadhan skb->cloned = 0; 53796fa01ccdSSowmini Varadhan skb->hdr_len = 0; 53806fa01ccdSSowmini Varadhan skb->nohdr = 0; 53816fa01ccdSSowmini Varadhan skb->len -= off; 53826fa01ccdSSowmini Varadhan skb->data_len = skb->len; 53836fa01ccdSSowmini Varadhan atomic_set(&skb_shinfo(skb)->dataref, 1); 53846fa01ccdSSowmini Varadhan return 0; 53856fa01ccdSSowmini Varadhan } 53866fa01ccdSSowmini Varadhan 53876fa01ccdSSowmini Varadhan /* remove len bytes from the beginning of the skb */ 53886fa01ccdSSowmini Varadhan static int pskb_carve(struct sk_buff *skb, const u32 len, gfp_t gfp) 53896fa01ccdSSowmini Varadhan { 53906fa01ccdSSowmini Varadhan int headlen = skb_headlen(skb); 53916fa01ccdSSowmini Varadhan 53926fa01ccdSSowmini Varadhan if (len < headlen) 53936fa01ccdSSowmini Varadhan return pskb_carve_inside_header(skb, len, headlen, gfp); 53946fa01ccdSSowmini Varadhan else 53956fa01ccdSSowmini Varadhan return pskb_carve_inside_nonlinear(skb, len, headlen, gfp); 53966fa01ccdSSowmini Varadhan } 53976fa01ccdSSowmini Varadhan 53986fa01ccdSSowmini Varadhan /* Extract to_copy bytes starting at off from skb, and return this in 53996fa01ccdSSowmini Varadhan * a new skb 54006fa01ccdSSowmini Varadhan */ 54016fa01ccdSSowmini Varadhan struct sk_buff *pskb_extract(struct sk_buff *skb, int off, 54026fa01ccdSSowmini Varadhan int to_copy, gfp_t gfp) 54036fa01ccdSSowmini Varadhan { 54046fa01ccdSSowmini Varadhan struct sk_buff *clone = skb_clone(skb, gfp); 54056fa01ccdSSowmini Varadhan 54066fa01ccdSSowmini Varadhan if (!clone) 54076fa01ccdSSowmini Varadhan return NULL; 54086fa01ccdSSowmini Varadhan 54096fa01ccdSSowmini Varadhan if (pskb_carve(clone, off, gfp) < 0 || 54106fa01ccdSSowmini Varadhan pskb_trim(clone, to_copy)) { 54116fa01ccdSSowmini Varadhan kfree_skb(clone); 54126fa01ccdSSowmini Varadhan return NULL; 54136fa01ccdSSowmini Varadhan } 54146fa01ccdSSowmini Varadhan return clone; 54156fa01ccdSSowmini Varadhan } 54166fa01ccdSSowmini Varadhan EXPORT_SYMBOL(pskb_extract); 5417c8c8b127SEric Dumazet 5418c8c8b127SEric Dumazet /** 5419c8c8b127SEric Dumazet * skb_condense - try to get rid of fragments/frag_list if possible 5420c8c8b127SEric Dumazet * @skb: buffer 5421c8c8b127SEric Dumazet * 5422c8c8b127SEric Dumazet * Can be used to save memory before skb is added to a busy queue. 5423c8c8b127SEric Dumazet * If packet has bytes in frags and enough tail room in skb->head, 5424c8c8b127SEric Dumazet * pull all of them, so that we can free the frags right now and adjust 5425c8c8b127SEric Dumazet * truesize. 5426c8c8b127SEric Dumazet * Notes: 5427c8c8b127SEric Dumazet * We do not reallocate skb->head thus can not fail. 5428c8c8b127SEric Dumazet * Caller must re-evaluate skb->truesize if needed. 5429c8c8b127SEric Dumazet */ 5430c8c8b127SEric Dumazet void skb_condense(struct sk_buff *skb) 5431c8c8b127SEric Dumazet { 54323174fed9SEric Dumazet if (skb->data_len) { 54333174fed9SEric Dumazet if (skb->data_len > skb->end - skb->tail || 5434c8c8b127SEric Dumazet skb_cloned(skb)) 5435c8c8b127SEric Dumazet return; 5436c8c8b127SEric Dumazet 5437c8c8b127SEric Dumazet /* Nice, we can free page frag(s) right now */ 5438c8c8b127SEric Dumazet __pskb_pull_tail(skb, skb->data_len); 54393174fed9SEric Dumazet } 54403174fed9SEric Dumazet /* At this point, skb->truesize might be over estimated, 54413174fed9SEric Dumazet * because skb had a fragment, and fragments do not tell 54423174fed9SEric Dumazet * their truesize. 54433174fed9SEric Dumazet * When we pulled its content into skb->head, fragment 54443174fed9SEric Dumazet * was freed, but __pskb_pull_tail() could not possibly 54453174fed9SEric Dumazet * adjust skb->truesize, not knowing the frag truesize. 5446c8c8b127SEric Dumazet */ 5447c8c8b127SEric Dumazet skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); 5448c8c8b127SEric Dumazet } 5449