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> 521da177e4SLinus Torvalds #include <linux/netdevice.h> 531da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 541da177e4SLinus Torvalds #include <net/pkt_sched.h> 551da177e4SLinus Torvalds #endif 561da177e4SLinus Torvalds #include <linux/string.h> 571da177e4SLinus Torvalds #include <linux/skbuff.h> 589c55e01cSJens Axboe #include <linux/splice.h> 591da177e4SLinus Torvalds #include <linux/cache.h> 601da177e4SLinus Torvalds #include <linux/rtnetlink.h> 611da177e4SLinus Torvalds #include <linux/init.h> 62716ea3a7SDavid Howells #include <linux/scatterlist.h> 63ac45f602SPatrick Ohly #include <linux/errqueue.h> 64268bb0ceSLinus Torvalds #include <linux/prefetch.h> 650d5501c1SVlad Yasevich #include <linux/if_vlan.h> 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds #include <net/protocol.h> 681da177e4SLinus Torvalds #include <net/dst.h> 691da177e4SLinus Torvalds #include <net/sock.h> 701da177e4SLinus Torvalds #include <net/checksum.h> 71ed1f50c3SPaul Durrant #include <net/ip6_checksum.h> 721da177e4SLinus Torvalds #include <net/xfrm.h> 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds #include <asm/uaccess.h> 75ad8d75ffSSteven Rostedt #include <trace/events/skb.h> 7651c56b00SEric Dumazet #include <linux/highmem.h> 77a1f8e7f7SAl Viro 78d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly; 79e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /** 82f05de73bSJean Sacren * skb_panic - private function for out-of-line support 831da177e4SLinus Torvalds * @skb: buffer 841da177e4SLinus Torvalds * @sz: size 85f05de73bSJean Sacren * @addr: address 8699d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 871da177e4SLinus Torvalds * 88f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 89f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 90f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 91f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 921da177e4SLinus Torvalds */ 93f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 9499d5851eSJames Hogan const char msg[]) 951da177e4SLinus Torvalds { 96e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 9799d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 984305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 9926095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1001da177e4SLinus Torvalds BUG(); 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 103f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1041da177e4SLinus Torvalds { 105f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 108f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 109f05de73bSJean Sacren { 110f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 111f05de73bSJean Sacren } 112c93bdd0eSMel Gorman 113c93bdd0eSMel Gorman /* 114c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 115c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 116c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 117c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 118c93bdd0eSMel Gorman * memory is free 119c93bdd0eSMel Gorman */ 120c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 121c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 12261c5e88aSstephen hemminger 12361c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 12461c5e88aSstephen hemminger unsigned long ip, bool *pfmemalloc) 125c93bdd0eSMel Gorman { 126c93bdd0eSMel Gorman void *obj; 127c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 128c93bdd0eSMel Gorman 129c93bdd0eSMel Gorman /* 130c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 131c93bdd0eSMel Gorman * to the reserves, fail. 132c93bdd0eSMel Gorman */ 133c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 134c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 135c93bdd0eSMel Gorman node); 136c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 137c93bdd0eSMel Gorman goto out; 138c93bdd0eSMel Gorman 139c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 140c93bdd0eSMel Gorman ret_pfmemalloc = true; 141c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 142c93bdd0eSMel Gorman 143c93bdd0eSMel Gorman out: 144c93bdd0eSMel Gorman if (pfmemalloc) 145c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 146c93bdd0eSMel Gorman 147c93bdd0eSMel Gorman return obj; 148c93bdd0eSMel Gorman } 149c93bdd0eSMel Gorman 1501da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1511da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1521da177e4SLinus Torvalds * [BEEP] leaks. 1531da177e4SLinus Torvalds * 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds 1560ebd0ac5SPatrick McHardy struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node) 1570ebd0ac5SPatrick McHardy { 1580ebd0ac5SPatrick McHardy struct sk_buff *skb; 1590ebd0ac5SPatrick McHardy 1600ebd0ac5SPatrick McHardy /* Get the HEAD */ 1610ebd0ac5SPatrick McHardy skb = kmem_cache_alloc_node(skbuff_head_cache, 1620ebd0ac5SPatrick McHardy gfp_mask & ~__GFP_DMA, node); 1630ebd0ac5SPatrick McHardy if (!skb) 1640ebd0ac5SPatrick McHardy goto out; 1650ebd0ac5SPatrick McHardy 1660ebd0ac5SPatrick McHardy /* 1670ebd0ac5SPatrick McHardy * Only clear those fields we need to clear, not those that we will 1680ebd0ac5SPatrick McHardy * actually initialise below. Hence, don't put any more fields after 1690ebd0ac5SPatrick McHardy * the tail pointer in struct sk_buff! 1700ebd0ac5SPatrick McHardy */ 1710ebd0ac5SPatrick McHardy memset(skb, 0, offsetof(struct sk_buff, tail)); 1725e71d9d7SPablo Neira skb->head = NULL; 1730ebd0ac5SPatrick McHardy skb->truesize = sizeof(struct sk_buff); 1740ebd0ac5SPatrick McHardy atomic_set(&skb->users, 1); 1750ebd0ac5SPatrick McHardy 17635d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 1770ebd0ac5SPatrick McHardy out: 1780ebd0ac5SPatrick McHardy return skb; 1790ebd0ac5SPatrick McHardy } 1800ebd0ac5SPatrick McHardy 1811da177e4SLinus Torvalds /** 182d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1831da177e4SLinus Torvalds * @size: size to allocate 1841da177e4SLinus Torvalds * @gfp_mask: allocation mask 185c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 186c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 187c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 188c93bdd0eSMel Gorman * allocations in case the data is required for writeback 189b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1901da177e4SLinus Torvalds * 1911da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 19294b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 19394b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 1941da177e4SLinus Torvalds * 1951da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1961da177e4SLinus Torvalds * %GFP_ATOMIC. 1971da177e4SLinus Torvalds */ 198dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 199c93bdd0eSMel Gorman int flags, int node) 2001da177e4SLinus Torvalds { 201e18b890bSChristoph Lameter struct kmem_cache *cache; 2024947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 2031da177e4SLinus Torvalds struct sk_buff *skb; 2041da177e4SLinus Torvalds u8 *data; 205c93bdd0eSMel Gorman bool pfmemalloc; 2061da177e4SLinus Torvalds 207c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 208c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 209c93bdd0eSMel Gorman 210c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 211c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 2128798b3fbSHerbert Xu 2131da177e4SLinus Torvalds /* Get the HEAD */ 214b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 2151da177e4SLinus Torvalds if (!skb) 2161da177e4SLinus Torvalds goto out; 217ec7d2f2cSEric Dumazet prefetchw(skb); 2181da177e4SLinus Torvalds 21987fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 22087fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 22187fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 22287fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 22387fb4b7bSEric Dumazet */ 224bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 22587fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 226c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2271da177e4SLinus Torvalds if (!data) 2281da177e4SLinus Torvalds goto nodata; 22987fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 23087fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 23187fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 23287fb4b7bSEric Dumazet */ 23387fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 234ec7d2f2cSEric Dumazet prefetchw(data + size); 2351da177e4SLinus Torvalds 236ca0605a7SArnaldo Carvalho de Melo /* 237c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 238c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 239c8005785SJohannes Berg * the tail pointer in struct sk_buff! 240ca0605a7SArnaldo Carvalho de Melo */ 241ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 24287fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 24387fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 244c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 2451da177e4SLinus Torvalds atomic_set(&skb->users, 1); 2461da177e4SLinus Torvalds skb->head = data; 2471da177e4SLinus Torvalds skb->data = data; 24827a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2494305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 25035d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 25135d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 25219633e12SStephen Hemminger 2534947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2544947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 255ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2564947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 257c2aa3665SEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 2584947d3efSBenjamin LaHaise 259c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 260d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 2611da177e4SLinus Torvalds 262d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 263d0bf4a9eSEric Dumazet 264d0bf4a9eSEric Dumazet kmemcheck_annotate_bitfield(&fclones->skb2, flags1); 265d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 266d0bf4a9eSEric Dumazet atomic_set(&fclones->fclone_ref, 1); 267d179cd12SDavid S. Miller 2686ffe75ebSEric Dumazet fclones->skb2.fclone = SKB_FCLONE_CLONE; 269d0bf4a9eSEric Dumazet fclones->skb2.pfmemalloc = pfmemalloc; 270d179cd12SDavid S. Miller } 2711da177e4SLinus Torvalds out: 2721da177e4SLinus Torvalds return skb; 2731da177e4SLinus Torvalds nodata: 2748798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2751da177e4SLinus Torvalds skb = NULL; 2761da177e4SLinus Torvalds goto out; 2771da177e4SLinus Torvalds } 278b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /** 281b2b5ce9dSEric Dumazet * build_skb - build a network buffer 282b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 283d3836f21SEric Dumazet * @frag_size: size of fragment, or 0 if head was kmalloced 284b2b5ce9dSEric Dumazet * 285b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 286deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 287deceb4c0SFlorian Fainelli * @frag_size is 0, otherwise data should come from the page allocator. 288b2b5ce9dSEric Dumazet * The return is the new skb buffer. 289b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 290b2b5ce9dSEric Dumazet * Notes : 291b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 292b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 293b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 294b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 295b2b5ce9dSEric Dumazet * before giving packet to stack. 296b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 297b2b5ce9dSEric Dumazet */ 298d3836f21SEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 299b2b5ce9dSEric Dumazet { 300b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 301b2b5ce9dSEric Dumazet struct sk_buff *skb; 302d3836f21SEric Dumazet unsigned int size = frag_size ? : ksize(data); 303b2b5ce9dSEric Dumazet 304b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 305b2b5ce9dSEric Dumazet if (!skb) 306b2b5ce9dSEric Dumazet return NULL; 307b2b5ce9dSEric Dumazet 308d3836f21SEric Dumazet size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 309b2b5ce9dSEric Dumazet 310b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 311b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 312d3836f21SEric Dumazet skb->head_frag = frag_size != 0; 313b2b5ce9dSEric Dumazet atomic_set(&skb->users, 1); 314b2b5ce9dSEric Dumazet skb->head = data; 315b2b5ce9dSEric Dumazet skb->data = data; 316b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 317b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 31835d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 31935d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 320b2b5ce9dSEric Dumazet 321b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 322b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 323b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 324b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 325b2b5ce9dSEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 326b2b5ce9dSEric Dumazet 327b2b5ce9dSEric Dumazet return skb; 328b2b5ce9dSEric Dumazet } 329b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 330b2b5ce9dSEric Dumazet 331a1c7fff7SEric Dumazet struct netdev_alloc_cache { 33269b08f62SEric Dumazet struct page_frag frag; 33369b08f62SEric Dumazet /* we maintain a pagecount bias, so that we dont dirty cache line 33469b08f62SEric Dumazet * containing page->_count every time we allocate a fragment. 33569b08f62SEric Dumazet */ 336540eb7bfSAlexander Duyck unsigned int pagecnt_bias; 337a1c7fff7SEric Dumazet }; 338a1c7fff7SEric Dumazet static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); 339ffde7328SAlexander Duyck static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache); 340a1c7fff7SEric Dumazet 341ffde7328SAlexander Duyck static struct page *__page_frag_refill(struct netdev_alloc_cache *nc, 342ffde7328SAlexander Duyck gfp_t gfp_mask) 3436f532612SEric Dumazet { 344ffde7328SAlexander Duyck const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER; 345ffde7328SAlexander Duyck struct page *page = NULL; 34669b08f62SEric Dumazet gfp_t gfp = gfp_mask; 34769b08f62SEric Dumazet 348ffde7328SAlexander Duyck if (order) { 349ffde7328SAlexander Duyck gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY; 350ffde7328SAlexander Duyck page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order); 351ffde7328SAlexander Duyck nc->frag.size = PAGE_SIZE << (page ? order : 0); 35269b08f62SEric Dumazet } 353ffde7328SAlexander Duyck 354ffde7328SAlexander Duyck if (unlikely(!page)) 355ffde7328SAlexander Duyck page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 356ffde7328SAlexander Duyck 357ffde7328SAlexander Duyck nc->frag.page = page; 358ffde7328SAlexander Duyck 359ffde7328SAlexander Duyck return page; 360ffde7328SAlexander Duyck } 361ffde7328SAlexander Duyck 362ffde7328SAlexander Duyck static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache, 363ffde7328SAlexander Duyck unsigned int fragsz, gfp_t gfp_mask) 364ffde7328SAlexander Duyck { 365ffde7328SAlexander Duyck struct netdev_alloc_cache *nc = this_cpu_ptr(cache); 366ffde7328SAlexander Duyck struct page *page = nc->frag.page; 367ffde7328SAlexander Duyck unsigned int size; 368ffde7328SAlexander Duyck int offset; 369ffde7328SAlexander Duyck 370ffde7328SAlexander Duyck if (unlikely(!page)) { 371ffde7328SAlexander Duyck refill: 372ffde7328SAlexander Duyck page = __page_frag_refill(nc, gfp_mask); 373ffde7328SAlexander Duyck if (!page) 374ffde7328SAlexander Duyck return NULL; 375ffde7328SAlexander Duyck 376ffde7328SAlexander Duyck /* if size can vary use frag.size else just use PAGE_SIZE */ 377ffde7328SAlexander Duyck size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; 378ffde7328SAlexander Duyck 3794c450583SEric Dumazet /* Even if we own the page, we do not use atomic_set(). 3804c450583SEric Dumazet * This would break get_page_unless_zero() users. 3814c450583SEric Dumazet */ 382ffde7328SAlexander Duyck atomic_add(size - 1, &page->_count); 383ffde7328SAlexander Duyck 384ffde7328SAlexander Duyck /* reset page count bias and offset to start of new frag */ 385ffde7328SAlexander Duyck nc->pagecnt_bias = size; 386ffde7328SAlexander Duyck nc->frag.offset = size; 3876f532612SEric Dumazet } 388540eb7bfSAlexander Duyck 389ffde7328SAlexander Duyck offset = nc->frag.offset - fragsz; 390ffde7328SAlexander Duyck if (unlikely(offset < 0)) { 391ffde7328SAlexander Duyck if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count)) 3926f532612SEric Dumazet goto refill; 393ffde7328SAlexander Duyck 394ffde7328SAlexander Duyck /* if size can vary use frag.size else just use PAGE_SIZE */ 395ffde7328SAlexander Duyck size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; 396ffde7328SAlexander Duyck 3974c450583SEric Dumazet /* OK, page count is 0, we can safely set it */ 398ffde7328SAlexander Duyck atomic_set(&page->_count, size); 399ffde7328SAlexander Duyck 400ffde7328SAlexander Duyck /* reset page count bias and offset to start of new frag */ 401ffde7328SAlexander Duyck nc->pagecnt_bias = size; 402ffde7328SAlexander Duyck offset = size - fragsz; 4036f532612SEric Dumazet } 404540eb7bfSAlexander Duyck 405540eb7bfSAlexander Duyck nc->pagecnt_bias--; 406ffde7328SAlexander Duyck nc->frag.offset = offset; 407ffde7328SAlexander Duyck 408ffde7328SAlexander Duyck return page_address(page) + offset; 409ffde7328SAlexander Duyck } 410ffde7328SAlexander Duyck 411ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 412ffde7328SAlexander Duyck { 413ffde7328SAlexander Duyck unsigned long flags; 414ffde7328SAlexander Duyck void *data; 415ffde7328SAlexander Duyck 416ffde7328SAlexander Duyck local_irq_save(flags); 417ffde7328SAlexander Duyck data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask); 4186f532612SEric Dumazet local_irq_restore(flags); 4196f532612SEric Dumazet return data; 4206f532612SEric Dumazet } 421c93bdd0eSMel Gorman 422c93bdd0eSMel Gorman /** 423c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 424c93bdd0eSMel Gorman * @fragsz: fragment size 425c93bdd0eSMel Gorman * 426c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 427c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 428c93bdd0eSMel Gorman */ 429c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 430c93bdd0eSMel Gorman { 431c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 432c93bdd0eSMel Gorman } 4336f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 4346f532612SEric Dumazet 435ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 436ffde7328SAlexander Duyck { 437ffde7328SAlexander Duyck return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask); 438ffde7328SAlexander Duyck } 439ffde7328SAlexander Duyck 440ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 441ffde7328SAlexander Duyck { 442ffde7328SAlexander Duyck return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 443ffde7328SAlexander Duyck } 444ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 445ffde7328SAlexander Duyck 4466f532612SEric Dumazet /** 447fd11a83dSAlexander Duyck * __alloc_rx_skb - allocate an skbuff for rx 4488af27456SChristoph Hellwig * @length: length to allocate 4498af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 450fd11a83dSAlexander Duyck * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 451fd11a83dSAlexander Duyck * allocations in case we have to fallback to __alloc_skb() 452fd11a83dSAlexander Duyck * If SKB_ALLOC_NAPI is set, page fragment will be allocated 453fd11a83dSAlexander Duyck * from napi_cache instead of netdev_cache. 4548af27456SChristoph Hellwig * 4558af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 4568af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 4578af27456SChristoph Hellwig * the headroom they think they need without accounting for the 4588af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 4598af27456SChristoph Hellwig * 4608af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 4618af27456SChristoph Hellwig */ 462fd11a83dSAlexander Duyck static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask, 463fd11a83dSAlexander Duyck int flags) 4648af27456SChristoph Hellwig { 4656f532612SEric Dumazet struct sk_buff *skb = NULL; 466fd11a83dSAlexander Duyck unsigned int fragsz = SKB_DATA_ALIGN(length) + 467a1c7fff7SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4688af27456SChristoph Hellwig 469310e158cSEric Dumazet if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { 470c93bdd0eSMel Gorman void *data; 471c93bdd0eSMel Gorman 472c93bdd0eSMel Gorman if (sk_memalloc_socks()) 473c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 474c93bdd0eSMel Gorman 475fd11a83dSAlexander Duyck data = (flags & SKB_ALLOC_NAPI) ? 476fd11a83dSAlexander Duyck __napi_alloc_frag(fragsz, gfp_mask) : 477fd11a83dSAlexander Duyck __netdev_alloc_frag(fragsz, gfp_mask); 478a1c7fff7SEric Dumazet 4796f532612SEric Dumazet if (likely(data)) { 4806f532612SEric Dumazet skb = build_skb(data, fragsz); 4816f532612SEric Dumazet if (unlikely(!skb)) 4826f532612SEric Dumazet put_page(virt_to_head_page(data)); 483a1c7fff7SEric Dumazet } 484a1c7fff7SEric Dumazet } else { 485fd11a83dSAlexander Duyck skb = __alloc_skb(length, gfp_mask, 486c93bdd0eSMel Gorman SKB_ALLOC_RX, NUMA_NO_NODE); 487a1c7fff7SEric Dumazet } 488fd11a83dSAlexander Duyck return skb; 489fd11a83dSAlexander Duyck } 490fd11a83dSAlexander Duyck 491fd11a83dSAlexander Duyck /** 492fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 493fd11a83dSAlexander Duyck * @dev: network device to receive on 494fd11a83dSAlexander Duyck * @length: length to allocate 495fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 496fd11a83dSAlexander Duyck * 497fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 498fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 499fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 500fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 501fd11a83dSAlexander Duyck * 502fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 503fd11a83dSAlexander Duyck */ 504fd11a83dSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 505fd11a83dSAlexander Duyck unsigned int length, gfp_t gfp_mask) 506fd11a83dSAlexander Duyck { 507fd11a83dSAlexander Duyck struct sk_buff *skb; 508fd11a83dSAlexander Duyck 509fd11a83dSAlexander Duyck length += NET_SKB_PAD; 510fd11a83dSAlexander Duyck skb = __alloc_rx_skb(length, gfp_mask, 0); 511fd11a83dSAlexander Duyck 5127b2e497aSChristoph Hellwig if (likely(skb)) { 5138af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 5147b2e497aSChristoph Hellwig skb->dev = dev; 5157b2e497aSChristoph Hellwig } 516fd11a83dSAlexander Duyck 5178af27456SChristoph Hellwig return skb; 5188af27456SChristoph Hellwig } 519b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 5201da177e4SLinus Torvalds 521fd11a83dSAlexander Duyck /** 522fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 523fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 524fd11a83dSAlexander Duyck * @length: length to allocate 525fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 526fd11a83dSAlexander Duyck * 527fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 528fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 529fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 530fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 531fd11a83dSAlexander Duyck * 532fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 533fd11a83dSAlexander Duyck */ 534fd11a83dSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, 535fd11a83dSAlexander Duyck unsigned int length, gfp_t gfp_mask) 536fd11a83dSAlexander Duyck { 537fd11a83dSAlexander Duyck struct sk_buff *skb; 538fd11a83dSAlexander Duyck 539fd11a83dSAlexander Duyck length += NET_SKB_PAD + NET_IP_ALIGN; 540fd11a83dSAlexander Duyck skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI); 541fd11a83dSAlexander Duyck 542fd11a83dSAlexander Duyck if (likely(skb)) { 543fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 544fd11a83dSAlexander Duyck skb->dev = napi->dev; 545fd11a83dSAlexander Duyck } 546fd11a83dSAlexander Duyck 547fd11a83dSAlexander Duyck return skb; 548fd11a83dSAlexander Duyck } 549fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 550fd11a83dSAlexander Duyck 551654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 55250269e19SEric Dumazet int size, unsigned int truesize) 553654bed16SPeter Zijlstra { 554654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 555654bed16SPeter Zijlstra skb->len += size; 556654bed16SPeter Zijlstra skb->data_len += size; 55750269e19SEric Dumazet skb->truesize += truesize; 558654bed16SPeter Zijlstra } 559654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 560654bed16SPeter Zijlstra 561f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 562f8e617e1SJason Wang unsigned int truesize) 563f8e617e1SJason Wang { 564f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 565f8e617e1SJason Wang 566f8e617e1SJason Wang skb_frag_size_add(frag, size); 567f8e617e1SJason Wang skb->len += size; 568f8e617e1SJason Wang skb->data_len += size; 569f8e617e1SJason Wang skb->truesize += truesize; 570f8e617e1SJason Wang } 571f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 572f8e617e1SJason Wang 57327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5741da177e4SLinus Torvalds { 575bd8a7036SEric Dumazet kfree_skb_list(*listp); 57627b437c8SHerbert Xu *listp = NULL; 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds 57927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 58027b437c8SHerbert Xu { 58127b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 58227b437c8SHerbert Xu } 58327b437c8SHerbert Xu 5841da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5851da177e4SLinus Torvalds { 5861da177e4SLinus Torvalds struct sk_buff *list; 5871da177e4SLinus Torvalds 588fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5891da177e4SLinus Torvalds skb_get(list); 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 593d3836f21SEric Dumazet { 594d3836f21SEric Dumazet if (skb->head_frag) 595d3836f21SEric Dumazet put_page(virt_to_head_page(skb->head)); 596d3836f21SEric Dumazet else 597d3836f21SEric Dumazet kfree(skb->head); 598d3836f21SEric Dumazet } 599d3836f21SEric Dumazet 6005bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 6011da177e4SLinus Torvalds { 602ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 6031da177e4SLinus Torvalds int i; 604ff04a771SEric Dumazet 605ff04a771SEric Dumazet if (skb->cloned && 606ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 607ff04a771SEric Dumazet &shinfo->dataref)) 608ff04a771SEric Dumazet return; 609ff04a771SEric Dumazet 610ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 611ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 6121da177e4SLinus Torvalds 613a6686f2fSShirley Ma /* 614a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 615a6686f2fSShirley Ma * the lower device DMA has done; 616a6686f2fSShirley Ma */ 617ff04a771SEric Dumazet if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { 618a6686f2fSShirley Ma struct ubuf_info *uarg; 619a6686f2fSShirley Ma 620ff04a771SEric Dumazet uarg = shinfo->destructor_arg; 621a6686f2fSShirley Ma if (uarg->callback) 622e19d6763SMichael S. Tsirkin uarg->callback(uarg, true); 623a6686f2fSShirley Ma } 624a6686f2fSShirley Ma 625ff04a771SEric Dumazet if (shinfo->frag_list) 626ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6271da177e4SLinus Torvalds 628d3836f21SEric Dumazet skb_free_head(skb); 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds /* 6321da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6331da177e4SLinus Torvalds */ 6342d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6351da177e4SLinus Torvalds { 636d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 637d179cd12SDavid S. Miller 638d179cd12SDavid S. Miller switch (skb->fclone) { 639d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6401da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 6416ffe75ebSEric Dumazet return; 642d179cd12SDavid S. Miller 643d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 644d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 6456ffe75ebSEric Dumazet 6466ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 6476ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 6486ffe75ebSEric Dumazet * while here, branch prediction will be good. 6496ffe75ebSEric Dumazet */ 6506ffe75ebSEric Dumazet if (atomic_read(&fclones->fclone_ref) == 1) 6516ffe75ebSEric Dumazet goto fastpath; 652d179cd12SDavid S. Miller break; 653d179cd12SDavid S. Miller 6546ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 655d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 656d179cd12SDavid S. Miller break; 6573ff50b79SStephen Hemminger } 6586ffe75ebSEric Dumazet if (!atomic_dec_and_test(&fclones->fclone_ref)) 6596ffe75ebSEric Dumazet return; 6606ffe75ebSEric Dumazet fastpath: 6616ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 66404a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 6651da177e4SLinus Torvalds { 666adf30907SEric Dumazet skb_dst_drop(skb); 6671da177e4SLinus Torvalds #ifdef CONFIG_XFRM 6681da177e4SLinus Torvalds secpath_put(skb->sp); 6691da177e4SLinus Torvalds #endif 6701da177e4SLinus Torvalds if (skb->destructor) { 6719c2b3328SStephen Hemminger WARN_ON(in_irq()); 6721da177e4SLinus Torvalds skb->destructor(skb); 6731da177e4SLinus Torvalds } 674a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 6755f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 6762fc72c7bSKOVACS Krisztian #endif 6771109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 6781da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 6791da177e4SLinus Torvalds #endif 68004a4bb55SLennert Buytenhek } 68104a4bb55SLennert Buytenhek 68204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 68304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 68404a4bb55SLennert Buytenhek { 68504a4bb55SLennert Buytenhek skb_release_head_state(skb); 6865e71d9d7SPablo Neira if (likely(skb->head)) 6872d4baff8SHerbert Xu skb_release_data(skb); 6882d4baff8SHerbert Xu } 6891da177e4SLinus Torvalds 6902d4baff8SHerbert Xu /** 6912d4baff8SHerbert Xu * __kfree_skb - private function 6922d4baff8SHerbert Xu * @skb: buffer 6932d4baff8SHerbert Xu * 6942d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6952d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6962d4baff8SHerbert Xu * always call kfree_skb 6972d4baff8SHerbert Xu */ 6982d4baff8SHerbert Xu 6992d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 7002d4baff8SHerbert Xu { 7012d4baff8SHerbert Xu skb_release_all(skb); 7021da177e4SLinus Torvalds kfree_skbmem(skb); 7031da177e4SLinus Torvalds } 704b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds /** 707231d06aeSJörn Engel * kfree_skb - free an sk_buff 708231d06aeSJörn Engel * @skb: buffer to free 709231d06aeSJörn Engel * 710231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 711231d06aeSJörn Engel * hit zero. 712231d06aeSJörn Engel */ 713231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 714231d06aeSJörn Engel { 715231d06aeSJörn Engel if (unlikely(!skb)) 716231d06aeSJörn Engel return; 717231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 718231d06aeSJörn Engel smp_rmb(); 719231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 720231d06aeSJörn Engel return; 721ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 722231d06aeSJörn Engel __kfree_skb(skb); 723231d06aeSJörn Engel } 724b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 725231d06aeSJörn Engel 726bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 727bd8a7036SEric Dumazet { 728bd8a7036SEric Dumazet while (segs) { 729bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 730bd8a7036SEric Dumazet 731bd8a7036SEric Dumazet kfree_skb(segs); 732bd8a7036SEric Dumazet segs = next; 733bd8a7036SEric Dumazet } 734bd8a7036SEric Dumazet } 735bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 736bd8a7036SEric Dumazet 737d1a203eaSStephen Hemminger /** 73825121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 73925121173SMichael S. Tsirkin * @skb: buffer that triggered an error 74025121173SMichael S. Tsirkin * 74125121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 74225121173SMichael S. Tsirkin * skb must be freed afterwards. 74325121173SMichael S. Tsirkin */ 74425121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 74525121173SMichael S. Tsirkin { 74625121173SMichael S. Tsirkin if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 74725121173SMichael S. Tsirkin struct ubuf_info *uarg; 74825121173SMichael S. Tsirkin 74925121173SMichael S. Tsirkin uarg = skb_shinfo(skb)->destructor_arg; 75025121173SMichael S. Tsirkin if (uarg->callback) 75125121173SMichael S. Tsirkin uarg->callback(uarg, false); 75225121173SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 75325121173SMichael S. Tsirkin } 75425121173SMichael S. Tsirkin } 75525121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 75625121173SMichael S. Tsirkin 75725121173SMichael S. Tsirkin /** 758ead2ceb0SNeil Horman * consume_skb - free an skbuff 759ead2ceb0SNeil Horman * @skb: buffer to free 760ead2ceb0SNeil Horman * 761ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 762ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 763ead2ceb0SNeil Horman * is being dropped after a failure and notes that 764ead2ceb0SNeil Horman */ 765ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 766ead2ceb0SNeil Horman { 767ead2ceb0SNeil Horman if (unlikely(!skb)) 768ead2ceb0SNeil Horman return; 769ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 770ead2ceb0SNeil Horman smp_rmb(); 771ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 772ead2ceb0SNeil Horman return; 77307dc22e7SKoki Sanagi trace_consume_skb(skb); 774ead2ceb0SNeil Horman __kfree_skb(skb); 775ead2ceb0SNeil Horman } 776ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 777ead2ceb0SNeil Horman 778b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 779b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 780b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 781b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 782b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 783b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 784b1937227SEric Dumazet 785dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 786dec18810SHerbert Xu { 787dec18810SHerbert Xu new->tstamp = old->tstamp; 788b1937227SEric Dumazet /* We do not copy old->sk */ 789dec18810SHerbert Xu new->dev = old->dev; 790b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 7917fee226aSEric Dumazet skb_dst_copy(new, old); 792def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 793dec18810SHerbert Xu new->sp = secpath_get(old->sp); 794dec18810SHerbert Xu #endif 795b1937227SEric Dumazet __nf_copy(new, old, false); 7966aa895b0SPatrick McHardy 797b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 798b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 799b1937227SEric Dumazet */ 800b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 80106021292SEliezer Tamir 802b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 803b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 804b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 805b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 806b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 807b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 808b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 809b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 810b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 811b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 812b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 813b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 814b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 815b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 816b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 817b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 818b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 819b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 820b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 821b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 822b1937227SEric Dumazet #endif 823e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 824b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 82506021292SEliezer Tamir #endif 826b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 827b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 828b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT 829b1937227SEric Dumazet CHECK_SKB_FIELD(tc_verd); 830b1937227SEric Dumazet #endif 831b1937227SEric Dumazet #endif 832b1937227SEric Dumazet 833dec18810SHerbert Xu } 834dec18810SHerbert Xu 83582c49a35SHerbert Xu /* 83682c49a35SHerbert Xu * You should not add any new code to this function. Add it to 83782c49a35SHerbert Xu * __copy_skb_header above instead. 83882c49a35SHerbert Xu */ 839e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds #define C(x) n->x = skb->x 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds n->next = n->prev = NULL; 8441da177e4SLinus Torvalds n->sk = NULL; 845dec18810SHerbert Xu __copy_skb_header(n, skb); 846dec18810SHerbert Xu 8471da177e4SLinus Torvalds C(len); 8481da177e4SLinus Torvalds C(data_len); 8493e6b3b2eSAlexey Dobriyan C(mac_len); 850334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 85102f1c89dSPaul Moore n->cloned = 1; 8521da177e4SLinus Torvalds n->nohdr = 0; 8531da177e4SLinus Torvalds n->destructor = NULL; 8541da177e4SLinus Torvalds C(tail); 8551da177e4SLinus Torvalds C(end); 85602f1c89dSPaul Moore C(head); 857d3836f21SEric Dumazet C(head_frag); 85802f1c89dSPaul Moore C(data); 85902f1c89dSPaul Moore C(truesize); 86002f1c89dSPaul Moore atomic_set(&n->users, 1); 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 8631da177e4SLinus Torvalds skb->cloned = 1; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds return n; 866e0053ec0SHerbert Xu #undef C 867e0053ec0SHerbert Xu } 868e0053ec0SHerbert Xu 869e0053ec0SHerbert Xu /** 870e0053ec0SHerbert Xu * skb_morph - morph one skb into another 871e0053ec0SHerbert Xu * @dst: the skb to receive the contents 872e0053ec0SHerbert Xu * @src: the skb to supply the contents 873e0053ec0SHerbert Xu * 874e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 875e0053ec0SHerbert Xu * supplied by the user. 876e0053ec0SHerbert Xu * 877e0053ec0SHerbert Xu * The target skb is returned upon exit. 878e0053ec0SHerbert Xu */ 879e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 880e0053ec0SHerbert Xu { 8812d4baff8SHerbert Xu skb_release_all(dst); 882e0053ec0SHerbert Xu return __skb_clone(dst, src); 883e0053ec0SHerbert Xu } 884e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 885e0053ec0SHerbert Xu 8862c53040fSBen Hutchings /** 8872c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 88848c83012SMichael S. Tsirkin * @skb: the skb to modify 88948c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 89048c83012SMichael S. Tsirkin * 89148c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 89248c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 89348c83012SMichael S. Tsirkin * to userspace pages. 89448c83012SMichael S. Tsirkin * 89548c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 89648c83012SMichael S. Tsirkin * %GFP_ATOMIC. 89748c83012SMichael S. Tsirkin * 89848c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 89948c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 90048c83012SMichael S. Tsirkin */ 90148c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 902a6686f2fSShirley Ma { 903a6686f2fSShirley Ma int i; 904a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 905a6686f2fSShirley Ma struct page *page, *head = NULL; 906a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 907a6686f2fSShirley Ma 908a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 909a6686f2fSShirley Ma u8 *vaddr; 910a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 911a6686f2fSShirley Ma 91202756ed4SKrishna Kumar page = alloc_page(gfp_mask); 913a6686f2fSShirley Ma if (!page) { 914a6686f2fSShirley Ma while (head) { 91540dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 916a6686f2fSShirley Ma put_page(head); 917a6686f2fSShirley Ma head = next; 918a6686f2fSShirley Ma } 919a6686f2fSShirley Ma return -ENOMEM; 920a6686f2fSShirley Ma } 92151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 922a6686f2fSShirley Ma memcpy(page_address(page), 9239e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 92451c56b00SEric Dumazet kunmap_atomic(vaddr); 92540dadff2SSunghan Suh set_page_private(page, (unsigned long)head); 926a6686f2fSShirley Ma head = page; 927a6686f2fSShirley Ma } 928a6686f2fSShirley Ma 929a6686f2fSShirley Ma /* skb frags release userspace buffers */ 93002756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 931a8605c60SIan Campbell skb_frag_unref(skb, i); 932a6686f2fSShirley Ma 933e19d6763SMichael S. Tsirkin uarg->callback(uarg, false); 934a6686f2fSShirley Ma 935a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 93602756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 93702756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 93802756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 93940dadff2SSunghan Suh head = (struct page *)page_private(head); 940a6686f2fSShirley Ma } 94148c83012SMichael S. Tsirkin 94248c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 943a6686f2fSShirley Ma return 0; 944a6686f2fSShirley Ma } 945dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 946a6686f2fSShirley Ma 947e0053ec0SHerbert Xu /** 948e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 949e0053ec0SHerbert Xu * @skb: buffer to clone 950e0053ec0SHerbert Xu * @gfp_mask: allocation priority 951e0053ec0SHerbert Xu * 952e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 953e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 954e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 955e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 956e0053ec0SHerbert Xu * 957e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 958e0053ec0SHerbert Xu * %GFP_ATOMIC. 959e0053ec0SHerbert Xu */ 960e0053ec0SHerbert Xu 961e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 962e0053ec0SHerbert Xu { 963d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 964d0bf4a9eSEric Dumazet struct sk_buff_fclones, 965d0bf4a9eSEric Dumazet skb1); 9666ffe75ebSEric Dumazet struct sk_buff *n; 967e0053ec0SHerbert Xu 96870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 969a6686f2fSShirley Ma return NULL; 970a6686f2fSShirley Ma 971e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 9726ffe75ebSEric Dumazet atomic_read(&fclones->fclone_ref) == 1) { 9736ffe75ebSEric Dumazet n = &fclones->skb2; 9746ffe75ebSEric Dumazet atomic_set(&fclones->fclone_ref, 2); 975e0053ec0SHerbert Xu } else { 976c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 977c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 978c93bdd0eSMel Gorman 979e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 980e0053ec0SHerbert Xu if (!n) 981e0053ec0SHerbert Xu return NULL; 982fe55f6d5SVegard Nossum 983fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 984e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 985e0053ec0SHerbert Xu } 986e0053ec0SHerbert Xu 987e0053ec0SHerbert Xu return __skb_clone(n, skb); 9881da177e4SLinus Torvalds } 989b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 9901da177e4SLinus Torvalds 991f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 992f5b17294SPravin B Shelar { 993030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 994030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 995030737bcSEric Dumazet skb->csum_start += off; 996f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 997f5b17294SPravin B Shelar skb->transport_header += off; 998f5b17294SPravin B Shelar skb->network_header += off; 999f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1000f5b17294SPravin B Shelar skb->mac_header += off; 1001f5b17294SPravin B Shelar skb->inner_transport_header += off; 1002f5b17294SPravin B Shelar skb->inner_network_header += off; 1003aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1004f5b17294SPravin B Shelar } 1005f5b17294SPravin B Shelar 10061da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 10071da177e4SLinus Torvalds { 1008dec18810SHerbert Xu __copy_skb_header(new, old); 1009dec18810SHerbert Xu 10107967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 10117967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 10127967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 1015c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1016c93bdd0eSMel Gorman { 1017c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1018c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1019c93bdd0eSMel Gorman return 0; 1020c93bdd0eSMel Gorman } 1021c93bdd0eSMel Gorman 10221da177e4SLinus Torvalds /** 10231da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 10241da177e4SLinus Torvalds * @skb: buffer to copy 10251da177e4SLinus Torvalds * @gfp_mask: allocation priority 10261da177e4SLinus Torvalds * 10271da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 10281da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 10291da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 10301da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 10311da177e4SLinus Torvalds * 10321da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 10331da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 10341da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 10351da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 10361da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 10371da177e4SLinus Torvalds */ 10381da177e4SLinus Torvalds 1039dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 10401da177e4SLinus Torvalds { 10416602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1042ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1043c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1044c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 10456602cebbSEric Dumazet 10461da177e4SLinus Torvalds if (!n) 10471da177e4SLinus Torvalds return NULL; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /* Set the data pointer */ 10501da177e4SLinus Torvalds skb_reserve(n, headerlen); 10511da177e4SLinus Torvalds /* Set the tail pointer and length */ 10521da177e4SLinus Torvalds skb_put(n, skb->len); 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 10551da177e4SLinus Torvalds BUG(); 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds copy_skb_header(n, skb); 10581da177e4SLinus Torvalds return n; 10591da177e4SLinus Torvalds } 1060b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds /** 1063bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 10641da177e4SLinus Torvalds * @skb: buffer to copy 1065117632e6SEric Dumazet * @headroom: headroom of new skb 10661da177e4SLinus Torvalds * @gfp_mask: allocation priority 1067bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1068bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1069bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 10701da177e4SLinus Torvalds * 10711da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 10721da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 10731da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 10741da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 10751da177e4SLinus Torvalds * or the pointer to the buffer on success. 10761da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 10771da177e4SLinus Torvalds */ 10781da177e4SLinus Torvalds 1079bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1080bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 10811da177e4SLinus Torvalds { 1082117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1083bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1084bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 10856602cebbSEric Dumazet 10861da177e4SLinus Torvalds if (!n) 10871da177e4SLinus Torvalds goto out; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* Set the data pointer */ 1090117632e6SEric Dumazet skb_reserve(n, headroom); 10911da177e4SLinus Torvalds /* Set the tail pointer and length */ 10921da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 10931da177e4SLinus Torvalds /* Copy the bytes */ 1094d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 10951da177e4SLinus Torvalds 109625f484a6SHerbert Xu n->truesize += skb->data_len; 10971da177e4SLinus Torvalds n->data_len = skb->data_len; 10981da177e4SLinus Torvalds n->len = skb->len; 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 11011da177e4SLinus Torvalds int i; 11021da177e4SLinus Torvalds 110370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 11041511022cSDan Carpenter kfree_skb(n); 11051511022cSDan Carpenter n = NULL; 1106a6686f2fSShirley Ma goto out; 1107a6686f2fSShirley Ma } 11081da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11091da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1110ea2ab693SIan Campbell skb_frag_ref(skb, i); 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 111521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 11161da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 11171da177e4SLinus Torvalds skb_clone_fraglist(n); 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds copy_skb_header(n, skb); 11211da177e4SLinus Torvalds out: 11221da177e4SLinus Torvalds return n; 11231da177e4SLinus Torvalds } 1124bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds /** 11271da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 11281da177e4SLinus Torvalds * @skb: buffer to reallocate 11291da177e4SLinus Torvalds * @nhead: room to add at head 11301da177e4SLinus Torvalds * @ntail: room to add at tail 11311da177e4SLinus Torvalds * @gfp_mask: allocation priority 11321da177e4SLinus Torvalds * 1133bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1134bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 11351da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 11361da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 11371da177e4SLinus Torvalds * 11381da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 11391da177e4SLinus Torvalds * reloaded after call to this function. 11401da177e4SLinus Torvalds */ 11411da177e4SLinus Torvalds 114286a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1143dd0fc66fSAl Viro gfp_t gfp_mask) 11441da177e4SLinus Torvalds { 11451da177e4SLinus Torvalds int i; 11461da177e4SLinus Torvalds u8 *data; 1147ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 11481da177e4SLinus Torvalds long off; 11491da177e4SLinus Torvalds 11504edd87adSHerbert Xu BUG_ON(nhead < 0); 11514edd87adSHerbert Xu 11521da177e4SLinus Torvalds if (skb_shared(skb)) 11531da177e4SLinus Torvalds BUG(); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 11561da177e4SLinus Torvalds 1157c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1158c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1159c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1160c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 11611da177e4SLinus Torvalds if (!data) 11621da177e4SLinus Torvalds goto nodata; 116387151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 11666602cebbSEric Dumazet * optimized for the cases when header is void. 11676602cebbSEric Dumazet */ 11686602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 11696602cebbSEric Dumazet 11706602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 11716602cebbSEric Dumazet skb_shinfo(skb), 1172fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 11731da177e4SLinus Torvalds 11743e24591aSAlexander Duyck /* 11753e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 11763e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 11773e24591aSAlexander Duyck * be since all we did is relocate the values 11783e24591aSAlexander Duyck */ 11793e24591aSAlexander Duyck if (skb_cloned(skb)) { 1180a6686f2fSShirley Ma /* copy this zero copy skb frags */ 118170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1182a6686f2fSShirley Ma goto nofrags; 11831da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1184ea2ab693SIan Campbell skb_frag_ref(skb, i); 11851da177e4SLinus Torvalds 118621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 11871da177e4SLinus Torvalds skb_clone_fraglist(skb); 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds skb_release_data(skb); 11903e24591aSAlexander Duyck } else { 11913e24591aSAlexander Duyck skb_free_head(skb); 11921fd63041SEric Dumazet } 11931da177e4SLinus Torvalds off = (data + nhead) - skb->head; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds skb->head = data; 1196d3836f21SEric Dumazet skb->head_frag = 0; 11971da177e4SLinus Torvalds skb->data += off; 11984305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 11994305b541SArnaldo Carvalho de Melo skb->end = size; 120056eb8882SPatrick McHardy off = nhead; 12014305b541SArnaldo Carvalho de Melo #else 12024305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 120356eb8882SPatrick McHardy #endif 120427a884dcSArnaldo Carvalho de Melo skb->tail += off; 1205b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 12061da177e4SLinus Torvalds skb->cloned = 0; 1207334a8132SPatrick McHardy skb->hdr_len = 0; 12081da177e4SLinus Torvalds skb->nohdr = 0; 12091da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 12101da177e4SLinus Torvalds return 0; 12111da177e4SLinus Torvalds 1212a6686f2fSShirley Ma nofrags: 1213a6686f2fSShirley Ma kfree(data); 12141da177e4SLinus Torvalds nodata: 12151da177e4SLinus Torvalds return -ENOMEM; 12161da177e4SLinus Torvalds } 1217b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 12221da177e4SLinus Torvalds { 12231da177e4SLinus Torvalds struct sk_buff *skb2; 12241da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds if (delta <= 0) 12271da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 12281da177e4SLinus Torvalds else { 12291da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 12301da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 12311da177e4SLinus Torvalds GFP_ATOMIC)) { 12321da177e4SLinus Torvalds kfree_skb(skb2); 12331da177e4SLinus Torvalds skb2 = NULL; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds return skb2; 12371da177e4SLinus Torvalds } 1238b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /** 12411da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 12421da177e4SLinus Torvalds * @skb: buffer to copy 12431da177e4SLinus Torvalds * @newheadroom: new free bytes at head 12441da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 12451da177e4SLinus Torvalds * @gfp_mask: allocation priority 12461da177e4SLinus Torvalds * 12471da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 12481da177e4SLinus Torvalds * allocate additional space. 12491da177e4SLinus Torvalds * 12501da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 12511da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 12521da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 12531da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 12541da177e4SLinus Torvalds * 12551da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 12561da177e4SLinus Torvalds * is called from an interrupt. 12571da177e4SLinus Torvalds */ 12581da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 125986a76cafSVictor Fusco int newheadroom, int newtailroom, 1260dd0fc66fSAl Viro gfp_t gfp_mask) 12611da177e4SLinus Torvalds { 12621da177e4SLinus Torvalds /* 12631da177e4SLinus Torvalds * Allocate the copy buffer 12641da177e4SLinus Torvalds */ 1265c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1266c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1267c93bdd0eSMel Gorman NUMA_NO_NODE); 1268efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 12691da177e4SLinus Torvalds int head_copy_len, head_copy_off; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds if (!n) 12721da177e4SLinus Torvalds return NULL; 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds skb_reserve(n, newheadroom); 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds /* Set the tail pointer and length */ 12771da177e4SLinus Torvalds skb_put(n, skb->len); 12781da177e4SLinus Torvalds 1279efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 12801da177e4SLinus Torvalds head_copy_off = 0; 12811da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 12821da177e4SLinus Torvalds head_copy_len = newheadroom; 12831da177e4SLinus Torvalds else 12841da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds /* Copy the linear header and data. */ 12871da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 12881da177e4SLinus Torvalds skb->len + head_copy_len)) 12891da177e4SLinus Torvalds BUG(); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds copy_skb_header(n, skb); 12921da177e4SLinus Torvalds 1293030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1294efd1e8d5SPatrick McHardy 12951da177e4SLinus Torvalds return n; 12961da177e4SLinus Torvalds } 1297b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /** 13001da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 13011da177e4SLinus Torvalds * @skb: buffer to pad 13021da177e4SLinus Torvalds * @pad: space to pad 13031da177e4SLinus Torvalds * 13041da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 13051da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 13061da177e4SLinus Torvalds * beyond the buffer end onto the wire. 13071da177e4SLinus Torvalds * 13085b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 13091da177e4SLinus Torvalds */ 13101da177e4SLinus Torvalds 13115b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 13121da177e4SLinus Torvalds { 13135b057c6bSHerbert Xu int err; 13145b057c6bSHerbert Xu int ntail; 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 13175b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 13181da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 13195b057c6bSHerbert Xu return 0; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13224305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 13235b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 13245b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 13255b057c6bSHerbert Xu if (unlikely(err)) 13265b057c6bSHerbert Xu goto free_skb; 13275b057c6bSHerbert Xu } 13285b057c6bSHerbert Xu 13295b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 13305b057c6bSHerbert Xu * to be audited. 13315b057c6bSHerbert Xu */ 13325b057c6bSHerbert Xu err = skb_linearize(skb); 13335b057c6bSHerbert Xu if (unlikely(err)) 13345b057c6bSHerbert Xu goto free_skb; 13355b057c6bSHerbert Xu 13365b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 13375b057c6bSHerbert Xu return 0; 13385b057c6bSHerbert Xu 13395b057c6bSHerbert Xu free_skb: 13401da177e4SLinus Torvalds kfree_skb(skb); 13415b057c6bSHerbert Xu return err; 13421da177e4SLinus Torvalds } 1343b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 13441da177e4SLinus Torvalds 13450dde3e16SIlpo Järvinen /** 13460c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 13470c7ddf36SMathias Krause * @skb: start of the buffer to use 13480c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 13490c7ddf36SMathias Krause * @len: amount of data to add 13500c7ddf36SMathias Krause * 13510c7ddf36SMathias Krause * This function extends the used data area of the potentially 13520c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 13530c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 13540c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 13550c7ddf36SMathias Krause * returned. 13560c7ddf36SMathias Krause */ 13570c7ddf36SMathias Krause 13580c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 13590c7ddf36SMathias Krause { 13600c7ddf36SMathias Krause if (tail != skb) { 13610c7ddf36SMathias Krause skb->data_len += len; 13620c7ddf36SMathias Krause skb->len += len; 13630c7ddf36SMathias Krause } 13640c7ddf36SMathias Krause return skb_put(tail, len); 13650c7ddf36SMathias Krause } 13660c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 13670c7ddf36SMathias Krause 13680c7ddf36SMathias Krause /** 13690dde3e16SIlpo Järvinen * skb_put - add data to a buffer 13700dde3e16SIlpo Järvinen * @skb: buffer to use 13710dde3e16SIlpo Järvinen * @len: amount of data to add 13720dde3e16SIlpo Järvinen * 13730dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 13740dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 13750dde3e16SIlpo Järvinen * first byte of the extra data is returned. 13760dde3e16SIlpo Järvinen */ 13770dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 13780dde3e16SIlpo Järvinen { 13790dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 13800dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 13810dde3e16SIlpo Järvinen skb->tail += len; 13820dde3e16SIlpo Järvinen skb->len += len; 13830dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 13840dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 13850dde3e16SIlpo Järvinen return tmp; 13860dde3e16SIlpo Järvinen } 13870dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 13880dde3e16SIlpo Järvinen 13896be8ac2fSIlpo Järvinen /** 1390c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1391c2aa270aSIlpo Järvinen * @skb: buffer to use 1392c2aa270aSIlpo Järvinen * @len: amount of data to add 1393c2aa270aSIlpo Järvinen * 1394c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1395c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1396c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1397c2aa270aSIlpo Järvinen */ 1398c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1399c2aa270aSIlpo Järvinen { 1400c2aa270aSIlpo Järvinen skb->data -= len; 1401c2aa270aSIlpo Järvinen skb->len += len; 1402c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1403c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1404c2aa270aSIlpo Järvinen return skb->data; 1405c2aa270aSIlpo Järvinen } 1406c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1407c2aa270aSIlpo Järvinen 1408c2aa270aSIlpo Järvinen /** 14096be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 14106be8ac2fSIlpo Järvinen * @skb: buffer to use 14116be8ac2fSIlpo Järvinen * @len: amount of data to remove 14126be8ac2fSIlpo Järvinen * 14136be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 14146be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 14156be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 14166be8ac2fSIlpo Järvinen * the old data. 14176be8ac2fSIlpo Järvinen */ 14186be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 14196be8ac2fSIlpo Järvinen { 142047d29646SDavid S. Miller return skb_pull_inline(skb, len); 14216be8ac2fSIlpo Järvinen } 14226be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 14236be8ac2fSIlpo Järvinen 1424419ae74eSIlpo Järvinen /** 1425419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1426419ae74eSIlpo Järvinen * @skb: buffer to alter 1427419ae74eSIlpo Järvinen * @len: new length 1428419ae74eSIlpo Järvinen * 1429419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1430419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1431419ae74eSIlpo Järvinen * The skb must be linear. 1432419ae74eSIlpo Järvinen */ 1433419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1434419ae74eSIlpo Järvinen { 1435419ae74eSIlpo Järvinen if (skb->len > len) 1436419ae74eSIlpo Järvinen __skb_trim(skb, len); 1437419ae74eSIlpo Järvinen } 1438419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1439419ae74eSIlpo Järvinen 14403cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 14411da177e4SLinus Torvalds */ 14421da177e4SLinus Torvalds 14433cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 14441da177e4SLinus Torvalds { 144527b437c8SHerbert Xu struct sk_buff **fragp; 144627b437c8SHerbert Xu struct sk_buff *frag; 14471da177e4SLinus Torvalds int offset = skb_headlen(skb); 14481da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 14491da177e4SLinus Torvalds int i; 145027b437c8SHerbert Xu int err; 145127b437c8SHerbert Xu 145227b437c8SHerbert Xu if (skb_cloned(skb) && 145327b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 145427b437c8SHerbert Xu return err; 14551da177e4SLinus Torvalds 1456f4d26fb3SHerbert Xu i = 0; 1457f4d26fb3SHerbert Xu if (offset >= len) 1458f4d26fb3SHerbert Xu goto drop_pages; 1459f4d26fb3SHerbert Xu 1460f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 14619e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 146227b437c8SHerbert Xu 146327b437c8SHerbert Xu if (end < len) { 14641da177e4SLinus Torvalds offset = end; 146527b437c8SHerbert Xu continue; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14689e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 146927b437c8SHerbert Xu 1470f4d26fb3SHerbert Xu drop_pages: 147127b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 147227b437c8SHerbert Xu 147327b437c8SHerbert Xu for (; i < nfrags; i++) 1474ea2ab693SIan Campbell skb_frag_unref(skb, i); 147527b437c8SHerbert Xu 147621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 147727b437c8SHerbert Xu skb_drop_fraglist(skb); 1478f4d26fb3SHerbert Xu goto done; 147927b437c8SHerbert Xu } 148027b437c8SHerbert Xu 148127b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 148227b437c8SHerbert Xu fragp = &frag->next) { 148327b437c8SHerbert Xu int end = offset + frag->len; 148427b437c8SHerbert Xu 148527b437c8SHerbert Xu if (skb_shared(frag)) { 148627b437c8SHerbert Xu struct sk_buff *nfrag; 148727b437c8SHerbert Xu 148827b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 148927b437c8SHerbert Xu if (unlikely(!nfrag)) 149027b437c8SHerbert Xu return -ENOMEM; 149127b437c8SHerbert Xu 149227b437c8SHerbert Xu nfrag->next = frag->next; 149385bb2a60SEric Dumazet consume_skb(frag); 149427b437c8SHerbert Xu frag = nfrag; 149527b437c8SHerbert Xu *fragp = frag; 149627b437c8SHerbert Xu } 149727b437c8SHerbert Xu 149827b437c8SHerbert Xu if (end < len) { 149927b437c8SHerbert Xu offset = end; 150027b437c8SHerbert Xu continue; 150127b437c8SHerbert Xu } 150227b437c8SHerbert Xu 150327b437c8SHerbert Xu if (end > len && 150427b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 150527b437c8SHerbert Xu return err; 150627b437c8SHerbert Xu 150727b437c8SHerbert Xu if (frag->next) 150827b437c8SHerbert Xu skb_drop_list(&frag->next); 150927b437c8SHerbert Xu break; 151027b437c8SHerbert Xu } 151127b437c8SHerbert Xu 1512f4d26fb3SHerbert Xu done: 151327b437c8SHerbert Xu if (len > skb_headlen(skb)) { 15141da177e4SLinus Torvalds skb->data_len -= skb->len - len; 15151da177e4SLinus Torvalds skb->len = len; 15161da177e4SLinus Torvalds } else { 15171da177e4SLinus Torvalds skb->len = len; 15181da177e4SLinus Torvalds skb->data_len = 0; 151927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds return 0; 15231da177e4SLinus Torvalds } 1524b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds /** 15271da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 15281da177e4SLinus Torvalds * @skb: buffer to reallocate 15291da177e4SLinus Torvalds * @delta: number of bytes to advance tail 15301da177e4SLinus Torvalds * 15311da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 15321da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 15331da177e4SLinus Torvalds * data from fragmented part. 15341da177e4SLinus Torvalds * 15351da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 15361da177e4SLinus Torvalds * 15371da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 15381da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 15391da177e4SLinus Torvalds * 15401da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 15411da177e4SLinus Torvalds * reloaded after call to this function. 15421da177e4SLinus Torvalds */ 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 15451da177e4SLinus Torvalds * when it is necessary. 15461da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 15471da177e4SLinus Torvalds * 2. It may change skb pointers. 15481da177e4SLinus Torvalds * 15491da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 15501da177e4SLinus Torvalds */ 15511da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 15521da177e4SLinus Torvalds { 15531da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 15541da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 15551da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 15561da177e4SLinus Torvalds */ 15574305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 15601da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 15611da177e4SLinus Torvalds GFP_ATOMIC)) 15621da177e4SLinus Torvalds return NULL; 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 156527a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 15661da177e4SLinus Torvalds BUG(); 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 15691da177e4SLinus Torvalds * size of pulled pages. Superb. 15701da177e4SLinus Torvalds */ 157121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 15721da177e4SLinus Torvalds goto pull_pages; 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 15751da177e4SLinus Torvalds eat = delta; 15761da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15779e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 15789e903e08SEric Dumazet 15799e903e08SEric Dumazet if (size >= eat) 15801da177e4SLinus Torvalds goto pull_pages; 15819e903e08SEric Dumazet eat -= size; 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 15851da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 15861da177e4SLinus Torvalds * but taking into account that pulling is expected to 15871da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 15881da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 15891da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 15901da177e4SLinus Torvalds */ 15911da177e4SLinus Torvalds if (eat) { 15921da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 15931da177e4SLinus Torvalds struct sk_buff *clone = NULL; 15941da177e4SLinus Torvalds struct sk_buff *insp = NULL; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds do { 159709a62660SKris Katterjohn BUG_ON(!list); 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds if (list->len <= eat) { 16001da177e4SLinus Torvalds /* Eaten as whole. */ 16011da177e4SLinus Torvalds eat -= list->len; 16021da177e4SLinus Torvalds list = list->next; 16031da177e4SLinus Torvalds insp = list; 16041da177e4SLinus Torvalds } else { 16051da177e4SLinus Torvalds /* Eaten partially. */ 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds if (skb_shared(list)) { 16081da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 16091da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 16101da177e4SLinus Torvalds if (!clone) 16111da177e4SLinus Torvalds return NULL; 16121da177e4SLinus Torvalds insp = list->next; 16131da177e4SLinus Torvalds list = clone; 16141da177e4SLinus Torvalds } else { 16151da177e4SLinus Torvalds /* This may be pulled without 16161da177e4SLinus Torvalds * problems. */ 16171da177e4SLinus Torvalds insp = list; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 16201da177e4SLinus Torvalds kfree_skb(clone); 16211da177e4SLinus Torvalds return NULL; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds break; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds } while (eat); 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds /* Free pulled out fragments. */ 16281da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 16291da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 16301da177e4SLinus Torvalds kfree_skb(list); 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds /* And insert new clone at head. */ 16331da177e4SLinus Torvalds if (clone) { 16341da177e4SLinus Torvalds clone->next = list; 16351da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds pull_pages: 16411da177e4SLinus Torvalds eat = delta; 16421da177e4SLinus Torvalds k = 0; 16431da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16449e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 16459e903e08SEric Dumazet 16469e903e08SEric Dumazet if (size <= eat) { 1647ea2ab693SIan Campbell skb_frag_unref(skb, i); 16489e903e08SEric Dumazet eat -= size; 16491da177e4SLinus Torvalds } else { 16501da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 16511da177e4SLinus Torvalds if (eat) { 16521da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 16539e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 16541da177e4SLinus Torvalds eat = 0; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds k++; 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds skb->tail += delta; 16621da177e4SLinus Torvalds skb->data_len -= delta; 16631da177e4SLinus Torvalds 166427a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 16651da177e4SLinus Torvalds } 1666b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 16671da177e4SLinus Torvalds 166822019b17SEric Dumazet /** 166922019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 167022019b17SEric Dumazet * @skb: source skb 167122019b17SEric Dumazet * @offset: offset in source 167222019b17SEric Dumazet * @to: destination buffer 167322019b17SEric Dumazet * @len: number of bytes to copy 167422019b17SEric Dumazet * 167522019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 167622019b17SEric Dumazet * destination buffer. 167722019b17SEric Dumazet * 167822019b17SEric Dumazet * CAUTION ! : 167922019b17SEric Dumazet * If its prototype is ever changed, 168022019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 168122019b17SEric Dumazet * since it is called from BPF assembly code. 168222019b17SEric Dumazet */ 16831da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 16841da177e4SLinus Torvalds { 16851a028e50SDavid S. Miller int start = skb_headlen(skb); 1686fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1687fbb398a8SDavid S. Miller int i, copy; 16881da177e4SLinus Torvalds 16891da177e4SLinus Torvalds if (offset > (int)skb->len - len) 16901da177e4SLinus Torvalds goto fault; 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds /* Copy header. */ 16931a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 16941da177e4SLinus Torvalds if (copy > len) 16951da177e4SLinus Torvalds copy = len; 1696d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 16971da177e4SLinus Torvalds if ((len -= copy) == 0) 16981da177e4SLinus Torvalds return 0; 16991da177e4SLinus Torvalds offset += copy; 17001da177e4SLinus Torvalds to += copy; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 17041a028e50SDavid S. Miller int end; 170551c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17061da177e4SLinus Torvalds 1707547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17081a028e50SDavid S. Miller 170951c56b00SEric Dumazet end = start + skb_frag_size(f); 17101da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17111da177e4SLinus Torvalds u8 *vaddr; 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds if (copy > len) 17141da177e4SLinus Torvalds copy = len; 17151da177e4SLinus Torvalds 171651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 17171da177e4SLinus Torvalds memcpy(to, 171851c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 171951c56b00SEric Dumazet copy); 172051c56b00SEric Dumazet kunmap_atomic(vaddr); 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds if ((len -= copy) == 0) 17231da177e4SLinus Torvalds return 0; 17241da177e4SLinus Torvalds offset += copy; 17251da177e4SLinus Torvalds to += copy; 17261da177e4SLinus Torvalds } 17271a028e50SDavid S. Miller start = end; 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds 1730fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 17311a028e50SDavid S. Miller int end; 17321da177e4SLinus Torvalds 1733547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17341a028e50SDavid S. Miller 1735fbb398a8SDavid S. Miller end = start + frag_iter->len; 17361da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17371da177e4SLinus Torvalds if (copy > len) 17381da177e4SLinus Torvalds copy = len; 1739fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 17401da177e4SLinus Torvalds goto fault; 17411da177e4SLinus Torvalds if ((len -= copy) == 0) 17421da177e4SLinus Torvalds return 0; 17431da177e4SLinus Torvalds offset += copy; 17441da177e4SLinus Torvalds to += copy; 17451da177e4SLinus Torvalds } 17461a028e50SDavid S. Miller start = end; 17471da177e4SLinus Torvalds } 1748a6686f2fSShirley Ma 17491da177e4SLinus Torvalds if (!len) 17501da177e4SLinus Torvalds return 0; 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds fault: 17531da177e4SLinus Torvalds return -EFAULT; 17541da177e4SLinus Torvalds } 1755b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 17561da177e4SLinus Torvalds 17579c55e01cSJens Axboe /* 17589c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 17599c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 17609c55e01cSJens Axboe */ 17619c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 17629c55e01cSJens Axboe { 17638b9d3728SJarek Poplawski put_page(spd->pages[i]); 17648b9d3728SJarek Poplawski } 17659c55e01cSJens Axboe 1766a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 17674fb66994SJarek Poplawski unsigned int *offset, 176818aafc62SEric Dumazet struct sock *sk) 17698b9d3728SJarek Poplawski { 17705640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 17718b9d3728SJarek Poplawski 17725640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 17738b9d3728SJarek Poplawski return NULL; 17744fb66994SJarek Poplawski 17755640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 17764fb66994SJarek Poplawski 17775640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 17785640f768SEric Dumazet page_address(page) + *offset, *len); 17795640f768SEric Dumazet *offset = pfrag->offset; 17805640f768SEric Dumazet pfrag->offset += *len; 17814fb66994SJarek Poplawski 17825640f768SEric Dumazet return pfrag->page; 17839c55e01cSJens Axboe } 17849c55e01cSJens Axboe 178541c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 178641c73a0dSEric Dumazet struct page *page, 178741c73a0dSEric Dumazet unsigned int offset) 178841c73a0dSEric Dumazet { 178941c73a0dSEric Dumazet return spd->nr_pages && 179041c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 179141c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 179241c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 179341c73a0dSEric Dumazet } 179441c73a0dSEric Dumazet 17959c55e01cSJens Axboe /* 17969c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 17979c55e01cSJens Axboe */ 1798a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 179935f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 18004fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 180118aafc62SEric Dumazet bool linear, 18027a67e56fSJarek Poplawski struct sock *sk) 18039c55e01cSJens Axboe { 180441c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1805a108d5f3SDavid S. Miller return true; 18069c55e01cSJens Axboe 18078b9d3728SJarek Poplawski if (linear) { 180818aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 18098b9d3728SJarek Poplawski if (!page) 1810a108d5f3SDavid S. Miller return true; 181141c73a0dSEric Dumazet } 181241c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 181341c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1814a108d5f3SDavid S. Miller return false; 181541c73a0dSEric Dumazet } 18168b9d3728SJarek Poplawski get_page(page); 18179c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 18184fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 18199c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 18209c55e01cSJens Axboe spd->nr_pages++; 18218b9d3728SJarek Poplawski 1822a108d5f3SDavid S. Miller return false; 18239c55e01cSJens Axboe } 18249c55e01cSJens Axboe 1825a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 18262870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 182718aafc62SEric Dumazet unsigned int *len, 1828d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 182935f3d14dSJens Axboe struct sock *sk, 183035f3d14dSJens Axboe struct pipe_inode_info *pipe) 18319c55e01cSJens Axboe { 18322870c43dSOctavian Purdila if (!*len) 1833a108d5f3SDavid S. Miller return true; 18349c55e01cSJens Axboe 18352870c43dSOctavian Purdila /* skip this segment if already processed */ 18362870c43dSOctavian Purdila if (*off >= plen) { 18372870c43dSOctavian Purdila *off -= plen; 1838a108d5f3SDavid S. Miller return false; 18392870c43dSOctavian Purdila } 18402870c43dSOctavian Purdila 18412870c43dSOctavian Purdila /* ignore any bits we already processed */ 18429ca1b22dSEric Dumazet poff += *off; 18439ca1b22dSEric Dumazet plen -= *off; 18442870c43dSOctavian Purdila *off = 0; 18452870c43dSOctavian Purdila 184618aafc62SEric Dumazet do { 184718aafc62SEric Dumazet unsigned int flen = min(*len, plen); 18482870c43dSOctavian Purdila 184918aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 185018aafc62SEric Dumazet linear, sk)) 1851a108d5f3SDavid S. Miller return true; 185218aafc62SEric Dumazet poff += flen; 185318aafc62SEric Dumazet plen -= flen; 18542870c43dSOctavian Purdila *len -= flen; 185518aafc62SEric Dumazet } while (*len && plen); 18562870c43dSOctavian Purdila 1857a108d5f3SDavid S. Miller return false; 1858db43a282SOctavian Purdila } 18599c55e01cSJens Axboe 18609c55e01cSJens Axboe /* 1861a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 18622870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 18639c55e01cSJens Axboe */ 1864a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 186535f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 186635f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 18672870c43dSOctavian Purdila { 18682870c43dSOctavian Purdila int seg; 18699c55e01cSJens Axboe 18701d0c0b32SEric Dumazet /* map the linear part : 18712996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 18722996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 18732996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 18749c55e01cSJens Axboe */ 18752870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 18762870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 18772870c43dSOctavian Purdila skb_headlen(skb), 187818aafc62SEric Dumazet offset, len, spd, 18793a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 18801d0c0b32SEric Dumazet sk, pipe)) 1881a108d5f3SDavid S. Miller return true; 18829c55e01cSJens Axboe 18839c55e01cSJens Axboe /* 18849c55e01cSJens Axboe * then map the fragments 18859c55e01cSJens Axboe */ 18869c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 18879c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 18889c55e01cSJens Axboe 1889ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 18909e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 189118aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 1892a108d5f3SDavid S. Miller return true; 18939c55e01cSJens Axboe } 18949c55e01cSJens Axboe 1895a108d5f3SDavid S. Miller return false; 18969c55e01cSJens Axboe } 18979c55e01cSJens Axboe 18989c55e01cSJens Axboe /* 18999c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 19009c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 19019c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 19029c55e01cSJens Axboe * handle that cleanly. 19039c55e01cSJens Axboe */ 19048b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 19059c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 19069c55e01cSJens Axboe unsigned int flags) 19079c55e01cSJens Axboe { 190841c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 190941c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 19109c55e01cSJens Axboe struct splice_pipe_desc spd = { 19119c55e01cSJens Axboe .pages = pages, 19129c55e01cSJens Axboe .partial = partial, 1913047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 19149c55e01cSJens Axboe .flags = flags, 191528a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 19169c55e01cSJens Axboe .spd_release = sock_spd_release, 19179c55e01cSJens Axboe }; 1918fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19197a67e56fSJarek Poplawski struct sock *sk = skb->sk; 192035f3d14dSJens Axboe int ret = 0; 192135f3d14dSJens Axboe 19229c55e01cSJens Axboe /* 19239c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 19249c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 19259c55e01cSJens Axboe */ 192635f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 19279c55e01cSJens Axboe goto done; 19289c55e01cSJens Axboe else if (!tlen) 19299c55e01cSJens Axboe goto done; 19309c55e01cSJens Axboe 19319c55e01cSJens Axboe /* 19329c55e01cSJens Axboe * now see if we have a frag_list to map 19339c55e01cSJens Axboe */ 1934fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1935fbb398a8SDavid S. Miller if (!tlen) 19369c55e01cSJens Axboe break; 193735f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1938fbb398a8SDavid S. Miller break; 19399c55e01cSJens Axboe } 19409c55e01cSJens Axboe 19419c55e01cSJens Axboe done: 19429c55e01cSJens Axboe if (spd.nr_pages) { 19439c55e01cSJens Axboe /* 19449c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 19459c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 19469c55e01cSJens Axboe * here as compared to sendfile(). We enter here 19479c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 19489c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 19499c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 19509c55e01cSJens Axboe * and networking will grab the socket lock. 19519c55e01cSJens Axboe */ 1952293ad604SOctavian Purdila release_sock(sk); 19539c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1954293ad604SOctavian Purdila lock_sock(sk); 19559c55e01cSJens Axboe } 19569c55e01cSJens Axboe 195735f3d14dSJens Axboe return ret; 19589c55e01cSJens Axboe } 19599c55e01cSJens Axboe 1960357b40a1SHerbert Xu /** 1961357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1962357b40a1SHerbert Xu * @skb: destination buffer 1963357b40a1SHerbert Xu * @offset: offset in destination 1964357b40a1SHerbert Xu * @from: source buffer 1965357b40a1SHerbert Xu * @len: number of bytes to copy 1966357b40a1SHerbert Xu * 1967357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1968357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1969357b40a1SHerbert Xu * traversing fragment lists and such. 1970357b40a1SHerbert Xu */ 1971357b40a1SHerbert Xu 19720c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1973357b40a1SHerbert Xu { 19741a028e50SDavid S. Miller int start = skb_headlen(skb); 1975fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1976fbb398a8SDavid S. Miller int i, copy; 1977357b40a1SHerbert Xu 1978357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1979357b40a1SHerbert Xu goto fault; 1980357b40a1SHerbert Xu 19811a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1982357b40a1SHerbert Xu if (copy > len) 1983357b40a1SHerbert Xu copy = len; 198427d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1985357b40a1SHerbert Xu if ((len -= copy) == 0) 1986357b40a1SHerbert Xu return 0; 1987357b40a1SHerbert Xu offset += copy; 1988357b40a1SHerbert Xu from += copy; 1989357b40a1SHerbert Xu } 1990357b40a1SHerbert Xu 1991357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1992357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19931a028e50SDavid S. Miller int end; 1994357b40a1SHerbert Xu 1995547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19961a028e50SDavid S. Miller 19979e903e08SEric Dumazet end = start + skb_frag_size(frag); 1998357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1999357b40a1SHerbert Xu u8 *vaddr; 2000357b40a1SHerbert Xu 2001357b40a1SHerbert Xu if (copy > len) 2002357b40a1SHerbert Xu copy = len; 2003357b40a1SHerbert Xu 200451c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20051a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 20061a028e50SDavid S. Miller from, copy); 200751c56b00SEric Dumazet kunmap_atomic(vaddr); 2008357b40a1SHerbert Xu 2009357b40a1SHerbert Xu if ((len -= copy) == 0) 2010357b40a1SHerbert Xu return 0; 2011357b40a1SHerbert Xu offset += copy; 2012357b40a1SHerbert Xu from += copy; 2013357b40a1SHerbert Xu } 20141a028e50SDavid S. Miller start = end; 2015357b40a1SHerbert Xu } 2016357b40a1SHerbert Xu 2017fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20181a028e50SDavid S. Miller int end; 2019357b40a1SHerbert Xu 2020547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20211a028e50SDavid S. Miller 2022fbb398a8SDavid S. Miller end = start + frag_iter->len; 2023357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2024357b40a1SHerbert Xu if (copy > len) 2025357b40a1SHerbert Xu copy = len; 2026fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 20271a028e50SDavid S. Miller from, copy)) 2028357b40a1SHerbert Xu goto fault; 2029357b40a1SHerbert Xu if ((len -= copy) == 0) 2030357b40a1SHerbert Xu return 0; 2031357b40a1SHerbert Xu offset += copy; 2032357b40a1SHerbert Xu from += copy; 2033357b40a1SHerbert Xu } 20341a028e50SDavid S. Miller start = end; 2035357b40a1SHerbert Xu } 2036357b40a1SHerbert Xu if (!len) 2037357b40a1SHerbert Xu return 0; 2038357b40a1SHerbert Xu 2039357b40a1SHerbert Xu fault: 2040357b40a1SHerbert Xu return -EFAULT; 2041357b40a1SHerbert Xu } 2042357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2043357b40a1SHerbert Xu 20441da177e4SLinus Torvalds /* Checksum skb data. */ 20452817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 20462817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 20471da177e4SLinus Torvalds { 20481a028e50SDavid S. Miller int start = skb_headlen(skb); 20491a028e50SDavid S. Miller int i, copy = start - offset; 2050fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 20511da177e4SLinus Torvalds int pos = 0; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds /* Checksum header. */ 20541da177e4SLinus Torvalds if (copy > 0) { 20551da177e4SLinus Torvalds if (copy > len) 20561da177e4SLinus Torvalds copy = len; 20572817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 20581da177e4SLinus Torvalds if ((len -= copy) == 0) 20591da177e4SLinus Torvalds return csum; 20601da177e4SLinus Torvalds offset += copy; 20611da177e4SLinus Torvalds pos = copy; 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20651a028e50SDavid S. Miller int end; 206651c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20671da177e4SLinus Torvalds 2068547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20691a028e50SDavid S. Miller 207051c56b00SEric Dumazet end = start + skb_frag_size(frag); 20711da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 207244bb9363SAl Viro __wsum csum2; 20731da177e4SLinus Torvalds u8 *vaddr; 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds if (copy > len) 20761da177e4SLinus Torvalds copy = len; 207751c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20782817a336SDaniel Borkmann csum2 = ops->update(vaddr + frag->page_offset + 20791a028e50SDavid S. Miller offset - start, copy, 0); 208051c56b00SEric Dumazet kunmap_atomic(vaddr); 20812817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 20821da177e4SLinus Torvalds if (!(len -= copy)) 20831da177e4SLinus Torvalds return csum; 20841da177e4SLinus Torvalds offset += copy; 20851da177e4SLinus Torvalds pos += copy; 20861da177e4SLinus Torvalds } 20871a028e50SDavid S. Miller start = end; 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds 2090fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20911a028e50SDavid S. Miller int end; 20921da177e4SLinus Torvalds 2093547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20941a028e50SDavid S. Miller 2095fbb398a8SDavid S. Miller end = start + frag_iter->len; 20961da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20975f92a738SAl Viro __wsum csum2; 20981da177e4SLinus Torvalds if (copy > len) 20991da177e4SLinus Torvalds copy = len; 21002817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 21012817a336SDaniel Borkmann copy, 0, ops); 21022817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 21031da177e4SLinus Torvalds if ((len -= copy) == 0) 21041da177e4SLinus Torvalds return csum; 21051da177e4SLinus Torvalds offset += copy; 21061da177e4SLinus Torvalds pos += copy; 21071da177e4SLinus Torvalds } 21081a028e50SDavid S. Miller start = end; 21091da177e4SLinus Torvalds } 211009a62660SKris Katterjohn BUG_ON(len); 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds return csum; 21131da177e4SLinus Torvalds } 21142817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 21152817a336SDaniel Borkmann 21162817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 21172817a336SDaniel Borkmann int len, __wsum csum) 21182817a336SDaniel Borkmann { 21192817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2120cea80ea8SDaniel Borkmann .update = csum_partial_ext, 21212817a336SDaniel Borkmann .combine = csum_block_add_ext, 21222817a336SDaniel Borkmann }; 21232817a336SDaniel Borkmann 21242817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 21252817a336SDaniel Borkmann } 2126b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds /* Both of above in one bottle. */ 21291da177e4SLinus Torvalds 213081d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 213181d77662SAl Viro u8 *to, int len, __wsum csum) 21321da177e4SLinus Torvalds { 21331a028e50SDavid S. Miller int start = skb_headlen(skb); 21341a028e50SDavid S. Miller int i, copy = start - offset; 2135fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 21361da177e4SLinus Torvalds int pos = 0; 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds /* Copy header. */ 21391da177e4SLinus Torvalds if (copy > 0) { 21401da177e4SLinus Torvalds if (copy > len) 21411da177e4SLinus Torvalds copy = len; 21421da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 21431da177e4SLinus Torvalds copy, csum); 21441da177e4SLinus Torvalds if ((len -= copy) == 0) 21451da177e4SLinus Torvalds return csum; 21461da177e4SLinus Torvalds offset += copy; 21471da177e4SLinus Torvalds to += copy; 21481da177e4SLinus Torvalds pos = copy; 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds 21511da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21521a028e50SDavid S. Miller int end; 21531da177e4SLinus Torvalds 2154547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21551a028e50SDavid S. Miller 21569e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 21571da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21585084205fSAl Viro __wsum csum2; 21591da177e4SLinus Torvalds u8 *vaddr; 21601da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds if (copy > len) 21631da177e4SLinus Torvalds copy = len; 216451c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 21651da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 21661a028e50SDavid S. Miller frag->page_offset + 21671a028e50SDavid S. Miller offset - start, to, 21681a028e50SDavid S. Miller copy, 0); 216951c56b00SEric Dumazet kunmap_atomic(vaddr); 21701da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 21711da177e4SLinus Torvalds if (!(len -= copy)) 21721da177e4SLinus Torvalds return csum; 21731da177e4SLinus Torvalds offset += copy; 21741da177e4SLinus Torvalds to += copy; 21751da177e4SLinus Torvalds pos += copy; 21761da177e4SLinus Torvalds } 21771a028e50SDavid S. Miller start = end; 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds 2180fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 218181d77662SAl Viro __wsum csum2; 21821a028e50SDavid S. Miller int end; 21831da177e4SLinus Torvalds 2184547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21851a028e50SDavid S. Miller 2186fbb398a8SDavid S. Miller end = start + frag_iter->len; 21871da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21881da177e4SLinus Torvalds if (copy > len) 21891da177e4SLinus Torvalds copy = len; 2190fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 21911a028e50SDavid S. Miller offset - start, 21921da177e4SLinus Torvalds to, copy, 0); 21931da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 21941da177e4SLinus Torvalds if ((len -= copy) == 0) 21951da177e4SLinus Torvalds return csum; 21961da177e4SLinus Torvalds offset += copy; 21971da177e4SLinus Torvalds to += copy; 21981da177e4SLinus Torvalds pos += copy; 21991da177e4SLinus Torvalds } 22001a028e50SDavid S. Miller start = end; 22011da177e4SLinus Torvalds } 220209a62660SKris Katterjohn BUG_ON(len); 22031da177e4SLinus Torvalds return csum; 22041da177e4SLinus Torvalds } 2205b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 22061da177e4SLinus Torvalds 2207af2806f8SThomas Graf /** 2208af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2209af2806f8SThomas Graf * @from: source buffer 2210af2806f8SThomas Graf * 2211af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2212af2806f8SThomas Graf * into skb_zerocopy(). 2213af2806f8SThomas Graf */ 2214af2806f8SThomas Graf unsigned int 2215af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2216af2806f8SThomas Graf { 2217af2806f8SThomas Graf unsigned int hlen = 0; 2218af2806f8SThomas Graf 2219af2806f8SThomas Graf if (!from->head_frag || 2220af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2221af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2222af2806f8SThomas Graf hlen = skb_headlen(from); 2223af2806f8SThomas Graf 2224af2806f8SThomas Graf if (skb_has_frag_list(from)) 2225af2806f8SThomas Graf hlen = from->len; 2226af2806f8SThomas Graf 2227af2806f8SThomas Graf return hlen; 2228af2806f8SThomas Graf } 2229af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2230af2806f8SThomas Graf 2231af2806f8SThomas Graf /** 2232af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2233af2806f8SThomas Graf * @to: destination buffer 22347fceb4deSMasanari Iida * @from: source buffer 2235af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2236af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2237af2806f8SThomas Graf * 2238af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2239af2806f8SThomas Graf * to the frags in the source buffer. 2240af2806f8SThomas Graf * 2241af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2242af2806f8SThomas Graf * headroom in the `to` buffer. 224336d5fe6aSZoltan Kiss * 224436d5fe6aSZoltan Kiss * Return value: 224536d5fe6aSZoltan Kiss * 0: everything is OK 224636d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 224736d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2248af2806f8SThomas Graf */ 224936d5fe6aSZoltan Kiss int 225036d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2251af2806f8SThomas Graf { 2252af2806f8SThomas Graf int i, j = 0; 2253af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 225436d5fe6aSZoltan Kiss int ret; 2255af2806f8SThomas Graf struct page *page; 2256af2806f8SThomas Graf unsigned int offset; 2257af2806f8SThomas Graf 2258af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2259af2806f8SThomas Graf 2260af2806f8SThomas Graf /* dont bother with small payloads */ 226136d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 226236d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2263af2806f8SThomas Graf 2264af2806f8SThomas Graf if (hlen) { 226536d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 226636d5fe6aSZoltan Kiss if (unlikely(ret)) 226736d5fe6aSZoltan Kiss return ret; 2268af2806f8SThomas Graf len -= hlen; 2269af2806f8SThomas Graf } else { 2270af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2271af2806f8SThomas Graf if (plen) { 2272af2806f8SThomas Graf page = virt_to_head_page(from->head); 2273af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2274af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2275af2806f8SThomas Graf get_page(page); 2276af2806f8SThomas Graf j = 1; 2277af2806f8SThomas Graf len -= plen; 2278af2806f8SThomas Graf } 2279af2806f8SThomas Graf } 2280af2806f8SThomas Graf 2281af2806f8SThomas Graf to->truesize += len + plen; 2282af2806f8SThomas Graf to->len += len + plen; 2283af2806f8SThomas Graf to->data_len += len + plen; 2284af2806f8SThomas Graf 228536d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 228636d5fe6aSZoltan Kiss skb_tx_error(from); 228736d5fe6aSZoltan Kiss return -ENOMEM; 228836d5fe6aSZoltan Kiss } 228936d5fe6aSZoltan Kiss 2290af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2291af2806f8SThomas Graf if (!len) 2292af2806f8SThomas Graf break; 2293af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2294af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2295af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2296af2806f8SThomas Graf skb_frag_ref(to, j); 2297af2806f8SThomas Graf j++; 2298af2806f8SThomas Graf } 2299af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 230036d5fe6aSZoltan Kiss 230136d5fe6aSZoltan Kiss return 0; 2302af2806f8SThomas Graf } 2303af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2304af2806f8SThomas Graf 23051da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 23061da177e4SLinus Torvalds { 2307d3bc23e7SAl Viro __wsum csum; 23081da177e4SLinus Torvalds long csstart; 23091da177e4SLinus Torvalds 231084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 231155508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 23121da177e4SLinus Torvalds else 23131da177e4SLinus Torvalds csstart = skb_headlen(skb); 23141da177e4SLinus Torvalds 231509a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 23161da177e4SLinus Torvalds 2317d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds csum = 0; 23201da177e4SLinus Torvalds if (csstart != skb->len) 23211da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 23221da177e4SLinus Torvalds skb->len - csstart, 0); 23231da177e4SLinus Torvalds 232484fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2325ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 23261da177e4SLinus Torvalds 2327d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds } 2330b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds /** 23331da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 23341da177e4SLinus Torvalds * @list: list to dequeue from 23351da177e4SLinus Torvalds * 23361da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 23371da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 23381da177e4SLinus Torvalds * returned or %NULL if the list is empty. 23391da177e4SLinus Torvalds */ 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 23421da177e4SLinus Torvalds { 23431da177e4SLinus Torvalds unsigned long flags; 23441da177e4SLinus Torvalds struct sk_buff *result; 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23471da177e4SLinus Torvalds result = __skb_dequeue(list); 23481da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23491da177e4SLinus Torvalds return result; 23501da177e4SLinus Torvalds } 2351b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds /** 23541da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 23551da177e4SLinus Torvalds * @list: list to dequeue from 23561da177e4SLinus Torvalds * 23571da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 23581da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 23591da177e4SLinus Torvalds * returned or %NULL if the list is empty. 23601da177e4SLinus Torvalds */ 23611da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 23621da177e4SLinus Torvalds { 23631da177e4SLinus Torvalds unsigned long flags; 23641da177e4SLinus Torvalds struct sk_buff *result; 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23671da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 23681da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23691da177e4SLinus Torvalds return result; 23701da177e4SLinus Torvalds } 2371b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds /** 23741da177e4SLinus Torvalds * skb_queue_purge - empty a list 23751da177e4SLinus Torvalds * @list: list to empty 23761da177e4SLinus Torvalds * 23771da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 23781da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 23791da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 23801da177e4SLinus Torvalds */ 23811da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 23821da177e4SLinus Torvalds { 23831da177e4SLinus Torvalds struct sk_buff *skb; 23841da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 23851da177e4SLinus Torvalds kfree_skb(skb); 23861da177e4SLinus Torvalds } 2387b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds /** 23901da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 23911da177e4SLinus Torvalds * @list: list to use 23921da177e4SLinus Torvalds * @newsk: buffer to queue 23931da177e4SLinus Torvalds * 23941da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 23951da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 23961da177e4SLinus Torvalds * safely. 23971da177e4SLinus Torvalds * 23981da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23991da177e4SLinus Torvalds */ 24001da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 24011da177e4SLinus Torvalds { 24021da177e4SLinus Torvalds unsigned long flags; 24031da177e4SLinus Torvalds 24041da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24051da177e4SLinus Torvalds __skb_queue_head(list, newsk); 24061da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24071da177e4SLinus Torvalds } 2408b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds /** 24111da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 24121da177e4SLinus Torvalds * @list: list to use 24131da177e4SLinus Torvalds * @newsk: buffer to queue 24141da177e4SLinus Torvalds * 24151da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 24161da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 24171da177e4SLinus Torvalds * safely. 24181da177e4SLinus Torvalds * 24191da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24201da177e4SLinus Torvalds */ 24211da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 24221da177e4SLinus Torvalds { 24231da177e4SLinus Torvalds unsigned long flags; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24261da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 24271da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24281da177e4SLinus Torvalds } 2429b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 24308728b834SDavid S. Miller 24311da177e4SLinus Torvalds /** 24321da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 24331da177e4SLinus Torvalds * @skb: buffer to remove 24348728b834SDavid S. Miller * @list: list to use 24351da177e4SLinus Torvalds * 24368728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 24378728b834SDavid S. Miller * function is atomic with respect to other list locked calls 24381da177e4SLinus Torvalds * 24398728b834SDavid S. Miller * You must know what list the SKB is on. 24401da177e4SLinus Torvalds */ 24418728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 24421da177e4SLinus Torvalds { 24431da177e4SLinus Torvalds unsigned long flags; 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24468728b834SDavid S. Miller __skb_unlink(skb, list); 24471da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24481da177e4SLinus Torvalds } 2449b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds /** 24521da177e4SLinus Torvalds * skb_append - append a buffer 24531da177e4SLinus Torvalds * @old: buffer to insert after 24541da177e4SLinus Torvalds * @newsk: buffer to insert 24558728b834SDavid S. Miller * @list: list to use 24561da177e4SLinus Torvalds * 24571da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 24581da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 24591da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24601da177e4SLinus Torvalds */ 24618728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 24621da177e4SLinus Torvalds { 24631da177e4SLinus Torvalds unsigned long flags; 24641da177e4SLinus Torvalds 24658728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 24667de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 24678728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 24681da177e4SLinus Torvalds } 2469b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds /** 24721da177e4SLinus Torvalds * skb_insert - insert a buffer 24731da177e4SLinus Torvalds * @old: buffer to insert before 24741da177e4SLinus Torvalds * @newsk: buffer to insert 24758728b834SDavid S. Miller * @list: list to use 24761da177e4SLinus Torvalds * 24778728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 24788728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 24798728b834SDavid S. Miller * calls. 24808728b834SDavid S. Miller * 24811da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24821da177e4SLinus Torvalds */ 24838728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 24841da177e4SLinus Torvalds { 24851da177e4SLinus Torvalds unsigned long flags; 24861da177e4SLinus Torvalds 24878728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 24888728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 24898728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 24901da177e4SLinus Torvalds } 2491b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 24941da177e4SLinus Torvalds struct sk_buff* skb1, 24951da177e4SLinus Torvalds const u32 len, const int pos) 24961da177e4SLinus Torvalds { 24971da177e4SLinus Torvalds int i; 24981da177e4SLinus Torvalds 2499d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2500d626f62bSArnaldo Carvalho de Melo pos - len); 25011da177e4SLinus Torvalds /* And move data appendix as is. */ 25021da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 25031da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 25041da177e4SLinus Torvalds 25051da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 25061da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 25071da177e4SLinus Torvalds skb1->data_len = skb->data_len; 25081da177e4SLinus Torvalds skb1->len += skb1->data_len; 25091da177e4SLinus Torvalds skb->data_len = 0; 25101da177e4SLinus Torvalds skb->len = len; 251127a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 25151da177e4SLinus Torvalds struct sk_buff* skb1, 25161da177e4SLinus Torvalds const u32 len, int pos) 25171da177e4SLinus Torvalds { 25181da177e4SLinus Torvalds int i, k = 0; 25191da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 25221da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 25231da177e4SLinus Torvalds skb->len = len; 25241da177e4SLinus Torvalds skb->data_len = len - pos; 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 25279e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds if (pos + size > len) { 25301da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 25311da177e4SLinus Torvalds 25321da177e4SLinus Torvalds if (pos < len) { 25331da177e4SLinus Torvalds /* Split frag. 25341da177e4SLinus Torvalds * We have two variants in this case: 25351da177e4SLinus Torvalds * 1. Move all the frag to the second 25361da177e4SLinus Torvalds * part, if it is possible. F.e. 25371da177e4SLinus Torvalds * this approach is mandatory for TUX, 25381da177e4SLinus Torvalds * where splitting is expensive. 25391da177e4SLinus Torvalds * 2. Split is accurately. We make this. 25401da177e4SLinus Torvalds */ 2541ea2ab693SIan Campbell skb_frag_ref(skb, i); 25421da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 25439e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 25449e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 25451da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds k++; 25481da177e4SLinus Torvalds } else 25491da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 25501da177e4SLinus Torvalds pos += size; 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds /** 25561da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 25571da177e4SLinus Torvalds * @skb: the buffer to split 25581da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 25591da177e4SLinus Torvalds * @len: new length for skb 25601da177e4SLinus Torvalds */ 25611da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 25621da177e4SLinus Torvalds { 25631da177e4SLinus Torvalds int pos = skb_headlen(skb); 25641da177e4SLinus Torvalds 256568534c68SAmerigo Wang skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 25661da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 25671da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 25681da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 25691da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 25701da177e4SLinus Torvalds } 2571b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 25721da177e4SLinus Torvalds 25739f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 25749f782db3SIlpo Järvinen * 25759f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 25769f782db3SIlpo Järvinen */ 2577832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2578832d11c5SIlpo Järvinen { 25790ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2580832d11c5SIlpo Järvinen } 2581832d11c5SIlpo Järvinen 2582832d11c5SIlpo Järvinen /** 2583832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2584832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2585832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2586832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2587832d11c5SIlpo Järvinen * 2588832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 258920e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2590832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2591832d11c5SIlpo Järvinen * 2592832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2593832d11c5SIlpo Järvinen * 2594832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2595832d11c5SIlpo Järvinen * to have non-paged data as well. 2596832d11c5SIlpo Järvinen * 2597832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2598832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2599832d11c5SIlpo Järvinen */ 2600832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2601832d11c5SIlpo Järvinen { 2602832d11c5SIlpo Järvinen int from, to, merge, todo; 2603832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2604832d11c5SIlpo Järvinen 2605832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2606832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2607832d11c5SIlpo Järvinen 2608832d11c5SIlpo Järvinen todo = shiftlen; 2609832d11c5SIlpo Järvinen from = 0; 2610832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2611832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2612832d11c5SIlpo Järvinen 2613832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2614832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2615832d11c5SIlpo Järvinen */ 2616832d11c5SIlpo Järvinen if (!to || 2617ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2618ea2ab693SIan Campbell fragfrom->page_offset)) { 2619832d11c5SIlpo Järvinen merge = -1; 2620832d11c5SIlpo Järvinen } else { 2621832d11c5SIlpo Järvinen merge = to - 1; 2622832d11c5SIlpo Järvinen 26239e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2624832d11c5SIlpo Järvinen if (todo < 0) { 2625832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2626832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2627832d11c5SIlpo Järvinen return 0; 2628832d11c5SIlpo Järvinen 26299f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 26309f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2631832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2632832d11c5SIlpo Järvinen 26339e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 26349e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2635832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2636832d11c5SIlpo Järvinen 2637832d11c5SIlpo Järvinen goto onlymerged; 2638832d11c5SIlpo Järvinen } 2639832d11c5SIlpo Järvinen 2640832d11c5SIlpo Järvinen from++; 2641832d11c5SIlpo Järvinen } 2642832d11c5SIlpo Järvinen 2643832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2644832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2645832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2646832d11c5SIlpo Järvinen return 0; 2647832d11c5SIlpo Järvinen 2648832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2649832d11c5SIlpo Järvinen return 0; 2650832d11c5SIlpo Järvinen 2651832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2652832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2653832d11c5SIlpo Järvinen return 0; 2654832d11c5SIlpo Järvinen 2655832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2656832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2657832d11c5SIlpo Järvinen 26589e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2659832d11c5SIlpo Järvinen *fragto = *fragfrom; 26609e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2661832d11c5SIlpo Järvinen from++; 2662832d11c5SIlpo Järvinen to++; 2663832d11c5SIlpo Järvinen 2664832d11c5SIlpo Järvinen } else { 2665ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2666832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2667832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 26689e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2669832d11c5SIlpo Järvinen 2670832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 26719e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2672832d11c5SIlpo Järvinen todo = 0; 2673832d11c5SIlpo Järvinen 2674832d11c5SIlpo Järvinen to++; 2675832d11c5SIlpo Järvinen break; 2676832d11c5SIlpo Järvinen } 2677832d11c5SIlpo Järvinen } 2678832d11c5SIlpo Järvinen 2679832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2680832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2681832d11c5SIlpo Järvinen 2682832d11c5SIlpo Järvinen if (merge >= 0) { 2683832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2684832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2685832d11c5SIlpo Järvinen 26869e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2687ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2688832d11c5SIlpo Järvinen } 2689832d11c5SIlpo Järvinen 2690832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2691832d11c5SIlpo Järvinen to = 0; 2692832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2693832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2694832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2695832d11c5SIlpo Järvinen 2696832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2697832d11c5SIlpo Järvinen 2698832d11c5SIlpo Järvinen onlymerged: 2699832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2700832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2701832d11c5SIlpo Järvinen */ 2702832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2703832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2704832d11c5SIlpo Järvinen 2705832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2706832d11c5SIlpo Järvinen skb->len -= shiftlen; 2707832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2708832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2709832d11c5SIlpo Järvinen tgt->len += shiftlen; 2710832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2711832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2712832d11c5SIlpo Järvinen 2713832d11c5SIlpo Järvinen return shiftlen; 2714832d11c5SIlpo Järvinen } 2715832d11c5SIlpo Järvinen 2716677e90edSThomas Graf /** 2717677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2718677e90edSThomas Graf * @skb: the buffer to read 2719677e90edSThomas Graf * @from: lower offset of data to be read 2720677e90edSThomas Graf * @to: upper offset of data to be read 2721677e90edSThomas Graf * @st: state variable 2722677e90edSThomas Graf * 2723677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2724677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2725677e90edSThomas Graf */ 2726677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2727677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2728677e90edSThomas Graf { 2729677e90edSThomas Graf st->lower_offset = from; 2730677e90edSThomas Graf st->upper_offset = to; 2731677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2732677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2733677e90edSThomas Graf st->frag_data = NULL; 2734677e90edSThomas Graf } 2735b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2736677e90edSThomas Graf 2737677e90edSThomas Graf /** 2738677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2739677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2740677e90edSThomas Graf * @data: destination pointer for data to be returned 2741677e90edSThomas Graf * @st: state variable 2742677e90edSThomas Graf * 2743bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 2744677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2745bc32383cSMathias Krause * the head of the data block to @data and returns the length 2746677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2747677e90edSThomas Graf * offset has been reached. 2748677e90edSThomas Graf * 2749677e90edSThomas Graf * The caller is not required to consume all of the data 2750bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 2751677e90edSThomas Graf * of bytes already consumed and the next call to 2752677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2753677e90edSThomas Graf * 275425985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2755e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 2756677e90edSThomas Graf * reads of potentially non linear data. 2757677e90edSThomas Graf * 2758bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2759677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2760677e90edSThomas Graf * a stack for this purpose. 2761677e90edSThomas Graf */ 2762677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2763677e90edSThomas Graf struct skb_seq_state *st) 2764677e90edSThomas Graf { 2765677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2766677e90edSThomas Graf skb_frag_t *frag; 2767677e90edSThomas Graf 2768aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 2769aeb193eaSWedson Almeida Filho if (st->frag_data) { 2770aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 2771aeb193eaSWedson Almeida Filho st->frag_data = NULL; 2772aeb193eaSWedson Almeida Filho } 2773677e90edSThomas Graf return 0; 2774aeb193eaSWedson Almeida Filho } 2775677e90edSThomas Graf 2776677e90edSThomas Graf next_skb: 277795e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2778677e90edSThomas Graf 2779995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 278095e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2781677e90edSThomas Graf return block_limit - abs_offset; 2782677e90edSThomas Graf } 2783677e90edSThomas Graf 2784677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2785677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2786677e90edSThomas Graf 2787677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2788677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 27899e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2790677e90edSThomas Graf 2791677e90edSThomas Graf if (abs_offset < block_limit) { 2792677e90edSThomas Graf if (!st->frag_data) 279351c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2794677e90edSThomas Graf 2795677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2796677e90edSThomas Graf (abs_offset - st->stepped_offset); 2797677e90edSThomas Graf 2798677e90edSThomas Graf return block_limit - abs_offset; 2799677e90edSThomas Graf } 2800677e90edSThomas Graf 2801677e90edSThomas Graf if (st->frag_data) { 280251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2803677e90edSThomas Graf st->frag_data = NULL; 2804677e90edSThomas Graf } 2805677e90edSThomas Graf 2806677e90edSThomas Graf st->frag_idx++; 28079e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2808677e90edSThomas Graf } 2809677e90edSThomas Graf 28105b5a60daSOlaf Kirch if (st->frag_data) { 281151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 28125b5a60daSOlaf Kirch st->frag_data = NULL; 28135b5a60daSOlaf Kirch } 28145b5a60daSOlaf Kirch 281521dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2816677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 281795e3b24cSHerbert Xu st->frag_idx = 0; 2818677e90edSThomas Graf goto next_skb; 281971b3346dSShyam Iyer } else if (st->cur_skb->next) { 282071b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 282171b3346dSShyam Iyer st->frag_idx = 0; 2822677e90edSThomas Graf goto next_skb; 2823677e90edSThomas Graf } 2824677e90edSThomas Graf 2825677e90edSThomas Graf return 0; 2826677e90edSThomas Graf } 2827b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2828677e90edSThomas Graf 2829677e90edSThomas Graf /** 2830677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2831677e90edSThomas Graf * @st: state variable 2832677e90edSThomas Graf * 2833677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2834677e90edSThomas Graf * returned 0. 2835677e90edSThomas Graf */ 2836677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2837677e90edSThomas Graf { 2838677e90edSThomas Graf if (st->frag_data) 283951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2840677e90edSThomas Graf } 2841b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2842677e90edSThomas Graf 28433fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 28443fc7e8a6SThomas Graf 28453fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 28463fc7e8a6SThomas Graf struct ts_config *conf, 28473fc7e8a6SThomas Graf struct ts_state *state) 28483fc7e8a6SThomas Graf { 28493fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 28503fc7e8a6SThomas Graf } 28513fc7e8a6SThomas Graf 28523fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 28533fc7e8a6SThomas Graf { 28543fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 28553fc7e8a6SThomas Graf } 28563fc7e8a6SThomas Graf 28573fc7e8a6SThomas Graf /** 28583fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 28593fc7e8a6SThomas Graf * @skb: the buffer to look in 28603fc7e8a6SThomas Graf * @from: search offset 28613fc7e8a6SThomas Graf * @to: search limit 28623fc7e8a6SThomas Graf * @config: textsearch configuration 28633fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 28643fc7e8a6SThomas Graf * 28653fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 28663fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 28673fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 28683fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 28693fc7e8a6SThomas Graf */ 28703fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 28713fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 28723fc7e8a6SThomas Graf struct ts_state *state) 28733fc7e8a6SThomas Graf { 2874f72b948dSPhil Oester unsigned int ret; 2875f72b948dSPhil Oester 28763fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 28773fc7e8a6SThomas Graf config->finish = skb_ts_finish; 28783fc7e8a6SThomas Graf 28793fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 28803fc7e8a6SThomas Graf 2881f72b948dSPhil Oester ret = textsearch_find(config, state); 2882f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 28833fc7e8a6SThomas Graf } 2884b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 28853fc7e8a6SThomas Graf 2886e89e9cf5SAnanda Raju /** 28872c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2888e89e9cf5SAnanda Raju * @sk: sock structure 2889e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 2890e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2891e89e9cf5SAnanda Raju * @from: pointer to user message iov 2892e89e9cf5SAnanda Raju * @length: length of the iov message 2893e89e9cf5SAnanda Raju * 2894e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2895e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2896e89e9cf5SAnanda Raju */ 2897e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2898dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2899e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2900e89e9cf5SAnanda Raju void *from, int length) 2901e89e9cf5SAnanda Raju { 2902b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 2903b2111724SEric Dumazet int copy; 2904e89e9cf5SAnanda Raju int offset = 0; 2905e89e9cf5SAnanda Raju int ret; 2906b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 2907e89e9cf5SAnanda Raju 2908e89e9cf5SAnanda Raju do { 2909e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2910e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2911b2111724SEric Dumazet return -EMSGSIZE; 2912e89e9cf5SAnanda Raju 2913b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 2914e89e9cf5SAnanda Raju return -ENOMEM; 2915e89e9cf5SAnanda Raju 2916e89e9cf5SAnanda Raju /* copy the user data to page */ 2917b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 2918e89e9cf5SAnanda Raju 2919b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 2920e89e9cf5SAnanda Raju offset, copy, 0, skb); 2921e89e9cf5SAnanda Raju if (ret < 0) 2922e89e9cf5SAnanda Raju return -EFAULT; 2923e89e9cf5SAnanda Raju 2924e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2925b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 2926b2111724SEric Dumazet copy); 2927b2111724SEric Dumazet frg_cnt++; 2928b2111724SEric Dumazet pfrag->offset += copy; 2929b2111724SEric Dumazet get_page(pfrag->page); 2930b2111724SEric Dumazet 2931b2111724SEric Dumazet skb->truesize += copy; 2932b2111724SEric Dumazet atomic_add(copy, &sk->sk_wmem_alloc); 2933e89e9cf5SAnanda Raju skb->len += copy; 2934e89e9cf5SAnanda Raju skb->data_len += copy; 2935e89e9cf5SAnanda Raju offset += copy; 2936e89e9cf5SAnanda Raju length -= copy; 2937e89e9cf5SAnanda Raju 2938e89e9cf5SAnanda Raju } while (length > 0); 2939e89e9cf5SAnanda Raju 2940e89e9cf5SAnanda Raju return 0; 2941e89e9cf5SAnanda Raju } 2942b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2943e89e9cf5SAnanda Raju 2944cbb042f9SHerbert Xu /** 2945cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2946cbb042f9SHerbert Xu * @skb: buffer to update 2947cbb042f9SHerbert Xu * @len: length of data pulled 2948cbb042f9SHerbert Xu * 2949cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2950fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 295184fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 295284fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 295384fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2954cbb042f9SHerbert Xu */ 2955cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2956cbb042f9SHerbert Xu { 2957cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2958cbb042f9SHerbert Xu skb->len -= len; 2959cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2960cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2961cbb042f9SHerbert Xu return skb->data += len; 2962cbb042f9SHerbert Xu } 2963f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2964f94691acSArnaldo Carvalho de Melo 2965f4c50d99SHerbert Xu /** 2966f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2967df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 2968576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2969f4c50d99SHerbert Xu * 2970f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 29714c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 29724c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2973f4c50d99SHerbert Xu */ 2974df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 2975df5771ffSMichael S. Tsirkin netdev_features_t features) 2976f4c50d99SHerbert Xu { 2977f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2978f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 29791a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 2980df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 2981df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 2982df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 29831fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 2984f4c50d99SHerbert Xu unsigned int offset = doffset; 2985df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 2986f4c50d99SHerbert Xu unsigned int headroom; 2987f4c50d99SHerbert Xu unsigned int len; 2988ec5f0615SPravin B Shelar __be16 proto; 2989ec5f0615SPravin B Shelar bool csum; 299004ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 2991df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 2992f4c50d99SHerbert Xu int err = -ENOMEM; 2993f4c50d99SHerbert Xu int i = 0; 2994f4c50d99SHerbert Xu int pos; 299553d6471cSVlad Yasevich int dummy; 2996f4c50d99SHerbert Xu 29975882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 299853d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 2999ec5f0615SPravin B Shelar if (unlikely(!proto)) 3000ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3001ec5f0615SPravin B Shelar 30027e2b10c1STom Herbert csum = !head_skb->encap_hdr_csum && 30037e2b10c1STom Herbert !!can_checksum_protocol(features, proto); 30047e2b10c1STom Herbert 3005df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3006df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3007f4c50d99SHerbert Xu 3008f4c50d99SHerbert Xu do { 3009f4c50d99SHerbert Xu struct sk_buff *nskb; 30108cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3011c8884eddSHerbert Xu int hsize; 3012f4c50d99SHerbert Xu int size; 3013f4c50d99SHerbert Xu 3014df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3015f4c50d99SHerbert Xu if (len > mss) 3016f4c50d99SHerbert Xu len = mss; 3017f4c50d99SHerbert Xu 3018df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3019f4c50d99SHerbert Xu if (hsize < 0) 3020f4c50d99SHerbert Xu hsize = 0; 3021c8884eddSHerbert Xu if (hsize > len || !sg) 3022c8884eddSHerbert Xu hsize = len; 3023f4c50d99SHerbert Xu 30241a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 30251a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 30261a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 302789319d38SHerbert Xu 30289d8506ccSHerbert Xu i = 0; 30291a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 30301a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 30311fd819ecSMichael S. Tsirkin frag_skb = list_skb; 30321a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 30339d8506ccSHerbert Xu 30349d8506ccSHerbert Xu while (pos < offset + len) { 30359d8506ccSHerbert Xu BUG_ON(i >= nfrags); 30369d8506ccSHerbert Xu 30374e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 30389d8506ccSHerbert Xu if (pos + size > offset + len) 30399d8506ccSHerbert Xu break; 30409d8506ccSHerbert Xu 30419d8506ccSHerbert Xu i++; 30429d8506ccSHerbert Xu pos += size; 30434e1beba1SMichael S. Tsirkin frag++; 30449d8506ccSHerbert Xu } 30459d8506ccSHerbert Xu 30461a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 30471a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 304889319d38SHerbert Xu 3049f4c50d99SHerbert Xu if (unlikely(!nskb)) 3050f4c50d99SHerbert Xu goto err; 3051f4c50d99SHerbert Xu 30529d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 30539d8506ccSHerbert Xu kfree_skb(nskb); 30549d8506ccSHerbert Xu goto err; 30559d8506ccSHerbert Xu } 30569d8506ccSHerbert Xu 3057ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 305889319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 305989319d38SHerbert Xu kfree_skb(nskb); 306089319d38SHerbert Xu goto err; 306189319d38SHerbert Xu } 306289319d38SHerbert Xu 3063ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 306489319d38SHerbert Xu skb_release_head_state(nskb); 306589319d38SHerbert Xu __skb_push(nskb, doffset); 306689319d38SHerbert Xu } else { 3067c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3068df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3069c93bdd0eSMel Gorman NUMA_NO_NODE); 307089319d38SHerbert Xu 307189319d38SHerbert Xu if (unlikely(!nskb)) 307289319d38SHerbert Xu goto err; 307389319d38SHerbert Xu 307489319d38SHerbert Xu skb_reserve(nskb, headroom); 307589319d38SHerbert Xu __skb_put(nskb, doffset); 307689319d38SHerbert Xu } 307789319d38SHerbert Xu 3078f4c50d99SHerbert Xu if (segs) 3079f4c50d99SHerbert Xu tail->next = nskb; 3080f4c50d99SHerbert Xu else 3081f4c50d99SHerbert Xu segs = nskb; 3082f4c50d99SHerbert Xu tail = nskb; 3083f4c50d99SHerbert Xu 3084df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3085f4c50d99SHerbert Xu 3086030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3087fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 308868c33163SPravin B Shelar 3089df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 309068c33163SPravin B Shelar nskb->data - tnl_hlen, 309168c33163SPravin B Shelar doffset + tnl_hlen); 309289319d38SHerbert Xu 30939d8506ccSHerbert Xu if (nskb->len == len + doffset) 30941cdbcb79SSimon Horman goto perform_csum_check; 309589319d38SHerbert Xu 3096e585f236STom Herbert if (!sg && !nskb->remcsum_offload) { 30976f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 3098df5771ffSMichael S. Tsirkin nskb->csum = skb_copy_and_csum_bits(head_skb, offset, 3099f4c50d99SHerbert Xu skb_put(nskb, len), 3100f4c50d99SHerbert Xu len, 0); 31017e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3102de843723STom Herbert skb_headroom(nskb) + doffset; 3103f4c50d99SHerbert Xu continue; 3104f4c50d99SHerbert Xu } 3105f4c50d99SHerbert Xu 31068cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3107f4c50d99SHerbert Xu 3108df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3109d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3110f4c50d99SHerbert Xu 3111df5771ffSMichael S. Tsirkin skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & 3112df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3113cef401deSEric Dumazet 31149d8506ccSHerbert Xu while (pos < offset + len) { 31159d8506ccSHerbert Xu if (i >= nfrags) { 31161a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 31179d8506ccSHerbert Xu 31189d8506ccSHerbert Xu i = 0; 31191a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 31201a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 31211fd819ecSMichael S. Tsirkin frag_skb = list_skb; 31229d8506ccSHerbert Xu 31239d8506ccSHerbert Xu BUG_ON(!nfrags); 31249d8506ccSHerbert Xu 31251a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 31269d8506ccSHerbert Xu } 31279d8506ccSHerbert Xu 31289d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 31299d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 31309d8506ccSHerbert Xu net_warn_ratelimited( 31319d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 31329d8506ccSHerbert Xu pos, mss); 31339d8506ccSHerbert Xu goto err; 31349d8506ccSHerbert Xu } 31359d8506ccSHerbert Xu 31361fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 31371fd819ecSMichael S. Tsirkin goto err; 31381fd819ecSMichael S. Tsirkin 31394e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 31408cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 31418cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3142f4c50d99SHerbert Xu 3143f4c50d99SHerbert Xu if (pos < offset) { 31448cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 31458cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3146f4c50d99SHerbert Xu } 3147f4c50d99SHerbert Xu 314889319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3149f4c50d99SHerbert Xu 3150f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3151f4c50d99SHerbert Xu i++; 31524e1beba1SMichael S. Tsirkin frag++; 3153f4c50d99SHerbert Xu pos += size; 3154f4c50d99SHerbert Xu } else { 31558cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 315689319d38SHerbert Xu goto skip_fraglist; 3157f4c50d99SHerbert Xu } 3158f4c50d99SHerbert Xu 31598cb19905SMichael S. Tsirkin nskb_frag++; 3160f4c50d99SHerbert Xu } 3161f4c50d99SHerbert Xu 316289319d38SHerbert Xu skip_fraglist: 3163f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3164f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3165f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3166ec5f0615SPravin B Shelar 31671cdbcb79SSimon Horman perform_csum_check: 3168e585f236STom Herbert if (!csum && !nskb->remcsum_offload) { 3169ec5f0615SPravin B Shelar nskb->csum = skb_checksum(nskb, doffset, 3170ec5f0615SPravin B Shelar nskb->len - doffset, 0); 3171ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 31727e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 31737e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3174ec5f0615SPravin B Shelar } 3175df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3176f4c50d99SHerbert Xu 3177bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3178bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3179bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3180bec3cfdcSEric Dumazet */ 3181bec3cfdcSEric Dumazet segs->prev = tail; 3182432c856fSToshiaki Makita 3183432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3184432c856fSToshiaki Makita * using skb_set_owner_w(). 3185432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3186432c856fSToshiaki Makita */ 3187432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3188432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3189432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3190432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3191432c856fSToshiaki Makita } 3192f4c50d99SHerbert Xu return segs; 3193f4c50d99SHerbert Xu 3194f4c50d99SHerbert Xu err: 3195289dccbeSEric Dumazet kfree_skb_list(segs); 3196f4c50d99SHerbert Xu return ERR_PTR(err); 3197f4c50d99SHerbert Xu } 3198f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3199f4c50d99SHerbert Xu 320071d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 320171d93b39SHerbert Xu { 32028a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 320367147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 320467147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 32058a29111cSEric Dumazet struct sk_buff *nskb, *lp, *p = *head; 32068a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 3207715dc1f3SEric Dumazet unsigned int delta_truesize; 32088a29111cSEric Dumazet unsigned int headroom; 320971d93b39SHerbert Xu 32108a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 321171d93b39SHerbert Xu return -E2BIG; 321271d93b39SHerbert Xu 321329e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 32148a29111cSEric Dumazet pinfo = skb_shinfo(lp); 32158a29111cSEric Dumazet 32168a29111cSEric Dumazet if (headlen <= offset) { 321742da6994SHerbert Xu skb_frag_t *frag; 321866e92fcfSHerbert Xu skb_frag_t *frag2; 32199aaa156cSHerbert Xu int i = skbinfo->nr_frags; 32209aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 322142da6994SHerbert Xu 322266e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 32238a29111cSEric Dumazet goto merge; 322481705ad1SHerbert Xu 32258a29111cSEric Dumazet offset -= headlen; 32269aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 32279aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3228f5572068SHerbert Xu 32299aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 32309aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 323166e92fcfSHerbert Xu do { 323266e92fcfSHerbert Xu *--frag = *--frag2; 323366e92fcfSHerbert Xu } while (--i); 323466e92fcfSHerbert Xu 323566e92fcfSHerbert Xu frag->page_offset += offset; 32369e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 323766e92fcfSHerbert Xu 3238715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3239ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3240ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3241715dc1f3SEric Dumazet 3242f5572068SHerbert Xu skb->truesize -= skb->data_len; 3243f5572068SHerbert Xu skb->len -= skb->data_len; 3244f5572068SHerbert Xu skb->data_len = 0; 3245f5572068SHerbert Xu 3246715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 32475d38a079SHerbert Xu goto done; 3248d7e8883cSEric Dumazet } else if (skb->head_frag) { 3249d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3250d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3251d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3252d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3253d7e8883cSEric Dumazet unsigned int first_offset; 3254d7e8883cSEric Dumazet 3255d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 32568a29111cSEric Dumazet goto merge; 3257d7e8883cSEric Dumazet 3258d7e8883cSEric Dumazet first_offset = skb->data - 3259d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3260d7e8883cSEric Dumazet offset; 3261d7e8883cSEric Dumazet 3262d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3263d7e8883cSEric Dumazet 3264d7e8883cSEric Dumazet frag->page.p = page; 3265d7e8883cSEric Dumazet frag->page_offset = first_offset; 3266d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3267d7e8883cSEric Dumazet 3268d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3269d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3270d7e8883cSEric Dumazet 3271715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3272d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3273d7e8883cSEric Dumazet goto done; 32748a29111cSEric Dumazet } 327573d3fe6dSEric Dumazet /* switch back to head shinfo */ 327673d3fe6dSEric Dumazet pinfo = skb_shinfo(p); 327773d3fe6dSEric Dumazet 32788a29111cSEric Dumazet if (pinfo->frag_list) 32798a29111cSEric Dumazet goto merge; 32808a29111cSEric Dumazet if (skb_gro_len(p) != pinfo->gso_size) 328169c0cab1SHerbert Xu return -E2BIG; 328271d93b39SHerbert Xu 328371d93b39SHerbert Xu headroom = skb_headroom(p); 32843d3be433SEric Dumazet nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 328571d93b39SHerbert Xu if (unlikely(!nskb)) 328671d93b39SHerbert Xu return -ENOMEM; 328771d93b39SHerbert Xu 328871d93b39SHerbert Xu __copy_skb_header(nskb, p); 328971d93b39SHerbert Xu nskb->mac_len = p->mac_len; 329071d93b39SHerbert Xu 329171d93b39SHerbert Xu skb_reserve(nskb, headroom); 329286911732SHerbert Xu __skb_put(nskb, skb_gro_offset(p)); 329371d93b39SHerbert Xu 329486911732SHerbert Xu skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 329571d93b39SHerbert Xu skb_set_network_header(nskb, skb_network_offset(p)); 329671d93b39SHerbert Xu skb_set_transport_header(nskb, skb_transport_offset(p)); 329771d93b39SHerbert Xu 329886911732SHerbert Xu __skb_pull(p, skb_gro_offset(p)); 329986911732SHerbert Xu memcpy(skb_mac_header(nskb), skb_mac_header(p), 330086911732SHerbert Xu p->data - skb_mac_header(p)); 330171d93b39SHerbert Xu 330271d93b39SHerbert Xu skb_shinfo(nskb)->frag_list = p; 33039aaa156cSHerbert Xu skb_shinfo(nskb)->gso_size = pinfo->gso_size; 3304622e0ca1SHerbert Xu pinfo->gso_size = 0; 3305f4a775d1SEric Dumazet __skb_header_release(p); 3306c3c7c254SEric Dumazet NAPI_GRO_CB(nskb)->last = p; 330771d93b39SHerbert Xu 330871d93b39SHerbert Xu nskb->data_len += p->len; 3309de8261c2SEric Dumazet nskb->truesize += p->truesize; 331071d93b39SHerbert Xu nskb->len += p->len; 331171d93b39SHerbert Xu 331271d93b39SHerbert Xu *head = nskb; 331371d93b39SHerbert Xu nskb->next = p->next; 331471d93b39SHerbert Xu p->next = NULL; 331571d93b39SHerbert Xu 331671d93b39SHerbert Xu p = nskb; 331771d93b39SHerbert Xu 331871d93b39SHerbert Xu merge: 3319715dc1f3SEric Dumazet delta_truesize = skb->truesize; 332067147ba9SHerbert Xu if (offset > headlen) { 3321d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3322d1dc7abfSMichal Schmidt 3323d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 33249e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3325d1dc7abfSMichal Schmidt skb->data_len -= eat; 3326d1dc7abfSMichal Schmidt skb->len -= eat; 332767147ba9SHerbert Xu offset = headlen; 332856035022SHerbert Xu } 332956035022SHerbert Xu 333067147ba9SHerbert Xu __skb_pull(skb, offset); 333156035022SHerbert Xu 333229e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 33338a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 33348a29111cSEric Dumazet else 3335c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3336c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3337f4a775d1SEric Dumazet __skb_header_release(skb); 33388a29111cSEric Dumazet lp = p; 333971d93b39SHerbert Xu 33405d38a079SHerbert Xu done: 33415d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 334237fe4732SHerbert Xu p->data_len += len; 3343715dc1f3SEric Dumazet p->truesize += delta_truesize; 334437fe4732SHerbert Xu p->len += len; 33458a29111cSEric Dumazet if (lp != p) { 33468a29111cSEric Dumazet lp->data_len += len; 33478a29111cSEric Dumazet lp->truesize += delta_truesize; 33488a29111cSEric Dumazet lp->len += len; 33498a29111cSEric Dumazet } 335071d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 335171d93b39SHerbert Xu return 0; 335271d93b39SHerbert Xu } 335371d93b39SHerbert Xu 33541da177e4SLinus Torvalds void __init skb_init(void) 33551da177e4SLinus Torvalds { 33561da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 33571da177e4SLinus Torvalds sizeof(struct sk_buff), 33581da177e4SLinus Torvalds 0, 3359e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 336020c2df83SPaul Mundt NULL); 3361d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3362d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3363d179cd12SDavid S. Miller 0, 3364e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 336520c2df83SPaul Mundt NULL); 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds 3368716ea3a7SDavid Howells /** 3369716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3370716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3371716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3372716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3373716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3374716ea3a7SDavid Howells * 3375716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3376716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3377716ea3a7SDavid Howells */ 337851c739d1SDavid S. Miller static int 337951c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3380716ea3a7SDavid Howells { 33811a028e50SDavid S. Miller int start = skb_headlen(skb); 33821a028e50SDavid S. Miller int i, copy = start - offset; 3383fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3384716ea3a7SDavid Howells int elt = 0; 3385716ea3a7SDavid Howells 3386716ea3a7SDavid Howells if (copy > 0) { 3387716ea3a7SDavid Howells if (copy > len) 3388716ea3a7SDavid Howells copy = len; 3389642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3390716ea3a7SDavid Howells elt++; 3391716ea3a7SDavid Howells if ((len -= copy) == 0) 3392716ea3a7SDavid Howells return elt; 3393716ea3a7SDavid Howells offset += copy; 3394716ea3a7SDavid Howells } 3395716ea3a7SDavid Howells 3396716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33971a028e50SDavid S. Miller int end; 3398716ea3a7SDavid Howells 3399547b792cSIlpo Järvinen WARN_ON(start > offset + len); 34001a028e50SDavid S. Miller 34019e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3402716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3403716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3404716ea3a7SDavid Howells 3405716ea3a7SDavid Howells if (copy > len) 3406716ea3a7SDavid Howells copy = len; 3407ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3408642f1490SJens Axboe frag->page_offset+offset-start); 3409716ea3a7SDavid Howells elt++; 3410716ea3a7SDavid Howells if (!(len -= copy)) 3411716ea3a7SDavid Howells return elt; 3412716ea3a7SDavid Howells offset += copy; 3413716ea3a7SDavid Howells } 34141a028e50SDavid S. Miller start = end; 3415716ea3a7SDavid Howells } 3416716ea3a7SDavid Howells 3417fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 34181a028e50SDavid S. Miller int end; 3419716ea3a7SDavid Howells 3420547b792cSIlpo Järvinen WARN_ON(start > offset + len); 34211a028e50SDavid S. Miller 3422fbb398a8SDavid S. Miller end = start + frag_iter->len; 3423716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3424716ea3a7SDavid Howells if (copy > len) 3425716ea3a7SDavid Howells copy = len; 3426fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 342751c739d1SDavid S. Miller copy); 3428716ea3a7SDavid Howells if ((len -= copy) == 0) 3429716ea3a7SDavid Howells return elt; 3430716ea3a7SDavid Howells offset += copy; 3431716ea3a7SDavid Howells } 34321a028e50SDavid S. Miller start = end; 3433716ea3a7SDavid Howells } 3434716ea3a7SDavid Howells BUG_ON(len); 3435716ea3a7SDavid Howells return elt; 3436716ea3a7SDavid Howells } 3437716ea3a7SDavid Howells 343825a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 343925a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 344025a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 344125a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 344225a91d8dSFan Du * 344325a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 344425a91d8dSFan Du * 1. sg_init_table 344525a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 344625a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 344725a91d8dSFan Du * 344825a91d8dSFan Du * This is equivalent to: 344925a91d8dSFan Du * 1. sg_init_table 345025a91d8dSFan Du * 2. skb_to_sgvec(payload1) 345125a91d8dSFan Du * 3. sg_unmark_end 345225a91d8dSFan Du * 4. skb_to_sgvec(payload2) 345325a91d8dSFan Du * 345425a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 345525a91d8dSFan Du * is more preferable. 345625a91d8dSFan Du */ 345725a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 345825a91d8dSFan Du int offset, int len) 345925a91d8dSFan Du { 346025a91d8dSFan Du return __skb_to_sgvec(skb, sg, offset, len); 346125a91d8dSFan Du } 346225a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 346325a91d8dSFan Du 346451c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 346551c739d1SDavid S. Miller { 346651c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 346751c739d1SDavid S. Miller 3468c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 346951c739d1SDavid S. Miller 347051c739d1SDavid S. Miller return nsg; 347151c739d1SDavid S. Miller } 3472b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 347351c739d1SDavid S. Miller 3474716ea3a7SDavid Howells /** 3475716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3476716ea3a7SDavid Howells * @skb: The socket buffer to check. 3477716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3478716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3479716ea3a7SDavid Howells * 3480716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3481716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3482716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3483716ea3a7SDavid Howells * 3484716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3485716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3486716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3487716ea3a7SDavid Howells * 3488716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3489716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3490716ea3a7SDavid Howells */ 3491716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3492716ea3a7SDavid Howells { 3493716ea3a7SDavid Howells int copyflag; 3494716ea3a7SDavid Howells int elt; 3495716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3496716ea3a7SDavid Howells 3497716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3498716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3499716ea3a7SDavid Howells * at the moment even if they are anonymous). 3500716ea3a7SDavid Howells */ 3501716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3502716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3503716ea3a7SDavid Howells return -ENOMEM; 3504716ea3a7SDavid Howells 3505716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 350621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3507716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3508716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3509716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3510716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3511716ea3a7SDavid Howells 3512716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3513716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3514716ea3a7SDavid Howells return -ENOMEM; 3515716ea3a7SDavid Howells 3516716ea3a7SDavid Howells /* Voila! */ 3517716ea3a7SDavid Howells *trailer = skb; 3518716ea3a7SDavid Howells return 1; 3519716ea3a7SDavid Howells } 3520716ea3a7SDavid Howells 3521716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3522716ea3a7SDavid Howells 3523716ea3a7SDavid Howells elt = 1; 3524716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3525716ea3a7SDavid Howells copyflag = 0; 3526716ea3a7SDavid Howells 3527716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3528716ea3a7SDavid Howells int ntail = 0; 3529716ea3a7SDavid Howells 3530716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3531716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3532716ea3a7SDavid Howells * after it. */ 3533716ea3a7SDavid Howells 3534716ea3a7SDavid Howells if (skb_shared(skb1)) 3535716ea3a7SDavid Howells copyflag = 1; 3536716ea3a7SDavid Howells 3537716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3538716ea3a7SDavid Howells 3539716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3540716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 354121dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3542716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3543716ea3a7SDavid Howells ntail = tailbits + 128; 3544716ea3a7SDavid Howells } 3545716ea3a7SDavid Howells 3546716ea3a7SDavid Howells if (copyflag || 3547716ea3a7SDavid Howells skb_cloned(skb1) || 3548716ea3a7SDavid Howells ntail || 3549716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 355021dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3551716ea3a7SDavid Howells struct sk_buff *skb2; 3552716ea3a7SDavid Howells 3553716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3554716ea3a7SDavid Howells if (ntail == 0) 3555716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3556716ea3a7SDavid Howells else 3557716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3558716ea3a7SDavid Howells skb_headroom(skb1), 3559716ea3a7SDavid Howells ntail, 3560716ea3a7SDavid Howells GFP_ATOMIC); 3561716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3562716ea3a7SDavid Howells return -ENOMEM; 3563716ea3a7SDavid Howells 3564716ea3a7SDavid Howells if (skb1->sk) 3565716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3566716ea3a7SDavid Howells 3567716ea3a7SDavid Howells /* Looking around. Are we still alive? 3568716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3569716ea3a7SDavid Howells 3570716ea3a7SDavid Howells skb2->next = skb1->next; 3571716ea3a7SDavid Howells *skb_p = skb2; 3572716ea3a7SDavid Howells kfree_skb(skb1); 3573716ea3a7SDavid Howells skb1 = skb2; 3574716ea3a7SDavid Howells } 3575716ea3a7SDavid Howells elt++; 3576716ea3a7SDavid Howells *trailer = skb1; 3577716ea3a7SDavid Howells skb_p = &skb1->next; 3578716ea3a7SDavid Howells } 3579716ea3a7SDavid Howells 3580716ea3a7SDavid Howells return elt; 3581716ea3a7SDavid Howells } 3582b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3583716ea3a7SDavid Howells 3584b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3585b1faf566SEric Dumazet { 3586b1faf566SEric Dumazet struct sock *sk = skb->sk; 3587b1faf566SEric Dumazet 3588b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3589b1faf566SEric Dumazet } 3590b1faf566SEric Dumazet 3591b1faf566SEric Dumazet /* 3592b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3593b1faf566SEric Dumazet */ 3594b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3595b1faf566SEric Dumazet { 3596b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 359795c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3598b1faf566SEric Dumazet return -ENOMEM; 3599b1faf566SEric Dumazet 3600b1faf566SEric Dumazet skb_orphan(skb); 3601b1faf566SEric Dumazet skb->sk = sk; 3602b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3603b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3604b1faf566SEric Dumazet 3605abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3606abb57ea4SEric Dumazet skb_dst_force(skb); 3607abb57ea4SEric Dumazet 3608b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3609b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3610676d2369SDavid S. Miller sk->sk_data_ready(sk); 3611b1faf566SEric Dumazet return 0; 3612b1faf566SEric Dumazet } 3613b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3614b1faf566SEric Dumazet 3615364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3616364a9e93SWillem de Bruijn { 3617364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 3618364a9e93SWillem de Bruijn struct sk_buff *skb, *skb_next; 3619364a9e93SWillem de Bruijn int err = 0; 3620364a9e93SWillem de Bruijn 3621364a9e93SWillem de Bruijn spin_lock_bh(&q->lock); 3622364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 3623364a9e93SWillem de Bruijn if (skb && (skb_next = skb_peek(q))) 3624364a9e93SWillem de Bruijn err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 3625364a9e93SWillem de Bruijn spin_unlock_bh(&q->lock); 3626364a9e93SWillem de Bruijn 3627364a9e93SWillem de Bruijn sk->sk_err = err; 3628364a9e93SWillem de Bruijn if (err) 3629364a9e93SWillem de Bruijn sk->sk_error_report(sk); 3630364a9e93SWillem de Bruijn 3631364a9e93SWillem de Bruijn return skb; 3632364a9e93SWillem de Bruijn } 3633364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 3634364a9e93SWillem de Bruijn 3635cab41c47SAlexander Duyck /** 3636cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 3637cab41c47SAlexander Duyck * @skb: the skb to clone 3638cab41c47SAlexander Duyck * 3639cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 3640cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 3641cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 3642cab41c47SAlexander Duyck * 3643cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 3644cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 3645cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 3646cab41c47SAlexander Duyck * the sk_error_queue. 3647cab41c47SAlexander Duyck */ 364862bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 364962bccb8cSAlexander Duyck { 365062bccb8cSAlexander Duyck struct sock *sk = skb->sk; 365162bccb8cSAlexander Duyck struct sk_buff *clone; 365262bccb8cSAlexander Duyck 365362bccb8cSAlexander Duyck if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) 365462bccb8cSAlexander Duyck return NULL; 365562bccb8cSAlexander Duyck 365662bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 365762bccb8cSAlexander Duyck if (!clone) { 365862bccb8cSAlexander Duyck sock_put(sk); 365962bccb8cSAlexander Duyck return NULL; 366062bccb8cSAlexander Duyck } 366162bccb8cSAlexander Duyck 366262bccb8cSAlexander Duyck clone->sk = sk; 366362bccb8cSAlexander Duyck clone->destructor = sock_efree; 366462bccb8cSAlexander Duyck 366562bccb8cSAlexander Duyck return clone; 366662bccb8cSAlexander Duyck } 366762bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 366862bccb8cSAlexander Duyck 366937846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 367037846ef0SAlexander Duyck struct sock *sk, 367137846ef0SAlexander Duyck int tstype) 3672ac45f602SPatrick Ohly { 3673ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3674ac45f602SPatrick Ohly int err; 3675ac45f602SPatrick Ohly 3676ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3677ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3678ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3679ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3680e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 36814ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 368209c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 36834ed2d765SWillem de Bruijn if (sk->sk_protocol == IPPROTO_TCP) 36844ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 36854ed2d765SWillem de Bruijn } 368629030374SEric Dumazet 3687ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 368829030374SEric Dumazet 3689ac45f602SPatrick Ohly if (err) 3690ac45f602SPatrick Ohly kfree_skb(skb); 3691ac45f602SPatrick Ohly } 369237846ef0SAlexander Duyck 369337846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 369437846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 369537846ef0SAlexander Duyck { 369637846ef0SAlexander Duyck struct sock *sk = skb->sk; 369737846ef0SAlexander Duyck 369862bccb8cSAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 369962bccb8cSAlexander Duyck sock_hold(sk); 370037846ef0SAlexander Duyck 370137846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 370237846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); 370337846ef0SAlexander Duyck 370437846ef0SAlexander Duyck sock_put(sk); 370537846ef0SAlexander Duyck } 370637846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 370737846ef0SAlexander Duyck 370837846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 370937846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 371037846ef0SAlexander Duyck struct sock *sk, int tstype) 371137846ef0SAlexander Duyck { 371237846ef0SAlexander Duyck struct sk_buff *skb; 371337846ef0SAlexander Duyck 371437846ef0SAlexander Duyck if (!sk) 371537846ef0SAlexander Duyck return; 371637846ef0SAlexander Duyck 371737846ef0SAlexander Duyck if (hwtstamps) 371837846ef0SAlexander Duyck *skb_hwtstamps(orig_skb) = *hwtstamps; 371937846ef0SAlexander Duyck else 372037846ef0SAlexander Duyck orig_skb->tstamp = ktime_get_real(); 372137846ef0SAlexander Duyck 372237846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 372337846ef0SAlexander Duyck if (!skb) 372437846ef0SAlexander Duyck return; 372537846ef0SAlexander Duyck 372637846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, tstype); 372737846ef0SAlexander Duyck } 3728e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3729e7fd2885SWillem de Bruijn 3730e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 3731e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 3732e7fd2885SWillem de Bruijn { 3733e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3734e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 3735e7fd2885SWillem de Bruijn } 3736ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3737ac45f602SPatrick Ohly 37386e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 37396e3e939fSJohannes Berg { 37406e3e939fSJohannes Berg struct sock *sk = skb->sk; 37416e3e939fSJohannes Berg struct sock_exterr_skb *serr; 37426e3e939fSJohannes Berg int err; 37436e3e939fSJohannes Berg 37446e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 37456e3e939fSJohannes Berg skb->wifi_acked = acked; 37466e3e939fSJohannes Berg 37476e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 37486e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 37496e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 37506e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 37516e3e939fSJohannes Berg 3752bf7fa551SAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 3753bf7fa551SAlexander Duyck sock_hold(sk); 3754bf7fa551SAlexander Duyck 37556e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 37566e3e939fSJohannes Berg if (err) 37576e3e939fSJohannes Berg kfree_skb(skb); 3758bf7fa551SAlexander Duyck 3759bf7fa551SAlexander Duyck sock_put(sk); 37606e3e939fSJohannes Berg } 37616e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 37626e3e939fSJohannes Berg 3763ac45f602SPatrick Ohly 3764f35d9d8aSRusty Russell /** 3765f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3766f35d9d8aSRusty Russell * @skb: the skb to set 3767f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3768f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3769f35d9d8aSRusty Russell * 3770f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3771f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3772f35d9d8aSRusty Russell * 3773f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3774f35d9d8aSRusty Russell * returns false you should drop the packet. 3775f35d9d8aSRusty Russell */ 3776f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3777f35d9d8aSRusty Russell { 37785ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 37795ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3780e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 37815ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3782f35d9d8aSRusty Russell return false; 3783f35d9d8aSRusty Russell } 3784f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3785f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3786f35d9d8aSRusty Russell skb->csum_offset = off; 3787e5d5decaSJason Wang skb_set_transport_header(skb, start); 3788f35d9d8aSRusty Russell return true; 3789f35d9d8aSRusty Russell } 3790b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3791f35d9d8aSRusty Russell 3792ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 3793ed1f50c3SPaul Durrant unsigned int max) 3794ed1f50c3SPaul Durrant { 3795ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 3796ed1f50c3SPaul Durrant return 0; 3797ed1f50c3SPaul Durrant 3798ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 3799ed1f50c3SPaul Durrant * won't need to do it again. 3800ed1f50c3SPaul Durrant */ 3801ed1f50c3SPaul Durrant if (max > skb->len) 3802ed1f50c3SPaul Durrant max = skb->len; 3803ed1f50c3SPaul Durrant 3804ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 3805ed1f50c3SPaul Durrant return -ENOMEM; 3806ed1f50c3SPaul Durrant 3807ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 3808ed1f50c3SPaul Durrant return -EPROTO; 3809ed1f50c3SPaul Durrant 3810ed1f50c3SPaul Durrant return 0; 3811ed1f50c3SPaul Durrant } 3812ed1f50c3SPaul Durrant 3813f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 3814f9708b43SJan Beulich 3815f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 3816f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 3817f9708b43SJan Beulich unsigned int off) 3818f9708b43SJan Beulich { 3819f9708b43SJan Beulich switch (proto) { 3820f9708b43SJan Beulich int err; 3821f9708b43SJan Beulich 3822f9708b43SJan Beulich case IPPROTO_TCP: 3823f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 3824f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 3825f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3826f9708b43SJan Beulich offsetof(struct tcphdr, 3827f9708b43SJan Beulich check))) 3828f9708b43SJan Beulich err = -EPROTO; 3829f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 3830f9708b43SJan Beulich 3831f9708b43SJan Beulich case IPPROTO_UDP: 3832f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 3833f9708b43SJan Beulich off + sizeof(struct udphdr)); 3834f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3835f9708b43SJan Beulich offsetof(struct udphdr, 3836f9708b43SJan Beulich check))) 3837f9708b43SJan Beulich err = -EPROTO; 3838f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 3839f9708b43SJan Beulich } 3840f9708b43SJan Beulich 3841f9708b43SJan Beulich return ERR_PTR(-EPROTO); 3842f9708b43SJan Beulich } 3843f9708b43SJan Beulich 3844ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3845ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 3846ed1f50c3SPaul Durrant */ 3847ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 3848ed1f50c3SPaul Durrant 3849f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 3850ed1f50c3SPaul Durrant { 3851ed1f50c3SPaul Durrant unsigned int off; 3852ed1f50c3SPaul Durrant bool fragment; 3853f9708b43SJan Beulich __sum16 *csum; 3854ed1f50c3SPaul Durrant int err; 3855ed1f50c3SPaul Durrant 3856ed1f50c3SPaul Durrant fragment = false; 3857ed1f50c3SPaul Durrant 3858ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3859ed1f50c3SPaul Durrant sizeof(struct iphdr), 3860ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 3861ed1f50c3SPaul Durrant if (err < 0) 3862ed1f50c3SPaul Durrant goto out; 3863ed1f50c3SPaul Durrant 3864ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 3865ed1f50c3SPaul Durrant fragment = true; 3866ed1f50c3SPaul Durrant 3867ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 3868ed1f50c3SPaul Durrant 3869ed1f50c3SPaul Durrant err = -EPROTO; 3870ed1f50c3SPaul Durrant 3871ed1f50c3SPaul Durrant if (fragment) 3872ed1f50c3SPaul Durrant goto out; 3873ed1f50c3SPaul Durrant 3874f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 3875f9708b43SJan Beulich if (IS_ERR(csum)) 3876f9708b43SJan Beulich return PTR_ERR(csum); 3877ed1f50c3SPaul Durrant 3878ed1f50c3SPaul Durrant if (recalculate) 3879f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 3880ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 3881ed1f50c3SPaul Durrant skb->len - off, 3882f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 3883ed1f50c3SPaul Durrant err = 0; 3884ed1f50c3SPaul Durrant 3885ed1f50c3SPaul Durrant out: 3886ed1f50c3SPaul Durrant return err; 3887ed1f50c3SPaul Durrant } 3888ed1f50c3SPaul Durrant 3889ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3890ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 3891ed1f50c3SPaul Durrant */ 3892ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 3893ed1f50c3SPaul Durrant 3894ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 3895ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 3896ed1f50c3SPaul Durrant 3897ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 3898ed1f50c3SPaul Durrant { 3899ed1f50c3SPaul Durrant int err; 3900ed1f50c3SPaul Durrant u8 nexthdr; 3901ed1f50c3SPaul Durrant unsigned int off; 3902ed1f50c3SPaul Durrant unsigned int len; 3903ed1f50c3SPaul Durrant bool fragment; 3904ed1f50c3SPaul Durrant bool done; 3905f9708b43SJan Beulich __sum16 *csum; 3906ed1f50c3SPaul Durrant 3907ed1f50c3SPaul Durrant fragment = false; 3908ed1f50c3SPaul Durrant done = false; 3909ed1f50c3SPaul Durrant 3910ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 3911ed1f50c3SPaul Durrant 3912ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 3913ed1f50c3SPaul Durrant if (err < 0) 3914ed1f50c3SPaul Durrant goto out; 3915ed1f50c3SPaul Durrant 3916ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 3917ed1f50c3SPaul Durrant 3918ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 3919ed1f50c3SPaul Durrant while (off <= len && !done) { 3920ed1f50c3SPaul Durrant switch (nexthdr) { 3921ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 3922ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 3923ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 3924ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 3925ed1f50c3SPaul Durrant 3926ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3927ed1f50c3SPaul Durrant off + 3928ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 3929ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3930ed1f50c3SPaul Durrant if (err < 0) 3931ed1f50c3SPaul Durrant goto out; 3932ed1f50c3SPaul Durrant 3933ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 3934ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3935ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 3936ed1f50c3SPaul Durrant break; 3937ed1f50c3SPaul Durrant } 3938ed1f50c3SPaul Durrant case IPPROTO_AH: { 3939ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 3940ed1f50c3SPaul Durrant 3941ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3942ed1f50c3SPaul Durrant off + 3943ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 3944ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3945ed1f50c3SPaul Durrant if (err < 0) 3946ed1f50c3SPaul Durrant goto out; 3947ed1f50c3SPaul Durrant 3948ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 3949ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3950ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 3951ed1f50c3SPaul Durrant break; 3952ed1f50c3SPaul Durrant } 3953ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 3954ed1f50c3SPaul Durrant struct frag_hdr *hp; 3955ed1f50c3SPaul Durrant 3956ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3957ed1f50c3SPaul Durrant off + 3958ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 3959ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3960ed1f50c3SPaul Durrant if (err < 0) 3961ed1f50c3SPaul Durrant goto out; 3962ed1f50c3SPaul Durrant 3963ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 3964ed1f50c3SPaul Durrant 3965ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 3966ed1f50c3SPaul Durrant fragment = true; 3967ed1f50c3SPaul Durrant 3968ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3969ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 3970ed1f50c3SPaul Durrant break; 3971ed1f50c3SPaul Durrant } 3972ed1f50c3SPaul Durrant default: 3973ed1f50c3SPaul Durrant done = true; 3974ed1f50c3SPaul Durrant break; 3975ed1f50c3SPaul Durrant } 3976ed1f50c3SPaul Durrant } 3977ed1f50c3SPaul Durrant 3978ed1f50c3SPaul Durrant err = -EPROTO; 3979ed1f50c3SPaul Durrant 3980ed1f50c3SPaul Durrant if (!done || fragment) 3981ed1f50c3SPaul Durrant goto out; 3982ed1f50c3SPaul Durrant 3983f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 3984f9708b43SJan Beulich if (IS_ERR(csum)) 3985f9708b43SJan Beulich return PTR_ERR(csum); 3986ed1f50c3SPaul Durrant 3987ed1f50c3SPaul Durrant if (recalculate) 3988f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 3989ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 3990f9708b43SJan Beulich skb->len - off, nexthdr, 0); 3991ed1f50c3SPaul Durrant err = 0; 3992ed1f50c3SPaul Durrant 3993ed1f50c3SPaul Durrant out: 3994ed1f50c3SPaul Durrant return err; 3995ed1f50c3SPaul Durrant } 3996ed1f50c3SPaul Durrant 3997ed1f50c3SPaul Durrant /** 3998ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 3999ed1f50c3SPaul Durrant * @skb: the skb to set up 4000ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4001ed1f50c3SPaul Durrant */ 4002ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4003ed1f50c3SPaul Durrant { 4004ed1f50c3SPaul Durrant int err; 4005ed1f50c3SPaul Durrant 4006ed1f50c3SPaul Durrant switch (skb->protocol) { 4007ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4008f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4009ed1f50c3SPaul Durrant break; 4010ed1f50c3SPaul Durrant 4011ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4012ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4013ed1f50c3SPaul Durrant break; 4014ed1f50c3SPaul Durrant 4015ed1f50c3SPaul Durrant default: 4016ed1f50c3SPaul Durrant err = -EPROTO; 4017ed1f50c3SPaul Durrant break; 4018ed1f50c3SPaul Durrant } 4019ed1f50c3SPaul Durrant 4020ed1f50c3SPaul Durrant return err; 4021ed1f50c3SPaul Durrant } 4022ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4023ed1f50c3SPaul Durrant 40244497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 40254497b076SBen Hutchings { 4026e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4027e87cc472SJoe Perches skb->dev->name); 40284497b076SBen Hutchings } 40294497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4030bad43ca8SEric Dumazet 4031bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4032bad43ca8SEric Dumazet { 40333d861f66SEric Dumazet if (head_stolen) { 40343d861f66SEric Dumazet skb_release_head_state(skb); 4035bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 40363d861f66SEric Dumazet } else { 4037bad43ca8SEric Dumazet __kfree_skb(skb); 4038bad43ca8SEric Dumazet } 40393d861f66SEric Dumazet } 4040bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4041bad43ca8SEric Dumazet 4042bad43ca8SEric Dumazet /** 4043bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4044bad43ca8SEric Dumazet * @to: prior buffer 4045bad43ca8SEric Dumazet * @from: buffer to add 4046bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4047c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4048bad43ca8SEric Dumazet */ 4049bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4050bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4051bad43ca8SEric Dumazet { 4052bad43ca8SEric Dumazet int i, delta, len = from->len; 4053bad43ca8SEric Dumazet 4054bad43ca8SEric Dumazet *fragstolen = false; 4055bad43ca8SEric Dumazet 4056bad43ca8SEric Dumazet if (skb_cloned(to)) 4057bad43ca8SEric Dumazet return false; 4058bad43ca8SEric Dumazet 4059bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4060e93a0435SEric Dumazet if (len) 4061bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4062bad43ca8SEric Dumazet *delta_truesize = 0; 4063bad43ca8SEric Dumazet return true; 4064bad43ca8SEric Dumazet } 4065bad43ca8SEric Dumazet 4066bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 4067bad43ca8SEric Dumazet return false; 4068bad43ca8SEric Dumazet 4069bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4070bad43ca8SEric Dumazet struct page *page; 4071bad43ca8SEric Dumazet unsigned int offset; 4072bad43ca8SEric Dumazet 4073bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4074bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 4075bad43ca8SEric Dumazet return false; 4076bad43ca8SEric Dumazet 4077bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4078bad43ca8SEric Dumazet return false; 4079bad43ca8SEric Dumazet 4080bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4081bad43ca8SEric Dumazet 4082bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4083bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4084bad43ca8SEric Dumazet 4085bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 4086bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4087bad43ca8SEric Dumazet *fragstolen = true; 4088bad43ca8SEric Dumazet } else { 4089bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4090bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4091bad43ca8SEric Dumazet return false; 4092bad43ca8SEric Dumazet 4093f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4094bad43ca8SEric Dumazet } 4095bad43ca8SEric Dumazet 4096bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4097bad43ca8SEric Dumazet 4098bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4099bad43ca8SEric Dumazet skb_shinfo(from)->frags, 4100bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4101bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4102bad43ca8SEric Dumazet 4103bad43ca8SEric Dumazet if (!skb_cloned(from)) 4104bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 4105bad43ca8SEric Dumazet 41068ea853fdSLi RongQing /* if the skb is not cloned this does nothing 41078ea853fdSLi RongQing * since we set nr_frags to 0. 41088ea853fdSLi RongQing */ 4109bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4110bad43ca8SEric Dumazet skb_frag_ref(from, i); 4111bad43ca8SEric Dumazet 4112bad43ca8SEric Dumazet to->truesize += delta; 4113bad43ca8SEric Dumazet to->len += len; 4114bad43ca8SEric Dumazet to->data_len += len; 4115bad43ca8SEric Dumazet 4116bad43ca8SEric Dumazet *delta_truesize = delta; 4117bad43ca8SEric Dumazet return true; 4118bad43ca8SEric Dumazet } 4119bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4120621e84d6SNicolas Dichtel 4121621e84d6SNicolas Dichtel /** 41228b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4123621e84d6SNicolas Dichtel * 4124621e84d6SNicolas Dichtel * @skb: buffer to clean 41258b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4126621e84d6SNicolas Dichtel * 41278b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 41288b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 41298b27f277SNicolas Dichtel * operations. 41308b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 41318b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 41328b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4133621e84d6SNicolas Dichtel */ 41348b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4135621e84d6SNicolas Dichtel { 41368b27f277SNicolas Dichtel if (xnet) 4137621e84d6SNicolas Dichtel skb_orphan(skb); 4138621e84d6SNicolas Dichtel skb->tstamp.tv64 = 0; 4139621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4140621e84d6SNicolas Dichtel skb->skb_iif = 0; 414160ff7467SWANG Cong skb->ignore_df = 0; 4142621e84d6SNicolas Dichtel skb_dst_drop(skb); 4143621e84d6SNicolas Dichtel skb->mark = 0; 4144b8fb4e06SThomas Graf skb_init_secmark(skb); 4145621e84d6SNicolas Dichtel secpath_reset(skb); 4146621e84d6SNicolas Dichtel nf_reset(skb); 4147621e84d6SNicolas Dichtel nf_reset_trace(skb); 4148621e84d6SNicolas Dichtel } 4149621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4150de960aa9SFlorian Westphal 4151de960aa9SFlorian Westphal /** 4152de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4153de960aa9SFlorian Westphal * 4154de960aa9SFlorian Westphal * @skb: GSO skb 4155de960aa9SFlorian Westphal * 4156de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4157de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4158de960aa9SFlorian Westphal * 4159de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4160de960aa9SFlorian Westphal */ 4161de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4162de960aa9SFlorian Westphal { 4163de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4164f993bc25SFlorian Westphal unsigned int thlen = 0; 4165f993bc25SFlorian Westphal 4166f993bc25SFlorian Westphal if (skb->encapsulation) { 4167f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4168f993bc25SFlorian Westphal skb_transport_header(skb); 4169de960aa9SFlorian Westphal 4170de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4171f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4172f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4173f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 4174f993bc25SFlorian Westphal } 41756d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 41766d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 41776d39d589SFlorian Westphal * accounted for. 41786d39d589SFlorian Westphal */ 4179f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4180de960aa9SFlorian Westphal } 4181de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 41820d5501c1SVlad Yasevich 41830d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 41840d5501c1SVlad Yasevich { 41850d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 41860d5501c1SVlad Yasevich kfree_skb(skb); 41870d5501c1SVlad Yasevich return NULL; 41880d5501c1SVlad Yasevich } 41890d5501c1SVlad Yasevich 41900d5501c1SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN); 41910d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 41920d5501c1SVlad Yasevich return skb; 41930d5501c1SVlad Yasevich } 41940d5501c1SVlad Yasevich 41950d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 41960d5501c1SVlad Yasevich { 41970d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 41980d5501c1SVlad Yasevich u16 vlan_tci; 41990d5501c1SVlad Yasevich 4200df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 42010d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 42020d5501c1SVlad Yasevich return skb; 42030d5501c1SVlad Yasevich } 42040d5501c1SVlad Yasevich 42050d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 42060d5501c1SVlad Yasevich if (unlikely(!skb)) 42070d5501c1SVlad Yasevich goto err_free; 42080d5501c1SVlad Yasevich 42090d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 42100d5501c1SVlad Yasevich goto err_free; 42110d5501c1SVlad Yasevich 42120d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 42130d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 42140d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 42150d5501c1SVlad Yasevich 42160d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 42170d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 42180d5501c1SVlad Yasevich 42190d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 42200d5501c1SVlad Yasevich if (unlikely(!skb)) 42210d5501c1SVlad Yasevich goto err_free; 42220d5501c1SVlad Yasevich 42230d5501c1SVlad Yasevich skb_reset_network_header(skb); 42240d5501c1SVlad Yasevich skb_reset_transport_header(skb); 42250d5501c1SVlad Yasevich skb_reset_mac_len(skb); 42260d5501c1SVlad Yasevich 42270d5501c1SVlad Yasevich return skb; 42280d5501c1SVlad Yasevich 42290d5501c1SVlad Yasevich err_free: 42300d5501c1SVlad Yasevich kfree_skb(skb); 42310d5501c1SVlad Yasevich return NULL; 42320d5501c1SVlad Yasevich } 42330d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 42342e4e4410SEric Dumazet 4235e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 4236e2195121SJiri Pirko { 4237e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 4238e2195121SJiri Pirko return -ENOMEM; 4239e2195121SJiri Pirko 4240e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4241e2195121SJiri Pirko return 0; 4242e2195121SJiri Pirko 4243e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 4244e2195121SJiri Pirko } 4245e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 4246e2195121SJiri Pirko 424793515d53SJiri Pirko /* remove VLAN header from packet and update csum accordingly. */ 424893515d53SJiri Pirko static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 424993515d53SJiri Pirko { 425093515d53SJiri Pirko struct vlan_hdr *vhdr; 425193515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 425293515d53SJiri Pirko int err; 425393515d53SJiri Pirko 425493515d53SJiri Pirko __skb_push(skb, offset); 425593515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 425693515d53SJiri Pirko if (unlikely(err)) 425793515d53SJiri Pirko goto pull; 425893515d53SJiri Pirko 425993515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 426093515d53SJiri Pirko 426193515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 426293515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 426393515d53SJiri Pirko 426493515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 426593515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 426693515d53SJiri Pirko 426793515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 426893515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 426993515d53SJiri Pirko 427093515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 427193515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 427293515d53SJiri Pirko 427393515d53SJiri Pirko skb_reset_mac_len(skb); 427493515d53SJiri Pirko pull: 427593515d53SJiri Pirko __skb_pull(skb, offset); 427693515d53SJiri Pirko 427793515d53SJiri Pirko return err; 427893515d53SJiri Pirko } 427993515d53SJiri Pirko 428093515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 428193515d53SJiri Pirko { 428293515d53SJiri Pirko u16 vlan_tci; 428393515d53SJiri Pirko __be16 vlan_proto; 428493515d53SJiri Pirko int err; 428593515d53SJiri Pirko 4286df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 428793515d53SJiri Pirko skb->vlan_tci = 0; 428893515d53SJiri Pirko } else { 428993515d53SJiri Pirko if (unlikely((skb->protocol != htons(ETH_P_8021Q) && 429093515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 429193515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 429293515d53SJiri Pirko return 0; 429393515d53SJiri Pirko 429493515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 429593515d53SJiri Pirko if (err) 429693515d53SJiri Pirko return err; 429793515d53SJiri Pirko } 429893515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 429993515d53SJiri Pirko if (likely((skb->protocol != htons(ETH_P_8021Q) && 430093515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 430193515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 430293515d53SJiri Pirko return 0; 430393515d53SJiri Pirko 430493515d53SJiri Pirko vlan_proto = skb->protocol; 430593515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 430693515d53SJiri Pirko if (unlikely(err)) 430793515d53SJiri Pirko return err; 430893515d53SJiri Pirko 430993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 431093515d53SJiri Pirko return 0; 431193515d53SJiri Pirko } 431293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 431393515d53SJiri Pirko 431493515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 431593515d53SJiri Pirko { 4316df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 431793515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 431893515d53SJiri Pirko int err; 431993515d53SJiri Pirko 432093515d53SJiri Pirko /* __vlan_insert_tag expect skb->data pointing to mac header. 432193515d53SJiri Pirko * So change skb->data before calling it and change back to 432293515d53SJiri Pirko * original position later 432393515d53SJiri Pirko */ 432493515d53SJiri Pirko __skb_push(skb, offset); 432593515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 4326df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 432793515d53SJiri Pirko if (err) 432893515d53SJiri Pirko return err; 432993515d53SJiri Pirko skb->protocol = skb->vlan_proto; 433093515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 433193515d53SJiri Pirko __skb_pull(skb, offset); 433293515d53SJiri Pirko 433393515d53SJiri Pirko if (skb->ip_summed == CHECKSUM_COMPLETE) 433493515d53SJiri Pirko skb->csum = csum_add(skb->csum, csum_partial(skb->data 433593515d53SJiri Pirko + (2 * ETH_ALEN), VLAN_HLEN, 0)); 433693515d53SJiri Pirko } 433793515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 433893515d53SJiri Pirko return 0; 433993515d53SJiri Pirko } 434093515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 434193515d53SJiri Pirko 43422e4e4410SEric Dumazet /** 43432e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 43442e4e4410SEric Dumazet * 4345de3f0d0eSMasanari Iida * @header_len: size of linear part 4346de3f0d0eSMasanari Iida * @data_len: needed length in frags 4347de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 4348de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 4349de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 43502e4e4410SEric Dumazet * 43512e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 43522e4e4410SEric Dumazet */ 43532e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 43542e4e4410SEric Dumazet unsigned long data_len, 43552e4e4410SEric Dumazet int max_page_order, 43562e4e4410SEric Dumazet int *errcode, 43572e4e4410SEric Dumazet gfp_t gfp_mask) 43582e4e4410SEric Dumazet { 43592e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 43602e4e4410SEric Dumazet unsigned long chunk; 43612e4e4410SEric Dumazet struct sk_buff *skb; 43622e4e4410SEric Dumazet struct page *page; 43632e4e4410SEric Dumazet gfp_t gfp_head; 43642e4e4410SEric Dumazet int i; 43652e4e4410SEric Dumazet 43662e4e4410SEric Dumazet *errcode = -EMSGSIZE; 43672e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 43682e4e4410SEric Dumazet * high order pages... 43692e4e4410SEric Dumazet */ 43702e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 43712e4e4410SEric Dumazet return NULL; 43722e4e4410SEric Dumazet 43732e4e4410SEric Dumazet gfp_head = gfp_mask; 43742e4e4410SEric Dumazet if (gfp_head & __GFP_WAIT) 43752e4e4410SEric Dumazet gfp_head |= __GFP_REPEAT; 43762e4e4410SEric Dumazet 43772e4e4410SEric Dumazet *errcode = -ENOBUFS; 43782e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 43792e4e4410SEric Dumazet if (!skb) 43802e4e4410SEric Dumazet return NULL; 43812e4e4410SEric Dumazet 43822e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 43832e4e4410SEric Dumazet 43842e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 43852e4e4410SEric Dumazet int order = max_page_order; 43862e4e4410SEric Dumazet 43872e4e4410SEric Dumazet while (order) { 43882e4e4410SEric Dumazet if (npages >= 1 << order) { 43892e4e4410SEric Dumazet page = alloc_pages(gfp_mask | 43902e4e4410SEric Dumazet __GFP_COMP | 43912e4e4410SEric Dumazet __GFP_NOWARN | 43922e4e4410SEric Dumazet __GFP_NORETRY, 43932e4e4410SEric Dumazet order); 43942e4e4410SEric Dumazet if (page) 43952e4e4410SEric Dumazet goto fill_page; 43962e4e4410SEric Dumazet /* Do not retry other high order allocations */ 43972e4e4410SEric Dumazet order = 1; 43982e4e4410SEric Dumazet max_page_order = 0; 43992e4e4410SEric Dumazet } 44002e4e4410SEric Dumazet order--; 44012e4e4410SEric Dumazet } 44022e4e4410SEric Dumazet page = alloc_page(gfp_mask); 44032e4e4410SEric Dumazet if (!page) 44042e4e4410SEric Dumazet goto failure; 44052e4e4410SEric Dumazet fill_page: 44062e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 44072e4e4410SEric Dumazet PAGE_SIZE << order); 44082e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 44092e4e4410SEric Dumazet data_len -= chunk; 44102e4e4410SEric Dumazet npages -= 1 << order; 44112e4e4410SEric Dumazet } 44122e4e4410SEric Dumazet return skb; 44132e4e4410SEric Dumazet 44142e4e4410SEric Dumazet failure: 44152e4e4410SEric Dumazet kfree_skb(skb); 44162e4e4410SEric Dumazet return NULL; 44172e4e4410SEric Dumazet } 44182e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 4419