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; 3432f064f34SMichal Hocko if (page_is_pfmemalloc(virt_to_head_page(data))) 3442ea2f62cSEric Dumazet skb->pfmemalloc = 1; 3452ea2f62cSEric Dumazet } 3462ea2f62cSEric Dumazet return skb; 3472ea2f62cSEric Dumazet } 348b2b5ce9dSEric Dumazet EXPORT_SYMBOL(build_skb); 349b2b5ce9dSEric Dumazet 350795bb1c0SJesper Dangaard Brouer #define NAPI_SKB_CACHE_SIZE 64 351795bb1c0SJesper Dangaard Brouer 352795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache { 353795bb1c0SJesper Dangaard Brouer struct page_frag_cache page; 354795bb1c0SJesper Dangaard Brouer size_t skb_count; 355795bb1c0SJesper Dangaard Brouer void *skb_cache[NAPI_SKB_CACHE_SIZE]; 356795bb1c0SJesper Dangaard Brouer }; 357795bb1c0SJesper Dangaard Brouer 358b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 359795bb1c0SJesper Dangaard Brouer static DEFINE_PER_CPU(struct napi_alloc_cache, napi_alloc_cache); 360ffde7328SAlexander Duyck 361ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 362ffde7328SAlexander Duyck { 363b63ae8caSAlexander Duyck struct page_frag_cache *nc; 364ffde7328SAlexander Duyck unsigned long flags; 365ffde7328SAlexander Duyck void *data; 366ffde7328SAlexander Duyck 367ffde7328SAlexander Duyck local_irq_save(flags); 3689451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 3699451980aSAlexander Duyck data = __alloc_page_frag(nc, fragsz, gfp_mask); 3706f532612SEric Dumazet local_irq_restore(flags); 3716f532612SEric Dumazet return data; 3726f532612SEric Dumazet } 373c93bdd0eSMel Gorman 374c93bdd0eSMel Gorman /** 375c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 376c93bdd0eSMel Gorman * @fragsz: fragment size 377c93bdd0eSMel Gorman * 378c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 379c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 380c93bdd0eSMel Gorman */ 381c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 382c93bdd0eSMel Gorman { 383c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 384c93bdd0eSMel Gorman } 3856f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3866f532612SEric Dumazet 387ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 388ffde7328SAlexander Duyck { 389795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3909451980aSAlexander Duyck 391795bb1c0SJesper Dangaard Brouer return __alloc_page_frag(&nc->page, fragsz, gfp_mask); 392ffde7328SAlexander Duyck } 393ffde7328SAlexander Duyck 394ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 395ffde7328SAlexander Duyck { 396ffde7328SAlexander Duyck return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 397ffde7328SAlexander Duyck } 398ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 399ffde7328SAlexander Duyck 4006f532612SEric Dumazet /** 401fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 402fd11a83dSAlexander Duyck * @dev: network device to receive on 403d7499160SMasanari Iida * @len: length to allocate 404fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 405fd11a83dSAlexander Duyck * 406fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 407fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 408fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 409fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 410fd11a83dSAlexander Duyck * 411fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 412fd11a83dSAlexander Duyck */ 4139451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 4149451980aSAlexander Duyck gfp_t gfp_mask) 415fd11a83dSAlexander Duyck { 416b63ae8caSAlexander Duyck struct page_frag_cache *nc; 4179451980aSAlexander Duyck unsigned long flags; 418fd11a83dSAlexander Duyck struct sk_buff *skb; 4199451980aSAlexander Duyck bool pfmemalloc; 4209451980aSAlexander Duyck void *data; 421fd11a83dSAlexander Duyck 4229451980aSAlexander Duyck len += NET_SKB_PAD; 423fd11a83dSAlexander Duyck 4249451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 425d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 426a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 427a080e7bdSAlexander Duyck if (!skb) 428a080e7bdSAlexander Duyck goto skb_fail; 429a080e7bdSAlexander Duyck goto skb_success; 430a080e7bdSAlexander Duyck } 4319451980aSAlexander Duyck 4329451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4339451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4349451980aSAlexander Duyck 4359451980aSAlexander Duyck if (sk_memalloc_socks()) 4369451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4379451980aSAlexander Duyck 4389451980aSAlexander Duyck local_irq_save(flags); 4399451980aSAlexander Duyck 4409451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4419451980aSAlexander Duyck data = __alloc_page_frag(nc, len, gfp_mask); 4429451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 4439451980aSAlexander Duyck 4449451980aSAlexander Duyck local_irq_restore(flags); 4459451980aSAlexander Duyck 4469451980aSAlexander Duyck if (unlikely(!data)) 4479451980aSAlexander Duyck return NULL; 4489451980aSAlexander Duyck 4499451980aSAlexander Duyck skb = __build_skb(data, len); 4509451980aSAlexander Duyck if (unlikely(!skb)) { 451181edb2bSAlexander Duyck skb_free_frag(data); 4529451980aSAlexander Duyck return NULL; 4539451980aSAlexander Duyck } 4549451980aSAlexander Duyck 4559451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 4569451980aSAlexander Duyck if (pfmemalloc) 4579451980aSAlexander Duyck skb->pfmemalloc = 1; 4589451980aSAlexander Duyck skb->head_frag = 1; 4599451980aSAlexander Duyck 460a080e7bdSAlexander Duyck skb_success: 4618af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4627b2e497aSChristoph Hellwig skb->dev = dev; 463fd11a83dSAlexander Duyck 464a080e7bdSAlexander Duyck skb_fail: 4658af27456SChristoph Hellwig return skb; 4668af27456SChristoph Hellwig } 467b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4681da177e4SLinus Torvalds 469fd11a83dSAlexander Duyck /** 470fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 471fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 472d7499160SMasanari Iida * @len: length to allocate 473fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 474fd11a83dSAlexander Duyck * 475fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 476fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 477fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 478fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 479fd11a83dSAlexander Duyck * 480fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 481fd11a83dSAlexander Duyck */ 4829451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 4839451980aSAlexander Duyck gfp_t gfp_mask) 484fd11a83dSAlexander Duyck { 485795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 486fd11a83dSAlexander Duyck struct sk_buff *skb; 4879451980aSAlexander Duyck void *data; 488fd11a83dSAlexander Duyck 4899451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 490fd11a83dSAlexander Duyck 4919451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 492d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 493a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 494a080e7bdSAlexander Duyck if (!skb) 495a080e7bdSAlexander Duyck goto skb_fail; 496a080e7bdSAlexander Duyck goto skb_success; 497a080e7bdSAlexander Duyck } 4989451980aSAlexander Duyck 4999451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 5009451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 5019451980aSAlexander Duyck 5029451980aSAlexander Duyck if (sk_memalloc_socks()) 5039451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 5049451980aSAlexander Duyck 505795bb1c0SJesper Dangaard Brouer data = __alloc_page_frag(&nc->page, len, gfp_mask); 5069451980aSAlexander Duyck if (unlikely(!data)) 5079451980aSAlexander Duyck return NULL; 5089451980aSAlexander Duyck 5099451980aSAlexander Duyck skb = __build_skb(data, len); 5109451980aSAlexander Duyck if (unlikely(!skb)) { 511181edb2bSAlexander Duyck skb_free_frag(data); 5129451980aSAlexander Duyck return NULL; 5139451980aSAlexander Duyck } 5149451980aSAlexander Duyck 5159451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 516795bb1c0SJesper Dangaard Brouer if (nc->page.pfmemalloc) 5179451980aSAlexander Duyck skb->pfmemalloc = 1; 5189451980aSAlexander Duyck skb->head_frag = 1; 5199451980aSAlexander Duyck 520a080e7bdSAlexander Duyck skb_success: 521fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 522fd11a83dSAlexander Duyck skb->dev = napi->dev; 523fd11a83dSAlexander Duyck 524a080e7bdSAlexander Duyck skb_fail: 525fd11a83dSAlexander Duyck return skb; 526fd11a83dSAlexander Duyck } 527fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 528fd11a83dSAlexander Duyck 529654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 53050269e19SEric Dumazet int size, unsigned int truesize) 531654bed16SPeter Zijlstra { 532654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 533654bed16SPeter Zijlstra skb->len += size; 534654bed16SPeter Zijlstra skb->data_len += size; 53550269e19SEric Dumazet skb->truesize += truesize; 536654bed16SPeter Zijlstra } 537654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 538654bed16SPeter Zijlstra 539f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 540f8e617e1SJason Wang unsigned int truesize) 541f8e617e1SJason Wang { 542f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 543f8e617e1SJason Wang 544f8e617e1SJason Wang skb_frag_size_add(frag, size); 545f8e617e1SJason Wang skb->len += size; 546f8e617e1SJason Wang skb->data_len += size; 547f8e617e1SJason Wang skb->truesize += truesize; 548f8e617e1SJason Wang } 549f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 550f8e617e1SJason Wang 55127b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5521da177e4SLinus Torvalds { 553bd8a7036SEric Dumazet kfree_skb_list(*listp); 55427b437c8SHerbert Xu *listp = NULL; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 55727b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 55827b437c8SHerbert Xu { 55927b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 56027b437c8SHerbert Xu } 56127b437c8SHerbert Xu 5621da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds struct sk_buff *list; 5651da177e4SLinus Torvalds 566fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5671da177e4SLinus Torvalds skb_get(list); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 570d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 571d3836f21SEric Dumazet { 572181edb2bSAlexander Duyck unsigned char *head = skb->head; 573181edb2bSAlexander Duyck 574d3836f21SEric Dumazet if (skb->head_frag) 575181edb2bSAlexander Duyck skb_free_frag(head); 576d3836f21SEric Dumazet else 577181edb2bSAlexander Duyck kfree(head); 578d3836f21SEric Dumazet } 579d3836f21SEric Dumazet 5805bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5811da177e4SLinus Torvalds { 582ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5831da177e4SLinus Torvalds int i; 584ff04a771SEric Dumazet 585ff04a771SEric Dumazet if (skb->cloned && 586ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 587ff04a771SEric Dumazet &shinfo->dataref)) 588ff04a771SEric Dumazet return; 589ff04a771SEric Dumazet 590ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 591ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 5921da177e4SLinus Torvalds 593a6686f2fSShirley Ma /* 594a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 595a6686f2fSShirley Ma * the lower device DMA has done; 596a6686f2fSShirley Ma */ 597ff04a771SEric Dumazet if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { 598a6686f2fSShirley Ma struct ubuf_info *uarg; 599a6686f2fSShirley Ma 600ff04a771SEric Dumazet uarg = shinfo->destructor_arg; 601a6686f2fSShirley Ma if (uarg->callback) 602e19d6763SMichael S. Tsirkin uarg->callback(uarg, true); 603a6686f2fSShirley Ma } 604a6686f2fSShirley Ma 605ff04a771SEric Dumazet if (shinfo->frag_list) 606ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 6071da177e4SLinus Torvalds 608d3836f21SEric Dumazet skb_free_head(skb); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* 6121da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6131da177e4SLinus Torvalds */ 6142d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6151da177e4SLinus Torvalds { 616d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 617d179cd12SDavid S. Miller 618d179cd12SDavid S. Miller switch (skb->fclone) { 619d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6201da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 6216ffe75ebSEric Dumazet return; 622d179cd12SDavid S. Miller 623d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 624d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 6256ffe75ebSEric Dumazet 6266ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 6276ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 6286ffe75ebSEric Dumazet * while here, branch prediction will be good. 6296ffe75ebSEric Dumazet */ 6306ffe75ebSEric Dumazet if (atomic_read(&fclones->fclone_ref) == 1) 6316ffe75ebSEric Dumazet goto fastpath; 632d179cd12SDavid S. Miller break; 633d179cd12SDavid S. Miller 6346ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 635d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 636d179cd12SDavid S. Miller break; 6373ff50b79SStephen Hemminger } 6386ffe75ebSEric Dumazet if (!atomic_dec_and_test(&fclones->fclone_ref)) 6396ffe75ebSEric Dumazet return; 6406ffe75ebSEric Dumazet fastpath: 6416ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6421da177e4SLinus Torvalds } 6431da177e4SLinus Torvalds 64404a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 6451da177e4SLinus Torvalds { 646adf30907SEric Dumazet skb_dst_drop(skb); 6471da177e4SLinus Torvalds #ifdef CONFIG_XFRM 6481da177e4SLinus Torvalds secpath_put(skb->sp); 6491da177e4SLinus Torvalds #endif 6501da177e4SLinus Torvalds if (skb->destructor) { 6519c2b3328SStephen Hemminger WARN_ON(in_irq()); 6521da177e4SLinus Torvalds skb->destructor(skb); 6531da177e4SLinus Torvalds } 654a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 6555f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 6562fc72c7bSKOVACS Krisztian #endif 6571109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 6581da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 6591da177e4SLinus Torvalds #endif 66004a4bb55SLennert Buytenhek } 66104a4bb55SLennert Buytenhek 66204a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 66304a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 66404a4bb55SLennert Buytenhek { 66504a4bb55SLennert Buytenhek skb_release_head_state(skb); 6665e71d9d7SPablo Neira if (likely(skb->head)) 6672d4baff8SHerbert Xu skb_release_data(skb); 6682d4baff8SHerbert Xu } 6691da177e4SLinus Torvalds 6702d4baff8SHerbert Xu /** 6712d4baff8SHerbert Xu * __kfree_skb - private function 6722d4baff8SHerbert Xu * @skb: buffer 6732d4baff8SHerbert Xu * 6742d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6752d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6762d4baff8SHerbert Xu * always call kfree_skb 6772d4baff8SHerbert Xu */ 6782d4baff8SHerbert Xu 6792d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6802d4baff8SHerbert Xu { 6812d4baff8SHerbert Xu skb_release_all(skb); 6821da177e4SLinus Torvalds kfree_skbmem(skb); 6831da177e4SLinus Torvalds } 684b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /** 687231d06aeSJörn Engel * kfree_skb - free an sk_buff 688231d06aeSJörn Engel * @skb: buffer to free 689231d06aeSJörn Engel * 690231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 691231d06aeSJörn Engel * hit zero. 692231d06aeSJörn Engel */ 693231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 694231d06aeSJörn Engel { 695231d06aeSJörn Engel if (unlikely(!skb)) 696231d06aeSJörn Engel return; 697231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 698231d06aeSJörn Engel smp_rmb(); 699231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 700231d06aeSJörn Engel return; 701ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 702231d06aeSJörn Engel __kfree_skb(skb); 703231d06aeSJörn Engel } 704b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 705231d06aeSJörn Engel 706bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 707bd8a7036SEric Dumazet { 708bd8a7036SEric Dumazet while (segs) { 709bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 710bd8a7036SEric Dumazet 711bd8a7036SEric Dumazet kfree_skb(segs); 712bd8a7036SEric Dumazet segs = next; 713bd8a7036SEric Dumazet } 714bd8a7036SEric Dumazet } 715bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 716bd8a7036SEric Dumazet 717d1a203eaSStephen Hemminger /** 71825121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 71925121173SMichael S. Tsirkin * @skb: buffer that triggered an error 72025121173SMichael S. Tsirkin * 72125121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 72225121173SMichael S. Tsirkin * skb must be freed afterwards. 72325121173SMichael S. Tsirkin */ 72425121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 72525121173SMichael S. Tsirkin { 72625121173SMichael S. Tsirkin if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 72725121173SMichael S. Tsirkin struct ubuf_info *uarg; 72825121173SMichael S. Tsirkin 72925121173SMichael S. Tsirkin uarg = skb_shinfo(skb)->destructor_arg; 73025121173SMichael S. Tsirkin if (uarg->callback) 73125121173SMichael S. Tsirkin uarg->callback(uarg, false); 73225121173SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 73325121173SMichael S. Tsirkin } 73425121173SMichael S. Tsirkin } 73525121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 73625121173SMichael S. Tsirkin 73725121173SMichael S. Tsirkin /** 738ead2ceb0SNeil Horman * consume_skb - free an skbuff 739ead2ceb0SNeil Horman * @skb: buffer to free 740ead2ceb0SNeil Horman * 741ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 742ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 743ead2ceb0SNeil Horman * is being dropped after a failure and notes that 744ead2ceb0SNeil Horman */ 745ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 746ead2ceb0SNeil Horman { 747ead2ceb0SNeil Horman if (unlikely(!skb)) 748ead2ceb0SNeil Horman return; 749ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 750ead2ceb0SNeil Horman smp_rmb(); 751ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 752ead2ceb0SNeil Horman return; 75307dc22e7SKoki Sanagi trace_consume_skb(skb); 754ead2ceb0SNeil Horman __kfree_skb(skb); 755ead2ceb0SNeil Horman } 756ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 757ead2ceb0SNeil Horman 758795bb1c0SJesper Dangaard Brouer void __kfree_skb_flush(void) 759795bb1c0SJesper Dangaard Brouer { 760795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 761795bb1c0SJesper Dangaard Brouer 762795bb1c0SJesper Dangaard Brouer /* flush skb_cache if containing objects */ 763795bb1c0SJesper Dangaard Brouer if (nc->skb_count) { 764795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, nc->skb_count, 765795bb1c0SJesper Dangaard Brouer nc->skb_cache); 766795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 767795bb1c0SJesper Dangaard Brouer } 768795bb1c0SJesper Dangaard Brouer } 769795bb1c0SJesper Dangaard Brouer 77015fad714SJesper Dangaard Brouer static inline void _kfree_skb_defer(struct sk_buff *skb) 771795bb1c0SJesper Dangaard Brouer { 772795bb1c0SJesper Dangaard Brouer struct napi_alloc_cache *nc = this_cpu_ptr(&napi_alloc_cache); 773795bb1c0SJesper Dangaard Brouer 774795bb1c0SJesper Dangaard Brouer /* drop skb->head and call any destructors for packet */ 775795bb1c0SJesper Dangaard Brouer skb_release_all(skb); 776795bb1c0SJesper Dangaard Brouer 777795bb1c0SJesper Dangaard Brouer /* record skb to CPU local list */ 778795bb1c0SJesper Dangaard Brouer nc->skb_cache[nc->skb_count++] = skb; 779795bb1c0SJesper Dangaard Brouer 780795bb1c0SJesper Dangaard Brouer #ifdef CONFIG_SLUB 781795bb1c0SJesper Dangaard Brouer /* SLUB writes into objects when freeing */ 782795bb1c0SJesper Dangaard Brouer prefetchw(skb); 783795bb1c0SJesper Dangaard Brouer #endif 784795bb1c0SJesper Dangaard Brouer 785795bb1c0SJesper Dangaard Brouer /* flush skb_cache if it is filled */ 786795bb1c0SJesper Dangaard Brouer if (unlikely(nc->skb_count == NAPI_SKB_CACHE_SIZE)) { 787795bb1c0SJesper Dangaard Brouer kmem_cache_free_bulk(skbuff_head_cache, NAPI_SKB_CACHE_SIZE, 788795bb1c0SJesper Dangaard Brouer nc->skb_cache); 789795bb1c0SJesper Dangaard Brouer nc->skb_count = 0; 790795bb1c0SJesper Dangaard Brouer } 791795bb1c0SJesper Dangaard Brouer } 79215fad714SJesper Dangaard Brouer void __kfree_skb_defer(struct sk_buff *skb) 79315fad714SJesper Dangaard Brouer { 79415fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 79515fad714SJesper Dangaard Brouer } 796795bb1c0SJesper Dangaard Brouer 797795bb1c0SJesper Dangaard Brouer void napi_consume_skb(struct sk_buff *skb, int budget) 798795bb1c0SJesper Dangaard Brouer { 799795bb1c0SJesper Dangaard Brouer if (unlikely(!skb)) 800795bb1c0SJesper Dangaard Brouer return; 801795bb1c0SJesper Dangaard Brouer 802795bb1c0SJesper Dangaard Brouer /* if budget is 0 assume netpoll w/ IRQs disabled */ 803795bb1c0SJesper Dangaard Brouer if (unlikely(!budget)) { 804795bb1c0SJesper Dangaard Brouer dev_consume_skb_irq(skb); 805795bb1c0SJesper Dangaard Brouer return; 806795bb1c0SJesper Dangaard Brouer } 807795bb1c0SJesper Dangaard Brouer 808795bb1c0SJesper Dangaard Brouer if (likely(atomic_read(&skb->users) == 1)) 809795bb1c0SJesper Dangaard Brouer smp_rmb(); 810795bb1c0SJesper Dangaard Brouer else if (likely(!atomic_dec_and_test(&skb->users))) 811795bb1c0SJesper Dangaard Brouer return; 812795bb1c0SJesper Dangaard Brouer /* if reaching here SKB is ready to free */ 813795bb1c0SJesper Dangaard Brouer trace_consume_skb(skb); 814795bb1c0SJesper Dangaard Brouer 815795bb1c0SJesper Dangaard Brouer /* if SKB is a clone, don't handle this case */ 816795bb1c0SJesper Dangaard Brouer if (unlikely(skb->fclone != SKB_FCLONE_UNAVAILABLE)) { 817795bb1c0SJesper Dangaard Brouer __kfree_skb(skb); 818795bb1c0SJesper Dangaard Brouer return; 819795bb1c0SJesper Dangaard Brouer } 820795bb1c0SJesper Dangaard Brouer 82115fad714SJesper Dangaard Brouer _kfree_skb_defer(skb); 822795bb1c0SJesper Dangaard Brouer } 823795bb1c0SJesper Dangaard Brouer EXPORT_SYMBOL(napi_consume_skb); 824795bb1c0SJesper Dangaard Brouer 825b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 826b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 827b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 828b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 829b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 830b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 831b1937227SEric Dumazet 832dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 833dec18810SHerbert Xu { 834dec18810SHerbert Xu new->tstamp = old->tstamp; 835b1937227SEric Dumazet /* We do not copy old->sk */ 836dec18810SHerbert Xu new->dev = old->dev; 837b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 8387fee226aSEric Dumazet skb_dst_copy(new, old); 839def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 840dec18810SHerbert Xu new->sp = secpath_get(old->sp); 841dec18810SHerbert Xu #endif 842b1937227SEric Dumazet __nf_copy(new, old, false); 8436aa895b0SPatrick McHardy 844b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 845b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 846b1937227SEric Dumazet */ 847b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 84806021292SEliezer Tamir 849b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 850b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 851b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 852b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 853b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 854b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 855b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 856b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 857b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 858b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 859b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 860b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 861b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 862b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 863b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 864b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 865b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 866b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 867b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 868b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 869b1937227SEric Dumazet #endif 870e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 871b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 87206021292SEliezer Tamir #endif 8732bd82484SEric Dumazet #ifdef CONFIG_XPS 8742bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 8752bd82484SEric Dumazet #endif 876b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 877b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 878b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT 879b1937227SEric Dumazet CHECK_SKB_FIELD(tc_verd); 880b1937227SEric Dumazet #endif 881b1937227SEric Dumazet #endif 882b1937227SEric Dumazet 883dec18810SHerbert Xu } 884dec18810SHerbert Xu 88582c49a35SHerbert Xu /* 88682c49a35SHerbert Xu * You should not add any new code to this function. Add it to 88782c49a35SHerbert Xu * __copy_skb_header above instead. 88882c49a35SHerbert Xu */ 889e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8901da177e4SLinus Torvalds { 8911da177e4SLinus Torvalds #define C(x) n->x = skb->x 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds n->next = n->prev = NULL; 8941da177e4SLinus Torvalds n->sk = NULL; 895dec18810SHerbert Xu __copy_skb_header(n, skb); 896dec18810SHerbert Xu 8971da177e4SLinus Torvalds C(len); 8981da177e4SLinus Torvalds C(data_len); 8993e6b3b2eSAlexey Dobriyan C(mac_len); 900334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 90102f1c89dSPaul Moore n->cloned = 1; 9021da177e4SLinus Torvalds n->nohdr = 0; 9031da177e4SLinus Torvalds n->destructor = NULL; 9041da177e4SLinus Torvalds C(tail); 9051da177e4SLinus Torvalds C(end); 90602f1c89dSPaul Moore C(head); 907d3836f21SEric Dumazet C(head_frag); 90802f1c89dSPaul Moore C(data); 90902f1c89dSPaul Moore C(truesize); 91002f1c89dSPaul Moore atomic_set(&n->users, 1); 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 9131da177e4SLinus Torvalds skb->cloned = 1; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds return n; 916e0053ec0SHerbert Xu #undef C 917e0053ec0SHerbert Xu } 918e0053ec0SHerbert Xu 919e0053ec0SHerbert Xu /** 920e0053ec0SHerbert Xu * skb_morph - morph one skb into another 921e0053ec0SHerbert Xu * @dst: the skb to receive the contents 922e0053ec0SHerbert Xu * @src: the skb to supply the contents 923e0053ec0SHerbert Xu * 924e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 925e0053ec0SHerbert Xu * supplied by the user. 926e0053ec0SHerbert Xu * 927e0053ec0SHerbert Xu * The target skb is returned upon exit. 928e0053ec0SHerbert Xu */ 929e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 930e0053ec0SHerbert Xu { 9312d4baff8SHerbert Xu skb_release_all(dst); 932e0053ec0SHerbert Xu return __skb_clone(dst, src); 933e0053ec0SHerbert Xu } 934e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 935e0053ec0SHerbert Xu 9362c53040fSBen Hutchings /** 9372c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 93848c83012SMichael S. Tsirkin * @skb: the skb to modify 93948c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 94048c83012SMichael S. Tsirkin * 94148c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 94248c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 94348c83012SMichael S. Tsirkin * to userspace pages. 94448c83012SMichael S. Tsirkin * 94548c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 94648c83012SMichael S. Tsirkin * %GFP_ATOMIC. 94748c83012SMichael S. Tsirkin * 94848c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 94948c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 95048c83012SMichael S. Tsirkin */ 95148c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 952a6686f2fSShirley Ma { 953a6686f2fSShirley Ma int i; 954a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 955a6686f2fSShirley Ma struct page *page, *head = NULL; 956a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 957a6686f2fSShirley Ma 958a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 959a6686f2fSShirley Ma u8 *vaddr; 960a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 961a6686f2fSShirley Ma 96202756ed4SKrishna Kumar page = alloc_page(gfp_mask); 963a6686f2fSShirley Ma if (!page) { 964a6686f2fSShirley Ma while (head) { 96540dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 966a6686f2fSShirley Ma put_page(head); 967a6686f2fSShirley Ma head = next; 968a6686f2fSShirley Ma } 969a6686f2fSShirley Ma return -ENOMEM; 970a6686f2fSShirley Ma } 97151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 972a6686f2fSShirley Ma memcpy(page_address(page), 9739e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 97451c56b00SEric Dumazet kunmap_atomic(vaddr); 97540dadff2SSunghan Suh set_page_private(page, (unsigned long)head); 976a6686f2fSShirley Ma head = page; 977a6686f2fSShirley Ma } 978a6686f2fSShirley Ma 979a6686f2fSShirley Ma /* skb frags release userspace buffers */ 98002756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 981a8605c60SIan Campbell skb_frag_unref(skb, i); 982a6686f2fSShirley Ma 983e19d6763SMichael S. Tsirkin uarg->callback(uarg, false); 984a6686f2fSShirley Ma 985a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 98602756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 98702756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 98802756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 98940dadff2SSunghan Suh head = (struct page *)page_private(head); 990a6686f2fSShirley Ma } 99148c83012SMichael S. Tsirkin 99248c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 993a6686f2fSShirley Ma return 0; 994a6686f2fSShirley Ma } 995dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 996a6686f2fSShirley Ma 997e0053ec0SHerbert Xu /** 998e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 999e0053ec0SHerbert Xu * @skb: buffer to clone 1000e0053ec0SHerbert Xu * @gfp_mask: allocation priority 1001e0053ec0SHerbert Xu * 1002e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 1003e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 1004e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 1005e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 1006e0053ec0SHerbert Xu * 1007e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 1008e0053ec0SHerbert Xu * %GFP_ATOMIC. 1009e0053ec0SHerbert Xu */ 1010e0053ec0SHerbert Xu 1011e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 1012e0053ec0SHerbert Xu { 1013d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 1014d0bf4a9eSEric Dumazet struct sk_buff_fclones, 1015d0bf4a9eSEric Dumazet skb1); 10166ffe75ebSEric Dumazet struct sk_buff *n; 1017e0053ec0SHerbert Xu 101870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1019a6686f2fSShirley Ma return NULL; 1020a6686f2fSShirley Ma 1021e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 10226ffe75ebSEric Dumazet atomic_read(&fclones->fclone_ref) == 1) { 10236ffe75ebSEric Dumazet n = &fclones->skb2; 10246ffe75ebSEric Dumazet atomic_set(&fclones->fclone_ref, 2); 1025e0053ec0SHerbert Xu } else { 1026c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1027c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1028c93bdd0eSMel Gorman 1029e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 1030e0053ec0SHerbert Xu if (!n) 1031e0053ec0SHerbert Xu return NULL; 1032fe55f6d5SVegard Nossum 1033fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 1034e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 1035e0053ec0SHerbert Xu } 1036e0053ec0SHerbert Xu 1037e0053ec0SHerbert Xu return __skb_clone(n, skb); 10381da177e4SLinus Torvalds } 1039b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 10401da177e4SLinus Torvalds 1041f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 1042f5b17294SPravin B Shelar { 1043030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 1044030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 1045030737bcSEric Dumazet skb->csum_start += off; 1046f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 1047f5b17294SPravin B Shelar skb->transport_header += off; 1048f5b17294SPravin B Shelar skb->network_header += off; 1049f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 1050f5b17294SPravin B Shelar skb->mac_header += off; 1051f5b17294SPravin B Shelar skb->inner_transport_header += off; 1052f5b17294SPravin B Shelar skb->inner_network_header += off; 1053aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 1054f5b17294SPravin B Shelar } 1055f5b17294SPravin B Shelar 10561da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 10571da177e4SLinus Torvalds { 1058dec18810SHerbert Xu __copy_skb_header(new, old); 1059dec18810SHerbert Xu 10607967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 10617967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 10627967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds 1065c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 1066c93bdd0eSMel Gorman { 1067c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1068c93bdd0eSMel Gorman return SKB_ALLOC_RX; 1069c93bdd0eSMel Gorman return 0; 1070c93bdd0eSMel Gorman } 1071c93bdd0eSMel Gorman 10721da177e4SLinus Torvalds /** 10731da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 10741da177e4SLinus Torvalds * @skb: buffer to copy 10751da177e4SLinus Torvalds * @gfp_mask: allocation priority 10761da177e4SLinus Torvalds * 10771da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 10781da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 10791da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 10801da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 10811da177e4SLinus Torvalds * 10821da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 10831da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 10841da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 10851da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 10861da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 10871da177e4SLinus Torvalds */ 10881da177e4SLinus Torvalds 1089dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 10901da177e4SLinus Torvalds { 10916602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1092ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1093c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1094c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 10956602cebbSEric Dumazet 10961da177e4SLinus Torvalds if (!n) 10971da177e4SLinus Torvalds return NULL; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* Set the data pointer */ 11001da177e4SLinus Torvalds skb_reserve(n, headerlen); 11011da177e4SLinus Torvalds /* Set the tail pointer and length */ 11021da177e4SLinus Torvalds skb_put(n, skb->len); 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 11051da177e4SLinus Torvalds BUG(); 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds copy_skb_header(n, skb); 11081da177e4SLinus Torvalds return n; 11091da177e4SLinus Torvalds } 1110b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds /** 1113bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 11141da177e4SLinus Torvalds * @skb: buffer to copy 1115117632e6SEric Dumazet * @headroom: headroom of new skb 11161da177e4SLinus Torvalds * @gfp_mask: allocation priority 1117bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1118bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1119bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 11201da177e4SLinus Torvalds * 11211da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 11221da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 11231da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 11241da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 11251da177e4SLinus Torvalds * or the pointer to the buffer on success. 11261da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 11271da177e4SLinus Torvalds */ 11281da177e4SLinus Torvalds 1129bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1130bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 11311da177e4SLinus Torvalds { 1132117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1133bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1134bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 11356602cebbSEric Dumazet 11361da177e4SLinus Torvalds if (!n) 11371da177e4SLinus Torvalds goto out; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds /* Set the data pointer */ 1140117632e6SEric Dumazet skb_reserve(n, headroom); 11411da177e4SLinus Torvalds /* Set the tail pointer and length */ 11421da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 11431da177e4SLinus Torvalds /* Copy the bytes */ 1144d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 11451da177e4SLinus Torvalds 114625f484a6SHerbert Xu n->truesize += skb->data_len; 11471da177e4SLinus Torvalds n->data_len = skb->data_len; 11481da177e4SLinus Torvalds n->len = skb->len; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 11511da177e4SLinus Torvalds int i; 11521da177e4SLinus Torvalds 115370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 11541511022cSDan Carpenter kfree_skb(n); 11551511022cSDan Carpenter n = NULL; 1156a6686f2fSShirley Ma goto out; 1157a6686f2fSShirley Ma } 11581da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 11591da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1160ea2ab693SIan Campbell skb_frag_ref(skb, i); 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 116521dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 11661da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 11671da177e4SLinus Torvalds skb_clone_fraglist(n); 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds copy_skb_header(n, skb); 11711da177e4SLinus Torvalds out: 11721da177e4SLinus Torvalds return n; 11731da177e4SLinus Torvalds } 1174bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /** 11771da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 11781da177e4SLinus Torvalds * @skb: buffer to reallocate 11791da177e4SLinus Torvalds * @nhead: room to add at head 11801da177e4SLinus Torvalds * @ntail: room to add at tail 11811da177e4SLinus Torvalds * @gfp_mask: allocation priority 11821da177e4SLinus Torvalds * 1183bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1184bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 11851da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 11861da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 11871da177e4SLinus Torvalds * 11881da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 11891da177e4SLinus Torvalds * reloaded after call to this function. 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds 119286a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1193dd0fc66fSAl Viro gfp_t gfp_mask) 11941da177e4SLinus Torvalds { 11951da177e4SLinus Torvalds int i; 11961da177e4SLinus Torvalds u8 *data; 1197ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 11981da177e4SLinus Torvalds long off; 11991da177e4SLinus Torvalds 12004edd87adSHerbert Xu BUG_ON(nhead < 0); 12014edd87adSHerbert Xu 12021da177e4SLinus Torvalds if (skb_shared(skb)) 12031da177e4SLinus Torvalds BUG(); 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 12061da177e4SLinus Torvalds 1207c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1208c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1209c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1210c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 12111da177e4SLinus Torvalds if (!data) 12121da177e4SLinus Torvalds goto nodata; 121387151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 12166602cebbSEric Dumazet * optimized for the cases when header is void. 12176602cebbSEric Dumazet */ 12186602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 12196602cebbSEric Dumazet 12206602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 12216602cebbSEric Dumazet skb_shinfo(skb), 1222fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 12231da177e4SLinus Torvalds 12243e24591aSAlexander Duyck /* 12253e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 12263e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 12273e24591aSAlexander Duyck * be since all we did is relocate the values 12283e24591aSAlexander Duyck */ 12293e24591aSAlexander Duyck if (skb_cloned(skb)) { 1230a6686f2fSShirley Ma /* copy this zero copy skb frags */ 123170008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1232a6686f2fSShirley Ma goto nofrags; 12331da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1234ea2ab693SIan Campbell skb_frag_ref(skb, i); 12351da177e4SLinus Torvalds 123621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 12371da177e4SLinus Torvalds skb_clone_fraglist(skb); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds skb_release_data(skb); 12403e24591aSAlexander Duyck } else { 12413e24591aSAlexander Duyck skb_free_head(skb); 12421fd63041SEric Dumazet } 12431da177e4SLinus Torvalds off = (data + nhead) - skb->head; 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds skb->head = data; 1246d3836f21SEric Dumazet skb->head_frag = 0; 12471da177e4SLinus Torvalds skb->data += off; 12484305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 12494305b541SArnaldo Carvalho de Melo skb->end = size; 125056eb8882SPatrick McHardy off = nhead; 12514305b541SArnaldo Carvalho de Melo #else 12524305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 125356eb8882SPatrick McHardy #endif 125427a884dcSArnaldo Carvalho de Melo skb->tail += off; 1255b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 12561da177e4SLinus Torvalds skb->cloned = 0; 1257334a8132SPatrick McHardy skb->hdr_len = 0; 12581da177e4SLinus Torvalds skb->nohdr = 0; 12591da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 12601da177e4SLinus Torvalds return 0; 12611da177e4SLinus Torvalds 1262a6686f2fSShirley Ma nofrags: 1263a6686f2fSShirley Ma kfree(data); 12641da177e4SLinus Torvalds nodata: 12651da177e4SLinus Torvalds return -ENOMEM; 12661da177e4SLinus Torvalds } 1267b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 12721da177e4SLinus Torvalds { 12731da177e4SLinus Torvalds struct sk_buff *skb2; 12741da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds if (delta <= 0) 12771da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 12781da177e4SLinus Torvalds else { 12791da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 12801da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 12811da177e4SLinus Torvalds GFP_ATOMIC)) { 12821da177e4SLinus Torvalds kfree_skb(skb2); 12831da177e4SLinus Torvalds skb2 = NULL; 12841da177e4SLinus Torvalds } 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds return skb2; 12871da177e4SLinus Torvalds } 1288b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds /** 12911da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 12921da177e4SLinus Torvalds * @skb: buffer to copy 12931da177e4SLinus Torvalds * @newheadroom: new free bytes at head 12941da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 12951da177e4SLinus Torvalds * @gfp_mask: allocation priority 12961da177e4SLinus Torvalds * 12971da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 12981da177e4SLinus Torvalds * allocate additional space. 12991da177e4SLinus Torvalds * 13001da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 13011da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 13021da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 13031da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 13041da177e4SLinus Torvalds * 13051da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 13061da177e4SLinus Torvalds * is called from an interrupt. 13071da177e4SLinus Torvalds */ 13081da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 130986a76cafSVictor Fusco int newheadroom, int newtailroom, 1310dd0fc66fSAl Viro gfp_t gfp_mask) 13111da177e4SLinus Torvalds { 13121da177e4SLinus Torvalds /* 13131da177e4SLinus Torvalds * Allocate the copy buffer 13141da177e4SLinus Torvalds */ 1315c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1316c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1317c93bdd0eSMel Gorman NUMA_NO_NODE); 1318efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 13191da177e4SLinus Torvalds int head_copy_len, head_copy_off; 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds if (!n) 13221da177e4SLinus Torvalds return NULL; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds skb_reserve(n, newheadroom); 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /* Set the tail pointer and length */ 13271da177e4SLinus Torvalds skb_put(n, skb->len); 13281da177e4SLinus Torvalds 1329efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 13301da177e4SLinus Torvalds head_copy_off = 0; 13311da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 13321da177e4SLinus Torvalds head_copy_len = newheadroom; 13331da177e4SLinus Torvalds else 13341da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /* Copy the linear header and data. */ 13371da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 13381da177e4SLinus Torvalds skb->len + head_copy_len)) 13391da177e4SLinus Torvalds BUG(); 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds copy_skb_header(n, skb); 13421da177e4SLinus Torvalds 1343030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1344efd1e8d5SPatrick McHardy 13451da177e4SLinus Torvalds return n; 13461da177e4SLinus Torvalds } 1347b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds /** 13501da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 13511da177e4SLinus Torvalds * @skb: buffer to pad 13521da177e4SLinus Torvalds * @pad: space to pad 13531da177e4SLinus Torvalds * 13541da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 13551da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 13561da177e4SLinus Torvalds * beyond the buffer end onto the wire. 13571da177e4SLinus Torvalds * 13585b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds 13615b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 13621da177e4SLinus Torvalds { 13635b057c6bSHerbert Xu int err; 13645b057c6bSHerbert Xu int ntail; 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 13675b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 13681da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 13695b057c6bSHerbert Xu return 0; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13724305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 13735b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 13745b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 13755b057c6bSHerbert Xu if (unlikely(err)) 13765b057c6bSHerbert Xu goto free_skb; 13775b057c6bSHerbert Xu } 13785b057c6bSHerbert Xu 13795b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 13805b057c6bSHerbert Xu * to be audited. 13815b057c6bSHerbert Xu */ 13825b057c6bSHerbert Xu err = skb_linearize(skb); 13835b057c6bSHerbert Xu if (unlikely(err)) 13845b057c6bSHerbert Xu goto free_skb; 13855b057c6bSHerbert Xu 13865b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 13875b057c6bSHerbert Xu return 0; 13885b057c6bSHerbert Xu 13895b057c6bSHerbert Xu free_skb: 13901da177e4SLinus Torvalds kfree_skb(skb); 13915b057c6bSHerbert Xu return err; 13921da177e4SLinus Torvalds } 1393b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 13941da177e4SLinus Torvalds 13950dde3e16SIlpo Järvinen /** 13960c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 13970c7ddf36SMathias Krause * @skb: start of the buffer to use 13980c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 13990c7ddf36SMathias Krause * @len: amount of data to add 14000c7ddf36SMathias Krause * 14010c7ddf36SMathias Krause * This function extends the used data area of the potentially 14020c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 14030c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 14040c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 14050c7ddf36SMathias Krause * returned. 14060c7ddf36SMathias Krause */ 14070c7ddf36SMathias Krause 14080c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 14090c7ddf36SMathias Krause { 14100c7ddf36SMathias Krause if (tail != skb) { 14110c7ddf36SMathias Krause skb->data_len += len; 14120c7ddf36SMathias Krause skb->len += len; 14130c7ddf36SMathias Krause } 14140c7ddf36SMathias Krause return skb_put(tail, len); 14150c7ddf36SMathias Krause } 14160c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 14170c7ddf36SMathias Krause 14180c7ddf36SMathias Krause /** 14190dde3e16SIlpo Järvinen * skb_put - add data to a buffer 14200dde3e16SIlpo Järvinen * @skb: buffer to use 14210dde3e16SIlpo Järvinen * @len: amount of data to add 14220dde3e16SIlpo Järvinen * 14230dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 14240dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 14250dde3e16SIlpo Järvinen * first byte of the extra data is returned. 14260dde3e16SIlpo Järvinen */ 14270dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 14280dde3e16SIlpo Järvinen { 14290dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 14300dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 14310dde3e16SIlpo Järvinen skb->tail += len; 14320dde3e16SIlpo Järvinen skb->len += len; 14330dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 14340dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 14350dde3e16SIlpo Järvinen return tmp; 14360dde3e16SIlpo Järvinen } 14370dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 14380dde3e16SIlpo Järvinen 14396be8ac2fSIlpo Järvinen /** 1440c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1441c2aa270aSIlpo Järvinen * @skb: buffer to use 1442c2aa270aSIlpo Järvinen * @len: amount of data to add 1443c2aa270aSIlpo Järvinen * 1444c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1445c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1446c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1447c2aa270aSIlpo Järvinen */ 1448c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1449c2aa270aSIlpo Järvinen { 1450c2aa270aSIlpo Järvinen skb->data -= len; 1451c2aa270aSIlpo Järvinen skb->len += len; 1452c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1453c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1454c2aa270aSIlpo Järvinen return skb->data; 1455c2aa270aSIlpo Järvinen } 1456c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1457c2aa270aSIlpo Järvinen 1458c2aa270aSIlpo Järvinen /** 14596be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 14606be8ac2fSIlpo Järvinen * @skb: buffer to use 14616be8ac2fSIlpo Järvinen * @len: amount of data to remove 14626be8ac2fSIlpo Järvinen * 14636be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 14646be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 14656be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 14666be8ac2fSIlpo Järvinen * the old data. 14676be8ac2fSIlpo Järvinen */ 14686be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 14696be8ac2fSIlpo Järvinen { 147047d29646SDavid S. Miller return skb_pull_inline(skb, len); 14716be8ac2fSIlpo Järvinen } 14726be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 14736be8ac2fSIlpo Järvinen 1474419ae74eSIlpo Järvinen /** 1475419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1476419ae74eSIlpo Järvinen * @skb: buffer to alter 1477419ae74eSIlpo Järvinen * @len: new length 1478419ae74eSIlpo Järvinen * 1479419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1480419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1481419ae74eSIlpo Järvinen * The skb must be linear. 1482419ae74eSIlpo Järvinen */ 1483419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1484419ae74eSIlpo Järvinen { 1485419ae74eSIlpo Järvinen if (skb->len > len) 1486419ae74eSIlpo Järvinen __skb_trim(skb, len); 1487419ae74eSIlpo Järvinen } 1488419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1489419ae74eSIlpo Järvinen 14903cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 14911da177e4SLinus Torvalds */ 14921da177e4SLinus Torvalds 14933cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 14941da177e4SLinus Torvalds { 149527b437c8SHerbert Xu struct sk_buff **fragp; 149627b437c8SHerbert Xu struct sk_buff *frag; 14971da177e4SLinus Torvalds int offset = skb_headlen(skb); 14981da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 14991da177e4SLinus Torvalds int i; 150027b437c8SHerbert Xu int err; 150127b437c8SHerbert Xu 150227b437c8SHerbert Xu if (skb_cloned(skb) && 150327b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 150427b437c8SHerbert Xu return err; 15051da177e4SLinus Torvalds 1506f4d26fb3SHerbert Xu i = 0; 1507f4d26fb3SHerbert Xu if (offset >= len) 1508f4d26fb3SHerbert Xu goto drop_pages; 1509f4d26fb3SHerbert Xu 1510f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 15119e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 151227b437c8SHerbert Xu 151327b437c8SHerbert Xu if (end < len) { 15141da177e4SLinus Torvalds offset = end; 151527b437c8SHerbert Xu continue; 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds 15189e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 151927b437c8SHerbert Xu 1520f4d26fb3SHerbert Xu drop_pages: 152127b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 152227b437c8SHerbert Xu 152327b437c8SHerbert Xu for (; i < nfrags; i++) 1524ea2ab693SIan Campbell skb_frag_unref(skb, i); 152527b437c8SHerbert Xu 152621dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 152727b437c8SHerbert Xu skb_drop_fraglist(skb); 1528f4d26fb3SHerbert Xu goto done; 152927b437c8SHerbert Xu } 153027b437c8SHerbert Xu 153127b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 153227b437c8SHerbert Xu fragp = &frag->next) { 153327b437c8SHerbert Xu int end = offset + frag->len; 153427b437c8SHerbert Xu 153527b437c8SHerbert Xu if (skb_shared(frag)) { 153627b437c8SHerbert Xu struct sk_buff *nfrag; 153727b437c8SHerbert Xu 153827b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 153927b437c8SHerbert Xu if (unlikely(!nfrag)) 154027b437c8SHerbert Xu return -ENOMEM; 154127b437c8SHerbert Xu 154227b437c8SHerbert Xu nfrag->next = frag->next; 154385bb2a60SEric Dumazet consume_skb(frag); 154427b437c8SHerbert Xu frag = nfrag; 154527b437c8SHerbert Xu *fragp = frag; 154627b437c8SHerbert Xu } 154727b437c8SHerbert Xu 154827b437c8SHerbert Xu if (end < len) { 154927b437c8SHerbert Xu offset = end; 155027b437c8SHerbert Xu continue; 155127b437c8SHerbert Xu } 155227b437c8SHerbert Xu 155327b437c8SHerbert Xu if (end > len && 155427b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 155527b437c8SHerbert Xu return err; 155627b437c8SHerbert Xu 155727b437c8SHerbert Xu if (frag->next) 155827b437c8SHerbert Xu skb_drop_list(&frag->next); 155927b437c8SHerbert Xu break; 156027b437c8SHerbert Xu } 156127b437c8SHerbert Xu 1562f4d26fb3SHerbert Xu done: 156327b437c8SHerbert Xu if (len > skb_headlen(skb)) { 15641da177e4SLinus Torvalds skb->data_len -= skb->len - len; 15651da177e4SLinus Torvalds skb->len = len; 15661da177e4SLinus Torvalds } else { 15671da177e4SLinus Torvalds skb->len = len; 15681da177e4SLinus Torvalds skb->data_len = 0; 156927a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds return 0; 15731da177e4SLinus Torvalds } 1574b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds /** 15771da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 15781da177e4SLinus Torvalds * @skb: buffer to reallocate 15791da177e4SLinus Torvalds * @delta: number of bytes to advance tail 15801da177e4SLinus Torvalds * 15811da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 15821da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 15831da177e4SLinus Torvalds * data from fragmented part. 15841da177e4SLinus Torvalds * 15851da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 15861da177e4SLinus Torvalds * 15871da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 15881da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 15891da177e4SLinus Torvalds * 15901da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 15911da177e4SLinus Torvalds * reloaded after call to this function. 15921da177e4SLinus Torvalds */ 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 15951da177e4SLinus Torvalds * when it is necessary. 15961da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 15971da177e4SLinus Torvalds * 2. It may change skb pointers. 15981da177e4SLinus Torvalds * 15991da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 16001da177e4SLinus Torvalds */ 16011da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 16021da177e4SLinus Torvalds { 16031da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 16041da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 16051da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 16061da177e4SLinus Torvalds */ 16074305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 16101da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 16111da177e4SLinus Torvalds GFP_ATOMIC)) 16121da177e4SLinus Torvalds return NULL; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds 161527a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 16161da177e4SLinus Torvalds BUG(); 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 16191da177e4SLinus Torvalds * size of pulled pages. Superb. 16201da177e4SLinus Torvalds */ 162121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 16221da177e4SLinus Torvalds goto pull_pages; 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 16251da177e4SLinus Torvalds eat = delta; 16261da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16279e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 16289e903e08SEric Dumazet 16299e903e08SEric Dumazet if (size >= eat) 16301da177e4SLinus Torvalds goto pull_pages; 16319e903e08SEric Dumazet eat -= size; 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 16351da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 16361da177e4SLinus Torvalds * but taking into account that pulling is expected to 16371da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 16381da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 16391da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 16401da177e4SLinus Torvalds */ 16411da177e4SLinus Torvalds if (eat) { 16421da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 16431da177e4SLinus Torvalds struct sk_buff *clone = NULL; 16441da177e4SLinus Torvalds struct sk_buff *insp = NULL; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds do { 164709a62660SKris Katterjohn BUG_ON(!list); 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds if (list->len <= eat) { 16501da177e4SLinus Torvalds /* Eaten as whole. */ 16511da177e4SLinus Torvalds eat -= list->len; 16521da177e4SLinus Torvalds list = list->next; 16531da177e4SLinus Torvalds insp = list; 16541da177e4SLinus Torvalds } else { 16551da177e4SLinus Torvalds /* Eaten partially. */ 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds if (skb_shared(list)) { 16581da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 16591da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 16601da177e4SLinus Torvalds if (!clone) 16611da177e4SLinus Torvalds return NULL; 16621da177e4SLinus Torvalds insp = list->next; 16631da177e4SLinus Torvalds list = clone; 16641da177e4SLinus Torvalds } else { 16651da177e4SLinus Torvalds /* This may be pulled without 16661da177e4SLinus Torvalds * problems. */ 16671da177e4SLinus Torvalds insp = list; 16681da177e4SLinus Torvalds } 16691da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 16701da177e4SLinus Torvalds kfree_skb(clone); 16711da177e4SLinus Torvalds return NULL; 16721da177e4SLinus Torvalds } 16731da177e4SLinus Torvalds break; 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds } while (eat); 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds /* Free pulled out fragments. */ 16781da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 16791da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 16801da177e4SLinus Torvalds kfree_skb(list); 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds /* And insert new clone at head. */ 16831da177e4SLinus Torvalds if (clone) { 16841da177e4SLinus Torvalds clone->next = list; 16851da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds pull_pages: 16911da177e4SLinus Torvalds eat = delta; 16921da177e4SLinus Torvalds k = 0; 16931da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16949e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 16959e903e08SEric Dumazet 16969e903e08SEric Dumazet if (size <= eat) { 1697ea2ab693SIan Campbell skb_frag_unref(skb, i); 16989e903e08SEric Dumazet eat -= size; 16991da177e4SLinus Torvalds } else { 17001da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 17011da177e4SLinus Torvalds if (eat) { 17021da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 17039e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 17041da177e4SLinus Torvalds eat = 0; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds k++; 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds skb->tail += delta; 17121da177e4SLinus Torvalds skb->data_len -= delta; 17131da177e4SLinus Torvalds 171427a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 17151da177e4SLinus Torvalds } 1716b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 17171da177e4SLinus Torvalds 171822019b17SEric Dumazet /** 171922019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 172022019b17SEric Dumazet * @skb: source skb 172122019b17SEric Dumazet * @offset: offset in source 172222019b17SEric Dumazet * @to: destination buffer 172322019b17SEric Dumazet * @len: number of bytes to copy 172422019b17SEric Dumazet * 172522019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 172622019b17SEric Dumazet * destination buffer. 172722019b17SEric Dumazet * 172822019b17SEric Dumazet * CAUTION ! : 172922019b17SEric Dumazet * If its prototype is ever changed, 173022019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 173122019b17SEric Dumazet * since it is called from BPF assembly code. 173222019b17SEric Dumazet */ 17331da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 17341da177e4SLinus Torvalds { 17351a028e50SDavid S. Miller int start = skb_headlen(skb); 1736fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1737fbb398a8SDavid S. Miller int i, copy; 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds if (offset > (int)skb->len - len) 17401da177e4SLinus Torvalds goto fault; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds /* Copy header. */ 17431a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 17441da177e4SLinus Torvalds if (copy > len) 17451da177e4SLinus Torvalds copy = len; 1746d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 17471da177e4SLinus Torvalds if ((len -= copy) == 0) 17481da177e4SLinus Torvalds return 0; 17491da177e4SLinus Torvalds offset += copy; 17501da177e4SLinus Torvalds to += copy; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 17541a028e50SDavid S. Miller int end; 175551c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 17561da177e4SLinus Torvalds 1757547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17581a028e50SDavid S. Miller 175951c56b00SEric Dumazet end = start + skb_frag_size(f); 17601da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17611da177e4SLinus Torvalds u8 *vaddr; 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds if (copy > len) 17641da177e4SLinus Torvalds copy = len; 17651da177e4SLinus Torvalds 176651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 17671da177e4SLinus Torvalds memcpy(to, 176851c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 176951c56b00SEric Dumazet copy); 177051c56b00SEric Dumazet kunmap_atomic(vaddr); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds if ((len -= copy) == 0) 17731da177e4SLinus Torvalds return 0; 17741da177e4SLinus Torvalds offset += copy; 17751da177e4SLinus Torvalds to += copy; 17761da177e4SLinus Torvalds } 17771a028e50SDavid S. Miller start = end; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 1780fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 17811a028e50SDavid S. Miller int end; 17821da177e4SLinus Torvalds 1783547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17841a028e50SDavid S. Miller 1785fbb398a8SDavid S. Miller end = start + frag_iter->len; 17861da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17871da177e4SLinus Torvalds if (copy > len) 17881da177e4SLinus Torvalds copy = len; 1789fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 17901da177e4SLinus Torvalds goto fault; 17911da177e4SLinus Torvalds if ((len -= copy) == 0) 17921da177e4SLinus Torvalds return 0; 17931da177e4SLinus Torvalds offset += copy; 17941da177e4SLinus Torvalds to += copy; 17951da177e4SLinus Torvalds } 17961a028e50SDavid S. Miller start = end; 17971da177e4SLinus Torvalds } 1798a6686f2fSShirley Ma 17991da177e4SLinus Torvalds if (!len) 18001da177e4SLinus Torvalds return 0; 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds fault: 18031da177e4SLinus Torvalds return -EFAULT; 18041da177e4SLinus Torvalds } 1805b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 18061da177e4SLinus Torvalds 18079c55e01cSJens Axboe /* 18089c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 18099c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 18109c55e01cSJens Axboe */ 18119c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 18129c55e01cSJens Axboe { 18138b9d3728SJarek Poplawski put_page(spd->pages[i]); 18148b9d3728SJarek Poplawski } 18159c55e01cSJens Axboe 1816a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 18174fb66994SJarek Poplawski unsigned int *offset, 181818aafc62SEric Dumazet struct sock *sk) 18198b9d3728SJarek Poplawski { 18205640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 18218b9d3728SJarek Poplawski 18225640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 18238b9d3728SJarek Poplawski return NULL; 18244fb66994SJarek Poplawski 18255640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 18264fb66994SJarek Poplawski 18275640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 18285640f768SEric Dumazet page_address(page) + *offset, *len); 18295640f768SEric Dumazet *offset = pfrag->offset; 18305640f768SEric Dumazet pfrag->offset += *len; 18314fb66994SJarek Poplawski 18325640f768SEric Dumazet return pfrag->page; 18339c55e01cSJens Axboe } 18349c55e01cSJens Axboe 183541c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 183641c73a0dSEric Dumazet struct page *page, 183741c73a0dSEric Dumazet unsigned int offset) 183841c73a0dSEric Dumazet { 183941c73a0dSEric Dumazet return spd->nr_pages && 184041c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 184141c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 184241c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 184341c73a0dSEric Dumazet } 184441c73a0dSEric Dumazet 18459c55e01cSJens Axboe /* 18469c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 18479c55e01cSJens Axboe */ 1848a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 184935f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 18504fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 185118aafc62SEric Dumazet bool linear, 18527a67e56fSJarek Poplawski struct sock *sk) 18539c55e01cSJens Axboe { 185441c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1855a108d5f3SDavid S. Miller return true; 18569c55e01cSJens Axboe 18578b9d3728SJarek Poplawski if (linear) { 185818aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 18598b9d3728SJarek Poplawski if (!page) 1860a108d5f3SDavid S. Miller return true; 186141c73a0dSEric Dumazet } 186241c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 186341c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1864a108d5f3SDavid S. Miller return false; 186541c73a0dSEric Dumazet } 18668b9d3728SJarek Poplawski get_page(page); 18679c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 18684fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 18699c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 18709c55e01cSJens Axboe spd->nr_pages++; 18718b9d3728SJarek Poplawski 1872a108d5f3SDavid S. Miller return false; 18739c55e01cSJens Axboe } 18749c55e01cSJens Axboe 1875a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 18762870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 187718aafc62SEric Dumazet unsigned int *len, 1878d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 187935f3d14dSJens Axboe struct sock *sk, 188035f3d14dSJens Axboe struct pipe_inode_info *pipe) 18819c55e01cSJens Axboe { 18822870c43dSOctavian Purdila if (!*len) 1883a108d5f3SDavid S. Miller return true; 18849c55e01cSJens Axboe 18852870c43dSOctavian Purdila /* skip this segment if already processed */ 18862870c43dSOctavian Purdila if (*off >= plen) { 18872870c43dSOctavian Purdila *off -= plen; 1888a108d5f3SDavid S. Miller return false; 18892870c43dSOctavian Purdila } 18902870c43dSOctavian Purdila 18912870c43dSOctavian Purdila /* ignore any bits we already processed */ 18929ca1b22dSEric Dumazet poff += *off; 18939ca1b22dSEric Dumazet plen -= *off; 18942870c43dSOctavian Purdila *off = 0; 18952870c43dSOctavian Purdila 189618aafc62SEric Dumazet do { 189718aafc62SEric Dumazet unsigned int flen = min(*len, plen); 18982870c43dSOctavian Purdila 189918aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 190018aafc62SEric Dumazet linear, sk)) 1901a108d5f3SDavid S. Miller return true; 190218aafc62SEric Dumazet poff += flen; 190318aafc62SEric Dumazet plen -= flen; 19042870c43dSOctavian Purdila *len -= flen; 190518aafc62SEric Dumazet } while (*len && plen); 19062870c43dSOctavian Purdila 1907a108d5f3SDavid S. Miller return false; 1908db43a282SOctavian Purdila } 19099c55e01cSJens Axboe 19109c55e01cSJens Axboe /* 1911a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 19122870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 19139c55e01cSJens Axboe */ 1914a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 191535f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 191635f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 19172870c43dSOctavian Purdila { 19182870c43dSOctavian Purdila int seg; 19199c55e01cSJens Axboe 19201d0c0b32SEric Dumazet /* map the linear part : 19212996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 19222996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 19232996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 19249c55e01cSJens Axboe */ 19252870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 19262870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 19272870c43dSOctavian Purdila skb_headlen(skb), 192818aafc62SEric Dumazet offset, len, spd, 19293a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 19301d0c0b32SEric Dumazet sk, pipe)) 1931a108d5f3SDavid S. Miller return true; 19329c55e01cSJens Axboe 19339c55e01cSJens Axboe /* 19349c55e01cSJens Axboe * then map the fragments 19359c55e01cSJens Axboe */ 19369c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 19379c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 19389c55e01cSJens Axboe 1939ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 19409e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 194118aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 1942a108d5f3SDavid S. Miller return true; 19439c55e01cSJens Axboe } 19449c55e01cSJens Axboe 1945a108d5f3SDavid S. Miller return false; 19469c55e01cSJens Axboe } 19479c55e01cSJens Axboe 1948a60e3cc7SHannes Frederic Sowa ssize_t skb_socket_splice(struct sock *sk, 1949a60e3cc7SHannes Frederic Sowa struct pipe_inode_info *pipe, 1950a60e3cc7SHannes Frederic Sowa struct splice_pipe_desc *spd) 1951a60e3cc7SHannes Frederic Sowa { 1952a60e3cc7SHannes Frederic Sowa int ret; 1953a60e3cc7SHannes Frederic Sowa 1954a60e3cc7SHannes Frederic Sowa /* Drop the socket lock, otherwise we have reverse 1955a60e3cc7SHannes Frederic Sowa * locking dependencies between sk_lock and i_mutex 1956a60e3cc7SHannes Frederic Sowa * here as compared to sendfile(). We enter here 1957a60e3cc7SHannes Frederic Sowa * with the socket lock held, and splice_to_pipe() will 1958a60e3cc7SHannes Frederic Sowa * grab the pipe inode lock. For sendfile() emulation, 1959a60e3cc7SHannes Frederic Sowa * we call into ->sendpage() with the i_mutex lock held 1960a60e3cc7SHannes Frederic Sowa * and networking will grab the socket lock. 1961a60e3cc7SHannes Frederic Sowa */ 1962a60e3cc7SHannes Frederic Sowa release_sock(sk); 1963a60e3cc7SHannes Frederic Sowa ret = splice_to_pipe(pipe, spd); 1964a60e3cc7SHannes Frederic Sowa lock_sock(sk); 1965a60e3cc7SHannes Frederic Sowa 1966a60e3cc7SHannes Frederic Sowa return ret; 1967a60e3cc7SHannes Frederic Sowa } 1968a60e3cc7SHannes Frederic Sowa 19699c55e01cSJens Axboe /* 19709c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 19719c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 19729c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 19739c55e01cSJens Axboe * handle that cleanly. 19749c55e01cSJens Axboe */ 1975a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 19769c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 1977a60e3cc7SHannes Frederic Sowa unsigned int flags, 1978a60e3cc7SHannes Frederic Sowa ssize_t (*splice_cb)(struct sock *, 1979a60e3cc7SHannes Frederic Sowa struct pipe_inode_info *, 1980a60e3cc7SHannes Frederic Sowa struct splice_pipe_desc *)) 19819c55e01cSJens Axboe { 198241c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 198341c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 19849c55e01cSJens Axboe struct splice_pipe_desc spd = { 19859c55e01cSJens Axboe .pages = pages, 19869c55e01cSJens Axboe .partial = partial, 1987047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 19889c55e01cSJens Axboe .flags = flags, 198928a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 19909c55e01cSJens Axboe .spd_release = sock_spd_release, 19919c55e01cSJens Axboe }; 1992fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 199335f3d14dSJens Axboe int ret = 0; 199435f3d14dSJens Axboe 19959c55e01cSJens Axboe /* 19969c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 19979c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 19989c55e01cSJens Axboe */ 199935f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 20009c55e01cSJens Axboe goto done; 20019c55e01cSJens Axboe else if (!tlen) 20029c55e01cSJens Axboe goto done; 20039c55e01cSJens Axboe 20049c55e01cSJens Axboe /* 20059c55e01cSJens Axboe * now see if we have a frag_list to map 20069c55e01cSJens Axboe */ 2007fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 2008fbb398a8SDavid S. Miller if (!tlen) 20099c55e01cSJens Axboe break; 201035f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 2011fbb398a8SDavid S. Miller break; 20129c55e01cSJens Axboe } 20139c55e01cSJens Axboe 20149c55e01cSJens Axboe done: 2015a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 2016a60e3cc7SHannes Frederic Sowa ret = splice_cb(sk, pipe, &spd); 20179c55e01cSJens Axboe 201835f3d14dSJens Axboe return ret; 20199c55e01cSJens Axboe } 20202b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 20219c55e01cSJens Axboe 2022357b40a1SHerbert Xu /** 2023357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 2024357b40a1SHerbert Xu * @skb: destination buffer 2025357b40a1SHerbert Xu * @offset: offset in destination 2026357b40a1SHerbert Xu * @from: source buffer 2027357b40a1SHerbert Xu * @len: number of bytes to copy 2028357b40a1SHerbert Xu * 2029357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 2030357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 2031357b40a1SHerbert Xu * traversing fragment lists and such. 2032357b40a1SHerbert Xu */ 2033357b40a1SHerbert Xu 20340c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 2035357b40a1SHerbert Xu { 20361a028e50SDavid S. Miller int start = skb_headlen(skb); 2037fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 2038fbb398a8SDavid S. Miller int i, copy; 2039357b40a1SHerbert Xu 2040357b40a1SHerbert Xu if (offset > (int)skb->len - len) 2041357b40a1SHerbert Xu goto fault; 2042357b40a1SHerbert Xu 20431a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 2044357b40a1SHerbert Xu if (copy > len) 2045357b40a1SHerbert Xu copy = len; 204627d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 2047357b40a1SHerbert Xu if ((len -= copy) == 0) 2048357b40a1SHerbert Xu return 0; 2049357b40a1SHerbert Xu offset += copy; 2050357b40a1SHerbert Xu from += copy; 2051357b40a1SHerbert Xu } 2052357b40a1SHerbert Xu 2053357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 2054357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20551a028e50SDavid S. Miller int end; 2056357b40a1SHerbert Xu 2057547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20581a028e50SDavid S. Miller 20599e903e08SEric Dumazet end = start + skb_frag_size(frag); 2060357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2061357b40a1SHerbert Xu u8 *vaddr; 2062357b40a1SHerbert Xu 2063357b40a1SHerbert Xu if (copy > len) 2064357b40a1SHerbert Xu copy = len; 2065357b40a1SHerbert Xu 206651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20671a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 20681a028e50SDavid S. Miller from, copy); 206951c56b00SEric Dumazet kunmap_atomic(vaddr); 2070357b40a1SHerbert Xu 2071357b40a1SHerbert Xu if ((len -= copy) == 0) 2072357b40a1SHerbert Xu return 0; 2073357b40a1SHerbert Xu offset += copy; 2074357b40a1SHerbert Xu from += copy; 2075357b40a1SHerbert Xu } 20761a028e50SDavid S. Miller start = end; 2077357b40a1SHerbert Xu } 2078357b40a1SHerbert Xu 2079fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20801a028e50SDavid S. Miller int end; 2081357b40a1SHerbert Xu 2082547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20831a028e50SDavid S. Miller 2084fbb398a8SDavid S. Miller end = start + frag_iter->len; 2085357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2086357b40a1SHerbert Xu if (copy > len) 2087357b40a1SHerbert Xu copy = len; 2088fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 20891a028e50SDavid S. Miller from, copy)) 2090357b40a1SHerbert Xu goto fault; 2091357b40a1SHerbert Xu if ((len -= copy) == 0) 2092357b40a1SHerbert Xu return 0; 2093357b40a1SHerbert Xu offset += copy; 2094357b40a1SHerbert Xu from += copy; 2095357b40a1SHerbert Xu } 20961a028e50SDavid S. Miller start = end; 2097357b40a1SHerbert Xu } 2098357b40a1SHerbert Xu if (!len) 2099357b40a1SHerbert Xu return 0; 2100357b40a1SHerbert Xu 2101357b40a1SHerbert Xu fault: 2102357b40a1SHerbert Xu return -EFAULT; 2103357b40a1SHerbert Xu } 2104357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2105357b40a1SHerbert Xu 21061da177e4SLinus Torvalds /* Checksum skb data. */ 21072817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 21082817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 21091da177e4SLinus Torvalds { 21101a028e50SDavid S. Miller int start = skb_headlen(skb); 21111a028e50SDavid S. Miller int i, copy = start - offset; 2112fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 21131da177e4SLinus Torvalds int pos = 0; 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds /* Checksum header. */ 21161da177e4SLinus Torvalds if (copy > 0) { 21171da177e4SLinus Torvalds if (copy > len) 21181da177e4SLinus Torvalds copy = len; 21192817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 21201da177e4SLinus Torvalds if ((len -= copy) == 0) 21211da177e4SLinus Torvalds return csum; 21221da177e4SLinus Torvalds offset += copy; 21231da177e4SLinus Torvalds pos = copy; 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21271a028e50SDavid S. Miller int end; 212851c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 21291da177e4SLinus Torvalds 2130547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21311a028e50SDavid S. Miller 213251c56b00SEric Dumazet end = start + skb_frag_size(frag); 21331da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 213444bb9363SAl Viro __wsum csum2; 21351da177e4SLinus Torvalds u8 *vaddr; 21361da177e4SLinus Torvalds 21371da177e4SLinus Torvalds if (copy > len) 21381da177e4SLinus Torvalds copy = len; 213951c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 21402817a336SDaniel Borkmann csum2 = ops->update(vaddr + frag->page_offset + 21411a028e50SDavid S. Miller offset - start, copy, 0); 214251c56b00SEric Dumazet kunmap_atomic(vaddr); 21432817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 21441da177e4SLinus Torvalds if (!(len -= copy)) 21451da177e4SLinus Torvalds return csum; 21461da177e4SLinus Torvalds offset += copy; 21471da177e4SLinus Torvalds pos += copy; 21481da177e4SLinus Torvalds } 21491a028e50SDavid S. Miller start = end; 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 2152fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 21531a028e50SDavid S. Miller int end; 21541da177e4SLinus Torvalds 2155547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21561a028e50SDavid S. Miller 2157fbb398a8SDavid S. Miller end = start + frag_iter->len; 21581da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21595f92a738SAl Viro __wsum csum2; 21601da177e4SLinus Torvalds if (copy > len) 21611da177e4SLinus Torvalds copy = len; 21622817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 21632817a336SDaniel Borkmann copy, 0, ops); 21642817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 21651da177e4SLinus Torvalds if ((len -= copy) == 0) 21661da177e4SLinus Torvalds return csum; 21671da177e4SLinus Torvalds offset += copy; 21681da177e4SLinus Torvalds pos += copy; 21691da177e4SLinus Torvalds } 21701a028e50SDavid S. Miller start = end; 21711da177e4SLinus Torvalds } 217209a62660SKris Katterjohn BUG_ON(len); 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds return csum; 21751da177e4SLinus Torvalds } 21762817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 21772817a336SDaniel Borkmann 21782817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 21792817a336SDaniel Borkmann int len, __wsum csum) 21802817a336SDaniel Borkmann { 21812817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2182cea80ea8SDaniel Borkmann .update = csum_partial_ext, 21832817a336SDaniel Borkmann .combine = csum_block_add_ext, 21842817a336SDaniel Borkmann }; 21852817a336SDaniel Borkmann 21862817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 21872817a336SDaniel Borkmann } 2188b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds /* Both of above in one bottle. */ 21911da177e4SLinus Torvalds 219281d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 219381d77662SAl Viro u8 *to, int len, __wsum csum) 21941da177e4SLinus Torvalds { 21951a028e50SDavid S. Miller int start = skb_headlen(skb); 21961a028e50SDavid S. Miller int i, copy = start - offset; 2197fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 21981da177e4SLinus Torvalds int pos = 0; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds /* Copy header. */ 22011da177e4SLinus Torvalds if (copy > 0) { 22021da177e4SLinus Torvalds if (copy > len) 22031da177e4SLinus Torvalds copy = len; 22041da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 22051da177e4SLinus Torvalds copy, csum); 22061da177e4SLinus Torvalds if ((len -= copy) == 0) 22071da177e4SLinus Torvalds return csum; 22081da177e4SLinus Torvalds offset += copy; 22091da177e4SLinus Torvalds to += copy; 22101da177e4SLinus Torvalds pos = copy; 22111da177e4SLinus Torvalds } 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 22141a028e50SDavid S. Miller int end; 22151da177e4SLinus Torvalds 2216547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22171a028e50SDavid S. Miller 22189e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 22191da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 22205084205fSAl Viro __wsum csum2; 22211da177e4SLinus Torvalds u8 *vaddr; 22221da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds if (copy > len) 22251da177e4SLinus Torvalds copy = len; 222651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 22271da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 22281a028e50SDavid S. Miller frag->page_offset + 22291a028e50SDavid S. Miller offset - start, to, 22301a028e50SDavid S. Miller copy, 0); 223151c56b00SEric Dumazet kunmap_atomic(vaddr); 22321da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 22331da177e4SLinus Torvalds if (!(len -= copy)) 22341da177e4SLinus Torvalds return csum; 22351da177e4SLinus Torvalds offset += copy; 22361da177e4SLinus Torvalds to += copy; 22371da177e4SLinus Torvalds pos += copy; 22381da177e4SLinus Torvalds } 22391a028e50SDavid S. Miller start = end; 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 2242fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 224381d77662SAl Viro __wsum csum2; 22441a028e50SDavid S. Miller int end; 22451da177e4SLinus Torvalds 2246547b792cSIlpo Järvinen WARN_ON(start > offset + len); 22471a028e50SDavid S. Miller 2248fbb398a8SDavid S. Miller end = start + frag_iter->len; 22491da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 22501da177e4SLinus Torvalds if (copy > len) 22511da177e4SLinus Torvalds copy = len; 2252fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 22531a028e50SDavid S. Miller offset - start, 22541da177e4SLinus Torvalds to, copy, 0); 22551da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 22561da177e4SLinus Torvalds if ((len -= copy) == 0) 22571da177e4SLinus Torvalds return csum; 22581da177e4SLinus Torvalds offset += copy; 22591da177e4SLinus Torvalds to += copy; 22601da177e4SLinus Torvalds pos += copy; 22611da177e4SLinus Torvalds } 22621a028e50SDavid S. Miller start = end; 22631da177e4SLinus Torvalds } 226409a62660SKris Katterjohn BUG_ON(len); 22651da177e4SLinus Torvalds return csum; 22661da177e4SLinus Torvalds } 2267b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 22681da177e4SLinus Torvalds 2269af2806f8SThomas Graf /** 2270af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2271af2806f8SThomas Graf * @from: source buffer 2272af2806f8SThomas Graf * 2273af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2274af2806f8SThomas Graf * into skb_zerocopy(). 2275af2806f8SThomas Graf */ 2276af2806f8SThomas Graf unsigned int 2277af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2278af2806f8SThomas Graf { 2279af2806f8SThomas Graf unsigned int hlen = 0; 2280af2806f8SThomas Graf 2281af2806f8SThomas Graf if (!from->head_frag || 2282af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2283af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2284af2806f8SThomas Graf hlen = skb_headlen(from); 2285af2806f8SThomas Graf 2286af2806f8SThomas Graf if (skb_has_frag_list(from)) 2287af2806f8SThomas Graf hlen = from->len; 2288af2806f8SThomas Graf 2289af2806f8SThomas Graf return hlen; 2290af2806f8SThomas Graf } 2291af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2292af2806f8SThomas Graf 2293af2806f8SThomas Graf /** 2294af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2295af2806f8SThomas Graf * @to: destination buffer 22967fceb4deSMasanari Iida * @from: source buffer 2297af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2298af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2299af2806f8SThomas Graf * 2300af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2301af2806f8SThomas Graf * to the frags in the source buffer. 2302af2806f8SThomas Graf * 2303af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2304af2806f8SThomas Graf * headroom in the `to` buffer. 230536d5fe6aSZoltan Kiss * 230636d5fe6aSZoltan Kiss * Return value: 230736d5fe6aSZoltan Kiss * 0: everything is OK 230836d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 230936d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2310af2806f8SThomas Graf */ 231136d5fe6aSZoltan Kiss int 231236d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2313af2806f8SThomas Graf { 2314af2806f8SThomas Graf int i, j = 0; 2315af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 231636d5fe6aSZoltan Kiss int ret; 2317af2806f8SThomas Graf struct page *page; 2318af2806f8SThomas Graf unsigned int offset; 2319af2806f8SThomas Graf 2320af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2321af2806f8SThomas Graf 2322af2806f8SThomas Graf /* dont bother with small payloads */ 232336d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 232436d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2325af2806f8SThomas Graf 2326af2806f8SThomas Graf if (hlen) { 232736d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 232836d5fe6aSZoltan Kiss if (unlikely(ret)) 232936d5fe6aSZoltan Kiss return ret; 2330af2806f8SThomas Graf len -= hlen; 2331af2806f8SThomas Graf } else { 2332af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2333af2806f8SThomas Graf if (plen) { 2334af2806f8SThomas Graf page = virt_to_head_page(from->head); 2335af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2336af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2337af2806f8SThomas Graf get_page(page); 2338af2806f8SThomas Graf j = 1; 2339af2806f8SThomas Graf len -= plen; 2340af2806f8SThomas Graf } 2341af2806f8SThomas Graf } 2342af2806f8SThomas Graf 2343af2806f8SThomas Graf to->truesize += len + plen; 2344af2806f8SThomas Graf to->len += len + plen; 2345af2806f8SThomas Graf to->data_len += len + plen; 2346af2806f8SThomas Graf 234736d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 234836d5fe6aSZoltan Kiss skb_tx_error(from); 234936d5fe6aSZoltan Kiss return -ENOMEM; 235036d5fe6aSZoltan Kiss } 235136d5fe6aSZoltan Kiss 2352af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2353af2806f8SThomas Graf if (!len) 2354af2806f8SThomas Graf break; 2355af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2356af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2357af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2358af2806f8SThomas Graf skb_frag_ref(to, j); 2359af2806f8SThomas Graf j++; 2360af2806f8SThomas Graf } 2361af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 236236d5fe6aSZoltan Kiss 236336d5fe6aSZoltan Kiss return 0; 2364af2806f8SThomas Graf } 2365af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2366af2806f8SThomas Graf 23671da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 23681da177e4SLinus Torvalds { 2369d3bc23e7SAl Viro __wsum csum; 23701da177e4SLinus Torvalds long csstart; 23711da177e4SLinus Torvalds 237284fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 237355508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 23741da177e4SLinus Torvalds else 23751da177e4SLinus Torvalds csstart = skb_headlen(skb); 23761da177e4SLinus Torvalds 237709a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 23781da177e4SLinus Torvalds 2379d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 23801da177e4SLinus Torvalds 23811da177e4SLinus Torvalds csum = 0; 23821da177e4SLinus Torvalds if (csstart != skb->len) 23831da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 23841da177e4SLinus Torvalds skb->len - csstart, 0); 23851da177e4SLinus Torvalds 238684fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2387ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 23881da177e4SLinus Torvalds 2389d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 23901da177e4SLinus Torvalds } 23911da177e4SLinus Torvalds } 2392b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 23931da177e4SLinus Torvalds 23941da177e4SLinus Torvalds /** 23951da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 23961da177e4SLinus Torvalds * @list: list to dequeue from 23971da177e4SLinus Torvalds * 23981da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 23991da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 24001da177e4SLinus Torvalds * returned or %NULL if the list is empty. 24011da177e4SLinus Torvalds */ 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 24041da177e4SLinus Torvalds { 24051da177e4SLinus Torvalds unsigned long flags; 24061da177e4SLinus Torvalds struct sk_buff *result; 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24091da177e4SLinus Torvalds result = __skb_dequeue(list); 24101da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24111da177e4SLinus Torvalds return result; 24121da177e4SLinus Torvalds } 2413b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds /** 24161da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 24171da177e4SLinus Torvalds * @list: list to dequeue from 24181da177e4SLinus Torvalds * 24191da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 24201da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 24211da177e4SLinus Torvalds * returned or %NULL if the list is empty. 24221da177e4SLinus Torvalds */ 24231da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 24241da177e4SLinus Torvalds { 24251da177e4SLinus Torvalds unsigned long flags; 24261da177e4SLinus Torvalds struct sk_buff *result; 24271da177e4SLinus Torvalds 24281da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24291da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 24301da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24311da177e4SLinus Torvalds return result; 24321da177e4SLinus Torvalds } 2433b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds /** 24361da177e4SLinus Torvalds * skb_queue_purge - empty a list 24371da177e4SLinus Torvalds * @list: list to empty 24381da177e4SLinus Torvalds * 24391da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 24401da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 24411da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 24421da177e4SLinus Torvalds */ 24431da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 24441da177e4SLinus Torvalds { 24451da177e4SLinus Torvalds struct sk_buff *skb; 24461da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 24471da177e4SLinus Torvalds kfree_skb(skb); 24481da177e4SLinus Torvalds } 2449b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 24501da177e4SLinus Torvalds 24511da177e4SLinus Torvalds /** 24521da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 24531da177e4SLinus Torvalds * @list: list to use 24541da177e4SLinus Torvalds * @newsk: buffer to queue 24551da177e4SLinus Torvalds * 24561da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 24571da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 24581da177e4SLinus Torvalds * safely. 24591da177e4SLinus Torvalds * 24601da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24611da177e4SLinus Torvalds */ 24621da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 24631da177e4SLinus Torvalds { 24641da177e4SLinus Torvalds unsigned long flags; 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24671da177e4SLinus Torvalds __skb_queue_head(list, newsk); 24681da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24691da177e4SLinus Torvalds } 2470b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds /** 24731da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 24741da177e4SLinus Torvalds * @list: list to use 24751da177e4SLinus Torvalds * @newsk: buffer to queue 24761da177e4SLinus Torvalds * 24771da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 24781da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 24791da177e4SLinus Torvalds * safely. 24801da177e4SLinus Torvalds * 24811da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24821da177e4SLinus Torvalds */ 24831da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 24841da177e4SLinus Torvalds { 24851da177e4SLinus Torvalds unsigned long flags; 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24881da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 24891da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24901da177e4SLinus Torvalds } 2491b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 24928728b834SDavid S. Miller 24931da177e4SLinus Torvalds /** 24941da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 24951da177e4SLinus Torvalds * @skb: buffer to remove 24968728b834SDavid S. Miller * @list: list to use 24971da177e4SLinus Torvalds * 24988728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 24998728b834SDavid S. Miller * function is atomic with respect to other list locked calls 25001da177e4SLinus Torvalds * 25018728b834SDavid S. Miller * You must know what list the SKB is on. 25021da177e4SLinus Torvalds */ 25038728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds unsigned long flags; 25061da177e4SLinus Torvalds 25071da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 25088728b834SDavid S. Miller __skb_unlink(skb, list); 25091da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 25101da177e4SLinus Torvalds } 2511b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds /** 25141da177e4SLinus Torvalds * skb_append - append a buffer 25151da177e4SLinus Torvalds * @old: buffer to insert after 25161da177e4SLinus Torvalds * @newsk: buffer to insert 25178728b834SDavid S. Miller * @list: list to use 25181da177e4SLinus Torvalds * 25191da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 25201da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 25211da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 25221da177e4SLinus Torvalds */ 25238728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 25241da177e4SLinus Torvalds { 25251da177e4SLinus Torvalds unsigned long flags; 25261da177e4SLinus Torvalds 25278728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 25287de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 25298728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 25301da177e4SLinus Torvalds } 2531b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds /** 25341da177e4SLinus Torvalds * skb_insert - insert a buffer 25351da177e4SLinus Torvalds * @old: buffer to insert before 25361da177e4SLinus Torvalds * @newsk: buffer to insert 25378728b834SDavid S. Miller * @list: list to use 25381da177e4SLinus Torvalds * 25398728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 25408728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 25418728b834SDavid S. Miller * calls. 25428728b834SDavid S. Miller * 25431da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 25441da177e4SLinus Torvalds */ 25458728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 25461da177e4SLinus Torvalds { 25471da177e4SLinus Torvalds unsigned long flags; 25481da177e4SLinus Torvalds 25498728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 25508728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 25518728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 25521da177e4SLinus Torvalds } 2553b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 25561da177e4SLinus Torvalds struct sk_buff* skb1, 25571da177e4SLinus Torvalds const u32 len, const int pos) 25581da177e4SLinus Torvalds { 25591da177e4SLinus Torvalds int i; 25601da177e4SLinus Torvalds 2561d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2562d626f62bSArnaldo Carvalho de Melo pos - len); 25631da177e4SLinus Torvalds /* And move data appendix as is. */ 25641da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 25651da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 25681da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 25691da177e4SLinus Torvalds skb1->data_len = skb->data_len; 25701da177e4SLinus Torvalds skb1->len += skb1->data_len; 25711da177e4SLinus Torvalds skb->data_len = 0; 25721da177e4SLinus Torvalds skb->len = len; 257327a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 25771da177e4SLinus Torvalds struct sk_buff* skb1, 25781da177e4SLinus Torvalds const u32 len, int pos) 25791da177e4SLinus Torvalds { 25801da177e4SLinus Torvalds int i, k = 0; 25811da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 25841da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 25851da177e4SLinus Torvalds skb->len = len; 25861da177e4SLinus Torvalds skb->data_len = len - pos; 25871da177e4SLinus Torvalds 25881da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 25899e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds if (pos + size > len) { 25921da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds if (pos < len) { 25951da177e4SLinus Torvalds /* Split frag. 25961da177e4SLinus Torvalds * We have two variants in this case: 25971da177e4SLinus Torvalds * 1. Move all the frag to the second 25981da177e4SLinus Torvalds * part, if it is possible. F.e. 25991da177e4SLinus Torvalds * this approach is mandatory for TUX, 26001da177e4SLinus Torvalds * where splitting is expensive. 26011da177e4SLinus Torvalds * 2. Split is accurately. We make this. 26021da177e4SLinus Torvalds */ 2603ea2ab693SIan Campbell skb_frag_ref(skb, i); 26041da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 26059e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 26069e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 26071da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 26081da177e4SLinus Torvalds } 26091da177e4SLinus Torvalds k++; 26101da177e4SLinus Torvalds } else 26111da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 26121da177e4SLinus Torvalds pos += size; 26131da177e4SLinus Torvalds } 26141da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 26171da177e4SLinus Torvalds /** 26181da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 26191da177e4SLinus Torvalds * @skb: the buffer to split 26201da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 26211da177e4SLinus Torvalds * @len: new length for skb 26221da177e4SLinus Torvalds */ 26231da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 26241da177e4SLinus Torvalds { 26251da177e4SLinus Torvalds int pos = skb_headlen(skb); 26261da177e4SLinus Torvalds 262768534c68SAmerigo Wang skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 26281da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 26291da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 26301da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 26311da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 26321da177e4SLinus Torvalds } 2633b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 26341da177e4SLinus Torvalds 26359f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 26369f782db3SIlpo Järvinen * 26379f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 26389f782db3SIlpo Järvinen */ 2639832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2640832d11c5SIlpo Järvinen { 26410ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2642832d11c5SIlpo Järvinen } 2643832d11c5SIlpo Järvinen 2644832d11c5SIlpo Järvinen /** 2645832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2646832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2647832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2648832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2649832d11c5SIlpo Järvinen * 2650832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 265120e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2652832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2653832d11c5SIlpo Järvinen * 2654832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2655832d11c5SIlpo Järvinen * 2656832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2657832d11c5SIlpo Järvinen * to have non-paged data as well. 2658832d11c5SIlpo Järvinen * 2659832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2660832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2661832d11c5SIlpo Järvinen */ 2662832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2663832d11c5SIlpo Järvinen { 2664832d11c5SIlpo Järvinen int from, to, merge, todo; 2665832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2666832d11c5SIlpo Järvinen 2667832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2668832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2669832d11c5SIlpo Järvinen 2670832d11c5SIlpo Järvinen todo = shiftlen; 2671832d11c5SIlpo Järvinen from = 0; 2672832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2673832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2674832d11c5SIlpo Järvinen 2675832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2676832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2677832d11c5SIlpo Järvinen */ 2678832d11c5SIlpo Järvinen if (!to || 2679ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2680ea2ab693SIan Campbell fragfrom->page_offset)) { 2681832d11c5SIlpo Järvinen merge = -1; 2682832d11c5SIlpo Järvinen } else { 2683832d11c5SIlpo Järvinen merge = to - 1; 2684832d11c5SIlpo Järvinen 26859e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2686832d11c5SIlpo Järvinen if (todo < 0) { 2687832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2688832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2689832d11c5SIlpo Järvinen return 0; 2690832d11c5SIlpo Järvinen 26919f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 26929f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2693832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2694832d11c5SIlpo Järvinen 26959e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 26969e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2697832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2698832d11c5SIlpo Järvinen 2699832d11c5SIlpo Järvinen goto onlymerged; 2700832d11c5SIlpo Järvinen } 2701832d11c5SIlpo Järvinen 2702832d11c5SIlpo Järvinen from++; 2703832d11c5SIlpo Järvinen } 2704832d11c5SIlpo Järvinen 2705832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2706832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2707832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2708832d11c5SIlpo Järvinen return 0; 2709832d11c5SIlpo Järvinen 2710832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2711832d11c5SIlpo Järvinen return 0; 2712832d11c5SIlpo Järvinen 2713832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2714832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2715832d11c5SIlpo Järvinen return 0; 2716832d11c5SIlpo Järvinen 2717832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2718832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2719832d11c5SIlpo Järvinen 27209e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2721832d11c5SIlpo Järvinen *fragto = *fragfrom; 27229e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2723832d11c5SIlpo Järvinen from++; 2724832d11c5SIlpo Järvinen to++; 2725832d11c5SIlpo Järvinen 2726832d11c5SIlpo Järvinen } else { 2727ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2728832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2729832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 27309e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2731832d11c5SIlpo Järvinen 2732832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 27339e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2734832d11c5SIlpo Järvinen todo = 0; 2735832d11c5SIlpo Järvinen 2736832d11c5SIlpo Järvinen to++; 2737832d11c5SIlpo Järvinen break; 2738832d11c5SIlpo Järvinen } 2739832d11c5SIlpo Järvinen } 2740832d11c5SIlpo Järvinen 2741832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2742832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2743832d11c5SIlpo Järvinen 2744832d11c5SIlpo Järvinen if (merge >= 0) { 2745832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2746832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2747832d11c5SIlpo Järvinen 27489e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2749ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2750832d11c5SIlpo Järvinen } 2751832d11c5SIlpo Järvinen 2752832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2753832d11c5SIlpo Järvinen to = 0; 2754832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2755832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2756832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2757832d11c5SIlpo Järvinen 2758832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2759832d11c5SIlpo Järvinen 2760832d11c5SIlpo Järvinen onlymerged: 2761832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2762832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2763832d11c5SIlpo Järvinen */ 2764832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2765832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2766832d11c5SIlpo Järvinen 2767832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2768832d11c5SIlpo Järvinen skb->len -= shiftlen; 2769832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2770832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2771832d11c5SIlpo Järvinen tgt->len += shiftlen; 2772832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2773832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2774832d11c5SIlpo Järvinen 2775832d11c5SIlpo Järvinen return shiftlen; 2776832d11c5SIlpo Järvinen } 2777832d11c5SIlpo Järvinen 2778677e90edSThomas Graf /** 2779677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2780677e90edSThomas Graf * @skb: the buffer to read 2781677e90edSThomas Graf * @from: lower offset of data to be read 2782677e90edSThomas Graf * @to: upper offset of data to be read 2783677e90edSThomas Graf * @st: state variable 2784677e90edSThomas Graf * 2785677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2786677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2787677e90edSThomas Graf */ 2788677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2789677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2790677e90edSThomas Graf { 2791677e90edSThomas Graf st->lower_offset = from; 2792677e90edSThomas Graf st->upper_offset = to; 2793677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2794677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2795677e90edSThomas Graf st->frag_data = NULL; 2796677e90edSThomas Graf } 2797b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2798677e90edSThomas Graf 2799677e90edSThomas Graf /** 2800677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2801677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2802677e90edSThomas Graf * @data: destination pointer for data to be returned 2803677e90edSThomas Graf * @st: state variable 2804677e90edSThomas Graf * 2805bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 2806677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2807bc32383cSMathias Krause * the head of the data block to @data and returns the length 2808677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2809677e90edSThomas Graf * offset has been reached. 2810677e90edSThomas Graf * 2811677e90edSThomas Graf * The caller is not required to consume all of the data 2812bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 2813677e90edSThomas Graf * of bytes already consumed and the next call to 2814677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2815677e90edSThomas Graf * 281625985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2817e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 2818677e90edSThomas Graf * reads of potentially non linear data. 2819677e90edSThomas Graf * 2820bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2821677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2822677e90edSThomas Graf * a stack for this purpose. 2823677e90edSThomas Graf */ 2824677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2825677e90edSThomas Graf struct skb_seq_state *st) 2826677e90edSThomas Graf { 2827677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2828677e90edSThomas Graf skb_frag_t *frag; 2829677e90edSThomas Graf 2830aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 2831aeb193eaSWedson Almeida Filho if (st->frag_data) { 2832aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 2833aeb193eaSWedson Almeida Filho st->frag_data = NULL; 2834aeb193eaSWedson Almeida Filho } 2835677e90edSThomas Graf return 0; 2836aeb193eaSWedson Almeida Filho } 2837677e90edSThomas Graf 2838677e90edSThomas Graf next_skb: 283995e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2840677e90edSThomas Graf 2841995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 284295e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2843677e90edSThomas Graf return block_limit - abs_offset; 2844677e90edSThomas Graf } 2845677e90edSThomas Graf 2846677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2847677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2848677e90edSThomas Graf 2849677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2850677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 28519e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2852677e90edSThomas Graf 2853677e90edSThomas Graf if (abs_offset < block_limit) { 2854677e90edSThomas Graf if (!st->frag_data) 285551c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2856677e90edSThomas Graf 2857677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2858677e90edSThomas Graf (abs_offset - st->stepped_offset); 2859677e90edSThomas Graf 2860677e90edSThomas Graf return block_limit - abs_offset; 2861677e90edSThomas Graf } 2862677e90edSThomas Graf 2863677e90edSThomas Graf if (st->frag_data) { 286451c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2865677e90edSThomas Graf st->frag_data = NULL; 2866677e90edSThomas Graf } 2867677e90edSThomas Graf 2868677e90edSThomas Graf st->frag_idx++; 28699e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2870677e90edSThomas Graf } 2871677e90edSThomas Graf 28725b5a60daSOlaf Kirch if (st->frag_data) { 287351c56b00SEric Dumazet kunmap_atomic(st->frag_data); 28745b5a60daSOlaf Kirch st->frag_data = NULL; 28755b5a60daSOlaf Kirch } 28765b5a60daSOlaf Kirch 287721dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2878677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 287995e3b24cSHerbert Xu st->frag_idx = 0; 2880677e90edSThomas Graf goto next_skb; 288171b3346dSShyam Iyer } else if (st->cur_skb->next) { 288271b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 288371b3346dSShyam Iyer st->frag_idx = 0; 2884677e90edSThomas Graf goto next_skb; 2885677e90edSThomas Graf } 2886677e90edSThomas Graf 2887677e90edSThomas Graf return 0; 2888677e90edSThomas Graf } 2889b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2890677e90edSThomas Graf 2891677e90edSThomas Graf /** 2892677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2893677e90edSThomas Graf * @st: state variable 2894677e90edSThomas Graf * 2895677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2896677e90edSThomas Graf * returned 0. 2897677e90edSThomas Graf */ 2898677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2899677e90edSThomas Graf { 2900677e90edSThomas Graf if (st->frag_data) 290151c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2902677e90edSThomas Graf } 2903b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2904677e90edSThomas Graf 29053fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 29063fc7e8a6SThomas Graf 29073fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 29083fc7e8a6SThomas Graf struct ts_config *conf, 29093fc7e8a6SThomas Graf struct ts_state *state) 29103fc7e8a6SThomas Graf { 29113fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 29123fc7e8a6SThomas Graf } 29133fc7e8a6SThomas Graf 29143fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 29153fc7e8a6SThomas Graf { 29163fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 29173fc7e8a6SThomas Graf } 29183fc7e8a6SThomas Graf 29193fc7e8a6SThomas Graf /** 29203fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 29213fc7e8a6SThomas Graf * @skb: the buffer to look in 29223fc7e8a6SThomas Graf * @from: search offset 29233fc7e8a6SThomas Graf * @to: search limit 29243fc7e8a6SThomas Graf * @config: textsearch configuration 29253fc7e8a6SThomas Graf * 29263fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 29273fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 29283fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 29293fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 29303fc7e8a6SThomas Graf */ 29313fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2932059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 29333fc7e8a6SThomas Graf { 2934059a2440SBojan Prtvar struct ts_state state; 2935f72b948dSPhil Oester unsigned int ret; 2936f72b948dSPhil Oester 29373fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 29383fc7e8a6SThomas Graf config->finish = skb_ts_finish; 29393fc7e8a6SThomas Graf 2940059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 29413fc7e8a6SThomas Graf 2942059a2440SBojan Prtvar ret = textsearch_find(config, &state); 2943f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 29443fc7e8a6SThomas Graf } 2945b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 29463fc7e8a6SThomas Graf 2947e89e9cf5SAnanda Raju /** 29482c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2949e89e9cf5SAnanda Raju * @sk: sock structure 2950e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 2951e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2952e89e9cf5SAnanda Raju * @from: pointer to user message iov 2953e89e9cf5SAnanda Raju * @length: length of the iov message 2954e89e9cf5SAnanda Raju * 2955e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2956e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2957e89e9cf5SAnanda Raju */ 2958e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2959dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2960e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2961e89e9cf5SAnanda Raju void *from, int length) 2962e89e9cf5SAnanda Raju { 2963b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 2964b2111724SEric Dumazet int copy; 2965e89e9cf5SAnanda Raju int offset = 0; 2966e89e9cf5SAnanda Raju int ret; 2967b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 2968e89e9cf5SAnanda Raju 2969e89e9cf5SAnanda Raju do { 2970e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2971e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2972b2111724SEric Dumazet return -EMSGSIZE; 2973e89e9cf5SAnanda Raju 2974b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 2975e89e9cf5SAnanda Raju return -ENOMEM; 2976e89e9cf5SAnanda Raju 2977e89e9cf5SAnanda Raju /* copy the user data to page */ 2978b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 2979e89e9cf5SAnanda Raju 2980b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 2981e89e9cf5SAnanda Raju offset, copy, 0, skb); 2982e89e9cf5SAnanda Raju if (ret < 0) 2983e89e9cf5SAnanda Raju return -EFAULT; 2984e89e9cf5SAnanda Raju 2985e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2986b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 2987b2111724SEric Dumazet copy); 2988b2111724SEric Dumazet frg_cnt++; 2989b2111724SEric Dumazet pfrag->offset += copy; 2990b2111724SEric Dumazet get_page(pfrag->page); 2991b2111724SEric Dumazet 2992b2111724SEric Dumazet skb->truesize += copy; 2993b2111724SEric Dumazet atomic_add(copy, &sk->sk_wmem_alloc); 2994e89e9cf5SAnanda Raju skb->len += copy; 2995e89e9cf5SAnanda Raju skb->data_len += copy; 2996e89e9cf5SAnanda Raju offset += copy; 2997e89e9cf5SAnanda Raju length -= copy; 2998e89e9cf5SAnanda Raju 2999e89e9cf5SAnanda Raju } while (length > 0); 3000e89e9cf5SAnanda Raju 3001e89e9cf5SAnanda Raju return 0; 3002e89e9cf5SAnanda Raju } 3003b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 3004e89e9cf5SAnanda Raju 3005be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 3006be12a1feSHannes Frederic Sowa int offset, size_t size) 3007be12a1feSHannes Frederic Sowa { 3008be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 3009be12a1feSHannes Frederic Sowa 3010be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 3011be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 3012be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 3013be12a1feSHannes Frederic Sowa get_page(page); 3014be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 3015be12a1feSHannes Frederic Sowa } else { 3016be12a1feSHannes Frederic Sowa return -EMSGSIZE; 3017be12a1feSHannes Frederic Sowa } 3018be12a1feSHannes Frederic Sowa 3019be12a1feSHannes Frederic Sowa return 0; 3020be12a1feSHannes Frederic Sowa } 3021be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 3022be12a1feSHannes Frederic Sowa 3023cbb042f9SHerbert Xu /** 3024cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 3025cbb042f9SHerbert Xu * @skb: buffer to update 3026cbb042f9SHerbert Xu * @len: length of data pulled 3027cbb042f9SHerbert Xu * 3028cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 3029fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 303084fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 303184fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 303284fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 3033cbb042f9SHerbert Xu */ 3034cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 3035cbb042f9SHerbert Xu { 303631b33dfbSPravin B Shelar unsigned char *data = skb->data; 303731b33dfbSPravin B Shelar 3038cbb042f9SHerbert Xu BUG_ON(len > skb->len); 303931b33dfbSPravin B Shelar __skb_pull(skb, len); 304031b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 304131b33dfbSPravin B Shelar return skb->data; 3042cbb042f9SHerbert Xu } 3043f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 3044f94691acSArnaldo Carvalho de Melo 3045f4c50d99SHerbert Xu /** 3046f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 3047df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 3048576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 3049f4c50d99SHerbert Xu * 3050f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 30514c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 30524c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 3053f4c50d99SHerbert Xu */ 3054df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 3055df5771ffSMichael S. Tsirkin netdev_features_t features) 3056f4c50d99SHerbert Xu { 3057f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 3058f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 30591a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 3060df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 3061df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 3062df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 30631fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 3064f4c50d99SHerbert Xu unsigned int offset = doffset; 3065df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 3066f4c50d99SHerbert Xu unsigned int headroom; 3067f4c50d99SHerbert Xu unsigned int len; 3068ec5f0615SPravin B Shelar __be16 proto; 3069ec5f0615SPravin B Shelar bool csum; 307004ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 3071df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 3072f4c50d99SHerbert Xu int err = -ENOMEM; 3073f4c50d99SHerbert Xu int i = 0; 3074f4c50d99SHerbert Xu int pos; 307553d6471cSVlad Yasevich int dummy; 3076f4c50d99SHerbert Xu 30775882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 307853d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3079ec5f0615SPravin B Shelar if (unlikely(!proto)) 3080ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3081ec5f0615SPravin B Shelar 3082f245d079SAlexander Duyck csum = !!can_checksum_protocol(features, proto); 30837e2b10c1STom Herbert 3084df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3085df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3086f4c50d99SHerbert Xu 3087f4c50d99SHerbert Xu do { 3088f4c50d99SHerbert Xu struct sk_buff *nskb; 30898cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3090c8884eddSHerbert Xu int hsize; 3091f4c50d99SHerbert Xu int size; 3092f4c50d99SHerbert Xu 3093df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3094f4c50d99SHerbert Xu if (len > mss) 3095f4c50d99SHerbert Xu len = mss; 3096f4c50d99SHerbert Xu 3097df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3098f4c50d99SHerbert Xu if (hsize < 0) 3099f4c50d99SHerbert Xu hsize = 0; 3100c8884eddSHerbert Xu if (hsize > len || !sg) 3101c8884eddSHerbert Xu hsize = len; 3102f4c50d99SHerbert Xu 31031a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 31041a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 31051a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 310689319d38SHerbert Xu 31079d8506ccSHerbert Xu i = 0; 31081a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 31091a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 31101fd819ecSMichael S. Tsirkin frag_skb = list_skb; 31111a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 31129d8506ccSHerbert Xu 31139d8506ccSHerbert Xu while (pos < offset + len) { 31149d8506ccSHerbert Xu BUG_ON(i >= nfrags); 31159d8506ccSHerbert Xu 31164e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 31179d8506ccSHerbert Xu if (pos + size > offset + len) 31189d8506ccSHerbert Xu break; 31199d8506ccSHerbert Xu 31209d8506ccSHerbert Xu i++; 31219d8506ccSHerbert Xu pos += size; 31224e1beba1SMichael S. Tsirkin frag++; 31239d8506ccSHerbert Xu } 31249d8506ccSHerbert Xu 31251a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 31261a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 312789319d38SHerbert Xu 3128f4c50d99SHerbert Xu if (unlikely(!nskb)) 3129f4c50d99SHerbert Xu goto err; 3130f4c50d99SHerbert Xu 31319d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 31329d8506ccSHerbert Xu kfree_skb(nskb); 31339d8506ccSHerbert Xu goto err; 31349d8506ccSHerbert Xu } 31359d8506ccSHerbert Xu 3136ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 313789319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 313889319d38SHerbert Xu kfree_skb(nskb); 313989319d38SHerbert Xu goto err; 314089319d38SHerbert Xu } 314189319d38SHerbert Xu 3142ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 314389319d38SHerbert Xu skb_release_head_state(nskb); 314489319d38SHerbert Xu __skb_push(nskb, doffset); 314589319d38SHerbert Xu } else { 3146c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3147df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3148c93bdd0eSMel Gorman NUMA_NO_NODE); 314989319d38SHerbert Xu 315089319d38SHerbert Xu if (unlikely(!nskb)) 315189319d38SHerbert Xu goto err; 315289319d38SHerbert Xu 315389319d38SHerbert Xu skb_reserve(nskb, headroom); 315489319d38SHerbert Xu __skb_put(nskb, doffset); 315589319d38SHerbert Xu } 315689319d38SHerbert Xu 3157f4c50d99SHerbert Xu if (segs) 3158f4c50d99SHerbert Xu tail->next = nskb; 3159f4c50d99SHerbert Xu else 3160f4c50d99SHerbert Xu segs = nskb; 3161f4c50d99SHerbert Xu tail = nskb; 3162f4c50d99SHerbert Xu 3163df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3164f4c50d99SHerbert Xu 3165030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3166fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 316768c33163SPravin B Shelar 3168df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 316968c33163SPravin B Shelar nskb->data - tnl_hlen, 317068c33163SPravin B Shelar doffset + tnl_hlen); 317189319d38SHerbert Xu 31729d8506ccSHerbert Xu if (nskb->len == len + doffset) 31731cdbcb79SSimon Horman goto perform_csum_check; 317489319d38SHerbert Xu 31757fbeffedSAlexander Duyck if (!sg) { 31767fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 31776f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 317876443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 317976443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 3180f4c50d99SHerbert Xu skb_put(nskb, len), 3181f4c50d99SHerbert Xu len, 0); 31827e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3183de843723STom Herbert skb_headroom(nskb) + doffset; 3184f4c50d99SHerbert Xu continue; 3185f4c50d99SHerbert Xu } 3186f4c50d99SHerbert Xu 31878cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3188f4c50d99SHerbert Xu 3189df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3190d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3191f4c50d99SHerbert Xu 3192df5771ffSMichael S. Tsirkin skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & 3193df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3194cef401deSEric Dumazet 31959d8506ccSHerbert Xu while (pos < offset + len) { 31969d8506ccSHerbert Xu if (i >= nfrags) { 31971a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 31989d8506ccSHerbert Xu 31999d8506ccSHerbert Xu i = 0; 32001a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 32011a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 32021fd819ecSMichael S. Tsirkin frag_skb = list_skb; 32039d8506ccSHerbert Xu 32049d8506ccSHerbert Xu BUG_ON(!nfrags); 32059d8506ccSHerbert Xu 32061a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 32079d8506ccSHerbert Xu } 32089d8506ccSHerbert Xu 32099d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 32109d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 32119d8506ccSHerbert Xu net_warn_ratelimited( 32129d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 32139d8506ccSHerbert Xu pos, mss); 32149d8506ccSHerbert Xu goto err; 32159d8506ccSHerbert Xu } 32169d8506ccSHerbert Xu 32171fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 32181fd819ecSMichael S. Tsirkin goto err; 32191fd819ecSMichael S. Tsirkin 32204e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 32218cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 32228cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3223f4c50d99SHerbert Xu 3224f4c50d99SHerbert Xu if (pos < offset) { 32258cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 32268cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3227f4c50d99SHerbert Xu } 3228f4c50d99SHerbert Xu 322989319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3230f4c50d99SHerbert Xu 3231f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3232f4c50d99SHerbert Xu i++; 32334e1beba1SMichael S. Tsirkin frag++; 3234f4c50d99SHerbert Xu pos += size; 3235f4c50d99SHerbert Xu } else { 32368cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 323789319d38SHerbert Xu goto skip_fraglist; 3238f4c50d99SHerbert Xu } 3239f4c50d99SHerbert Xu 32408cb19905SMichael S. Tsirkin nskb_frag++; 3241f4c50d99SHerbert Xu } 3242f4c50d99SHerbert Xu 324389319d38SHerbert Xu skip_fraglist: 3244f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3245f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3246f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3247ec5f0615SPravin B Shelar 32481cdbcb79SSimon Horman perform_csum_check: 32497fbeffedSAlexander Duyck if (!csum) { 3250ddff00d4SAlexander Duyck if (skb_has_shared_frag(nskb)) { 3251ddff00d4SAlexander Duyck err = __skb_linearize(nskb); 3252ddff00d4SAlexander Duyck if (err) 3253ddff00d4SAlexander Duyck goto err; 3254ddff00d4SAlexander Duyck } 32557fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 3256ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 325776443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 325876443456SAlexander Duyck skb_checksum(nskb, doffset, 325976443456SAlexander Duyck nskb->len - doffset, 0); 32607e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 32617e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3262ec5f0615SPravin B Shelar } 3263df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3264f4c50d99SHerbert Xu 3265bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3266bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3267bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3268bec3cfdcSEric Dumazet */ 3269bec3cfdcSEric Dumazet segs->prev = tail; 3270432c856fSToshiaki Makita 3271432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3272432c856fSToshiaki Makita * using skb_set_owner_w(). 3273432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3274432c856fSToshiaki Makita */ 3275432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3276432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3277432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3278432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3279432c856fSToshiaki Makita } 3280f4c50d99SHerbert Xu return segs; 3281f4c50d99SHerbert Xu 3282f4c50d99SHerbert Xu err: 3283289dccbeSEric Dumazet kfree_skb_list(segs); 3284f4c50d99SHerbert Xu return ERR_PTR(err); 3285f4c50d99SHerbert Xu } 3286f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3287f4c50d99SHerbert Xu 328871d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 328971d93b39SHerbert Xu { 32908a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 329167147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 329267147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 32938a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 329458025e46SEric Dumazet struct sk_buff *lp, *p = *head; 3295715dc1f3SEric Dumazet unsigned int delta_truesize; 329671d93b39SHerbert Xu 32978a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 329871d93b39SHerbert Xu return -E2BIG; 329971d93b39SHerbert Xu 330029e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 33018a29111cSEric Dumazet pinfo = skb_shinfo(lp); 33028a29111cSEric Dumazet 33038a29111cSEric Dumazet if (headlen <= offset) { 330442da6994SHerbert Xu skb_frag_t *frag; 330566e92fcfSHerbert Xu skb_frag_t *frag2; 33069aaa156cSHerbert Xu int i = skbinfo->nr_frags; 33079aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 330842da6994SHerbert Xu 330966e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 33108a29111cSEric Dumazet goto merge; 331181705ad1SHerbert Xu 33128a29111cSEric Dumazet offset -= headlen; 33139aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 33149aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3315f5572068SHerbert Xu 33169aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 33179aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 331866e92fcfSHerbert Xu do { 331966e92fcfSHerbert Xu *--frag = *--frag2; 332066e92fcfSHerbert Xu } while (--i); 332166e92fcfSHerbert Xu 332266e92fcfSHerbert Xu frag->page_offset += offset; 33239e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 332466e92fcfSHerbert Xu 3325715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3326ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3327ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3328715dc1f3SEric Dumazet 3329f5572068SHerbert Xu skb->truesize -= skb->data_len; 3330f5572068SHerbert Xu skb->len -= skb->data_len; 3331f5572068SHerbert Xu skb->data_len = 0; 3332f5572068SHerbert Xu 3333715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 33345d38a079SHerbert Xu goto done; 3335d7e8883cSEric Dumazet } else if (skb->head_frag) { 3336d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3337d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3338d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3339d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3340d7e8883cSEric Dumazet unsigned int first_offset; 3341d7e8883cSEric Dumazet 3342d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 33438a29111cSEric Dumazet goto merge; 3344d7e8883cSEric Dumazet 3345d7e8883cSEric Dumazet first_offset = skb->data - 3346d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3347d7e8883cSEric Dumazet offset; 3348d7e8883cSEric Dumazet 3349d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3350d7e8883cSEric Dumazet 3351d7e8883cSEric Dumazet frag->page.p = page; 3352d7e8883cSEric Dumazet frag->page_offset = first_offset; 3353d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3354d7e8883cSEric Dumazet 3355d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3356d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3357d7e8883cSEric Dumazet 3358715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3359d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3360d7e8883cSEric Dumazet goto done; 33618a29111cSEric Dumazet } 336271d93b39SHerbert Xu 336371d93b39SHerbert Xu merge: 3364715dc1f3SEric Dumazet delta_truesize = skb->truesize; 336567147ba9SHerbert Xu if (offset > headlen) { 3366d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3367d1dc7abfSMichal Schmidt 3368d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 33699e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3370d1dc7abfSMichal Schmidt skb->data_len -= eat; 3371d1dc7abfSMichal Schmidt skb->len -= eat; 337267147ba9SHerbert Xu offset = headlen; 337356035022SHerbert Xu } 337456035022SHerbert Xu 337567147ba9SHerbert Xu __skb_pull(skb, offset); 337656035022SHerbert Xu 337729e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 33788a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 33798a29111cSEric Dumazet else 3380c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3381c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3382f4a775d1SEric Dumazet __skb_header_release(skb); 33838a29111cSEric Dumazet lp = p; 338471d93b39SHerbert Xu 33855d38a079SHerbert Xu done: 33865d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 338737fe4732SHerbert Xu p->data_len += len; 3388715dc1f3SEric Dumazet p->truesize += delta_truesize; 338937fe4732SHerbert Xu p->len += len; 33908a29111cSEric Dumazet if (lp != p) { 33918a29111cSEric Dumazet lp->data_len += len; 33928a29111cSEric Dumazet lp->truesize += delta_truesize; 33938a29111cSEric Dumazet lp->len += len; 33948a29111cSEric Dumazet } 339571d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 339671d93b39SHerbert Xu return 0; 339771d93b39SHerbert Xu } 339871d93b39SHerbert Xu 33991da177e4SLinus Torvalds void __init skb_init(void) 34001da177e4SLinus Torvalds { 34011da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 34021da177e4SLinus Torvalds sizeof(struct sk_buff), 34031da177e4SLinus Torvalds 0, 3404e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 340520c2df83SPaul Mundt NULL); 3406d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3407d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3408d179cd12SDavid S. Miller 0, 3409e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 341020c2df83SPaul Mundt NULL); 34111da177e4SLinus Torvalds } 34121da177e4SLinus Torvalds 3413716ea3a7SDavid Howells /** 3414716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3415716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3416716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3417716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3418716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3419716ea3a7SDavid Howells * 3420716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3421716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3422716ea3a7SDavid Howells */ 342351c739d1SDavid S. Miller static int 342451c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3425716ea3a7SDavid Howells { 34261a028e50SDavid S. Miller int start = skb_headlen(skb); 34271a028e50SDavid S. Miller int i, copy = start - offset; 3428fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3429716ea3a7SDavid Howells int elt = 0; 3430716ea3a7SDavid Howells 3431716ea3a7SDavid Howells if (copy > 0) { 3432716ea3a7SDavid Howells if (copy > len) 3433716ea3a7SDavid Howells copy = len; 3434642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3435716ea3a7SDavid Howells elt++; 3436716ea3a7SDavid Howells if ((len -= copy) == 0) 3437716ea3a7SDavid Howells return elt; 3438716ea3a7SDavid Howells offset += copy; 3439716ea3a7SDavid Howells } 3440716ea3a7SDavid Howells 3441716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 34421a028e50SDavid S. Miller int end; 3443716ea3a7SDavid Howells 3444547b792cSIlpo Järvinen WARN_ON(start > offset + len); 34451a028e50SDavid S. Miller 34469e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3447716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3448716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3449716ea3a7SDavid Howells 3450716ea3a7SDavid Howells if (copy > len) 3451716ea3a7SDavid Howells copy = len; 3452ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3453642f1490SJens Axboe frag->page_offset+offset-start); 3454716ea3a7SDavid Howells elt++; 3455716ea3a7SDavid Howells if (!(len -= copy)) 3456716ea3a7SDavid Howells return elt; 3457716ea3a7SDavid Howells offset += copy; 3458716ea3a7SDavid Howells } 34591a028e50SDavid S. Miller start = end; 3460716ea3a7SDavid Howells } 3461716ea3a7SDavid Howells 3462fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 34631a028e50SDavid S. Miller int end; 3464716ea3a7SDavid Howells 3465547b792cSIlpo Järvinen WARN_ON(start > offset + len); 34661a028e50SDavid S. Miller 3467fbb398a8SDavid S. Miller end = start + frag_iter->len; 3468716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3469716ea3a7SDavid Howells if (copy > len) 3470716ea3a7SDavid Howells copy = len; 3471fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 347251c739d1SDavid S. Miller copy); 3473716ea3a7SDavid Howells if ((len -= copy) == 0) 3474716ea3a7SDavid Howells return elt; 3475716ea3a7SDavid Howells offset += copy; 3476716ea3a7SDavid Howells } 34771a028e50SDavid S. Miller start = end; 3478716ea3a7SDavid Howells } 3479716ea3a7SDavid Howells BUG_ON(len); 3480716ea3a7SDavid Howells return elt; 3481716ea3a7SDavid Howells } 3482716ea3a7SDavid Howells 348325a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 348425a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 348525a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 348625a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 348725a91d8dSFan Du * 348825a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 348925a91d8dSFan Du * 1. sg_init_table 349025a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 349125a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 349225a91d8dSFan Du * 349325a91d8dSFan Du * This is equivalent to: 349425a91d8dSFan Du * 1. sg_init_table 349525a91d8dSFan Du * 2. skb_to_sgvec(payload1) 349625a91d8dSFan Du * 3. sg_unmark_end 349725a91d8dSFan Du * 4. skb_to_sgvec(payload2) 349825a91d8dSFan Du * 349925a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 350025a91d8dSFan Du * is more preferable. 350125a91d8dSFan Du */ 350225a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 350325a91d8dSFan Du int offset, int len) 350425a91d8dSFan Du { 350525a91d8dSFan Du return __skb_to_sgvec(skb, sg, offset, len); 350625a91d8dSFan Du } 350725a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 350825a91d8dSFan Du 350951c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 351051c739d1SDavid S. Miller { 351151c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 351251c739d1SDavid S. Miller 3513c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 351451c739d1SDavid S. Miller 351551c739d1SDavid S. Miller return nsg; 351651c739d1SDavid S. Miller } 3517b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 351851c739d1SDavid S. Miller 3519716ea3a7SDavid Howells /** 3520716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3521716ea3a7SDavid Howells * @skb: The socket buffer to check. 3522716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3523716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3524716ea3a7SDavid Howells * 3525716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3526716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3527716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3528716ea3a7SDavid Howells * 3529716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3530716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3531716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3532716ea3a7SDavid Howells * 3533716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3534716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3535716ea3a7SDavid Howells */ 3536716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3537716ea3a7SDavid Howells { 3538716ea3a7SDavid Howells int copyflag; 3539716ea3a7SDavid Howells int elt; 3540716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3541716ea3a7SDavid Howells 3542716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3543716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3544716ea3a7SDavid Howells * at the moment even if they are anonymous). 3545716ea3a7SDavid Howells */ 3546716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3547716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3548716ea3a7SDavid Howells return -ENOMEM; 3549716ea3a7SDavid Howells 3550716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 355121dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3552716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3553716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3554716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3555716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3556716ea3a7SDavid Howells 3557716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3558716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3559716ea3a7SDavid Howells return -ENOMEM; 3560716ea3a7SDavid Howells 3561716ea3a7SDavid Howells /* Voila! */ 3562716ea3a7SDavid Howells *trailer = skb; 3563716ea3a7SDavid Howells return 1; 3564716ea3a7SDavid Howells } 3565716ea3a7SDavid Howells 3566716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3567716ea3a7SDavid Howells 3568716ea3a7SDavid Howells elt = 1; 3569716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3570716ea3a7SDavid Howells copyflag = 0; 3571716ea3a7SDavid Howells 3572716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3573716ea3a7SDavid Howells int ntail = 0; 3574716ea3a7SDavid Howells 3575716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3576716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3577716ea3a7SDavid Howells * after it. */ 3578716ea3a7SDavid Howells 3579716ea3a7SDavid Howells if (skb_shared(skb1)) 3580716ea3a7SDavid Howells copyflag = 1; 3581716ea3a7SDavid Howells 3582716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3583716ea3a7SDavid Howells 3584716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3585716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 358621dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3587716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3588716ea3a7SDavid Howells ntail = tailbits + 128; 3589716ea3a7SDavid Howells } 3590716ea3a7SDavid Howells 3591716ea3a7SDavid Howells if (copyflag || 3592716ea3a7SDavid Howells skb_cloned(skb1) || 3593716ea3a7SDavid Howells ntail || 3594716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 359521dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3596716ea3a7SDavid Howells struct sk_buff *skb2; 3597716ea3a7SDavid Howells 3598716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3599716ea3a7SDavid Howells if (ntail == 0) 3600716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3601716ea3a7SDavid Howells else 3602716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3603716ea3a7SDavid Howells skb_headroom(skb1), 3604716ea3a7SDavid Howells ntail, 3605716ea3a7SDavid Howells GFP_ATOMIC); 3606716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3607716ea3a7SDavid Howells return -ENOMEM; 3608716ea3a7SDavid Howells 3609716ea3a7SDavid Howells if (skb1->sk) 3610716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3611716ea3a7SDavid Howells 3612716ea3a7SDavid Howells /* Looking around. Are we still alive? 3613716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3614716ea3a7SDavid Howells 3615716ea3a7SDavid Howells skb2->next = skb1->next; 3616716ea3a7SDavid Howells *skb_p = skb2; 3617716ea3a7SDavid Howells kfree_skb(skb1); 3618716ea3a7SDavid Howells skb1 = skb2; 3619716ea3a7SDavid Howells } 3620716ea3a7SDavid Howells elt++; 3621716ea3a7SDavid Howells *trailer = skb1; 3622716ea3a7SDavid Howells skb_p = &skb1->next; 3623716ea3a7SDavid Howells } 3624716ea3a7SDavid Howells 3625716ea3a7SDavid Howells return elt; 3626716ea3a7SDavid Howells } 3627b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3628716ea3a7SDavid Howells 3629b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3630b1faf566SEric Dumazet { 3631b1faf566SEric Dumazet struct sock *sk = skb->sk; 3632b1faf566SEric Dumazet 3633b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3634b1faf566SEric Dumazet } 3635b1faf566SEric Dumazet 3636b1faf566SEric Dumazet /* 3637b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3638b1faf566SEric Dumazet */ 3639b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3640b1faf566SEric Dumazet { 3641b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 364295c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3643b1faf566SEric Dumazet return -ENOMEM; 3644b1faf566SEric Dumazet 3645b1faf566SEric Dumazet skb_orphan(skb); 3646b1faf566SEric Dumazet skb->sk = sk; 3647b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3648b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3649b1faf566SEric Dumazet 3650abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3651abb57ea4SEric Dumazet skb_dst_force(skb); 3652abb57ea4SEric Dumazet 3653b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3654b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3655676d2369SDavid S. Miller sk->sk_data_ready(sk); 3656b1faf566SEric Dumazet return 0; 3657b1faf566SEric Dumazet } 3658b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3659b1faf566SEric Dumazet 3660364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3661364a9e93SWillem de Bruijn { 3662364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 3663364a9e93SWillem de Bruijn struct sk_buff *skb, *skb_next; 3664997d5c3fSEric Dumazet unsigned long flags; 3665364a9e93SWillem de Bruijn int err = 0; 3666364a9e93SWillem de Bruijn 3667997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 3668364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 3669364a9e93SWillem de Bruijn if (skb && (skb_next = skb_peek(q))) 3670364a9e93SWillem de Bruijn err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 3671997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 3672364a9e93SWillem de Bruijn 3673364a9e93SWillem de Bruijn sk->sk_err = err; 3674364a9e93SWillem de Bruijn if (err) 3675364a9e93SWillem de Bruijn sk->sk_error_report(sk); 3676364a9e93SWillem de Bruijn 3677364a9e93SWillem de Bruijn return skb; 3678364a9e93SWillem de Bruijn } 3679364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 3680364a9e93SWillem de Bruijn 3681cab41c47SAlexander Duyck /** 3682cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 3683cab41c47SAlexander Duyck * @skb: the skb to clone 3684cab41c47SAlexander Duyck * 3685cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 3686cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 3687cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 3688cab41c47SAlexander Duyck * 3689cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 3690cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 3691cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 3692cab41c47SAlexander Duyck * the sk_error_queue. 3693cab41c47SAlexander Duyck */ 369462bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 369562bccb8cSAlexander Duyck { 369662bccb8cSAlexander Duyck struct sock *sk = skb->sk; 369762bccb8cSAlexander Duyck struct sk_buff *clone; 369862bccb8cSAlexander Duyck 369962bccb8cSAlexander Duyck if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) 370062bccb8cSAlexander Duyck return NULL; 370162bccb8cSAlexander Duyck 370262bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 370362bccb8cSAlexander Duyck if (!clone) { 370462bccb8cSAlexander Duyck sock_put(sk); 370562bccb8cSAlexander Duyck return NULL; 370662bccb8cSAlexander Duyck } 370762bccb8cSAlexander Duyck 370862bccb8cSAlexander Duyck clone->sk = sk; 370962bccb8cSAlexander Duyck clone->destructor = sock_efree; 371062bccb8cSAlexander Duyck 371162bccb8cSAlexander Duyck return clone; 371262bccb8cSAlexander Duyck } 371362bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 371462bccb8cSAlexander Duyck 371537846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 371637846ef0SAlexander Duyck struct sock *sk, 371737846ef0SAlexander Duyck int tstype) 3718ac45f602SPatrick Ohly { 3719ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3720ac45f602SPatrick Ohly int err; 3721ac45f602SPatrick Ohly 3722ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3723ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3724ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3725ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3726e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 37274ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 372809c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 3729ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 3730ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 37314ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 37324ed2d765SWillem de Bruijn } 373329030374SEric Dumazet 3734ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 373529030374SEric Dumazet 3736ac45f602SPatrick Ohly if (err) 3737ac45f602SPatrick Ohly kfree_skb(skb); 3738ac45f602SPatrick Ohly } 373937846ef0SAlexander Duyck 3740b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 3741b245be1fSWillem de Bruijn { 3742b245be1fSWillem de Bruijn bool ret; 3743b245be1fSWillem de Bruijn 3744b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 3745b245be1fSWillem de Bruijn return true; 3746b245be1fSWillem de Bruijn 3747b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 3748b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 3749b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 3750b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 3751b245be1fSWillem de Bruijn return ret; 3752b245be1fSWillem de Bruijn } 3753b245be1fSWillem de Bruijn 375437846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 375537846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 375637846ef0SAlexander Duyck { 375737846ef0SAlexander Duyck struct sock *sk = skb->sk; 375837846ef0SAlexander Duyck 3759b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 3760b245be1fSWillem de Bruijn return; 3761b245be1fSWillem de Bruijn 376262bccb8cSAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 376362bccb8cSAlexander Duyck sock_hold(sk); 376437846ef0SAlexander Duyck 376537846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 376637846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); 376737846ef0SAlexander Duyck 376837846ef0SAlexander Duyck sock_put(sk); 376937846ef0SAlexander Duyck } 377037846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 377137846ef0SAlexander Duyck 377237846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 377337846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 377437846ef0SAlexander Duyck struct sock *sk, int tstype) 377537846ef0SAlexander Duyck { 377637846ef0SAlexander Duyck struct sk_buff *skb; 37773a8dd971SWillem de Bruijn bool tsonly; 377837846ef0SAlexander Duyck 37793a8dd971SWillem de Bruijn if (!sk) 37803a8dd971SWillem de Bruijn return; 37813a8dd971SWillem de Bruijn 37823a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 37833a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 378437846ef0SAlexander Duyck return; 378537846ef0SAlexander Duyck 378649ca0d8bSWillem de Bruijn if (tsonly) 378749ca0d8bSWillem de Bruijn skb = alloc_skb(0, GFP_ATOMIC); 378837846ef0SAlexander Duyck else 378937846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 379037846ef0SAlexander Duyck if (!skb) 379137846ef0SAlexander Duyck return; 379237846ef0SAlexander Duyck 379349ca0d8bSWillem de Bruijn if (tsonly) { 379449ca0d8bSWillem de Bruijn skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags; 379549ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 379649ca0d8bSWillem de Bruijn } 379749ca0d8bSWillem de Bruijn 379849ca0d8bSWillem de Bruijn if (hwtstamps) 379949ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 380049ca0d8bSWillem de Bruijn else 380149ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 380249ca0d8bSWillem de Bruijn 380337846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, tstype); 380437846ef0SAlexander Duyck } 3805e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3806e7fd2885SWillem de Bruijn 3807e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 3808e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 3809e7fd2885SWillem de Bruijn { 3810e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3811e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 3812e7fd2885SWillem de Bruijn } 3813ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3814ac45f602SPatrick Ohly 38156e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 38166e3e939fSJohannes Berg { 38176e3e939fSJohannes Berg struct sock *sk = skb->sk; 38186e3e939fSJohannes Berg struct sock_exterr_skb *serr; 38196e3e939fSJohannes Berg int err; 38206e3e939fSJohannes Berg 38216e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 38226e3e939fSJohannes Berg skb->wifi_acked = acked; 38236e3e939fSJohannes Berg 38246e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 38256e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 38266e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 38276e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 38286e3e939fSJohannes Berg 3829bf7fa551SAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 3830bf7fa551SAlexander Duyck sock_hold(sk); 3831bf7fa551SAlexander Duyck 38326e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 38336e3e939fSJohannes Berg if (err) 38346e3e939fSJohannes Berg kfree_skb(skb); 3835bf7fa551SAlexander Duyck 3836bf7fa551SAlexander Duyck sock_put(sk); 38376e3e939fSJohannes Berg } 38386e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 38396e3e939fSJohannes Berg 3840f35d9d8aSRusty Russell /** 3841f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3842f35d9d8aSRusty Russell * @skb: the skb to set 3843f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3844f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3845f35d9d8aSRusty Russell * 3846f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3847f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3848f35d9d8aSRusty Russell * 3849f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3850f35d9d8aSRusty Russell * returns false you should drop the packet. 3851f35d9d8aSRusty Russell */ 3852f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3853f35d9d8aSRusty Russell { 38545ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 38555ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3856e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 38575ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3858f35d9d8aSRusty Russell return false; 3859f35d9d8aSRusty Russell } 3860f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3861f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3862f35d9d8aSRusty Russell skb->csum_offset = off; 3863e5d5decaSJason Wang skb_set_transport_header(skb, start); 3864f35d9d8aSRusty Russell return true; 3865f35d9d8aSRusty Russell } 3866b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3867f35d9d8aSRusty Russell 3868ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 3869ed1f50c3SPaul Durrant unsigned int max) 3870ed1f50c3SPaul Durrant { 3871ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 3872ed1f50c3SPaul Durrant return 0; 3873ed1f50c3SPaul Durrant 3874ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 3875ed1f50c3SPaul Durrant * won't need to do it again. 3876ed1f50c3SPaul Durrant */ 3877ed1f50c3SPaul Durrant if (max > skb->len) 3878ed1f50c3SPaul Durrant max = skb->len; 3879ed1f50c3SPaul Durrant 3880ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 3881ed1f50c3SPaul Durrant return -ENOMEM; 3882ed1f50c3SPaul Durrant 3883ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 3884ed1f50c3SPaul Durrant return -EPROTO; 3885ed1f50c3SPaul Durrant 3886ed1f50c3SPaul Durrant return 0; 3887ed1f50c3SPaul Durrant } 3888ed1f50c3SPaul Durrant 3889f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 3890f9708b43SJan Beulich 3891f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 3892f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 3893f9708b43SJan Beulich unsigned int off) 3894f9708b43SJan Beulich { 3895f9708b43SJan Beulich switch (proto) { 3896f9708b43SJan Beulich int err; 3897f9708b43SJan Beulich 3898f9708b43SJan Beulich case IPPROTO_TCP: 3899f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 3900f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 3901f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3902f9708b43SJan Beulich offsetof(struct tcphdr, 3903f9708b43SJan Beulich check))) 3904f9708b43SJan Beulich err = -EPROTO; 3905f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 3906f9708b43SJan Beulich 3907f9708b43SJan Beulich case IPPROTO_UDP: 3908f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 3909f9708b43SJan Beulich off + sizeof(struct udphdr)); 3910f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3911f9708b43SJan Beulich offsetof(struct udphdr, 3912f9708b43SJan Beulich check))) 3913f9708b43SJan Beulich err = -EPROTO; 3914f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 3915f9708b43SJan Beulich } 3916f9708b43SJan Beulich 3917f9708b43SJan Beulich return ERR_PTR(-EPROTO); 3918f9708b43SJan Beulich } 3919f9708b43SJan Beulich 3920ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3921ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 3922ed1f50c3SPaul Durrant */ 3923ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 3924ed1f50c3SPaul Durrant 3925f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 3926ed1f50c3SPaul Durrant { 3927ed1f50c3SPaul Durrant unsigned int off; 3928ed1f50c3SPaul Durrant bool fragment; 3929f9708b43SJan Beulich __sum16 *csum; 3930ed1f50c3SPaul Durrant int err; 3931ed1f50c3SPaul Durrant 3932ed1f50c3SPaul Durrant fragment = false; 3933ed1f50c3SPaul Durrant 3934ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3935ed1f50c3SPaul Durrant sizeof(struct iphdr), 3936ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 3937ed1f50c3SPaul Durrant if (err < 0) 3938ed1f50c3SPaul Durrant goto out; 3939ed1f50c3SPaul Durrant 3940ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 3941ed1f50c3SPaul Durrant fragment = true; 3942ed1f50c3SPaul Durrant 3943ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 3944ed1f50c3SPaul Durrant 3945ed1f50c3SPaul Durrant err = -EPROTO; 3946ed1f50c3SPaul Durrant 3947ed1f50c3SPaul Durrant if (fragment) 3948ed1f50c3SPaul Durrant goto out; 3949ed1f50c3SPaul Durrant 3950f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 3951f9708b43SJan Beulich if (IS_ERR(csum)) 3952f9708b43SJan Beulich return PTR_ERR(csum); 3953ed1f50c3SPaul Durrant 3954ed1f50c3SPaul Durrant if (recalculate) 3955f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 3956ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 3957ed1f50c3SPaul Durrant skb->len - off, 3958f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 3959ed1f50c3SPaul Durrant err = 0; 3960ed1f50c3SPaul Durrant 3961ed1f50c3SPaul Durrant out: 3962ed1f50c3SPaul Durrant return err; 3963ed1f50c3SPaul Durrant } 3964ed1f50c3SPaul Durrant 3965ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3966ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 3967ed1f50c3SPaul Durrant */ 3968ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 3969ed1f50c3SPaul Durrant 3970ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 3971ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 3972ed1f50c3SPaul Durrant 3973ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 3974ed1f50c3SPaul Durrant { 3975ed1f50c3SPaul Durrant int err; 3976ed1f50c3SPaul Durrant u8 nexthdr; 3977ed1f50c3SPaul Durrant unsigned int off; 3978ed1f50c3SPaul Durrant unsigned int len; 3979ed1f50c3SPaul Durrant bool fragment; 3980ed1f50c3SPaul Durrant bool done; 3981f9708b43SJan Beulich __sum16 *csum; 3982ed1f50c3SPaul Durrant 3983ed1f50c3SPaul Durrant fragment = false; 3984ed1f50c3SPaul Durrant done = false; 3985ed1f50c3SPaul Durrant 3986ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 3987ed1f50c3SPaul Durrant 3988ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 3989ed1f50c3SPaul Durrant if (err < 0) 3990ed1f50c3SPaul Durrant goto out; 3991ed1f50c3SPaul Durrant 3992ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 3993ed1f50c3SPaul Durrant 3994ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 3995ed1f50c3SPaul Durrant while (off <= len && !done) { 3996ed1f50c3SPaul Durrant switch (nexthdr) { 3997ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 3998ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 3999ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 4000ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 4001ed1f50c3SPaul Durrant 4002ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4003ed1f50c3SPaul Durrant off + 4004ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 4005ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4006ed1f50c3SPaul Durrant if (err < 0) 4007ed1f50c3SPaul Durrant goto out; 4008ed1f50c3SPaul Durrant 4009ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 4010ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4011ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 4012ed1f50c3SPaul Durrant break; 4013ed1f50c3SPaul Durrant } 4014ed1f50c3SPaul Durrant case IPPROTO_AH: { 4015ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 4016ed1f50c3SPaul Durrant 4017ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4018ed1f50c3SPaul Durrant off + 4019ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 4020ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4021ed1f50c3SPaul Durrant if (err < 0) 4022ed1f50c3SPaul Durrant goto out; 4023ed1f50c3SPaul Durrant 4024ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 4025ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4026ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 4027ed1f50c3SPaul Durrant break; 4028ed1f50c3SPaul Durrant } 4029ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 4030ed1f50c3SPaul Durrant struct frag_hdr *hp; 4031ed1f50c3SPaul Durrant 4032ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 4033ed1f50c3SPaul Durrant off + 4034ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 4035ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 4036ed1f50c3SPaul Durrant if (err < 0) 4037ed1f50c3SPaul Durrant goto out; 4038ed1f50c3SPaul Durrant 4039ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 4040ed1f50c3SPaul Durrant 4041ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 4042ed1f50c3SPaul Durrant fragment = true; 4043ed1f50c3SPaul Durrant 4044ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 4045ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 4046ed1f50c3SPaul Durrant break; 4047ed1f50c3SPaul Durrant } 4048ed1f50c3SPaul Durrant default: 4049ed1f50c3SPaul Durrant done = true; 4050ed1f50c3SPaul Durrant break; 4051ed1f50c3SPaul Durrant } 4052ed1f50c3SPaul Durrant } 4053ed1f50c3SPaul Durrant 4054ed1f50c3SPaul Durrant err = -EPROTO; 4055ed1f50c3SPaul Durrant 4056ed1f50c3SPaul Durrant if (!done || fragment) 4057ed1f50c3SPaul Durrant goto out; 4058ed1f50c3SPaul Durrant 4059f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 4060f9708b43SJan Beulich if (IS_ERR(csum)) 4061f9708b43SJan Beulich return PTR_ERR(csum); 4062ed1f50c3SPaul Durrant 4063ed1f50c3SPaul Durrant if (recalculate) 4064f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 4065ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 4066f9708b43SJan Beulich skb->len - off, nexthdr, 0); 4067ed1f50c3SPaul Durrant err = 0; 4068ed1f50c3SPaul Durrant 4069ed1f50c3SPaul Durrant out: 4070ed1f50c3SPaul Durrant return err; 4071ed1f50c3SPaul Durrant } 4072ed1f50c3SPaul Durrant 4073ed1f50c3SPaul Durrant /** 4074ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 4075ed1f50c3SPaul Durrant * @skb: the skb to set up 4076ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 4077ed1f50c3SPaul Durrant */ 4078ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4079ed1f50c3SPaul Durrant { 4080ed1f50c3SPaul Durrant int err; 4081ed1f50c3SPaul Durrant 4082ed1f50c3SPaul Durrant switch (skb->protocol) { 4083ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4084f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4085ed1f50c3SPaul Durrant break; 4086ed1f50c3SPaul Durrant 4087ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4088ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4089ed1f50c3SPaul Durrant break; 4090ed1f50c3SPaul Durrant 4091ed1f50c3SPaul Durrant default: 4092ed1f50c3SPaul Durrant err = -EPROTO; 4093ed1f50c3SPaul Durrant break; 4094ed1f50c3SPaul Durrant } 4095ed1f50c3SPaul Durrant 4096ed1f50c3SPaul Durrant return err; 4097ed1f50c3SPaul Durrant } 4098ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4099ed1f50c3SPaul Durrant 41009afd85c9SLinus Lüssing /** 41019afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 41029afd85c9SLinus Lüssing * @skb: the skb to check 41039afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 41049afd85c9SLinus Lüssing * 41059afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 41069afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 41079afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 41089afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 41099afd85c9SLinus Lüssing * 4110a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4111a516993fSLinus Lüssing * differs from the provided skb. 41129afd85c9SLinus Lüssing */ 41139afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 41149afd85c9SLinus Lüssing unsigned int transport_len) 41159afd85c9SLinus Lüssing { 41169afd85c9SLinus Lüssing struct sk_buff *skb_chk; 41179afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 41189afd85c9SLinus Lüssing int ret; 41199afd85c9SLinus Lüssing 4120a516993fSLinus Lüssing if (skb->len < len) 41219afd85c9SLinus Lüssing return NULL; 4122a516993fSLinus Lüssing else if (skb->len == len) 41239afd85c9SLinus Lüssing return skb; 41249afd85c9SLinus Lüssing 41259afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 41269afd85c9SLinus Lüssing if (!skb_chk) 41279afd85c9SLinus Lüssing return NULL; 41289afd85c9SLinus Lüssing 41299afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 41309afd85c9SLinus Lüssing if (ret) { 41319afd85c9SLinus Lüssing kfree_skb(skb_chk); 41329afd85c9SLinus Lüssing return NULL; 41339afd85c9SLinus Lüssing } 41349afd85c9SLinus Lüssing 41359afd85c9SLinus Lüssing return skb_chk; 41369afd85c9SLinus Lüssing } 41379afd85c9SLinus Lüssing 41389afd85c9SLinus Lüssing /** 41399afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 41409afd85c9SLinus Lüssing * @skb: the skb to check 41419afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 41429afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 41439afd85c9SLinus Lüssing * 41449afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 41459afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 41469afd85c9SLinus Lüssing * 41479afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 41489afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 41499afd85c9SLinus Lüssing * 4150a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4151a516993fSLinus Lüssing * differs from the provided skb. 41529afd85c9SLinus Lüssing */ 41539afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 41549afd85c9SLinus Lüssing unsigned int transport_len, 41559afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 41569afd85c9SLinus Lüssing { 41579afd85c9SLinus Lüssing struct sk_buff *skb_chk; 41589afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 4159fcba67c9SLinus Lüssing __sum16 ret; 41609afd85c9SLinus Lüssing 41619afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 41629afd85c9SLinus Lüssing if (!skb_chk) 4163a516993fSLinus Lüssing goto err; 41649afd85c9SLinus Lüssing 4165a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 4166a516993fSLinus Lüssing goto err; 41679afd85c9SLinus Lüssing 41689afd85c9SLinus Lüssing __skb_pull(skb_chk, offset); 41699afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 41709afd85c9SLinus Lüssing __skb_push(skb_chk, offset); 41719afd85c9SLinus Lüssing 4172a516993fSLinus Lüssing if (ret) 4173a516993fSLinus Lüssing goto err; 41749afd85c9SLinus Lüssing 41759afd85c9SLinus Lüssing return skb_chk; 4176a516993fSLinus Lüssing 4177a516993fSLinus Lüssing err: 4178a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 4179a516993fSLinus Lüssing kfree_skb(skb_chk); 4180a516993fSLinus Lüssing 4181a516993fSLinus Lüssing return NULL; 4182a516993fSLinus Lüssing 41839afd85c9SLinus Lüssing } 41849afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 41859afd85c9SLinus Lüssing 41864497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 41874497b076SBen Hutchings { 4188e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4189e87cc472SJoe Perches skb->dev->name); 41904497b076SBen Hutchings } 41914497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4192bad43ca8SEric Dumazet 4193bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4194bad43ca8SEric Dumazet { 41953d861f66SEric Dumazet if (head_stolen) { 41963d861f66SEric Dumazet skb_release_head_state(skb); 4197bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 41983d861f66SEric Dumazet } else { 4199bad43ca8SEric Dumazet __kfree_skb(skb); 4200bad43ca8SEric Dumazet } 42013d861f66SEric Dumazet } 4202bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4203bad43ca8SEric Dumazet 4204bad43ca8SEric Dumazet /** 4205bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4206bad43ca8SEric Dumazet * @to: prior buffer 4207bad43ca8SEric Dumazet * @from: buffer to add 4208bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4209c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4210bad43ca8SEric Dumazet */ 4211bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4212bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4213bad43ca8SEric Dumazet { 4214bad43ca8SEric Dumazet int i, delta, len = from->len; 4215bad43ca8SEric Dumazet 4216bad43ca8SEric Dumazet *fragstolen = false; 4217bad43ca8SEric Dumazet 4218bad43ca8SEric Dumazet if (skb_cloned(to)) 4219bad43ca8SEric Dumazet return false; 4220bad43ca8SEric Dumazet 4221bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4222e93a0435SEric Dumazet if (len) 4223bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4224bad43ca8SEric Dumazet *delta_truesize = 0; 4225bad43ca8SEric Dumazet return true; 4226bad43ca8SEric Dumazet } 4227bad43ca8SEric Dumazet 4228bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 4229bad43ca8SEric Dumazet return false; 4230bad43ca8SEric Dumazet 4231bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4232bad43ca8SEric Dumazet struct page *page; 4233bad43ca8SEric Dumazet unsigned int offset; 4234bad43ca8SEric Dumazet 4235bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4236bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 4237bad43ca8SEric Dumazet return false; 4238bad43ca8SEric Dumazet 4239bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4240bad43ca8SEric Dumazet return false; 4241bad43ca8SEric Dumazet 4242bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4243bad43ca8SEric Dumazet 4244bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4245bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4246bad43ca8SEric Dumazet 4247bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 4248bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4249bad43ca8SEric Dumazet *fragstolen = true; 4250bad43ca8SEric Dumazet } else { 4251bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4252bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4253bad43ca8SEric Dumazet return false; 4254bad43ca8SEric Dumazet 4255f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4256bad43ca8SEric Dumazet } 4257bad43ca8SEric Dumazet 4258bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4259bad43ca8SEric Dumazet 4260bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4261bad43ca8SEric Dumazet skb_shinfo(from)->frags, 4262bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4263bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4264bad43ca8SEric Dumazet 4265bad43ca8SEric Dumazet if (!skb_cloned(from)) 4266bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 4267bad43ca8SEric Dumazet 42688ea853fdSLi RongQing /* if the skb is not cloned this does nothing 42698ea853fdSLi RongQing * since we set nr_frags to 0. 42708ea853fdSLi RongQing */ 4271bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4272bad43ca8SEric Dumazet skb_frag_ref(from, i); 4273bad43ca8SEric Dumazet 4274bad43ca8SEric Dumazet to->truesize += delta; 4275bad43ca8SEric Dumazet to->len += len; 4276bad43ca8SEric Dumazet to->data_len += len; 4277bad43ca8SEric Dumazet 4278bad43ca8SEric Dumazet *delta_truesize = delta; 4279bad43ca8SEric Dumazet return true; 4280bad43ca8SEric Dumazet } 4281bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4282621e84d6SNicolas Dichtel 4283621e84d6SNicolas Dichtel /** 42848b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4285621e84d6SNicolas Dichtel * 4286621e84d6SNicolas Dichtel * @skb: buffer to clean 42878b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4288621e84d6SNicolas Dichtel * 42898b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 42908b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 42918b27f277SNicolas Dichtel * operations. 42928b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 42938b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 42948b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4295621e84d6SNicolas Dichtel */ 42968b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4297621e84d6SNicolas Dichtel { 4298621e84d6SNicolas Dichtel skb->tstamp.tv64 = 0; 4299621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4300621e84d6SNicolas Dichtel skb->skb_iif = 0; 430160ff7467SWANG Cong skb->ignore_df = 0; 4302621e84d6SNicolas Dichtel skb_dst_drop(skb); 4303c29390c6SEric Dumazet skb_sender_cpu_clear(skb); 4304621e84d6SNicolas Dichtel secpath_reset(skb); 4305621e84d6SNicolas Dichtel nf_reset(skb); 4306621e84d6SNicolas Dichtel nf_reset_trace(skb); 4307213dd74aSHerbert Xu 4308213dd74aSHerbert Xu if (!xnet) 4309213dd74aSHerbert Xu return; 4310213dd74aSHerbert Xu 4311213dd74aSHerbert Xu skb_orphan(skb); 4312213dd74aSHerbert Xu skb->mark = 0; 4313621e84d6SNicolas Dichtel } 4314621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4315de960aa9SFlorian Westphal 4316de960aa9SFlorian Westphal /** 4317de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4318de960aa9SFlorian Westphal * 4319de960aa9SFlorian Westphal * @skb: GSO skb 4320de960aa9SFlorian Westphal * 4321de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4322de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4323de960aa9SFlorian Westphal * 4324de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4325de960aa9SFlorian Westphal */ 4326de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4327de960aa9SFlorian Westphal { 4328de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4329f993bc25SFlorian Westphal unsigned int thlen = 0; 4330f993bc25SFlorian Westphal 4331f993bc25SFlorian Westphal if (skb->encapsulation) { 4332f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4333f993bc25SFlorian Westphal skb_transport_header(skb); 4334de960aa9SFlorian Westphal 4335de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4336f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4337f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4338f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 4339f993bc25SFlorian Westphal } 43406d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 43416d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 43426d39d589SFlorian Westphal * accounted for. 43436d39d589SFlorian Westphal */ 4344f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4345de960aa9SFlorian Westphal } 4346de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 43470d5501c1SVlad Yasevich 43480d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 43490d5501c1SVlad Yasevich { 43500d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 43510d5501c1SVlad Yasevich kfree_skb(skb); 43520d5501c1SVlad Yasevich return NULL; 43530d5501c1SVlad Yasevich } 43540d5501c1SVlad Yasevich 4355f6548615SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN, 4356a6e18ff1SVlad Yasevich 2 * ETH_ALEN); 43570d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 43580d5501c1SVlad Yasevich return skb; 43590d5501c1SVlad Yasevich } 43600d5501c1SVlad Yasevich 43610d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 43620d5501c1SVlad Yasevich { 43630d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 43640d5501c1SVlad Yasevich u16 vlan_tci; 43650d5501c1SVlad Yasevich 4366df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 43670d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 43680d5501c1SVlad Yasevich return skb; 43690d5501c1SVlad Yasevich } 43700d5501c1SVlad Yasevich 43710d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 43720d5501c1SVlad Yasevich if (unlikely(!skb)) 43730d5501c1SVlad Yasevich goto err_free; 43740d5501c1SVlad Yasevich 43750d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 43760d5501c1SVlad Yasevich goto err_free; 43770d5501c1SVlad Yasevich 43780d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 43790d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 43800d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 43810d5501c1SVlad Yasevich 43820d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 43830d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 43840d5501c1SVlad Yasevich 43850d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 43860d5501c1SVlad Yasevich if (unlikely(!skb)) 43870d5501c1SVlad Yasevich goto err_free; 43880d5501c1SVlad Yasevich 43890d5501c1SVlad Yasevich skb_reset_network_header(skb); 43900d5501c1SVlad Yasevich skb_reset_transport_header(skb); 43910d5501c1SVlad Yasevich skb_reset_mac_len(skb); 43920d5501c1SVlad Yasevich 43930d5501c1SVlad Yasevich return skb; 43940d5501c1SVlad Yasevich 43950d5501c1SVlad Yasevich err_free: 43960d5501c1SVlad Yasevich kfree_skb(skb); 43970d5501c1SVlad Yasevich return NULL; 43980d5501c1SVlad Yasevich } 43990d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 44002e4e4410SEric Dumazet 4401e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 4402e2195121SJiri Pirko { 4403e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 4404e2195121SJiri Pirko return -ENOMEM; 4405e2195121SJiri Pirko 4406e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4407e2195121SJiri Pirko return 0; 4408e2195121SJiri Pirko 4409e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 4410e2195121SJiri Pirko } 4411e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 4412e2195121SJiri Pirko 441393515d53SJiri Pirko /* remove VLAN header from packet and update csum accordingly. */ 441493515d53SJiri Pirko static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 441593515d53SJiri Pirko { 441693515d53SJiri Pirko struct vlan_hdr *vhdr; 441793515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 441893515d53SJiri Pirko int err; 441993515d53SJiri Pirko 442093515d53SJiri Pirko __skb_push(skb, offset); 442193515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 442293515d53SJiri Pirko if (unlikely(err)) 442393515d53SJiri Pirko goto pull; 442493515d53SJiri Pirko 442593515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 442693515d53SJiri Pirko 442793515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 442893515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 442993515d53SJiri Pirko 443093515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 443193515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 443293515d53SJiri Pirko 443393515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 443493515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 443593515d53SJiri Pirko 443693515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 443793515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 443893515d53SJiri Pirko 443993515d53SJiri Pirko skb_reset_mac_len(skb); 444093515d53SJiri Pirko pull: 444193515d53SJiri Pirko __skb_pull(skb, offset); 444293515d53SJiri Pirko 444393515d53SJiri Pirko return err; 444493515d53SJiri Pirko } 444593515d53SJiri Pirko 444693515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 444793515d53SJiri Pirko { 444893515d53SJiri Pirko u16 vlan_tci; 444993515d53SJiri Pirko __be16 vlan_proto; 445093515d53SJiri Pirko int err; 445193515d53SJiri Pirko 4452df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 445393515d53SJiri Pirko skb->vlan_tci = 0; 445493515d53SJiri Pirko } else { 445593515d53SJiri Pirko if (unlikely((skb->protocol != htons(ETH_P_8021Q) && 445693515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 445793515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 445893515d53SJiri Pirko return 0; 445993515d53SJiri Pirko 446093515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 446193515d53SJiri Pirko if (err) 446293515d53SJiri Pirko return err; 446393515d53SJiri Pirko } 446493515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 446593515d53SJiri Pirko if (likely((skb->protocol != htons(ETH_P_8021Q) && 446693515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 446793515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 446893515d53SJiri Pirko return 0; 446993515d53SJiri Pirko 447093515d53SJiri Pirko vlan_proto = skb->protocol; 447193515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 447293515d53SJiri Pirko if (unlikely(err)) 447393515d53SJiri Pirko return err; 447493515d53SJiri Pirko 447593515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 447693515d53SJiri Pirko return 0; 447793515d53SJiri Pirko } 447893515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 447993515d53SJiri Pirko 448093515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 448193515d53SJiri Pirko { 4482df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 448393515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 448493515d53SJiri Pirko int err; 448593515d53SJiri Pirko 448693515d53SJiri Pirko /* __vlan_insert_tag expect skb->data pointing to mac header. 448793515d53SJiri Pirko * So change skb->data before calling it and change back to 448893515d53SJiri Pirko * original position later 448993515d53SJiri Pirko */ 449093515d53SJiri Pirko __skb_push(skb, offset); 449193515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 4492df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 449393515d53SJiri Pirko if (err) 449493515d53SJiri Pirko return err; 449593515d53SJiri Pirko skb->protocol = skb->vlan_proto; 449693515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 449793515d53SJiri Pirko __skb_pull(skb, offset); 449893515d53SJiri Pirko 44996b83d28aSDaniel Borkmann skb_postpush_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 450093515d53SJiri Pirko } 450193515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 450293515d53SJiri Pirko return 0; 450393515d53SJiri Pirko } 450493515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 450593515d53SJiri Pirko 45062e4e4410SEric Dumazet /** 45072e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 45082e4e4410SEric Dumazet * 4509de3f0d0eSMasanari Iida * @header_len: size of linear part 4510de3f0d0eSMasanari Iida * @data_len: needed length in frags 4511de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 4512de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 4513de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 45142e4e4410SEric Dumazet * 45152e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 45162e4e4410SEric Dumazet */ 45172e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 45182e4e4410SEric Dumazet unsigned long data_len, 45192e4e4410SEric Dumazet int max_page_order, 45202e4e4410SEric Dumazet int *errcode, 45212e4e4410SEric Dumazet gfp_t gfp_mask) 45222e4e4410SEric Dumazet { 45232e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 45242e4e4410SEric Dumazet unsigned long chunk; 45252e4e4410SEric Dumazet struct sk_buff *skb; 45262e4e4410SEric Dumazet struct page *page; 45272e4e4410SEric Dumazet gfp_t gfp_head; 45282e4e4410SEric Dumazet int i; 45292e4e4410SEric Dumazet 45302e4e4410SEric Dumazet *errcode = -EMSGSIZE; 45312e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 45322e4e4410SEric Dumazet * high order pages... 45332e4e4410SEric Dumazet */ 45342e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 45352e4e4410SEric Dumazet return NULL; 45362e4e4410SEric Dumazet 45372e4e4410SEric Dumazet gfp_head = gfp_mask; 4538d0164adcSMel Gorman if (gfp_head & __GFP_DIRECT_RECLAIM) 45392e4e4410SEric Dumazet gfp_head |= __GFP_REPEAT; 45402e4e4410SEric Dumazet 45412e4e4410SEric Dumazet *errcode = -ENOBUFS; 45422e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 45432e4e4410SEric Dumazet if (!skb) 45442e4e4410SEric Dumazet return NULL; 45452e4e4410SEric Dumazet 45462e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 45472e4e4410SEric Dumazet 45482e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 45492e4e4410SEric Dumazet int order = max_page_order; 45502e4e4410SEric Dumazet 45512e4e4410SEric Dumazet while (order) { 45522e4e4410SEric Dumazet if (npages >= 1 << order) { 4553d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 45542e4e4410SEric Dumazet __GFP_COMP | 45552e4e4410SEric Dumazet __GFP_NOWARN | 45562e4e4410SEric Dumazet __GFP_NORETRY, 45572e4e4410SEric Dumazet order); 45582e4e4410SEric Dumazet if (page) 45592e4e4410SEric Dumazet goto fill_page; 45602e4e4410SEric Dumazet /* Do not retry other high order allocations */ 45612e4e4410SEric Dumazet order = 1; 45622e4e4410SEric Dumazet max_page_order = 0; 45632e4e4410SEric Dumazet } 45642e4e4410SEric Dumazet order--; 45652e4e4410SEric Dumazet } 45662e4e4410SEric Dumazet page = alloc_page(gfp_mask); 45672e4e4410SEric Dumazet if (!page) 45682e4e4410SEric Dumazet goto failure; 45692e4e4410SEric Dumazet fill_page: 45702e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 45712e4e4410SEric Dumazet PAGE_SIZE << order); 45722e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 45732e4e4410SEric Dumazet data_len -= chunk; 45742e4e4410SEric Dumazet npages -= 1 << order; 45752e4e4410SEric Dumazet } 45762e4e4410SEric Dumazet return skb; 45772e4e4410SEric Dumazet 45782e4e4410SEric Dumazet failure: 45792e4e4410SEric Dumazet kfree_skb(skb); 45802e4e4410SEric Dumazet return NULL; 45812e4e4410SEric Dumazet } 45822e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 4583