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 268c8753d55SVijay Subramanian fclones->skb2.fclone = SKB_FCLONE_FREE; 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); 339a1c7fff7SEric Dumazet 340c93bdd0eSMel Gorman static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 3416f532612SEric Dumazet { 3426f532612SEric Dumazet struct netdev_alloc_cache *nc; 3436f532612SEric Dumazet void *data = NULL; 34469b08f62SEric Dumazet int order; 3456f532612SEric Dumazet unsigned long flags; 3466f532612SEric Dumazet 3476f532612SEric Dumazet local_irq_save(flags); 348903ceff7SChristoph Lameter nc = this_cpu_ptr(&netdev_alloc_cache); 34969b08f62SEric Dumazet if (unlikely(!nc->frag.page)) { 3506f532612SEric Dumazet refill: 35169b08f62SEric Dumazet for (order = NETDEV_FRAG_PAGE_MAX_ORDER; ;) { 35269b08f62SEric Dumazet gfp_t gfp = gfp_mask; 35369b08f62SEric Dumazet 35469b08f62SEric Dumazet if (order) 35569b08f62SEric Dumazet gfp |= __GFP_COMP | __GFP_NOWARN; 35669b08f62SEric Dumazet nc->frag.page = alloc_pages(gfp, order); 35769b08f62SEric Dumazet if (likely(nc->frag.page)) 35869b08f62SEric Dumazet break; 35969b08f62SEric Dumazet if (--order < 0) 360540eb7bfSAlexander Duyck goto end; 36169b08f62SEric Dumazet } 36269b08f62SEric Dumazet nc->frag.size = PAGE_SIZE << order; 3634c450583SEric Dumazet /* Even if we own the page, we do not use atomic_set(). 3644c450583SEric Dumazet * This would break get_page_unless_zero() users. 3654c450583SEric Dumazet */ 3664c450583SEric Dumazet atomic_add(NETDEV_PAGECNT_MAX_BIAS - 1, 3674c450583SEric Dumazet &nc->frag.page->_count); 36869b08f62SEric Dumazet nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; 36969b08f62SEric Dumazet nc->frag.offset = 0; 3706f532612SEric Dumazet } 371540eb7bfSAlexander Duyck 37269b08f62SEric Dumazet if (nc->frag.offset + fragsz > nc->frag.size) { 3734c450583SEric Dumazet if (atomic_read(&nc->frag.page->_count) != nc->pagecnt_bias) { 3744c450583SEric Dumazet if (!atomic_sub_and_test(nc->pagecnt_bias, 3754c450583SEric Dumazet &nc->frag.page->_count)) 3766f532612SEric Dumazet goto refill; 3774c450583SEric Dumazet /* OK, page count is 0, we can safely set it */ 3784c450583SEric Dumazet atomic_set(&nc->frag.page->_count, 3794c450583SEric Dumazet NETDEV_PAGECNT_MAX_BIAS); 3804c450583SEric Dumazet } else { 3814c450583SEric Dumazet atomic_add(NETDEV_PAGECNT_MAX_BIAS - nc->pagecnt_bias, 3824c450583SEric Dumazet &nc->frag.page->_count); 3834c450583SEric Dumazet } 3844c450583SEric Dumazet nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; 3854c450583SEric Dumazet nc->frag.offset = 0; 3866f532612SEric Dumazet } 387540eb7bfSAlexander Duyck 38869b08f62SEric Dumazet data = page_address(nc->frag.page) + nc->frag.offset; 38969b08f62SEric Dumazet nc->frag.offset += fragsz; 390540eb7bfSAlexander Duyck nc->pagecnt_bias--; 391540eb7bfSAlexander Duyck end: 3926f532612SEric Dumazet local_irq_restore(flags); 3936f532612SEric Dumazet return data; 3946f532612SEric Dumazet } 395c93bdd0eSMel Gorman 396c93bdd0eSMel Gorman /** 397c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 398c93bdd0eSMel Gorman * @fragsz: fragment size 399c93bdd0eSMel Gorman * 400c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 401c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 402c93bdd0eSMel Gorman */ 403c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 404c93bdd0eSMel Gorman { 405c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 406c93bdd0eSMel Gorman } 4076f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 4086f532612SEric Dumazet 4096f532612SEric Dumazet /** 4108af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 4118af27456SChristoph Hellwig * @dev: network device to receive on 4128af27456SChristoph Hellwig * @length: length to allocate 4138af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 4148af27456SChristoph Hellwig * 4158af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 4168af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 4178af27456SChristoph Hellwig * the headroom they think they need without accounting for the 4188af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 4198af27456SChristoph Hellwig * 4208af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 4218af27456SChristoph Hellwig */ 4228af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 4238af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 4248af27456SChristoph Hellwig { 4256f532612SEric Dumazet struct sk_buff *skb = NULL; 426a1c7fff7SEric Dumazet unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + 427a1c7fff7SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4288af27456SChristoph Hellwig 429310e158cSEric Dumazet if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { 430c93bdd0eSMel Gorman void *data; 431c93bdd0eSMel Gorman 432c93bdd0eSMel Gorman if (sk_memalloc_socks()) 433c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 434c93bdd0eSMel Gorman 435c93bdd0eSMel Gorman data = __netdev_alloc_frag(fragsz, gfp_mask); 436a1c7fff7SEric Dumazet 4376f532612SEric Dumazet if (likely(data)) { 4386f532612SEric Dumazet skb = build_skb(data, fragsz); 4396f532612SEric Dumazet if (unlikely(!skb)) 4406f532612SEric Dumazet put_page(virt_to_head_page(data)); 441a1c7fff7SEric Dumazet } 442a1c7fff7SEric Dumazet } else { 443c93bdd0eSMel Gorman skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 444c93bdd0eSMel Gorman SKB_ALLOC_RX, NUMA_NO_NODE); 445a1c7fff7SEric Dumazet } 4467b2e497aSChristoph Hellwig if (likely(skb)) { 4478af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4487b2e497aSChristoph Hellwig skb->dev = dev; 4497b2e497aSChristoph Hellwig } 4508af27456SChristoph Hellwig return skb; 4518af27456SChristoph Hellwig } 452b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4531da177e4SLinus Torvalds 454654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 45550269e19SEric Dumazet int size, unsigned int truesize) 456654bed16SPeter Zijlstra { 457654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 458654bed16SPeter Zijlstra skb->len += size; 459654bed16SPeter Zijlstra skb->data_len += size; 46050269e19SEric Dumazet skb->truesize += truesize; 461654bed16SPeter Zijlstra } 462654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 463654bed16SPeter Zijlstra 464f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 465f8e617e1SJason Wang unsigned int truesize) 466f8e617e1SJason Wang { 467f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 468f8e617e1SJason Wang 469f8e617e1SJason Wang skb_frag_size_add(frag, size); 470f8e617e1SJason Wang skb->len += size; 471f8e617e1SJason Wang skb->data_len += size; 472f8e617e1SJason Wang skb->truesize += truesize; 473f8e617e1SJason Wang } 474f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 475f8e617e1SJason Wang 47627b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 4771da177e4SLinus Torvalds { 478bd8a7036SEric Dumazet kfree_skb_list(*listp); 47927b437c8SHerbert Xu *listp = NULL; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 48227b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 48327b437c8SHerbert Xu { 48427b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 48527b437c8SHerbert Xu } 48627b437c8SHerbert Xu 4871da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 4881da177e4SLinus Torvalds { 4891da177e4SLinus Torvalds struct sk_buff *list; 4901da177e4SLinus Torvalds 491fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 4921da177e4SLinus Torvalds skb_get(list); 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 495d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 496d3836f21SEric Dumazet { 497d3836f21SEric Dumazet if (skb->head_frag) 498d3836f21SEric Dumazet put_page(virt_to_head_page(skb->head)); 499d3836f21SEric Dumazet else 500d3836f21SEric Dumazet kfree(skb->head); 501d3836f21SEric Dumazet } 502d3836f21SEric Dumazet 5035bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5041da177e4SLinus Torvalds { 505ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5061da177e4SLinus Torvalds int i; 507ff04a771SEric Dumazet 508ff04a771SEric Dumazet if (skb->cloned && 509ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 510ff04a771SEric Dumazet &shinfo->dataref)) 511ff04a771SEric Dumazet return; 512ff04a771SEric Dumazet 513ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 514ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 5151da177e4SLinus Torvalds 516a6686f2fSShirley Ma /* 517a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 518a6686f2fSShirley Ma * the lower device DMA has done; 519a6686f2fSShirley Ma */ 520ff04a771SEric Dumazet if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { 521a6686f2fSShirley Ma struct ubuf_info *uarg; 522a6686f2fSShirley Ma 523ff04a771SEric Dumazet uarg = shinfo->destructor_arg; 524a6686f2fSShirley Ma if (uarg->callback) 525e19d6763SMichael S. Tsirkin uarg->callback(uarg, true); 526a6686f2fSShirley Ma } 527a6686f2fSShirley Ma 528ff04a771SEric Dumazet if (shinfo->frag_list) 529ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 5301da177e4SLinus Torvalds 531d3836f21SEric Dumazet skb_free_head(skb); 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds /* 5351da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 5361da177e4SLinus Torvalds */ 5372d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 5381da177e4SLinus Torvalds { 539d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 540d179cd12SDavid S. Miller 541d179cd12SDavid S. Miller switch (skb->fclone) { 542d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 5431da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 544d179cd12SDavid S. Miller break; 545d179cd12SDavid S. Miller 546d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 547d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 548d0bf4a9eSEric Dumazet if (atomic_dec_and_test(&fclones->fclone_ref)) 549d0bf4a9eSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 550d179cd12SDavid S. Miller break; 551d179cd12SDavid S. Miller 552d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 553d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 554d179cd12SDavid S. Miller 555ce1a4ea3SEric Dumazet /* Warning : We must perform the atomic_dec_and_test() before 556c8753d55SVijay Subramanian * setting skb->fclone back to SKB_FCLONE_FREE, otherwise 557ce1a4ea3SEric Dumazet * skb_clone() could set clone_ref to 2 before our decrement. 558ce1a4ea3SEric Dumazet * Anyway, if we are going to free the structure, no need to 559ce1a4ea3SEric Dumazet * rewrite skb->fclone. 560ce1a4ea3SEric Dumazet */ 561ce1a4ea3SEric Dumazet if (atomic_dec_and_test(&fclones->fclone_ref)) { 562ce1a4ea3SEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 563ce1a4ea3SEric Dumazet } else { 564d179cd12SDavid S. Miller /* The clone portion is available for 565d179cd12SDavid S. Miller * fast-cloning again. 566d179cd12SDavid S. Miller */ 567c8753d55SVijay Subramanian skb->fclone = SKB_FCLONE_FREE; 568ce1a4ea3SEric Dumazet } 569d179cd12SDavid S. Miller break; 5703ff50b79SStephen Hemminger } 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 57304a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 5741da177e4SLinus Torvalds { 575adf30907SEric Dumazet skb_dst_drop(skb); 5761da177e4SLinus Torvalds #ifdef CONFIG_XFRM 5771da177e4SLinus Torvalds secpath_put(skb->sp); 5781da177e4SLinus Torvalds #endif 5791da177e4SLinus Torvalds if (skb->destructor) { 5809c2b3328SStephen Hemminger WARN_ON(in_irq()); 5811da177e4SLinus Torvalds skb->destructor(skb); 5821da177e4SLinus Torvalds } 583a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 5845f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 5852fc72c7bSKOVACS Krisztian #endif 5861109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 5871da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 5881da177e4SLinus Torvalds #endif 5891da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 5901da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5911da177e4SLinus Torvalds skb->tc_index = 0; 5921da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 5931da177e4SLinus Torvalds skb->tc_verd = 0; 5941da177e4SLinus Torvalds #endif 5951da177e4SLinus Torvalds #endif 59604a4bb55SLennert Buytenhek } 59704a4bb55SLennert Buytenhek 59804a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 59904a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 60004a4bb55SLennert Buytenhek { 60104a4bb55SLennert Buytenhek skb_release_head_state(skb); 6025e71d9d7SPablo Neira if (likely(skb->head)) 6032d4baff8SHerbert Xu skb_release_data(skb); 6042d4baff8SHerbert Xu } 6051da177e4SLinus Torvalds 6062d4baff8SHerbert Xu /** 6072d4baff8SHerbert Xu * __kfree_skb - private function 6082d4baff8SHerbert Xu * @skb: buffer 6092d4baff8SHerbert Xu * 6102d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6112d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6122d4baff8SHerbert Xu * always call kfree_skb 6132d4baff8SHerbert Xu */ 6142d4baff8SHerbert Xu 6152d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6162d4baff8SHerbert Xu { 6172d4baff8SHerbert Xu skb_release_all(skb); 6181da177e4SLinus Torvalds kfree_skbmem(skb); 6191da177e4SLinus Torvalds } 620b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds /** 623231d06aeSJörn Engel * kfree_skb - free an sk_buff 624231d06aeSJörn Engel * @skb: buffer to free 625231d06aeSJörn Engel * 626231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 627231d06aeSJörn Engel * hit zero. 628231d06aeSJörn Engel */ 629231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 630231d06aeSJörn Engel { 631231d06aeSJörn Engel if (unlikely(!skb)) 632231d06aeSJörn Engel return; 633231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 634231d06aeSJörn Engel smp_rmb(); 635231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 636231d06aeSJörn Engel return; 637ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 638231d06aeSJörn Engel __kfree_skb(skb); 639231d06aeSJörn Engel } 640b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 641231d06aeSJörn Engel 642bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 643bd8a7036SEric Dumazet { 644bd8a7036SEric Dumazet while (segs) { 645bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 646bd8a7036SEric Dumazet 647bd8a7036SEric Dumazet kfree_skb(segs); 648bd8a7036SEric Dumazet segs = next; 649bd8a7036SEric Dumazet } 650bd8a7036SEric Dumazet } 651bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 652bd8a7036SEric Dumazet 653d1a203eaSStephen Hemminger /** 65425121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 65525121173SMichael S. Tsirkin * @skb: buffer that triggered an error 65625121173SMichael S. Tsirkin * 65725121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 65825121173SMichael S. Tsirkin * skb must be freed afterwards. 65925121173SMichael S. Tsirkin */ 66025121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 66125121173SMichael S. Tsirkin { 66225121173SMichael S. Tsirkin if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 66325121173SMichael S. Tsirkin struct ubuf_info *uarg; 66425121173SMichael S. Tsirkin 66525121173SMichael S. Tsirkin uarg = skb_shinfo(skb)->destructor_arg; 66625121173SMichael S. Tsirkin if (uarg->callback) 66725121173SMichael S. Tsirkin uarg->callback(uarg, false); 66825121173SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 66925121173SMichael S. Tsirkin } 67025121173SMichael S. Tsirkin } 67125121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 67225121173SMichael S. Tsirkin 67325121173SMichael S. Tsirkin /** 674ead2ceb0SNeil Horman * consume_skb - free an skbuff 675ead2ceb0SNeil Horman * @skb: buffer to free 676ead2ceb0SNeil Horman * 677ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 678ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 679ead2ceb0SNeil Horman * is being dropped after a failure and notes that 680ead2ceb0SNeil Horman */ 681ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 682ead2ceb0SNeil Horman { 683ead2ceb0SNeil Horman if (unlikely(!skb)) 684ead2ceb0SNeil Horman return; 685ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 686ead2ceb0SNeil Horman smp_rmb(); 687ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 688ead2ceb0SNeil Horman return; 68907dc22e7SKoki Sanagi trace_consume_skb(skb); 690ead2ceb0SNeil Horman __kfree_skb(skb); 691ead2ceb0SNeil Horman } 692ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 693ead2ceb0SNeil Horman 694b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 695b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 696b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 697b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 698b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 699b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 700b1937227SEric Dumazet 701dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 702dec18810SHerbert Xu { 703dec18810SHerbert Xu new->tstamp = old->tstamp; 704b1937227SEric Dumazet /* We do not copy old->sk */ 705dec18810SHerbert Xu new->dev = old->dev; 706b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 7077fee226aSEric Dumazet skb_dst_copy(new, old); 708def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 709dec18810SHerbert Xu new->sp = secpath_get(old->sp); 710dec18810SHerbert Xu #endif 711b1937227SEric Dumazet __nf_copy(new, old, false); 7126aa895b0SPatrick McHardy 713b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 714b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 715b1937227SEric Dumazet */ 716b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 71706021292SEliezer Tamir 718b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 719b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 720b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 721b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 722b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 723b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 724b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 725b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 726b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 727b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 728b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 729b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 730b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 731b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 732b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 733b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 734b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 735b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 736b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 737b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 738b1937227SEric Dumazet #endif 739e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 740b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 74106021292SEliezer Tamir #endif 742b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 743b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 744b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT 745b1937227SEric Dumazet CHECK_SKB_FIELD(tc_verd); 746b1937227SEric Dumazet #endif 747b1937227SEric Dumazet #endif 748b1937227SEric Dumazet 749dec18810SHerbert Xu } 750dec18810SHerbert Xu 75182c49a35SHerbert Xu /* 75282c49a35SHerbert Xu * You should not add any new code to this function. Add it to 75382c49a35SHerbert Xu * __copy_skb_header above instead. 75482c49a35SHerbert Xu */ 755e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 7561da177e4SLinus Torvalds { 7571da177e4SLinus Torvalds #define C(x) n->x = skb->x 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds n->next = n->prev = NULL; 7601da177e4SLinus Torvalds n->sk = NULL; 761dec18810SHerbert Xu __copy_skb_header(n, skb); 762dec18810SHerbert Xu 7631da177e4SLinus Torvalds C(len); 7641da177e4SLinus Torvalds C(data_len); 7653e6b3b2eSAlexey Dobriyan C(mac_len); 766334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 76702f1c89dSPaul Moore n->cloned = 1; 7681da177e4SLinus Torvalds n->nohdr = 0; 7691da177e4SLinus Torvalds n->destructor = NULL; 7701da177e4SLinus Torvalds C(tail); 7711da177e4SLinus Torvalds C(end); 77202f1c89dSPaul Moore C(head); 773d3836f21SEric Dumazet C(head_frag); 77402f1c89dSPaul Moore C(data); 77502f1c89dSPaul Moore C(truesize); 77602f1c89dSPaul Moore atomic_set(&n->users, 1); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 7791da177e4SLinus Torvalds skb->cloned = 1; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds return n; 782e0053ec0SHerbert Xu #undef C 783e0053ec0SHerbert Xu } 784e0053ec0SHerbert Xu 785e0053ec0SHerbert Xu /** 786e0053ec0SHerbert Xu * skb_morph - morph one skb into another 787e0053ec0SHerbert Xu * @dst: the skb to receive the contents 788e0053ec0SHerbert Xu * @src: the skb to supply the contents 789e0053ec0SHerbert Xu * 790e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 791e0053ec0SHerbert Xu * supplied by the user. 792e0053ec0SHerbert Xu * 793e0053ec0SHerbert Xu * The target skb is returned upon exit. 794e0053ec0SHerbert Xu */ 795e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 796e0053ec0SHerbert Xu { 7972d4baff8SHerbert Xu skb_release_all(dst); 798e0053ec0SHerbert Xu return __skb_clone(dst, src); 799e0053ec0SHerbert Xu } 800e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 801e0053ec0SHerbert Xu 8022c53040fSBen Hutchings /** 8032c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 80448c83012SMichael S. Tsirkin * @skb: the skb to modify 80548c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 80648c83012SMichael S. Tsirkin * 80748c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 80848c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 80948c83012SMichael S. Tsirkin * to userspace pages. 81048c83012SMichael S. Tsirkin * 81148c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 81248c83012SMichael S. Tsirkin * %GFP_ATOMIC. 81348c83012SMichael S. Tsirkin * 81448c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 81548c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 81648c83012SMichael S. Tsirkin */ 81748c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 818a6686f2fSShirley Ma { 819a6686f2fSShirley Ma int i; 820a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 821a6686f2fSShirley Ma struct page *page, *head = NULL; 822a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 823a6686f2fSShirley Ma 824a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 825a6686f2fSShirley Ma u8 *vaddr; 826a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 827a6686f2fSShirley Ma 82802756ed4SKrishna Kumar page = alloc_page(gfp_mask); 829a6686f2fSShirley Ma if (!page) { 830a6686f2fSShirley Ma while (head) { 83140dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 832a6686f2fSShirley Ma put_page(head); 833a6686f2fSShirley Ma head = next; 834a6686f2fSShirley Ma } 835a6686f2fSShirley Ma return -ENOMEM; 836a6686f2fSShirley Ma } 83751c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 838a6686f2fSShirley Ma memcpy(page_address(page), 8399e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 84051c56b00SEric Dumazet kunmap_atomic(vaddr); 84140dadff2SSunghan Suh set_page_private(page, (unsigned long)head); 842a6686f2fSShirley Ma head = page; 843a6686f2fSShirley Ma } 844a6686f2fSShirley Ma 845a6686f2fSShirley Ma /* skb frags release userspace buffers */ 84602756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 847a8605c60SIan Campbell skb_frag_unref(skb, i); 848a6686f2fSShirley Ma 849e19d6763SMichael S. Tsirkin uarg->callback(uarg, false); 850a6686f2fSShirley Ma 851a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 85202756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 85302756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 85402756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 85540dadff2SSunghan Suh head = (struct page *)page_private(head); 856a6686f2fSShirley Ma } 85748c83012SMichael S. Tsirkin 85848c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 859a6686f2fSShirley Ma return 0; 860a6686f2fSShirley Ma } 861dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 862a6686f2fSShirley Ma 863e0053ec0SHerbert Xu /** 864e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 865e0053ec0SHerbert Xu * @skb: buffer to clone 866e0053ec0SHerbert Xu * @gfp_mask: allocation priority 867e0053ec0SHerbert Xu * 868e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 869e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 870e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 871e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 872e0053ec0SHerbert Xu * 873e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 874e0053ec0SHerbert Xu * %GFP_ATOMIC. 875e0053ec0SHerbert Xu */ 876e0053ec0SHerbert Xu 877e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 878e0053ec0SHerbert Xu { 879d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 880d0bf4a9eSEric Dumazet struct sk_buff_fclones, 881d0bf4a9eSEric Dumazet skb1); 882d0bf4a9eSEric Dumazet struct sk_buff *n = &fclones->skb2; 883e0053ec0SHerbert Xu 88470008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 885a6686f2fSShirley Ma return NULL; 886a6686f2fSShirley Ma 887e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 888c8753d55SVijay Subramanian n->fclone == SKB_FCLONE_FREE) { 889e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 890ce1a4ea3SEric Dumazet /* As our fastclone was free, clone_ref must be 1 at this point. 891ce1a4ea3SEric Dumazet * We could use atomic_inc() here, but it is faster 892ce1a4ea3SEric Dumazet * to set the final value. 893ce1a4ea3SEric Dumazet */ 894ce1a4ea3SEric Dumazet atomic_set(&fclones->fclone_ref, 2); 895e0053ec0SHerbert Xu } else { 896c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 897c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 898c93bdd0eSMel Gorman 899e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 900e0053ec0SHerbert Xu if (!n) 901e0053ec0SHerbert Xu return NULL; 902fe55f6d5SVegard Nossum 903fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 904e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 905e0053ec0SHerbert Xu } 906e0053ec0SHerbert Xu 907e0053ec0SHerbert Xu return __skb_clone(n, skb); 9081da177e4SLinus Torvalds } 909b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 9101da177e4SLinus Torvalds 911f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 912f5b17294SPravin B Shelar { 913030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 914030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 915030737bcSEric Dumazet skb->csum_start += off; 916f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 917f5b17294SPravin B Shelar skb->transport_header += off; 918f5b17294SPravin B Shelar skb->network_header += off; 919f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 920f5b17294SPravin B Shelar skb->mac_header += off; 921f5b17294SPravin B Shelar skb->inner_transport_header += off; 922f5b17294SPravin B Shelar skb->inner_network_header += off; 923aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 924f5b17294SPravin B Shelar } 925f5b17294SPravin B Shelar 9261da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 9271da177e4SLinus Torvalds { 928dec18810SHerbert Xu __copy_skb_header(new, old); 929dec18810SHerbert Xu 9307967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 9317967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 9327967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 935c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 936c93bdd0eSMel Gorman { 937c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 938c93bdd0eSMel Gorman return SKB_ALLOC_RX; 939c93bdd0eSMel Gorman return 0; 940c93bdd0eSMel Gorman } 941c93bdd0eSMel Gorman 9421da177e4SLinus Torvalds /** 9431da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 9441da177e4SLinus Torvalds * @skb: buffer to copy 9451da177e4SLinus Torvalds * @gfp_mask: allocation priority 9461da177e4SLinus Torvalds * 9471da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 9481da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 9491da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 9501da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 9511da177e4SLinus Torvalds * 9521da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 9531da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 9541da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 9551da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 9561da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 9571da177e4SLinus Torvalds */ 9581da177e4SLinus Torvalds 959dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 9601da177e4SLinus Torvalds { 9616602cebbSEric Dumazet int headerlen = skb_headroom(skb); 962ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 963c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 964c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 9656602cebbSEric Dumazet 9661da177e4SLinus Torvalds if (!n) 9671da177e4SLinus Torvalds return NULL; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* Set the data pointer */ 9701da177e4SLinus Torvalds skb_reserve(n, headerlen); 9711da177e4SLinus Torvalds /* Set the tail pointer and length */ 9721da177e4SLinus Torvalds skb_put(n, skb->len); 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 9751da177e4SLinus Torvalds BUG(); 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds copy_skb_header(n, skb); 9781da177e4SLinus Torvalds return n; 9791da177e4SLinus Torvalds } 980b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds /** 983bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 9841da177e4SLinus Torvalds * @skb: buffer to copy 985117632e6SEric Dumazet * @headroom: headroom of new skb 9861da177e4SLinus Torvalds * @gfp_mask: allocation priority 987bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 988bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 989bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 9901da177e4SLinus Torvalds * 9911da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 9921da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 9931da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 9941da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 9951da177e4SLinus Torvalds * or the pointer to the buffer on success. 9961da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 9971da177e4SLinus Torvalds */ 9981da177e4SLinus Torvalds 999bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1000bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 10011da177e4SLinus Torvalds { 1002117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1003bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1004bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 10056602cebbSEric Dumazet 10061da177e4SLinus Torvalds if (!n) 10071da177e4SLinus Torvalds goto out; 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds /* Set the data pointer */ 1010117632e6SEric Dumazet skb_reserve(n, headroom); 10111da177e4SLinus Torvalds /* Set the tail pointer and length */ 10121da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 10131da177e4SLinus Torvalds /* Copy the bytes */ 1014d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 10151da177e4SLinus Torvalds 101625f484a6SHerbert Xu n->truesize += skb->data_len; 10171da177e4SLinus Torvalds n->data_len = skb->data_len; 10181da177e4SLinus Torvalds n->len = skb->len; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 10211da177e4SLinus Torvalds int i; 10221da177e4SLinus Torvalds 102370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 10241511022cSDan Carpenter kfree_skb(n); 10251511022cSDan Carpenter n = NULL; 1026a6686f2fSShirley Ma goto out; 1027a6686f2fSShirley Ma } 10281da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 10291da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1030ea2ab693SIan Campbell skb_frag_ref(skb, i); 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 103521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 10361da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 10371da177e4SLinus Torvalds skb_clone_fraglist(n); 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds copy_skb_header(n, skb); 10411da177e4SLinus Torvalds out: 10421da177e4SLinus Torvalds return n; 10431da177e4SLinus Torvalds } 1044bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds /** 10471da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 10481da177e4SLinus Torvalds * @skb: buffer to reallocate 10491da177e4SLinus Torvalds * @nhead: room to add at head 10501da177e4SLinus Torvalds * @ntail: room to add at tail 10511da177e4SLinus Torvalds * @gfp_mask: allocation priority 10521da177e4SLinus Torvalds * 1053bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1054bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 10551da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 10561da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 10571da177e4SLinus Torvalds * 10581da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 10591da177e4SLinus Torvalds * reloaded after call to this function. 10601da177e4SLinus Torvalds */ 10611da177e4SLinus Torvalds 106286a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1063dd0fc66fSAl Viro gfp_t gfp_mask) 10641da177e4SLinus Torvalds { 10651da177e4SLinus Torvalds int i; 10661da177e4SLinus Torvalds u8 *data; 1067ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 10681da177e4SLinus Torvalds long off; 10691da177e4SLinus Torvalds 10704edd87adSHerbert Xu BUG_ON(nhead < 0); 10714edd87adSHerbert Xu 10721da177e4SLinus Torvalds if (skb_shared(skb)) 10731da177e4SLinus Torvalds BUG(); 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 10761da177e4SLinus Torvalds 1077c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1078c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1079c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1080c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 10811da177e4SLinus Torvalds if (!data) 10821da177e4SLinus Torvalds goto nodata; 108387151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 10866602cebbSEric Dumazet * optimized for the cases when header is void. 10876602cebbSEric Dumazet */ 10886602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 10896602cebbSEric Dumazet 10906602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 10916602cebbSEric Dumazet skb_shinfo(skb), 1092fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 10931da177e4SLinus Torvalds 10943e24591aSAlexander Duyck /* 10953e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 10963e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 10973e24591aSAlexander Duyck * be since all we did is relocate the values 10983e24591aSAlexander Duyck */ 10993e24591aSAlexander Duyck if (skb_cloned(skb)) { 1100a6686f2fSShirley Ma /* copy this zero copy skb frags */ 110170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1102a6686f2fSShirley Ma goto nofrags; 11031da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1104ea2ab693SIan Campbell skb_frag_ref(skb, i); 11051da177e4SLinus Torvalds 110621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 11071da177e4SLinus Torvalds skb_clone_fraglist(skb); 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds skb_release_data(skb); 11103e24591aSAlexander Duyck } else { 11113e24591aSAlexander Duyck skb_free_head(skb); 11121fd63041SEric Dumazet } 11131da177e4SLinus Torvalds off = (data + nhead) - skb->head; 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds skb->head = data; 1116d3836f21SEric Dumazet skb->head_frag = 0; 11171da177e4SLinus Torvalds skb->data += off; 11184305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 11194305b541SArnaldo Carvalho de Melo skb->end = size; 112056eb8882SPatrick McHardy off = nhead; 11214305b541SArnaldo Carvalho de Melo #else 11224305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 112356eb8882SPatrick McHardy #endif 112427a884dcSArnaldo Carvalho de Melo skb->tail += off; 1125b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 11261da177e4SLinus Torvalds skb->cloned = 0; 1127334a8132SPatrick McHardy skb->hdr_len = 0; 11281da177e4SLinus Torvalds skb->nohdr = 0; 11291da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 11301da177e4SLinus Torvalds return 0; 11311da177e4SLinus Torvalds 1132a6686f2fSShirley Ma nofrags: 1133a6686f2fSShirley Ma kfree(data); 11341da177e4SLinus Torvalds nodata: 11351da177e4SLinus Torvalds return -ENOMEM; 11361da177e4SLinus Torvalds } 1137b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 11421da177e4SLinus Torvalds { 11431da177e4SLinus Torvalds struct sk_buff *skb2; 11441da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds if (delta <= 0) 11471da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 11481da177e4SLinus Torvalds else { 11491da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 11501da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 11511da177e4SLinus Torvalds GFP_ATOMIC)) { 11521da177e4SLinus Torvalds kfree_skb(skb2); 11531da177e4SLinus Torvalds skb2 = NULL; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds return skb2; 11571da177e4SLinus Torvalds } 1158b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds /** 11611da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 11621da177e4SLinus Torvalds * @skb: buffer to copy 11631da177e4SLinus Torvalds * @newheadroom: new free bytes at head 11641da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 11651da177e4SLinus Torvalds * @gfp_mask: allocation priority 11661da177e4SLinus Torvalds * 11671da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 11681da177e4SLinus Torvalds * allocate additional space. 11691da177e4SLinus Torvalds * 11701da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 11711da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 11721da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 11731da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 11741da177e4SLinus Torvalds * 11751da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 11761da177e4SLinus Torvalds * is called from an interrupt. 11771da177e4SLinus Torvalds */ 11781da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 117986a76cafSVictor Fusco int newheadroom, int newtailroom, 1180dd0fc66fSAl Viro gfp_t gfp_mask) 11811da177e4SLinus Torvalds { 11821da177e4SLinus Torvalds /* 11831da177e4SLinus Torvalds * Allocate the copy buffer 11841da177e4SLinus Torvalds */ 1185c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1186c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1187c93bdd0eSMel Gorman NUMA_NO_NODE); 1188efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 11891da177e4SLinus Torvalds int head_copy_len, head_copy_off; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds if (!n) 11921da177e4SLinus Torvalds return NULL; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds skb_reserve(n, newheadroom); 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /* Set the tail pointer and length */ 11971da177e4SLinus Torvalds skb_put(n, skb->len); 11981da177e4SLinus Torvalds 1199efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 12001da177e4SLinus Torvalds head_copy_off = 0; 12011da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 12021da177e4SLinus Torvalds head_copy_len = newheadroom; 12031da177e4SLinus Torvalds else 12041da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds /* Copy the linear header and data. */ 12071da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 12081da177e4SLinus Torvalds skb->len + head_copy_len)) 12091da177e4SLinus Torvalds BUG(); 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds copy_skb_header(n, skb); 12121da177e4SLinus Torvalds 1213030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1214efd1e8d5SPatrick McHardy 12151da177e4SLinus Torvalds return n; 12161da177e4SLinus Torvalds } 1217b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds /** 12201da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 12211da177e4SLinus Torvalds * @skb: buffer to pad 12221da177e4SLinus Torvalds * @pad: space to pad 12231da177e4SLinus Torvalds * 12241da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 12251da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 12261da177e4SLinus Torvalds * beyond the buffer end onto the wire. 12271da177e4SLinus Torvalds * 12285b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 12291da177e4SLinus Torvalds */ 12301da177e4SLinus Torvalds 12315b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 12321da177e4SLinus Torvalds { 12335b057c6bSHerbert Xu int err; 12345b057c6bSHerbert Xu int ntail; 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 12375b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 12381da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 12395b057c6bSHerbert Xu return 0; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 12424305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 12435b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 12445b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 12455b057c6bSHerbert Xu if (unlikely(err)) 12465b057c6bSHerbert Xu goto free_skb; 12475b057c6bSHerbert Xu } 12485b057c6bSHerbert Xu 12495b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 12505b057c6bSHerbert Xu * to be audited. 12515b057c6bSHerbert Xu */ 12525b057c6bSHerbert Xu err = skb_linearize(skb); 12535b057c6bSHerbert Xu if (unlikely(err)) 12545b057c6bSHerbert Xu goto free_skb; 12555b057c6bSHerbert Xu 12565b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 12575b057c6bSHerbert Xu return 0; 12585b057c6bSHerbert Xu 12595b057c6bSHerbert Xu free_skb: 12601da177e4SLinus Torvalds kfree_skb(skb); 12615b057c6bSHerbert Xu return err; 12621da177e4SLinus Torvalds } 1263b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 12641da177e4SLinus Torvalds 12650dde3e16SIlpo Järvinen /** 12660c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 12670c7ddf36SMathias Krause * @skb: start of the buffer to use 12680c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 12690c7ddf36SMathias Krause * @len: amount of data to add 12700c7ddf36SMathias Krause * 12710c7ddf36SMathias Krause * This function extends the used data area of the potentially 12720c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 12730c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 12740c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 12750c7ddf36SMathias Krause * returned. 12760c7ddf36SMathias Krause */ 12770c7ddf36SMathias Krause 12780c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 12790c7ddf36SMathias Krause { 12800c7ddf36SMathias Krause if (tail != skb) { 12810c7ddf36SMathias Krause skb->data_len += len; 12820c7ddf36SMathias Krause skb->len += len; 12830c7ddf36SMathias Krause } 12840c7ddf36SMathias Krause return skb_put(tail, len); 12850c7ddf36SMathias Krause } 12860c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 12870c7ddf36SMathias Krause 12880c7ddf36SMathias Krause /** 12890dde3e16SIlpo Järvinen * skb_put - add data to a buffer 12900dde3e16SIlpo Järvinen * @skb: buffer to use 12910dde3e16SIlpo Järvinen * @len: amount of data to add 12920dde3e16SIlpo Järvinen * 12930dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 12940dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 12950dde3e16SIlpo Järvinen * first byte of the extra data is returned. 12960dde3e16SIlpo Järvinen */ 12970dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 12980dde3e16SIlpo Järvinen { 12990dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 13000dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 13010dde3e16SIlpo Järvinen skb->tail += len; 13020dde3e16SIlpo Järvinen skb->len += len; 13030dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 13040dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 13050dde3e16SIlpo Järvinen return tmp; 13060dde3e16SIlpo Järvinen } 13070dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 13080dde3e16SIlpo Järvinen 13096be8ac2fSIlpo Järvinen /** 1310c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1311c2aa270aSIlpo Järvinen * @skb: buffer to use 1312c2aa270aSIlpo Järvinen * @len: amount of data to add 1313c2aa270aSIlpo Järvinen * 1314c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1315c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1316c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1317c2aa270aSIlpo Järvinen */ 1318c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1319c2aa270aSIlpo Järvinen { 1320c2aa270aSIlpo Järvinen skb->data -= len; 1321c2aa270aSIlpo Järvinen skb->len += len; 1322c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1323c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1324c2aa270aSIlpo Järvinen return skb->data; 1325c2aa270aSIlpo Järvinen } 1326c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1327c2aa270aSIlpo Järvinen 1328c2aa270aSIlpo Järvinen /** 13296be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 13306be8ac2fSIlpo Järvinen * @skb: buffer to use 13316be8ac2fSIlpo Järvinen * @len: amount of data to remove 13326be8ac2fSIlpo Järvinen * 13336be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 13346be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 13356be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 13366be8ac2fSIlpo Järvinen * the old data. 13376be8ac2fSIlpo Järvinen */ 13386be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 13396be8ac2fSIlpo Järvinen { 134047d29646SDavid S. Miller return skb_pull_inline(skb, len); 13416be8ac2fSIlpo Järvinen } 13426be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 13436be8ac2fSIlpo Järvinen 1344419ae74eSIlpo Järvinen /** 1345419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1346419ae74eSIlpo Järvinen * @skb: buffer to alter 1347419ae74eSIlpo Järvinen * @len: new length 1348419ae74eSIlpo Järvinen * 1349419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1350419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1351419ae74eSIlpo Järvinen * The skb must be linear. 1352419ae74eSIlpo Järvinen */ 1353419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1354419ae74eSIlpo Järvinen { 1355419ae74eSIlpo Järvinen if (skb->len > len) 1356419ae74eSIlpo Järvinen __skb_trim(skb, len); 1357419ae74eSIlpo Järvinen } 1358419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1359419ae74eSIlpo Järvinen 13603cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 13611da177e4SLinus Torvalds */ 13621da177e4SLinus Torvalds 13633cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 13641da177e4SLinus Torvalds { 136527b437c8SHerbert Xu struct sk_buff **fragp; 136627b437c8SHerbert Xu struct sk_buff *frag; 13671da177e4SLinus Torvalds int offset = skb_headlen(skb); 13681da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 13691da177e4SLinus Torvalds int i; 137027b437c8SHerbert Xu int err; 137127b437c8SHerbert Xu 137227b437c8SHerbert Xu if (skb_cloned(skb) && 137327b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 137427b437c8SHerbert Xu return err; 13751da177e4SLinus Torvalds 1376f4d26fb3SHerbert Xu i = 0; 1377f4d26fb3SHerbert Xu if (offset >= len) 1378f4d26fb3SHerbert Xu goto drop_pages; 1379f4d26fb3SHerbert Xu 1380f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 13819e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 138227b437c8SHerbert Xu 138327b437c8SHerbert Xu if (end < len) { 13841da177e4SLinus Torvalds offset = end; 138527b437c8SHerbert Xu continue; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 13889e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 138927b437c8SHerbert Xu 1390f4d26fb3SHerbert Xu drop_pages: 139127b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 139227b437c8SHerbert Xu 139327b437c8SHerbert Xu for (; i < nfrags; i++) 1394ea2ab693SIan Campbell skb_frag_unref(skb, i); 139527b437c8SHerbert Xu 139621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 139727b437c8SHerbert Xu skb_drop_fraglist(skb); 1398f4d26fb3SHerbert Xu goto done; 139927b437c8SHerbert Xu } 140027b437c8SHerbert Xu 140127b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 140227b437c8SHerbert Xu fragp = &frag->next) { 140327b437c8SHerbert Xu int end = offset + frag->len; 140427b437c8SHerbert Xu 140527b437c8SHerbert Xu if (skb_shared(frag)) { 140627b437c8SHerbert Xu struct sk_buff *nfrag; 140727b437c8SHerbert Xu 140827b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 140927b437c8SHerbert Xu if (unlikely(!nfrag)) 141027b437c8SHerbert Xu return -ENOMEM; 141127b437c8SHerbert Xu 141227b437c8SHerbert Xu nfrag->next = frag->next; 141385bb2a60SEric Dumazet consume_skb(frag); 141427b437c8SHerbert Xu frag = nfrag; 141527b437c8SHerbert Xu *fragp = frag; 141627b437c8SHerbert Xu } 141727b437c8SHerbert Xu 141827b437c8SHerbert Xu if (end < len) { 141927b437c8SHerbert Xu offset = end; 142027b437c8SHerbert Xu continue; 142127b437c8SHerbert Xu } 142227b437c8SHerbert Xu 142327b437c8SHerbert Xu if (end > len && 142427b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 142527b437c8SHerbert Xu return err; 142627b437c8SHerbert Xu 142727b437c8SHerbert Xu if (frag->next) 142827b437c8SHerbert Xu skb_drop_list(&frag->next); 142927b437c8SHerbert Xu break; 143027b437c8SHerbert Xu } 143127b437c8SHerbert Xu 1432f4d26fb3SHerbert Xu done: 143327b437c8SHerbert Xu if (len > skb_headlen(skb)) { 14341da177e4SLinus Torvalds skb->data_len -= skb->len - len; 14351da177e4SLinus Torvalds skb->len = len; 14361da177e4SLinus Torvalds } else { 14371da177e4SLinus Torvalds skb->len = len; 14381da177e4SLinus Torvalds skb->data_len = 0; 143927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 14401da177e4SLinus Torvalds } 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds return 0; 14431da177e4SLinus Torvalds } 1444b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds /** 14471da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 14481da177e4SLinus Torvalds * @skb: buffer to reallocate 14491da177e4SLinus Torvalds * @delta: number of bytes to advance tail 14501da177e4SLinus Torvalds * 14511da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 14521da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 14531da177e4SLinus Torvalds * data from fragmented part. 14541da177e4SLinus Torvalds * 14551da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 14561da177e4SLinus Torvalds * 14571da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 14581da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 14591da177e4SLinus Torvalds * 14601da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 14611da177e4SLinus Torvalds * reloaded after call to this function. 14621da177e4SLinus Torvalds */ 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 14651da177e4SLinus Torvalds * when it is necessary. 14661da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 14671da177e4SLinus Torvalds * 2. It may change skb pointers. 14681da177e4SLinus Torvalds * 14691da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 14701da177e4SLinus Torvalds */ 14711da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 14721da177e4SLinus Torvalds { 14731da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 14741da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 14751da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 14761da177e4SLinus Torvalds */ 14774305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 14801da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 14811da177e4SLinus Torvalds GFP_ATOMIC)) 14821da177e4SLinus Torvalds return NULL; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 148527a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 14861da177e4SLinus Torvalds BUG(); 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 14891da177e4SLinus Torvalds * size of pulled pages. Superb. 14901da177e4SLinus Torvalds */ 149121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 14921da177e4SLinus Torvalds goto pull_pages; 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 14951da177e4SLinus Torvalds eat = delta; 14961da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14979e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 14989e903e08SEric Dumazet 14999e903e08SEric Dumazet if (size >= eat) 15001da177e4SLinus Torvalds goto pull_pages; 15019e903e08SEric Dumazet eat -= size; 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 15051da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 15061da177e4SLinus Torvalds * but taking into account that pulling is expected to 15071da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 15081da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 15091da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 15101da177e4SLinus Torvalds */ 15111da177e4SLinus Torvalds if (eat) { 15121da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 15131da177e4SLinus Torvalds struct sk_buff *clone = NULL; 15141da177e4SLinus Torvalds struct sk_buff *insp = NULL; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds do { 151709a62660SKris Katterjohn BUG_ON(!list); 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds if (list->len <= eat) { 15201da177e4SLinus Torvalds /* Eaten as whole. */ 15211da177e4SLinus Torvalds eat -= list->len; 15221da177e4SLinus Torvalds list = list->next; 15231da177e4SLinus Torvalds insp = list; 15241da177e4SLinus Torvalds } else { 15251da177e4SLinus Torvalds /* Eaten partially. */ 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds if (skb_shared(list)) { 15281da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 15291da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 15301da177e4SLinus Torvalds if (!clone) 15311da177e4SLinus Torvalds return NULL; 15321da177e4SLinus Torvalds insp = list->next; 15331da177e4SLinus Torvalds list = clone; 15341da177e4SLinus Torvalds } else { 15351da177e4SLinus Torvalds /* This may be pulled without 15361da177e4SLinus Torvalds * problems. */ 15371da177e4SLinus Torvalds insp = list; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 15401da177e4SLinus Torvalds kfree_skb(clone); 15411da177e4SLinus Torvalds return NULL; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds break; 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds } while (eat); 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds /* Free pulled out fragments. */ 15481da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 15491da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 15501da177e4SLinus Torvalds kfree_skb(list); 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds /* And insert new clone at head. */ 15531da177e4SLinus Torvalds if (clone) { 15541da177e4SLinus Torvalds clone->next = list; 15551da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds pull_pages: 15611da177e4SLinus Torvalds eat = delta; 15621da177e4SLinus Torvalds k = 0; 15631da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15649e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 15659e903e08SEric Dumazet 15669e903e08SEric Dumazet if (size <= eat) { 1567ea2ab693SIan Campbell skb_frag_unref(skb, i); 15689e903e08SEric Dumazet eat -= size; 15691da177e4SLinus Torvalds } else { 15701da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 15711da177e4SLinus Torvalds if (eat) { 15721da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 15739e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 15741da177e4SLinus Torvalds eat = 0; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds k++; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds skb->tail += delta; 15821da177e4SLinus Torvalds skb->data_len -= delta; 15831da177e4SLinus Torvalds 158427a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 15851da177e4SLinus Torvalds } 1586b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 15871da177e4SLinus Torvalds 158822019b17SEric Dumazet /** 158922019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 159022019b17SEric Dumazet * @skb: source skb 159122019b17SEric Dumazet * @offset: offset in source 159222019b17SEric Dumazet * @to: destination buffer 159322019b17SEric Dumazet * @len: number of bytes to copy 159422019b17SEric Dumazet * 159522019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 159622019b17SEric Dumazet * destination buffer. 159722019b17SEric Dumazet * 159822019b17SEric Dumazet * CAUTION ! : 159922019b17SEric Dumazet * If its prototype is ever changed, 160022019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 160122019b17SEric Dumazet * since it is called from BPF assembly code. 160222019b17SEric Dumazet */ 16031da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 16041da177e4SLinus Torvalds { 16051a028e50SDavid S. Miller int start = skb_headlen(skb); 1606fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1607fbb398a8SDavid S. Miller int i, copy; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds if (offset > (int)skb->len - len) 16101da177e4SLinus Torvalds goto fault; 16111da177e4SLinus Torvalds 16121da177e4SLinus Torvalds /* Copy header. */ 16131a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 16141da177e4SLinus Torvalds if (copy > len) 16151da177e4SLinus Torvalds copy = len; 1616d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 16171da177e4SLinus Torvalds if ((len -= copy) == 0) 16181da177e4SLinus Torvalds return 0; 16191da177e4SLinus Torvalds offset += copy; 16201da177e4SLinus Torvalds to += copy; 16211da177e4SLinus Torvalds } 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16241a028e50SDavid S. Miller int end; 162551c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 16261da177e4SLinus Torvalds 1627547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16281a028e50SDavid S. Miller 162951c56b00SEric Dumazet end = start + skb_frag_size(f); 16301da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16311da177e4SLinus Torvalds u8 *vaddr; 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds if (copy > len) 16341da177e4SLinus Torvalds copy = len; 16351da177e4SLinus Torvalds 163651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 16371da177e4SLinus Torvalds memcpy(to, 163851c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 163951c56b00SEric Dumazet copy); 164051c56b00SEric Dumazet kunmap_atomic(vaddr); 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds if ((len -= copy) == 0) 16431da177e4SLinus Torvalds return 0; 16441da177e4SLinus Torvalds offset += copy; 16451da177e4SLinus Torvalds to += copy; 16461da177e4SLinus Torvalds } 16471a028e50SDavid S. Miller start = end; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 1650fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 16511a028e50SDavid S. Miller int end; 16521da177e4SLinus Torvalds 1653547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16541a028e50SDavid S. Miller 1655fbb398a8SDavid S. Miller end = start + frag_iter->len; 16561da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16571da177e4SLinus Torvalds if (copy > len) 16581da177e4SLinus Torvalds copy = len; 1659fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 16601da177e4SLinus Torvalds goto fault; 16611da177e4SLinus Torvalds if ((len -= copy) == 0) 16621da177e4SLinus Torvalds return 0; 16631da177e4SLinus Torvalds offset += copy; 16641da177e4SLinus Torvalds to += copy; 16651da177e4SLinus Torvalds } 16661a028e50SDavid S. Miller start = end; 16671da177e4SLinus Torvalds } 1668a6686f2fSShirley Ma 16691da177e4SLinus Torvalds if (!len) 16701da177e4SLinus Torvalds return 0; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds fault: 16731da177e4SLinus Torvalds return -EFAULT; 16741da177e4SLinus Torvalds } 1675b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 16761da177e4SLinus Torvalds 16779c55e01cSJens Axboe /* 16789c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 16799c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 16809c55e01cSJens Axboe */ 16819c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 16829c55e01cSJens Axboe { 16838b9d3728SJarek Poplawski put_page(spd->pages[i]); 16848b9d3728SJarek Poplawski } 16859c55e01cSJens Axboe 1686a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 16874fb66994SJarek Poplawski unsigned int *offset, 168818aafc62SEric Dumazet struct sock *sk) 16898b9d3728SJarek Poplawski { 16905640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 16918b9d3728SJarek Poplawski 16925640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 16938b9d3728SJarek Poplawski return NULL; 16944fb66994SJarek Poplawski 16955640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 16964fb66994SJarek Poplawski 16975640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 16985640f768SEric Dumazet page_address(page) + *offset, *len); 16995640f768SEric Dumazet *offset = pfrag->offset; 17005640f768SEric Dumazet pfrag->offset += *len; 17014fb66994SJarek Poplawski 17025640f768SEric Dumazet return pfrag->page; 17039c55e01cSJens Axboe } 17049c55e01cSJens Axboe 170541c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 170641c73a0dSEric Dumazet struct page *page, 170741c73a0dSEric Dumazet unsigned int offset) 170841c73a0dSEric Dumazet { 170941c73a0dSEric Dumazet return spd->nr_pages && 171041c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 171141c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 171241c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 171341c73a0dSEric Dumazet } 171441c73a0dSEric Dumazet 17159c55e01cSJens Axboe /* 17169c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 17179c55e01cSJens Axboe */ 1718a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 171935f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 17204fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 172118aafc62SEric Dumazet bool linear, 17227a67e56fSJarek Poplawski struct sock *sk) 17239c55e01cSJens Axboe { 172441c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1725a108d5f3SDavid S. Miller return true; 17269c55e01cSJens Axboe 17278b9d3728SJarek Poplawski if (linear) { 172818aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 17298b9d3728SJarek Poplawski if (!page) 1730a108d5f3SDavid S. Miller return true; 173141c73a0dSEric Dumazet } 173241c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 173341c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1734a108d5f3SDavid S. Miller return false; 173541c73a0dSEric Dumazet } 17368b9d3728SJarek Poplawski get_page(page); 17379c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 17384fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 17399c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 17409c55e01cSJens Axboe spd->nr_pages++; 17418b9d3728SJarek Poplawski 1742a108d5f3SDavid S. Miller return false; 17439c55e01cSJens Axboe } 17449c55e01cSJens Axboe 1745a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 17462870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 174718aafc62SEric Dumazet unsigned int *len, 1748d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 174935f3d14dSJens Axboe struct sock *sk, 175035f3d14dSJens Axboe struct pipe_inode_info *pipe) 17519c55e01cSJens Axboe { 17522870c43dSOctavian Purdila if (!*len) 1753a108d5f3SDavid S. Miller return true; 17549c55e01cSJens Axboe 17552870c43dSOctavian Purdila /* skip this segment if already processed */ 17562870c43dSOctavian Purdila if (*off >= plen) { 17572870c43dSOctavian Purdila *off -= plen; 1758a108d5f3SDavid S. Miller return false; 17592870c43dSOctavian Purdila } 17602870c43dSOctavian Purdila 17612870c43dSOctavian Purdila /* ignore any bits we already processed */ 17629ca1b22dSEric Dumazet poff += *off; 17639ca1b22dSEric Dumazet plen -= *off; 17642870c43dSOctavian Purdila *off = 0; 17652870c43dSOctavian Purdila 176618aafc62SEric Dumazet do { 176718aafc62SEric Dumazet unsigned int flen = min(*len, plen); 17682870c43dSOctavian Purdila 176918aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 177018aafc62SEric Dumazet linear, sk)) 1771a108d5f3SDavid S. Miller return true; 177218aafc62SEric Dumazet poff += flen; 177318aafc62SEric Dumazet plen -= flen; 17742870c43dSOctavian Purdila *len -= flen; 177518aafc62SEric Dumazet } while (*len && plen); 17762870c43dSOctavian Purdila 1777a108d5f3SDavid S. Miller return false; 1778db43a282SOctavian Purdila } 17799c55e01cSJens Axboe 17809c55e01cSJens Axboe /* 1781a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 17822870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 17839c55e01cSJens Axboe */ 1784a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 178535f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 178635f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 17872870c43dSOctavian Purdila { 17882870c43dSOctavian Purdila int seg; 17899c55e01cSJens Axboe 17901d0c0b32SEric Dumazet /* map the linear part : 17912996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 17922996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 17932996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 17949c55e01cSJens Axboe */ 17952870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 17962870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 17972870c43dSOctavian Purdila skb_headlen(skb), 179818aafc62SEric Dumazet offset, len, spd, 17993a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 18001d0c0b32SEric Dumazet sk, pipe)) 1801a108d5f3SDavid S. Miller return true; 18029c55e01cSJens Axboe 18039c55e01cSJens Axboe /* 18049c55e01cSJens Axboe * then map the fragments 18059c55e01cSJens Axboe */ 18069c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 18079c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 18089c55e01cSJens Axboe 1809ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 18109e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 181118aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 1812a108d5f3SDavid S. Miller return true; 18139c55e01cSJens Axboe } 18149c55e01cSJens Axboe 1815a108d5f3SDavid S. Miller return false; 18169c55e01cSJens Axboe } 18179c55e01cSJens Axboe 18189c55e01cSJens Axboe /* 18199c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 18209c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 18219c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 18229c55e01cSJens Axboe * handle that cleanly. 18239c55e01cSJens Axboe */ 18248b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 18259c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 18269c55e01cSJens Axboe unsigned int flags) 18279c55e01cSJens Axboe { 182841c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 182941c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 18309c55e01cSJens Axboe struct splice_pipe_desc spd = { 18319c55e01cSJens Axboe .pages = pages, 18329c55e01cSJens Axboe .partial = partial, 1833047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 18349c55e01cSJens Axboe .flags = flags, 183528a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 18369c55e01cSJens Axboe .spd_release = sock_spd_release, 18379c55e01cSJens Axboe }; 1838fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 18397a67e56fSJarek Poplawski struct sock *sk = skb->sk; 184035f3d14dSJens Axboe int ret = 0; 184135f3d14dSJens Axboe 18429c55e01cSJens Axboe /* 18439c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 18449c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 18459c55e01cSJens Axboe */ 184635f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 18479c55e01cSJens Axboe goto done; 18489c55e01cSJens Axboe else if (!tlen) 18499c55e01cSJens Axboe goto done; 18509c55e01cSJens Axboe 18519c55e01cSJens Axboe /* 18529c55e01cSJens Axboe * now see if we have a frag_list to map 18539c55e01cSJens Axboe */ 1854fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1855fbb398a8SDavid S. Miller if (!tlen) 18569c55e01cSJens Axboe break; 185735f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1858fbb398a8SDavid S. Miller break; 18599c55e01cSJens Axboe } 18609c55e01cSJens Axboe 18619c55e01cSJens Axboe done: 18629c55e01cSJens Axboe if (spd.nr_pages) { 18639c55e01cSJens Axboe /* 18649c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 18659c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 18669c55e01cSJens Axboe * here as compared to sendfile(). We enter here 18679c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 18689c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 18699c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 18709c55e01cSJens Axboe * and networking will grab the socket lock. 18719c55e01cSJens Axboe */ 1872293ad604SOctavian Purdila release_sock(sk); 18739c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1874293ad604SOctavian Purdila lock_sock(sk); 18759c55e01cSJens Axboe } 18769c55e01cSJens Axboe 187735f3d14dSJens Axboe return ret; 18789c55e01cSJens Axboe } 18799c55e01cSJens Axboe 1880357b40a1SHerbert Xu /** 1881357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1882357b40a1SHerbert Xu * @skb: destination buffer 1883357b40a1SHerbert Xu * @offset: offset in destination 1884357b40a1SHerbert Xu * @from: source buffer 1885357b40a1SHerbert Xu * @len: number of bytes to copy 1886357b40a1SHerbert Xu * 1887357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1888357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1889357b40a1SHerbert Xu * traversing fragment lists and such. 1890357b40a1SHerbert Xu */ 1891357b40a1SHerbert Xu 18920c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1893357b40a1SHerbert Xu { 18941a028e50SDavid S. Miller int start = skb_headlen(skb); 1895fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1896fbb398a8SDavid S. Miller int i, copy; 1897357b40a1SHerbert Xu 1898357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1899357b40a1SHerbert Xu goto fault; 1900357b40a1SHerbert Xu 19011a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1902357b40a1SHerbert Xu if (copy > len) 1903357b40a1SHerbert Xu copy = len; 190427d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1905357b40a1SHerbert Xu if ((len -= copy) == 0) 1906357b40a1SHerbert Xu return 0; 1907357b40a1SHerbert Xu offset += copy; 1908357b40a1SHerbert Xu from += copy; 1909357b40a1SHerbert Xu } 1910357b40a1SHerbert Xu 1911357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1912357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19131a028e50SDavid S. Miller int end; 1914357b40a1SHerbert Xu 1915547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19161a028e50SDavid S. Miller 19179e903e08SEric Dumazet end = start + skb_frag_size(frag); 1918357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1919357b40a1SHerbert Xu u8 *vaddr; 1920357b40a1SHerbert Xu 1921357b40a1SHerbert Xu if (copy > len) 1922357b40a1SHerbert Xu copy = len; 1923357b40a1SHerbert Xu 192451c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19251a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 19261a028e50SDavid S. Miller from, copy); 192751c56b00SEric Dumazet kunmap_atomic(vaddr); 1928357b40a1SHerbert Xu 1929357b40a1SHerbert Xu if ((len -= copy) == 0) 1930357b40a1SHerbert Xu return 0; 1931357b40a1SHerbert Xu offset += copy; 1932357b40a1SHerbert Xu from += copy; 1933357b40a1SHerbert Xu } 19341a028e50SDavid S. Miller start = end; 1935357b40a1SHerbert Xu } 1936357b40a1SHerbert Xu 1937fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 19381a028e50SDavid S. Miller int end; 1939357b40a1SHerbert Xu 1940547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19411a028e50SDavid S. Miller 1942fbb398a8SDavid S. Miller end = start + frag_iter->len; 1943357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1944357b40a1SHerbert Xu if (copy > len) 1945357b40a1SHerbert Xu copy = len; 1946fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 19471a028e50SDavid S. Miller from, copy)) 1948357b40a1SHerbert Xu goto fault; 1949357b40a1SHerbert Xu if ((len -= copy) == 0) 1950357b40a1SHerbert Xu return 0; 1951357b40a1SHerbert Xu offset += copy; 1952357b40a1SHerbert Xu from += copy; 1953357b40a1SHerbert Xu } 19541a028e50SDavid S. Miller start = end; 1955357b40a1SHerbert Xu } 1956357b40a1SHerbert Xu if (!len) 1957357b40a1SHerbert Xu return 0; 1958357b40a1SHerbert Xu 1959357b40a1SHerbert Xu fault: 1960357b40a1SHerbert Xu return -EFAULT; 1961357b40a1SHerbert Xu } 1962357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1963357b40a1SHerbert Xu 19641da177e4SLinus Torvalds /* Checksum skb data. */ 19652817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 19662817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 19671da177e4SLinus Torvalds { 19681a028e50SDavid S. Miller int start = skb_headlen(skb); 19691a028e50SDavid S. Miller int i, copy = start - offset; 1970fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19711da177e4SLinus Torvalds int pos = 0; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds /* Checksum header. */ 19741da177e4SLinus Torvalds if (copy > 0) { 19751da177e4SLinus Torvalds if (copy > len) 19761da177e4SLinus Torvalds copy = len; 19772817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 19781da177e4SLinus Torvalds if ((len -= copy) == 0) 19791da177e4SLinus Torvalds return csum; 19801da177e4SLinus Torvalds offset += copy; 19811da177e4SLinus Torvalds pos = copy; 19821da177e4SLinus Torvalds } 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19851a028e50SDavid S. Miller int end; 198651c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19871da177e4SLinus Torvalds 1988547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19891a028e50SDavid S. Miller 199051c56b00SEric Dumazet end = start + skb_frag_size(frag); 19911da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 199244bb9363SAl Viro __wsum csum2; 19931da177e4SLinus Torvalds u8 *vaddr; 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds if (copy > len) 19961da177e4SLinus Torvalds copy = len; 199751c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19982817a336SDaniel Borkmann csum2 = ops->update(vaddr + frag->page_offset + 19991a028e50SDavid S. Miller offset - start, copy, 0); 200051c56b00SEric Dumazet kunmap_atomic(vaddr); 20012817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 20021da177e4SLinus Torvalds if (!(len -= copy)) 20031da177e4SLinus Torvalds return csum; 20041da177e4SLinus Torvalds offset += copy; 20051da177e4SLinus Torvalds pos += copy; 20061da177e4SLinus Torvalds } 20071a028e50SDavid S. Miller start = end; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 2010fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20111a028e50SDavid S. Miller int end; 20121da177e4SLinus Torvalds 2013547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20141a028e50SDavid S. Miller 2015fbb398a8SDavid S. Miller end = start + frag_iter->len; 20161da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20175f92a738SAl Viro __wsum csum2; 20181da177e4SLinus Torvalds if (copy > len) 20191da177e4SLinus Torvalds copy = len; 20202817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 20212817a336SDaniel Borkmann copy, 0, ops); 20222817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 20231da177e4SLinus Torvalds if ((len -= copy) == 0) 20241da177e4SLinus Torvalds return csum; 20251da177e4SLinus Torvalds offset += copy; 20261da177e4SLinus Torvalds pos += copy; 20271da177e4SLinus Torvalds } 20281a028e50SDavid S. Miller start = end; 20291da177e4SLinus Torvalds } 203009a62660SKris Katterjohn BUG_ON(len); 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds return csum; 20331da177e4SLinus Torvalds } 20342817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 20352817a336SDaniel Borkmann 20362817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 20372817a336SDaniel Borkmann int len, __wsum csum) 20382817a336SDaniel Borkmann { 20392817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2040cea80ea8SDaniel Borkmann .update = csum_partial_ext, 20412817a336SDaniel Borkmann .combine = csum_block_add_ext, 20422817a336SDaniel Borkmann }; 20432817a336SDaniel Borkmann 20442817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 20452817a336SDaniel Borkmann } 2046b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds /* Both of above in one bottle. */ 20491da177e4SLinus Torvalds 205081d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 205181d77662SAl Viro u8 *to, int len, __wsum csum) 20521da177e4SLinus Torvalds { 20531a028e50SDavid S. Miller int start = skb_headlen(skb); 20541a028e50SDavid S. Miller int i, copy = start - offset; 2055fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 20561da177e4SLinus Torvalds int pos = 0; 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds /* Copy header. */ 20591da177e4SLinus Torvalds if (copy > 0) { 20601da177e4SLinus Torvalds if (copy > len) 20611da177e4SLinus Torvalds copy = len; 20621da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 20631da177e4SLinus Torvalds copy, csum); 20641da177e4SLinus Torvalds if ((len -= copy) == 0) 20651da177e4SLinus Torvalds return csum; 20661da177e4SLinus Torvalds offset += copy; 20671da177e4SLinus Torvalds to += copy; 20681da177e4SLinus Torvalds pos = copy; 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 20711da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20721a028e50SDavid S. Miller int end; 20731da177e4SLinus Torvalds 2074547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20751a028e50SDavid S. Miller 20769e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 20771da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20785084205fSAl Viro __wsum csum2; 20791da177e4SLinus Torvalds u8 *vaddr; 20801da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20811da177e4SLinus Torvalds 20821da177e4SLinus Torvalds if (copy > len) 20831da177e4SLinus Torvalds copy = len; 208451c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20851da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 20861a028e50SDavid S. Miller frag->page_offset + 20871a028e50SDavid S. Miller offset - start, to, 20881a028e50SDavid S. Miller copy, 0); 208951c56b00SEric Dumazet kunmap_atomic(vaddr); 20901da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 20911da177e4SLinus Torvalds if (!(len -= copy)) 20921da177e4SLinus Torvalds return csum; 20931da177e4SLinus Torvalds offset += copy; 20941da177e4SLinus Torvalds to += copy; 20951da177e4SLinus Torvalds pos += copy; 20961da177e4SLinus Torvalds } 20971a028e50SDavid S. Miller start = end; 20981da177e4SLinus Torvalds } 20991da177e4SLinus Torvalds 2100fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 210181d77662SAl Viro __wsum csum2; 21021a028e50SDavid S. Miller int end; 21031da177e4SLinus Torvalds 2104547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21051a028e50SDavid S. Miller 2106fbb398a8SDavid S. Miller end = start + frag_iter->len; 21071da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21081da177e4SLinus Torvalds if (copy > len) 21091da177e4SLinus Torvalds copy = len; 2110fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 21111a028e50SDavid S. Miller offset - start, 21121da177e4SLinus Torvalds to, copy, 0); 21131da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 21141da177e4SLinus Torvalds if ((len -= copy) == 0) 21151da177e4SLinus Torvalds return csum; 21161da177e4SLinus Torvalds offset += copy; 21171da177e4SLinus Torvalds to += copy; 21181da177e4SLinus Torvalds pos += copy; 21191da177e4SLinus Torvalds } 21201a028e50SDavid S. Miller start = end; 21211da177e4SLinus Torvalds } 212209a62660SKris Katterjohn BUG_ON(len); 21231da177e4SLinus Torvalds return csum; 21241da177e4SLinus Torvalds } 2125b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 21261da177e4SLinus Torvalds 2127af2806f8SThomas Graf /** 2128af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2129af2806f8SThomas Graf * @from: source buffer 2130af2806f8SThomas Graf * 2131af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2132af2806f8SThomas Graf * into skb_zerocopy(). 2133af2806f8SThomas Graf */ 2134af2806f8SThomas Graf unsigned int 2135af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2136af2806f8SThomas Graf { 2137af2806f8SThomas Graf unsigned int hlen = 0; 2138af2806f8SThomas Graf 2139af2806f8SThomas Graf if (!from->head_frag || 2140af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2141af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2142af2806f8SThomas Graf hlen = skb_headlen(from); 2143af2806f8SThomas Graf 2144af2806f8SThomas Graf if (skb_has_frag_list(from)) 2145af2806f8SThomas Graf hlen = from->len; 2146af2806f8SThomas Graf 2147af2806f8SThomas Graf return hlen; 2148af2806f8SThomas Graf } 2149af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2150af2806f8SThomas Graf 2151af2806f8SThomas Graf /** 2152af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2153af2806f8SThomas Graf * @to: destination buffer 21547fceb4deSMasanari Iida * @from: source buffer 2155af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2156af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2157af2806f8SThomas Graf * 2158af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2159af2806f8SThomas Graf * to the frags in the source buffer. 2160af2806f8SThomas Graf * 2161af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2162af2806f8SThomas Graf * headroom in the `to` buffer. 216336d5fe6aSZoltan Kiss * 216436d5fe6aSZoltan Kiss * Return value: 216536d5fe6aSZoltan Kiss * 0: everything is OK 216636d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 216736d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2168af2806f8SThomas Graf */ 216936d5fe6aSZoltan Kiss int 217036d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2171af2806f8SThomas Graf { 2172af2806f8SThomas Graf int i, j = 0; 2173af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 217436d5fe6aSZoltan Kiss int ret; 2175af2806f8SThomas Graf struct page *page; 2176af2806f8SThomas Graf unsigned int offset; 2177af2806f8SThomas Graf 2178af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2179af2806f8SThomas Graf 2180af2806f8SThomas Graf /* dont bother with small payloads */ 218136d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 218236d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2183af2806f8SThomas Graf 2184af2806f8SThomas Graf if (hlen) { 218536d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 218636d5fe6aSZoltan Kiss if (unlikely(ret)) 218736d5fe6aSZoltan Kiss return ret; 2188af2806f8SThomas Graf len -= hlen; 2189af2806f8SThomas Graf } else { 2190af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2191af2806f8SThomas Graf if (plen) { 2192af2806f8SThomas Graf page = virt_to_head_page(from->head); 2193af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2194af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2195af2806f8SThomas Graf get_page(page); 2196af2806f8SThomas Graf j = 1; 2197af2806f8SThomas Graf len -= plen; 2198af2806f8SThomas Graf } 2199af2806f8SThomas Graf } 2200af2806f8SThomas Graf 2201af2806f8SThomas Graf to->truesize += len + plen; 2202af2806f8SThomas Graf to->len += len + plen; 2203af2806f8SThomas Graf to->data_len += len + plen; 2204af2806f8SThomas Graf 220536d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 220636d5fe6aSZoltan Kiss skb_tx_error(from); 220736d5fe6aSZoltan Kiss return -ENOMEM; 220836d5fe6aSZoltan Kiss } 220936d5fe6aSZoltan Kiss 2210af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2211af2806f8SThomas Graf if (!len) 2212af2806f8SThomas Graf break; 2213af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2214af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2215af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2216af2806f8SThomas Graf skb_frag_ref(to, j); 2217af2806f8SThomas Graf j++; 2218af2806f8SThomas Graf } 2219af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 222036d5fe6aSZoltan Kiss 222136d5fe6aSZoltan Kiss return 0; 2222af2806f8SThomas Graf } 2223af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2224af2806f8SThomas Graf 22251da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 22261da177e4SLinus Torvalds { 2227d3bc23e7SAl Viro __wsum csum; 22281da177e4SLinus Torvalds long csstart; 22291da177e4SLinus Torvalds 223084fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 223155508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 22321da177e4SLinus Torvalds else 22331da177e4SLinus Torvalds csstart = skb_headlen(skb); 22341da177e4SLinus Torvalds 223509a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 22361da177e4SLinus Torvalds 2237d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds csum = 0; 22401da177e4SLinus Torvalds if (csstart != skb->len) 22411da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 22421da177e4SLinus Torvalds skb->len - csstart, 0); 22431da177e4SLinus Torvalds 224484fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2245ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 22461da177e4SLinus Torvalds 2247d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 22481da177e4SLinus Torvalds } 22491da177e4SLinus Torvalds } 2250b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds /** 22531da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 22541da177e4SLinus Torvalds * @list: list to dequeue from 22551da177e4SLinus Torvalds * 22561da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 22571da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 22581da177e4SLinus Torvalds * returned or %NULL if the list is empty. 22591da177e4SLinus Torvalds */ 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 22621da177e4SLinus Torvalds { 22631da177e4SLinus Torvalds unsigned long flags; 22641da177e4SLinus Torvalds struct sk_buff *result; 22651da177e4SLinus Torvalds 22661da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 22671da177e4SLinus Torvalds result = __skb_dequeue(list); 22681da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 22691da177e4SLinus Torvalds return result; 22701da177e4SLinus Torvalds } 2271b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 22721da177e4SLinus Torvalds 22731da177e4SLinus Torvalds /** 22741da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 22751da177e4SLinus Torvalds * @list: list to dequeue from 22761da177e4SLinus Torvalds * 22771da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 22781da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 22791da177e4SLinus Torvalds * returned or %NULL if the list is empty. 22801da177e4SLinus Torvalds */ 22811da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 22821da177e4SLinus Torvalds { 22831da177e4SLinus Torvalds unsigned long flags; 22841da177e4SLinus Torvalds struct sk_buff *result; 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 22871da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 22881da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 22891da177e4SLinus Torvalds return result; 22901da177e4SLinus Torvalds } 2291b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds /** 22941da177e4SLinus Torvalds * skb_queue_purge - empty a list 22951da177e4SLinus Torvalds * @list: list to empty 22961da177e4SLinus Torvalds * 22971da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 22981da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 22991da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 23001da177e4SLinus Torvalds */ 23011da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 23021da177e4SLinus Torvalds { 23031da177e4SLinus Torvalds struct sk_buff *skb; 23041da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 23051da177e4SLinus Torvalds kfree_skb(skb); 23061da177e4SLinus Torvalds } 2307b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 23081da177e4SLinus Torvalds 23091da177e4SLinus Torvalds /** 23101da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 23111da177e4SLinus Torvalds * @list: list to use 23121da177e4SLinus Torvalds * @newsk: buffer to queue 23131da177e4SLinus Torvalds * 23141da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 23151da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 23161da177e4SLinus Torvalds * safely. 23171da177e4SLinus Torvalds * 23181da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23191da177e4SLinus Torvalds */ 23201da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 23211da177e4SLinus Torvalds { 23221da177e4SLinus Torvalds unsigned long flags; 23231da177e4SLinus Torvalds 23241da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23251da177e4SLinus Torvalds __skb_queue_head(list, newsk); 23261da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23271da177e4SLinus Torvalds } 2328b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds /** 23311da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 23321da177e4SLinus Torvalds * @list: list to use 23331da177e4SLinus Torvalds * @newsk: buffer to queue 23341da177e4SLinus Torvalds * 23351da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 23361da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 23371da177e4SLinus Torvalds * safely. 23381da177e4SLinus Torvalds * 23391da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23401da177e4SLinus Torvalds */ 23411da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 23421da177e4SLinus Torvalds { 23431da177e4SLinus Torvalds unsigned long flags; 23441da177e4SLinus Torvalds 23451da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23461da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 23471da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23481da177e4SLinus Torvalds } 2349b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 23508728b834SDavid S. Miller 23511da177e4SLinus Torvalds /** 23521da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 23531da177e4SLinus Torvalds * @skb: buffer to remove 23548728b834SDavid S. Miller * @list: list to use 23551da177e4SLinus Torvalds * 23568728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 23578728b834SDavid S. Miller * function is atomic with respect to other list locked calls 23581da177e4SLinus Torvalds * 23598728b834SDavid S. Miller * You must know what list the SKB is on. 23601da177e4SLinus Torvalds */ 23618728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 23621da177e4SLinus Torvalds { 23631da177e4SLinus Torvalds unsigned long flags; 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23668728b834SDavid S. Miller __skb_unlink(skb, list); 23671da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23681da177e4SLinus Torvalds } 2369b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds /** 23721da177e4SLinus Torvalds * skb_append - append a buffer 23731da177e4SLinus Torvalds * @old: buffer to insert after 23741da177e4SLinus Torvalds * @newsk: buffer to insert 23758728b834SDavid S. Miller * @list: list to use 23761da177e4SLinus Torvalds * 23771da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 23781da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 23791da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23801da177e4SLinus Torvalds */ 23818728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 23821da177e4SLinus Torvalds { 23831da177e4SLinus Torvalds unsigned long flags; 23841da177e4SLinus Torvalds 23858728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 23867de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 23878728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 23881da177e4SLinus Torvalds } 2389b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds /** 23921da177e4SLinus Torvalds * skb_insert - insert a buffer 23931da177e4SLinus Torvalds * @old: buffer to insert before 23941da177e4SLinus Torvalds * @newsk: buffer to insert 23958728b834SDavid S. Miller * @list: list to use 23961da177e4SLinus Torvalds * 23978728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 23988728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 23998728b834SDavid S. Miller * calls. 24008728b834SDavid S. Miller * 24011da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24021da177e4SLinus Torvalds */ 24038728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 24041da177e4SLinus Torvalds { 24051da177e4SLinus Torvalds unsigned long flags; 24061da177e4SLinus Torvalds 24078728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 24088728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 24098728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 24101da177e4SLinus Torvalds } 2411b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 24141da177e4SLinus Torvalds struct sk_buff* skb1, 24151da177e4SLinus Torvalds const u32 len, const int pos) 24161da177e4SLinus Torvalds { 24171da177e4SLinus Torvalds int i; 24181da177e4SLinus Torvalds 2419d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2420d626f62bSArnaldo Carvalho de Melo pos - len); 24211da177e4SLinus Torvalds /* And move data appendix as is. */ 24221da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 24231da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 24261da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 24271da177e4SLinus Torvalds skb1->data_len = skb->data_len; 24281da177e4SLinus Torvalds skb1->len += skb1->data_len; 24291da177e4SLinus Torvalds skb->data_len = 0; 24301da177e4SLinus Torvalds skb->len = len; 243127a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 24321da177e4SLinus Torvalds } 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 24351da177e4SLinus Torvalds struct sk_buff* skb1, 24361da177e4SLinus Torvalds const u32 len, int pos) 24371da177e4SLinus Torvalds { 24381da177e4SLinus Torvalds int i, k = 0; 24391da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 24421da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 24431da177e4SLinus Torvalds skb->len = len; 24441da177e4SLinus Torvalds skb->data_len = len - pos; 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 24479e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds if (pos + size > len) { 24501da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 24511da177e4SLinus Torvalds 24521da177e4SLinus Torvalds if (pos < len) { 24531da177e4SLinus Torvalds /* Split frag. 24541da177e4SLinus Torvalds * We have two variants in this case: 24551da177e4SLinus Torvalds * 1. Move all the frag to the second 24561da177e4SLinus Torvalds * part, if it is possible. F.e. 24571da177e4SLinus Torvalds * this approach is mandatory for TUX, 24581da177e4SLinus Torvalds * where splitting is expensive. 24591da177e4SLinus Torvalds * 2. Split is accurately. We make this. 24601da177e4SLinus Torvalds */ 2461ea2ab693SIan Campbell skb_frag_ref(skb, i); 24621da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 24639e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 24649e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 24651da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds k++; 24681da177e4SLinus Torvalds } else 24691da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 24701da177e4SLinus Torvalds pos += size; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 24731da177e4SLinus Torvalds } 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds /** 24761da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 24771da177e4SLinus Torvalds * @skb: the buffer to split 24781da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 24791da177e4SLinus Torvalds * @len: new length for skb 24801da177e4SLinus Torvalds */ 24811da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 24821da177e4SLinus Torvalds { 24831da177e4SLinus Torvalds int pos = skb_headlen(skb); 24841da177e4SLinus Torvalds 248568534c68SAmerigo Wang skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 24861da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 24871da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 24881da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 24891da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 24901da177e4SLinus Torvalds } 2491b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 24921da177e4SLinus Torvalds 24939f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 24949f782db3SIlpo Järvinen * 24959f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 24969f782db3SIlpo Järvinen */ 2497832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2498832d11c5SIlpo Järvinen { 24990ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2500832d11c5SIlpo Järvinen } 2501832d11c5SIlpo Järvinen 2502832d11c5SIlpo Järvinen /** 2503832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2504832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2505832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2506832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2507832d11c5SIlpo Järvinen * 2508832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 250920e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2510832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2511832d11c5SIlpo Järvinen * 2512832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2513832d11c5SIlpo Järvinen * 2514832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2515832d11c5SIlpo Järvinen * to have non-paged data as well. 2516832d11c5SIlpo Järvinen * 2517832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2518832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2519832d11c5SIlpo Järvinen */ 2520832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2521832d11c5SIlpo Järvinen { 2522832d11c5SIlpo Järvinen int from, to, merge, todo; 2523832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2524832d11c5SIlpo Järvinen 2525832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2526832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2527832d11c5SIlpo Järvinen 2528832d11c5SIlpo Järvinen todo = shiftlen; 2529832d11c5SIlpo Järvinen from = 0; 2530832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2531832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2532832d11c5SIlpo Järvinen 2533832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2534832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2535832d11c5SIlpo Järvinen */ 2536832d11c5SIlpo Järvinen if (!to || 2537ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2538ea2ab693SIan Campbell fragfrom->page_offset)) { 2539832d11c5SIlpo Järvinen merge = -1; 2540832d11c5SIlpo Järvinen } else { 2541832d11c5SIlpo Järvinen merge = to - 1; 2542832d11c5SIlpo Järvinen 25439e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2544832d11c5SIlpo Järvinen if (todo < 0) { 2545832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2546832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2547832d11c5SIlpo Järvinen return 0; 2548832d11c5SIlpo Järvinen 25499f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 25509f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2551832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2552832d11c5SIlpo Järvinen 25539e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 25549e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2555832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2556832d11c5SIlpo Järvinen 2557832d11c5SIlpo Järvinen goto onlymerged; 2558832d11c5SIlpo Järvinen } 2559832d11c5SIlpo Järvinen 2560832d11c5SIlpo Järvinen from++; 2561832d11c5SIlpo Järvinen } 2562832d11c5SIlpo Järvinen 2563832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2564832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2565832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2566832d11c5SIlpo Järvinen return 0; 2567832d11c5SIlpo Järvinen 2568832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2569832d11c5SIlpo Järvinen return 0; 2570832d11c5SIlpo Järvinen 2571832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2572832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2573832d11c5SIlpo Järvinen return 0; 2574832d11c5SIlpo Järvinen 2575832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2576832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2577832d11c5SIlpo Järvinen 25789e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2579832d11c5SIlpo Järvinen *fragto = *fragfrom; 25809e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2581832d11c5SIlpo Järvinen from++; 2582832d11c5SIlpo Järvinen to++; 2583832d11c5SIlpo Järvinen 2584832d11c5SIlpo Järvinen } else { 2585ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2586832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2587832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 25889e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2589832d11c5SIlpo Järvinen 2590832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 25919e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2592832d11c5SIlpo Järvinen todo = 0; 2593832d11c5SIlpo Järvinen 2594832d11c5SIlpo Järvinen to++; 2595832d11c5SIlpo Järvinen break; 2596832d11c5SIlpo Järvinen } 2597832d11c5SIlpo Järvinen } 2598832d11c5SIlpo Järvinen 2599832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2600832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2601832d11c5SIlpo Järvinen 2602832d11c5SIlpo Järvinen if (merge >= 0) { 2603832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2604832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2605832d11c5SIlpo Järvinen 26069e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2607ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2608832d11c5SIlpo Järvinen } 2609832d11c5SIlpo Järvinen 2610832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2611832d11c5SIlpo Järvinen to = 0; 2612832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2613832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2614832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2615832d11c5SIlpo Järvinen 2616832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2617832d11c5SIlpo Järvinen 2618832d11c5SIlpo Järvinen onlymerged: 2619832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2620832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2621832d11c5SIlpo Järvinen */ 2622832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2623832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2624832d11c5SIlpo Järvinen 2625832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2626832d11c5SIlpo Järvinen skb->len -= shiftlen; 2627832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2628832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2629832d11c5SIlpo Järvinen tgt->len += shiftlen; 2630832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2631832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2632832d11c5SIlpo Järvinen 2633832d11c5SIlpo Järvinen return shiftlen; 2634832d11c5SIlpo Järvinen } 2635832d11c5SIlpo Järvinen 2636677e90edSThomas Graf /** 2637677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2638677e90edSThomas Graf * @skb: the buffer to read 2639677e90edSThomas Graf * @from: lower offset of data to be read 2640677e90edSThomas Graf * @to: upper offset of data to be read 2641677e90edSThomas Graf * @st: state variable 2642677e90edSThomas Graf * 2643677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2644677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2645677e90edSThomas Graf */ 2646677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2647677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2648677e90edSThomas Graf { 2649677e90edSThomas Graf st->lower_offset = from; 2650677e90edSThomas Graf st->upper_offset = to; 2651677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2652677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2653677e90edSThomas Graf st->frag_data = NULL; 2654677e90edSThomas Graf } 2655b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2656677e90edSThomas Graf 2657677e90edSThomas Graf /** 2658677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2659677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2660677e90edSThomas Graf * @data: destination pointer for data to be returned 2661677e90edSThomas Graf * @st: state variable 2662677e90edSThomas Graf * 2663bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 2664677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2665bc32383cSMathias Krause * the head of the data block to @data and returns the length 2666677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2667677e90edSThomas Graf * offset has been reached. 2668677e90edSThomas Graf * 2669677e90edSThomas Graf * The caller is not required to consume all of the data 2670bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 2671677e90edSThomas Graf * of bytes already consumed and the next call to 2672677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2673677e90edSThomas Graf * 267425985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2675e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 2676677e90edSThomas Graf * reads of potentially non linear data. 2677677e90edSThomas Graf * 2678bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2679677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2680677e90edSThomas Graf * a stack for this purpose. 2681677e90edSThomas Graf */ 2682677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2683677e90edSThomas Graf struct skb_seq_state *st) 2684677e90edSThomas Graf { 2685677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2686677e90edSThomas Graf skb_frag_t *frag; 2687677e90edSThomas Graf 2688aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 2689aeb193eaSWedson Almeida Filho if (st->frag_data) { 2690aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 2691aeb193eaSWedson Almeida Filho st->frag_data = NULL; 2692aeb193eaSWedson Almeida Filho } 2693677e90edSThomas Graf return 0; 2694aeb193eaSWedson Almeida Filho } 2695677e90edSThomas Graf 2696677e90edSThomas Graf next_skb: 269795e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2698677e90edSThomas Graf 2699995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 270095e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2701677e90edSThomas Graf return block_limit - abs_offset; 2702677e90edSThomas Graf } 2703677e90edSThomas Graf 2704677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2705677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2706677e90edSThomas Graf 2707677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2708677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 27099e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2710677e90edSThomas Graf 2711677e90edSThomas Graf if (abs_offset < block_limit) { 2712677e90edSThomas Graf if (!st->frag_data) 271351c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2714677e90edSThomas Graf 2715677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2716677e90edSThomas Graf (abs_offset - st->stepped_offset); 2717677e90edSThomas Graf 2718677e90edSThomas Graf return block_limit - abs_offset; 2719677e90edSThomas Graf } 2720677e90edSThomas Graf 2721677e90edSThomas Graf if (st->frag_data) { 272251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2723677e90edSThomas Graf st->frag_data = NULL; 2724677e90edSThomas Graf } 2725677e90edSThomas Graf 2726677e90edSThomas Graf st->frag_idx++; 27279e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2728677e90edSThomas Graf } 2729677e90edSThomas Graf 27305b5a60daSOlaf Kirch if (st->frag_data) { 273151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 27325b5a60daSOlaf Kirch st->frag_data = NULL; 27335b5a60daSOlaf Kirch } 27345b5a60daSOlaf Kirch 273521dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2736677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 273795e3b24cSHerbert Xu st->frag_idx = 0; 2738677e90edSThomas Graf goto next_skb; 273971b3346dSShyam Iyer } else if (st->cur_skb->next) { 274071b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 274171b3346dSShyam Iyer st->frag_idx = 0; 2742677e90edSThomas Graf goto next_skb; 2743677e90edSThomas Graf } 2744677e90edSThomas Graf 2745677e90edSThomas Graf return 0; 2746677e90edSThomas Graf } 2747b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2748677e90edSThomas Graf 2749677e90edSThomas Graf /** 2750677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2751677e90edSThomas Graf * @st: state variable 2752677e90edSThomas Graf * 2753677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2754677e90edSThomas Graf * returned 0. 2755677e90edSThomas Graf */ 2756677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2757677e90edSThomas Graf { 2758677e90edSThomas Graf if (st->frag_data) 275951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2760677e90edSThomas Graf } 2761b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2762677e90edSThomas Graf 27633fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 27643fc7e8a6SThomas Graf 27653fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 27663fc7e8a6SThomas Graf struct ts_config *conf, 27673fc7e8a6SThomas Graf struct ts_state *state) 27683fc7e8a6SThomas Graf { 27693fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 27703fc7e8a6SThomas Graf } 27713fc7e8a6SThomas Graf 27723fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 27733fc7e8a6SThomas Graf { 27743fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 27753fc7e8a6SThomas Graf } 27763fc7e8a6SThomas Graf 27773fc7e8a6SThomas Graf /** 27783fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 27793fc7e8a6SThomas Graf * @skb: the buffer to look in 27803fc7e8a6SThomas Graf * @from: search offset 27813fc7e8a6SThomas Graf * @to: search limit 27823fc7e8a6SThomas Graf * @config: textsearch configuration 27833fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 27843fc7e8a6SThomas Graf * 27853fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 27863fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 27873fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 27883fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 27893fc7e8a6SThomas Graf */ 27903fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 27913fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 27923fc7e8a6SThomas Graf struct ts_state *state) 27933fc7e8a6SThomas Graf { 2794f72b948dSPhil Oester unsigned int ret; 2795f72b948dSPhil Oester 27963fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 27973fc7e8a6SThomas Graf config->finish = skb_ts_finish; 27983fc7e8a6SThomas Graf 27993fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 28003fc7e8a6SThomas Graf 2801f72b948dSPhil Oester ret = textsearch_find(config, state); 2802f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 28033fc7e8a6SThomas Graf } 2804b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 28053fc7e8a6SThomas Graf 2806e89e9cf5SAnanda Raju /** 28072c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2808e89e9cf5SAnanda Raju * @sk: sock structure 2809e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 2810e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2811e89e9cf5SAnanda Raju * @from: pointer to user message iov 2812e89e9cf5SAnanda Raju * @length: length of the iov message 2813e89e9cf5SAnanda Raju * 2814e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2815e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2816e89e9cf5SAnanda Raju */ 2817e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2818dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2819e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2820e89e9cf5SAnanda Raju void *from, int length) 2821e89e9cf5SAnanda Raju { 2822b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 2823b2111724SEric Dumazet int copy; 2824e89e9cf5SAnanda Raju int offset = 0; 2825e89e9cf5SAnanda Raju int ret; 2826b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 2827e89e9cf5SAnanda Raju 2828e89e9cf5SAnanda Raju do { 2829e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2830e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2831b2111724SEric Dumazet return -EMSGSIZE; 2832e89e9cf5SAnanda Raju 2833b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 2834e89e9cf5SAnanda Raju return -ENOMEM; 2835e89e9cf5SAnanda Raju 2836e89e9cf5SAnanda Raju /* copy the user data to page */ 2837b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 2838e89e9cf5SAnanda Raju 2839b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 2840e89e9cf5SAnanda Raju offset, copy, 0, skb); 2841e89e9cf5SAnanda Raju if (ret < 0) 2842e89e9cf5SAnanda Raju return -EFAULT; 2843e89e9cf5SAnanda Raju 2844e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2845b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 2846b2111724SEric Dumazet copy); 2847b2111724SEric Dumazet frg_cnt++; 2848b2111724SEric Dumazet pfrag->offset += copy; 2849b2111724SEric Dumazet get_page(pfrag->page); 2850b2111724SEric Dumazet 2851b2111724SEric Dumazet skb->truesize += copy; 2852b2111724SEric Dumazet atomic_add(copy, &sk->sk_wmem_alloc); 2853e89e9cf5SAnanda Raju skb->len += copy; 2854e89e9cf5SAnanda Raju skb->data_len += copy; 2855e89e9cf5SAnanda Raju offset += copy; 2856e89e9cf5SAnanda Raju length -= copy; 2857e89e9cf5SAnanda Raju 2858e89e9cf5SAnanda Raju } while (length > 0); 2859e89e9cf5SAnanda Raju 2860e89e9cf5SAnanda Raju return 0; 2861e89e9cf5SAnanda Raju } 2862b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2863e89e9cf5SAnanda Raju 2864cbb042f9SHerbert Xu /** 2865cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2866cbb042f9SHerbert Xu * @skb: buffer to update 2867cbb042f9SHerbert Xu * @len: length of data pulled 2868cbb042f9SHerbert Xu * 2869cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2870fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 287184fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 287284fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 287384fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2874cbb042f9SHerbert Xu */ 2875cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2876cbb042f9SHerbert Xu { 2877cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2878cbb042f9SHerbert Xu skb->len -= len; 2879cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2880cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2881cbb042f9SHerbert Xu return skb->data += len; 2882cbb042f9SHerbert Xu } 2883f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2884f94691acSArnaldo Carvalho de Melo 2885f4c50d99SHerbert Xu /** 2886f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2887df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 2888576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2889f4c50d99SHerbert Xu * 2890f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 28914c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 28924c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2893f4c50d99SHerbert Xu */ 2894df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 2895df5771ffSMichael S. Tsirkin netdev_features_t features) 2896f4c50d99SHerbert Xu { 2897f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2898f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 28991a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 2900df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 2901df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 2902df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 29031fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 2904f4c50d99SHerbert Xu unsigned int offset = doffset; 2905df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 2906f4c50d99SHerbert Xu unsigned int headroom; 2907f4c50d99SHerbert Xu unsigned int len; 2908ec5f0615SPravin B Shelar __be16 proto; 2909ec5f0615SPravin B Shelar bool csum; 291004ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 2911df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 2912f4c50d99SHerbert Xu int err = -ENOMEM; 2913f4c50d99SHerbert Xu int i = 0; 2914f4c50d99SHerbert Xu int pos; 291553d6471cSVlad Yasevich int dummy; 2916f4c50d99SHerbert Xu 29175882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 291853d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 2919ec5f0615SPravin B Shelar if (unlikely(!proto)) 2920ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 2921ec5f0615SPravin B Shelar 29227e2b10c1STom Herbert csum = !head_skb->encap_hdr_csum && 29237e2b10c1STom Herbert !!can_checksum_protocol(features, proto); 29247e2b10c1STom Herbert 2925df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 2926df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 2927f4c50d99SHerbert Xu 2928f4c50d99SHerbert Xu do { 2929f4c50d99SHerbert Xu struct sk_buff *nskb; 29308cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 2931c8884eddSHerbert Xu int hsize; 2932f4c50d99SHerbert Xu int size; 2933f4c50d99SHerbert Xu 2934df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 2935f4c50d99SHerbert Xu if (len > mss) 2936f4c50d99SHerbert Xu len = mss; 2937f4c50d99SHerbert Xu 2938df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 2939f4c50d99SHerbert Xu if (hsize < 0) 2940f4c50d99SHerbert Xu hsize = 0; 2941c8884eddSHerbert Xu if (hsize > len || !sg) 2942c8884eddSHerbert Xu hsize = len; 2943f4c50d99SHerbert Xu 29441a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 29451a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 29461a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 294789319d38SHerbert Xu 29489d8506ccSHerbert Xu i = 0; 29491a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 29501a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 29511fd819ecSMichael S. Tsirkin frag_skb = list_skb; 29521a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 29539d8506ccSHerbert Xu 29549d8506ccSHerbert Xu while (pos < offset + len) { 29559d8506ccSHerbert Xu BUG_ON(i >= nfrags); 29569d8506ccSHerbert Xu 29574e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 29589d8506ccSHerbert Xu if (pos + size > offset + len) 29599d8506ccSHerbert Xu break; 29609d8506ccSHerbert Xu 29619d8506ccSHerbert Xu i++; 29629d8506ccSHerbert Xu pos += size; 29634e1beba1SMichael S. Tsirkin frag++; 29649d8506ccSHerbert Xu } 29659d8506ccSHerbert Xu 29661a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 29671a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 296889319d38SHerbert Xu 2969f4c50d99SHerbert Xu if (unlikely(!nskb)) 2970f4c50d99SHerbert Xu goto err; 2971f4c50d99SHerbert Xu 29729d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 29739d8506ccSHerbert Xu kfree_skb(nskb); 29749d8506ccSHerbert Xu goto err; 29759d8506ccSHerbert Xu } 29769d8506ccSHerbert Xu 2977ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 297889319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 297989319d38SHerbert Xu kfree_skb(nskb); 298089319d38SHerbert Xu goto err; 298189319d38SHerbert Xu } 298289319d38SHerbert Xu 2983ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 298489319d38SHerbert Xu skb_release_head_state(nskb); 298589319d38SHerbert Xu __skb_push(nskb, doffset); 298689319d38SHerbert Xu } else { 2987c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 2988df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 2989c93bdd0eSMel Gorman NUMA_NO_NODE); 299089319d38SHerbert Xu 299189319d38SHerbert Xu if (unlikely(!nskb)) 299289319d38SHerbert Xu goto err; 299389319d38SHerbert Xu 299489319d38SHerbert Xu skb_reserve(nskb, headroom); 299589319d38SHerbert Xu __skb_put(nskb, doffset); 299689319d38SHerbert Xu } 299789319d38SHerbert Xu 2998f4c50d99SHerbert Xu if (segs) 2999f4c50d99SHerbert Xu tail->next = nskb; 3000f4c50d99SHerbert Xu else 3001f4c50d99SHerbert Xu segs = nskb; 3002f4c50d99SHerbert Xu tail = nskb; 3003f4c50d99SHerbert Xu 3004df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3005f4c50d99SHerbert Xu 3006030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3007fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 300868c33163SPravin B Shelar 3009df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 301068c33163SPravin B Shelar nskb->data - tnl_hlen, 301168c33163SPravin B Shelar doffset + tnl_hlen); 301289319d38SHerbert Xu 30139d8506ccSHerbert Xu if (nskb->len == len + doffset) 30141cdbcb79SSimon Horman goto perform_csum_check; 301589319d38SHerbert Xu 3016e585f236STom Herbert if (!sg && !nskb->remcsum_offload) { 30176f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 3018df5771ffSMichael S. Tsirkin nskb->csum = skb_copy_and_csum_bits(head_skb, offset, 3019f4c50d99SHerbert Xu skb_put(nskb, len), 3020f4c50d99SHerbert Xu len, 0); 30217e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3022de843723STom Herbert skb_headroom(nskb) + doffset; 3023f4c50d99SHerbert Xu continue; 3024f4c50d99SHerbert Xu } 3025f4c50d99SHerbert Xu 30268cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3027f4c50d99SHerbert Xu 3028df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3029d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3030f4c50d99SHerbert Xu 3031df5771ffSMichael S. Tsirkin skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & 3032df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3033cef401deSEric Dumazet 30349d8506ccSHerbert Xu while (pos < offset + len) { 30359d8506ccSHerbert Xu if (i >= nfrags) { 30361a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 30379d8506ccSHerbert Xu 30389d8506ccSHerbert Xu i = 0; 30391a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 30401a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 30411fd819ecSMichael S. Tsirkin frag_skb = list_skb; 30429d8506ccSHerbert Xu 30439d8506ccSHerbert Xu BUG_ON(!nfrags); 30449d8506ccSHerbert Xu 30451a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 30469d8506ccSHerbert Xu } 30479d8506ccSHerbert Xu 30489d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 30499d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 30509d8506ccSHerbert Xu net_warn_ratelimited( 30519d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 30529d8506ccSHerbert Xu pos, mss); 30539d8506ccSHerbert Xu goto err; 30549d8506ccSHerbert Xu } 30559d8506ccSHerbert Xu 30561fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 30571fd819ecSMichael S. Tsirkin goto err; 30581fd819ecSMichael S. Tsirkin 30594e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 30608cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 30618cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3062f4c50d99SHerbert Xu 3063f4c50d99SHerbert Xu if (pos < offset) { 30648cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 30658cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3066f4c50d99SHerbert Xu } 3067f4c50d99SHerbert Xu 306889319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3069f4c50d99SHerbert Xu 3070f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3071f4c50d99SHerbert Xu i++; 30724e1beba1SMichael S. Tsirkin frag++; 3073f4c50d99SHerbert Xu pos += size; 3074f4c50d99SHerbert Xu } else { 30758cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 307689319d38SHerbert Xu goto skip_fraglist; 3077f4c50d99SHerbert Xu } 3078f4c50d99SHerbert Xu 30798cb19905SMichael S. Tsirkin nskb_frag++; 3080f4c50d99SHerbert Xu } 3081f4c50d99SHerbert Xu 308289319d38SHerbert Xu skip_fraglist: 3083f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3084f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3085f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3086ec5f0615SPravin B Shelar 30871cdbcb79SSimon Horman perform_csum_check: 3088e585f236STom Herbert if (!csum && !nskb->remcsum_offload) { 3089ec5f0615SPravin B Shelar nskb->csum = skb_checksum(nskb, doffset, 3090ec5f0615SPravin B Shelar nskb->len - doffset, 0); 3091ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 30927e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 30937e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3094ec5f0615SPravin B Shelar } 3095df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3096f4c50d99SHerbert Xu 3097bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3098bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3099bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3100bec3cfdcSEric Dumazet */ 3101bec3cfdcSEric Dumazet segs->prev = tail; 3102432c856fSToshiaki Makita 3103432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3104432c856fSToshiaki Makita * using skb_set_owner_w(). 3105432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3106432c856fSToshiaki Makita */ 3107432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3108432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3109432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3110432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3111432c856fSToshiaki Makita } 3112f4c50d99SHerbert Xu return segs; 3113f4c50d99SHerbert Xu 3114f4c50d99SHerbert Xu err: 3115289dccbeSEric Dumazet kfree_skb_list(segs); 3116f4c50d99SHerbert Xu return ERR_PTR(err); 3117f4c50d99SHerbert Xu } 3118f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3119f4c50d99SHerbert Xu 312071d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 312171d93b39SHerbert Xu { 31228a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 312367147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 312467147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 31258a29111cSEric Dumazet struct sk_buff *nskb, *lp, *p = *head; 31268a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 3127715dc1f3SEric Dumazet unsigned int delta_truesize; 31288a29111cSEric Dumazet unsigned int headroom; 312971d93b39SHerbert Xu 31308a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 313171d93b39SHerbert Xu return -E2BIG; 313271d93b39SHerbert Xu 313329e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 31348a29111cSEric Dumazet pinfo = skb_shinfo(lp); 31358a29111cSEric Dumazet 31368a29111cSEric Dumazet if (headlen <= offset) { 313742da6994SHerbert Xu skb_frag_t *frag; 313866e92fcfSHerbert Xu skb_frag_t *frag2; 31399aaa156cSHerbert Xu int i = skbinfo->nr_frags; 31409aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 314142da6994SHerbert Xu 314266e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 31438a29111cSEric Dumazet goto merge; 314481705ad1SHerbert Xu 31458a29111cSEric Dumazet offset -= headlen; 31469aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 31479aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3148f5572068SHerbert Xu 31499aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 31509aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 315166e92fcfSHerbert Xu do { 315266e92fcfSHerbert Xu *--frag = *--frag2; 315366e92fcfSHerbert Xu } while (--i); 315466e92fcfSHerbert Xu 315566e92fcfSHerbert Xu frag->page_offset += offset; 31569e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 315766e92fcfSHerbert Xu 3158715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3159ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3160ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3161715dc1f3SEric Dumazet 3162f5572068SHerbert Xu skb->truesize -= skb->data_len; 3163f5572068SHerbert Xu skb->len -= skb->data_len; 3164f5572068SHerbert Xu skb->data_len = 0; 3165f5572068SHerbert Xu 3166715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 31675d38a079SHerbert Xu goto done; 3168d7e8883cSEric Dumazet } else if (skb->head_frag) { 3169d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3170d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3171d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3172d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3173d7e8883cSEric Dumazet unsigned int first_offset; 3174d7e8883cSEric Dumazet 3175d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 31768a29111cSEric Dumazet goto merge; 3177d7e8883cSEric Dumazet 3178d7e8883cSEric Dumazet first_offset = skb->data - 3179d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3180d7e8883cSEric Dumazet offset; 3181d7e8883cSEric Dumazet 3182d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3183d7e8883cSEric Dumazet 3184d7e8883cSEric Dumazet frag->page.p = page; 3185d7e8883cSEric Dumazet frag->page_offset = first_offset; 3186d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3187d7e8883cSEric Dumazet 3188d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3189d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3190d7e8883cSEric Dumazet 3191715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3192d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3193d7e8883cSEric Dumazet goto done; 31948a29111cSEric Dumazet } 319573d3fe6dSEric Dumazet /* switch back to head shinfo */ 319673d3fe6dSEric Dumazet pinfo = skb_shinfo(p); 319773d3fe6dSEric Dumazet 31988a29111cSEric Dumazet if (pinfo->frag_list) 31998a29111cSEric Dumazet goto merge; 32008a29111cSEric Dumazet if (skb_gro_len(p) != pinfo->gso_size) 320169c0cab1SHerbert Xu return -E2BIG; 320271d93b39SHerbert Xu 320371d93b39SHerbert Xu headroom = skb_headroom(p); 32043d3be433SEric Dumazet nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 320571d93b39SHerbert Xu if (unlikely(!nskb)) 320671d93b39SHerbert Xu return -ENOMEM; 320771d93b39SHerbert Xu 320871d93b39SHerbert Xu __copy_skb_header(nskb, p); 320971d93b39SHerbert Xu nskb->mac_len = p->mac_len; 321071d93b39SHerbert Xu 321171d93b39SHerbert Xu skb_reserve(nskb, headroom); 321286911732SHerbert Xu __skb_put(nskb, skb_gro_offset(p)); 321371d93b39SHerbert Xu 321486911732SHerbert Xu skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 321571d93b39SHerbert Xu skb_set_network_header(nskb, skb_network_offset(p)); 321671d93b39SHerbert Xu skb_set_transport_header(nskb, skb_transport_offset(p)); 321771d93b39SHerbert Xu 321886911732SHerbert Xu __skb_pull(p, skb_gro_offset(p)); 321986911732SHerbert Xu memcpy(skb_mac_header(nskb), skb_mac_header(p), 322086911732SHerbert Xu p->data - skb_mac_header(p)); 322171d93b39SHerbert Xu 322271d93b39SHerbert Xu skb_shinfo(nskb)->frag_list = p; 32239aaa156cSHerbert Xu skb_shinfo(nskb)->gso_size = pinfo->gso_size; 3224622e0ca1SHerbert Xu pinfo->gso_size = 0; 3225f4a775d1SEric Dumazet __skb_header_release(p); 3226c3c7c254SEric Dumazet NAPI_GRO_CB(nskb)->last = p; 322771d93b39SHerbert Xu 322871d93b39SHerbert Xu nskb->data_len += p->len; 3229de8261c2SEric Dumazet nskb->truesize += p->truesize; 323071d93b39SHerbert Xu nskb->len += p->len; 323171d93b39SHerbert Xu 323271d93b39SHerbert Xu *head = nskb; 323371d93b39SHerbert Xu nskb->next = p->next; 323471d93b39SHerbert Xu p->next = NULL; 323571d93b39SHerbert Xu 323671d93b39SHerbert Xu p = nskb; 323771d93b39SHerbert Xu 323871d93b39SHerbert Xu merge: 3239715dc1f3SEric Dumazet delta_truesize = skb->truesize; 324067147ba9SHerbert Xu if (offset > headlen) { 3241d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3242d1dc7abfSMichal Schmidt 3243d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 32449e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3245d1dc7abfSMichal Schmidt skb->data_len -= eat; 3246d1dc7abfSMichal Schmidt skb->len -= eat; 324767147ba9SHerbert Xu offset = headlen; 324856035022SHerbert Xu } 324956035022SHerbert Xu 325067147ba9SHerbert Xu __skb_pull(skb, offset); 325156035022SHerbert Xu 325229e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 32538a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 32548a29111cSEric Dumazet else 3255c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3256c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3257f4a775d1SEric Dumazet __skb_header_release(skb); 32588a29111cSEric Dumazet lp = p; 325971d93b39SHerbert Xu 32605d38a079SHerbert Xu done: 32615d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 326237fe4732SHerbert Xu p->data_len += len; 3263715dc1f3SEric Dumazet p->truesize += delta_truesize; 326437fe4732SHerbert Xu p->len += len; 32658a29111cSEric Dumazet if (lp != p) { 32668a29111cSEric Dumazet lp->data_len += len; 32678a29111cSEric Dumazet lp->truesize += delta_truesize; 32688a29111cSEric Dumazet lp->len += len; 32698a29111cSEric Dumazet } 327071d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 327171d93b39SHerbert Xu return 0; 327271d93b39SHerbert Xu } 327371d93b39SHerbert Xu 32741da177e4SLinus Torvalds void __init skb_init(void) 32751da177e4SLinus Torvalds { 32761da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 32771da177e4SLinus Torvalds sizeof(struct sk_buff), 32781da177e4SLinus Torvalds 0, 3279e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 328020c2df83SPaul Mundt NULL); 3281d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3282d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3283d179cd12SDavid S. Miller 0, 3284e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 328520c2df83SPaul Mundt NULL); 32861da177e4SLinus Torvalds } 32871da177e4SLinus Torvalds 3288716ea3a7SDavid Howells /** 3289716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3290716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3291716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3292716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3293716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3294716ea3a7SDavid Howells * 3295716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3296716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3297716ea3a7SDavid Howells */ 329851c739d1SDavid S. Miller static int 329951c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3300716ea3a7SDavid Howells { 33011a028e50SDavid S. Miller int start = skb_headlen(skb); 33021a028e50SDavid S. Miller int i, copy = start - offset; 3303fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3304716ea3a7SDavid Howells int elt = 0; 3305716ea3a7SDavid Howells 3306716ea3a7SDavid Howells if (copy > 0) { 3307716ea3a7SDavid Howells if (copy > len) 3308716ea3a7SDavid Howells copy = len; 3309642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3310716ea3a7SDavid Howells elt++; 3311716ea3a7SDavid Howells if ((len -= copy) == 0) 3312716ea3a7SDavid Howells return elt; 3313716ea3a7SDavid Howells offset += copy; 3314716ea3a7SDavid Howells } 3315716ea3a7SDavid Howells 3316716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33171a028e50SDavid S. Miller int end; 3318716ea3a7SDavid Howells 3319547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33201a028e50SDavid S. Miller 33219e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3322716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3323716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3324716ea3a7SDavid Howells 3325716ea3a7SDavid Howells if (copy > len) 3326716ea3a7SDavid Howells copy = len; 3327ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3328642f1490SJens Axboe frag->page_offset+offset-start); 3329716ea3a7SDavid Howells elt++; 3330716ea3a7SDavid Howells if (!(len -= copy)) 3331716ea3a7SDavid Howells return elt; 3332716ea3a7SDavid Howells offset += copy; 3333716ea3a7SDavid Howells } 33341a028e50SDavid S. Miller start = end; 3335716ea3a7SDavid Howells } 3336716ea3a7SDavid Howells 3337fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 33381a028e50SDavid S. Miller int end; 3339716ea3a7SDavid Howells 3340547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33411a028e50SDavid S. Miller 3342fbb398a8SDavid S. Miller end = start + frag_iter->len; 3343716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3344716ea3a7SDavid Howells if (copy > len) 3345716ea3a7SDavid Howells copy = len; 3346fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 334751c739d1SDavid S. Miller copy); 3348716ea3a7SDavid Howells if ((len -= copy) == 0) 3349716ea3a7SDavid Howells return elt; 3350716ea3a7SDavid Howells offset += copy; 3351716ea3a7SDavid Howells } 33521a028e50SDavid S. Miller start = end; 3353716ea3a7SDavid Howells } 3354716ea3a7SDavid Howells BUG_ON(len); 3355716ea3a7SDavid Howells return elt; 3356716ea3a7SDavid Howells } 3357716ea3a7SDavid Howells 335825a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 335925a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 336025a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 336125a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 336225a91d8dSFan Du * 336325a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 336425a91d8dSFan Du * 1. sg_init_table 336525a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 336625a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 336725a91d8dSFan Du * 336825a91d8dSFan Du * This is equivalent to: 336925a91d8dSFan Du * 1. sg_init_table 337025a91d8dSFan Du * 2. skb_to_sgvec(payload1) 337125a91d8dSFan Du * 3. sg_unmark_end 337225a91d8dSFan Du * 4. skb_to_sgvec(payload2) 337325a91d8dSFan Du * 337425a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 337525a91d8dSFan Du * is more preferable. 337625a91d8dSFan Du */ 337725a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 337825a91d8dSFan Du int offset, int len) 337925a91d8dSFan Du { 338025a91d8dSFan Du return __skb_to_sgvec(skb, sg, offset, len); 338125a91d8dSFan Du } 338225a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 338325a91d8dSFan Du 338451c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 338551c739d1SDavid S. Miller { 338651c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 338751c739d1SDavid S. Miller 3388c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 338951c739d1SDavid S. Miller 339051c739d1SDavid S. Miller return nsg; 339151c739d1SDavid S. Miller } 3392b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 339351c739d1SDavid S. Miller 3394716ea3a7SDavid Howells /** 3395716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3396716ea3a7SDavid Howells * @skb: The socket buffer to check. 3397716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3398716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3399716ea3a7SDavid Howells * 3400716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3401716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3402716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3403716ea3a7SDavid Howells * 3404716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3405716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3406716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3407716ea3a7SDavid Howells * 3408716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3409716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3410716ea3a7SDavid Howells */ 3411716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3412716ea3a7SDavid Howells { 3413716ea3a7SDavid Howells int copyflag; 3414716ea3a7SDavid Howells int elt; 3415716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3416716ea3a7SDavid Howells 3417716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3418716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3419716ea3a7SDavid Howells * at the moment even if they are anonymous). 3420716ea3a7SDavid Howells */ 3421716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3422716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3423716ea3a7SDavid Howells return -ENOMEM; 3424716ea3a7SDavid Howells 3425716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 342621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3427716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3428716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3429716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3430716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3431716ea3a7SDavid Howells 3432716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3433716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3434716ea3a7SDavid Howells return -ENOMEM; 3435716ea3a7SDavid Howells 3436716ea3a7SDavid Howells /* Voila! */ 3437716ea3a7SDavid Howells *trailer = skb; 3438716ea3a7SDavid Howells return 1; 3439716ea3a7SDavid Howells } 3440716ea3a7SDavid Howells 3441716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3442716ea3a7SDavid Howells 3443716ea3a7SDavid Howells elt = 1; 3444716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3445716ea3a7SDavid Howells copyflag = 0; 3446716ea3a7SDavid Howells 3447716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3448716ea3a7SDavid Howells int ntail = 0; 3449716ea3a7SDavid Howells 3450716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3451716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3452716ea3a7SDavid Howells * after it. */ 3453716ea3a7SDavid Howells 3454716ea3a7SDavid Howells if (skb_shared(skb1)) 3455716ea3a7SDavid Howells copyflag = 1; 3456716ea3a7SDavid Howells 3457716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3458716ea3a7SDavid Howells 3459716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3460716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 346121dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3462716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3463716ea3a7SDavid Howells ntail = tailbits + 128; 3464716ea3a7SDavid Howells } 3465716ea3a7SDavid Howells 3466716ea3a7SDavid Howells if (copyflag || 3467716ea3a7SDavid Howells skb_cloned(skb1) || 3468716ea3a7SDavid Howells ntail || 3469716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 347021dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3471716ea3a7SDavid Howells struct sk_buff *skb2; 3472716ea3a7SDavid Howells 3473716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3474716ea3a7SDavid Howells if (ntail == 0) 3475716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3476716ea3a7SDavid Howells else 3477716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3478716ea3a7SDavid Howells skb_headroom(skb1), 3479716ea3a7SDavid Howells ntail, 3480716ea3a7SDavid Howells GFP_ATOMIC); 3481716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3482716ea3a7SDavid Howells return -ENOMEM; 3483716ea3a7SDavid Howells 3484716ea3a7SDavid Howells if (skb1->sk) 3485716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3486716ea3a7SDavid Howells 3487716ea3a7SDavid Howells /* Looking around. Are we still alive? 3488716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3489716ea3a7SDavid Howells 3490716ea3a7SDavid Howells skb2->next = skb1->next; 3491716ea3a7SDavid Howells *skb_p = skb2; 3492716ea3a7SDavid Howells kfree_skb(skb1); 3493716ea3a7SDavid Howells skb1 = skb2; 3494716ea3a7SDavid Howells } 3495716ea3a7SDavid Howells elt++; 3496716ea3a7SDavid Howells *trailer = skb1; 3497716ea3a7SDavid Howells skb_p = &skb1->next; 3498716ea3a7SDavid Howells } 3499716ea3a7SDavid Howells 3500716ea3a7SDavid Howells return elt; 3501716ea3a7SDavid Howells } 3502b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3503716ea3a7SDavid Howells 3504b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3505b1faf566SEric Dumazet { 3506b1faf566SEric Dumazet struct sock *sk = skb->sk; 3507b1faf566SEric Dumazet 3508b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3509b1faf566SEric Dumazet } 3510b1faf566SEric Dumazet 3511b1faf566SEric Dumazet /* 3512b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3513b1faf566SEric Dumazet */ 3514b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3515b1faf566SEric Dumazet { 3516b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 351795c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3518b1faf566SEric Dumazet return -ENOMEM; 3519b1faf566SEric Dumazet 3520b1faf566SEric Dumazet skb_orphan(skb); 3521b1faf566SEric Dumazet skb->sk = sk; 3522b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3523b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3524b1faf566SEric Dumazet 3525abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3526abb57ea4SEric Dumazet skb_dst_force(skb); 3527abb57ea4SEric Dumazet 3528b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3529b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3530676d2369SDavid S. Miller sk->sk_data_ready(sk); 3531b1faf566SEric Dumazet return 0; 3532b1faf566SEric Dumazet } 3533b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3534b1faf566SEric Dumazet 3535364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3536364a9e93SWillem de Bruijn { 3537364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 3538364a9e93SWillem de Bruijn struct sk_buff *skb, *skb_next; 3539364a9e93SWillem de Bruijn int err = 0; 3540364a9e93SWillem de Bruijn 3541364a9e93SWillem de Bruijn spin_lock_bh(&q->lock); 3542364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 3543364a9e93SWillem de Bruijn if (skb && (skb_next = skb_peek(q))) 3544364a9e93SWillem de Bruijn err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 3545364a9e93SWillem de Bruijn spin_unlock_bh(&q->lock); 3546364a9e93SWillem de Bruijn 3547364a9e93SWillem de Bruijn sk->sk_err = err; 3548364a9e93SWillem de Bruijn if (err) 3549364a9e93SWillem de Bruijn sk->sk_error_report(sk); 3550364a9e93SWillem de Bruijn 3551364a9e93SWillem de Bruijn return skb; 3552364a9e93SWillem de Bruijn } 3553364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 3554364a9e93SWillem de Bruijn 3555cab41c47SAlexander Duyck /** 3556cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 3557cab41c47SAlexander Duyck * @skb: the skb to clone 3558cab41c47SAlexander Duyck * 3559cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 3560cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 3561cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 3562cab41c47SAlexander Duyck * 3563cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 3564cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 3565cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 3566cab41c47SAlexander Duyck * the sk_error_queue. 3567cab41c47SAlexander Duyck */ 356862bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 356962bccb8cSAlexander Duyck { 357062bccb8cSAlexander Duyck struct sock *sk = skb->sk; 357162bccb8cSAlexander Duyck struct sk_buff *clone; 357262bccb8cSAlexander Duyck 357362bccb8cSAlexander Duyck if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) 357462bccb8cSAlexander Duyck return NULL; 357562bccb8cSAlexander Duyck 357662bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 357762bccb8cSAlexander Duyck if (!clone) { 357862bccb8cSAlexander Duyck sock_put(sk); 357962bccb8cSAlexander Duyck return NULL; 358062bccb8cSAlexander Duyck } 358162bccb8cSAlexander Duyck 358262bccb8cSAlexander Duyck clone->sk = sk; 358362bccb8cSAlexander Duyck clone->destructor = sock_efree; 358462bccb8cSAlexander Duyck 358562bccb8cSAlexander Duyck return clone; 358662bccb8cSAlexander Duyck } 358762bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 358862bccb8cSAlexander Duyck 358937846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 359037846ef0SAlexander Duyck struct sock *sk, 359137846ef0SAlexander Duyck int tstype) 3592ac45f602SPatrick Ohly { 3593ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3594ac45f602SPatrick Ohly int err; 3595ac45f602SPatrick Ohly 3596ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3597ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3598ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3599ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3600e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 36014ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 360209c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 36034ed2d765SWillem de Bruijn if (sk->sk_protocol == IPPROTO_TCP) 36044ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 36054ed2d765SWillem de Bruijn } 360629030374SEric Dumazet 3607ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 360829030374SEric Dumazet 3609ac45f602SPatrick Ohly if (err) 3610ac45f602SPatrick Ohly kfree_skb(skb); 3611ac45f602SPatrick Ohly } 361237846ef0SAlexander Duyck 361337846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 361437846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 361537846ef0SAlexander Duyck { 361637846ef0SAlexander Duyck struct sock *sk = skb->sk; 361737846ef0SAlexander Duyck 361862bccb8cSAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 361962bccb8cSAlexander Duyck sock_hold(sk); 362037846ef0SAlexander Duyck 362137846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 362237846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); 362337846ef0SAlexander Duyck 362437846ef0SAlexander Duyck sock_put(sk); 362537846ef0SAlexander Duyck } 362637846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 362737846ef0SAlexander Duyck 362837846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 362937846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 363037846ef0SAlexander Duyck struct sock *sk, int tstype) 363137846ef0SAlexander Duyck { 363237846ef0SAlexander Duyck struct sk_buff *skb; 363337846ef0SAlexander Duyck 363437846ef0SAlexander Duyck if (!sk) 363537846ef0SAlexander Duyck return; 363637846ef0SAlexander Duyck 363737846ef0SAlexander Duyck if (hwtstamps) 363837846ef0SAlexander Duyck *skb_hwtstamps(orig_skb) = *hwtstamps; 363937846ef0SAlexander Duyck else 364037846ef0SAlexander Duyck orig_skb->tstamp = ktime_get_real(); 364137846ef0SAlexander Duyck 364237846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 364337846ef0SAlexander Duyck if (!skb) 364437846ef0SAlexander Duyck return; 364537846ef0SAlexander Duyck 364637846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, tstype); 364737846ef0SAlexander Duyck } 3648e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3649e7fd2885SWillem de Bruijn 3650e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 3651e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 3652e7fd2885SWillem de Bruijn { 3653e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3654e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 3655e7fd2885SWillem de Bruijn } 3656ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3657ac45f602SPatrick Ohly 36586e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 36596e3e939fSJohannes Berg { 36606e3e939fSJohannes Berg struct sock *sk = skb->sk; 36616e3e939fSJohannes Berg struct sock_exterr_skb *serr; 36626e3e939fSJohannes Berg int err; 36636e3e939fSJohannes Berg 36646e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 36656e3e939fSJohannes Berg skb->wifi_acked = acked; 36666e3e939fSJohannes Berg 36676e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 36686e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 36696e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 36706e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 36716e3e939fSJohannes Berg 3672bf7fa551SAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 3673bf7fa551SAlexander Duyck sock_hold(sk); 3674bf7fa551SAlexander Duyck 36756e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 36766e3e939fSJohannes Berg if (err) 36776e3e939fSJohannes Berg kfree_skb(skb); 3678bf7fa551SAlexander Duyck 3679bf7fa551SAlexander Duyck sock_put(sk); 36806e3e939fSJohannes Berg } 36816e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 36826e3e939fSJohannes Berg 3683ac45f602SPatrick Ohly 3684f35d9d8aSRusty Russell /** 3685f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3686f35d9d8aSRusty Russell * @skb: the skb to set 3687f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3688f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3689f35d9d8aSRusty Russell * 3690f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3691f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3692f35d9d8aSRusty Russell * 3693f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3694f35d9d8aSRusty Russell * returns false you should drop the packet. 3695f35d9d8aSRusty Russell */ 3696f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3697f35d9d8aSRusty Russell { 36985ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 36995ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3700e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 37015ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3702f35d9d8aSRusty Russell return false; 3703f35d9d8aSRusty Russell } 3704f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3705f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3706f35d9d8aSRusty Russell skb->csum_offset = off; 3707e5d5decaSJason Wang skb_set_transport_header(skb, start); 3708f35d9d8aSRusty Russell return true; 3709f35d9d8aSRusty Russell } 3710b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3711f35d9d8aSRusty Russell 3712ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 3713ed1f50c3SPaul Durrant unsigned int max) 3714ed1f50c3SPaul Durrant { 3715ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 3716ed1f50c3SPaul Durrant return 0; 3717ed1f50c3SPaul Durrant 3718ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 3719ed1f50c3SPaul Durrant * won't need to do it again. 3720ed1f50c3SPaul Durrant */ 3721ed1f50c3SPaul Durrant if (max > skb->len) 3722ed1f50c3SPaul Durrant max = skb->len; 3723ed1f50c3SPaul Durrant 3724ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 3725ed1f50c3SPaul Durrant return -ENOMEM; 3726ed1f50c3SPaul Durrant 3727ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 3728ed1f50c3SPaul Durrant return -EPROTO; 3729ed1f50c3SPaul Durrant 3730ed1f50c3SPaul Durrant return 0; 3731ed1f50c3SPaul Durrant } 3732ed1f50c3SPaul Durrant 3733f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 3734f9708b43SJan Beulich 3735f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 3736f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 3737f9708b43SJan Beulich unsigned int off) 3738f9708b43SJan Beulich { 3739f9708b43SJan Beulich switch (proto) { 3740f9708b43SJan Beulich int err; 3741f9708b43SJan Beulich 3742f9708b43SJan Beulich case IPPROTO_TCP: 3743f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 3744f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 3745f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3746f9708b43SJan Beulich offsetof(struct tcphdr, 3747f9708b43SJan Beulich check))) 3748f9708b43SJan Beulich err = -EPROTO; 3749f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 3750f9708b43SJan Beulich 3751f9708b43SJan Beulich case IPPROTO_UDP: 3752f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 3753f9708b43SJan Beulich off + sizeof(struct udphdr)); 3754f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3755f9708b43SJan Beulich offsetof(struct udphdr, 3756f9708b43SJan Beulich check))) 3757f9708b43SJan Beulich err = -EPROTO; 3758f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 3759f9708b43SJan Beulich } 3760f9708b43SJan Beulich 3761f9708b43SJan Beulich return ERR_PTR(-EPROTO); 3762f9708b43SJan Beulich } 3763f9708b43SJan Beulich 3764ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3765ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 3766ed1f50c3SPaul Durrant */ 3767ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 3768ed1f50c3SPaul Durrant 3769f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 3770ed1f50c3SPaul Durrant { 3771ed1f50c3SPaul Durrant unsigned int off; 3772ed1f50c3SPaul Durrant bool fragment; 3773f9708b43SJan Beulich __sum16 *csum; 3774ed1f50c3SPaul Durrant int err; 3775ed1f50c3SPaul Durrant 3776ed1f50c3SPaul Durrant fragment = false; 3777ed1f50c3SPaul Durrant 3778ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3779ed1f50c3SPaul Durrant sizeof(struct iphdr), 3780ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 3781ed1f50c3SPaul Durrant if (err < 0) 3782ed1f50c3SPaul Durrant goto out; 3783ed1f50c3SPaul Durrant 3784ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 3785ed1f50c3SPaul Durrant fragment = true; 3786ed1f50c3SPaul Durrant 3787ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 3788ed1f50c3SPaul Durrant 3789ed1f50c3SPaul Durrant err = -EPROTO; 3790ed1f50c3SPaul Durrant 3791ed1f50c3SPaul Durrant if (fragment) 3792ed1f50c3SPaul Durrant goto out; 3793ed1f50c3SPaul Durrant 3794f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 3795f9708b43SJan Beulich if (IS_ERR(csum)) 3796f9708b43SJan Beulich return PTR_ERR(csum); 3797ed1f50c3SPaul Durrant 3798ed1f50c3SPaul Durrant if (recalculate) 3799f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 3800ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 3801ed1f50c3SPaul Durrant skb->len - off, 3802f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 3803ed1f50c3SPaul Durrant err = 0; 3804ed1f50c3SPaul Durrant 3805ed1f50c3SPaul Durrant out: 3806ed1f50c3SPaul Durrant return err; 3807ed1f50c3SPaul Durrant } 3808ed1f50c3SPaul Durrant 3809ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3810ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 3811ed1f50c3SPaul Durrant */ 3812ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 3813ed1f50c3SPaul Durrant 3814ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 3815ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 3816ed1f50c3SPaul Durrant 3817ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 3818ed1f50c3SPaul Durrant { 3819ed1f50c3SPaul Durrant int err; 3820ed1f50c3SPaul Durrant u8 nexthdr; 3821ed1f50c3SPaul Durrant unsigned int off; 3822ed1f50c3SPaul Durrant unsigned int len; 3823ed1f50c3SPaul Durrant bool fragment; 3824ed1f50c3SPaul Durrant bool done; 3825f9708b43SJan Beulich __sum16 *csum; 3826ed1f50c3SPaul Durrant 3827ed1f50c3SPaul Durrant fragment = false; 3828ed1f50c3SPaul Durrant done = false; 3829ed1f50c3SPaul Durrant 3830ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 3831ed1f50c3SPaul Durrant 3832ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 3833ed1f50c3SPaul Durrant if (err < 0) 3834ed1f50c3SPaul Durrant goto out; 3835ed1f50c3SPaul Durrant 3836ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 3837ed1f50c3SPaul Durrant 3838ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 3839ed1f50c3SPaul Durrant while (off <= len && !done) { 3840ed1f50c3SPaul Durrant switch (nexthdr) { 3841ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 3842ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 3843ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 3844ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 3845ed1f50c3SPaul Durrant 3846ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3847ed1f50c3SPaul Durrant off + 3848ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 3849ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3850ed1f50c3SPaul Durrant if (err < 0) 3851ed1f50c3SPaul Durrant goto out; 3852ed1f50c3SPaul Durrant 3853ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 3854ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3855ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 3856ed1f50c3SPaul Durrant break; 3857ed1f50c3SPaul Durrant } 3858ed1f50c3SPaul Durrant case IPPROTO_AH: { 3859ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 3860ed1f50c3SPaul Durrant 3861ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3862ed1f50c3SPaul Durrant off + 3863ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 3864ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3865ed1f50c3SPaul Durrant if (err < 0) 3866ed1f50c3SPaul Durrant goto out; 3867ed1f50c3SPaul Durrant 3868ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 3869ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3870ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 3871ed1f50c3SPaul Durrant break; 3872ed1f50c3SPaul Durrant } 3873ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 3874ed1f50c3SPaul Durrant struct frag_hdr *hp; 3875ed1f50c3SPaul Durrant 3876ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3877ed1f50c3SPaul Durrant off + 3878ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 3879ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3880ed1f50c3SPaul Durrant if (err < 0) 3881ed1f50c3SPaul Durrant goto out; 3882ed1f50c3SPaul Durrant 3883ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 3884ed1f50c3SPaul Durrant 3885ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 3886ed1f50c3SPaul Durrant fragment = true; 3887ed1f50c3SPaul Durrant 3888ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3889ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 3890ed1f50c3SPaul Durrant break; 3891ed1f50c3SPaul Durrant } 3892ed1f50c3SPaul Durrant default: 3893ed1f50c3SPaul Durrant done = true; 3894ed1f50c3SPaul Durrant break; 3895ed1f50c3SPaul Durrant } 3896ed1f50c3SPaul Durrant } 3897ed1f50c3SPaul Durrant 3898ed1f50c3SPaul Durrant err = -EPROTO; 3899ed1f50c3SPaul Durrant 3900ed1f50c3SPaul Durrant if (!done || fragment) 3901ed1f50c3SPaul Durrant goto out; 3902ed1f50c3SPaul Durrant 3903f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 3904f9708b43SJan Beulich if (IS_ERR(csum)) 3905f9708b43SJan Beulich return PTR_ERR(csum); 3906ed1f50c3SPaul Durrant 3907ed1f50c3SPaul Durrant if (recalculate) 3908f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 3909ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 3910f9708b43SJan Beulich skb->len - off, nexthdr, 0); 3911ed1f50c3SPaul Durrant err = 0; 3912ed1f50c3SPaul Durrant 3913ed1f50c3SPaul Durrant out: 3914ed1f50c3SPaul Durrant return err; 3915ed1f50c3SPaul Durrant } 3916ed1f50c3SPaul Durrant 3917ed1f50c3SPaul Durrant /** 3918ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 3919ed1f50c3SPaul Durrant * @skb: the skb to set up 3920ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 3921ed1f50c3SPaul Durrant */ 3922ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 3923ed1f50c3SPaul Durrant { 3924ed1f50c3SPaul Durrant int err; 3925ed1f50c3SPaul Durrant 3926ed1f50c3SPaul Durrant switch (skb->protocol) { 3927ed1f50c3SPaul Durrant case htons(ETH_P_IP): 3928f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 3929ed1f50c3SPaul Durrant break; 3930ed1f50c3SPaul Durrant 3931ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 3932ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 3933ed1f50c3SPaul Durrant break; 3934ed1f50c3SPaul Durrant 3935ed1f50c3SPaul Durrant default: 3936ed1f50c3SPaul Durrant err = -EPROTO; 3937ed1f50c3SPaul Durrant break; 3938ed1f50c3SPaul Durrant } 3939ed1f50c3SPaul Durrant 3940ed1f50c3SPaul Durrant return err; 3941ed1f50c3SPaul Durrant } 3942ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 3943ed1f50c3SPaul Durrant 39444497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 39454497b076SBen Hutchings { 3946e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 3947e87cc472SJoe Perches skb->dev->name); 39484497b076SBen Hutchings } 39494497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 3950bad43ca8SEric Dumazet 3951bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 3952bad43ca8SEric Dumazet { 39533d861f66SEric Dumazet if (head_stolen) { 39543d861f66SEric Dumazet skb_release_head_state(skb); 3955bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 39563d861f66SEric Dumazet } else { 3957bad43ca8SEric Dumazet __kfree_skb(skb); 3958bad43ca8SEric Dumazet } 39593d861f66SEric Dumazet } 3960bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 3961bad43ca8SEric Dumazet 3962bad43ca8SEric Dumazet /** 3963bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 3964bad43ca8SEric Dumazet * @to: prior buffer 3965bad43ca8SEric Dumazet * @from: buffer to add 3966bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 3967c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 3968bad43ca8SEric Dumazet */ 3969bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 3970bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 3971bad43ca8SEric Dumazet { 3972bad43ca8SEric Dumazet int i, delta, len = from->len; 3973bad43ca8SEric Dumazet 3974bad43ca8SEric Dumazet *fragstolen = false; 3975bad43ca8SEric Dumazet 3976bad43ca8SEric Dumazet if (skb_cloned(to)) 3977bad43ca8SEric Dumazet return false; 3978bad43ca8SEric Dumazet 3979bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 3980e93a0435SEric Dumazet if (len) 3981bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 3982bad43ca8SEric Dumazet *delta_truesize = 0; 3983bad43ca8SEric Dumazet return true; 3984bad43ca8SEric Dumazet } 3985bad43ca8SEric Dumazet 3986bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 3987bad43ca8SEric Dumazet return false; 3988bad43ca8SEric Dumazet 3989bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 3990bad43ca8SEric Dumazet struct page *page; 3991bad43ca8SEric Dumazet unsigned int offset; 3992bad43ca8SEric Dumazet 3993bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 3994bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 3995bad43ca8SEric Dumazet return false; 3996bad43ca8SEric Dumazet 3997bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 3998bad43ca8SEric Dumazet return false; 3999bad43ca8SEric Dumazet 4000bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4001bad43ca8SEric Dumazet 4002bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4003bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4004bad43ca8SEric Dumazet 4005bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 4006bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4007bad43ca8SEric Dumazet *fragstolen = true; 4008bad43ca8SEric Dumazet } else { 4009bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4010bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4011bad43ca8SEric Dumazet return false; 4012bad43ca8SEric Dumazet 4013f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4014bad43ca8SEric Dumazet } 4015bad43ca8SEric Dumazet 4016bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4017bad43ca8SEric Dumazet 4018bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4019bad43ca8SEric Dumazet skb_shinfo(from)->frags, 4020bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4021bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4022bad43ca8SEric Dumazet 4023bad43ca8SEric Dumazet if (!skb_cloned(from)) 4024bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 4025bad43ca8SEric Dumazet 40268ea853fdSLi RongQing /* if the skb is not cloned this does nothing 40278ea853fdSLi RongQing * since we set nr_frags to 0. 40288ea853fdSLi RongQing */ 4029bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4030bad43ca8SEric Dumazet skb_frag_ref(from, i); 4031bad43ca8SEric Dumazet 4032bad43ca8SEric Dumazet to->truesize += delta; 4033bad43ca8SEric Dumazet to->len += len; 4034bad43ca8SEric Dumazet to->data_len += len; 4035bad43ca8SEric Dumazet 4036bad43ca8SEric Dumazet *delta_truesize = delta; 4037bad43ca8SEric Dumazet return true; 4038bad43ca8SEric Dumazet } 4039bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4040621e84d6SNicolas Dichtel 4041621e84d6SNicolas Dichtel /** 40428b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4043621e84d6SNicolas Dichtel * 4044621e84d6SNicolas Dichtel * @skb: buffer to clean 40458b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4046621e84d6SNicolas Dichtel * 40478b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 40488b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 40498b27f277SNicolas Dichtel * operations. 40508b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 40518b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 40528b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4053621e84d6SNicolas Dichtel */ 40548b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4055621e84d6SNicolas Dichtel { 40568b27f277SNicolas Dichtel if (xnet) 4057621e84d6SNicolas Dichtel skb_orphan(skb); 4058621e84d6SNicolas Dichtel skb->tstamp.tv64 = 0; 4059621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4060621e84d6SNicolas Dichtel skb->skb_iif = 0; 406160ff7467SWANG Cong skb->ignore_df = 0; 4062621e84d6SNicolas Dichtel skb_dst_drop(skb); 4063621e84d6SNicolas Dichtel skb->mark = 0; 4064621e84d6SNicolas Dichtel secpath_reset(skb); 4065621e84d6SNicolas Dichtel nf_reset(skb); 4066621e84d6SNicolas Dichtel nf_reset_trace(skb); 4067621e84d6SNicolas Dichtel } 4068621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4069de960aa9SFlorian Westphal 4070de960aa9SFlorian Westphal /** 4071de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4072de960aa9SFlorian Westphal * 4073de960aa9SFlorian Westphal * @skb: GSO skb 4074de960aa9SFlorian Westphal * 4075de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4076de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4077de960aa9SFlorian Westphal * 4078de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4079de960aa9SFlorian Westphal */ 4080de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4081de960aa9SFlorian Westphal { 4082de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4083f993bc25SFlorian Westphal unsigned int thlen = 0; 4084f993bc25SFlorian Westphal 4085f993bc25SFlorian Westphal if (skb->encapsulation) { 4086f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4087f993bc25SFlorian Westphal skb_transport_header(skb); 4088de960aa9SFlorian Westphal 4089de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4090f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4091f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4092f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 4093f993bc25SFlorian Westphal } 40946d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 40956d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 40966d39d589SFlorian Westphal * accounted for. 40976d39d589SFlorian Westphal */ 4098f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4099de960aa9SFlorian Westphal } 4100de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 41010d5501c1SVlad Yasevich 41020d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 41030d5501c1SVlad Yasevich { 41040d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 41050d5501c1SVlad Yasevich kfree_skb(skb); 41060d5501c1SVlad Yasevich return NULL; 41070d5501c1SVlad Yasevich } 41080d5501c1SVlad Yasevich 41090d5501c1SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN); 41100d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 41110d5501c1SVlad Yasevich return skb; 41120d5501c1SVlad Yasevich } 41130d5501c1SVlad Yasevich 41140d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 41150d5501c1SVlad Yasevich { 41160d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 41170d5501c1SVlad Yasevich u16 vlan_tci; 41180d5501c1SVlad Yasevich 41190d5501c1SVlad Yasevich if (unlikely(vlan_tx_tag_present(skb))) { 41200d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 41210d5501c1SVlad Yasevich return skb; 41220d5501c1SVlad Yasevich } 41230d5501c1SVlad Yasevich 41240d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 41250d5501c1SVlad Yasevich if (unlikely(!skb)) 41260d5501c1SVlad Yasevich goto err_free; 41270d5501c1SVlad Yasevich 41280d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 41290d5501c1SVlad Yasevich goto err_free; 41300d5501c1SVlad Yasevich 41310d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 41320d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 41330d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 41340d5501c1SVlad Yasevich 41350d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 41360d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 41370d5501c1SVlad Yasevich 41380d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 41390d5501c1SVlad Yasevich if (unlikely(!skb)) 41400d5501c1SVlad Yasevich goto err_free; 41410d5501c1SVlad Yasevich 41420d5501c1SVlad Yasevich skb_reset_network_header(skb); 41430d5501c1SVlad Yasevich skb_reset_transport_header(skb); 41440d5501c1SVlad Yasevich skb_reset_mac_len(skb); 41450d5501c1SVlad Yasevich 41460d5501c1SVlad Yasevich return skb; 41470d5501c1SVlad Yasevich 41480d5501c1SVlad Yasevich err_free: 41490d5501c1SVlad Yasevich kfree_skb(skb); 41500d5501c1SVlad Yasevich return NULL; 41510d5501c1SVlad Yasevich } 41520d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 41532e4e4410SEric Dumazet 4154e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 4155e2195121SJiri Pirko { 4156e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 4157e2195121SJiri Pirko return -ENOMEM; 4158e2195121SJiri Pirko 4159e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4160e2195121SJiri Pirko return 0; 4161e2195121SJiri Pirko 4162e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 4163e2195121SJiri Pirko } 4164e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 4165e2195121SJiri Pirko 41662e4e4410SEric Dumazet /** 41672e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 41682e4e4410SEric Dumazet * 4169de3f0d0eSMasanari Iida * @header_len: size of linear part 4170de3f0d0eSMasanari Iida * @data_len: needed length in frags 4171de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 4172de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 4173de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 41742e4e4410SEric Dumazet * 41752e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 41762e4e4410SEric Dumazet */ 41772e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 41782e4e4410SEric Dumazet unsigned long data_len, 41792e4e4410SEric Dumazet int max_page_order, 41802e4e4410SEric Dumazet int *errcode, 41812e4e4410SEric Dumazet gfp_t gfp_mask) 41822e4e4410SEric Dumazet { 41832e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 41842e4e4410SEric Dumazet unsigned long chunk; 41852e4e4410SEric Dumazet struct sk_buff *skb; 41862e4e4410SEric Dumazet struct page *page; 41872e4e4410SEric Dumazet gfp_t gfp_head; 41882e4e4410SEric Dumazet int i; 41892e4e4410SEric Dumazet 41902e4e4410SEric Dumazet *errcode = -EMSGSIZE; 41912e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 41922e4e4410SEric Dumazet * high order pages... 41932e4e4410SEric Dumazet */ 41942e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 41952e4e4410SEric Dumazet return NULL; 41962e4e4410SEric Dumazet 41972e4e4410SEric Dumazet gfp_head = gfp_mask; 41982e4e4410SEric Dumazet if (gfp_head & __GFP_WAIT) 41992e4e4410SEric Dumazet gfp_head |= __GFP_REPEAT; 42002e4e4410SEric Dumazet 42012e4e4410SEric Dumazet *errcode = -ENOBUFS; 42022e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 42032e4e4410SEric Dumazet if (!skb) 42042e4e4410SEric Dumazet return NULL; 42052e4e4410SEric Dumazet 42062e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 42072e4e4410SEric Dumazet 42082e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 42092e4e4410SEric Dumazet int order = max_page_order; 42102e4e4410SEric Dumazet 42112e4e4410SEric Dumazet while (order) { 42122e4e4410SEric Dumazet if (npages >= 1 << order) { 42132e4e4410SEric Dumazet page = alloc_pages(gfp_mask | 42142e4e4410SEric Dumazet __GFP_COMP | 42152e4e4410SEric Dumazet __GFP_NOWARN | 42162e4e4410SEric Dumazet __GFP_NORETRY, 42172e4e4410SEric Dumazet order); 42182e4e4410SEric Dumazet if (page) 42192e4e4410SEric Dumazet goto fill_page; 42202e4e4410SEric Dumazet /* Do not retry other high order allocations */ 42212e4e4410SEric Dumazet order = 1; 42222e4e4410SEric Dumazet max_page_order = 0; 42232e4e4410SEric Dumazet } 42242e4e4410SEric Dumazet order--; 42252e4e4410SEric Dumazet } 42262e4e4410SEric Dumazet page = alloc_page(gfp_mask); 42272e4e4410SEric Dumazet if (!page) 42282e4e4410SEric Dumazet goto failure; 42292e4e4410SEric Dumazet fill_page: 42302e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 42312e4e4410SEric Dumazet PAGE_SIZE << order); 42322e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 42332e4e4410SEric Dumazet data_len -= chunk; 42342e4e4410SEric Dumazet npages -= 1 << order; 42352e4e4410SEric Dumazet } 42362e4e4410SEric Dumazet return skb; 42372e4e4410SEric Dumazet 42382e4e4410SEric Dumazet failure: 42392e4e4410SEric Dumazet kfree_skb(skb); 42402e4e4410SEric Dumazet return NULL; 42412e4e4410SEric Dumazet } 42422e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 4243