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 350b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, netdev_alloc_cache); 351b63ae8caSAlexander Duyck static DEFINE_PER_CPU(struct page_frag_cache, napi_alloc_cache); 352ffde7328SAlexander Duyck 353ffde7328SAlexander Duyck static void *__netdev_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 354ffde7328SAlexander Duyck { 355b63ae8caSAlexander Duyck struct page_frag_cache *nc; 356ffde7328SAlexander Duyck unsigned long flags; 357ffde7328SAlexander Duyck void *data; 358ffde7328SAlexander Duyck 359ffde7328SAlexander Duyck local_irq_save(flags); 3609451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 3619451980aSAlexander Duyck data = __alloc_page_frag(nc, fragsz, gfp_mask); 3626f532612SEric Dumazet local_irq_restore(flags); 3636f532612SEric Dumazet return data; 3646f532612SEric Dumazet } 365c93bdd0eSMel Gorman 366c93bdd0eSMel Gorman /** 367c93bdd0eSMel Gorman * netdev_alloc_frag - allocate a page fragment 368c93bdd0eSMel Gorman * @fragsz: fragment size 369c93bdd0eSMel Gorman * 370c93bdd0eSMel Gorman * Allocates a frag from a page for receive buffer. 371c93bdd0eSMel Gorman * Uses GFP_ATOMIC allocations. 372c93bdd0eSMel Gorman */ 373c93bdd0eSMel Gorman void *netdev_alloc_frag(unsigned int fragsz) 374c93bdd0eSMel Gorman { 375c93bdd0eSMel Gorman return __netdev_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 376c93bdd0eSMel Gorman } 3776f532612SEric Dumazet EXPORT_SYMBOL(netdev_alloc_frag); 3786f532612SEric Dumazet 379ffde7328SAlexander Duyck static void *__napi_alloc_frag(unsigned int fragsz, gfp_t gfp_mask) 380ffde7328SAlexander Duyck { 381b63ae8caSAlexander Duyck struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache); 3829451980aSAlexander Duyck 3839451980aSAlexander Duyck return __alloc_page_frag(nc, fragsz, gfp_mask); 384ffde7328SAlexander Duyck } 385ffde7328SAlexander Duyck 386ffde7328SAlexander Duyck void *napi_alloc_frag(unsigned int fragsz) 387ffde7328SAlexander Duyck { 388ffde7328SAlexander Duyck return __napi_alloc_frag(fragsz, GFP_ATOMIC | __GFP_COLD); 389ffde7328SAlexander Duyck } 390ffde7328SAlexander Duyck EXPORT_SYMBOL(napi_alloc_frag); 391ffde7328SAlexander Duyck 3926f532612SEric Dumazet /** 393fd11a83dSAlexander Duyck * __netdev_alloc_skb - allocate an skbuff for rx on a specific device 394fd11a83dSAlexander Duyck * @dev: network device to receive on 395d7499160SMasanari Iida * @len: length to allocate 396fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb 397fd11a83dSAlexander Duyck * 398fd11a83dSAlexander Duyck * Allocate a new &sk_buff and assign it a usage count of one. The 399fd11a83dSAlexander Duyck * buffer has NET_SKB_PAD headroom built in. Users should allocate 400fd11a83dSAlexander Duyck * the headroom they think they need without accounting for the 401fd11a83dSAlexander Duyck * built in space. The built in space is used for optimisations. 402fd11a83dSAlexander Duyck * 403fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 404fd11a83dSAlexander Duyck */ 4059451980aSAlexander Duyck struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int len, 4069451980aSAlexander Duyck gfp_t gfp_mask) 407fd11a83dSAlexander Duyck { 408b63ae8caSAlexander Duyck struct page_frag_cache *nc; 4099451980aSAlexander Duyck unsigned long flags; 410fd11a83dSAlexander Duyck struct sk_buff *skb; 4119451980aSAlexander Duyck bool pfmemalloc; 4129451980aSAlexander Duyck void *data; 413fd11a83dSAlexander Duyck 4149451980aSAlexander Duyck len += NET_SKB_PAD; 415fd11a83dSAlexander Duyck 4169451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 417d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 418a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 419a080e7bdSAlexander Duyck if (!skb) 420a080e7bdSAlexander Duyck goto skb_fail; 421a080e7bdSAlexander Duyck goto skb_success; 422a080e7bdSAlexander Duyck } 4239451980aSAlexander Duyck 4249451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4259451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4269451980aSAlexander Duyck 4279451980aSAlexander Duyck if (sk_memalloc_socks()) 4289451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4299451980aSAlexander Duyck 4309451980aSAlexander Duyck local_irq_save(flags); 4319451980aSAlexander Duyck 4329451980aSAlexander Duyck nc = this_cpu_ptr(&netdev_alloc_cache); 4339451980aSAlexander Duyck data = __alloc_page_frag(nc, len, gfp_mask); 4349451980aSAlexander Duyck pfmemalloc = nc->pfmemalloc; 4359451980aSAlexander Duyck 4369451980aSAlexander Duyck local_irq_restore(flags); 4379451980aSAlexander Duyck 4389451980aSAlexander Duyck if (unlikely(!data)) 4399451980aSAlexander Duyck return NULL; 4409451980aSAlexander Duyck 4419451980aSAlexander Duyck skb = __build_skb(data, len); 4429451980aSAlexander Duyck if (unlikely(!skb)) { 443181edb2bSAlexander Duyck skb_free_frag(data); 4449451980aSAlexander Duyck return NULL; 4459451980aSAlexander Duyck } 4469451980aSAlexander Duyck 4479451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 4489451980aSAlexander Duyck if (pfmemalloc) 4499451980aSAlexander Duyck skb->pfmemalloc = 1; 4509451980aSAlexander Duyck skb->head_frag = 1; 4519451980aSAlexander Duyck 452a080e7bdSAlexander Duyck skb_success: 4538af27456SChristoph Hellwig skb_reserve(skb, NET_SKB_PAD); 4547b2e497aSChristoph Hellwig skb->dev = dev; 455fd11a83dSAlexander Duyck 456a080e7bdSAlexander Duyck skb_fail: 4578af27456SChristoph Hellwig return skb; 4588af27456SChristoph Hellwig } 459b4ac530fSDavid S. Miller EXPORT_SYMBOL(__netdev_alloc_skb); 4601da177e4SLinus Torvalds 461fd11a83dSAlexander Duyck /** 462fd11a83dSAlexander Duyck * __napi_alloc_skb - allocate skbuff for rx in a specific NAPI instance 463fd11a83dSAlexander Duyck * @napi: napi instance this buffer was allocated for 464d7499160SMasanari Iida * @len: length to allocate 465fd11a83dSAlexander Duyck * @gfp_mask: get_free_pages mask, passed to alloc_skb and alloc_pages 466fd11a83dSAlexander Duyck * 467fd11a83dSAlexander Duyck * Allocate a new sk_buff for use in NAPI receive. This buffer will 468fd11a83dSAlexander Duyck * attempt to allocate the head from a special reserved region used 469fd11a83dSAlexander Duyck * only for NAPI Rx allocation. By doing this we can save several 470fd11a83dSAlexander Duyck * CPU cycles by avoiding having to disable and re-enable IRQs. 471fd11a83dSAlexander Duyck * 472fd11a83dSAlexander Duyck * %NULL is returned if there is no free memory. 473fd11a83dSAlexander Duyck */ 4749451980aSAlexander Duyck struct sk_buff *__napi_alloc_skb(struct napi_struct *napi, unsigned int len, 4759451980aSAlexander Duyck gfp_t gfp_mask) 476fd11a83dSAlexander Duyck { 477b63ae8caSAlexander Duyck struct page_frag_cache *nc = this_cpu_ptr(&napi_alloc_cache); 478fd11a83dSAlexander Duyck struct sk_buff *skb; 4799451980aSAlexander Duyck void *data; 480fd11a83dSAlexander Duyck 4819451980aSAlexander Duyck len += NET_SKB_PAD + NET_IP_ALIGN; 482fd11a83dSAlexander Duyck 4839451980aSAlexander Duyck if ((len > SKB_WITH_OVERHEAD(PAGE_SIZE)) || 484d0164adcSMel Gorman (gfp_mask & (__GFP_DIRECT_RECLAIM | GFP_DMA))) { 485a080e7bdSAlexander Duyck skb = __alloc_skb(len, gfp_mask, SKB_ALLOC_RX, NUMA_NO_NODE); 486a080e7bdSAlexander Duyck if (!skb) 487a080e7bdSAlexander Duyck goto skb_fail; 488a080e7bdSAlexander Duyck goto skb_success; 489a080e7bdSAlexander Duyck } 4909451980aSAlexander Duyck 4919451980aSAlexander Duyck len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 4929451980aSAlexander Duyck len = SKB_DATA_ALIGN(len); 4939451980aSAlexander Duyck 4949451980aSAlexander Duyck if (sk_memalloc_socks()) 4959451980aSAlexander Duyck gfp_mask |= __GFP_MEMALLOC; 4969451980aSAlexander Duyck 4979451980aSAlexander Duyck data = __alloc_page_frag(nc, len, gfp_mask); 4989451980aSAlexander Duyck if (unlikely(!data)) 4999451980aSAlexander Duyck return NULL; 5009451980aSAlexander Duyck 5019451980aSAlexander Duyck skb = __build_skb(data, len); 5029451980aSAlexander Duyck if (unlikely(!skb)) { 503181edb2bSAlexander Duyck skb_free_frag(data); 5049451980aSAlexander Duyck return NULL; 5059451980aSAlexander Duyck } 5069451980aSAlexander Duyck 5079451980aSAlexander Duyck /* use OR instead of assignment to avoid clearing of bits in mask */ 5089451980aSAlexander Duyck if (nc->pfmemalloc) 5099451980aSAlexander Duyck skb->pfmemalloc = 1; 5109451980aSAlexander Duyck skb->head_frag = 1; 5119451980aSAlexander Duyck 512a080e7bdSAlexander Duyck skb_success: 513fd11a83dSAlexander Duyck skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN); 514fd11a83dSAlexander Duyck skb->dev = napi->dev; 515fd11a83dSAlexander Duyck 516a080e7bdSAlexander Duyck skb_fail: 517fd11a83dSAlexander Duyck return skb; 518fd11a83dSAlexander Duyck } 519fd11a83dSAlexander Duyck EXPORT_SYMBOL(__napi_alloc_skb); 520fd11a83dSAlexander Duyck 521654bed16SPeter Zijlstra void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off, 52250269e19SEric Dumazet int size, unsigned int truesize) 523654bed16SPeter Zijlstra { 524654bed16SPeter Zijlstra skb_fill_page_desc(skb, i, page, off, size); 525654bed16SPeter Zijlstra skb->len += size; 526654bed16SPeter Zijlstra skb->data_len += size; 52750269e19SEric Dumazet skb->truesize += truesize; 528654bed16SPeter Zijlstra } 529654bed16SPeter Zijlstra EXPORT_SYMBOL(skb_add_rx_frag); 530654bed16SPeter Zijlstra 531f8e617e1SJason Wang void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size, 532f8e617e1SJason Wang unsigned int truesize) 533f8e617e1SJason Wang { 534f8e617e1SJason Wang skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 535f8e617e1SJason Wang 536f8e617e1SJason Wang skb_frag_size_add(frag, size); 537f8e617e1SJason Wang skb->len += size; 538f8e617e1SJason Wang skb->data_len += size; 539f8e617e1SJason Wang skb->truesize += truesize; 540f8e617e1SJason Wang } 541f8e617e1SJason Wang EXPORT_SYMBOL(skb_coalesce_rx_frag); 542f8e617e1SJason Wang 54327b437c8SHerbert Xu static void skb_drop_list(struct sk_buff **listp) 5441da177e4SLinus Torvalds { 545bd8a7036SEric Dumazet kfree_skb_list(*listp); 54627b437c8SHerbert Xu *listp = NULL; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 54927b437c8SHerbert Xu static inline void skb_drop_fraglist(struct sk_buff *skb) 55027b437c8SHerbert Xu { 55127b437c8SHerbert Xu skb_drop_list(&skb_shinfo(skb)->frag_list); 55227b437c8SHerbert Xu } 55327b437c8SHerbert Xu 5541da177e4SLinus Torvalds static void skb_clone_fraglist(struct sk_buff *skb) 5551da177e4SLinus Torvalds { 5561da177e4SLinus Torvalds struct sk_buff *list; 5571da177e4SLinus Torvalds 558fbb398a8SDavid S. Miller skb_walk_frags(skb, list) 5591da177e4SLinus Torvalds skb_get(list); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 562d3836f21SEric Dumazet static void skb_free_head(struct sk_buff *skb) 563d3836f21SEric Dumazet { 564181edb2bSAlexander Duyck unsigned char *head = skb->head; 565181edb2bSAlexander Duyck 566d3836f21SEric Dumazet if (skb->head_frag) 567181edb2bSAlexander Duyck skb_free_frag(head); 568d3836f21SEric Dumazet else 569181edb2bSAlexander Duyck kfree(head); 570d3836f21SEric Dumazet } 571d3836f21SEric Dumazet 5725bba1712SAdrian Bunk static void skb_release_data(struct sk_buff *skb) 5731da177e4SLinus Torvalds { 574ff04a771SEric Dumazet struct skb_shared_info *shinfo = skb_shinfo(skb); 5751da177e4SLinus Torvalds int i; 576ff04a771SEric Dumazet 577ff04a771SEric Dumazet if (skb->cloned && 578ff04a771SEric Dumazet atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, 579ff04a771SEric Dumazet &shinfo->dataref)) 580ff04a771SEric Dumazet return; 581ff04a771SEric Dumazet 582ff04a771SEric Dumazet for (i = 0; i < shinfo->nr_frags; i++) 583ff04a771SEric Dumazet __skb_frag_unref(&shinfo->frags[i]); 5841da177e4SLinus Torvalds 585a6686f2fSShirley Ma /* 586a6686f2fSShirley Ma * If skb buf is from userspace, we need to notify the caller 587a6686f2fSShirley Ma * the lower device DMA has done; 588a6686f2fSShirley Ma */ 589ff04a771SEric Dumazet if (shinfo->tx_flags & SKBTX_DEV_ZEROCOPY) { 590a6686f2fSShirley Ma struct ubuf_info *uarg; 591a6686f2fSShirley Ma 592ff04a771SEric Dumazet uarg = shinfo->destructor_arg; 593a6686f2fSShirley Ma if (uarg->callback) 594e19d6763SMichael S. Tsirkin uarg->callback(uarg, true); 595a6686f2fSShirley Ma } 596a6686f2fSShirley Ma 597ff04a771SEric Dumazet if (shinfo->frag_list) 598ff04a771SEric Dumazet kfree_skb_list(shinfo->frag_list); 5991da177e4SLinus Torvalds 600d3836f21SEric Dumazet skb_free_head(skb); 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /* 6041da177e4SLinus Torvalds * Free an skbuff by memory without cleaning the state. 6051da177e4SLinus Torvalds */ 6062d4baff8SHerbert Xu static void kfree_skbmem(struct sk_buff *skb) 6071da177e4SLinus Torvalds { 608d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones; 609d179cd12SDavid S. Miller 610d179cd12SDavid S. Miller switch (skb->fclone) { 611d179cd12SDavid S. Miller case SKB_FCLONE_UNAVAILABLE: 6121da177e4SLinus Torvalds kmem_cache_free(skbuff_head_cache, skb); 6136ffe75ebSEric Dumazet return; 614d179cd12SDavid S. Miller 615d179cd12SDavid S. Miller case SKB_FCLONE_ORIG: 616d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb1); 6176ffe75ebSEric Dumazet 6186ffe75ebSEric Dumazet /* We usually free the clone (TX completion) before original skb 6196ffe75ebSEric Dumazet * This test would have no chance to be true for the clone, 6206ffe75ebSEric Dumazet * while here, branch prediction will be good. 6216ffe75ebSEric Dumazet */ 6226ffe75ebSEric Dumazet if (atomic_read(&fclones->fclone_ref) == 1) 6236ffe75ebSEric Dumazet goto fastpath; 624d179cd12SDavid S. Miller break; 625d179cd12SDavid S. Miller 6266ffe75ebSEric Dumazet default: /* SKB_FCLONE_CLONE */ 627d0bf4a9eSEric Dumazet fclones = container_of(skb, struct sk_buff_fclones, skb2); 628d179cd12SDavid S. Miller break; 6293ff50b79SStephen Hemminger } 6306ffe75ebSEric Dumazet if (!atomic_dec_and_test(&fclones->fclone_ref)) 6316ffe75ebSEric Dumazet return; 6326ffe75ebSEric Dumazet fastpath: 6336ffe75ebSEric Dumazet kmem_cache_free(skbuff_fclone_cache, fclones); 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 63604a4bb55SLennert Buytenhek static void skb_release_head_state(struct sk_buff *skb) 6371da177e4SLinus Torvalds { 638adf30907SEric Dumazet skb_dst_drop(skb); 6391da177e4SLinus Torvalds #ifdef CONFIG_XFRM 6401da177e4SLinus Torvalds secpath_put(skb->sp); 6411da177e4SLinus Torvalds #endif 6421da177e4SLinus Torvalds if (skb->destructor) { 6439c2b3328SStephen Hemminger WARN_ON(in_irq()); 6441da177e4SLinus Torvalds skb->destructor(skb); 6451da177e4SLinus Torvalds } 646a3bf7ae9SIgor Maravić #if IS_ENABLED(CONFIG_NF_CONNTRACK) 6475f79e0f9SYasuyuki Kozakai nf_conntrack_put(skb->nfct); 6482fc72c7bSKOVACS Krisztian #endif 6491109a90cSPablo Neira Ayuso #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 6501da177e4SLinus Torvalds nf_bridge_put(skb->nf_bridge); 6511da177e4SLinus Torvalds #endif 65204a4bb55SLennert Buytenhek } 65304a4bb55SLennert Buytenhek 65404a4bb55SLennert Buytenhek /* Free everything but the sk_buff shell. */ 65504a4bb55SLennert Buytenhek static void skb_release_all(struct sk_buff *skb) 65604a4bb55SLennert Buytenhek { 65704a4bb55SLennert Buytenhek skb_release_head_state(skb); 6585e71d9d7SPablo Neira if (likely(skb->head)) 6592d4baff8SHerbert Xu skb_release_data(skb); 6602d4baff8SHerbert Xu } 6611da177e4SLinus Torvalds 6622d4baff8SHerbert Xu /** 6632d4baff8SHerbert Xu * __kfree_skb - private function 6642d4baff8SHerbert Xu * @skb: buffer 6652d4baff8SHerbert Xu * 6662d4baff8SHerbert Xu * Free an sk_buff. Release anything attached to the buffer. 6672d4baff8SHerbert Xu * Clean the state. This is an internal helper function. Users should 6682d4baff8SHerbert Xu * always call kfree_skb 6692d4baff8SHerbert Xu */ 6702d4baff8SHerbert Xu 6712d4baff8SHerbert Xu void __kfree_skb(struct sk_buff *skb) 6722d4baff8SHerbert Xu { 6732d4baff8SHerbert Xu skb_release_all(skb); 6741da177e4SLinus Torvalds kfree_skbmem(skb); 6751da177e4SLinus Torvalds } 676b4ac530fSDavid S. Miller EXPORT_SYMBOL(__kfree_skb); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /** 679231d06aeSJörn Engel * kfree_skb - free an sk_buff 680231d06aeSJörn Engel * @skb: buffer to free 681231d06aeSJörn Engel * 682231d06aeSJörn Engel * Drop a reference to the buffer and free it if the usage count has 683231d06aeSJörn Engel * hit zero. 684231d06aeSJörn Engel */ 685231d06aeSJörn Engel void kfree_skb(struct sk_buff *skb) 686231d06aeSJörn Engel { 687231d06aeSJörn Engel if (unlikely(!skb)) 688231d06aeSJörn Engel return; 689231d06aeSJörn Engel if (likely(atomic_read(&skb->users) == 1)) 690231d06aeSJörn Engel smp_rmb(); 691231d06aeSJörn Engel else if (likely(!atomic_dec_and_test(&skb->users))) 692231d06aeSJörn Engel return; 693ead2ceb0SNeil Horman trace_kfree_skb(skb, __builtin_return_address(0)); 694231d06aeSJörn Engel __kfree_skb(skb); 695231d06aeSJörn Engel } 696b4ac530fSDavid S. Miller EXPORT_SYMBOL(kfree_skb); 697231d06aeSJörn Engel 698bd8a7036SEric Dumazet void kfree_skb_list(struct sk_buff *segs) 699bd8a7036SEric Dumazet { 700bd8a7036SEric Dumazet while (segs) { 701bd8a7036SEric Dumazet struct sk_buff *next = segs->next; 702bd8a7036SEric Dumazet 703bd8a7036SEric Dumazet kfree_skb(segs); 704bd8a7036SEric Dumazet segs = next; 705bd8a7036SEric Dumazet } 706bd8a7036SEric Dumazet } 707bd8a7036SEric Dumazet EXPORT_SYMBOL(kfree_skb_list); 708bd8a7036SEric Dumazet 709d1a203eaSStephen Hemminger /** 71025121173SMichael S. Tsirkin * skb_tx_error - report an sk_buff xmit error 71125121173SMichael S. Tsirkin * @skb: buffer that triggered an error 71225121173SMichael S. Tsirkin * 71325121173SMichael S. Tsirkin * Report xmit error if a device callback is tracking this skb. 71425121173SMichael S. Tsirkin * skb must be freed afterwards. 71525121173SMichael S. Tsirkin */ 71625121173SMichael S. Tsirkin void skb_tx_error(struct sk_buff *skb) 71725121173SMichael S. Tsirkin { 71825121173SMichael S. Tsirkin if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { 71925121173SMichael S. Tsirkin struct ubuf_info *uarg; 72025121173SMichael S. Tsirkin 72125121173SMichael S. Tsirkin uarg = skb_shinfo(skb)->destructor_arg; 72225121173SMichael S. Tsirkin if (uarg->callback) 72325121173SMichael S. Tsirkin uarg->callback(uarg, false); 72425121173SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 72525121173SMichael S. Tsirkin } 72625121173SMichael S. Tsirkin } 72725121173SMichael S. Tsirkin EXPORT_SYMBOL(skb_tx_error); 72825121173SMichael S. Tsirkin 72925121173SMichael S. Tsirkin /** 730ead2ceb0SNeil Horman * consume_skb - free an skbuff 731ead2ceb0SNeil Horman * @skb: buffer to free 732ead2ceb0SNeil Horman * 733ead2ceb0SNeil Horman * Drop a ref to the buffer and free it if the usage count has hit zero 734ead2ceb0SNeil Horman * Functions identically to kfree_skb, but kfree_skb assumes that the frame 735ead2ceb0SNeil Horman * is being dropped after a failure and notes that 736ead2ceb0SNeil Horman */ 737ead2ceb0SNeil Horman void consume_skb(struct sk_buff *skb) 738ead2ceb0SNeil Horman { 739ead2ceb0SNeil Horman if (unlikely(!skb)) 740ead2ceb0SNeil Horman return; 741ead2ceb0SNeil Horman if (likely(atomic_read(&skb->users) == 1)) 742ead2ceb0SNeil Horman smp_rmb(); 743ead2ceb0SNeil Horman else if (likely(!atomic_dec_and_test(&skb->users))) 744ead2ceb0SNeil Horman return; 74507dc22e7SKoki Sanagi trace_consume_skb(skb); 746ead2ceb0SNeil Horman __kfree_skb(skb); 747ead2ceb0SNeil Horman } 748ead2ceb0SNeil Horman EXPORT_SYMBOL(consume_skb); 749ead2ceb0SNeil Horman 750b1937227SEric Dumazet /* Make sure a field is enclosed inside headers_start/headers_end section */ 751b1937227SEric Dumazet #define CHECK_SKB_FIELD(field) \ 752b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) < \ 753b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); \ 754b1937227SEric Dumazet BUILD_BUG_ON(offsetof(struct sk_buff, field) > \ 755b1937227SEric Dumazet offsetof(struct sk_buff, headers_end)); \ 756b1937227SEric Dumazet 757dec18810SHerbert Xu static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 758dec18810SHerbert Xu { 759dec18810SHerbert Xu new->tstamp = old->tstamp; 760b1937227SEric Dumazet /* We do not copy old->sk */ 761dec18810SHerbert Xu new->dev = old->dev; 762b1937227SEric Dumazet memcpy(new->cb, old->cb, sizeof(old->cb)); 7637fee226aSEric Dumazet skb_dst_copy(new, old); 764def8b4faSAlexey Dobriyan #ifdef CONFIG_XFRM 765dec18810SHerbert Xu new->sp = secpath_get(old->sp); 766dec18810SHerbert Xu #endif 767b1937227SEric Dumazet __nf_copy(new, old, false); 7686aa895b0SPatrick McHardy 769b1937227SEric Dumazet /* Note : this field could be in headers_start/headers_end section 770b1937227SEric Dumazet * It is not yet because we do not want to have a 16 bit hole 771b1937227SEric Dumazet */ 772b1937227SEric Dumazet new->queue_mapping = old->queue_mapping; 77306021292SEliezer Tamir 774b1937227SEric Dumazet memcpy(&new->headers_start, &old->headers_start, 775b1937227SEric Dumazet offsetof(struct sk_buff, headers_end) - 776b1937227SEric Dumazet offsetof(struct sk_buff, headers_start)); 777b1937227SEric Dumazet CHECK_SKB_FIELD(protocol); 778b1937227SEric Dumazet CHECK_SKB_FIELD(csum); 779b1937227SEric Dumazet CHECK_SKB_FIELD(hash); 780b1937227SEric Dumazet CHECK_SKB_FIELD(priority); 781b1937227SEric Dumazet CHECK_SKB_FIELD(skb_iif); 782b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_proto); 783b1937227SEric Dumazet CHECK_SKB_FIELD(vlan_tci); 784b1937227SEric Dumazet CHECK_SKB_FIELD(transport_header); 785b1937227SEric Dumazet CHECK_SKB_FIELD(network_header); 786b1937227SEric Dumazet CHECK_SKB_FIELD(mac_header); 787b1937227SEric Dumazet CHECK_SKB_FIELD(inner_protocol); 788b1937227SEric Dumazet CHECK_SKB_FIELD(inner_transport_header); 789b1937227SEric Dumazet CHECK_SKB_FIELD(inner_network_header); 790b1937227SEric Dumazet CHECK_SKB_FIELD(inner_mac_header); 791b1937227SEric Dumazet CHECK_SKB_FIELD(mark); 792b1937227SEric Dumazet #ifdef CONFIG_NETWORK_SECMARK 793b1937227SEric Dumazet CHECK_SKB_FIELD(secmark); 794b1937227SEric Dumazet #endif 795e0d1095aSCong Wang #ifdef CONFIG_NET_RX_BUSY_POLL 796b1937227SEric Dumazet CHECK_SKB_FIELD(napi_id); 79706021292SEliezer Tamir #endif 7982bd82484SEric Dumazet #ifdef CONFIG_XPS 7992bd82484SEric Dumazet CHECK_SKB_FIELD(sender_cpu); 8002bd82484SEric Dumazet #endif 801b1937227SEric Dumazet #ifdef CONFIG_NET_SCHED 802b1937227SEric Dumazet CHECK_SKB_FIELD(tc_index); 803b1937227SEric Dumazet #ifdef CONFIG_NET_CLS_ACT 804b1937227SEric Dumazet CHECK_SKB_FIELD(tc_verd); 805b1937227SEric Dumazet #endif 806b1937227SEric Dumazet #endif 807b1937227SEric Dumazet 808dec18810SHerbert Xu } 809dec18810SHerbert Xu 81082c49a35SHerbert Xu /* 81182c49a35SHerbert Xu * You should not add any new code to this function. Add it to 81282c49a35SHerbert Xu * __copy_skb_header above instead. 81382c49a35SHerbert Xu */ 814e0053ec0SHerbert Xu static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb) 8151da177e4SLinus Torvalds { 8161da177e4SLinus Torvalds #define C(x) n->x = skb->x 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds n->next = n->prev = NULL; 8191da177e4SLinus Torvalds n->sk = NULL; 820dec18810SHerbert Xu __copy_skb_header(n, skb); 821dec18810SHerbert Xu 8221da177e4SLinus Torvalds C(len); 8231da177e4SLinus Torvalds C(data_len); 8243e6b3b2eSAlexey Dobriyan C(mac_len); 825334a8132SPatrick McHardy n->hdr_len = skb->nohdr ? skb_headroom(skb) : skb->hdr_len; 82602f1c89dSPaul Moore n->cloned = 1; 8271da177e4SLinus Torvalds n->nohdr = 0; 8281da177e4SLinus Torvalds n->destructor = NULL; 8291da177e4SLinus Torvalds C(tail); 8301da177e4SLinus Torvalds C(end); 83102f1c89dSPaul Moore C(head); 832d3836f21SEric Dumazet C(head_frag); 83302f1c89dSPaul Moore C(data); 83402f1c89dSPaul Moore C(truesize); 83502f1c89dSPaul Moore atomic_set(&n->users, 1); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds atomic_inc(&(skb_shinfo(skb)->dataref)); 8381da177e4SLinus Torvalds skb->cloned = 1; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds return n; 841e0053ec0SHerbert Xu #undef C 842e0053ec0SHerbert Xu } 843e0053ec0SHerbert Xu 844e0053ec0SHerbert Xu /** 845e0053ec0SHerbert Xu * skb_morph - morph one skb into another 846e0053ec0SHerbert Xu * @dst: the skb to receive the contents 847e0053ec0SHerbert Xu * @src: the skb to supply the contents 848e0053ec0SHerbert Xu * 849e0053ec0SHerbert Xu * This is identical to skb_clone except that the target skb is 850e0053ec0SHerbert Xu * supplied by the user. 851e0053ec0SHerbert Xu * 852e0053ec0SHerbert Xu * The target skb is returned upon exit. 853e0053ec0SHerbert Xu */ 854e0053ec0SHerbert Xu struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src) 855e0053ec0SHerbert Xu { 8562d4baff8SHerbert Xu skb_release_all(dst); 857e0053ec0SHerbert Xu return __skb_clone(dst, src); 858e0053ec0SHerbert Xu } 859e0053ec0SHerbert Xu EXPORT_SYMBOL_GPL(skb_morph); 860e0053ec0SHerbert Xu 8612c53040fSBen Hutchings /** 8622c53040fSBen Hutchings * skb_copy_ubufs - copy userspace skb frags buffers to kernel 86348c83012SMichael S. Tsirkin * @skb: the skb to modify 86448c83012SMichael S. Tsirkin * @gfp_mask: allocation priority 86548c83012SMichael S. Tsirkin * 86648c83012SMichael S. Tsirkin * This must be called on SKBTX_DEV_ZEROCOPY skb. 86748c83012SMichael S. Tsirkin * It will copy all frags into kernel and drop the reference 86848c83012SMichael S. Tsirkin * to userspace pages. 86948c83012SMichael S. Tsirkin * 87048c83012SMichael S. Tsirkin * If this function is called from an interrupt gfp_mask() must be 87148c83012SMichael S. Tsirkin * %GFP_ATOMIC. 87248c83012SMichael S. Tsirkin * 87348c83012SMichael S. Tsirkin * Returns 0 on success or a negative error code on failure 87448c83012SMichael S. Tsirkin * to allocate kernel memory to copy to. 87548c83012SMichael S. Tsirkin */ 87648c83012SMichael S. Tsirkin int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask) 877a6686f2fSShirley Ma { 878a6686f2fSShirley Ma int i; 879a6686f2fSShirley Ma int num_frags = skb_shinfo(skb)->nr_frags; 880a6686f2fSShirley Ma struct page *page, *head = NULL; 881a6686f2fSShirley Ma struct ubuf_info *uarg = skb_shinfo(skb)->destructor_arg; 882a6686f2fSShirley Ma 883a6686f2fSShirley Ma for (i = 0; i < num_frags; i++) { 884a6686f2fSShirley Ma u8 *vaddr; 885a6686f2fSShirley Ma skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 886a6686f2fSShirley Ma 88702756ed4SKrishna Kumar page = alloc_page(gfp_mask); 888a6686f2fSShirley Ma if (!page) { 889a6686f2fSShirley Ma while (head) { 89040dadff2SSunghan Suh struct page *next = (struct page *)page_private(head); 891a6686f2fSShirley Ma put_page(head); 892a6686f2fSShirley Ma head = next; 893a6686f2fSShirley Ma } 894a6686f2fSShirley Ma return -ENOMEM; 895a6686f2fSShirley Ma } 89651c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 897a6686f2fSShirley Ma memcpy(page_address(page), 8989e903e08SEric Dumazet vaddr + f->page_offset, skb_frag_size(f)); 89951c56b00SEric Dumazet kunmap_atomic(vaddr); 90040dadff2SSunghan Suh set_page_private(page, (unsigned long)head); 901a6686f2fSShirley Ma head = page; 902a6686f2fSShirley Ma } 903a6686f2fSShirley Ma 904a6686f2fSShirley Ma /* skb frags release userspace buffers */ 90502756ed4SKrishna Kumar for (i = 0; i < num_frags; i++) 906a8605c60SIan Campbell skb_frag_unref(skb, i); 907a6686f2fSShirley Ma 908e19d6763SMichael S. Tsirkin uarg->callback(uarg, false); 909a6686f2fSShirley Ma 910a6686f2fSShirley Ma /* skb frags point to kernel buffers */ 91102756ed4SKrishna Kumar for (i = num_frags - 1; i >= 0; i--) { 91202756ed4SKrishna Kumar __skb_fill_page_desc(skb, i, head, 0, 91302756ed4SKrishna Kumar skb_shinfo(skb)->frags[i].size); 91440dadff2SSunghan Suh head = (struct page *)page_private(head); 915a6686f2fSShirley Ma } 91648c83012SMichael S. Tsirkin 91748c83012SMichael S. Tsirkin skb_shinfo(skb)->tx_flags &= ~SKBTX_DEV_ZEROCOPY; 918a6686f2fSShirley Ma return 0; 919a6686f2fSShirley Ma } 920dcc0fb78SMichael S. Tsirkin EXPORT_SYMBOL_GPL(skb_copy_ubufs); 921a6686f2fSShirley Ma 922e0053ec0SHerbert Xu /** 923e0053ec0SHerbert Xu * skb_clone - duplicate an sk_buff 924e0053ec0SHerbert Xu * @skb: buffer to clone 925e0053ec0SHerbert Xu * @gfp_mask: allocation priority 926e0053ec0SHerbert Xu * 927e0053ec0SHerbert Xu * Duplicate an &sk_buff. The new one is not owned by a socket. Both 928e0053ec0SHerbert Xu * copies share the same packet data but not structure. The new 929e0053ec0SHerbert Xu * buffer has a reference count of 1. If the allocation fails the 930e0053ec0SHerbert Xu * function returns %NULL otherwise the new buffer is returned. 931e0053ec0SHerbert Xu * 932e0053ec0SHerbert Xu * If this function is called from an interrupt gfp_mask() must be 933e0053ec0SHerbert Xu * %GFP_ATOMIC. 934e0053ec0SHerbert Xu */ 935e0053ec0SHerbert Xu 936e0053ec0SHerbert Xu struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) 937e0053ec0SHerbert Xu { 938d0bf4a9eSEric Dumazet struct sk_buff_fclones *fclones = container_of(skb, 939d0bf4a9eSEric Dumazet struct sk_buff_fclones, 940d0bf4a9eSEric Dumazet skb1); 9416ffe75ebSEric Dumazet struct sk_buff *n; 942e0053ec0SHerbert Xu 94370008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 944a6686f2fSShirley Ma return NULL; 945a6686f2fSShirley Ma 946e0053ec0SHerbert Xu if (skb->fclone == SKB_FCLONE_ORIG && 9476ffe75ebSEric Dumazet atomic_read(&fclones->fclone_ref) == 1) { 9486ffe75ebSEric Dumazet n = &fclones->skb2; 9496ffe75ebSEric Dumazet atomic_set(&fclones->fclone_ref, 2); 950e0053ec0SHerbert Xu } else { 951c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 952c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 953c93bdd0eSMel Gorman 954e0053ec0SHerbert Xu n = kmem_cache_alloc(skbuff_head_cache, gfp_mask); 955e0053ec0SHerbert Xu if (!n) 956e0053ec0SHerbert Xu return NULL; 957fe55f6d5SVegard Nossum 958fe55f6d5SVegard Nossum kmemcheck_annotate_bitfield(n, flags1); 959e0053ec0SHerbert Xu n->fclone = SKB_FCLONE_UNAVAILABLE; 960e0053ec0SHerbert Xu } 961e0053ec0SHerbert Xu 962e0053ec0SHerbert Xu return __skb_clone(n, skb); 9631da177e4SLinus Torvalds } 964b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_clone); 9651da177e4SLinus Torvalds 966f5b17294SPravin B Shelar static void skb_headers_offset_update(struct sk_buff *skb, int off) 967f5b17294SPravin B Shelar { 968030737bcSEric Dumazet /* Only adjust this if it actually is csum_start rather than csum */ 969030737bcSEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL) 970030737bcSEric Dumazet skb->csum_start += off; 971f5b17294SPravin B Shelar /* {transport,network,mac}_header and tail are relative to skb->head */ 972f5b17294SPravin B Shelar skb->transport_header += off; 973f5b17294SPravin B Shelar skb->network_header += off; 974f5b17294SPravin B Shelar if (skb_mac_header_was_set(skb)) 975f5b17294SPravin B Shelar skb->mac_header += off; 976f5b17294SPravin B Shelar skb->inner_transport_header += off; 977f5b17294SPravin B Shelar skb->inner_network_header += off; 978aefbd2b3SPravin B Shelar skb->inner_mac_header += off; 979f5b17294SPravin B Shelar } 980f5b17294SPravin B Shelar 9811da177e4SLinus Torvalds static void copy_skb_header(struct sk_buff *new, const struct sk_buff *old) 9821da177e4SLinus Torvalds { 983dec18810SHerbert Xu __copy_skb_header(new, old); 984dec18810SHerbert Xu 9857967168cSHerbert Xu skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size; 9867967168cSHerbert Xu skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs; 9877967168cSHerbert Xu skb_shinfo(new)->gso_type = skb_shinfo(old)->gso_type; 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds 990c93bdd0eSMel Gorman static inline int skb_alloc_rx_flag(const struct sk_buff *skb) 991c93bdd0eSMel Gorman { 992c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 993c93bdd0eSMel Gorman return SKB_ALLOC_RX; 994c93bdd0eSMel Gorman return 0; 995c93bdd0eSMel Gorman } 996c93bdd0eSMel Gorman 9971da177e4SLinus Torvalds /** 9981da177e4SLinus Torvalds * skb_copy - create private copy of an sk_buff 9991da177e4SLinus Torvalds * @skb: buffer to copy 10001da177e4SLinus Torvalds * @gfp_mask: allocation priority 10011da177e4SLinus Torvalds * 10021da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data. This is used when the 10031da177e4SLinus Torvalds * caller wishes to modify the data and needs a private copy of the 10041da177e4SLinus Torvalds * data to alter. Returns %NULL on failure or the pointer to the buffer 10051da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 10061da177e4SLinus Torvalds * 10071da177e4SLinus Torvalds * As by-product this function converts non-linear &sk_buff to linear 10081da177e4SLinus Torvalds * one, so that &sk_buff becomes completely private and caller is allowed 10091da177e4SLinus Torvalds * to modify all the data of returned buffer. This means that this 10101da177e4SLinus Torvalds * function is not recommended for use in circumstances when only 10111da177e4SLinus Torvalds * header is going to be modified. Use pskb_copy() instead. 10121da177e4SLinus Torvalds */ 10131da177e4SLinus Torvalds 1014dd0fc66fSAl Viro struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t gfp_mask) 10151da177e4SLinus Torvalds { 10166602cebbSEric Dumazet int headerlen = skb_headroom(skb); 1017ec47ea82SAlexander Duyck unsigned int size = skb_end_offset(skb) + skb->data_len; 1018c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(size, gfp_mask, 1019c93bdd0eSMel Gorman skb_alloc_rx_flag(skb), NUMA_NO_NODE); 10206602cebbSEric Dumazet 10211da177e4SLinus Torvalds if (!n) 10221da177e4SLinus Torvalds return NULL; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* Set the data pointer */ 10251da177e4SLinus Torvalds skb_reserve(n, headerlen); 10261da177e4SLinus Torvalds /* Set the tail pointer and length */ 10271da177e4SLinus Torvalds skb_put(n, skb->len); 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (skb_copy_bits(skb, -headerlen, n->head, headerlen + skb->len)) 10301da177e4SLinus Torvalds BUG(); 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds copy_skb_header(n, skb); 10331da177e4SLinus Torvalds return n; 10341da177e4SLinus Torvalds } 1035b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy); 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds /** 1038bad93e9dSOctavian Purdila * __pskb_copy_fclone - create copy of an sk_buff with private head. 10391da177e4SLinus Torvalds * @skb: buffer to copy 1040117632e6SEric Dumazet * @headroom: headroom of new skb 10411da177e4SLinus Torvalds * @gfp_mask: allocation priority 1042bad93e9dSOctavian Purdila * @fclone: if true allocate the copy of the skb from the fclone 1043bad93e9dSOctavian Purdila * cache instead of the head cache; it is recommended to set this 1044bad93e9dSOctavian Purdila * to true for the cases where the copy will likely be cloned 10451da177e4SLinus Torvalds * 10461da177e4SLinus Torvalds * Make a copy of both an &sk_buff and part of its data, located 10471da177e4SLinus Torvalds * in header. Fragmented data remain shared. This is used when 10481da177e4SLinus Torvalds * the caller wishes to modify only header of &sk_buff and needs 10491da177e4SLinus Torvalds * private copy of the header to alter. Returns %NULL on failure 10501da177e4SLinus Torvalds * or the pointer to the buffer on success. 10511da177e4SLinus Torvalds * The returned buffer has a reference count of 1. 10521da177e4SLinus Torvalds */ 10531da177e4SLinus Torvalds 1054bad93e9dSOctavian Purdila struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom, 1055bad93e9dSOctavian Purdila gfp_t gfp_mask, bool fclone) 10561da177e4SLinus Torvalds { 1057117632e6SEric Dumazet unsigned int size = skb_headlen(skb) + headroom; 1058bad93e9dSOctavian Purdila int flags = skb_alloc_rx_flag(skb) | (fclone ? SKB_ALLOC_FCLONE : 0); 1059bad93e9dSOctavian Purdila struct sk_buff *n = __alloc_skb(size, gfp_mask, flags, NUMA_NO_NODE); 10606602cebbSEric Dumazet 10611da177e4SLinus Torvalds if (!n) 10621da177e4SLinus Torvalds goto out; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /* Set the data pointer */ 1065117632e6SEric Dumazet skb_reserve(n, headroom); 10661da177e4SLinus Torvalds /* Set the tail pointer and length */ 10671da177e4SLinus Torvalds skb_put(n, skb_headlen(skb)); 10681da177e4SLinus Torvalds /* Copy the bytes */ 1069d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, n->data, n->len); 10701da177e4SLinus Torvalds 107125f484a6SHerbert Xu n->truesize += skb->data_len; 10721da177e4SLinus Torvalds n->data_len = skb->data_len; 10731da177e4SLinus Torvalds n->len = skb->len; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds if (skb_shinfo(skb)->nr_frags) { 10761da177e4SLinus Torvalds int i; 10771da177e4SLinus Torvalds 107870008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) { 10791511022cSDan Carpenter kfree_skb(n); 10801511022cSDan Carpenter n = NULL; 1081a6686f2fSShirley Ma goto out; 1082a6686f2fSShirley Ma } 10831da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 10841da177e4SLinus Torvalds skb_shinfo(n)->frags[i] = skb_shinfo(skb)->frags[i]; 1085ea2ab693SIan Campbell skb_frag_ref(skb, i); 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds skb_shinfo(n)->nr_frags = i; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 109021dc3301SDavid S. Miller if (skb_has_frag_list(skb)) { 10911da177e4SLinus Torvalds skb_shinfo(n)->frag_list = skb_shinfo(skb)->frag_list; 10921da177e4SLinus Torvalds skb_clone_fraglist(n); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds copy_skb_header(n, skb); 10961da177e4SLinus Torvalds out: 10971da177e4SLinus Torvalds return n; 10981da177e4SLinus Torvalds } 1099bad93e9dSOctavian Purdila EXPORT_SYMBOL(__pskb_copy_fclone); 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds /** 11021da177e4SLinus Torvalds * pskb_expand_head - reallocate header of &sk_buff 11031da177e4SLinus Torvalds * @skb: buffer to reallocate 11041da177e4SLinus Torvalds * @nhead: room to add at head 11051da177e4SLinus Torvalds * @ntail: room to add at tail 11061da177e4SLinus Torvalds * @gfp_mask: allocation priority 11071da177e4SLinus Torvalds * 1108bc32383cSMathias Krause * Expands (or creates identical copy, if @nhead and @ntail are zero) 1109bc32383cSMathias Krause * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have 11101da177e4SLinus Torvalds * reference count of 1. Returns zero in the case of success or error, 11111da177e4SLinus Torvalds * if expansion failed. In the last case, &sk_buff is not changed. 11121da177e4SLinus Torvalds * 11131da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 11141da177e4SLinus Torvalds * reloaded after call to this function. 11151da177e4SLinus Torvalds */ 11161da177e4SLinus Torvalds 111786a76cafSVictor Fusco int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, 1118dd0fc66fSAl Viro gfp_t gfp_mask) 11191da177e4SLinus Torvalds { 11201da177e4SLinus Torvalds int i; 11211da177e4SLinus Torvalds u8 *data; 1122ec47ea82SAlexander Duyck int size = nhead + skb_end_offset(skb) + ntail; 11231da177e4SLinus Torvalds long off; 11241da177e4SLinus Torvalds 11254edd87adSHerbert Xu BUG_ON(nhead < 0); 11264edd87adSHerbert Xu 11271da177e4SLinus Torvalds if (skb_shared(skb)) 11281da177e4SLinus Torvalds BUG(); 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds size = SKB_DATA_ALIGN(size); 11311da177e4SLinus Torvalds 1132c93bdd0eSMel Gorman if (skb_pfmemalloc(skb)) 1133c93bdd0eSMel Gorman gfp_mask |= __GFP_MEMALLOC; 1134c93bdd0eSMel Gorman data = kmalloc_reserve(size + SKB_DATA_ALIGN(sizeof(struct skb_shared_info)), 1135c93bdd0eSMel Gorman gfp_mask, NUMA_NO_NODE, NULL); 11361da177e4SLinus Torvalds if (!data) 11371da177e4SLinus Torvalds goto nodata; 113887151b86SEric Dumazet size = SKB_WITH_OVERHEAD(ksize(data)); 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds /* Copy only real data... and, alas, header. This should be 11416602cebbSEric Dumazet * optimized for the cases when header is void. 11426602cebbSEric Dumazet */ 11436602cebbSEric Dumazet memcpy(data + nhead, skb->head, skb_tail_pointer(skb) - skb->head); 11446602cebbSEric Dumazet 11456602cebbSEric Dumazet memcpy((struct skb_shared_info *)(data + size), 11466602cebbSEric Dumazet skb_shinfo(skb), 1147fed66381SEric Dumazet offsetof(struct skb_shared_info, frags[skb_shinfo(skb)->nr_frags])); 11481da177e4SLinus Torvalds 11493e24591aSAlexander Duyck /* 11503e24591aSAlexander Duyck * if shinfo is shared we must drop the old head gracefully, but if it 11513e24591aSAlexander Duyck * is not we can just drop the old head and let the existing refcount 11523e24591aSAlexander Duyck * be since all we did is relocate the values 11533e24591aSAlexander Duyck */ 11543e24591aSAlexander Duyck if (skb_cloned(skb)) { 1155a6686f2fSShirley Ma /* copy this zero copy skb frags */ 115670008aa5SMichael S. Tsirkin if (skb_orphan_frags(skb, gfp_mask)) 1157a6686f2fSShirley Ma goto nofrags; 11581da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 1159ea2ab693SIan Campbell skb_frag_ref(skb, i); 11601da177e4SLinus Torvalds 116121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 11621da177e4SLinus Torvalds skb_clone_fraglist(skb); 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds skb_release_data(skb); 11653e24591aSAlexander Duyck } else { 11663e24591aSAlexander Duyck skb_free_head(skb); 11671fd63041SEric Dumazet } 11681da177e4SLinus Torvalds off = (data + nhead) - skb->head; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds skb->head = data; 1171d3836f21SEric Dumazet skb->head_frag = 0; 11721da177e4SLinus Torvalds skb->data += off; 11734305b541SArnaldo Carvalho de Melo #ifdef NET_SKBUFF_DATA_USES_OFFSET 11744305b541SArnaldo Carvalho de Melo skb->end = size; 117556eb8882SPatrick McHardy off = nhead; 11764305b541SArnaldo Carvalho de Melo #else 11774305b541SArnaldo Carvalho de Melo skb->end = skb->head + size; 117856eb8882SPatrick McHardy #endif 117927a884dcSArnaldo Carvalho de Melo skb->tail += off; 1180b41abb42SPeter Pan(潘卫平) skb_headers_offset_update(skb, nhead); 11811da177e4SLinus Torvalds skb->cloned = 0; 1182334a8132SPatrick McHardy skb->hdr_len = 0; 11831da177e4SLinus Torvalds skb->nohdr = 0; 11841da177e4SLinus Torvalds atomic_set(&skb_shinfo(skb)->dataref, 1); 11851da177e4SLinus Torvalds return 0; 11861da177e4SLinus Torvalds 1187a6686f2fSShirley Ma nofrags: 1188a6686f2fSShirley Ma kfree(data); 11891da177e4SLinus Torvalds nodata: 11901da177e4SLinus Torvalds return -ENOMEM; 11911da177e4SLinus Torvalds } 1192b4ac530fSDavid S. Miller EXPORT_SYMBOL(pskb_expand_head); 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /* Make private copy of skb with writable head and some headroom */ 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) 11971da177e4SLinus Torvalds { 11981da177e4SLinus Torvalds struct sk_buff *skb2; 11991da177e4SLinus Torvalds int delta = headroom - skb_headroom(skb); 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds if (delta <= 0) 12021da177e4SLinus Torvalds skb2 = pskb_copy(skb, GFP_ATOMIC); 12031da177e4SLinus Torvalds else { 12041da177e4SLinus Torvalds skb2 = skb_clone(skb, GFP_ATOMIC); 12051da177e4SLinus Torvalds if (skb2 && pskb_expand_head(skb2, SKB_DATA_ALIGN(delta), 0, 12061da177e4SLinus Torvalds GFP_ATOMIC)) { 12071da177e4SLinus Torvalds kfree_skb(skb2); 12081da177e4SLinus Torvalds skb2 = NULL; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds } 12111da177e4SLinus Torvalds return skb2; 12121da177e4SLinus Torvalds } 1213b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_realloc_headroom); 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /** 12161da177e4SLinus Torvalds * skb_copy_expand - copy and expand sk_buff 12171da177e4SLinus Torvalds * @skb: buffer to copy 12181da177e4SLinus Torvalds * @newheadroom: new free bytes at head 12191da177e4SLinus Torvalds * @newtailroom: new free bytes at tail 12201da177e4SLinus Torvalds * @gfp_mask: allocation priority 12211da177e4SLinus Torvalds * 12221da177e4SLinus Torvalds * Make a copy of both an &sk_buff and its data and while doing so 12231da177e4SLinus Torvalds * allocate additional space. 12241da177e4SLinus Torvalds * 12251da177e4SLinus Torvalds * This is used when the caller wishes to modify the data and needs a 12261da177e4SLinus Torvalds * private copy of the data to alter as well as more space for new fields. 12271da177e4SLinus Torvalds * Returns %NULL on failure or the pointer to the buffer 12281da177e4SLinus Torvalds * on success. The returned buffer has a reference count of 1. 12291da177e4SLinus Torvalds * 12301da177e4SLinus Torvalds * You must pass %GFP_ATOMIC as the allocation priority if this function 12311da177e4SLinus Torvalds * is called from an interrupt. 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds struct sk_buff *skb_copy_expand(const struct sk_buff *skb, 123486a76cafSVictor Fusco int newheadroom, int newtailroom, 1235dd0fc66fSAl Viro gfp_t gfp_mask) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds /* 12381da177e4SLinus Torvalds * Allocate the copy buffer 12391da177e4SLinus Torvalds */ 1240c93bdd0eSMel Gorman struct sk_buff *n = __alloc_skb(newheadroom + skb->len + newtailroom, 1241c93bdd0eSMel Gorman gfp_mask, skb_alloc_rx_flag(skb), 1242c93bdd0eSMel Gorman NUMA_NO_NODE); 1243efd1e8d5SPatrick McHardy int oldheadroom = skb_headroom(skb); 12441da177e4SLinus Torvalds int head_copy_len, head_copy_off; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds if (!n) 12471da177e4SLinus Torvalds return NULL; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds skb_reserve(n, newheadroom); 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds /* Set the tail pointer and length */ 12521da177e4SLinus Torvalds skb_put(n, skb->len); 12531da177e4SLinus Torvalds 1254efd1e8d5SPatrick McHardy head_copy_len = oldheadroom; 12551da177e4SLinus Torvalds head_copy_off = 0; 12561da177e4SLinus Torvalds if (newheadroom <= head_copy_len) 12571da177e4SLinus Torvalds head_copy_len = newheadroom; 12581da177e4SLinus Torvalds else 12591da177e4SLinus Torvalds head_copy_off = newheadroom - head_copy_len; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds /* Copy the linear header and data. */ 12621da177e4SLinus Torvalds if (skb_copy_bits(skb, -head_copy_len, n->head + head_copy_off, 12631da177e4SLinus Torvalds skb->len + head_copy_len)) 12641da177e4SLinus Torvalds BUG(); 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds copy_skb_header(n, skb); 12671da177e4SLinus Torvalds 1268030737bcSEric Dumazet skb_headers_offset_update(n, newheadroom - oldheadroom); 1269efd1e8d5SPatrick McHardy 12701da177e4SLinus Torvalds return n; 12711da177e4SLinus Torvalds } 1272b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_expand); 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds /** 12751da177e4SLinus Torvalds * skb_pad - zero pad the tail of an skb 12761da177e4SLinus Torvalds * @skb: buffer to pad 12771da177e4SLinus Torvalds * @pad: space to pad 12781da177e4SLinus Torvalds * 12791da177e4SLinus Torvalds * Ensure that a buffer is followed by a padding area that is zero 12801da177e4SLinus Torvalds * filled. Used by network drivers which may DMA or transfer data 12811da177e4SLinus Torvalds * beyond the buffer end onto the wire. 12821da177e4SLinus Torvalds * 12835b057c6bSHerbert Xu * May return error in out of memory cases. The skb is freed on error. 12841da177e4SLinus Torvalds */ 12851da177e4SLinus Torvalds 12865b057c6bSHerbert Xu int skb_pad(struct sk_buff *skb, int pad) 12871da177e4SLinus Torvalds { 12885b057c6bSHerbert Xu int err; 12895b057c6bSHerbert Xu int ntail; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds /* If the skbuff is non linear tailroom is always zero.. */ 12925b057c6bSHerbert Xu if (!skb_cloned(skb) && skb_tailroom(skb) >= pad) { 12931da177e4SLinus Torvalds memset(skb->data+skb->len, 0, pad); 12945b057c6bSHerbert Xu return 0; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12974305b541SArnaldo Carvalho de Melo ntail = skb->data_len + pad - (skb->end - skb->tail); 12985b057c6bSHerbert Xu if (likely(skb_cloned(skb) || ntail > 0)) { 12995b057c6bSHerbert Xu err = pskb_expand_head(skb, 0, ntail, GFP_ATOMIC); 13005b057c6bSHerbert Xu if (unlikely(err)) 13015b057c6bSHerbert Xu goto free_skb; 13025b057c6bSHerbert Xu } 13035b057c6bSHerbert Xu 13045b057c6bSHerbert Xu /* FIXME: The use of this function with non-linear skb's really needs 13055b057c6bSHerbert Xu * to be audited. 13065b057c6bSHerbert Xu */ 13075b057c6bSHerbert Xu err = skb_linearize(skb); 13085b057c6bSHerbert Xu if (unlikely(err)) 13095b057c6bSHerbert Xu goto free_skb; 13105b057c6bSHerbert Xu 13115b057c6bSHerbert Xu memset(skb->data + skb->len, 0, pad); 13125b057c6bSHerbert Xu return 0; 13135b057c6bSHerbert Xu 13145b057c6bSHerbert Xu free_skb: 13151da177e4SLinus Torvalds kfree_skb(skb); 13165b057c6bSHerbert Xu return err; 13171da177e4SLinus Torvalds } 1318b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_pad); 13191da177e4SLinus Torvalds 13200dde3e16SIlpo Järvinen /** 13210c7ddf36SMathias Krause * pskb_put - add data to the tail of a potentially fragmented buffer 13220c7ddf36SMathias Krause * @skb: start of the buffer to use 13230c7ddf36SMathias Krause * @tail: tail fragment of the buffer to use 13240c7ddf36SMathias Krause * @len: amount of data to add 13250c7ddf36SMathias Krause * 13260c7ddf36SMathias Krause * This function extends the used data area of the potentially 13270c7ddf36SMathias Krause * fragmented buffer. @tail must be the last fragment of @skb -- or 13280c7ddf36SMathias Krause * @skb itself. If this would exceed the total buffer size the kernel 13290c7ddf36SMathias Krause * will panic. A pointer to the first byte of the extra data is 13300c7ddf36SMathias Krause * returned. 13310c7ddf36SMathias Krause */ 13320c7ddf36SMathias Krause 13330c7ddf36SMathias Krause unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) 13340c7ddf36SMathias Krause { 13350c7ddf36SMathias Krause if (tail != skb) { 13360c7ddf36SMathias Krause skb->data_len += len; 13370c7ddf36SMathias Krause skb->len += len; 13380c7ddf36SMathias Krause } 13390c7ddf36SMathias Krause return skb_put(tail, len); 13400c7ddf36SMathias Krause } 13410c7ddf36SMathias Krause EXPORT_SYMBOL_GPL(pskb_put); 13420c7ddf36SMathias Krause 13430c7ddf36SMathias Krause /** 13440dde3e16SIlpo Järvinen * skb_put - add data to a buffer 13450dde3e16SIlpo Järvinen * @skb: buffer to use 13460dde3e16SIlpo Järvinen * @len: amount of data to add 13470dde3e16SIlpo Järvinen * 13480dde3e16SIlpo Järvinen * This function extends the used data area of the buffer. If this would 13490dde3e16SIlpo Järvinen * exceed the total buffer size the kernel will panic. A pointer to the 13500dde3e16SIlpo Järvinen * first byte of the extra data is returned. 13510dde3e16SIlpo Järvinen */ 13520dde3e16SIlpo Järvinen unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 13530dde3e16SIlpo Järvinen { 13540dde3e16SIlpo Järvinen unsigned char *tmp = skb_tail_pointer(skb); 13550dde3e16SIlpo Järvinen SKB_LINEAR_ASSERT(skb); 13560dde3e16SIlpo Järvinen skb->tail += len; 13570dde3e16SIlpo Järvinen skb->len += len; 13580dde3e16SIlpo Järvinen if (unlikely(skb->tail > skb->end)) 13590dde3e16SIlpo Järvinen skb_over_panic(skb, len, __builtin_return_address(0)); 13600dde3e16SIlpo Järvinen return tmp; 13610dde3e16SIlpo Järvinen } 13620dde3e16SIlpo Järvinen EXPORT_SYMBOL(skb_put); 13630dde3e16SIlpo Järvinen 13646be8ac2fSIlpo Järvinen /** 1365c2aa270aSIlpo Järvinen * skb_push - add data to the start of a buffer 1366c2aa270aSIlpo Järvinen * @skb: buffer to use 1367c2aa270aSIlpo Järvinen * @len: amount of data to add 1368c2aa270aSIlpo Järvinen * 1369c2aa270aSIlpo Järvinen * This function extends the used data area of the buffer at the buffer 1370c2aa270aSIlpo Järvinen * start. If this would exceed the total buffer headroom the kernel will 1371c2aa270aSIlpo Järvinen * panic. A pointer to the first byte of the extra data is returned. 1372c2aa270aSIlpo Järvinen */ 1373c2aa270aSIlpo Järvinen unsigned char *skb_push(struct sk_buff *skb, unsigned int len) 1374c2aa270aSIlpo Järvinen { 1375c2aa270aSIlpo Järvinen skb->data -= len; 1376c2aa270aSIlpo Järvinen skb->len += len; 1377c2aa270aSIlpo Järvinen if (unlikely(skb->data<skb->head)) 1378c2aa270aSIlpo Järvinen skb_under_panic(skb, len, __builtin_return_address(0)); 1379c2aa270aSIlpo Järvinen return skb->data; 1380c2aa270aSIlpo Järvinen } 1381c2aa270aSIlpo Järvinen EXPORT_SYMBOL(skb_push); 1382c2aa270aSIlpo Järvinen 1383c2aa270aSIlpo Järvinen /** 13846be8ac2fSIlpo Järvinen * skb_pull - remove data from the start of a buffer 13856be8ac2fSIlpo Järvinen * @skb: buffer to use 13866be8ac2fSIlpo Järvinen * @len: amount of data to remove 13876be8ac2fSIlpo Järvinen * 13886be8ac2fSIlpo Järvinen * This function removes data from the start of a buffer, returning 13896be8ac2fSIlpo Järvinen * the memory to the headroom. A pointer to the next data in the buffer 13906be8ac2fSIlpo Järvinen * is returned. Once the data has been pulled future pushes will overwrite 13916be8ac2fSIlpo Järvinen * the old data. 13926be8ac2fSIlpo Järvinen */ 13936be8ac2fSIlpo Järvinen unsigned char *skb_pull(struct sk_buff *skb, unsigned int len) 13946be8ac2fSIlpo Järvinen { 139547d29646SDavid S. Miller return skb_pull_inline(skb, len); 13966be8ac2fSIlpo Järvinen } 13976be8ac2fSIlpo Järvinen EXPORT_SYMBOL(skb_pull); 13986be8ac2fSIlpo Järvinen 1399419ae74eSIlpo Järvinen /** 1400419ae74eSIlpo Järvinen * skb_trim - remove end from a buffer 1401419ae74eSIlpo Järvinen * @skb: buffer to alter 1402419ae74eSIlpo Järvinen * @len: new length 1403419ae74eSIlpo Järvinen * 1404419ae74eSIlpo Järvinen * Cut the length of a buffer down by removing data from the tail. If 1405419ae74eSIlpo Järvinen * the buffer is already under the length specified it is not modified. 1406419ae74eSIlpo Järvinen * The skb must be linear. 1407419ae74eSIlpo Järvinen */ 1408419ae74eSIlpo Järvinen void skb_trim(struct sk_buff *skb, unsigned int len) 1409419ae74eSIlpo Järvinen { 1410419ae74eSIlpo Järvinen if (skb->len > len) 1411419ae74eSIlpo Järvinen __skb_trim(skb, len); 1412419ae74eSIlpo Järvinen } 1413419ae74eSIlpo Järvinen EXPORT_SYMBOL(skb_trim); 1414419ae74eSIlpo Järvinen 14153cc0e873SHerbert Xu /* Trims skb to length len. It can change skb pointers. 14161da177e4SLinus Torvalds */ 14171da177e4SLinus Torvalds 14183cc0e873SHerbert Xu int ___pskb_trim(struct sk_buff *skb, unsigned int len) 14191da177e4SLinus Torvalds { 142027b437c8SHerbert Xu struct sk_buff **fragp; 142127b437c8SHerbert Xu struct sk_buff *frag; 14221da177e4SLinus Torvalds int offset = skb_headlen(skb); 14231da177e4SLinus Torvalds int nfrags = skb_shinfo(skb)->nr_frags; 14241da177e4SLinus Torvalds int i; 142527b437c8SHerbert Xu int err; 142627b437c8SHerbert Xu 142727b437c8SHerbert Xu if (skb_cloned(skb) && 142827b437c8SHerbert Xu unlikely((err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))) 142927b437c8SHerbert Xu return err; 14301da177e4SLinus Torvalds 1431f4d26fb3SHerbert Xu i = 0; 1432f4d26fb3SHerbert Xu if (offset >= len) 1433f4d26fb3SHerbert Xu goto drop_pages; 1434f4d26fb3SHerbert Xu 1435f4d26fb3SHerbert Xu for (; i < nfrags; i++) { 14369e903e08SEric Dumazet int end = offset + skb_frag_size(&skb_shinfo(skb)->frags[i]); 143727b437c8SHerbert Xu 143827b437c8SHerbert Xu if (end < len) { 14391da177e4SLinus Torvalds offset = end; 144027b437c8SHerbert Xu continue; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 14439e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i++], len - offset); 144427b437c8SHerbert Xu 1445f4d26fb3SHerbert Xu drop_pages: 144627b437c8SHerbert Xu skb_shinfo(skb)->nr_frags = i; 144727b437c8SHerbert Xu 144827b437c8SHerbert Xu for (; i < nfrags; i++) 1449ea2ab693SIan Campbell skb_frag_unref(skb, i); 145027b437c8SHerbert Xu 145121dc3301SDavid S. Miller if (skb_has_frag_list(skb)) 145227b437c8SHerbert Xu skb_drop_fraglist(skb); 1453f4d26fb3SHerbert Xu goto done; 145427b437c8SHerbert Xu } 145527b437c8SHerbert Xu 145627b437c8SHerbert Xu for (fragp = &skb_shinfo(skb)->frag_list; (frag = *fragp); 145727b437c8SHerbert Xu fragp = &frag->next) { 145827b437c8SHerbert Xu int end = offset + frag->len; 145927b437c8SHerbert Xu 146027b437c8SHerbert Xu if (skb_shared(frag)) { 146127b437c8SHerbert Xu struct sk_buff *nfrag; 146227b437c8SHerbert Xu 146327b437c8SHerbert Xu nfrag = skb_clone(frag, GFP_ATOMIC); 146427b437c8SHerbert Xu if (unlikely(!nfrag)) 146527b437c8SHerbert Xu return -ENOMEM; 146627b437c8SHerbert Xu 146727b437c8SHerbert Xu nfrag->next = frag->next; 146885bb2a60SEric Dumazet consume_skb(frag); 146927b437c8SHerbert Xu frag = nfrag; 147027b437c8SHerbert Xu *fragp = frag; 147127b437c8SHerbert Xu } 147227b437c8SHerbert Xu 147327b437c8SHerbert Xu if (end < len) { 147427b437c8SHerbert Xu offset = end; 147527b437c8SHerbert Xu continue; 147627b437c8SHerbert Xu } 147727b437c8SHerbert Xu 147827b437c8SHerbert Xu if (end > len && 147927b437c8SHerbert Xu unlikely((err = pskb_trim(frag, len - offset)))) 148027b437c8SHerbert Xu return err; 148127b437c8SHerbert Xu 148227b437c8SHerbert Xu if (frag->next) 148327b437c8SHerbert Xu skb_drop_list(&frag->next); 148427b437c8SHerbert Xu break; 148527b437c8SHerbert Xu } 148627b437c8SHerbert Xu 1487f4d26fb3SHerbert Xu done: 148827b437c8SHerbert Xu if (len > skb_headlen(skb)) { 14891da177e4SLinus Torvalds skb->data_len -= skb->len - len; 14901da177e4SLinus Torvalds skb->len = len; 14911da177e4SLinus Torvalds } else { 14921da177e4SLinus Torvalds skb->len = len; 14931da177e4SLinus Torvalds skb->data_len = 0; 149427a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds return 0; 14981da177e4SLinus Torvalds } 1499b4ac530fSDavid S. Miller EXPORT_SYMBOL(___pskb_trim); 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds /** 15021da177e4SLinus Torvalds * __pskb_pull_tail - advance tail of skb header 15031da177e4SLinus Torvalds * @skb: buffer to reallocate 15041da177e4SLinus Torvalds * @delta: number of bytes to advance tail 15051da177e4SLinus Torvalds * 15061da177e4SLinus Torvalds * The function makes a sense only on a fragmented &sk_buff, 15071da177e4SLinus Torvalds * it expands header moving its tail forward and copying necessary 15081da177e4SLinus Torvalds * data from fragmented part. 15091da177e4SLinus Torvalds * 15101da177e4SLinus Torvalds * &sk_buff MUST have reference count of 1. 15111da177e4SLinus Torvalds * 15121da177e4SLinus Torvalds * Returns %NULL (and &sk_buff does not change) if pull failed 15131da177e4SLinus Torvalds * or value of new tail of skb in the case of success. 15141da177e4SLinus Torvalds * 15151da177e4SLinus Torvalds * All the pointers pointing into skb header may change and must be 15161da177e4SLinus Torvalds * reloaded after call to this function. 15171da177e4SLinus Torvalds */ 15181da177e4SLinus Torvalds 15191da177e4SLinus Torvalds /* Moves tail of skb head forward, copying data from fragmented part, 15201da177e4SLinus Torvalds * when it is necessary. 15211da177e4SLinus Torvalds * 1. It may fail due to malloc failure. 15221da177e4SLinus Torvalds * 2. It may change skb pointers. 15231da177e4SLinus Torvalds * 15241da177e4SLinus Torvalds * It is pretty complicated. Luckily, it is called only in exceptional cases. 15251da177e4SLinus Torvalds */ 15261da177e4SLinus Torvalds unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta) 15271da177e4SLinus Torvalds { 15281da177e4SLinus Torvalds /* If skb has not enough free space at tail, get new one 15291da177e4SLinus Torvalds * plus 128 bytes for future expansions. If we have enough 15301da177e4SLinus Torvalds * room at tail, reallocate without expansion only if skb is cloned. 15311da177e4SLinus Torvalds */ 15324305b541SArnaldo Carvalho de Melo int i, k, eat = (skb->tail + delta) - skb->end; 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds if (eat > 0 || skb_cloned(skb)) { 15351da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, eat > 0 ? eat + 128 : 0, 15361da177e4SLinus Torvalds GFP_ATOMIC)) 15371da177e4SLinus Torvalds return NULL; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 154027a884dcSArnaldo Carvalho de Melo if (skb_copy_bits(skb, skb_headlen(skb), skb_tail_pointer(skb), delta)) 15411da177e4SLinus Torvalds BUG(); 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds /* Optimization: no fragments, no reasons to preestimate 15441da177e4SLinus Torvalds * size of pulled pages. Superb. 15451da177e4SLinus Torvalds */ 154621dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) 15471da177e4SLinus Torvalds goto pull_pages; 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds /* Estimate size of pulled pages. */ 15501da177e4SLinus Torvalds eat = delta; 15511da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 15529e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 15539e903e08SEric Dumazet 15549e903e08SEric Dumazet if (size >= eat) 15551da177e4SLinus Torvalds goto pull_pages; 15569e903e08SEric Dumazet eat -= size; 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds /* If we need update frag list, we are in troubles. 15601da177e4SLinus Torvalds * Certainly, it possible to add an offset to skb data, 15611da177e4SLinus Torvalds * but taking into account that pulling is expected to 15621da177e4SLinus Torvalds * be very rare operation, it is worth to fight against 15631da177e4SLinus Torvalds * further bloating skb head and crucify ourselves here instead. 15641da177e4SLinus Torvalds * Pure masohism, indeed. 8)8) 15651da177e4SLinus Torvalds */ 15661da177e4SLinus Torvalds if (eat) { 15671da177e4SLinus Torvalds struct sk_buff *list = skb_shinfo(skb)->frag_list; 15681da177e4SLinus Torvalds struct sk_buff *clone = NULL; 15691da177e4SLinus Torvalds struct sk_buff *insp = NULL; 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds do { 157209a62660SKris Katterjohn BUG_ON(!list); 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds if (list->len <= eat) { 15751da177e4SLinus Torvalds /* Eaten as whole. */ 15761da177e4SLinus Torvalds eat -= list->len; 15771da177e4SLinus Torvalds list = list->next; 15781da177e4SLinus Torvalds insp = list; 15791da177e4SLinus Torvalds } else { 15801da177e4SLinus Torvalds /* Eaten partially. */ 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds if (skb_shared(list)) { 15831da177e4SLinus Torvalds /* Sucks! We need to fork list. :-( */ 15841da177e4SLinus Torvalds clone = skb_clone(list, GFP_ATOMIC); 15851da177e4SLinus Torvalds if (!clone) 15861da177e4SLinus Torvalds return NULL; 15871da177e4SLinus Torvalds insp = list->next; 15881da177e4SLinus Torvalds list = clone; 15891da177e4SLinus Torvalds } else { 15901da177e4SLinus Torvalds /* This may be pulled without 15911da177e4SLinus Torvalds * problems. */ 15921da177e4SLinus Torvalds insp = list; 15931da177e4SLinus Torvalds } 15941da177e4SLinus Torvalds if (!pskb_pull(list, eat)) { 15951da177e4SLinus Torvalds kfree_skb(clone); 15961da177e4SLinus Torvalds return NULL; 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds break; 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds } while (eat); 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds /* Free pulled out fragments. */ 16031da177e4SLinus Torvalds while ((list = skb_shinfo(skb)->frag_list) != insp) { 16041da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = list->next; 16051da177e4SLinus Torvalds kfree_skb(list); 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds /* And insert new clone at head. */ 16081da177e4SLinus Torvalds if (clone) { 16091da177e4SLinus Torvalds clone->next = list; 16101da177e4SLinus Torvalds skb_shinfo(skb)->frag_list = clone; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds /* Success! Now we may commit changes to skb data. */ 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds pull_pages: 16161da177e4SLinus Torvalds eat = delta; 16171da177e4SLinus Torvalds k = 0; 16181da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16199e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 16209e903e08SEric Dumazet 16219e903e08SEric Dumazet if (size <= eat) { 1622ea2ab693SIan Campbell skb_frag_unref(skb, i); 16239e903e08SEric Dumazet eat -= size; 16241da177e4SLinus Torvalds } else { 16251da177e4SLinus Torvalds skb_shinfo(skb)->frags[k] = skb_shinfo(skb)->frags[i]; 16261da177e4SLinus Torvalds if (eat) { 16271da177e4SLinus Torvalds skb_shinfo(skb)->frags[k].page_offset += eat; 16289e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb)->frags[k], eat); 16291da177e4SLinus Torvalds eat = 0; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds k++; 16321da177e4SLinus Torvalds } 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = k; 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds skb->tail += delta; 16371da177e4SLinus Torvalds skb->data_len -= delta; 16381da177e4SLinus Torvalds 163927a884dcSArnaldo Carvalho de Melo return skb_tail_pointer(skb); 16401da177e4SLinus Torvalds } 1641b4ac530fSDavid S. Miller EXPORT_SYMBOL(__pskb_pull_tail); 16421da177e4SLinus Torvalds 164322019b17SEric Dumazet /** 164422019b17SEric Dumazet * skb_copy_bits - copy bits from skb to kernel buffer 164522019b17SEric Dumazet * @skb: source skb 164622019b17SEric Dumazet * @offset: offset in source 164722019b17SEric Dumazet * @to: destination buffer 164822019b17SEric Dumazet * @len: number of bytes to copy 164922019b17SEric Dumazet * 165022019b17SEric Dumazet * Copy the specified number of bytes from the source skb to the 165122019b17SEric Dumazet * destination buffer. 165222019b17SEric Dumazet * 165322019b17SEric Dumazet * CAUTION ! : 165422019b17SEric Dumazet * If its prototype is ever changed, 165522019b17SEric Dumazet * check arch/{*}/net/{*}.S files, 165622019b17SEric Dumazet * since it is called from BPF assembly code. 165722019b17SEric Dumazet */ 16581da177e4SLinus Torvalds int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len) 16591da177e4SLinus Torvalds { 16601a028e50SDavid S. Miller int start = skb_headlen(skb); 1661fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1662fbb398a8SDavid S. Miller int i, copy; 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds if (offset > (int)skb->len - len) 16651da177e4SLinus Torvalds goto fault; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds /* Copy header. */ 16681a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 16691da177e4SLinus Torvalds if (copy > len) 16701da177e4SLinus Torvalds copy = len; 1671d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, offset, to, copy); 16721da177e4SLinus Torvalds if ((len -= copy) == 0) 16731da177e4SLinus Torvalds return 0; 16741da177e4SLinus Torvalds offset += copy; 16751da177e4SLinus Torvalds to += copy; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 16791a028e50SDavid S. Miller int end; 168051c56b00SEric Dumazet skb_frag_t *f = &skb_shinfo(skb)->frags[i]; 16811da177e4SLinus Torvalds 1682547b792cSIlpo Järvinen WARN_ON(start > offset + len); 16831a028e50SDavid S. Miller 168451c56b00SEric Dumazet end = start + skb_frag_size(f); 16851da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 16861da177e4SLinus Torvalds u8 *vaddr; 16871da177e4SLinus Torvalds 16881da177e4SLinus Torvalds if (copy > len) 16891da177e4SLinus Torvalds copy = len; 16901da177e4SLinus Torvalds 169151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(f)); 16921da177e4SLinus Torvalds memcpy(to, 169351c56b00SEric Dumazet vaddr + f->page_offset + offset - start, 169451c56b00SEric Dumazet copy); 169551c56b00SEric Dumazet kunmap_atomic(vaddr); 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds if ((len -= copy) == 0) 16981da177e4SLinus Torvalds return 0; 16991da177e4SLinus Torvalds offset += copy; 17001da177e4SLinus Torvalds to += copy; 17011da177e4SLinus Torvalds } 17021a028e50SDavid S. Miller start = end; 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 1705fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 17061a028e50SDavid S. Miller int end; 17071da177e4SLinus Torvalds 1708547b792cSIlpo Järvinen WARN_ON(start > offset + len); 17091a028e50SDavid S. Miller 1710fbb398a8SDavid S. Miller end = start + frag_iter->len; 17111da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 17121da177e4SLinus Torvalds if (copy > len) 17131da177e4SLinus Torvalds copy = len; 1714fbb398a8SDavid S. Miller if (skb_copy_bits(frag_iter, offset - start, to, copy)) 17151da177e4SLinus Torvalds goto fault; 17161da177e4SLinus Torvalds if ((len -= copy) == 0) 17171da177e4SLinus Torvalds return 0; 17181da177e4SLinus Torvalds offset += copy; 17191da177e4SLinus Torvalds to += copy; 17201da177e4SLinus Torvalds } 17211a028e50SDavid S. Miller start = end; 17221da177e4SLinus Torvalds } 1723a6686f2fSShirley Ma 17241da177e4SLinus Torvalds if (!len) 17251da177e4SLinus Torvalds return 0; 17261da177e4SLinus Torvalds 17271da177e4SLinus Torvalds fault: 17281da177e4SLinus Torvalds return -EFAULT; 17291da177e4SLinus Torvalds } 1730b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_bits); 17311da177e4SLinus Torvalds 17329c55e01cSJens Axboe /* 17339c55e01cSJens Axboe * Callback from splice_to_pipe(), if we need to release some pages 17349c55e01cSJens Axboe * at the end of the spd in case we error'ed out in filling the pipe. 17359c55e01cSJens Axboe */ 17369c55e01cSJens Axboe static void sock_spd_release(struct splice_pipe_desc *spd, unsigned int i) 17379c55e01cSJens Axboe { 17388b9d3728SJarek Poplawski put_page(spd->pages[i]); 17398b9d3728SJarek Poplawski } 17409c55e01cSJens Axboe 1741a108d5f3SDavid S. Miller static struct page *linear_to_page(struct page *page, unsigned int *len, 17424fb66994SJarek Poplawski unsigned int *offset, 174318aafc62SEric Dumazet struct sock *sk) 17448b9d3728SJarek Poplawski { 17455640f768SEric Dumazet struct page_frag *pfrag = sk_page_frag(sk); 17468b9d3728SJarek Poplawski 17475640f768SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 17488b9d3728SJarek Poplawski return NULL; 17494fb66994SJarek Poplawski 17505640f768SEric Dumazet *len = min_t(unsigned int, *len, pfrag->size - pfrag->offset); 17514fb66994SJarek Poplawski 17525640f768SEric Dumazet memcpy(page_address(pfrag->page) + pfrag->offset, 17535640f768SEric Dumazet page_address(page) + *offset, *len); 17545640f768SEric Dumazet *offset = pfrag->offset; 17555640f768SEric Dumazet pfrag->offset += *len; 17564fb66994SJarek Poplawski 17575640f768SEric Dumazet return pfrag->page; 17589c55e01cSJens Axboe } 17599c55e01cSJens Axboe 176041c73a0dSEric Dumazet static bool spd_can_coalesce(const struct splice_pipe_desc *spd, 176141c73a0dSEric Dumazet struct page *page, 176241c73a0dSEric Dumazet unsigned int offset) 176341c73a0dSEric Dumazet { 176441c73a0dSEric Dumazet return spd->nr_pages && 176541c73a0dSEric Dumazet spd->pages[spd->nr_pages - 1] == page && 176641c73a0dSEric Dumazet (spd->partial[spd->nr_pages - 1].offset + 176741c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len == offset); 176841c73a0dSEric Dumazet } 176941c73a0dSEric Dumazet 17709c55e01cSJens Axboe /* 17719c55e01cSJens Axboe * Fill page/offset/length into spd, if it can hold more pages. 17729c55e01cSJens Axboe */ 1773a108d5f3SDavid S. Miller static bool spd_fill_page(struct splice_pipe_desc *spd, 177435f3d14dSJens Axboe struct pipe_inode_info *pipe, struct page *page, 17754fb66994SJarek Poplawski unsigned int *len, unsigned int offset, 177618aafc62SEric Dumazet bool linear, 17777a67e56fSJarek Poplawski struct sock *sk) 17789c55e01cSJens Axboe { 177941c73a0dSEric Dumazet if (unlikely(spd->nr_pages == MAX_SKB_FRAGS)) 1780a108d5f3SDavid S. Miller return true; 17819c55e01cSJens Axboe 17828b9d3728SJarek Poplawski if (linear) { 178318aafc62SEric Dumazet page = linear_to_page(page, len, &offset, sk); 17848b9d3728SJarek Poplawski if (!page) 1785a108d5f3SDavid S. Miller return true; 178641c73a0dSEric Dumazet } 178741c73a0dSEric Dumazet if (spd_can_coalesce(spd, page, offset)) { 178841c73a0dSEric Dumazet spd->partial[spd->nr_pages - 1].len += *len; 1789a108d5f3SDavid S. Miller return false; 179041c73a0dSEric Dumazet } 17918b9d3728SJarek Poplawski get_page(page); 17929c55e01cSJens Axboe spd->pages[spd->nr_pages] = page; 17934fb66994SJarek Poplawski spd->partial[spd->nr_pages].len = *len; 17949c55e01cSJens Axboe spd->partial[spd->nr_pages].offset = offset; 17959c55e01cSJens Axboe spd->nr_pages++; 17968b9d3728SJarek Poplawski 1797a108d5f3SDavid S. Miller return false; 17989c55e01cSJens Axboe } 17999c55e01cSJens Axboe 1800a108d5f3SDavid S. Miller static bool __splice_segment(struct page *page, unsigned int poff, 18012870c43dSOctavian Purdila unsigned int plen, unsigned int *off, 180218aafc62SEric Dumazet unsigned int *len, 1803d7ccf7c0SEric Dumazet struct splice_pipe_desc *spd, bool linear, 180435f3d14dSJens Axboe struct sock *sk, 180535f3d14dSJens Axboe struct pipe_inode_info *pipe) 18069c55e01cSJens Axboe { 18072870c43dSOctavian Purdila if (!*len) 1808a108d5f3SDavid S. Miller return true; 18099c55e01cSJens Axboe 18102870c43dSOctavian Purdila /* skip this segment if already processed */ 18112870c43dSOctavian Purdila if (*off >= plen) { 18122870c43dSOctavian Purdila *off -= plen; 1813a108d5f3SDavid S. Miller return false; 18142870c43dSOctavian Purdila } 18152870c43dSOctavian Purdila 18162870c43dSOctavian Purdila /* ignore any bits we already processed */ 18179ca1b22dSEric Dumazet poff += *off; 18189ca1b22dSEric Dumazet plen -= *off; 18192870c43dSOctavian Purdila *off = 0; 18202870c43dSOctavian Purdila 182118aafc62SEric Dumazet do { 182218aafc62SEric Dumazet unsigned int flen = min(*len, plen); 18232870c43dSOctavian Purdila 182418aafc62SEric Dumazet if (spd_fill_page(spd, pipe, page, &flen, poff, 182518aafc62SEric Dumazet linear, sk)) 1826a108d5f3SDavid S. Miller return true; 182718aafc62SEric Dumazet poff += flen; 182818aafc62SEric Dumazet plen -= flen; 18292870c43dSOctavian Purdila *len -= flen; 183018aafc62SEric Dumazet } while (*len && plen); 18312870c43dSOctavian Purdila 1832a108d5f3SDavid S. Miller return false; 1833db43a282SOctavian Purdila } 18349c55e01cSJens Axboe 18359c55e01cSJens Axboe /* 1836a108d5f3SDavid S. Miller * Map linear and fragment data from the skb to spd. It reports true if the 18372870c43dSOctavian Purdila * pipe is full or if we already spliced the requested length. 18389c55e01cSJens Axboe */ 1839a108d5f3SDavid S. Miller static bool __skb_splice_bits(struct sk_buff *skb, struct pipe_inode_info *pipe, 184035f3d14dSJens Axboe unsigned int *offset, unsigned int *len, 184135f3d14dSJens Axboe struct splice_pipe_desc *spd, struct sock *sk) 18422870c43dSOctavian Purdila { 18432870c43dSOctavian Purdila int seg; 18449c55e01cSJens Axboe 18451d0c0b32SEric Dumazet /* map the linear part : 18462996d31fSAlexander Duyck * If skb->head_frag is set, this 'linear' part is backed by a 18472996d31fSAlexander Duyck * fragment, and if the head is not shared with any clones then 18482996d31fSAlexander Duyck * we can avoid a copy since we own the head portion of this page. 18499c55e01cSJens Axboe */ 18502870c43dSOctavian Purdila if (__splice_segment(virt_to_page(skb->data), 18512870c43dSOctavian Purdila (unsigned long) skb->data & (PAGE_SIZE - 1), 18522870c43dSOctavian Purdila skb_headlen(skb), 185318aafc62SEric Dumazet offset, len, spd, 18543a7c1ee4SAlexander Duyck skb_head_is_locked(skb), 18551d0c0b32SEric Dumazet sk, pipe)) 1856a108d5f3SDavid S. Miller return true; 18579c55e01cSJens Axboe 18589c55e01cSJens Axboe /* 18599c55e01cSJens Axboe * then map the fragments 18609c55e01cSJens Axboe */ 18619c55e01cSJens Axboe for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) { 18629c55e01cSJens Axboe const skb_frag_t *f = &skb_shinfo(skb)->frags[seg]; 18639c55e01cSJens Axboe 1864ea2ab693SIan Campbell if (__splice_segment(skb_frag_page(f), 18659e903e08SEric Dumazet f->page_offset, skb_frag_size(f), 186618aafc62SEric Dumazet offset, len, spd, false, sk, pipe)) 1867a108d5f3SDavid S. Miller return true; 18689c55e01cSJens Axboe } 18699c55e01cSJens Axboe 1870a108d5f3SDavid S. Miller return false; 18719c55e01cSJens Axboe } 18729c55e01cSJens Axboe 1873a60e3cc7SHannes Frederic Sowa ssize_t skb_socket_splice(struct sock *sk, 1874a60e3cc7SHannes Frederic Sowa struct pipe_inode_info *pipe, 1875a60e3cc7SHannes Frederic Sowa struct splice_pipe_desc *spd) 1876a60e3cc7SHannes Frederic Sowa { 1877a60e3cc7SHannes Frederic Sowa int ret; 1878a60e3cc7SHannes Frederic Sowa 1879a60e3cc7SHannes Frederic Sowa /* Drop the socket lock, otherwise we have reverse 1880a60e3cc7SHannes Frederic Sowa * locking dependencies between sk_lock and i_mutex 1881a60e3cc7SHannes Frederic Sowa * here as compared to sendfile(). We enter here 1882a60e3cc7SHannes Frederic Sowa * with the socket lock held, and splice_to_pipe() will 1883a60e3cc7SHannes Frederic Sowa * grab the pipe inode lock. For sendfile() emulation, 1884a60e3cc7SHannes Frederic Sowa * we call into ->sendpage() with the i_mutex lock held 1885a60e3cc7SHannes Frederic Sowa * and networking will grab the socket lock. 1886a60e3cc7SHannes Frederic Sowa */ 1887a60e3cc7SHannes Frederic Sowa release_sock(sk); 1888a60e3cc7SHannes Frederic Sowa ret = splice_to_pipe(pipe, spd); 1889a60e3cc7SHannes Frederic Sowa lock_sock(sk); 1890a60e3cc7SHannes Frederic Sowa 1891a60e3cc7SHannes Frederic Sowa return ret; 1892a60e3cc7SHannes Frederic Sowa } 1893a60e3cc7SHannes Frederic Sowa 18949c55e01cSJens Axboe /* 18959c55e01cSJens Axboe * Map data from the skb to a pipe. Should handle both the linear part, 18969c55e01cSJens Axboe * the fragments, and the frag list. It does NOT handle frag lists within 18979c55e01cSJens Axboe * the frag list, if such a thing exists. We'd probably need to recurse to 18989c55e01cSJens Axboe * handle that cleanly. 18999c55e01cSJens Axboe */ 1900a60e3cc7SHannes Frederic Sowa int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset, 19019c55e01cSJens Axboe struct pipe_inode_info *pipe, unsigned int tlen, 1902a60e3cc7SHannes Frederic Sowa unsigned int flags, 1903a60e3cc7SHannes Frederic Sowa ssize_t (*splice_cb)(struct sock *, 1904a60e3cc7SHannes Frederic Sowa struct pipe_inode_info *, 1905a60e3cc7SHannes Frederic Sowa struct splice_pipe_desc *)) 19069c55e01cSJens Axboe { 190741c73a0dSEric Dumazet struct partial_page partial[MAX_SKB_FRAGS]; 190841c73a0dSEric Dumazet struct page *pages[MAX_SKB_FRAGS]; 19099c55e01cSJens Axboe struct splice_pipe_desc spd = { 19109c55e01cSJens Axboe .pages = pages, 19119c55e01cSJens Axboe .partial = partial, 1912047fe360SEric Dumazet .nr_pages_max = MAX_SKB_FRAGS, 19139c55e01cSJens Axboe .flags = flags, 191428a625cbSMiklos Szeredi .ops = &nosteal_pipe_buf_ops, 19159c55e01cSJens Axboe .spd_release = sock_spd_release, 19169c55e01cSJens Axboe }; 1917fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 191835f3d14dSJens Axboe int ret = 0; 191935f3d14dSJens Axboe 19209c55e01cSJens Axboe /* 19219c55e01cSJens Axboe * __skb_splice_bits() only fails if the output has no room left, 19229c55e01cSJens Axboe * so no point in going over the frag_list for the error case. 19239c55e01cSJens Axboe */ 192435f3d14dSJens Axboe if (__skb_splice_bits(skb, pipe, &offset, &tlen, &spd, sk)) 19259c55e01cSJens Axboe goto done; 19269c55e01cSJens Axboe else if (!tlen) 19279c55e01cSJens Axboe goto done; 19289c55e01cSJens Axboe 19299c55e01cSJens Axboe /* 19309c55e01cSJens Axboe * now see if we have a frag_list to map 19319c55e01cSJens Axboe */ 1932fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 1933fbb398a8SDavid S. Miller if (!tlen) 19349c55e01cSJens Axboe break; 193535f3d14dSJens Axboe if (__skb_splice_bits(frag_iter, pipe, &offset, &tlen, &spd, sk)) 1936fbb398a8SDavid S. Miller break; 19379c55e01cSJens Axboe } 19389c55e01cSJens Axboe 19399c55e01cSJens Axboe done: 1940a60e3cc7SHannes Frederic Sowa if (spd.nr_pages) 1941a60e3cc7SHannes Frederic Sowa ret = splice_cb(sk, pipe, &spd); 19429c55e01cSJens Axboe 194335f3d14dSJens Axboe return ret; 19449c55e01cSJens Axboe } 19452b514574SHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_splice_bits); 19469c55e01cSJens Axboe 1947357b40a1SHerbert Xu /** 1948357b40a1SHerbert Xu * skb_store_bits - store bits from kernel buffer to skb 1949357b40a1SHerbert Xu * @skb: destination buffer 1950357b40a1SHerbert Xu * @offset: offset in destination 1951357b40a1SHerbert Xu * @from: source buffer 1952357b40a1SHerbert Xu * @len: number of bytes to copy 1953357b40a1SHerbert Xu * 1954357b40a1SHerbert Xu * Copy the specified number of bytes from the source buffer to the 1955357b40a1SHerbert Xu * destination skb. This function handles all the messy bits of 1956357b40a1SHerbert Xu * traversing fragment lists and such. 1957357b40a1SHerbert Xu */ 1958357b40a1SHerbert Xu 19590c6fcc8aSStephen Hemminger int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len) 1960357b40a1SHerbert Xu { 19611a028e50SDavid S. Miller int start = skb_headlen(skb); 1962fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 1963fbb398a8SDavid S. Miller int i, copy; 1964357b40a1SHerbert Xu 1965357b40a1SHerbert Xu if (offset > (int)skb->len - len) 1966357b40a1SHerbert Xu goto fault; 1967357b40a1SHerbert Xu 19681a028e50SDavid S. Miller if ((copy = start - offset) > 0) { 1969357b40a1SHerbert Xu if (copy > len) 1970357b40a1SHerbert Xu copy = len; 197127d7ff46SArnaldo Carvalho de Melo skb_copy_to_linear_data_offset(skb, offset, from, copy); 1972357b40a1SHerbert Xu if ((len -= copy) == 0) 1973357b40a1SHerbert Xu return 0; 1974357b40a1SHerbert Xu offset += copy; 1975357b40a1SHerbert Xu from += copy; 1976357b40a1SHerbert Xu } 1977357b40a1SHerbert Xu 1978357b40a1SHerbert Xu for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1979357b40a1SHerbert Xu skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 19801a028e50SDavid S. Miller int end; 1981357b40a1SHerbert Xu 1982547b792cSIlpo Järvinen WARN_ON(start > offset + len); 19831a028e50SDavid S. Miller 19849e903e08SEric Dumazet end = start + skb_frag_size(frag); 1985357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 1986357b40a1SHerbert Xu u8 *vaddr; 1987357b40a1SHerbert Xu 1988357b40a1SHerbert Xu if (copy > len) 1989357b40a1SHerbert Xu copy = len; 1990357b40a1SHerbert Xu 199151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 19921a028e50SDavid S. Miller memcpy(vaddr + frag->page_offset + offset - start, 19931a028e50SDavid S. Miller from, copy); 199451c56b00SEric Dumazet kunmap_atomic(vaddr); 1995357b40a1SHerbert Xu 1996357b40a1SHerbert Xu if ((len -= copy) == 0) 1997357b40a1SHerbert Xu return 0; 1998357b40a1SHerbert Xu offset += copy; 1999357b40a1SHerbert Xu from += copy; 2000357b40a1SHerbert Xu } 20011a028e50SDavid S. Miller start = end; 2002357b40a1SHerbert Xu } 2003357b40a1SHerbert Xu 2004fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20051a028e50SDavid S. Miller int end; 2006357b40a1SHerbert Xu 2007547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20081a028e50SDavid S. Miller 2009fbb398a8SDavid S. Miller end = start + frag_iter->len; 2010357b40a1SHerbert Xu if ((copy = end - offset) > 0) { 2011357b40a1SHerbert Xu if (copy > len) 2012357b40a1SHerbert Xu copy = len; 2013fbb398a8SDavid S. Miller if (skb_store_bits(frag_iter, offset - start, 20141a028e50SDavid S. Miller from, copy)) 2015357b40a1SHerbert Xu goto fault; 2016357b40a1SHerbert Xu if ((len -= copy) == 0) 2017357b40a1SHerbert Xu return 0; 2018357b40a1SHerbert Xu offset += copy; 2019357b40a1SHerbert Xu from += copy; 2020357b40a1SHerbert Xu } 20211a028e50SDavid S. Miller start = end; 2022357b40a1SHerbert Xu } 2023357b40a1SHerbert Xu if (!len) 2024357b40a1SHerbert Xu return 0; 2025357b40a1SHerbert Xu 2026357b40a1SHerbert Xu fault: 2027357b40a1SHerbert Xu return -EFAULT; 2028357b40a1SHerbert Xu } 2029357b40a1SHerbert Xu EXPORT_SYMBOL(skb_store_bits); 2030357b40a1SHerbert Xu 20311da177e4SLinus Torvalds /* Checksum skb data. */ 20322817a336SDaniel Borkmann __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len, 20332817a336SDaniel Borkmann __wsum csum, const struct skb_checksum_ops *ops) 20341da177e4SLinus Torvalds { 20351a028e50SDavid S. Miller int start = skb_headlen(skb); 20361a028e50SDavid S. Miller int i, copy = start - offset; 2037fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 20381da177e4SLinus Torvalds int pos = 0; 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds /* Checksum header. */ 20411da177e4SLinus Torvalds if (copy > 0) { 20421da177e4SLinus Torvalds if (copy > len) 20431da177e4SLinus Torvalds copy = len; 20442817a336SDaniel Borkmann csum = ops->update(skb->data + offset, copy, csum); 20451da177e4SLinus Torvalds if ((len -= copy) == 0) 20461da177e4SLinus Torvalds return csum; 20471da177e4SLinus Torvalds offset += copy; 20481da177e4SLinus Torvalds pos = copy; 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds 20511da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 20521a028e50SDavid S. Miller int end; 205351c56b00SEric Dumazet skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 20541da177e4SLinus Torvalds 2055547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20561a028e50SDavid S. Miller 205751c56b00SEric Dumazet end = start + skb_frag_size(frag); 20581da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 205944bb9363SAl Viro __wsum csum2; 20601da177e4SLinus Torvalds u8 *vaddr; 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds if (copy > len) 20631da177e4SLinus Torvalds copy = len; 206451c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 20652817a336SDaniel Borkmann csum2 = ops->update(vaddr + frag->page_offset + 20661a028e50SDavid S. Miller offset - start, copy, 0); 206751c56b00SEric Dumazet kunmap_atomic(vaddr); 20682817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 20691da177e4SLinus Torvalds if (!(len -= copy)) 20701da177e4SLinus Torvalds return csum; 20711da177e4SLinus Torvalds offset += copy; 20721da177e4SLinus Torvalds pos += copy; 20731da177e4SLinus Torvalds } 20741a028e50SDavid S. Miller start = end; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 2077fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 20781a028e50SDavid S. Miller int end; 20791da177e4SLinus Torvalds 2080547b792cSIlpo Järvinen WARN_ON(start > offset + len); 20811a028e50SDavid S. Miller 2082fbb398a8SDavid S. Miller end = start + frag_iter->len; 20831da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 20845f92a738SAl Viro __wsum csum2; 20851da177e4SLinus Torvalds if (copy > len) 20861da177e4SLinus Torvalds copy = len; 20872817a336SDaniel Borkmann csum2 = __skb_checksum(frag_iter, offset - start, 20882817a336SDaniel Borkmann copy, 0, ops); 20892817a336SDaniel Borkmann csum = ops->combine(csum, csum2, pos, copy); 20901da177e4SLinus Torvalds if ((len -= copy) == 0) 20911da177e4SLinus Torvalds return csum; 20921da177e4SLinus Torvalds offset += copy; 20931da177e4SLinus Torvalds pos += copy; 20941da177e4SLinus Torvalds } 20951a028e50SDavid S. Miller start = end; 20961da177e4SLinus Torvalds } 209709a62660SKris Katterjohn BUG_ON(len); 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds return csum; 21001da177e4SLinus Torvalds } 21012817a336SDaniel Borkmann EXPORT_SYMBOL(__skb_checksum); 21022817a336SDaniel Borkmann 21032817a336SDaniel Borkmann __wsum skb_checksum(const struct sk_buff *skb, int offset, 21042817a336SDaniel Borkmann int len, __wsum csum) 21052817a336SDaniel Borkmann { 21062817a336SDaniel Borkmann const struct skb_checksum_ops ops = { 2107cea80ea8SDaniel Borkmann .update = csum_partial_ext, 21082817a336SDaniel Borkmann .combine = csum_block_add_ext, 21092817a336SDaniel Borkmann }; 21102817a336SDaniel Borkmann 21112817a336SDaniel Borkmann return __skb_checksum(skb, offset, len, csum, &ops); 21122817a336SDaniel Borkmann } 2113b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_checksum); 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds /* Both of above in one bottle. */ 21161da177e4SLinus Torvalds 211781d77662SAl Viro __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, 211881d77662SAl Viro u8 *to, int len, __wsum csum) 21191da177e4SLinus Torvalds { 21201a028e50SDavid S. Miller int start = skb_headlen(skb); 21211a028e50SDavid S. Miller int i, copy = start - offset; 2122fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 21231da177e4SLinus Torvalds int pos = 0; 21241da177e4SLinus Torvalds 21251da177e4SLinus Torvalds /* Copy header. */ 21261da177e4SLinus Torvalds if (copy > 0) { 21271da177e4SLinus Torvalds if (copy > len) 21281da177e4SLinus Torvalds copy = len; 21291da177e4SLinus Torvalds csum = csum_partial_copy_nocheck(skb->data + offset, to, 21301da177e4SLinus Torvalds copy, csum); 21311da177e4SLinus Torvalds if ((len -= copy) == 0) 21321da177e4SLinus Torvalds return csum; 21331da177e4SLinus Torvalds offset += copy; 21341da177e4SLinus Torvalds to += copy; 21351da177e4SLinus Torvalds pos = copy; 21361da177e4SLinus Torvalds } 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 21391a028e50SDavid S. Miller int end; 21401da177e4SLinus Torvalds 2141547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21421a028e50SDavid S. Miller 21439e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 21441da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21455084205fSAl Viro __wsum csum2; 21461da177e4SLinus Torvalds u8 *vaddr; 21471da177e4SLinus Torvalds skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 21481da177e4SLinus Torvalds 21491da177e4SLinus Torvalds if (copy > len) 21501da177e4SLinus Torvalds copy = len; 215151c56b00SEric Dumazet vaddr = kmap_atomic(skb_frag_page(frag)); 21521da177e4SLinus Torvalds csum2 = csum_partial_copy_nocheck(vaddr + 21531a028e50SDavid S. Miller frag->page_offset + 21541a028e50SDavid S. Miller offset - start, to, 21551a028e50SDavid S. Miller copy, 0); 215651c56b00SEric Dumazet kunmap_atomic(vaddr); 21571da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 21581da177e4SLinus Torvalds if (!(len -= copy)) 21591da177e4SLinus Torvalds return csum; 21601da177e4SLinus Torvalds offset += copy; 21611da177e4SLinus Torvalds to += copy; 21621da177e4SLinus Torvalds pos += copy; 21631da177e4SLinus Torvalds } 21641a028e50SDavid S. Miller start = end; 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 2167fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 216881d77662SAl Viro __wsum csum2; 21691a028e50SDavid S. Miller int end; 21701da177e4SLinus Torvalds 2171547b792cSIlpo Järvinen WARN_ON(start > offset + len); 21721a028e50SDavid S. Miller 2173fbb398a8SDavid S. Miller end = start + frag_iter->len; 21741da177e4SLinus Torvalds if ((copy = end - offset) > 0) { 21751da177e4SLinus Torvalds if (copy > len) 21761da177e4SLinus Torvalds copy = len; 2177fbb398a8SDavid S. Miller csum2 = skb_copy_and_csum_bits(frag_iter, 21781a028e50SDavid S. Miller offset - start, 21791da177e4SLinus Torvalds to, copy, 0); 21801da177e4SLinus Torvalds csum = csum_block_add(csum, csum2, pos); 21811da177e4SLinus Torvalds if ((len -= copy) == 0) 21821da177e4SLinus Torvalds return csum; 21831da177e4SLinus Torvalds offset += copy; 21841da177e4SLinus Torvalds to += copy; 21851da177e4SLinus Torvalds pos += copy; 21861da177e4SLinus Torvalds } 21871a028e50SDavid S. Miller start = end; 21881da177e4SLinus Torvalds } 218909a62660SKris Katterjohn BUG_ON(len); 21901da177e4SLinus Torvalds return csum; 21911da177e4SLinus Torvalds } 2192b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_bits); 21931da177e4SLinus Torvalds 2194af2806f8SThomas Graf /** 2195af2806f8SThomas Graf * skb_zerocopy_headlen - Calculate headroom needed for skb_zerocopy() 2196af2806f8SThomas Graf * @from: source buffer 2197af2806f8SThomas Graf * 2198af2806f8SThomas Graf * Calculates the amount of linear headroom needed in the 'to' skb passed 2199af2806f8SThomas Graf * into skb_zerocopy(). 2200af2806f8SThomas Graf */ 2201af2806f8SThomas Graf unsigned int 2202af2806f8SThomas Graf skb_zerocopy_headlen(const struct sk_buff *from) 2203af2806f8SThomas Graf { 2204af2806f8SThomas Graf unsigned int hlen = 0; 2205af2806f8SThomas Graf 2206af2806f8SThomas Graf if (!from->head_frag || 2207af2806f8SThomas Graf skb_headlen(from) < L1_CACHE_BYTES || 2208af2806f8SThomas Graf skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 2209af2806f8SThomas Graf hlen = skb_headlen(from); 2210af2806f8SThomas Graf 2211af2806f8SThomas Graf if (skb_has_frag_list(from)) 2212af2806f8SThomas Graf hlen = from->len; 2213af2806f8SThomas Graf 2214af2806f8SThomas Graf return hlen; 2215af2806f8SThomas Graf } 2216af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy_headlen); 2217af2806f8SThomas Graf 2218af2806f8SThomas Graf /** 2219af2806f8SThomas Graf * skb_zerocopy - Zero copy skb to skb 2220af2806f8SThomas Graf * @to: destination buffer 22217fceb4deSMasanari Iida * @from: source buffer 2222af2806f8SThomas Graf * @len: number of bytes to copy from source buffer 2223af2806f8SThomas Graf * @hlen: size of linear headroom in destination buffer 2224af2806f8SThomas Graf * 2225af2806f8SThomas Graf * Copies up to `len` bytes from `from` to `to` by creating references 2226af2806f8SThomas Graf * to the frags in the source buffer. 2227af2806f8SThomas Graf * 2228af2806f8SThomas Graf * The `hlen` as calculated by skb_zerocopy_headlen() specifies the 2229af2806f8SThomas Graf * headroom in the `to` buffer. 223036d5fe6aSZoltan Kiss * 223136d5fe6aSZoltan Kiss * Return value: 223236d5fe6aSZoltan Kiss * 0: everything is OK 223336d5fe6aSZoltan Kiss * -ENOMEM: couldn't orphan frags of @from due to lack of memory 223436d5fe6aSZoltan Kiss * -EFAULT: skb_copy_bits() found some problem with skb geometry 2235af2806f8SThomas Graf */ 223636d5fe6aSZoltan Kiss int 223736d5fe6aSZoltan Kiss skb_zerocopy(struct sk_buff *to, struct sk_buff *from, int len, int hlen) 2238af2806f8SThomas Graf { 2239af2806f8SThomas Graf int i, j = 0; 2240af2806f8SThomas Graf int plen = 0; /* length of skb->head fragment */ 224136d5fe6aSZoltan Kiss int ret; 2242af2806f8SThomas Graf struct page *page; 2243af2806f8SThomas Graf unsigned int offset; 2244af2806f8SThomas Graf 2245af2806f8SThomas Graf BUG_ON(!from->head_frag && !hlen); 2246af2806f8SThomas Graf 2247af2806f8SThomas Graf /* dont bother with small payloads */ 224836d5fe6aSZoltan Kiss if (len <= skb_tailroom(to)) 224936d5fe6aSZoltan Kiss return skb_copy_bits(from, 0, skb_put(to, len), len); 2250af2806f8SThomas Graf 2251af2806f8SThomas Graf if (hlen) { 225236d5fe6aSZoltan Kiss ret = skb_copy_bits(from, 0, skb_put(to, hlen), hlen); 225336d5fe6aSZoltan Kiss if (unlikely(ret)) 225436d5fe6aSZoltan Kiss return ret; 2255af2806f8SThomas Graf len -= hlen; 2256af2806f8SThomas Graf } else { 2257af2806f8SThomas Graf plen = min_t(int, skb_headlen(from), len); 2258af2806f8SThomas Graf if (plen) { 2259af2806f8SThomas Graf page = virt_to_head_page(from->head); 2260af2806f8SThomas Graf offset = from->data - (unsigned char *)page_address(page); 2261af2806f8SThomas Graf __skb_fill_page_desc(to, 0, page, offset, plen); 2262af2806f8SThomas Graf get_page(page); 2263af2806f8SThomas Graf j = 1; 2264af2806f8SThomas Graf len -= plen; 2265af2806f8SThomas Graf } 2266af2806f8SThomas Graf } 2267af2806f8SThomas Graf 2268af2806f8SThomas Graf to->truesize += len + plen; 2269af2806f8SThomas Graf to->len += len + plen; 2270af2806f8SThomas Graf to->data_len += len + plen; 2271af2806f8SThomas Graf 227236d5fe6aSZoltan Kiss if (unlikely(skb_orphan_frags(from, GFP_ATOMIC))) { 227336d5fe6aSZoltan Kiss skb_tx_error(from); 227436d5fe6aSZoltan Kiss return -ENOMEM; 227536d5fe6aSZoltan Kiss } 227636d5fe6aSZoltan Kiss 2277af2806f8SThomas Graf for (i = 0; i < skb_shinfo(from)->nr_frags; i++) { 2278af2806f8SThomas Graf if (!len) 2279af2806f8SThomas Graf break; 2280af2806f8SThomas Graf skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i]; 2281af2806f8SThomas Graf skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len); 2282af2806f8SThomas Graf len -= skb_shinfo(to)->frags[j].size; 2283af2806f8SThomas Graf skb_frag_ref(to, j); 2284af2806f8SThomas Graf j++; 2285af2806f8SThomas Graf } 2286af2806f8SThomas Graf skb_shinfo(to)->nr_frags = j; 228736d5fe6aSZoltan Kiss 228836d5fe6aSZoltan Kiss return 0; 2289af2806f8SThomas Graf } 2290af2806f8SThomas Graf EXPORT_SYMBOL_GPL(skb_zerocopy); 2291af2806f8SThomas Graf 22921da177e4SLinus Torvalds void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to) 22931da177e4SLinus Torvalds { 2294d3bc23e7SAl Viro __wsum csum; 22951da177e4SLinus Torvalds long csstart; 22961da177e4SLinus Torvalds 229784fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) 229855508d60SMichał Mirosław csstart = skb_checksum_start_offset(skb); 22991da177e4SLinus Torvalds else 23001da177e4SLinus Torvalds csstart = skb_headlen(skb); 23011da177e4SLinus Torvalds 230209a62660SKris Katterjohn BUG_ON(csstart > skb_headlen(skb)); 23031da177e4SLinus Torvalds 2304d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, to, csstart); 23051da177e4SLinus Torvalds 23061da177e4SLinus Torvalds csum = 0; 23071da177e4SLinus Torvalds if (csstart != skb->len) 23081da177e4SLinus Torvalds csum = skb_copy_and_csum_bits(skb, csstart, to + csstart, 23091da177e4SLinus Torvalds skb->len - csstart, 0); 23101da177e4SLinus Torvalds 231184fa7933SPatrick McHardy if (skb->ip_summed == CHECKSUM_PARTIAL) { 2312ff1dcadbSAl Viro long csstuff = csstart + skb->csum_offset; 23131da177e4SLinus Torvalds 2314d3bc23e7SAl Viro *((__sum16 *)(to + csstuff)) = csum_fold(csum); 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds } 2317b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_copy_and_csum_dev); 23181da177e4SLinus Torvalds 23191da177e4SLinus Torvalds /** 23201da177e4SLinus Torvalds * skb_dequeue - remove from the head of the queue 23211da177e4SLinus Torvalds * @list: list to dequeue from 23221da177e4SLinus Torvalds * 23231da177e4SLinus Torvalds * Remove the head of the list. The list lock is taken so the function 23241da177e4SLinus Torvalds * may be used safely with other locking list functions. The head item is 23251da177e4SLinus Torvalds * returned or %NULL if the list is empty. 23261da177e4SLinus Torvalds */ 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds struct sk_buff *skb_dequeue(struct sk_buff_head *list) 23291da177e4SLinus Torvalds { 23301da177e4SLinus Torvalds unsigned long flags; 23311da177e4SLinus Torvalds struct sk_buff *result; 23321da177e4SLinus Torvalds 23331da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23341da177e4SLinus Torvalds result = __skb_dequeue(list); 23351da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23361da177e4SLinus Torvalds return result; 23371da177e4SLinus Torvalds } 2338b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue); 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds /** 23411da177e4SLinus Torvalds * skb_dequeue_tail - remove from the tail of the queue 23421da177e4SLinus Torvalds * @list: list to dequeue from 23431da177e4SLinus Torvalds * 23441da177e4SLinus Torvalds * Remove the tail of the list. The list lock is taken so the function 23451da177e4SLinus Torvalds * may be used safely with other locking list functions. The tail item is 23461da177e4SLinus Torvalds * returned or %NULL if the list is empty. 23471da177e4SLinus Torvalds */ 23481da177e4SLinus Torvalds struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list) 23491da177e4SLinus Torvalds { 23501da177e4SLinus Torvalds unsigned long flags; 23511da177e4SLinus Torvalds struct sk_buff *result; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23541da177e4SLinus Torvalds result = __skb_dequeue_tail(list); 23551da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23561da177e4SLinus Torvalds return result; 23571da177e4SLinus Torvalds } 2358b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_dequeue_tail); 23591da177e4SLinus Torvalds 23601da177e4SLinus Torvalds /** 23611da177e4SLinus Torvalds * skb_queue_purge - empty a list 23621da177e4SLinus Torvalds * @list: list to empty 23631da177e4SLinus Torvalds * 23641da177e4SLinus Torvalds * Delete all buffers on an &sk_buff list. Each buffer is removed from 23651da177e4SLinus Torvalds * the list and one reference dropped. This function takes the list 23661da177e4SLinus Torvalds * lock and is atomic with respect to other list locking functions. 23671da177e4SLinus Torvalds */ 23681da177e4SLinus Torvalds void skb_queue_purge(struct sk_buff_head *list) 23691da177e4SLinus Torvalds { 23701da177e4SLinus Torvalds struct sk_buff *skb; 23711da177e4SLinus Torvalds while ((skb = skb_dequeue(list)) != NULL) 23721da177e4SLinus Torvalds kfree_skb(skb); 23731da177e4SLinus Torvalds } 2374b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_purge); 23751da177e4SLinus Torvalds 23761da177e4SLinus Torvalds /** 23771da177e4SLinus Torvalds * skb_queue_head - queue a buffer at the list head 23781da177e4SLinus Torvalds * @list: list to use 23791da177e4SLinus Torvalds * @newsk: buffer to queue 23801da177e4SLinus Torvalds * 23811da177e4SLinus Torvalds * Queue a buffer at the start of the list. This function takes the 23821da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 23831da177e4SLinus Torvalds * safely. 23841da177e4SLinus Torvalds * 23851da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 23861da177e4SLinus Torvalds */ 23871da177e4SLinus Torvalds void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk) 23881da177e4SLinus Torvalds { 23891da177e4SLinus Torvalds unsigned long flags; 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 23921da177e4SLinus Torvalds __skb_queue_head(list, newsk); 23931da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 23941da177e4SLinus Torvalds } 2395b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_head); 23961da177e4SLinus Torvalds 23971da177e4SLinus Torvalds /** 23981da177e4SLinus Torvalds * skb_queue_tail - queue a buffer at the list tail 23991da177e4SLinus Torvalds * @list: list to use 24001da177e4SLinus Torvalds * @newsk: buffer to queue 24011da177e4SLinus Torvalds * 24021da177e4SLinus Torvalds * Queue a buffer at the tail of the list. This function takes the 24031da177e4SLinus Torvalds * list lock and can be used safely with other locking &sk_buff functions 24041da177e4SLinus Torvalds * safely. 24051da177e4SLinus Torvalds * 24061da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24071da177e4SLinus Torvalds */ 24081da177e4SLinus Torvalds void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk) 24091da177e4SLinus Torvalds { 24101da177e4SLinus Torvalds unsigned long flags; 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24131da177e4SLinus Torvalds __skb_queue_tail(list, newsk); 24141da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24151da177e4SLinus Torvalds } 2416b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_queue_tail); 24178728b834SDavid S. Miller 24181da177e4SLinus Torvalds /** 24191da177e4SLinus Torvalds * skb_unlink - remove a buffer from a list 24201da177e4SLinus Torvalds * @skb: buffer to remove 24218728b834SDavid S. Miller * @list: list to use 24221da177e4SLinus Torvalds * 24238728b834SDavid S. Miller * Remove a packet from a list. The list locks are taken and this 24248728b834SDavid S. Miller * function is atomic with respect to other list locked calls 24251da177e4SLinus Torvalds * 24268728b834SDavid S. Miller * You must know what list the SKB is on. 24271da177e4SLinus Torvalds */ 24288728b834SDavid S. Miller void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) 24291da177e4SLinus Torvalds { 24301da177e4SLinus Torvalds unsigned long flags; 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds spin_lock_irqsave(&list->lock, flags); 24338728b834SDavid S. Miller __skb_unlink(skb, list); 24341da177e4SLinus Torvalds spin_unlock_irqrestore(&list->lock, flags); 24351da177e4SLinus Torvalds } 2436b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_unlink); 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds /** 24391da177e4SLinus Torvalds * skb_append - append a buffer 24401da177e4SLinus Torvalds * @old: buffer to insert after 24411da177e4SLinus Torvalds * @newsk: buffer to insert 24428728b834SDavid S. Miller * @list: list to use 24431da177e4SLinus Torvalds * 24441da177e4SLinus Torvalds * Place a packet after a given packet in a list. The list locks are taken 24451da177e4SLinus Torvalds * and this function is atomic with respect to other list locked calls. 24461da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24471da177e4SLinus Torvalds */ 24488728b834SDavid S. Miller void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 24491da177e4SLinus Torvalds { 24501da177e4SLinus Torvalds unsigned long flags; 24511da177e4SLinus Torvalds 24528728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 24537de6c033SGerrit Renker __skb_queue_after(list, old, newsk); 24548728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 24551da177e4SLinus Torvalds } 2456b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append); 24571da177e4SLinus Torvalds 24581da177e4SLinus Torvalds /** 24591da177e4SLinus Torvalds * skb_insert - insert a buffer 24601da177e4SLinus Torvalds * @old: buffer to insert before 24611da177e4SLinus Torvalds * @newsk: buffer to insert 24628728b834SDavid S. Miller * @list: list to use 24631da177e4SLinus Torvalds * 24648728b834SDavid S. Miller * Place a packet before a given packet in a list. The list locks are 24658728b834SDavid S. Miller * taken and this function is atomic with respect to other list locked 24668728b834SDavid S. Miller * calls. 24678728b834SDavid S. Miller * 24681da177e4SLinus Torvalds * A buffer cannot be placed on two lists at the same time. 24691da177e4SLinus Torvalds */ 24708728b834SDavid S. Miller void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list) 24711da177e4SLinus Torvalds { 24721da177e4SLinus Torvalds unsigned long flags; 24731da177e4SLinus Torvalds 24748728b834SDavid S. Miller spin_lock_irqsave(&list->lock, flags); 24758728b834SDavid S. Miller __skb_insert(newsk, old->prev, old, list); 24768728b834SDavid S. Miller spin_unlock_irqrestore(&list->lock, flags); 24771da177e4SLinus Torvalds } 2478b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_insert); 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds static inline void skb_split_inside_header(struct sk_buff *skb, 24811da177e4SLinus Torvalds struct sk_buff* skb1, 24821da177e4SLinus Torvalds const u32 len, const int pos) 24831da177e4SLinus Torvalds { 24841da177e4SLinus Torvalds int i; 24851da177e4SLinus Torvalds 2486d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data_offset(skb, len, skb_put(skb1, pos - len), 2487d626f62bSArnaldo Carvalho de Melo pos - len); 24881da177e4SLinus Torvalds /* And move data appendix as is. */ 24891da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) 24901da177e4SLinus Torvalds skb_shinfo(skb1)->frags[i] = skb_shinfo(skb)->frags[i]; 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = skb_shinfo(skb)->nr_frags; 24931da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 24941da177e4SLinus Torvalds skb1->data_len = skb->data_len; 24951da177e4SLinus Torvalds skb1->len += skb1->data_len; 24961da177e4SLinus Torvalds skb->data_len = 0; 24971da177e4SLinus Torvalds skb->len = len; 249827a884dcSArnaldo Carvalho de Melo skb_set_tail_pointer(skb, len); 24991da177e4SLinus Torvalds } 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds static inline void skb_split_no_header(struct sk_buff *skb, 25021da177e4SLinus Torvalds struct sk_buff* skb1, 25031da177e4SLinus Torvalds const u32 len, int pos) 25041da177e4SLinus Torvalds { 25051da177e4SLinus Torvalds int i, k = 0; 25061da177e4SLinus Torvalds const int nfrags = skb_shinfo(skb)->nr_frags; 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags = 0; 25091da177e4SLinus Torvalds skb1->len = skb1->data_len = skb->len - len; 25101da177e4SLinus Torvalds skb->len = len; 25111da177e4SLinus Torvalds skb->data_len = len - pos; 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds for (i = 0; i < nfrags; i++) { 25149e903e08SEric Dumazet int size = skb_frag_size(&skb_shinfo(skb)->frags[i]); 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds if (pos + size > len) { 25171da177e4SLinus Torvalds skb_shinfo(skb1)->frags[k] = skb_shinfo(skb)->frags[i]; 25181da177e4SLinus Torvalds 25191da177e4SLinus Torvalds if (pos < len) { 25201da177e4SLinus Torvalds /* Split frag. 25211da177e4SLinus Torvalds * We have two variants in this case: 25221da177e4SLinus Torvalds * 1. Move all the frag to the second 25231da177e4SLinus Torvalds * part, if it is possible. F.e. 25241da177e4SLinus Torvalds * this approach is mandatory for TUX, 25251da177e4SLinus Torvalds * where splitting is expensive. 25261da177e4SLinus Torvalds * 2. Split is accurately. We make this. 25271da177e4SLinus Torvalds */ 2528ea2ab693SIan Campbell skb_frag_ref(skb, i); 25291da177e4SLinus Torvalds skb_shinfo(skb1)->frags[0].page_offset += len - pos; 25309e903e08SEric Dumazet skb_frag_size_sub(&skb_shinfo(skb1)->frags[0], len - pos); 25319e903e08SEric Dumazet skb_frag_size_set(&skb_shinfo(skb)->frags[i], len - pos); 25321da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 25331da177e4SLinus Torvalds } 25341da177e4SLinus Torvalds k++; 25351da177e4SLinus Torvalds } else 25361da177e4SLinus Torvalds skb_shinfo(skb)->nr_frags++; 25371da177e4SLinus Torvalds pos += size; 25381da177e4SLinus Torvalds } 25391da177e4SLinus Torvalds skb_shinfo(skb1)->nr_frags = k; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds /** 25431da177e4SLinus Torvalds * skb_split - Split fragmented skb to two parts at length len. 25441da177e4SLinus Torvalds * @skb: the buffer to split 25451da177e4SLinus Torvalds * @skb1: the buffer to receive the second part 25461da177e4SLinus Torvalds * @len: new length for skb 25471da177e4SLinus Torvalds */ 25481da177e4SLinus Torvalds void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len) 25491da177e4SLinus Torvalds { 25501da177e4SLinus Torvalds int pos = skb_headlen(skb); 25511da177e4SLinus Torvalds 255268534c68SAmerigo Wang skb_shinfo(skb1)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 25531da177e4SLinus Torvalds if (len < pos) /* Split line is inside header. */ 25541da177e4SLinus Torvalds skb_split_inside_header(skb, skb1, len, pos); 25551da177e4SLinus Torvalds else /* Second chunk has no header, nothing to copy. */ 25561da177e4SLinus Torvalds skb_split_no_header(skb, skb1, len, pos); 25571da177e4SLinus Torvalds } 2558b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_split); 25591da177e4SLinus Torvalds 25609f782db3SIlpo Järvinen /* Shifting from/to a cloned skb is a no-go. 25619f782db3SIlpo Järvinen * 25629f782db3SIlpo Järvinen * Caller cannot keep skb_shinfo related pointers past calling here! 25639f782db3SIlpo Järvinen */ 2564832d11c5SIlpo Järvinen static int skb_prepare_for_shift(struct sk_buff *skb) 2565832d11c5SIlpo Järvinen { 25660ace2856SIlpo Järvinen return skb_cloned(skb) && pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 2567832d11c5SIlpo Järvinen } 2568832d11c5SIlpo Järvinen 2569832d11c5SIlpo Järvinen /** 2570832d11c5SIlpo Järvinen * skb_shift - Shifts paged data partially from skb to another 2571832d11c5SIlpo Järvinen * @tgt: buffer into which tail data gets added 2572832d11c5SIlpo Järvinen * @skb: buffer from which the paged data comes from 2573832d11c5SIlpo Järvinen * @shiftlen: shift up to this many bytes 2574832d11c5SIlpo Järvinen * 2575832d11c5SIlpo Järvinen * Attempts to shift up to shiftlen worth of bytes, which may be less than 257620e994a0SFeng King * the length of the skb, from skb to tgt. Returns number bytes shifted. 2577832d11c5SIlpo Järvinen * It's up to caller to free skb if everything was shifted. 2578832d11c5SIlpo Järvinen * 2579832d11c5SIlpo Järvinen * If @tgt runs out of frags, the whole operation is aborted. 2580832d11c5SIlpo Järvinen * 2581832d11c5SIlpo Järvinen * Skb cannot include anything else but paged data while tgt is allowed 2582832d11c5SIlpo Järvinen * to have non-paged data as well. 2583832d11c5SIlpo Järvinen * 2584832d11c5SIlpo Järvinen * TODO: full sized shift could be optimized but that would need 2585832d11c5SIlpo Järvinen * specialized skb free'er to handle frags without up-to-date nr_frags. 2586832d11c5SIlpo Järvinen */ 2587832d11c5SIlpo Järvinen int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen) 2588832d11c5SIlpo Järvinen { 2589832d11c5SIlpo Järvinen int from, to, merge, todo; 2590832d11c5SIlpo Järvinen struct skb_frag_struct *fragfrom, *fragto; 2591832d11c5SIlpo Järvinen 2592832d11c5SIlpo Järvinen BUG_ON(shiftlen > skb->len); 2593832d11c5SIlpo Järvinen BUG_ON(skb_headlen(skb)); /* Would corrupt stream */ 2594832d11c5SIlpo Järvinen 2595832d11c5SIlpo Järvinen todo = shiftlen; 2596832d11c5SIlpo Järvinen from = 0; 2597832d11c5SIlpo Järvinen to = skb_shinfo(tgt)->nr_frags; 2598832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2599832d11c5SIlpo Järvinen 2600832d11c5SIlpo Järvinen /* Actual merge is delayed until the point when we know we can 2601832d11c5SIlpo Järvinen * commit all, so that we don't have to undo partial changes 2602832d11c5SIlpo Järvinen */ 2603832d11c5SIlpo Järvinen if (!to || 2604ea2ab693SIan Campbell !skb_can_coalesce(tgt, to, skb_frag_page(fragfrom), 2605ea2ab693SIan Campbell fragfrom->page_offset)) { 2606832d11c5SIlpo Järvinen merge = -1; 2607832d11c5SIlpo Järvinen } else { 2608832d11c5SIlpo Järvinen merge = to - 1; 2609832d11c5SIlpo Järvinen 26109e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2611832d11c5SIlpo Järvinen if (todo < 0) { 2612832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || 2613832d11c5SIlpo Järvinen skb_prepare_for_shift(tgt)) 2614832d11c5SIlpo Järvinen return 0; 2615832d11c5SIlpo Järvinen 26169f782db3SIlpo Järvinen /* All previous frag pointers might be stale! */ 26179f782db3SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2618832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2619832d11c5SIlpo Järvinen 26209e903e08SEric Dumazet skb_frag_size_add(fragto, shiftlen); 26219e903e08SEric Dumazet skb_frag_size_sub(fragfrom, shiftlen); 2622832d11c5SIlpo Järvinen fragfrom->page_offset += shiftlen; 2623832d11c5SIlpo Järvinen 2624832d11c5SIlpo Järvinen goto onlymerged; 2625832d11c5SIlpo Järvinen } 2626832d11c5SIlpo Järvinen 2627832d11c5SIlpo Järvinen from++; 2628832d11c5SIlpo Järvinen } 2629832d11c5SIlpo Järvinen 2630832d11c5SIlpo Järvinen /* Skip full, not-fitting skb to avoid expensive operations */ 2631832d11c5SIlpo Järvinen if ((shiftlen == skb->len) && 2632832d11c5SIlpo Järvinen (skb_shinfo(skb)->nr_frags - from) > (MAX_SKB_FRAGS - to)) 2633832d11c5SIlpo Järvinen return 0; 2634832d11c5SIlpo Järvinen 2635832d11c5SIlpo Järvinen if (skb_prepare_for_shift(skb) || skb_prepare_for_shift(tgt)) 2636832d11c5SIlpo Järvinen return 0; 2637832d11c5SIlpo Järvinen 2638832d11c5SIlpo Järvinen while ((todo > 0) && (from < skb_shinfo(skb)->nr_frags)) { 2639832d11c5SIlpo Järvinen if (to == MAX_SKB_FRAGS) 2640832d11c5SIlpo Järvinen return 0; 2641832d11c5SIlpo Järvinen 2642832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[from]; 2643832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[to]; 2644832d11c5SIlpo Järvinen 26459e903e08SEric Dumazet if (todo >= skb_frag_size(fragfrom)) { 2646832d11c5SIlpo Järvinen *fragto = *fragfrom; 26479e903e08SEric Dumazet todo -= skb_frag_size(fragfrom); 2648832d11c5SIlpo Järvinen from++; 2649832d11c5SIlpo Järvinen to++; 2650832d11c5SIlpo Järvinen 2651832d11c5SIlpo Järvinen } else { 2652ea2ab693SIan Campbell __skb_frag_ref(fragfrom); 2653832d11c5SIlpo Järvinen fragto->page = fragfrom->page; 2654832d11c5SIlpo Järvinen fragto->page_offset = fragfrom->page_offset; 26559e903e08SEric Dumazet skb_frag_size_set(fragto, todo); 2656832d11c5SIlpo Järvinen 2657832d11c5SIlpo Järvinen fragfrom->page_offset += todo; 26589e903e08SEric Dumazet skb_frag_size_sub(fragfrom, todo); 2659832d11c5SIlpo Järvinen todo = 0; 2660832d11c5SIlpo Järvinen 2661832d11c5SIlpo Järvinen to++; 2662832d11c5SIlpo Järvinen break; 2663832d11c5SIlpo Järvinen } 2664832d11c5SIlpo Järvinen } 2665832d11c5SIlpo Järvinen 2666832d11c5SIlpo Järvinen /* Ready to "commit" this state change to tgt */ 2667832d11c5SIlpo Järvinen skb_shinfo(tgt)->nr_frags = to; 2668832d11c5SIlpo Järvinen 2669832d11c5SIlpo Järvinen if (merge >= 0) { 2670832d11c5SIlpo Järvinen fragfrom = &skb_shinfo(skb)->frags[0]; 2671832d11c5SIlpo Järvinen fragto = &skb_shinfo(tgt)->frags[merge]; 2672832d11c5SIlpo Järvinen 26739e903e08SEric Dumazet skb_frag_size_add(fragto, skb_frag_size(fragfrom)); 2674ea2ab693SIan Campbell __skb_frag_unref(fragfrom); 2675832d11c5SIlpo Järvinen } 2676832d11c5SIlpo Järvinen 2677832d11c5SIlpo Järvinen /* Reposition in the original skb */ 2678832d11c5SIlpo Järvinen to = 0; 2679832d11c5SIlpo Järvinen while (from < skb_shinfo(skb)->nr_frags) 2680832d11c5SIlpo Järvinen skb_shinfo(skb)->frags[to++] = skb_shinfo(skb)->frags[from++]; 2681832d11c5SIlpo Järvinen skb_shinfo(skb)->nr_frags = to; 2682832d11c5SIlpo Järvinen 2683832d11c5SIlpo Järvinen BUG_ON(todo > 0 && !skb_shinfo(skb)->nr_frags); 2684832d11c5SIlpo Järvinen 2685832d11c5SIlpo Järvinen onlymerged: 2686832d11c5SIlpo Järvinen /* Most likely the tgt won't ever need its checksum anymore, skb on 2687832d11c5SIlpo Järvinen * the other hand might need it if it needs to be resent 2688832d11c5SIlpo Järvinen */ 2689832d11c5SIlpo Järvinen tgt->ip_summed = CHECKSUM_PARTIAL; 2690832d11c5SIlpo Järvinen skb->ip_summed = CHECKSUM_PARTIAL; 2691832d11c5SIlpo Järvinen 2692832d11c5SIlpo Järvinen /* Yak, is it really working this way? Some helper please? */ 2693832d11c5SIlpo Järvinen skb->len -= shiftlen; 2694832d11c5SIlpo Järvinen skb->data_len -= shiftlen; 2695832d11c5SIlpo Järvinen skb->truesize -= shiftlen; 2696832d11c5SIlpo Järvinen tgt->len += shiftlen; 2697832d11c5SIlpo Järvinen tgt->data_len += shiftlen; 2698832d11c5SIlpo Järvinen tgt->truesize += shiftlen; 2699832d11c5SIlpo Järvinen 2700832d11c5SIlpo Järvinen return shiftlen; 2701832d11c5SIlpo Järvinen } 2702832d11c5SIlpo Järvinen 2703677e90edSThomas Graf /** 2704677e90edSThomas Graf * skb_prepare_seq_read - Prepare a sequential read of skb data 2705677e90edSThomas Graf * @skb: the buffer to read 2706677e90edSThomas Graf * @from: lower offset of data to be read 2707677e90edSThomas Graf * @to: upper offset of data to be read 2708677e90edSThomas Graf * @st: state variable 2709677e90edSThomas Graf * 2710677e90edSThomas Graf * Initializes the specified state variable. Must be called before 2711677e90edSThomas Graf * invoking skb_seq_read() for the first time. 2712677e90edSThomas Graf */ 2713677e90edSThomas Graf void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from, 2714677e90edSThomas Graf unsigned int to, struct skb_seq_state *st) 2715677e90edSThomas Graf { 2716677e90edSThomas Graf st->lower_offset = from; 2717677e90edSThomas Graf st->upper_offset = to; 2718677e90edSThomas Graf st->root_skb = st->cur_skb = skb; 2719677e90edSThomas Graf st->frag_idx = st->stepped_offset = 0; 2720677e90edSThomas Graf st->frag_data = NULL; 2721677e90edSThomas Graf } 2722b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_prepare_seq_read); 2723677e90edSThomas Graf 2724677e90edSThomas Graf /** 2725677e90edSThomas Graf * skb_seq_read - Sequentially read skb data 2726677e90edSThomas Graf * @consumed: number of bytes consumed by the caller so far 2727677e90edSThomas Graf * @data: destination pointer for data to be returned 2728677e90edSThomas Graf * @st: state variable 2729677e90edSThomas Graf * 2730bc32383cSMathias Krause * Reads a block of skb data at @consumed relative to the 2731677e90edSThomas Graf * lower offset specified to skb_prepare_seq_read(). Assigns 2732bc32383cSMathias Krause * the head of the data block to @data and returns the length 2733677e90edSThomas Graf * of the block or 0 if the end of the skb data or the upper 2734677e90edSThomas Graf * offset has been reached. 2735677e90edSThomas Graf * 2736677e90edSThomas Graf * The caller is not required to consume all of the data 2737bc32383cSMathias Krause * returned, i.e. @consumed is typically set to the number 2738677e90edSThomas Graf * of bytes already consumed and the next call to 2739677e90edSThomas Graf * skb_seq_read() will return the remaining part of the block. 2740677e90edSThomas Graf * 274125985edcSLucas De Marchi * Note 1: The size of each block of data returned can be arbitrary, 2742e793c0f7SMasanari Iida * this limitation is the cost for zerocopy sequential 2743677e90edSThomas Graf * reads of potentially non linear data. 2744677e90edSThomas Graf * 2745bc2cda1eSRandy Dunlap * Note 2: Fragment lists within fragments are not implemented 2746677e90edSThomas Graf * at the moment, state->root_skb could be replaced with 2747677e90edSThomas Graf * a stack for this purpose. 2748677e90edSThomas Graf */ 2749677e90edSThomas Graf unsigned int skb_seq_read(unsigned int consumed, const u8 **data, 2750677e90edSThomas Graf struct skb_seq_state *st) 2751677e90edSThomas Graf { 2752677e90edSThomas Graf unsigned int block_limit, abs_offset = consumed + st->lower_offset; 2753677e90edSThomas Graf skb_frag_t *frag; 2754677e90edSThomas Graf 2755aeb193eaSWedson Almeida Filho if (unlikely(abs_offset >= st->upper_offset)) { 2756aeb193eaSWedson Almeida Filho if (st->frag_data) { 2757aeb193eaSWedson Almeida Filho kunmap_atomic(st->frag_data); 2758aeb193eaSWedson Almeida Filho st->frag_data = NULL; 2759aeb193eaSWedson Almeida Filho } 2760677e90edSThomas Graf return 0; 2761aeb193eaSWedson Almeida Filho } 2762677e90edSThomas Graf 2763677e90edSThomas Graf next_skb: 276495e3b24cSHerbert Xu block_limit = skb_headlen(st->cur_skb) + st->stepped_offset; 2765677e90edSThomas Graf 2766995b3379SThomas Chenault if (abs_offset < block_limit && !st->frag_data) { 276795e3b24cSHerbert Xu *data = st->cur_skb->data + (abs_offset - st->stepped_offset); 2768677e90edSThomas Graf return block_limit - abs_offset; 2769677e90edSThomas Graf } 2770677e90edSThomas Graf 2771677e90edSThomas Graf if (st->frag_idx == 0 && !st->frag_data) 2772677e90edSThomas Graf st->stepped_offset += skb_headlen(st->cur_skb); 2773677e90edSThomas Graf 2774677e90edSThomas Graf while (st->frag_idx < skb_shinfo(st->cur_skb)->nr_frags) { 2775677e90edSThomas Graf frag = &skb_shinfo(st->cur_skb)->frags[st->frag_idx]; 27769e903e08SEric Dumazet block_limit = skb_frag_size(frag) + st->stepped_offset; 2777677e90edSThomas Graf 2778677e90edSThomas Graf if (abs_offset < block_limit) { 2779677e90edSThomas Graf if (!st->frag_data) 278051c56b00SEric Dumazet st->frag_data = kmap_atomic(skb_frag_page(frag)); 2781677e90edSThomas Graf 2782677e90edSThomas Graf *data = (u8 *) st->frag_data + frag->page_offset + 2783677e90edSThomas Graf (abs_offset - st->stepped_offset); 2784677e90edSThomas Graf 2785677e90edSThomas Graf return block_limit - abs_offset; 2786677e90edSThomas Graf } 2787677e90edSThomas Graf 2788677e90edSThomas Graf if (st->frag_data) { 278951c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2790677e90edSThomas Graf st->frag_data = NULL; 2791677e90edSThomas Graf } 2792677e90edSThomas Graf 2793677e90edSThomas Graf st->frag_idx++; 27949e903e08SEric Dumazet st->stepped_offset += skb_frag_size(frag); 2795677e90edSThomas Graf } 2796677e90edSThomas Graf 27975b5a60daSOlaf Kirch if (st->frag_data) { 279851c56b00SEric Dumazet kunmap_atomic(st->frag_data); 27995b5a60daSOlaf Kirch st->frag_data = NULL; 28005b5a60daSOlaf Kirch } 28015b5a60daSOlaf Kirch 280221dc3301SDavid S. Miller if (st->root_skb == st->cur_skb && skb_has_frag_list(st->root_skb)) { 2803677e90edSThomas Graf st->cur_skb = skb_shinfo(st->root_skb)->frag_list; 280495e3b24cSHerbert Xu st->frag_idx = 0; 2805677e90edSThomas Graf goto next_skb; 280671b3346dSShyam Iyer } else if (st->cur_skb->next) { 280771b3346dSShyam Iyer st->cur_skb = st->cur_skb->next; 280871b3346dSShyam Iyer st->frag_idx = 0; 2809677e90edSThomas Graf goto next_skb; 2810677e90edSThomas Graf } 2811677e90edSThomas Graf 2812677e90edSThomas Graf return 0; 2813677e90edSThomas Graf } 2814b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_seq_read); 2815677e90edSThomas Graf 2816677e90edSThomas Graf /** 2817677e90edSThomas Graf * skb_abort_seq_read - Abort a sequential read of skb data 2818677e90edSThomas Graf * @st: state variable 2819677e90edSThomas Graf * 2820677e90edSThomas Graf * Must be called if skb_seq_read() was not called until it 2821677e90edSThomas Graf * returned 0. 2822677e90edSThomas Graf */ 2823677e90edSThomas Graf void skb_abort_seq_read(struct skb_seq_state *st) 2824677e90edSThomas Graf { 2825677e90edSThomas Graf if (st->frag_data) 282651c56b00SEric Dumazet kunmap_atomic(st->frag_data); 2827677e90edSThomas Graf } 2828b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_abort_seq_read); 2829677e90edSThomas Graf 28303fc7e8a6SThomas Graf #define TS_SKB_CB(state) ((struct skb_seq_state *) &((state)->cb)) 28313fc7e8a6SThomas Graf 28323fc7e8a6SThomas Graf static unsigned int skb_ts_get_next_block(unsigned int offset, const u8 **text, 28333fc7e8a6SThomas Graf struct ts_config *conf, 28343fc7e8a6SThomas Graf struct ts_state *state) 28353fc7e8a6SThomas Graf { 28363fc7e8a6SThomas Graf return skb_seq_read(offset, text, TS_SKB_CB(state)); 28373fc7e8a6SThomas Graf } 28383fc7e8a6SThomas Graf 28393fc7e8a6SThomas Graf static void skb_ts_finish(struct ts_config *conf, struct ts_state *state) 28403fc7e8a6SThomas Graf { 28413fc7e8a6SThomas Graf skb_abort_seq_read(TS_SKB_CB(state)); 28423fc7e8a6SThomas Graf } 28433fc7e8a6SThomas Graf 28443fc7e8a6SThomas Graf /** 28453fc7e8a6SThomas Graf * skb_find_text - Find a text pattern in skb data 28463fc7e8a6SThomas Graf * @skb: the buffer to look in 28473fc7e8a6SThomas Graf * @from: search offset 28483fc7e8a6SThomas Graf * @to: search limit 28493fc7e8a6SThomas Graf * @config: textsearch configuration 28503fc7e8a6SThomas Graf * 28513fc7e8a6SThomas Graf * Finds a pattern in the skb data according to the specified 28523fc7e8a6SThomas Graf * textsearch configuration. Use textsearch_next() to retrieve 28533fc7e8a6SThomas Graf * subsequent occurrences of the pattern. Returns the offset 28543fc7e8a6SThomas Graf * to the first occurrence or UINT_MAX if no match was found. 28553fc7e8a6SThomas Graf */ 28563fc7e8a6SThomas Graf unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2857059a2440SBojan Prtvar unsigned int to, struct ts_config *config) 28583fc7e8a6SThomas Graf { 2859059a2440SBojan Prtvar struct ts_state state; 2860f72b948dSPhil Oester unsigned int ret; 2861f72b948dSPhil Oester 28623fc7e8a6SThomas Graf config->get_next_block = skb_ts_get_next_block; 28633fc7e8a6SThomas Graf config->finish = skb_ts_finish; 28643fc7e8a6SThomas Graf 2865059a2440SBojan Prtvar skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state)); 28663fc7e8a6SThomas Graf 2867059a2440SBojan Prtvar ret = textsearch_find(config, &state); 2868f72b948dSPhil Oester return (ret <= to - from ? ret : UINT_MAX); 28693fc7e8a6SThomas Graf } 2870b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_find_text); 28713fc7e8a6SThomas Graf 2872e89e9cf5SAnanda Raju /** 28732c53040fSBen Hutchings * skb_append_datato_frags - append the user data to a skb 2874e89e9cf5SAnanda Raju * @sk: sock structure 2875e793c0f7SMasanari Iida * @skb: skb structure to be appended with user data. 2876e89e9cf5SAnanda Raju * @getfrag: call back function to be used for getting the user data 2877e89e9cf5SAnanda Raju * @from: pointer to user message iov 2878e89e9cf5SAnanda Raju * @length: length of the iov message 2879e89e9cf5SAnanda Raju * 2880e89e9cf5SAnanda Raju * Description: This procedure append the user data in the fragment part 2881e89e9cf5SAnanda Raju * of the skb if any page alloc fails user this procedure returns -ENOMEM 2882e89e9cf5SAnanda Raju */ 2883e89e9cf5SAnanda Raju int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, 2884dab9630fSMartin Waitz int (*getfrag)(void *from, char *to, int offset, 2885e89e9cf5SAnanda Raju int len, int odd, struct sk_buff *skb), 2886e89e9cf5SAnanda Raju void *from, int length) 2887e89e9cf5SAnanda Raju { 2888b2111724SEric Dumazet int frg_cnt = skb_shinfo(skb)->nr_frags; 2889b2111724SEric Dumazet int copy; 2890e89e9cf5SAnanda Raju int offset = 0; 2891e89e9cf5SAnanda Raju int ret; 2892b2111724SEric Dumazet struct page_frag *pfrag = ¤t->task_frag; 2893e89e9cf5SAnanda Raju 2894e89e9cf5SAnanda Raju do { 2895e89e9cf5SAnanda Raju /* Return error if we don't have space for new frag */ 2896e89e9cf5SAnanda Raju if (frg_cnt >= MAX_SKB_FRAGS) 2897b2111724SEric Dumazet return -EMSGSIZE; 2898e89e9cf5SAnanda Raju 2899b2111724SEric Dumazet if (!sk_page_frag_refill(sk, pfrag)) 2900e89e9cf5SAnanda Raju return -ENOMEM; 2901e89e9cf5SAnanda Raju 2902e89e9cf5SAnanda Raju /* copy the user data to page */ 2903b2111724SEric Dumazet copy = min_t(int, length, pfrag->size - pfrag->offset); 2904e89e9cf5SAnanda Raju 2905b2111724SEric Dumazet ret = getfrag(from, page_address(pfrag->page) + pfrag->offset, 2906e89e9cf5SAnanda Raju offset, copy, 0, skb); 2907e89e9cf5SAnanda Raju if (ret < 0) 2908e89e9cf5SAnanda Raju return -EFAULT; 2909e89e9cf5SAnanda Raju 2910e89e9cf5SAnanda Raju /* copy was successful so update the size parameters */ 2911b2111724SEric Dumazet skb_fill_page_desc(skb, frg_cnt, pfrag->page, pfrag->offset, 2912b2111724SEric Dumazet copy); 2913b2111724SEric Dumazet frg_cnt++; 2914b2111724SEric Dumazet pfrag->offset += copy; 2915b2111724SEric Dumazet get_page(pfrag->page); 2916b2111724SEric Dumazet 2917b2111724SEric Dumazet skb->truesize += copy; 2918b2111724SEric Dumazet atomic_add(copy, &sk->sk_wmem_alloc); 2919e89e9cf5SAnanda Raju skb->len += copy; 2920e89e9cf5SAnanda Raju skb->data_len += copy; 2921e89e9cf5SAnanda Raju offset += copy; 2922e89e9cf5SAnanda Raju length -= copy; 2923e89e9cf5SAnanda Raju 2924e89e9cf5SAnanda Raju } while (length > 0); 2925e89e9cf5SAnanda Raju 2926e89e9cf5SAnanda Raju return 0; 2927e89e9cf5SAnanda Raju } 2928b4ac530fSDavid S. Miller EXPORT_SYMBOL(skb_append_datato_frags); 2929e89e9cf5SAnanda Raju 2930be12a1feSHannes Frederic Sowa int skb_append_pagefrags(struct sk_buff *skb, struct page *page, 2931be12a1feSHannes Frederic Sowa int offset, size_t size) 2932be12a1feSHannes Frederic Sowa { 2933be12a1feSHannes Frederic Sowa int i = skb_shinfo(skb)->nr_frags; 2934be12a1feSHannes Frederic Sowa 2935be12a1feSHannes Frederic Sowa if (skb_can_coalesce(skb, i, page, offset)) { 2936be12a1feSHannes Frederic Sowa skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], size); 2937be12a1feSHannes Frederic Sowa } else if (i < MAX_SKB_FRAGS) { 2938be12a1feSHannes Frederic Sowa get_page(page); 2939be12a1feSHannes Frederic Sowa skb_fill_page_desc(skb, i, page, offset, size); 2940be12a1feSHannes Frederic Sowa } else { 2941be12a1feSHannes Frederic Sowa return -EMSGSIZE; 2942be12a1feSHannes Frederic Sowa } 2943be12a1feSHannes Frederic Sowa 2944be12a1feSHannes Frederic Sowa return 0; 2945be12a1feSHannes Frederic Sowa } 2946be12a1feSHannes Frederic Sowa EXPORT_SYMBOL_GPL(skb_append_pagefrags); 2947be12a1feSHannes Frederic Sowa 2948cbb042f9SHerbert Xu /** 2949cbb042f9SHerbert Xu * skb_pull_rcsum - pull skb and update receive checksum 2950cbb042f9SHerbert Xu * @skb: buffer to update 2951cbb042f9SHerbert Xu * @len: length of data pulled 2952cbb042f9SHerbert Xu * 2953cbb042f9SHerbert Xu * This function performs an skb_pull on the packet and updates 2954fee54fa5SUrs Thuermann * the CHECKSUM_COMPLETE checksum. It should be used on 295584fa7933SPatrick McHardy * receive path processing instead of skb_pull unless you know 295684fa7933SPatrick McHardy * that the checksum difference is zero (e.g., a valid IP header) 295784fa7933SPatrick McHardy * or you are setting ip_summed to CHECKSUM_NONE. 2958cbb042f9SHerbert Xu */ 2959cbb042f9SHerbert Xu unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len) 2960cbb042f9SHerbert Xu { 296131b33dfbSPravin B Shelar unsigned char *data = skb->data; 296231b33dfbSPravin B Shelar 2963cbb042f9SHerbert Xu BUG_ON(len > skb->len); 296431b33dfbSPravin B Shelar __skb_pull(skb, len); 296531b33dfbSPravin B Shelar skb_postpull_rcsum(skb, data, len); 296631b33dfbSPravin B Shelar return skb->data; 2967cbb042f9SHerbert Xu } 2968f94691acSArnaldo Carvalho de Melo EXPORT_SYMBOL_GPL(skb_pull_rcsum); 2969f94691acSArnaldo Carvalho de Melo 2970f4c50d99SHerbert Xu /** 2971f4c50d99SHerbert Xu * skb_segment - Perform protocol segmentation on skb. 2972df5771ffSMichael S. Tsirkin * @head_skb: buffer to segment 2973576a30ebSHerbert Xu * @features: features for the output path (see dev->features) 2974f4c50d99SHerbert Xu * 2975f4c50d99SHerbert Xu * This function performs segmentation on the given skb. It returns 29764c821d75SBen Hutchings * a pointer to the first in a list of new skbs for the segments. 29774c821d75SBen Hutchings * In case of error it returns ERR_PTR(err). 2978f4c50d99SHerbert Xu */ 2979df5771ffSMichael S. Tsirkin struct sk_buff *skb_segment(struct sk_buff *head_skb, 2980df5771ffSMichael S. Tsirkin netdev_features_t features) 2981f4c50d99SHerbert Xu { 2982f4c50d99SHerbert Xu struct sk_buff *segs = NULL; 2983f4c50d99SHerbert Xu struct sk_buff *tail = NULL; 29841a4cedafSMichael S. Tsirkin struct sk_buff *list_skb = skb_shinfo(head_skb)->frag_list; 2985df5771ffSMichael S. Tsirkin skb_frag_t *frag = skb_shinfo(head_skb)->frags; 2986df5771ffSMichael S. Tsirkin unsigned int mss = skb_shinfo(head_skb)->gso_size; 2987df5771ffSMichael S. Tsirkin unsigned int doffset = head_skb->data - skb_mac_header(head_skb); 29881fd819ecSMichael S. Tsirkin struct sk_buff *frag_skb = head_skb; 2989f4c50d99SHerbert Xu unsigned int offset = doffset; 2990df5771ffSMichael S. Tsirkin unsigned int tnl_hlen = skb_tnl_header_len(head_skb); 2991f4c50d99SHerbert Xu unsigned int headroom; 2992f4c50d99SHerbert Xu unsigned int len; 2993ec5f0615SPravin B Shelar __be16 proto; 2994ec5f0615SPravin B Shelar bool csum; 299504ed3e74SMichał Mirosław int sg = !!(features & NETIF_F_SG); 2996df5771ffSMichael S. Tsirkin int nfrags = skb_shinfo(head_skb)->nr_frags; 2997f4c50d99SHerbert Xu int err = -ENOMEM; 2998f4c50d99SHerbert Xu int i = 0; 2999f4c50d99SHerbert Xu int pos; 300053d6471cSVlad Yasevich int dummy; 3001f4c50d99SHerbert Xu 30025882a07cSWei-Chun Chao __skb_push(head_skb, doffset); 300353d6471cSVlad Yasevich proto = skb_network_protocol(head_skb, &dummy); 3004ec5f0615SPravin B Shelar if (unlikely(!proto)) 3005ec5f0615SPravin B Shelar return ERR_PTR(-EINVAL); 3006ec5f0615SPravin B Shelar 30077e2b10c1STom Herbert csum = !head_skb->encap_hdr_csum && 30087e2b10c1STom Herbert !!can_checksum_protocol(features, proto); 30097e2b10c1STom Herbert 3010df5771ffSMichael S. Tsirkin headroom = skb_headroom(head_skb); 3011df5771ffSMichael S. Tsirkin pos = skb_headlen(head_skb); 3012f4c50d99SHerbert Xu 3013f4c50d99SHerbert Xu do { 3014f4c50d99SHerbert Xu struct sk_buff *nskb; 30158cb19905SMichael S. Tsirkin skb_frag_t *nskb_frag; 3016c8884eddSHerbert Xu int hsize; 3017f4c50d99SHerbert Xu int size; 3018f4c50d99SHerbert Xu 3019df5771ffSMichael S. Tsirkin len = head_skb->len - offset; 3020f4c50d99SHerbert Xu if (len > mss) 3021f4c50d99SHerbert Xu len = mss; 3022f4c50d99SHerbert Xu 3023df5771ffSMichael S. Tsirkin hsize = skb_headlen(head_skb) - offset; 3024f4c50d99SHerbert Xu if (hsize < 0) 3025f4c50d99SHerbert Xu hsize = 0; 3026c8884eddSHerbert Xu if (hsize > len || !sg) 3027c8884eddSHerbert Xu hsize = len; 3028f4c50d99SHerbert Xu 30291a4cedafSMichael S. Tsirkin if (!hsize && i >= nfrags && skb_headlen(list_skb) && 30301a4cedafSMichael S. Tsirkin (skb_headlen(list_skb) == len || sg)) { 30311a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb) > len); 303289319d38SHerbert Xu 30339d8506ccSHerbert Xu i = 0; 30341a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 30351a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 30361fd819ecSMichael S. Tsirkin frag_skb = list_skb; 30371a4cedafSMichael S. Tsirkin pos += skb_headlen(list_skb); 30389d8506ccSHerbert Xu 30399d8506ccSHerbert Xu while (pos < offset + len) { 30409d8506ccSHerbert Xu BUG_ON(i >= nfrags); 30419d8506ccSHerbert Xu 30424e1beba1SMichael S. Tsirkin size = skb_frag_size(frag); 30439d8506ccSHerbert Xu if (pos + size > offset + len) 30449d8506ccSHerbert Xu break; 30459d8506ccSHerbert Xu 30469d8506ccSHerbert Xu i++; 30479d8506ccSHerbert Xu pos += size; 30484e1beba1SMichael S. Tsirkin frag++; 30499d8506ccSHerbert Xu } 30509d8506ccSHerbert Xu 30511a4cedafSMichael S. Tsirkin nskb = skb_clone(list_skb, GFP_ATOMIC); 30521a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 305389319d38SHerbert Xu 3054f4c50d99SHerbert Xu if (unlikely(!nskb)) 3055f4c50d99SHerbert Xu goto err; 3056f4c50d99SHerbert Xu 30579d8506ccSHerbert Xu if (unlikely(pskb_trim(nskb, len))) { 30589d8506ccSHerbert Xu kfree_skb(nskb); 30599d8506ccSHerbert Xu goto err; 30609d8506ccSHerbert Xu } 30619d8506ccSHerbert Xu 3062ec47ea82SAlexander Duyck hsize = skb_end_offset(nskb); 306389319d38SHerbert Xu if (skb_cow_head(nskb, doffset + headroom)) { 306489319d38SHerbert Xu kfree_skb(nskb); 306589319d38SHerbert Xu goto err; 306689319d38SHerbert Xu } 306789319d38SHerbert Xu 3068ec47ea82SAlexander Duyck nskb->truesize += skb_end_offset(nskb) - hsize; 306989319d38SHerbert Xu skb_release_head_state(nskb); 307089319d38SHerbert Xu __skb_push(nskb, doffset); 307189319d38SHerbert Xu } else { 3072c93bdd0eSMel Gorman nskb = __alloc_skb(hsize + doffset + headroom, 3073df5771ffSMichael S. Tsirkin GFP_ATOMIC, skb_alloc_rx_flag(head_skb), 3074c93bdd0eSMel Gorman NUMA_NO_NODE); 307589319d38SHerbert Xu 307689319d38SHerbert Xu if (unlikely(!nskb)) 307789319d38SHerbert Xu goto err; 307889319d38SHerbert Xu 307989319d38SHerbert Xu skb_reserve(nskb, headroom); 308089319d38SHerbert Xu __skb_put(nskb, doffset); 308189319d38SHerbert Xu } 308289319d38SHerbert Xu 3083f4c50d99SHerbert Xu if (segs) 3084f4c50d99SHerbert Xu tail->next = nskb; 3085f4c50d99SHerbert Xu else 3086f4c50d99SHerbert Xu segs = nskb; 3087f4c50d99SHerbert Xu tail = nskb; 3088f4c50d99SHerbert Xu 3089df5771ffSMichael S. Tsirkin __copy_skb_header(nskb, head_skb); 3090f4c50d99SHerbert Xu 3091030737bcSEric Dumazet skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom); 3092fcdfe3a7SVlad Yasevich skb_reset_mac_len(nskb); 309368c33163SPravin B Shelar 3094df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, -tnl_hlen, 309568c33163SPravin B Shelar nskb->data - tnl_hlen, 309668c33163SPravin B Shelar doffset + tnl_hlen); 309789319d38SHerbert Xu 30989d8506ccSHerbert Xu if (nskb->len == len + doffset) 30991cdbcb79SSimon Horman goto perform_csum_check; 310089319d38SHerbert Xu 31017fbeffedSAlexander Duyck if (!sg) { 31027fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 31036f85a124SHerbert Xu nskb->ip_summed = CHECKSUM_NONE; 310476443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 310576443456SAlexander Duyck skb_copy_and_csum_bits(head_skb, offset, 3106f4c50d99SHerbert Xu skb_put(nskb, len), 3107f4c50d99SHerbert Xu len, 0); 31087e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 3109de843723STom Herbert skb_headroom(nskb) + doffset; 3110f4c50d99SHerbert Xu continue; 3111f4c50d99SHerbert Xu } 3112f4c50d99SHerbert Xu 31138cb19905SMichael S. Tsirkin nskb_frag = skb_shinfo(nskb)->frags; 3114f4c50d99SHerbert Xu 3115df5771ffSMichael S. Tsirkin skb_copy_from_linear_data_offset(head_skb, offset, 3116d626f62bSArnaldo Carvalho de Melo skb_put(nskb, hsize), hsize); 3117f4c50d99SHerbert Xu 3118df5771ffSMichael S. Tsirkin skb_shinfo(nskb)->tx_flags = skb_shinfo(head_skb)->tx_flags & 3119df5771ffSMichael S. Tsirkin SKBTX_SHARED_FRAG; 3120cef401deSEric Dumazet 31219d8506ccSHerbert Xu while (pos < offset + len) { 31229d8506ccSHerbert Xu if (i >= nfrags) { 31231a4cedafSMichael S. Tsirkin BUG_ON(skb_headlen(list_skb)); 31249d8506ccSHerbert Xu 31259d8506ccSHerbert Xu i = 0; 31261a4cedafSMichael S. Tsirkin nfrags = skb_shinfo(list_skb)->nr_frags; 31271a4cedafSMichael S. Tsirkin frag = skb_shinfo(list_skb)->frags; 31281fd819ecSMichael S. Tsirkin frag_skb = list_skb; 31299d8506ccSHerbert Xu 31309d8506ccSHerbert Xu BUG_ON(!nfrags); 31319d8506ccSHerbert Xu 31321a4cedafSMichael S. Tsirkin list_skb = list_skb->next; 31339d8506ccSHerbert Xu } 31349d8506ccSHerbert Xu 31359d8506ccSHerbert Xu if (unlikely(skb_shinfo(nskb)->nr_frags >= 31369d8506ccSHerbert Xu MAX_SKB_FRAGS)) { 31379d8506ccSHerbert Xu net_warn_ratelimited( 31389d8506ccSHerbert Xu "skb_segment: too many frags: %u %u\n", 31399d8506ccSHerbert Xu pos, mss); 31409d8506ccSHerbert Xu goto err; 31419d8506ccSHerbert Xu } 31429d8506ccSHerbert Xu 31431fd819ecSMichael S. Tsirkin if (unlikely(skb_orphan_frags(frag_skb, GFP_ATOMIC))) 31441fd819ecSMichael S. Tsirkin goto err; 31451fd819ecSMichael S. Tsirkin 31464e1beba1SMichael S. Tsirkin *nskb_frag = *frag; 31478cb19905SMichael S. Tsirkin __skb_frag_ref(nskb_frag); 31488cb19905SMichael S. Tsirkin size = skb_frag_size(nskb_frag); 3149f4c50d99SHerbert Xu 3150f4c50d99SHerbert Xu if (pos < offset) { 31518cb19905SMichael S. Tsirkin nskb_frag->page_offset += offset - pos; 31528cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, offset - pos); 3153f4c50d99SHerbert Xu } 3154f4c50d99SHerbert Xu 315589319d38SHerbert Xu skb_shinfo(nskb)->nr_frags++; 3156f4c50d99SHerbert Xu 3157f4c50d99SHerbert Xu if (pos + size <= offset + len) { 3158f4c50d99SHerbert Xu i++; 31594e1beba1SMichael S. Tsirkin frag++; 3160f4c50d99SHerbert Xu pos += size; 3161f4c50d99SHerbert Xu } else { 31628cb19905SMichael S. Tsirkin skb_frag_size_sub(nskb_frag, pos + size - (offset + len)); 316389319d38SHerbert Xu goto skip_fraglist; 3164f4c50d99SHerbert Xu } 3165f4c50d99SHerbert Xu 31668cb19905SMichael S. Tsirkin nskb_frag++; 3167f4c50d99SHerbert Xu } 3168f4c50d99SHerbert Xu 316989319d38SHerbert Xu skip_fraglist: 3170f4c50d99SHerbert Xu nskb->data_len = len - hsize; 3171f4c50d99SHerbert Xu nskb->len += nskb->data_len; 3172f4c50d99SHerbert Xu nskb->truesize += nskb->data_len; 3173ec5f0615SPravin B Shelar 31741cdbcb79SSimon Horman perform_csum_check: 31757fbeffedSAlexander Duyck if (!csum) { 31767fbeffedSAlexander Duyck if (!nskb->remcsum_offload) 3177ec5f0615SPravin B Shelar nskb->ip_summed = CHECKSUM_NONE; 317876443456SAlexander Duyck SKB_GSO_CB(nskb)->csum = 317976443456SAlexander Duyck skb_checksum(nskb, doffset, 318076443456SAlexander Duyck nskb->len - doffset, 0); 31817e2b10c1STom Herbert SKB_GSO_CB(nskb)->csum_start = 31827e2b10c1STom Herbert skb_headroom(nskb) + doffset; 3183ec5f0615SPravin B Shelar } 3184df5771ffSMichael S. Tsirkin } while ((offset += len) < head_skb->len); 3185f4c50d99SHerbert Xu 3186bec3cfdcSEric Dumazet /* Some callers want to get the end of the list. 3187bec3cfdcSEric Dumazet * Put it in segs->prev to avoid walking the list. 3188bec3cfdcSEric Dumazet * (see validate_xmit_skb_list() for example) 3189bec3cfdcSEric Dumazet */ 3190bec3cfdcSEric Dumazet segs->prev = tail; 3191432c856fSToshiaki Makita 3192432c856fSToshiaki Makita /* Following permits correct backpressure, for protocols 3193432c856fSToshiaki Makita * using skb_set_owner_w(). 3194432c856fSToshiaki Makita * Idea is to tranfert ownership from head_skb to last segment. 3195432c856fSToshiaki Makita */ 3196432c856fSToshiaki Makita if (head_skb->destructor == sock_wfree) { 3197432c856fSToshiaki Makita swap(tail->truesize, head_skb->truesize); 3198432c856fSToshiaki Makita swap(tail->destructor, head_skb->destructor); 3199432c856fSToshiaki Makita swap(tail->sk, head_skb->sk); 3200432c856fSToshiaki Makita } 3201f4c50d99SHerbert Xu return segs; 3202f4c50d99SHerbert Xu 3203f4c50d99SHerbert Xu err: 3204289dccbeSEric Dumazet kfree_skb_list(segs); 3205f4c50d99SHerbert Xu return ERR_PTR(err); 3206f4c50d99SHerbert Xu } 3207f4c50d99SHerbert Xu EXPORT_SYMBOL_GPL(skb_segment); 3208f4c50d99SHerbert Xu 320971d93b39SHerbert Xu int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 321071d93b39SHerbert Xu { 32118a29111cSEric Dumazet struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 321267147ba9SHerbert Xu unsigned int offset = skb_gro_offset(skb); 321367147ba9SHerbert Xu unsigned int headlen = skb_headlen(skb); 32148a29111cSEric Dumazet unsigned int len = skb_gro_len(skb); 321558025e46SEric Dumazet struct sk_buff *lp, *p = *head; 3216715dc1f3SEric Dumazet unsigned int delta_truesize; 321771d93b39SHerbert Xu 32188a29111cSEric Dumazet if (unlikely(p->len + len >= 65536)) 321971d93b39SHerbert Xu return -E2BIG; 322071d93b39SHerbert Xu 322129e98242SEric Dumazet lp = NAPI_GRO_CB(p)->last; 32228a29111cSEric Dumazet pinfo = skb_shinfo(lp); 32238a29111cSEric Dumazet 32248a29111cSEric Dumazet if (headlen <= offset) { 322542da6994SHerbert Xu skb_frag_t *frag; 322666e92fcfSHerbert Xu skb_frag_t *frag2; 32279aaa156cSHerbert Xu int i = skbinfo->nr_frags; 32289aaa156cSHerbert Xu int nr_frags = pinfo->nr_frags + i; 322942da6994SHerbert Xu 323066e92fcfSHerbert Xu if (nr_frags > MAX_SKB_FRAGS) 32318a29111cSEric Dumazet goto merge; 323281705ad1SHerbert Xu 32338a29111cSEric Dumazet offset -= headlen; 32349aaa156cSHerbert Xu pinfo->nr_frags = nr_frags; 32359aaa156cSHerbert Xu skbinfo->nr_frags = 0; 3236f5572068SHerbert Xu 32379aaa156cSHerbert Xu frag = pinfo->frags + nr_frags; 32389aaa156cSHerbert Xu frag2 = skbinfo->frags + i; 323966e92fcfSHerbert Xu do { 324066e92fcfSHerbert Xu *--frag = *--frag2; 324166e92fcfSHerbert Xu } while (--i); 324266e92fcfSHerbert Xu 324366e92fcfSHerbert Xu frag->page_offset += offset; 32449e903e08SEric Dumazet skb_frag_size_sub(frag, offset); 324566e92fcfSHerbert Xu 3246715dc1f3SEric Dumazet /* all fragments truesize : remove (head size + sk_buff) */ 3247ec47ea82SAlexander Duyck delta_truesize = skb->truesize - 3248ec47ea82SAlexander Duyck SKB_TRUESIZE(skb_end_offset(skb)); 3249715dc1f3SEric Dumazet 3250f5572068SHerbert Xu skb->truesize -= skb->data_len; 3251f5572068SHerbert Xu skb->len -= skb->data_len; 3252f5572068SHerbert Xu skb->data_len = 0; 3253f5572068SHerbert Xu 3254715dc1f3SEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE; 32555d38a079SHerbert Xu goto done; 3256d7e8883cSEric Dumazet } else if (skb->head_frag) { 3257d7e8883cSEric Dumazet int nr_frags = pinfo->nr_frags; 3258d7e8883cSEric Dumazet skb_frag_t *frag = pinfo->frags + nr_frags; 3259d7e8883cSEric Dumazet struct page *page = virt_to_head_page(skb->head); 3260d7e8883cSEric Dumazet unsigned int first_size = headlen - offset; 3261d7e8883cSEric Dumazet unsigned int first_offset; 3262d7e8883cSEric Dumazet 3263d7e8883cSEric Dumazet if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 32648a29111cSEric Dumazet goto merge; 3265d7e8883cSEric Dumazet 3266d7e8883cSEric Dumazet first_offset = skb->data - 3267d7e8883cSEric Dumazet (unsigned char *)page_address(page) + 3268d7e8883cSEric Dumazet offset; 3269d7e8883cSEric Dumazet 3270d7e8883cSEric Dumazet pinfo->nr_frags = nr_frags + 1 + skbinfo->nr_frags; 3271d7e8883cSEric Dumazet 3272d7e8883cSEric Dumazet frag->page.p = page; 3273d7e8883cSEric Dumazet frag->page_offset = first_offset; 3274d7e8883cSEric Dumazet skb_frag_size_set(frag, first_size); 3275d7e8883cSEric Dumazet 3276d7e8883cSEric Dumazet memcpy(frag + 1, skbinfo->frags, sizeof(*frag) * skbinfo->nr_frags); 3277d7e8883cSEric Dumazet /* We dont need to clear skbinfo->nr_frags here */ 3278d7e8883cSEric Dumazet 3279715dc1f3SEric Dumazet delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3280d7e8883cSEric Dumazet NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3281d7e8883cSEric Dumazet goto done; 32828a29111cSEric Dumazet } 328371d93b39SHerbert Xu 328471d93b39SHerbert Xu merge: 3285715dc1f3SEric Dumazet delta_truesize = skb->truesize; 328667147ba9SHerbert Xu if (offset > headlen) { 3287d1dc7abfSMichal Schmidt unsigned int eat = offset - headlen; 3288d1dc7abfSMichal Schmidt 3289d1dc7abfSMichal Schmidt skbinfo->frags[0].page_offset += eat; 32909e903e08SEric Dumazet skb_frag_size_sub(&skbinfo->frags[0], eat); 3291d1dc7abfSMichal Schmidt skb->data_len -= eat; 3292d1dc7abfSMichal Schmidt skb->len -= eat; 329367147ba9SHerbert Xu offset = headlen; 329456035022SHerbert Xu } 329556035022SHerbert Xu 329667147ba9SHerbert Xu __skb_pull(skb, offset); 329756035022SHerbert Xu 329829e98242SEric Dumazet if (NAPI_GRO_CB(p)->last == p) 32998a29111cSEric Dumazet skb_shinfo(p)->frag_list = skb; 33008a29111cSEric Dumazet else 3301c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last->next = skb; 3302c3c7c254SEric Dumazet NAPI_GRO_CB(p)->last = skb; 3303f4a775d1SEric Dumazet __skb_header_release(skb); 33048a29111cSEric Dumazet lp = p; 330571d93b39SHerbert Xu 33065d38a079SHerbert Xu done: 33075d38a079SHerbert Xu NAPI_GRO_CB(p)->count++; 330837fe4732SHerbert Xu p->data_len += len; 3309715dc1f3SEric Dumazet p->truesize += delta_truesize; 331037fe4732SHerbert Xu p->len += len; 33118a29111cSEric Dumazet if (lp != p) { 33128a29111cSEric Dumazet lp->data_len += len; 33138a29111cSEric Dumazet lp->truesize += delta_truesize; 33148a29111cSEric Dumazet lp->len += len; 33158a29111cSEric Dumazet } 331671d93b39SHerbert Xu NAPI_GRO_CB(skb)->same_flow = 1; 331771d93b39SHerbert Xu return 0; 331871d93b39SHerbert Xu } 331971d93b39SHerbert Xu 33201da177e4SLinus Torvalds void __init skb_init(void) 33211da177e4SLinus Torvalds { 33221da177e4SLinus Torvalds skbuff_head_cache = kmem_cache_create("skbuff_head_cache", 33231da177e4SLinus Torvalds sizeof(struct sk_buff), 33241da177e4SLinus Torvalds 0, 3325e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 332620c2df83SPaul Mundt NULL); 3327d179cd12SDavid S. Miller skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache", 3328d0bf4a9eSEric Dumazet sizeof(struct sk_buff_fclones), 3329d179cd12SDavid S. Miller 0, 3330e5d679f3SAlexey Dobriyan SLAB_HWCACHE_ALIGN|SLAB_PANIC, 333120c2df83SPaul Mundt NULL); 33321da177e4SLinus Torvalds } 33331da177e4SLinus Torvalds 3334716ea3a7SDavid Howells /** 3335716ea3a7SDavid Howells * skb_to_sgvec - Fill a scatter-gather list from a socket buffer 3336716ea3a7SDavid Howells * @skb: Socket buffer containing the buffers to be mapped 3337716ea3a7SDavid Howells * @sg: The scatter-gather list to map into 3338716ea3a7SDavid Howells * @offset: The offset into the buffer's contents to start mapping 3339716ea3a7SDavid Howells * @len: Length of buffer space to be mapped 3340716ea3a7SDavid Howells * 3341716ea3a7SDavid Howells * Fill the specified scatter-gather list with mappings/pointers into a 3342716ea3a7SDavid Howells * region of the buffer space attached to a socket buffer. 3343716ea3a7SDavid Howells */ 334451c739d1SDavid S. Miller static int 334551c739d1SDavid S. Miller __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 3346716ea3a7SDavid Howells { 33471a028e50SDavid S. Miller int start = skb_headlen(skb); 33481a028e50SDavid S. Miller int i, copy = start - offset; 3349fbb398a8SDavid S. Miller struct sk_buff *frag_iter; 3350716ea3a7SDavid Howells int elt = 0; 3351716ea3a7SDavid Howells 3352716ea3a7SDavid Howells if (copy > 0) { 3353716ea3a7SDavid Howells if (copy > len) 3354716ea3a7SDavid Howells copy = len; 3355642f1490SJens Axboe sg_set_buf(sg, skb->data + offset, copy); 3356716ea3a7SDavid Howells elt++; 3357716ea3a7SDavid Howells if ((len -= copy) == 0) 3358716ea3a7SDavid Howells return elt; 3359716ea3a7SDavid Howells offset += copy; 3360716ea3a7SDavid Howells } 3361716ea3a7SDavid Howells 3362716ea3a7SDavid Howells for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 33631a028e50SDavid S. Miller int end; 3364716ea3a7SDavid Howells 3365547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33661a028e50SDavid S. Miller 33679e903e08SEric Dumazet end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); 3368716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3369716ea3a7SDavid Howells skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3370716ea3a7SDavid Howells 3371716ea3a7SDavid Howells if (copy > len) 3372716ea3a7SDavid Howells copy = len; 3373ea2ab693SIan Campbell sg_set_page(&sg[elt], skb_frag_page(frag), copy, 3374642f1490SJens Axboe frag->page_offset+offset-start); 3375716ea3a7SDavid Howells elt++; 3376716ea3a7SDavid Howells if (!(len -= copy)) 3377716ea3a7SDavid Howells return elt; 3378716ea3a7SDavid Howells offset += copy; 3379716ea3a7SDavid Howells } 33801a028e50SDavid S. Miller start = end; 3381716ea3a7SDavid Howells } 3382716ea3a7SDavid Howells 3383fbb398a8SDavid S. Miller skb_walk_frags(skb, frag_iter) { 33841a028e50SDavid S. Miller int end; 3385716ea3a7SDavid Howells 3386547b792cSIlpo Järvinen WARN_ON(start > offset + len); 33871a028e50SDavid S. Miller 3388fbb398a8SDavid S. Miller end = start + frag_iter->len; 3389716ea3a7SDavid Howells if ((copy = end - offset) > 0) { 3390716ea3a7SDavid Howells if (copy > len) 3391716ea3a7SDavid Howells copy = len; 3392fbb398a8SDavid S. Miller elt += __skb_to_sgvec(frag_iter, sg+elt, offset - start, 339351c739d1SDavid S. Miller copy); 3394716ea3a7SDavid Howells if ((len -= copy) == 0) 3395716ea3a7SDavid Howells return elt; 3396716ea3a7SDavid Howells offset += copy; 3397716ea3a7SDavid Howells } 33981a028e50SDavid S. Miller start = end; 3399716ea3a7SDavid Howells } 3400716ea3a7SDavid Howells BUG_ON(len); 3401716ea3a7SDavid Howells return elt; 3402716ea3a7SDavid Howells } 3403716ea3a7SDavid Howells 340425a91d8dSFan Du /* As compared with skb_to_sgvec, skb_to_sgvec_nomark only map skb to given 340525a91d8dSFan Du * sglist without mark the sg which contain last skb data as the end. 340625a91d8dSFan Du * So the caller can mannipulate sg list as will when padding new data after 340725a91d8dSFan Du * the first call without calling sg_unmark_end to expend sg list. 340825a91d8dSFan Du * 340925a91d8dSFan Du * Scenario to use skb_to_sgvec_nomark: 341025a91d8dSFan Du * 1. sg_init_table 341125a91d8dSFan Du * 2. skb_to_sgvec_nomark(payload1) 341225a91d8dSFan Du * 3. skb_to_sgvec_nomark(payload2) 341325a91d8dSFan Du * 341425a91d8dSFan Du * This is equivalent to: 341525a91d8dSFan Du * 1. sg_init_table 341625a91d8dSFan Du * 2. skb_to_sgvec(payload1) 341725a91d8dSFan Du * 3. sg_unmark_end 341825a91d8dSFan Du * 4. skb_to_sgvec(payload2) 341925a91d8dSFan Du * 342025a91d8dSFan Du * When mapping mutilple payload conditionally, skb_to_sgvec_nomark 342125a91d8dSFan Du * is more preferable. 342225a91d8dSFan Du */ 342325a91d8dSFan Du int skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, 342425a91d8dSFan Du int offset, int len) 342525a91d8dSFan Du { 342625a91d8dSFan Du return __skb_to_sgvec(skb, sg, offset, len); 342725a91d8dSFan Du } 342825a91d8dSFan Du EXPORT_SYMBOL_GPL(skb_to_sgvec_nomark); 342925a91d8dSFan Du 343051c739d1SDavid S. Miller int skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len) 343151c739d1SDavid S. Miller { 343251c739d1SDavid S. Miller int nsg = __skb_to_sgvec(skb, sg, offset, len); 343351c739d1SDavid S. Miller 3434c46f2334SJens Axboe sg_mark_end(&sg[nsg - 1]); 343551c739d1SDavid S. Miller 343651c739d1SDavid S. Miller return nsg; 343751c739d1SDavid S. Miller } 3438b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_to_sgvec); 343951c739d1SDavid S. Miller 3440716ea3a7SDavid Howells /** 3441716ea3a7SDavid Howells * skb_cow_data - Check that a socket buffer's data buffers are writable 3442716ea3a7SDavid Howells * @skb: The socket buffer to check. 3443716ea3a7SDavid Howells * @tailbits: Amount of trailing space to be added 3444716ea3a7SDavid Howells * @trailer: Returned pointer to the skb where the @tailbits space begins 3445716ea3a7SDavid Howells * 3446716ea3a7SDavid Howells * Make sure that the data buffers attached to a socket buffer are 3447716ea3a7SDavid Howells * writable. If they are not, private copies are made of the data buffers 3448716ea3a7SDavid Howells * and the socket buffer is set to use these instead. 3449716ea3a7SDavid Howells * 3450716ea3a7SDavid Howells * If @tailbits is given, make sure that there is space to write @tailbits 3451716ea3a7SDavid Howells * bytes of data beyond current end of socket buffer. @trailer will be 3452716ea3a7SDavid Howells * set to point to the skb in which this space begins. 3453716ea3a7SDavid Howells * 3454716ea3a7SDavid Howells * The number of scatterlist elements required to completely map the 3455716ea3a7SDavid Howells * COW'd and extended socket buffer will be returned. 3456716ea3a7SDavid Howells */ 3457716ea3a7SDavid Howells int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer) 3458716ea3a7SDavid Howells { 3459716ea3a7SDavid Howells int copyflag; 3460716ea3a7SDavid Howells int elt; 3461716ea3a7SDavid Howells struct sk_buff *skb1, **skb_p; 3462716ea3a7SDavid Howells 3463716ea3a7SDavid Howells /* If skb is cloned or its head is paged, reallocate 3464716ea3a7SDavid Howells * head pulling out all the pages (pages are considered not writable 3465716ea3a7SDavid Howells * at the moment even if they are anonymous). 3466716ea3a7SDavid Howells */ 3467716ea3a7SDavid Howells if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) && 3468716ea3a7SDavid Howells __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL) 3469716ea3a7SDavid Howells return -ENOMEM; 3470716ea3a7SDavid Howells 3471716ea3a7SDavid Howells /* Easy case. Most of packets will go this way. */ 347221dc3301SDavid S. Miller if (!skb_has_frag_list(skb)) { 3473716ea3a7SDavid Howells /* A little of trouble, not enough of space for trailer. 3474716ea3a7SDavid Howells * This should not happen, when stack is tuned to generate 3475716ea3a7SDavid Howells * good frames. OK, on miss we reallocate and reserve even more 3476716ea3a7SDavid Howells * space, 128 bytes is fair. */ 3477716ea3a7SDavid Howells 3478716ea3a7SDavid Howells if (skb_tailroom(skb) < tailbits && 3479716ea3a7SDavid Howells pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC)) 3480716ea3a7SDavid Howells return -ENOMEM; 3481716ea3a7SDavid Howells 3482716ea3a7SDavid Howells /* Voila! */ 3483716ea3a7SDavid Howells *trailer = skb; 3484716ea3a7SDavid Howells return 1; 3485716ea3a7SDavid Howells } 3486716ea3a7SDavid Howells 3487716ea3a7SDavid Howells /* Misery. We are in troubles, going to mincer fragments... */ 3488716ea3a7SDavid Howells 3489716ea3a7SDavid Howells elt = 1; 3490716ea3a7SDavid Howells skb_p = &skb_shinfo(skb)->frag_list; 3491716ea3a7SDavid Howells copyflag = 0; 3492716ea3a7SDavid Howells 3493716ea3a7SDavid Howells while ((skb1 = *skb_p) != NULL) { 3494716ea3a7SDavid Howells int ntail = 0; 3495716ea3a7SDavid Howells 3496716ea3a7SDavid Howells /* The fragment is partially pulled by someone, 3497716ea3a7SDavid Howells * this can happen on input. Copy it and everything 3498716ea3a7SDavid Howells * after it. */ 3499716ea3a7SDavid Howells 3500716ea3a7SDavid Howells if (skb_shared(skb1)) 3501716ea3a7SDavid Howells copyflag = 1; 3502716ea3a7SDavid Howells 3503716ea3a7SDavid Howells /* If the skb is the last, worry about trailer. */ 3504716ea3a7SDavid Howells 3505716ea3a7SDavid Howells if (skb1->next == NULL && tailbits) { 3506716ea3a7SDavid Howells if (skb_shinfo(skb1)->nr_frags || 350721dc3301SDavid S. Miller skb_has_frag_list(skb1) || 3508716ea3a7SDavid Howells skb_tailroom(skb1) < tailbits) 3509716ea3a7SDavid Howells ntail = tailbits + 128; 3510716ea3a7SDavid Howells } 3511716ea3a7SDavid Howells 3512716ea3a7SDavid Howells if (copyflag || 3513716ea3a7SDavid Howells skb_cloned(skb1) || 3514716ea3a7SDavid Howells ntail || 3515716ea3a7SDavid Howells skb_shinfo(skb1)->nr_frags || 351621dc3301SDavid S. Miller skb_has_frag_list(skb1)) { 3517716ea3a7SDavid Howells struct sk_buff *skb2; 3518716ea3a7SDavid Howells 3519716ea3a7SDavid Howells /* Fuck, we are miserable poor guys... */ 3520716ea3a7SDavid Howells if (ntail == 0) 3521716ea3a7SDavid Howells skb2 = skb_copy(skb1, GFP_ATOMIC); 3522716ea3a7SDavid Howells else 3523716ea3a7SDavid Howells skb2 = skb_copy_expand(skb1, 3524716ea3a7SDavid Howells skb_headroom(skb1), 3525716ea3a7SDavid Howells ntail, 3526716ea3a7SDavid Howells GFP_ATOMIC); 3527716ea3a7SDavid Howells if (unlikely(skb2 == NULL)) 3528716ea3a7SDavid Howells return -ENOMEM; 3529716ea3a7SDavid Howells 3530716ea3a7SDavid Howells if (skb1->sk) 3531716ea3a7SDavid Howells skb_set_owner_w(skb2, skb1->sk); 3532716ea3a7SDavid Howells 3533716ea3a7SDavid Howells /* Looking around. Are we still alive? 3534716ea3a7SDavid Howells * OK, link new skb, drop old one */ 3535716ea3a7SDavid Howells 3536716ea3a7SDavid Howells skb2->next = skb1->next; 3537716ea3a7SDavid Howells *skb_p = skb2; 3538716ea3a7SDavid Howells kfree_skb(skb1); 3539716ea3a7SDavid Howells skb1 = skb2; 3540716ea3a7SDavid Howells } 3541716ea3a7SDavid Howells elt++; 3542716ea3a7SDavid Howells *trailer = skb1; 3543716ea3a7SDavid Howells skb_p = &skb1->next; 3544716ea3a7SDavid Howells } 3545716ea3a7SDavid Howells 3546716ea3a7SDavid Howells return elt; 3547716ea3a7SDavid Howells } 3548b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_cow_data); 3549716ea3a7SDavid Howells 3550b1faf566SEric Dumazet static void sock_rmem_free(struct sk_buff *skb) 3551b1faf566SEric Dumazet { 3552b1faf566SEric Dumazet struct sock *sk = skb->sk; 3553b1faf566SEric Dumazet 3554b1faf566SEric Dumazet atomic_sub(skb->truesize, &sk->sk_rmem_alloc); 3555b1faf566SEric Dumazet } 3556b1faf566SEric Dumazet 3557b1faf566SEric Dumazet /* 3558b1faf566SEric Dumazet * Note: We dont mem charge error packets (no sk_forward_alloc changes) 3559b1faf566SEric Dumazet */ 3560b1faf566SEric Dumazet int sock_queue_err_skb(struct sock *sk, struct sk_buff *skb) 3561b1faf566SEric Dumazet { 3562b1faf566SEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >= 356395c96174SEric Dumazet (unsigned int)sk->sk_rcvbuf) 3564b1faf566SEric Dumazet return -ENOMEM; 3565b1faf566SEric Dumazet 3566b1faf566SEric Dumazet skb_orphan(skb); 3567b1faf566SEric Dumazet skb->sk = sk; 3568b1faf566SEric Dumazet skb->destructor = sock_rmem_free; 3569b1faf566SEric Dumazet atomic_add(skb->truesize, &sk->sk_rmem_alloc); 3570b1faf566SEric Dumazet 3571abb57ea4SEric Dumazet /* before exiting rcu section, make sure dst is refcounted */ 3572abb57ea4SEric Dumazet skb_dst_force(skb); 3573abb57ea4SEric Dumazet 3574b1faf566SEric Dumazet skb_queue_tail(&sk->sk_error_queue, skb); 3575b1faf566SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 3576676d2369SDavid S. Miller sk->sk_data_ready(sk); 3577b1faf566SEric Dumazet return 0; 3578b1faf566SEric Dumazet } 3579b1faf566SEric Dumazet EXPORT_SYMBOL(sock_queue_err_skb); 3580b1faf566SEric Dumazet 3581364a9e93SWillem de Bruijn struct sk_buff *sock_dequeue_err_skb(struct sock *sk) 3582364a9e93SWillem de Bruijn { 3583364a9e93SWillem de Bruijn struct sk_buff_head *q = &sk->sk_error_queue; 3584364a9e93SWillem de Bruijn struct sk_buff *skb, *skb_next; 3585997d5c3fSEric Dumazet unsigned long flags; 3586364a9e93SWillem de Bruijn int err = 0; 3587364a9e93SWillem de Bruijn 3588997d5c3fSEric Dumazet spin_lock_irqsave(&q->lock, flags); 3589364a9e93SWillem de Bruijn skb = __skb_dequeue(q); 3590364a9e93SWillem de Bruijn if (skb && (skb_next = skb_peek(q))) 3591364a9e93SWillem de Bruijn err = SKB_EXT_ERR(skb_next)->ee.ee_errno; 3592997d5c3fSEric Dumazet spin_unlock_irqrestore(&q->lock, flags); 3593364a9e93SWillem de Bruijn 3594364a9e93SWillem de Bruijn sk->sk_err = err; 3595364a9e93SWillem de Bruijn if (err) 3596364a9e93SWillem de Bruijn sk->sk_error_report(sk); 3597364a9e93SWillem de Bruijn 3598364a9e93SWillem de Bruijn return skb; 3599364a9e93SWillem de Bruijn } 3600364a9e93SWillem de Bruijn EXPORT_SYMBOL(sock_dequeue_err_skb); 3601364a9e93SWillem de Bruijn 3602cab41c47SAlexander Duyck /** 3603cab41c47SAlexander Duyck * skb_clone_sk - create clone of skb, and take reference to socket 3604cab41c47SAlexander Duyck * @skb: the skb to clone 3605cab41c47SAlexander Duyck * 3606cab41c47SAlexander Duyck * This function creates a clone of a buffer that holds a reference on 3607cab41c47SAlexander Duyck * sk_refcnt. Buffers created via this function are meant to be 3608cab41c47SAlexander Duyck * returned using sock_queue_err_skb, or free via kfree_skb. 3609cab41c47SAlexander Duyck * 3610cab41c47SAlexander Duyck * When passing buffers allocated with this function to sock_queue_err_skb 3611cab41c47SAlexander Duyck * it is necessary to wrap the call with sock_hold/sock_put in order to 3612cab41c47SAlexander Duyck * prevent the socket from being released prior to being enqueued on 3613cab41c47SAlexander Duyck * the sk_error_queue. 3614cab41c47SAlexander Duyck */ 361562bccb8cSAlexander Duyck struct sk_buff *skb_clone_sk(struct sk_buff *skb) 361662bccb8cSAlexander Duyck { 361762bccb8cSAlexander Duyck struct sock *sk = skb->sk; 361862bccb8cSAlexander Duyck struct sk_buff *clone; 361962bccb8cSAlexander Duyck 362062bccb8cSAlexander Duyck if (!sk || !atomic_inc_not_zero(&sk->sk_refcnt)) 362162bccb8cSAlexander Duyck return NULL; 362262bccb8cSAlexander Duyck 362362bccb8cSAlexander Duyck clone = skb_clone(skb, GFP_ATOMIC); 362462bccb8cSAlexander Duyck if (!clone) { 362562bccb8cSAlexander Duyck sock_put(sk); 362662bccb8cSAlexander Duyck return NULL; 362762bccb8cSAlexander Duyck } 362862bccb8cSAlexander Duyck 362962bccb8cSAlexander Duyck clone->sk = sk; 363062bccb8cSAlexander Duyck clone->destructor = sock_efree; 363162bccb8cSAlexander Duyck 363262bccb8cSAlexander Duyck return clone; 363362bccb8cSAlexander Duyck } 363462bccb8cSAlexander Duyck EXPORT_SYMBOL(skb_clone_sk); 363562bccb8cSAlexander Duyck 363637846ef0SAlexander Duyck static void __skb_complete_tx_timestamp(struct sk_buff *skb, 363737846ef0SAlexander Duyck struct sock *sk, 363837846ef0SAlexander Duyck int tstype) 3639ac45f602SPatrick Ohly { 3640ac45f602SPatrick Ohly struct sock_exterr_skb *serr; 3641ac45f602SPatrick Ohly int err; 3642ac45f602SPatrick Ohly 3643ac45f602SPatrick Ohly serr = SKB_EXT_ERR(skb); 3644ac45f602SPatrick Ohly memset(serr, 0, sizeof(*serr)); 3645ac45f602SPatrick Ohly serr->ee.ee_errno = ENOMSG; 3646ac45f602SPatrick Ohly serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING; 3647e7fd2885SWillem de Bruijn serr->ee.ee_info = tstype; 36484ed2d765SWillem de Bruijn if (sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID) { 364909c2d251SWillem de Bruijn serr->ee.ee_data = skb_shinfo(skb)->tskey; 3650ac5cc977SWANG Cong if (sk->sk_protocol == IPPROTO_TCP && 3651ac5cc977SWANG Cong sk->sk_type == SOCK_STREAM) 36524ed2d765SWillem de Bruijn serr->ee.ee_data -= sk->sk_tskey; 36534ed2d765SWillem de Bruijn } 365429030374SEric Dumazet 3655ac45f602SPatrick Ohly err = sock_queue_err_skb(sk, skb); 365629030374SEric Dumazet 3657ac45f602SPatrick Ohly if (err) 3658ac45f602SPatrick Ohly kfree_skb(skb); 3659ac45f602SPatrick Ohly } 366037846ef0SAlexander Duyck 3661b245be1fSWillem de Bruijn static bool skb_may_tx_timestamp(struct sock *sk, bool tsonly) 3662b245be1fSWillem de Bruijn { 3663b245be1fSWillem de Bruijn bool ret; 3664b245be1fSWillem de Bruijn 3665b245be1fSWillem de Bruijn if (likely(sysctl_tstamp_allow_data || tsonly)) 3666b245be1fSWillem de Bruijn return true; 3667b245be1fSWillem de Bruijn 3668b245be1fSWillem de Bruijn read_lock_bh(&sk->sk_callback_lock); 3669b245be1fSWillem de Bruijn ret = sk->sk_socket && sk->sk_socket->file && 3670b245be1fSWillem de Bruijn file_ns_capable(sk->sk_socket->file, &init_user_ns, CAP_NET_RAW); 3671b245be1fSWillem de Bruijn read_unlock_bh(&sk->sk_callback_lock); 3672b245be1fSWillem de Bruijn return ret; 3673b245be1fSWillem de Bruijn } 3674b245be1fSWillem de Bruijn 367537846ef0SAlexander Duyck void skb_complete_tx_timestamp(struct sk_buff *skb, 367637846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps) 367737846ef0SAlexander Duyck { 367837846ef0SAlexander Duyck struct sock *sk = skb->sk; 367937846ef0SAlexander Duyck 3680b245be1fSWillem de Bruijn if (!skb_may_tx_timestamp(sk, false)) 3681b245be1fSWillem de Bruijn return; 3682b245be1fSWillem de Bruijn 368362bccb8cSAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 368462bccb8cSAlexander Duyck sock_hold(sk); 368537846ef0SAlexander Duyck 368637846ef0SAlexander Duyck *skb_hwtstamps(skb) = *hwtstamps; 368737846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, SCM_TSTAMP_SND); 368837846ef0SAlexander Duyck 368937846ef0SAlexander Duyck sock_put(sk); 369037846ef0SAlexander Duyck } 369137846ef0SAlexander Duyck EXPORT_SYMBOL_GPL(skb_complete_tx_timestamp); 369237846ef0SAlexander Duyck 369337846ef0SAlexander Duyck void __skb_tstamp_tx(struct sk_buff *orig_skb, 369437846ef0SAlexander Duyck struct skb_shared_hwtstamps *hwtstamps, 369537846ef0SAlexander Duyck struct sock *sk, int tstype) 369637846ef0SAlexander Duyck { 369737846ef0SAlexander Duyck struct sk_buff *skb; 36983a8dd971SWillem de Bruijn bool tsonly; 369937846ef0SAlexander Duyck 37003a8dd971SWillem de Bruijn if (!sk) 37013a8dd971SWillem de Bruijn return; 37023a8dd971SWillem de Bruijn 37033a8dd971SWillem de Bruijn tsonly = sk->sk_tsflags & SOF_TIMESTAMPING_OPT_TSONLY; 37043a8dd971SWillem de Bruijn if (!skb_may_tx_timestamp(sk, tsonly)) 370537846ef0SAlexander Duyck return; 370637846ef0SAlexander Duyck 370749ca0d8bSWillem de Bruijn if (tsonly) 370849ca0d8bSWillem de Bruijn skb = alloc_skb(0, GFP_ATOMIC); 370937846ef0SAlexander Duyck else 371037846ef0SAlexander Duyck skb = skb_clone(orig_skb, GFP_ATOMIC); 371137846ef0SAlexander Duyck if (!skb) 371237846ef0SAlexander Duyck return; 371337846ef0SAlexander Duyck 371449ca0d8bSWillem de Bruijn if (tsonly) { 371549ca0d8bSWillem de Bruijn skb_shinfo(skb)->tx_flags = skb_shinfo(orig_skb)->tx_flags; 371649ca0d8bSWillem de Bruijn skb_shinfo(skb)->tskey = skb_shinfo(orig_skb)->tskey; 371749ca0d8bSWillem de Bruijn } 371849ca0d8bSWillem de Bruijn 371949ca0d8bSWillem de Bruijn if (hwtstamps) 372049ca0d8bSWillem de Bruijn *skb_hwtstamps(skb) = *hwtstamps; 372149ca0d8bSWillem de Bruijn else 372249ca0d8bSWillem de Bruijn skb->tstamp = ktime_get_real(); 372349ca0d8bSWillem de Bruijn 372437846ef0SAlexander Duyck __skb_complete_tx_timestamp(skb, sk, tstype); 372537846ef0SAlexander Duyck } 3726e7fd2885SWillem de Bruijn EXPORT_SYMBOL_GPL(__skb_tstamp_tx); 3727e7fd2885SWillem de Bruijn 3728e7fd2885SWillem de Bruijn void skb_tstamp_tx(struct sk_buff *orig_skb, 3729e7fd2885SWillem de Bruijn struct skb_shared_hwtstamps *hwtstamps) 3730e7fd2885SWillem de Bruijn { 3731e7fd2885SWillem de Bruijn return __skb_tstamp_tx(orig_skb, hwtstamps, orig_skb->sk, 3732e7fd2885SWillem de Bruijn SCM_TSTAMP_SND); 3733e7fd2885SWillem de Bruijn } 3734ac45f602SPatrick Ohly EXPORT_SYMBOL_GPL(skb_tstamp_tx); 3735ac45f602SPatrick Ohly 37366e3e939fSJohannes Berg void skb_complete_wifi_ack(struct sk_buff *skb, bool acked) 37376e3e939fSJohannes Berg { 37386e3e939fSJohannes Berg struct sock *sk = skb->sk; 37396e3e939fSJohannes Berg struct sock_exterr_skb *serr; 37406e3e939fSJohannes Berg int err; 37416e3e939fSJohannes Berg 37426e3e939fSJohannes Berg skb->wifi_acked_valid = 1; 37436e3e939fSJohannes Berg skb->wifi_acked = acked; 37446e3e939fSJohannes Berg 37456e3e939fSJohannes Berg serr = SKB_EXT_ERR(skb); 37466e3e939fSJohannes Berg memset(serr, 0, sizeof(*serr)); 37476e3e939fSJohannes Berg serr->ee.ee_errno = ENOMSG; 37486e3e939fSJohannes Berg serr->ee.ee_origin = SO_EE_ORIGIN_TXSTATUS; 37496e3e939fSJohannes Berg 3750bf7fa551SAlexander Duyck /* take a reference to prevent skb_orphan() from freeing the socket */ 3751bf7fa551SAlexander Duyck sock_hold(sk); 3752bf7fa551SAlexander Duyck 37536e3e939fSJohannes Berg err = sock_queue_err_skb(sk, skb); 37546e3e939fSJohannes Berg if (err) 37556e3e939fSJohannes Berg kfree_skb(skb); 3756bf7fa551SAlexander Duyck 3757bf7fa551SAlexander Duyck sock_put(sk); 37586e3e939fSJohannes Berg } 37596e3e939fSJohannes Berg EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 37606e3e939fSJohannes Berg 3761f35d9d8aSRusty Russell /** 3762f35d9d8aSRusty Russell * skb_partial_csum_set - set up and verify partial csum values for packet 3763f35d9d8aSRusty Russell * @skb: the skb to set 3764f35d9d8aSRusty Russell * @start: the number of bytes after skb->data to start checksumming. 3765f35d9d8aSRusty Russell * @off: the offset from start to place the checksum. 3766f35d9d8aSRusty Russell * 3767f35d9d8aSRusty Russell * For untrusted partially-checksummed packets, we need to make sure the values 3768f35d9d8aSRusty Russell * for skb->csum_start and skb->csum_offset are valid so we don't oops. 3769f35d9d8aSRusty Russell * 3770f35d9d8aSRusty Russell * This function checks and sets those values and skb->ip_summed: if this 3771f35d9d8aSRusty Russell * returns false you should drop the packet. 3772f35d9d8aSRusty Russell */ 3773f35d9d8aSRusty Russell bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off) 3774f35d9d8aSRusty Russell { 37755ff8dda3SHerbert Xu if (unlikely(start > skb_headlen(skb)) || 37765ff8dda3SHerbert Xu unlikely((int)start + off > skb_headlen(skb) - 2)) { 3777e87cc472SJoe Perches net_warn_ratelimited("bad partial csum: csum=%u/%u len=%u\n", 37785ff8dda3SHerbert Xu start, off, skb_headlen(skb)); 3779f35d9d8aSRusty Russell return false; 3780f35d9d8aSRusty Russell } 3781f35d9d8aSRusty Russell skb->ip_summed = CHECKSUM_PARTIAL; 3782f35d9d8aSRusty Russell skb->csum_start = skb_headroom(skb) + start; 3783f35d9d8aSRusty Russell skb->csum_offset = off; 3784e5d5decaSJason Wang skb_set_transport_header(skb, start); 3785f35d9d8aSRusty Russell return true; 3786f35d9d8aSRusty Russell } 3787b4ac530fSDavid S. Miller EXPORT_SYMBOL_GPL(skb_partial_csum_set); 3788f35d9d8aSRusty Russell 3789ed1f50c3SPaul Durrant static int skb_maybe_pull_tail(struct sk_buff *skb, unsigned int len, 3790ed1f50c3SPaul Durrant unsigned int max) 3791ed1f50c3SPaul Durrant { 3792ed1f50c3SPaul Durrant if (skb_headlen(skb) >= len) 3793ed1f50c3SPaul Durrant return 0; 3794ed1f50c3SPaul Durrant 3795ed1f50c3SPaul Durrant /* If we need to pullup then pullup to the max, so we 3796ed1f50c3SPaul Durrant * won't need to do it again. 3797ed1f50c3SPaul Durrant */ 3798ed1f50c3SPaul Durrant if (max > skb->len) 3799ed1f50c3SPaul Durrant max = skb->len; 3800ed1f50c3SPaul Durrant 3801ed1f50c3SPaul Durrant if (__pskb_pull_tail(skb, max - skb_headlen(skb)) == NULL) 3802ed1f50c3SPaul Durrant return -ENOMEM; 3803ed1f50c3SPaul Durrant 3804ed1f50c3SPaul Durrant if (skb_headlen(skb) < len) 3805ed1f50c3SPaul Durrant return -EPROTO; 3806ed1f50c3SPaul Durrant 3807ed1f50c3SPaul Durrant return 0; 3808ed1f50c3SPaul Durrant } 3809ed1f50c3SPaul Durrant 3810f9708b43SJan Beulich #define MAX_TCP_HDR_LEN (15 * 4) 3811f9708b43SJan Beulich 3812f9708b43SJan Beulich static __sum16 *skb_checksum_setup_ip(struct sk_buff *skb, 3813f9708b43SJan Beulich typeof(IPPROTO_IP) proto, 3814f9708b43SJan Beulich unsigned int off) 3815f9708b43SJan Beulich { 3816f9708b43SJan Beulich switch (proto) { 3817f9708b43SJan Beulich int err; 3818f9708b43SJan Beulich 3819f9708b43SJan Beulich case IPPROTO_TCP: 3820f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct tcphdr), 3821f9708b43SJan Beulich off + MAX_TCP_HDR_LEN); 3822f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3823f9708b43SJan Beulich offsetof(struct tcphdr, 3824f9708b43SJan Beulich check))) 3825f9708b43SJan Beulich err = -EPROTO; 3826f9708b43SJan Beulich return err ? ERR_PTR(err) : &tcp_hdr(skb)->check; 3827f9708b43SJan Beulich 3828f9708b43SJan Beulich case IPPROTO_UDP: 3829f9708b43SJan Beulich err = skb_maybe_pull_tail(skb, off + sizeof(struct udphdr), 3830f9708b43SJan Beulich off + sizeof(struct udphdr)); 3831f9708b43SJan Beulich if (!err && !skb_partial_csum_set(skb, off, 3832f9708b43SJan Beulich offsetof(struct udphdr, 3833f9708b43SJan Beulich check))) 3834f9708b43SJan Beulich err = -EPROTO; 3835f9708b43SJan Beulich return err ? ERR_PTR(err) : &udp_hdr(skb)->check; 3836f9708b43SJan Beulich } 3837f9708b43SJan Beulich 3838f9708b43SJan Beulich return ERR_PTR(-EPROTO); 3839f9708b43SJan Beulich } 3840f9708b43SJan Beulich 3841ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3842ed1f50c3SPaul Durrant * maximally sized IP and TCP or UDP headers. 3843ed1f50c3SPaul Durrant */ 3844ed1f50c3SPaul Durrant #define MAX_IP_HDR_LEN 128 3845ed1f50c3SPaul Durrant 3846f9708b43SJan Beulich static int skb_checksum_setup_ipv4(struct sk_buff *skb, bool recalculate) 3847ed1f50c3SPaul Durrant { 3848ed1f50c3SPaul Durrant unsigned int off; 3849ed1f50c3SPaul Durrant bool fragment; 3850f9708b43SJan Beulich __sum16 *csum; 3851ed1f50c3SPaul Durrant int err; 3852ed1f50c3SPaul Durrant 3853ed1f50c3SPaul Durrant fragment = false; 3854ed1f50c3SPaul Durrant 3855ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3856ed1f50c3SPaul Durrant sizeof(struct iphdr), 3857ed1f50c3SPaul Durrant MAX_IP_HDR_LEN); 3858ed1f50c3SPaul Durrant if (err < 0) 3859ed1f50c3SPaul Durrant goto out; 3860ed1f50c3SPaul Durrant 3861ed1f50c3SPaul Durrant if (ip_hdr(skb)->frag_off & htons(IP_OFFSET | IP_MF)) 3862ed1f50c3SPaul Durrant fragment = true; 3863ed1f50c3SPaul Durrant 3864ed1f50c3SPaul Durrant off = ip_hdrlen(skb); 3865ed1f50c3SPaul Durrant 3866ed1f50c3SPaul Durrant err = -EPROTO; 3867ed1f50c3SPaul Durrant 3868ed1f50c3SPaul Durrant if (fragment) 3869ed1f50c3SPaul Durrant goto out; 3870ed1f50c3SPaul Durrant 3871f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, ip_hdr(skb)->protocol, off); 3872f9708b43SJan Beulich if (IS_ERR(csum)) 3873f9708b43SJan Beulich return PTR_ERR(csum); 3874ed1f50c3SPaul Durrant 3875ed1f50c3SPaul Durrant if (recalculate) 3876f9708b43SJan Beulich *csum = ~csum_tcpudp_magic(ip_hdr(skb)->saddr, 3877ed1f50c3SPaul Durrant ip_hdr(skb)->daddr, 3878ed1f50c3SPaul Durrant skb->len - off, 3879f9708b43SJan Beulich ip_hdr(skb)->protocol, 0); 3880ed1f50c3SPaul Durrant err = 0; 3881ed1f50c3SPaul Durrant 3882ed1f50c3SPaul Durrant out: 3883ed1f50c3SPaul Durrant return err; 3884ed1f50c3SPaul Durrant } 3885ed1f50c3SPaul Durrant 3886ed1f50c3SPaul Durrant /* This value should be large enough to cover a tagged ethernet header plus 3887ed1f50c3SPaul Durrant * an IPv6 header, all options, and a maximal TCP or UDP header. 3888ed1f50c3SPaul Durrant */ 3889ed1f50c3SPaul Durrant #define MAX_IPV6_HDR_LEN 256 3890ed1f50c3SPaul Durrant 3891ed1f50c3SPaul Durrant #define OPT_HDR(type, skb, off) \ 3892ed1f50c3SPaul Durrant (type *)(skb_network_header(skb) + (off)) 3893ed1f50c3SPaul Durrant 3894ed1f50c3SPaul Durrant static int skb_checksum_setup_ipv6(struct sk_buff *skb, bool recalculate) 3895ed1f50c3SPaul Durrant { 3896ed1f50c3SPaul Durrant int err; 3897ed1f50c3SPaul Durrant u8 nexthdr; 3898ed1f50c3SPaul Durrant unsigned int off; 3899ed1f50c3SPaul Durrant unsigned int len; 3900ed1f50c3SPaul Durrant bool fragment; 3901ed1f50c3SPaul Durrant bool done; 3902f9708b43SJan Beulich __sum16 *csum; 3903ed1f50c3SPaul Durrant 3904ed1f50c3SPaul Durrant fragment = false; 3905ed1f50c3SPaul Durrant done = false; 3906ed1f50c3SPaul Durrant 3907ed1f50c3SPaul Durrant off = sizeof(struct ipv6hdr); 3908ed1f50c3SPaul Durrant 3909ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, off, MAX_IPV6_HDR_LEN); 3910ed1f50c3SPaul Durrant if (err < 0) 3911ed1f50c3SPaul Durrant goto out; 3912ed1f50c3SPaul Durrant 3913ed1f50c3SPaul Durrant nexthdr = ipv6_hdr(skb)->nexthdr; 3914ed1f50c3SPaul Durrant 3915ed1f50c3SPaul Durrant len = sizeof(struct ipv6hdr) + ntohs(ipv6_hdr(skb)->payload_len); 3916ed1f50c3SPaul Durrant while (off <= len && !done) { 3917ed1f50c3SPaul Durrant switch (nexthdr) { 3918ed1f50c3SPaul Durrant case IPPROTO_DSTOPTS: 3919ed1f50c3SPaul Durrant case IPPROTO_HOPOPTS: 3920ed1f50c3SPaul Durrant case IPPROTO_ROUTING: { 3921ed1f50c3SPaul Durrant struct ipv6_opt_hdr *hp; 3922ed1f50c3SPaul Durrant 3923ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3924ed1f50c3SPaul Durrant off + 3925ed1f50c3SPaul Durrant sizeof(struct ipv6_opt_hdr), 3926ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3927ed1f50c3SPaul Durrant if (err < 0) 3928ed1f50c3SPaul Durrant goto out; 3929ed1f50c3SPaul Durrant 3930ed1f50c3SPaul Durrant hp = OPT_HDR(struct ipv6_opt_hdr, skb, off); 3931ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3932ed1f50c3SPaul Durrant off += ipv6_optlen(hp); 3933ed1f50c3SPaul Durrant break; 3934ed1f50c3SPaul Durrant } 3935ed1f50c3SPaul Durrant case IPPROTO_AH: { 3936ed1f50c3SPaul Durrant struct ip_auth_hdr *hp; 3937ed1f50c3SPaul Durrant 3938ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3939ed1f50c3SPaul Durrant off + 3940ed1f50c3SPaul Durrant sizeof(struct ip_auth_hdr), 3941ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3942ed1f50c3SPaul Durrant if (err < 0) 3943ed1f50c3SPaul Durrant goto out; 3944ed1f50c3SPaul Durrant 3945ed1f50c3SPaul Durrant hp = OPT_HDR(struct ip_auth_hdr, skb, off); 3946ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3947ed1f50c3SPaul Durrant off += ipv6_authlen(hp); 3948ed1f50c3SPaul Durrant break; 3949ed1f50c3SPaul Durrant } 3950ed1f50c3SPaul Durrant case IPPROTO_FRAGMENT: { 3951ed1f50c3SPaul Durrant struct frag_hdr *hp; 3952ed1f50c3SPaul Durrant 3953ed1f50c3SPaul Durrant err = skb_maybe_pull_tail(skb, 3954ed1f50c3SPaul Durrant off + 3955ed1f50c3SPaul Durrant sizeof(struct frag_hdr), 3956ed1f50c3SPaul Durrant MAX_IPV6_HDR_LEN); 3957ed1f50c3SPaul Durrant if (err < 0) 3958ed1f50c3SPaul Durrant goto out; 3959ed1f50c3SPaul Durrant 3960ed1f50c3SPaul Durrant hp = OPT_HDR(struct frag_hdr, skb, off); 3961ed1f50c3SPaul Durrant 3962ed1f50c3SPaul Durrant if (hp->frag_off & htons(IP6_OFFSET | IP6_MF)) 3963ed1f50c3SPaul Durrant fragment = true; 3964ed1f50c3SPaul Durrant 3965ed1f50c3SPaul Durrant nexthdr = hp->nexthdr; 3966ed1f50c3SPaul Durrant off += sizeof(struct frag_hdr); 3967ed1f50c3SPaul Durrant break; 3968ed1f50c3SPaul Durrant } 3969ed1f50c3SPaul Durrant default: 3970ed1f50c3SPaul Durrant done = true; 3971ed1f50c3SPaul Durrant break; 3972ed1f50c3SPaul Durrant } 3973ed1f50c3SPaul Durrant } 3974ed1f50c3SPaul Durrant 3975ed1f50c3SPaul Durrant err = -EPROTO; 3976ed1f50c3SPaul Durrant 3977ed1f50c3SPaul Durrant if (!done || fragment) 3978ed1f50c3SPaul Durrant goto out; 3979ed1f50c3SPaul Durrant 3980f9708b43SJan Beulich csum = skb_checksum_setup_ip(skb, nexthdr, off); 3981f9708b43SJan Beulich if (IS_ERR(csum)) 3982f9708b43SJan Beulich return PTR_ERR(csum); 3983ed1f50c3SPaul Durrant 3984ed1f50c3SPaul Durrant if (recalculate) 3985f9708b43SJan Beulich *csum = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr, 3986ed1f50c3SPaul Durrant &ipv6_hdr(skb)->daddr, 3987f9708b43SJan Beulich skb->len - off, nexthdr, 0); 3988ed1f50c3SPaul Durrant err = 0; 3989ed1f50c3SPaul Durrant 3990ed1f50c3SPaul Durrant out: 3991ed1f50c3SPaul Durrant return err; 3992ed1f50c3SPaul Durrant } 3993ed1f50c3SPaul Durrant 3994ed1f50c3SPaul Durrant /** 3995ed1f50c3SPaul Durrant * skb_checksum_setup - set up partial checksum offset 3996ed1f50c3SPaul Durrant * @skb: the skb to set up 3997ed1f50c3SPaul Durrant * @recalculate: if true the pseudo-header checksum will be recalculated 3998ed1f50c3SPaul Durrant */ 3999ed1f50c3SPaul Durrant int skb_checksum_setup(struct sk_buff *skb, bool recalculate) 4000ed1f50c3SPaul Durrant { 4001ed1f50c3SPaul Durrant int err; 4002ed1f50c3SPaul Durrant 4003ed1f50c3SPaul Durrant switch (skb->protocol) { 4004ed1f50c3SPaul Durrant case htons(ETH_P_IP): 4005f9708b43SJan Beulich err = skb_checksum_setup_ipv4(skb, recalculate); 4006ed1f50c3SPaul Durrant break; 4007ed1f50c3SPaul Durrant 4008ed1f50c3SPaul Durrant case htons(ETH_P_IPV6): 4009ed1f50c3SPaul Durrant err = skb_checksum_setup_ipv6(skb, recalculate); 4010ed1f50c3SPaul Durrant break; 4011ed1f50c3SPaul Durrant 4012ed1f50c3SPaul Durrant default: 4013ed1f50c3SPaul Durrant err = -EPROTO; 4014ed1f50c3SPaul Durrant break; 4015ed1f50c3SPaul Durrant } 4016ed1f50c3SPaul Durrant 4017ed1f50c3SPaul Durrant return err; 4018ed1f50c3SPaul Durrant } 4019ed1f50c3SPaul Durrant EXPORT_SYMBOL(skb_checksum_setup); 4020ed1f50c3SPaul Durrant 40219afd85c9SLinus Lüssing /** 40229afd85c9SLinus Lüssing * skb_checksum_maybe_trim - maybe trims the given skb 40239afd85c9SLinus Lüssing * @skb: the skb to check 40249afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 40259afd85c9SLinus Lüssing * 40269afd85c9SLinus Lüssing * Checks whether the given skb has data beyond the given transport length. 40279afd85c9SLinus Lüssing * If so, returns a cloned skb trimmed to this transport length. 40289afd85c9SLinus Lüssing * Otherwise returns the provided skb. Returns NULL in error cases 40299afd85c9SLinus Lüssing * (e.g. transport_len exceeds skb length or out-of-memory). 40309afd85c9SLinus Lüssing * 4031a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4032a516993fSLinus Lüssing * differs from the provided skb. 40339afd85c9SLinus Lüssing */ 40349afd85c9SLinus Lüssing static struct sk_buff *skb_checksum_maybe_trim(struct sk_buff *skb, 40359afd85c9SLinus Lüssing unsigned int transport_len) 40369afd85c9SLinus Lüssing { 40379afd85c9SLinus Lüssing struct sk_buff *skb_chk; 40389afd85c9SLinus Lüssing unsigned int len = skb_transport_offset(skb) + transport_len; 40399afd85c9SLinus Lüssing int ret; 40409afd85c9SLinus Lüssing 4041a516993fSLinus Lüssing if (skb->len < len) 40429afd85c9SLinus Lüssing return NULL; 4043a516993fSLinus Lüssing else if (skb->len == len) 40449afd85c9SLinus Lüssing return skb; 40459afd85c9SLinus Lüssing 40469afd85c9SLinus Lüssing skb_chk = skb_clone(skb, GFP_ATOMIC); 40479afd85c9SLinus Lüssing if (!skb_chk) 40489afd85c9SLinus Lüssing return NULL; 40499afd85c9SLinus Lüssing 40509afd85c9SLinus Lüssing ret = pskb_trim_rcsum(skb_chk, len); 40519afd85c9SLinus Lüssing if (ret) { 40529afd85c9SLinus Lüssing kfree_skb(skb_chk); 40539afd85c9SLinus Lüssing return NULL; 40549afd85c9SLinus Lüssing } 40559afd85c9SLinus Lüssing 40569afd85c9SLinus Lüssing return skb_chk; 40579afd85c9SLinus Lüssing } 40589afd85c9SLinus Lüssing 40599afd85c9SLinus Lüssing /** 40609afd85c9SLinus Lüssing * skb_checksum_trimmed - validate checksum of an skb 40619afd85c9SLinus Lüssing * @skb: the skb to check 40629afd85c9SLinus Lüssing * @transport_len: the data length beyond the network header 40639afd85c9SLinus Lüssing * @skb_chkf: checksum function to use 40649afd85c9SLinus Lüssing * 40659afd85c9SLinus Lüssing * Applies the given checksum function skb_chkf to the provided skb. 40669afd85c9SLinus Lüssing * Returns a checked and maybe trimmed skb. Returns NULL on error. 40679afd85c9SLinus Lüssing * 40689afd85c9SLinus Lüssing * If the skb has data beyond the given transport length, then a 40699afd85c9SLinus Lüssing * trimmed & cloned skb is checked and returned. 40709afd85c9SLinus Lüssing * 4071a516993fSLinus Lüssing * Caller needs to set the skb transport header and free any returned skb if it 4072a516993fSLinus Lüssing * differs from the provided skb. 40739afd85c9SLinus Lüssing */ 40749afd85c9SLinus Lüssing struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb, 40759afd85c9SLinus Lüssing unsigned int transport_len, 40769afd85c9SLinus Lüssing __sum16(*skb_chkf)(struct sk_buff *skb)) 40779afd85c9SLinus Lüssing { 40789afd85c9SLinus Lüssing struct sk_buff *skb_chk; 40799afd85c9SLinus Lüssing unsigned int offset = skb_transport_offset(skb); 4080fcba67c9SLinus Lüssing __sum16 ret; 40819afd85c9SLinus Lüssing 40829afd85c9SLinus Lüssing skb_chk = skb_checksum_maybe_trim(skb, transport_len); 40839afd85c9SLinus Lüssing if (!skb_chk) 4084a516993fSLinus Lüssing goto err; 40859afd85c9SLinus Lüssing 4086a516993fSLinus Lüssing if (!pskb_may_pull(skb_chk, offset)) 4087a516993fSLinus Lüssing goto err; 40889afd85c9SLinus Lüssing 40899afd85c9SLinus Lüssing __skb_pull(skb_chk, offset); 40909afd85c9SLinus Lüssing ret = skb_chkf(skb_chk); 40919afd85c9SLinus Lüssing __skb_push(skb_chk, offset); 40929afd85c9SLinus Lüssing 4093a516993fSLinus Lüssing if (ret) 4094a516993fSLinus Lüssing goto err; 40959afd85c9SLinus Lüssing 40969afd85c9SLinus Lüssing return skb_chk; 4097a516993fSLinus Lüssing 4098a516993fSLinus Lüssing err: 4099a516993fSLinus Lüssing if (skb_chk && skb_chk != skb) 4100a516993fSLinus Lüssing kfree_skb(skb_chk); 4101a516993fSLinus Lüssing 4102a516993fSLinus Lüssing return NULL; 4103a516993fSLinus Lüssing 41049afd85c9SLinus Lüssing } 41059afd85c9SLinus Lüssing EXPORT_SYMBOL(skb_checksum_trimmed); 41069afd85c9SLinus Lüssing 41074497b076SBen Hutchings void __skb_warn_lro_forwarding(const struct sk_buff *skb) 41084497b076SBen Hutchings { 4109e87cc472SJoe Perches net_warn_ratelimited("%s: received packets cannot be forwarded while LRO is enabled\n", 4110e87cc472SJoe Perches skb->dev->name); 41114497b076SBen Hutchings } 41124497b076SBen Hutchings EXPORT_SYMBOL(__skb_warn_lro_forwarding); 4113bad43ca8SEric Dumazet 4114bad43ca8SEric Dumazet void kfree_skb_partial(struct sk_buff *skb, bool head_stolen) 4115bad43ca8SEric Dumazet { 41163d861f66SEric Dumazet if (head_stolen) { 41173d861f66SEric Dumazet skb_release_head_state(skb); 4118bad43ca8SEric Dumazet kmem_cache_free(skbuff_head_cache, skb); 41193d861f66SEric Dumazet } else { 4120bad43ca8SEric Dumazet __kfree_skb(skb); 4121bad43ca8SEric Dumazet } 41223d861f66SEric Dumazet } 4123bad43ca8SEric Dumazet EXPORT_SYMBOL(kfree_skb_partial); 4124bad43ca8SEric Dumazet 4125bad43ca8SEric Dumazet /** 4126bad43ca8SEric Dumazet * skb_try_coalesce - try to merge skb to prior one 4127bad43ca8SEric Dumazet * @to: prior buffer 4128bad43ca8SEric Dumazet * @from: buffer to add 4129bad43ca8SEric Dumazet * @fragstolen: pointer to boolean 4130c6c4b97cSRandy Dunlap * @delta_truesize: how much more was allocated than was requested 4131bad43ca8SEric Dumazet */ 4132bad43ca8SEric Dumazet bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from, 4133bad43ca8SEric Dumazet bool *fragstolen, int *delta_truesize) 4134bad43ca8SEric Dumazet { 4135bad43ca8SEric Dumazet int i, delta, len = from->len; 4136bad43ca8SEric Dumazet 4137bad43ca8SEric Dumazet *fragstolen = false; 4138bad43ca8SEric Dumazet 4139bad43ca8SEric Dumazet if (skb_cloned(to)) 4140bad43ca8SEric Dumazet return false; 4141bad43ca8SEric Dumazet 4142bad43ca8SEric Dumazet if (len <= skb_tailroom(to)) { 4143e93a0435SEric Dumazet if (len) 4144bad43ca8SEric Dumazet BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len)); 4145bad43ca8SEric Dumazet *delta_truesize = 0; 4146bad43ca8SEric Dumazet return true; 4147bad43ca8SEric Dumazet } 4148bad43ca8SEric Dumazet 4149bad43ca8SEric Dumazet if (skb_has_frag_list(to) || skb_has_frag_list(from)) 4150bad43ca8SEric Dumazet return false; 4151bad43ca8SEric Dumazet 4152bad43ca8SEric Dumazet if (skb_headlen(from) != 0) { 4153bad43ca8SEric Dumazet struct page *page; 4154bad43ca8SEric Dumazet unsigned int offset; 4155bad43ca8SEric Dumazet 4156bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4157bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags >= MAX_SKB_FRAGS) 4158bad43ca8SEric Dumazet return false; 4159bad43ca8SEric Dumazet 4160bad43ca8SEric Dumazet if (skb_head_is_locked(from)) 4161bad43ca8SEric Dumazet return false; 4162bad43ca8SEric Dumazet 4163bad43ca8SEric Dumazet delta = from->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 4164bad43ca8SEric Dumazet 4165bad43ca8SEric Dumazet page = virt_to_head_page(from->head); 4166bad43ca8SEric Dumazet offset = from->data - (unsigned char *)page_address(page); 4167bad43ca8SEric Dumazet 4168bad43ca8SEric Dumazet skb_fill_page_desc(to, skb_shinfo(to)->nr_frags, 4169bad43ca8SEric Dumazet page, offset, skb_headlen(from)); 4170bad43ca8SEric Dumazet *fragstolen = true; 4171bad43ca8SEric Dumazet } else { 4172bad43ca8SEric Dumazet if (skb_shinfo(to)->nr_frags + 4173bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags > MAX_SKB_FRAGS) 4174bad43ca8SEric Dumazet return false; 4175bad43ca8SEric Dumazet 4176f4b549a5SWeiping Pan delta = from->truesize - SKB_TRUESIZE(skb_end_offset(from)); 4177bad43ca8SEric Dumazet } 4178bad43ca8SEric Dumazet 4179bad43ca8SEric Dumazet WARN_ON_ONCE(delta < len); 4180bad43ca8SEric Dumazet 4181bad43ca8SEric Dumazet memcpy(skb_shinfo(to)->frags + skb_shinfo(to)->nr_frags, 4182bad43ca8SEric Dumazet skb_shinfo(from)->frags, 4183bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags * sizeof(skb_frag_t)); 4184bad43ca8SEric Dumazet skb_shinfo(to)->nr_frags += skb_shinfo(from)->nr_frags; 4185bad43ca8SEric Dumazet 4186bad43ca8SEric Dumazet if (!skb_cloned(from)) 4187bad43ca8SEric Dumazet skb_shinfo(from)->nr_frags = 0; 4188bad43ca8SEric Dumazet 41898ea853fdSLi RongQing /* if the skb is not cloned this does nothing 41908ea853fdSLi RongQing * since we set nr_frags to 0. 41918ea853fdSLi RongQing */ 4192bad43ca8SEric Dumazet for (i = 0; i < skb_shinfo(from)->nr_frags; i++) 4193bad43ca8SEric Dumazet skb_frag_ref(from, i); 4194bad43ca8SEric Dumazet 4195bad43ca8SEric Dumazet to->truesize += delta; 4196bad43ca8SEric Dumazet to->len += len; 4197bad43ca8SEric Dumazet to->data_len += len; 4198bad43ca8SEric Dumazet 4199bad43ca8SEric Dumazet *delta_truesize = delta; 4200bad43ca8SEric Dumazet return true; 4201bad43ca8SEric Dumazet } 4202bad43ca8SEric Dumazet EXPORT_SYMBOL(skb_try_coalesce); 4203621e84d6SNicolas Dichtel 4204621e84d6SNicolas Dichtel /** 42058b27f277SNicolas Dichtel * skb_scrub_packet - scrub an skb 4206621e84d6SNicolas Dichtel * 4207621e84d6SNicolas Dichtel * @skb: buffer to clean 42088b27f277SNicolas Dichtel * @xnet: packet is crossing netns 4209621e84d6SNicolas Dichtel * 42108b27f277SNicolas Dichtel * skb_scrub_packet can be used after encapsulating or decapsulting a packet 42118b27f277SNicolas Dichtel * into/from a tunnel. Some information have to be cleared during these 42128b27f277SNicolas Dichtel * operations. 42138b27f277SNicolas Dichtel * skb_scrub_packet can also be used to clean a skb before injecting it in 42148b27f277SNicolas Dichtel * another namespace (@xnet == true). We have to clear all information in the 42158b27f277SNicolas Dichtel * skb that could impact namespace isolation. 4216621e84d6SNicolas Dichtel */ 42178b27f277SNicolas Dichtel void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4218621e84d6SNicolas Dichtel { 4219621e84d6SNicolas Dichtel skb->tstamp.tv64 = 0; 4220621e84d6SNicolas Dichtel skb->pkt_type = PACKET_HOST; 4221621e84d6SNicolas Dichtel skb->skb_iif = 0; 422260ff7467SWANG Cong skb->ignore_df = 0; 4223621e84d6SNicolas Dichtel skb_dst_drop(skb); 4224c29390c6SEric Dumazet skb_sender_cpu_clear(skb); 4225621e84d6SNicolas Dichtel secpath_reset(skb); 4226621e84d6SNicolas Dichtel nf_reset(skb); 4227621e84d6SNicolas Dichtel nf_reset_trace(skb); 4228213dd74aSHerbert Xu 4229213dd74aSHerbert Xu if (!xnet) 4230213dd74aSHerbert Xu return; 4231213dd74aSHerbert Xu 4232213dd74aSHerbert Xu skb_orphan(skb); 4233213dd74aSHerbert Xu skb->mark = 0; 4234621e84d6SNicolas Dichtel } 4235621e84d6SNicolas Dichtel EXPORT_SYMBOL_GPL(skb_scrub_packet); 4236de960aa9SFlorian Westphal 4237de960aa9SFlorian Westphal /** 4238de960aa9SFlorian Westphal * skb_gso_transport_seglen - Return length of individual segments of a gso packet 4239de960aa9SFlorian Westphal * 4240de960aa9SFlorian Westphal * @skb: GSO skb 4241de960aa9SFlorian Westphal * 4242de960aa9SFlorian Westphal * skb_gso_transport_seglen is used to determine the real size of the 4243de960aa9SFlorian Westphal * individual segments, including Layer4 headers (TCP/UDP). 4244de960aa9SFlorian Westphal * 4245de960aa9SFlorian Westphal * The MAC/L2 or network (IP, IPv6) headers are not accounted for. 4246de960aa9SFlorian Westphal */ 4247de960aa9SFlorian Westphal unsigned int skb_gso_transport_seglen(const struct sk_buff *skb) 4248de960aa9SFlorian Westphal { 4249de960aa9SFlorian Westphal const struct skb_shared_info *shinfo = skb_shinfo(skb); 4250f993bc25SFlorian Westphal unsigned int thlen = 0; 4251f993bc25SFlorian Westphal 4252f993bc25SFlorian Westphal if (skb->encapsulation) { 4253f993bc25SFlorian Westphal thlen = skb_inner_transport_header(skb) - 4254f993bc25SFlorian Westphal skb_transport_header(skb); 4255de960aa9SFlorian Westphal 4256de960aa9SFlorian Westphal if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) 4257f993bc25SFlorian Westphal thlen += inner_tcp_hdrlen(skb); 4258f993bc25SFlorian Westphal } else if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { 4259f993bc25SFlorian Westphal thlen = tcp_hdrlen(skb); 4260f993bc25SFlorian Westphal } 42616d39d589SFlorian Westphal /* UFO sets gso_size to the size of the fragmentation 42626d39d589SFlorian Westphal * payload, i.e. the size of the L4 (UDP) header is already 42636d39d589SFlorian Westphal * accounted for. 42646d39d589SFlorian Westphal */ 4265f993bc25SFlorian Westphal return thlen + shinfo->gso_size; 4266de960aa9SFlorian Westphal } 4267de960aa9SFlorian Westphal EXPORT_SYMBOL_GPL(skb_gso_transport_seglen); 42680d5501c1SVlad Yasevich 42690d5501c1SVlad Yasevich static struct sk_buff *skb_reorder_vlan_header(struct sk_buff *skb) 42700d5501c1SVlad Yasevich { 42710d5501c1SVlad Yasevich if (skb_cow(skb, skb_headroom(skb)) < 0) { 42720d5501c1SVlad Yasevich kfree_skb(skb); 42730d5501c1SVlad Yasevich return NULL; 42740d5501c1SVlad Yasevich } 42750d5501c1SVlad Yasevich 4276f6548615SVlad Yasevich memmove(skb->data - ETH_HLEN, skb->data - skb->mac_len - VLAN_HLEN, 4277a6e18ff1SVlad Yasevich 2 * ETH_ALEN); 42780d5501c1SVlad Yasevich skb->mac_header += VLAN_HLEN; 42790d5501c1SVlad Yasevich return skb; 42800d5501c1SVlad Yasevich } 42810d5501c1SVlad Yasevich 42820d5501c1SVlad Yasevich struct sk_buff *skb_vlan_untag(struct sk_buff *skb) 42830d5501c1SVlad Yasevich { 42840d5501c1SVlad Yasevich struct vlan_hdr *vhdr; 42850d5501c1SVlad Yasevich u16 vlan_tci; 42860d5501c1SVlad Yasevich 4287df8a39deSJiri Pirko if (unlikely(skb_vlan_tag_present(skb))) { 42880d5501c1SVlad Yasevich /* vlan_tci is already set-up so leave this for another time */ 42890d5501c1SVlad Yasevich return skb; 42900d5501c1SVlad Yasevich } 42910d5501c1SVlad Yasevich 42920d5501c1SVlad Yasevich skb = skb_share_check(skb, GFP_ATOMIC); 42930d5501c1SVlad Yasevich if (unlikely(!skb)) 42940d5501c1SVlad Yasevich goto err_free; 42950d5501c1SVlad Yasevich 42960d5501c1SVlad Yasevich if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 42970d5501c1SVlad Yasevich goto err_free; 42980d5501c1SVlad Yasevich 42990d5501c1SVlad Yasevich vhdr = (struct vlan_hdr *)skb->data; 43000d5501c1SVlad Yasevich vlan_tci = ntohs(vhdr->h_vlan_TCI); 43010d5501c1SVlad Yasevich __vlan_hwaccel_put_tag(skb, skb->protocol, vlan_tci); 43020d5501c1SVlad Yasevich 43030d5501c1SVlad Yasevich skb_pull_rcsum(skb, VLAN_HLEN); 43040d5501c1SVlad Yasevich vlan_set_encap_proto(skb, vhdr); 43050d5501c1SVlad Yasevich 43060d5501c1SVlad Yasevich skb = skb_reorder_vlan_header(skb); 43070d5501c1SVlad Yasevich if (unlikely(!skb)) 43080d5501c1SVlad Yasevich goto err_free; 43090d5501c1SVlad Yasevich 43100d5501c1SVlad Yasevich skb_reset_network_header(skb); 43110d5501c1SVlad Yasevich skb_reset_transport_header(skb); 43120d5501c1SVlad Yasevich skb_reset_mac_len(skb); 43130d5501c1SVlad Yasevich 43140d5501c1SVlad Yasevich return skb; 43150d5501c1SVlad Yasevich 43160d5501c1SVlad Yasevich err_free: 43170d5501c1SVlad Yasevich kfree_skb(skb); 43180d5501c1SVlad Yasevich return NULL; 43190d5501c1SVlad Yasevich } 43200d5501c1SVlad Yasevich EXPORT_SYMBOL(skb_vlan_untag); 43212e4e4410SEric Dumazet 4322e2195121SJiri Pirko int skb_ensure_writable(struct sk_buff *skb, int write_len) 4323e2195121SJiri Pirko { 4324e2195121SJiri Pirko if (!pskb_may_pull(skb, write_len)) 4325e2195121SJiri Pirko return -ENOMEM; 4326e2195121SJiri Pirko 4327e2195121SJiri Pirko if (!skb_cloned(skb) || skb_clone_writable(skb, write_len)) 4328e2195121SJiri Pirko return 0; 4329e2195121SJiri Pirko 4330e2195121SJiri Pirko return pskb_expand_head(skb, 0, 0, GFP_ATOMIC); 4331e2195121SJiri Pirko } 4332e2195121SJiri Pirko EXPORT_SYMBOL(skb_ensure_writable); 4333e2195121SJiri Pirko 433493515d53SJiri Pirko /* remove VLAN header from packet and update csum accordingly. */ 433593515d53SJiri Pirko static int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci) 433693515d53SJiri Pirko { 433793515d53SJiri Pirko struct vlan_hdr *vhdr; 433893515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 433993515d53SJiri Pirko int err; 434093515d53SJiri Pirko 434193515d53SJiri Pirko __skb_push(skb, offset); 434293515d53SJiri Pirko err = skb_ensure_writable(skb, VLAN_ETH_HLEN); 434393515d53SJiri Pirko if (unlikely(err)) 434493515d53SJiri Pirko goto pull; 434593515d53SJiri Pirko 434693515d53SJiri Pirko skb_postpull_rcsum(skb, skb->data + (2 * ETH_ALEN), VLAN_HLEN); 434793515d53SJiri Pirko 434893515d53SJiri Pirko vhdr = (struct vlan_hdr *)(skb->data + ETH_HLEN); 434993515d53SJiri Pirko *vlan_tci = ntohs(vhdr->h_vlan_TCI); 435093515d53SJiri Pirko 435193515d53SJiri Pirko memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 435293515d53SJiri Pirko __skb_pull(skb, VLAN_HLEN); 435393515d53SJiri Pirko 435493515d53SJiri Pirko vlan_set_encap_proto(skb, vhdr); 435593515d53SJiri Pirko skb->mac_header += VLAN_HLEN; 435693515d53SJiri Pirko 435793515d53SJiri Pirko if (skb_network_offset(skb) < ETH_HLEN) 435893515d53SJiri Pirko skb_set_network_header(skb, ETH_HLEN); 435993515d53SJiri Pirko 436093515d53SJiri Pirko skb_reset_mac_len(skb); 436193515d53SJiri Pirko pull: 436293515d53SJiri Pirko __skb_pull(skb, offset); 436393515d53SJiri Pirko 436493515d53SJiri Pirko return err; 436593515d53SJiri Pirko } 436693515d53SJiri Pirko 436793515d53SJiri Pirko int skb_vlan_pop(struct sk_buff *skb) 436893515d53SJiri Pirko { 436993515d53SJiri Pirko u16 vlan_tci; 437093515d53SJiri Pirko __be16 vlan_proto; 437193515d53SJiri Pirko int err; 437293515d53SJiri Pirko 4373df8a39deSJiri Pirko if (likely(skb_vlan_tag_present(skb))) { 437493515d53SJiri Pirko skb->vlan_tci = 0; 437593515d53SJiri Pirko } else { 437693515d53SJiri Pirko if (unlikely((skb->protocol != htons(ETH_P_8021Q) && 437793515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 437893515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 437993515d53SJiri Pirko return 0; 438093515d53SJiri Pirko 438193515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 438293515d53SJiri Pirko if (err) 438393515d53SJiri Pirko return err; 438493515d53SJiri Pirko } 438593515d53SJiri Pirko /* move next vlan tag to hw accel tag */ 438693515d53SJiri Pirko if (likely((skb->protocol != htons(ETH_P_8021Q) && 438793515d53SJiri Pirko skb->protocol != htons(ETH_P_8021AD)) || 438893515d53SJiri Pirko skb->len < VLAN_ETH_HLEN)) 438993515d53SJiri Pirko return 0; 439093515d53SJiri Pirko 439193515d53SJiri Pirko vlan_proto = skb->protocol; 439293515d53SJiri Pirko err = __skb_vlan_pop(skb, &vlan_tci); 439393515d53SJiri Pirko if (unlikely(err)) 439493515d53SJiri Pirko return err; 439593515d53SJiri Pirko 439693515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 439793515d53SJiri Pirko return 0; 439893515d53SJiri Pirko } 439993515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_pop); 440093515d53SJiri Pirko 440193515d53SJiri Pirko int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci) 440293515d53SJiri Pirko { 4403df8a39deSJiri Pirko if (skb_vlan_tag_present(skb)) { 440493515d53SJiri Pirko unsigned int offset = skb->data - skb_mac_header(skb); 440593515d53SJiri Pirko int err; 440693515d53SJiri Pirko 440793515d53SJiri Pirko /* __vlan_insert_tag expect skb->data pointing to mac header. 440893515d53SJiri Pirko * So change skb->data before calling it and change back to 440993515d53SJiri Pirko * original position later 441093515d53SJiri Pirko */ 441193515d53SJiri Pirko __skb_push(skb, offset); 441293515d53SJiri Pirko err = __vlan_insert_tag(skb, skb->vlan_proto, 4413df8a39deSJiri Pirko skb_vlan_tag_get(skb)); 441493515d53SJiri Pirko if (err) 441593515d53SJiri Pirko return err; 441693515d53SJiri Pirko skb->protocol = skb->vlan_proto; 441793515d53SJiri Pirko skb->mac_len += VLAN_HLEN; 441893515d53SJiri Pirko __skb_pull(skb, offset); 441993515d53SJiri Pirko 442093515d53SJiri Pirko if (skb->ip_summed == CHECKSUM_COMPLETE) 442193515d53SJiri Pirko skb->csum = csum_add(skb->csum, csum_partial(skb->data 442293515d53SJiri Pirko + (2 * ETH_ALEN), VLAN_HLEN, 0)); 442393515d53SJiri Pirko } 442493515d53SJiri Pirko __vlan_hwaccel_put_tag(skb, vlan_proto, vlan_tci); 442593515d53SJiri Pirko return 0; 442693515d53SJiri Pirko } 442793515d53SJiri Pirko EXPORT_SYMBOL(skb_vlan_push); 442893515d53SJiri Pirko 44292e4e4410SEric Dumazet /** 44302e4e4410SEric Dumazet * alloc_skb_with_frags - allocate skb with page frags 44312e4e4410SEric Dumazet * 4432de3f0d0eSMasanari Iida * @header_len: size of linear part 4433de3f0d0eSMasanari Iida * @data_len: needed length in frags 4434de3f0d0eSMasanari Iida * @max_page_order: max page order desired. 4435de3f0d0eSMasanari Iida * @errcode: pointer to error code if any 4436de3f0d0eSMasanari Iida * @gfp_mask: allocation mask 44372e4e4410SEric Dumazet * 44382e4e4410SEric Dumazet * This can be used to allocate a paged skb, given a maximal order for frags. 44392e4e4410SEric Dumazet */ 44402e4e4410SEric Dumazet struct sk_buff *alloc_skb_with_frags(unsigned long header_len, 44412e4e4410SEric Dumazet unsigned long data_len, 44422e4e4410SEric Dumazet int max_page_order, 44432e4e4410SEric Dumazet int *errcode, 44442e4e4410SEric Dumazet gfp_t gfp_mask) 44452e4e4410SEric Dumazet { 44462e4e4410SEric Dumazet int npages = (data_len + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 44472e4e4410SEric Dumazet unsigned long chunk; 44482e4e4410SEric Dumazet struct sk_buff *skb; 44492e4e4410SEric Dumazet struct page *page; 44502e4e4410SEric Dumazet gfp_t gfp_head; 44512e4e4410SEric Dumazet int i; 44522e4e4410SEric Dumazet 44532e4e4410SEric Dumazet *errcode = -EMSGSIZE; 44542e4e4410SEric Dumazet /* Note this test could be relaxed, if we succeed to allocate 44552e4e4410SEric Dumazet * high order pages... 44562e4e4410SEric Dumazet */ 44572e4e4410SEric Dumazet if (npages > MAX_SKB_FRAGS) 44582e4e4410SEric Dumazet return NULL; 44592e4e4410SEric Dumazet 44602e4e4410SEric Dumazet gfp_head = gfp_mask; 4461d0164adcSMel Gorman if (gfp_head & __GFP_DIRECT_RECLAIM) 44622e4e4410SEric Dumazet gfp_head |= __GFP_REPEAT; 44632e4e4410SEric Dumazet 44642e4e4410SEric Dumazet *errcode = -ENOBUFS; 44652e4e4410SEric Dumazet skb = alloc_skb(header_len, gfp_head); 44662e4e4410SEric Dumazet if (!skb) 44672e4e4410SEric Dumazet return NULL; 44682e4e4410SEric Dumazet 44692e4e4410SEric Dumazet skb->truesize += npages << PAGE_SHIFT; 44702e4e4410SEric Dumazet 44712e4e4410SEric Dumazet for (i = 0; npages > 0; i++) { 44722e4e4410SEric Dumazet int order = max_page_order; 44732e4e4410SEric Dumazet 44742e4e4410SEric Dumazet while (order) { 44752e4e4410SEric Dumazet if (npages >= 1 << order) { 4476d0164adcSMel Gorman page = alloc_pages((gfp_mask & ~__GFP_DIRECT_RECLAIM) | 44772e4e4410SEric Dumazet __GFP_COMP | 44782e4e4410SEric Dumazet __GFP_NOWARN | 44792e4e4410SEric Dumazet __GFP_NORETRY, 44802e4e4410SEric Dumazet order); 44812e4e4410SEric Dumazet if (page) 44822e4e4410SEric Dumazet goto fill_page; 44832e4e4410SEric Dumazet /* Do not retry other high order allocations */ 44842e4e4410SEric Dumazet order = 1; 44852e4e4410SEric Dumazet max_page_order = 0; 44862e4e4410SEric Dumazet } 44872e4e4410SEric Dumazet order--; 44882e4e4410SEric Dumazet } 44892e4e4410SEric Dumazet page = alloc_page(gfp_mask); 44902e4e4410SEric Dumazet if (!page) 44912e4e4410SEric Dumazet goto failure; 44922e4e4410SEric Dumazet fill_page: 44932e4e4410SEric Dumazet chunk = min_t(unsigned long, data_len, 44942e4e4410SEric Dumazet PAGE_SIZE << order); 44952e4e4410SEric Dumazet skb_fill_page_desc(skb, i, page, 0, chunk); 44962e4e4410SEric Dumazet data_len -= chunk; 44972e4e4410SEric Dumazet npages -= 1 << order; 44982e4e4410SEric Dumazet } 44992e4e4410SEric Dumazet return skb; 45002e4e4410SEric Dumazet 45012e4e4410SEric Dumazet failure: 45022e4e4410SEric Dumazet kfree_skb(skb); 45032e4e4410SEric Dumazet return NULL; 45042e4e4410SEric Dumazet } 45052e4e4410SEric Dumazet EXPORT_SYMBOL(alloc_skb_with_frags); 4506