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> 77b245be1fSWillem de Bruijn #include <linux/capability.h> 78b245be1fSWillem de Bruijn #include <linux/user_namespace.h> 79a1f8e7f7SAl Viro 80d7e8883cSEric Dumazet struct kmem_cache *skbuff_head_cache __read_mostly; 81e18b890bSChristoph Lameter static struct kmem_cache *skbuff_fclone_cache __read_mostly; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /** 84f05de73bSJean Sacren * skb_panic - private function for out-of-line support 851da177e4SLinus Torvalds * @skb: buffer 861da177e4SLinus Torvalds * @sz: size 87f05de73bSJean Sacren * @addr: address 8899d5851eSJames Hogan * @msg: skb_over_panic or skb_under_panic 891da177e4SLinus Torvalds * 90f05de73bSJean Sacren * Out-of-line support for skb_put() and skb_push(). 91f05de73bSJean Sacren * Called via the wrapper skb_over_panic() or skb_under_panic(). 92f05de73bSJean Sacren * Keep out of line to prevent kernel bloat. 93f05de73bSJean Sacren * __builtin_return_address is not used because it is not always reliable. 941da177e4SLinus Torvalds */ 95f05de73bSJean Sacren static void skb_panic(struct sk_buff *skb, unsigned int sz, void *addr, 9699d5851eSJames Hogan const char msg[]) 971da177e4SLinus Torvalds { 98e005d193SJoe Perches pr_emerg("%s: text:%p len:%d put:%d head:%p data:%p tail:%#lx end:%#lx dev:%s\n", 9999d5851eSJames Hogan msg, addr, skb->len, sz, skb->head, skb->data, 1004305b541SArnaldo Carvalho de Melo (unsigned long)skb->tail, (unsigned long)skb->end, 10126095455SPatrick McHardy skb->dev ? skb->dev->name : "<NULL>"); 1021da177e4SLinus Torvalds BUG(); 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 105f05de73bSJean Sacren static void skb_over_panic(struct sk_buff *skb, unsigned int sz, void *addr) 1061da177e4SLinus Torvalds { 107f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 110f05de73bSJean Sacren static void skb_under_panic(struct sk_buff *skb, unsigned int sz, void *addr) 111f05de73bSJean Sacren { 112f05de73bSJean Sacren skb_panic(skb, sz, addr, __func__); 113f05de73bSJean Sacren } 114c93bdd0eSMel Gorman 115c93bdd0eSMel Gorman /* 116c93bdd0eSMel Gorman * kmalloc_reserve is a wrapper around kmalloc_node_track_caller that tells 117c93bdd0eSMel Gorman * the caller if emergency pfmemalloc reserves are being used. If it is and 118c93bdd0eSMel Gorman * the socket is later found to be SOCK_MEMALLOC then PFMEMALLOC reserves 119c93bdd0eSMel Gorman * may be used. Otherwise, the packet data may be discarded until enough 120c93bdd0eSMel Gorman * memory is free 121c93bdd0eSMel Gorman */ 122c93bdd0eSMel Gorman #define kmalloc_reserve(size, gfp, node, pfmemalloc) \ 123c93bdd0eSMel Gorman __kmalloc_reserve(size, gfp, node, _RET_IP_, pfmemalloc) 12461c5e88aSstephen hemminger 12561c5e88aSstephen hemminger static void *__kmalloc_reserve(size_t size, gfp_t flags, int node, 12661c5e88aSstephen hemminger unsigned long ip, bool *pfmemalloc) 127c93bdd0eSMel Gorman { 128c93bdd0eSMel Gorman void *obj; 129c93bdd0eSMel Gorman bool ret_pfmemalloc = false; 130c93bdd0eSMel Gorman 131c93bdd0eSMel Gorman /* 132c93bdd0eSMel Gorman * Try a regular allocation, when that fails and we're not entitled 133c93bdd0eSMel Gorman * to the reserves, fail. 134c93bdd0eSMel Gorman */ 135c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, 136c93bdd0eSMel Gorman flags | __GFP_NOMEMALLOC | __GFP_NOWARN, 137c93bdd0eSMel Gorman node); 138c93bdd0eSMel Gorman if (obj || !(gfp_pfmemalloc_allowed(flags))) 139c93bdd0eSMel Gorman goto out; 140c93bdd0eSMel Gorman 141c93bdd0eSMel Gorman /* Try again but now we are using pfmemalloc reserves */ 142c93bdd0eSMel Gorman ret_pfmemalloc = true; 143c93bdd0eSMel Gorman obj = kmalloc_node_track_caller(size, flags, node); 144c93bdd0eSMel Gorman 145c93bdd0eSMel Gorman out: 146c93bdd0eSMel Gorman if (pfmemalloc) 147c93bdd0eSMel Gorman *pfmemalloc = ret_pfmemalloc; 148c93bdd0eSMel Gorman 149c93bdd0eSMel Gorman return obj; 150c93bdd0eSMel Gorman } 151c93bdd0eSMel Gorman 1521da177e4SLinus Torvalds /* Allocate a new skbuff. We do this ourselves so we can fill in a few 1531da177e4SLinus Torvalds * 'private' fields and also do memory statistics to find all the 1541da177e4SLinus Torvalds * [BEEP] leaks. 1551da177e4SLinus Torvalds * 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds 1580ebd0ac5SPatrick McHardy struct sk_buff *__alloc_skb_head(gfp_t gfp_mask, int node) 1590ebd0ac5SPatrick McHardy { 1600ebd0ac5SPatrick McHardy struct sk_buff *skb; 1610ebd0ac5SPatrick McHardy 1620ebd0ac5SPatrick McHardy /* Get the HEAD */ 1630ebd0ac5SPatrick McHardy skb = kmem_cache_alloc_node(skbuff_head_cache, 1640ebd0ac5SPatrick McHardy gfp_mask & ~__GFP_DMA, node); 1650ebd0ac5SPatrick McHardy if (!skb) 1660ebd0ac5SPatrick McHardy goto out; 1670ebd0ac5SPatrick McHardy 1680ebd0ac5SPatrick McHardy /* 1690ebd0ac5SPatrick McHardy * Only clear those fields we need to clear, not those that we will 1700ebd0ac5SPatrick McHardy * actually initialise below. Hence, don't put any more fields after 1710ebd0ac5SPatrick McHardy * the tail pointer in struct sk_buff! 1720ebd0ac5SPatrick McHardy */ 1730ebd0ac5SPatrick McHardy memset(skb, 0, offsetof(struct sk_buff, tail)); 1745e71d9d7SPablo Neira skb->head = NULL; 1750ebd0ac5SPatrick McHardy skb->truesize = sizeof(struct sk_buff); 1760ebd0ac5SPatrick McHardy atomic_set(&skb->users, 1); 1770ebd0ac5SPatrick McHardy 17835d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 1790ebd0ac5SPatrick McHardy out: 1800ebd0ac5SPatrick McHardy return skb; 1810ebd0ac5SPatrick McHardy } 1820ebd0ac5SPatrick McHardy 1831da177e4SLinus Torvalds /** 184d179cd12SDavid S. Miller * __alloc_skb - allocate a network buffer 1851da177e4SLinus Torvalds * @size: size to allocate 1861da177e4SLinus Torvalds * @gfp_mask: allocation mask 187c93bdd0eSMel Gorman * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 188c93bdd0eSMel Gorman * instead of head cache and allocate a cloned (child) skb. 189c93bdd0eSMel Gorman * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 190c93bdd0eSMel Gorman * allocations in case the data is required for writeback 191b30973f8SChristoph Hellwig * @node: numa node to allocate memory on 1921da177e4SLinus Torvalds * 1931da177e4SLinus Torvalds * Allocate a new &sk_buff. The returned buffer has no headroom and a 19494b6042cSBen Hutchings * tail room of at least size bytes. The object has a reference count 19594b6042cSBen Hutchings * of one. The return is the buffer. On a failure the return is %NULL. 1961da177e4SLinus Torvalds * 1971da177e4SLinus Torvalds * Buffers may only be allocated from interrupts using a @gfp_mask of 1981da177e4SLinus Torvalds * %GFP_ATOMIC. 1991da177e4SLinus Torvalds */ 200dd0fc66fSAl Viro struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 201c93bdd0eSMel Gorman int flags, int node) 2021da177e4SLinus Torvalds { 203e18b890bSChristoph Lameter struct kmem_cache *cache; 2044947d3efSBenjamin LaHaise struct skb_shared_info *shinfo; 2051da177e4SLinus Torvalds struct sk_buff *skb; 2061da177e4SLinus Torvalds u8 *data; 207c93bdd0eSMel Gorman bool pfmemalloc; 2081da177e4SLinus Torvalds 209c93bdd0eSMel Gorman cache = (flags & SKB_ALLOC_FCLONE) 210c93bdd0eSMel Gorman ? skbuff_fclone_cache : skbuff_head_cache; 211c93bdd0eSMel Gorman 212c93bdd0eSMel Gorman if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 213c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 2148798b3fbSHerbert Xu 2151da177e4SLinus Torvalds /* Get the HEAD */ 216b30973f8SChristoph Hellwig skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 2171da177e4SLinus Torvalds if (!skb) 2181da177e4SLinus Torvalds goto out; 219ec7d2f2cSEric Dumazet prefetchw(skb); 2201da177e4SLinus Torvalds 22187fb4b7bSEric Dumazet /* We do our best to align skb_shared_info on a separate cache 22287fb4b7bSEric Dumazet * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 22387fb4b7bSEric Dumazet * aligned memory blocks, unless SLUB/SLAB debug is enabled. 22487fb4b7bSEric Dumazet * Both skb->head and skb_shared_info are cache line aligned. 22587fb4b7bSEric Dumazet */ 226bc417e30STony Lindgren size = SKB_DATA_ALIGN(size); 22787fb4b7bSEric Dumazet size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 228c93bdd0eSMel Gorman data = kmalloc_reserve(size, gfp_mask, node, &pfmemalloc); 2291da177e4SLinus Torvalds if (!data) 2301da177e4SLinus Torvalds goto nodata; 23187fb4b7bSEric Dumazet /* kmalloc(size) might give us more room than requested. 23287fb4b7bSEric Dumazet * Put skb_shared_info exactly at the end of allocated zone, 23387fb4b7bSEric Dumazet * to allow max possible filling before reallocation. 23487fb4b7bSEric Dumazet */ 23587fb4b7bSEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 236ec7d2f2cSEric Dumazet prefetchw(data + size); 2371da177e4SLinus Torvalds 238ca0605a7SArnaldo Carvalho de Melo /* 239c8005785SJohannes Berg * Only clear those fields we need to clear, not those that we will 240c8005785SJohannes Berg * actually initialise below. Hence, don't put any more fields after 241c8005785SJohannes Berg * the tail pointer in struct sk_buff! 242ca0605a7SArnaldo Carvalho de Melo */ 243ca0605a7SArnaldo Carvalho de Melo memset(skb, 0, offsetof(struct sk_buff, tail)); 24487fb4b7bSEric Dumazet /* Account for allocated memory : skb + skb->head */ 24587fb4b7bSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 246c93bdd0eSMel Gorman skb->pfmemalloc = pfmemalloc; 2471da177e4SLinus Torvalds atomic_set(&skb->users, 1); 2481da177e4SLinus Torvalds skb->head = data; 2491da177e4SLinus Torvalds skb->data = data; 25027a884dcSArnaldo Carvalho de Melo skb_reset_tail_pointer(skb); 2514305b541SArnaldo Carvalho de Melo skb->end = skb->tail + size; 25235d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 25335d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 25419633e12SStephen Hemminger 2554947d3efSBenjamin LaHaise /* make sure we initialize shinfo sequentially */ 2564947d3efSBenjamin LaHaise shinfo = skb_shinfo(skb); 257ec7d2f2cSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 2584947d3efSBenjamin LaHaise atomic_set(&shinfo->dataref, 1); 259c2aa3665SEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 2604947d3efSBenjamin LaHaise 261c93bdd0eSMel Gorman if (flags & SKB_ALLOC_FCLONE) { 262d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 2631da177e4SLinus Torvalds 264d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 265d0bf4a9eSEric Dumazet 266d0bf4a9eSEric Dumazet kmemcheck_annotate_bitfield(&fclones->skb2, flags1); 267d179cd12SDavid S. Miller skb->fclone = SKB_FCLONE_ORIG; 268d0bf4a9eSEric Dumazet atomic_set(&fclones->fclone_ref, 1); 269d179cd12SDavid S. Miller 2706ffe75ebSEric Dumazet fclones->skb2.fclone = SKB_FCLONE_CLONE; 271d0bf4a9eSEric Dumazet fclones->skb2.pfmemalloc = pfmemalloc; 272d179cd12SDavid S. Miller } 2731da177e4SLinus Torvalds out: 2741da177e4SLinus Torvalds return skb; 2751da177e4SLinus Torvalds nodata: 2768798b3fbSHerbert Xu kmem_cache_free(cache, skb); 2771da177e4SLinus Torvalds skb = NULL; 2781da177e4SLinus Torvalds goto out; 2791da177e4SLinus Torvalds } 280b4ac530fSDavid S. Miller EXPORT_SYMBOL(__alloc_skb); 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /** 2832ea2f62cSEric Dumazet * __build_skb - build a network buffer 284b2b5ce9dSEric Dumazet * @data: data buffer provided by caller 2852ea2f62cSEric Dumazet * @frag_size: size of data, or 0 if head was kmalloced 286b2b5ce9dSEric Dumazet * 287b2b5ce9dSEric Dumazet * Allocate a new &sk_buff. Caller provides space holding head and 288deceb4c0SFlorian Fainelli * skb_shared_info. @data must have been allocated by kmalloc() only if 2892ea2f62cSEric Dumazet * @frag_size is 0, otherwise data should come from the page allocator 2902ea2f62cSEric Dumazet * or vmalloc() 291b2b5ce9dSEric Dumazet * The return is the new skb buffer. 292b2b5ce9dSEric Dumazet * On a failure the return is %NULL, and @data is not freed. 293b2b5ce9dSEric Dumazet * Notes : 294b2b5ce9dSEric Dumazet * Before IO, driver allocates only data buffer where NIC put incoming frame 295b2b5ce9dSEric Dumazet * Driver should add room at head (NET_SKB_PAD) and 296b2b5ce9dSEric Dumazet * MUST add room at tail (SKB_DATA_ALIGN(skb_shared_info)) 297b2b5ce9dSEric Dumazet * After IO, driver calls build_skb(), to allocate sk_buff and populate it 298b2b5ce9dSEric Dumazet * before giving packet to stack. 299b2b5ce9dSEric Dumazet * RX rings only contains data buffers, not full skbs. 300b2b5ce9dSEric Dumazet */ 3012ea2f62cSEric Dumazet struct sk_buff *__build_skb(void *data, unsigned int frag_size) 302b2b5ce9dSEric Dumazet { 303b2b5ce9dSEric Dumazet struct skb_shared_info *shinfo; 304b2b5ce9dSEric Dumazet struct sk_buff *skb; 305d3836f21SEric Dumazet unsigned int size = frag_size ? : ksize(data); 306b2b5ce9dSEric Dumazet 307b2b5ce9dSEric Dumazet skb = kmem_cache_alloc(skbuff_head_cache, GFP_ATOMIC); 308b2b5ce9dSEric Dumazet if (!skb) 309b2b5ce9dSEric Dumazet return NULL; 310b2b5ce9dSEric Dumazet 311d3836f21SEric Dumazet size -= SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 312b2b5ce9dSEric Dumazet 313b2b5ce9dSEric Dumazet memset(skb, 0, offsetof(struct sk_buff, tail)); 314b2b5ce9dSEric Dumazet skb->truesize = SKB_TRUESIZE(size); 315b2b5ce9dSEric Dumazet atomic_set(&skb->users, 1); 316b2b5ce9dSEric Dumazet skb->head = data; 317b2b5ce9dSEric Dumazet skb->data = data; 318b2b5ce9dSEric Dumazet skb_reset_tail_pointer(skb); 319b2b5ce9dSEric Dumazet skb->end = skb->tail + size; 32035d04610SCong Wang skb->mac_header = (typeof(skb->mac_header))~0U; 32135d04610SCong Wang skb->transport_header = (typeof(skb->transport_header))~0U; 322b2b5ce9dSEric Dumazet 323b2b5ce9dSEric Dumazet /* make sure we initialize shinfo sequentially */ 324b2b5ce9dSEric Dumazet shinfo = skb_shinfo(skb); 325b2b5ce9dSEric Dumazet memset(shinfo, 0, offsetof(struct skb_shared_info, dataref)); 326b2b5ce9dSEric Dumazet atomic_set(&shinfo->dataref, 1); 327b2b5ce9dSEric Dumazet kmemcheck_annotate_variable(shinfo->destructor_arg); 328b2b5ce9dSEric Dumazet 329b2b5ce9dSEric Dumazet return skb; 330b2b5ce9dSEric Dumazet } 3312ea2f62cSEric Dumazet 3322ea2f62cSEric Dumazet /* build_skb() is wrapper over __build_skb(), that specifically 3332ea2f62cSEric Dumazet * takes care of skb->head and skb->pfmemalloc 3342ea2f62cSEric Dumazet * This means that if @frag_size is not zero, then @data must be backed 3352ea2f62cSEric Dumazet * by a page fragment, not kmalloc() or vmalloc() 3362ea2f62cSEric Dumazet */ 3372ea2f62cSEric Dumazet struct sk_buff *build_skb(void *data, unsigned int frag_size) 3382ea2f62cSEric Dumazet { 3392ea2f62cSEric Dumazet struct sk_buff *skb = __build_skb(data, frag_size); 3402ea2f62cSEric Dumazet 3412ea2f62cSEric Dumazet if (skb && frag_size) { 3422ea2f62cSEric Dumazet skb->head_frag = 1; 3432ea2f62cSEric Dumazet if (virt_to_head_page(data)->pfmemalloc) 3442ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3452ea2f62cSEric Dumazet } 3462ea2f62cSEric Dumazet return skb; 3472ea2f62cSEric Dumazet } 348b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 349b2b5ce9dSEric Dumazet 350a1c7fff7SEric Dumazet struct netdev_alloc_cache { 35169b08f62SEric Dumazet struct page_frag frag; 35269b08f62SEric Dumazet /* we maintain a pagecount bias, so that we dont dirty cache line 35369b08f62SEric Dumazet * containing page->_count every time we allocate a fragment. 35469b08f62SEric Dumazet */ 355540eb7bfSAlexander Duyck unsigned int pagecnt_bias; 356a1c7fff7SEric Dumazet }; 357a1c7fff7SEric Dumazet static DEFINE_PER_CPU(struct netdev_alloc_cache, netdev_alloc_cache); 358ffde7328SAlexander Duyck static DEFINE_PER_CPU(struct netdev_alloc_cache, napi_alloc_cache); 359a1c7fff7SEric Dumazet 360ffde7328SAlexander Duyck static struct page *__page_frag_refill(struct netdev_alloc_cache *nc, 361ffde7328SAlexander Duyck gfp_t gfp_mask) 3626f532612SEric Dumazet { 363ffde7328SAlexander Duyck const unsigned int order = NETDEV_FRAG_PAGE_MAX_ORDER; 364ffde7328SAlexander Duyck struct page *page = NULL; 36569b08f62SEric Dumazet gfp_t gfp = gfp_mask; 36669b08f62SEric Dumazet 367ffde7328SAlexander Duyck if (order) { 36879930f58SEric Dumazet gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | 36979930f58SEric Dumazet __GFP_NOMEMALLOC; 370ffde7328SAlexander Duyck page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, order); 371ffde7328SAlexander Duyck nc->frag.size = PAGE_SIZE << (page ? order : 0); 37269b08f62SEric Dumazet } 373ffde7328SAlexander Duyck 374ffde7328SAlexander Duyck if (unlikely(!page)) 375ffde7328SAlexander Duyck page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); 376ffde7328SAlexander Duyck 377ffde7328SAlexander Duyck nc->frag.page = page; 378ffde7328SAlexander Duyck 379ffde7328SAlexander Duyck return page; 380ffde7328SAlexander Duyck } 381ffde7328SAlexander Duyck 382ffde7328SAlexander Duyck static void *__alloc_page_frag(struct netdev_alloc_cache __percpu *cache, 383ffde7328SAlexander Duyck unsigned int fragsz, gfp_t gfp_mask) 384ffde7328SAlexander Duyck { 385ffde7328SAlexander Duyck struct netdev_alloc_cache *nc = this_cpu_ptr(cache); 386ffde7328SAlexander Duyck struct page *page = nc->frag.page; 387ffde7328SAlexander Duyck unsigned int size; 388ffde7328SAlexander Duyck int offset; 389ffde7328SAlexander Duyck 390ffde7328SAlexander Duyck if (unlikely(!page)) { 391ffde7328SAlexander Duyck refill: 392ffde7328SAlexander Duyck page = __page_frag_refill(nc, gfp_mask); 393ffde7328SAlexander Duyck if (!page) 394ffde7328SAlexander Duyck return NULL; 395ffde7328SAlexander Duyck 396ffde7328SAlexander Duyck /* if size can vary use frag.size else just use PAGE_SIZE */ 397ffde7328SAlexander Duyck size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; 398ffde7328SAlexander Duyck 3994c450583SEric Dumazet /* Even if we own the page, we do not use atomic_set(). 4004c450583SEric Dumazet * This would break get_page_unless_zero() users. 4014c450583SEric Dumazet */ 402ffde7328SAlexander Duyck atomic_add(size - 1, &page->_count); 403ffde7328SAlexander Duyck 404ffde7328SAlexander Duyck /* reset page count bias and offset to start of new frag */ 405ffde7328SAlexander Duyck nc->pagecnt_bias = size; 406ffde7328SAlexander Duyck nc->frag.offset = size; 4076f532612SEric Dumazet } 408540eb7bfSAlexander Duyck 409ffde7328SAlexander Duyck offset = nc->frag.offset - fragsz; 410ffde7328SAlexander Duyck if (unlikely(offset < 0)) { 411ffde7328SAlexander Duyck if (!atomic_sub_and_test(nc->pagecnt_bias, &page->_count)) 4126f532612SEric Dumazet goto refill; 413ffde7328SAlexander Duyck 414ffde7328SAlexander Duyck /* if size can vary use frag.size else just use PAGE_SIZE */ 415ffde7328SAlexander Duyck size = NETDEV_FRAG_PAGE_MAX_ORDER ? nc->frag.size : PAGE_SIZE; 416ffde7328SAlexander Duyck 4174c450583SEric Dumazet /* OK, page count is 0, we can safely set it */ 418ffde7328SAlexander Duyck atomic_set(&page->_count, size); 419ffde7328SAlexander Duyck 420ffde7328SAlexander Duyck /* reset page count bias and offset to start of new frag */ 421ffde7328SAlexander Duyck nc->pagecnt_bias = size; 422ffde7328SAlexander Duyck offset = size - fragsz; 4236f532612SEric Dumazet } 424540eb7bfSAlexander Duyck 425540eb7bfSAlexander Duyck nc->pagecnt_bias--; 426ffde7328SAlexander Duyck nc->frag.offset = offset; 427ffde7328SAlexander Duyck 428ffde7328SAlexander Duyck return page_address(page) + offset; 429ffde7328SAlexander Duyck } 430ffde7328SAlexander Duyck 431ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 432ffde7328SAlexander Duyck { 433ffde7328SAlexander Duyck unsigned long flags; 434ffde7328SAlexander Duyck void *data; 435ffde7328SAlexander Duyck 436ffde7328SAlexander Duyck local_irq_save(flags); 437ffde7328SAlexander Duyck data = __alloc_page_frag(&netdev_alloc_cache, fragsz, gfp_mask); 4386f532612SEric Dumazet local_irq_restore(flags); 4396f532612SEric Dumazet return data; 4406f532612SEric Dumazet } 441c93bdd0eSMel Gorman 442c93bdd0eSMel Gorman /** 443c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 444c93bdd0eSMel Gorman * @fragsz: fragment size 445c93bdd0eSMel Gorman * 446c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 447c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 448c93bdd0eSMel Gorman */ 449c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 450c93bdd0eSMel Gorman { 451c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 452c93bdd0eSMel Gorman } 4536f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 4546f532612SEric Dumazet 455ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 456ffde7328SAlexander Duyck { 457ffde7328SAlexander Duyck return __alloc_page_frag(&napi_alloc_cache, fragsz, gfp_mask); 458ffde7328SAlexander Duyck } 459ffde7328SAlexander Duyck 460ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 461ffde7328SAlexander Duyck { 462ffde7328SAlexander Duyck return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 463ffde7328SAlexander Duyck } 464ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 465ffde7328SAlexander Duyck 4666f532612SEric Dumazet /** 467fd11a83dSAlexander Duyck * __alloc_rx_skb - allocate an skbuff for rx 4688af27456SChristoph Hellwig * @length: length to allocate 4698af27456SChristoph Hellwig * @gfp_mask: get_free_pages mask, passed to alloc_skb 470fd11a83dSAlexander Duyck * @flags: If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 471fd11a83dSAlexander Duyck * allocations in case we have to fallback to __alloc_skb() 472fd11a83dSAlexander Duyck * If SKB_ALLOC_NAPI is set, page fragment will be allocated 473fd11a83dSAlexander Duyck * from napi_cache instead of netdev_cache. 4748af27456SChristoph Hellwig * 4758af27456SChristoph Hellwig * Allocate a new &sk_buff and assign it a usage count of one. The 4768af27456SChristoph Hellwig * buffer has unspecified headroom built in. Users should allocate 4778af27456SChristoph Hellwig * the headroom they think they need without accounting for the 4788af27456SChristoph Hellwig * built in space. The built in space is used for optimisations. 4798af27456SChristoph Hellwig * 4808af27456SChristoph Hellwig * %NULL is returned if there is no free memory. 4818af27456SChristoph Hellwig */ 482fd11a83dSAlexander Duyck static struct sk_buff *__alloc_rx_skb(unsigned int length, gfp_t gfp_mask, 483fd11a83dSAlexander Duyck int flags) 4848af27456SChristoph Hellwig { 4856f532612SEric Dumazet struct sk_buff *skb = NULL; 486fd11a83dSAlexander Duyck unsigned int fragsz = SKB_DATA_ALIGN(length) + 487a1c7fff7SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4888af27456SChristoph Hellwig 489310e158cSEric Dumazet if (fragsz <= PAGE_SIZE && !(gfp_mask & (__GFP_WAIT | GFP_DMA))) { 490c93bdd0eSMel Gorman void *data; 491c93bdd0eSMel Gorman 492c93bdd0eSMel Gorman if (sk_memalloc_socks()) 493c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 494c93bdd0eSMel Gorman 495fd11a83dSAlexander Duyck data = (flags & SKB_ALLOC_NAPI) ? 496fd11a83dSAlexander Duyck __napi_alloc_frag(fragsz, gfp_mask) : 497fd11a83dSAlexander Duyck __netdev_alloc_frag(fragsz, gfp_mask); 498a1c7fff7SEric Dumazet 4996f532612SEric Dumazet if (likely(data)) { 5006f532612SEric Dumazet skb = build_skb(data, fragsz); 5016f532612SEric Dumazet if (unlikely(!skb)) 5026f532612SEric Dumazet put_page(virt_to_head_page(data)); 503a1c7fff7SEric Dumazet } 504a1c7fff7SEric Dumazet } else { 505fd11a83dSAlexander Duyck skb = __alloc_skb(length, gfp_mask, 506c93bdd0eSMel Gorman SKB_ALLOC_RX, NUMA_NO_NODE); 507a1c7fff7SEric Dumazet } 508fd11a83dSAlexander Duyck return skb; 509fd11a83dSAlexander Duyck } 510fd11a83dSAlexander Duyck 511fd11a83dSAlexander Duyck /** 512fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 513fd11a83dSAlexander Duyck * @dev: network device to receive on 514fd11a83dSAlexander Duyck * @length: length to allocate 515fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 516fd11a83dSAlexander Duyck * 517fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 518fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 519fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 520fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 521fd11a83dSAlexander Duyck * 522fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 523fd11a83dSAlexander Duyck */ 524fd11a83dSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, 525fd11a83dSAlexander Duyck unsigned int length, gfp_t gfp_mask) 526fd11a83dSAlexander Duyck { 527fd11a83dSAlexander Duyck struct sk_buff *skb; 528fd11a83dSAlexander Duyck 529fd11a83dSAlexander Duyck length += NET_SKB_PAD; 530fd11a83dSAlexander Duyck skb = __alloc_rx_skb(length, gfp_mask, 0); 531fd11a83dSAlexander Duyck 5327b2e497aSChristoph Hellwig if (likely(skb)) { 5338af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 5347b2e497aSChristoph Hellwig skb->dev = dev; 5357b2e497aSChristoph Hellwig } 536fd11a83dSAlexander Duyck 5378af27456SChristoph Hellwig return skb; 5388af27456SChristoph Hellwig } 539b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 5401da177e4SLinus Torvalds 541fd11a83dSAlexander Duyck /** 542fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 543fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 544fd11a83dSAlexander Duyck * @length: length to allocate 545fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 546fd11a83dSAlexander Duyck * 547fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 548fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 549fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 550fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 551fd11a83dSAlexander Duyck * 552fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 553fd11a83dSAlexander Duyck */ 554fd11a83dSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, 555fd11a83dSAlexander Duyck unsigned int length, gfp_t gfp_mask) 556fd11a83dSAlexander Duyck { 557fd11a83dSAlexander Duyck struct sk_buff *skb; 558fd11a83dSAlexander Duyck 559fd11a83dSAlexander Duyck length += NET_SKB_PAD + NET_IP_ALIGN; 560fd11a83dSAlexander Duyck skb = __alloc_rx_skb(length, gfp_mask, SKB_ALLOC_NAPI); 561fd11a83dSAlexander Duyck 562fd11a83dSAlexander Duyck if (likely(skb)) { 563fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 564fd11a83dSAlexander Duyck skb->dev = napi->dev; 565fd11a83dSAlexander Duyck } 566fd11a83dSAlexander Duyck 567fd11a83dSAlexander Duyck return skb; 568fd11a83dSAlexander Duyck } 569fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 570fd11a83dSAlexander Duyck 571654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 57250269e19SEric Dumazet int size, unsigned int truesize) 573654bed16SPeter Zijlstra { 574654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 575654bed16SPeter Zijlstra skb->len += size; 576654bed16SPeter Zijlstra skb->data_len += size; 57750269e19SEric Dumazet skb->truesize += truesize; 578654bed16SPeter Zijlstra } 579654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 580654bed16SPeter Zijlstra 581f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 582f8e617e1SJason Wang unsigned int truesize) 583f8e617e1SJason Wang { 584f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 585f8e617e1SJason Wang 586f8e617e1SJason Wang skb_frag_size_add(frag, size); 587f8e617e1SJason Wang skb->len += size; 588f8e617e1SJason Wang skb->data_len += size; 589f8e617e1SJason Wang skb->truesize += truesize; 590f8e617e1SJason Wang } 591f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 592f8e617e1SJason Wang 59327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5941da177e4SLinus Torvalds { 595bd8a7036SEric Dumazet kfree_skb_list(*listp); 59627b437c8SHerbert Xu *listp = NULL; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 59927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 60027b437c8SHerbert Xu { 60127b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 60227b437c8SHerbert Xu } 60327b437c8SHerbert Xu 6041da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 6051da177e4SLinus Torvalds { 6061da177e4SLinus Torvalds struct sk_buff *list; 6071da177e4SLinus Torvalds 608fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 6091da177e4SLinus Torvalds skb_get(list); 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 612d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 613d3836f21SEric Dumazet { 614d3836f21SEric Dumazet if (skb->head_frag) 615d3836f21SEric Dumazet put_page(virt_to_head_page(skb->head)); 616d3836f21SEric Dumazet else 617d3836f21SEric Dumazet kfree(skb->head); 618d3836f21SEric Dumazet } 619d3836f21SEric Dumazet 6205bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 6211da177e4SLinus Torvalds { 622ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 6231da177e4SLinus Torvalds int i; 624ff04a771SEric Dumazet 625ff04a771SEric Dumazet if (skb->cloned && 626ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 627ff04a771SEric Dumazet &shinfo->dataref)) 628ff04a771SEric Dumazet return; 629ff04a771SEric Dumazet 630ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 631ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 6321da177e4SLinus Torvalds 633a6686f2fSShirley Ma /* 634a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 635a6686f2fSShirley Ma * the lower device DMA has done; 636a6686f2fSShirley Ma */ 637ff04a771SEric Dumazet if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { 638a6686f2fSShirley Ma struct ubuf_info *uarg; 639a6686f2fSShirley Ma 640ff04a771SEric Dumazet uarg = shinfo->destructor_arg; 641a6686f2fSShirley Ma if (uarg->callback) 642e19d6763SMichael S. Tsirkin uarg->callback(uarg, true); 643a6686f2fSShirley Ma } 644a6686f2fSShirley Ma 645ff04a771SEric Dumazet if (shinfo->frag_list) 646ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6471da177e4SLinus Torvalds 648d3836f21SEric Dumazet skb_free_head(skb); 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds /* 6521da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6531da177e4SLinus Torvalds */ 6542d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6551da177e4SLinus Torvalds { 656d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 657d179cd12SDavid S. Miller 658d179cd12SDavid S. Miller switch (skb->fclone) { 659d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6601da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 6616ffe75ebSEric Dumazet return; 662d179cd12SDavid S. Miller 663d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 664d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 6656ffe75ebSEric Dumazet 6666ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 6676ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 6686ffe75ebSEric Dumazet * while here, branch prediction will be good. 6696ffe75ebSEric Dumazet */ 6706ffe75ebSEric Dumazet if (atomic_read(&fclones->fclone_ref) == 1) 6716ffe75ebSEric Dumazet goto fastpath; 672d179cd12SDavid S. Miller break; 673d179cd12SDavid S. Miller 6746ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 675d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 676d179cd12SDavid S. Miller break; 6773ff50b79SStephen Hemminger } 6786ffe75ebSEric Dumazet if (!atomic_dec_and_test(&fclones->fclone_ref)) 6796ffe75ebSEric Dumazet return; 6806ffe75ebSEric Dumazet fastpath: 6816ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds 68404a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 6851da177e4SLinus Torvalds { 686adf30907SEric Dumazet skb_dst_drop(skb); 6871da177e4SLinus Torvalds #ifdef CONFIG_XFRM 6881da177e4SLinus Torvalds secpath_put(skb->sp); 6891da177e4SLinus Torvalds #endif 6901da177e4SLinus Torvalds if (skb->destructor) { 6919c2b3328SStephen Hemminger WARN_ON(in_irq()); 6921da177e4SLinus Torvalds skb->destructor(skb); 6931da177e4SLinus Torvalds } 694a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 6955f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 6962fc72c7bSKOVACS Krisztian #endif 6971109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 6981da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 6991da177e4SLinus Torvalds #endif 70004a4bb55SLennert Buytenhek } 70104a4bb55SLennert Buytenhek 70204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 70304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 70404a4bb55SLennert Buytenhek { 70504a4bb55SLennert Buytenhek skb_release_head_state(skb); 7065e71d9d7SPablo Neira if (likely(skb->head)) 7072d4baff8SHerbert Xu skb_release_data(skb); 7082d4baff8SHerbert Xu } 7091da177e4SLinus Torvalds 7102d4baff8SHerbert Xu /** 7112d4baff8SHerbert Xu * __kfree_skb - private function 7122d4baff8SHerbert Xu * @skb: buffer 7132d4baff8SHerbert Xu * 7142d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 7152d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 7162d4baff8SHerbert Xu * always call kfree_skb 7172d4baff8SHerbert Xu */ 7182d4baff8SHerbert Xu 7192d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 7202d4baff8SHerbert Xu { 7212d4baff8SHerbert Xu skb_release_all(skb); 7221da177e4SLinus Torvalds kfree_skbmem(skb); 7231da177e4SLinus Torvalds } 724b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds /** 727231d06aeSJörn Engel * kfree_skb - free an sk_buff 728231d06aeSJörn Engel * @skb: buffer to free 729231d06aeSJörn Engel * 730231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 731231d06aeSJörn Engel * hit zero. 732231d06aeSJörn Engel */ 733231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 734231d06aeSJörn Engel { 735231d06aeSJörn Engel if (unlikely(!skb)) 736231d06aeSJörn Engel return; 737231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 738231d06aeSJörn Engel smp_rmb(); 739231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 740231d06aeSJörn Engel return; 741ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 742231d06aeSJörn Engel __kfree_skb(skb); 743231d06aeSJörn Engel } 744b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 745231d06aeSJörn Engel 746bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 747bd8a7036SEric Dumazet { 748bd8a7036SEric Dumazet while (segs) { 749bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 750bd8a7036SEric Dumazet 751bd8a7036SEric Dumazet kfree_skb(segs); 752bd8a7036SEric Dumazet segs = next; 753bd8a7036SEric Dumazet } 754bd8a7036SEric Dumazet } 755bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 756bd8a7036SEric Dumazet 757d1a203eaSStephen Hemminger /** 75825121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 75925121173SMichael S. Tsirkin * @skb: buffer that triggered an error 76025121173SMichael S. Tsirkin * 76125121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 76225121173SMichael S. Tsirkin * skb must be freed afterwards. 76325121173SMichael S. Tsirkin */ 76425121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 76525121173SMichael S. Tsirkin { 76625121173SMichael S. Tsirkin if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 76725121173SMichael S. Tsirkin struct ubuf_info *uarg; 76825121173SMichael S. Tsirkin 76925121173SMichael S. Tsirkin uarg = skb_shinfo(skb)->destructor_arg; 77025121173SMichael S. Tsirkin if (uarg->callback) 77125121173SMichael S. Tsirkin uarg->callback(uarg, false); 77225121173SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 77325121173SMichael S. Tsirkin } 77425121173SMichael S. Tsirkin } 77525121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 77625121173SMichael S. Tsirkin 77725121173SMichael S. Tsirkin /** 778ead2ceb0SNeil Horman * consume_skb - free an skbuff 779ead2ceb0SNeil Horman * @skb: buffer to free 780ead2ceb0SNeil Horman * 781ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 782ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 783ead2ceb0SNeil Horman * is being dropped after a failure and notes that 784ead2ceb0SNeil Horman */ 785ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 786ead2ceb0SNeil Horman { 787ead2ceb0SNeil Horman if (unlikely(!skb)) 788ead2ceb0SNeil Horman return; 789ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 790ead2ceb0SNeil Horman smp_rmb(); 791ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 792ead2ceb0SNeil Horman return; 79307dc22e7SKoki Sanagi trace_consume_skb(skb); 794ead2ceb0SNeil Horman __kfree_skb(skb); 795ead2ceb0SNeil Horman } 796ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 797ead2ceb0SNeil Horman 798b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 799b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 800b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 801b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 802b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 803b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 804b1937227SEric Dumazet 805dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 806dec18810SHerbert Xu { 807dec18810SHerbert Xu new->tstamp = old->tstamp; 808b1937227SEric Dumazet /* We do not copy old->sk */ 809dec18810SHerbert Xu new->dev = old->dev; 810b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 8117fee226aSEric Dumazet skb_dst_copy(new, old); 812def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 813dec18810SHerbert Xu new->sp = secpath_get(old->sp); 814dec18810SHerbert Xu #endif 815b1937227SEric Dumazet __nf_copy(new, old, false); 8166aa895b0SPatrick McHardy 817b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 818b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 819b1937227SEric Dumazet */ 820b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 82106021292SEliezer Tamir 822b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 823b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 824b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 825b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 826b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 827b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 828b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 829b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 830b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 831b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 832b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 833b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 834b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 835b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 836b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 837b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 838b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 839b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 840b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 841b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 842b1937227SEric Dumazet #endif 843e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 844b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 84506021292SEliezer Tamir #endif 8462bd82484SEric Dumazet #ifdef CONFIG_XPS 8472bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 8482bd82484SEric Dumazet #endif 849b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 850b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 851b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT 852b1937227SEric Dumazet CHECK_SKB_FIELD(tc_verd); 853b1937227SEric Dumazet #endif 854b1937227SEric Dumazet #endif 855b1937227SEric Dumazet 856dec18810SHerbert Xu } 857dec18810SHerbert Xu 85882c49a35SHerbert Xu /* 85982c49a35SHerbert Xu * You should not add any new code to this function. Add it to 86082c49a35SHerbert Xu * __copy_skb_header above instead. 86182c49a35SHerbert Xu */ 862e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds #define C(x) n->x = skb->x 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds n->next = n->prev = NULL; 8671da177e4SLinus Torvalds n->sk = NULL; 868dec18810SHerbert Xu __copy_skb_header(n, skb); 869dec18810SHerbert Xu 8701da177e4SLinus Torvalds C(len); 8711da177e4SLinus Torvalds C(data_len); 8723e6b3b2eSAlexey Dobriyan C(mac_len); 873334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 87402f1c89dSPaul Moore n->cloned = 1; 8751da177e4SLinus Torvalds n->nohdr = 0; 8761da177e4SLinus Torvalds n->destructor = NULL; 8771da177e4SLinus Torvalds C(tail); 8781da177e4SLinus Torvalds C(end); 87902f1c89dSPaul Moore C(head); 880d3836f21SEric Dumazet C(head_frag); 88102f1c89dSPaul Moore C(data); 88202f1c89dSPaul Moore C(truesize); 88302f1c89dSPaul Moore atomic_set(&n->users, 1); 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 8861da177e4SLinus Torvalds skb->cloned = 1; 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds return n; 889e0053ec0SHerbert Xu #undef C 890e0053ec0SHerbert Xu } 891e0053ec0SHerbert Xu 892e0053ec0SHerbert Xu /** 893e0053ec0SHerbert Xu * skb_morph - morph one skb into another 894e0053ec0SHerbert Xu * @dst: the skb to receive the contents 895e0053ec0SHerbert Xu * @src: the skb to supply the contents 896e0053ec0SHerbert Xu * 897e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 898e0053ec0SHerbert Xu * supplied by the user. 899e0053ec0SHerbert Xu * 900e0053ec0SHerbert Xu * The target skb is returned upon exit. 901e0053ec0SHerbert Xu */ 902e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 903e0053ec0SHerbert Xu { 9042d4baff8SHerbert Xu skb_release_all(dst); 905e0053ec0SHerbert Xu return __skb_clone(dst, src); 906e0053ec0SHerbert Xu } 907e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 908e0053ec0SHerbert Xu 9092c53040fSBen Hutchings /** 9102c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 91148c83012SMichael S. Tsirkin * @skb: the skb to modify 91248c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 91348c83012SMichael S. Tsirkin * 91448c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 91548c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 91648c83012SMichael S. Tsirkin * to userspace pages. 91748c83012SMichael S. Tsirkin * 91848c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 91948c83012SMichael S. Tsirkin * %GFP_ATOMIC. 92048c83012SMichael S. Tsirkin * 92148c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 92248c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 92348c83012SMichael S. Tsirkin */ 92448c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 925a6686f2fSShirley Ma { 926a6686f2fSShirley Ma int i; 927a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 928a6686f2fSShirley Ma struct page *page, *head = NULL; 929a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 930a6686f2fSShirley Ma 931a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 932a6686f2fSShirley Ma u8 *vaddr; 933a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 934a6686f2fSShirley Ma 93502756ed4SKrishna Kumar page = alloc_page(gfp_mask); 936a6686f2fSShirley Ma if (!page) { 937a6686f2fSShirley Ma while (head) { 93840dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 939a6686f2fSShirley Ma put_page(head); 940a6686f2fSShirley Ma head = next; 941a6686f2fSShirley Ma } 942a6686f2fSShirley Ma return -ENOMEM; 943a6686f2fSShirley Ma } 94451c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 945a6686f2fSShirley Ma memcpy(page_address(page), 9469e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 94751c56b00SEric Dumazet kunmap_atomic(vaddr); 94840dadff2SSunghan Suh set_page_private(page, (unsigned long)head); 949a6686f2fSShirley Ma head = page; 950a6686f2fSShirley Ma } 951a6686f2fSShirley Ma 952a6686f2fSShirley Ma /* skb frags release userspace buffers */ 95302756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 954a8605c60SIan Campbell skb_frag_unref(skb, i); 955a6686f2fSShirley Ma 956e19d6763SMichael S. Tsirkin uarg->callback(uarg, false); 957a6686f2fSShirley Ma 958a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 95902756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 96002756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 96102756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 96240dadff2SSunghan Suh head = (struct page *)page_private(head); 963a6686f2fSShirley Ma } 96448c83012SMichael S. Tsirkin 96548c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 966a6686f2fSShirley Ma return 0; 967a6686f2fSShirley Ma } 968dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 969a6686f2fSShirley Ma 970e0053ec0SHerbert Xu /** 971e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 972e0053ec0SHerbert Xu * @skb: buffer to clone 973e0053ec0SHerbert Xu * @gfp_mask: allocation priority 974e0053ec0SHerbert Xu * 975e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 976e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 977e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 978e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 979e0053ec0SHerbert Xu * 980e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 981e0053ec0SHerbert Xu * %GFP_ATOMIC. 982e0053ec0SHerbert Xu */ 983e0053ec0SHerbert Xu 984e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 985e0053ec0SHerbert Xu { 986d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 987d0bf4a9eSEric Dumazet struct sk_buff_fclones, 988d0bf4a9eSEric Dumazet skb1); 9896ffe75ebSEric Dumazet struct sk_buff *n; 990e0053ec0SHerbert Xu 99170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 992a6686f2fSShirley Ma return NULL; 993a6686f2fSShirley Ma 994e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 9956ffe75ebSEric Dumazet atomic_read(&fclones->fclone_ref) == 1) { 9966ffe75ebSEric Dumazet n = &fclones->skb2; 9976ffe75ebSEric Dumazet atomic_set(&fclones->fclone_ref, 2); 998e0053ec0SHerbert Xu } else { 999c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1000c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1001c93bdd0eSMel Gorman 1002e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1003e0053ec0SHerbert Xu if (!n) 1004e0053ec0SHerbert Xu return NULL; 1005fe55f6d5SVegard Nossum 1006fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 1007e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1008e0053ec0SHerbert Xu } 1009e0053ec0SHerbert Xu 1010e0053ec0SHerbert Xu return __skb_clone(n, skb); 10111da177e4SLinus Torvalds } 1012b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 10131da177e4SLinus Torvalds 1014f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 1015f5b17294SPravin B Shelar { 1016030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1017030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1018030737bcSEric Dumazet skb->csum_start += off; 1019f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1020f5b17294SPravin B Shelar skb->transport_header += off; 1021f5b17294SPravin B Shelar skb->network_header += off; 1022f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1023f5b17294SPravin B Shelar skb->mac_header += off; 1024f5b17294SPravin B Shelar skb->inner_transport_header += off; 1025f5b17294SPravin B Shelar skb->inner_network_header += off; 1026aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1027f5b17294SPravin B Shelar } 1028f5b17294SPravin B Shelar 10291da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 10301da177e4SLinus Torvalds { 1031dec18810SHerbert Xu __copy_skb_header(new, old); 1032dec18810SHerbert Xu 10337967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 10347967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 10357967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 1038c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1039c93bdd0eSMel Gorman { 1040c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1041c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1042c93bdd0eSMel Gorman return 0; 1043c93bdd0eSMel Gorman } 1044c93bdd0eSMel Gorman 10451da177e4SLinus Torvalds /** 10461da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 10471da177e4SLinus Torvalds * @skb: buffer to copy 10481da177e4SLinus Torvalds * @gfp_mask: allocation priority 10491da177e4SLinus Torvalds * 10501da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 10511da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 10521da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 10531da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 10541da177e4SLinus Torvalds * 10551da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 10561da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 10571da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 10581da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 10591da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 10601da177e4SLinus Torvalds */ 10611da177e4SLinus Torvalds 1062dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 10631da177e4SLinus Torvalds { 10646602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1065ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1066c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1067c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 10686602cebbSEric Dumazet 10691da177e4SLinus Torvalds if (!n) 10701da177e4SLinus Torvalds return NULL; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds /* Set the data pointer */ 10731da177e4SLinus Torvalds skb_reserve(n, headerlen); 10741da177e4SLinus Torvalds /* Set the tail pointer and length */ 10751da177e4SLinus Torvalds skb_put(n, skb->len); 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 10781da177e4SLinus Torvalds BUG(); 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds copy_skb_header(n, skb); 10811da177e4SLinus Torvalds return n; 10821da177e4SLinus Torvalds } 1083b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds /** 1086bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 10871da177e4SLinus Torvalds * @skb: buffer to copy 1088117632e6SEric Dumazet * @headroom: headroom of new skb 10891da177e4SLinus Torvalds * @gfp_mask: allocation priority 1090bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1091bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1092bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 10931da177e4SLinus Torvalds * 10941da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 10951da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 10961da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 10971da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 10981da177e4SLinus Torvalds * or the pointer to the buffer on success. 10991da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 11001da177e4SLinus Torvalds */ 11011da177e4SLinus Torvalds 1102bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1103bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 11041da177e4SLinus Torvalds { 1105117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1106bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1107bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 11086602cebbSEric Dumazet 11091da177e4SLinus Torvalds if (!n) 11101da177e4SLinus Torvalds goto out; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds /* Set the data pointer */ 1113117632e6SEric Dumazet skb_reserve(n, headroom); 11141da177e4SLinus Torvalds /* Set the tail pointer and length */ 11151da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 11161da177e4SLinus Torvalds /* Copy the bytes */ 1117d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 11181da177e4SLinus Torvalds 111925f484a6SHerbert Xu n->truesize += skb->data_len; 11201da177e4SLinus Torvalds n->data_len = skb->data_len; 11211da177e4SLinus Torvalds n->len = skb->len; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 11241da177e4SLinus Torvalds int i; 11251da177e4SLinus Torvalds 112670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 11271511022cSDan Carpenter kfree_skb(n); 11281511022cSDan Carpenter n = NULL; 1129a6686f2fSShirley Ma goto out; 1130a6686f2fSShirley Ma } 11311da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11321da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1133ea2ab693SIan Campbell skb_frag_ref(skb, i); 11341da177e4SLinus Torvalds } 11351da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 113821dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 11391da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 11401da177e4SLinus Torvalds skb_clone_fraglist(n); 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds copy_skb_header(n, skb); 11441da177e4SLinus Torvalds out: 11451da177e4SLinus Torvalds return n; 11461da177e4SLinus Torvalds } 1147bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds /** 11501da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 11511da177e4SLinus Torvalds * @skb: buffer to reallocate 11521da177e4SLinus Torvalds * @nhead: room to add at head 11531da177e4SLinus Torvalds * @ntail: room to add at tail 11541da177e4SLinus Torvalds * @gfp_mask: allocation priority 11551da177e4SLinus Torvalds * 1156bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1157bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 11581da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 11591da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 11601da177e4SLinus Torvalds * 11611da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 11621da177e4SLinus Torvalds * reloaded after call to this function. 11631da177e4SLinus Torvalds */ 11641da177e4SLinus Torvalds 116586a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1166dd0fc66fSAl Viro gfp_t gfp_mask) 11671da177e4SLinus Torvalds { 11681da177e4SLinus Torvalds int i; 11691da177e4SLinus Torvalds u8 *data; 1170ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 11711da177e4SLinus Torvalds long off; 11721da177e4SLinus Torvalds 11734edd87adSHerbert Xu BUG_ON(nhead < 0); 11744edd87adSHerbert Xu 11751da177e4SLinus Torvalds if (skb_shared(skb)) 11761da177e4SLinus Torvalds BUG(); 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 11791da177e4SLinus Torvalds 1180c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1181c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1182c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1183c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 11841da177e4SLinus Torvalds if (!data) 11851da177e4SLinus Torvalds goto nodata; 118687151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 11896602cebbSEric Dumazet * optimized for the cases when header is void. 11906602cebbSEric Dumazet */ 11916602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 11926602cebbSEric Dumazet 11936602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 11946602cebbSEric Dumazet skb_shinfo(skb), 1195fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 11961da177e4SLinus Torvalds 11973e24591aSAlexander Duyck /* 11983e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 11993e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 12003e24591aSAlexander Duyck * be since all we did is relocate the values 12013e24591aSAlexander Duyck */ 12023e24591aSAlexander Duyck if (skb_cloned(skb)) { 1203a6686f2fSShirley Ma /* copy this zero copy skb frags */ 120470008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1205a6686f2fSShirley Ma goto nofrags; 12061da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1207ea2ab693SIan Campbell skb_frag_ref(skb, i); 12081da177e4SLinus Torvalds 120921dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 12101da177e4SLinus Torvalds skb_clone_fraglist(skb); 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds skb_release_data(skb); 12133e24591aSAlexander Duyck } else { 12143e24591aSAlexander Duyck skb_free_head(skb); 12151fd63041SEric Dumazet } 12161da177e4SLinus Torvalds off = (data + nhead) - skb->head; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds skb->head = data; 1219d3836f21SEric Dumazet skb->head_frag = 0; 12201da177e4SLinus Torvalds skb->data += off; 12214305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 12224305b541SArnaldo Carvalho de Melo skb->end = size; 122356eb8882SPatrick McHardy off = nhead; 12244305b541SArnaldo Carvalho de Melo #else 12254305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 122656eb8882SPatrick McHardy #endif 122727a884dcSArnaldo Carvalho de Melo skb->tail += off; 1228b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 12291da177e4SLinus Torvalds skb->cloned = 0; 1230334a8132SPatrick McHardy skb->hdr_len = 0; 12311da177e4SLinus Torvalds skb->nohdr = 0; 12321da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 12331da177e4SLinus Torvalds return 0; 12341da177e4SLinus Torvalds 1235a6686f2fSShirley Ma nofrags: 1236a6686f2fSShirley Ma kfree(data); 12371da177e4SLinus Torvalds nodata: 12381da177e4SLinus Torvalds return -ENOMEM; 12391da177e4SLinus Torvalds } 1240b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 12451da177e4SLinus Torvalds { 12461da177e4SLinus Torvalds struct sk_buff *skb2; 12471da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds if (delta <= 0) 12501da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 12511da177e4SLinus Torvalds else { 12521da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 12531da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 12541da177e4SLinus Torvalds GFP_ATOMIC)) { 12551da177e4SLinus Torvalds kfree_skb(skb2); 12561da177e4SLinus Torvalds skb2 = NULL; 12571da177e4SLinus Torvalds } 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds return skb2; 12601da177e4SLinus Torvalds } 1261b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds /** 12641da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 12651da177e4SLinus Torvalds * @skb: buffer to copy 12661da177e4SLinus Torvalds * @newheadroom: new free bytes at head 12671da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 12681da177e4SLinus Torvalds * @gfp_mask: allocation priority 12691da177e4SLinus Torvalds * 12701da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 12711da177e4SLinus Torvalds * allocate additional space. 12721da177e4SLinus Torvalds * 12731da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 12741da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 12751da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 12761da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 12771da177e4SLinus Torvalds * 12781da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 12791da177e4SLinus Torvalds * is called from an interrupt. 12801da177e4SLinus Torvalds */ 12811da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 128286a76cafSVictor Fusco int newheadroom, int newtailroom, 1283dd0fc66fSAl Viro gfp_t gfp_mask) 12841da177e4SLinus Torvalds { 12851da177e4SLinus Torvalds /* 12861da177e4SLinus Torvalds * Allocate the copy buffer 12871da177e4SLinus Torvalds */ 1288c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1289c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1290c93bdd0eSMel Gorman NUMA_NO_NODE); 1291efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 12921da177e4SLinus Torvalds int head_copy_len, head_copy_off; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds if (!n) 12951da177e4SLinus Torvalds return NULL; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds skb_reserve(n, newheadroom); 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* Set the tail pointer and length */ 13001da177e4SLinus Torvalds skb_put(n, skb->len); 13011da177e4SLinus Torvalds 1302efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 13031da177e4SLinus Torvalds head_copy_off = 0; 13041da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 13051da177e4SLinus Torvalds head_copy_len = newheadroom; 13061da177e4SLinus Torvalds else 13071da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds /* Copy the linear header and data. */ 13101da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 13111da177e4SLinus Torvalds skb->len + head_copy_len)) 13121da177e4SLinus Torvalds BUG(); 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds copy_skb_header(n, skb); 13151da177e4SLinus Torvalds 1316030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1317efd1e8d5SPatrick McHardy 13181da177e4SLinus Torvalds return n; 13191da177e4SLinus Torvalds } 1320b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /** 13231da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 13241da177e4SLinus Torvalds * @skb: buffer to pad 13251da177e4SLinus Torvalds * @pad: space to pad 13261da177e4SLinus Torvalds * 13271da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 13281da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 13291da177e4SLinus Torvalds * beyond the buffer end onto the wire. 13301da177e4SLinus Torvalds * 13315b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 13321da177e4SLinus Torvalds */ 13331da177e4SLinus Torvalds 13345b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 13351da177e4SLinus Torvalds { 13365b057c6bSHerbert Xu int err; 13375b057c6bSHerbert Xu int ntail; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 13405b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 13411da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 13425b057c6bSHerbert Xu return 0; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13454305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 13465b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 13475b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 13485b057c6bSHerbert Xu if (unlikely(err)) 13495b057c6bSHerbert Xu goto free_skb; 13505b057c6bSHerbert Xu } 13515b057c6bSHerbert Xu 13525b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 13535b057c6bSHerbert Xu * to be audited. 13545b057c6bSHerbert Xu */ 13555b057c6bSHerbert Xu err = skb_linearize(skb); 13565b057c6bSHerbert Xu if (unlikely(err)) 13575b057c6bSHerbert Xu goto free_skb; 13585b057c6bSHerbert Xu 13595b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 13605b057c6bSHerbert Xu return 0; 13615b057c6bSHerbert Xu 13625b057c6bSHerbert Xu free_skb: 13631da177e4SLinus Torvalds kfree_skb(skb); 13645b057c6bSHerbert Xu return err; 13651da177e4SLinus Torvalds } 1366b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 13671da177e4SLinus Torvalds 13680dde3e16SIlpo Järvinen /** 13690c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 13700c7ddf36SMathias Krause * @skb: start of the buffer to use 13710c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 13720c7ddf36SMathias Krause * @len: amount of data to add 13730c7ddf36SMathias Krause * 13740c7ddf36SMathias Krause * This function extends the used data area of the potentially 13750c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 13760c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 13770c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 13780c7ddf36SMathias Krause * returned. 13790c7ddf36SMathias Krause */ 13800c7ddf36SMathias Krause 13810c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 13820c7ddf36SMathias Krause { 13830c7ddf36SMathias Krause if (tail != skb) { 13840c7ddf36SMathias Krause skb->data_len += len; 13850c7ddf36SMathias Krause skb->len += len; 13860c7ddf36SMathias Krause } 13870c7ddf36SMathias Krause return skb_put(tail, len); 13880c7ddf36SMathias Krause } 13890c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 13900c7ddf36SMathias Krause 13910c7ddf36SMathias Krause /** 13920dde3e16SIlpo Järvinen * skb_put - add data to a buffer 13930dde3e16SIlpo Järvinen * @skb: buffer to use 13940dde3e16SIlpo Järvinen * @len: amount of data to add 13950dde3e16SIlpo Järvinen * 13960dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 13970dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 13980dde3e16SIlpo Järvinen * first byte of the extra data is returned. 13990dde3e16SIlpo Järvinen */ 14000dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 14010dde3e16SIlpo Järvinen { 14020dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 14030dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 14040dde3e16SIlpo Järvinen skb->tail += len; 14050dde3e16SIlpo Järvinen skb->len += len; 14060dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 14070dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 14080dde3e16SIlpo Järvinen return tmp; 14090dde3e16SIlpo Järvinen } 14100dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 14110dde3e16SIlpo Järvinen 14126be8ac2fSIlpo Järvinen /** 1413c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1414c2aa270aSIlpo Järvinen * @skb: buffer to use 1415c2aa270aSIlpo Järvinen * @len: amount of data to add 1416c2aa270aSIlpo Järvinen * 1417c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1418c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1419c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1420c2aa270aSIlpo Järvinen */ 1421c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1422c2aa270aSIlpo Järvinen { 1423c2aa270aSIlpo Järvinen skb->data -= len; 1424c2aa270aSIlpo Järvinen skb->len += len; 1425c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1426c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1427c2aa270aSIlpo Järvinen return skb->data; 1428c2aa270aSIlpo Järvinen } 1429c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1430c2aa270aSIlpo Järvinen 1431c2aa270aSIlpo Järvinen /** 14326be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 14336be8ac2fSIlpo Järvinen * @skb: buffer to use 14346be8ac2fSIlpo Järvinen * @len: amount of data to remove 14356be8ac2fSIlpo Järvinen * 14366be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 14376be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 14386be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 14396be8ac2fSIlpo Järvinen * the old data. 14406be8ac2fSIlpo Järvinen */ 14416be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 14426be8ac2fSIlpo Järvinen { 144347d29646SDavid S. Miller return skb_pull_inline(skb, len); 14446be8ac2fSIlpo Järvinen } 14456be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 14466be8ac2fSIlpo Järvinen 1447419ae74eSIlpo Järvinen /** 1448419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1449419ae74eSIlpo Järvinen * @skb: buffer to alter 1450419ae74eSIlpo Järvinen * @len: new length 1451419ae74eSIlpo Järvinen * 1452419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1453419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1454419ae74eSIlpo Järvinen * The skb must be linear. 1455419ae74eSIlpo Järvinen */ 1456419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1457419ae74eSIlpo Järvinen { 1458419ae74eSIlpo Järvinen if (skb->len > len) 1459419ae74eSIlpo Järvinen __skb_trim(skb, len); 1460419ae74eSIlpo Järvinen } 1461419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1462419ae74eSIlpo Järvinen 14633cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 14641da177e4SLinus Torvalds */ 14651da177e4SLinus Torvalds 14663cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 14671da177e4SLinus Torvalds { 146827b437c8SHerbert Xu struct sk_buff **fragp; 146927b437c8SHerbert Xu struct sk_buff *frag; 14701da177e4SLinus Torvalds int offset = skb_headlen(skb); 14711da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 14721da177e4SLinus Torvalds int i; 147327b437c8SHerbert Xu int err; 147427b437c8SHerbert Xu 147527b437c8SHerbert Xu if (skb_cloned(skb) && 147627b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 147727b437c8SHerbert Xu return err; 14781da177e4SLinus Torvalds 1479f4d26fb3SHerbert Xu i = 0; 1480f4d26fb3SHerbert Xu if (offset >= len) 1481f4d26fb3SHerbert Xu goto drop_pages; 1482f4d26fb3SHerbert Xu 1483f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 14849e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 148527b437c8SHerbert Xu 148627b437c8SHerbert Xu if (end < len) { 14871da177e4SLinus Torvalds offset = end; 148827b437c8SHerbert Xu continue; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14919e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 149227b437c8SHerbert Xu 1493f4d26fb3SHerbert Xu drop_pages: 149427b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 149527b437c8SHerbert Xu 149627b437c8SHerbert Xu for (; i < nfrags; i++) 1497ea2ab693SIan Campbell skb_frag_unref(skb, i); 149827b437c8SHerbert Xu 149921dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 150027b437c8SHerbert Xu skb_drop_fraglist(skb); 1501f4d26fb3SHerbert Xu goto done; 150227b437c8SHerbert Xu } 150327b437c8SHerbert Xu 150427b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 150527b437c8SHerbert Xu fragp = &frag->next) { 150627b437c8SHerbert Xu int end = offset + frag->len; 150727b437c8SHerbert Xu 150827b437c8SHerbert Xu if (skb_shared(frag)) { 150927b437c8SHerbert Xu struct sk_buff *nfrag; 151027b437c8SHerbert Xu 151127b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 151227b437c8SHerbert Xu if (unlikely(!nfrag)) 151327b437c8SHerbert Xu return -ENOMEM; 151427b437c8SHerbert Xu 151527b437c8SHerbert Xu nfrag->next = frag->next; 151685bb2a60SEric Dumazet consume_skb(frag); 151727b437c8SHerbert Xu frag = nfrag; 151827b437c8SHerbert Xu *fragp = frag; 151927b437c8SHerbert Xu } 152027b437c8SHerbert Xu 152127b437c8SHerbert Xu if (end < len) { 152227b437c8SHerbert Xu offset = end; 152327b437c8SHerbert Xu continue; 152427b437c8SHerbert Xu } 152527b437c8SHerbert Xu 152627b437c8SHerbert Xu if (end > len && 152727b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 152827b437c8SHerbert Xu return err; 152927b437c8SHerbert Xu 153027b437c8SHerbert Xu if (frag->next) 153127b437c8SHerbert Xu skb_drop_list(&frag->next); 153227b437c8SHerbert Xu break; 153327b437c8SHerbert Xu } 153427b437c8SHerbert Xu 1535f4d26fb3SHerbert Xu done: 153627b437c8SHerbert Xu if (len > skb_headlen(skb)) { 15371da177e4SLinus Torvalds skb->data_len -= skb->len - len; 15381da177e4SLinus Torvalds skb->len = len; 15391da177e4SLinus Torvalds } else { 15401da177e4SLinus Torvalds skb->len = len; 15411da177e4SLinus Torvalds skb->data_len = 0; 154227a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds return 0; 15461da177e4SLinus Torvalds } 1547b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds /** 15501da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 15511da177e4SLinus Torvalds * @skb: buffer to reallocate 15521da177e4SLinus Torvalds * @delta: number of bytes to advance tail 15531da177e4SLinus Torvalds * 15541da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 15551da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 15561da177e4SLinus Torvalds * data from fragmented part. 15571da177e4SLinus Torvalds * 15581da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 15591da177e4SLinus Torvalds * 15601da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 15611da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 15621da177e4SLinus Torvalds * 15631da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 15641da177e4SLinus Torvalds * reloaded after call to this function. 15651da177e4SLinus Torvalds */ 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 15681da177e4SLinus Torvalds * when it is necessary. 15691da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 15701da177e4SLinus Torvalds * 2. It may change skb pointers. 15711da177e4SLinus Torvalds * 15721da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 15731da177e4SLinus Torvalds */ 15741da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 15751da177e4SLinus Torvalds { 15761da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 15771da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 15781da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 15791da177e4SLinus Torvalds */ 15804305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 15831da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 15841da177e4SLinus Torvalds GFP_ATOMIC)) 15851da177e4SLinus Torvalds return NULL; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 158827a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 15891da177e4SLinus Torvalds BUG(); 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 15921da177e4SLinus Torvalds * size of pulled pages. Superb. 15931da177e4SLinus Torvalds */ 159421dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 15951da177e4SLinus Torvalds goto pull_pages; 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 15981da177e4SLinus Torvalds eat = delta; 15991da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16009e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 16019e903e08SEric Dumazet 16029e903e08SEric Dumazet if (size >= eat) 16031da177e4SLinus Torvalds goto pull_pages; 16049e903e08SEric Dumazet eat -= size; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 16081da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 16091da177e4SLinus Torvalds * but taking into account that pulling is expected to 16101da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 16111da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 16121da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 16131da177e4SLinus Torvalds */ 16141da177e4SLinus Torvalds if (eat) { 16151da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 16161da177e4SLinus Torvalds struct sk_buff *clone = NULL; 16171da177e4SLinus Torvalds struct sk_buff *insp = NULL; 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds do { 162009a62660SKris Katterjohn BUG_ON(!list); 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds if (list->len <= eat) { 16231da177e4SLinus Torvalds /* Eaten as whole. */ 16241da177e4SLinus Torvalds eat -= list->len; 16251da177e4SLinus Torvalds list = list->next; 16261da177e4SLinus Torvalds insp = list; 16271da177e4SLinus Torvalds } else { 16281da177e4SLinus Torvalds /* Eaten partially. */ 16291da177e4SLinus Torvalds 16301da177e4SLinus Torvalds if (skb_shared(list)) { 16311da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 16321da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 16331da177e4SLinus Torvalds if (!clone) 16341da177e4SLinus Torvalds return NULL; 16351da177e4SLinus Torvalds insp = list->next; 16361da177e4SLinus Torvalds list = clone; 16371da177e4SLinus Torvalds } else { 16381da177e4SLinus Torvalds /* This may be pulled without 16391da177e4SLinus Torvalds * problems. */ 16401da177e4SLinus Torvalds insp = list; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 16431da177e4SLinus Torvalds kfree_skb(clone); 16441da177e4SLinus Torvalds return NULL; 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds break; 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds } while (eat); 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds /* Free pulled out fragments. */ 16511da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 16521da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 16531da177e4SLinus Torvalds kfree_skb(list); 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds /* And insert new clone at head. */ 16561da177e4SLinus Torvalds if (clone) { 16571da177e4SLinus Torvalds clone->next = list; 16581da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds } 16611da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds pull_pages: 16641da177e4SLinus Torvalds eat = delta; 16651da177e4SLinus Torvalds k = 0; 16661da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16679e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 16689e903e08SEric Dumazet 16699e903e08SEric Dumazet if (size <= eat) { 1670ea2ab693SIan Campbell skb_frag_unref(skb, i); 16719e903e08SEric Dumazet eat -= size; 16721da177e4SLinus Torvalds } else { 16731da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 16741da177e4SLinus Torvalds if (eat) { 16751da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 16769e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 16771da177e4SLinus Torvalds eat = 0; 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds k++; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds skb->tail += delta; 16851da177e4SLinus Torvalds skb->data_len -= delta; 16861da177e4SLinus Torvalds 168727a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 16881da177e4SLinus Torvalds } 1689b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 16901da177e4SLinus Torvalds 169122019b17SEric Dumazet /** 169222019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 169322019b17SEric Dumazet * @skb: source skb 169422019b17SEric Dumazet * @offset: offset in source 169522019b17SEric Dumazet * @to: destination buffer 169622019b17SEric Dumazet * @len: number of bytes to copy 169722019b17SEric Dumazet * 169822019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 169922019b17SEric Dumazet * destination buffer. 170022019b17SEric Dumazet * 170122019b17SEric Dumazet * CAUTION ! : 170222019b17SEric Dumazet * If its prototype is ever changed, 170322019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 170422019b17SEric Dumazet * since it is called from BPF assembly code. 170522019b17SEric Dumazet */ 17061da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 17071da177e4SLinus Torvalds { 17081a028e50SDavid S. Miller int start = skb_headlen(skb); 1709fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1710fbb398a8SDavid S. Miller int i, copy; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds if (offset > (int)skb->len - len) 17131da177e4SLinus Torvalds goto fault; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds /* Copy header. */ 17161a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 17171da177e4SLinus Torvalds if (copy > len) 17181da177e4SLinus Torvalds copy = len; 1719d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 17201da177e4SLinus Torvalds if ((len -= copy) == 0) 17211da177e4SLinus Torvalds return 0; 17221da177e4SLinus Torvalds offset += copy; 17231da177e4SLinus Torvalds to += copy; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 17271a028e50SDavid S. Miller int end; 172851c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17291da177e4SLinus Torvalds 1730547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17311a028e50SDavid S. Miller 173251c56b00SEric Dumazet end = start + skb_frag_size(f); 17331da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17341da177e4SLinus Torvalds u8 *vaddr; 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds if (copy > len) 17371da177e4SLinus Torvalds copy = len; 17381da177e4SLinus Torvalds 173951c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 17401da177e4SLinus Torvalds memcpy(to, 174151c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 174251c56b00SEric Dumazet copy); 174351c56b00SEric Dumazet kunmap_atomic(vaddr); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds if ((len -= copy) == 0) 17461da177e4SLinus Torvalds return 0; 17471da177e4SLinus Torvalds offset += copy; 17481da177e4SLinus Torvalds to += copy; 17491da177e4SLinus Torvalds } 17501a028e50SDavid S. Miller start = end; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 1753fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 17541a028e50SDavid S. Miller int end; 17551da177e4SLinus Torvalds 1756547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17571a028e50SDavid S. Miller 1758fbb398a8SDavid S. Miller end = start + frag_iter->len; 17591da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17601da177e4SLinus Torvalds if (copy > len) 17611da177e4SLinus Torvalds copy = len; 1762fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 17631da177e4SLinus Torvalds goto fault; 17641da177e4SLinus Torvalds if ((len -= copy) == 0) 17651da177e4SLinus Torvalds return 0; 17661da177e4SLinus Torvalds offset += copy; 17671da177e4SLinus Torvalds to += copy; 17681da177e4SLinus Torvalds } 17691a028e50SDavid S. Miller start = end; 17701da177e4SLinus Torvalds } 1771a6686f2fSShirley Ma 17721da177e4SLinus Torvalds if (!len) 17731da177e4SLinus Torvalds return 0; 17741da177e4SLinus Torvalds 17751da177e4SLinus Torvalds fault: 17761da177e4SLinus Torvalds return -EFAULT; 17771da177e4SLinus Torvalds } 1778b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 17791da177e4SLinus Torvalds 17809c55e01cSJens Axboe /* 17819c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 17829c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 17839c55e01cSJens Axboe */ 17849c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 17859c55e01cSJens Axboe { 17868b9d3728SJarek Poplawski put_page(spd->pages[i]); 17878b9d3728SJarek Poplawski } 17889c55e01cSJens Axboe 1789a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 17904fb66994SJarek Poplawski unsigned int *offset, 179118aafc62SEric Dumazet struct sock *sk) 17928b9d3728SJarek Poplawski { 17935640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 17948b9d3728SJarek Poplawski 17955640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 17968b9d3728SJarek Poplawski return NULL; 17974fb66994SJarek Poplawski 17985640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 17994fb66994SJarek Poplawski 18005640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 18015640f768SEric Dumazet page_address(page) + *offset, *len); 18025640f768SEric Dumazet *offset = pfrag->offset; 18035640f768SEric Dumazet pfrag->offset += *len; 18044fb66994SJarek Poplawski 18055640f768SEric Dumazet return pfrag->page; 18069c55e01cSJens Axboe } 18079c55e01cSJens Axboe 180841c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 180941c73a0dSEric Dumazet struct page *page, 181041c73a0dSEric Dumazet unsigned int offset) 181141c73a0dSEric Dumazet { 181241c73a0dSEric Dumazet return spd->nr_pages && 181341c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 181441c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 181541c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 181641c73a0dSEric Dumazet } 181741c73a0dSEric Dumazet 18189c55e01cSJens Axboe /* 18199c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 18209c55e01cSJens Axboe */ 1821a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 182235f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 18234fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 182418aafc62SEric Dumazet bool linear, 18257a67e56fSJarek Poplawski struct sock *sk) 18269c55e01cSJens Axboe { 182741c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1828a108d5f3SDavid S. Miller return true; 18299c55e01cSJens Axboe 18308b9d3728SJarek Poplawski if (linear) { 183118aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 18328b9d3728SJarek Poplawski if (!page) 1833a108d5f3SDavid S. Miller return true; 183441c73a0dSEric Dumazet } 183541c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 183641c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1837a108d5f3SDavid S. Miller return false; 183841c73a0dSEric Dumazet } 18398b9d3728SJarek Poplawski get_page(page); 18409c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 18414fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 18429c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 18439c55e01cSJens Axboe spd->nr_pages++; 18448b9d3728SJarek Poplawski 1845a108d5f3SDavid S. Miller return false; 18469c55e01cSJens Axboe } 18479c55e01cSJens Axboe 1848a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 18492870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 185018aafc62SEric Dumazet unsigned int *len, 1851d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 185235f3d14dSJens Axboe struct sock *sk, 185335f3d14dSJens Axboe struct pipe_inode_info *pipe) 18549c55e01cSJens Axboe { 18552870c43dSOctavian Purdila if (!*len) 1856a108d5f3SDavid S. Miller return true; 18579c55e01cSJens Axboe 18582870c43dSOctavian Purdila /* skip this segment if already processed */ 18592870c43dSOctavian Purdila if (*off >= plen) { 18602870c43dSOctavian Purdila *off -= plen; 1861a108d5f3SDavid S. Miller return false; 18622870c43dSOctavian Purdila } 18632870c43dSOctavian Purdila 18642870c43dSOctavian Purdila /* ignore any bits we already processed */ 18659ca1b22dSEric Dumazet poff += *off; 18669ca1b22dSEric Dumazet plen -= *off; 18672870c43dSOctavian Purdila *off = 0; 18682870c43dSOctavian Purdila 186918aafc62SEric Dumazet do { 187018aafc62SEric Dumazet unsigned int flen = min(*len, plen); 18712870c43dSOctavian Purdila 187218aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 187318aafc62SEric Dumazet linear, sk)) 1874a108d5f3SDavid S. Miller return true; 187518aafc62SEric Dumazet poff += flen; 187618aafc62SEric Dumazet plen -= flen; 18772870c43dSOctavian Purdila *len -= flen; 187818aafc62SEric Dumazet } while (*len && plen); 18792870c43dSOctavian Purdila 1880a108d5f3SDavid S. Miller return false; 1881db43a282SOctavian Purdila } 18829c55e01cSJens Axboe 18839c55e01cSJens Axboe /* 1884a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 18852870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 18869c55e01cSJens Axboe */ 1887a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 188835f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 188935f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 18902870c43dSOctavian Purdila { 18912870c43dSOctavian Purdila int seg; 18929c55e01cSJens Axboe 18931d0c0b32SEric Dumazet /* map the linear part : 18942996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 18952996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 18962996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 18979c55e01cSJens Axboe */ 18982870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 18992870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 19002870c43dSOctavian Purdila skb_headlen(skb), 190118aafc62SEric Dumazet offset, len, spd, 19023a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 19031d0c0b32SEric Dumazet sk, pipe)) 1904a108d5f3SDavid S. Miller return true; 19059c55e01cSJens Axboe 19069c55e01cSJens Axboe /* 19079c55e01cSJens Axboe * then map the fragments 19089c55e01cSJens Axboe */ 19099c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 19109c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 19119c55e01cSJens Axboe 1912ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 19139e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 191418aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 1915a108d5f3SDavid S. Miller return true; 19169c55e01cSJens Axboe } 19179c55e01cSJens Axboe 1918a108d5f3SDavid S. Miller return false; 19199c55e01cSJens Axboe } 19209c55e01cSJens Axboe 19219c55e01cSJens Axboe /* 19229c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 19239c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 19249c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 19259c55e01cSJens Axboe * handle that cleanly. 19269c55e01cSJens Axboe */ 19278b9d3728SJarek Poplawski int skb_splice_bits(struct sk_buff *skb, unsigned int offset, 19289c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 19299c55e01cSJens Axboe unsigned int flags) 19309c55e01cSJens Axboe { 193141c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 193241c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 19339c55e01cSJens Axboe struct splice_pipe_desc spd = { 19349c55e01cSJens Axboe .pages = pages, 19359c55e01cSJens Axboe .partial = partial, 1936047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 19379c55e01cSJens Axboe .flags = flags, 193828a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 19399c55e01cSJens Axboe .spd_release = sock_spd_release, 19409c55e01cSJens Axboe }; 1941fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 19427a67e56fSJarek Poplawski struct sock *sk = skb->sk; 194335f3d14dSJens Axboe int ret = 0; 194435f3d14dSJens Axboe 19459c55e01cSJens Axboe /* 19469c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 19479c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 19489c55e01cSJens Axboe */ 194935f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 19509c55e01cSJens Axboe goto done; 19519c55e01cSJens Axboe else if (!tlen) 19529c55e01cSJens Axboe goto done; 19539c55e01cSJens Axboe 19549c55e01cSJens Axboe /* 19559c55e01cSJens Axboe * now see if we have a frag_list to map 19569c55e01cSJens Axboe */ 1957fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1958fbb398a8SDavid S. Miller if (!tlen) 19599c55e01cSJens Axboe break; 196035f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1961fbb398a8SDavid S. Miller break; 19629c55e01cSJens Axboe } 19639c55e01cSJens Axboe 19649c55e01cSJens Axboe done: 19659c55e01cSJens Axboe if (spd.nr_pages) { 19669c55e01cSJens Axboe /* 19679c55e01cSJens Axboe * Drop the socket lock, otherwise we have reverse 19689c55e01cSJens Axboe * locking dependencies between sk_lock and i_mutex 19699c55e01cSJens Axboe * here as compared to sendfile(). We enter here 19709c55e01cSJens Axboe * with the socket lock held, and splice_to_pipe() will 19719c55e01cSJens Axboe * grab the pipe inode lock. For sendfile() emulation, 19729c55e01cSJens Axboe * we call into ->sendpage() with the i_mutex lock held 19739c55e01cSJens Axboe * and networking will grab the socket lock. 19749c55e01cSJens Axboe */ 1975293ad604SOctavian Purdila release_sock(sk); 19769c55e01cSJens Axboe ret = splice_to_pipe(pipe, &spd); 1977293ad604SOctavian Purdila lock_sock(sk); 19789c55e01cSJens Axboe } 19799c55e01cSJens Axboe 198035f3d14dSJens Axboe return ret; 19819c55e01cSJens Axboe } 19829c55e01cSJens Axboe 1983357b40a1SHerbert Xu /** 1984357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1985357b40a1SHerbert Xu * @skb: destination buffer 1986357b40a1SHerbert Xu * @offset: offset in destination 1987357b40a1SHerbert Xu * @from: source buffer 1988357b40a1SHerbert Xu * @len: number of bytes to copy 1989357b40a1SHerbert Xu * 1990357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1991357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1992357b40a1SHerbert Xu * traversing fragment lists and such. 1993357b40a1SHerbert Xu */ 1994357b40a1SHerbert Xu 19950c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1996357b40a1SHerbert Xu { 19971a028e50SDavid S. Miller int start = skb_headlen(skb); 1998fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1999fbb398a8SDavid S. Miller int i, copy; 2000357b40a1SHerbert Xu 2001357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2002357b40a1SHerbert Xu goto fault; 2003357b40a1SHerbert Xu 20041a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2005357b40a1SHerbert Xu if (copy > len) 2006357b40a1SHerbert Xu copy = len; 200727d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2008357b40a1SHerbert Xu if ((len -= copy) == 0) 2009357b40a1SHerbert Xu return 0; 2010357b40a1SHerbert Xu offset += copy; 2011357b40a1SHerbert Xu from += copy; 2012357b40a1SHerbert Xu } 2013357b40a1SHerbert Xu 2014357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2015357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20161a028e50SDavid S. Miller int end; 2017357b40a1SHerbert Xu 2018547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20191a028e50SDavid S. Miller 20209e903e08SEric Dumazet end = start + skb_frag_size(frag); 2021357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2022357b40a1SHerbert Xu u8 *vaddr; 2023357b40a1SHerbert Xu 2024357b40a1SHerbert Xu if (copy > len) 2025357b40a1SHerbert Xu copy = len; 2026357b40a1SHerbert Xu 202751c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20281a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 20291a028e50SDavid S. Miller from, copy); 203051c56b00SEric Dumazet kunmap_atomic(vaddr); 2031357b40a1SHerbert Xu 2032357b40a1SHerbert Xu if ((len -= copy) == 0) 2033357b40a1SHerbert Xu return 0; 2034357b40a1SHerbert Xu offset += copy; 2035357b40a1SHerbert Xu from += copy; 2036357b40a1SHerbert Xu } 20371a028e50SDavid S. Miller start = end; 2038357b40a1SHerbert Xu } 2039357b40a1SHerbert Xu 2040fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20411a028e50SDavid S. Miller int end; 2042357b40a1SHerbert Xu 2043547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20441a028e50SDavid S. Miller 2045fbb398a8SDavid S. Miller end = start + frag_iter->len; 2046357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2047357b40a1SHerbert Xu if (copy > len) 2048357b40a1SHerbert Xu copy = len; 2049fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 20501a028e50SDavid S. Miller from, copy)) 2051357b40a1SHerbert Xu goto fault; 2052357b40a1SHerbert Xu if ((len -= copy) == 0) 2053357b40a1SHerbert Xu return 0; 2054357b40a1SHerbert Xu offset += copy; 2055357b40a1SHerbert Xu from += copy; 2056357b40a1SHerbert Xu } 20571a028e50SDavid S. Miller start = end; 2058357b40a1SHerbert Xu } 2059357b40a1SHerbert Xu if (!len) 2060357b40a1SHerbert Xu return 0; 2061357b40a1SHerbert Xu 2062357b40a1SHerbert Xu fault: 2063357b40a1SHerbert Xu return -EFAULT; 2064357b40a1SHerbert Xu } 2065357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2066357b40a1SHerbert Xu 20671da177e4SLinus Torvalds /* Checksum skb data. */ 20682817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 20692817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 20701da177e4SLinus Torvalds { 20711a028e50SDavid S. Miller int start = skb_headlen(skb); 20721a028e50SDavid S. Miller int i, copy = start - offset; 2073fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 20741da177e4SLinus Torvalds int pos = 0; 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds /* Checksum header. */ 20771da177e4SLinus Torvalds if (copy > 0) { 20781da177e4SLinus Torvalds if (copy > len) 20791da177e4SLinus Torvalds copy = len; 20802817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 20811da177e4SLinus Torvalds if ((len -= copy) == 0) 20821da177e4SLinus Torvalds return csum; 20831da177e4SLinus Torvalds offset += copy; 20841da177e4SLinus Torvalds pos = copy; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 20871da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20881a028e50SDavid S. Miller int end; 208951c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20901da177e4SLinus Torvalds 2091547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20921a028e50SDavid S. Miller 209351c56b00SEric Dumazet end = start + skb_frag_size(frag); 20941da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 209544bb9363SAl Viro __wsum csum2; 20961da177e4SLinus Torvalds u8 *vaddr; 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds if (copy > len) 20991da177e4SLinus Torvalds copy = len; 210051c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 21012817a336SDaniel Borkmann csum2 = ops->update(vaddr + frag->page_offset + 21021a028e50SDavid S. Miller offset - start, copy, 0); 210351c56b00SEric Dumazet kunmap_atomic(vaddr); 21042817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 21051da177e4SLinus Torvalds if (!(len -= copy)) 21061da177e4SLinus Torvalds return csum; 21071da177e4SLinus Torvalds offset += copy; 21081da177e4SLinus Torvalds pos += copy; 21091da177e4SLinus Torvalds } 21101a028e50SDavid S. Miller start = end; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds 2113fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 21141a028e50SDavid S. Miller int end; 21151da177e4SLinus Torvalds 2116547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21171a028e50SDavid S. Miller 2118fbb398a8SDavid S. Miller end = start + frag_iter->len; 21191da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21205f92a738SAl Viro __wsum csum2; 21211da177e4SLinus Torvalds if (copy > len) 21221da177e4SLinus Torvalds copy = len; 21232817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 21242817a336SDaniel Borkmann copy, 0, ops); 21252817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 21261da177e4SLinus Torvalds if ((len -= copy) == 0) 21271da177e4SLinus Torvalds return csum; 21281da177e4SLinus Torvalds offset += copy; 21291da177e4SLinus Torvalds pos += copy; 21301da177e4SLinus Torvalds } 21311a028e50SDavid S. Miller start = end; 21321da177e4SLinus Torvalds } 213309a62660SKris Katterjohn BUG_ON(len); 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds return csum; 21361da177e4SLinus Torvalds } 21372817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 21382817a336SDaniel Borkmann 21392817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 21402817a336SDaniel Borkmann int len, __wsum csum) 21412817a336SDaniel Borkmann { 21422817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2143cea80ea8SDaniel Borkmann .update = csum_partial_ext, 21442817a336SDaniel Borkmann .combine = csum_block_add_ext, 21452817a336SDaniel Borkmann }; 21462817a336SDaniel Borkmann 21472817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 21482817a336SDaniel Borkmann } 2149b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 21501da177e4SLinus Torvalds 21511da177e4SLinus Torvalds /* Both of above in one bottle. */ 21521da177e4SLinus Torvalds 215381d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 215481d77662SAl Viro u8 *to, int len, __wsum csum) 21551da177e4SLinus Torvalds { 21561a028e50SDavid S. Miller int start = skb_headlen(skb); 21571a028e50SDavid S. Miller int i, copy = start - offset; 2158fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 21591da177e4SLinus Torvalds int pos = 0; 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds /* Copy header. */ 21621da177e4SLinus Torvalds if (copy > 0) { 21631da177e4SLinus Torvalds if (copy > len) 21641da177e4SLinus Torvalds copy = len; 21651da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 21661da177e4SLinus Torvalds copy, csum); 21671da177e4SLinus Torvalds if ((len -= copy) == 0) 21681da177e4SLinus Torvalds return csum; 21691da177e4SLinus Torvalds offset += copy; 21701da177e4SLinus Torvalds to += copy; 21711da177e4SLinus Torvalds pos = copy; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21751a028e50SDavid S. Miller int end; 21761da177e4SLinus Torvalds 2177547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21781a028e50SDavid S. Miller 21799e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 21801da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21815084205fSAl Viro __wsum csum2; 21821da177e4SLinus Torvalds u8 *vaddr; 21831da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds if (copy > len) 21861da177e4SLinus Torvalds copy = len; 218751c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 21881da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 21891a028e50SDavid S. Miller frag->page_offset + 21901a028e50SDavid S. Miller offset - start, to, 21911a028e50SDavid S. Miller copy, 0); 219251c56b00SEric Dumazet kunmap_atomic(vaddr); 21931da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 21941da177e4SLinus Torvalds if (!(len -= copy)) 21951da177e4SLinus Torvalds return csum; 21961da177e4SLinus Torvalds offset += copy; 21971da177e4SLinus Torvalds to += copy; 21981da177e4SLinus Torvalds pos += copy; 21991da177e4SLinus Torvalds } 22001a028e50SDavid S. Miller start = end; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 2203fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 220481d77662SAl Viro __wsum csum2; 22051a028e50SDavid S. Miller int end; 22061da177e4SLinus Torvalds 2207547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22081a028e50SDavid S. Miller 2209fbb398a8SDavid S. Miller end = start + frag_iter->len; 22101da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 22111da177e4SLinus Torvalds if (copy > len) 22121da177e4SLinus Torvalds copy = len; 2213fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 22141a028e50SDavid S. Miller offset - start, 22151da177e4SLinus Torvalds to, copy, 0); 22161da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 22171da177e4SLinus Torvalds if ((len -= copy) == 0) 22181da177e4SLinus Torvalds return csum; 22191da177e4SLinus Torvalds offset += copy; 22201da177e4SLinus Torvalds to += copy; 22211da177e4SLinus Torvalds pos += copy; 22221da177e4SLinus Torvalds } 22231a028e50SDavid S. Miller start = end; 22241da177e4SLinus Torvalds } 222509a62660SKris Katterjohn BUG_ON(len); 22261da177e4SLinus Torvalds return csum; 22271da177e4SLinus Torvalds } 2228b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 22291da177e4SLinus Torvalds 2230af2806f8SThomas Graf /** 2231af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2232af2806f8SThomas Graf * @from: source buffer 2233af2806f8SThomas Graf * 2234af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2235af2806f8SThomas Graf * into skb_zerocopy(). 2236af2806f8SThomas Graf */ 2237af2806f8SThomas Graf unsigned int 2238af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2239af2806f8SThomas Graf { 2240af2806f8SThomas Graf unsigned int hlen = 0; 2241af2806f8SThomas Graf 2242af2806f8SThomas Graf if (!from->head_frag || 2243af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2244af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2245af2806f8SThomas Graf hlen = skb_headlen(from); 2246af2806f8SThomas Graf 2247af2806f8SThomas Graf if (skb_has_frag_list(from)) 2248af2806f8SThomas Graf hlen = from->len; 2249af2806f8SThomas Graf 2250af2806f8SThomas Graf return hlen; 2251af2806f8SThomas Graf } 2252af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2253af2806f8SThomas Graf 2254af2806f8SThomas Graf /** 2255af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2256af2806f8SThomas Graf * @to: destination buffer 22577fceb4deSMasanari Iida * @from: source buffer 2258af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2259af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2260af2806f8SThomas Graf * 2261af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2262af2806f8SThomas Graf * to the frags in the source buffer. 2263af2806f8SThomas Graf * 2264af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2265af2806f8SThomas Graf * headroom in the `to` buffer. 226636d5fe6aSZoltan Kiss * 226736d5fe6aSZoltan Kiss * Return value: 226836d5fe6aSZoltan Kiss * 0: everything is OK 226936d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 227036d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2271af2806f8SThomas Graf */ 227236d5fe6aSZoltan Kiss int 227336d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2274af2806f8SThomas Graf { 2275af2806f8SThomas Graf int i, j = 0; 2276af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 227736d5fe6aSZoltan Kiss int ret; 2278af2806f8SThomas Graf struct page *page; 2279af2806f8SThomas Graf unsigned int offset; 2280af2806f8SThomas Graf 2281af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2282af2806f8SThomas Graf 2283af2806f8SThomas Graf /* dont bother with small payloads */ 228436d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 228536d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2286af2806f8SThomas Graf 2287af2806f8SThomas Graf if (hlen) { 228836d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 228936d5fe6aSZoltan Kiss if (unlikely(ret)) 229036d5fe6aSZoltan Kiss return ret; 2291af2806f8SThomas Graf len -= hlen; 2292af2806f8SThomas Graf } else { 2293af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2294af2806f8SThomas Graf if (plen) { 2295af2806f8SThomas Graf page = virt_to_head_page(from->head); 2296af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2297af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2298af2806f8SThomas Graf get_page(page); 2299af2806f8SThomas Graf j = 1; 2300af2806f8SThomas Graf len -= plen; 2301af2806f8SThomas Graf } 2302af2806f8SThomas Graf } 2303af2806f8SThomas Graf 2304af2806f8SThomas Graf to->truesize += len + plen; 2305af2806f8SThomas Graf to->len += len + plen; 2306af2806f8SThomas Graf to->data_len += len + plen; 2307af2806f8SThomas Graf 230836d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 230936d5fe6aSZoltan Kiss skb_tx_error(from); 231036d5fe6aSZoltan Kiss return -ENOMEM; 231136d5fe6aSZoltan Kiss } 231236d5fe6aSZoltan Kiss 2313af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2314af2806f8SThomas Graf if (!len) 2315af2806f8SThomas Graf break; 2316af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2317af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2318af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2319af2806f8SThomas Graf skb_frag_ref(to, j); 2320af2806f8SThomas Graf j++; 2321af2806f8SThomas Graf } 2322af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 232336d5fe6aSZoltan Kiss 232436d5fe6aSZoltan Kiss return 0; 2325af2806f8SThomas Graf } 2326af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2327af2806f8SThomas Graf 23281da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 23291da177e4SLinus Torvalds { 2330d3bc23e7SAl Viro __wsum csum; 23311da177e4SLinus Torvalds long csstart; 23321da177e4SLinus Torvalds 233384fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 233455508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 23351da177e4SLinus Torvalds else 23361da177e4SLinus Torvalds csstart = skb_headlen(skb); 23371da177e4SLinus Torvalds 233809a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 23391da177e4SLinus Torvalds 2340d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds csum = 0; 23431da177e4SLinus Torvalds if (csstart != skb->len) 23441da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 23451da177e4SLinus Torvalds skb->len - csstart, 0); 23461da177e4SLinus Torvalds 234784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2348ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 23491da177e4SLinus Torvalds 2350d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 23511da177e4SLinus Torvalds } 23521da177e4SLinus Torvalds } 2353b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 23541da177e4SLinus Torvalds 23551da177e4SLinus Torvalds /** 23561da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 23571da177e4SLinus Torvalds * @list: list to dequeue from 23581da177e4SLinus Torvalds * 23591da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 23601da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 23611da177e4SLinus Torvalds * returned or %NULL if the list is empty. 23621da177e4SLinus Torvalds */ 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 23651da177e4SLinus Torvalds { 23661da177e4SLinus Torvalds unsigned long flags; 23671da177e4SLinus Torvalds struct sk_buff *result; 23681da177e4SLinus Torvalds 23691da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23701da177e4SLinus Torvalds result = __skb_dequeue(list); 23711da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23721da177e4SLinus Torvalds return result; 23731da177e4SLinus Torvalds } 2374b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds /** 23771da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 23781da177e4SLinus Torvalds * @list: list to dequeue from 23791da177e4SLinus Torvalds * 23801da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 23811da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 23821da177e4SLinus Torvalds * returned or %NULL if the list is empty. 23831da177e4SLinus Torvalds */ 23841da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 23851da177e4SLinus Torvalds { 23861da177e4SLinus Torvalds unsigned long flags; 23871da177e4SLinus Torvalds struct sk_buff *result; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23901da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 23911da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23921da177e4SLinus Torvalds return result; 23931da177e4SLinus Torvalds } 2394b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds /** 23971da177e4SLinus Torvalds * skb_queue_purge - empty a list 23981da177e4SLinus Torvalds * @list: list to empty 23991da177e4SLinus Torvalds * 24001da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 24011da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 24021da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 24031da177e4SLinus Torvalds */ 24041da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 24051da177e4SLinus Torvalds { 24061da177e4SLinus Torvalds struct sk_buff *skb; 24071da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 24081da177e4SLinus Torvalds kfree_skb(skb); 24091da177e4SLinus Torvalds } 2410b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds /** 24131da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 24141da177e4SLinus Torvalds * @list: list to use 24151da177e4SLinus Torvalds * @newsk: buffer to queue 24161da177e4SLinus Torvalds * 24171da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 24181da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 24191da177e4SLinus Torvalds * safely. 24201da177e4SLinus Torvalds * 24211da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24221da177e4SLinus Torvalds */ 24231da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 24241da177e4SLinus Torvalds { 24251da177e4SLinus Torvalds unsigned long flags; 24261da177e4SLinus Torvalds 24271da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24281da177e4SLinus Torvalds __skb_queue_head(list, newsk); 24291da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24301da177e4SLinus Torvalds } 2431b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds /** 24341da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 24351da177e4SLinus Torvalds * @list: list to use 24361da177e4SLinus Torvalds * @newsk: buffer to queue 24371da177e4SLinus Torvalds * 24381da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 24391da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 24401da177e4SLinus Torvalds * safely. 24411da177e4SLinus Torvalds * 24421da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24431da177e4SLinus Torvalds */ 24441da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 24451da177e4SLinus Torvalds { 24461da177e4SLinus Torvalds unsigned long flags; 24471da177e4SLinus Torvalds 24481da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24491da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 24501da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24511da177e4SLinus Torvalds } 2452b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 24538728b834SDavid S. Miller 24541da177e4SLinus Torvalds /** 24551da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 24561da177e4SLinus Torvalds * @skb: buffer to remove 24578728b834SDavid S. Miller * @list: list to use 24581da177e4SLinus Torvalds * 24598728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 24608728b834SDavid S. Miller * function is atomic with respect to other list locked calls 24611da177e4SLinus Torvalds * 24628728b834SDavid S. Miller * You must know what list the SKB is on. 24631da177e4SLinus Torvalds */ 24648728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 24651da177e4SLinus Torvalds { 24661da177e4SLinus Torvalds unsigned long flags; 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24698728b834SDavid S. Miller __skb_unlink(skb, list); 24701da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24711da177e4SLinus Torvalds } 2472b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds /** 24751da177e4SLinus Torvalds * skb_append - append a buffer 24761da177e4SLinus Torvalds * @old: buffer to insert after 24771da177e4SLinus Torvalds * @newsk: buffer to insert 24788728b834SDavid S. Miller * @list: list to use 24791da177e4SLinus Torvalds * 24801da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 24811da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 24821da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24831da177e4SLinus Torvalds */ 24848728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 24851da177e4SLinus Torvalds { 24861da177e4SLinus Torvalds unsigned long flags; 24871da177e4SLinus Torvalds 24888728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 24897de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 24908728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 24911da177e4SLinus Torvalds } 2492b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds /** 24951da177e4SLinus Torvalds * skb_insert - insert a buffer 24961da177e4SLinus Torvalds * @old: buffer to insert before 24971da177e4SLinus Torvalds * @newsk: buffer to insert 24988728b834SDavid S. Miller * @list: list to use 24991da177e4SLinus Torvalds * 25008728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 25018728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 25028728b834SDavid S. Miller * calls. 25038728b834SDavid S. Miller * 25041da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 25051da177e4SLinus Torvalds */ 25068728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 25071da177e4SLinus Torvalds { 25081da177e4SLinus Torvalds unsigned long flags; 25091da177e4SLinus Torvalds 25108728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 25118728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 25128728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 25131da177e4SLinus Torvalds } 2514b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 25171da177e4SLinus Torvalds struct sk_buff* skb1, 25181da177e4SLinus Torvalds const u32 len, const int pos) 25191da177e4SLinus Torvalds { 25201da177e4SLinus Torvalds int i; 25211da177e4SLinus Torvalds 2522d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2523d626f62bSArnaldo Carvalho de Melo pos - len); 25241da177e4SLinus Torvalds /* And move data appendix as is. */ 25251da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 25261da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 25291da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 25301da177e4SLinus Torvalds skb1->data_len = skb->data_len; 25311da177e4SLinus Torvalds skb1->len += skb1->data_len; 25321da177e4SLinus Torvalds skb->data_len = 0; 25331da177e4SLinus Torvalds skb->len = len; 253427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 25371da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 25381da177e4SLinus Torvalds struct sk_buff* skb1, 25391da177e4SLinus Torvalds const u32 len, int pos) 25401da177e4SLinus Torvalds { 25411da177e4SLinus Torvalds int i, k = 0; 25421da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 25451da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 25461da177e4SLinus Torvalds skb->len = len; 25471da177e4SLinus Torvalds skb->data_len = len - pos; 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 25509e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds if (pos + size > len) { 25531da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds if (pos < len) { 25561da177e4SLinus Torvalds /* Split frag. 25571da177e4SLinus Torvalds * We have two variants in this case: 25581da177e4SLinus Torvalds * 1. Move all the frag to the second 25591da177e4SLinus Torvalds * part, if it is possible. F.e. 25601da177e4SLinus Torvalds * this approach is mandatory for TUX, 25611da177e4SLinus Torvalds * where splitting is expensive. 25621da177e4SLinus Torvalds * 2. Split is accurately. We make this. 25631da177e4SLinus Torvalds */ 2564ea2ab693SIan Campbell skb_frag_ref(skb, i); 25651da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 25669e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 25679e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 25681da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 25691da177e4SLinus Torvalds } 25701da177e4SLinus Torvalds k++; 25711da177e4SLinus Torvalds } else 25721da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 25731da177e4SLinus Torvalds pos += size; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds /** 25791da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 25801da177e4SLinus Torvalds * @skb: the buffer to split 25811da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 25821da177e4SLinus Torvalds * @len: new length for skb 25831da177e4SLinus Torvalds */ 25841da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 25851da177e4SLinus Torvalds { 25861da177e4SLinus Torvalds int pos = skb_headlen(skb); 25871da177e4SLinus Torvalds 258868534c68SAmerigo Wang skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 25891da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 25901da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 25911da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 25921da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 25931da177e4SLinus Torvalds } 2594b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 25951da177e4SLinus Torvalds 25969f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 25979f782db3SIlpo Järvinen * 25989f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 25999f782db3SIlpo Järvinen */ 2600832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2601832d11c5SIlpo Järvinen { 26020ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2603832d11c5SIlpo Järvinen } 2604832d11c5SIlpo Järvinen 2605832d11c5SIlpo Järvinen /** 2606832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2607832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2608832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2609832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2610832d11c5SIlpo Järvinen * 2611832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 261220e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2613832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2614832d11c5SIlpo Järvinen * 2615832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2616832d11c5SIlpo Järvinen * 2617832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2618832d11c5SIlpo Järvinen * to have non-paged data as well. 2619832d11c5SIlpo Järvinen * 2620832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2621832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2622832d11c5SIlpo Järvinen */ 2623832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2624832d11c5SIlpo Järvinen { 2625832d11c5SIlpo Järvinen int from, to, merge, todo; 2626832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2627832d11c5SIlpo Järvinen 2628832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2629832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2630832d11c5SIlpo Järvinen 2631832d11c5SIlpo Järvinen todo = shiftlen; 2632832d11c5SIlpo Järvinen from = 0; 2633832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2634832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2635832d11c5SIlpo Järvinen 2636832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2637832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2638832d11c5SIlpo Järvinen */ 2639832d11c5SIlpo Järvinen if (!to || 2640ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2641ea2ab693SIan Campbell fragfrom->page_offset)) { 2642832d11c5SIlpo Järvinen merge = -1; 2643832d11c5SIlpo Järvinen } else { 2644832d11c5SIlpo Järvinen merge = to - 1; 2645832d11c5SIlpo Järvinen 26469e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2647832d11c5SIlpo Järvinen if (todo < 0) { 2648832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2649832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2650832d11c5SIlpo Järvinen return 0; 2651832d11c5SIlpo Järvinen 26529f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 26539f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2654832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2655832d11c5SIlpo Järvinen 26569e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 26579e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2658832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2659832d11c5SIlpo Järvinen 2660832d11c5SIlpo Järvinen goto onlymerged; 2661832d11c5SIlpo Järvinen } 2662832d11c5SIlpo Järvinen 2663832d11c5SIlpo Järvinen from++; 2664832d11c5SIlpo Järvinen } 2665832d11c5SIlpo Järvinen 2666832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2667832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2668832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2669832d11c5SIlpo Järvinen return 0; 2670832d11c5SIlpo Järvinen 2671832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2672832d11c5SIlpo Järvinen return 0; 2673832d11c5SIlpo Järvinen 2674832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2675832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2676832d11c5SIlpo Järvinen return 0; 2677832d11c5SIlpo Järvinen 2678832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2679832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2680832d11c5SIlpo Järvinen 26819e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2682832d11c5SIlpo Järvinen *fragto = *fragfrom; 26839e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2684832d11c5SIlpo Järvinen from++; 2685832d11c5SIlpo Järvinen to++; 2686832d11c5SIlpo Järvinen 2687832d11c5SIlpo Järvinen } else { 2688ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2689832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2690832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 26919e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2692832d11c5SIlpo Järvinen 2693832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 26949e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2695832d11c5SIlpo Järvinen todo = 0; 2696832d11c5SIlpo Järvinen 2697832d11c5SIlpo Järvinen to++; 2698832d11c5SIlpo Järvinen break; 2699832d11c5SIlpo Järvinen } 2700832d11c5SIlpo Järvinen } 2701832d11c5SIlpo Järvinen 2702832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2703832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2704832d11c5SIlpo Järvinen 2705832d11c5SIlpo Järvinen if (merge >= 0) { 2706832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2707832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2708832d11c5SIlpo Järvinen 27099e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2710ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2711832d11c5SIlpo Järvinen } 2712832d11c5SIlpo Järvinen 2713832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2714832d11c5SIlpo Järvinen to = 0; 2715832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2716832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2717832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2718832d11c5SIlpo Järvinen 2719832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2720832d11c5SIlpo Järvinen 2721832d11c5SIlpo Järvinen onlymerged: 2722832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2723832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2724832d11c5SIlpo Järvinen */ 2725832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2726832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2727832d11c5SIlpo Järvinen 2728832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2729832d11c5SIlpo Järvinen skb->len -= shiftlen; 2730832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2731832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2732832d11c5SIlpo Järvinen tgt->len += shiftlen; 2733832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2734832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2735832d11c5SIlpo Järvinen 2736832d11c5SIlpo Järvinen return shiftlen; 2737832d11c5SIlpo Järvinen } 2738832d11c5SIlpo Järvinen 2739677e90edSThomas Graf /** 2740677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2741677e90edSThomas Graf * @skb: the buffer to read 2742677e90edSThomas Graf * @from: lower offset of data to be read 2743677e90edSThomas Graf * @to: upper offset of data to be read 2744677e90edSThomas Graf * @st: state variable 2745677e90edSThomas Graf * 2746677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2747677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2748677e90edSThomas Graf */ 2749677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2750677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2751677e90edSThomas Graf { 2752677e90edSThomas Graf st->lower_offset = from; 2753677e90edSThomas Graf st->upper_offset = to; 2754677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2755677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2756677e90edSThomas Graf st->frag_data = NULL; 2757677e90edSThomas Graf } 2758b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2759677e90edSThomas Graf 2760677e90edSThomas Graf /** 2761677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2762677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2763677e90edSThomas Graf * @data: destination pointer for data to be returned 2764677e90edSThomas Graf * @st: state variable 2765677e90edSThomas Graf * 2766bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 2767677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2768bc32383cSMathias Krause * the head of the data block to @data and returns the length 2769677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2770677e90edSThomas Graf * offset has been reached. 2771677e90edSThomas Graf * 2772677e90edSThomas Graf * The caller is not required to consume all of the data 2773bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 2774677e90edSThomas Graf * of bytes already consumed and the next call to 2775677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2776677e90edSThomas Graf * 277725985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2778e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 2779677e90edSThomas Graf * reads of potentially non linear data. 2780677e90edSThomas Graf * 2781bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2782677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2783677e90edSThomas Graf * a stack for this purpose. 2784677e90edSThomas Graf */ 2785677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2786677e90edSThomas Graf struct skb_seq_state *st) 2787677e90edSThomas Graf { 2788677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2789677e90edSThomas Graf skb_frag_t *frag; 2790677e90edSThomas Graf 2791aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 2792aeb193eaSWedson Almeida Filho if (st->frag_data) { 2793aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 2794aeb193eaSWedson Almeida Filho st->frag_data = NULL; 2795aeb193eaSWedson Almeida Filho } 2796677e90edSThomas Graf return 0; 2797aeb193eaSWedson Almeida Filho } 2798677e90edSThomas Graf 2799677e90edSThomas Graf next_skb: 280095e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2801677e90edSThomas Graf 2802995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 280395e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2804677e90edSThomas Graf return block_limit - abs_offset; 2805677e90edSThomas Graf } 2806677e90edSThomas Graf 2807677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2808677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2809677e90edSThomas Graf 2810677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2811677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 28129e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2813677e90edSThomas Graf 2814677e90edSThomas Graf if (abs_offset < block_limit) { 2815677e90edSThomas Graf if (!st->frag_data) 281651c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2817677e90edSThomas Graf 2818677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2819677e90edSThomas Graf (abs_offset - st->stepped_offset); 2820677e90edSThomas Graf 2821677e90edSThomas Graf return block_limit - abs_offset; 2822677e90edSThomas Graf } 2823677e90edSThomas Graf 2824677e90edSThomas Graf if (st->frag_data) { 282551c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2826677e90edSThomas Graf st->frag_data = NULL; 2827677e90edSThomas Graf } 2828677e90edSThomas Graf 2829677e90edSThomas Graf st->frag_idx++; 28309e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2831677e90edSThomas Graf } 2832677e90edSThomas Graf 28335b5a60daSOlaf Kirch if (st->frag_data) { 283451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 28355b5a60daSOlaf Kirch st->frag_data = NULL; 28365b5a60daSOlaf Kirch } 28375b5a60daSOlaf Kirch 283821dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2839677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 284095e3b24cSHerbert Xu st->frag_idx = 0; 2841677e90edSThomas Graf goto next_skb; 284271b3346dSShyam Iyer } else if (st->cur_skb->next) { 284371b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 284471b3346dSShyam Iyer st->frag_idx = 0; 2845677e90edSThomas Graf goto next_skb; 2846677e90edSThomas Graf } 2847677e90edSThomas Graf 2848677e90edSThomas Graf return 0; 2849677e90edSThomas Graf } 2850b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2851677e90edSThomas Graf 2852677e90edSThomas Graf /** 2853677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2854677e90edSThomas Graf * @st: state variable 2855677e90edSThomas Graf * 2856677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2857677e90edSThomas Graf * returned 0. 2858677e90edSThomas Graf */ 2859677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2860677e90edSThomas Graf { 2861677e90edSThomas Graf if (st->frag_data) 286251c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2863677e90edSThomas Graf } 2864b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2865677e90edSThomas Graf 28663fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 28673fc7e8a6SThomas Graf 28683fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 28693fc7e8a6SThomas Graf struct ts_config *conf, 28703fc7e8a6SThomas Graf struct ts_state *state) 28713fc7e8a6SThomas Graf { 28723fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 28733fc7e8a6SThomas Graf } 28743fc7e8a6SThomas Graf 28753fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 28763fc7e8a6SThomas Graf { 28773fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 28783fc7e8a6SThomas Graf } 28793fc7e8a6SThomas Graf 28803fc7e8a6SThomas Graf /** 28813fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 28823fc7e8a6SThomas Graf * @skb: the buffer to look in 28833fc7e8a6SThomas Graf * @from: search offset 28843fc7e8a6SThomas Graf * @to: search limit 28853fc7e8a6SThomas Graf * @config: textsearch configuration 28863fc7e8a6SThomas Graf * 28873fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 28883fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 28893fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 28903fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 28913fc7e8a6SThomas Graf */ 28923fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2893059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 28943fc7e8a6SThomas Graf { 2895059a2440SBojan Prtvar struct ts_state state; 2896f72b948dSPhil Oester unsigned int ret; 2897f72b948dSPhil Oester 28983fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 28993fc7e8a6SThomas Graf config->finish = skb_ts_finish; 29003fc7e8a6SThomas Graf 2901059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 29023fc7e8a6SThomas Graf 2903059a2440SBojan Prtvar ret = textsearch_find(config, &state); 2904f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 29053fc7e8a6SThomas Graf } 2906b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 29073fc7e8a6SThomas Graf 2908e89e9cf5SAnanda Raju /** 29092c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2910e89e9cf5SAnanda Raju * @sk: sock structure 2911e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 2912e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2913e89e9cf5SAnanda Raju * @from: pointer to user message iov 2914e89e9cf5SAnanda Raju * @length: length of the iov message 2915e89e9cf5SAnanda Raju * 2916e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2917e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2918e89e9cf5SAnanda Raju */ 2919e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2920dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2921e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2922e89e9cf5SAnanda Raju void *from, int length) 2923e89e9cf5SAnanda Raju { 2924b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 2925b2111724SEric Dumazet int copy; 2926e89e9cf5SAnanda Raju int offset = 0; 2927e89e9cf5SAnanda Raju int ret; 2928b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 2929e89e9cf5SAnanda Raju 2930e89e9cf5SAnanda Raju do { 2931e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2932e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2933b2111724SEric Dumazet return -EMSGSIZE; 2934e89e9cf5SAnanda Raju 2935b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 2936e89e9cf5SAnanda Raju return -ENOMEM; 2937e89e9cf5SAnanda Raju 2938e89e9cf5SAnanda Raju /* copy the user data to page */ 2939b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 2940e89e9cf5SAnanda Raju 2941b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 2942e89e9cf5SAnanda Raju offset, copy, 0, skb); 2943e89e9cf5SAnanda Raju if (ret < 0) 2944e89e9cf5SAnanda Raju return -EFAULT; 2945e89e9cf5SAnanda Raju 2946e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2947b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 2948b2111724SEric Dumazet copy); 2949b2111724SEric Dumazet frg_cnt++; 2950b2111724SEric Dumazet pfrag->offset += copy; 2951b2111724SEric Dumazet get_page(pfrag->page); 2952b2111724SEric Dumazet 2953b2111724SEric Dumazet skb->truesize += copy; 2954b2111724SEric Dumazet atomic_add(copy, &sk->sk_wmem_alloc); 2955e89e9cf5SAnanda Raju skb->len += copy; 2956e89e9cf5SAnanda Raju skb->data_len += copy; 2957e89e9cf5SAnanda Raju offset += copy; 2958e89e9cf5SAnanda Raju length -= copy; 2959e89e9cf5SAnanda Raju 2960e89e9cf5SAnanda Raju } while (length > 0); 2961e89e9cf5SAnanda Raju 2962e89e9cf5SAnanda Raju return 0; 2963e89e9cf5SAnanda Raju } 2964b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2965e89e9cf5SAnanda Raju 2966cbb042f9SHerbert Xu /** 2967cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2968cbb042f9SHerbert Xu * @skb: buffer to update 2969cbb042f9SHerbert Xu * @len: length of data pulled 2970cbb042f9SHerbert Xu * 2971cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2972fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 297384fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 297484fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 297584fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2976cbb042f9SHerbert Xu */ 2977cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2978cbb042f9SHerbert Xu { 2979cbb042f9SHerbert Xu BUG_ON(len > skb->len); 2980cbb042f9SHerbert Xu skb->len -= len; 2981cbb042f9SHerbert Xu BUG_ON(skb->len < skb->data_len); 2982cbb042f9SHerbert Xu skb_postpull_rcsum(skb, skb->data, len); 2983cbb042f9SHerbert Xu return skb->data += len; 2984cbb042f9SHerbert Xu } 2985f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2986f94691acSArnaldo Carvalho de Melo 2987f4c50d99SHerbert Xu /** 2988f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2989df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 2990576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2991f4c50d99SHerbert Xu * 2992f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 29934c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 29944c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2995f4c50d99SHerbert Xu */ 2996df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 2997df5771ffSMichael S. Tsirkin netdev_features_t features) 2998f4c50d99SHerbert Xu { 2999f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3000f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 30011a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3002df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3003df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3004df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 30051fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3006f4c50d99SHerbert Xu unsigned int offset = doffset; 3007df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3008f4c50d99SHerbert Xu unsigned int headroom; 3009f4c50d99SHerbert Xu unsigned int len; 3010ec5f0615SPravin B Shelar __be16 proto; 3011ec5f0615SPravin B Shelar bool csum; 301204ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 3013df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3014f4c50d99SHerbert Xu int err = -ENOMEM; 3015f4c50d99SHerbert Xu int i = 0; 3016f4c50d99SHerbert Xu int pos; 301753d6471cSVlad Yasevich int dummy; 3018f4c50d99SHerbert Xu 30195882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 302053d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3021ec5f0615SPravin B Shelar if (unlikely(!proto)) 3022ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3023ec5f0615SPravin B Shelar 30247e2b10c1STom Herbert csum = !head_skb->encap_hdr_csum && 30257e2b10c1STom Herbert !!can_checksum_protocol(features, proto); 30267e2b10c1STom Herbert 3027df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3028df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3029f4c50d99SHerbert Xu 3030f4c50d99SHerbert Xu do { 3031f4c50d99SHerbert Xu struct sk_buff *nskb; 30328cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3033c8884eddSHerbert Xu int hsize; 3034f4c50d99SHerbert Xu int size; 3035f4c50d99SHerbert Xu 3036df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3037f4c50d99SHerbert Xu if (len > mss) 3038f4c50d99SHerbert Xu len = mss; 3039f4c50d99SHerbert Xu 3040df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3041f4c50d99SHerbert Xu if (hsize < 0) 3042f4c50d99SHerbert Xu hsize = 0; 3043c8884eddSHerbert Xu if (hsize > len || !sg) 3044c8884eddSHerbert Xu hsize = len; 3045f4c50d99SHerbert Xu 30461a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 30471a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 30481a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 304989319d38SHerbert Xu 30509d8506ccSHerbert Xu i = 0; 30511a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 30521a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 30531fd819ecSMichael S. Tsirkin frag_skb = list_skb; 30541a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 30559d8506ccSHerbert Xu 30569d8506ccSHerbert Xu while (pos < offset + len) { 30579d8506ccSHerbert Xu BUG_ON(i >= nfrags); 30589d8506ccSHerbert Xu 30594e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 30609d8506ccSHerbert Xu if (pos + size > offset + len) 30619d8506ccSHerbert Xu break; 30629d8506ccSHerbert Xu 30639d8506ccSHerbert Xu i++; 30649d8506ccSHerbert Xu pos += size; 30654e1beba1SMichael S. Tsirkin frag++; 30669d8506ccSHerbert Xu } 30679d8506ccSHerbert Xu 30681a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 30691a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 307089319d38SHerbert Xu 3071f4c50d99SHerbert Xu if (unlikely(!nskb)) 3072f4c50d99SHerbert Xu goto err; 3073f4c50d99SHerbert Xu 30749d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 30759d8506ccSHerbert Xu kfree_skb(nskb); 30769d8506ccSHerbert Xu goto err; 30779d8506ccSHerbert Xu } 30789d8506ccSHerbert Xu 3079ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 308089319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 308189319d38SHerbert Xu kfree_skb(nskb); 308289319d38SHerbert Xu goto err; 308389319d38SHerbert Xu } 308489319d38SHerbert Xu 3085ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 308689319d38SHerbert Xu skb_release_head_state(nskb); 308789319d38SHerbert Xu __skb_push(nskb, doffset); 308889319d38SHerbert Xu } else { 3089c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3090df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3091c93bdd0eSMel Gorman NUMA_NO_NODE); 309289319d38SHerbert Xu 309389319d38SHerbert Xu if (unlikely(!nskb)) 309489319d38SHerbert Xu goto err; 309589319d38SHerbert Xu 309689319d38SHerbert Xu skb_reserve(nskb, headroom); 309789319d38SHerbert Xu __skb_put(nskb, doffset); 309889319d38SHerbert Xu } 309989319d38SHerbert Xu 3100f4c50d99SHerbert Xu if (segs) 3101f4c50d99SHerbert Xu tail->next = nskb; 3102f4c50d99SHerbert Xu else 3103f4c50d99SHerbert Xu segs = nskb; 3104f4c50d99SHerbert Xu tail = nskb; 3105f4c50d99SHerbert Xu 3106df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3107f4c50d99SHerbert Xu 3108030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3109fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 311068c33163SPravin B Shelar 3111df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 311268c33163SPravin B Shelar nskb->data - tnl_hlen, 311368c33163SPravin B Shelar doffset + tnl_hlen); 311489319d38SHerbert Xu 31159d8506ccSHerbert Xu if (nskb->len == len + doffset) 31161cdbcb79SSimon Horman goto perform_csum_check; 311789319d38SHerbert Xu 3118e585f236STom Herbert if (!sg && !nskb->remcsum_offload) { 31196f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 3120df5771ffSMichael S. Tsirkin nskb->csum = skb_copy_and_csum_bits(head_skb, offset, 3121f4c50d99SHerbert Xu skb_put(nskb, len), 3122f4c50d99SHerbert Xu len, 0); 31237e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3124de843723STom Herbert skb_headroom(nskb) + doffset; 3125f4c50d99SHerbert Xu continue; 3126f4c50d99SHerbert Xu } 3127f4c50d99SHerbert Xu 31288cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3129f4c50d99SHerbert Xu 3130df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3131d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3132f4c50d99SHerbert Xu 3133df5771ffSMichael S. Tsirkin skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & 3134df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3135cef401deSEric Dumazet 31369d8506ccSHerbert Xu while (pos < offset + len) { 31379d8506ccSHerbert Xu if (i >= nfrags) { 31381a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 31399d8506ccSHerbert Xu 31409d8506ccSHerbert Xu i = 0; 31411a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 31421a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 31431fd819ecSMichael S. Tsirkin frag_skb = list_skb; 31449d8506ccSHerbert Xu 31459d8506ccSHerbert Xu BUG_ON(!nfrags); 31469d8506ccSHerbert Xu 31471a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 31489d8506ccSHerbert Xu } 31499d8506ccSHerbert Xu 31509d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 31519d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 31529d8506ccSHerbert Xu net_warn_ratelimited( 31539d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 31549d8506ccSHerbert Xu pos, mss); 31559d8506ccSHerbert Xu goto err; 31569d8506ccSHerbert Xu } 31579d8506ccSHerbert Xu 31581fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 31591fd819ecSMichael S. Tsirkin goto err; 31601fd819ecSMichael S. Tsirkin 31614e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 31628cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 31638cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3164f4c50d99SHerbert Xu 3165f4c50d99SHerbert Xu if (pos < offset) { 31668cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 31678cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3168f4c50d99SHerbert Xu } 3169f4c50d99SHerbert Xu 317089319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3171f4c50d99SHerbert Xu 3172f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3173f4c50d99SHerbert Xu i++; 31744e1beba1SMichael S. Tsirkin frag++; 3175f4c50d99SHerbert Xu pos += size; 3176f4c50d99SHerbert Xu } else { 31778cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 317889319d38SHerbert Xu goto skip_fraglist; 3179f4c50d99SHerbert Xu } 3180f4c50d99SHerbert Xu 31818cb19905SMichael S. Tsirkin nskb_frag++; 3182f4c50d99SHerbert Xu } 3183f4c50d99SHerbert Xu 318489319d38SHerbert Xu skip_fraglist: 3185f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3186f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3187f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3188ec5f0615SPravin B Shelar 31891cdbcb79SSimon Horman perform_csum_check: 3190e585f236STom Herbert if (!csum && !nskb->remcsum_offload) { 3191ec5f0615SPravin B Shelar nskb->csum = skb_checksum(nskb, doffset, 3192ec5f0615SPravin B Shelar nskb->len - doffset, 0); 3193ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 31947e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 31957e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3196ec5f0615SPravin B Shelar } 3197df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3198f4c50d99SHerbert Xu 3199bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3200bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3201bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3202bec3cfdcSEric Dumazet */ 3203bec3cfdcSEric Dumazet segs->prev = tail; 3204432c856fSToshiaki Makita 3205432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3206432c856fSToshiaki Makita * using skb_set_owner_w(). 3207432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3208432c856fSToshiaki Makita */ 3209432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3210432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3211432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3212432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3213432c856fSToshiaki Makita } 3214f4c50d99SHerbert Xu return segs; 3215f4c50d99SHerbert Xu 3216f4c50d99SHerbert Xu err: 3217289dccbeSEric Dumazet kfree_skb_list(segs); 3218f4c50d99SHerbert Xu return ERR_PTR(err); 3219f4c50d99SHerbert Xu } 3220f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3221f4c50d99SHerbert Xu 322271d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 322371d93b39SHerbert Xu { 32248a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 322567147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 322667147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 32278a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 322858025e46SEric Dumazet struct sk_buff *lp, *p = *head; 3229715dc1f3SEric Dumazet unsigned int delta_truesize; 323071d93b39SHerbert Xu 32318a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 323271d93b39SHerbert Xu return -E2BIG; 323371d93b39SHerbert Xu 323429e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 32358a29111cSEric Dumazet pinfo = skb_shinfo(lp); 32368a29111cSEric Dumazet 32378a29111cSEric Dumazet if (headlen <= offset) { 323842da6994SHerbert Xu skb_frag_t *frag; 323966e92fcfSHerbert Xu skb_frag_t *frag2; 32409aaa156cSHerbert Xu int i = skbinfo->nr_frags; 32419aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 324242da6994SHerbert Xu 324366e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 32448a29111cSEric Dumazet goto merge; 324581705ad1SHerbert Xu 32468a29111cSEric Dumazet offset -= headlen; 32479aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 32489aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3249f5572068SHerbert Xu 32509aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 32519aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 325266e92fcfSHerbert Xu do { 325366e92fcfSHerbert Xu *--frag = *--frag2; 325466e92fcfSHerbert Xu } while (--i); 325566e92fcfSHerbert Xu 325666e92fcfSHerbert Xu frag->page_offset += offset; 32579e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 325866e92fcfSHerbert Xu 3259715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3260ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3261ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3262715dc1f3SEric Dumazet 3263f5572068SHerbert Xu skb->truesize -= skb->data_len; 3264f5572068SHerbert Xu skb->len -= skb->data_len; 3265f5572068SHerbert Xu skb->data_len = 0; 3266f5572068SHerbert Xu 3267715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 32685d38a079SHerbert Xu goto done; 3269d7e8883cSEric Dumazet } else if (skb->head_frag) { 3270d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3271d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3272d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3273d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3274d7e8883cSEric Dumazet unsigned int first_offset; 3275d7e8883cSEric Dumazet 3276d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 32778a29111cSEric Dumazet goto merge; 3278d7e8883cSEric Dumazet 3279d7e8883cSEric Dumazet first_offset = skb->data - 3280d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3281d7e8883cSEric Dumazet offset; 3282d7e8883cSEric Dumazet 3283d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3284d7e8883cSEric Dumazet 3285d7e8883cSEric Dumazet frag->page.p = page; 3286d7e8883cSEric Dumazet frag->page_offset = first_offset; 3287d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3288d7e8883cSEric Dumazet 3289d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3290d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3291d7e8883cSEric Dumazet 3292715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3293d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3294d7e8883cSEric Dumazet goto done; 32958a29111cSEric Dumazet } 329671d93b39SHerbert Xu 329771d93b39SHerbert Xu merge: 3298715dc1f3SEric Dumazet delta_truesize = skb->truesize; 329967147ba9SHerbert Xu if (offset > headlen) { 3300d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3301d1dc7abfSMichal Schmidt 3302d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 33039e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3304d1dc7abfSMichal Schmidt skb->data_len -= eat; 3305d1dc7abfSMichal Schmidt skb->len -= eat; 330667147ba9SHerbert Xu offset = headlen; 330756035022SHerbert Xu } 330856035022SHerbert Xu 330967147ba9SHerbert Xu __skb_pull(skb, offset); 331056035022SHerbert Xu 331129e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 33128a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 33138a29111cSEric Dumazet else 3314c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3315c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3316f4a775d1SEric Dumazet __skb_header_release(skb); 33178a29111cSEric Dumazet lp = p; 331871d93b39SHerbert Xu 33195d38a079SHerbert Xu done: 33205d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 332137fe4732SHerbert Xu p->data_len += len; 3322715dc1f3SEric Dumazet p->truesize += delta_truesize; 332337fe4732SHerbert Xu p->len += len; 33248a29111cSEric Dumazet if (lp != p) { 33258a29111cSEric Dumazet lp->data_len += len; 33268a29111cSEric Dumazet lp->truesize += delta_truesize; 33278a29111cSEric Dumazet lp->len += len; 33288a29111cSEric Dumazet } 332971d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 333071d93b39SHerbert Xu return 0; 333171d93b39SHerbert Xu } 333271d93b39SHerbert Xu 33331da177e4SLinus Torvalds void __init skb_init(void) 33341da177e4SLinus Torvalds { 33351da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 33361da177e4SLinus Torvalds sizeof(struct sk_buff), 33371da177e4SLinus Torvalds 0, 3338e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 333920c2df83SPaul Mundt NULL); 3340d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3341d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3342d179cd12SDavid S. Miller 0, 3343e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 334420c2df83SPaul Mundt NULL); 33451da177e4SLinus Torvalds } 33461da177e4SLinus Torvalds 3347716ea3a7SDavid Howells /** 3348716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3349716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3350716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3351716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3352716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3353716ea3a7SDavid Howells * 3354716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3355716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3356716ea3a7SDavid Howells */ 335751c739d1SDavid S. Miller static int 335851c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3359716ea3a7SDavid Howells { 33601a028e50SDavid S. Miller int start = skb_headlen(skb); 33611a028e50SDavid S. Miller int i, copy = start - offset; 3362fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3363716ea3a7SDavid Howells int elt = 0; 3364716ea3a7SDavid Howells 3365716ea3a7SDavid Howells if (copy > 0) { 3366716ea3a7SDavid Howells if (copy > len) 3367716ea3a7SDavid Howells copy = len; 3368642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3369716ea3a7SDavid Howells elt++; 3370716ea3a7SDavid Howells if ((len -= copy) == 0) 3371716ea3a7SDavid Howells return elt; 3372716ea3a7SDavid Howells offset += copy; 3373716ea3a7SDavid Howells } 3374716ea3a7SDavid Howells 3375716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33761a028e50SDavid S. Miller int end; 3377716ea3a7SDavid Howells 3378547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33791a028e50SDavid S. Miller 33809e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3381716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3382716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3383716ea3a7SDavid Howells 3384716ea3a7SDavid Howells if (copy > len) 3385716ea3a7SDavid Howells copy = len; 3386ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3387642f1490SJens Axboe frag->page_offset+offset-start); 3388716ea3a7SDavid Howells elt++; 3389716ea3a7SDavid Howells if (!(len -= copy)) 3390716ea3a7SDavid Howells return elt; 3391716ea3a7SDavid Howells offset += copy; 3392716ea3a7SDavid Howells } 33931a028e50SDavid S. Miller start = end; 3394716ea3a7SDavid Howells } 3395716ea3a7SDavid Howells 3396fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 33971a028e50SDavid S. Miller int end; 3398716ea3a7SDavid Howells 3399547b792cSIlpo Järvinen WARN_ON(start > offset + len); 34001a028e50SDavid S. Miller 3401fbb398a8SDavid S. Miller end = start + frag_iter->len; 3402716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3403716ea3a7SDavid Howells if (copy > len) 3404716ea3a7SDavid Howells copy = len; 3405fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 340651c739d1SDavid S. Miller copy); 3407716ea3a7SDavid Howells if ((len -= copy) == 0) 3408716ea3a7SDavid Howells return elt; 3409716ea3a7SDavid Howells offset += copy; 3410716ea3a7SDavid Howells } 34111a028e50SDavid S. Miller start = end; 3412716ea3a7SDavid Howells } 3413716ea3a7SDavid Howells BUG_ON(len); 3414716ea3a7SDavid Howells return elt; 3415716ea3a7SDavid Howells } 3416716ea3a7SDavid Howells 341725a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 341825a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 341925a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 342025a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 342125a91d8dSFan Du * 342225a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 342325a91d8dSFan Du * 1. sg_init_table 342425a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 342525a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 342625a91d8dSFan Du * 342725a91d8dSFan Du * This is equivalent to: 342825a91d8dSFan Du * 1. sg_init_table 342925a91d8dSFan Du * 2. skb_to_sgvec(payload1) 343025a91d8dSFan Du * 3. sg_unmark_end 343125a91d8dSFan Du * 4. skb_to_sgvec(payload2) 343225a91d8dSFan Du * 343325a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 343425a91d8dSFan Du * is more preferable. 343525a91d8dSFan Du */ 343625a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 343725a91d8dSFan Du int offset, int len) 343825a91d8dSFan Du { 343925a91d8dSFan Du return __skb_to_sgvec(skb, sg, offset, len); 344025a91d8dSFan Du } 344125a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 344225a91d8dSFan Du 344351c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 344451c739d1SDavid S. Miller { 344551c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 344651c739d1SDavid S. Miller 3447c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 344851c739d1SDavid S. Miller 344951c739d1SDavid S. Miller return nsg; 345051c739d1SDavid S. Miller } 3451b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 345251c739d1SDavid S. Miller 3453716ea3a7SDavid Howells /** 3454716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3455716ea3a7SDavid Howells * @skb: The socket buffer to check. 3456716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3457716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3458716ea3a7SDavid Howells * 3459716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3460716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3461716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3462716ea3a7SDavid Howells * 3463716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3464716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3465716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3466716ea3a7SDavid Howells * 3467716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3468716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3469716ea3a7SDavid Howells */ 3470716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3471716ea3a7SDavid Howells { 3472716ea3a7SDavid Howells int copyflag; 3473716ea3a7SDavid Howells int elt; 3474716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3475716ea3a7SDavid Howells 3476716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3477716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3478716ea3a7SDavid Howells * at the moment even if they are anonymous). 3479716ea3a7SDavid Howells */ 3480716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3481716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3482716ea3a7SDavid Howells return -ENOMEM; 3483716ea3a7SDavid Howells 3484716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 348521dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3486716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3487716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3488716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3489716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3490716ea3a7SDavid Howells 3491716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3492716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3493716ea3a7SDavid Howells return -ENOMEM; 3494716ea3a7SDavid Howells 3495716ea3a7SDavid Howells /* Voila! */ 3496716ea3a7SDavid Howells *trailer = skb; 3497716ea3a7SDavid Howells return 1; 3498716ea3a7SDavid Howells } 3499716ea3a7SDavid Howells 3500716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3501716ea3a7SDavid Howells 3502716ea3a7SDavid Howells elt = 1; 3503716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3504716ea3a7SDavid Howells copyflag = 0; 3505716ea3a7SDavid Howells 3506716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3507716ea3a7SDavid Howells int ntail = 0; 3508716ea3a7SDavid Howells 3509716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3510716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3511716ea3a7SDavid Howells * after it. */ 3512716ea3a7SDavid Howells 3513716ea3a7SDavid Howells if (skb_shared(skb1)) 3514716ea3a7SDavid Howells copyflag = 1; 3515716ea3a7SDavid Howells 3516716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3517716ea3a7SDavid Howells 3518716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3519716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 352021dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3521716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3522716ea3a7SDavid Howells ntail = tailbits + 128; 3523716ea3a7SDavid Howells } 3524716ea3a7SDavid Howells 3525716ea3a7SDavid Howells if (copyflag || 3526716ea3a7SDavid Howells skb_cloned(skb1) || 3527716ea3a7SDavid Howells ntail || 3528716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 352921dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3530716ea3a7SDavid Howells struct sk_buff *skb2; 3531716ea3a7SDavid Howells 3532716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3533716ea3a7SDavid Howells if (ntail == 0) 3534716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3535716ea3a7SDavid Howells else 3536716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3537716ea3a7SDavid Howells skb_headroom(skb1), 3538716ea3a7SDavid Howells ntail, 3539716ea3a7SDavid Howells GFP_ATOMIC); 3540716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3541716ea3a7SDavid Howells return -ENOMEM; 3542716ea3a7SDavid Howells 3543716ea3a7SDavid Howells if (skb1->sk) 3544716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3545716ea3a7SDavid Howells 3546716ea3a7SDavid Howells /* Looking around. Are we still alive? 3547716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3548716ea3a7SDavid Howells 3549716ea3a7SDavid Howells skb2->next = skb1->next; 3550716ea3a7SDavid Howells *skb_p = skb2; 3551716ea3a7SDavid Howells kfree_skb(skb1); 3552716ea3a7SDavid Howells skb1 = skb2; 3553716ea3a7SDavid Howells } 3554716ea3a7SDavid Howells elt++; 3555716ea3a7SDavid Howells *trailer = skb1; 3556716ea3a7SDavid Howells skb_p = &skb1->next; 3557716ea3a7SDavid Howells } 3558716ea3a7SDavid Howells 3559716ea3a7SDavid Howells return elt; 3560716ea3a7SDavid Howells } 3561b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3562716ea3a7SDavid Howells 3563b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3564b1faf566SEric Dumazet { 3565b1faf566SEric Dumazet struct sock *sk = skb->sk; 3566b1faf566SEric Dumazet 3567b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3568b1faf566SEric Dumazet } 3569b1faf566SEric Dumazet 3570b1faf566SEric Dumazet /* 3571b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3572b1faf566SEric Dumazet */ 3573b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3574b1faf566SEric Dumazet { 3575b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 357695c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3577b1faf566SEric Dumazet return -ENOMEM; 3578b1faf566SEric Dumazet 3579b1faf566SEric Dumazet skb_orphan(skb); 3580b1faf566SEric Dumazet skb->sk = sk; 3581b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3582b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3583b1faf566SEric Dumazet 3584abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3585abb57ea4SEric Dumazet skb_dst_force(skb); 3586abb57ea4SEric Dumazet 3587b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3588b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3589676d2369SDavid S. Miller sk->sk_data_ready(sk); 3590b1faf566SEric Dumazet return 0; 3591b1faf566SEric Dumazet } 3592b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3593b1faf566SEric Dumazet 3594364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3595364a9e93SWillem de Bruijn { 3596364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 3597364a9e93SWillem de Bruijn struct sk_buff *skb, *skb_next; 3598997d5c3fSEric Dumazet unsigned long flags; 3599364a9e93SWillem de Bruijn int err = 0; 3600364a9e93SWillem de Bruijn 3601997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 3602364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 3603364a9e93SWillem de Bruijn if (skb && (skb_next = skb_peek(q))) 3604364a9e93SWillem de Bruijn err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 3605997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 3606364a9e93SWillem de Bruijn 3607364a9e93SWillem de Bruijn sk->sk_err = err; 3608364a9e93SWillem de Bruijn if (err) 3609364a9e93SWillem de Bruijn sk->sk_error_report(sk); 3610364a9e93SWillem de Bruijn 3611364a9e93SWillem de Bruijn return skb; 3612364a9e93SWillem de Bruijn } 3613364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 3614364a9e93SWillem de Bruijn 3615cab41c47SAlexander Duyck /** 3616cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 3617cab41c47SAlexander Duyck * @skb: the skb to clone 3618cab41c47SAlexander Duyck * 3619cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 3620cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 3621cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 3622cab41c47SAlexander Duyck * 3623cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 3624cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 3625cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 3626cab41c47SAlexander Duyck * the sk_error_queue. 3627cab41c47SAlexander Duyck */ 362862bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 362962bccb8cSAlexander Duyck { 363062bccb8cSAlexander Duyck struct sock *sk = skb->sk; 363162bccb8cSAlexander Duyck struct sk_buff *clone; 363262bccb8cSAlexander Duyck 363362bccb8cSAlexander Duyck if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) 363462bccb8cSAlexander Duyck return NULL; 363562bccb8cSAlexander Duyck 363662bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 363762bccb8cSAlexander Duyck if (!clone) { 363862bccb8cSAlexander Duyck sock_put(sk); 363962bccb8cSAlexander Duyck return NULL; 364062bccb8cSAlexander Duyck } 364162bccb8cSAlexander Duyck 364262bccb8cSAlexander Duyck clone->sk = sk; 364362bccb8cSAlexander Duyck clone->destructor = sock_efree; 364462bccb8cSAlexander Duyck 364562bccb8cSAlexander Duyck return clone; 364662bccb8cSAlexander Duyck } 364762bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 364862bccb8cSAlexander Duyck 364937846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 365037846ef0SAlexander Duyck struct sock *sk, 365137846ef0SAlexander Duyck int tstype) 3652ac45f602SPatrick Ohly { 3653ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3654ac45f602SPatrick Ohly int err; 3655ac45f602SPatrick Ohly 3656ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3657ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3658ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3659ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3660e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 36614ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 366209c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 36634ed2d765SWillem de Bruijn if (sk->sk_protocol == IPPROTO_TCP) 36644ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 36654ed2d765SWillem de Bruijn } 366629030374SEric Dumazet 3667ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 366829030374SEric Dumazet 3669ac45f602SPatrick Ohly if (err) 3670ac45f602SPatrick Ohly kfree_skb(skb); 3671ac45f602SPatrick Ohly } 367237846ef0SAlexander Duyck 3673b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 3674b245be1fSWillem de Bruijn { 3675b245be1fSWillem de Bruijn bool ret; 3676b245be1fSWillem de Bruijn 3677b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 3678b245be1fSWillem de Bruijn return true; 3679b245be1fSWillem de Bruijn 3680b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 3681b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 3682b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 3683b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 3684b245be1fSWillem de Bruijn return ret; 3685b245be1fSWillem de Bruijn } 3686b245be1fSWillem de Bruijn 368737846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 368837846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 368937846ef0SAlexander Duyck { 369037846ef0SAlexander Duyck struct sock *sk = skb->sk; 369137846ef0SAlexander Duyck 3692b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 3693b245be1fSWillem de Bruijn return; 3694b245be1fSWillem de Bruijn 369562bccb8cSAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 369662bccb8cSAlexander Duyck sock_hold(sk); 369737846ef0SAlexander Duyck 369837846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 369937846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); 370037846ef0SAlexander Duyck 370137846ef0SAlexander Duyck sock_put(sk); 370237846ef0SAlexander Duyck } 370337846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 370437846ef0SAlexander Duyck 370537846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 370637846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 370737846ef0SAlexander Duyck struct sock *sk, int tstype) 370837846ef0SAlexander Duyck { 370937846ef0SAlexander Duyck struct sk_buff *skb; 37103a8dd971SWillem de Bruijn bool tsonly; 371137846ef0SAlexander Duyck 37123a8dd971SWillem de Bruijn if (!sk) 37133a8dd971SWillem de Bruijn return; 37143a8dd971SWillem de Bruijn 37153a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 37163a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 371737846ef0SAlexander Duyck return; 371837846ef0SAlexander Duyck 371949ca0d8bSWillem de Bruijn if (tsonly) 372049ca0d8bSWillem de Bruijn skb = alloc_skb(0, GFP_ATOMIC); 372137846ef0SAlexander Duyck else 372237846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 372337846ef0SAlexander Duyck if (!skb) 372437846ef0SAlexander Duyck return; 372537846ef0SAlexander Duyck 372649ca0d8bSWillem de Bruijn if (tsonly) { 372749ca0d8bSWillem de Bruijn skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags; 372849ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 372949ca0d8bSWillem de Bruijn } 373049ca0d8bSWillem de Bruijn 373149ca0d8bSWillem de Bruijn if (hwtstamps) 373249ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 373349ca0d8bSWillem de Bruijn else 373449ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 373549ca0d8bSWillem de Bruijn 373637846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, tstype); 373737846ef0SAlexander Duyck } 3738e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3739e7fd2885SWillem de Bruijn 3740e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 3741e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 3742e7fd2885SWillem de Bruijn { 3743e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3744e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 3745e7fd2885SWillem de Bruijn } 3746ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3747ac45f602SPatrick Ohly 37486e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 37496e3e939fSJohannes Berg { 37506e3e939fSJohannes Berg struct sock *sk = skb->sk; 37516e3e939fSJohannes Berg struct sock_exterr_skb *serr; 37526e3e939fSJohannes Berg int err; 37536e3e939fSJohannes Berg 37546e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 37556e3e939fSJohannes Berg skb->wifi_acked = acked; 37566e3e939fSJohannes Berg 37576e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 37586e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 37596e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 37606e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 37616e3e939fSJohannes Berg 3762bf7fa551SAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 3763bf7fa551SAlexander Duyck sock_hold(sk); 3764bf7fa551SAlexander Duyck 37656e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 37666e3e939fSJohannes Berg if (err) 37676e3e939fSJohannes Berg kfree_skb(skb); 3768bf7fa551SAlexander Duyck 3769bf7fa551SAlexander Duyck sock_put(sk); 37706e3e939fSJohannes Berg } 37716e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 37726e3e939fSJohannes Berg 3773f35d9d8aSRusty Russell /** 3774f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3775f35d9d8aSRusty Russell * @skb: the skb to set 3776f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3777f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3778f35d9d8aSRusty Russell * 3779f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3780f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3781f35d9d8aSRusty Russell * 3782f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3783f35d9d8aSRusty Russell * returns false you should drop the packet. 3784f35d9d8aSRusty Russell */ 3785f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3786f35d9d8aSRusty Russell { 37875ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 37885ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3789e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 37905ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3791f35d9d8aSRusty Russell return false; 3792f35d9d8aSRusty Russell } 3793f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3794f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3795f35d9d8aSRusty Russell skb->csum_offset = off; 3796e5d5decaSJason Wang skb_set_transport_header(skb, start); 3797f35d9d8aSRusty Russell return true; 3798f35d9d8aSRusty Russell } 3799b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3800f35d9d8aSRusty Russell 3801ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 3802ed1f50c3SPaul Durrant unsigned int max) 3803ed1f50c3SPaul Durrant { 3804ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 3805ed1f50c3SPaul Durrant return 0; 3806ed1f50c3SPaul Durrant 3807ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 3808ed1f50c3SPaul Durrant * won't need to do it again. 3809ed1f50c3SPaul Durrant */ 3810ed1f50c3SPaul Durrant if (max > skb->len) 3811ed1f50c3SPaul Durrant max = skb->len; 3812ed1f50c3SPaul Durrant 3813ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 3814ed1f50c3SPaul Durrant return -ENOMEM; 3815ed1f50c3SPaul Durrant 3816ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 3817ed1f50c3SPaul Durrant return -EPROTO; 3818ed1f50c3SPaul Durrant 3819ed1f50c3SPaul Durrant return 0; 3820ed1f50c3SPaul Durrant } 3821ed1f50c3SPaul Durrant 3822f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 3823f9708b43SJan Beulich 3824f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 3825f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 3826f9708b43SJan Beulich unsigned int off) 3827f9708b43SJan Beulich { 3828f9708b43SJan Beulich switch (proto) { 3829f9708b43SJan Beulich int err; 3830f9708b43SJan Beulich 3831f9708b43SJan Beulich case IPPROTO_TCP: 3832f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 3833f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 3834f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3835f9708b43SJan Beulich offsetof(struct tcphdr, 3836f9708b43SJan Beulich check))) 3837f9708b43SJan Beulich err = -EPROTO; 3838f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 3839f9708b43SJan Beulich 3840f9708b43SJan Beulich case IPPROTO_UDP: 3841f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 3842f9708b43SJan Beulich off + sizeof(struct udphdr)); 3843f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3844f9708b43SJan Beulich offsetof(struct udphdr, 3845f9708b43SJan Beulich check))) 3846f9708b43SJan Beulich err = -EPROTO; 3847f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 3848f9708b43SJan Beulich } 3849f9708b43SJan Beulich 3850f9708b43SJan Beulich return ERR_PTR(-EPROTO); 3851f9708b43SJan Beulich } 3852f9708b43SJan Beulich 3853ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3854ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 3855ed1f50c3SPaul Durrant */ 3856ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 3857ed1f50c3SPaul Durrant 3858f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 3859ed1f50c3SPaul Durrant { 3860ed1f50c3SPaul Durrant unsigned int off; 3861ed1f50c3SPaul Durrant bool fragment; 3862f9708b43SJan Beulich __sum16 *csum; 3863ed1f50c3SPaul Durrant int err; 3864ed1f50c3SPaul Durrant 3865ed1f50c3SPaul Durrant fragment = false; 3866ed1f50c3SPaul Durrant 3867ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3868ed1f50c3SPaul Durrant sizeof(struct iphdr), 3869ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 3870ed1f50c3SPaul Durrant if (err < 0) 3871ed1f50c3SPaul Durrant goto out; 3872ed1f50c3SPaul Durrant 3873ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 3874ed1f50c3SPaul Durrant fragment = true; 3875ed1f50c3SPaul Durrant 3876ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 3877ed1f50c3SPaul Durrant 3878ed1f50c3SPaul Durrant err = -EPROTO; 3879ed1f50c3SPaul Durrant 3880ed1f50c3SPaul Durrant if (fragment) 3881ed1f50c3SPaul Durrant goto out; 3882ed1f50c3SPaul Durrant 3883f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 3884f9708b43SJan Beulich if (IS_ERR(csum)) 3885f9708b43SJan Beulich return PTR_ERR(csum); 3886ed1f50c3SPaul Durrant 3887ed1f50c3SPaul Durrant if (recalculate) 3888f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 3889ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 3890ed1f50c3SPaul Durrant skb->len - off, 3891f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 3892ed1f50c3SPaul Durrant err = 0; 3893ed1f50c3SPaul Durrant 3894ed1f50c3SPaul Durrant out: 3895ed1f50c3SPaul Durrant return err; 3896ed1f50c3SPaul Durrant } 3897ed1f50c3SPaul Durrant 3898ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3899ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 3900ed1f50c3SPaul Durrant */ 3901ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 3902ed1f50c3SPaul Durrant 3903ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 3904ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 3905ed1f50c3SPaul Durrant 3906ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 3907ed1f50c3SPaul Durrant { 3908ed1f50c3SPaul Durrant int err; 3909ed1f50c3SPaul Durrant u8 nexthdr; 3910ed1f50c3SPaul Durrant unsigned int off; 3911ed1f50c3SPaul Durrant unsigned int len; 3912ed1f50c3SPaul Durrant bool fragment; 3913ed1f50c3SPaul Durrant bool done; 3914f9708b43SJan Beulich __sum16 *csum; 3915ed1f50c3SPaul Durrant 3916ed1f50c3SPaul Durrant fragment = false; 3917ed1f50c3SPaul Durrant done = false; 3918ed1f50c3SPaul Durrant 3919ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 3920ed1f50c3SPaul Durrant 3921ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 3922ed1f50c3SPaul Durrant if (err < 0) 3923ed1f50c3SPaul Durrant goto out; 3924ed1f50c3SPaul Durrant 3925ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 3926ed1f50c3SPaul Durrant 3927ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 3928ed1f50c3SPaul Durrant while (off <= len && !done) { 3929ed1f50c3SPaul Durrant switch (nexthdr) { 3930ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 3931ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 3932ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 3933ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 3934ed1f50c3SPaul Durrant 3935ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3936ed1f50c3SPaul Durrant off + 3937ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 3938ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3939ed1f50c3SPaul Durrant if (err < 0) 3940ed1f50c3SPaul Durrant goto out; 3941ed1f50c3SPaul Durrant 3942ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 3943ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3944ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 3945ed1f50c3SPaul Durrant break; 3946ed1f50c3SPaul Durrant } 3947ed1f50c3SPaul Durrant case IPPROTO_AH: { 3948ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 3949ed1f50c3SPaul Durrant 3950ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3951ed1f50c3SPaul Durrant off + 3952ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 3953ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3954ed1f50c3SPaul Durrant if (err < 0) 3955ed1f50c3SPaul Durrant goto out; 3956ed1f50c3SPaul Durrant 3957ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 3958ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3959ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 3960ed1f50c3SPaul Durrant break; 3961ed1f50c3SPaul Durrant } 3962ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 3963ed1f50c3SPaul Durrant struct frag_hdr *hp; 3964ed1f50c3SPaul Durrant 3965ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3966ed1f50c3SPaul Durrant off + 3967ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 3968ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3969ed1f50c3SPaul Durrant if (err < 0) 3970ed1f50c3SPaul Durrant goto out; 3971ed1f50c3SPaul Durrant 3972ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 3973ed1f50c3SPaul Durrant 3974ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 3975ed1f50c3SPaul Durrant fragment = true; 3976ed1f50c3SPaul Durrant 3977ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3978ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 3979ed1f50c3SPaul Durrant break; 3980ed1f50c3SPaul Durrant } 3981ed1f50c3SPaul Durrant default: 3982ed1f50c3SPaul Durrant done = true; 3983ed1f50c3SPaul Durrant break; 3984ed1f50c3SPaul Durrant } 3985ed1f50c3SPaul Durrant } 3986ed1f50c3SPaul Durrant 3987ed1f50c3SPaul Durrant err = -EPROTO; 3988ed1f50c3SPaul Durrant 3989ed1f50c3SPaul Durrant if (!done || fragment) 3990ed1f50c3SPaul Durrant goto out; 3991ed1f50c3SPaul Durrant 3992f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 3993f9708b43SJan Beulich if (IS_ERR(csum)) 3994f9708b43SJan Beulich return PTR_ERR(csum); 3995ed1f50c3SPaul Durrant 3996ed1f50c3SPaul Durrant if (recalculate) 3997f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 3998ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 3999f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4000ed1f50c3SPaul Durrant err = 0; 4001ed1f50c3SPaul Durrant 4002ed1f50c3SPaul Durrant out: 4003ed1f50c3SPaul Durrant return err; 4004ed1f50c3SPaul Durrant } 4005ed1f50c3SPaul Durrant 4006ed1f50c3SPaul Durrant /** 4007ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4008ed1f50c3SPaul Durrant * @skb: the skb to set up 4009ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4010ed1f50c3SPaul Durrant */ 4011ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4012ed1f50c3SPaul Durrant { 4013ed1f50c3SPaul Durrant int err; 4014ed1f50c3SPaul Durrant 4015ed1f50c3SPaul Durrant switch (skb->protocol) { 4016ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4017f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4018ed1f50c3SPaul Durrant break; 4019ed1f50c3SPaul Durrant 4020ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4021ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4022ed1f50c3SPaul Durrant break; 4023ed1f50c3SPaul Durrant 4024ed1f50c3SPaul Durrant default: 4025ed1f50c3SPaul Durrant err = -EPROTO; 4026ed1f50c3SPaul Durrant break; 4027ed1f50c3SPaul Durrant } 4028ed1f50c3SPaul Durrant 4029ed1f50c3SPaul Durrant return err; 4030ed1f50c3SPaul Durrant } 4031ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4032ed1f50c3SPaul Durrant 40334497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 40344497b076SBen Hutchings { 4035e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4036e87cc472SJoe Perches skb->dev->name); 40374497b076SBen Hutchings } 40384497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4039bad43ca8SEric Dumazet 4040bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4041bad43ca8SEric Dumazet { 40423d861f66SEric Dumazet if (head_stolen) { 40433d861f66SEric Dumazet skb_release_head_state(skb); 4044bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 40453d861f66SEric Dumazet } else { 4046bad43ca8SEric Dumazet __kfree_skb(skb); 4047bad43ca8SEric Dumazet } 40483d861f66SEric Dumazet } 4049bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4050bad43ca8SEric Dumazet 4051bad43ca8SEric Dumazet /** 4052bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4053bad43ca8SEric Dumazet * @to: prior buffer 4054bad43ca8SEric Dumazet * @from: buffer to add 4055bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4056c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4057bad43ca8SEric Dumazet */ 4058bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4059bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4060bad43ca8SEric Dumazet { 4061bad43ca8SEric Dumazet int i, delta, len = from->len; 4062bad43ca8SEric Dumazet 4063bad43ca8SEric Dumazet *fragstolen = false; 4064bad43ca8SEric Dumazet 4065bad43ca8SEric Dumazet if (skb_cloned(to)) 4066bad43ca8SEric Dumazet return false; 4067bad43ca8SEric Dumazet 4068bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4069e93a0435SEric Dumazet if (len) 4070bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4071bad43ca8SEric Dumazet *delta_truesize = 0; 4072bad43ca8SEric Dumazet return true; 4073bad43ca8SEric Dumazet } 4074bad43ca8SEric Dumazet 4075bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 4076bad43ca8SEric Dumazet return false; 4077bad43ca8SEric Dumazet 4078bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4079bad43ca8SEric Dumazet struct page *page; 4080bad43ca8SEric Dumazet unsigned int offset; 4081bad43ca8SEric Dumazet 4082bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4083bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 4084bad43ca8SEric Dumazet return false; 4085bad43ca8SEric Dumazet 4086bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4087bad43ca8SEric Dumazet return false; 4088bad43ca8SEric Dumazet 4089bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4090bad43ca8SEric Dumazet 4091bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4092bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4093bad43ca8SEric Dumazet 4094bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 4095bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4096bad43ca8SEric Dumazet *fragstolen = true; 4097bad43ca8SEric Dumazet } else { 4098bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4099bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4100bad43ca8SEric Dumazet return false; 4101bad43ca8SEric Dumazet 4102f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4103bad43ca8SEric Dumazet } 4104bad43ca8SEric Dumazet 4105bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4106bad43ca8SEric Dumazet 4107bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4108bad43ca8SEric Dumazet skb_shinfo(from)->frags, 4109bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4110bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4111bad43ca8SEric Dumazet 4112bad43ca8SEric Dumazet if (!skb_cloned(from)) 4113bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 4114bad43ca8SEric Dumazet 41158ea853fdSLi RongQing /* if the skb is not cloned this does nothing 41168ea853fdSLi RongQing * since we set nr_frags to 0. 41178ea853fdSLi RongQing */ 4118bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4119bad43ca8SEric Dumazet skb_frag_ref(from, i); 4120bad43ca8SEric Dumazet 4121bad43ca8SEric Dumazet to->truesize += delta; 4122bad43ca8SEric Dumazet to->len += len; 4123bad43ca8SEric Dumazet to->data_len += len; 4124bad43ca8SEric Dumazet 4125bad43ca8SEric Dumazet *delta_truesize = delta; 4126bad43ca8SEric Dumazet return true; 4127bad43ca8SEric Dumazet } 4128bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4129621e84d6SNicolas Dichtel 4130621e84d6SNicolas Dichtel /** 41318b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4132621e84d6SNicolas Dichtel * 4133621e84d6SNicolas Dichtel * @skb: buffer to clean 41348b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4135621e84d6SNicolas Dichtel * 41368b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 41378b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 41388b27f277SNicolas Dichtel * operations. 41398b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 41408b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 41418b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4142621e84d6SNicolas Dichtel */ 41438b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4144621e84d6SNicolas Dichtel { 4145621e84d6SNicolas Dichtel skb->tstamp.tv64 = 0; 4146621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4147621e84d6SNicolas Dichtel skb->skb_iif = 0; 414860ff7467SWANG Cong skb->ignore_df = 0; 4149621e84d6SNicolas Dichtel skb_dst_drop(skb); 4150c29390c6SEric Dumazet skb_sender_cpu_clear(skb); 4151621e84d6SNicolas Dichtel secpath_reset(skb); 4152621e84d6SNicolas Dichtel nf_reset(skb); 4153621e84d6SNicolas Dichtel nf_reset_trace(skb); 4154213dd74aSHerbert Xu 4155213dd74aSHerbert Xu if (!xnet) 4156213dd74aSHerbert Xu return; 4157213dd74aSHerbert Xu 4158213dd74aSHerbert Xu skb_orphan(skb); 4159213dd74aSHerbert Xu skb->mark = 0; 4160621e84d6SNicolas Dichtel } 4161621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4162de960aa9SFlorian Westphal 4163de960aa9SFlorian Westphal /** 4164de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4165de960aa9SFlorian Westphal * 4166de960aa9SFlorian Westphal * @skb: GSO skb 4167de960aa9SFlorian Westphal * 4168de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4169de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4170de960aa9SFlorian Westphal * 4171de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4172de960aa9SFlorian Westphal */ 4173de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4174de960aa9SFlorian Westphal { 4175de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4176f993bc25SFlorian Westphal unsigned int thlen = 0; 4177f993bc25SFlorian Westphal 4178f993bc25SFlorian Westphal if (skb->encapsulation) { 4179f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4180f993bc25SFlorian Westphal skb_transport_header(skb); 4181de960aa9SFlorian Westphal 4182de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4183f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4184f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4185f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 4186f993bc25SFlorian Westphal } 41876d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 41886d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 41896d39d589SFlorian Westphal * accounted for. 41906d39d589SFlorian Westphal */ 4191f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4192de960aa9SFlorian Westphal } 4193de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 41940d5501c1SVlad Yasevich 41950d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 41960d5501c1SVlad Yasevich { 41970d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 41980d5501c1SVlad Yasevich kfree_skb(skb); 41990d5501c1SVlad Yasevich return NULL; 42000d5501c1SVlad Yasevich } 42010d5501c1SVlad Yasevich 42020d5501c1SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - VLAN_ETH_HLEN, 2 * ETH_ALEN); 42030d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 42040d5501c1SVlad Yasevich return skb; 42050d5501c1SVlad Yasevich } 42060d5501c1SVlad Yasevich 42070d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 42080d5501c1SVlad Yasevich { 42090d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 42100d5501c1SVlad Yasevich u16 vlan_tci; 42110d5501c1SVlad Yasevich 4212df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 42130d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 42140d5501c1SVlad Yasevich return skb; 42150d5501c1SVlad Yasevich } 42160d5501c1SVlad Yasevich 42170d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 42180d5501c1SVlad Yasevich if (unlikely(!skb)) 42190d5501c1SVlad Yasevich goto err_free; 42200d5501c1SVlad Yasevich 42210d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 42220d5501c1SVlad Yasevich goto err_free; 42230d5501c1SVlad Yasevich 42240d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 42250d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 42260d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 42270d5501c1SVlad Yasevich 42280d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 42290d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 42300d5501c1SVlad Yasevich 42310d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 42320d5501c1SVlad Yasevich if (unlikely(!skb)) 42330d5501c1SVlad Yasevich goto err_free; 42340d5501c1SVlad Yasevich 42350d5501c1SVlad Yasevich skb_reset_network_header(skb); 42360d5501c1SVlad Yasevich skb_reset_transport_header(skb); 42370d5501c1SVlad Yasevich skb_reset_mac_len(skb); 42380d5501c1SVlad Yasevich 42390d5501c1SVlad Yasevich return skb; 42400d5501c1SVlad Yasevich 42410d5501c1SVlad Yasevich err_free: 42420d5501c1SVlad Yasevich kfree_skb(skb); 42430d5501c1SVlad Yasevich return NULL; 42440d5501c1SVlad Yasevich } 42450d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 42462e4e4410SEric Dumazet 4247e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 4248e2195121SJiri Pirko { 4249e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 4250e2195121SJiri Pirko return -ENOMEM; 4251e2195121SJiri Pirko 4252e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4253e2195121SJiri Pirko return 0; 4254e2195121SJiri Pirko 4255e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 4256e2195121SJiri Pirko } 4257e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 4258e2195121SJiri Pirko 425993515d53SJiri Pirko /* remove VLAN header from packet and update csum accordingly. */ 426093515d53SJiri Pirko static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 426193515d53SJiri Pirko { 426293515d53SJiri Pirko struct vlan_hdr *vhdr; 426393515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 426493515d53SJiri Pirko int err; 426593515d53SJiri Pirko 426693515d53SJiri Pirko __skb_push(skb, offset); 426793515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 426893515d53SJiri Pirko if (unlikely(err)) 426993515d53SJiri Pirko goto pull; 427093515d53SJiri Pirko 427193515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 427293515d53SJiri Pirko 427393515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 427493515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 427593515d53SJiri Pirko 427693515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 427793515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 427893515d53SJiri Pirko 427993515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 428093515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 428193515d53SJiri Pirko 428293515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 428393515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 428493515d53SJiri Pirko 428593515d53SJiri Pirko skb_reset_mac_len(skb); 428693515d53SJiri Pirko pull: 428793515d53SJiri Pirko __skb_pull(skb, offset); 428893515d53SJiri Pirko 428993515d53SJiri Pirko return err; 429093515d53SJiri Pirko } 429193515d53SJiri Pirko 429293515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 429393515d53SJiri Pirko { 429493515d53SJiri Pirko u16 vlan_tci; 429593515d53SJiri Pirko __be16 vlan_proto; 429693515d53SJiri Pirko int err; 429793515d53SJiri Pirko 4298df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 429993515d53SJiri Pirko skb->vlan_tci = 0; 430093515d53SJiri Pirko } else { 430193515d53SJiri Pirko if (unlikely((skb->protocol != htons(ETH_P_8021Q) && 430293515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 430393515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 430493515d53SJiri Pirko return 0; 430593515d53SJiri Pirko 430693515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 430793515d53SJiri Pirko if (err) 430893515d53SJiri Pirko return err; 430993515d53SJiri Pirko } 431093515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 431193515d53SJiri Pirko if (likely((skb->protocol != htons(ETH_P_8021Q) && 431293515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 431393515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 431493515d53SJiri Pirko return 0; 431593515d53SJiri Pirko 431693515d53SJiri Pirko vlan_proto = skb->protocol; 431793515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 431893515d53SJiri Pirko if (unlikely(err)) 431993515d53SJiri Pirko return err; 432093515d53SJiri Pirko 432193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 432293515d53SJiri Pirko return 0; 432393515d53SJiri Pirko } 432493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 432593515d53SJiri Pirko 432693515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 432793515d53SJiri Pirko { 4328df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 432993515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 433093515d53SJiri Pirko int err; 433193515d53SJiri Pirko 433293515d53SJiri Pirko /* __vlan_insert_tag expect skb->data pointing to mac header. 433393515d53SJiri Pirko * So change skb->data before calling it and change back to 433493515d53SJiri Pirko * original position later 433593515d53SJiri Pirko */ 433693515d53SJiri Pirko __skb_push(skb, offset); 433793515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 4338df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 433993515d53SJiri Pirko if (err) 434093515d53SJiri Pirko return err; 434193515d53SJiri Pirko skb->protocol = skb->vlan_proto; 434293515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 434393515d53SJiri Pirko __skb_pull(skb, offset); 434493515d53SJiri Pirko 434593515d53SJiri Pirko if (skb->ip_summed == CHECKSUM_COMPLETE) 434693515d53SJiri Pirko skb->csum = csum_add(skb->csum, csum_partial(skb->data 434793515d53SJiri Pirko + (2 * ETH_ALEN), VLAN_HLEN, 0)); 434893515d53SJiri Pirko } 434993515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 435093515d53SJiri Pirko return 0; 435193515d53SJiri Pirko } 435293515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 435393515d53SJiri Pirko 43542e4e4410SEric Dumazet /** 43552e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 43562e4e4410SEric Dumazet * 4357de3f0d0eSMasanari Iida * @header_len: size of linear part 4358de3f0d0eSMasanari Iida * @data_len: needed length in frags 4359de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 4360de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 4361de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 43622e4e4410SEric Dumazet * 43632e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 43642e4e4410SEric Dumazet */ 43652e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 43662e4e4410SEric Dumazet unsigned long data_len, 43672e4e4410SEric Dumazet int max_page_order, 43682e4e4410SEric Dumazet int *errcode, 43692e4e4410SEric Dumazet gfp_t gfp_mask) 43702e4e4410SEric Dumazet { 43712e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 43722e4e4410SEric Dumazet unsigned long chunk; 43732e4e4410SEric Dumazet struct sk_buff *skb; 43742e4e4410SEric Dumazet struct page *page; 43752e4e4410SEric Dumazet gfp_t gfp_head; 43762e4e4410SEric Dumazet int i; 43772e4e4410SEric Dumazet 43782e4e4410SEric Dumazet *errcode = -EMSGSIZE; 43792e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 43802e4e4410SEric Dumazet * high order pages... 43812e4e4410SEric Dumazet */ 43822e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 43832e4e4410SEric Dumazet return NULL; 43842e4e4410SEric Dumazet 43852e4e4410SEric Dumazet gfp_head = gfp_mask; 43862e4e4410SEric Dumazet if (gfp_head & __GFP_WAIT) 43872e4e4410SEric Dumazet gfp_head |= __GFP_REPEAT; 43882e4e4410SEric Dumazet 43892e4e4410SEric Dumazet *errcode = -ENOBUFS; 43902e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 43912e4e4410SEric Dumazet if (!skb) 43922e4e4410SEric Dumazet return NULL; 43932e4e4410SEric Dumazet 43942e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 43952e4e4410SEric Dumazet 43962e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 43972e4e4410SEric Dumazet int order = max_page_order; 43982e4e4410SEric Dumazet 43992e4e4410SEric Dumazet while (order) { 44002e4e4410SEric Dumazet if (npages >= 1 << order) { 44012e4e4410SEric Dumazet page = alloc_pages(gfp_mask | 44022e4e4410SEric Dumazet __GFP_COMP | 44032e4e4410SEric Dumazet __GFP_NOWARN | 44042e4e4410SEric Dumazet __GFP_NORETRY, 44052e4e4410SEric Dumazet order); 44062e4e4410SEric Dumazet if (page) 44072e4e4410SEric Dumazet goto fill_page; 44082e4e4410SEric Dumazet /* Do not retry other high order allocations */ 44092e4e4410SEric Dumazet order = 1; 44102e4e4410SEric Dumazet max_page_order = 0; 44112e4e4410SEric Dumazet } 44122e4e4410SEric Dumazet order--; 44132e4e4410SEric Dumazet } 44142e4e4410SEric Dumazet page = alloc_page(gfp_mask); 44152e4e4410SEric Dumazet if (!page) 44162e4e4410SEric Dumazet goto failure; 44172e4e4410SEric Dumazet fill_page: 44182e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 44192e4e4410SEric Dumazet PAGE_SIZE << order); 44202e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 44212e4e4410SEric Dumazet data_len -= chunk; 44222e4e4410SEric Dumazet npages -= 1 << order; 44232e4e4410SEric Dumazet } 44242e4e4410SEric Dumazet return skb; 44252e4e4410SEric Dumazet 44262e4e4410SEric Dumazet failure: 44272e4e4410SEric Dumazet kfree_skb(skb); 44282e4e4410SEric Dumazet return NULL; 44292e4e4410SEric Dumazet } 44302e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 4431