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) { 260d179cd12SDavid S. Miller struct sk_buff *child = skb + 1; 261d179cd12SDavid S. Miller atomic_t *fclone_ref = (atomic_t *) (child + 1); 2621da177e4SLinus Torvalds 263fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(child, flags1); 264fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(child, flags2); 265d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 266d179cd12SDavid S. Miller atomic_set(fclone_ref, 1); 267d179cd12SDavid S. Miller 268d179cd12SDavid S. Miller child->fclone = SKB_FCLONE_UNAVAILABLE; 269c93bdd0eSMel Gorman child->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); 3486f532612SEric Dumazet nc = &__get_cpu_var(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; 363540eb7bfSAlexander Duyck recycle: 36469b08f62SEric Dumazet atomic_set(&nc->frag.page->_count, NETDEV_PAGECNT_MAX_BIAS); 36569b08f62SEric Dumazet nc->pagecnt_bias = NETDEV_PAGECNT_MAX_BIAS; 36669b08f62SEric Dumazet nc->frag.offset = 0; 3676f532612SEric Dumazet } 368540eb7bfSAlexander Duyck 36969b08f62SEric Dumazet if (nc->frag.offset + fragsz > nc->frag.size) { 370540eb7bfSAlexander Duyck /* avoid unnecessary locked operations if possible */ 37169b08f62SEric Dumazet if ((atomic_read(&nc->frag.page->_count) == nc->pagecnt_bias) || 37269b08f62SEric Dumazet atomic_sub_and_test(nc->pagecnt_bias, &nc->frag.page->_count)) 373540eb7bfSAlexander Duyck goto recycle; 3746f532612SEric Dumazet goto refill; 3756f532612SEric Dumazet } 376540eb7bfSAlexander Duyck 37769b08f62SEric Dumazet data = page_address(nc->frag.page) + nc->frag.offset; 37869b08f62SEric Dumazet nc->frag.offset += fragsz; 379540eb7bfSAlexander Duyck nc->pagecnt_bias--; 380540eb7bfSAlexander Duyck end: 3816f532612SEric Dumazet local_irq_restore(flags); 3826f532612SEric Dumazet return data; 3836f532612SEric Dumazet } 384c93bdd0eSMel Gorman 385c93bdd0eSMel Gorman /** 386c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 387c93bdd0eSMel Gorman * @fragsz: fragment size 388c93bdd0eSMel Gorman * 389c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 390c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 391c93bdd0eSMel Gorman */ 392c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 393c93bdd0eSMel Gorman { 394c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 395c93bdd0eSMel Gorman } 3966f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3976f532612SEric Dumazet 3986f532612SEric Dumazet /** 3998af27456SChristoph Hellwig * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 4008af27456SChristoph Hellwig * @dev: network device to receive on 4018af27456SChristoph Hellwig * @length: length to allocate 4028af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 4038af27456SChristoph Hellwig * 4048af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 4058af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 4068af27456SChristoph Hellwig * the headroom they think they need without accounting for the 4078af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 4088af27456SChristoph Hellwig * 4098af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 4108af27456SChristoph Hellwig */ 4118af27456SChristoph Hellwig struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 4128af27456SChristoph Hellwig unsigned int length, gfp_t gfp_mask) 4138af27456SChristoph Hellwig { 4146f532612SEric Dumazet struct sk_buff *skb = NULL; 415a1c7fff7SEric Dumazet unsigned int fragsz = SKB_DATA_ALIGN(length + NET_SKB_PAD) + 416a1c7fff7SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4178af27456SChristoph Hellwig 418310e158cSEric Dumazet if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { 419c93bdd0eSMel Gorman void *data; 420c93bdd0eSMel Gorman 421c93bdd0eSMel Gorman if (sk_memalloc_socks()) 422c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 423c93bdd0eSMel Gorman 424c93bdd0eSMel Gorman data = __netdev_alloc_frag(fragsz, gfp_mask); 425a1c7fff7SEric Dumazet 4266f532612SEric Dumazet if (likely(data)) { 4276f532612SEric Dumazet skb = build_skb(data, fragsz); 4286f532612SEric Dumazet if (unlikely(!skb)) 4296f532612SEric Dumazet put_page(virt_to_head_page(data)); 430a1c7fff7SEric Dumazet } 431a1c7fff7SEric Dumazet } else { 432c93bdd0eSMel Gorman skb = __alloc_skb(length + NET_SKB_PAD, gfp_mask, 433c93bdd0eSMel Gorman SKB_ALLOC_RX, NUMA_NO_NODE); 434a1c7fff7SEric Dumazet } 4357b2e497aSChristoph Hellwig if (likely(skb)) { 4368af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4377b2e497aSChristoph Hellwig skb->dev = dev; 4387b2e497aSChristoph Hellwig } 4398af27456SChristoph Hellwig return skb; 4408af27456SChristoph Hellwig } 441b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4421da177e4SLinus Torvalds 443654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 44450269e19SEric Dumazet int size, unsigned int truesize) 445654bed16SPeter Zijlstra { 446654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 447654bed16SPeter Zijlstra skb->len += size; 448654bed16SPeter Zijlstra skb->data_len += size; 44950269e19SEric Dumazet skb->truesize += truesize; 450654bed16SPeter Zijlstra } 451654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 452654bed16SPeter Zijlstra 453f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 454f8e617e1SJason Wang unsigned int truesize) 455f8e617e1SJason Wang { 456f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 457f8e617e1SJason Wang 458f8e617e1SJason Wang skb_frag_size_add(frag, size); 459f8e617e1SJason Wang skb->len += size; 460f8e617e1SJason Wang skb->data_len += size; 461f8e617e1SJason Wang skb->truesize += truesize; 462f8e617e1SJason Wang } 463f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 464f8e617e1SJason Wang 46527b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 4661da177e4SLinus Torvalds { 467bd8a7036SEric Dumazet kfree_skb_list(*listp); 46827b437c8SHerbert Xu *listp = NULL; 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 47127b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 47227b437c8SHerbert Xu { 47327b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 47427b437c8SHerbert Xu } 47527b437c8SHerbert Xu 4761da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds struct sk_buff *list; 4791da177e4SLinus Torvalds 480fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 4811da177e4SLinus Torvalds skb_get(list); 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds 484d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 485d3836f21SEric Dumazet { 486d3836f21SEric Dumazet if (skb->head_frag) 487d3836f21SEric Dumazet put_page(virt_to_head_page(skb->head)); 488d3836f21SEric Dumazet else 489d3836f21SEric Dumazet kfree(skb->head); 490d3836f21SEric Dumazet } 491d3836f21SEric Dumazet 4925bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 4931da177e4SLinus Torvalds { 4941da177e4SLinus Torvalds if (!skb->cloned || 4951da177e4SLinus Torvalds !atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 4961da177e4SLinus Torvalds &skb_shinfo(skb)->dataref)) { 4971da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 4981da177e4SLinus Torvalds int i; 4991da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 500ea2ab693SIan Campbell skb_frag_unref(skb, i); 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 503a6686f2fSShirley Ma /* 504a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 505a6686f2fSShirley Ma * the lower device DMA has done; 506a6686f2fSShirley Ma */ 507a6686f2fSShirley Ma if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 508a6686f2fSShirley Ma struct ubuf_info *uarg; 509a6686f2fSShirley Ma 510a6686f2fSShirley Ma uarg = skb_shinfo(skb)->destructor_arg; 511a6686f2fSShirley Ma if (uarg->callback) 512e19d6763SMichael S. Tsirkin uarg->callback(uarg, true); 513a6686f2fSShirley Ma } 514a6686f2fSShirley Ma 51521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 5161da177e4SLinus Torvalds skb_drop_fraglist(skb); 5171da177e4SLinus Torvalds 518d3836f21SEric Dumazet skb_free_head(skb); 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* 5231da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 5241da177e4SLinus Torvalds */ 5252d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 5261da177e4SLinus Torvalds { 527d179cd12SDavid S. Miller struct sk_buff *other; 528d179cd12SDavid S. Miller atomic_t *fclone_ref; 529d179cd12SDavid S. Miller 530d179cd12SDavid S. Miller switch (skb->fclone) { 531d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 5321da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 533d179cd12SDavid S. Miller break; 534d179cd12SDavid S. Miller 535d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 536d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 2); 537d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 538d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, skb); 539d179cd12SDavid S. Miller break; 540d179cd12SDavid S. Miller 541d179cd12SDavid S. Miller case SKB_FCLONE_CLONE: 542d179cd12SDavid S. Miller fclone_ref = (atomic_t *) (skb + 1); 543d179cd12SDavid S. Miller other = skb - 1; 544d179cd12SDavid S. Miller 545d179cd12SDavid S. Miller /* The clone portion is available for 546d179cd12SDavid S. Miller * fast-cloning again. 547d179cd12SDavid S. Miller */ 548d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_UNAVAILABLE; 549d179cd12SDavid S. Miller 550d179cd12SDavid S. Miller if (atomic_dec_and_test(fclone_ref)) 551d179cd12SDavid S. Miller kmem_cache_free(skbuff_fclone_cache, other); 552d179cd12SDavid S. Miller break; 5533ff50b79SStephen Hemminger } 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 55604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 5571da177e4SLinus Torvalds { 558adf30907SEric Dumazet skb_dst_drop(skb); 5591da177e4SLinus Torvalds #ifdef CONFIG_XFRM 5601da177e4SLinus Torvalds secpath_put(skb->sp); 5611da177e4SLinus Torvalds #endif 5621da177e4SLinus Torvalds if (skb->destructor) { 5639c2b3328SStephen Hemminger WARN_ON(in_irq()); 5641da177e4SLinus Torvalds skb->destructor(skb); 5651da177e4SLinus Torvalds } 566a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 5675f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 5682fc72c7bSKOVACS Krisztian #endif 5691da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER 5701da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 5711da177e4SLinus Torvalds #endif 5721da177e4SLinus Torvalds /* XXX: IS this still necessary? - JHS */ 5731da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED 5741da177e4SLinus Torvalds skb->tc_index = 0; 5751da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 5761da177e4SLinus Torvalds skb->tc_verd = 0; 5771da177e4SLinus Torvalds #endif 5781da177e4SLinus Torvalds #endif 57904a4bb55SLennert Buytenhek } 58004a4bb55SLennert Buytenhek 58104a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 58204a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 58304a4bb55SLennert Buytenhek { 58404a4bb55SLennert Buytenhek skb_release_head_state(skb); 5855e71d9d7SPablo Neira if (likely(skb->head)) 5862d4baff8SHerbert Xu skb_release_data(skb); 5872d4baff8SHerbert Xu } 5881da177e4SLinus Torvalds 5892d4baff8SHerbert Xu /** 5902d4baff8SHerbert Xu * __kfree_skb - private function 5912d4baff8SHerbert Xu * @skb: buffer 5922d4baff8SHerbert Xu * 5932d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 5942d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 5952d4baff8SHerbert Xu * always call kfree_skb 5962d4baff8SHerbert Xu */ 5972d4baff8SHerbert Xu 5982d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 5992d4baff8SHerbert Xu { 6002d4baff8SHerbert Xu skb_release_all(skb); 6011da177e4SLinus Torvalds kfree_skbmem(skb); 6021da177e4SLinus Torvalds } 603b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /** 606231d06aeSJörn Engel * kfree_skb - free an sk_buff 607231d06aeSJörn Engel * @skb: buffer to free 608231d06aeSJörn Engel * 609231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 610231d06aeSJörn Engel * hit zero. 611231d06aeSJörn Engel */ 612231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 613231d06aeSJörn Engel { 614231d06aeSJörn Engel if (unlikely(!skb)) 615231d06aeSJörn Engel return; 616231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 617231d06aeSJörn Engel smp_rmb(); 618231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 619231d06aeSJörn Engel return; 620ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 621231d06aeSJörn Engel __kfree_skb(skb); 622231d06aeSJörn Engel } 623b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 624231d06aeSJörn Engel 625bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 626bd8a7036SEric Dumazet { 627bd8a7036SEric Dumazet while (segs) { 628bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 629bd8a7036SEric Dumazet 630bd8a7036SEric Dumazet kfree_skb(segs); 631bd8a7036SEric Dumazet segs = next; 632bd8a7036SEric Dumazet } 633bd8a7036SEric Dumazet } 634bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 635bd8a7036SEric Dumazet 636d1a203eaSStephen Hemminger /** 63725121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 63825121173SMichael S. Tsirkin * @skb: buffer that triggered an error 63925121173SMichael S. Tsirkin * 64025121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 64125121173SMichael S. Tsirkin * skb must be freed afterwards. 64225121173SMichael S. Tsirkin */ 64325121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 64425121173SMichael S. Tsirkin { 64525121173SMichael S. Tsirkin if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 64625121173SMichael S. Tsirkin struct ubuf_info *uarg; 64725121173SMichael S. Tsirkin 64825121173SMichael S. Tsirkin uarg = skb_shinfo(skb)->destructor_arg; 64925121173SMichael S. Tsirkin if (uarg->callback) 65025121173SMichael S. Tsirkin uarg->callback(uarg, false); 65125121173SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 65225121173SMichael S. Tsirkin } 65325121173SMichael S. Tsirkin } 65425121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 65525121173SMichael S. Tsirkin 65625121173SMichael S. Tsirkin /** 657ead2ceb0SNeil Horman * consume_skb - free an skbuff 658ead2ceb0SNeil Horman * @skb: buffer to free 659ead2ceb0SNeil Horman * 660ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 661ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 662ead2ceb0SNeil Horman * is being dropped after a failure and notes that 663ead2ceb0SNeil Horman */ 664ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 665ead2ceb0SNeil Horman { 666ead2ceb0SNeil Horman if (unlikely(!skb)) 667ead2ceb0SNeil Horman return; 668ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 669ead2ceb0SNeil Horman smp_rmb(); 670ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 671ead2ceb0SNeil Horman return; 67207dc22e7SKoki Sanagi trace_consume_skb(skb); 673ead2ceb0SNeil Horman __kfree_skb(skb); 674ead2ceb0SNeil Horman } 675ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 676ead2ceb0SNeil Horman 677dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 678dec18810SHerbert Xu { 679dec18810SHerbert Xu new->tstamp = old->tstamp; 680dec18810SHerbert Xu new->dev = old->dev; 681dec18810SHerbert Xu new->transport_header = old->transport_header; 682dec18810SHerbert Xu new->network_header = old->network_header; 683dec18810SHerbert Xu new->mac_header = old->mac_header; 6844bc41b84SJoe Stringer new->inner_protocol = old->inner_protocol; 6856a674e9cSJoseph Gasparakis new->inner_transport_header = old->inner_transport_header; 68692df9b21SPravin B Shelar new->inner_network_header = old->inner_network_header; 687aefbd2b3SPravin B Shelar new->inner_mac_header = old->inner_mac_header; 6887fee226aSEric Dumazet skb_dst_copy(new, old); 6893df7a74eSTom Herbert skb_copy_hash(new, old); 6906461be3aSChangli Gao new->ooo_okay = old->ooo_okay; 6913bdc0ebaSBen Greear new->no_fcs = old->no_fcs; 6926a674e9cSJoseph Gasparakis new->encapsulation = old->encapsulation; 69346fb51ebSTom Herbert new->encap_hdr_csum = old->encap_hdr_csum; 69446fb51ebSTom Herbert new->csum_valid = old->csum_valid; 69546fb51ebSTom Herbert new->csum_complete_sw = old->csum_complete_sw; 696def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 697dec18810SHerbert Xu new->sp = secpath_get(old->sp); 698dec18810SHerbert Xu #endif 699dec18810SHerbert Xu memcpy(new->cb, old->cb, sizeof(old->cb)); 7009bcb97caSHerbert Xu new->csum = old->csum; 70160ff7467SWANG Cong new->ignore_df = old->ignore_df; 702dec18810SHerbert Xu new->pkt_type = old->pkt_type; 703dec18810SHerbert Xu new->ip_summed = old->ip_summed; 704dec18810SHerbert Xu skb_copy_queue_mapping(new, old); 705dec18810SHerbert Xu new->priority = old->priority; 706a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_IP_VS) 707dec18810SHerbert Xu new->ipvs_property = old->ipvs_property; 708dec18810SHerbert Xu #endif 709c93bdd0eSMel Gorman new->pfmemalloc = old->pfmemalloc; 710dec18810SHerbert Xu new->protocol = old->protocol; 711dec18810SHerbert Xu new->mark = old->mark; 7128964be4aSEric Dumazet new->skb_iif = old->skb_iif; 713dec18810SHerbert Xu __nf_copy(new, old); 714dec18810SHerbert Xu #ifdef CONFIG_NET_SCHED 715dec18810SHerbert Xu new->tc_index = old->tc_index; 716dec18810SHerbert Xu #ifdef CONFIG_NET_CLS_ACT 717dec18810SHerbert Xu new->tc_verd = old->tc_verd; 718dec18810SHerbert Xu #endif 719dec18810SHerbert Xu #endif 72086a9bad3SPatrick McHardy new->vlan_proto = old->vlan_proto; 7216aa895b0SPatrick McHardy new->vlan_tci = old->vlan_tci; 7226aa895b0SPatrick McHardy 723dec18810SHerbert Xu skb_copy_secmark(new, old); 72406021292SEliezer Tamir 725e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 72606021292SEliezer Tamir new->napi_id = old->napi_id; 72706021292SEliezer Tamir #endif 728dec18810SHerbert Xu } 729dec18810SHerbert Xu 73082c49a35SHerbert Xu /* 73182c49a35SHerbert Xu * You should not add any new code to this function. Add it to 73282c49a35SHerbert Xu * __copy_skb_header above instead. 73382c49a35SHerbert Xu */ 734e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds #define C(x) n->x = skb->x 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds n->next = n->prev = NULL; 7391da177e4SLinus Torvalds n->sk = NULL; 740dec18810SHerbert Xu __copy_skb_header(n, skb); 741dec18810SHerbert Xu 7421da177e4SLinus Torvalds C(len); 7431da177e4SLinus Torvalds C(data_len); 7443e6b3b2eSAlexey Dobriyan C(mac_len); 745334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 74602f1c89dSPaul Moore n->cloned = 1; 7471da177e4SLinus Torvalds n->nohdr = 0; 7481da177e4SLinus Torvalds n->destructor = NULL; 7491da177e4SLinus Torvalds C(tail); 7501da177e4SLinus Torvalds C(end); 75102f1c89dSPaul Moore C(head); 752d3836f21SEric Dumazet C(head_frag); 75302f1c89dSPaul Moore C(data); 75402f1c89dSPaul Moore C(truesize); 75502f1c89dSPaul Moore atomic_set(&n->users, 1); 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 7581da177e4SLinus Torvalds skb->cloned = 1; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds return n; 761e0053ec0SHerbert Xu #undef C 762e0053ec0SHerbert Xu } 763e0053ec0SHerbert Xu 764e0053ec0SHerbert Xu /** 765e0053ec0SHerbert Xu * skb_morph - morph one skb into another 766e0053ec0SHerbert Xu * @dst: the skb to receive the contents 767e0053ec0SHerbert Xu * @src: the skb to supply the contents 768e0053ec0SHerbert Xu * 769e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 770e0053ec0SHerbert Xu * supplied by the user. 771e0053ec0SHerbert Xu * 772e0053ec0SHerbert Xu * The target skb is returned upon exit. 773e0053ec0SHerbert Xu */ 774e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 775e0053ec0SHerbert Xu { 7762d4baff8SHerbert Xu skb_release_all(dst); 777e0053ec0SHerbert Xu return __skb_clone(dst, src); 778e0053ec0SHerbert Xu } 779e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 780e0053ec0SHerbert Xu 7812c53040fSBen Hutchings /** 7822c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 78348c83012SMichael S. Tsirkin * @skb: the skb to modify 78448c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 78548c83012SMichael S. Tsirkin * 78648c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 78748c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 78848c83012SMichael S. Tsirkin * to userspace pages. 78948c83012SMichael S. Tsirkin * 79048c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 79148c83012SMichael S. Tsirkin * %GFP_ATOMIC. 79248c83012SMichael S. Tsirkin * 79348c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 79448c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 79548c83012SMichael S. Tsirkin */ 79648c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 797a6686f2fSShirley Ma { 798a6686f2fSShirley Ma int i; 799a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 800a6686f2fSShirley Ma struct page *page, *head = NULL; 801a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 802a6686f2fSShirley Ma 803a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 804a6686f2fSShirley Ma u8 *vaddr; 805a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 806a6686f2fSShirley Ma 80702756ed4SKrishna Kumar page = alloc_page(gfp_mask); 808a6686f2fSShirley Ma if (!page) { 809a6686f2fSShirley Ma while (head) { 81040dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 811a6686f2fSShirley Ma put_page(head); 812a6686f2fSShirley Ma head = next; 813a6686f2fSShirley Ma } 814a6686f2fSShirley Ma return -ENOMEM; 815a6686f2fSShirley Ma } 81651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 817a6686f2fSShirley Ma memcpy(page_address(page), 8189e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 81951c56b00SEric Dumazet kunmap_atomic(vaddr); 82040dadff2SSunghan Suh set_page_private(page, (unsigned long)head); 821a6686f2fSShirley Ma head = page; 822a6686f2fSShirley Ma } 823a6686f2fSShirley Ma 824a6686f2fSShirley Ma /* skb frags release userspace buffers */ 82502756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 826a8605c60SIan Campbell skb_frag_unref(skb, i); 827a6686f2fSShirley Ma 828e19d6763SMichael S. Tsirkin uarg->callback(uarg, false); 829a6686f2fSShirley Ma 830a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 83102756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 83202756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 83302756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 83440dadff2SSunghan Suh head = (struct page *)page_private(head); 835a6686f2fSShirley Ma } 83648c83012SMichael S. Tsirkin 83748c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 838a6686f2fSShirley Ma return 0; 839a6686f2fSShirley Ma } 840dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 841a6686f2fSShirley Ma 842e0053ec0SHerbert Xu /** 843e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 844e0053ec0SHerbert Xu * @skb: buffer to clone 845e0053ec0SHerbert Xu * @gfp_mask: allocation priority 846e0053ec0SHerbert Xu * 847e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 848e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 849e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 850e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 851e0053ec0SHerbert Xu * 852e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 853e0053ec0SHerbert Xu * %GFP_ATOMIC. 854e0053ec0SHerbert Xu */ 855e0053ec0SHerbert Xu 856e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 857e0053ec0SHerbert Xu { 858e0053ec0SHerbert Xu struct sk_buff *n; 859e0053ec0SHerbert Xu 86070008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 861a6686f2fSShirley Ma return NULL; 862a6686f2fSShirley Ma 863e0053ec0SHerbert Xu n = skb + 1; 864e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 865e0053ec0SHerbert Xu n->fclone == SKB_FCLONE_UNAVAILABLE) { 866e0053ec0SHerbert Xu atomic_t *fclone_ref = (atomic_t *) (n + 1); 867e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_CLONE; 868e0053ec0SHerbert Xu atomic_inc(fclone_ref); 869e0053ec0SHerbert Xu } else { 870c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 871c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 872c93bdd0eSMel Gorman 873e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 874e0053ec0SHerbert Xu if (!n) 875e0053ec0SHerbert Xu return NULL; 876fe55f6d5SVegard Nossum 877fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 878fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags2); 879e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 880e0053ec0SHerbert Xu } 881e0053ec0SHerbert Xu 882e0053ec0SHerbert Xu return __skb_clone(n, skb); 8831da177e4SLinus Torvalds } 884b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 8851da177e4SLinus Torvalds 886f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 887f5b17294SPravin B Shelar { 888030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 889030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 890030737bcSEric Dumazet skb->csum_start += off; 891f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 892f5b17294SPravin B Shelar skb->transport_header += off; 893f5b17294SPravin B Shelar skb->network_header += off; 894f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 895f5b17294SPravin B Shelar skb->mac_header += off; 896f5b17294SPravin B Shelar skb->inner_transport_header += off; 897f5b17294SPravin B Shelar skb->inner_network_header += off; 898aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 899f5b17294SPravin B Shelar } 900f5b17294SPravin B Shelar 9011da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 9021da177e4SLinus Torvalds { 903dec18810SHerbert Xu __copy_skb_header(new, old); 904dec18810SHerbert Xu 9057967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 9067967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 9077967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 910c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 911c93bdd0eSMel Gorman { 912c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 913c93bdd0eSMel Gorman return SKB_ALLOC_RX; 914c93bdd0eSMel Gorman return 0; 915c93bdd0eSMel Gorman } 916c93bdd0eSMel Gorman 9171da177e4SLinus Torvalds /** 9181da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 9191da177e4SLinus Torvalds * @skb: buffer to copy 9201da177e4SLinus Torvalds * @gfp_mask: allocation priority 9211da177e4SLinus Torvalds * 9221da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 9231da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 9241da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 9251da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 9261da177e4SLinus Torvalds * 9271da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 9281da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 9291da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 9301da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 9311da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 9321da177e4SLinus Torvalds */ 9331da177e4SLinus Torvalds 934dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 9351da177e4SLinus Torvalds { 9366602cebbSEric Dumazet int headerlen = skb_headroom(skb); 937ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 938c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 939c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 9406602cebbSEric Dumazet 9411da177e4SLinus Torvalds if (!n) 9421da177e4SLinus Torvalds return NULL; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds /* Set the data pointer */ 9451da177e4SLinus Torvalds skb_reserve(n, headerlen); 9461da177e4SLinus Torvalds /* Set the tail pointer and length */ 9471da177e4SLinus Torvalds skb_put(n, skb->len); 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 9501da177e4SLinus Torvalds BUG(); 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds copy_skb_header(n, skb); 9531da177e4SLinus Torvalds return n; 9541da177e4SLinus Torvalds } 955b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds /** 958bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 9591da177e4SLinus Torvalds * @skb: buffer to copy 960117632e6SEric Dumazet * @headroom: headroom of new skb 9611da177e4SLinus Torvalds * @gfp_mask: allocation priority 962bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 963bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 964bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 9651da177e4SLinus Torvalds * 9661da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 9671da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 9681da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 9691da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 9701da177e4SLinus Torvalds * or the pointer to the buffer on success. 9711da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 9721da177e4SLinus Torvalds */ 9731da177e4SLinus Torvalds 974bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 975bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 9761da177e4SLinus Torvalds { 977117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 978bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 979bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 9806602cebbSEric Dumazet 9811da177e4SLinus Torvalds if (!n) 9821da177e4SLinus Torvalds goto out; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds /* Set the data pointer */ 985117632e6SEric Dumazet skb_reserve(n, headroom); 9861da177e4SLinus Torvalds /* Set the tail pointer and length */ 9871da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 9881da177e4SLinus Torvalds /* Copy the bytes */ 989d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 9901da177e4SLinus Torvalds 99125f484a6SHerbert Xu n->truesize += skb->data_len; 9921da177e4SLinus Torvalds n->data_len = skb->data_len; 9931da177e4SLinus Torvalds n->len = skb->len; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 9961da177e4SLinus Torvalds int i; 9971da177e4SLinus Torvalds 99870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 9991511022cSDan Carpenter kfree_skb(n); 10001511022cSDan Carpenter n = NULL; 1001a6686f2fSShirley Ma goto out; 1002a6686f2fSShirley Ma } 10031da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 10041da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1005ea2ab693SIan Campbell skb_frag_ref(skb, i); 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 101021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 10111da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 10121da177e4SLinus Torvalds skb_clone_fraglist(n); 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds copy_skb_header(n, skb); 10161da177e4SLinus Torvalds out: 10171da177e4SLinus Torvalds return n; 10181da177e4SLinus Torvalds } 1019bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds /** 10221da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 10231da177e4SLinus Torvalds * @skb: buffer to reallocate 10241da177e4SLinus Torvalds * @nhead: room to add at head 10251da177e4SLinus Torvalds * @ntail: room to add at tail 10261da177e4SLinus Torvalds * @gfp_mask: allocation priority 10271da177e4SLinus Torvalds * 1028bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1029bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 10301da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 10311da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 10321da177e4SLinus Torvalds * 10331da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 10341da177e4SLinus Torvalds * reloaded after call to this function. 10351da177e4SLinus Torvalds */ 10361da177e4SLinus Torvalds 103786a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1038dd0fc66fSAl Viro gfp_t gfp_mask) 10391da177e4SLinus Torvalds { 10401da177e4SLinus Torvalds int i; 10411da177e4SLinus Torvalds u8 *data; 1042ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 10431da177e4SLinus Torvalds long off; 10441da177e4SLinus Torvalds 10454edd87adSHerbert Xu BUG_ON(nhead < 0); 10464edd87adSHerbert Xu 10471da177e4SLinus Torvalds if (skb_shared(skb)) 10481da177e4SLinus Torvalds BUG(); 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 10511da177e4SLinus Torvalds 1052c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1053c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1054c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1055c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 10561da177e4SLinus Torvalds if (!data) 10571da177e4SLinus Torvalds goto nodata; 105887151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 10616602cebbSEric Dumazet * optimized for the cases when header is void. 10626602cebbSEric Dumazet */ 10636602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 10646602cebbSEric Dumazet 10656602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 10666602cebbSEric Dumazet skb_shinfo(skb), 1067fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 10681da177e4SLinus Torvalds 10693e24591aSAlexander Duyck /* 10703e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 10713e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 10723e24591aSAlexander Duyck * be since all we did is relocate the values 10733e24591aSAlexander Duyck */ 10743e24591aSAlexander Duyck if (skb_cloned(skb)) { 1075a6686f2fSShirley Ma /* copy this zero copy skb frags */ 107670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1077a6686f2fSShirley Ma goto nofrags; 10781da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1079ea2ab693SIan Campbell skb_frag_ref(skb, i); 10801da177e4SLinus Torvalds 108121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 10821da177e4SLinus Torvalds skb_clone_fraglist(skb); 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds skb_release_data(skb); 10853e24591aSAlexander Duyck } else { 10863e24591aSAlexander Duyck skb_free_head(skb); 10871fd63041SEric Dumazet } 10881da177e4SLinus Torvalds off = (data + nhead) - skb->head; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds skb->head = data; 1091d3836f21SEric Dumazet skb->head_frag = 0; 10921da177e4SLinus Torvalds skb->data += off; 10934305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 10944305b541SArnaldo Carvalho de Melo skb->end = size; 109556eb8882SPatrick McHardy off = nhead; 10964305b541SArnaldo Carvalho de Melo #else 10974305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 109856eb8882SPatrick McHardy #endif 109927a884dcSArnaldo Carvalho de Melo skb->tail += off; 1100b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 11011da177e4SLinus Torvalds skb->cloned = 0; 1102334a8132SPatrick McHardy skb->hdr_len = 0; 11031da177e4SLinus Torvalds skb->nohdr = 0; 11041da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 11051da177e4SLinus Torvalds return 0; 11061da177e4SLinus Torvalds 1107a6686f2fSShirley Ma nofrags: 1108a6686f2fSShirley Ma kfree(data); 11091da177e4SLinus Torvalds nodata: 11101da177e4SLinus Torvalds return -ENOMEM; 11111da177e4SLinus Torvalds } 1112b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 11171da177e4SLinus Torvalds { 11181da177e4SLinus Torvalds struct sk_buff *skb2; 11191da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds if (delta <= 0) 11221da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 11231da177e4SLinus Torvalds else { 11241da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 11251da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 11261da177e4SLinus Torvalds GFP_ATOMIC)) { 11271da177e4SLinus Torvalds kfree_skb(skb2); 11281da177e4SLinus Torvalds skb2 = NULL; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds return skb2; 11321da177e4SLinus Torvalds } 1133b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds /** 11361da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 11371da177e4SLinus Torvalds * @skb: buffer to copy 11381da177e4SLinus Torvalds * @newheadroom: new free bytes at head 11391da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 11401da177e4SLinus Torvalds * @gfp_mask: allocation priority 11411da177e4SLinus Torvalds * 11421da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 11431da177e4SLinus Torvalds * allocate additional space. 11441da177e4SLinus Torvalds * 11451da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 11461da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 11471da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 11481da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 11491da177e4SLinus Torvalds * 11501da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 11511da177e4SLinus Torvalds * is called from an interrupt. 11521da177e4SLinus Torvalds */ 11531da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 115486a76cafSVictor Fusco int newheadroom, int newtailroom, 1155dd0fc66fSAl Viro gfp_t gfp_mask) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds /* 11581da177e4SLinus Torvalds * Allocate the copy buffer 11591da177e4SLinus Torvalds */ 1160c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1161c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1162c93bdd0eSMel Gorman NUMA_NO_NODE); 1163efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 11641da177e4SLinus Torvalds int head_copy_len, head_copy_off; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds if (!n) 11671da177e4SLinus Torvalds return NULL; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds skb_reserve(n, newheadroom); 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* Set the tail pointer and length */ 11721da177e4SLinus Torvalds skb_put(n, skb->len); 11731da177e4SLinus Torvalds 1174efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 11751da177e4SLinus Torvalds head_copy_off = 0; 11761da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 11771da177e4SLinus Torvalds head_copy_len = newheadroom; 11781da177e4SLinus Torvalds else 11791da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds /* Copy the linear header and data. */ 11821da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 11831da177e4SLinus Torvalds skb->len + head_copy_len)) 11841da177e4SLinus Torvalds BUG(); 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds copy_skb_header(n, skb); 11871da177e4SLinus Torvalds 1188030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1189efd1e8d5SPatrick McHardy 11901da177e4SLinus Torvalds return n; 11911da177e4SLinus Torvalds } 1192b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /** 11951da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 11961da177e4SLinus Torvalds * @skb: buffer to pad 11971da177e4SLinus Torvalds * @pad: space to pad 11981da177e4SLinus Torvalds * 11991da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 12001da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 12011da177e4SLinus Torvalds * beyond the buffer end onto the wire. 12021da177e4SLinus Torvalds * 12035b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 12041da177e4SLinus Torvalds */ 12051da177e4SLinus Torvalds 12065b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 12071da177e4SLinus Torvalds { 12085b057c6bSHerbert Xu int err; 12095b057c6bSHerbert Xu int ntail; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 12125b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 12131da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 12145b057c6bSHerbert Xu return 0; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 12174305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 12185b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 12195b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 12205b057c6bSHerbert Xu if (unlikely(err)) 12215b057c6bSHerbert Xu goto free_skb; 12225b057c6bSHerbert Xu } 12235b057c6bSHerbert Xu 12245b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 12255b057c6bSHerbert Xu * to be audited. 12265b057c6bSHerbert Xu */ 12275b057c6bSHerbert Xu err = skb_linearize(skb); 12285b057c6bSHerbert Xu if (unlikely(err)) 12295b057c6bSHerbert Xu goto free_skb; 12305b057c6bSHerbert Xu 12315b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 12325b057c6bSHerbert Xu return 0; 12335b057c6bSHerbert Xu 12345b057c6bSHerbert Xu free_skb: 12351da177e4SLinus Torvalds kfree_skb(skb); 12365b057c6bSHerbert Xu return err; 12371da177e4SLinus Torvalds } 1238b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 12391da177e4SLinus Torvalds 12400dde3e16SIlpo Järvinen /** 12410c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 12420c7ddf36SMathias Krause * @skb: start of the buffer to use 12430c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 12440c7ddf36SMathias Krause * @len: amount of data to add 12450c7ddf36SMathias Krause * 12460c7ddf36SMathias Krause * This function extends the used data area of the potentially 12470c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 12480c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 12490c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 12500c7ddf36SMathias Krause * returned. 12510c7ddf36SMathias Krause */ 12520c7ddf36SMathias Krause 12530c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 12540c7ddf36SMathias Krause { 12550c7ddf36SMathias Krause if (tail != skb) { 12560c7ddf36SMathias Krause skb->data_len += len; 12570c7ddf36SMathias Krause skb->len += len; 12580c7ddf36SMathias Krause } 12590c7ddf36SMathias Krause return skb_put(tail, len); 12600c7ddf36SMathias Krause } 12610c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 12620c7ddf36SMathias Krause 12630c7ddf36SMathias Krause /** 12640dde3e16SIlpo Järvinen * skb_put - add data to a buffer 12650dde3e16SIlpo Järvinen * @skb: buffer to use 12660dde3e16SIlpo Järvinen * @len: amount of data to add 12670dde3e16SIlpo Järvinen * 12680dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 12690dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 12700dde3e16SIlpo Järvinen * first byte of the extra data is returned. 12710dde3e16SIlpo Järvinen */ 12720dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 12730dde3e16SIlpo Järvinen { 12740dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 12750dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 12760dde3e16SIlpo Järvinen skb->tail += len; 12770dde3e16SIlpo Järvinen skb->len += len; 12780dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 12790dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 12800dde3e16SIlpo Järvinen return tmp; 12810dde3e16SIlpo Järvinen } 12820dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 12830dde3e16SIlpo Järvinen 12846be8ac2fSIlpo Järvinen /** 1285c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1286c2aa270aSIlpo Järvinen * @skb: buffer to use 1287c2aa270aSIlpo Järvinen * @len: amount of data to add 1288c2aa270aSIlpo Järvinen * 1289c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1290c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1291c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1292c2aa270aSIlpo Järvinen */ 1293c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1294c2aa270aSIlpo Järvinen { 1295c2aa270aSIlpo Järvinen skb->data -= len; 1296c2aa270aSIlpo Järvinen skb->len += len; 1297c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1298c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1299c2aa270aSIlpo Järvinen return skb->data; 1300c2aa270aSIlpo Järvinen } 1301c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1302c2aa270aSIlpo Järvinen 1303c2aa270aSIlpo Järvinen /** 13046be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 13056be8ac2fSIlpo Järvinen * @skb: buffer to use 13066be8ac2fSIlpo Järvinen * @len: amount of data to remove 13076be8ac2fSIlpo Järvinen * 13086be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 13096be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 13106be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 13116be8ac2fSIlpo Järvinen * the old data. 13126be8ac2fSIlpo Järvinen */ 13136be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 13146be8ac2fSIlpo Järvinen { 131547d29646SDavid S. Miller return skb_pull_inline(skb, len); 13166be8ac2fSIlpo Järvinen } 13176be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 13186be8ac2fSIlpo Järvinen 1319419ae74eSIlpo Järvinen /** 1320419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1321419ae74eSIlpo Järvinen * @skb: buffer to alter 1322419ae74eSIlpo Järvinen * @len: new length 1323419ae74eSIlpo Järvinen * 1324419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1325419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1326419ae74eSIlpo Järvinen * The skb must be linear. 1327419ae74eSIlpo Järvinen */ 1328419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1329419ae74eSIlpo Järvinen { 1330419ae74eSIlpo Järvinen if (skb->len > len) 1331419ae74eSIlpo Järvinen __skb_trim(skb, len); 1332419ae74eSIlpo Järvinen } 1333419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1334419ae74eSIlpo Järvinen 13353cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 13361da177e4SLinus Torvalds */ 13371da177e4SLinus Torvalds 13383cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 13391da177e4SLinus Torvalds { 134027b437c8SHerbert Xu struct sk_buff **fragp; 134127b437c8SHerbert Xu struct sk_buff *frag; 13421da177e4SLinus Torvalds int offset = skb_headlen(skb); 13431da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 13441da177e4SLinus Torvalds int i; 134527b437c8SHerbert Xu int err; 134627b437c8SHerbert Xu 134727b437c8SHerbert Xu if (skb_cloned(skb) && 134827b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 134927b437c8SHerbert Xu return err; 13501da177e4SLinus Torvalds 1351f4d26fb3SHerbert Xu i = 0; 1352f4d26fb3SHerbert Xu if (offset >= len) 1353f4d26fb3SHerbert Xu goto drop_pages; 1354f4d26fb3SHerbert Xu 1355f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 13569e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 135727b437c8SHerbert Xu 135827b437c8SHerbert Xu if (end < len) { 13591da177e4SLinus Torvalds offset = end; 136027b437c8SHerbert Xu continue; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13639e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 136427b437c8SHerbert Xu 1365f4d26fb3SHerbert Xu drop_pages: 136627b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 136727b437c8SHerbert Xu 136827b437c8SHerbert Xu for (; i < nfrags; i++) 1369ea2ab693SIan Campbell skb_frag_unref(skb, i); 137027b437c8SHerbert Xu 137121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 137227b437c8SHerbert Xu skb_drop_fraglist(skb); 1373f4d26fb3SHerbert Xu goto done; 137427b437c8SHerbert Xu } 137527b437c8SHerbert Xu 137627b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 137727b437c8SHerbert Xu fragp = &frag->next) { 137827b437c8SHerbert Xu int end = offset + frag->len; 137927b437c8SHerbert Xu 138027b437c8SHerbert Xu if (skb_shared(frag)) { 138127b437c8SHerbert Xu struct sk_buff *nfrag; 138227b437c8SHerbert Xu 138327b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 138427b437c8SHerbert Xu if (unlikely(!nfrag)) 138527b437c8SHerbert Xu return -ENOMEM; 138627b437c8SHerbert Xu 138727b437c8SHerbert Xu nfrag->next = frag->next; 138885bb2a60SEric Dumazet consume_skb(frag); 138927b437c8SHerbert Xu frag = nfrag; 139027b437c8SHerbert Xu *fragp = frag; 139127b437c8SHerbert Xu } 139227b437c8SHerbert Xu 139327b437c8SHerbert Xu if (end < len) { 139427b437c8SHerbert Xu offset = end; 139527b437c8SHerbert Xu continue; 139627b437c8SHerbert Xu } 139727b437c8SHerbert Xu 139827b437c8SHerbert Xu if (end > len && 139927b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 140027b437c8SHerbert Xu return err; 140127b437c8SHerbert Xu 140227b437c8SHerbert Xu if (frag->next) 140327b437c8SHerbert Xu skb_drop_list(&frag->next); 140427b437c8SHerbert Xu break; 140527b437c8SHerbert Xu } 140627b437c8SHerbert Xu 1407f4d26fb3SHerbert Xu done: 140827b437c8SHerbert Xu if (len > skb_headlen(skb)) { 14091da177e4SLinus Torvalds skb->data_len -= skb->len - len; 14101da177e4SLinus Torvalds skb->len = len; 14111da177e4SLinus Torvalds } else { 14121da177e4SLinus Torvalds skb->len = len; 14131da177e4SLinus Torvalds skb->data_len = 0; 141427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds return 0; 14181da177e4SLinus Torvalds } 1419b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds /** 14221da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 14231da177e4SLinus Torvalds * @skb: buffer to reallocate 14241da177e4SLinus Torvalds * @delta: number of bytes to advance tail 14251da177e4SLinus Torvalds * 14261da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 14271da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 14281da177e4SLinus Torvalds * data from fragmented part. 14291da177e4SLinus Torvalds * 14301da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 14311da177e4SLinus Torvalds * 14321da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 14331da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 14341da177e4SLinus Torvalds * 14351da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 14361da177e4SLinus Torvalds * reloaded after call to this function. 14371da177e4SLinus Torvalds */ 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 14401da177e4SLinus Torvalds * when it is necessary. 14411da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 14421da177e4SLinus Torvalds * 2. It may change skb pointers. 14431da177e4SLinus Torvalds * 14441da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 14451da177e4SLinus Torvalds */ 14461da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 14471da177e4SLinus Torvalds { 14481da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 14491da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 14501da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 14511da177e4SLinus Torvalds */ 14524305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 14551da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 14561da177e4SLinus Torvalds GFP_ATOMIC)) 14571da177e4SLinus Torvalds return NULL; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 146027a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 14611da177e4SLinus Torvalds BUG(); 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 14641da177e4SLinus Torvalds * size of pulled pages. Superb. 14651da177e4SLinus Torvalds */ 146621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 14671da177e4SLinus Torvalds goto pull_pages; 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 14701da177e4SLinus Torvalds eat = delta; 14711da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 14729e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 14739e903e08SEric Dumazet 14749e903e08SEric Dumazet if (size >= eat) 14751da177e4SLinus Torvalds goto pull_pages; 14769e903e08SEric Dumazet eat -= size; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 14801da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 14811da177e4SLinus Torvalds * but taking into account that pulling is expected to 14821da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 14831da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 14841da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 14851da177e4SLinus Torvalds */ 14861da177e4SLinus Torvalds if (eat) { 14871da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 14881da177e4SLinus Torvalds struct sk_buff *clone = NULL; 14891da177e4SLinus Torvalds struct sk_buff *insp = NULL; 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds do { 149209a62660SKris Katterjohn BUG_ON(!list); 14931da177e4SLinus Torvalds 14941da177e4SLinus Torvalds if (list->len <= eat) { 14951da177e4SLinus Torvalds /* Eaten as whole. */ 14961da177e4SLinus Torvalds eat -= list->len; 14971da177e4SLinus Torvalds list = list->next; 14981da177e4SLinus Torvalds insp = list; 14991da177e4SLinus Torvalds } else { 15001da177e4SLinus Torvalds /* Eaten partially. */ 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds if (skb_shared(list)) { 15031da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 15041da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 15051da177e4SLinus Torvalds if (!clone) 15061da177e4SLinus Torvalds return NULL; 15071da177e4SLinus Torvalds insp = list->next; 15081da177e4SLinus Torvalds list = clone; 15091da177e4SLinus Torvalds } else { 15101da177e4SLinus Torvalds /* This may be pulled without 15111da177e4SLinus Torvalds * problems. */ 15121da177e4SLinus Torvalds insp = list; 15131da177e4SLinus Torvalds } 15141da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 15151da177e4SLinus Torvalds kfree_skb(clone); 15161da177e4SLinus Torvalds return NULL; 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds break; 15191da177e4SLinus Torvalds } 15201da177e4SLinus Torvalds } while (eat); 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds /* Free pulled out fragments. */ 15231da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 15241da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 15251da177e4SLinus Torvalds kfree_skb(list); 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds /* And insert new clone at head. */ 15281da177e4SLinus Torvalds if (clone) { 15291da177e4SLinus Torvalds clone->next = list; 15301da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds pull_pages: 15361da177e4SLinus Torvalds eat = delta; 15371da177e4SLinus Torvalds k = 0; 15381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15399e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 15409e903e08SEric Dumazet 15419e903e08SEric Dumazet if (size <= eat) { 1542ea2ab693SIan Campbell skb_frag_unref(skb, i); 15439e903e08SEric Dumazet eat -= size; 15441da177e4SLinus Torvalds } else { 15451da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 15461da177e4SLinus Torvalds if (eat) { 15471da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 15489e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 15491da177e4SLinus Torvalds eat = 0; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds k++; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds skb->tail += delta; 15571da177e4SLinus Torvalds skb->data_len -= delta; 15581da177e4SLinus Torvalds 155927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 15601da177e4SLinus Torvalds } 1561b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 15621da177e4SLinus Torvalds 156322019b17SEric Dumazet /** 156422019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 156522019b17SEric Dumazet * @skb: source skb 156622019b17SEric Dumazet * @offset: offset in source 156722019b17SEric Dumazet * @to: destination buffer 156822019b17SEric Dumazet * @len: number of bytes to copy 156922019b17SEric Dumazet * 157022019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 157122019b17SEric Dumazet * destination buffer. 157222019b17SEric Dumazet * 157322019b17SEric Dumazet * CAUTION ! : 157422019b17SEric Dumazet * If its prototype is ever changed, 157522019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 157622019b17SEric Dumazet * since it is called from BPF assembly code. 157722019b17SEric Dumazet */ 15781da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 15791da177e4SLinus Torvalds { 15801a028e50SDavid S. Miller int start = skb_headlen(skb); 1581fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1582fbb398a8SDavid S. Miller int i, copy; 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds if (offset > (int)skb->len - len) 15851da177e4SLinus Torvalds goto fault; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds /* Copy header. */ 15881a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 15891da177e4SLinus Torvalds if (copy > len) 15901da177e4SLinus Torvalds copy = len; 1591d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 15921da177e4SLinus Torvalds if ((len -= copy) == 0) 15931da177e4SLinus Torvalds return 0; 15941da177e4SLinus Torvalds offset += copy; 15951da177e4SLinus Torvalds to += copy; 15961da177e4SLinus Torvalds } 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15991a028e50SDavid S. Miller int end; 160051c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 16011da177e4SLinus Torvalds 1602547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16031a028e50SDavid S. Miller 160451c56b00SEric Dumazet end = start + skb_frag_size(f); 16051da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16061da177e4SLinus Torvalds u8 *vaddr; 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds if (copy > len) 16091da177e4SLinus Torvalds copy = len; 16101da177e4SLinus Torvalds 161151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 16121da177e4SLinus Torvalds memcpy(to, 161351c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 161451c56b00SEric Dumazet copy); 161551c56b00SEric Dumazet kunmap_atomic(vaddr); 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds if ((len -= copy) == 0) 16181da177e4SLinus Torvalds return 0; 16191da177e4SLinus Torvalds offset += copy; 16201da177e4SLinus Torvalds to += copy; 16211da177e4SLinus Torvalds } 16221a028e50SDavid S. Miller start = end; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 1625fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 16261a028e50SDavid S. Miller int end; 16271da177e4SLinus Torvalds 1628547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16291a028e50SDavid S. Miller 1630fbb398a8SDavid S. Miller end = start + frag_iter->len; 16311da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16321da177e4SLinus Torvalds if (copy > len) 16331da177e4SLinus Torvalds copy = len; 1634fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 16351da177e4SLinus Torvalds goto fault; 16361da177e4SLinus Torvalds if ((len -= copy) == 0) 16371da177e4SLinus Torvalds return 0; 16381da177e4SLinus Torvalds offset += copy; 16391da177e4SLinus Torvalds to += copy; 16401da177e4SLinus Torvalds } 16411a028e50SDavid S. Miller start = end; 16421da177e4SLinus Torvalds } 1643a6686f2fSShirley Ma 16441da177e4SLinus Torvalds if (!len) 16451da177e4SLinus Torvalds return 0; 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds fault: 16481da177e4SLinus Torvalds return -EFAULT; 16491da177e4SLinus Torvalds } 1650b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 16511da177e4SLinus Torvalds 16529c55e01cSJens Axboe /* 16539c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 16549c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 16559c55e01cSJens Axboe */ 16569c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 16579c55e01cSJens Axboe { 16588b9d3728SJarek Poplawski put_page(spd->pages[i]); 16598b9d3728SJarek Poplawski } 16609c55e01cSJens Axboe 1661a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 16624fb66994SJarek Poplawski unsigned int *offset, 166318aafc62SEric Dumazet struct sock *sk) 16648b9d3728SJarek Poplawski { 16655640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 16668b9d3728SJarek Poplawski 16675640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 16688b9d3728SJarek Poplawski return NULL; 16694fb66994SJarek Poplawski 16705640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 16714fb66994SJarek Poplawski 16725640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 16735640f768SEric Dumazet page_address(page) + *offset, *len); 16745640f768SEric Dumazet *offset = pfrag->offset; 16755640f768SEric Dumazet pfrag->offset += *len; 16764fb66994SJarek Poplawski 16775640f768SEric Dumazet return pfrag->page; 16789c55e01cSJens Axboe } 16799c55e01cSJens Axboe 168041c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 168141c73a0dSEric Dumazet struct page *page, 168241c73a0dSEric Dumazet unsigned int offset) 168341c73a0dSEric Dumazet { 168441c73a0dSEric Dumazet return spd->nr_pages && 168541c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 168641c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 168741c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 168841c73a0dSEric Dumazet } 168941c73a0dSEric Dumazet 16909c55e01cSJens Axboe /* 16919c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 16929c55e01cSJens Axboe */ 1693a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 169435f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 16954fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 169618aafc62SEric Dumazet bool linear, 16977a67e56fSJarek Poplawski struct sock *sk) 16989c55e01cSJens Axboe { 169941c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1700a108d5f3SDavid S. Miller return true; 17019c55e01cSJens Axboe 17028b9d3728SJarek Poplawski if (linear) { 170318aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 17048b9d3728SJarek Poplawski if (!page) 1705a108d5f3SDavid S. Miller return true; 170641c73a0dSEric Dumazet } 170741c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 170841c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1709a108d5f3SDavid S. Miller return false; 171041c73a0dSEric Dumazet } 17118b9d3728SJarek Poplawski get_page(page); 17129c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 17134fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 17149c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 17159c55e01cSJens Axboe spd->nr_pages++; 17168b9d3728SJarek Poplawski 1717a108d5f3SDavid S. Miller return false; 17189c55e01cSJens Axboe } 17199c55e01cSJens Axboe 1720a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 17212870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 172218aafc62SEric Dumazet unsigned int *len, 1723d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 172435f3d14dSJens Axboe struct sock *sk, 172535f3d14dSJens Axboe struct pipe_inode_info *pipe) 17269c55e01cSJens Axboe { 17272870c43dSOctavian Purdila if (!*len) 1728a108d5f3SDavid S. Miller return true; 17299c55e01cSJens Axboe 17302870c43dSOctavian Purdila /* skip this segment if already processed */ 17312870c43dSOctavian Purdila if (*off >= plen) { 17322870c43dSOctavian Purdila *off -= plen; 1733a108d5f3SDavid S. Miller return false; 17342870c43dSOctavian Purdila } 17352870c43dSOctavian Purdila 17362870c43dSOctavian Purdila /* ignore any bits we already processed */ 17379ca1b22dSEric Dumazet poff += *off; 17389ca1b22dSEric Dumazet plen -= *off; 17392870c43dSOctavian Purdila *off = 0; 17402870c43dSOctavian Purdila 174118aafc62SEric Dumazet do { 174218aafc62SEric Dumazet unsigned int flen = min(*len, plen); 17432870c43dSOctavian Purdila 174418aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 174518aafc62SEric Dumazet linear, sk)) 1746a108d5f3SDavid S. Miller return true; 174718aafc62SEric Dumazet poff += flen; 174818aafc62SEric Dumazet plen -= flen; 17492870c43dSOctavian Purdila *len -= flen; 175018aafc62SEric Dumazet } while (*len && plen); 17512870c43dSOctavian Purdila 1752a108d5f3SDavid S. Miller return false; 1753db43a282SOctavian Purdila } 17549c55e01cSJens Axboe 17559c55e01cSJens Axboe /* 1756a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 17572870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 17589c55e01cSJens Axboe */ 1759a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 176035f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 176135f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 17622870c43dSOctavian Purdila { 17632870c43dSOctavian Purdila int seg; 17649c55e01cSJens Axboe 17651d0c0b32SEric Dumazet /* map the linear part : 17662996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 17672996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 17682996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 17699c55e01cSJens Axboe */ 17702870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 17712870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 17722870c43dSOctavian Purdila skb_headlen(skb), 177318aafc62SEric Dumazet offset, len, spd, 17743a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 17751d0c0b32SEric Dumazet sk, pipe)) 1776a108d5f3SDavid S. Miller return true; 17779c55e01cSJens Axboe 17789c55e01cSJens Axboe /* 17799c55e01cSJens Axboe * then map the fragments 17809c55e01cSJens Axboe */ 17819c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 17829c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 17839c55e01cSJens Axboe 1784ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 17859e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 178618aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 1787a108d5f3SDavid S. Miller return true; 17889c55e01cSJens Axboe } 17899c55e01cSJens Axboe 1790a108d5f3SDavid S. Miller return false; 17919c55e01cSJens Axboe } 17929c55e01cSJens Axboe 17939c55e01cSJens Axboe /* 17949c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 17959c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 17969c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 17979c55e01cSJens Axboe * handle that cleanly. 17989c55e01cSJens Axboe */ 17998b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 18009c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 18019c55e01cSJens Axboe unsigned int flags) 18029c55e01cSJens Axboe { 180341c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 180441c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 18059c55e01cSJens Axboe struct splice_pipe_desc spd = { 18069c55e01cSJens Axboe .pages = pages, 18079c55e01cSJens Axboe .partial = partial, 1808047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 18099c55e01cSJens Axboe .flags = flags, 181028a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 18119c55e01cSJens Axboe .spd_release = sock_spd_release, 18129c55e01cSJens Axboe }; 1813fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 18147a67e56fSJarek Poplawski struct sock *sk = skb->sk; 181535f3d14dSJens Axboe int ret = 0; 181635f3d14dSJens Axboe 18179c55e01cSJens Axboe /* 18189c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 18199c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 18209c55e01cSJens Axboe */ 182135f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 18229c55e01cSJens Axboe goto done; 18239c55e01cSJens Axboe else if (!tlen) 18249c55e01cSJens Axboe goto done; 18259c55e01cSJens Axboe 18269c55e01cSJens Axboe /* 18279c55e01cSJens Axboe * now see if we have a frag_list to map 18289c55e01cSJens Axboe */ 1829fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1830fbb398a8SDavid S. Miller if (!tlen) 18319c55e01cSJens Axboe break; 183235f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1833fbb398a8SDavid S. Miller break; 18349c55e01cSJens Axboe } 18359c55e01cSJens Axboe 18369c55e01cSJens Axboe done: 18379c55e01cSJens Axboe if (spd.nr_pages) { 18389c55e01cSJens Axboe /* 18399c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 18409c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 18419c55e01cSJens Axboe * here as compared to sendfile(). We enter here 18429c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 18439c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 18449c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 18459c55e01cSJens Axboe * and networking will grab the socket lock. 18469c55e01cSJens Axboe */ 1847293ad604SOctavian Purdila release_sock(sk); 18489c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1849293ad604SOctavian Purdila lock_sock(sk); 18509c55e01cSJens Axboe } 18519c55e01cSJens Axboe 185235f3d14dSJens Axboe return ret; 18539c55e01cSJens Axboe } 18549c55e01cSJens Axboe 1855357b40a1SHerbert Xu /** 1856357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1857357b40a1SHerbert Xu * @skb: destination buffer 1858357b40a1SHerbert Xu * @offset: offset in destination 1859357b40a1SHerbert Xu * @from: source buffer 1860357b40a1SHerbert Xu * @len: number of bytes to copy 1861357b40a1SHerbert Xu * 1862357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1863357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1864357b40a1SHerbert Xu * traversing fragment lists and such. 1865357b40a1SHerbert Xu */ 1866357b40a1SHerbert Xu 18670c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1868357b40a1SHerbert Xu { 18691a028e50SDavid S. Miller int start = skb_headlen(skb); 1870fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1871fbb398a8SDavid S. Miller int i, copy; 1872357b40a1SHerbert Xu 1873357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1874357b40a1SHerbert Xu goto fault; 1875357b40a1SHerbert Xu 18761a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1877357b40a1SHerbert Xu if (copy > len) 1878357b40a1SHerbert Xu copy = len; 187927d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1880357b40a1SHerbert Xu if ((len -= copy) == 0) 1881357b40a1SHerbert Xu return 0; 1882357b40a1SHerbert Xu offset += copy; 1883357b40a1SHerbert Xu from += copy; 1884357b40a1SHerbert Xu } 1885357b40a1SHerbert Xu 1886357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1887357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 18881a028e50SDavid S. Miller int end; 1889357b40a1SHerbert Xu 1890547b792cSIlpo Järvinen WARN_ON(start > offset + len); 18911a028e50SDavid S. Miller 18929e903e08SEric Dumazet end = start + skb_frag_size(frag); 1893357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1894357b40a1SHerbert Xu u8 *vaddr; 1895357b40a1SHerbert Xu 1896357b40a1SHerbert Xu if (copy > len) 1897357b40a1SHerbert Xu copy = len; 1898357b40a1SHerbert Xu 189951c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19001a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 19011a028e50SDavid S. Miller from, copy); 190251c56b00SEric Dumazet kunmap_atomic(vaddr); 1903357b40a1SHerbert Xu 1904357b40a1SHerbert Xu if ((len -= copy) == 0) 1905357b40a1SHerbert Xu return 0; 1906357b40a1SHerbert Xu offset += copy; 1907357b40a1SHerbert Xu from += copy; 1908357b40a1SHerbert Xu } 19091a028e50SDavid S. Miller start = end; 1910357b40a1SHerbert Xu } 1911357b40a1SHerbert Xu 1912fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 19131a028e50SDavid S. Miller int end; 1914357b40a1SHerbert Xu 1915547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19161a028e50SDavid S. Miller 1917fbb398a8SDavid S. Miller end = start + frag_iter->len; 1918357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1919357b40a1SHerbert Xu if (copy > len) 1920357b40a1SHerbert Xu copy = len; 1921fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 19221a028e50SDavid S. Miller from, copy)) 1923357b40a1SHerbert Xu goto fault; 1924357b40a1SHerbert Xu if ((len -= copy) == 0) 1925357b40a1SHerbert Xu return 0; 1926357b40a1SHerbert Xu offset += copy; 1927357b40a1SHerbert Xu from += copy; 1928357b40a1SHerbert Xu } 19291a028e50SDavid S. Miller start = end; 1930357b40a1SHerbert Xu } 1931357b40a1SHerbert Xu if (!len) 1932357b40a1SHerbert Xu return 0; 1933357b40a1SHerbert Xu 1934357b40a1SHerbert Xu fault: 1935357b40a1SHerbert Xu return -EFAULT; 1936357b40a1SHerbert Xu } 1937357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 1938357b40a1SHerbert Xu 19391da177e4SLinus Torvalds /* Checksum skb data. */ 19402817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 19412817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 19421da177e4SLinus Torvalds { 19431a028e50SDavid S. Miller int start = skb_headlen(skb); 19441a028e50SDavid S. Miller int i, copy = start - offset; 1945fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19461da177e4SLinus Torvalds int pos = 0; 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds /* Checksum header. */ 19491da177e4SLinus Torvalds if (copy > 0) { 19501da177e4SLinus Torvalds if (copy > len) 19511da177e4SLinus Torvalds copy = len; 19522817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 19531da177e4SLinus Torvalds if ((len -= copy) == 0) 19541da177e4SLinus Torvalds return csum; 19551da177e4SLinus Torvalds offset += copy; 19561da177e4SLinus Torvalds pos = copy; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 19601a028e50SDavid S. Miller int end; 196151c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19621da177e4SLinus Torvalds 1963547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19641a028e50SDavid S. Miller 196551c56b00SEric Dumazet end = start + skb_frag_size(frag); 19661da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 196744bb9363SAl Viro __wsum csum2; 19681da177e4SLinus Torvalds u8 *vaddr; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds if (copy > len) 19711da177e4SLinus Torvalds copy = len; 197251c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19732817a336SDaniel Borkmann csum2 = ops->update(vaddr + frag->page_offset + 19741a028e50SDavid S. Miller offset - start, copy, 0); 197551c56b00SEric Dumazet kunmap_atomic(vaddr); 19762817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 19771da177e4SLinus Torvalds if (!(len -= copy)) 19781da177e4SLinus Torvalds return csum; 19791da177e4SLinus Torvalds offset += copy; 19801da177e4SLinus Torvalds pos += copy; 19811da177e4SLinus Torvalds } 19821a028e50SDavid S. Miller start = end; 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 1985fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 19861a028e50SDavid S. Miller int end; 19871da177e4SLinus Torvalds 1988547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19891a028e50SDavid S. Miller 1990fbb398a8SDavid S. Miller end = start + frag_iter->len; 19911da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 19925f92a738SAl Viro __wsum csum2; 19931da177e4SLinus Torvalds if (copy > len) 19941da177e4SLinus Torvalds copy = len; 19952817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 19962817a336SDaniel Borkmann copy, 0, ops); 19972817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 19981da177e4SLinus Torvalds if ((len -= copy) == 0) 19991da177e4SLinus Torvalds return csum; 20001da177e4SLinus Torvalds offset += copy; 20011da177e4SLinus Torvalds pos += copy; 20021da177e4SLinus Torvalds } 20031a028e50SDavid S. Miller start = end; 20041da177e4SLinus Torvalds } 200509a62660SKris Katterjohn BUG_ON(len); 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds return csum; 20081da177e4SLinus Torvalds } 20092817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 20102817a336SDaniel Borkmann 20112817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 20122817a336SDaniel Borkmann int len, __wsum csum) 20132817a336SDaniel Borkmann { 20142817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2015cea80ea8SDaniel Borkmann .update = csum_partial_ext, 20162817a336SDaniel Borkmann .combine = csum_block_add_ext, 20172817a336SDaniel Borkmann }; 20182817a336SDaniel Borkmann 20192817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 20202817a336SDaniel Borkmann } 2021b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds /* Both of above in one bottle. */ 20241da177e4SLinus Torvalds 202581d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 202681d77662SAl Viro u8 *to, int len, __wsum csum) 20271da177e4SLinus Torvalds { 20281a028e50SDavid S. Miller int start = skb_headlen(skb); 20291a028e50SDavid S. Miller int i, copy = start - offset; 2030fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 20311da177e4SLinus Torvalds int pos = 0; 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds /* Copy header. */ 20341da177e4SLinus Torvalds if (copy > 0) { 20351da177e4SLinus Torvalds if (copy > len) 20361da177e4SLinus Torvalds copy = len; 20371da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 20381da177e4SLinus Torvalds copy, csum); 20391da177e4SLinus Torvalds if ((len -= copy) == 0) 20401da177e4SLinus Torvalds return csum; 20411da177e4SLinus Torvalds offset += copy; 20421da177e4SLinus Torvalds to += copy; 20431da177e4SLinus Torvalds pos = copy; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20471a028e50SDavid S. Miller int end; 20481da177e4SLinus Torvalds 2049547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20501a028e50SDavid S. Miller 20519e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 20521da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20535084205fSAl Viro __wsum csum2; 20541da177e4SLinus Torvalds u8 *vaddr; 20551da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds if (copy > len) 20581da177e4SLinus Torvalds copy = len; 205951c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20601da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 20611a028e50SDavid S. Miller frag->page_offset + 20621a028e50SDavid S. Miller offset - start, to, 20631a028e50SDavid S. Miller copy, 0); 206451c56b00SEric Dumazet kunmap_atomic(vaddr); 20651da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 20661da177e4SLinus Torvalds if (!(len -= copy)) 20671da177e4SLinus Torvalds return csum; 20681da177e4SLinus Torvalds offset += copy; 20691da177e4SLinus Torvalds to += copy; 20701da177e4SLinus Torvalds pos += copy; 20711da177e4SLinus Torvalds } 20721a028e50SDavid S. Miller start = end; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 2075fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 207681d77662SAl Viro __wsum csum2; 20771a028e50SDavid S. Miller int end; 20781da177e4SLinus Torvalds 2079547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20801a028e50SDavid S. Miller 2081fbb398a8SDavid S. Miller end = start + frag_iter->len; 20821da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20831da177e4SLinus Torvalds if (copy > len) 20841da177e4SLinus Torvalds copy = len; 2085fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 20861a028e50SDavid S. Miller offset - start, 20871da177e4SLinus Torvalds to, copy, 0); 20881da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 20891da177e4SLinus Torvalds if ((len -= copy) == 0) 20901da177e4SLinus Torvalds return csum; 20911da177e4SLinus Torvalds offset += copy; 20921da177e4SLinus Torvalds to += copy; 20931da177e4SLinus Torvalds pos += copy; 20941da177e4SLinus Torvalds } 20951a028e50SDavid S. Miller start = end; 20961da177e4SLinus Torvalds } 209709a62660SKris Katterjohn BUG_ON(len); 20981da177e4SLinus Torvalds return csum; 20991da177e4SLinus Torvalds } 2100b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 21011da177e4SLinus Torvalds 2102af2806f8SThomas Graf /** 2103af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2104af2806f8SThomas Graf * @from: source buffer 2105af2806f8SThomas Graf * 2106af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2107af2806f8SThomas Graf * into skb_zerocopy(). 2108af2806f8SThomas Graf */ 2109af2806f8SThomas Graf unsigned int 2110af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2111af2806f8SThomas Graf { 2112af2806f8SThomas Graf unsigned int hlen = 0; 2113af2806f8SThomas Graf 2114af2806f8SThomas Graf if (!from->head_frag || 2115af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2116af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2117af2806f8SThomas Graf hlen = skb_headlen(from); 2118af2806f8SThomas Graf 2119af2806f8SThomas Graf if (skb_has_frag_list(from)) 2120af2806f8SThomas Graf hlen = from->len; 2121af2806f8SThomas Graf 2122af2806f8SThomas Graf return hlen; 2123af2806f8SThomas Graf } 2124af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2125af2806f8SThomas Graf 2126af2806f8SThomas Graf /** 2127af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2128af2806f8SThomas Graf * @to: destination buffer 21297fceb4deSMasanari Iida * @from: source buffer 2130af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2131af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2132af2806f8SThomas Graf * 2133af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2134af2806f8SThomas Graf * to the frags in the source buffer. 2135af2806f8SThomas Graf * 2136af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2137af2806f8SThomas Graf * headroom in the `to` buffer. 213836d5fe6aSZoltan Kiss * 213936d5fe6aSZoltan Kiss * Return value: 214036d5fe6aSZoltan Kiss * 0: everything is OK 214136d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 214236d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2143af2806f8SThomas Graf */ 214436d5fe6aSZoltan Kiss int 214536d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2146af2806f8SThomas Graf { 2147af2806f8SThomas Graf int i, j = 0; 2148af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 214936d5fe6aSZoltan Kiss int ret; 2150af2806f8SThomas Graf struct page *page; 2151af2806f8SThomas Graf unsigned int offset; 2152af2806f8SThomas Graf 2153af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2154af2806f8SThomas Graf 2155af2806f8SThomas Graf /* dont bother with small payloads */ 215636d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 215736d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2158af2806f8SThomas Graf 2159af2806f8SThomas Graf if (hlen) { 216036d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 216136d5fe6aSZoltan Kiss if (unlikely(ret)) 216236d5fe6aSZoltan Kiss return ret; 2163af2806f8SThomas Graf len -= hlen; 2164af2806f8SThomas Graf } else { 2165af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2166af2806f8SThomas Graf if (plen) { 2167af2806f8SThomas Graf page = virt_to_head_page(from->head); 2168af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2169af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2170af2806f8SThomas Graf get_page(page); 2171af2806f8SThomas Graf j = 1; 2172af2806f8SThomas Graf len -= plen; 2173af2806f8SThomas Graf } 2174af2806f8SThomas Graf } 2175af2806f8SThomas Graf 2176af2806f8SThomas Graf to->truesize += len + plen; 2177af2806f8SThomas Graf to->len += len + plen; 2178af2806f8SThomas Graf to->data_len += len + plen; 2179af2806f8SThomas Graf 218036d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 218136d5fe6aSZoltan Kiss skb_tx_error(from); 218236d5fe6aSZoltan Kiss return -ENOMEM; 218336d5fe6aSZoltan Kiss } 218436d5fe6aSZoltan Kiss 2185af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2186af2806f8SThomas Graf if (!len) 2187af2806f8SThomas Graf break; 2188af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2189af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2190af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2191af2806f8SThomas Graf skb_frag_ref(to, j); 2192af2806f8SThomas Graf j++; 2193af2806f8SThomas Graf } 2194af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 219536d5fe6aSZoltan Kiss 219636d5fe6aSZoltan Kiss return 0; 2197af2806f8SThomas Graf } 2198af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2199af2806f8SThomas Graf 22001da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 22011da177e4SLinus Torvalds { 2202d3bc23e7SAl Viro __wsum csum; 22031da177e4SLinus Torvalds long csstart; 22041da177e4SLinus Torvalds 220584fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 220655508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 22071da177e4SLinus Torvalds else 22081da177e4SLinus Torvalds csstart = skb_headlen(skb); 22091da177e4SLinus Torvalds 221009a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 22111da177e4SLinus Torvalds 2212d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds csum = 0; 22151da177e4SLinus Torvalds if (csstart != skb->len) 22161da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 22171da177e4SLinus Torvalds skb->len - csstart, 0); 22181da177e4SLinus Torvalds 221984fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2220ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 22211da177e4SLinus Torvalds 2222d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 22231da177e4SLinus Torvalds } 22241da177e4SLinus Torvalds } 2225b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds /** 22281da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 22291da177e4SLinus Torvalds * @list: list to dequeue from 22301da177e4SLinus Torvalds * 22311da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 22321da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 22331da177e4SLinus Torvalds * returned or %NULL if the list is empty. 22341da177e4SLinus Torvalds */ 22351da177e4SLinus Torvalds 22361da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 22371da177e4SLinus Torvalds { 22381da177e4SLinus Torvalds unsigned long flags; 22391da177e4SLinus Torvalds struct sk_buff *result; 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 22421da177e4SLinus Torvalds result = __skb_dequeue(list); 22431da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 22441da177e4SLinus Torvalds return result; 22451da177e4SLinus Torvalds } 2246b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds /** 22491da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 22501da177e4SLinus Torvalds * @list: list to dequeue from 22511da177e4SLinus Torvalds * 22521da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 22531da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 22541da177e4SLinus Torvalds * returned or %NULL if the list is empty. 22551da177e4SLinus Torvalds */ 22561da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 22571da177e4SLinus Torvalds { 22581da177e4SLinus Torvalds unsigned long flags; 22591da177e4SLinus Torvalds struct sk_buff *result; 22601da177e4SLinus Torvalds 22611da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 22621da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 22631da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 22641da177e4SLinus Torvalds return result; 22651da177e4SLinus Torvalds } 2266b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds /** 22691da177e4SLinus Torvalds * skb_queue_purge - empty a list 22701da177e4SLinus Torvalds * @list: list to empty 22711da177e4SLinus Torvalds * 22721da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 22731da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 22741da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 22751da177e4SLinus Torvalds */ 22761da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 22771da177e4SLinus Torvalds { 22781da177e4SLinus Torvalds struct sk_buff *skb; 22791da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 22801da177e4SLinus Torvalds kfree_skb(skb); 22811da177e4SLinus Torvalds } 2282b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /** 22851da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 22861da177e4SLinus Torvalds * @list: list to use 22871da177e4SLinus Torvalds * @newsk: buffer to queue 22881da177e4SLinus Torvalds * 22891da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 22901da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 22911da177e4SLinus Torvalds * safely. 22921da177e4SLinus Torvalds * 22931da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 22941da177e4SLinus Torvalds */ 22951da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 22961da177e4SLinus Torvalds { 22971da177e4SLinus Torvalds unsigned long flags; 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23001da177e4SLinus Torvalds __skb_queue_head(list, newsk); 23011da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23021da177e4SLinus Torvalds } 2303b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds /** 23061da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 23071da177e4SLinus Torvalds * @list: list to use 23081da177e4SLinus Torvalds * @newsk: buffer to queue 23091da177e4SLinus Torvalds * 23101da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 23111da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 23121da177e4SLinus Torvalds * safely. 23131da177e4SLinus Torvalds * 23141da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23151da177e4SLinus Torvalds */ 23161da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 23171da177e4SLinus Torvalds { 23181da177e4SLinus Torvalds unsigned long flags; 23191da177e4SLinus Torvalds 23201da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23211da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 23221da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23231da177e4SLinus Torvalds } 2324b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 23258728b834SDavid S. Miller 23261da177e4SLinus Torvalds /** 23271da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 23281da177e4SLinus Torvalds * @skb: buffer to remove 23298728b834SDavid S. Miller * @list: list to use 23301da177e4SLinus Torvalds * 23318728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 23328728b834SDavid S. Miller * function is atomic with respect to other list locked calls 23331da177e4SLinus Torvalds * 23348728b834SDavid S. Miller * You must know what list the SKB is on. 23351da177e4SLinus Torvalds */ 23368728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 23371da177e4SLinus Torvalds { 23381da177e4SLinus Torvalds unsigned long flags; 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23418728b834SDavid S. Miller __skb_unlink(skb, list); 23421da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23431da177e4SLinus Torvalds } 2344b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds /** 23471da177e4SLinus Torvalds * skb_append - append a buffer 23481da177e4SLinus Torvalds * @old: buffer to insert after 23491da177e4SLinus Torvalds * @newsk: buffer to insert 23508728b834SDavid S. Miller * @list: list to use 23511da177e4SLinus Torvalds * 23521da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 23531da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 23541da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23551da177e4SLinus Torvalds */ 23568728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 23571da177e4SLinus Torvalds { 23581da177e4SLinus Torvalds unsigned long flags; 23591da177e4SLinus Torvalds 23608728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 23617de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 23628728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 23631da177e4SLinus Torvalds } 2364b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds /** 23671da177e4SLinus Torvalds * skb_insert - insert a buffer 23681da177e4SLinus Torvalds * @old: buffer to insert before 23691da177e4SLinus Torvalds * @newsk: buffer to insert 23708728b834SDavid S. Miller * @list: list to use 23711da177e4SLinus Torvalds * 23728728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 23738728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 23748728b834SDavid S. Miller * calls. 23758728b834SDavid S. Miller * 23761da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23771da177e4SLinus Torvalds */ 23788728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 23791da177e4SLinus Torvalds { 23801da177e4SLinus Torvalds unsigned long flags; 23811da177e4SLinus Torvalds 23828728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 23838728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 23848728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 23851da177e4SLinus Torvalds } 2386b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 23891da177e4SLinus Torvalds struct sk_buff* skb1, 23901da177e4SLinus Torvalds const u32 len, const int pos) 23911da177e4SLinus Torvalds { 23921da177e4SLinus Torvalds int i; 23931da177e4SLinus Torvalds 2394d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2395d626f62bSArnaldo Carvalho de Melo pos - len); 23961da177e4SLinus Torvalds /* And move data appendix as is. */ 23971da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 23981da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 24011da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 24021da177e4SLinus Torvalds skb1->data_len = skb->data_len; 24031da177e4SLinus Torvalds skb1->len += skb1->data_len; 24041da177e4SLinus Torvalds skb->data_len = 0; 24051da177e4SLinus Torvalds skb->len = len; 240627a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 24101da177e4SLinus Torvalds struct sk_buff* skb1, 24111da177e4SLinus Torvalds const u32 len, int pos) 24121da177e4SLinus Torvalds { 24131da177e4SLinus Torvalds int i, k = 0; 24141da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 24171da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 24181da177e4SLinus Torvalds skb->len = len; 24191da177e4SLinus Torvalds skb->data_len = len - pos; 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 24229e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds if (pos + size > len) { 24251da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds if (pos < len) { 24281da177e4SLinus Torvalds /* Split frag. 24291da177e4SLinus Torvalds * We have two variants in this case: 24301da177e4SLinus Torvalds * 1. Move all the frag to the second 24311da177e4SLinus Torvalds * part, if it is possible. F.e. 24321da177e4SLinus Torvalds * this approach is mandatory for TUX, 24331da177e4SLinus Torvalds * where splitting is expensive. 24341da177e4SLinus Torvalds * 2. Split is accurately. We make this. 24351da177e4SLinus Torvalds */ 2436ea2ab693SIan Campbell skb_frag_ref(skb, i); 24371da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 24389e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 24399e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 24401da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 24411da177e4SLinus Torvalds } 24421da177e4SLinus Torvalds k++; 24431da177e4SLinus Torvalds } else 24441da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 24451da177e4SLinus Torvalds pos += size; 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 24481da177e4SLinus Torvalds } 24491da177e4SLinus Torvalds 24501da177e4SLinus Torvalds /** 24511da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 24521da177e4SLinus Torvalds * @skb: the buffer to split 24531da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 24541da177e4SLinus Torvalds * @len: new length for skb 24551da177e4SLinus Torvalds */ 24561da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 24571da177e4SLinus Torvalds { 24581da177e4SLinus Torvalds int pos = skb_headlen(skb); 24591da177e4SLinus Torvalds 246068534c68SAmerigo Wang skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 24611da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 24621da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 24631da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 24641da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 24651da177e4SLinus Torvalds } 2466b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 24671da177e4SLinus Torvalds 24689f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 24699f782db3SIlpo Järvinen * 24709f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 24719f782db3SIlpo Järvinen */ 2472832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2473832d11c5SIlpo Järvinen { 24740ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2475832d11c5SIlpo Järvinen } 2476832d11c5SIlpo Järvinen 2477832d11c5SIlpo Järvinen /** 2478832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2479832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2480832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2481832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2482832d11c5SIlpo Järvinen * 2483832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 248420e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2485832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2486832d11c5SIlpo Järvinen * 2487832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2488832d11c5SIlpo Järvinen * 2489832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2490832d11c5SIlpo Järvinen * to have non-paged data as well. 2491832d11c5SIlpo Järvinen * 2492832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2493832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2494832d11c5SIlpo Järvinen */ 2495832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2496832d11c5SIlpo Järvinen { 2497832d11c5SIlpo Järvinen int from, to, merge, todo; 2498832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2499832d11c5SIlpo Järvinen 2500832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2501832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2502832d11c5SIlpo Järvinen 2503832d11c5SIlpo Järvinen todo = shiftlen; 2504832d11c5SIlpo Järvinen from = 0; 2505832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2506832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2507832d11c5SIlpo Järvinen 2508832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2509832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2510832d11c5SIlpo Järvinen */ 2511832d11c5SIlpo Järvinen if (!to || 2512ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2513ea2ab693SIan Campbell fragfrom->page_offset)) { 2514832d11c5SIlpo Järvinen merge = -1; 2515832d11c5SIlpo Järvinen } else { 2516832d11c5SIlpo Järvinen merge = to - 1; 2517832d11c5SIlpo Järvinen 25189e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2519832d11c5SIlpo Järvinen if (todo < 0) { 2520832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2521832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2522832d11c5SIlpo Järvinen return 0; 2523832d11c5SIlpo Järvinen 25249f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 25259f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2526832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2527832d11c5SIlpo Järvinen 25289e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 25299e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2530832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2531832d11c5SIlpo Järvinen 2532832d11c5SIlpo Järvinen goto onlymerged; 2533832d11c5SIlpo Järvinen } 2534832d11c5SIlpo Järvinen 2535832d11c5SIlpo Järvinen from++; 2536832d11c5SIlpo Järvinen } 2537832d11c5SIlpo Järvinen 2538832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2539832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2540832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2541832d11c5SIlpo Järvinen return 0; 2542832d11c5SIlpo Järvinen 2543832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2544832d11c5SIlpo Järvinen return 0; 2545832d11c5SIlpo Järvinen 2546832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2547832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2548832d11c5SIlpo Järvinen return 0; 2549832d11c5SIlpo Järvinen 2550832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2551832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2552832d11c5SIlpo Järvinen 25539e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2554832d11c5SIlpo Järvinen *fragto = *fragfrom; 25559e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2556832d11c5SIlpo Järvinen from++; 2557832d11c5SIlpo Järvinen to++; 2558832d11c5SIlpo Järvinen 2559832d11c5SIlpo Järvinen } else { 2560ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2561832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2562832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 25639e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2564832d11c5SIlpo Järvinen 2565832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 25669e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2567832d11c5SIlpo Järvinen todo = 0; 2568832d11c5SIlpo Järvinen 2569832d11c5SIlpo Järvinen to++; 2570832d11c5SIlpo Järvinen break; 2571832d11c5SIlpo Järvinen } 2572832d11c5SIlpo Järvinen } 2573832d11c5SIlpo Järvinen 2574832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2575832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2576832d11c5SIlpo Järvinen 2577832d11c5SIlpo Järvinen if (merge >= 0) { 2578832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2579832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2580832d11c5SIlpo Järvinen 25819e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2582ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2583832d11c5SIlpo Järvinen } 2584832d11c5SIlpo Järvinen 2585832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2586832d11c5SIlpo Järvinen to = 0; 2587832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2588832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2589832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2590832d11c5SIlpo Järvinen 2591832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2592832d11c5SIlpo Järvinen 2593832d11c5SIlpo Järvinen onlymerged: 2594832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2595832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2596832d11c5SIlpo Järvinen */ 2597832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2598832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2599832d11c5SIlpo Järvinen 2600832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2601832d11c5SIlpo Järvinen skb->len -= shiftlen; 2602832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2603832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2604832d11c5SIlpo Järvinen tgt->len += shiftlen; 2605832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2606832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2607832d11c5SIlpo Järvinen 2608832d11c5SIlpo Järvinen return shiftlen; 2609832d11c5SIlpo Järvinen } 2610832d11c5SIlpo Järvinen 2611677e90edSThomas Graf /** 2612677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2613677e90edSThomas Graf * @skb: the buffer to read 2614677e90edSThomas Graf * @from: lower offset of data to be read 2615677e90edSThomas Graf * @to: upper offset of data to be read 2616677e90edSThomas Graf * @st: state variable 2617677e90edSThomas Graf * 2618677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2619677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2620677e90edSThomas Graf */ 2621677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2622677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2623677e90edSThomas Graf { 2624677e90edSThomas Graf st->lower_offset = from; 2625677e90edSThomas Graf st->upper_offset = to; 2626677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2627677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2628677e90edSThomas Graf st->frag_data = NULL; 2629677e90edSThomas Graf } 2630b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2631677e90edSThomas Graf 2632677e90edSThomas Graf /** 2633677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2634677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2635677e90edSThomas Graf * @data: destination pointer for data to be returned 2636677e90edSThomas Graf * @st: state variable 2637677e90edSThomas Graf * 2638bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 2639677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2640bc32383cSMathias Krause * the head of the data block to @data and returns the length 2641677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2642677e90edSThomas Graf * offset has been reached. 2643677e90edSThomas Graf * 2644677e90edSThomas Graf * The caller is not required to consume all of the data 2645bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 2646677e90edSThomas Graf * of bytes already consumed and the next call to 2647677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2648677e90edSThomas Graf * 264925985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2650e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 2651677e90edSThomas Graf * reads of potentially non linear data. 2652677e90edSThomas Graf * 2653bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2654677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2655677e90edSThomas Graf * a stack for this purpose. 2656677e90edSThomas Graf */ 2657677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2658677e90edSThomas Graf struct skb_seq_state *st) 2659677e90edSThomas Graf { 2660677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2661677e90edSThomas Graf skb_frag_t *frag; 2662677e90edSThomas Graf 2663aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 2664aeb193eaSWedson Almeida Filho if (st->frag_data) { 2665aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 2666aeb193eaSWedson Almeida Filho st->frag_data = NULL; 2667aeb193eaSWedson Almeida Filho } 2668677e90edSThomas Graf return 0; 2669aeb193eaSWedson Almeida Filho } 2670677e90edSThomas Graf 2671677e90edSThomas Graf next_skb: 267295e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2673677e90edSThomas Graf 2674995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 267595e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2676677e90edSThomas Graf return block_limit - abs_offset; 2677677e90edSThomas Graf } 2678677e90edSThomas Graf 2679677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2680677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2681677e90edSThomas Graf 2682677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2683677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 26849e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2685677e90edSThomas Graf 2686677e90edSThomas Graf if (abs_offset < block_limit) { 2687677e90edSThomas Graf if (!st->frag_data) 268851c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2689677e90edSThomas Graf 2690677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2691677e90edSThomas Graf (abs_offset - st->stepped_offset); 2692677e90edSThomas Graf 2693677e90edSThomas Graf return block_limit - abs_offset; 2694677e90edSThomas Graf } 2695677e90edSThomas Graf 2696677e90edSThomas Graf if (st->frag_data) { 269751c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2698677e90edSThomas Graf st->frag_data = NULL; 2699677e90edSThomas Graf } 2700677e90edSThomas Graf 2701677e90edSThomas Graf st->frag_idx++; 27029e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2703677e90edSThomas Graf } 2704677e90edSThomas Graf 27055b5a60daSOlaf Kirch if (st->frag_data) { 270651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 27075b5a60daSOlaf Kirch st->frag_data = NULL; 27085b5a60daSOlaf Kirch } 27095b5a60daSOlaf Kirch 271021dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2711677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 271295e3b24cSHerbert Xu st->frag_idx = 0; 2713677e90edSThomas Graf goto next_skb; 271471b3346dSShyam Iyer } else if (st->cur_skb->next) { 271571b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 271671b3346dSShyam Iyer st->frag_idx = 0; 2717677e90edSThomas Graf goto next_skb; 2718677e90edSThomas Graf } 2719677e90edSThomas Graf 2720677e90edSThomas Graf return 0; 2721677e90edSThomas Graf } 2722b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2723677e90edSThomas Graf 2724677e90edSThomas Graf /** 2725677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2726677e90edSThomas Graf * @st: state variable 2727677e90edSThomas Graf * 2728677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2729677e90edSThomas Graf * returned 0. 2730677e90edSThomas Graf */ 2731677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2732677e90edSThomas Graf { 2733677e90edSThomas Graf if (st->frag_data) 273451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2735677e90edSThomas Graf } 2736b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2737677e90edSThomas Graf 27383fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 27393fc7e8a6SThomas Graf 27403fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 27413fc7e8a6SThomas Graf struct ts_config *conf, 27423fc7e8a6SThomas Graf struct ts_state *state) 27433fc7e8a6SThomas Graf { 27443fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 27453fc7e8a6SThomas Graf } 27463fc7e8a6SThomas Graf 27473fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 27483fc7e8a6SThomas Graf { 27493fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 27503fc7e8a6SThomas Graf } 27513fc7e8a6SThomas Graf 27523fc7e8a6SThomas Graf /** 27533fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 27543fc7e8a6SThomas Graf * @skb: the buffer to look in 27553fc7e8a6SThomas Graf * @from: search offset 27563fc7e8a6SThomas Graf * @to: search limit 27573fc7e8a6SThomas Graf * @config: textsearch configuration 27583fc7e8a6SThomas Graf * @state: uninitialized textsearch state variable 27593fc7e8a6SThomas Graf * 27603fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 27613fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 27623fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 27633fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 27643fc7e8a6SThomas Graf */ 27653fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 27663fc7e8a6SThomas Graf unsigned int to, struct ts_config *config, 27673fc7e8a6SThomas Graf struct ts_state *state) 27683fc7e8a6SThomas Graf { 2769f72b948dSPhil Oester unsigned int ret; 2770f72b948dSPhil Oester 27713fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 27723fc7e8a6SThomas Graf config->finish = skb_ts_finish; 27733fc7e8a6SThomas Graf 27743fc7e8a6SThomas Graf skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 27753fc7e8a6SThomas Graf 2776f72b948dSPhil Oester ret = textsearch_find(config, state); 2777f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 27783fc7e8a6SThomas Graf } 2779b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 27803fc7e8a6SThomas Graf 2781e89e9cf5SAnanda Raju /** 27822c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2783e89e9cf5SAnanda Raju * @sk: sock structure 2784e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 2785e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2786e89e9cf5SAnanda Raju * @from: pointer to user message iov 2787e89e9cf5SAnanda Raju * @length: length of the iov message 2788e89e9cf5SAnanda Raju * 2789e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2790e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2791e89e9cf5SAnanda Raju */ 2792e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2793dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2794e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2795e89e9cf5SAnanda Raju void *from, int length) 2796e89e9cf5SAnanda Raju { 2797b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 2798b2111724SEric Dumazet int copy; 2799e89e9cf5SAnanda Raju int offset = 0; 2800e89e9cf5SAnanda Raju int ret; 2801b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 2802e89e9cf5SAnanda Raju 2803e89e9cf5SAnanda Raju do { 2804e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2805e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2806b2111724SEric Dumazet return -EMSGSIZE; 2807e89e9cf5SAnanda Raju 2808b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 2809e89e9cf5SAnanda Raju return -ENOMEM; 2810e89e9cf5SAnanda Raju 2811e89e9cf5SAnanda Raju /* copy the user data to page */ 2812b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 2813e89e9cf5SAnanda Raju 2814b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 2815e89e9cf5SAnanda Raju offset, copy, 0, skb); 2816e89e9cf5SAnanda Raju if (ret < 0) 2817e89e9cf5SAnanda Raju return -EFAULT; 2818e89e9cf5SAnanda Raju 2819e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2820b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 2821b2111724SEric Dumazet copy); 2822b2111724SEric Dumazet frg_cnt++; 2823b2111724SEric Dumazet pfrag->offset += copy; 2824b2111724SEric Dumazet get_page(pfrag->page); 2825b2111724SEric Dumazet 2826b2111724SEric Dumazet skb->truesize += copy; 2827b2111724SEric Dumazet atomic_add(copy, &sk->sk_wmem_alloc); 2828e89e9cf5SAnanda Raju skb->len += copy; 2829e89e9cf5SAnanda Raju skb->data_len += copy; 2830e89e9cf5SAnanda Raju offset += copy; 2831e89e9cf5SAnanda Raju length -= copy; 2832e89e9cf5SAnanda Raju 2833e89e9cf5SAnanda Raju } while (length > 0); 2834e89e9cf5SAnanda Raju 2835e89e9cf5SAnanda Raju return 0; 2836e89e9cf5SAnanda Raju } 2837b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2838e89e9cf5SAnanda Raju 2839cbb042f9SHerbert Xu /** 2840cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2841cbb042f9SHerbert Xu * @skb: buffer to update 2842cbb042f9SHerbert Xu * @len: length of data pulled 2843cbb042f9SHerbert Xu * 2844cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2845fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 284684fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 284784fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 284884fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2849cbb042f9SHerbert Xu */ 2850cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2851cbb042f9SHerbert Xu { 2852cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2853cbb042f9SHerbert Xu skb->len -= len; 2854cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2855cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2856cbb042f9SHerbert Xu return skb->data += len; 2857cbb042f9SHerbert Xu } 2858f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2859f94691acSArnaldo Carvalho de Melo 2860f4c50d99SHerbert Xu /** 2861f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2862df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 2863576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2864f4c50d99SHerbert Xu * 2865f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 28664c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 28674c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2868f4c50d99SHerbert Xu */ 2869df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 2870df5771ffSMichael S. Tsirkin netdev_features_t features) 2871f4c50d99SHerbert Xu { 2872f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2873f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 28741a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 2875df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 2876df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 2877df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 28781fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 2879f4c50d99SHerbert Xu unsigned int offset = doffset; 2880df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 2881f4c50d99SHerbert Xu unsigned int headroom; 2882f4c50d99SHerbert Xu unsigned int len; 2883ec5f0615SPravin B Shelar __be16 proto; 2884ec5f0615SPravin B Shelar bool csum; 288504ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 2886df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 2887f4c50d99SHerbert Xu int err = -ENOMEM; 2888f4c50d99SHerbert Xu int i = 0; 2889f4c50d99SHerbert Xu int pos; 289053d6471cSVlad Yasevich int dummy; 2891f4c50d99SHerbert Xu 28925882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 289353d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 2894ec5f0615SPravin B Shelar if (unlikely(!proto)) 2895ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 2896ec5f0615SPravin B Shelar 28977e2b10c1STom Herbert csum = !head_skb->encap_hdr_csum && 28987e2b10c1STom Herbert !!can_checksum_protocol(features, proto); 28997e2b10c1STom Herbert 2900df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 2901df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 2902f4c50d99SHerbert Xu 2903f4c50d99SHerbert Xu do { 2904f4c50d99SHerbert Xu struct sk_buff *nskb; 29058cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 2906c8884eddSHerbert Xu int hsize; 2907f4c50d99SHerbert Xu int size; 2908f4c50d99SHerbert Xu 2909df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 2910f4c50d99SHerbert Xu if (len > mss) 2911f4c50d99SHerbert Xu len = mss; 2912f4c50d99SHerbert Xu 2913df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 2914f4c50d99SHerbert Xu if (hsize < 0) 2915f4c50d99SHerbert Xu hsize = 0; 2916c8884eddSHerbert Xu if (hsize > len || !sg) 2917c8884eddSHerbert Xu hsize = len; 2918f4c50d99SHerbert Xu 29191a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 29201a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 29211a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 292289319d38SHerbert Xu 29239d8506ccSHerbert Xu i = 0; 29241a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 29251a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 29261fd819ecSMichael S. Tsirkin frag_skb = list_skb; 29271a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 29289d8506ccSHerbert Xu 29299d8506ccSHerbert Xu while (pos < offset + len) { 29309d8506ccSHerbert Xu BUG_ON(i >= nfrags); 29319d8506ccSHerbert Xu 29324e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 29339d8506ccSHerbert Xu if (pos + size > offset + len) 29349d8506ccSHerbert Xu break; 29359d8506ccSHerbert Xu 29369d8506ccSHerbert Xu i++; 29379d8506ccSHerbert Xu pos += size; 29384e1beba1SMichael S. Tsirkin frag++; 29399d8506ccSHerbert Xu } 29409d8506ccSHerbert Xu 29411a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 29421a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 294389319d38SHerbert Xu 2944f4c50d99SHerbert Xu if (unlikely(!nskb)) 2945f4c50d99SHerbert Xu goto err; 2946f4c50d99SHerbert Xu 29479d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 29489d8506ccSHerbert Xu kfree_skb(nskb); 29499d8506ccSHerbert Xu goto err; 29509d8506ccSHerbert Xu } 29519d8506ccSHerbert Xu 2952ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 295389319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 295489319d38SHerbert Xu kfree_skb(nskb); 295589319d38SHerbert Xu goto err; 295689319d38SHerbert Xu } 295789319d38SHerbert Xu 2958ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 295989319d38SHerbert Xu skb_release_head_state(nskb); 296089319d38SHerbert Xu __skb_push(nskb, doffset); 296189319d38SHerbert Xu } else { 2962c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 2963df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 2964c93bdd0eSMel Gorman NUMA_NO_NODE); 296589319d38SHerbert Xu 296689319d38SHerbert Xu if (unlikely(!nskb)) 296789319d38SHerbert Xu goto err; 296889319d38SHerbert Xu 296989319d38SHerbert Xu skb_reserve(nskb, headroom); 297089319d38SHerbert Xu __skb_put(nskb, doffset); 297189319d38SHerbert Xu } 297289319d38SHerbert Xu 2973f4c50d99SHerbert Xu if (segs) 2974f4c50d99SHerbert Xu tail->next = nskb; 2975f4c50d99SHerbert Xu else 2976f4c50d99SHerbert Xu segs = nskb; 2977f4c50d99SHerbert Xu tail = nskb; 2978f4c50d99SHerbert Xu 2979df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 2980f4c50d99SHerbert Xu 2981030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 2982fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 298368c33163SPravin B Shelar 2984df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 298568c33163SPravin B Shelar nskb->data - tnl_hlen, 298668c33163SPravin B Shelar doffset + tnl_hlen); 298789319d38SHerbert Xu 29889d8506ccSHerbert Xu if (nskb->len == len + doffset) 29891cdbcb79SSimon Horman goto perform_csum_check; 299089319d38SHerbert Xu 2991f4c50d99SHerbert Xu if (!sg) { 29926f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 2993df5771ffSMichael S. Tsirkin nskb->csum = skb_copy_and_csum_bits(head_skb, offset, 2994f4c50d99SHerbert Xu skb_put(nskb, len), 2995f4c50d99SHerbert Xu len, 0); 29967e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 2997de843723STom Herbert skb_headroom(nskb) + doffset; 2998f4c50d99SHerbert Xu continue; 2999f4c50d99SHerbert Xu } 3000f4c50d99SHerbert Xu 30018cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3002f4c50d99SHerbert Xu 3003df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3004d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3005f4c50d99SHerbert Xu 3006df5771ffSMichael S. Tsirkin skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & 3007df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3008cef401deSEric Dumazet 30099d8506ccSHerbert Xu while (pos < offset + len) { 30109d8506ccSHerbert Xu if (i >= nfrags) { 30111a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 30129d8506ccSHerbert Xu 30139d8506ccSHerbert Xu i = 0; 30141a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 30151a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 30161fd819ecSMichael S. Tsirkin frag_skb = list_skb; 30179d8506ccSHerbert Xu 30189d8506ccSHerbert Xu BUG_ON(!nfrags); 30199d8506ccSHerbert Xu 30201a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 30219d8506ccSHerbert Xu } 30229d8506ccSHerbert Xu 30239d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 30249d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 30259d8506ccSHerbert Xu net_warn_ratelimited( 30269d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 30279d8506ccSHerbert Xu pos, mss); 30289d8506ccSHerbert Xu goto err; 30299d8506ccSHerbert Xu } 30309d8506ccSHerbert Xu 30311fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 30321fd819ecSMichael S. Tsirkin goto err; 30331fd819ecSMichael S. Tsirkin 30344e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 30358cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 30368cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3037f4c50d99SHerbert Xu 3038f4c50d99SHerbert Xu if (pos < offset) { 30398cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 30408cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3041f4c50d99SHerbert Xu } 3042f4c50d99SHerbert Xu 304389319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3044f4c50d99SHerbert Xu 3045f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3046f4c50d99SHerbert Xu i++; 30474e1beba1SMichael S. Tsirkin frag++; 3048f4c50d99SHerbert Xu pos += size; 3049f4c50d99SHerbert Xu } else { 30508cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 305189319d38SHerbert Xu goto skip_fraglist; 3052f4c50d99SHerbert Xu } 3053f4c50d99SHerbert Xu 30548cb19905SMichael S. Tsirkin nskb_frag++; 3055f4c50d99SHerbert Xu } 3056f4c50d99SHerbert Xu 305789319d38SHerbert Xu skip_fraglist: 3058f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3059f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3060f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3061ec5f0615SPravin B Shelar 30621cdbcb79SSimon Horman perform_csum_check: 3063ec5f0615SPravin B Shelar if (!csum) { 3064ec5f0615SPravin B Shelar nskb->csum = skb_checksum(nskb, doffset, 3065ec5f0615SPravin B Shelar nskb->len - doffset, 0); 3066ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 30677e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 30687e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3069ec5f0615SPravin B Shelar } 3070df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3071f4c50d99SHerbert Xu 3072f4c50d99SHerbert Xu return segs; 3073f4c50d99SHerbert Xu 3074f4c50d99SHerbert Xu err: 3075289dccbeSEric Dumazet kfree_skb_list(segs); 3076f4c50d99SHerbert Xu return ERR_PTR(err); 3077f4c50d99SHerbert Xu } 3078f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3079f4c50d99SHerbert Xu 308071d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 308171d93b39SHerbert Xu { 30828a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 308367147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 308467147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 30858a29111cSEric Dumazet struct sk_buff *nskb, *lp, *p = *head; 30868a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 3087715dc1f3SEric Dumazet unsigned int delta_truesize; 30888a29111cSEric Dumazet unsigned int headroom; 308971d93b39SHerbert Xu 30908a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 309171d93b39SHerbert Xu return -E2BIG; 309271d93b39SHerbert Xu 309329e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 30948a29111cSEric Dumazet pinfo = skb_shinfo(lp); 30958a29111cSEric Dumazet 30968a29111cSEric Dumazet if (headlen <= offset) { 309742da6994SHerbert Xu skb_frag_t *frag; 309866e92fcfSHerbert Xu skb_frag_t *frag2; 30999aaa156cSHerbert Xu int i = skbinfo->nr_frags; 31009aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 310142da6994SHerbert Xu 310266e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 31038a29111cSEric Dumazet goto merge; 310481705ad1SHerbert Xu 31058a29111cSEric Dumazet offset -= headlen; 31069aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 31079aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3108f5572068SHerbert Xu 31099aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 31109aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 311166e92fcfSHerbert Xu do { 311266e92fcfSHerbert Xu *--frag = *--frag2; 311366e92fcfSHerbert Xu } while (--i); 311466e92fcfSHerbert Xu 311566e92fcfSHerbert Xu frag->page_offset += offset; 31169e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 311766e92fcfSHerbert Xu 3118715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3119ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3120ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3121715dc1f3SEric Dumazet 3122f5572068SHerbert Xu skb->truesize -= skb->data_len; 3123f5572068SHerbert Xu skb->len -= skb->data_len; 3124f5572068SHerbert Xu skb->data_len = 0; 3125f5572068SHerbert Xu 3126715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 31275d38a079SHerbert Xu goto done; 3128d7e8883cSEric Dumazet } else if (skb->head_frag) { 3129d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3130d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3131d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3132d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3133d7e8883cSEric Dumazet unsigned int first_offset; 3134d7e8883cSEric Dumazet 3135d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 31368a29111cSEric Dumazet goto merge; 3137d7e8883cSEric Dumazet 3138d7e8883cSEric Dumazet first_offset = skb->data - 3139d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3140d7e8883cSEric Dumazet offset; 3141d7e8883cSEric Dumazet 3142d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3143d7e8883cSEric Dumazet 3144d7e8883cSEric Dumazet frag->page.p = page; 3145d7e8883cSEric Dumazet frag->page_offset = first_offset; 3146d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3147d7e8883cSEric Dumazet 3148d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3149d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3150d7e8883cSEric Dumazet 3151715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3152d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3153d7e8883cSEric Dumazet goto done; 31548a29111cSEric Dumazet } 31558a29111cSEric Dumazet if (pinfo->frag_list) 31568a29111cSEric Dumazet goto merge; 31578a29111cSEric Dumazet if (skb_gro_len(p) != pinfo->gso_size) 315869c0cab1SHerbert Xu return -E2BIG; 315971d93b39SHerbert Xu 316071d93b39SHerbert Xu headroom = skb_headroom(p); 31613d3be433SEric Dumazet nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC); 316271d93b39SHerbert Xu if (unlikely(!nskb)) 316371d93b39SHerbert Xu return -ENOMEM; 316471d93b39SHerbert Xu 316571d93b39SHerbert Xu __copy_skb_header(nskb, p); 316671d93b39SHerbert Xu nskb->mac_len = p->mac_len; 316771d93b39SHerbert Xu 316871d93b39SHerbert Xu skb_reserve(nskb, headroom); 316986911732SHerbert Xu __skb_put(nskb, skb_gro_offset(p)); 317071d93b39SHerbert Xu 317186911732SHerbert Xu skb_set_mac_header(nskb, skb_mac_header(p) - p->data); 317271d93b39SHerbert Xu skb_set_network_header(nskb, skb_network_offset(p)); 317371d93b39SHerbert Xu skb_set_transport_header(nskb, skb_transport_offset(p)); 317471d93b39SHerbert Xu 317586911732SHerbert Xu __skb_pull(p, skb_gro_offset(p)); 317686911732SHerbert Xu memcpy(skb_mac_header(nskb), skb_mac_header(p), 317786911732SHerbert Xu p->data - skb_mac_header(p)); 317871d93b39SHerbert Xu 317971d93b39SHerbert Xu skb_shinfo(nskb)->frag_list = p; 31809aaa156cSHerbert Xu skb_shinfo(nskb)->gso_size = pinfo->gso_size; 3181622e0ca1SHerbert Xu pinfo->gso_size = 0; 318271d93b39SHerbert Xu skb_header_release(p); 3183c3c7c254SEric Dumazet NAPI_GRO_CB(nskb)->last = p; 318471d93b39SHerbert Xu 318571d93b39SHerbert Xu nskb->data_len += p->len; 3186de8261c2SEric Dumazet nskb->truesize += p->truesize; 318771d93b39SHerbert Xu nskb->len += p->len; 318871d93b39SHerbert Xu 318971d93b39SHerbert Xu *head = nskb; 319071d93b39SHerbert Xu nskb->next = p->next; 319171d93b39SHerbert Xu p->next = NULL; 319271d93b39SHerbert Xu 319371d93b39SHerbert Xu p = nskb; 319471d93b39SHerbert Xu 319571d93b39SHerbert Xu merge: 3196715dc1f3SEric Dumazet delta_truesize = skb->truesize; 319767147ba9SHerbert Xu if (offset > headlen) { 3198d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3199d1dc7abfSMichal Schmidt 3200d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 32019e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3202d1dc7abfSMichal Schmidt skb->data_len -= eat; 3203d1dc7abfSMichal Schmidt skb->len -= eat; 320467147ba9SHerbert Xu offset = headlen; 320556035022SHerbert Xu } 320656035022SHerbert Xu 320767147ba9SHerbert Xu __skb_pull(skb, offset); 320856035022SHerbert Xu 320929e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 32108a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 32118a29111cSEric Dumazet else 3212c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3213c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 321471d93b39SHerbert Xu skb_header_release(skb); 32158a29111cSEric Dumazet lp = p; 321671d93b39SHerbert Xu 32175d38a079SHerbert Xu done: 32185d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 321937fe4732SHerbert Xu p->data_len += len; 3220715dc1f3SEric Dumazet p->truesize += delta_truesize; 322137fe4732SHerbert Xu p->len += len; 32228a29111cSEric Dumazet if (lp != p) { 32238a29111cSEric Dumazet lp->data_len += len; 32248a29111cSEric Dumazet lp->truesize += delta_truesize; 32258a29111cSEric Dumazet lp->len += len; 32268a29111cSEric Dumazet } 322771d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 322871d93b39SHerbert Xu return 0; 322971d93b39SHerbert Xu } 323071d93b39SHerbert Xu EXPORT_SYMBOL_GPL(skb_gro_receive); 323171d93b39SHerbert Xu 32321da177e4SLinus Torvalds void __init skb_init(void) 32331da177e4SLinus Torvalds { 32341da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 32351da177e4SLinus Torvalds sizeof(struct sk_buff), 32361da177e4SLinus Torvalds 0, 3237e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 323820c2df83SPaul Mundt NULL); 3239d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3240d179cd12SDavid S. Miller (2*sizeof(struct sk_buff)) + 3241d179cd12SDavid S. Miller sizeof(atomic_t), 3242d179cd12SDavid S. Miller 0, 3243e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 324420c2df83SPaul Mundt NULL); 32451da177e4SLinus Torvalds } 32461da177e4SLinus Torvalds 3247716ea3a7SDavid Howells /** 3248716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3249716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3250716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3251716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3252716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3253716ea3a7SDavid Howells * 3254716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3255716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3256716ea3a7SDavid Howells */ 325751c739d1SDavid S. Miller static int 325851c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3259716ea3a7SDavid Howells { 32601a028e50SDavid S. Miller int start = skb_headlen(skb); 32611a028e50SDavid S. Miller int i, copy = start - offset; 3262fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3263716ea3a7SDavid Howells int elt = 0; 3264716ea3a7SDavid Howells 3265716ea3a7SDavid Howells if (copy > 0) { 3266716ea3a7SDavid Howells if (copy > len) 3267716ea3a7SDavid Howells copy = len; 3268642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3269716ea3a7SDavid Howells elt++; 3270716ea3a7SDavid Howells if ((len -= copy) == 0) 3271716ea3a7SDavid Howells return elt; 3272716ea3a7SDavid Howells offset += copy; 3273716ea3a7SDavid Howells } 3274716ea3a7SDavid Howells 3275716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 32761a028e50SDavid S. Miller int end; 3277716ea3a7SDavid Howells 3278547b792cSIlpo Järvinen WARN_ON(start > offset + len); 32791a028e50SDavid S. Miller 32809e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3281716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3282716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3283716ea3a7SDavid Howells 3284716ea3a7SDavid Howells if (copy > len) 3285716ea3a7SDavid Howells copy = len; 3286ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3287642f1490SJens Axboe frag->page_offset+offset-start); 3288716ea3a7SDavid Howells elt++; 3289716ea3a7SDavid Howells if (!(len -= copy)) 3290716ea3a7SDavid Howells return elt; 3291716ea3a7SDavid Howells offset += copy; 3292716ea3a7SDavid Howells } 32931a028e50SDavid S. Miller start = end; 3294716ea3a7SDavid Howells } 3295716ea3a7SDavid Howells 3296fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 32971a028e50SDavid S. Miller int end; 3298716ea3a7SDavid Howells 3299547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33001a028e50SDavid S. Miller 3301fbb398a8SDavid S. Miller end = start + frag_iter->len; 3302716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3303716ea3a7SDavid Howells if (copy > len) 3304716ea3a7SDavid Howells copy = len; 3305fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 330651c739d1SDavid S. Miller copy); 3307716ea3a7SDavid Howells if ((len -= copy) == 0) 3308716ea3a7SDavid Howells return elt; 3309716ea3a7SDavid Howells offset += copy; 3310716ea3a7SDavid Howells } 33111a028e50SDavid S. Miller start = end; 3312716ea3a7SDavid Howells } 3313716ea3a7SDavid Howells BUG_ON(len); 3314716ea3a7SDavid Howells return elt; 3315716ea3a7SDavid Howells } 3316716ea3a7SDavid Howells 331725a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 331825a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 331925a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 332025a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 332125a91d8dSFan Du * 332225a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 332325a91d8dSFan Du * 1. sg_init_table 332425a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 332525a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 332625a91d8dSFan Du * 332725a91d8dSFan Du * This is equivalent to: 332825a91d8dSFan Du * 1. sg_init_table 332925a91d8dSFan Du * 2. skb_to_sgvec(payload1) 333025a91d8dSFan Du * 3. sg_unmark_end 333125a91d8dSFan Du * 4. skb_to_sgvec(payload2) 333225a91d8dSFan Du * 333325a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 333425a91d8dSFan Du * is more preferable. 333525a91d8dSFan Du */ 333625a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 333725a91d8dSFan Du int offset, int len) 333825a91d8dSFan Du { 333925a91d8dSFan Du return __skb_to_sgvec(skb, sg, offset, len); 334025a91d8dSFan Du } 334125a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 334225a91d8dSFan Du 334351c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 334451c739d1SDavid S. Miller { 334551c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 334651c739d1SDavid S. Miller 3347c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 334851c739d1SDavid S. Miller 334951c739d1SDavid S. Miller return nsg; 335051c739d1SDavid S. Miller } 3351b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 335251c739d1SDavid S. Miller 3353716ea3a7SDavid Howells /** 3354716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3355716ea3a7SDavid Howells * @skb: The socket buffer to check. 3356716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3357716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3358716ea3a7SDavid Howells * 3359716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3360716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3361716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3362716ea3a7SDavid Howells * 3363716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3364716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3365716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3366716ea3a7SDavid Howells * 3367716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3368716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3369716ea3a7SDavid Howells */ 3370716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3371716ea3a7SDavid Howells { 3372716ea3a7SDavid Howells int copyflag; 3373716ea3a7SDavid Howells int elt; 3374716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3375716ea3a7SDavid Howells 3376716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3377716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3378716ea3a7SDavid Howells * at the moment even if they are anonymous). 3379716ea3a7SDavid Howells */ 3380716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3381716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3382716ea3a7SDavid Howells return -ENOMEM; 3383716ea3a7SDavid Howells 3384716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 338521dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3386716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3387716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3388716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3389716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3390716ea3a7SDavid Howells 3391716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3392716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3393716ea3a7SDavid Howells return -ENOMEM; 3394716ea3a7SDavid Howells 3395716ea3a7SDavid Howells /* Voila! */ 3396716ea3a7SDavid Howells *trailer = skb; 3397716ea3a7SDavid Howells return 1; 3398716ea3a7SDavid Howells } 3399716ea3a7SDavid Howells 3400716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3401716ea3a7SDavid Howells 3402716ea3a7SDavid Howells elt = 1; 3403716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3404716ea3a7SDavid Howells copyflag = 0; 3405716ea3a7SDavid Howells 3406716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3407716ea3a7SDavid Howells int ntail = 0; 3408716ea3a7SDavid Howells 3409716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3410716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3411716ea3a7SDavid Howells * after it. */ 3412716ea3a7SDavid Howells 3413716ea3a7SDavid Howells if (skb_shared(skb1)) 3414716ea3a7SDavid Howells copyflag = 1; 3415716ea3a7SDavid Howells 3416716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3417716ea3a7SDavid Howells 3418716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3419716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 342021dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3421716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3422716ea3a7SDavid Howells ntail = tailbits + 128; 3423716ea3a7SDavid Howells } 3424716ea3a7SDavid Howells 3425716ea3a7SDavid Howells if (copyflag || 3426716ea3a7SDavid Howells skb_cloned(skb1) || 3427716ea3a7SDavid Howells ntail || 3428716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 342921dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3430716ea3a7SDavid Howells struct sk_buff *skb2; 3431716ea3a7SDavid Howells 3432716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3433716ea3a7SDavid Howells if (ntail == 0) 3434716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3435716ea3a7SDavid Howells else 3436716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3437716ea3a7SDavid Howells skb_headroom(skb1), 3438716ea3a7SDavid Howells ntail, 3439716ea3a7SDavid Howells GFP_ATOMIC); 3440716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3441716ea3a7SDavid Howells return -ENOMEM; 3442716ea3a7SDavid Howells 3443716ea3a7SDavid Howells if (skb1->sk) 3444716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3445716ea3a7SDavid Howells 3446716ea3a7SDavid Howells /* Looking around. Are we still alive? 3447716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3448716ea3a7SDavid Howells 3449716ea3a7SDavid Howells skb2->next = skb1->next; 3450716ea3a7SDavid Howells *skb_p = skb2; 3451716ea3a7SDavid Howells kfree_skb(skb1); 3452716ea3a7SDavid Howells skb1 = skb2; 3453716ea3a7SDavid Howells } 3454716ea3a7SDavid Howells elt++; 3455716ea3a7SDavid Howells *trailer = skb1; 3456716ea3a7SDavid Howells skb_p = &skb1->next; 3457716ea3a7SDavid Howells } 3458716ea3a7SDavid Howells 3459716ea3a7SDavid Howells return elt; 3460716ea3a7SDavid Howells } 3461b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3462716ea3a7SDavid Howells 3463b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3464b1faf566SEric Dumazet { 3465b1faf566SEric Dumazet struct sock *sk = skb->sk; 3466b1faf566SEric Dumazet 3467b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3468b1faf566SEric Dumazet } 3469b1faf566SEric Dumazet 3470b1faf566SEric Dumazet /* 3471b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3472b1faf566SEric Dumazet */ 3473b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3474b1faf566SEric Dumazet { 3475b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 347695c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3477b1faf566SEric Dumazet return -ENOMEM; 3478b1faf566SEric Dumazet 3479b1faf566SEric Dumazet skb_orphan(skb); 3480b1faf566SEric Dumazet skb->sk = sk; 3481b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3482b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3483b1faf566SEric Dumazet 3484abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3485abb57ea4SEric Dumazet skb_dst_force(skb); 3486abb57ea4SEric Dumazet 3487b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3488b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3489676d2369SDavid S. Miller sk->sk_data_ready(sk); 3490b1faf566SEric Dumazet return 0; 3491b1faf566SEric Dumazet } 3492b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3493b1faf566SEric Dumazet 3494364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3495364a9e93SWillem de Bruijn { 3496364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 3497364a9e93SWillem de Bruijn struct sk_buff *skb, *skb_next; 3498364a9e93SWillem de Bruijn int err = 0; 3499364a9e93SWillem de Bruijn 3500364a9e93SWillem de Bruijn spin_lock_bh(&q->lock); 3501364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 3502364a9e93SWillem de Bruijn if (skb && (skb_next = skb_peek(q))) 3503364a9e93SWillem de Bruijn err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 3504364a9e93SWillem de Bruijn spin_unlock_bh(&q->lock); 3505364a9e93SWillem de Bruijn 3506364a9e93SWillem de Bruijn sk->sk_err = err; 3507364a9e93SWillem de Bruijn if (err) 3508364a9e93SWillem de Bruijn sk->sk_error_report(sk); 3509364a9e93SWillem de Bruijn 3510364a9e93SWillem de Bruijn return skb; 3511364a9e93SWillem de Bruijn } 3512364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 3513364a9e93SWillem de Bruijn 3514cab41c47SAlexander Duyck /** 3515cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 3516cab41c47SAlexander Duyck * @skb: the skb to clone 3517cab41c47SAlexander Duyck * 3518cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 3519cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 3520cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 3521cab41c47SAlexander Duyck * 3522cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 3523cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 3524cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 3525cab41c47SAlexander Duyck * the sk_error_queue. 3526cab41c47SAlexander Duyck */ 352762bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 352862bccb8cSAlexander Duyck { 352962bccb8cSAlexander Duyck struct sock *sk = skb->sk; 353062bccb8cSAlexander Duyck struct sk_buff *clone; 353162bccb8cSAlexander Duyck 353262bccb8cSAlexander Duyck if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) 353362bccb8cSAlexander Duyck return NULL; 353462bccb8cSAlexander Duyck 353562bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 353662bccb8cSAlexander Duyck if (!clone) { 353762bccb8cSAlexander Duyck sock_put(sk); 353862bccb8cSAlexander Duyck return NULL; 353962bccb8cSAlexander Duyck } 354062bccb8cSAlexander Duyck 354162bccb8cSAlexander Duyck clone->sk = sk; 354262bccb8cSAlexander Duyck clone->destructor = sock_efree; 354362bccb8cSAlexander Duyck 354462bccb8cSAlexander Duyck return clone; 354562bccb8cSAlexander Duyck } 354662bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 354762bccb8cSAlexander Duyck 354837846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 354937846ef0SAlexander Duyck struct sock *sk, 355037846ef0SAlexander Duyck int tstype) 3551ac45f602SPatrick Ohly { 3552ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3553ac45f602SPatrick Ohly int err; 3554ac45f602SPatrick Ohly 3555ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3556ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3557ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3558ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3559e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 35604ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 356109c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 35624ed2d765SWillem de Bruijn if (sk->sk_protocol == IPPROTO_TCP) 35634ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 35644ed2d765SWillem de Bruijn } 356529030374SEric Dumazet 3566ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 356729030374SEric Dumazet 3568ac45f602SPatrick Ohly if (err) 3569ac45f602SPatrick Ohly kfree_skb(skb); 3570ac45f602SPatrick Ohly } 357137846ef0SAlexander Duyck 357237846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 357337846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 357437846ef0SAlexander Duyck { 357537846ef0SAlexander Duyck struct sock *sk = skb->sk; 357637846ef0SAlexander Duyck 357762bccb8cSAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 357862bccb8cSAlexander Duyck sock_hold(sk); 357937846ef0SAlexander Duyck 358037846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 358137846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); 358237846ef0SAlexander Duyck 358337846ef0SAlexander Duyck sock_put(sk); 358437846ef0SAlexander Duyck } 358537846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 358637846ef0SAlexander Duyck 358737846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 358837846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 358937846ef0SAlexander Duyck struct sock *sk, int tstype) 359037846ef0SAlexander Duyck { 359137846ef0SAlexander Duyck struct sk_buff *skb; 359237846ef0SAlexander Duyck 359337846ef0SAlexander Duyck if (!sk) 359437846ef0SAlexander Duyck return; 359537846ef0SAlexander Duyck 359637846ef0SAlexander Duyck if (hwtstamps) 359737846ef0SAlexander Duyck *skb_hwtstamps(orig_skb) = *hwtstamps; 359837846ef0SAlexander Duyck else 359937846ef0SAlexander Duyck orig_skb->tstamp = ktime_get_real(); 360037846ef0SAlexander Duyck 360137846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 360237846ef0SAlexander Duyck if (!skb) 360337846ef0SAlexander Duyck return; 360437846ef0SAlexander Duyck 360537846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, tstype); 360637846ef0SAlexander Duyck } 3607e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3608e7fd2885SWillem de Bruijn 3609e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 3610e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 3611e7fd2885SWillem de Bruijn { 3612e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3613e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 3614e7fd2885SWillem de Bruijn } 3615ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3616ac45f602SPatrick Ohly 36176e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 36186e3e939fSJohannes Berg { 36196e3e939fSJohannes Berg struct sock *sk = skb->sk; 36206e3e939fSJohannes Berg struct sock_exterr_skb *serr; 36216e3e939fSJohannes Berg int err; 36226e3e939fSJohannes Berg 36236e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 36246e3e939fSJohannes Berg skb->wifi_acked = acked; 36256e3e939fSJohannes Berg 36266e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 36276e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 36286e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 36296e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 36306e3e939fSJohannes Berg 3631bf7fa551SAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 3632bf7fa551SAlexander Duyck sock_hold(sk); 3633bf7fa551SAlexander Duyck 36346e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 36356e3e939fSJohannes Berg if (err) 36366e3e939fSJohannes Berg kfree_skb(skb); 3637bf7fa551SAlexander Duyck 3638bf7fa551SAlexander Duyck sock_put(sk); 36396e3e939fSJohannes Berg } 36406e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 36416e3e939fSJohannes Berg 3642ac45f602SPatrick Ohly 3643f35d9d8aSRusty Russell /** 3644f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3645f35d9d8aSRusty Russell * @skb: the skb to set 3646f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3647f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3648f35d9d8aSRusty Russell * 3649f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3650f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3651f35d9d8aSRusty Russell * 3652f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3653f35d9d8aSRusty Russell * returns false you should drop the packet. 3654f35d9d8aSRusty Russell */ 3655f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3656f35d9d8aSRusty Russell { 36575ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 36585ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3659e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 36605ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3661f35d9d8aSRusty Russell return false; 3662f35d9d8aSRusty Russell } 3663f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3664f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3665f35d9d8aSRusty Russell skb->csum_offset = off; 3666e5d5decaSJason Wang skb_set_transport_header(skb, start); 3667f35d9d8aSRusty Russell return true; 3668f35d9d8aSRusty Russell } 3669b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3670f35d9d8aSRusty Russell 3671ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 3672ed1f50c3SPaul Durrant unsigned int max) 3673ed1f50c3SPaul Durrant { 3674ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 3675ed1f50c3SPaul Durrant return 0; 3676ed1f50c3SPaul Durrant 3677ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 3678ed1f50c3SPaul Durrant * won't need to do it again. 3679ed1f50c3SPaul Durrant */ 3680ed1f50c3SPaul Durrant if (max > skb->len) 3681ed1f50c3SPaul Durrant max = skb->len; 3682ed1f50c3SPaul Durrant 3683ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 3684ed1f50c3SPaul Durrant return -ENOMEM; 3685ed1f50c3SPaul Durrant 3686ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 3687ed1f50c3SPaul Durrant return -EPROTO; 3688ed1f50c3SPaul Durrant 3689ed1f50c3SPaul Durrant return 0; 3690ed1f50c3SPaul Durrant } 3691ed1f50c3SPaul Durrant 3692f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 3693f9708b43SJan Beulich 3694f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 3695f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 3696f9708b43SJan Beulich unsigned int off) 3697f9708b43SJan Beulich { 3698f9708b43SJan Beulich switch (proto) { 3699f9708b43SJan Beulich int err; 3700f9708b43SJan Beulich 3701f9708b43SJan Beulich case IPPROTO_TCP: 3702f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 3703f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 3704f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3705f9708b43SJan Beulich offsetof(struct tcphdr, 3706f9708b43SJan Beulich check))) 3707f9708b43SJan Beulich err = -EPROTO; 3708f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 3709f9708b43SJan Beulich 3710f9708b43SJan Beulich case IPPROTO_UDP: 3711f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 3712f9708b43SJan Beulich off + sizeof(struct udphdr)); 3713f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3714f9708b43SJan Beulich offsetof(struct udphdr, 3715f9708b43SJan Beulich check))) 3716f9708b43SJan Beulich err = -EPROTO; 3717f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 3718f9708b43SJan Beulich } 3719f9708b43SJan Beulich 3720f9708b43SJan Beulich return ERR_PTR(-EPROTO); 3721f9708b43SJan Beulich } 3722f9708b43SJan Beulich 3723ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3724ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 3725ed1f50c3SPaul Durrant */ 3726ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 3727ed1f50c3SPaul Durrant 3728f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 3729ed1f50c3SPaul Durrant { 3730ed1f50c3SPaul Durrant unsigned int off; 3731ed1f50c3SPaul Durrant bool fragment; 3732f9708b43SJan Beulich __sum16 *csum; 3733ed1f50c3SPaul Durrant int err; 3734ed1f50c3SPaul Durrant 3735ed1f50c3SPaul Durrant fragment = false; 3736ed1f50c3SPaul Durrant 3737ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3738ed1f50c3SPaul Durrant sizeof(struct iphdr), 3739ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 3740ed1f50c3SPaul Durrant if (err < 0) 3741ed1f50c3SPaul Durrant goto out; 3742ed1f50c3SPaul Durrant 3743ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 3744ed1f50c3SPaul Durrant fragment = true; 3745ed1f50c3SPaul Durrant 3746ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 3747ed1f50c3SPaul Durrant 3748ed1f50c3SPaul Durrant err = -EPROTO; 3749ed1f50c3SPaul Durrant 3750ed1f50c3SPaul Durrant if (fragment) 3751ed1f50c3SPaul Durrant goto out; 3752ed1f50c3SPaul Durrant 3753f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 3754f9708b43SJan Beulich if (IS_ERR(csum)) 3755f9708b43SJan Beulich return PTR_ERR(csum); 3756ed1f50c3SPaul Durrant 3757ed1f50c3SPaul Durrant if (recalculate) 3758f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 3759ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 3760ed1f50c3SPaul Durrant skb->len - off, 3761f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 3762ed1f50c3SPaul Durrant err = 0; 3763ed1f50c3SPaul Durrant 3764ed1f50c3SPaul Durrant out: 3765ed1f50c3SPaul Durrant return err; 3766ed1f50c3SPaul Durrant } 3767ed1f50c3SPaul Durrant 3768ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3769ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 3770ed1f50c3SPaul Durrant */ 3771ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 3772ed1f50c3SPaul Durrant 3773ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 3774ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 3775ed1f50c3SPaul Durrant 3776ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 3777ed1f50c3SPaul Durrant { 3778ed1f50c3SPaul Durrant int err; 3779ed1f50c3SPaul Durrant u8 nexthdr; 3780ed1f50c3SPaul Durrant unsigned int off; 3781ed1f50c3SPaul Durrant unsigned int len; 3782ed1f50c3SPaul Durrant bool fragment; 3783ed1f50c3SPaul Durrant bool done; 3784f9708b43SJan Beulich __sum16 *csum; 3785ed1f50c3SPaul Durrant 3786ed1f50c3SPaul Durrant fragment = false; 3787ed1f50c3SPaul Durrant done = false; 3788ed1f50c3SPaul Durrant 3789ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 3790ed1f50c3SPaul Durrant 3791ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 3792ed1f50c3SPaul Durrant if (err < 0) 3793ed1f50c3SPaul Durrant goto out; 3794ed1f50c3SPaul Durrant 3795ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 3796ed1f50c3SPaul Durrant 3797ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 3798ed1f50c3SPaul Durrant while (off <= len && !done) { 3799ed1f50c3SPaul Durrant switch (nexthdr) { 3800ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 3801ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 3802ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 3803ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 3804ed1f50c3SPaul Durrant 3805ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3806ed1f50c3SPaul Durrant off + 3807ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 3808ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3809ed1f50c3SPaul Durrant if (err < 0) 3810ed1f50c3SPaul Durrant goto out; 3811ed1f50c3SPaul Durrant 3812ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 3813ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3814ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 3815ed1f50c3SPaul Durrant break; 3816ed1f50c3SPaul Durrant } 3817ed1f50c3SPaul Durrant case IPPROTO_AH: { 3818ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 3819ed1f50c3SPaul Durrant 3820ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3821ed1f50c3SPaul Durrant off + 3822ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 3823ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3824ed1f50c3SPaul Durrant if (err < 0) 3825ed1f50c3SPaul Durrant goto out; 3826ed1f50c3SPaul Durrant 3827ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 3828ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3829ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 3830ed1f50c3SPaul Durrant break; 3831ed1f50c3SPaul Durrant } 3832ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 3833ed1f50c3SPaul Durrant struct frag_hdr *hp; 3834ed1f50c3SPaul Durrant 3835ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3836ed1f50c3SPaul Durrant off + 3837ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 3838ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3839ed1f50c3SPaul Durrant if (err < 0) 3840ed1f50c3SPaul Durrant goto out; 3841ed1f50c3SPaul Durrant 3842ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 3843ed1f50c3SPaul Durrant 3844ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 3845ed1f50c3SPaul Durrant fragment = true; 3846ed1f50c3SPaul Durrant 3847ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3848ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 3849ed1f50c3SPaul Durrant break; 3850ed1f50c3SPaul Durrant } 3851ed1f50c3SPaul Durrant default: 3852ed1f50c3SPaul Durrant done = true; 3853ed1f50c3SPaul Durrant break; 3854ed1f50c3SPaul Durrant } 3855ed1f50c3SPaul Durrant } 3856ed1f50c3SPaul Durrant 3857ed1f50c3SPaul Durrant err = -EPROTO; 3858ed1f50c3SPaul Durrant 3859ed1f50c3SPaul Durrant if (!done || fragment) 3860ed1f50c3SPaul Durrant goto out; 3861ed1f50c3SPaul Durrant 3862f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 3863f9708b43SJan Beulich if (IS_ERR(csum)) 3864f9708b43SJan Beulich return PTR_ERR(csum); 3865ed1f50c3SPaul Durrant 3866ed1f50c3SPaul Durrant if (recalculate) 3867f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 3868ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 3869f9708b43SJan Beulich skb->len - off, nexthdr, 0); 3870ed1f50c3SPaul Durrant err = 0; 3871ed1f50c3SPaul Durrant 3872ed1f50c3SPaul Durrant out: 3873ed1f50c3SPaul Durrant return err; 3874ed1f50c3SPaul Durrant } 3875ed1f50c3SPaul Durrant 3876ed1f50c3SPaul Durrant /** 3877ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 3878ed1f50c3SPaul Durrant * @skb: the skb to set up 3879ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 3880ed1f50c3SPaul Durrant */ 3881ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 3882ed1f50c3SPaul Durrant { 3883ed1f50c3SPaul Durrant int err; 3884ed1f50c3SPaul Durrant 3885ed1f50c3SPaul Durrant switch (skb->protocol) { 3886ed1f50c3SPaul Durrant case htons(ETH_P_IP): 3887f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 3888ed1f50c3SPaul Durrant break; 3889ed1f50c3SPaul Durrant 3890ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 3891ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 3892ed1f50c3SPaul Durrant break; 3893ed1f50c3SPaul Durrant 3894ed1f50c3SPaul Durrant default: 3895ed1f50c3SPaul Durrant err = -EPROTO; 3896ed1f50c3SPaul Durrant break; 3897ed1f50c3SPaul Durrant } 3898ed1f50c3SPaul Durrant 3899ed1f50c3SPaul Durrant return err; 3900ed1f50c3SPaul Durrant } 3901ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 3902ed1f50c3SPaul Durrant 39034497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 39044497b076SBen Hutchings { 3905e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 3906e87cc472SJoe Perches skb->dev->name); 39074497b076SBen Hutchings } 39084497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 3909bad43ca8SEric Dumazet 3910bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 3911bad43ca8SEric Dumazet { 39123d861f66SEric Dumazet if (head_stolen) { 39133d861f66SEric Dumazet skb_release_head_state(skb); 3914bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 39153d861f66SEric Dumazet } else { 3916bad43ca8SEric Dumazet __kfree_skb(skb); 3917bad43ca8SEric Dumazet } 39183d861f66SEric Dumazet } 3919bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 3920bad43ca8SEric Dumazet 3921bad43ca8SEric Dumazet /** 3922bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 3923bad43ca8SEric Dumazet * @to: prior buffer 3924bad43ca8SEric Dumazet * @from: buffer to add 3925bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 3926c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 3927bad43ca8SEric Dumazet */ 3928bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 3929bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 3930bad43ca8SEric Dumazet { 3931bad43ca8SEric Dumazet int i, delta, len = from->len; 3932bad43ca8SEric Dumazet 3933bad43ca8SEric Dumazet *fragstolen = false; 3934bad43ca8SEric Dumazet 3935bad43ca8SEric Dumazet if (skb_cloned(to)) 3936bad43ca8SEric Dumazet return false; 3937bad43ca8SEric Dumazet 3938bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 3939e93a0435SEric Dumazet if (len) 3940bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 3941bad43ca8SEric Dumazet *delta_truesize = 0; 3942bad43ca8SEric Dumazet return true; 3943bad43ca8SEric Dumazet } 3944bad43ca8SEric Dumazet 3945bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 3946bad43ca8SEric Dumazet return false; 3947bad43ca8SEric Dumazet 3948bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 3949bad43ca8SEric Dumazet struct page *page; 3950bad43ca8SEric Dumazet unsigned int offset; 3951bad43ca8SEric Dumazet 3952bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 3953bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 3954bad43ca8SEric Dumazet return false; 3955bad43ca8SEric Dumazet 3956bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 3957bad43ca8SEric Dumazet return false; 3958bad43ca8SEric Dumazet 3959bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3960bad43ca8SEric Dumazet 3961bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 3962bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 3963bad43ca8SEric Dumazet 3964bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 3965bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 3966bad43ca8SEric Dumazet *fragstolen = true; 3967bad43ca8SEric Dumazet } else { 3968bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 3969bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 3970bad43ca8SEric Dumazet return false; 3971bad43ca8SEric Dumazet 3972f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 3973bad43ca8SEric Dumazet } 3974bad43ca8SEric Dumazet 3975bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 3976bad43ca8SEric Dumazet 3977bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 3978bad43ca8SEric Dumazet skb_shinfo(from)->frags, 3979bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 3980bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 3981bad43ca8SEric Dumazet 3982bad43ca8SEric Dumazet if (!skb_cloned(from)) 3983bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 3984bad43ca8SEric Dumazet 39858ea853fdSLi RongQing /* if the skb is not cloned this does nothing 39868ea853fdSLi RongQing * since we set nr_frags to 0. 39878ea853fdSLi RongQing */ 3988bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 3989bad43ca8SEric Dumazet skb_frag_ref(from, i); 3990bad43ca8SEric Dumazet 3991bad43ca8SEric Dumazet to->truesize += delta; 3992bad43ca8SEric Dumazet to->len += len; 3993bad43ca8SEric Dumazet to->data_len += len; 3994bad43ca8SEric Dumazet 3995bad43ca8SEric Dumazet *delta_truesize = delta; 3996bad43ca8SEric Dumazet return true; 3997bad43ca8SEric Dumazet } 3998bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 3999621e84d6SNicolas Dichtel 4000621e84d6SNicolas Dichtel /** 40018b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4002621e84d6SNicolas Dichtel * 4003621e84d6SNicolas Dichtel * @skb: buffer to clean 40048b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4005621e84d6SNicolas Dichtel * 40068b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 40078b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 40088b27f277SNicolas Dichtel * operations. 40098b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 40108b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 40118b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4012621e84d6SNicolas Dichtel */ 40138b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4014621e84d6SNicolas Dichtel { 40158b27f277SNicolas Dichtel if (xnet) 4016621e84d6SNicolas Dichtel skb_orphan(skb); 4017621e84d6SNicolas Dichtel skb->tstamp.tv64 = 0; 4018621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4019621e84d6SNicolas Dichtel skb->skb_iif = 0; 402060ff7467SWANG Cong skb->ignore_df = 0; 4021621e84d6SNicolas Dichtel skb_dst_drop(skb); 4022621e84d6SNicolas Dichtel skb->mark = 0; 4023621e84d6SNicolas Dichtel secpath_reset(skb); 4024621e84d6SNicolas Dichtel nf_reset(skb); 4025621e84d6SNicolas Dichtel nf_reset_trace(skb); 4026621e84d6SNicolas Dichtel } 4027621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4028de960aa9SFlorian Westphal 4029de960aa9SFlorian Westphal /** 4030de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4031de960aa9SFlorian Westphal * 4032de960aa9SFlorian Westphal * @skb: GSO skb 4033de960aa9SFlorian Westphal * 4034de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4035de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4036de960aa9SFlorian Westphal * 4037de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4038de960aa9SFlorian Westphal */ 4039de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4040de960aa9SFlorian Westphal { 4041de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4042de960aa9SFlorian Westphal 4043de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 40446d39d589SFlorian Westphal return tcp_hdrlen(skb) + shinfo->gso_size; 40456d39d589SFlorian Westphal 40466d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 40476d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 40486d39d589SFlorian Westphal * accounted for. 40496d39d589SFlorian Westphal */ 40506d39d589SFlorian Westphal return shinfo->gso_size; 4051de960aa9SFlorian Westphal } 4052de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 40530d5501c1SVlad Yasevich 40540d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 40550d5501c1SVlad Yasevich { 40560d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 40570d5501c1SVlad Yasevich kfree_skb(skb); 40580d5501c1SVlad Yasevich return NULL; 40590d5501c1SVlad Yasevich } 40600d5501c1SVlad Yasevich 40610d5501c1SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN); 40620d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 40630d5501c1SVlad Yasevich return skb; 40640d5501c1SVlad Yasevich } 40650d5501c1SVlad Yasevich 40660d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 40670d5501c1SVlad Yasevich { 40680d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 40690d5501c1SVlad Yasevich u16 vlan_tci; 40700d5501c1SVlad Yasevich 40710d5501c1SVlad Yasevich if (unlikely(vlan_tx_tag_present(skb))) { 40720d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 40730d5501c1SVlad Yasevich return skb; 40740d5501c1SVlad Yasevich } 40750d5501c1SVlad Yasevich 40760d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 40770d5501c1SVlad Yasevich if (unlikely(!skb)) 40780d5501c1SVlad Yasevich goto err_free; 40790d5501c1SVlad Yasevich 40800d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 40810d5501c1SVlad Yasevich goto err_free; 40820d5501c1SVlad Yasevich 40830d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 40840d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 40850d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 40860d5501c1SVlad Yasevich 40870d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 40880d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 40890d5501c1SVlad Yasevich 40900d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 40910d5501c1SVlad Yasevich if (unlikely(!skb)) 40920d5501c1SVlad Yasevich goto err_free; 40930d5501c1SVlad Yasevich 40940d5501c1SVlad Yasevich skb_reset_network_header(skb); 40950d5501c1SVlad Yasevich skb_reset_transport_header(skb); 40960d5501c1SVlad Yasevich skb_reset_mac_len(skb); 40970d5501c1SVlad Yasevich 40980d5501c1SVlad Yasevich return skb; 40990d5501c1SVlad Yasevich 41000d5501c1SVlad Yasevich err_free: 41010d5501c1SVlad Yasevich kfree_skb(skb); 41020d5501c1SVlad Yasevich return NULL; 41030d5501c1SVlad Yasevich } 41040d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 4105